refactor: 拆分 bytecode/cache crate 并模块化 counter

This commit is contained in:
爱特每羊队
2026-06-23 17:06:39 +08:00
parent f7db0230f7
commit dbc15f6d1f
38 changed files with 1332 additions and 1306 deletions
@@ -0,0 +1,158 @@
use super::super::*;
impl Compiler {
pub(in crate::counter) fn count_binding_pattern(
&self, pattern: &oxide_parser::BindingPattern, ctx: &mut CompileCtx,
) {
match pattern {
oxide_parser::BindingPattern::BindingIdentifier(_) => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
oxide_parser::BindingPattern::ArrayPattern(ap) => {
ctx.count_instr(); // FOR_OF_INIT
for elem in &ap.elements {
ctx.alloc_reg();
ctx.count_instr(); // FOR_OF_DONE
ctx.alloc_reg();
ctx.count_instr(); // FOR_OF_NEXT
if let Some(inner) = elem {
self.count_binding_pattern(inner, ctx);
}
}
if let Some(rest) = &ap.rest {
self.count_rest_array(ctx);
self.count_binding_pattern(&rest.argument, ctx);
}
ctx.count_instr(); // FOR_OF_CLOSE
}
oxide_parser::BindingPattern::ObjectPattern(op) => {
for prop in &op.properties {
self.count_object_property_read_key(&prop.key, prop.computed, ctx);
self.count_binding_pattern(&prop.value, ctx);
}
if let Some(rest) = &op.rest {
ctx.alloc_reg();
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
self.count_binding_pattern(&rest.argument, ctx);
}
}
oxide_parser::BindingPattern::AssignmentPattern(ap) => {
ctx.count_load_const(); // undefined
ctx.count_instr(); // STRICT_EQ
ctx.count_jump(); // JMP_IF_FALSE
self.count_expression(&ap.right, ctx);
ctx.count_instr(); // LOAD_VAR default
self.count_binding_pattern(&ap.left, ctx);
}
}
}
pub(in crate::counter) fn count_rest_array(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // rest
ctx.count_instr(); // NEW_ARRAY
ctx.count_load_const(); // idx = 0
ctx.alloc_reg(); // has
ctx.count_instr(); // FOR_OF_DONE
ctx.count_jump(); // JMP_IF_FALSE
ctx.alloc_reg(); // val
ctx.count_instr(); // FOR_OF_NEXT
ctx.count_instr(); // SET_ELEM
ctx.alloc_reg(); // inc tmp
ctx.count_instr(); // INC_PRE
ctx.count_jump(); // JMP
}
pub(in crate::counter) fn count_array_assignment(
&self, ap: &oxide_parser::ArrayAssignmentTarget, ctx: &mut CompileCtx,
) {
ctx.count_instr(); // FOR_OF_INIT
for elem in &ap.elements {
ctx.alloc_reg();
ctx.count_instr(); // FOR_OF_DONE
ctx.alloc_reg();
ctx.count_instr(); // FOR_OF_NEXT
if elem.is_some() {
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
}
if ap.rest.is_some() {
self.count_rest_array(ctx);
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
ctx.count_instr(); // FOR_OF_CLOSE
}
pub(in crate::counter) fn count_object_assignment(
&self, op: &oxide_parser::ObjectAssignmentTarget, ctx: &mut CompileCtx,
) {
for prop in &op.properties {
match prop {
oxide_parser::AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
self.count_object_property_read_static(ctx);
if let Some(default_expr) = &id.init {
ctx.count_load_const(); // undefined
ctx.count_instr(); // STRICT_EQ
ctx.count_jump(); // JMP_IF_FALSE
self.count_expression(default_expr, ctx);
ctx.count_instr(); // LOAD_VAR default
}
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
oxide_parser::AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
self.count_object_property_read_key(&prop.name, prop.computed, ctx);
self.count_assignment_maybe_default(&prop.binding, ctx);
}
}
}
if op.rest.is_some() {
ctx.alloc_reg();
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
}
pub(in crate::counter) fn count_assignment_maybe_default(
&self, target: &oxide_parser::AssignmentTargetMaybeDefault, ctx: &mut CompileCtx,
) {
match target {
oxide_parser::AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
ctx.count_load_const(); // undefined
ctx.count_instr(); // STRICT_EQ
ctx.count_jump(); // JMP_IF_FALSE
self.count_expression(&default.init, ctx);
ctx.count_instr(); // LOAD_VAR default
self.count_assign_target(&default.binding, ctx);
}
oxide_parser::AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => {
self.count_array_assignment(ap, ctx)
}
oxide_parser::AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => {
self.count_object_assignment(op, ctx)
}
oxide_parser::AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
_ => {}
}
}
pub(in crate::counter) fn count_assign_target(
&self, target: &oxide_parser::AssignmentTarget, ctx: &mut CompileCtx,
) {
match target {
oxide_parser::AssignmentTarget::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
oxide_parser::AssignmentTarget::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
oxide_parser::AssignmentTarget::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
_ => {}
}
}
}
@@ -0,0 +1,80 @@
use super::super::*;
impl Compiler {
pub(in crate::counter) fn count_class(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // ctor_reg
ctx.alloc_reg(); // proto_reg
if let Some(super_class) = &class.super_class {
self.count_expression(super_class, ctx);
}
ctx.count_instr(); // LOAD_CONST ctor
ctx.count_instr(); // NEW_OBJECT proto
if class.super_class.is_some() {
ctx.alloc_reg(); // parent prototype key
ctx.count_instr(); // LOAD_CONST
ctx.alloc_reg(); // parent prototype value
ctx.count_instr(); // GET_PROP
ctx.alloc_reg(); // __proto__ key
ctx.count_instr(); // LOAD_CONST
ctx.count_instr(); // SET_PROP proto.__proto__
ctx.count_instr(); // SET_PROP ctor.__proto__
}
ctx.count_load_const(); // constructor key
ctx.count_instr(); // SET_PROP proto.constructor = ctor
ctx.count_load_const(); // prototype key
ctx.count_instr(); // SET_PROP ctor.prototype = proto
for element in &class.body.body {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if matches!(method.kind, MethodDefinitionKind::Constructor) {
continue;
}
if matches!(method.key, oxide_parser::PropertyKey::PrivateIdentifier(_)) {
if method.r#static {
ctx.count_load_const(); // private id
ctx.count_load_const(); // method
ctx.count_instr(); // INIT_PRIVATE
}
continue;
}
ctx.count_load_const(); // key_reg
ctx.count_load_const(); // method_reg
ctx.count_instr(); // SET_HOME_OBJECT
match method.kind {
MethodDefinitionKind::Method => {
ctx.count_instr(); // SET_PROP
}
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
ctx.count_load_const(); // undefined placeholder
ctx.count_define_accessor(); // DEFINE_ACCESSOR + ext
}
MethodDefinitionKind::Constructor => {}
}
}
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if prop.r#static {
ctx.count_load_const(); // key_reg
if let Some(value) = &prop.value {
self.count_expression(value, ctx);
} else {
ctx.count_load_const();
}
ctx.count_instr(); // SET_PROP
}
}
ClassElement::StaticBlock(block) => {
for stmt in &block.body {
self.count_statement(stmt, ctx);
}
}
ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => {}
}
}
}
}
@@ -0,0 +1,173 @@
use super::super::*;
impl Compiler {
pub(in crate::counter) fn count_object_property_read_static(&self, ctx: &mut CompileCtx) {
ctx.count_load_var(); // prop/object temp
ctx.count_load_const(); // key reg
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
}
pub(in crate::counter) fn count_object_property_read_key(
&self, key: &oxide_parser::PropertyKey, computed: bool, ctx: &mut CompileCtx,
) {
if !computed {
self.count_object_property_read_static(ctx);
let _ = key;
return;
}
ctx.count_load_var(); // prop/object temp
match key {
oxide_parser::PropertyKey::Identifier(ident) => {
let name = ident.name.as_str();
let _ = ctx.lookup_or_builtin(name);
ctx.count_load_var(); // key
}
oxide_parser::PropertyKey::StringLiteral(_) => {
ctx.count_load_const(); // key
}
oxide_parser::PropertyKey::NumericLiteral(_) => {
ctx.count_load_const(); // key
}
oxide_parser::PropertyKey::StaticIdentifier(_) => {
ctx.count_load_const(); // key
}
_ => {}
}
ctx.alloc_reg(); // result reg
ctx.count_instr(); // GET_PROP_DYNAMIC
}
pub(in crate::counter) fn count_optional_guard(&self, ctx: &mut CompileCtx) {
ctx.count_load_var(); // dup
ctx.count_jump(); // JMP_IF_NULLISH
}
pub(in crate::counter) fn count_static_member_get_preserve_base(
&self, member: &oxide_parser::StaticMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
) {
self.count_chainable_expression(&member.object, short_chain, ctx);
if short_chain && member.optional {
self.count_optional_guard(ctx);
}
ctx.count_load_const(); // key
ctx.count_load_var(); // receiver copy
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
}
pub(in crate::counter) fn count_computed_member_get_preserve_base(
&self, member: &oxide_parser::ComputedMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
) {
self.count_chainable_expression(&member.object, short_chain, ctx);
if short_chain && member.optional {
self.count_optional_guard(ctx);
}
self.count_expression(&member.expression, ctx);
ctx.alloc_reg();
ctx.count_instr(); // GET_PROP_DYNAMIC
}
pub(in crate::counter) fn count_chain_call(
&self, call: &oxide_parser::CallExpression, short_chain: bool, ctx: &mut CompileCtx,
) {
match &call.callee {
Expression::StaticMemberExpression(member) => {
self.count_static_member_get_preserve_base(member, short_chain, ctx)
}
Expression::ComputedMemberExpression(member) => {
self.count_computed_member_get_preserve_base(member, short_chain, ctx);
}
Expression::PrivateFieldExpression(member) => {
self.count_chainable_expression(&member.object, short_chain, ctx);
if short_chain && member.optional {
self.count_optional_guard(ctx);
}
ctx.count_private_access();
}
_ => {
self.count_chainable_expression(&call.callee, short_chain, ctx);
if short_chain && call.optional {
self.count_optional_guard(ctx);
}
ctx.alloc_reg();
ctx.projected_pc += 1; // LOAD_CONST undefined this
}
}
if short_chain
&& call.optional
&& matches!(
&call.callee,
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
)
{
self.count_optional_guard(ctx);
}
for arg in &call.arguments {
if let Some(expr) = arg.as_expression() {
self.count_expression(expr, ctx);
}
}
ctx.count_call_instr_with_arg_ext(); // CALL + arg_count ext word
ctx.count_load_var(); // result <- regs[0]
}
pub(in crate::counter) fn count_chainable_expression(
&self, expr: &Expression, short_chain: bool, ctx: &mut CompileCtx,
) {
match expr {
Expression::StaticMemberExpression(member) => {
self.count_static_member_get_preserve_base(member, short_chain, ctx)
}
Expression::ComputedMemberExpression(member) => {
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
}
Expression::PrivateFieldExpression(member) => {
self.count_chainable_expression(&member.object, short_chain, ctx);
if short_chain && member.optional {
self.count_optional_guard(ctx);
}
ctx.count_private_access();
}
Expression::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
Expression::ChainExpression(chain) => self.count_chain_element(&chain.expression, short_chain, ctx),
_ => self.count_expression(expr, ctx),
}
}
pub(in crate::counter) fn count_chain_element(
&self, element: &ChainElement, short_chain: bool, ctx: &mut CompileCtx,
) {
match element {
ChainElement::StaticMemberExpression(member) => {
self.count_static_member_get_preserve_base(member, short_chain, ctx)
}
ChainElement::ComputedMemberExpression(member) => {
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
}
ChainElement::PrivateFieldExpression(member) => {
self.count_chainable_expression(&member.object, short_chain, ctx);
if short_chain && member.optional {
self.count_optional_guard(ctx);
}
ctx.count_private_access();
}
ChainElement::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
ChainElement::TSNonNullExpression(_) => {}
}
}
pub(in crate::counter) fn count_logical_assign_test(&self, op: LogicalOperator, id: u32, ctx: &mut CompileCtx) {
match op {
LogicalOperator::And | LogicalOperator::Or => {
ctx.projected_pc += 1;
}
LogicalOperator::Coalesce => {
ctx.projected_pc += 1; // JMP_IF_NULLISH to store body
ctx.projected_pc += 1; // JMP to end on non-nullish
ctx.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
}
}
}
}
@@ -0,0 +1,3 @@
mod binding;
mod class;
mod member;