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
+1 -1
View File
@@ -9,7 +9,7 @@ use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Class, ClassElement, MethodDefinitionKind, PropertyKey};
impl Emitter {
pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u32, String> {
let mut constructor_method = None;
let mut instance_fields = Vec::new();
let mut private_names = Vec::<(String, u32)>::new();
+7 -4
View File
@@ -10,19 +10,22 @@ impl Emitter {
/// this.y = 1 } static z = this.y + 1` must run in that order). Emitting
/// all fields before all blocks would make `z` read `y` before it is set.
pub(crate) fn emit_class_static_elements(
&self, elements: &[ClassElement], ctor_reg: u8, ctx: &mut CompileCtx,
&self, elements: &[ClassElement], ctor_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
// static_block_this_reg 保留 u8(this 槽语义,254 特判),ctor_reg 超 u8 界时报错而非静默截断。
ctx.static_block_this_reg = Some(
u8::try_from(ctor_reg).map_err(|_| format!("class constructor register {ctor_reg} exceeds u8 limit"))?,
);
for element in elements {
match element {
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if prop.r#static {
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
self.emit_private_field_init(Operand::Reg(ctor_reg as u32), private.name.as_str(), prop.value.as_ref(), ctx)?;
self.emit_private_field_init(Operand::Reg(ctor_reg), private.name.as_str(), prop.value.as_ref(), ctx)?;
} else {
self.emit_public_field_init(Operand::Reg(ctor_reg as u32), &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
self.emit_public_field_init(Operand::Reg(ctor_reg), &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
}
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ use crate::{CompileCtx, Emitter};
impl Emitter {
pub(crate) fn emit_class_header(
&self, class: &oxide_parser::Class, ctx: &mut CompileCtx,
) -> Result<(u8, u8, Option<u8>), String> {
) -> Result<(u32, u32, Option<u32>), String> {
let ctor_reg = ctx.alloc_reg();
let proto_reg = ctx.alloc_reg();
let super_reg = if let Some(super_expr) = &class.super_class {
+12 -12
View File
@@ -10,7 +10,7 @@ use oxide_parser::{ClassElement, MethodDefinitionKind, PropertyKey};
impl Emitter {
pub(crate) fn emit_class_methods(
&self, elements: &[ClassElement], ctor_reg: u8, proto_reg: u8, self_binding: &[(&str, u8)],
&self, elements: &[ClassElement], ctor_reg: u32, proto_reg: u32, self_binding: &[(&str, u32)],
ctx: &mut CompileCtx,
) -> Result<(), String> {
for element in elements {
@@ -21,7 +21,7 @@ impl Emitter {
}
if matches!(method.key, PropertyKey::PrivateIdentifier(_)) {
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
self.emit_private_method_init(Operand::Reg(home_reg as u32), method, Operand::Reg(home_reg as u32), ctx)?;
self.emit_private_method_init(Operand::Reg(home_reg), method, Operand::Reg(home_reg), ctx)?;
continue;
}
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
@@ -32,13 +32,13 @@ impl Emitter {
self.class_property_name(&method.key)?
};
let accessor_reg =
self.emit_class_method_function(method, &method_name, Operand::Reg(home_reg as u32), ctx, self_binding)?;
self.emit_class_method_function(method, &method_name, Operand::Reg(home_reg), ctx, self_binding)?;
match method.kind {
MethodDefinitionKind::Method => {
if method.computed {
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(home_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(accessor_reg as u32)));
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(home_reg), Operand::Reg(key_reg), Operand::Reg(accessor_reg)));
} else {
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(home_reg as u32), Operand::Reg(accessor_reg as u32), Operand::Reg(key_reg as u32)));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(home_reg), Operand::Reg(accessor_reg), Operand::Reg(key_reg)));
}
}
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
@@ -54,9 +54,9 @@ impl Emitter {
let key_name = self.class_property_name(&method.key)?;
let key_idx = ctx.add_constant(Constant::String(key_name));
ctx.inst(Inst::define_accessor(
Operand::Reg(home_reg as u32),
Operand::Reg(get_reg as u32),
Operand::Reg(set_reg as u32),
Operand::Reg(home_reg),
Operand::Reg(get_reg),
Operand::Reg(set_reg),
key_idx as u32,
));
}
@@ -69,8 +69,8 @@ impl Emitter {
pub(crate) fn emit_class_method_function(
&self, method: &oxide_parser::MethodDefinition, method_name: &str, home_reg: Operand, ctx: &mut CompileCtx,
self_binding: &[(&str, u8)],
) -> Result<u8, String> {
self_binding: &[(&str, u32)],
) -> Result<u32, String> {
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
let saved_instance = ctx.in_instance_method;
let saved_static = ctx.in_static_method;
@@ -90,8 +90,8 @@ impl Emitter {
method_module.needs_home_object = true;
ctx.nested.push(method_module);
let method_reg = ctx.alloc_reg();
ctx.inst(Inst::create_closure(Operand::Reg(method_reg as u32), ctx.nested.len() as u16));
ctx.inst(Inst::new(OpCode::SET_HOME_OBJECT, Operand::Reg(method_reg as u32), home_reg, Operand::None));
ctx.inst(Inst::create_closure(Operand::Reg(method_reg), ctx.nested.len() as u16));
ctx.inst(Inst::new(OpCode::SET_HOME_OBJECT, Operand::Reg(method_reg), home_reg, Operand::None));
Ok(method_reg)
}
}
+12 -12
View File
@@ -8,30 +8,30 @@ use oxide_bytecode::opcode::OpCode;
impl Emitter {
pub(crate) fn emit_class_prototype(
&self, ctor_reg: u8, proto_reg: u8, super_reg: Option<u8>, sub_idx: u16, ctx: &mut CompileCtx,
&self, ctor_reg: u32, proto_reg: u32, super_reg: Option<u32>, sub_idx: u16, ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.inst(Inst::create_closure(Operand::Reg(ctor_reg as u32), sub_idx));
ctx.inst(Inst::new(OpCode::NEW_OBJECT, Operand::Reg(proto_reg as u32), Operand::None, Operand::None));
ctx.inst(Inst::create_closure(Operand::Reg(ctor_reg), sub_idx));
ctx.inst(Inst::new(OpCode::NEW_OBJECT, Operand::Reg(proto_reg), Operand::None, Operand::None));
if let Some(super_reg) = super_reg {
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let parent_proto_key_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(parent_proto_key_reg as u32), proto_key_idx));
ctx.inst(Inst::load_const(Operand::Reg(parent_proto_key_reg), proto_key_idx));
let parent_proto_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::GET_PROP, Operand::Reg(super_reg as u32), Operand::Reg(parent_proto_reg as u32), Operand::Reg(parent_proto_key_reg as u32)));
ctx.inst(Inst::new(OpCode::GET_PROP, Operand::Reg(super_reg), Operand::Reg(parent_proto_reg), Operand::Reg(parent_proto_key_reg)));
let proto_link_idx = ctx.add_constant(Constant::String("__proto__".to_string()));
let proto_link_key_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(proto_link_key_reg as u32), proto_link_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg as u32), Operand::Reg(parent_proto_reg as u32), Operand::Reg(proto_link_key_reg as u32)));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg as u32), Operand::Reg(super_reg as u32), Operand::Reg(proto_link_key_reg as u32)));
ctx.inst(Inst::load_const(Operand::Reg(proto_link_key_reg), proto_link_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg), Operand::Reg(parent_proto_reg), Operand::Reg(proto_link_key_reg)));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg), Operand::Reg(super_reg), Operand::Reg(proto_link_key_reg)));
}
let ctor_key_idx = ctx.add_constant(Constant::String("constructor".to_string()));
let ctor_key_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(ctor_key_reg as u32), ctor_key_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg as u32), Operand::Reg(ctor_reg as u32), Operand::Reg(ctor_key_reg as u32)));
ctx.inst(Inst::load_const(Operand::Reg(ctor_key_reg), ctor_key_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg), Operand::Reg(ctor_reg), Operand::Reg(ctor_key_reg)));
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let proto_key_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(proto_key_reg as u32), proto_key_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg as u32), Operand::Reg(proto_reg as u32), Operand::Reg(proto_key_reg as u32)));
ctx.inst(Inst::load_const(Operand::Reg(proto_key_reg), proto_key_idx));
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg), Operand::Reg(proto_reg), Operand::Reg(proto_key_reg)));
Ok(())
}
}