refactor(emit): 寄存器号 u8→u32 全链放宽,vreg 化去 checkpoint/reg_overflow(B005)
This commit is contained in:
@@ -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(())
|
||||
|
||||
@@ -9,16 +9,16 @@ use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_parser::{ChainElement, Expression, LogicalOperator, PropertyKey};
|
||||
|
||||
impl Emitter {
|
||||
fn emit_optional_guard(&self, reg: u8, short_label: LabelId, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
fn emit_optional_guard(&self, reg: u32, short_label: LabelId, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
let dup_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(reg as u32), Operand::None));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(reg), Operand::None));
|
||||
ctx.inst(Inst::jmp_if_nullish(dup_reg, short_label));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn emit_static_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::StaticMemberExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
) -> Result<(u32, u32), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
@@ -27,16 +27,16 @@ impl Emitter {
|
||||
}
|
||||
let idx = ctx.add_constant(Constant::String(member.property.name.as_str().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 value_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(value_reg as u32), Operand::Reg(obj_reg as u32), Operand::None));
|
||||
ctx.inst(Inst::ic_get(Operand::Reg(value_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(value_reg), Operand::Reg(obj_reg), Operand::None));
|
||||
ctx.inst(Inst::ic_get(Operand::Reg(value_reg), Operand::Reg(key_reg)));
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_computed_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
) -> Result<(u32, u32), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
@@ -45,13 +45,13 @@ impl Emitter {
|
||||
}
|
||||
let key_reg = self.emit_expression(&member.expression, ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(value_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(value_reg)));
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_chain_call(
|
||||
&self, call: &oxide_parser::CallExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
let (callee_reg, this_reg) = match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.emit_static_member_get_preserve_base(member, short_label, ctx)?
|
||||
@@ -68,7 +68,7 @@ impl Emitter {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let callee_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(callee_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(callee_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg)));
|
||||
(callee_reg, obj_reg)
|
||||
}
|
||||
_ => {
|
||||
@@ -80,7 +80,7 @@ impl Emitter {
|
||||
}
|
||||
let this_idx = ctx.add_constant(Constant::Undefined);
|
||||
let this_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(this_reg as u32), this_idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(this_reg), this_idx));
|
||||
(callee_reg, this_reg)
|
||||
}
|
||||
};
|
||||
@@ -102,7 +102,7 @@ impl Emitter {
|
||||
arg_regs.push(self.emit_expression(expr, ctx)?);
|
||||
}
|
||||
}
|
||||
let first_arg_reg = if arg_regs.is_empty() { 0u8 } else { arg_regs[0] };
|
||||
let first_arg_reg = if arg_regs.is_empty() { 0u32 } else { arg_regs[0] };
|
||||
let op = match &call.callee {
|
||||
Expression::Identifier(ident) if ctx.is_builtin(ident.name.as_str()) => OpCode::CALL_NATIVE,
|
||||
_ => OpCode::CALL,
|
||||
@@ -110,30 +110,30 @@ impl Emitter {
|
||||
match op {
|
||||
OpCode::CALL => {
|
||||
ctx.inst(Inst::call(
|
||||
Operand::Reg(callee_reg as u32),
|
||||
Operand::Reg(this_reg as u32),
|
||||
Operand::Reg(first_arg_reg as u32),
|
||||
Operand::Reg(callee_reg),
|
||||
Operand::Reg(this_reg),
|
||||
Operand::Reg(first_arg_reg),
|
||||
arg_regs.len() as u8,
|
||||
));
|
||||
}
|
||||
OpCode::CALL_NATIVE => {
|
||||
ctx.inst(Inst::call_native(
|
||||
Operand::Reg(callee_reg as u32),
|
||||
Operand::Reg(this_reg as u32),
|
||||
Operand::Reg(first_arg_reg as u32),
|
||||
Operand::Reg(callee_reg),
|
||||
Operand::Reg(this_reg),
|
||||
Operand::Reg(first_arg_reg),
|
||||
arg_regs.len() as u8,
|
||||
));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
let result_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::None, Operand::None));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::None, Operand::None));
|
||||
Ok(result_reg)
|
||||
}
|
||||
|
||||
fn emit_chainable_expression(
|
||||
&self, expr: &Expression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
|
||||
@@ -152,7 +152,7 @@ impl Emitter {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(value_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(value_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg)));
|
||||
Ok(value_reg)
|
||||
}
|
||||
Expression::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
@@ -163,7 +163,7 @@ impl Emitter {
|
||||
|
||||
pub(crate) fn emit_chain_element(
|
||||
&self, element: &ChainElement, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
match element {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
|
||||
@@ -182,7 +182,7 @@ impl Emitter {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(value_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(value_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg)));
|
||||
Ok(value_reg)
|
||||
}
|
||||
ChainElement::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
@@ -191,7 +191,7 @@ impl Emitter {
|
||||
}
|
||||
|
||||
pub(crate) fn emit_logical_assign_test(
|
||||
&self, op: LogicalOperator, test_reg: u8, store_label: LabelId, end_label: LabelId, ctx: &mut CompileCtx,
|
||||
&self, op: LogicalOperator, test_reg: u32, store_label: LabelId, end_label: LabelId, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
match op {
|
||||
LogicalOperator::And => ctx.inst(Inst::jmp_if_false(test_reg, end_label)),
|
||||
@@ -204,41 +204,41 @@ impl Emitter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn emit_object_property_read(&self, src_reg: u8, key: &str, ctx: &mut CompileCtx) -> u8 {
|
||||
pub(crate) fn emit_object_property_read( &self, src_reg: u32, key: &str, ctx: &mut CompileCtx) -> u32 {
|
||||
let prop_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg as u32), Operand::Reg(src_reg as u32), Operand::None));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg), Operand::Reg(src_reg), Operand::None));
|
||||
let key_idx = ctx.add_constant(Constant::String(key.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), key_idx));
|
||||
ctx.inst(Inst::ic_get(Operand::Reg(prop_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg), key_idx));
|
||||
ctx.inst(Inst::ic_get(Operand::Reg(prop_reg), Operand::Reg(key_reg)));
|
||||
prop_reg
|
||||
}
|
||||
|
||||
fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||
match key {
|
||||
PropertyKey::Identifier(ident) => {
|
||||
let name = ident.name.as_str();
|
||||
let var_reg = ctx.lookup_or_builtin(name)?;
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(key_reg as u32), Operand::Reg(var_reg as u32), Operand::None));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(key_reg), Operand::Reg(var_reg), Operand::None));
|
||||
Ok(key_reg)
|
||||
}
|
||||
PropertyKey::StringLiteral(s) => {
|
||||
let key_idx = ctx.add_constant(Constant::String(s.value.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), key_idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg), key_idx));
|
||||
Ok(key_reg)
|
||||
}
|
||||
PropertyKey::NumericLiteral(n) => {
|
||||
let key_idx = ctx.add_constant(Constant::Number(n.value));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), key_idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg), key_idx));
|
||||
Ok(key_reg)
|
||||
}
|
||||
PropertyKey::StaticIdentifier(ident) => {
|
||||
let key_idx = ctx.add_constant(Constant::String(ident.name.as_str().to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), key_idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg), key_idx));
|
||||
Ok(key_reg)
|
||||
}
|
||||
_ => Err("computed destructuring key expression not supported".into()),
|
||||
@@ -246,18 +246,18 @@ impl Emitter {
|
||||
}
|
||||
|
||||
pub(crate) fn emit_object_property_read_key(
|
||||
&self, src_reg: u8, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, Option<String>), String> {
|
||||
&self, src_reg: u32, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<(u32, Option<String>), String> {
|
||||
if !computed {
|
||||
let key_name = self.static_property_name(key)?;
|
||||
let prop_reg = self.emit_object_property_read(src_reg, &key_name, ctx);
|
||||
return Ok((prop_reg, Some(key_name)));
|
||||
}
|
||||
let prop_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg as u32), Operand::Reg(src_reg as u32), Operand::None));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg), Operand::Reg(src_reg), Operand::None));
|
||||
let key_reg = self.emit_property_key_expression(key, ctx)?;
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(prop_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(val_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(prop_reg), Operand::Reg(key_reg), Operand::Reg(val_reg)));
|
||||
Ok((val_reg, None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ impl Emitter {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
if computed {
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, target, Operand::Reg(key_reg as u32), Operand::Reg(value_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, target, Operand::Reg(key_reg), Operand::Reg(value_reg)));
|
||||
} else {
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, target, Operand::Reg(value_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, target, Operand::Reg(value_reg), Operand::Reg(key_reg)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -33,7 +33,7 @@ impl Emitter {
|
||||
} else {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(value_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(value_reg), Operand::Reg(key_reg)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ impl Emitter {
|
||||
let name = private.name.as_str();
|
||||
let key_reg = self.emit_private_id_reg(name, ctx)?;
|
||||
let method_reg = self.emit_class_method_function(method, name, home_reg, ctx, &[])?;
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(method_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(method_reg), Operand::Reg(key_reg)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,22 +16,22 @@ impl Emitter {
|
||||
.ok_or_else(|| format!("private name #{name} is not defined"))
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||
let id = self.private_name_id(name, ctx)?;
|
||||
let idx = ctx.add_constant(Constant::Int(id as i32));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
||||
Ok(reg)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_class_key_reg(
|
||||
&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
if !computed {
|
||||
let name = self.class_property_name(key)?;
|
||||
let idx = ctx.add_constant(Constant::String(name));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
||||
return Ok(reg);
|
||||
}
|
||||
|
||||
@@ -62,10 +62,10 @@ impl Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_undefined(&self, ctx: &mut CompileCtx) -> u8 {
|
||||
pub(crate) fn emit_undefined(&self, ctx: &mut CompileCtx) -> u32 {
|
||||
let idx = ctx.add_constant(Constant::Undefined);
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
||||
reg
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user