fix: 修复字节码函数调用污染调用者寄存器问题
This commit is contained in:
@@ -56,6 +56,7 @@ pub(crate) struct CompileCtx {
|
||||
pub(crate) constants: Vec<Constant>,
|
||||
next_reg: u8,
|
||||
pub(crate) max_regs: u8,
|
||||
reserved_reg_start: u8,
|
||||
symbols: SymbolTable,
|
||||
pub(crate) label_map: HashMap<Label, usize>,
|
||||
pub(crate) loop_stack: Vec<(Label, Label)>,
|
||||
@@ -78,6 +79,7 @@ impl CompileCtx {
|
||||
constants: Vec::new(),
|
||||
next_reg: 0,
|
||||
max_regs: 0,
|
||||
reserved_reg_start: 0,
|
||||
symbols: SymbolTable::new(),
|
||||
label_map: HashMap::new(),
|
||||
loop_stack: Vec::new(),
|
||||
@@ -104,7 +106,7 @@ impl CompileCtx {
|
||||
}
|
||||
|
||||
pub(crate) fn reset_regs(&mut self) {
|
||||
self.next_reg = self.builtin_reg_map.len() as u8;
|
||||
self.next_reg = (self.builtin_reg_map.len() as u8).max(self.reserved_reg_start);
|
||||
self.projected_pc = 0;
|
||||
self.label_counter = 0;
|
||||
}
|
||||
@@ -275,6 +277,7 @@ impl Compiler {
|
||||
|
||||
// Inherit parent's global scope entries so previously-declared function names
|
||||
// are visible from within the body.
|
||||
let mut inherited_reg_start = ctx.builtin_reg_map.len() as u8;
|
||||
for (name, binding) in &parent_ctx.symbols.scopes[0].bindings {
|
||||
ctx.symbols.scopes[0].bindings.insert(
|
||||
name.clone(),
|
||||
@@ -284,7 +287,9 @@ impl Compiler {
|
||||
is_const: binding.is_const,
|
||||
},
|
||||
);
|
||||
inherited_reg_start = inherited_reg_start.max(binding.reg.saturating_add(1));
|
||||
}
|
||||
ctx.reserved_reg_start = inherited_reg_start;
|
||||
|
||||
// Align next_reg with builtin count so both count and emit passes start at the
|
||||
// same register offset (params go after builtin slots).
|
||||
@@ -293,6 +298,8 @@ impl Compiler {
|
||||
// Function body scope - params and local vars
|
||||
ctx.push_scope_with_kind(ScopeKind::FunctionScope);
|
||||
|
||||
let param_base = ctx.next_reg;
|
||||
|
||||
// Register parameters as initialized.
|
||||
for name in param_names {
|
||||
let reg = ctx.alloc_reg();
|
||||
@@ -354,6 +361,7 @@ impl Compiler {
|
||||
constants: ctx.constants,
|
||||
n_registers: ctx.max_regs,
|
||||
n_args: param_names.len() as u8,
|
||||
param_base,
|
||||
builtin_reg_map: ctx.builtin_reg_map,
|
||||
sub_modules: ctx.sub_modules,
|
||||
is_arrow: false,
|
||||
@@ -410,6 +418,7 @@ impl Compiler {
|
||||
constants: ctx.constants,
|
||||
n_registers: ctx.max_regs,
|
||||
n_args: 0,
|
||||
param_base: ctx.builtin_reg_map.len() as u8,
|
||||
builtin_reg_map: ctx.builtin_reg_map,
|
||||
sub_modules: ctx.sub_modules,
|
||||
is_arrow: false,
|
||||
|
||||
@@ -19,6 +19,7 @@ pub struct CompiledModule {
|
||||
pub constants: Vec<Constant>,
|
||||
pub n_registers: u8,
|
||||
pub n_args: u8,
|
||||
pub param_base: u8,
|
||||
pub builtin_reg_map: Vec<(String, u8)>,
|
||||
pub sub_modules: Vec<CompiledModule>,
|
||||
/// True when this module is an arrow function body (D-01).
|
||||
@@ -39,6 +40,7 @@ impl CompiledModule {
|
||||
constants: Vec::new(),
|
||||
n_registers: 0,
|
||||
n_args: 0,
|
||||
param_base: 0,
|
||||
builtin_reg_map: Vec::new(),
|
||||
sub_modules: Vec::new(),
|
||||
is_arrow: false,
|
||||
@@ -61,6 +63,7 @@ impl Clone for CompiledModule {
|
||||
constants: self.constants.clone(),
|
||||
n_registers: self.n_registers,
|
||||
n_args: self.n_args,
|
||||
param_base: self.param_base,
|
||||
builtin_reg_map: self.builtin_reg_map.clone(),
|
||||
sub_modules: self.sub_modules.clone(),
|
||||
is_arrow: self.is_arrow,
|
||||
|
||||
Reference in New Issue
Block a user