Skip to content

Commit

Permalink
Add bitflag to JumpControlInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Jan 12, 2023
1 parent 57a7ab6 commit 8e984b6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 67 deletions.
115 changes: 75 additions & 40 deletions boa_engine/src/bytecompiler/jump_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
bytecompiler::{ByteCompiler, Label},
vm::Opcode,
};
use bitflags::bitflags;
use boa_interner::Sym;
use std::mem::size_of;

Expand All @@ -21,38 +22,42 @@ use std::mem::size_of;
pub(crate) struct JumpControlInfo {
label: Option<Sym>,
start_address: u32,
kind: JumpControlInfoKind,
decl_envs: u32,
flags: JumpControlInfoFlags,
breaks: Vec<Label>,
try_continues: Vec<Label>,
in_catch: bool,
has_finally: bool,
finally_start: Option<Label>,
for_of_in_loop: bool,
decl_envs: u32,
}

/// An enum that sets the type of the current `JumpControlInfo`
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum JumpControlInfoKind {
Loop,
Switch,
Try,
LabelledBlock,
bitflags! {
/// A bitflag that contains the type flags and relevant booleans for `JumpControlInfo`.
pub(crate) struct JumpControlInfoFlags: u8 {
const LOOP = 0b0000_0001;
const SWITCH = 0b0000_0010;
const TRY_BLOCK = 0b0000_0100;
const LABELLED_BLOCK = 0b0000_1000;
const IN_CATCH = 0b0001_0000;
const HAS_FINALLY = 0b0010_0000;
const FOR_OF_IN_LOOP = 0b0100_0000;
}
}

impl Default for JumpControlInfoFlags {
fn default() -> Self {
JumpControlInfoFlags::empty()
}
}

impl Default for JumpControlInfo {
fn default() -> Self {
Self {
label: None,
start_address: u32::MAX,
kind: JumpControlInfoKind::Loop,
decl_envs: 0,
flags: JumpControlInfoFlags::default(),
breaks: Vec::new(),
try_continues: Vec::new(),
in_catch: false,
has_finally: false,
finally_start: None,
for_of_in_loop: false,
decl_envs: 0,
}
}
}
Expand All @@ -70,18 +75,33 @@ impl JumpControlInfo {
self
}

pub(crate) const fn with_kind(mut self, kind: JumpControlInfoKind) -> Self {
self.kind = kind;
pub(crate) fn with_loop_flag(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::LOOP, value);
self
}

pub(crate) fn with_switch_flag(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::SWITCH, value);
self
}

pub(crate) const fn with_has_finally(mut self, value: bool) -> Self {
self.has_finally = value;
pub(crate) fn with_try_block_flag(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::TRY_BLOCK, value);
self
}

pub(crate) const fn with_for_of_in_loop(mut self, value: bool) -> Self {
self.for_of_in_loop = value;
pub(crate) fn with_labelled_block_flag(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::LABELLED_BLOCK, value);
self
}

pub(crate) fn with_has_finally(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::HAS_FINALLY, value);
self
}

pub(crate) fn with_for_of_in_loop(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::FOR_OF_IN_LOOP, value);
self
}
}
Expand All @@ -97,24 +117,36 @@ impl JumpControlInfo {
self.start_address
}

pub(crate) const fn kind(&self) -> JumpControlInfoKind {
self.kind
pub(crate) const fn is_loop(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::LOOP)
}

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

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

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

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

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

pub(crate) const fn finally_start(&self) -> Option<Label> {
self.finally_start
}

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

