Skip to content

Commit

Permalink
Decouple bytecompiler from CodeBlock
Browse files Browse the repository at this point in the history
This also give us some memory benefits, it reduces CodeBlock size from
264 => 208 (removes 56 bytes).

Additionally calling `into_boxed_slice` If the vector has excess capacity,
its items will be moved into a newly-allocated buffer with exactly the
right capacity.
  • Loading branch information
HalidOdat committed Mar 15, 2023
1 parent 0a843d2 commit 8bec300
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 88 deletions.
137 changes: 102 additions & 35 deletions boa_engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::{ByteCompiler, Literal, NodeKind};
use crate::vm::{BindingOpcode, CodeBlock, Opcode};
use crate::{
builtins::function::ThisMode,
vm::{BindingOpcode, Opcode},
};
use boa_ast::{
declaration::Binding,
expression::Identifier,
function::{Class, ClassElement},
function::{Class, ClassElement, FormalParameterList},
operations::bound_names,
property::{MethodDefinition, PropertyName},
};
Expand All @@ -20,9 +23,25 @@ impl ByteCompiler<'_, '_> {
pub(crate) fn compile_class(&mut self, class: &Class, expression: bool) {
let class_name = class.name().map_or(Sym::EMPTY_STRING, Identifier::sym);

let code = CodeBlock::new(class_name, 0, true);
let mut compiler = ByteCompiler {
code_block: code,
function_name: class_name,
has_binding_identifier: false,
length: 0,
strict: true,
bytecode: Vec::default(),
literals: Vec::default(),
names: Vec::default(),
private_names: Vec::default(),
bindings: Vec::default(),
num_bindings: 0,
functions: Vec::default(),
compile_environments: Vec::default(),
this_mode: ThisMode::Global,
params: FormalParameterList::default(),
arguments_binding: None,
is_class_constructor: false,
class_field_initializer_name: None,
function_environment_push_location: 0,
literals_map: FxHashMap::default(),
names_map: FxHashMap::default(),
private_names_map: FxHashMap::default(),
Expand All @@ -35,7 +54,7 @@ impl ByteCompiler<'_, '_> {

if let Some(class_name) = class.name() {
if class.has_binding_identifier() {
compiler.code_block.has_binding_identifier = true;
compiler.has_binding_identifier = true;
compiler.context.push_compile_time_environment(false);
compiler.context.create_immutable_binding(class_name, true);
}
Expand All @@ -44,12 +63,12 @@ impl ByteCompiler<'_, '_> {
compiler.context.push_compile_time_environment(true);

if let Some(expr) = class.constructor() {
compiler.code_block.length = expr.parameters().length();
compiler.code_block.params = expr.parameters().clone();
compiler.length = expr.parameters().length();
compiler.params = expr.parameters().clone();
compiler
.context
.create_mutable_binding(Sym::ARGUMENTS.into(), false, false);
compiler.code_block.arguments_binding = Some(
compiler.arguments_binding = Some(
compiler
.context
.initialize_mutable_binding(Sym::ARGUMENTS.into(), false),
Expand Down Expand Up @@ -84,10 +103,9 @@ impl ByteCompiler<'_, '_> {
compiler.emit_opcode(Opcode::RestParameterPop);
}
let env_label = if expr.parameters().has_expressions() {
compiler.code_block.num_bindings = compiler.context.get_binding_number();
compiler.num_bindings = compiler.context.get_binding_number();
compiler.context.push_compile_time_environment(true);
compiler.code_block.function_environment_push_location =
compiler.next_opcode_location();
compiler.function_environment_push_location = compiler.next_opcode_location();
Some(compiler.emit_opcode_with_two_operands(Opcode::PushFunctionEnvironment))
} else {
None
Expand All @@ -107,8 +125,8 @@ impl ByteCompiler<'_, '_> {
let (num_bindings, compile_environment) =
compiler.context.pop_compile_time_environment();
compiler.push_compile_environment(compile_environment);
compiler.code_block.num_bindings = num_bindings;
compiler.code_block.is_class_constructor = true;
compiler.num_bindings = num_bindings;
compiler.is_class_constructor = true;
}
} else {
if class.super_ref().is_some() {
Expand All @@ -117,8 +135,8 @@ impl ByteCompiler<'_, '_> {
let (num_bindings, compile_environment) =
compiler.context.pop_compile_time_environment();
compiler.push_compile_environment(compile_environment);
compiler.code_block.num_bindings = num_bindings;
compiler.code_block.is_class_constructor = true;
compiler.num_bindings = num_bindings;
compiler.is_class_constructor = true;
}

if class.name().is_some() && class.has_binding_identifier() {
Expand All @@ -130,8 +148,8 @@ impl ByteCompiler<'_, '_> {
compiler.emit_opcode(Opcode::Return);

let code = Gc::new(compiler.finish());
let index = self.code_block.functions.len() as u32;
self.code_block.functions.push(code);
let index = self.functions.len() as u32;
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);

Expand Down Expand Up @@ -279,9 +297,26 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(name, true);
}
}
let field_code = CodeBlock::new(Sym::EMPTY_STRING, 0, true);
let mut field_compiler = ByteCompiler {
code_block: field_code,
function_name: Sym::EMPTY_STRING,
has_binding_identifier: false,
length: 0,
strict: true,
bytecode: Vec::default(),
literals: Vec::default(),
names: Vec::default(),
private_names: Vec::default(),
bindings: Vec::default(),
num_bindings: 0,
functions: Vec::default(),
compile_environments: Vec::default(),
this_mode: ThisMode::Global,
params: FormalParameterList::default(),
arguments_binding: None,
is_class_constructor: false,
class_field_initializer_name: None,
function_environment_push_location: 0,

literals_map: FxHashMap::default(),
names_map: FxHashMap::default(),
private_names_map: FxHashMap::default(),
Expand All @@ -307,24 +342,40 @@ impl ByteCompiler<'_, '_> {
let (_, compile_environment) =
field_compiler.context.pop_compile_time_environment();
field_compiler.push_compile_environment(compile_environment);
field_compiler.code_block.num_bindings = num_bindings;
field_compiler.num_bindings = num_bindings;
field_compiler.emit_opcode(Opcode::Return);

let mut code = field_compiler.finish();
code.class_field_initializer_name = Some(Sym::EMPTY_STRING);
let code = Gc::new(code);
let index = self.code_block.functions.len() as u32;
self.code_block.functions.push(code);
let index = self.functions.len() as u32;
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit_opcode(Opcode::PushClassField);
}
ClassElement::PrivateFieldDefinition(name, field) => {
self.emit_opcode(Opcode::Dup);
let name_index = self.get_or_insert_private_name(*name);
let field_code = CodeBlock::new(Sym::EMPTY_STRING, 0, true);
let mut field_compiler = ByteCompiler {
code_block: field_code,
function_name: class_name,
has_binding_identifier: false,
length: 0,
strict: true,
bytecode: Vec::default(),
literals: Vec::default(),
names: Vec::default(),
private_names: Vec::default(),
bindings: Vec::default(),
num_bindings: 0,
functions: Vec::default(),
compile_environments: Vec::default(),
this_mode: ThisMode::Global,
params: FormalParameterList::default(),
arguments_binding: None,
is_class_constructor: false,
class_field_initializer_name: None,
function_environment_push_location: 0,
literals_map: FxHashMap::default(),
names_map: FxHashMap::default(),
private_names_map: FxHashMap::default(),
Expand All @@ -350,14 +401,14 @@ impl ByteCompiler<'_, '_> {
let (_, compile_environment) =
field_compiler.context.pop_compile_time_environment();
field_compiler.push_compile_environment(compile_environment);
field_compiler.code_block.num_bindings = num_bindings;
field_compiler.num_bindings = num_bindings;
field_compiler.emit_opcode(Opcode::Return);

let mut code = field_compiler.finish();
code.class_field_initializer_name = Some(Sym::EMPTY_STRING);
let code = Gc::new(code);
let index = self.code_block.functions.len() as u32;
self.code_block.functions.push(code);
let index = self.functions.len() as u32;
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit(Opcode::PushClassFieldPrivate, &[name_index]);
Expand All @@ -375,9 +426,25 @@ impl ByteCompiler<'_, '_> {
None
}
};
let field_code = CodeBlock::new(Sym::EMPTY_STRING, 0, true);
let mut field_compiler = ByteCompiler {
code_block: field_code,
function_name: class_name,
has_binding_identifier: false,
length: 0,
strict: true,
bytecode: Vec::default(),
literals: Vec::default(),
names: Vec::default(),
private_names: Vec::default(),
bindings: Vec::default(),
num_bindings: 0,
functions: Vec::default(),
compile_environments: Vec::default(),
this_mode: ThisMode::Global,
params: FormalParameterList::default(),
arguments_binding: None,
is_class_constructor: false,
class_field_initializer_name: None,
function_environment_push_location: 0,
literals_map: FxHashMap::default(),
names_map: FxHashMap::default(),
private_names_map: FxHashMap::default(),
Expand All @@ -403,14 +470,14 @@ impl ByteCompiler<'_, '_> {
let (_, compile_environment) =
field_compiler.context.pop_compile_time_environment();
field_compiler.push_compile_environment(compile_environment);
field_compiler.code_block.num_bindings = num_bindings;
field_compiler.num_bindings = num_bindings;
field_compiler.emit_opcode(Opcode::Return);

let mut code = field_compiler.finish();
code.class_field_initializer_name = Some(Sym::EMPTY_STRING);
let code = Gc::new(code);
let index = self.code_block.functions.len() as u32;
self.code_block.functions.push(code);
let index = self.functions.len() as u32;
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit_opcode(Opcode::SetHomeObject);
Expand Down Expand Up @@ -447,11 +514,11 @@ impl ByteCompiler<'_, '_> {
compiler.push_compile_environment(compile_environment);
let (_, compile_environment) = compiler.context.pop_compile_time_environment();
compiler.push_compile_environment(compile_environment);
compiler.code_block.num_bindings = num_bindings;
compiler.num_bindings = num_bindings;

let code = Gc::new(compiler.finish());
let index = self.code_block.functions.len() as u32;
self.code_block.functions.push(code);
let index = self.functions.len() as u32;
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit_opcode(Opcode::SetHomeObject);
Expand Down
32 changes: 24 additions & 8 deletions boa_engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,24 @@ impl FunctionCompiler {
}

let mut compiler = ByteCompiler {
code_block: code,
function_name: self.name,
has_binding_identifier: false,
length,
strict: self.strict,
bytecode: Vec::default(),
literals: Vec::default(),
names: Vec::default(),
private_names: Vec::default(),
bindings: Vec::default(),
num_bindings: 0,
functions: Vec::default(),
compile_environments: Vec::default(),
this_mode: ThisMode::Global,
params: FormalParameterList::default(),
arguments_binding: None,
is_class_constructor: false,
class_field_initializer_name: None,
function_environment_push_location: 0,
literals_map: FxHashMap::default(),
names_map: FxHashMap::default(),
private_names_map: FxHashMap::default(),
Expand All @@ -121,7 +138,7 @@ impl FunctionCompiler {
}

if let Some(binding_identifier) = self.binding_identifier {
compiler.code_block.has_binding_identifier = true;
compiler.has_binding_identifier = true;
compiler.context.push_compile_time_environment(false);
compiler
.context
Expand All @@ -139,7 +156,7 @@ impl FunctionCompiler {
compiler
.context
.create_mutable_binding(Sym::ARGUMENTS.into(), false, false);
compiler.code_block.arguments_binding = Some(
compiler.arguments_binding = Some(
compiler
.context
.initialize_mutable_binding(Sym::ARGUMENTS.into(), false),
Expand Down Expand Up @@ -184,10 +201,9 @@ impl FunctionCompiler {
}

let env_label = if parameters.has_expressions() {
compiler.code_block.num_bindings = compiler.context.get_binding_number();
compiler.num_bindings = compiler.context.get_binding_number();
compiler.context.push_compile_time_environment(true);
compiler.code_block.function_environment_push_location =
compiler.next_opcode_location();
compiler.function_environment_push_location = compiler.next_opcode_location();
Some(compiler.emit_opcode_with_two_operands(Opcode::PushFunctionEnvironment))
} else {
None
Expand Down Expand Up @@ -215,7 +231,7 @@ impl FunctionCompiler {
let (num_bindings, compile_environment) =
compiler.context.pop_compile_time_environment();
compiler.push_compile_environment(compile_environment);
compiler.code_block.num_bindings = num_bindings;
compiler.num_bindings = num_bindings;
}

if self.binding_identifier.is_some() {
Expand All @@ -228,7 +244,7 @@ impl FunctionCompiler {
compiler.push_compile_environment(compile_environment);
}

compiler.code_block.params = parameters.clone();
compiler.params = parameters.clone();

// TODO These are redundant if a function returns so may need to check if a function returns and adding these if it doesn't
compiler.emit(Opcode::PushUndefined, &[]);
Expand Down

0 comments on commit 8bec300

Please sign in to comment.