refactor(compiler): remove iteration count helpers

This commit is contained in:
爱特每羊队
2026-08-05 01:23:39 +08:00
parent 4a2bf09f78
commit b8bb208825
8 changed files with 1 additions and 188 deletions
+1 -8
View File
@@ -1286,14 +1286,7 @@ impl Compiler {
}
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.count_iteration_domain(stmt, ctx),
_ => {}
}
let _ = (stmt, ctx);
}
pub(crate) fn count_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
@@ -7,51 +7,6 @@ use oxide_parser::{
};
impl Compiler {
pub(crate) fn count_binding_pattern(&self, pattern: &BindingPattern, ctx: &mut CompileCtx) {
match pattern {
BindingPattern::BindingIdentifier(_) => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
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
}
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);
}
}
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(crate) fn count_rest_array(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // rest
ctx.count_instr(); // NEW_ARRAY
@@ -2,17 +2,6 @@ use crate::compiler::{CompileCtx, Compiler, Label};
use oxide_parser::Statement;
impl Compiler {
pub(crate) fn count_do_while_statement(&self, stmt: &oxide_parser::DoWhileStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::DoWhileStart(id);
let end_label = Label::DoWhileEnd(id);
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
self.count_statement(&stmt.body, ctx);
self.count_expression(&stmt.test, ctx);
ctx.projected_pc += 1;
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
pub(crate) fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::DoWhileStatement(dw) = stmt else {
return Ok(None);
@@ -6,41 +6,6 @@ use oxide_bytecode::{
use oxide_parser::{BindingPattern, ForStatementInit, Statement, VariableDeclarationKind};
impl Compiler {
pub(crate) fn count_for_statement(&self, stmt: &oxide_parser::ForStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::ForStart(id);
let update_label = Label::ForUpdate(id);
let end_label = Label::ForEnd(id);
if let Some(init) = &stmt.init {
if let Some(expr) = init.as_expression() {
self.count_expression(expr, ctx);
} else if let ForStatementInit::VariableDeclaration(decl) = init {
for d in &decl.declarations {
if let Some(init_expr) = &d.init {
self.count_expression(init_expr, ctx);
self.count_binding_pattern(&d.id, ctx);
} else {
ctx.alloc_reg();
ctx.alloc_reg();
ctx.count_words(2);
}
}
}
}
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
if let Some(test) = &stmt.test {
self.count_expression(test, ctx);
ctx.projected_pc += 1;
}
self.count_statement(&stmt.body, ctx);
ctx.labels.label_map.insert(update_label, ctx.projected_pc);
if let Some(update) = &stmt.update {
self.count_expression(update, ctx);
}
ctx.projected_pc += 1;
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
pub(crate) fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForStatement(fr) = stmt else {
return Ok(None);
@@ -3,36 +3,6 @@ use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{ForStatementLeft, Statement, VariableDeclarationKind};
impl Compiler {
pub(crate) fn count_for_in_statement(&self, stmt: &oxide_parser::ForInStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::ForInStart(id);
let end_label = Label::ForInEnd(id);
self.count_expression(&stmt.right, ctx);
ctx.count_instr();
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
ctx.count_instr();
ctx.count_jump();
ctx.count_jump();
ctx.count_instr();
match &stmt.left {
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
for _d in &decl.declarations {
ctx.alloc_reg();
ctx.count_instr();
}
}
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr();
}
_ => {}
}
self.count_statement(&stmt.body, ctx);
ctx.count_jump();
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
ctx.count_instr();
}
pub(crate) fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForInStatement(fi) = stmt else {
return Ok(None);
@@ -3,42 +3,6 @@ use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{ForStatementLeft, Statement};
impl Compiler {
pub(crate) fn count_for_of_statement(&self, stmt: &oxide_parser::ForOfStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::ForOfStart(id);
let end_label = Label::ForOfEnd(id);
self.count_expression(&stmt.right, ctx);
ctx.count_instr();
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
ctx.alloc_reg();
ctx.count_instr();
ctx.count_jump();
ctx.alloc_reg();
ctx.count_instr();
match &stmt.left {
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
for d in &decl.declarations {
self.count_binding_pattern(&d.id, ctx);
}
}
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr();
}
oxide_parser::ForStatementLeft::ArrayAssignmentTarget(ap) => {
self.count_array_assignment(ap, ctx);
}
oxide_parser::ForStatementLeft::ObjectAssignmentTarget(op) => {
self.count_object_assignment(op, ctx);
}
_ => {}
}
self.count_statement(&stmt.body, ctx);
ctx.count_jump();
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
ctx.count_instr();
}
pub(crate) fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForOfStatement(fo) = stmt else {
return Ok(None);
@@ -8,17 +8,6 @@ mod for_of;
mod while_loop;
impl Compiler {
pub(crate) fn count_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::WhileStatement(wh) => self.count_while_statement(wh, ctx),
Statement::DoWhileStatement(dw) => self.count_do_while_statement(dw, ctx),
Statement::ForStatement(fr) => self.count_for_statement(fr, ctx),
Statement::ForInStatement(fi) => self.count_for_in_statement(fi, ctx),
Statement::ForOfStatement(fo) => self.count_for_of_statement(fo, ctx),
_ => {}
}
}
pub(crate) fn emit_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
match stmt {
Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx),
@@ -2,18 +2,6 @@ use crate::compiler::{CompileCtx, Compiler, Label};
use oxide_parser::Statement;
impl Compiler {
pub(crate) fn count_while_statement(&self, stmt: &oxide_parser::WhileStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::WhileStart(id);
let end_label = Label::WhileEnd(id);
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
self.count_expression(&stmt.test, ctx);
ctx.projected_pc += 1;
self.count_statement(&stmt.body, ctx);
ctx.projected_pc += 1;
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
pub(crate) fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::WhileStatement(wh) = stmt else {
return Ok(None);