refactor: 清除旧代码并展开 crate

This commit is contained in:
2026-07-05 18:43:22 +08:00
parent 9e3ed194e1
commit ea70e3965d
66 changed files with 1413 additions and 2566 deletions
+326
View File
@@ -0,0 +1,326 @@
use crate::compiler::{CompileCtx, Compiler, FunctionBodyContext};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{Class, ClassElement, MethodDefinitionKind, PropertyKey};
impl Compiler {
pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
let mut constructor_method = None;
let mut instance_fields = Vec::new();
let mut private_names = Vec::<(String, u32)>::new();
let is_derived = class.super_class.is_some();
for element in &class.body.body {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if let PropertyKey::PrivateIdentifier(private) = &method.key {
let name = private.name.as_str().to_string();
if private_names.iter().any(|(existing, _)| existing == &name) {
return Err(format!("duplicate private name #{name}"));
}
let id = ctx.scopes.next_private_name_id;
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
private_names.push((name, id));
}
if method.kind == MethodDefinitionKind::Constructor {
if constructor_method.is_some() {
return Err("duplicate class constructor".into());
}
constructor_method = Some(method);
}
}
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
let name = private.name.as_str().to_string();
if private_names.iter().any(|(existing, _)| existing == &name) {
return Err(format!("duplicate private name #{name}"));
}
let id = ctx.scopes.next_private_name_id;
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
private_names.push((name, id));
}
if !prop.r#static {
instance_fields.push(prop);
}
}
ClassElement::AccessorProperty(_) => return Err("class accessor properties not yet supported".into()),
ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => {}
}
}
let ctor_name = class.id.as_ref().map(|id| id.name.to_string());
let ctor_reg = ctx.alloc_reg();
let proto_reg = ctx.alloc_reg();
let self_binding = ctor_name.as_deref().map(|name| vec![(name, ctor_reg)]).unwrap_or_default();
let super_reg = if let Some(super_expr) = &class.super_class {
Some(self.emit_expression(super_expr, ctx)?)
} else {
None
};
let saved_derived = ctx.in_derived_constructor;
let saved_private_names = ctx.scopes.private_name_map.clone();
ctx.in_derived_constructor = is_derived;
ctx.scopes.private_name_map = private_names.clone();
let mut ctor_module = if let Some(method) = constructor_method {
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
self.compile_function_body_with_field_hooks(
&param_names,
body_stmts,
ctx,
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
compiler.emit_private_field_init(
254,
private.name.as_str(),
field.value.as_ref(),
field_ctx,
)?;
} else {
compiler.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
)?;
}
}
Ok(())
}),
is_derived,
)?
} else {
let mut module = self.compile_function_body_with_field_hooks(
&[],
&[],
ctx,
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
compiler.emit_private_field_init(
254,
private.name.as_str(),
field.value.as_ref(),
field_ctx,
)?;
} else {
compiler.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
)?;
}
}
Ok(())
}),
is_derived,
)?;
if is_derived {
module.bytecode.clear();
module.constants.clear();
module.n_registers = 1;
module.bytecode.push(opcode::encode(OpCode::SUPER_CALL, 0, 0, 0));
module.bytecode.push(0);
let mut field_ctx = CompileCtx::new();
field_ctx.scopes.private_name_map = private_names.clone();
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
self.emit_private_field_init(254, private.name.as_str(), field.value.as_ref(), &mut field_ctx)?;
} else {
self.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
&mut field_ctx,
)?;
}
}
module.bytecode.extend(field_ctx.bytecode);
module.constants = field_ctx.constants;
module.n_registers = field_ctx.max_regs.max(1);
module.bytecode.push(opcode::encode(OpCode::RETURN, 0, 0, 0));
}
module
};
ctx.in_derived_constructor = saved_derived;
ctor_module.is_class_constructor = true;
ctor_module.is_derived_constructor = is_derived;
ctor_module.function_name = ctor_name.clone();
ctx.sub_modules.push(ctor_module);
ctx.emit_create_closure(ctor_reg, ctx.sub_modules.len() as u32);
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, proto_reg, 0, 0));
if let Some(super_reg) = super_reg {
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let parent_proto_key_reg = ctx.alloc_reg();
ctx.emit_load_const(parent_proto_key_reg, proto_key_idx);
let parent_proto_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::GET_PROP, super_reg, parent_proto_reg, parent_proto_key_reg));
let proto_link_idx = ctx.add_constant(Constant::String("__proto__".to_string()));
let proto_link_key_reg = ctx.alloc_reg();
ctx.emit_load_const(proto_link_key_reg, proto_link_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, proto_reg, parent_proto_reg, proto_link_key_reg));
ctx.emit(opcode::encode(OpCode::SET_PROP, ctor_reg, super_reg, proto_link_key_reg));
}
let ctor_key_idx = ctx.add_constant(Constant::String("constructor".to_string()));
let ctor_key_reg = ctx.alloc_reg();
ctx.emit_load_const(ctor_key_reg, ctor_key_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, proto_reg, ctor_reg, ctor_key_reg));
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let proto_key_reg = ctx.alloc_reg();
ctx.emit_load_const(proto_key_reg, proto_key_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, ctor_reg, proto_reg, proto_key_reg));
for element in &class.body.body {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if method.kind == MethodDefinitionKind::Constructor {
continue;
}
if matches!(method.key, PropertyKey::PrivateIdentifier(_)) {
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
self.emit_private_method_init(home_reg, method, home_reg, ctx)?;
continue;
}
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
let key_reg = self.emit_class_key_reg(&method.key, method.computed, ctx)?;
let method_name = if method.computed {
"<computed>".to_string()
} else {
self.class_property_name(&method.key)?
};
let accessor_reg =
self.emit_class_method_function(method, &method_name, home_reg, ctx, &self_binding)?;
match method.kind {
MethodDefinitionKind::Method => {
if method.computed {
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, home_reg, key_reg, accessor_reg));
} else {
ctx.emit(opcode::encode(OpCode::SET_PROP, home_reg, accessor_reg, key_reg));
}
}
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
if method.computed {
return Err("computed class accessors not yet supported".into());
}
let undef_reg = self.emit_undefined(ctx);
let (get_reg, set_reg) = if method.kind == MethodDefinitionKind::Get {
(accessor_reg, undef_reg)
} else {
(undef_reg, accessor_reg)
};
let key_name = self.class_property_name(&method.key)?;
let key_idx = ctx.add_constant(Constant::String(key_name));
ctx.emit(opcode::encode(OpCode::DEFINE_ACCESSOR, home_reg, get_reg, set_reg));
ctx.emit(key_idx as u32);
}
MethodDefinitionKind::Constructor => continue,
}
}
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if prop.r#static {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
self.emit_private_field_init(ctor_reg, private.name.as_str(), prop.value.as_ref(), ctx)?;
} else {
self.emit_public_field_init(ctor_reg, &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
}
ctx.static_block_this_reg = saved_static_this;
}
}
ClassElement::StaticBlock(block) => {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
ctx.push_scope();
for stmt in &block.body {
self.emit_statement(stmt, ctx)?;
}
ctx.pop_scope();
ctx.static_block_this_reg = saved_static_this;
}
ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => {}
}
}
ctx.scopes.private_name_map = saved_private_names;
Ok(ctor_reg)
}
pub(crate) fn emit_class_method_function(
&self, method: &oxide_parser::MethodDefinition, method_name: &str, home_reg: u8, ctx: &mut CompileCtx,
self_binding: &[(&str, u8)],
) -> Result<u8, String> {
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
let saved_instance = ctx.in_instance_method;
let saved_static = ctx.in_static_method;
ctx.in_instance_method = !method.r#static;
ctx.in_static_method = method.r#static;
let mut method_module = self.compile_function_body_with_bindings(
&param_names,
body_stmts,
ctx,
false,
self_binding,
FunctionBodyContext::ClassElement,
)?;
ctx.in_instance_method = saved_instance;
ctx.in_static_method = saved_static;
method_module.function_name = Some(method_name.to_string());
method_module.needs_home_object = true;
ctx.sub_modules.push(method_module);
let method_reg = ctx.alloc_reg();
ctx.emit_create_closure(method_reg, ctx.sub_modules.len() as u32);
ctx.emit(opcode::encode(OpCode::SET_HOME_OBJECT, method_reg, home_reg, 0));
Ok(method_reg)
}
}
@@ -1 +1,2 @@
pub mod core;
pub mod emit;
+116
View File
@@ -1227,6 +1227,122 @@ impl Compiler {
})
}
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::ExpressionStatement(_)
| Statement::ReturnStatement(_)
| Statement::BreakStatement(_)
| Statement::ContinueStatement(_)
| Statement::LabeledStatement(_) => self.count_basic(stmt, ctx),
Statement::BlockStatement(_) => self.count_block_domain(stmt, ctx),
Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => {
self.count_declaration_domain(stmt, ctx)
}
Statement::IfStatement(_) => self.count_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.count_iteration_domain(stmt, ctx),
Statement::SwitchStatement(_) => self.count_switch_domain(stmt, ctx),
Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.count_exception_domain(stmt, ctx),
_ => {}
}
}
pub(crate) fn count_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::BinaryExpression(_)
| Expression::PrivateInExpression(_)
| Expression::UnaryExpression(_)
| Expression::UpdateExpression(_) => self.count_operator(expr, ctx),
Expression::CallExpression(_) | Expression::NewExpression(_) => self.count_call_domain(expr, ctx),
Expression::AssignmentExpression(_) => self.count_assignment(expr, ctx),
Expression::ConditionalExpression(_)
| Expression::SequenceExpression(_)
| Expression::LogicalExpression(_) => self.count_conditional_chain(expr, ctx),
Expression::ChainExpression(_) => self.count_chain_expression(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.count_object_domain(expr, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.count_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_) => self.count_function_domain(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_) => self.count_member_domain(expr, ctx),
Expression::ParenthesizedExpression(_) => self.count_parenthesized_expression(expr, ctx),
Expression::ThisExpression(_) => self.count_this_expression(ctx),
Expression::Identifier(_) => self.count_identifier_expression(expr, ctx),
Expression::NumericLiteral(_)
| Expression::StringLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.count_literal(expr, ctx),
_ => self.count_default_expression(ctx),
}
}
pub(crate) fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
match stmt {
Statement::ExpressionStatement(_) | Statement::ReturnStatement(_) | Statement::EmptyStatement(_) => {
self.emit_basic_domain(stmt, ctx)
}
Statement::BlockStatement(_) => self.emit_block_domain(stmt, ctx),
Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => {
self.emit_declaration_domain(stmt, ctx)
}
Statement::IfStatement(_) => self.emit_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.emit_iteration_domain(stmt, ctx),
Statement::SwitchStatement(_) => self.emit_switch_domain(stmt, ctx),
Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.emit_exception_domain(stmt, ctx),
Statement::BreakStatement(b) => self.emit_break_statement(b, ctx),
Statement::ContinueStatement(c) => self.emit_continue_statement(c, ctx),
Statement::LabeledStatement(ls) => self.emit_labeled_statement(ls, ctx),
_ => Ok(None),
}
}
pub(crate) fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::NumericLiteral(_)
| Expression::StringLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.emit_literal(expr, ctx),
Expression::BinaryExpression(_)
| Expression::PrivateInExpression(_)
| Expression::UnaryExpression(_)
| Expression::ConditionalExpression(_)
| Expression::LogicalExpression(_)
| Expression::UpdateExpression(_) => self.emit_operator(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
| Expression::ChainExpression(_) => self.emit_member_domain(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.emit_object_domain(expr, ctx),
Expression::AssignmentExpression(assign) => self.emit_assignment_expression(assign, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.emit_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_)
| Expression::NewExpression(_) => self.emit_function_domain(expr, ctx),
Expression::Identifier(ident) => self.emit_identifier_expression(ident, ctx),
Expression::CallExpression(_) => self.emit_call_domain(expr, ctx),
Expression::ThisExpression(_) => self.emit_this_expression(ctx),
Expression::SequenceExpression(seq) => self.emit_sequence_expression(seq, ctx),
Expression::ParenthesizedExpression(p) => self.emit_parenthesized_expression(p, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
pub fn compile(&self, program: &oxide_parser::Program) -> Result<CompiledModule, String> {
crate::compiler_debug!("compile: starting...");
let mut ctx = CompileCtx::new();
@@ -1,53 +0,0 @@
use super::*;
impl Compiler {
fn count_object_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ObjectExpression(obj) = expr else {
return;
};
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);
}
}
}
fn count_array_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ArrayExpression(arr) = expr else {
return;
};
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);
}
}
}
pub(in crate::counter) fn count_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::ObjectExpression(_) => self.count_object_expression(expr, ctx),
Expression::ArrayExpression(_) => self.count_array_expression(expr, ctx),
_ => {}
}
}
}
@@ -1,61 +0,0 @@
use super::*;
impl Compiler {
fn count_template_literal(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::TemplateLiteral(tl) = expr else {
return;
};
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);
}
fn count_tagged_template_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::TaggedTemplateExpression(tt) = expr else {
return;
};
self.count_expression(&tt.tag, ctx);
let quasi_count = tt.quasi.quasis.len();
for _ in 0..quasi_count {
ctx.count_load_const(); // string
ctx.count_load_const(); // index
ctx.count_instr(); // SET_ELEM (cooked)
}
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)
}
ctx.alloc_reg(); // raw_temp
ctx.count_instr(); // NEW_ARRAY
for expr in &tt.quasi.expressions {
self.count_expression(expr, ctx);
}
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
}
ctx.count_words(2); // LOAD_VAR for cooked and raw
ctx.count_load_const(); // undefined this arg
ctx.count_call_instr_with_arg_ext();
ctx.count_load_var(); // result <- regs[0]
}
pub(in crate::counter) fn count_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::TemplateLiteral(_) => self.count_template_literal(expr, ctx),
Expression::TaggedTemplateExpression(_) => self.count_tagged_template_expression(expr, ctx),
_ => {}
}
}
}
@@ -1,154 +0,0 @@
use super::super::*;
impl Compiler {
pub(crate) 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(crate) 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(crate) 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 let Some(elem) = elem {
// Mirror emit_array_assignment: an element may be a default (`[a = 1]`) or a
// nested pattern (`[[a], b]`), not just an identifier — recurse through the
// shared dispatch instead of hard-coding one STORE_VAR.
self.count_assignment_maybe_default(elem, ctx);
}
}
if let Some(rest) = &ap.rest {
self.count_rest_array(ctx);
// Mirror the emitter (emit_array_assignment -> emit_assign_target): the rest
// target may itself be a nested pattern, not just an identifier.
self.count_assign_target(&rest.target, ctx);
}
ctx.count_instr(); // FOR_OF_CLOSE
}
pub(crate) 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 let Some(rest) = &op.rest {
ctx.alloc_reg();
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
// Mirror the emitter (emit_object_assignment -> emit_assign_target): the rest
// target may itself be a nested pattern, not just an identifier.
self.count_assign_target(&rest.target, ctx);
}
}
pub(crate) 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(crate) 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),
_ => {}
}
}
}
@@ -1,169 +0,0 @@
use super::super::*;
impl Compiler {
pub(crate) 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(crate) 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(crate) fn count_optional_guard(&self, ctx: &mut CompileCtx) {
ctx.count_load_var(); // dup
ctx.count_jump(); // JMP_IF_NULLISH
}
pub(crate) 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(crate) 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(crate) 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(crate) 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(crate) 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(crate) 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.labels.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
}
}
}
}
@@ -1,2 +0,0 @@
mod binding;
mod member;
-17
View File
@@ -1,17 +0,0 @@
#[allow(unused_imports)]
use crate::compiler::{
is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext, Label, ParamSpec,
};
#[allow(unused_imports)]
use oxide_bytecode::module::Constant;
#[allow(unused_imports)]
use oxide_bytecode::opcode::{self, OpCode};
#[allow(unused_imports)]
use oxide_parser::{
AssignmentOperator, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty, BindingPattern,
ChainElement, Class, ClassElement, Expression, ForStatementInit, ForStatementLeft, LogicalOperator,
MethodDefinitionKind, ObjectAssignmentTarget, PropertyKey, PropertyKind, SimpleAssignmentTarget, Statement,
UnaryOperator, UpdateOperator, VariableDeclarationKind,
};
pub mod helper;
@@ -1,57 +0,0 @@
use super::*;
impl Compiler {
fn count_variable_declaration(&self, decl: &oxide_parser::VariableDeclaration<'_>, ctx: &mut CompileCtx) {
let is_var = matches!(decl.kind, VariableDeclarationKind::Var);
for d in &decl.declarations {
if let Some(init) = &d.init {
self.count_expression(init, ctx);
if is_var {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
// `var` hoisting: register the name during the count pass (mirrors
// count_function_declaration) so function bodies emitted earlier in the
// hoisting sub-pass can resolve it. count_binding_pattern would only
// allocate a register without declaring the name.
let reg = ctx.alloc_reg();
let _ = ctx.declare_initialized(bi.name.as_str(), reg, VariableDeclarationKind::Var, false);
ctx.projected_pc += 1; // STORE_VAR
continue;
}
}
self.count_binding_pattern(&d.id, ctx);
} else {
ctx.alloc_reg();
ctx.count_words(2); // LOAD_CONST(undefined) + STORE_VAR
}
}
}
fn count_function_declaration(&self, decl: &oxide_parser::Function<'_>, ctx: &mut CompileCtx) {
let name = if let Some(id) = &decl.id {
id.name.to_string()
} else {
return;
};
let func_reg = ctx.alloc_reg();
let _ = ctx.declare_initialized(&name, func_reg, VariableDeclarationKind::Var, false);
// Body is compiled in the emit pass only.
ctx.count_words(2); // LOAD_CONST(BytecodeFunc) + STORE_VAR
}
fn count_class_declaration(&self, class: &oxide_parser::Class<'_>, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // class binding reg
self.count_class(class, ctx);
ctx.projected_pc += 1; // STORE_VAR binding <- ctor
}
pub(in crate::counter) fn count_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::VariableDeclaration(decl) => self.count_variable_declaration(decl, ctx),
Statement::FunctionDeclaration(fd) => self.count_function_declaration(fd, ctx),
Statement::ClassDeclaration(class) => self.count_class_declaration(class, ctx),
_ => {}
}
}
}
@@ -1,79 +0,0 @@
use super::*;
impl Compiler {
fn count_throw_statement(&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx) {
self.count_expression(&stmt.argument, ctx);
ctx.projected_pc += 1; // THROW
}
fn count_try_statement(&self, stmt: &oxide_parser::TryStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let catch_label = Label::CatchBody(id);
let try_end_label = Label::TryEnd(id);
let has_catch = stmt.handler.is_some();
let has_finally = stmt.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 &stmt.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.labels.label_map.insert(catch_label, ctx.projected_pc);
if let Some(catch) = &stmt.handler {
ctx.push_scope();
if let Some(param) = &catch.param {
// Mirror the emitter: the catch register is always allocated, but STORE_VAR is
// emitted only for an identifier binding — a destructuring catch param emits no
// store (that binding form is not implemented yet).
ctx.alloc_reg();
if let oxide_parser::BindingPattern::BindingIdentifier(_) = &param.pattern {
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) = &stmt.finalizer {
let finally_label = Label::FinallyBody(id);
ctx.labels.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.labels.label_map.insert(try_end_label, ctx.projected_pc);
}
pub(in crate::counter) fn count_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::ThrowStatement(ts) => self.count_throw_statement(ts, ctx),
Statement::TryStatement(ts) => self.count_try_statement(ts, ctx),
_ => {}
}
}
}
@@ -1,147 +0,0 @@
use super::*;
impl Compiler {
fn count_while_statement(&self, stmt: &oxide_parser::WhileStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::WhileStart(id);
let end_label = Label::WhileEnd(id);
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
self.count_expression(&stmt.test, ctx);
ctx.projected_pc += 1; // JMP_IF_FALSE
self.count_statement(&stmt.body, ctx);
ctx.projected_pc += 1; // JMP (backward)
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
fn count_do_while_statement(&self, stmt: &oxide_parser::DoWhileStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::DoWhileStart(id);
let end_label = Label::DoWhileEnd(id);
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
self.count_statement(&stmt.body, ctx);
self.count_expression(&stmt.test, ctx);
ctx.projected_pc += 1; // JMP_IF_TRUE (backward)
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
fn count_for_statement(&self, stmt: &oxide_parser::ForStatement<'_>, ctx: &mut CompileCtx) {
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) = &stmt.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(); // tmp holding undefined
ctx.alloc_reg(); // var register
ctx.count_words(2); // LOAD_CONST(undefined) + STORE_VAR
}
}
}
}
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
if let Some(test) = &stmt.test {
self.count_expression(test, ctx);
ctx.projected_pc += 1; // JMP_IF_FALSE
}
self.count_statement(&stmt.body, ctx);
ctx.labels.label_map.insert(update_label, ctx.projected_pc);
if let Some(update) = &stmt.update {
self.count_expression(update, ctx);
}
ctx.projected_pc += 1; // JMP (backward)
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
}
fn count_for_in_statement(&self, stmt: &oxide_parser::ForInStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::ForInStart(id);
let end_label = Label::ForInEnd(id);
self.count_expression(&stmt.right, ctx);
ctx.count_instr(); // FOR_IN_INIT
ctx.labels.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 &stmt.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(&stmt.body, ctx);
ctx.count_jump(); // JMP back to start
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
ctx.count_instr(); // FOR_IN_CLEANUP
}
fn count_for_of_statement(&self, stmt: &oxide_parser::ForOfStatement<'_>, ctx: &mut CompileCtx) {
let id = ctx.next_label_id();
let start_label = Label::ForOfStart(id);
let end_label = Label::ForOfEnd(id);
self.count_expression(&stmt.right, ctx);
ctx.count_instr(); // FOR_OF_INIT
ctx.labels.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 &stmt.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(&stmt.body, ctx);
ctx.count_jump(); // JMP back
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
ctx.count_instr(); // FOR_OF_CLOSE
}
pub(in crate::counter) fn count_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::WhileStatement(wh) => self.count_while_statement(wh, ctx),
Statement::DoWhileStatement(dw) => self.count_do_while_statement(dw, ctx),
Statement::ForStatement(fr) => self.count_for_statement(fr, ctx),
Statement::ForInStatement(fi) => self.count_for_in_statement(fi, ctx),
Statement::ForOfStatement(fo) => self.count_for_of_statement(fo, ctx),
_ => {}
}
}
}
@@ -1,80 +0,0 @@
use super::*;
impl Compiler {
fn emit_object_expression(&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let obj_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, obj_reg, 0, 0));
let prop_checkpoint = ctx.reg_checkpoint();
for prop in &obj.properties {
let oxide_parser::ObjectPropertyKind::ObjectProperty(p) = prop else {
return Err("spread properties not yet supported".into());
};
let prop_name = match &p.key {
oxide_parser::PropertyKey::StaticIdentifier(ident) => ident.name.as_str().to_string(),
oxide_parser::PropertyKey::StringLiteral(s) => s.value.to_string(),
_ => return Err("unsupported object property key type".into()),
};
match p.kind {
PropertyKind::Get | PropertyKind::Set => {
let accessor_reg = self.emit_expression(&p.value, ctx)?;
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
sub_mod.function_name = Some(prop_name.to_string());
}
let undef_reg = self.emit_undefined(ctx);
let (get_reg, set_reg) = if p.kind == PropertyKind::Get {
(accessor_reg, undef_reg)
} else {
(undef_reg, accessor_reg)
};
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
ctx.emit(opcode::encode(OpCode::DEFINE_ACCESSOR, obj_reg, get_reg, set_reg));
ctx.emit(idx as u32);
ctx.restore_reg_checkpoint(prop_checkpoint);
}
_ => {
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
let key_reg = ctx.alloc_reg();
ctx.emit_load_const(key_reg, idx);
let val_reg = self.emit_expression(&p.value, ctx)?;
// Name inference: if property value is an arrow function,
// set the compiled sub_module's function_name to the property key.
if matches!(&p.value, Expression::ArrowFunctionExpression(_)) {
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
sub_mod.function_name = Some(prop_name.to_string());
}
}
ctx.emit(opcode::encode(OpCode::SET_PROP, obj_reg, val_reg, key_reg));
ctx.restore_reg_checkpoint(prop_checkpoint);
}
}
}
Ok(obj_reg)
}
fn emit_array_expression(&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let arr_reg = ctx.alloc_reg();
let n = arr.elements.len() as u16;
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, arr_reg, (n & 0xFF) as u8, ((n >> 8) & 0xFF) as u8));
let elem_checkpoint = ctx.reg_checkpoint();
for (i, elem) in arr.elements.iter().enumerate() {
let Some(e) = elem.as_expression() else {
return Err("spread not supported".into());
};
let val_reg = self.emit_expression(e, ctx)?;
let idx_reg = ctx.alloc_reg();
let idx = ctx.add_constant(Constant::Int(i as i32));
ctx.emit_load_const(idx_reg, idx);
ctx.emit(opcode::encode(OpCode::SET_ELEM, arr_reg, idx_reg, val_reg));
ctx.restore_reg_checkpoint(elem_checkpoint);
}
Ok(arr_reg)
}
pub(in crate::emitter) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx),
Expression::ArrayExpression(arr) => self.emit_array_expression(arr, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1,138 +0,0 @@
use super::*;
impl Compiler {
fn emit_template_literal_expression(
&self, tl: &oxide_parser::TemplateLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let r = ctx.alloc_reg();
let quasis = &tl.quasis;
let expressions = &tl.expressions;
let segment_count = quasis.len() + expressions.len();
// Evaluate each expression, collecting registers
let expr_regs: Vec<u8> = expressions
.iter()
.map(|e| self.emit_expression(e, ctx))
.collect::<Result<Vec<_>, _>>()?;
// Add each quasi string to constant pool
let quasi_const_idxs: Vec<u16> = quasis
.iter()
.map(|q| {
let s = q.value.cooked.as_ref().map(|c| c.to_string()).unwrap_or_default();
ctx.add_constant(Constant::String(s))
})
.collect();
// Compute total length hint
let total_len_hint: usize = quasis
.iter()
.map(|q| q.value.cooked.as_ref().map(|c| c.len()).unwrap_or(0))
.sum();
// Emit TEMPLATE_STR rd, 0, 0
ctx.emit(opcode::encode(OpCode::TEMPLATE_STR, r, 0, 0));
// Ext word 0: (segment_count << 16) | (total_len_hint & 0xFFFF)
ctx.emit(((segment_count as u32) << 16) | (total_len_hint as u32 & 0xFFFF));
// Interleave segments: quasi[0], expr[0], quasi[1], expr[1], ...
let mut expr_iter = expr_regs.iter();
for const_idx in quasi_const_idxs.iter() {
// Quasi: is_expression=0, bits 0-15 = const_idx
ctx.emit(*const_idx as u32 & 0x7FFF_FFFF);
// Expression (if any remaining)
if let Some(expr_reg) = expr_iter.next() {
// Expression: is_expression=1, bits 0-15 = reg
ctx.emit(0x8000_0000u32 | (*expr_reg as u32));
}
}
Ok(r)
}
fn emit_tagged_template_expression(
&self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let quasis = &tt.quasi.quasis;
let expressions = &tt.quasi.expressions;
// 1. Evaluate tag expression
let tag_reg = self.emit_expression(&tt.tag, ctx)?;
// 2. Build cooked strings array (into a temp register)
let cooked_temp = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, cooked_temp, quasis.len() as u8, 0));
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.emit_load_const(str_reg, const_idx);
let idx_const = ctx.add_constant(Constant::Int(i as i32));
let idx_reg = ctx.alloc_reg();
ctx.emit_load_const(idx_reg, idx_const);
ctx.emit(opcode::encode(OpCode::SET_ELEM, cooked_temp, idx_reg, str_reg));
}
// 3. Build raw strings array (into a temp register)
let raw_temp = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, raw_temp, quasis.len() as u8, 0));
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.emit_load_const(str_reg, const_idx);
let idx_const = ctx.add_constant(Constant::Int(i as i32));
let idx_reg = ctx.alloc_reg();
ctx.emit_load_const(idx_reg, idx_const);
ctx.emit(opcode::encode(OpCode::SET_ELEM, raw_temp, idx_reg, str_reg));
}
// 4. Evaluate expression arguments (into temp registers)
let mut expr_temps = Vec::new();
for expr in expressions {
expr_temps.push(self.emit_expression(expr, ctx)?);
}
// 5. Allocate consecutive argument slots: cooked, raw, expr[0], expr[1], ...
let cooked_slot = ctx.alloc_reg();
let raw_slot = ctx.alloc_reg();
let mut expr_slots = Vec::new();
for _ in expressions {
expr_slots.push(ctx.alloc_reg());
}
// Copy temps to consecutive slots using LOAD_VAR (register-to-register move)
ctx.emit(opcode::encode(OpCode::LOAD_VAR, cooked_slot, cooked_temp, 0));
ctx.emit(opcode::encode(OpCode::LOAD_VAR, raw_slot, raw_temp, 0));
for (slot, temp) in expr_slots.iter().zip(expr_temps.iter()) {
ctx.emit(opcode::encode(OpCode::LOAD_VAR, *slot, *temp, 0));
}
// 6. Emit undefined as this_arg
let undef_idx = ctx.add_constant(Constant::Undefined);
let undef_reg = ctx.alloc_reg();
ctx.emit_load_const(undef_reg, undef_idx);
// 7. Emit CALL(tag, undefined, cooked_slot)
let arg_count = 2 + expressions.len();
ctx.emit(opcode::encode(OpCode::CALL, tag_reg, undef_reg, cooked_slot));
ctx.emit(arg_count as u32);
// 8. Result from regs[0]
let result_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
Ok(result_reg)
}
pub(in crate::emitter) fn emit_template_domain(
&self, expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match expr {
Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx),
Expression::TaggedTemplateExpression(tt) => self.emit_tagged_template_expression(tt, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
-969
View File
@@ -1,969 +0,0 @@
#![allow(unused_imports)]
use crate::compiler::Label;
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,
MethodDefinitionKind, ObjectAssignmentTarget, PropertyKey, PropertyKind, SimpleAssignmentTarget, Statement,
UnaryOperator, UpdateOperator, VariableDeclarationKind,
};
use crate::compiler::{
is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext, ParamSpec,
};
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)?;
let dup_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, dup_reg, reg, 0));
let offset = (short_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_nullish(dup_reg, offset));
Ok(())
}
fn emit_static_member_get_preserve_base(
&self, member: &oxide_parser::StaticMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<(u8, u8), String> {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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);
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);
Ok((value_reg, obj_reg))
}
fn emit_computed_member_get_preserve_base(
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<(u8, u8), String> {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok((value_reg, obj_reg))
}
fn emit_chain_call(
&self, call: &oxide_parser::CallExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let (callee_reg, this_reg) = match &call.callee {
Expression::StaticMemberExpression(member) => {
self.emit_static_member_get_preserve_base(member, short_label, ctx)?
}
Expression::ComputedMemberExpression(member) => {
self.emit_computed_member_get_preserve_base(member, short_label, ctx)?
}
Expression::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
(callee_reg, obj_reg)
}
_ => {
let callee_reg = self.emit_chainable_expression(&call.callee, short_label, ctx)?;
if call.optional {
if let Some(label) = short_label {
self.emit_optional_guard(callee_reg, label, ctx)?;
}
}
let this_idx = ctx.add_constant(Constant::Undefined);
let this_reg = ctx.alloc_reg();
ctx.emit_load_const(this_reg, this_idx);
(callee_reg, this_reg)
}
};
if call.optional
&& matches!(
&call.callee,
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
)
{
if let Some(label) = short_label {
self.emit_optional_guard(callee_reg, label, ctx)?;
}
}
let mut arg_regs = Vec::new();
for arg in &call.arguments {
if let Some(expr) = arg.as_expression() {
arg_regs.push(self.emit_expression(expr, ctx)?);
}
}
let first_arg_reg = if arg_regs.is_empty() { 0u8 } else { arg_regs[0] };
let op = match &call.callee {
Expression::Identifier(ident) if ctx.is_builtin(ident.name.as_str()) => OpCode::CALL_NATIVE,
_ => OpCode::CALL,
};
ctx.emit(opcode::encode(op, callee_reg, this_reg, first_arg_reg));
ctx.emit(arg_regs.len() as u32);
let result_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
Ok(result_reg)
}
fn emit_chainable_expression(
&self, expr: &Expression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match expr {
Expression::StaticMemberExpression(member) => {
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
Expression::ComputedMemberExpression(member) => {
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
Expression::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok(value_reg)
}
Expression::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
Expression::ChainExpression(chain) => self.emit_chain_element(&chain.expression, short_label, ctx),
_ => self.emit_expression(expr, ctx),
}
}
pub(crate) fn emit_chain_element(
&self, element: &ChainElement, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match element {
ChainElement::StaticMemberExpression(member) => {
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
ChainElement::ComputedMemberExpression(member) => {
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
ChainElement::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok(value_reg)
}
ChainElement::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
ChainElement::TSNonNullExpression(_) => Err("TS non-null expressions are not supported in JS mode".into()),
}
}
pub(crate) fn emit_logical_assign_test(
&self, op: LogicalOperator, test_reg: u8, store_label: Label, end_label: Label, ctx: &mut CompileCtx,
) -> Result<(), String> {
match op {
LogicalOperator::And => {
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
}
LogicalOperator::Or => {
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_true(test_reg, offset));
}
LogicalOperator::Coalesce => {
let store_pos = ctx.resolve_label(store_label)?;
let offset = (store_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_nullish(test_reg, offset));
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
}
}
Ok(())
}
fn private_name_id(&self, name: &str, ctx: &CompileCtx) -> Result<u32, String> {
ctx.scopes
.private_name_map
.iter()
.find_map(|(n, id)| (n == name).then_some(*id))
.ok_or_else(|| format!("private name #{name} is not defined"))
}
pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result<u8, String> {
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);
Ok(reg)
}
fn emit_class_key_reg(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) -> Result<u8, String> {
if !computed {
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);
return Ok(reg);
}
if matches!(key, PropertyKey::PrivateIdentifier(_)) {
return Err("private class elements not yet supported".into());
}
self.emit_expression(key.to_expression(), ctx)
}
pub(crate) fn count_class_key(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) {
if computed {
self.count_expression(key.to_expression(), ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
fn emit_public_field_init(
&self, target_reg: u8, 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 {
self.emit_expression(expr, ctx)?
} else {
self.emit_undefined(ctx)
};
if computed {
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, target_reg, key_reg, value_reg));
} else {
ctx.emit(opcode::encode(OpCode::SET_PROP, target_reg, value_reg, key_reg));
}
Ok(())
}
fn emit_private_field_init(
&self, target_reg: u8, 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 {
self.emit_expression(expr, ctx)?
} else {
self.emit_undefined(ctx)
};
ctx.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, value_reg, key_reg));
Ok(())
}
fn emit_private_method_init(
&self, target_reg: u8, method: &oxide_parser::MethodDefinition, home_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let PropertyKey::PrivateIdentifier(private) = &method.key else {
return Err("expected private method key".into());
};
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));
Ok(())
}
fn count_public_field_init(
&self, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
) {
self.count_class_key(key, computed, ctx);
if let Some(expr) = value {
self.count_expression(expr, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
ctx.projected_pc += 1;
}
fn count_private_field_init(&self, value: Option<&Expression>, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.projected_pc += 1;
if let Some(expr) = value {
self.count_expression(expr, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
ctx.projected_pc += 1;
}
pub(crate) fn static_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::Identifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::StringLiteral(s) => Ok(s.value.to_string()),
PropertyKey::NumericLiteral(n) => Ok(n.value.to_string()),
_ => Err("computed destructuring keys not yet supported".into()),
}
}
fn emit_default_if_undefined(
&self, val_reg: u8, default_expr: &Expression, ctx: &mut CompileCtx,
) -> 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));
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);
Ok(val_reg)
}
fn emit_bind_target(
&self, name: &str, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let var_reg = ctx.alloc_reg();
let target_reg = if matches!(kind, VariableDeclarationKind::Var) {
match ctx.declare(name, var_reg, kind, is_const) {
Ok(()) => var_reg,
Err(_) => ctx.lookup(name).unwrap_or(var_reg),
}
} else {
ctx.declare(name, var_reg, kind, is_const)?;
var_reg
};
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));
} else {
ctx.emit(opcode::encode(OpCode::STORE_VAR, target_reg, src_reg, if is_const { 1 } else { 0 }));
}
ctx.init_var(name);
Ok(())
}
pub(crate) fn emit_assign_target(
&self, target: &AssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTarget::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
AssignmentTarget::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTarget::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
_ => Err("assignment target not supported".into()),
}
}
pub(crate) fn emit_binding_pattern(
&self, pattern: &BindingPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
match pattern {
BindingPattern::BindingIdentifier(bi) => {
self.emit_bind_target(bi.name.as_str(), src_reg, kind, is_const, ctx)
}
BindingPattern::ArrayPattern(ap) => self.emit_array_binding(ap, src_reg, kind, is_const, ctx),
BindingPattern::ObjectPattern(op) => self.emit_object_binding(op, src_reg, kind, is_const, ctx),
BindingPattern::AssignmentPattern(ap) => {
let val_reg = self.emit_default_if_undefined(src_reg, &ap.right, ctx)?;
self.emit_binding_pattern(&ap.left, val_reg, kind, is_const, ctx)
}
}
}
fn emit_array_binding(
&self, ap: &oxide_parser::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));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(pattern) = elem {
self.emit_binding_pattern(pattern, val_reg, kind, is_const, ctx)?;
}
}
if let Some(rest) = &ap.rest {
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));
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));
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();
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));
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));
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);
Ok(rest_reg)
}
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));
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);
prop_reg
}
fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result<u8, String> {
match key {
PropertyKey::Identifier(ident) => {
let name = ident.name.as_str();
let var_reg = ctx.lookup_or_builtin(name)?;
let key_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, key_reg, var_reg, 0));
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);
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);
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);
Ok(key_reg)
}
_ => Err("computed destructuring key expression not supported".into()),
}
}
fn emit_object_property_read_key(
&self, src_reg: u8, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
) -> Result<(u8, Option<String>), String> {
if !computed {
let key_name = self.static_property_name(key)?;
let prop_reg = self.emit_object_property_read(src_reg, &key_name, ctx);
return Ok((prop_reg, Some(key_name)));
}
let prop_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
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));
Ok((val_reg, None))
}
fn emit_object_binding(
&self, op: &oxide_parser::ObjectPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
let (prop_reg, static_key) = self.emit_object_property_read_key(src_reg, &prop.key, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_binding_pattern(&prop.value, prop_reg, kind, is_const, ctx)?;
}
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);
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
Ok(())
}
fn emit_assignment_maybe_default(
&self, target: &AssignmentTargetMaybeDefault, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
let val_reg = self.emit_default_if_undefined(src_reg, &default.init, ctx)?;
self.emit_assign_target(&default.binding, val_reg, ctx)
}
AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
_ => Err("assignment target not supported".into()),
}
}
pub(crate) fn emit_array_assignment(
&self, ap: &oxide_parser::ArrayAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, src_reg, 0));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(target) = elem {
self.emit_assignment_maybe_default(target, val_reg, ctx)?;
}
}
if let Some(rest) = &ap.rest {
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));
Ok(())
}
pub(crate) fn emit_object_assignment(
&self, op: &ObjectAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
match prop {
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
let key = id.binding.name.as_str().to_string();
excluded.push(key.clone());
let mut prop_reg = self.emit_object_property_read(src_reg, &key, ctx);
if let Some(default_expr) = &id.init {
prop_reg = self.emit_default_if_undefined(prop_reg, default_expr, ctx)?;
}
let name = id.binding.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
prop_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
}
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
let (prop_reg, static_key) =
self.emit_object_property_read_key(src_reg, &prop.name, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_assignment_maybe_default(&prop.binding, prop_reg, ctx)?;
}
}
}
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);
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
Ok(())
}
fn class_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::Identifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::StringLiteral(s) => Ok(s.value.to_string()),
PropertyKey::NumericLiteral(n) => Ok(n.value.to_string()),
PropertyKey::PrivateIdentifier(_) => Err("private class elements not yet supported".into()),
_ => Err("unsupported class property key type".into()),
}
}
pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
let mut constructor_method = None;
let mut instance_fields = Vec::new();
let mut private_names = Vec::<(String, u32)>::new();
let is_derived = class.super_class.is_some();
for element in &class.body.body {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if let PropertyKey::PrivateIdentifier(private) = &method.key {
let name = private.name.as_str().to_string();
if private_names.iter().any(|(existing, _)| existing == &name) {
return Err(format!("duplicate private name #{name}"));
}
let id = ctx.scopes.next_private_name_id;
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
private_names.push((name, id));
}
if method.kind == MethodDefinitionKind::Constructor {
if constructor_method.is_some() {
return Err("duplicate class constructor".into());
}
constructor_method = Some(method);
}
}
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
let name = private.name.as_str().to_string();
if private_names.iter().any(|(existing, _)| existing == &name) {
return Err(format!("duplicate private name #{name}"));
}
let id = ctx.scopes.next_private_name_id;
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
private_names.push((name, id));
}
if !prop.r#static {
instance_fields.push(prop);
}
}
ClassElement::AccessorProperty(_) => return Err("class accessor properties not yet supported".into()),
ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => {}
}
}
let ctor_name = class.id.as_ref().map(|id| id.name.to_string());
let ctor_reg = ctx.alloc_reg();
let proto_reg = ctx.alloc_reg();
let self_binding = ctor_name.as_deref().map(|name| vec![(name, ctor_reg)]).unwrap_or_default();
let super_reg = if let Some(super_expr) = &class.super_class {
Some(self.emit_expression(super_expr, ctx)?)
} else {
None
};
let saved_derived = ctx.in_derived_constructor;
let saved_private_names = ctx.scopes.private_name_map.clone();
ctx.in_derived_constructor = is_derived;
ctx.scopes.private_name_map = private_names.clone();
let mut ctor_module = if let Some(method) = constructor_method {
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
self.compile_function_body_with_field_hooks(
&param_names,
body_stmts,
ctx,
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
compiler.emit_private_field_init(
254,
private.name.as_str(),
field.value.as_ref(),
field_ctx,
)?;
} else {
compiler.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
)?;
}
}
Ok(())
}),
is_derived,
)?
} else {
let mut module = self.compile_function_body_with_field_hooks(
&[],
&[],
ctx,
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
compiler.emit_private_field_init(
254,
private.name.as_str(),
field.value.as_ref(),
field_ctx,
)?;
} else {
compiler.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
)?;
}
}
Ok(())
}),
is_derived,
)?;
if is_derived {
module.bytecode.clear();
module.constants.clear();
module.n_registers = 1;
module.bytecode.push(opcode::encode(OpCode::SUPER_CALL, 0, 0, 0));
module.bytecode.push(0);
let mut field_ctx = CompileCtx::new();
field_ctx.scopes.private_name_map = private_names.clone();
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
self.emit_private_field_init(254, private.name.as_str(), field.value.as_ref(), &mut field_ctx)?;
} else {
self.emit_public_field_init(
254,
&field.key,
field.computed,
field.value.as_ref(),
&mut field_ctx,
)?;
}
}
module.bytecode.extend(field_ctx.bytecode);
module.constants = field_ctx.constants;
module.n_registers = field_ctx.max_regs.max(1);
module.bytecode.push(opcode::encode(OpCode::RETURN, 0, 0, 0));
}
module
};
ctx.in_derived_constructor = saved_derived;
ctor_module.is_class_constructor = true;
ctor_module.is_derived_constructor = is_derived;
ctor_module.function_name = ctor_name.clone();
ctx.sub_modules.push(ctor_module);
ctx.emit_create_closure(ctor_reg, ctx.sub_modules.len() as u32);
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, proto_reg, 0, 0));
if let Some(super_reg) = super_reg {
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let parent_proto_key_reg = ctx.alloc_reg();
ctx.emit_load_const(parent_proto_key_reg, proto_key_idx);
let parent_proto_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::GET_PROP, super_reg, parent_proto_reg, parent_proto_key_reg));
let proto_link_idx = ctx.add_constant(Constant::String("__proto__".to_string()));
let proto_link_key_reg = ctx.alloc_reg();
ctx.emit_load_const(proto_link_key_reg, proto_link_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, proto_reg, parent_proto_reg, proto_link_key_reg));
ctx.emit(opcode::encode(OpCode::SET_PROP, ctor_reg, super_reg, proto_link_key_reg));
}
let ctor_key_idx = ctx.add_constant(Constant::String("constructor".to_string()));
let ctor_key_reg = ctx.alloc_reg();
ctx.emit_load_const(ctor_key_reg, ctor_key_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, proto_reg, ctor_reg, ctor_key_reg));
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
let proto_key_reg = ctx.alloc_reg();
ctx.emit_load_const(proto_key_reg, proto_key_idx);
ctx.emit(opcode::encode(OpCode::SET_PROP, ctor_reg, proto_reg, proto_key_reg));
for element in &class.body.body {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if method.kind == MethodDefinitionKind::Constructor {
continue;
}
if matches!(method.key, PropertyKey::PrivateIdentifier(_)) {
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
self.emit_private_method_init(home_reg, method, home_reg, ctx)?;
continue;
}
let home_reg = if method.r#static { ctor_reg } else { proto_reg };
let key_reg = self.emit_class_key_reg(&method.key, method.computed, ctx)?;
let method_name = if method.computed {
"<computed>".to_string()
} else {
self.class_property_name(&method.key)?
};
let accessor_reg =
self.emit_class_method_function(method, &method_name, home_reg, ctx, &self_binding)?;
match method.kind {
MethodDefinitionKind::Method => {
if method.computed {
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, home_reg, key_reg, accessor_reg));
} else {
ctx.emit(opcode::encode(OpCode::SET_PROP, home_reg, accessor_reg, key_reg));
}
}
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
if method.computed {
return Err("computed class accessors not yet supported".into());
}
let undef_reg = self.emit_undefined(ctx);
let (get_reg, set_reg) = if method.kind == MethodDefinitionKind::Get {
(accessor_reg, undef_reg)
} else {
(undef_reg, accessor_reg)
};
let key_name = self.class_property_name(&method.key)?;
let key_idx = ctx.add_constant(Constant::String(key_name));
ctx.emit(opcode::encode(OpCode::DEFINE_ACCESSOR, home_reg, get_reg, set_reg));
ctx.emit(key_idx as u32);
}
MethodDefinitionKind::Constructor => continue,
}
}
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if prop.r#static {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
self.emit_private_field_init(ctor_reg, private.name.as_str(), prop.value.as_ref(), ctx)?;
} else {
self.emit_public_field_init(ctor_reg, &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
}
ctx.static_block_this_reg = saved_static_this;
}
}
ClassElement::StaticBlock(block) => {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
ctx.push_scope();
for stmt in &block.body {
self.emit_statement(stmt, ctx)?;
}
ctx.pop_scope();
ctx.static_block_this_reg = saved_static_this;
}
ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => {}
}
}
ctx.scopes.private_name_map = saved_private_names;
Ok(ctor_reg)
}
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);
reg
}
fn emit_class_method_function(
&self, method: &oxide_parser::MethodDefinition, method_name: &str, home_reg: u8, ctx: &mut CompileCtx,
self_binding: &[(&str, u8)],
) -> Result<u8, String> {
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
let saved_instance = ctx.in_instance_method;
let saved_static = ctx.in_static_method;
ctx.in_instance_method = !method.r#static;
ctx.in_static_method = method.r#static;
let mut method_module = self.compile_function_body_with_bindings(
&param_names,
body_stmts,
ctx,
false,
self_binding,
FunctionBodyContext::ClassElement,
)?;
ctx.in_instance_method = saved_instance;
ctx.in_static_method = saved_static;
method_module.function_name = Some(method_name.to_string());
method_module.needs_home_object = true;
ctx.sub_modules.push(method_module);
let method_reg = ctx.alloc_reg();
ctx.emit_create_closure(method_reg, ctx.sub_modules.len() as u32);
ctx.emit(opcode::encode(OpCode::SET_HOME_OBJECT, method_reg, home_reg, 0));
Ok(method_reg)
}
}
@@ -1,120 +0,0 @@
use super::*;
impl Compiler {
fn emit_variable_declaration_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
let Statement::VariableDeclaration(decl) = stmt else {
return Ok(None);
};
let mut r = None;
for d in &decl.declarations {
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
if is_const && d.init.is_none() {
return Err("const declaration must have an initializer".into());
}
if let Some(init) = &d.init {
let val_reg = self.emit_expression(init, ctx)?;
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
if let BindingPattern::BindingIdentifier(bi) = &d.id {
if matches!(*init, Expression::ArrowFunctionExpression(_)) {
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
sub_mod.function_name = Some(bi.name.to_string());
}
}
}
r = Some(val_reg);
} else {
let BindingPattern::BindingIdentifier(bi) = &d.id else {
return Err("destructuring declaration requires an initializer".into());
};
let idx = ctx.add_constant(Constant::Undefined);
let tmp = ctx.alloc_reg();
ctx.emit_load_const(tmp, idx);
let var_reg = ctx.alloc_reg();
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
let is_captured = ctx.scopes.symbols.lookup_is_captured(bi.name.as_str());
if is_captured {
let cell_idx = ctx.scopes.cell_registry.len() as u8;
ctx.scopes.cell_registry.push((bi.name.to_string(), cell_idx));
ctx.emit(opcode::encode(OpCode::MAKE_CELL, tmp, cell_idx, 0));
} else {
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, tmp, 0));
}
ctx.init_var(bi.name.as_str());
r = Some(var_reg);
}
}
Ok(r)
}
fn emit_function_declaration_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
let Statement::FunctionDeclaration(fd) = stmt else {
return Ok(None);
};
let name = if let Some(id) = &fd.id {
id.name.to_string()
} else {
return Err("FunctionDeclaration without name".into());
};
let mut param_names = Vec::new();
for (idx, param) in fd.params.items.iter().enumerate() {
match &param.pattern {
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
param_names.push(ParamSpec::Identifier(bi.name.to_string()));
}
pattern => {
param_names.push(ParamSpec::Pattern {
synthetic_name: format!("@@param_{idx}"),
pattern,
});
}
}
}
let body_stmts: &[Statement] = if let Some(body) = &fd.body { &body.statements } else { &[] };
let mut sub_module = self.compile_function_body(&param_names, body_stmts, ctx, false, false)?;
sub_module.function_name = Some(name.clone());
ctx.sub_modules.push(sub_module);
let sub_idx = ctx.sub_modules.len() as u32;
let var_reg = ctx.lookup(&name)?;
ctx.reserve_reg(var_reg);
ctx.emit_create_closure(var_reg, sub_idx);
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, var_reg, 0));
Ok(None)
}
fn emit_class_declaration_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ClassDeclaration(class) = stmt else {
return Ok(None);
};
let name = class
.id
.as_ref()
.map(|id| id.name.to_string())
.ok_or_else(|| "ClassDeclaration without name".to_string())?;
let var_reg = ctx.alloc_reg();
ctx.declare(&name, var_reg, VariableDeclarationKind::Let, false)?;
ctx.init_var(&name);
let ctor_reg = self.emit_class(class, ctx)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, ctor_reg, 0));
Ok(None)
}
pub(in crate::emitter) fn emit_declaration_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx),
Statement::FunctionDeclaration(_) => self.emit_function_declaration_statement(stmt, ctx),
Statement::ClassDeclaration(_) => self.emit_class_declaration_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,131 +0,0 @@
use super::*;
impl Compiler {
fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ThrowStatement(ts) = stmt else {
return Ok(None);
};
let exc_reg = self.emit_expression(&ts.argument, ctx)?;
ctx.emit(opcode::encode(OpCode::THROW, exc_reg, 0, 0));
Ok(None)
}
fn emit_try_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::TryStatement(ts) = stmt else {
return Ok(None);
};
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();
let result_reg = ctx.alloc_reg();
let mut try_finally_begin_pos: Option<usize> = None;
let mut try_begin_pos: Option<usize> = None;
if has_finally {
try_finally_begin_pos = Some(ctx.bytecode.len());
ctx.emit(opcode::encode_try_finally_begin(0));
}
if has_catch {
try_begin_pos = Some(ctx.bytecode.len());
ctx.emit(opcode::encode_try_begin(0));
}
let mut last_try_result: Option<u8> = None;
for s in &ts.block.body {
if let Some(r) = self.emit_statement(s, ctx)? {
last_try_result = Some(r);
}
}
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_try_result.unwrap_or(result_reg), 0));
if has_catch {
ctx.emit(opcode::encode(OpCode::TRY_END, 0, 0, 0));
}
let jmp_needed = has_catch || has_finally;
let jmp_skip_pos = if jmp_needed {
let pos = ctx.bytecode.len();
ctx.emit(opcode::encode_jmp(0));
Some(pos)
} else {
None
};
let catch_label_pc = ctx.bytecode.len();
ctx.labels.label_map.insert(catch_label, catch_label_pc);
if let Some(try_begin_pc) = try_begin_pos {
let offset = catch_label_pc as isize - (try_begin_pc as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.bytecode[try_begin_pc] = opcode::encode_try_begin(offset);
}
if let Some(catch) = &ts.handler {
ctx.push_scope();
if let Some(param) = &catch.param {
let catch_reg = ctx.alloc_reg();
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &param.pattern {
ctx.declare_initialized(bi.name.as_str(), catch_reg, VariableDeclarationKind::Let, false)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, catch_reg, 0, 0));
}
}
let mut last_catch_result: Option<u8> = None;
for s in &catch.body.body {
if let Some(r) = self.emit_statement(s, ctx)? {
last_catch_result = Some(r);
}
}
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_catch_result.unwrap_or(result_reg), 0));
ctx.pop_scope();
}
if has_finally {
let finally_label = Label::FinallyBody(id);
let finally_label_pc = ctx.bytecode.len();
ctx.labels.label_map.insert(finally_label, finally_label_pc);
if let Some(fb_pos) = try_finally_begin_pos {
let offset = finally_label_pc as isize - (fb_pos as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.bytecode[fb_pos] = opcode::encode_try_finally_begin(offset);
}
if let Some(jmp_pos) = jmp_skip_pos {
let offset = finally_label_pc as isize - (jmp_pos as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.bytecode[jmp_pos] = opcode::encode_jmp(offset);
}
let mut last_finally_result: Option<u8> = None;
for s in &ts.finalizer.as_ref().unwrap().body {
if let Some(r) = self.emit_statement(s, ctx)? {
last_finally_result = Some(r);
}
}
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_finally_result.unwrap_or(result_reg), 0));
ctx.emit(opcode::encode(OpCode::TRY_FINALLY_END, 0, 0, 0));
} else if let Some(jmp_pos) = jmp_skip_pos {
let offset = ctx.bytecode.len() as isize - (jmp_pos as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.bytecode[jmp_pos] = opcode::encode_jmp(offset);
}
let try_end_pc = ctx.bytecode.len();
ctx.labels.label_map.insert(try_end_label, try_end_pc);
Ok(Some(result_reg))
}
pub(in crate::emitter) fn emit_exception_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::ThrowStatement(_) => self.emit_throw_statement(stmt, ctx),
Statement::TryStatement(_) => self.emit_try_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,245 +0,0 @@
use super::*;
impl Compiler {
fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::WhileStatement(wh) = stmt else {
return Ok(None);
};
let id = ctx.next_label_id();
let start_label = Label::WhileStart(id);
let end_label = Label::WhileEnd(id);
ctx.push_loop(end_label, start_label);
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
let test_reg = self.emit_expression(&wh.test, ctx)?;
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
self.emit_statement(&wh.body, ctx)?;
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
ctx.pop_label_scopes(n_labeled);
ctx.pop_loop();
Ok(None)
}
fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::DoWhileStatement(dw) = stmt else {
return Ok(None);
};
let id = ctx.next_label_id();
let start_label = Label::DoWhileStart(id);
let end_label = Label::DoWhileEnd(id);
ctx.push_loop(end_label, start_label);
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
self.emit_statement(&dw.body, ctx)?;
let test_reg = self.emit_expression(&dw.test, ctx)?;
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_true(test_reg, offset));
ctx.pop_label_scopes(n_labeled);
ctx.pop_loop();
Ok(None)
}
fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForStatement(fr) = stmt else {
return Ok(None);
};
let id = ctx.next_label_id();
let start_label = Label::ForStart(id);
let update_label = Label::ForUpdate(id);
let end_label = Label::ForEnd(id);
ctx.push_loop(end_label, update_label);
let n_labeled = ctx.take_pending_loop_labels(end_label, update_label);
if let Some(init) = &fr.init {
if let Some(expr) = init.as_expression() {
self.emit_expression(expr, ctx)?;
} else if let ForStatementInit::VariableDeclaration(decl) = init {
for d in &decl.declarations {
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
if let Some(init_expr) = &d.init {
let val_reg = self.emit_expression(init_expr, ctx)?;
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
} else if let BindingPattern::BindingIdentifier(bi) = &d.id {
// `for (var i; ...)` initializes `i` to undefined, mirroring a
// statement-level `var i;` — emit LOAD_CONST undefined + STORE_VAR.
// The count pass already counts these two instructions; emitting
// nothing here drifts every jump target after the for-init.
let idx = ctx.add_constant(Constant::Undefined);
let tmp = ctx.alloc_reg();
ctx.emit_load_const(tmp, idx);
let var_reg = ctx.alloc_reg();
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, tmp, 0));
ctx.init_var(bi.name.as_str());
}
}
}
}
if let Some(test) = &fr.test {
let test_reg = self.emit_expression(test, ctx)?;
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
}
self.emit_statement(&fr.body, ctx)?;
if let Some(update) = &fr.update {
self.emit_expression(update, ctx)?;
}
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
ctx.pop_label_scopes(n_labeled);
ctx.pop_loop();
Ok(None)
}
fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForInStatement(fi) = stmt else {
return Ok(None);
};
let id = ctx.next_label_id();
let start_label = Label::ForInStart(id);
let end_label = Label::ForInEnd(id);
let obj_reg = self.emit_expression(&fi.right, ctx)?;
ctx.emit(opcode::encode(OpCode::FOR_IN_INIT, 0, obj_reg, 0));
ctx.push_loop(end_label, start_label);
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
let done_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_IN_DONE, done_reg, 0, 0));
let end_pos = ctx.resolve_label(end_label)?;
let cleanup_jmp_offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
ctx.emit(opcode::encode_jmp_if_false(done_reg, 2));
let cleanup_jmp_offset = ctx.checked_jump_offset(cleanup_jmp_offset);
ctx.emit(opcode::encode_jmp(cleanup_jmp_offset));
let key_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_IN_NEXT, key_reg, 0, 0));
match &fi.left {
ForStatementLeft::VariableDeclaration(decl) => {
for d in &decl.declarations {
let name = match &d.id {
oxide_parser::BindingPattern::BindingIdentifier(bi) => bi.name.as_str(),
_ => return Err("destructuring not supported".into()),
};
let var_reg = ctx.alloc_reg();
ctx.declare(name, var_reg, decl.kind, matches!(decl.kind, VariableDeclarationKind::Const))?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, key_reg, 0));
ctx.init_var(name);
}
}
ForStatementLeft::AssignmentTargetIdentifier(id_ref) => {
let name = id_ref.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, key_reg, 0));
}
_ => return Err("unsupported for-in left-hand side".into()),
}
self.emit_statement(&fi.body, ctx)?;
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
ctx.emit(opcode::encode(OpCode::FOR_IN_CLEANUP, 0, 0, 0));
ctx.pop_label_scopes(n_labeled);
ctx.pop_loop();
Ok(None)
}
fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForOfStatement(fo) = stmt else {
return Ok(None);
};
let id = ctx.next_label_id();
let start_label = Label::ForOfStart(id);
let end_label = Label::ForOfEnd(id);
let iter_src_reg = self.emit_expression(&fo.right, ctx)?;
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, iter_src_reg, 0));
ctx.push_loop(end_label, start_label);
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_false(has_reg, offset));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
match &fo.left {
ForStatementLeft::VariableDeclaration(decl) => {
for d in &decl.declarations {
self.emit_binding_pattern(&d.id, val_reg, decl.kind, false, ctx)?;
}
}
ForStatementLeft::AssignmentTargetIdentifier(id_ref) => {
let name = id_ref.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, 0));
}
ForStatementLeft::ArrayAssignmentTarget(ap) => {
self.emit_array_assignment(ap, val_reg, ctx)?;
}
ForStatementLeft::ObjectAssignmentTarget(op) => {
self.emit_object_assignment(op, val_reg, ctx)?;
}
_ => return Err("unsupported for-of left-hand side".into()),
}
self.emit_statement(&fo.body, ctx)?;
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
ctx.pop_label_scopes(n_labeled);
ctx.pop_loop();
Ok(None)
}
pub(in crate::emitter) fn emit_iteration_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx),
Statement::DoWhileStatement(_) => self.emit_do_while_statement(stmt, ctx),
Statement::ForStatement(_) => self.emit_for_statement(stmt, ctx),
Statement::ForInStatement(_) => self.emit_for_in_statement(stmt, ctx),
Statement::ForOfStatement(_) => self.emit_for_of_statement(stmt, ctx),
_ => Ok(None),
}
}
}
+4 -3
View File
@@ -1,10 +1,11 @@
#![doc = "OxideJS - AST to bytecode compiler"]
pub mod class;
pub mod compiler;
pub mod compiler_log;
pub mod counter;
pub mod emit_ctx;
pub mod emitter;
pub mod expr;
pub mod hash;
pub mod passes;
pub mod shared;
pub mod stmt;
pub mod symbol_table;
-139
View File
@@ -1,139 +0,0 @@
#[allow(unused_imports)]
use oxide_bytecode::module::Constant;
#[allow(unused_imports)]
use oxide_bytecode::opcode::{self, OpCode};
#[allow(unused_imports)]
use oxide_parser::{
AssignmentOperator, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty, BindingPattern,
ChainElement, Class, ClassElement, Expression, ForStatementInit, ForStatementLeft, LogicalOperator,
MethodDefinitionKind, ObjectAssignmentTarget, PropertyKey, PropertyKind, SimpleAssignmentTarget, Statement,
UnaryOperator, UpdateOperator, VariableDeclarationKind,
};
#[allow(unused_imports)]
use crate::compiler::{
is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext, Label, ParamSpec,
};
pub mod class;
pub mod expr;
pub mod shared;
pub mod stmt;
impl Compiler {
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
Statement::ExpressionStatement(_)
| Statement::ReturnStatement(_)
| Statement::BreakStatement(_)
| Statement::ContinueStatement(_)
| Statement::LabeledStatement(_) => self.count_basic(stmt, ctx),
Statement::BlockStatement(_) => self.count_block_domain(stmt, ctx),
Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => {
self.count_declaration_domain(stmt, ctx)
}
Statement::IfStatement(_) => self.count_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.count_iteration_domain(stmt, ctx),
Statement::SwitchStatement(_) => self.count_switch_domain(stmt, ctx),
Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.count_exception_domain(stmt, ctx),
_ => {}
}
}
pub(crate) fn count_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::BinaryExpression(_)
| Expression::PrivateInExpression(_)
| Expression::UnaryExpression(_)
| Expression::UpdateExpression(_) => self.count_operator(expr, ctx),
Expression::CallExpression(_) | Expression::NewExpression(_) => self.count_call_domain(expr, ctx),
Expression::AssignmentExpression(_) => self.count_assignment(expr, ctx),
Expression::ConditionalExpression(_)
| Expression::SequenceExpression(_)
| Expression::LogicalExpression(_) => self.count_conditional_chain(expr, ctx),
Expression::ChainExpression(_) => self.count_chain_expression(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.count_object_domain(expr, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.count_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_) => self.count_function_domain(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_) => self.count_member_domain(expr, ctx),
Expression::ParenthesizedExpression(_) => self.count_parenthesized_expression(expr, ctx),
Expression::ThisExpression(_) => self.count_this_expression(ctx),
Expression::Identifier(_) => self.count_identifier_expression(expr, ctx),
Expression::NumericLiteral(_)
| Expression::StringLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.count_literal(expr, ctx),
_ => self.count_default_expression(ctx),
}
}
pub(crate) fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
match stmt {
Statement::ExpressionStatement(_) | Statement::ReturnStatement(_) | Statement::EmptyStatement(_) => {
self.emit_basic_domain(stmt, ctx)
}
Statement::BlockStatement(_) => self.emit_block_domain(stmt, ctx),
Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => {
self.emit_declaration_domain(stmt, ctx)
}
Statement::IfStatement(_) => self.emit_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.emit_iteration_domain(stmt, ctx),
Statement::SwitchStatement(_) => self.emit_switch_domain(stmt, ctx),
Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.emit_exception_domain(stmt, ctx),
Statement::BreakStatement(b) => self.emit_break_statement(b, ctx),
Statement::ContinueStatement(c) => self.emit_continue_statement(c, ctx),
Statement::LabeledStatement(ls) => self.emit_labeled_statement(ls, ctx),
_ => Ok(None),
}
}
pub(crate) fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::NumericLiteral(_)
| Expression::StringLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.emit_literal(expr, ctx),
Expression::BinaryExpression(_)
| Expression::PrivateInExpression(_)
| Expression::UnaryExpression(_)
| Expression::ConditionalExpression(_)
| Expression::LogicalExpression(_)
| Expression::UpdateExpression(_) => self.emit_operator(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
| Expression::ChainExpression(_) => self.emit_member_domain(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.emit_object_domain(expr, ctx),
Expression::AssignmentExpression(assign) => self.emit_assignment_expression(assign, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.emit_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_)
| Expression::NewExpression(_) => self.emit_function_domain(expr, ctx),
Expression::Identifier(ident) => self.emit_identifier_expression(ident, ctx),
Expression::CallExpression(_) => self.emit_call_domain(expr, ctx),
Expression::ThisExpression(_) => self.emit_this_expression(ctx),
Expression::SequenceExpression(seq) => self.emit_sequence_expression(seq, ctx),
Expression::ParenthesizedExpression(p) => self.emit_parenthesized_expression(p, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1 +0,0 @@
// placeholder
@@ -1 +0,0 @@
// placeholder
+382
View File
@@ -0,0 +1,382 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{
ArrayAssignmentTarget, ArrayPattern, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty,
BindingPattern, Expression, ObjectAssignmentTarget, ObjectPattern, VariableDeclarationKind,
};
impl Compiler {
pub(crate) fn count_binding_pattern(&self, pattern: &BindingPattern, ctx: &mut CompileCtx) {
match pattern {
BindingPattern::BindingIdentifier(_) => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
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
}
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);
}
}
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(crate) 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(crate) fn count_array_assignment(&self, ap: &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 let Some(elem) = elem {
self.count_assignment_maybe_default(elem, ctx);
}
}
if let Some(rest) = &ap.rest {
self.count_rest_array(ctx);
self.count_assign_target(&rest.target, ctx);
}
ctx.count_instr(); // FOR_OF_CLOSE
}
pub(crate) fn count_object_assignment(&self, op: &ObjectAssignmentTarget, ctx: &mut CompileCtx) {
for prop in &op.properties {
match prop {
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
}
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
self.count_object_property_read_key(&prop.name, prop.computed, ctx);
self.count_assignment_maybe_default(&prop.binding, ctx);
}
}
}
if let Some(rest) = &op.rest {
ctx.alloc_reg();
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
self.count_assign_target(&rest.target, ctx);
}
}
pub(crate) fn count_assignment_maybe_default(&self, target: &AssignmentTargetMaybeDefault, ctx: &mut CompileCtx) {
match target {
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);
}
AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
_ => {}
}
}
pub(crate) fn count_assign_target(&self, target: &AssignmentTarget, ctx: &mut CompileCtx) {
match target {
AssignmentTarget::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.count_instr(); // STORE_VAR
}
AssignmentTarget::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
AssignmentTarget::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
_ => {}
}
}
fn emit_default_if_undefined(
&self, val_reg: u8, default_expr: &Expression, ctx: &mut CompileCtx,
) -> 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));
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);
Ok(val_reg)
}
fn emit_bind_target(
&self, name: &str, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let var_reg = ctx.alloc_reg();
let target_reg = if matches!(kind, VariableDeclarationKind::Var) {
match ctx.declare(name, var_reg, kind, is_const) {
Ok(()) => var_reg,
Err(_) => ctx.lookup(name).unwrap_or(var_reg),
}
} else {
ctx.declare(name, var_reg, kind, is_const)?;
var_reg
};
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));
} else {
ctx.emit(opcode::encode(OpCode::STORE_VAR, target_reg, src_reg, if is_const { 1 } else { 0 }));
}
ctx.init_var(name);
Ok(())
}
pub(crate) fn emit_assign_target(
&self, target: &AssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTarget::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
AssignmentTarget::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTarget::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
_ => Err("assignment target not supported".into()),
}
}
pub(crate) fn emit_binding_pattern(
&self, pattern: &BindingPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
match pattern {
BindingPattern::BindingIdentifier(bi) => {
self.emit_bind_target(bi.name.as_str(), src_reg, kind, is_const, ctx)
}
BindingPattern::ArrayPattern(ap) => self.emit_array_binding(ap, src_reg, kind, is_const, ctx),
BindingPattern::ObjectPattern(op) => self.emit_object_binding(op, src_reg, kind, is_const, ctx),
BindingPattern::AssignmentPattern(ap) => {
let val_reg = self.emit_default_if_undefined(src_reg, &ap.right, ctx)?;
self.emit_binding_pattern(&ap.left, val_reg, kind, is_const, ctx)
}
}
}
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));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(pattern) = elem {
self.emit_binding_pattern(pattern, val_reg, kind, is_const, ctx)?;
}
}
if let Some(rest) = &ap.rest {
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));
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));
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();
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));
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));
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);
Ok(rest_reg)
}
fn emit_object_binding(
&self, op: &ObjectPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
let (prop_reg, static_key) = self.emit_object_property_read_key(src_reg, &prop.key, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_binding_pattern(&prop.value, prop_reg, kind, is_const, ctx)?;
}
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);
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
Ok(())
}
fn emit_assignment_maybe_default(
&self, target: &AssignmentTargetMaybeDefault, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
let val_reg = self.emit_default_if_undefined(src_reg, &default.init, ctx)?;
self.emit_assign_target(&default.binding, val_reg, ctx)
}
AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
_ => Err("assignment target not supported".into()),
}
}
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));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(target) = elem {
self.emit_assignment_maybe_default(target, val_reg, ctx)?;
}
}
if let Some(rest) = &ap.rest {
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));
Ok(())
}
pub(crate) fn emit_object_assignment(
&self, op: &ObjectAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
match prop {
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
let key = id.binding.name.as_str().to_string();
excluded.push(key.clone());
let mut prop_reg = self.emit_object_property_read(src_reg, &key, ctx);
if let Some(default_expr) = &id.init {
prop_reg = self.emit_default_if_undefined(prop_reg, default_expr, ctx)?;
}
let name = id.binding.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
prop_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
}
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
let (prop_reg, static_key) =
self.emit_object_property_read_key(src_reg, &prop.name, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_assignment_maybe_default(&prop.binding, prop_reg, ctx)?;
}
}
}
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);
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
Ok(())
}
}
+432
View File
@@ -0,0 +1,432 @@
use crate::compiler::{CompileCtx, Compiler, Label};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{ChainElement, Expression, LogicalOperator, PropertyKey};
impl Compiler {
pub(crate) fn count_object_property_read_static(&self, ctx: &mut CompileCtx) {
ctx.count_load_var();
ctx.count_load_const();
ctx.count_ic_instr_with_ext();
}
pub(crate) fn count_object_property_read_key(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) {
if !computed {
self.count_object_property_read_static(ctx);
let _ = key;
return;
}
ctx.count_load_var();
match key {
PropertyKey::Identifier(ident) => {
let name = ident.name.as_str();
let _ = ctx.lookup_or_builtin(name);
ctx.count_load_var();
}
PropertyKey::StringLiteral(_) => {
ctx.count_load_const();
}
PropertyKey::NumericLiteral(_) => {
ctx.count_load_const();
}
PropertyKey::StaticIdentifier(_) => {
ctx.count_load_const();
}
_ => {}
}
ctx.alloc_reg();
ctx.count_instr();
}
pub(crate) fn count_optional_guard(&self, ctx: &mut CompileCtx) {
ctx.count_load_var();
ctx.count_jump();
}
pub(crate) 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();
ctx.count_load_var();
ctx.count_ic_instr_with_ext();
}
pub(crate) 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();
}
pub(crate) 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;
}
}
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();
ctx.count_load_var();
}
pub(crate) 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(crate) 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(crate) 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;
ctx.projected_pc += 1;
ctx.labels.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
}
}
}
}
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)?;
let dup_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, dup_reg, reg, 0));
let offset = (short_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_nullish(dup_reg, offset));
Ok(())
}
fn emit_static_member_get_preserve_base(
&self, member: &oxide_parser::StaticMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<(u8, u8), String> {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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);
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);
Ok((value_reg, obj_reg))
}
fn emit_computed_member_get_preserve_base(
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<(u8, u8), String> {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok((value_reg, obj_reg))
}
fn emit_chain_call(
&self, call: &oxide_parser::CallExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let (callee_reg, this_reg) = match &call.callee {
Expression::StaticMemberExpression(member) => {
self.emit_static_member_get_preserve_base(member, short_label, ctx)?
}
Expression::ComputedMemberExpression(member) => {
self.emit_computed_member_get_preserve_base(member, short_label, ctx)?
}
Expression::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
(callee_reg, obj_reg)
}
_ => {
let callee_reg = self.emit_chainable_expression(&call.callee, short_label, ctx)?;
if call.optional {
if let Some(label) = short_label {
self.emit_optional_guard(callee_reg, label, ctx)?;
}
}
let this_idx = ctx.add_constant(Constant::Undefined);
let this_reg = ctx.alloc_reg();
ctx.emit_load_const(this_reg, this_idx);
(callee_reg, this_reg)
}
};
if call.optional
&& matches!(
&call.callee,
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
)
{
if let Some(label) = short_label {
self.emit_optional_guard(callee_reg, label, ctx)?;
}
}
let mut arg_regs = Vec::new();
for arg in &call.arguments {
if let Some(expr) = arg.as_expression() {
arg_regs.push(self.emit_expression(expr, ctx)?);
}
}
let first_arg_reg = if arg_regs.is_empty() { 0u8 } else { arg_regs[0] };
let op = match &call.callee {
Expression::Identifier(ident) if ctx.is_builtin(ident.name.as_str()) => OpCode::CALL_NATIVE,
_ => OpCode::CALL,
};
ctx.emit(opcode::encode(op, callee_reg, this_reg, first_arg_reg));
ctx.emit(arg_regs.len() as u32);
let result_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
Ok(result_reg)
}
fn emit_chainable_expression(
&self, expr: &Expression, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match expr {
Expression::StaticMemberExpression(member) => {
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
Expression::ComputedMemberExpression(member) => {
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
Expression::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok(value_reg)
}
Expression::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
Expression::ChainExpression(chain) => self.emit_chain_element(&chain.expression, short_label, ctx),
_ => self.emit_expression(expr, ctx),
}
}
pub(crate) fn emit_chain_element(
&self, element: &ChainElement, short_label: Option<Label>, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match element {
ChainElement::StaticMemberExpression(member) => {
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
ChainElement::ComputedMemberExpression(member) => {
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
Ok(value_reg)
}
ChainElement::PrivateFieldExpression(member) => {
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
if member.optional {
if let Some(label) = short_label {
self.emit_optional_guard(obj_reg, label, ctx)?;
}
}
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));
Ok(value_reg)
}
ChainElement::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
ChainElement::TSNonNullExpression(_) => Err("TS non-null expressions are not supported in JS mode".into()),
}
}
pub(crate) fn emit_logical_assign_test(
&self, op: LogicalOperator, test_reg: u8, store_label: Label, end_label: Label, ctx: &mut CompileCtx,
) -> Result<(), String> {
match op {
LogicalOperator::And => {
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
}
LogicalOperator::Or => {
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_true(test_reg, offset));
}
LogicalOperator::Coalesce => {
let store_pos = ctx.resolve_label(store_label)?;
let offset = (store_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp_if_nullish(test_reg, offset));
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
let offset = ctx.checked_jump_offset(offset);
ctx.emit(opcode::encode_jmp(offset));
}
}
Ok(())
}
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));
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);
prop_reg
}
fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result<u8, String> {
match key {
PropertyKey::Identifier(ident) => {
let name = ident.name.as_str();
let var_reg = ctx.lookup_or_builtin(name)?;
let key_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, key_reg, var_reg, 0));
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);
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);
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);
Ok(key_reg)
}
_ => Err("computed destructuring key expression not supported".into()),
}
}
pub(crate) fn emit_object_property_read_key(
&self, src_reg: u8, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
) -> Result<(u8, Option<String>), String> {
if !computed {
let key_name = self.static_property_name(key)?;
let prop_reg = self.emit_object_property_read(src_reg, &key_name, ctx);
return Ok((prop_reg, Some(key_name)));
}
let prop_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
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));
Ok((val_reg, None))
}
}
@@ -0,0 +1,73 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::opcode::{self, 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,
) -> Result<(), String> {
let key_reg = self.emit_class_key_reg(key, computed, ctx)?;
let value_reg = if let Some(expr) = value {
self.emit_expression(expr, ctx)?
} else {
self.emit_undefined(ctx)
};
if computed {
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, target_reg, key_reg, value_reg));
} else {
ctx.emit(opcode::encode(OpCode::SET_PROP, target_reg, value_reg, key_reg));
}
Ok(())
}
pub(crate) fn emit_private_field_init(
&self, target_reg: u8, 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 {
self.emit_expression(expr, ctx)?
} else {
self.emit_undefined(ctx)
};
ctx.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, value_reg, key_reg));
Ok(())
}
pub(crate) fn emit_private_method_init(
&self, target_reg: u8, method: &oxide_parser::MethodDefinition, home_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let PropertyKey::PrivateIdentifier(private) = &method.key else {
return Err("expected private method key".into());
};
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));
Ok(())
}
pub(crate) fn count_public_field_init(
&self, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
) {
self.count_class_key(key, computed, ctx);
if let Some(expr) = value {
self.count_expression(expr, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
ctx.projected_pc += 1;
}
pub(crate) fn count_private_field_init(&self, value: Option<&Expression>, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.projected_pc += 1;
if let Some(expr) = value {
self.count_expression(expr, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
ctx.projected_pc += 1;
}
}
@@ -0,0 +1,75 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::module::Constant;
use oxide_parser::PropertyKey;
impl Compiler {
fn private_name_id(&self, name: &str, ctx: &CompileCtx) -> Result<u32, String> {
ctx.scopes
.private_name_map
.iter()
.find_map(|(n, id)| (n == name).then_some(*id))
.ok_or_else(|| format!("private name #{name} is not defined"))
}
pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result<u8, String> {
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);
Ok(reg)
}
pub(crate) fn emit_class_key_reg(
&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
) -> Result<u8, String> {
if !computed {
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);
return Ok(reg);
}
if matches!(key, PropertyKey::PrivateIdentifier(_)) {
return Err("private class elements not yet supported".into());
}
self.emit_expression(key.to_expression(), ctx)
}
pub(crate) fn count_class_key(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) {
if computed {
self.count_expression(key.to_expression(), ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
pub(crate) fn static_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::Identifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::StringLiteral(s) => Ok(s.value.to_string()),
PropertyKey::NumericLiteral(n) => Ok(n.value.to_string()),
_ => Err("computed destructuring keys not yet supported".into()),
}
}
pub(crate) fn class_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::Identifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::StringLiteral(s) => Ok(s.value.to_string()),
PropertyKey::NumericLiteral(n) => Ok(n.value.to_string()),
PropertyKey::PrivateIdentifier(_) => Err("private class elements not yet supported".into()),
_ => Err("unsupported class property key type".into()),
}
}
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);
reg
}
}
+4
View File
@@ -0,0 +1,4 @@
pub mod binding;
pub mod chain;
pub mod class_field;
pub mod class_key;