diff --git a/crates/oxide_bytecode/src/module.rs b/crates/oxide_bytecode/src/module.rs index 3f07eca..983cb36 100644 --- a/crates/oxide_bytecode/src/module.rs +++ b/crates/oxide_bytecode/src/module.rs @@ -28,7 +28,7 @@ pub enum Constant { #[derive(Debug, Clone)] pub struct UpvalueCapture { pub name: String, - pub enclosing_reg: u8, + pub enclosing_reg: u32, pub cell_idx: u8, } @@ -48,7 +48,7 @@ pub struct CompiledModule { pub n_registers: u8, pub n_args: u8, pub param_base: u8, - pub builtin_reg_map: Vec<(String, u8)>, + pub builtin_reg_map: Vec<(String, u32)>, pub sub_modules: Vec, /// True when this module is an arrow function body. /// Arrow functions capture lexical `this` from the enclosing scope. diff --git a/crates/oxide_emit/src/class/emit.rs b/crates/oxide_emit/src/class/emit.rs index d00f023..450dd86 100644 --- a/crates/oxide_emit/src/class/emit.rs +++ b/crates/oxide_emit/src/class/emit.rs @@ -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 { + pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result { let mut constructor_method = None; let mut instance_fields = Vec::new(); let mut private_names = Vec::<(String, u32)>::new(); diff --git a/crates/oxide_emit/src/class/field.rs b/crates/oxide_emit/src/class/field.rs index 18b6d1d..2091a61 100644 --- a/crates/oxide_emit/src/class/field.rs +++ b/crates/oxide_emit/src/class/field.rs @@ -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)?; } } } diff --git a/crates/oxide_emit/src/class/header.rs b/crates/oxide_emit/src/class/header.rs index 956a82e..c7402c4 100644 --- a/crates/oxide_emit/src/class/header.rs +++ b/crates/oxide_emit/src/class/header.rs @@ -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), String> { + ) -> Result<(u32, u32, Option), String> { let ctor_reg = ctx.alloc_reg(); let proto_reg = ctx.alloc_reg(); let super_reg = if let Some(super_expr) = &class.super_class { diff --git a/crates/oxide_emit/src/class/method.rs b/crates/oxide_emit/src/class/method.rs index 0f89d45..cdbbb0b 100644 --- a/crates/oxide_emit/src/class/method.rs +++ b/crates/oxide_emit/src/class/method.rs @@ -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 { + self_binding: &[(&str, u32)], + ) -> Result { 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) } } diff --git a/crates/oxide_emit/src/class/prototype.rs b/crates/oxide_emit/src/class/prototype.rs index 86d6acb..5020b5a 100644 --- a/crates/oxide_emit/src/class/prototype.rs +++ b/crates/oxide_emit/src/class/prototype.rs @@ -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, sub_idx: u16, ctx: &mut CompileCtx, + &self, ctor_reg: u32, proto_reg: u32, super_reg: Option, 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(()) } } diff --git a/crates/oxide_emit/src/emit.rs b/crates/oxide_emit/src/emit.rs index f88bb24..35cccd4 100644 --- a/crates/oxide_emit/src/emit.rs +++ b/crates/oxide_emit/src/emit.rs @@ -140,9 +140,9 @@ pub struct CompileCtx { pub(crate) insts: Vec, pub(crate) constants: Vec, constant_map: HashMap, - next_reg: u8, - pub(crate) max_regs: u8, - reserved_reg_start: u8, + next_reg: u32, + pub(crate) max_regs: u32, + reserved_reg_start: u32, pub(crate) labels: LabelCtx, pub(crate) scopes: ScopeCtx, pub(crate) nested: Vec, @@ -162,9 +162,6 @@ pub struct CompileCtx { /// 捕获判断(MAKE_CELL / CELL_GET / CELL_SET)与子函数 upvalue cell_idx 统一查此映射, /// 消除符号表时序依赖与 cell 索引错位。 pub(crate) captured_bindings: BTreeMap, - /// Set when alloc_reg() overflows into the reserved this/new.target range (≥254). - /// Checked after each emit phase to produce a compile error rather than silent corruption. - pub(crate) reg_overflow: bool, pub(crate) const_overflow: bool, } @@ -240,7 +237,6 @@ impl CompileCtx { current_upvalue_captures: Vec::new(), own_bindings: HashSet::new(), captured_bindings: BTreeMap::new(), - reg_overflow: false, const_overflow: false, } } @@ -249,17 +245,12 @@ impl CompileCtx { self.insts.push(inst); } - pub(crate) fn alloc_reg(&mut self) -> u8 { + pub(crate) fn alloc_reg(&mut self) -> u32 { let r = self.next_reg; - // Registers 254 (this) and 255 (new.target) are reserved by the VM. - // Allocating into them silently corrupts the call convention, turning - // method calls' `this` into garbage. Clamp to 253 and set a flag so - // the compiler can surface a proper error after the emit pass. - if r >= 254 { - self.reg_overflow = true; - return 253; // clamp to last safe register; emit continues but reg_overflow triggers error - } - self.next_reg = self.next_reg.wrapping_add(1); + // vreg 化(D-02):寄存器号无上限,RegAlloc 阶段负责压缩到物理域(≤253)。 + // 254/255 是 VM 保留的 this/new.target,vreg 世界允许虚拟号越过它们, + // 只有 RegAlloc 完成映射后 lower 的物理域检查才相关。 + self.next_reg += 1; if self.next_reg > self.max_regs { self.max_regs = self.next_reg; } @@ -271,15 +262,7 @@ impl CompileCtx { self.labels.label_counter = 0; } - pub(crate) fn reg_checkpoint(&self) -> u8 { - self.next_reg - } - - pub(crate) fn restore_reg_checkpoint(&mut self, checkpoint: u8) { - self.next_reg = checkpoint; - } - - pub(crate) fn reserve_reg(&mut self, reg: u8) { + pub(crate) fn reserve_reg(&mut self, reg: u32) { let next = reg.wrapping_add(1); if self.next_reg <= reg { self.next_reg = next; @@ -323,13 +306,13 @@ impl CompileCtx { } pub(crate) fn declare( - &mut self, name: &str, reg: u8, kind: VariableDeclarationKind, is_const: bool, + &mut self, name: &str, reg: u32, kind: VariableDeclarationKind, is_const: bool, ) -> Result<(), String> { self.scopes.symbols.declare(name, reg, kind, is_const) } pub(crate) fn declare_initialized( - &mut self, name: &str, reg: u8, kind: VariableDeclarationKind, is_const: bool, + &mut self, name: &str, reg: u32, kind: VariableDeclarationKind, is_const: bool, ) -> Result<(), String> { self.scopes.symbols.declare_initialized(name, reg, kind, is_const) } @@ -338,11 +321,11 @@ impl CompileCtx { self.scopes.symbols.push_scope_with_kind(kind); } - pub(crate) fn lookup(&self, name: &str) -> Result { + pub(crate) fn lookup(&self, name: &str) -> Result { self.scopes.symbols.lookup(name) } - pub(crate) fn lookup_or_builtin(&mut self, name: &str) -> Result { + pub(crate) fn lookup_or_builtin(&mut self, name: &str) -> Result { match self.scopes.symbols.lookup(name) { Ok(reg) => Ok(reg), Err(err) if Self::is_known_builtin(name) && err.contains("is not defined") => { @@ -355,7 +338,7 @@ impl CompileCtx { } } - pub(crate) fn lookup_or_global(&mut self, name: &str) -> u8 { + pub(crate) fn lookup_or_global(&mut self, name: &str) -> u32 { if let Some(reg) = self.scopes.symbols.lookup_any(name) { return reg; } @@ -465,7 +448,7 @@ impl CompileCtx { BUILTIN_GLOBALS.contains(&name) } - fn builtin_reg_floor(&self) -> u8 { + fn builtin_reg_floor(&self) -> u32 { self.scopes .builtin_reg_map .iter() @@ -513,7 +496,6 @@ impl CompileCtx { needs_home_object: false, captured_this_const_idx: 0, function_name: None, - reg_overflow: self.reg_overflow, const_overflow: self.const_overflow, nested: std::mem::take(&mut self.nested), } @@ -591,7 +573,7 @@ impl Emitter { pub(crate) fn compile_function_body_with_bindings<'a>( &self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, - is_expression_body: bool, extra_bindings: &[(&str, u8)], body_context: FunctionBodyContext, + is_expression_body: bool, extra_bindings: &[(&str, u32)], body_context: FunctionBodyContext, ) -> Result { self.compile_function_body_with_field_hooks( param_specs, @@ -610,14 +592,12 @@ impl Emitter { /// (expressions, member objects, call args, class fields, etc.) so their /// register slots are reserved *before* any temporary register is emitted. /// - /// Without this, single-pass emission lazily allocates builtin slots via - /// `lookup_or_builtin` at first use, which can collide with a temporary - /// register already reused by `restore_reg_checkpoint`. Pre-scanning moves - /// builtin slot allocation ahead of the temporary register pool. + /// vreg 化(D-01)后临时值不复用(独立 vreg),但 builtin 槽预注册仍 + /// 保证分配序稳定:builtin 槽先于临时值池,nested 继承边界(B012)不受扰。 #[allow(clippy::too_many_arguments)] pub(crate) fn compile_function_body_with_field_hooks<'a, E>( &self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, - is_expression_body: bool, extra_bindings: &[(&str, u8)], body_context: FunctionBodyContext, + is_expression_body: bool, extra_bindings: &[(&str, u32)], body_context: FunctionBodyContext, mut emit_fields: Option, fields_after_super: bool, ) -> Result where @@ -647,7 +627,7 @@ impl Emitter { // Inherit parent's global scope entries so previously-declared function names // are visible from within the body. - let mut inherited_reg_start = 1u8.max(ctx.builtin_reg_floor()); + let mut inherited_reg_start = 1u32.max(ctx.builtin_reg_floor()); for (name, binding) in &parent_ctx.scopes.symbols.scopes[0].bindings { ctx.scopes.symbols.scopes[0].bindings.insert( name.clone(), @@ -718,23 +698,23 @@ impl Emitter { // statement body returns undefined. if is_expression_body { if let Some(reg) = last_result_reg { - ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(reg), Operand::None, Operand::None)); } else { let undef_idx = ctx.add_constant(Constant::Undefined); let undef_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(undef_reg as u32), undef_idx)); - ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(undef_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::load_const(Operand::Reg(undef_reg), undef_idx)); + ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(undef_reg), Operand::None, Operand::None)); } } else { let undef_idx = ctx.add_constant(Constant::Undefined); let undef_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(undef_reg as u32), undef_idx)); - ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(undef_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::load_const(Operand::Reg(undef_reg), undef_idx)); + ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(undef_reg), Operand::None, Operand::None)); } let ir = ctx.assemble_ir( oxide_ir::ParamLayout { - base: param_base as u32, + base: param_base, count: param_specs.len() as u32, }, Some(parent_ctx), @@ -746,7 +726,7 @@ impl Emitter { fn emit_params_prologue<'a>( &self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, ctx: &mut CompileCtx, body_context: FunctionBodyContext, - ) -> Result { + ) -> Result { ctx.push_scope_with_kind(ScopeKind::FunctionScope); let param_base = ctx.next_reg; @@ -779,7 +759,7 @@ impl Emitter { } /// 双 sub-pass emit body:先函数声明(hoisting),再其余语句。返回最后结果寄存器。 - fn emit_body_stmts(&self, body_stmts: &[Statement], ctx: &mut CompileCtx) -> Result, String> { + fn emit_body_stmts(&self, body_stmts: &[Statement], ctx: &mut CompileCtx) -> Result, String> { let mut last_result_reg = None; for stmt in body_stmts { if matches!(stmt, Statement::FunctionDeclaration(_)) { @@ -799,7 +779,7 @@ impl Emitter { Ok(last_result_reg) } - pub(crate) fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::ExpressionStatement(_) | Statement::ReturnStatement(_) | Statement::EmptyStatement(_) => { self.emit_basic_domain(stmt, ctx) @@ -823,7 +803,7 @@ impl Emitter { } } - pub(crate) fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::NumericLiteral(_) | Expression::StringLiteral(_) @@ -887,7 +867,7 @@ impl Emitter { } // Second sub-pass: emit all other statements - let mut last_result: Option = None; + let mut last_result: Option = None; for stmt in &program.body { if matches!(stmt, Statement::FunctionDeclaration(_)) { continue; // Already emitted above @@ -898,7 +878,7 @@ impl Emitter { } } if let Some(r) = last_result { - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::None, Operand::Reg(r as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::None, Operand::Reg(r), Operand::None)); } else { let undef_idx = ctx.add_constant(Constant::Undefined); ctx.inst(Inst::load_const(Operand::None, undef_idx)); diff --git a/crates/oxide_emit/src/emit_ctx.rs b/crates/oxide_emit/src/emit_ctx.rs index 03998c5..b9135a9 100644 --- a/crates/oxide_emit/src/emit_ctx.rs +++ b/crates/oxide_emit/src/emit_ctx.rs @@ -36,7 +36,7 @@ impl LabelCtx { /// Identifier-binding state: symbols, builtin registers, private names. pub(crate) struct ScopeCtx { pub(crate) symbols: SymbolTable, - pub(crate) builtin_reg_map: Vec<(String, u8)>, + pub(crate) builtin_reg_map: Vec<(String, u32)>, pub(crate) private_name_map: Vec<(String, u32)>, pub(crate) next_private_name_id: u32, } diff --git a/crates/oxide_emit/src/expr/assignment.rs b/crates/oxide_emit/src/expr/assignment.rs index 6d155b0..22771d5 100644 --- a/crates/oxide_emit/src/expr/assignment.rs +++ b/crates/oxide_emit/src/expr/assignment.rs @@ -11,7 +11,7 @@ use oxide_parser::AssignmentOperator; impl Emitter { pub(crate) fn emit_assignment_expression( &self, assign: &oxide_parser::AssignmentExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { if let oxide_parser::AssignmentTarget::StaticMemberExpression(member) = &assign.left { if let Some(logical_op) = assign.operator.to_logical_operator() { let store_label = ctx.next_label_id(); @@ -20,15 +20,15 @@ impl Emitter { let prop_name = member.property.name.as_str(); 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 result_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(obj_reg as u32), Operand::None)); - ctx.inst(Inst::ic_get(Operand::Reg(result_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(obj_reg), Operand::None)); + ctx.inst(Inst::ic_get(Operand::Reg(result_reg), Operand::Reg(key_reg))); self.emit_logical_assign_test(logical_op, result_reg, store_label, end_label, ctx)?; ctx.labels.set_label_pos(store_label, ctx.insts.len()); let val_reg = self.emit_expression(&assign.right, ctx)?; - ctx.inst(Inst::ic_set(Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32))); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(val_reg as u32), Operand::None)); + ctx.inst(Inst::ic_set(Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg))); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(val_reg), Operand::None)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); return Ok(result_reg); } @@ -37,11 +37,11 @@ impl Emitter { let prop_name = member.property.name.as_str(); 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)); if assign.operator != AssignmentOperator::Assign { - let obj = Operand::Reg(obj_reg as u32); - let val = Operand::Reg(val_reg as u32); - let key = Operand::Reg(key_reg as u32); + let obj = Operand::Reg(obj_reg); + let val = Operand::Reg(val_reg); + let key = Operand::Reg(key_reg); match assign.operator { AssignmentOperator::Addition => ctx.inst(Inst::compound_member_add(obj, val, key)), AssignmentOperator::Subtraction => ctx.inst(Inst::compound_member_sub(obj, val, key)), @@ -53,7 +53,7 @@ impl Emitter { } Ok(val_reg) } else { - ctx.inst(Inst::ic_set(Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::ic_set(Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg))); Ok(val_reg) } } else if let oxide_parser::AssignmentTarget::ComputedMemberExpression(member) = &assign.left { @@ -63,19 +63,19 @@ impl Emitter { let obj_reg = self.emit_expression(&member.object, ctx)?; let key_reg = self.emit_expression(&member.expression, ctx)?; let result_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(result_reg as u32))); + ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(result_reg))); self.emit_logical_assign_test(logical_op, result_reg, store_label, end_label, ctx)?; ctx.labels.set_label_pos(store_label, ctx.insts.len()); let val_reg = self.emit_expression(&assign.right, ctx)?; - ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(val_reg as u32))); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(val_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(val_reg))); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(val_reg), Operand::None)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); return Ok(result_reg); } let obj_reg = self.emit_expression(&member.object, ctx)?; let key_reg = self.emit_expression(&member.expression, ctx)?; let val_reg = self.emit_expression(&assign.right, ctx)?; - ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(val_reg as u32))); + ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(val_reg))); Ok(val_reg) } else if let oxide_parser::AssignmentTarget::PrivateFieldExpression(member) = &assign.left { if assign.operator != AssignmentOperator::Assign { @@ -84,7 +84,7 @@ impl Emitter { let obj_reg = self.emit_expression(&member.object, ctx)?; let val_reg = self.emit_expression(&assign.right, ctx)?; let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?; - ctx.inst(Inst::new(OpCode::SET_PRIVATE, Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::SET_PRIVATE, Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg))); Ok(val_reg) } else if let oxide_parser::AssignmentTarget::AssignmentTargetIdentifier(id_ref) = &assign.left { if assign.operator != AssignmentOperator::Assign { @@ -94,14 +94,14 @@ impl Emitter { let name = id_ref.name.as_str(); let var_reg = ctx.lookup_or_global(name); let result_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(var_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(var_reg), Operand::None)); self.emit_logical_assign_test(logical_op, result_reg, store_label, end_label, ctx)?; ctx.labels.set_label_pos(store_label, ctx.insts.len()); let val_reg = self.emit_expression(&assign.right, ctx)?; let is_const = ctx.lookup_const_flag(name); let const_flag = if is_const { 1 } else { 0 }; - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(val_reg as u32), Operand::Imm(const_flag))); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(val_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(val_reg), Operand::Imm(const_flag))); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(val_reg), Operand::None)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); Ok(result_reg) } else if assign.operator == AssignmentOperator::Addition @@ -135,7 +135,7 @@ impl Emitter { AssignmentOperator::ShiftRightZeroFill => OpCode::COMPOUND_USHR, _ => return Err(format!("compound assignment operator {:?} not supported", assign.operator)), }; - ctx.inst(Inst::new(op, Operand::Reg(var_reg as u32), Operand::Reg(rhs as u32), Operand::None)); + ctx.inst(Inst::new(op, Operand::Reg(var_reg), Operand::Reg(rhs), Operand::None)); Ok(var_reg) } else { Err(format!("compound assignment operator {:?} not supported", assign.operator)) @@ -145,18 +145,18 @@ impl Emitter { let name = id_ref.name.as_str(); // Check if target is an upvalue reference if let Some(uv_idx) = ctx.current_upvalue_captures.iter().position(|u| u.name == name) { - ctx.inst(Inst::new(OpCode::STORE_UPVALUE, Operand::None, Operand::Reg(val_reg as u32), Operand::Imm(uv_idx as u16))); + ctx.inst(Inst::new(OpCode::STORE_UPVALUE, Operand::None, Operand::Reg(val_reg), Operand::Imm(uv_idx as u16))); return Ok(val_reg); } // Check if target is a captured cell if let Some(&cell_idx) = ctx.captured_bindings.get(name) { - ctx.inst(Inst::new(OpCode::CELL_SET, Operand::None, Operand::Reg(val_reg as u32), Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(OpCode::CELL_SET, Operand::None, Operand::Reg(val_reg), Operand::Imm(cell_idx as u16))); return Ok(val_reg); } let var_reg = ctx.lookup_or_global(name); let is_const = ctx.lookup_const_flag(name); let const_flag = if is_const { 1 } else { 0 }; - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(val_reg as u32), Operand::Imm(const_flag))); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(val_reg), Operand::Imm(const_flag))); Ok(val_reg) } } else if matches!( diff --git a/crates/oxide_emit/src/expr/call.rs b/crates/oxide_emit/src/expr/call.rs index c7ade3f..7fdf0f4 100644 --- a/crates/oxide_emit/src/expr/call.rs +++ b/crates/oxide_emit/src/expr/call.rs @@ -9,7 +9,7 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Expression; impl Emitter { - fn emit_call_expression(&self, call: &oxide_parser::CallExpression, ctx: &mut CompileCtx) -> Result { + fn emit_call_expression(&self, call: &oxide_parser::CallExpression, ctx: &mut CompileCtx) -> Result { if matches!(&call.callee, Expression::Super(_)) { if !ctx.in_derived_constructor { return Err("super() only supported in derived constructors".into()); @@ -20,11 +20,11 @@ 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 result_reg = ctx.alloc_reg(); ctx.inst(Inst::super_call( - Operand::Reg(result_reg as u32), - Operand::Reg(first_arg_reg as u32), + Operand::Reg(result_reg), + Operand::Reg(first_arg_reg), arg_regs.len() as u8, )); if let Some(mut field_buffer) = ctx.field_buffer.take() { @@ -41,14 +41,14 @@ impl Emitter { let obj_reg = self.emit_expression(&member.object, ctx)?; 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) } Expression::StaticMemberExpression(member) => { let is_super_member = matches!(&member.object, Expression::Super(_)); let obj_reg = if is_super_member { let this_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(this_reg as u32), Operand::This, Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(this_reg), Operand::This, Operand::None)); this_reg } else { self.emit_expression(&member.object, ctx)? @@ -56,7 +56,7 @@ impl Emitter { let prop_name = member.property.name.as_str(); 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 callee_reg = ctx.alloc_reg(); if is_super_member { if !ctx.in_instance_method && !ctx.in_static_method && !ctx.in_derived_constructor { @@ -67,10 +67,10 @@ impl Emitter { } else { OpCode::SUPER_GET_PROP }; - ctx.inst(Inst::new(op, Operand::Reg(callee_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(op, Operand::Reg(callee_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg))); } else { - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(callee_reg as u32), Operand::Reg(obj_reg as u32), Operand::None)); - ctx.inst(Inst::ic_get(Operand::Reg(callee_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(callee_reg), Operand::Reg(obj_reg), Operand::None)); + ctx.inst(Inst::ic_get(Operand::Reg(callee_reg), Operand::Reg(key_reg))); } (callee_reg, obj_reg) } @@ -78,7 +78,7 @@ impl Emitter { let callee_reg = self.emit_expression(&call.callee, ctx)?; 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) } }; @@ -88,7 +88,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, @@ -96,28 +96,28 @@ 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) } - pub(crate) fn emit_call_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_call_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::CallExpression(call) => self.emit_call_expression(call, ctx), _ => self.emit_unsupported_expression(expr, ctx), diff --git a/crates/oxide_emit/src/expr/function/mod.rs b/crates/oxide_emit/src/expr/function/mod.rs index 6fb184f..4d67ca2 100644 --- a/crates/oxide_emit/src/expr/function/mod.rs +++ b/crates/oxide_emit/src/expr/function/mod.rs @@ -10,7 +10,7 @@ use oxide_parser::{Class, Expression, Statement}; impl Emitter { fn emit_arrow_function_expression( &self, arrow: &oxide_parser::ArrowFunctionExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { // Rest params not yet supported if let Some(_rest) = &arrow.params.rest { return Err("rest params in arrow functions not yet supported".into()); @@ -45,11 +45,11 @@ impl Emitter { let sub_idx = ctx.nested.len() as u16; let r = ctx.alloc_reg(); - ctx.inst(Inst::create_closure(Operand::Reg(r as u32), sub_idx)); + ctx.inst(Inst::create_closure(Operand::Reg(r), sub_idx)); Ok(r) } - fn emit_function_expression(&self, fe: &oxide_parser::Function, ctx: &mut CompileCtx) -> Result { + fn emit_function_expression(&self, fe: &oxide_parser::Function, ctx: &mut CompileCtx) -> Result { // FunctionExpression: compile body, emit LOAD_CONST(BytecodeFunc) let mut param_names = Vec::new(); for (idx, param) in fe.params.items.iter().enumerate() { @@ -77,15 +77,15 @@ impl Emitter { let sub_idx = ctx.nested.len() as u16; let r = ctx.alloc_reg(); - ctx.inst(Inst::create_closure(Operand::Reg(r as u32), sub_idx)); + ctx.inst(Inst::create_closure(Operand::Reg(r), sub_idx)); Ok(r) } - fn emit_class_expression(&self, class: &Class, ctx: &mut CompileCtx) -> Result { + fn emit_class_expression(&self, class: &Class, ctx: &mut CompileCtx) -> Result { self.emit_class(class, ctx) } - fn emit_new_expression(&self, ne: &oxide_parser::NewExpression, ctx: &mut CompileCtx) -> Result { + fn emit_new_expression(&self, ne: &oxide_parser::NewExpression, ctx: &mut CompileCtx) -> Result { let constructor_reg = self.emit_expression(&ne.callee, ctx)?; let mut arg_regs = Vec::new(); for arg in &ne.arguments { @@ -93,18 +93,18 @@ 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 r = ctx.alloc_reg(); ctx.inst(Inst::new_expression( - Operand::Reg(r as u32), - Operand::Reg(constructor_reg as u32), - Operand::Reg(first_arg_reg as u32), + Operand::Reg(r), + Operand::Reg(constructor_reg), + Operand::Reg(first_arg_reg), arg_regs.len() as u8, )); Ok(r) } - pub(crate) fn emit_function_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_function_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::ArrowFunctionExpression(arrow) => self.emit_arrow_function_expression(arrow, ctx), Expression::FunctionExpression(fe) => self.emit_function_expression(fe, ctx), diff --git a/crates/oxide_emit/src/expr/identifier.rs b/crates/oxide_emit/src/expr/identifier.rs index 27a7322..0b84420 100644 --- a/crates/oxide_emit/src/expr/identifier.rs +++ b/crates/oxide_emit/src/expr/identifier.rs @@ -8,14 +8,14 @@ use oxide_bytecode::opcode::OpCode; impl Emitter { pub(crate) fn emit_identifier_expression( &self, ident: &oxide_parser::IdentifierReference, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let name = ident.name.as_str(); for (uv_idx, up) in ctx.current_upvalue_captures.iter().enumerate() { if up.name == name { let r = ctx.alloc_reg(); let idx = uv_idx as u8; - ctx.inst(Inst::new(OpCode::LOAD_UPVALUE, Operand::Reg(r as u32), Operand::Reg(idx as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_UPVALUE, Operand::Reg(r), Operand::Reg(idx as u32), Operand::None)); return Ok(r); } } @@ -23,16 +23,16 @@ impl Emitter { if let Some(&cell_idx) = ctx.captured_bindings.get(name) { let r = ctx.alloc_reg(); if let Some((binding, _)) = ctx.scopes.symbols.lookup_any_binding(name) { - ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(r as u32), Operand::Reg(binding.reg as u32), Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(r), Operand::Reg(binding.reg), Operand::Imm(cell_idx as u16))); } else { - ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(r as u32), Operand::None, Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(r), Operand::None, Operand::Imm(cell_idx as u16))); } return Ok(r); } let var_reg = ctx.lookup_or_builtin(name)?; let r = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r as u32), Operand::Reg(var_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r), Operand::Reg(var_reg), Operand::None)); Ok(r) } } diff --git a/crates/oxide_emit/src/expr/literal.rs b/crates/oxide_emit/src/expr/literal.rs index 36a0c06..dbdc00f 100644 --- a/crates/oxide_emit/src/expr/literal.rs +++ b/crates/oxide_emit/src/expr/literal.rs @@ -9,7 +9,7 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Expression; impl Emitter { - pub(crate) fn emit_literal(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_literal(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::NumericLiteral(n) => self.emit_numeric_literal_expression(n, ctx), Expression::StringLiteral(s) => self.emit_string_literal_expression(s, ctx), @@ -22,45 +22,45 @@ impl Emitter { fn emit_numeric_literal_expression( &self, n: &oxide_parser::NumericLiteral, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let idx = if is_int_literal(n.value) { ctx.add_constant(Constant::Int(n.value as i32)) } else { ctx.add_constant(Constant::Number(n.value)) }; let r = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(r as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(r), idx)); Ok(r) } fn emit_string_literal_expression( &self, s: &oxide_parser::StringLiteral, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let idx = ctx.add_constant(Constant::String(s.value.to_string())); let r = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(r as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(r), idx)); Ok(r) } fn emit_boolean_literal_expression( &self, b: &oxide_parser::BooleanLiteral, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let idx = ctx.add_constant(Constant::Boolean(b.value)); let r = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(r as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(r), idx)); Ok(r) } - fn emit_null_literal_expression(&self, ctx: &mut CompileCtx) -> Result { + fn emit_null_literal_expression(&self, ctx: &mut CompileCtx) -> Result { let idx = ctx.add_constant(Constant::Null); let r = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(r as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(r), idx)); Ok(r) } fn emit_reg_exp_literal_expression( &self, lit: &oxide_parser::RegExpLiteral, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { if let Some(raw) = &lit.raw { let raw_str = raw.to_string(); if raw_str.len() >= 2 && raw_str.starts_with('/') { @@ -69,12 +69,12 @@ impl Emitter { let flags = raw_str[last_slash + 1..].to_string(); let pat_ci = ctx.add_constant(Constant::String(pattern)); let pat_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(pat_reg as u32), pat_ci)); + ctx.inst(Inst::load_const(Operand::Reg(pat_reg), pat_ci)); let flags_ci = ctx.add_constant(Constant::String(flags)); let flags_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(flags_reg as u32), flags_ci)); + ctx.inst(Inst::load_const(Operand::Reg(flags_reg), flags_ci)); let r = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::CREATE_REGEXP, Operand::Reg(r as u32), Operand::Reg(pat_reg as u32), Operand::Reg(flags_reg as u32))); + ctx.inst(Inst::new(OpCode::CREATE_REGEXP, Operand::Reg(r), Operand::Reg(pat_reg), Operand::Reg(flags_reg))); Ok(r) } else { Err(format!("unsupported regexp literal: {:?}", lit)) diff --git a/crates/oxide_emit/src/expr/member/mod.rs b/crates/oxide_emit/src/expr/member/mod.rs index 0214d17..60179c2 100644 --- a/crates/oxide_emit/src/expr/member/mod.rs +++ b/crates/oxide_emit/src/expr/member/mod.rs @@ -12,7 +12,7 @@ use oxide_parser::Expression; impl Emitter { fn emit_static_member_expression( &self, member: &oxide_parser::StaticMemberExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { if matches!(&member.object, Expression::Super(_)) { if !ctx.in_instance_method && !ctx.in_static_method && !ctx.in_derived_constructor { return Err("super property only supported in class methods".into()); @@ -20,62 +20,62 @@ impl Emitter { let prop_name = member.property.name.as_str(); 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 this_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(this_reg as u32), Operand::This, Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(this_reg), Operand::This, Operand::None)); let result_reg = ctx.alloc_reg(); let op = if ctx.in_static_method { OpCode::SUPER_STATIC_GET_PROP } else { OpCode::SUPER_GET_PROP }; - ctx.inst(Inst::new(op, Operand::Reg(result_reg as u32), Operand::Reg(this_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(op, Operand::Reg(result_reg), Operand::Reg(this_reg), Operand::Reg(key_reg))); return Ok(result_reg); } let obj_reg = self.emit_expression(&member.object, ctx)?; let prop_name = member.property.name.as_str(); 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::ic_get(Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::load_const(Operand::Reg(key_reg), idx)); + ctx.inst(Inst::ic_get(Operand::Reg(obj_reg), Operand::Reg(key_reg))); Ok(obj_reg) } fn emit_computed_member_expression( &self, member: &oxide_parser::ComputedMemberExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let obj_reg = self.emit_expression(&member.object, ctx)?; let key_reg = self.emit_expression(&member.expression, ctx)?; let r = 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(r as u32))); + ctx.inst(Inst::new(OpCode::GET_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(r))); Ok(r) } fn emit_private_field_expression( &self, member: &oxide_parser::PrivateFieldExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let obj_reg = self.emit_expression(&member.object, ctx)?; let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?; let r = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(r as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::GET_PRIVATE, Operand::Reg(r), Operand::Reg(obj_reg), Operand::Reg(key_reg))); Ok(r) } - fn emit_chain_expression(&self, chain: &oxide_parser::ChainExpression, ctx: &mut CompileCtx) -> Result { + fn emit_chain_expression(&self, chain: &oxide_parser::ChainExpression, ctx: &mut CompileCtx) -> Result { let short_label = ctx.next_label_id(); let end_label = ctx.next_label_id(); let value_reg = self.emit_chain_element(&chain.expression, Some(short_label), ctx)?; let result_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(value_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(value_reg), Operand::None)); ctx.inst(Inst::jmp(end_label)); ctx.labels.set_label_pos(short_label, ctx.insts.len()); let undefined_idx = ctx.add_constant(Constant::Undefined); - ctx.inst(Inst::load_const(Operand::Reg(result_reg as u32), undefined_idx)); + ctx.inst(Inst::load_const(Operand::Reg(result_reg), undefined_idx)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); Ok(result_reg) } - pub(crate) fn emit_member_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_member_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::StaticMemberExpression(member) => self.emit_static_member_expression(member, ctx), Expression::ComputedMemberExpression(member) => self.emit_computed_member_expression(member, ctx), diff --git a/crates/oxide_emit/src/expr/mod.rs b/crates/oxide_emit/src/expr/mod.rs index 47acb74..32e43f4 100644 --- a/crates/oxide_emit/src/expr/mod.rs +++ b/crates/oxide_emit/src/expr/mod.rs @@ -19,15 +19,15 @@ pub mod template; pub mod this; impl Emitter { - pub(crate) fn emit_unsupported_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_unsupported_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { let _ = ctx; Err(format!("unsupported expression type: {:?}", expr)) } pub(crate) fn emit_sequence_expression( &self, seq: &oxide_parser::SequenceExpression, ctx: &mut CompileCtx, - ) -> Result { - let mut last = 0u8; + ) -> Result { + let mut last = 0u32; for e in &seq.expressions { last = self.emit_expression(e, ctx)?; } diff --git a/crates/oxide_emit/src/expr/object/array.rs b/crates/oxide_emit/src/expr/object/array.rs index 2c62609..d82e9cc 100644 --- a/crates/oxide_emit/src/expr/object/array.rs +++ b/crates/oxide_emit/src/expr/object/array.rs @@ -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 { + ) -> Result { 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) } diff --git a/crates/oxide_emit/src/expr/object/mod.rs b/crates/oxide_emit/src/expr/object/mod.rs index f873af8..7269955 100644 --- a/crates/oxide_emit/src/expr/object/mod.rs +++ b/crates/oxide_emit/src/expr/object/mod.rs @@ -7,7 +7,7 @@ mod array; mod object_literal; impl Emitter { - pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx), Expression::ArrayExpression(arr) => self.emit_array_expression(arr, ctx), diff --git a/crates/oxide_emit/src/expr/object/object_literal.rs b/crates/oxide_emit/src/expr/object/object_literal.rs index 11c0149..a789b5d 100644 --- a/crates/oxide_emit/src/expr/object/object_literal.rs +++ b/crates/oxide_emit/src/expr/object/object_literal.rs @@ -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 { + ) -> Result { 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))); } } } diff --git a/crates/oxide_emit/src/expr/operator/mod.rs b/crates/oxide_emit/src/expr/operator/mod.rs index 34f8a7a..e42f628 100644 --- a/crates/oxide_emit/src/expr/operator/mod.rs +++ b/crates/oxide_emit/src/expr/operator/mod.rs @@ -12,16 +12,15 @@ use oxide_parser::{ChainElement, Expression, LogicalOperator, SimpleAssignmentTa impl Emitter { fn emit_private_in_expression( &self, pin: &oxide_parser::PrivateInExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let obj_reg = self.emit_expression(&pin.right, ctx)?; let key_reg = self.emit_private_id_reg(pin.left.name.as_str(), ctx)?; let result_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::PRIVATE_BRAND_IN, Operand::Reg(result_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::PRIVATE_BRAND_IN, Operand::Reg(result_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg))); Ok(result_reg) } - fn emit_binary_expression(&self, bin: &oxide_parser::BinaryExpression, ctx: &mut CompileCtx) -> Result { - let checkpoint = ctx.reg_checkpoint(); + fn emit_binary_expression(&self, bin: &oxide_parser::BinaryExpression, ctx: &mut CompileCtx) -> Result { let left = self.emit_expression(&bin.left, ctx)?; let right = self.emit_expression(&bin.right, ctx)?; let op = match bin.operator { @@ -48,14 +47,11 @@ impl Emitter { BinaryOperator::StrictInequality => OpCode::STRICT_NEQ, _ => return Err(format!("unsupported binary operator: {:?}", bin.operator)), }; - ctx.inst(Inst::new(op, Operand::Reg(left as u32), Operand::Reg(left as u32), Operand::Reg(right as u32))); - if is_side_effect_free(&bin.left) && is_side_effect_free(&bin.right) { - ctx.restore_reg_checkpoint(checkpoint.saturating_add(1).max(left.saturating_add(1))); - } + ctx.inst(Inst::new(op, Operand::Reg(left), Operand::Reg(left), Operand::Reg(right))); Ok(left) } - fn emit_unary_expression(&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx) -> Result { + fn emit_unary_expression(&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx) -> Result { if matches!(un.operator, UnaryOperator::Delete) { return match &un.argument { Expression::Identifier(_) => { @@ -65,13 +61,13 @@ impl Emitter { let obj_reg = self.emit_expression(&member.object, ctx)?; let prop_name = member.property.name.as_str(); let const_idx = ctx.add_constant(Constant::String(prop_name.to_string())); - ctx.inst(Inst::delete_prop_static(Operand::Reg(obj_reg as u32), const_idx as u32)); + ctx.inst(Inst::delete_prop_static(Operand::Reg(obj_reg), const_idx as u32)); Ok(obj_reg) } Expression::ComputedMemberExpression(member) => { let obj_reg = self.emit_expression(&member.object, ctx)?; let key_reg = self.emit_expression(&member.expression, ctx)?; - ctx.inst(Inst::new(OpCode::DELETE_PROP_DYNAMIC, Operand::Reg(obj_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::DELETE_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg))); Ok(obj_reg) } Expression::ChainExpression(chain) => { @@ -81,23 +77,23 @@ impl Emitter { let obj_reg = self.emit_expression(&member.object, ctx)?; if member.optional { let dup_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(obj_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(obj_reg), Operand::None)); ctx.inst(Inst::jmp_if_nullish(dup_reg, short_label)); } let prop_name = member.property.name.as_str(); let const_idx = ctx.add_constant(Constant::String(prop_name.to_string())); - ctx.inst(Inst::delete_prop_static(Operand::Reg(obj_reg as u32), const_idx as u32)); + ctx.inst(Inst::delete_prop_static(Operand::Reg(obj_reg), const_idx as u32)); obj_reg } ChainElement::ComputedMemberExpression(member) => { let obj_reg = self.emit_expression(&member.object, ctx)?; if member.optional { let dup_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(obj_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(obj_reg), Operand::None)); ctx.inst(Inst::jmp_if_nullish(dup_reg, short_label)); } let key_reg = self.emit_expression(&member.expression, ctx)?; - ctx.inst(Inst::new(OpCode::DELETE_PROP_DYNAMIC, Operand::Reg(obj_reg as u32), Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::new(OpCode::DELETE_PROP_DYNAMIC, Operand::Reg(obj_reg), Operand::Reg(obj_reg), Operand::Reg(key_reg))); obj_reg } _ => return Err("invalid delete target".into()), @@ -106,7 +102,7 @@ impl Emitter { ctx.inst(Inst::jmp(end_label)); ctx.labels.set_label_pos(short_label, ctx.insts.len()); let true_idx = ctx.add_constant(Constant::Boolean(true)); - ctx.inst(Inst::load_const(Operand::Reg(result_reg as u32), true_idx)); + ctx.inst(Inst::load_const(Operand::Reg(result_reg), true_idx)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); Ok(result_reg) } @@ -116,27 +112,27 @@ impl Emitter { let arg = self.emit_expression(&un.argument, ctx)?; match un.operator { UnaryOperator::UnaryNegation => { - ctx.inst(Inst::new(OpCode::NEG, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::NEG, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::Typeof => { - ctx.inst(Inst::new(OpCode::TYPEOF, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::TYPEOF, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::Void => { - ctx.inst(Inst::new(OpCode::VOID, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::VOID, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::LogicalNot => { - ctx.inst(Inst::new(OpCode::NOT, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::BitwiseNot => { - ctx.inst(Inst::new(OpCode::BIT_NOT, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::BIT_NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::UnaryPlus => { - ctx.inst(Inst::new(OpCode::UNARY_PLUS, Operand::Reg(arg as u32), Operand::Reg(arg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::UNARY_PLUS, Operand::Reg(arg), Operand::Reg(arg), Operand::None)); Ok(arg) } UnaryOperator::Delete => Err("invalid delete target".into()), @@ -145,7 +141,7 @@ impl Emitter { fn emit_conditional_expression( &self, cond: &oxide_parser::ConditionalExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let else_label = ctx.next_label_id(); let end_label = ctx.next_label_id(); @@ -154,13 +150,13 @@ impl Emitter { let cons_reg = self.emit_expression(&cond.consequent, ctx)?; let result_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(cons_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(cons_reg), Operand::None)); ctx.inst(Inst::jmp(end_label)); ctx.labels.set_label_pos(else_label, ctx.insts.len()); let alt_reg = self.emit_expression(&cond.alternate, ctx)?; - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(alt_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(alt_reg), Operand::None)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); Ok(result_reg) @@ -168,7 +164,7 @@ impl Emitter { fn emit_logical_expression( &self, log: &oxide_parser::LogicalExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let left_reg = self.emit_expression(&log.left, ctx)?; if is_side_effect_free(&log.left) && is_side_effect_free(&log.right) { @@ -179,7 +175,7 @@ impl Emitter { LogicalOperator::Or => OpCode::OR, LogicalOperator::Coalesce => OpCode::NULLISH, }; - ctx.inst(Inst::new(op, Operand::Reg(r as u32), Operand::Reg(left_reg as u32), Operand::Reg(right_reg as u32))); + ctx.inst(Inst::new(op, Operand::Reg(r), Operand::Reg(left_reg), Operand::Reg(right_reg))); return Ok(r); } @@ -187,12 +183,12 @@ impl Emitter { let rhs_label = ctx.next_label_id(); let end_label = ctx.next_label_id(); let dup_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(left_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(left_reg), Operand::None)); ctx.inst(Inst::jmp_if_nullish(dup_reg, rhs_label)); ctx.inst(Inst::jmp(end_label)); ctx.labels.set_label_pos(rhs_label, ctx.insts.len()); let right_reg = self.emit_expression(&log.right, ctx)?; - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(right_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(right_reg), Operand::None)); ctx.labels.set_label_pos(end_label, ctx.insts.len()); return Ok(dup_reg); } @@ -203,7 +199,7 @@ impl Emitter { LogicalOperator::Coalesce => return Err("invalid logical operator dispatch".into()), }; let dup_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(left_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(left_reg), Operand::None)); match log.operator { LogicalOperator::And => ctx.inst(Inst::jmp_if_false(dup_reg, skip_label)), @@ -212,7 +208,7 @@ impl Emitter { } let right_reg = self.emit_expression(&log.right, ctx)?; - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(right_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg), Operand::Reg(right_reg), Operand::None)); ctx.labels.set_label_pos(skip_label, ctx.insts.len()); Ok(dup_reg) @@ -220,7 +216,7 @@ impl Emitter { fn emit_update_expression( &self, update: &oxide_parser::UpdateExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { match &update.argument { SimpleAssignmentTarget::AssignmentTargetIdentifier(id) => { let name = id.name.as_str(); @@ -230,36 +226,36 @@ impl Emitter { if let Some(uv) = uv_idx { // Upvalue: LOAD_UPVALUE + CONST(1) + ADD/SUB + STORE_UPVALUE let val_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::LOAD_UPVALUE, Operand::Reg(val_reg as u32), Operand::Imm(uv as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_UPVALUE, Operand::Reg(val_reg), Operand::Imm(uv as u16), Operand::None)); let one_idx = ctx.add_constant(Constant::Int(1)); let one_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(one_reg as u32), one_idx)); + ctx.inst(Inst::load_const(Operand::Reg(one_reg), one_idx)); let op = if update.operator == UpdateOperator::Increment { OpCode::ADD } else { OpCode::SUB }; - ctx.inst(Inst::new(op, Operand::Reg(val_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(one_reg as u32))); - ctx.inst(Inst::new(OpCode::STORE_UPVALUE, Operand::None, Operand::Reg(val_reg as u32), Operand::Imm(uv as u16))); + ctx.inst(Inst::new(op, Operand::Reg(val_reg), Operand::Reg(val_reg), Operand::Reg(one_reg))); + ctx.inst(Inst::new(OpCode::STORE_UPVALUE, Operand::None, Operand::Reg(val_reg), Operand::Imm(uv as u16))); Ok(val_reg) } else if let Some(cell_idx) = captured_cell { // Captured cell: CELL_GET + CONST(1) + ADD/SUB + CELL_SET let val_reg = ctx.alloc_reg(); if let Some((binding, _)) = ctx.scopes.symbols.lookup_any_binding(name) { - ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(val_reg as u32), Operand::Reg(binding.reg as u32), Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(val_reg), Operand::Reg(binding.reg), Operand::Imm(cell_idx as u16))); } else { - ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(val_reg as u32), Operand::None, Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(OpCode::CELL_GET, Operand::Reg(val_reg), Operand::None, Operand::Imm(cell_idx as u16))); } let one_idx = ctx.add_constant(Constant::Int(1)); let one_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(one_reg as u32), one_idx)); + ctx.inst(Inst::load_const(Operand::Reg(one_reg), one_idx)); let op = if update.operator == UpdateOperator::Increment { OpCode::ADD } else { OpCode::SUB }; - ctx.inst(Inst::new(op, Operand::Reg(val_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(one_reg as u32))); - ctx.inst(Inst::new(OpCode::CELL_SET, Operand::None, Operand::Reg(val_reg as u32), Operand::Imm(cell_idx as u16))); + ctx.inst(Inst::new(op, Operand::Reg(val_reg), Operand::Reg(val_reg), Operand::Reg(one_reg))); + ctx.inst(Inst::new(OpCode::CELL_SET, Operand::None, Operand::Reg(val_reg), Operand::Imm(cell_idx as u16))); Ok(val_reg) } else { let var_reg = ctx.lookup_or_global(name); @@ -270,7 +266,7 @@ impl Emitter { (UpdateOperator::Decrement, true) => OpCode::DEC_PRE, (UpdateOperator::Decrement, false) => OpCode::DEC_POST, }; - ctx.inst(Inst::new(op, Operand::Reg(var_reg as u32), Operand::Reg(result_reg as u32), Operand::Reg(result_reg as u32))); + ctx.inst(Inst::new(op, Operand::Reg(var_reg), Operand::Reg(result_reg), Operand::Reg(result_reg))); Ok(result_reg) } } @@ -279,7 +275,7 @@ impl Emitter { let prop_name = member.property.name.as_str(); let key_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), key_idx)); + ctx.inst(Inst::load_const(Operand::Reg(key_reg), key_idx)); let val_reg = ctx.alloc_reg(); let op = match update.operator { UpdateOperator::Increment => OpCode::MEMBER_INC, @@ -287,10 +283,10 @@ impl Emitter { }; match op { OpCode::MEMBER_INC => { - ctx.inst(Inst::member_inc(Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::member_inc(Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg))); } OpCode::MEMBER_DEC => { - ctx.inst(Inst::member_dec(Operand::Reg(obj_reg as u32), Operand::Reg(val_reg as u32), Operand::Reg(key_reg as u32))); + ctx.inst(Inst::member_dec(Operand::Reg(obj_reg), Operand::Reg(val_reg), Operand::Reg(key_reg))); } _ => unreachable!(), } @@ -304,14 +300,14 @@ impl Emitter { UpdateOperator::Increment => OpCode::DYN_MEMBER_INC, UpdateOperator::Decrement => OpCode::DYN_MEMBER_DEC, }; - ctx.inst(Inst::new(op, Operand::Reg(obj_reg as u32), Operand::Reg(key_reg as u32), Operand::Reg(val_reg as u32))); + ctx.inst(Inst::new(op, Operand::Reg(obj_reg), Operand::Reg(key_reg), Operand::Reg(val_reg))); Ok(val_reg) } _ => Err("member update not yet supported".into()), } } - pub(crate) fn emit_operator(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_operator(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::BinaryExpression(bin) => self.emit_binary_expression(bin, ctx), Expression::PrivateInExpression(pin) => self.emit_private_in_expression(pin, ctx), diff --git a/crates/oxide_emit/src/expr/parenthesized.rs b/crates/oxide_emit/src/expr/parenthesized.rs index afbd2ab..b6b4c21 100644 --- a/crates/oxide_emit/src/expr/parenthesized.rs +++ b/crates/oxide_emit/src/expr/parenthesized.rs @@ -5,7 +5,7 @@ use crate::{CompileCtx, Emitter}; impl Emitter { pub(crate) fn emit_parenthesized_expression( &self, p: &oxide_parser::ParenthesizedExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { self.emit_expression(&p.expression, ctx) } } diff --git a/crates/oxide_emit/src/expr/template/mod.rs b/crates/oxide_emit/src/expr/template/mod.rs index 25c2344..67d7bb6 100644 --- a/crates/oxide_emit/src/expr/template/mod.rs +++ b/crates/oxide_emit/src/expr/template/mod.rs @@ -7,7 +7,7 @@ mod tagged; mod template_literal; impl Emitter { - pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx), Expression::TaggedTemplateExpression(tt) => self.emit_tagged_template_expression(tt, ctx), diff --git a/crates/oxide_emit/src/expr/template/tagged.rs b/crates/oxide_emit/src/expr/template/tagged.rs index fc7b552..e5158ce 100644 --- a/crates/oxide_emit/src/expr/template/tagged.rs +++ b/crates/oxide_emit/src/expr/template/tagged.rs @@ -8,36 +8,36 @@ use oxide_bytecode::opcode::OpCode; impl Emitter { pub(crate) fn emit_tagged_template_expression( &self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let quasis = &tt.quasi.quasis; let expressions = &tt.quasi.expressions; let tag_reg = self.emit_expression(&tt.tag, ctx)?; let cooked_temp = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(cooked_temp as u32), Operand::Imm(quasis.len() as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(cooked_temp), Operand::Imm(quasis.len() as u16), Operand::None)); for (i, quasi) in quasis.iter().enumerate() { let s = quasi.value.cooked.as_ref().map(|c| c.to_string()).unwrap_or_default(); let const_idx = ctx.add_constant(Constant::String(s)); let str_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(str_reg as u32), const_idx)); + ctx.inst(Inst::load_const(Operand::Reg(str_reg), const_idx)); let idx_const = ctx.add_constant(Constant::Int(i as i32)); let idx_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(idx_reg as u32), idx_const)); - ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(cooked_temp as u32), Operand::Reg(idx_reg as u32), Operand::Reg(str_reg as u32))); + ctx.inst(Inst::load_const(Operand::Reg(idx_reg), idx_const)); + ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(cooked_temp), Operand::Reg(idx_reg), Operand::Reg(str_reg))); } let raw_temp = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(raw_temp as u32), Operand::Imm(quasis.len() as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(raw_temp), Operand::Imm(quasis.len() as u16), Operand::None)); for (i, quasi) in quasis.iter().enumerate() { let raw = quasi.value.raw.to_string(); let const_idx = ctx.add_constant(Constant::String(raw)); let str_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(str_reg as u32), const_idx)); + ctx.inst(Inst::load_const(Operand::Reg(str_reg), const_idx)); let idx_const = ctx.add_constant(Constant::Int(i as i32)); let idx_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(idx_reg as u32), idx_const)); - ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(raw_temp as u32), Operand::Reg(idx_reg as u32), Operand::Reg(str_reg as u32))); + ctx.inst(Inst::load_const(Operand::Reg(idx_reg), idx_const)); + ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(raw_temp), Operand::Reg(idx_reg), Operand::Reg(str_reg))); } let mut expr_temps = Vec::new(); @@ -52,26 +52,26 @@ impl Emitter { expr_slots.push(ctx.alloc_reg()); } - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(cooked_slot as u32), Operand::Reg(cooked_temp as u32), Operand::None)); - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(raw_slot as u32), Operand::Reg(raw_temp as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(cooked_slot), Operand::Reg(cooked_temp), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(raw_slot), Operand::Reg(raw_temp), Operand::None)); for (slot, temp) in expr_slots.iter().zip(expr_temps.iter()) { - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(*slot as u32), Operand::Reg(*temp as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(*slot), Operand::Reg(*temp), Operand::None)); } let undef_idx = ctx.add_constant(Constant::Undefined); let undef_reg = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(undef_reg as u32), undef_idx)); + ctx.inst(Inst::load_const(Operand::Reg(undef_reg), undef_idx)); let arg_count = 2 + expressions.len(); ctx.inst(Inst::call( - Operand::Reg(tag_reg as u32), - Operand::Reg(undef_reg as u32), - Operand::Reg(cooked_slot as u32), + Operand::Reg(tag_reg), + Operand::Reg(undef_reg), + Operand::Reg(cooked_slot), arg_count as u8, )); 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) } } diff --git a/crates/oxide_emit/src/expr/template/template_literal.rs b/crates/oxide_emit/src/expr/template/template_literal.rs index bb4bec9..19c706f 100644 --- a/crates/oxide_emit/src/expr/template/template_literal.rs +++ b/crates/oxide_emit/src/expr/template/template_literal.rs @@ -9,13 +9,13 @@ use oxide_bytecode::module::Constant; impl Emitter { pub(crate) fn emit_template_literal_expression( &self, tl: &oxide_parser::TemplateLiteral, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { let r = ctx.alloc_reg(); let quasis = &tl.quasis; let expressions = &tl.expressions; let segment_count = quasis.len() + expressions.len(); - let expr_regs: Vec = expressions + let expr_regs: Vec = expressions .iter() .map(|e| self.emit_expression(e, ctx)) .collect::, _>>()?; @@ -38,12 +38,12 @@ impl Emitter { for const_idx in quasi_const_idxs.iter() { parts.push(*const_idx as u32 & 0x7FFF_FFFF); if let Some(expr_reg) = expr_iter.next() { - parts.push(0x8000_0000u32 | (*expr_reg as u32)); + parts.push(0x8000_0000u32 | (*expr_reg)); } } ctx.inst(Inst::template_str( - Operand::Reg(r as u32), + Operand::Reg(r), segment_count as u32, total_len_hint as u16, &parts, diff --git a/crates/oxide_emit/src/expr/this.rs b/crates/oxide_emit/src/expr/this.rs index d42e571..0c19be1 100644 --- a/crates/oxide_emit/src/expr/this.rs +++ b/crates/oxide_emit/src/expr/this.rs @@ -6,11 +6,11 @@ use oxide_ir::operand::Operand; use oxide_bytecode::opcode::OpCode; impl Emitter { - pub(crate) fn emit_this_expression(&self, ctx: &mut CompileCtx) -> Result { + pub(crate) fn emit_this_expression(&self, ctx: &mut CompileCtx) -> Result { let r = ctx.alloc_reg(); let src = ctx.static_block_this_reg.unwrap_or(254); let src_op = if src == 254 { Operand::This } else { Operand::Reg(src as u32) }; - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r as u32), src_op, Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r), src_op, Operand::None)); Ok(r) } } diff --git a/crates/oxide_emit/src/shared/binding.rs b/crates/oxide_emit/src/shared/binding.rs index d400c48..f9566fa 100644 --- a/crates/oxide_emit/src/shared/binding.rs +++ b/crates/oxide_emit/src/shared/binding.rs @@ -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 { + &self, val_reg: u32, default_expr: &Expression, ctx: &mut CompileCtx, + ) -> Result { 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 { + fn emit_collect_rest_array(&self, ctx: &mut CompileCtx) -> Result { 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(()) diff --git a/crates/oxide_emit/src/shared/chain.rs b/crates/oxide_emit/src/shared/chain.rs index 7bb0676..2378a95 100644 --- a/crates/oxide_emit/src/shared/chain.rs +++ b/crates/oxide_emit/src/shared/chain.rs @@ -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, 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, 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, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { 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, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { 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, ctx: &mut CompileCtx, - ) -> Result { + ) -> Result { 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 { + fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result { 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> { + &self, src_reg: u32, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx, + ) -> Result<(u32, Option), 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)) } } diff --git a/crates/oxide_emit/src/shared/class_field.rs b/crates/oxide_emit/src/shared/class_field.rs index 6343608..ed3381c 100644 --- a/crates/oxide_emit/src/shared/class_field.rs +++ b/crates/oxide_emit/src/shared/class_field.rs @@ -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(()) } } diff --git a/crates/oxide_emit/src/shared/class_key.rs b/crates/oxide_emit/src/shared/class_key.rs index c8ec33b..978a36b 100644 --- a/crates/oxide_emit/src/shared/class_key.rs +++ b/crates/oxide_emit/src/shared/class_key.rs @@ -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 { + pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result { 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 { + ) -> Result { 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 } } diff --git a/crates/oxide_emit/src/stmt/basic/break_continue.rs b/crates/oxide_emit/src/stmt/basic/break_continue.rs index e58c073..1109304 100644 --- a/crates/oxide_emit/src/stmt/basic/break_continue.rs +++ b/crates/oxide_emit/src/stmt/basic/break_continue.rs @@ -6,7 +6,7 @@ use oxide_ir::inst::Inst; impl Emitter { pub(crate) fn emit_break_statement( &self, stmt: &oxide_parser::BreakStatement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let break_label = if let Some(label) = &stmt.label { let name = label.name.as_str(); let scope = ctx @@ -25,7 +25,7 @@ impl Emitter { pub(crate) fn emit_continue_statement( &self, stmt: &oxide_parser::ContinueStatement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let continue_label = if let Some(label) = &stmt.label { let name = label.name.as_str(); let scope = ctx diff --git a/crates/oxide_emit/src/stmt/basic/expression.rs b/crates/oxide_emit/src/stmt/basic/expression.rs index dbf18fd..195890d 100644 --- a/crates/oxide_emit/src/stmt/basic/expression.rs +++ b/crates/oxide_emit/src/stmt/basic/expression.rs @@ -4,14 +4,14 @@ use crate::{CompileCtx, Emitter}; use oxide_parser::Statement; impl Emitter { - fn emit_expression_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + fn emit_expression_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ExpressionStatement(es) = stmt else { return Ok(None); }; Ok(Some(self.emit_expression(&es.expression, ctx)?)) } - pub(crate) fn emit_basic_expression(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_basic_expression(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { self.emit_expression_statement(stmt, ctx) } } diff --git a/crates/oxide_emit/src/stmt/basic/labeled.rs b/crates/oxide_emit/src/stmt/basic/labeled.rs index 7f6739e..0e7b957 100644 --- a/crates/oxide_emit/src/stmt/basic/labeled.rs +++ b/crates/oxide_emit/src/stmt/basic/labeled.rs @@ -17,7 +17,7 @@ impl Emitter { pub(crate) fn emit_labeled_statement( &self, stmt: &oxide_parser::LabeledStatement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let name = stmt.label.name.as_str(); if Self::is_iteration_statement(&stmt.body) { ctx.queue_loop_label(name)?; diff --git a/crates/oxide_emit/src/stmt/basic/mod.rs b/crates/oxide_emit/src/stmt/basic/mod.rs index 4542339..6eb4332 100644 --- a/crates/oxide_emit/src/stmt/basic/mod.rs +++ b/crates/oxide_emit/src/stmt/basic/mod.rs @@ -9,7 +9,7 @@ mod labeled; mod return_stmt; impl Emitter { - pub(crate) fn emit_basic_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_basic_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::ExpressionStatement(_) => self.emit_basic_expression(stmt, ctx), Statement::ReturnStatement(_) => self.emit_basic_return(stmt, ctx), diff --git a/crates/oxide_emit/src/stmt/basic/return_stmt.rs b/crates/oxide_emit/src/stmt/basic/return_stmt.rs index 3f1407a..373d336 100644 --- a/crates/oxide_emit/src/stmt/basic/return_stmt.rs +++ b/crates/oxide_emit/src/stmt/basic/return_stmt.rs @@ -7,14 +7,14 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Statement; impl Emitter { - fn emit_return_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + fn emit_return_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ReturnStatement(ret) = stmt else { return Ok(None); }; match &ret.argument { Some(expr) => { let r = self.emit_expression(expr, ctx)?; - ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(r as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(r), Operand::None, Operand::None)); } None => { ctx.inst(Inst::new(OpCode::RETURN, Operand::None, Operand::None, Operand::None)); @@ -23,7 +23,7 @@ impl Emitter { Ok(None) } - pub(crate) fn emit_basic_return(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_basic_return(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { self.emit_return_statement(stmt, ctx) } } diff --git a/crates/oxide_emit/src/stmt/block.rs b/crates/oxide_emit/src/stmt/block.rs index e228975..7c04d4f 100644 --- a/crates/oxide_emit/src/stmt/block.rs +++ b/crates/oxide_emit/src/stmt/block.rs @@ -4,7 +4,7 @@ use crate::{CompileCtx, Emitter}; use oxide_parser::Statement; impl Emitter { - fn emit_block_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + fn emit_block_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::BlockStatement(block) = stmt else { return Ok(None); }; @@ -19,7 +19,7 @@ impl Emitter { Ok(r) } - pub(crate) fn emit_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx), _ => Ok(None), diff --git a/crates/oxide_emit/src/stmt/control.rs b/crates/oxide_emit/src/stmt/control.rs index 0a6221b..64ca753 100644 --- a/crates/oxide_emit/src/stmt/control.rs +++ b/crates/oxide_emit/src/stmt/control.rs @@ -8,7 +8,7 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Statement; impl Emitter { - fn emit_if_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + fn emit_if_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::IfStatement(ifs) = stmt else { return Ok(None); }; @@ -22,10 +22,10 @@ impl Emitter { let cons_reg = self.emit_statement(&ifs.consequent, ctx)?; let result_reg = ctx.alloc_reg(); if let Some(r) = cons_reg { - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(r as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(r), Operand::None)); } else { let undef_idx = ctx.add_constant(Constant::Undefined); - ctx.inst(Inst::load_const(Operand::Reg(result_reg as u32), undef_idx)); + ctx.inst(Inst::load_const(Operand::Reg(result_reg), undef_idx)); } if ifs.alternate.is_some() { @@ -36,10 +36,10 @@ impl Emitter { if let Some(alt) = &ifs.alternate { let alt_reg = self.emit_statement(alt, ctx)?; if let Some(r) = alt_reg { - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(r as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(r), Operand::None)); } else { let undef_idx = ctx.add_constant(Constant::Undefined); - ctx.inst(Inst::load_const(Operand::Reg(result_reg as u32), undef_idx)); + ctx.inst(Inst::load_const(Operand::Reg(result_reg), undef_idx)); } } @@ -48,7 +48,7 @@ impl Emitter { Ok(Some(result_reg)) } - pub(crate) fn emit_control_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_control_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::IfStatement(_) => self.emit_if_statement(stmt, ctx), _ => Ok(None), diff --git a/crates/oxide_emit/src/stmt/declaration/class_decl.rs b/crates/oxide_emit/src/stmt/declaration/class_decl.rs index 47a7918..20b0262 100644 --- a/crates/oxide_emit/src/stmt/declaration/class_decl.rs +++ b/crates/oxide_emit/src/stmt/declaration/class_decl.rs @@ -9,7 +9,7 @@ use oxide_parser::{Statement, VariableDeclarationKind}; impl Emitter { pub(crate) fn emit_class_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let Statement::ClassDeclaration(class) = stmt else { return Err("ClassDeclaration without name".into()); }; @@ -23,9 +23,9 @@ impl Emitter { ctx.init_var(&name); let ctor_reg = self.emit_class(class, ctx)?; if let Some(&cell_idx) = ctx.captured_bindings.get(&name) { - ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(var_reg as u32), Operand::Imm(cell_idx as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(var_reg), Operand::Imm(cell_idx as u16), Operand::None)); } else { - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(ctor_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(ctor_reg), Operand::None)); } Ok(None) } diff --git a/crates/oxide_emit/src/stmt/declaration/function_decl.rs b/crates/oxide_emit/src/stmt/declaration/function_decl.rs index e37b651..53522da 100644 --- a/crates/oxide_emit/src/stmt/declaration/function_decl.rs +++ b/crates/oxide_emit/src/stmt/declaration/function_decl.rs @@ -9,7 +9,7 @@ use oxide_parser::Statement; impl Emitter { pub(crate) fn emit_function_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let Statement::FunctionDeclaration(fd) = stmt else { return Err("FunctionDeclaration without name".into()); }; @@ -36,11 +36,11 @@ impl Emitter { ctx.nested.push(sub_module); let var_reg = ctx.lookup(&name)?; ctx.reserve_reg(var_reg); - ctx.inst(Inst::create_closure(Operand::Reg(var_reg as u32), ctx.nested.len() as u16)); + ctx.inst(Inst::create_closure(Operand::Reg(var_reg), ctx.nested.len() as u16)); if let Some(&cell_idx) = ctx.captured_bindings.get(&name) { - ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(var_reg as u32), Operand::Imm(cell_idx as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(var_reg), Operand::Imm(cell_idx as u16), Operand::None)); } else { - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(var_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(var_reg), Operand::None)); } Ok(None) } diff --git a/crates/oxide_emit/src/stmt/declaration/mod.rs b/crates/oxide_emit/src/stmt/declaration/mod.rs index dbe8afc..f8b6237 100644 --- a/crates/oxide_emit/src/stmt/declaration/mod.rs +++ b/crates/oxide_emit/src/stmt/declaration/mod.rs @@ -8,7 +8,7 @@ mod function_decl; mod var; impl Emitter { - pub(crate) fn emit_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx), Statement::FunctionDeclaration(_) => self.emit_function_declaration_statement(stmt, ctx), diff --git a/crates/oxide_emit/src/stmt/declaration/var.rs b/crates/oxide_emit/src/stmt/declaration/var.rs index 71b5de9..c49a5c2 100644 --- a/crates/oxide_emit/src/stmt/declaration/var.rs +++ b/crates/oxide_emit/src/stmt/declaration/var.rs @@ -10,7 +10,7 @@ use oxide_parser::{BindingPattern, Expression, Statement, VariableDeclarationKin impl Emitter { pub(crate) fn emit_variable_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, - ) -> Result, String> { + ) -> Result, String> { let Statement::VariableDeclaration(decl) = stmt else { return Ok(None); }; @@ -37,7 +37,7 @@ impl Emitter { }; let idx = ctx.add_constant(Constant::Undefined); let tmp = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(tmp as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(tmp), idx)); let var_reg = ctx.alloc_reg(); let target_reg = if matches!(decl.kind, VariableDeclarationKind::Var) { match ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const) { @@ -49,9 +49,9 @@ impl Emitter { var_reg }; if let Some(&cell_idx) = ctx.captured_bindings.get(bi.name.as_str()) { - ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(tmp as u32), Operand::Imm(cell_idx as u16), Operand::None)); + ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(tmp), Operand::Imm(cell_idx as u16), Operand::None)); } else { - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg as u32), Operand::Reg(tmp as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg), Operand::Reg(tmp), Operand::None)); } ctx.init_var(bi.name.as_str()); r = Some(var_reg); diff --git a/crates/oxide_emit/src/stmt/exception/mod.rs b/crates/oxide_emit/src/stmt/exception/mod.rs index fde4d62..6b57b45 100644 --- a/crates/oxide_emit/src/stmt/exception/mod.rs +++ b/crates/oxide_emit/src/stmt/exception/mod.rs @@ -7,7 +7,7 @@ mod throw; mod try_catch; impl Emitter { - pub(crate) fn emit_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::ThrowStatement(_) => self.emit_throw_statement(stmt, ctx), Statement::TryStatement(_) => self.emit_try_statement(stmt, ctx), diff --git a/crates/oxide_emit/src/stmt/exception/throw.rs b/crates/oxide_emit/src/stmt/exception/throw.rs index c28b756..da19234 100644 --- a/crates/oxide_emit/src/stmt/exception/throw.rs +++ b/crates/oxide_emit/src/stmt/exception/throw.rs @@ -7,12 +7,12 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Statement; impl Emitter { - pub(crate) fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ThrowStatement(ts) = stmt else { return Ok(None); }; let exc_reg = self.emit_expression(&ts.argument, ctx)?; - ctx.inst(Inst::new(OpCode::THROW, Operand::Reg(exc_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::THROW, Operand::Reg(exc_reg), Operand::None, Operand::None)); Ok(None) } } diff --git a/crates/oxide_emit/src/stmt/exception/try_catch.rs b/crates/oxide_emit/src/stmt/exception/try_catch.rs index 1250095..765c29e 100644 --- a/crates/oxide_emit/src/stmt/exception/try_catch.rs +++ b/crates/oxide_emit/src/stmt/exception/try_catch.rs @@ -7,7 +7,7 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::{Statement, VariableDeclarationKind}; impl Emitter { - pub(crate) fn emit_try_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_try_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::TryStatement(ts) = stmt else { return Ok(None); }; @@ -23,13 +23,13 @@ impl Emitter { if has_catch { ctx.inst(Inst::try_begin(catch_label)); } - let mut last_try_result: Option = None; + let mut last_try_result: Option = None; for s in &ts.block.body { if let Some(r) = self.emit_statement(s, ctx)? { last_try_result = Some(r); } } - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(last_try_result.unwrap_or(result_reg) as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(last_try_result.unwrap_or(result_reg)), Operand::None)); if has_catch { ctx.inst(Inst::new(OpCode::TRY_END, Operand::None, Operand::None, Operand::None)); } @@ -45,27 +45,27 @@ impl Emitter { let catch_reg = ctx.alloc_reg(); if let oxide_parser::BindingPattern::BindingIdentifier(bi) = ¶m.pattern { ctx.declare_initialized(bi.name.as_str(), catch_reg, VariableDeclarationKind::Let, false)?; - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(catch_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(catch_reg), Operand::None, Operand::None)); } } - let mut last_catch_result: Option = None; + let mut last_catch_result: Option = None; for s in &catch.body.body { if let Some(r) = self.emit_statement(s, ctx)? { last_catch_result = Some(r); } } - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(last_catch_result.unwrap_or(result_reg) as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(last_catch_result.unwrap_or(result_reg)), Operand::None)); ctx.pop_scope(); } if has_finally { ctx.labels.set_label_pos(finally_label, ctx.insts.len()); - let mut last_finally_result: Option = None; + let mut last_finally_result: Option = None; for s in &ts.finalizer.as_ref().unwrap().body { if let Some(r) = self.emit_statement(s, ctx)? { last_finally_result = Some(r); } } - ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::Reg(last_finally_result.unwrap_or(result_reg) as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::Reg(last_finally_result.unwrap_or(result_reg)), Operand::None)); ctx.inst(Inst::new(OpCode::TRY_FINALLY_END, Operand::None, Operand::None, Operand::None)); } ctx.labels.set_label_pos(try_end_label, ctx.insts.len()); diff --git a/crates/oxide_emit/src/stmt/iteration/do_while.rs b/crates/oxide_emit/src/stmt/iteration/do_while.rs index 2ce2739..8be473f 100644 --- a/crates/oxide_emit/src/stmt/iteration/do_while.rs +++ b/crates/oxide_emit/src/stmt/iteration/do_while.rs @@ -5,7 +5,7 @@ use oxide_ir::inst::Inst; use oxide_parser::Statement; impl Emitter { - pub(crate) fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::DoWhileStatement(dw) = stmt else { return Ok(None); }; diff --git a/crates/oxide_emit/src/stmt/iteration/for_.rs b/crates/oxide_emit/src/stmt/iteration/for_.rs index fbc655c..f5029ca 100644 --- a/crates/oxide_emit/src/stmt/iteration/for_.rs +++ b/crates/oxide_emit/src/stmt/iteration/for_.rs @@ -8,7 +8,7 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::{BindingPattern, ForStatementInit, Statement, VariableDeclarationKind}; impl Emitter { - pub(crate) fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ForStatement(fr) = stmt else { return Ok(None); }; @@ -29,7 +29,7 @@ impl Emitter { } else if let BindingPattern::BindingIdentifier(bi) = &d.id { let idx = ctx.add_constant(Constant::Undefined); let tmp = ctx.alloc_reg(); - ctx.inst(Inst::load_const(Operand::Reg(tmp as u32), idx)); + ctx.inst(Inst::load_const(Operand::Reg(tmp), idx)); let var_reg = ctx.alloc_reg(); let target_reg = if matches!(decl.kind, VariableDeclarationKind::Var) { match ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const) { @@ -40,7 +40,7 @@ impl Emitter { ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?; var_reg }; - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg as u32), Operand::Reg(tmp as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg), Operand::Reg(tmp), Operand::None)); ctx.init_var(bi.name.as_str()); } } diff --git a/crates/oxide_emit/src/stmt/iteration/for_in.rs b/crates/oxide_emit/src/stmt/iteration/for_in.rs index 4e58803..2f1f14c 100644 --- a/crates/oxide_emit/src/stmt/iteration/for_in.rs +++ b/crates/oxide_emit/src/stmt/iteration/for_in.rs @@ -7,26 +7,26 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::{ForStatementLeft, Statement, VariableDeclarationKind}; impl Emitter { - pub(crate) fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ForInStatement(fi) = stmt else { return Ok(None); }; let start_label = ctx.next_label_id(); let end_label = ctx.next_label_id(); let obj_reg = self.emit_expression(&fi.right, ctx)?; - ctx.inst(Inst::new(OpCode::FOR_IN_INIT, Operand::None, Operand::Reg(obj_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::FOR_IN_INIT, Operand::None, Operand::Reg(obj_reg), Operand::None)); ctx.labels.set_label_pos(start_label, ctx.insts.len()); ctx.push_loop(end_label, start_label); let n_labeled = ctx.take_pending_loop_labels(end_label, start_label); let done_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::FOR_IN_DONE, Operand::Reg(done_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::FOR_IN_DONE, Operand::Reg(done_reg), Operand::None, Operand::None)); // done=true 表示迭代结束:done=false 时跳过 end jmp 继续迭代 let continue_label = ctx.next_label_id(); ctx.inst(Inst::jmp_if_false(done_reg, continue_label)); ctx.inst(Inst::jmp(end_label)); ctx.labels.set_label_pos(continue_label, ctx.insts.len()); let key_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::FOR_IN_NEXT, Operand::Reg(key_reg as u32), Operand::None, Operand::None)); + ctx.inst(Inst::new(OpCode::FOR_IN_NEXT, Operand::Reg(key_reg), Operand::None, Operand::None)); match &fi.left { ForStatementLeft::VariableDeclaration(decl) => { for d in &decl.declarations { @@ -36,14 +36,14 @@ impl Emitter { }; let var_reg = ctx.alloc_reg(); ctx.declare(name, var_reg, decl.kind, matches!(decl.kind, VariableDeclarationKind::Const))?; - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(key_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(key_reg), Operand::None)); ctx.init_var(name); } } ForStatementLeft::AssignmentTargetIdentifier(id_ref) => { let name = id_ref.name.as_str(); let var_reg = ctx.lookup_or_global(name); - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(key_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(key_reg), Operand::None)); } _ => return Err("unsupported for-in left-hand side".into()), } diff --git a/crates/oxide_emit/src/stmt/iteration/for_of.rs b/crates/oxide_emit/src/stmt/iteration/for_of.rs index 52f71eb..515ef5d 100644 --- a/crates/oxide_emit/src/stmt/iteration/for_of.rs +++ b/crates/oxide_emit/src/stmt/iteration/for_of.rs @@ -7,22 +7,22 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::{ForStatementLeft, Statement}; impl Emitter { - pub(crate) fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ForOfStatement(fo) = stmt else { return Ok(None); }; let start_label = ctx.next_label_id(); let end_label = ctx.next_label_id(); let iter_src_reg = self.emit_expression(&fo.right, ctx)?; - ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(iter_src_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(iter_src_reg), Operand::None)); ctx.labels.set_label_pos(start_label, ctx.insts.len()); ctx.push_loop(end_label, start_label); let n_labeled = ctx.take_pending_loop_labels(end_label, start_label); 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, end_label)); 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)); match &fo.left { ForStatementLeft::VariableDeclaration(decl) => { for d in &decl.declarations { @@ -32,7 +32,7 @@ impl Emitter { ForStatementLeft::AssignmentTargetIdentifier(id_ref) => { let name = id_ref.name.as_str(); let var_reg = ctx.lookup_or_global(name); - ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg as u32), Operand::Reg(val_reg as u32), Operand::None)); + ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(val_reg), Operand::None)); } ForStatementLeft::ArrayAssignmentTarget(ap) => { self.emit_array_assignment(ap, val_reg, ctx)?; diff --git a/crates/oxide_emit/src/stmt/iteration/mod.rs b/crates/oxide_emit/src/stmt/iteration/mod.rs index 260f912..903b10c 100644 --- a/crates/oxide_emit/src/stmt/iteration/mod.rs +++ b/crates/oxide_emit/src/stmt/iteration/mod.rs @@ -10,7 +10,7 @@ mod for_of; mod while_loop; impl Emitter { - pub(crate) fn emit_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx), Statement::DoWhileStatement(_) => self.emit_do_while_statement(stmt, ctx), diff --git a/crates/oxide_emit/src/stmt/iteration/while_loop.rs b/crates/oxide_emit/src/stmt/iteration/while_loop.rs index 8d096dd..1da1880 100644 --- a/crates/oxide_emit/src/stmt/iteration/while_loop.rs +++ b/crates/oxide_emit/src/stmt/iteration/while_loop.rs @@ -5,7 +5,7 @@ use oxide_ir::inst::Inst; use oxide_parser::Statement; impl Emitter { - pub(crate) fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::WhileStatement(wh) = stmt else { return Ok(None); }; diff --git a/crates/oxide_emit/src/stmt/switch.rs b/crates/oxide_emit/src/stmt/switch.rs index 1c4c5d0..b6dea5f 100644 --- a/crates/oxide_emit/src/stmt/switch.rs +++ b/crates/oxide_emit/src/stmt/switch.rs @@ -7,14 +7,13 @@ use oxide_bytecode::opcode::OpCode; use oxide_parser::Statement; impl Emitter { - fn emit_switch_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + fn emit_switch_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::SwitchStatement(sw) = stmt else { return Ok(None); }; let end_label = ctx.next_label_id(); ctx.push_switch(end_label); let disc_reg = self.emit_expression(&sw.discriminant, ctx)?; - let compare_reg_checkpoint = ctx.reg_checkpoint(); let cases = &sw.cases; let mut case_labels = Vec::with_capacity(cases.len()); for case in cases.iter() { @@ -23,9 +22,8 @@ impl Emitter { if let Some(test) = &case.test { let test_reg = self.emit_expression(test, ctx)?; let eq_reg = ctx.alloc_reg(); - ctx.inst(Inst::new(OpCode::EQ, Operand::Reg(eq_reg as u32), Operand::Reg(disc_reg as u32), Operand::Reg(test_reg as u32))); + ctx.inst(Inst::new(OpCode::EQ, Operand::Reg(eq_reg), Operand::Reg(disc_reg), Operand::Reg(test_reg))); ctx.inst(Inst::jmp_if_true(eq_reg, case_label)); - ctx.restore_reg_checkpoint(compare_reg_checkpoint); } } let has_default = cases.iter().any(|c| c.test.is_none()); @@ -44,7 +42,7 @@ impl Emitter { Ok(None) } - pub(crate) fn emit_switch_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { + pub(crate) fn emit_switch_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::SwitchStatement(_) => self.emit_switch_statement(stmt, ctx), _ => Ok(None), diff --git a/crates/oxide_emit/src/symbol_table.rs b/crates/oxide_emit/src/symbol_table.rs index 2010014..48177ea 100644 --- a/crates/oxide_emit/src/symbol_table.rs +++ b/crates/oxide_emit/src/symbol_table.rs @@ -20,7 +20,7 @@ pub(crate) struct Scope { } pub(crate) struct Binding { - pub(crate) reg: u8, + pub(crate) reg: u32, pub(crate) initialized: bool, pub(crate) is_const: bool, } @@ -79,7 +79,7 @@ impl SymbolTable { /// 声明绑定:`var` 提升到函数作用域,`let`/`const` 落在当前块。 /// 初始为未初始化态(TDZ);重复声明报错。 pub fn declare( - &mut self, name: &str, reg: u8, kind: VariableDeclarationKind, is_const: bool, + &mut self, name: &str, reg: u32, kind: VariableDeclarationKind, is_const: bool, ) -> Result<(), String> { let target_idx = if matches!(kind, VariableDeclarationKind::Var) { self.find_var_target_scope() @@ -103,7 +103,7 @@ impl SymbolTable { } /// 从内到外查找已初始化绑定;命中未初始化绑定报 TDZ 错误,未找到报未定义。 - pub fn lookup(&self, name: &str) -> Result { + pub fn lookup(&self, name: &str) -> Result { for scope in self.scopes.iter().rev() { if let Some(b) = scope.bindings.get(name) { if b.initialized { @@ -115,7 +115,7 @@ impl SymbolTable { Err(format!("Identifier '{name}' is not defined")) } - pub(crate) fn lookup_any(&self, name: &str) -> Option { + pub(crate) fn lookup_any(&self, name: &str) -> Option { self.scopes .iter() .rev() @@ -132,7 +132,7 @@ impl SymbolTable { } /// 查找或视为全局:未命中时以 `reg_for_new` 在全局作用域登记并返回(隐式全局)。 - pub fn lookup_or_global(&mut self, name: &str, reg_for_new: u8) -> u8 { + pub fn lookup_or_global(&mut self, name: &str, reg_for_new: u32) -> u32 { for scope in self.scopes.iter().rev() { if let Some(b) = scope.bindings.get(name) { return b.reg; @@ -164,7 +164,7 @@ impl SymbolTable { /// 声明并直接标记为已初始化;绑定已存在时仅补初始化标志(供预声明路径)。 pub fn declare_initialized( - &mut self, name: &str, reg: u8, kind: VariableDeclarationKind, is_const: bool, + &mut self, name: &str, reg: u32, kind: VariableDeclarationKind, is_const: bool, ) -> Result<(), String> { let target_idx = if matches!(kind, VariableDeclarationKind::Var) { self.find_var_target_scope() @@ -189,7 +189,7 @@ impl SymbolTable { } /// 在全局作用域预登记绑定(builtin 全局等),不覆盖已存在的同名绑定。 - pub fn pre_register_global(&mut self, name: &str, reg: u8) { + pub fn pre_register_global(&mut self, name: &str, reg: u32) { self.scopes[0].bindings.entry(name.to_string()).or_insert(Binding { reg, initialized: true, diff --git a/crates/oxide_ir/src/inst.rs b/crates/oxide_ir/src/inst.rs index 97cbb6a..a10b428 100644 --- a/crates/oxide_ir/src/inst.rs +++ b/crates/oxide_ir/src/inst.rs @@ -168,30 +168,30 @@ impl Inst { } /// 条件寄存器为 false 时跳转。 - pub fn jmp_if_false(cond_reg: u8, label: LabelId) -> Self { + pub fn jmp_if_false(cond_reg: u32, label: LabelId) -> Self { Self::new( OpCode::JMP_IF_FALSE, - Operand::Reg(cond_reg as u32), + Operand::Reg(cond_reg), Operand::None, Operand::Label(label), ) } /// 条件寄存器为 true 时跳转。 - pub fn jmp_if_true(cond_reg: u8, label: LabelId) -> Self { + pub fn jmp_if_true(cond_reg: u32, label: LabelId) -> Self { Self::new( OpCode::JMP_IF_TRUE, - Operand::Reg(cond_reg as u32), + Operand::Reg(cond_reg), Operand::None, Operand::Label(label), ) } /// 条件寄存器为 null/undefined 时跳转(`??` / 可选链短路)。 - pub fn jmp_if_nullish(cond_reg: u8, label: LabelId) -> Self { + pub fn jmp_if_nullish(cond_reg: u32, label: LabelId) -> Self { Self::new( OpCode::JMP_IF_NULLISH, - Operand::Reg(cond_reg as u32), + Operand::Reg(cond_reg), Operand::None, Operand::Label(label), ) diff --git a/crates/oxide_ir/src/lib.rs b/crates/oxide_ir/src/lib.rs index 0d94cf7..9d262a4 100644 --- a/crates/oxide_ir/src/lib.rs +++ b/crates/oxide_ir/src/lib.rs @@ -40,20 +40,18 @@ pub struct IRFunction { // bindings 域 pub param_layout: ParamLayout, // builtins 域 - pub builtin_reg_map: Vec<(String, u8)>, + pub builtin_reg_map: Vec<(String, u32)>, // closures 域 pub upvalue_captures: Vec, pub cells_needed: u8, // meta 域 - pub n_registers: u8, + pub n_registers: u32, pub is_arrow: bool, pub is_class_constructor: bool, pub is_derived_constructor: bool, pub needs_home_object: bool, pub captured_this_const_idx: u16, pub function_name: Option, - /// emit 侧溢出证据,lowering 读标志报错(D-20)。 - pub reg_overflow: bool, pub const_overflow: bool, // nested 域 pub nested: Vec, @@ -78,7 +76,6 @@ impl IRFunction { needs_home_object: false, captured_this_const_idx: 0, function_name: None, - reg_overflow: false, const_overflow: false, nested: Vec::new(), } diff --git a/crates/oxide_ir/src/lower.rs b/crates/oxide_ir/src/lower.rs index 92d5add..6341ccc 100644 --- a/crates/oxide_ir/src/lower.rs +++ b/crates/oxide_ir/src/lower.rs @@ -14,9 +14,6 @@ use crate::IRFunction; /// IRFunction → CompiledModule。错误消息与现状(退役前 compiler.rs)逐字一致。 pub fn lower(f: &IRFunction) -> Result { // 溢出检查 1:寄存器(仅 Operand::Reg 变体;This/NewTarget 是语义操作数,映射 254/255 合法) - if f.reg_overflow { - return Err("RangeError: function body uses too many registers (max 253)".into()); - } for inst in &f.insts { for o in [&inst.rd, &inst.a, &inst.b] { if let Operand::Reg(r) = o { @@ -27,6 +24,13 @@ pub fn lower(f: &IRFunction) -> Result { } } + // 溢出检查 1b:n_registers(u32 → CompiledModule u8 前的显式截断检查)。 + // n_registers = max_regs = 最高分配号 + 1,合法上限 254(reg 0..253)。 + // 纯参数/空体函数可能无指令引用最高号寄存器,逐指令检查漏掉,计数检查兜底防 u8 静默截断。 + if f.n_registers > 254 { + return Err("RangeError: function body uses too many registers (max 253)".into()); + } + // 溢出检查 2:常量池 if f.const_overflow || f.constants.len() > u16::MAX as usize { return Err("RangeError: too many constants".into()); @@ -72,7 +76,7 @@ pub fn lower(f: &IRFunction) -> Result { Ok(CompiledModule { bytecode: instrs, constants: f.constants.clone(), - n_registers: f.n_registers, + n_registers: f.n_registers as u8, n_args: f.param_layout.count as u8, param_base: f.param_layout.base as u8, builtin_reg_map: f.builtin_reg_map.clone(), diff --git a/crates/oxide_ir/tests/lowering_tests.rs b/crates/oxide_ir/tests/lowering_tests.rs index 810a465..a63cff5 100644 --- a/crates/oxide_ir/tests/lowering_tests.rs +++ b/crates/oxide_ir/tests/lowering_tests.rs @@ -188,10 +188,10 @@ fn module_fields_are_copied_through() { } #[test] -fn reg_overflow_flag_errors() { +fn n_registers_overflow_errors() { let mut f = base_module(); f.insts.push(Inst::new(OpCode::NOP, Operand::None, Operand::None, Operand::None)); - f.reg_overflow = true; + f.n_registers = 300; let err = lower_err(&f); assert!(err.contains("too many registers"), "unexpected error: {err}"); }