refactor(compiler): remove expression count helpers

This commit is contained in:
爱特每羊队
2026-08-05 01:33:13 +08:00
parent b8bb208825
commit fd4172203e
6 changed files with 2 additions and 661 deletions
+2 -114
View File
@@ -1,121 +1,9 @@
use crate::compiler::{CompileCtx, Compiler, Label};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{AssignmentOperator, Expression, LogicalOperator};
use oxide_parser::AssignmentOperator;
impl Compiler {
fn count_assignment_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::AssignmentExpression(assign) = expr else {
return;
};
if let oxide_parser::AssignmentTarget::StaticMemberExpression(member) = &assign.left {
if let Some(logical_op) = assign.operator.to_logical_operator() {
self.count_static_member_logical_assignment(member, logical_op, &assign.right, ctx);
return;
}
self.count_expression(&member.object, ctx);
self.count_expression(&assign.right, ctx);
// Both plain (IC_SET_PROP) and compound (COMPOUND_MEMBER_*) emit one opcode + 3 ext
// words after the key load — no extra instruction for the compound case.
ctx.count_load_const(); // key
ctx.count_ic_set_with_ext(); // IC_SET_PROP or COMPOUND_MEMBER_* + 3 ext words
} else if let oxide_parser::AssignmentTarget::ComputedMemberExpression(member) = &assign.left {
if let Some(logical_op) = assign.operator.to_logical_operator() {
self.count_computed_member_logical_assignment(member, logical_op, &assign.right, ctx);
return;
}
self.count_expression(&member.object, ctx);
self.count_expression(&member.expression, ctx);
self.count_expression(&assign.right, ctx);
ctx.projected_pc += 1; // SET_PROP_DYNAMIC
} else if let oxide_parser::AssignmentTarget::PrivateFieldExpression(member) = &assign.left {
self.count_expression(&member.object, ctx);
self.count_expression(&assign.right, ctx);
ctx.count_load_const(); // private id
ctx.count_instr(); // SET_PRIVATE
} else if let oxide_parser::AssignmentTarget::AssignmentTargetIdentifier(ati) = &assign.left {
let name = ati.name.as_str();
// Check if target is an upvalue or captured cell
let is_upvalue = ctx.current_upvalue_captures.iter().any(|u| u.name == name);
let is_captured = ctx.scopes.symbols.lookup_is_captured(name);
if let Some(logical_op) = assign.operator.to_logical_operator() {
self.count_identifier_logical_assignment(logical_op, &assign.right, ctx);
} else {
self.count_expression(&assign.right, ctx);
if assign.operator != AssignmentOperator::Assign {
ctx.projected_pc += 1; // COMPOUND_* on var
} else if is_upvalue {
ctx.count_instr(); // STORE_UPVALUE
} else if is_captured {
ctx.count_instr(); // CELL_SET
} else {
ctx.alloc_reg();
ctx.projected_pc += 1; // STORE_VAR
}
}
} else if let oxide_parser::AssignmentTarget::ArrayAssignmentTarget(ap) = &assign.left {
self.count_expression(&assign.right, ctx);
self.count_array_assignment(ap, ctx);
} else if let oxide_parser::AssignmentTarget::ObjectAssignmentTarget(op) = &assign.left {
self.count_expression(&assign.right, ctx);
self.count_object_assignment(op, ctx);
} else {
self.count_expression(&assign.right, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
fn count_static_member_logical_assignment(
&self, member: &oxide_parser::StaticMemberExpression<'_>, logical_op: LogicalOperator, right: &Expression,
ctx: &mut CompileCtx,
) {
let id = ctx.next_label_id();
self.count_expression(&member.object, ctx);
ctx.count_load_const(); // key
ctx.count_load_var(); // current value copy
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext
self.count_logical_assign_test(logical_op, id, ctx);
self.count_expression(right, ctx);
ctx.count_ic_set_with_ext(); // IC_SET_PROP + 3 ext
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
}
fn count_computed_member_logical_assignment(
&self, member: &oxide_parser::ComputedMemberExpression<'_>, logical_op: LogicalOperator, right: &Expression,
ctx: &mut CompileCtx,
) {
let id = ctx.next_label_id();
self.count_expression(&member.object, ctx);
self.count_expression(&member.expression, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1; // GET_PROP_DYNAMIC
self.count_logical_assign_test(logical_op, id, ctx);
self.count_expression(right, ctx);
ctx.projected_pc += 1; // SET_PROP_DYNAMIC
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
}
fn count_identifier_logical_assignment(
&self, logical_op: LogicalOperator, right: &Expression, ctx: &mut CompileCtx,
) {
let id = ctx.next_label_id();
ctx.alloc_reg();
ctx.projected_pc += 1; // LOAD_VAR result <- current var
self.count_logical_assign_test(logical_op, id, ctx);
self.count_expression(right, ctx);
ctx.projected_pc += 1; // STORE_VAR
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
}
pub(crate) fn count_assignment(&self, expr: &Expression, ctx: &mut CompileCtx) {
self.count_assignment_expression(expr, ctx);
}
}
impl Compiler {}
impl Compiler {
pub(crate) fn emit_assignment_expression(
@@ -3,65 +3,6 @@ use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::Expression;
impl Compiler {
fn count_static_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::StaticMemberExpression(member) = expr else {
return;
};
if matches!(&member.object, Expression::Super(_)) {
ctx.count_load_const(); // key
ctx.count_load_var(); // this
ctx.alloc_reg(); // result
ctx.count_instr(); // SUPER_GET_PROP
return;
}
self.count_expression(&member.object, ctx);
ctx.count_load_const(); // key
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
}
fn count_computed_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ComputedMemberExpression(member) = expr else {
return;
};
self.count_expression(&member.object, ctx);
self.count_expression(&member.expression, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1; // GET_PROP_DYNAMIC
}
fn count_private_field_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::PrivateFieldExpression(member) = expr else {
return;
};
self.count_expression(&member.object, ctx);
ctx.count_private_access();
}
pub(crate) fn count_chain_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ChainExpression(chain) = expr else {
return;
};
let id = ctx.next_label_id();
self.count_chain_element(&chain.expression, true, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1; // LOAD_VAR result <- chain value
ctx.projected_pc += 1; // JMP over short-circuit writer
ctx.labels.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
ctx.projected_pc += 1; // LOAD_CONST undefined
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
}
pub(crate) fn count_member_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::StaticMemberExpression(_) => self.count_static_member_expression(expr, ctx),
Expression::ComputedMemberExpression(_) => self.count_computed_member_expression(expr, ctx),
Expression::PrivateFieldExpression(_) => self.count_private_field_expression(expr, ctx),
_ => {}
}
}
}
impl Compiler {
fn emit_static_member_expression(
&self, member: &oxide_parser::StaticMemberExpression, ctx: &mut CompileCtx,
@@ -3,211 +3,6 @@ use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{ChainElement, Expression, LogicalOperator, SimpleAssignmentTarget, UnaryOperator, UpdateOperator};
impl Compiler {
fn count_binary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::BinaryExpression(bin) = expr else {
return;
};
let checkpoint = ctx.reg_checkpoint();
self.count_expression(&bin.left, ctx);
self.count_expression(&bin.right, ctx);
ctx.projected_pc += 1; // ADD/SUB/MUL/DIV/etc.
if is_side_effect_free(&bin.left) && is_side_effect_free(&bin.right) {
ctx.restore_reg_checkpoint(checkpoint.saturating_add(1));
}
}
fn count_private_in_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::PrivateInExpression(pin) = expr else {
return;
};
self.count_expression(&pin.right, ctx);
ctx.count_private_access();
}
fn count_unary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::UnaryExpression(un) = expr else {
return;
};
match un.operator {
UnaryOperator::Delete => self.count_delete_expression(&un.argument, ctx),
_ => {
self.count_expression(&un.argument, ctx);
ctx.projected_pc += 1; // NEG/TYPEOF/VOID/NOT
}
}
}
fn count_delete_expression(&self, argument: &Expression, ctx: &mut CompileCtx) {
match argument {
Expression::Identifier(_) => {
// SyntaxError at compile time, no bytecode cost.
}
Expression::StaticMemberExpression(member) => {
self.count_expression(&member.object, ctx);
ctx.count_delete_static();
}
Expression::ComputedMemberExpression(member) => {
self.count_expression(&member.object, ctx);
self.count_expression(&member.expression, ctx);
ctx.projected_pc += 1; // DELETE_PROP_DYNAMIC
}
Expression::ChainExpression(chain) => {
self.count_delete_chain_expression(&chain.expression, ctx);
}
_ => {}
}
}
fn count_delete_chain_expression(&self, chain: &ChainElement, ctx: &mut CompileCtx) {
match chain {
ChainElement::StaticMemberExpression(member) => {
self.count_expression(&member.object, ctx);
if member.optional {
self.count_optional_guard(ctx);
}
ctx.count_delete_static();
}
ChainElement::ComputedMemberExpression(member) => {
self.count_expression(&member.object, ctx);
if member.optional {
self.count_optional_guard(ctx);
}
self.count_expression(&member.expression, ctx);
ctx.projected_pc += 1; // DELETE_PROP_DYNAMIC
}
_ => {}
}
ctx.projected_pc += 1; // JMP over nullish true writer
ctx.projected_pc += 1; // LOAD_CONST true
}
fn count_update_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::UpdateExpression(update) = expr else {
return;
};
match &update.argument {
SimpleAssignmentTarget::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let is_upvalue = ctx.current_upvalue_captures.iter().any(|u| u.name == name);
let is_captured = ctx.scopes.symbols.lookup_is_captured(name);
if is_upvalue || is_captured {
ctx.alloc_reg(); // val_reg
ctx.count_instr(); // LOAD_UPVALUE or CELL_GET
ctx.count_load_const(); // 1, alloc + instr
ctx.count_instr(); // ADD/SUB
ctx.count_instr(); // STORE_UPVALUE or CELL_SET
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
SimpleAssignmentTarget::StaticMemberExpression(member) => {
self.count_expression(&member.object, ctx);
ctx.count_load_const(); // key
ctx.alloc_reg();
ctx.count_ic_instr_with_ext(); // MEMBER_INC/MEMBER_DEC + 3 ext words
}
SimpleAssignmentTarget::ComputedMemberExpression(member) => {
self.count_expression(&member.object, ctx);
self.count_expression(&member.expression, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1;
}
_ => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
}
pub(crate) fn count_operator(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::BinaryExpression(_) => self.count_binary_expression(expr, ctx),
Expression::PrivateInExpression(_) => self.count_private_in_expression(expr, ctx),
Expression::UnaryExpression(_) => self.count_unary_expression(expr, ctx),
Expression::UpdateExpression(_) => self.count_update_expression(expr, ctx),
_ => {}
}
}
fn count_conditional_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ConditionalExpression(cond) = expr else {
return;
};
let id = ctx.next_label_id();
let else_label = Label::TernaryElse(id);
let end_label = Label::TernaryEnd(id);
self.count_expression(&cond.test, ctx);
ctx.projected_pc += 1; // JMP_IF_FALSE
self.count_expression(&cond.consequent, ctx);
ctx.alloc_reg(); // result register
ctx.projected_pc += 1; // LOAD_VAR result <- consequent
ctx.projected_pc += 1; // JMP to end
ctx.labels.label_map.insert(else_label, ctx.projected_pc);
self.count_expression(&cond.alternate, ctx);
ctx.projected_pc += 1; // LOAD_VAR result <- alternate
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
fn count_sequence_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::SequenceExpression(seq) = expr else {
return;
};
for e in &seq.expressions {
self.count_expression(e, ctx);
}
}
fn count_logical_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::LogicalExpression(log) = expr else {
return;
};
self.count_expression(&log.left, ctx);
let is_simple = is_side_effect_free(&log.left) && is_side_effect_free(&log.right);
if is_simple {
self.count_expression(&log.right, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1; // AND/OR
} else if matches!(log.operator, LogicalOperator::Coalesce) {
let id = ctx.next_label_id();
ctx.alloc_reg(); // dup register
ctx.projected_pc += 1; // LOAD_VAR (DUP)
ctx.projected_pc += 1; // JMP_IF_NULLISH
ctx.projected_pc += 1; // JMP over RHS on non-nullish
self.count_expression(&log.right, ctx);
ctx.projected_pc += 1; // LOAD_VAR (overwrite)
ctx.labels.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
} else {
let id = ctx.next_label_id();
ctx.alloc_reg(); // dup register
ctx.projected_pc += 1; // LOAD_VAR (DUP)
ctx.projected_pc += 1; // JMP_IF_FALSE or JMP_IF_TRUE
self.count_expression(&log.right, ctx);
ctx.projected_pc += 1; // LOAD_VAR (overwrite)
let skip_label = match log.operator {
LogicalOperator::And => Label::TernaryEnd(id),
LogicalOperator::Or => Label::TernaryElse(id),
LogicalOperator::Coalesce => unreachable!("coalesce handled above"),
};
ctx.labels.label_map.insert(skip_label, ctx.projected_pc);
}
}
pub(crate) fn count_conditional_chain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::ConditionalExpression(_) => self.count_conditional_expression(expr, ctx),
Expression::SequenceExpression(_) => self.count_sequence_expression(expr, ctx),
Expression::LogicalExpression(_) => self.count_logical_expression(expr, ctx),
_ => {}
}
}
}
impl Compiler {
fn emit_private_in_expression(
&self, pin: &oxide_parser::PrivateInExpression, ctx: &mut CompileCtx,