pub(crate) const fn decl_envs(&self) -> u32 {
Expand All @@ -136,7 +168,7 @@ impl JumpControlInfo {

/// Sets the `in_catch` field of `JumpControlInfo`.
pub(crate) fn set_in_catch(&mut self, value: bool) {
self.in_catch = value;
self.flags.set(JumpControlInfoFlags::IN_CATCH, value);
}

/// Sets the `finally_start` field of `JumpControlInfo`.
Expand Down Expand Up @@ -170,8 +202,9 @@ impl ByteCompiler<'_, '_> {
/// Pushes a generic `JumpControlInfo` onto `ByteCompiler`
///
/// Default `JumpControlInfoKind` is `JumpControlInfoKind::Loop`
pub(crate) fn push_new_jump_control(&mut self) {
self.jump_info.push(JumpControlInfo::default());
pub(crate) fn push_empty_loop_jump_control(&mut self) {
self.jump_info
.push(JumpControlInfo::default().with_loop_flag(true));
}

pub(crate) fn current_jump_control_mut(&mut self) -> Option<&mut JumpControlInfo> {
Expand All @@ -184,7 +217,7 @@ impl ByteCompiler<'_, '_> {
.jump_info
.last_mut()
.expect("must have try control label");
assert!(info.kind == JumpControlInfoKind::Try);
assert!(info.is_try_block());
info.set_finally_start(start);
}
}
Expand All @@ -195,7 +228,7 @@ impl ByteCompiler<'_, '_> {
.jump_info
.last_mut()
.expect("must have try control label");
assert!(info.kind == JumpControlInfoKind::Try);
assert!(info.is_try_block());
info.set_in_catch(value);
}
}
Expand Down Expand Up @@ -224,6 +257,7 @@ impl ByteCompiler<'_, '_> {

pub(crate) fn push_loop_control_info(&mut self, label: Option<Sym>, start_address: u32) {
let new_info = JumpControlInfo::default()
.with_loop_flag(true)
.with_label(label)
.with_start_address(start_address);
self.jump_info.push(new_info);
Expand All @@ -235,6 +269,7 @@ impl ByteCompiler<'_, '_> {
start_address: u32,
) {
let new_info = JumpControlInfo::default()
.with_loop_flag(true)
.with_label(label)
.with_start_address(start_address)
.with_for_of_in_loop(true);
Expand All @@ -244,7 +279,7 @@ impl ByteCompiler<'_, '_> {
pub(crate) fn pop_loop_control_info(&mut self) {
let loop_info = self.jump_info.pop().expect("no jump information found");

assert!(loop_info.kind == JumpControlInfoKind::Loop);
assert!(loop_info.is_loop());

for label in loop_info.breaks {
self.patch_jump(label);
Expand All @@ -257,7 +292,7 @@ impl ByteCompiler<'_, '_> {

pub(crate) fn push_switch_control_info(&mut self, label: Option<Sym>, start_address: u32) {
let new_info = JumpControlInfo::default()
.with_kind(JumpControlInfoKind::Switch)
.with_switch_flag(true)
.with_label(label)
.with_start_address(start_address);
self.jump_info.push(new_info);
Expand All @@ -266,7 +301,7 @@ impl ByteCompiler<'_, '_> {
pub(crate) fn pop_switch_control_info(&mut self) {
let info = self.jump_info.pop().expect("no jump information found");

assert!(info.kind == JumpControlInfoKind::Switch);
assert!(info.is_switch());

for label in info.breaks {
self.patch_jump(label);
Expand All @@ -282,7 +317,7 @@ impl ByteCompiler<'_, '_> {
.start_address();

let new_info = JumpControlInfo::default()
.with_kind(JumpControlInfoKind::Try)
.with_try_block_flag(true)
.with_start_address(start_address)
.with_has_finally(has_finally);

Expand All @@ -294,7 +329,7 @@ impl ByteCompiler<'_, '_> {
if !self.jump_info.is_empty() {
let mut info = self.jump_info.pop().expect("no jump information found");

assert!(info.kind == JumpControlInfoKind::Try);
assert!(info.is_try_block());

let mut breaks = Vec::with_capacity(info.breaks.len());

Expand Down Expand Up @@ -330,7 +365,7 @@ impl ByteCompiler<'_, '_> {

pub(crate) fn push_labelled_block_control_info(&mut self, label: Sym, start_address: u32) {
let new_info = JumpControlInfo::default()
.with_kind(JumpControlInfoKind::LabelledBlock)
.with_labelled_block_flag(true)
.with_label(Some(label))
.with_start_address(start_address);
self.jump_info.push(new_info);
Expand All @@ -339,7 +374,7 @@ impl ByteCompiler<'_, '_> {
pub(crate) fn pop_labelled_block_control_info(&mut self) {
let info = self.jump_info.pop().expect("no jump information found");

assert!(info.kind == JumpControlInfoKind::LabelledBlock);
assert!(info.is_labelled_block());

self.emit_opcode(Opcode::PopEnvironment);

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use boa_interner::{Interner, Sym};
use rustc_hash::FxHashMap;

pub(crate) use function::FunctionCompiler;
pub(crate) use jump_control::{JumpControlInfo, JumpControlInfoKind};
pub(crate) use jump_control::JumpControlInfo;

/// Describes how a node has been defined in the source code.
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
12 changes: 2 additions & 10 deletions boa_engine/src/bytecompiler/statement/break.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
use boa_ast::statement::Break;

use crate::{
bytecompiler::{ByteCompiler, JumpControlInfoKind},
vm::Opcode,
JsNativeError, JsResult,
};
use crate::{bytecompiler::ByteCompiler, vm::Opcode, JsNativeError, JsResult};

impl ByteCompiler<'_, '_> {
/// Compile a [`Break`] `boa_ast` node
pub(crate) fn compile_break(&mut self, node: Break) -> JsResult<()> {
let next = self.next_opcode_location();
if let Some(info) = self
.jump_info
.last()
.filter(|info| info.kind() == JumpControlInfoKind::Try)
{
if let Some(info) = self.jump_info.last().filter(|info| info.is_try_block()) {
let in_finally = if let Some(finally_start) = info.finally_start() {
next >= finally_start.index
} else {
Expand Down
18 changes: 3 additions & 15 deletions boa_engine/src/bytecompiler/statement/continue.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use boa_ast::statement::Continue;

use crate::{
bytecompiler::{ByteCompiler, JumpControlInfoKind},
vm::Opcode,
JsNativeError, JsResult,
};
use crate::{bytecompiler::ByteCompiler, vm::Opcode, JsNativeError, JsResult};

impl ByteCompiler<'_, '_> {
pub(crate) fn compile_continue(&mut self, node: Continue) -> JsResult<()> {
let next = self.next_opcode_location();
if let Some(info) = self
.jump_info
.last()
.filter(|info| info.kind() == JumpControlInfoKind::Try)
{
if let Some(info) = self.jump_info.last().filter(|info| info.is_try_block()) {
let start_address = info.start_address();
let in_finally = if let Some(finally_start) = info.finally_start() {
next > finally_start.index
Expand All @@ -39,11 +31,7 @@ impl ByteCompiler<'_, '_> {
.expect("no jump information found")
.push_try_continue_label(label);
} else {
let mut items = self
.jump_info
.iter()
.rev()
.filter(|info| info.kind() == JumpControlInfoKind::Loop);
let mut items = self.jump_info.iter().rev().filter(|info| info.is_loop());
let address = if let Some(label_name) = node.label() {
let mut num_loops = 0;
let mut emit_for_of_in_exit = 0;
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/bytecompiler/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ByteCompiler<'_, '_> {
) -> JsResult<()> {
self.context.push_compile_time_environment(false);
let push_env = self.emit_and_track_decl_env();
self.push_new_jump_control();
self.push_empty_loop_jump_control();

if let Some(init) = for_loop.init() {
match init {
Expand Down

0 comments on commit 8e984b6

Please sign in to comment.