Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 25, 2023
1 parent fed05d7 commit 3ea7f1a
Show file tree
Hide file tree
Showing 29 changed files with 863 additions and 1,130 deletions.
2 changes: 2 additions & 0 deletions boa_engine/src/bytecompiler/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use boa_ast::expression::Identifier;
impl ByteCompiler<'_, '_> {
/// Push either a new declarative or function environment on the compile time environment stack.
pub(crate) fn push_compile_environment(&mut self, function_scope: bool) {
self.current_open_environments_count += 1;
self.current_environment = Rc::new(RefCell::new(CompileTimeEnvironment::new(
self.current_environment.clone(),
function_scope,
Expand All @@ -16,6 +17,7 @@ impl ByteCompiler<'_, '_> {
/// Pops the top compile time environment and returns its index in the compile time environments array.
#[track_caller]
pub(crate) fn pop_compile_environment(&mut self) -> u32 {
self.current_open_environments_count -= 1;
let index = self.compile_environments.len() as u32;
self.compile_environments
.push(self.current_environment.clone());
Expand Down
181 changes: 90 additions & 91 deletions boa_engine/src/bytecompiler/jump_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,44 @@ use crate::bytecompiler::{ByteCompiler, Label};
use bitflags::bitflags;
use boa_interner::Sym;

#[derive(Debug, Clone)]
pub(crate) struct JumpRecord {
label: Option<Sym>,
address: Label,
target_index: Option<usize>,
}

impl JumpRecord {
pub(crate) const fn new(
label: Option<Sym>,
address: Label,
target_index: Option<usize>,
) -> Self {
Self {
label,
address,
target_index,
}
}

pub(crate) const fn label(&self) -> Option<Sym> {
self.label
}

pub(crate) const fn target_index(&self) -> Option<usize> {
self.target_index
}
}

/// Boa's `ByteCompiler` jump information tracking struct.
#[derive(Debug, Clone)]
pub(crate) struct JumpControlInfo {
label: Option<Sym>,
start_address: u32,
flags: JumpControlInfoFlags,
set_jumps: Vec<Label>,
breaks: Vec<Label>,
try_continues: Vec<Label>,
pub(crate) breaks: Vec<JumpRecord>,
try_continues: Vec<JumpRecord>,
current_open_environments_count: u32,
}

bitflags! {
Expand All @@ -32,7 +61,6 @@ bitflags! {
const SWITCH = 0b0000_0010;
const TRY_BLOCK = 0b0000_0100;
const LABELLED = 0b0000_1000;
const IN_FINALLY = 0b0001_0000;
const HAS_FINALLY = 0b0010_0000;
const ITERATOR_LOOP = 0b0100_0000;
const FOR_AWAIT_OF_LOOP = 0b1000_0000;
Expand All @@ -41,6 +69,13 @@ bitflags! {
///
/// This bitflag is inherited if the previous [`JumpControlInfo`].
const USE_EXPR = 0b0001_0000_0000;

/// Does the control flow jump (`break` or `continue`) require to special finally code generation.
///
/// This is needed for `break` and `continue`s that are in a try statement
/// (try or catch blocks, for finally not needed because it is already executing it).
///
const REQUIRE_FINALLY_HANDLING = 0b0010_0000_0000;
}
}

Expand All @@ -50,21 +85,19 @@ impl Default for JumpControlInfoFlags {
}
}

impl Default for JumpControlInfo {
fn default() -> Self {
/// ---- `JumpControlInfo` Creation Methods ----
impl JumpControlInfo {
fn new(current_open_environments_count: u32) -> Self {
Self {
label: None,
start_address: u32::MAX,
flags: JumpControlInfoFlags::default(),
set_jumps: Vec::new(),
breaks: Vec::new(),
try_continues: Vec::new(),
current_open_environments_count,
}
}
}

/// ---- `JumpControlInfo` Creation Methods ----
impl JumpControlInfo {
pub(crate) const fn with_label(mut self, label: Option<Sym>) -> Self {
self.label = label;
self
Expand Down Expand Up @@ -138,10 +171,6 @@ impl JumpControlInfo {
self.flags.contains(JumpControlInfoFlags::LABELLED)
}

pub(crate) const fn in_finally(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::IN_FINALLY)
}

pub(crate) const fn has_finally(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::HAS_FINALLY)
}
Expand All @@ -150,10 +179,12 @@ impl JumpControlInfo {
self.flags.contains(JumpControlInfoFlags::USE_EXPR)
}

#[allow(dead_code)]
pub(crate) const fn iterator_loop(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::ITERATOR_LOOP)
}

#[allow(dead_code)]
pub(crate) const fn for_await_of_loop(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::FOR_AWAIT_OF_LOOP)
}
Expand All @@ -172,23 +203,15 @@ impl JumpControlInfo {
self.start_address = start_address;
}

/// Set the `in_finally` field of `JumpControlInfo`.
pub(crate) fn set_in_finally(&mut self, value: bool) {
self.flags.set(JumpControlInfoFlags::IN_FINALLY, value);
}

/// Pushes a `Label` onto the `break` vector of `JumpControlInfo`.
pub(crate) fn push_break_label(&mut self, break_label: Label) {
self.breaks.push(break_label);
}

/// Pushes a `Label` onto the `try_continues` vector of `JumpControlInfo`.
pub(crate) fn push_try_continue_label(&mut self, try_continue_label: Label) {
self.try_continues.push(try_continue_label);
}

pub(crate) fn push_set_jumps(&mut self, set_jump_label: Label) {
self.set_jumps.push(set_jump_label);
pub(crate) fn push_break_label(
&mut self,
label: Option<Sym>,
address: Label,
final_target: Option<usize>,
) {
self.breaks
.push(JumpRecord::new(label, address, final_target));
}
}

Expand All @@ -198,7 +221,8 @@ impl ByteCompiler<'_, '_> {
///
/// Default `JumpControlInfoKind` is `JumpControlInfoKind::Loop`
pub(crate) fn push_empty_loop_jump_control(&mut self, use_expr: bool) {
let new_info = JumpControlInfo::default().with_loop_flag(true);
let new_info =
JumpControlInfo::new(self.current_open_environments_count).with_loop_flag(true);
self.push_contol_info(new_info, use_expr);
}

Expand Down Expand Up @@ -242,7 +266,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_labelled_block_flag(true)
.with_label(Some(label))
.with_start_address(start_address);
Expand All @@ -261,12 +285,12 @@ impl ByteCompiler<'_, '_> {

assert!(info.is_labelled());

for label in info.breaks {
self.patch_jump(label);
for JumpRecord { address, .. } in info.breaks {
self.patch_jump(address);
}

for label in info.try_continues {
self.patch_jump_with_target(label, info.start_address);
for JumpRecord { address, .. } in info.try_continues {
self.patch_jump_with_target(address, info.start_address);
}
}
// ---- `IterationStatement`'s `JumpControlInfo` methods ---- //
Expand All @@ -278,7 +302,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_loop_flag(true)
.with_label(label)
.with_start_address(start_address);
Expand All @@ -293,7 +317,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_loop_flag(true)
.with_label(label)
.with_start_address(start_address)
Expand All @@ -308,7 +332,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_loop_flag(true)
.with_label(label)
.with_start_address(start_address)
Expand All @@ -330,12 +354,12 @@ impl ByteCompiler<'_, '_> {
assert!(info.is_loop());

let start_address = info.start_address();
for label in info.try_continues {
self.patch_jump_with_target(label, start_address);
for JumpRecord { address, .. } in info.try_continues {
self.patch_jump_with_target(address, start_address);
}

for label in info.breaks {
self.patch_jump(label);
for JumpRecord { address, .. } in info.breaks {
self.patch_jump(address);
}
}

Expand All @@ -348,7 +372,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_switch_flag(true)
.with_label(label)
.with_start_address(start_address);
Expand All @@ -367,8 +391,8 @@ impl ByteCompiler<'_, '_> {

assert!(info.is_switch());

for label in info.breaks {
self.patch_jump(label);
for JumpRecord { address, .. } in info.breaks {
self.patch_jump(address);
}
}

Expand All @@ -381,7 +405,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
use_expr: bool,
) {
let new_info = JumpControlInfo::default()
let new_info = JumpControlInfo::new(self.current_open_environments_count)
.with_try_block_flag(true)
.with_start_address(start_address)
.with_has_finally(has_finally);
Expand All @@ -402,9 +426,21 @@ impl ByteCompiler<'_, '_> {

// Handle breaks. If there is a finally, breaks should go to the finally
if info.has_finally() {
for label in info.breaks {
self.patch_jump_with_target(label, try_end);
for JumpRecord { address, .. } in &info.breaks {
self.patch_jump_with_target(*address, try_end);
}

let (jumps, default) = self.jump_table(info.breaks.len() as u32);

// Handle breaks in a finally block
for (i, label) in jumps.iter().enumerate() {
if let Some(jump_info) = self.jump_info.last_mut() {
let breakk = &info.breaks[i];
jump_info.push_break_label(breakk.label(), *label, breakk.target_index());
}
}

self.patch_jump(default);
} else {
// When there is no finally, search for the break point.
for jump_info in self.jump_info.iter_mut().rev() {
Expand All @@ -415,55 +451,18 @@ impl ByteCompiler<'_, '_> {
}
}

// Handle set_jumps
for label in info.set_jumps {
for jump_info in self.jump_info.iter_mut().rev() {
if jump_info.is_loop() || jump_info.is_switch() {
jump_info.breaks.push(label);
break;
}
}
}

// Pass continues down the stack.
if let Some(jump_info) = self.jump_info.last_mut() {
jump_info.try_continues.append(&mut info.try_continues);
}
}

/// Pushes a `TryStatement`'s Finally block `JumpControlInfo` onto the `jump_info` stack.
pub(crate) fn push_init_finally_control_info(&mut self, use_expr: bool) {
let mut new_info = JumpControlInfo::default().with_try_block_flag(true);

new_info.set_in_finally(true);

self.push_contol_info(new_info, use_expr);
}

pub(crate) fn pop_finally_control_info(&mut self) {
assert!(!self.jump_info.is_empty());
let mut info = self.jump_info.pop().expect("no jump information found");

assert!(info.in_finally());

// Handle set_jumps
for label in info.set_jumps {
for jump_info in self.jump_info.iter_mut().rev() {
if jump_info.is_loop() || jump_info.is_switch() {
jump_info.breaks.push(label);
break;
}
}
}

// Handle breaks in a finally block
for label in info.breaks {
self.patch_jump(label);
pub(crate) fn jump_info_open_environment_count(&self, index: usize) -> u32 {
let current = &self.jump_info[index];
if let Some(next) = self.jump_info.get(index + 1) {
return next.current_open_environments_count - current.current_open_environments_count;
}

// Pass continues down the stack.
if let Some(jump_info) = self.jump_info.last_mut() {
jump_info.try_continues.append(&mut info.try_continues);
}
self.current_open_environments_count - self.current_open_environments_count
}
}
Loading

0 comments on commit 3ea7f1a

Please sign in to comment.