Skip to content

Commit

Permalink
Implement varying length operands
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Sep 16, 2023
1 parent 3c15e59 commit eca1b75
Show file tree
Hide file tree
Showing 44 changed files with 1,723 additions and 719 deletions.
98 changes: 58 additions & 40 deletions boa_engine/src/bytecompiler/class.rs

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions boa_engine/src/bytecompiler/declaration/declaration_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(
Opcode::GetPropertyByName,
index,
);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down Expand Up @@ -81,8 +84,8 @@ impl ByteCompiler<'_, '_> {
self.emit(
Opcode::CopyDataProperties,
&[
Operand::U32(excluded_keys.len() as u32),
Operand::U32(additional_excluded_keys_count),
Operand::Varying(excluded_keys.len() as u32),
Operand::Varying(additional_excluded_keys_count),
],
);
self.emit_binding(def, *ident);
Expand All @@ -100,7 +103,10 @@ impl ByteCompiler<'_, '_> {
}
self.emit(
Opcode::CopyDataProperties,
&[Operand::U32(excluded_keys.len() as u32), Operand::U32(0)],
&[
Operand::Varying(excluded_keys.len() as u32),
Operand::Varying(0),
],
);
self.access_set(
Access::Property { access },
Expand All @@ -118,7 +124,10 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(
Opcode::GetPropertyByName,
index,
);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down Expand Up @@ -158,7 +167,10 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(
Opcode::GetPropertyByName,
index,
);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down
26 changes: 13 additions & 13 deletions boa_engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ impl ByteCompiler<'_, '_> {
let binding = self.initialize_mutable_binding(f, true);
let index = self.get_or_insert_binding(binding);
self.emit_opcode(Opcode::PushUndefined);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);
}
}

Expand Down Expand Up @@ -719,18 +719,18 @@ impl ByteCompiler<'_, '_> {
let index = self.functions.len() as u32;
self.functions.push(code);
if r#async && generator {
self.emit(Opcode::GetGeneratorAsync, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetGeneratorAsync, index);
} else if generator {
self.emit(Opcode::GetGenerator, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetGenerator, index);
} else if r#async {
self.emit(
Opcode::GetFunctionAsync,
&[Operand::U32(index), Operand::Bool(false)],
&[Operand::Varying(index), Operand::Bool(false)],
);
} else {
self.emit(
Opcode::GetFunction,
&[Operand::U32(index), Operand::Bool(false)],
&[Operand::Varying(index), Operand::Bool(false)],
);
}

Expand All @@ -744,11 +744,11 @@ impl ByteCompiler<'_, '_> {
match self.set_mutable_binding(name) {
Ok(binding) => {
let index = self.get_or_insert_binding(binding);
self.emit(Opcode::SetName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::SetName, index);
}
Err(BindingLocatorError::MutateImmutable) => {
let index = self.get_or_insert_name(name);
self.emit(Opcode::ThrowMutateImmutable, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::ThrowMutateImmutable, index);
}
Err(BindingLocatorError::Silent) => {
self.emit_opcode(Opcode::Pop);
Expand All @@ -761,7 +761,7 @@ impl ByteCompiler<'_, '_> {
self.create_mutable_binding(name, !strict);
let binding = self.initialize_mutable_binding(name, !strict);
let index = self.get_or_insert_binding(binding);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);
}
}
}
Expand All @@ -787,7 +787,7 @@ impl ByteCompiler<'_, '_> {
let binding = self.initialize_mutable_binding(name, !strict);
let index = self.get_or_insert_binding(binding);
self.emit_opcode(Opcode::PushUndefined);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);
}
}
}
Expand Down Expand Up @@ -1056,14 +1056,14 @@ impl ByteCompiler<'_, '_> {
// a. Let initialValue be ! env.GetBindingValue(n, false).
let binding = self.get_binding_value(n);
let index = self.get_or_insert_binding(binding);
self.emit(Opcode::GetName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetName, index);
}

// 5. Perform ! varEnv.InitializeBinding(n, initialValue).
let binding = self.initialize_mutable_binding(n, true);
let index = self.get_or_insert_binding(binding);
self.emit_opcode(Opcode::PushUndefined);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);

// 6. NOTE: A var with the same name as a formal parameter initially has
// the same value as the corresponding initialized parameter.
Expand All @@ -1090,7 +1090,7 @@ impl ByteCompiler<'_, '_> {
let binding = self.initialize_mutable_binding(n, true);
let index = self.get_or_insert_binding(binding);
self.emit_opcode(Opcode::PushUndefined);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);
}
}

Expand Down Expand Up @@ -1122,7 +1122,7 @@ impl ByteCompiler<'_, '_> {
let binding = self.initialize_mutable_binding(f, true);
let index = self.get_or_insert_binding(binding);
self.emit_opcode(Opcode::PushUndefined);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::DefInitVar, index);

