refactor(emit): 寄存器号 u8→u32 全链放宽,vreg 化去 checkpoint/reg_overflow(B005)
This commit is contained in:
@@ -8,11 +8,10 @@ use oxide_bytecode::opcode::OpCode;
|
||||
impl Emitter {
|
||||
pub(crate) fn emit_array_expression(
|
||||
&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
let arr_reg = ctx.alloc_reg();
|
||||
let n = arr.elements.len() as u16;
|
||||
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(arr_reg as u32), Operand::Imm(n), Operand::None));
|
||||
let elem_checkpoint = ctx.reg_checkpoint();
|
||||
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(arr_reg), Operand::Imm(n), Operand::None));
|
||||
for (i, elem) in arr.elements.iter().enumerate() {
|
||||
let Some(e) = elem.as_expression() else {
|
||||
return Err("spread not supported".into());
|
||||
@@ -20,9 +19,8 @@ impl Emitter {
|
||||
let val_reg = self.emit_expression(e, ctx)?;
|
||||
let idx_reg = ctx.alloc_reg();
|
||||
let idx = ctx.add_constant(Constant::Int(i as i32));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(idx_reg as u32), idx));
|
||||
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(arr_reg as u32), Operand::Reg(idx_reg as u32), Operand::Reg(val_reg as u32)));
|
||||
ctx.restore_reg_checkpoint(elem_checkpoint);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(idx_reg), idx));
|
||||
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(arr_reg), Operand::Reg(idx_reg), Operand::Reg(val_reg)));
|
||||
}
|
||||
Ok(arr_reg)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ mod array;
|
||||
mod object_literal;
|
||||
|
||||
impl Emitter {
|
||||
pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||
match expr {
|
||||
Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx),
|
||||
Expression::ArrayExpression(arr) => self.emit_array_expression(arr, ctx),
|
||||
|
||||
@@ -10,10 +10,9 @@ use oxide_parser::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind};
|
||||
impl Emitter {
|
||||
pub(crate) fn emit_object_expression(
|
||||
&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
let obj_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::NEW_OBJECT, Operand::Reg(obj_reg as u32), Operand::None, Operand::None));
|
||||
let prop_checkpoint = ctx.reg_checkpoint();
|
||||
ctx.inst(Inst::new(OpCode::NEW_OBJECT, Operand::Reg(obj_reg), Operand::None, Operand::None));
|
||||
for prop in &obj.properties {
|
||||
let ObjectPropertyKind::ObjectProperty(p) = prop else {
|
||||
return Err("spread properties not yet supported".into());
|
||||
@@ -37,25 +36,23 @@ impl Emitter {
|
||||
};
|
||||
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
|
||||
ctx.inst(Inst::define_accessor(
|
||||
Operand::Reg(obj_reg as u32),
|
||||
Operand::Reg(get_reg as u32),
|
||||
Operand::Reg(set_reg as u32),
|
||||
Operand::Reg(obj_reg),
|
||||
Operand::Reg(get_reg),
|
||||
Operand::Reg(set_reg),
|
||||
idx as u32,
|
||||
));
|
||||
ctx.restore_reg_checkpoint(prop_checkpoint);
|
||||
}
|
||||
_ => {
|
||||
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg), idx));
|
||||
let val_reg = self.emit_expression(&p.value, ctx)?;
|
||||
if matches!(&p.value, Expression::ArrowFunctionExpression(_)) {
|
||||
if let Some(sub_mod) = ctx.nested.last_mut() {
|
||||
sub_mod.function_name = Some(prop_name.to_string());
|
||||
}
|
||||
}
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.restore_reg_checkpoint(prop_checkpoint);
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user