Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bytecompiler bytecode generation. #3188

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 68 additions & 40 deletions boa_engine/src/bytecompiler/class.rs

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions boa_engine/src/bytecompiler/declaration/declaration_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
bytecompiler::{Access, ByteCompiler, Literal},
bytecompiler::{Access, ByteCompiler, Literal, Operand},
vm::{BindingOpcode, Opcode},
};
use boa_ast::{
Expand Down Expand Up @@ -40,7 +40,7 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[index]);
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down Expand Up @@ -80,7 +80,10 @@ impl ByteCompiler<'_, '_> {

self.emit(
Opcode::CopyDataProperties,
&[excluded_keys.len() as u32, additional_excluded_keys_count],
&[
Operand::U32(excluded_keys.len() as u32),
Operand::U32(additional_excluded_keys_count),
],
);
self.emit_binding(def, *ident);
}
Expand All @@ -95,7 +98,10 @@ impl ByteCompiler<'_, '_> {
self.interner().resolve_expect(key.sym()).into_common(false),
));
}
self.emit(Opcode::CopyDataProperties, &[excluded_keys.len() as u32, 0]);
self.emit(
Opcode::CopyDataProperties,
&[Operand::U32(excluded_keys.len() as u32), Operand::U32(0)],
);
self.access_set(
Access::Property { access },
false,
Expand All @@ -112,7 +118,7 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[index]);
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down Expand Up @@ -152,7 +158,7 @@ impl ByteCompiler<'_, '_> {
match name {
PropertyName::Literal(name) => {
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::GetPropertyByName, &[index]);
self.emit(Opcode::GetPropertyByName, &[Operand::U32(index)]);
}
PropertyName::Computed(node) => {
self.compile_expr(node, true);
Expand Down
42 changes: 23 additions & 19 deletions boa_engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use boa_interner::Sym;
#[cfg(feature = "annex-b")]
use boa_ast::operations::annex_b_function_declarations_names;

use super::Operand;

impl ByteCompiler<'_, '_> {
/// `GlobalDeclarationInstantiation ( script, env )`
///
Expand Down Expand Up @@ -570,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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
}
}

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

// i. Let bindingExists be ! varEnv.HasBinding(fn).
Expand All @@ -739,14 +744,14 @@ impl ByteCompiler<'_, '_> {
match self.set_mutable_binding(name) {
Ok(binding) => {
let index = self.get_or_insert_binding(binding);
self.emit(Opcode::SetName, &[index]);
self.emit(Opcode::SetName, &[Operand::U32(index)]);
}
Err(BindingLocatorError::MutateImmutable) => {
let index = self.get_or_insert_name(name);
self.emit(Opcode::ThrowMutateImmutable, &[index]);
self.emit(Opcode::ThrowMutateImmutable, &[Operand::U32(index)]);
}
Err(BindingLocatorError::Silent) => {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
} else {
Expand All @@ -756,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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
}
}
}
Expand All @@ -782,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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
}
}
}
Expand Down Expand Up @@ -1012,8 +1017,7 @@ impl ByteCompiler<'_, '_> {
}

if generator {
self.emit_opcode(Opcode::Generator);
self.emit_u8(self.in_async().into());
self.emit(Opcode::Generator, &[Operand::U8(self.in_async().into())]);
self.emit_opcode(Opcode::Pop);
}

Expand Down Expand Up @@ -1052,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, &[index]);
self.emit(Opcode::GetName, &[Operand::U32(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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(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 @@ -1086,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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);
}
}

Expand Down Expand Up @@ -1118,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, &[index]);
self.emit(Opcode::DefInitVar, &[Operand::U32(index)]);

// c. Append F to instantiatedVarNames.
instantiated_var_names.push(f);
Expand Down
27 changes: 13 additions & 14 deletions boa_engine/src/bytecompiler/expression/assign.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
bytecompiler::{Access, ByteCompiler},
bytecompiler::{Access, ByteCompiler, Operand},
environments::BindingLocatorError,
vm::{BindingOpcode, Opcode},
};
Expand Down Expand Up @@ -60,9 +60,9 @@ impl ByteCompiler<'_, '_> {
let lex = self.current_environment.is_lex_binding(name);

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

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

self.emit(Opcode::GetPropertyByName, &[index]);
self.emit(Opcode::GetPropertyByName, &[Operand::U32(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, &[index]);
self.emit(Opcode::SetPropertyByName, &[Operand::U32(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, &[index]);
self.emit(Opcode::GetPrivateField, &[Operand::U32(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, &[index]);
self.emit(Opcode::SetPrivateField, &[Operand::U32(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, &[index]);
self.emit(Opcode::GetPropertyByName, &[Operand::U32(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, &[index]);
self.emit(Opcode::SetPropertyByName, &[Operand::U32(index)]);
if !use_expr {
self.emit_opcode(Opcode::Pop);
}
Expand All @@ -201,8 +201,7 @@ impl ByteCompiler<'_, '_> {
}

self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(2);
self.emit(Opcode::RotateRight, &[Operand::U8(2)]);

self.emit_opcode(Opcode::SetPropertyByValue);
if !use_expr {
Expand Down
19 changes: 11 additions & 8 deletions boa_engine/src/bytecompiler/expression/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use boa_ast::expression::operator::{
Binary, BinaryInPrivate,
};

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

impl ByteCompiler<'_, '_> {
pub(crate) fn compile_binary(&mut self, binary: &Binary, use_expr: bool) {
Expand All @@ -21,7 +24,7 @@ impl ByteCompiler<'_, '_> {
}

if !use_expr {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
BinaryOp::Bitwise(op) => {
Expand All @@ -36,7 +39,7 @@ impl ByteCompiler<'_, '_> {
}

if !use_expr {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
BinaryOp::Relational(op) => {
Expand All @@ -57,7 +60,7 @@ impl ByteCompiler<'_, '_> {
}

if !use_expr {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
BinaryOp::Logical(op) => {
Expand All @@ -80,15 +83,15 @@ impl ByteCompiler<'_, '_> {
};

if !use_expr {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
BinaryOp::Comma => {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
self.compile_expr(binary.rhs(), true);

if !use_expr {
self.emit(Opcode::Pop, &[]);
self.emit_opcode(Opcode::Pop);
}
}
};
Expand All @@ -97,7 +100,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, &[index]);
self.emit(Opcode::InPrivate, &[Operand::U32(index)]);

if !use_expr {
self.emit_opcode(Opcode::Pop);
Expand Down
Loading