refactor(compiler): 原子切换 emit 产出符号化 IR

This commit is contained in:
爱特每羊队
2026-08-05 17:52:42 +08:00
parent 097153ec08
commit 1c328f5081
38 changed files with 720 additions and 866 deletions
@@ -1,15 +1,15 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::{
module::Constant,
opcode::{self, OpCode},
};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
impl Compiler {
pub(crate) fn emit_array_expression(
&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let arr_reg = ctx.alloc_reg();
let n = arr.elements.len() as u16;
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, arr_reg, (n & 0xFF) as u8, ((n >> 8) & 0xFF) as u8));
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(arr_reg as u32), Operand::Imm(n), Operand::None));
let elem_checkpoint = ctx.reg_checkpoint();
for (i, elem) in arr.elements.iter().enumerate() {
let Some(e) = elem.as_expression() else {
@@ -18,8 +18,8 @@ impl Compiler {
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.emit_load_const(idx_reg, idx);
ctx.emit(opcode::encode(OpCode::SET_ELEM, arr_reg, idx_reg, val_reg));
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);
}
Ok(arr_reg)
@@ -1,8 +1,8 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::{
module::Constant,
opcode::{self, OpCode},
};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind};
impl Compiler {
@@ -10,7 +10,7 @@ impl Compiler {
&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let obj_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, obj_reg, 0, 0));
ctx.inst(Inst::new(OpCode::NEW_OBJECT, Operand::Reg(obj_reg as u32), Operand::None, Operand::None));
let prop_checkpoint = ctx.reg_checkpoint();
for prop in &obj.properties {
let ObjectPropertyKind::ObjectProperty(p) = prop else {
@@ -24,7 +24,7 @@ impl Compiler {
match p.kind {
PropertyKind::Get | PropertyKind::Set => {
let accessor_reg = self.emit_expression(&p.value, ctx)?;
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
if let Some(sub_mod) = ctx.nested.last_mut() {
sub_mod.function_name = Some(prop_name.to_string());
}
let undef_reg = self.emit_undefined(ctx);
@@ -34,21 +34,25 @@ impl Compiler {
(undef_reg, accessor_reg)
};
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
ctx.emit(opcode::encode(OpCode::DEFINE_ACCESSOR, obj_reg, get_reg, set_reg));
ctx.emit(idx as u32);
ctx.inst(Inst::define_accessor(
Operand::Reg(obj_reg as u32),
Operand::Reg(get_reg as u32),
Operand::Reg(set_reg as u32),
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.emit_load_const(key_reg, idx);
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), idx));
let val_reg = self.emit_expression(&p.value, ctx)?;
if matches!(&p.value, Expression::ArrowFunctionExpression(_)) {
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
if let Some(sub_mod) = ctx.nested.last_mut() {
sub_mod.function_name = Some(prop_name.to_string());
}
}
ctx.emit(opcode::encode(OpCode::SET_PROP, obj_reg, val_reg, key_reg));
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);
}
}