mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 02:31:13 +08:00
refactor: 拆分 bytecode/cache crate 并模块化 counter
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::module::CompiledModule;
|
||||
use crate::opcode::{self, OpCode};
|
||||
use crate::symbol_table::{Binding, SymbolTable};
|
||||
use oxide_bytecode::module::CompiledModule;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
|
||||
pub use crate::hash::{compiled_module_hash, structural_hash};
|
||||
pub use crate::module::Constant;
|
||||
use crate::symbol_table::ScopeKind;
|
||||
pub use oxide_bytecode::module::Constant;
|
||||
pub use oxide_parser::VariableDeclarationKind;
|
||||
pub use oxide_parser::{AssignmentOperator, BinaryOperator, Expression, Statement, UnaryOperator};
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,434 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn count_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::BinaryExpression(bin) => {
|
||||
let checkpoint = ctx.reg_checkpoint();
|
||||
self.count_expression(&bin.left, ctx);
|
||||
self.count_expression(&bin.right, ctx);
|
||||
ctx.projected_pc += 1; // ADD/SUB/MUL/DIV/etc.
|
||||
if is_side_effect_free(&bin.left) && is_side_effect_free(&bin.right) {
|
||||
ctx.restore_reg_checkpoint(checkpoint.saturating_add(1));
|
||||
}
|
||||
}
|
||||
Expression::PrivateInExpression(pin) => {
|
||||
self.count_expression(&pin.right, ctx);
|
||||
ctx.count_private_access();
|
||||
}
|
||||
Expression::UnaryExpression(un) => {
|
||||
match un.operator {
|
||||
UnaryOperator::Delete => {
|
||||
match &un.argument {
|
||||
Expression::Identifier(_) => {
|
||||
// SyntaxError at compile time, no bytecode cost
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_delete_static();
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.projected_pc += 1; // DELETE_PROP_DYNAMIC
|
||||
}
|
||||
Expression::ChainExpression(chain) => {
|
||||
match &chain.expression {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
if member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_delete_static();
|
||||
}
|
||||
ChainElement::ComputedMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
if member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.projected_pc += 1; // DELETE_PROP_DYNAMIC
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
ctx.projected_pc += 1; // JMP over nullish true writer
|
||||
ctx.projected_pc += 1; // LOAD_CONST true
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.count_expression(&un.argument, ctx);
|
||||
ctx.projected_pc += 1; // NEG/TYPEOF/VOID/NOT
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::CallExpression(call) => {
|
||||
if matches!(&call.callee, Expression::Super(_)) {
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.count_call_instr_with_arg_ext(); // SUPER_CALL + arg_count ext word
|
||||
return;
|
||||
}
|
||||
match &call.callee {
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_private_access();
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
if matches!(&member.object, Expression::Super(_)) {
|
||||
ctx.count_load_var(); // this register
|
||||
ctx.count_load_const(); // key
|
||||
ctx.alloc_reg(); // callee
|
||||
ctx.count_instr(); // SUPER_GET_PROP
|
||||
} else {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_load_var(); // callee object copy
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.count_expression(&call.callee, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.count_call_instr_with_arg_ext(); // CALL/CALL_NATIVE + arg_count ext word
|
||||
ctx.count_load_var(); // result <- regs[0]
|
||||
}
|
||||
Expression::AssignmentExpression(assign) => {
|
||||
if let oxide_parser::AssignmentTarget::StaticMemberExpression(member) = &assign.left {
|
||||
if let Some(logical_op) = assign.operator.to_logical_operator() {
|
||||
let id = ctx.next_label_id();
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_load_var(); // current value copy
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext
|
||||
self.count_logical_assign_test(logical_op, id, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.count_ic_set_with_ext(); // IC_SET_PROP + 3 ext
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
|
||||
ctx.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
|
||||
return;
|
||||
}
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
if assign.operator != AssignmentOperator::Assign {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_ic_set_with_ext(); // IC_SET_PROP or COMPOUND_MEMBER_* + 3 ext words
|
||||
} else if let oxide_parser::AssignmentTarget::ComputedMemberExpression(member) = &assign.left {
|
||||
if let Some(logical_op) = assign.operator.to_logical_operator() {
|
||||
let id = ctx.next_label_id();
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // GET_PROP_DYNAMIC
|
||||
self.count_logical_assign_test(logical_op, id, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.projected_pc += 1; // SET_PROP_DYNAMIC
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
|
||||
ctx.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
|
||||
return;
|
||||
}
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&member.expression, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // SET_PROP_DYNAMIC
|
||||
} else if let oxide_parser::AssignmentTarget::PrivateFieldExpression(member) = &assign.left {
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.count_load_const(); // private id
|
||||
ctx.count_instr(); // SET_PRIVATE
|
||||
} else if let oxide_parser::AssignmentTarget::AssignmentTargetIdentifier(_) = &assign.left {
|
||||
if let Some(logical_op) = assign.operator.to_logical_operator() {
|
||||
let id = ctx.next_label_id();
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- current var
|
||||
self.count_logical_assign_test(logical_op, id, ctx);
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.projected_pc += 1; // STORE_VAR
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
|
||||
ctx.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
|
||||
} else {
|
||||
self.count_expression(&assign.right, ctx);
|
||||
if assign.operator != AssignmentOperator::Assign {
|
||||
ctx.projected_pc += 1; // COMPOUND_* on var
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // STORE_VAR
|
||||
}
|
||||
}
|
||||
} else if let oxide_parser::AssignmentTarget::ArrayAssignmentTarget(ap) = &assign.left {
|
||||
self.count_expression(&assign.right, ctx);
|
||||
self.count_array_assignment(ap, ctx);
|
||||
} else if let oxide_parser::AssignmentTarget::ObjectAssignmentTarget(op) = &assign.left {
|
||||
self.count_expression(&assign.right, ctx);
|
||||
self.count_object_assignment(op, ctx);
|
||||
} else {
|
||||
self.count_expression(&assign.right, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
Expression::ConditionalExpression(cond) => {
|
||||
let id = ctx.next_label_id();
|
||||
let else_label = Label::TernaryElse(id);
|
||||
let end_label = Label::TernaryEnd(id);
|
||||
|
||||
self.count_expression(&cond.test, ctx);
|
||||
ctx.projected_pc += 1; // JMP_IF_FALSE
|
||||
self.count_expression(&cond.consequent, ctx);
|
||||
ctx.alloc_reg(); // result register
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- consequent
|
||||
ctx.projected_pc += 1; // JMP to end
|
||||
ctx.label_map.insert(else_label, ctx.projected_pc);
|
||||
self.count_expression(&cond.alternate, ctx);
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- alternate
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
Expression::SequenceExpression(seq) => {
|
||||
for e in &seq.expressions {
|
||||
self.count_expression(e, ctx);
|
||||
}
|
||||
}
|
||||
Expression::LogicalExpression(log) => {
|
||||
self.count_expression(&log.left, ctx);
|
||||
|
||||
let is_simple = is_side_effect_free(&log.left) && is_side_effect_free(&log.right);
|
||||
|
||||
if is_simple {
|
||||
self.count_expression(&log.right, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // AND/OR
|
||||
} else {
|
||||
use oxide_parser::LogicalOperator;
|
||||
let id = ctx.next_label_id();
|
||||
ctx.alloc_reg(); // dup register
|
||||
ctx.projected_pc += 1; // LOAD_VAR (DUP)
|
||||
ctx.projected_pc += 1; // JMP_IF_FALSE, JMP_IF_TRUE, or JMP_IF_NULLISH
|
||||
if matches!(log.operator, LogicalOperator::Coalesce) {
|
||||
ctx.projected_pc += 1; // JMP over RHS on non-nullish
|
||||
ctx.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
|
||||
}
|
||||
self.count_expression(&log.right, ctx);
|
||||
ctx.projected_pc += 1; // LOAD_VAR (overwrite)
|
||||
let skip_label = match log.operator {
|
||||
LogicalOperator::And => Label::TernaryEnd(id),
|
||||
LogicalOperator::Or => Label::TernaryElse(id),
|
||||
LogicalOperator::Coalesce => Label::TernaryEnd(id),
|
||||
};
|
||||
ctx.label_map.insert(skip_label, ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
Expression::ChainExpression(chain) => {
|
||||
let id = ctx.next_label_id();
|
||||
self.count_chain_element(&chain.expression, true, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- chain value
|
||||
ctx.projected_pc += 1; // JMP over short-circuit writer
|
||||
ctx.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
|
||||
ctx.projected_pc += 1; // LOAD_CONST undefined
|
||||
ctx.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
|
||||
}
|
||||
Expression::ObjectExpression(obj) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // NEW_OBJECT
|
||||
let prop_checkpoint = ctx.reg_checkpoint();
|
||||
for prop in &obj.properties {
|
||||
if let oxide_parser::ObjectPropertyKind::ObjectProperty(p) = prop {
|
||||
if matches!(p.kind, oxide_parser::PropertyKind::Get | oxide_parser::PropertyKind::Set) {
|
||||
self.count_expression(&p.value, ctx);
|
||||
ctx.count_load_const(); // undefined getter/setter placeholder
|
||||
ctx.count_define_accessor(); // DEFINE_ACCESSOR + ext
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST key
|
||||
self.count_expression(&p.value, ctx);
|
||||
ctx.projected_pc += 1; // SET_PROP
|
||||
}
|
||||
ctx.restore_reg_checkpoint(prop_checkpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::TemplateLiteral(tl) => {
|
||||
// TemplateLiteral: N quasis (strings) + M expressions interleaved.
|
||||
// Allocate 1 result reg + count expressions.
|
||||
for expr in &tl.expressions {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
let segment_count = tl.quasis.len() + tl.expressions.len();
|
||||
ctx.count_template_str(segment_count);
|
||||
}
|
||||
Expression::TaggedTemplateExpression(tt) => {
|
||||
// Tagged template: tag expr + quasis as LOAD_CONST + expression args + CALL
|
||||
// Consecutive arg registers at end (via LOAD_VAR copies)
|
||||
self.count_expression(&tt.tag, ctx);
|
||||
let quasi_count = tt.quasi.quasis.len();
|
||||
// Each quasi: LOAD_CONST string + LOAD_CONST index + SET_ELEM
|
||||
// for both cooked and raw arrays
|
||||
for _ in 0..quasi_count {
|
||||
ctx.count_load_const(); // string
|
||||
ctx.count_load_const(); // index
|
||||
ctx.count_instr(); // SET_ELEM (cooked)
|
||||
}
|
||||
// Cooked array: alloc + NEW_ARRAY
|
||||
ctx.alloc_reg(); // cooked_temp
|
||||
ctx.count_instr(); // NEW_ARRAY
|
||||
|
||||
for _ in 0..quasi_count {
|
||||
ctx.count_load_const(); // string
|
||||
ctx.count_load_const(); // index
|
||||
ctx.count_instr(); // SET_ELEM (raw)
|
||||
}
|
||||
// Raw array: alloc + NEW_ARRAY
|
||||
ctx.alloc_reg(); // raw_temp
|
||||
ctx.count_instr(); // NEW_ARRAY
|
||||
|
||||
// Count expression arguments
|
||||
for expr in &tt.quasi.expressions {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
|
||||
// Consecutive arg slots: cooked_slot, raw_slot, N expr_slots
|
||||
ctx.alloc_reg(); // cooked_slot
|
||||
ctx.alloc_reg(); // raw_slot
|
||||
for _ in &tt.quasi.expressions {
|
||||
ctx.alloc_reg(); // expr_slot
|
||||
ctx.count_instr(); // LOAD_VAR
|
||||
}
|
||||
// LOAD_VAR for cooked and raw
|
||||
ctx.count_words(2);
|
||||
|
||||
// undefined this arg
|
||||
ctx.count_load_const();
|
||||
|
||||
// CALL + ext word
|
||||
ctx.count_call_instr_with_arg_ext();
|
||||
|
||||
// Result reg + LOAD_VAR
|
||||
ctx.count_load_var();
|
||||
}
|
||||
Expression::ArrowFunctionExpression(_arrow) => {
|
||||
// Arrow functions: alloc 1 register for the function value.
|
||||
// Body is compiled in the emit pass only (same pattern as FE).
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST
|
||||
}
|
||||
Expression::FunctionExpression(_fe) => {
|
||||
// No hoisting - function created at expression position.
|
||||
// Body is compiled in the emit pass only.
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST
|
||||
}
|
||||
Expression::ClassExpression(class) => {
|
||||
self.count_class(class, ctx);
|
||||
}
|
||||
Expression::NewExpression(ne) => {
|
||||
// Count callee expression
|
||||
self.count_expression(&ne.callee, ctx);
|
||||
// Count arguments
|
||||
for arg in &ne.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.alloc_reg(); // result register
|
||||
ctx.count_instr_with_ext(1); // NEW_EXPRESSION + arg_count ext word
|
||||
}
|
||||
Expression::ArrayExpression(arr) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // NEW_ARRAY
|
||||
let elem_checkpoint = ctx.reg_checkpoint();
|
||||
for elem in &arr.elements {
|
||||
if let Some(e) = elem.as_expression() {
|
||||
self.count_expression(e, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST index
|
||||
ctx.projected_pc += 1; // SET_ELEM
|
||||
ctx.restore_reg_checkpoint(elem_checkpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
if matches!(&member.object, Expression::Super(_)) {
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_load_var(); // this
|
||||
ctx.alloc_reg(); // result
|
||||
ctx.count_instr(); // SUPER_GET_PROP
|
||||
return;
|
||||
}
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // GET_PROP_DYNAMIC
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_private_access();
|
||||
}
|
||||
Expression::ParenthesizedExpression(p) => {
|
||||
self.count_expression(&p.expression, ctx);
|
||||
}
|
||||
Expression::ThisExpression(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_VAR from reg 254
|
||||
}
|
||||
Expression::Identifier(ident) => {
|
||||
if CompileCtx::is_known_builtin(ident.name.as_str()) {
|
||||
let _ = ctx.lookup_or_builtin(ident.name.as_str());
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_VAR
|
||||
}
|
||||
Expression::UpdateExpression(update) => match &update.argument {
|
||||
SimpleAssignmentTarget::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
SimpleAssignmentTarget::StaticMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.count_load_const(); // key
|
||||
ctx.alloc_reg();
|
||||
ctx.count_ic_instr_with_ext(); // MEMBER_INC/MEMBER_DEC + 3 ext words
|
||||
}
|
||||
SimpleAssignmentTarget::ComputedMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
_ => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
},
|
||||
Expression::RegExpLiteral(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
_ => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST or LOAD_VAR
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_binding_pattern(
|
||||
&self, pattern: &oxide_parser::BindingPattern, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match pattern {
|
||||
oxide_parser::BindingPattern::BindingIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
oxide_parser::BindingPattern::ArrayPattern(ap) => {
|
||||
ctx.count_instr(); // FOR_OF_INIT
|
||||
for elem in &ap.elements {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
if let Some(inner) = elem {
|
||||
self.count_binding_pattern(inner, ctx);
|
||||
}
|
||||
}
|
||||
if let Some(rest) = &ap.rest {
|
||||
self.count_rest_array(ctx);
|
||||
self.count_binding_pattern(&rest.argument, ctx);
|
||||
}
|
||||
ctx.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
oxide_parser::BindingPattern::ObjectPattern(op) => {
|
||||
for prop in &op.properties {
|
||||
self.count_object_property_read_key(&prop.key, prop.computed, ctx);
|
||||
self.count_binding_pattern(&prop.value, ctx);
|
||||
}
|
||||
if let Some(rest) = &op.rest {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
|
||||
self.count_binding_pattern(&rest.argument, ctx);
|
||||
}
|
||||
}
|
||||
oxide_parser::BindingPattern::AssignmentPattern(ap) => {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(&ap.right, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
self.count_binding_pattern(&ap.left, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_rest_array(&self, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // rest
|
||||
ctx.count_instr(); // NEW_ARRAY
|
||||
ctx.count_load_const(); // idx = 0
|
||||
ctx.alloc_reg(); // has
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
ctx.alloc_reg(); // val
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
ctx.count_instr(); // SET_ELEM
|
||||
ctx.alloc_reg(); // inc tmp
|
||||
ctx.count_instr(); // INC_PRE
|
||||
ctx.count_jump(); // JMP
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_array_assignment(
|
||||
&self, ap: &oxide_parser::ArrayAssignmentTarget, ctx: &mut CompileCtx,
|
||||
) {
|
||||
ctx.count_instr(); // FOR_OF_INIT
|
||||
for elem in &ap.elements {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
if elem.is_some() {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
}
|
||||
if ap.rest.is_some() {
|
||||
self.count_rest_array(ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
ctx.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_object_assignment(
|
||||
&self, op: &oxide_parser::ObjectAssignmentTarget, ctx: &mut CompileCtx,
|
||||
) {
|
||||
for prop in &op.properties {
|
||||
match prop {
|
||||
oxide_parser::AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
|
||||
self.count_object_property_read_static(ctx);
|
||||
if let Some(default_expr) = &id.init {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(default_expr, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
oxide_parser::AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
|
||||
self.count_object_property_read_key(&prop.name, prop.computed, ctx);
|
||||
self.count_assignment_maybe_default(&prop.binding, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
if op.rest.is_some() {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_assignment_maybe_default(
|
||||
&self, target: &oxide_parser::AssignmentTargetMaybeDefault, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match target {
|
||||
oxide_parser::AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(&default.init, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
self.count_assign_target(&default.binding, ctx);
|
||||
}
|
||||
oxide_parser::AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => {
|
||||
self.count_array_assignment(ap, ctx)
|
||||
}
|
||||
oxide_parser::AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => {
|
||||
self.count_object_assignment(op, ctx)
|
||||
}
|
||||
oxide_parser::AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_assign_target(
|
||||
&self, target: &oxide_parser::AssignmentTarget, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match target {
|
||||
oxide_parser::AssignmentTarget::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
oxide_parser::AssignmentTarget::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
|
||||
oxide_parser::AssignmentTarget::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_class(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // ctor_reg
|
||||
ctx.alloc_reg(); // proto_reg
|
||||
if let Some(super_class) = &class.super_class {
|
||||
self.count_expression(super_class, ctx);
|
||||
}
|
||||
ctx.count_instr(); // LOAD_CONST ctor
|
||||
ctx.count_instr(); // NEW_OBJECT proto
|
||||
|
||||
if class.super_class.is_some() {
|
||||
ctx.alloc_reg(); // parent prototype key
|
||||
ctx.count_instr(); // LOAD_CONST
|
||||
ctx.alloc_reg(); // parent prototype value
|
||||
ctx.count_instr(); // GET_PROP
|
||||
ctx.alloc_reg(); // __proto__ key
|
||||
ctx.count_instr(); // LOAD_CONST
|
||||
ctx.count_instr(); // SET_PROP proto.__proto__
|
||||
ctx.count_instr(); // SET_PROP ctor.__proto__
|
||||
}
|
||||
|
||||
ctx.count_load_const(); // constructor key
|
||||
ctx.count_instr(); // SET_PROP proto.constructor = ctor
|
||||
|
||||
ctx.count_load_const(); // prototype key
|
||||
ctx.count_instr(); // SET_PROP ctor.prototype = proto
|
||||
|
||||
for element in &class.body.body {
|
||||
match element {
|
||||
ClassElement::MethodDefinition(method) => {
|
||||
let method = method.as_ref();
|
||||
if matches!(method.kind, MethodDefinitionKind::Constructor) {
|
||||
continue;
|
||||
}
|
||||
if matches!(method.key, oxide_parser::PropertyKey::PrivateIdentifier(_)) {
|
||||
if method.r#static {
|
||||
ctx.count_load_const(); // private id
|
||||
ctx.count_load_const(); // method
|
||||
ctx.count_instr(); // INIT_PRIVATE
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ctx.count_load_const(); // key_reg
|
||||
ctx.count_load_const(); // method_reg
|
||||
ctx.count_instr(); // SET_HOME_OBJECT
|
||||
match method.kind {
|
||||
MethodDefinitionKind::Method => {
|
||||
ctx.count_instr(); // SET_PROP
|
||||
}
|
||||
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
|
||||
ctx.count_load_const(); // undefined placeholder
|
||||
ctx.count_define_accessor(); // DEFINE_ACCESSOR + ext
|
||||
}
|
||||
MethodDefinitionKind::Constructor => {}
|
||||
}
|
||||
}
|
||||
ClassElement::PropertyDefinition(prop) => {
|
||||
let prop = prop.as_ref();
|
||||
if prop.r#static {
|
||||
ctx.count_load_const(); // key_reg
|
||||
if let Some(value) = &prop.value {
|
||||
self.count_expression(value, ctx);
|
||||
} else {
|
||||
ctx.count_load_const();
|
||||
}
|
||||
ctx.count_instr(); // SET_PROP
|
||||
}
|
||||
}
|
||||
ClassElement::StaticBlock(block) => {
|
||||
for stmt in &block.body {
|
||||
self.count_statement(stmt, ctx);
|
||||
}
|
||||
}
|
||||
ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_object_property_read_static(&self, ctx: &mut CompileCtx) {
|
||||
ctx.count_load_var(); // prop/object temp
|
||||
ctx.count_load_const(); // key reg
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_object_property_read_key(
|
||||
&self, key: &oxide_parser::PropertyKey, computed: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
if !computed {
|
||||
self.count_object_property_read_static(ctx);
|
||||
let _ = key;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.count_load_var(); // prop/object temp
|
||||
match key {
|
||||
oxide_parser::PropertyKey::Identifier(ident) => {
|
||||
let name = ident.name.as_str();
|
||||
let _ = ctx.lookup_or_builtin(name);
|
||||
ctx.count_load_var(); // key
|
||||
}
|
||||
oxide_parser::PropertyKey::StringLiteral(_) => {
|
||||
ctx.count_load_const(); // key
|
||||
}
|
||||
oxide_parser::PropertyKey::NumericLiteral(_) => {
|
||||
ctx.count_load_const(); // key
|
||||
}
|
||||
oxide_parser::PropertyKey::StaticIdentifier(_) => {
|
||||
ctx.count_load_const(); // key
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
ctx.alloc_reg(); // result reg
|
||||
ctx.count_instr(); // GET_PROP_DYNAMIC
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_optional_guard(&self, ctx: &mut CompileCtx) {
|
||||
ctx.count_load_var(); // dup
|
||||
ctx.count_jump(); // JMP_IF_NULLISH
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_static_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::StaticMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_load_const(); // key
|
||||
ctx.count_load_var(); // receiver copy
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_computed_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // GET_PROP_DYNAMIC
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_chain_call(
|
||||
&self, call: &oxide_parser::CallExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx);
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
_ => {
|
||||
self.count_chainable_expression(&call.callee, short_chain, ctx);
|
||||
if short_chain && call.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST undefined this
|
||||
}
|
||||
}
|
||||
if short_chain
|
||||
&& call.optional
|
||||
&& matches!(
|
||||
&call.callee,
|
||||
Expression::StaticMemberExpression(_)
|
||||
| Expression::ComputedMemberExpression(_)
|
||||
| Expression::PrivateFieldExpression(_)
|
||||
)
|
||||
{
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.count_call_instr_with_arg_ext(); // CALL + arg_count ext word
|
||||
ctx.count_load_var(); // result <- regs[0]
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_chainable_expression(
|
||||
&self, expr: &Expression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
Expression::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
|
||||
Expression::ChainExpression(chain) => self.count_chain_element(&chain.expression, short_chain, ctx),
|
||||
_ => self.count_expression(expr, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_chain_element(
|
||||
&self, element: &ChainElement, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match element {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
ChainElement::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
ChainElement::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
ChainElement::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
|
||||
ChainElement::TSNonNullExpression(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_logical_assign_test(&self, op: LogicalOperator, id: u32, ctx: &mut CompileCtx) {
|
||||
match op {
|
||||
LogicalOperator::And | LogicalOperator::Or => {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
LogicalOperator::Coalesce => {
|
||||
ctx.projected_pc += 1; // JMP_IF_NULLISH to store body
|
||||
ctx.projected_pc += 1; // JMP to end on non-nullish
|
||||
ctx.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
mod binding;
|
||||
mod class;
|
||||
mod member;
|
||||
@@ -0,0 +1,10 @@
|
||||
use oxide_parser::{
|
||||
AssignmentOperator, ChainElement, ClassElement, Expression, ForStatementInit, LogicalOperator,
|
||||
MethodDefinitionKind, SimpleAssignmentTarget, Statement, UnaryOperator, VariableDeclarationKind,
|
||||
};
|
||||
|
||||
use crate::compiler::{is_side_effect_free, CompileCtx, Compiler, Label};
|
||||
|
||||
mod expr;
|
||||
mod helper;
|
||||
mod stmt;
|
||||
@@ -0,0 +1,303 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::ExpressionStatement(es) => {
|
||||
self.count_expression(&es.expression, ctx);
|
||||
}
|
||||
Statement::VariableDeclaration(decl) => {
|
||||
for d in &decl.declarations {
|
||||
if let Some(init) = &d.init {
|
||||
self.count_expression(init, ctx);
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_words(2); // LOAD_CONST(undefined) + STORE_VAR
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ReturnStatement(ret) => {
|
||||
if let Some(arg) = &ret.argument {
|
||||
self.count_expression(arg, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1; // RETURN
|
||||
}
|
||||
Statement::IfStatement(ifs) => {
|
||||
let id = ctx.next_label_id();
|
||||
let else_label = Label::IfElse(id);
|
||||
let end_label = Label::IfEnd(id);
|
||||
|
||||
self.count_expression(&ifs.test, ctx);
|
||||
ctx.projected_pc += 1; // JMP_IF_FALSE
|
||||
self.count_statement(&ifs.consequent, ctx);
|
||||
ctx.alloc_reg(); // result register
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- consequent
|
||||
if ifs.alternate.is_some() {
|
||||
ctx.projected_pc += 1; // JMP (skip else)
|
||||
}
|
||||
ctx.label_map.insert(else_label, ctx.projected_pc);
|
||||
if let Some(alt_stmt) = &ifs.alternate {
|
||||
self.count_statement(alt_stmt, ctx);
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- alternate
|
||||
}
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
Statement::WhileStatement(wh) => {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::WhileStart(id);
|
||||
let end_label = Label::WhileEnd(id);
|
||||
|
||||
ctx.label_map.insert(start_label, ctx.projected_pc);
|
||||
self.count_expression(&wh.test, ctx);
|
||||
ctx.projected_pc += 1; // JMP_IF_FALSE
|
||||
self.count_statement(&wh.body, ctx);
|
||||
ctx.projected_pc += 1; // JMP (backward)
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
Statement::DoWhileStatement(dw) => {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::DoWhileStart(id);
|
||||
let end_label = Label::DoWhileEnd(id);
|
||||
|
||||
ctx.label_map.insert(start_label, ctx.projected_pc);
|
||||
self.count_statement(&dw.body, ctx);
|
||||
self.count_expression(&dw.test, ctx);
|
||||
ctx.projected_pc += 1; // JMP_IF_TRUE (backward)
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
Statement::ForStatement(fr) => {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForStart(id);
|
||||
let update_label = Label::ForUpdate(id);
|
||||
let end_label = Label::ForEnd(id);
|
||||
|
||||
if let Some(init) = &fr.init {
|
||||
if let Some(expr) = init.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
} else if let ForStatementInit::VariableDeclaration(decl) = init {
|
||||
for d in &decl.declarations {
|
||||
if let Some(init_expr) = &d.init {
|
||||
self.count_expression(init_expr, ctx);
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_words(2); // LOAD_CONST(undefined) + STORE_VAR
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.label_map.insert(start_label, ctx.projected_pc);
|
||||
if let Some(test) = &fr.test {
|
||||
self.count_expression(test, ctx);
|
||||
ctx.projected_pc += 1; // JMP_IF_FALSE
|
||||
}
|
||||
self.count_statement(&fr.body, ctx);
|
||||
ctx.label_map.insert(update_label, ctx.projected_pc);
|
||||
if let Some(update) = &fr.update {
|
||||
self.count_expression(update, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1; // JMP (backward)
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
Statement::ForInStatement(fi) => {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForInStart(id);
|
||||
let end_label = Label::ForInEnd(id);
|
||||
|
||||
self.count_expression(&fi.right, ctx);
|
||||
ctx.count_instr(); // FOR_IN_INIT
|
||||
|
||||
ctx.label_map.insert(start_label, ctx.projected_pc);
|
||||
ctx.count_instr(); // FOR_IN_DONE
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
ctx.count_jump(); // JMP cleanup
|
||||
|
||||
ctx.count_instr(); // FOR_IN_NEXT
|
||||
match &fi.left {
|
||||
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for _d in &decl.declarations {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
}
|
||||
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg(); // key register
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
self.count_statement(&fi.body, ctx);
|
||||
ctx.count_jump(); // JMP back to start
|
||||
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.count_instr(); // FOR_IN_CLEANUP
|
||||
}
|
||||
Statement::ForOfStatement(fo) => {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForOfStart(id);
|
||||
let end_label = Label::ForOfEnd(id);
|
||||
|
||||
self.count_expression(&fo.right, ctx);
|
||||
ctx.count_instr(); // FOR_OF_INIT
|
||||
ctx.label_map.insert(start_label, ctx.projected_pc);
|
||||
ctx.alloc_reg(); // has_reg
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
ctx.alloc_reg(); // val_reg
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
match &fo.left {
|
||||
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for d in &decl.declarations {
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
}
|
||||
}
|
||||
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
oxide_parser::ForStatementLeft::ArrayAssignmentTarget(ap) => {
|
||||
self.count_array_assignment(ap, ctx);
|
||||
}
|
||||
oxide_parser::ForStatementLeft::ObjectAssignmentTarget(op) => {
|
||||
self.count_object_assignment(op, ctx);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.count_statement(&fo.body, ctx);
|
||||
ctx.count_jump(); // JMP back
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
Statement::SwitchStatement(sw) => {
|
||||
let id = ctx.next_label_id();
|
||||
let end_label = Label::SwitchEnd(id);
|
||||
ctx.push_switch(end_label);
|
||||
|
||||
self.count_expression(&sw.discriminant, ctx);
|
||||
let compare_reg_checkpoint = ctx.reg_checkpoint();
|
||||
|
||||
let cases = &sw.cases;
|
||||
for case in cases.iter() {
|
||||
if let Some(test) = &case.test {
|
||||
self.count_expression(test, ctx);
|
||||
ctx.projected_pc += 1; // EQ
|
||||
ctx.alloc_reg(); // eq result
|
||||
ctx.projected_pc += 1; // JMP_IF_TRUE
|
||||
ctx.restore_reg_checkpoint(compare_reg_checkpoint);
|
||||
}
|
||||
}
|
||||
|
||||
let has_default = cases.iter().any(|c| c.test.is_none());
|
||||
if !has_default {
|
||||
ctx.projected_pc += 1; // JMP to SwitchEnd (no match)
|
||||
}
|
||||
|
||||
for (case_idx, case) in cases.iter().enumerate() {
|
||||
let case_label = Label::SwitchCase(id, case_idx as u32);
|
||||
ctx.label_map.insert(case_label, ctx.projected_pc);
|
||||
for s in &case.consequent {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.pop_switch();
|
||||
}
|
||||
Statement::BreakStatement(_) => {
|
||||
ctx.projected_pc += 1; // JMP
|
||||
}
|
||||
Statement::ContinueStatement(_) => {
|
||||
ctx.projected_pc += 1; // JMP
|
||||
}
|
||||
Statement::BlockStatement(block) => {
|
||||
for s in &block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
}
|
||||
Statement::FunctionDeclaration(fd) => {
|
||||
let name = if let Some(id) = &fd.id {
|
||||
id.name.to_string()
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Hoisting: declare function name as initialized
|
||||
let func_reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(&name, func_reg, VariableDeclarationKind::Var, false);
|
||||
|
||||
// Body is compiled in the emit pass only.
|
||||
// FD emits LOAD_CONST(BytecodeFunc) + STORE_VAR
|
||||
ctx.count_words(2);
|
||||
}
|
||||
Statement::ClassDeclaration(class) => {
|
||||
ctx.alloc_reg(); // class binding reg
|
||||
self.count_class(class, ctx);
|
||||
ctx.projected_pc += 1; // STORE_VAR binding <- ctor
|
||||
}
|
||||
Statement::ThrowStatement(ts) => {
|
||||
self.count_expression(&ts.argument, ctx);
|
||||
ctx.projected_pc += 1; // THROW
|
||||
}
|
||||
Statement::TryStatement(ts) => {
|
||||
let id = ctx.next_label_id();
|
||||
let catch_label = Label::CatchBody(id);
|
||||
let try_end_label = Label::TryEnd(id);
|
||||
let has_catch = ts.handler.is_some();
|
||||
let has_finally = ts.finalizer.is_some();
|
||||
|
||||
ctx.alloc_reg(); // result_reg
|
||||
|
||||
if has_finally {
|
||||
ctx.projected_pc += 1; // TRY_FINALLY_BEGIN (before try body)
|
||||
}
|
||||
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1; // TRY_BEGIN (before try body)
|
||||
}
|
||||
|
||||
for s in &ts.block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1; // LOAD_VAR result_reg (if try body has result)
|
||||
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1; // TRY_END
|
||||
}
|
||||
|
||||
let jmp_needed = has_catch || has_finally;
|
||||
if jmp_needed {
|
||||
ctx.projected_pc += 1; // JMP
|
||||
}
|
||||
|
||||
ctx.label_map.insert(catch_label, ctx.projected_pc);
|
||||
if let Some(catch) = &ts.handler {
|
||||
ctx.push_scope();
|
||||
if let Some(_param) = &catch.param {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // STORE_VAR
|
||||
}
|
||||
for cs in &catch.body.body {
|
||||
self.count_statement(cs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1; // LOAD_VAR result_reg (if catch body has result)
|
||||
ctx.pop_scope();
|
||||
}
|
||||
|
||||
if let Some(finally) = &ts.finalizer {
|
||||
let finally_label = Label::FinallyBody(id);
|
||||
ctx.label_map.insert(finally_label, ctx.projected_pc);
|
||||
for fs in &finally.body {
|
||||
self.count_statement(fs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1; // LOAD_VAR result_reg (if finally has result)
|
||||
ctx.projected_pc += 1; // TRY_FINALLY_END
|
||||
}
|
||||
|
||||
ctx.label_map.insert(try_end_label, ctx.projected_pc);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::compiler::Label;
|
||||
use crate::opcode::{self, OpCode};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_parser::{
|
||||
AssignmentOperator, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty, BindingPattern,
|
||||
ChainElement, Class, ClassElement, Expression, ForStatementInit, ForStatementLeft, LogicalOperator,
|
||||
@@ -10,8 +11,6 @@ use oxide_parser::{
|
||||
use crate::compiler::{
|
||||
is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext, ParamSpec,
|
||||
};
|
||||
use crate::module::Constant;
|
||||
|
||||
impl Compiler {
|
||||
fn emit_optional_guard(&self, reg: u8, short_label: Label, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
let short_pos = ctx.resolve_label(short_label)?;
|
||||
|
||||
@@ -4,6 +4,4 @@ pub mod compiler;
|
||||
pub mod counter;
|
||||
pub mod emitter;
|
||||
pub mod hash;
|
||||
pub mod module;
|
||||
pub mod opcode;
|
||||
pub mod symbol_table;
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::opcode::{self, OpCode};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Constant {
|
||||
Number(f64),
|
||||
Int(i32),
|
||||
String(String),
|
||||
Boolean(bool),
|
||||
Null,
|
||||
Undefined,
|
||||
BytecodeFunc(u32),
|
||||
RegExp(String, String),
|
||||
}
|
||||
|
||||
pub struct CompiledModule {
|
||||
pub bytecode: Vec<opcode::Instr>,
|
||||
pub constants: Vec<Constant>,
|
||||
pub n_registers: u8,
|
||||
pub n_args: u8,
|
||||
pub param_base: u8,
|
||||
pub builtin_reg_map: Vec<(String, u8)>,
|
||||
pub sub_modules: Vec<CompiledModule>,
|
||||
/// True when this module is an arrow function body (D-01).
|
||||
/// Arrow functions capture lexical `this` from the enclosing scope.
|
||||
pub is_arrow: bool,
|
||||
/// Index into `constants` holding the captured `this` JsValue.
|
||||
/// 0 means "not captured - use standard this binding".
|
||||
pub captured_this_const_idx: u16,
|
||||
/// Function name inferred from assignment context (D-04).
|
||||
/// Set at the VariableDeclaration / ObjectProperty assignment site.
|
||||
pub function_name: Option<String>,
|
||||
/// True when this bytecode function is a class constructor.
|
||||
/// Ordinary CALL must reject it, while NEW_EXPRESSION may construct through it.
|
||||
pub is_class_constructor: bool,
|
||||
/// True when this class constructor has an `extends` clause.
|
||||
/// `this` stays uninitialized until SUPER_CALL completes.
|
||||
pub is_derived_constructor: bool,
|
||||
/// True for prototype methods whose function object needs a runtime home_object.
|
||||
pub needs_home_object: bool,
|
||||
}
|
||||
|
||||
impl CompiledModule {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
bytecode: Vec::new(),
|
||||
constants: Vec::new(),
|
||||
n_registers: 0,
|
||||
n_args: 0,
|
||||
param_base: 0,
|
||||
builtin_reg_map: Vec::new(),
|
||||
sub_modules: Vec::new(),
|
||||
is_arrow: false,
|
||||
captured_this_const_idx: 0,
|
||||
function_name: None,
|
||||
is_class_constructor: false,
|
||||
is_derived_constructor: false,
|
||||
needs_home_object: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CompiledModule {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for CompiledModule {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
bytecode: self.bytecode.clone(),
|
||||
constants: self.constants.clone(),
|
||||
n_registers: self.n_registers,
|
||||
n_args: self.n_args,
|
||||
param_base: self.param_base,
|
||||
builtin_reg_map: self.builtin_reg_map.clone(),
|
||||
sub_modules: self.sub_modules.clone(),
|
||||
is_arrow: self.is_arrow,
|
||||
captured_this_const_idx: self.captured_this_const_idx,
|
||||
function_name: self.function_name.clone(),
|
||||
is_class_constructor: self.is_class_constructor,
|
||||
is_derived_constructor: self.is_derived_constructor,
|
||||
needs_home_object: self.needs_home_object,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CompiledModule {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
writeln!(f, "; n_registers = {}", self.n_registers)?;
|
||||
writeln!(f, "; constants:")?;
|
||||
for (i, c) in self.constants.iter().enumerate() {
|
||||
match c {
|
||||
Constant::BytecodeFunc(idx) => writeln!(f, "; [{i}] = BytecodeFunc(sub_module[{idx}])")?,
|
||||
other => writeln!(f, "; [{i}] = {other:?}")?,
|
||||
}
|
||||
}
|
||||
writeln!(f)?;
|
||||
for (offset, &instr) in self.bytecode.iter().enumerate() {
|
||||
let op = opcode::opcode(instr);
|
||||
let rd = opcode::rd(instr);
|
||||
let a = opcode::a(instr);
|
||||
let b = opcode::b(instr);
|
||||
write!(f, " {offset:04} {op}")?;
|
||||
match op {
|
||||
OpCode::LOAD_CONST => {
|
||||
write!(f, " r{rd}, const[{}]", opcode::imm16(instr))?;
|
||||
}
|
||||
OpCode::JMP | OpCode::JMP_IF_FALSE | OpCode::JMP_IF_TRUE => {
|
||||
write!(f, " r{rd}, {offset:+}", offset = opcode::offset16(instr))?;
|
||||
}
|
||||
OpCode::SWITCH_TABLE => {
|
||||
let n_cases = rd as u16 | ((b as u16) << 8);
|
||||
write!(f, " r{disc_reg}={a}, {n_cases} cases", disc_reg = a)?;
|
||||
}
|
||||
OpCode::RETURN | OpCode::HALT | OpCode::NOP => {
|
||||
write!(f, " r{rd}")?;
|
||||
}
|
||||
OpCode::NEG => {
|
||||
write!(f, " r{rd}, r{a}")?;
|
||||
}
|
||||
_ => {
|
||||
write!(f, " r{rd}, r{a}, r{b}")?;
|
||||
}
|
||||
}
|
||||
writeln!(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,495 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
/// OpCode for register-based bytecode VM.
|
||||
///
|
||||
/// Organized in groups of 16 for readability. Implemented opcodes have
|
||||
/// emitter support in the compiler; placeholder opcodes are reserved
|
||||
/// for future phases (IC, profiling, parallelization).
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum OpCode {
|
||||
// ── Arithmetic (0x00-0x0F) ──
|
||||
ADD = 0x00,
|
||||
SUB = 0x01,
|
||||
MUL = 0x02,
|
||||
DIV = 0x03,
|
||||
MOD = 0x04,
|
||||
NEG = 0x05,
|
||||
COMPOUND_ADD = 0x06,
|
||||
COMPOUND_SUB = 0x07,
|
||||
COMPOUND_MUL = 0x08,
|
||||
COMPOUND_DIV = 0x09,
|
||||
COMPOUND_MOD = 0x0A,
|
||||
COMPOUND_EXP = 0x0B,
|
||||
|
||||
// -- Comparison (0x10-0x1F) --
|
||||
EQ = 0x10,
|
||||
NEQ = 0x11,
|
||||
LT = 0x12,
|
||||
GT = 0x13,
|
||||
LTE = 0x14,
|
||||
GTE = 0x15,
|
||||
IN = 0x16,
|
||||
NOT = 0x17,
|
||||
AND = 0x18,
|
||||
OR = 0x19,
|
||||
STRICT_EQ = 0x1A,
|
||||
STRICT_NEQ = 0x1C,
|
||||
UNARY_PLUS = 0x1D,
|
||||
|
||||
// -- Control Flow (0x20-0x2F) --
|
||||
JMP = 0x20,
|
||||
JMP_IF_FALSE = 0x21,
|
||||
JMP_IF_TRUE = 0x22,
|
||||
FOR_OF_INIT = 0x23,
|
||||
FOR_OF_NEXT = 0x24,
|
||||
|
||||
// -- Update (0x25-0x28) --
|
||||
INC_PRE = 0x25,
|
||||
INC_POST = 0x26,
|
||||
DEC_PRE = 0x27,
|
||||
DEC_POST = 0x28,
|
||||
|
||||
FOR_IN_INIT = 0x29,
|
||||
FOR_IN_NEXT = 0x2A,
|
||||
FOR_IN_DONE = 0x2B,
|
||||
SWITCH_TABLE = 0x2C,
|
||||
FOR_IN_CLEANUP = 0x2D,
|
||||
|
||||
// -- Exception (0x2E-0x2F, 0x33-0x35) --
|
||||
THROW = 0x2E,
|
||||
TRY_BEGIN = 0x2F,
|
||||
TRY_END = 0x33,
|
||||
TRY_FINALLY_BEGIN = 0x34,
|
||||
TRY_FINALLY_END = 0x35,
|
||||
FOR_OF_DONE = 0x36,
|
||||
FOR_OF_CLOSE = 0x37,
|
||||
|
||||
// -- Template Literal (0x38) --
|
||||
TEMPLATE_STR = 0x38,
|
||||
|
||||
// -- Small Language Features (0x39-0x3B) --
|
||||
DELETE_PROP_STATIC = 0x39,
|
||||
DELETE_PROP_DYNAMIC = 0x3A,
|
||||
INSTANCEOF = 0x3B,
|
||||
REST_OBJECT = 0x3E,
|
||||
|
||||
// -- Variable (0x30-0x32) --
|
||||
LOAD_VAR = 0x30,
|
||||
STORE_VAR = 0x31,
|
||||
LOAD_CONST = 0x32,
|
||||
|
||||
// -- Call (0x40-0x4F) --
|
||||
CALL = 0x40,
|
||||
RETURN = 0x41,
|
||||
CALL_NATIVE = 0x42,
|
||||
NEW_EXPRESSION = 0x43,
|
||||
SUPER_CALL = 0x44,
|
||||
SUPER_GET_PROP = 0x45,
|
||||
SUPER_STATIC_GET_PROP = 0x46,
|
||||
SET_HOME_OBJECT = 0x47,
|
||||
DEFINE_ACCESSOR = 0x48,
|
||||
|
||||
// ── Object Property (0x50-0x5F) ──
|
||||
IC_GET_PROP = 0x50,
|
||||
IC_SET_PROP = 0x51,
|
||||
GET_PROP = 0x52,
|
||||
GET_PROP_DYNAMIC = 0x53,
|
||||
SET_PROP = 0x54,
|
||||
SET_PROP_DYNAMIC = 0x55,
|
||||
NEW_OBJECT = 0x56,
|
||||
NEW_ARRAY = 0x57,
|
||||
SET_ELEM = 0x58,
|
||||
|
||||
// -- Member Update (0x59-0x62) --
|
||||
MEMBER_INC = 0x59,
|
||||
MEMBER_DEC = 0x5A,
|
||||
DYN_MEMBER_INC = 0x5B,
|
||||
DYN_MEMBER_DEC = 0x5C,
|
||||
COMPOUND_MEMBER_ADD = 0x5D,
|
||||
COMPOUND_MEMBER_SUB = 0x5E,
|
||||
COMPOUND_MEMBER_MUL = 0x5F,
|
||||
COMPOUND_MEMBER_DIV = 0x60,
|
||||
COMPOUND_MEMBER_MOD = 0x61,
|
||||
COMPOUND_MEMBER_EXP = 0x62,
|
||||
|
||||
// -- Profiling - placeholders (0x63-0x6F) --
|
||||
PROFILE_TYPE = 0x63,
|
||||
PROFILE_SHAPE = 0x64,
|
||||
PROFILE_BRANCH = 0x65,
|
||||
PROFILE_CALL = 0x66,
|
||||
|
||||
// ── Parallel — placeholders (0x70-0x7F) ──
|
||||
FORK = 0x70,
|
||||
JOIN = 0x71,
|
||||
GET_PRIVATE = 0x72,
|
||||
SET_PRIVATE = 0x73,
|
||||
INIT_PRIVATE = 0x74,
|
||||
PRIVATE_BRAND_IN = 0x75,
|
||||
|
||||
// -- Bitwise (0x80-0x8F) --
|
||||
BIT_AND = 0x80,
|
||||
BIT_OR = 0x81,
|
||||
BIT_XOR = 0x82,
|
||||
SHL = 0x83,
|
||||
SHR = 0x84,
|
||||
USHR = 0x85,
|
||||
BIT_NOT = 0x86,
|
||||
COMPOUND_AND = 0x88,
|
||||
COMPOUND_OR = 0x89,
|
||||
COMPOUND_XOR = 0x8A,
|
||||
COMPOUND_SHL = 0x8B,
|
||||
COMPOUND_SHR = 0x8C,
|
||||
COMPOUND_USHR = 0x8D,
|
||||
NULLISH = 0x8E,
|
||||
JMP_IF_NULLISH = 0x8F,
|
||||
|
||||
// ── Misc (0xF0-0xFF) ──
|
||||
NOP = 0xF0,
|
||||
HALT = 0xF1,
|
||||
TYPEOF = 0xF2,
|
||||
VOID = 0xF3,
|
||||
}
|
||||
|
||||
impl OpCode {
|
||||
pub fn is_implemented(&self) -> bool {
|
||||
!matches!(
|
||||
self,
|
||||
OpCode::PROFILE_TYPE
|
||||
| OpCode::PROFILE_SHAPE
|
||||
| OpCode::PROFILE_BRANCH
|
||||
| OpCode::PROFILE_CALL
|
||||
| OpCode::FORK
|
||||
| OpCode::JOIN
|
||||
)
|
||||
}
|
||||
|
||||
pub fn has_ic_ext_words(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
OpCode::IC_GET_PROP
|
||||
| OpCode::IC_SET_PROP
|
||||
| OpCode::MEMBER_INC
|
||||
| OpCode::MEMBER_DEC
|
||||
| OpCode::COMPOUND_MEMBER_ADD
|
||||
| OpCode::COMPOUND_MEMBER_SUB
|
||||
| OpCode::COMPOUND_MEMBER_MUL
|
||||
| OpCode::COMPOUND_MEMBER_DIV
|
||||
| OpCode::COMPOUND_MEMBER_MOD
|
||||
| OpCode::COMPOUND_MEMBER_EXP
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for OpCode {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
0x00 => Ok(OpCode::ADD),
|
||||
0x01 => Ok(OpCode::SUB),
|
||||
0x02 => Ok(OpCode::MUL),
|
||||
0x03 => Ok(OpCode::DIV),
|
||||
0x04 => Ok(OpCode::MOD),
|
||||
0x05 => Ok(OpCode::NEG),
|
||||
0x06 => Ok(OpCode::COMPOUND_ADD),
|
||||
0x07 => Ok(OpCode::COMPOUND_SUB),
|
||||
0x08 => Ok(OpCode::COMPOUND_MUL),
|
||||
0x09 => Ok(OpCode::COMPOUND_DIV),
|
||||
0x0A => Ok(OpCode::COMPOUND_MOD),
|
||||
0x0B => Ok(OpCode::COMPOUND_EXP),
|
||||
0x10 => Ok(OpCode::EQ),
|
||||
0x11 => Ok(OpCode::NEQ),
|
||||
0x12 => Ok(OpCode::LT),
|
||||
0x13 => Ok(OpCode::GT),
|
||||
0x14 => Ok(OpCode::LTE),
|
||||
0x15 => Ok(OpCode::GTE),
|
||||
0x16 => Ok(OpCode::IN),
|
||||
0x17 => Ok(OpCode::NOT),
|
||||
0x18 => Ok(OpCode::AND),
|
||||
0x19 => Ok(OpCode::OR),
|
||||
0x1A => Ok(OpCode::STRICT_EQ),
|
||||
0x1C => Ok(OpCode::STRICT_NEQ),
|
||||
0x1D => Ok(OpCode::UNARY_PLUS),
|
||||
0x20 => Ok(OpCode::JMP),
|
||||
0x21 => Ok(OpCode::JMP_IF_FALSE),
|
||||
0x22 => Ok(OpCode::JMP_IF_TRUE),
|
||||
0x23 => Ok(OpCode::FOR_OF_INIT),
|
||||
0x24 => Ok(OpCode::FOR_OF_NEXT),
|
||||
0x25 => Ok(OpCode::INC_PRE),
|
||||
0x26 => Ok(OpCode::INC_POST),
|
||||
0x27 => Ok(OpCode::DEC_PRE),
|
||||
0x28 => Ok(OpCode::DEC_POST),
|
||||
0x29 => Ok(OpCode::FOR_IN_INIT),
|
||||
0x2A => Ok(OpCode::FOR_IN_NEXT),
|
||||
0x2B => Ok(OpCode::FOR_IN_DONE),
|
||||
0x2C => Ok(OpCode::SWITCH_TABLE),
|
||||
0x2D => Ok(OpCode::FOR_IN_CLEANUP),
|
||||
0x2E => Ok(OpCode::THROW),
|
||||
0x2F => Ok(OpCode::TRY_BEGIN),
|
||||
0x30 => Ok(OpCode::LOAD_VAR),
|
||||
0x31 => Ok(OpCode::STORE_VAR),
|
||||
0x32 => Ok(OpCode::LOAD_CONST),
|
||||
0x33 => Ok(OpCode::TRY_END),
|
||||
0x34 => Ok(OpCode::TRY_FINALLY_BEGIN),
|
||||
0x35 => Ok(OpCode::TRY_FINALLY_END),
|
||||
0x36 => Ok(OpCode::FOR_OF_DONE),
|
||||
0x37 => Ok(OpCode::FOR_OF_CLOSE),
|
||||
0x38 => Ok(OpCode::TEMPLATE_STR),
|
||||
0x39 => Ok(OpCode::DELETE_PROP_STATIC),
|
||||
0x3A => Ok(OpCode::DELETE_PROP_DYNAMIC),
|
||||
0x3B => Ok(OpCode::INSTANCEOF),
|
||||
0x3E => Ok(OpCode::REST_OBJECT),
|
||||
0x40 => Ok(OpCode::CALL),
|
||||
0x41 => Ok(OpCode::RETURN),
|
||||
0x42 => Ok(OpCode::CALL_NATIVE),
|
||||
0x43 => Ok(OpCode::NEW_EXPRESSION),
|
||||
0x44 => Ok(OpCode::SUPER_CALL),
|
||||
0x45 => Ok(OpCode::SUPER_GET_PROP),
|
||||
0x46 => Ok(OpCode::SUPER_STATIC_GET_PROP),
|
||||
0x47 => Ok(OpCode::SET_HOME_OBJECT),
|
||||
0x48 => Ok(OpCode::DEFINE_ACCESSOR),
|
||||
0x50 => Ok(OpCode::IC_GET_PROP),
|
||||
0x51 => Ok(OpCode::IC_SET_PROP),
|
||||
0x52 => Ok(OpCode::GET_PROP),
|
||||
0x53 => Ok(OpCode::GET_PROP_DYNAMIC),
|
||||
0x54 => Ok(OpCode::SET_PROP),
|
||||
0x55 => Ok(OpCode::SET_PROP_DYNAMIC),
|
||||
0x56 => Ok(OpCode::NEW_OBJECT),
|
||||
0x57 => Ok(OpCode::NEW_ARRAY),
|
||||
0x58 => Ok(OpCode::SET_ELEM),
|
||||
0x59 => Ok(OpCode::MEMBER_INC),
|
||||
0x5A => Ok(OpCode::MEMBER_DEC),
|
||||
0x5B => Ok(OpCode::DYN_MEMBER_INC),
|
||||
0x5C => Ok(OpCode::DYN_MEMBER_DEC),
|
||||
0x5D => Ok(OpCode::COMPOUND_MEMBER_ADD),
|
||||
0x5E => Ok(OpCode::COMPOUND_MEMBER_SUB),
|
||||
0x5F => Ok(OpCode::COMPOUND_MEMBER_MUL),
|
||||
0x60 => Ok(OpCode::COMPOUND_MEMBER_DIV),
|
||||
0x61 => Ok(OpCode::COMPOUND_MEMBER_MOD),
|
||||
0x62 => Ok(OpCode::COMPOUND_MEMBER_EXP),
|
||||
0x63 => Ok(OpCode::PROFILE_TYPE),
|
||||
0x64 => Ok(OpCode::PROFILE_SHAPE),
|
||||
0x65 => Ok(OpCode::PROFILE_BRANCH),
|
||||
0x66 => Ok(OpCode::PROFILE_CALL),
|
||||
0x70 => Ok(OpCode::FORK),
|
||||
0x71 => Ok(OpCode::JOIN),
|
||||
0x72 => Ok(OpCode::GET_PRIVATE),
|
||||
0x73 => Ok(OpCode::SET_PRIVATE),
|
||||
0x74 => Ok(OpCode::INIT_PRIVATE),
|
||||
0x75 => Ok(OpCode::PRIVATE_BRAND_IN),
|
||||
0x80 => Ok(OpCode::BIT_AND),
|
||||
0x81 => Ok(OpCode::BIT_OR),
|
||||
0x82 => Ok(OpCode::BIT_XOR),
|
||||
0x83 => Ok(OpCode::SHL),
|
||||
0x84 => Ok(OpCode::SHR),
|
||||
0x85 => Ok(OpCode::USHR),
|
||||
0x86 => Ok(OpCode::BIT_NOT),
|
||||
0x88 => Ok(OpCode::COMPOUND_AND),
|
||||
0x89 => Ok(OpCode::COMPOUND_OR),
|
||||
0x8A => Ok(OpCode::COMPOUND_XOR),
|
||||
0x8B => Ok(OpCode::COMPOUND_SHL),
|
||||
0x8C => Ok(OpCode::COMPOUND_SHR),
|
||||
0x8D => Ok(OpCode::COMPOUND_USHR),
|
||||
0x8E => Ok(OpCode::NULLISH),
|
||||
0x8F => Ok(OpCode::JMP_IF_NULLISH),
|
||||
0xF0 => Ok(OpCode::NOP),
|
||||
0xF1 => Ok(OpCode::HALT),
|
||||
0xF2 => Ok(OpCode::TYPEOF),
|
||||
0xF3 => Ok(OpCode::VOID),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for OpCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let name = match self {
|
||||
OpCode::ADD => "ADD",
|
||||
OpCode::SUB => "SUB",
|
||||
OpCode::MUL => "MUL",
|
||||
OpCode::DIV => "DIV",
|
||||
OpCode::MOD => "MOD",
|
||||
OpCode::NEG => "NEG",
|
||||
OpCode::COMPOUND_ADD => "COMPOUND_ADD",
|
||||
OpCode::COMPOUND_SUB => "COMPOUND_SUB",
|
||||
OpCode::COMPOUND_MUL => "COMPOUND_MUL",
|
||||
OpCode::COMPOUND_DIV => "COMPOUND_DIV",
|
||||
OpCode::COMPOUND_MOD => "COMPOUND_MOD",
|
||||
OpCode::COMPOUND_EXP => "COMPOUND_EXP",
|
||||
OpCode::EQ => "EQ",
|
||||
OpCode::NEQ => "NEQ",
|
||||
OpCode::LT => "LT",
|
||||
OpCode::GT => "GT",
|
||||
OpCode::LTE => "LTE",
|
||||
OpCode::GTE => "GTE",
|
||||
OpCode::IN => "IN",
|
||||
OpCode::NOT => "NOT",
|
||||
OpCode::AND => "AND",
|
||||
OpCode::OR => "OR",
|
||||
OpCode::STRICT_EQ => "STRICT_EQ",
|
||||
OpCode::STRICT_NEQ => "STRICT_NEQ",
|
||||
OpCode::UNARY_PLUS => "UNARY_PLUS",
|
||||
OpCode::JMP => "JMP",
|
||||
OpCode::JMP_IF_FALSE => "JMP_IF_FALSE",
|
||||
OpCode::JMP_IF_TRUE => "JMP_IF_TRUE",
|
||||
OpCode::FOR_OF_INIT => "FOR_OF_INIT",
|
||||
OpCode::FOR_OF_NEXT => "FOR_OF_NEXT",
|
||||
OpCode::INC_PRE => "INC_PRE",
|
||||
OpCode::INC_POST => "INC_POST",
|
||||
OpCode::DEC_PRE => "DEC_PRE",
|
||||
OpCode::DEC_POST => "DEC_POST",
|
||||
OpCode::FOR_IN_INIT => "FOR_IN_INIT",
|
||||
OpCode::FOR_IN_NEXT => "FOR_IN_NEXT",
|
||||
OpCode::FOR_IN_DONE => "FOR_IN_DONE",
|
||||
OpCode::SWITCH_TABLE => "SWITCH_TABLE",
|
||||
OpCode::FOR_IN_CLEANUP => "FOR_IN_CLEANUP",
|
||||
OpCode::THROW => "THROW",
|
||||
OpCode::TRY_BEGIN => "TRY_BEGIN",
|
||||
OpCode::TRY_END => "TRY_END",
|
||||
OpCode::TRY_FINALLY_BEGIN => "TRY_FINALLY_BEGIN",
|
||||
OpCode::TRY_FINALLY_END => "TRY_FINALLY_END",
|
||||
OpCode::FOR_OF_DONE => "FOR_OF_DONE",
|
||||
OpCode::FOR_OF_CLOSE => "FOR_OF_CLOSE",
|
||||
OpCode::TEMPLATE_STR => "TEMPLATE_STR",
|
||||
OpCode::DELETE_PROP_STATIC => "DELETE_PROP_STATIC",
|
||||
OpCode::DELETE_PROP_DYNAMIC => "DELETE_PROP_DYNAMIC",
|
||||
OpCode::INSTANCEOF => "INSTANCEOF",
|
||||
OpCode::REST_OBJECT => "REST_OBJECT",
|
||||
OpCode::LOAD_VAR => "LOAD_VAR",
|
||||
OpCode::STORE_VAR => "STORE_VAR",
|
||||
OpCode::LOAD_CONST => "LOAD_CONST",
|
||||
OpCode::CALL => "CALL",
|
||||
OpCode::RETURN => "RETURN",
|
||||
OpCode::CALL_NATIVE => "CALL_NATIVE",
|
||||
OpCode::NEW_EXPRESSION => "NEW_EXPRESSION",
|
||||
OpCode::SUPER_CALL => "SUPER_CALL",
|
||||
OpCode::SUPER_GET_PROP => "SUPER_GET_PROP",
|
||||
OpCode::SUPER_STATIC_GET_PROP => "SUPER_STATIC_GET_PROP",
|
||||
OpCode::SET_HOME_OBJECT => "SET_HOME_OBJECT",
|
||||
OpCode::DEFINE_ACCESSOR => "DEFINE_ACCESSOR",
|
||||
OpCode::IC_GET_PROP => "IC_GET_PROP",
|
||||
OpCode::IC_SET_PROP => "IC_SET_PROP",
|
||||
OpCode::GET_PROP => "GET_PROP",
|
||||
OpCode::GET_PROP_DYNAMIC => "GET_PROP_DYNAMIC",
|
||||
OpCode::SET_PROP => "SET_PROP",
|
||||
OpCode::SET_PROP_DYNAMIC => "SET_PROP_DYNAMIC",
|
||||
OpCode::NEW_OBJECT => "NEW_OBJECT",
|
||||
OpCode::NEW_ARRAY => "NEW_ARRAY",
|
||||
OpCode::SET_ELEM => "SET_ELEM",
|
||||
OpCode::MEMBER_INC => "MEMBER_INC",
|
||||
OpCode::MEMBER_DEC => "MEMBER_DEC",
|
||||
OpCode::DYN_MEMBER_INC => "DYN_MEMBER_INC",
|
||||
OpCode::DYN_MEMBER_DEC => "DYN_MEMBER_DEC",
|
||||
OpCode::COMPOUND_MEMBER_ADD => "COMPOUND_MEMBER_ADD",
|
||||
OpCode::COMPOUND_MEMBER_SUB => "COMPOUND_MEMBER_SUB",
|
||||
OpCode::COMPOUND_MEMBER_MUL => "COMPOUND_MEMBER_MUL",
|
||||
OpCode::COMPOUND_MEMBER_DIV => "COMPOUND_MEMBER_DIV",
|
||||
OpCode::COMPOUND_MEMBER_MOD => "COMPOUND_MEMBER_MOD",
|
||||
OpCode::COMPOUND_MEMBER_EXP => "COMPOUND_MEMBER_EXP",
|
||||
OpCode::PROFILE_TYPE => "PROFILE_TYPE",
|
||||
OpCode::PROFILE_SHAPE => "PROFILE_SHAPE",
|
||||
OpCode::PROFILE_BRANCH => "PROFILE_BRANCH",
|
||||
OpCode::PROFILE_CALL => "PROFILE_CALL",
|
||||
OpCode::FORK => "FORK",
|
||||
OpCode::JOIN => "JOIN",
|
||||
OpCode::GET_PRIVATE => "GET_PRIVATE",
|
||||
OpCode::SET_PRIVATE => "SET_PRIVATE",
|
||||
OpCode::INIT_PRIVATE => "INIT_PRIVATE",
|
||||
OpCode::PRIVATE_BRAND_IN => "PRIVATE_BRAND_IN",
|
||||
OpCode::BIT_AND => "BIT_AND",
|
||||
OpCode::BIT_OR => "BIT_OR",
|
||||
OpCode::BIT_XOR => "BIT_XOR",
|
||||
OpCode::SHL => "SHL",
|
||||
OpCode::SHR => "SHR",
|
||||
OpCode::USHR => "USHR",
|
||||
OpCode::BIT_NOT => "BIT_NOT",
|
||||
OpCode::COMPOUND_AND => "COMPOUND_AND",
|
||||
OpCode::COMPOUND_OR => "COMPOUND_OR",
|
||||
OpCode::COMPOUND_XOR => "COMPOUND_XOR",
|
||||
OpCode::COMPOUND_SHL => "COMPOUND_SHL",
|
||||
OpCode::COMPOUND_SHR => "COMPOUND_SHR",
|
||||
OpCode::COMPOUND_USHR => "COMPOUND_USHR",
|
||||
OpCode::NULLISH => "NULLISH",
|
||||
OpCode::JMP_IF_NULLISH => "JMP_IF_NULLISH",
|
||||
OpCode::NOP => "NOP",
|
||||
OpCode::HALT => "HALT",
|
||||
OpCode::TYPEOF => "TYPEOF",
|
||||
OpCode::VOID => "VOID",
|
||||
};
|
||||
write!(f, "{name}")
|
||||
}
|
||||
}
|
||||
|
||||
/// 4-byte instruction.
|
||||
///
|
||||
/// Layout: `[opcode: u8] [rd: u8] [a: u8] [b: u8]`
|
||||
/// - `rd` — destination register
|
||||
/// - `a` — first source register, or imm16 low byte
|
||||
/// - `b` — second source register, or imm16 high byte
|
||||
pub type Instr = u32;
|
||||
|
||||
pub fn encode(op: OpCode, rd: u8, a: u8, b: u8) -> Instr {
|
||||
((b as Instr) << 24) | ((a as Instr) << 16) | ((rd as Instr) << 8) | (op as Instr)
|
||||
}
|
||||
|
||||
pub fn opcode(instr: Instr) -> OpCode {
|
||||
OpCode::try_from((instr & 0xFF) as u8).unwrap_or(OpCode::NOP)
|
||||
}
|
||||
|
||||
pub fn rd(instr: Instr) -> u8 {
|
||||
((instr >> 8) & 0xFF) as u8
|
||||
}
|
||||
|
||||
pub fn a(instr: Instr) -> u8 {
|
||||
((instr >> 16) & 0xFF) as u8
|
||||
}
|
||||
|
||||
pub fn b(instr: Instr) -> u8 {
|
||||
((instr >> 24) & 0xFF) as u8
|
||||
}
|
||||
|
||||
pub fn imm16(instr: Instr) -> u16 {
|
||||
((instr >> 16) & 0xFFFF) as u16
|
||||
}
|
||||
|
||||
pub fn offset16(instr: Instr) -> i16 {
|
||||
((instr >> 16) & 0xFFFF) as i16
|
||||
}
|
||||
|
||||
pub fn encode_jmp(offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::JMP, 0, lo, hi)
|
||||
}
|
||||
|
||||
pub fn encode_jmp_if_false(rd: u8, offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::JMP_IF_FALSE, rd, lo, hi)
|
||||
}
|
||||
|
||||
pub fn encode_jmp_if_true(rd: u8, offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::JMP_IF_TRUE, rd, lo, hi)
|
||||
}
|
||||
|
||||
pub fn encode_jmp_if_nullish(rd: u8, offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::JMP_IF_NULLISH, rd, lo, hi)
|
||||
}
|
||||
|
||||
pub fn encode_try_begin(offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::TRY_BEGIN, 0, lo, hi)
|
||||
}
|
||||
|
||||
pub fn encode_try_finally_begin(offset: i16) -> Instr {
|
||||
let lo = (offset as u16 & 0xFF) as u8;
|
||||
let hi = ((offset as u16 >> 8) & 0xFF) as u8;
|
||||
encode(OpCode::TRY_FINALLY_BEGIN, 0, lo, hi)
|
||||
}
|
||||
Reference in New Issue
Block a user