refactor(emit): 寄存器号 u8→u32 全链放宽,vreg 化去 checkpoint/reg_overflow(B005)

This commit is contained in:
爱特每羊队
2026-08-06 16:31:16 +08:00
parent c52bf6313e
commit 38d6c7a60e
54 changed files with 404 additions and 431 deletions
+35 -35
View File
@@ -14,22 +14,22 @@ use oxide_parser::{
impl Emitter {
fn emit_default_if_undefined(
&self, val_reg: u8, default_expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
&self, val_reg: u32, default_expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u32, String> {
let undef_reg = self.emit_undefined(ctx);
let eq_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::STRICT_EQ, Operand::Reg(eq_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(undef_reg as u32)));
ctx.inst(Inst::new(OpCode::STRICT_EQ, Operand::Reg(eq_reg), Operand::Reg(val_reg), Operand::Reg(undef_reg)));
// val != undefined(eq 为 false)→ 跳过默认值
let end_label = ctx.next_label_id();
ctx.inst(Inst::jmp_if_false(eq_reg, end_label));
let default_reg = self.emit_expression(default_expr, ctx)?;
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(val_reg as u32), Operand::Reg(default_reg as u32), Operand::None));
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(val_reg), Operand::Reg(default_reg), Operand::None));
ctx.labels.set_label_pos(end_label, ctx.insts.len());
Ok(val_reg)
}
fn emit_bind_target(
&self, name: &str, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
&self, name: &str, src_reg: u32, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let target_reg = if matches!(kind, VariableDeclarationKind::Var) {
// `var` names are pre-declared (hoisting); reuse the pre-registered slot
@@ -47,20 +47,20 @@ impl Emitter {
var_reg
};
if let Some(&cell_idx) = ctx.captured_bindings.get(name) {
ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(src_reg as u32), Operand::Imm(cell_idx as u16), Operand::None));
ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(src_reg), Operand::Imm(cell_idx as u16), Operand::None));
} else {
// B011: const 声明路径 STORE_VAR 恒 b=0,不查运行时 guard。
// guard 读 regs[rd] 判断槽是否已初始化,依赖槽初始为 undefined;
// 声明冗余检查由编译期 declare() 完成(重复声明编译报错),
// 运行时 guard 只服务"对 const 再赋值"(赋值路径,见 emit_assign_target)。
ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg as u32), Operand::Reg(src_reg as u32), Operand::Imm(0)));
ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg), Operand::Reg(src_reg), Operand::Imm(0)));
}
ctx.init_var(name);
Ok(())
}
pub(crate) fn emit_assign_target(
&self, target: &AssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
&self, target: &AssignmentTarget, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTarget::AssignmentTargetIdentifier(id) => {
@@ -68,8 +68,8 @@ impl Emitter {
let var_reg = ctx.lookup_or_global(name);
ctx.inst(Inst::new(
OpCode::STORE_VAR,
Operand::Reg(var_reg as u32),
Operand::Reg(src_reg as u32),
Operand::Reg(var_reg),
Operand::Reg(src_reg),
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
));
Ok(())
@@ -81,7 +81,7 @@ impl Emitter {
}
pub(crate) fn emit_binding_pattern(
&self, pattern: &BindingPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
&self, pattern: &BindingPattern, src_reg: u32, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
match pattern {
@@ -98,14 +98,14 @@ impl Emitter {
}
fn emit_array_binding(
&self, ap: &ArrayPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
&self, ap: &ArrayPattern, src_reg: u32, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg as u32), Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg), Operand::None));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg), Operand::None, Operand::None));
let val_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg), Operand::None, Operand::None));
if let Some(pattern) = elem {
self.emit_binding_pattern(pattern, val_reg, kind, is_const, ctx)?;
}
@@ -118,30 +118,30 @@ impl Emitter {
Ok(())
}
fn emit_collect_rest_array(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
fn emit_collect_rest_array(&self, ctx: &mut CompileCtx) -> Result<u32, String> {
let rest_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(rest_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(rest_reg), Operand::None, Operand::None));
let idx_reg = ctx.alloc_reg();
let zero_idx = ctx.add_constant(Constant::Int(0));
ctx.inst(Inst::load_const(Operand::Reg(idx_reg as u32), zero_idx));
ctx.inst(Inst::load_const(Operand::Reg(idx_reg), zero_idx));
let loop_start = ctx.next_label_id();
let loop_end = ctx.next_label_id();
ctx.labels.set_label_pos(loop_start, ctx.insts.len());
let has_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg), Operand::None, Operand::None));
ctx.inst(Inst::jmp_if_false(has_reg, loop_end));
let val_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(rest_reg as u32), Operand::Reg(idx_reg as u32), Operand::Reg(val_reg as u32)));
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(rest_reg), Operand::Reg(idx_reg), Operand::Reg(val_reg)));
let tmp_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::INC_PRE, Operand::Reg(idx_reg as u32), Operand::Reg(tmp_reg as u32), Operand::Reg(tmp_reg as u32)));
ctx.inst(Inst::new(OpCode::INC_PRE, Operand::Reg(idx_reg), Operand::Reg(tmp_reg), Operand::Reg(tmp_reg)));
ctx.inst(Inst::jmp(loop_start));
ctx.labels.set_label_pos(loop_end, ctx.insts.len());
Ok(rest_reg)
}
fn emit_object_binding(
&self, op: &ObjectPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
&self, op: &ObjectPattern, src_reg: u32, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
@@ -154,14 +154,14 @@ impl Emitter {
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg as u32), Operand::Reg(src_reg as u32), excluded_idx as u32));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg), Operand::Reg(src_reg), excluded_idx as u32));
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
Ok(())
}
fn emit_assignment_maybe_default(
&self, target: &AssignmentTargetMaybeDefault, src_reg: u8, ctx: &mut CompileCtx,
&self, target: &AssignmentTargetMaybeDefault, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
@@ -175,8 +175,8 @@ impl Emitter {
let var_reg = ctx.lookup_or_global(name);
ctx.inst(Inst::new(
OpCode::STORE_VAR,
Operand::Reg(var_reg as u32),
Operand::Reg(src_reg as u32),
Operand::Reg(var_reg),
Operand::Reg(src_reg),
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
));
Ok(())
@@ -186,14 +186,14 @@ impl Emitter {
}
pub(crate) fn emit_array_assignment(
&self, ap: &ArrayAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
&self, ap: &ArrayAssignmentTarget, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg as u32), Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg), Operand::None));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg), Operand::None, Operand::None));
let val_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg), Operand::None, Operand::None));
if let Some(target) = elem {
self.emit_assignment_maybe_default(target, val_reg, ctx)?;
}
@@ -207,7 +207,7 @@ impl Emitter {
}
pub(crate) fn emit_object_assignment(
&self, op: &ObjectAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
&self, op: &ObjectAssignmentTarget, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
@@ -223,8 +223,8 @@ impl Emitter {
let var_reg = ctx.lookup_or_global(name);
ctx.inst(Inst::new(
OpCode::STORE_VAR,
Operand::Reg(var_reg as u32),
Operand::Reg(prop_reg as u32),
Operand::Reg(var_reg),
Operand::Reg(prop_reg),
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
));
}
@@ -241,7 +241,7 @@ impl Emitter {
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg as u32), Operand::Reg(src_reg as u32), excluded_idx as u32));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg), Operand::Reg(src_reg), excluded_idx as u32));
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
Ok(())