mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 03:01:12 +08:00
refactor: 寄存器号 u8→u32 全链放宽,vreg 化去 checkpoint/reg_overflow
This commit is contained in:
@@ -7,7 +7,7 @@ mod tagged;
|
||||
mod template_literal;
|
||||
|
||||
impl Emitter {
|
||||
pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||
match expr {
|
||||
Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx),
|
||||
Expression::TaggedTemplateExpression(tt) => self.emit_tagged_template_expression(tt, ctx),
|
||||
|
||||
@@ -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<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<u8, String> {
|
||||
) -> Result<u32, String> {
|
||||
let r = ctx.alloc_reg();
|
||||
let quasis = &tl.quasis;
|
||||
let expressions = &tl.expressions;
|
||||
let segment_count = quasis.len() + expressions.len();
|
||||
|
||||
let expr_regs: Vec<u8> = expressions
|
||||
let expr_regs: Vec<u32> = expressions
|
||||
.iter()
|
||||
.map(|e| self.emit_expression(e, ctx))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user