// c. Append F to instantiatedVarNames.
instantiated_var_names.push(f);
Expand Down
20 changes: 10 additions & 10 deletions boa_engine/src/bytecompiler/expression/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ impl ByteCompiler<'_, '_> {
let lex = self.current_environment.is_lex_binding(name);

if lex {
self.emit(Opcode::GetName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetName, index);
} else {
self.emit(Opcode::GetNameAndLocator, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetNameAndLocator, index);
}

if short_circuit {
Expand All @@ -79,11 +79,11 @@ impl ByteCompiler<'_, '_> {
match self.set_mutable_binding(name) {
Ok(binding) => {
let index = self.get_or_insert_binding(binding);
self.emit(Opcode::SetName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::SetName, index);
}
Err(BindingLocatorError::MutateImmutable) => {
let index = self.get_or_insert_name(name);
self.emit(Opcode::ThrowMutateImmutable, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::ThrowMutateImmutable, index);
}
Err(BindingLocatorError::Silent) => {
self.emit_opcode(Opcode::Pop);
Expand All @@ -102,7 +102,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);

self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetPropertyByName, index);
if short_circuit {
pop_count = 2;
early_exit = Some(self.emit_opcode_with_operand(opcode));
Expand All @@ -112,7 +112,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(opcode);
}

self.emit(Opcode::SetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::SetPropertyByName, index);
if !use_expr {
self.emit_opcode(Opcode::Pop);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(access.target(), true);
self.emit_opcode(Opcode::Dup);

self.emit(Opcode::GetPrivateField, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetPrivateField, index);
if short_circuit {
pop_count = 1;
early_exit = Some(self.emit_opcode_with_operand(opcode));
Expand All @@ -155,7 +155,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(opcode);
}

self.emit(Opcode::SetPrivateField, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::SetPrivateField, index);
if !use_expr {
self.emit_opcode(Opcode::Pop);
}
Expand All @@ -169,7 +169,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::This);

self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetPropertyByName, index);
if short_circuit {
pop_count = 2;
early_exit = Some(self.emit_opcode_with_operand(opcode));
Expand All @@ -179,7 +179,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(opcode);
}

self.emit(Opcode::SetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::SetPropertyByName, index);
if !use_expr {
self.emit_opcode(Opcode::Pop);
}
Expand Down
7 changes: 2 additions & 5 deletions boa_engine/src/bytecompiler/expression/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use boa_ast::expression::operator::{
Binary, BinaryInPrivate,
};

use crate::{
bytecompiler::{ByteCompiler, Operand},
vm::Opcode,
};
use crate::{bytecompiler::ByteCompiler, vm::Opcode};

impl ByteCompiler<'_, '_> {
pub(crate) fn compile_binary(&mut self, binary: &Binary, use_expr: bool) {
Expand Down Expand Up @@ -100,7 +97,7 @@ impl ByteCompiler<'_, '_> {
pub(crate) fn compile_binary_in_private(&mut self, binary: &BinaryInPrivate, use_expr: bool) {
let index = self.get_or_insert_private_name(*binary.lhs());
self.compile_expr(binary.rhs(), true);
self.emit(Opcode::InPrivate, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::InPrivate, index);

if !use_expr {
self.emit_opcode(Opcode::Pop);
Expand Down
23 changes: 11 additions & 12 deletions boa_engine/src/bytecompiler/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl ByteCompiler<'_, '_> {
}
}

self.emit(
self.emit_with_varying_operand(
Opcode::ConcatToString,
&[Operand::U32(template_literal.elements().len() as u32)],
template_literal.elements().len() as u32,
);

if !use_expr {
Expand Down Expand Up @@ -228,7 +228,7 @@ impl ByteCompiler<'_, '_> {
match access.field() {
PropertyAccessField::Const(field) => {
let index = self.get_or_insert_name((*field).into());
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetPropertyByName, index);
}
PropertyAccessField::Expr(field) => {
self.compile_expr(field, true);
Expand All @@ -240,7 +240,7 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(access.target(), true);
self.emit(Opcode::Dup, &[]);
let index = self.get_or_insert_private_name(access.field());
self.emit(Opcode::GetPrivateField, &[Operand::U32(index)]);
self.emit_with_varying_operand(Opcode::GetPrivateField, index);
}
expr => {
self.compile_expr(expr, true);
Expand Down Expand Up @@ -268,19 +268,18 @@ impl ByteCompiler<'_, '_> {
));
}

self.emit(Opcode::TemplateCreate, &[Operand::U32(count)]);
self.emit_u64(site);
self.emit(
Opcode::TemplateCreate,
&[Operand::Varying(count), Operand::U64(site)],
);

self.patch_jump(jump_label);

for expr in template.exprs() {
self.compile_expr(expr, true);
}

self.emit(
Opcode::Call,
&[Operand::U32(template.exprs().len() as u32 + 1)],
);
self.emit_with_varying_operand(Opcode::Call, template.exprs().len() as u32 + 1);
}
Expression::Class(class) => self.class(class, true),
Expression::SuperCall(super_call) => {
Expand Down Expand Up @@ -311,9 +310,9 @@ impl ByteCompiler<'_, '_> {
if contains_spread {
self.emit_opcode(Opcode::SuperCallSpread);
} else {
self.emit(
self.emit_with_varying_operand(
Opcode::SuperCall,
&[Operand::U32(super_call.arguments().len() as u32)],
super_call.arguments().len() as u32,
);
}

Expand Down
Loading

0 comments on commit eca1b75

Please sign in to comment.