mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 18:51:13 +08:00
refactor: 原子切换 emit 产出符号化 IR
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use crate::ir::inst::Inst;
|
||||
use crate::ir::operand::Operand;
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_parser::{
|
||||
ArrayAssignmentTarget, ArrayPattern, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty,
|
||||
BindingPattern, Expression, ObjectAssignmentTarget, ObjectPattern, VariableDeclarationKind,
|
||||
@@ -12,15 +14,13 @@ impl Compiler {
|
||||
) -> Result<u8, String> {
|
||||
let undef_reg = self.emit_undefined(ctx);
|
||||
let eq_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::STRICT_EQ, eq_reg, val_reg, undef_reg));
|
||||
let jump_pos = ctx.bytecode.len();
|
||||
ctx.emit(opcode::encode_jmp_if_false(eq_reg, 0));
|
||||
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)));
|
||||
// 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.emit(opcode::encode(OpCode::LOAD_VAR, val_reg, default_reg, 0));
|
||||
let after = ctx.bytecode.len();
|
||||
let offset = after as isize - jump_pos as isize;
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[jump_pos] = opcode::encode_jmp_if_false(eq_reg, offset);
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(val_reg as u32), Operand::Reg(default_reg as u32), Operand::None));
|
||||
ctx.labels.set_label_pos(end_label, ctx.insts.len());
|
||||
Ok(val_reg)
|
||||
}
|
||||
|
||||
@@ -45,9 +45,9 @@ impl Compiler {
|
||||
if ctx.scopes.symbols.lookup_is_captured(name) {
|
||||
let cell_idx = ctx.scopes.cell_registry.len() as u8;
|
||||
ctx.scopes.cell_registry.push((name.to_string(), cell_idx));
|
||||
ctx.emit(opcode::encode(OpCode::MAKE_CELL, src_reg, cell_idx, 0));
|
||||
ctx.inst(Inst::new(OpCode::MAKE_CELL, Operand::Reg(src_reg as u32), Operand::Imm(cell_idx as u16), Operand::None));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, target_reg, src_reg, if is_const { 1 } else { 0 }));
|
||||
ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg as u32), Operand::Reg(src_reg as u32), Operand::Imm(if is_const { 1 } else { 0 })));
|
||||
}
|
||||
ctx.init_var(name);
|
||||
Ok(())
|
||||
@@ -60,11 +60,11 @@ impl Compiler {
|
||||
AssignmentTarget::AssignmentTargetIdentifier(id) => {
|
||||
let name = id.name.as_str();
|
||||
let var_reg = ctx.lookup_or_global(name);
|
||||
ctx.emit(opcode::encode(
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::STORE_VAR,
|
||||
var_reg,
|
||||
src_reg,
|
||||
if ctx.lookup_const_flag(name) { 1 } else { 0 },
|
||||
Operand::Reg(var_reg as u32),
|
||||
Operand::Reg(src_reg as u32),
|
||||
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
@@ -94,12 +94,12 @@ impl Compiler {
|
||||
fn emit_array_binding(
|
||||
&self, ap: &ArrayPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, src_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg as u32), Operand::None));
|
||||
for elem in &ap.elements {
|
||||
let has_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg as u32), Operand::None, Operand::None));
|
||||
if let Some(pattern) = elem {
|
||||
self.emit_binding_pattern(pattern, val_reg, kind, is_const, ctx)?;
|
||||
}
|
||||
@@ -108,33 +108,29 @@ impl Compiler {
|
||||
let rest_reg = self.emit_collect_rest_array(ctx)?;
|
||||
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_CLOSE, Operand::None, Operand::None, Operand::None));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn emit_collect_rest_array(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
let rest_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, rest_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(rest_reg as u32), Operand::None, Operand::None));
|
||||
let idx_reg = ctx.alloc_reg();
|
||||
let zero_idx = ctx.add_constant(Constant::Int(0));
|
||||
ctx.emit_load_const(idx_reg, zero_idx);
|
||||
let loop_start = ctx.bytecode.len();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(idx_reg as u32), 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.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
|
||||
let end_jmp = ctx.bytecode.len();
|
||||
ctx.emit(opcode::encode_jmp_if_false(has_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
|
||||
ctx.inst(Inst::jmp_if_false(has_reg, loop_end));
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
|
||||
ctx.emit(opcode::encode(OpCode::SET_ELEM, rest_reg, idx_reg, val_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)));
|
||||
let tmp_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::INC_PRE, idx_reg, tmp_reg, tmp_reg));
|
||||
let back = loop_start as isize - ctx.bytecode.len() as isize;
|
||||
let back = ctx.checked_jump_offset(back);
|
||||
ctx.emit(opcode::encode_jmp(back));
|
||||
let after = ctx.bytecode.len();
|
||||
let end_offset = after as isize - end_jmp as isize;
|
||||
let end_offset = ctx.checked_jump_offset(end_offset);
|
||||
ctx.bytecode[end_jmp] = opcode::encode_jmp_if_false(has_reg, end_offset);
|
||||
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::jmp(loop_start));
|
||||
ctx.labels.set_label_pos(loop_end, ctx.insts.len());
|
||||
Ok(rest_reg)
|
||||
}
|
||||
|
||||
@@ -152,8 +148,7 @@ impl Compiler {
|
||||
if let Some(rest) = &op.rest {
|
||||
let rest_reg = ctx.alloc_reg();
|
||||
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
|
||||
ctx.emit(opcode::encode(OpCode::REST_OBJECT, rest_reg, src_reg, 0));
|
||||
ctx.emit(excluded_idx as u32);
|
||||
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg as u32), Operand::Reg(src_reg as u32), excluded_idx as u32));
|
||||
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
|
||||
}
|
||||
Ok(())
|
||||
@@ -172,11 +167,11 @@ impl Compiler {
|
||||
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(id) => {
|
||||
let name = id.name.as_str();
|
||||
let var_reg = ctx.lookup_or_global(name);
|
||||
ctx.emit(opcode::encode(
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::STORE_VAR,
|
||||
var_reg,
|
||||
src_reg,
|
||||
if ctx.lookup_const_flag(name) { 1 } else { 0 },
|
||||
Operand::Reg(var_reg as u32),
|
||||
Operand::Reg(src_reg as u32),
|
||||
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
@@ -187,12 +182,12 @@ impl Compiler {
|
||||
pub(crate) fn emit_array_assignment(
|
||||
&self, ap: &ArrayAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, src_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_INIT, Operand::None, Operand::Reg(src_reg as u32), Operand::None));
|
||||
for elem in &ap.elements {
|
||||
let has_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_DONE, Operand::Reg(has_reg as u32), Operand::None, Operand::None));
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_NEXT, Operand::Reg(val_reg as u32), Operand::None, Operand::None));
|
||||
if let Some(target) = elem {
|
||||
self.emit_assignment_maybe_default(target, val_reg, ctx)?;
|
||||
}
|
||||
@@ -201,7 +196,7 @@ impl Compiler {
|
||||
let rest_reg = self.emit_collect_rest_array(ctx)?;
|
||||
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::FOR_OF_CLOSE, Operand::None, Operand::None, Operand::None));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -220,11 +215,11 @@ impl Compiler {
|
||||
}
|
||||
let name = id.binding.name.as_str();
|
||||
let var_reg = ctx.lookup_or_global(name);
|
||||
ctx.emit(opcode::encode(
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::STORE_VAR,
|
||||
var_reg,
|
||||
prop_reg,
|
||||
if ctx.lookup_const_flag(name) { 1 } else { 0 },
|
||||
Operand::Reg(var_reg as u32),
|
||||
Operand::Reg(prop_reg as u32),
|
||||
Operand::Imm(if ctx.lookup_const_flag(name) { 1 } else { 0 }),
|
||||
));
|
||||
}
|
||||
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
|
||||
@@ -240,8 +235,7 @@ impl Compiler {
|
||||
if let Some(rest) = &op.rest {
|
||||
let rest_reg = ctx.alloc_reg();
|
||||
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
|
||||
ctx.emit(opcode::encode(OpCode::REST_OBJECT, rest_reg, src_reg, 0));
|
||||
ctx.emit(excluded_idx as u32);
|
||||
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg as u32), Operand::Reg(src_reg as u32), excluded_idx as u32));
|
||||
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
use crate::compiler::{CompileCtx, Compiler, Label};
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use crate::ir::inst::Inst;
|
||||
use crate::ir::operand::{LabelId, Operand};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_parser::{ChainElement, Expression, LogicalOperator, PropertyKey};
|
||||
|
||||
impl Compiler {
|
||||
fn emit_optional_guard(&self, reg: u8, short_label: Label, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
fn emit_optional_guard(&self, reg: u8, short_label: LabelId, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
let dup_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, dup_reg, reg, 0));
|
||||
ctx.emit_jmp_if_nullish_labeled(dup_reg, short_label);
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(dup_reg as u32), Operand::Reg(reg as u32), 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<Label>, ctx: &mut CompileCtx,
|
||||
&self, member: &oxide_parser::StaticMemberExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
@@ -22,18 +24,15 @@ impl Compiler {
|
||||
}
|
||||
let idx = ctx.add_constant(Constant::String(member.property.name.as_str().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 value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, value_reg, obj_reg, 0));
|
||||
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, value_reg, key_reg));
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
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)));
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_computed_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
@@ -43,12 +42,12 @@ impl Compiler {
|
||||
}
|
||||
let key_reg = self.emit_expression(&member.expression, ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PROP_DYNAMIC, obj_reg, key_reg, value_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)));
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_chain_call(
|
||||
&self, call: &oxide_parser::CallExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
&self, call: &oxide_parser::CallExpression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
let (callee_reg, this_reg) = match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
@@ -66,7 +65,7 @@ impl Compiler {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let callee_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, callee_reg, obj_reg, key_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)));
|
||||
(callee_reg, obj_reg)
|
||||
}
|
||||
_ => {
|
||||
@@ -78,7 +77,7 @@ impl Compiler {
|
||||
}
|
||||
let this_idx = ctx.add_constant(Constant::Undefined);
|
||||
let this_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(this_reg, this_idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(this_reg as u32), this_idx));
|
||||
(callee_reg, this_reg)
|
||||
}
|
||||
};
|
||||
@@ -105,15 +104,32 @@ impl Compiler {
|
||||
Expression::Identifier(ident) if ctx.is_builtin(ident.name.as_str()) => OpCode::CALL_NATIVE,
|
||||
_ => OpCode::CALL,
|
||||
};
|
||||
ctx.emit(opcode::encode(op, callee_reg, this_reg, first_arg_reg));
|
||||
ctx.emit(arg_regs.len() as u32);
|
||||
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),
|
||||
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),
|
||||
arg_regs.len() as u8,
|
||||
));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
let result_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg as u32), Operand::None, Operand::None));
|
||||
Ok(result_reg)
|
||||
}
|
||||
|
||||
fn emit_chainable_expression(
|
||||
&self, expr: &Expression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
&self, expr: &Expression, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
@@ -133,7 +149,7 @@ impl Compiler {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, value_reg, obj_reg, key_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)));
|
||||
Ok(value_reg)
|
||||
}
|
||||
Expression::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
@@ -143,7 +159,7 @@ impl Compiler {
|
||||
}
|
||||
|
||||
pub(crate) fn emit_chain_element(
|
||||
&self, element: &ChainElement, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
&self, element: &ChainElement, short_label: Option<LabelId>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
match element {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
@@ -163,7 +179,7 @@ impl Compiler {
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, value_reg, obj_reg, key_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)));
|
||||
Ok(value_reg)
|
||||
}
|
||||
ChainElement::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
@@ -172,14 +188,14 @@ impl Compiler {
|
||||
}
|
||||
|
||||
pub(crate) fn emit_logical_assign_test(
|
||||
&self, op: LogicalOperator, test_reg: u8, store_label: Label, end_label: Label, ctx: &mut CompileCtx,
|
||||
&self, op: LogicalOperator, test_reg: u8, store_label: LabelId, end_label: LabelId, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
match op {
|
||||
LogicalOperator::And => ctx.emit_jmp_if_false_labeled(test_reg, end_label),
|
||||
LogicalOperator::Or => ctx.emit_jmp_if_true_labeled(test_reg, end_label),
|
||||
LogicalOperator::And => ctx.inst(Inst::jmp_if_false(test_reg, end_label)),
|
||||
LogicalOperator::Or => ctx.inst(Inst::jmp_if_true(test_reg, end_label)),
|
||||
LogicalOperator::Coalesce => {
|
||||
ctx.emit_jmp_if_nullish_labeled(test_reg, store_label);
|
||||
ctx.emit_jmp_labeled(end_label);
|
||||
ctx.inst(Inst::jmp_if_nullish(test_reg, store_label));
|
||||
ctx.inst(Inst::jmp(end_label));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -187,14 +203,11 @@ impl Compiler {
|
||||
|
||||
pub(crate) fn emit_object_property_read(&self, src_reg: u8, key: &str, ctx: &mut CompileCtx) -> u8 {
|
||||
let prop_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg as u32), Operand::Reg(src_reg as u32), Operand::None));
|
||||
let key_idx = ctx.add_constant(Constant::String(key.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(key_reg, key_idx);
|
||||
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, prop_reg, key_reg));
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
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)));
|
||||
prop_reg
|
||||
}
|
||||
|
||||
@@ -204,25 +217,25 @@ impl Compiler {
|
||||
let name = ident.name.as_str();
|
||||
let var_reg = ctx.lookup_or_builtin(name)?;
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, key_reg, var_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(key_reg as u32), Operand::Reg(var_reg as u32), 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.emit_load_const(key_reg, key_idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), 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.emit_load_const(key_reg, key_idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), 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.emit_load_const(key_reg, key_idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(key_reg as u32), key_idx));
|
||||
Ok(key_reg)
|
||||
}
|
||||
_ => Err("computed destructuring key expression not supported".into()),
|
||||
@@ -238,10 +251,10 @@ impl Compiler {
|
||||
return Ok((prop_reg, Some(key_name)));
|
||||
}
|
||||
let prop_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
|
||||
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg as u32), Operand::Reg(src_reg as u32), Operand::None));
|
||||
let key_reg = self.emit_property_key_expression(key, ctx)?;
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PROP_DYNAMIC, prop_reg, key_reg, val_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)));
|
||||
Ok((val_reg, None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use crate::ir::inst::Inst;
|
||||
use crate::ir::operand::Operand;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_parser::{Expression, PropertyKey};
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_public_field_init(
|
||||
&self, target_reg: u8, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
&self, target: Operand, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let key_reg = self.emit_class_key_reg(key, computed, ctx)?;
|
||||
let value_reg = if let Some(expr) = value {
|
||||
@@ -13,15 +15,15 @@ impl Compiler {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
if computed {
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, target_reg, key_reg, value_reg));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, target, Operand::Reg(key_reg as u32), Operand::Reg(value_reg as u32)));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP, target_reg, value_reg, key_reg));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, target, Operand::Reg(value_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_field_init(
|
||||
&self, target_reg: u8, name: &str, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
&self, target: Operand, name: &str, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let key_reg = self.emit_private_id_reg(name, ctx)?;
|
||||
let value_reg = if let Some(expr) = value {
|
||||
@@ -29,12 +31,12 @@ impl Compiler {
|
||||
} else {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
ctx.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, value_reg, key_reg));
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(value_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_method_init(
|
||||
&self, target_reg: u8, method: &oxide_parser::MethodDefinition, home_reg: u8, ctx: &mut CompileCtx,
|
||||
&self, target: Operand, method: &oxide_parser::MethodDefinition, home_reg: Operand, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let PropertyKey::PrivateIdentifier(private) = &method.key else {
|
||||
return Err("expected private method key".into());
|
||||
@@ -42,7 +44,7 @@ impl Compiler {
|
||||
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.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, method_reg, key_reg));
|
||||
ctx.inst(Inst::new(OpCode::INIT_PRIVATE, target, Operand::Reg(method_reg as u32), Operand::Reg(key_reg as u32)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use crate::ir::inst::Inst;
|
||||
use crate::ir::operand::Operand;
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_parser::PropertyKey;
|
||||
|
||||
@@ -15,7 +17,7 @@ impl Compiler {
|
||||
let id = self.private_name_id(name, ctx)?;
|
||||
let idx = ctx.add_constant(Constant::Int(id as i32));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
Ok(reg)
|
||||
}
|
||||
|
||||
@@ -26,7 +28,7 @@ impl Compiler {
|
||||
let name = self.class_property_name(key)?;
|
||||
let idx = ctx.add_constant(Constant::String(name));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
return Ok(reg);
|
||||
}
|
||||
|
||||
@@ -60,7 +62,7 @@ impl Compiler {
|
||||
pub(crate) fn emit_undefined(&self, ctx: &mut CompileCtx) -> u8 {
|
||||
let idx = ctx.add_constant(Constant::Undefined);
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
ctx.inst(Inst::load_const(Operand::Reg(reg as u32), idx));
|
||||
reg
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user