Skip to content

Commit

Permalink
Remove BreakLabel opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 20, 2023
1 parent 3e66d24 commit bcb7631
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 67 deletions.
11 changes: 3 additions & 8 deletions boa_engine/src/bytecompiler/statement/break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ use boa_interner::Sym;
impl ByteCompiler<'_, '_> {
/// Compile a [`Break`] `boa_ast` node
pub(crate) fn compile_break(&mut self, node: Break, _use_expr: bool) {
let opcode = if node.label().is_some() {
Opcode::BreakLabel
} else {
Opcode::Break
};

if let Some(info) = self.jump_info.last().filter(|info| info.is_try_block()) {
let has_finally_or_is_finally = info.has_finally() || info.in_finally();

let (break_label, target_jump_label) = self.emit_opcode_with_two_operands(opcode);
let (break_label, target_jump_label) =
self.emit_opcode_with_two_operands(Opcode::Break);

if let Some(node_label) = node.label() {
let info = self.jump_info_label(node_label);
Expand Down Expand Up @@ -45,7 +40,7 @@ impl ByteCompiler<'_, '_> {
}

// Emit the break opcode -> (Label, Label)
let (break_label, target_label) = self.emit_opcode_with_two_operands(opcode);
let (break_label, target_label) = self.emit_opcode_with_two_operands(Opcode::Break);
if let Some(label) = node.label() {
let info = self.jump_info_label(label);
info.push_break_label(break_label);
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ impl CodeBlock {
}
Opcode::CopyDataProperties
| Opcode::Break
| Opcode::BreakLabel
| Opcode::Continue
| Opcode::LoopStart
| Opcode::IteratorLoopStart
Expand Down Expand Up @@ -610,7 +609,8 @@ impl CodeBlock {
| Opcode::Reserved52
| Opcode::Reserved53
| Opcode::Reserved54
| Opcode::Reserved55 => unreachable!("Reserved opcodes are unrechable"),
| Opcode::Reserved55
| Opcode::Reserved56 => unreachable!("Reserved opcodes are unrechable"),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::Red);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::Break | Opcode::BreakLabel => {
Opcode::Break => {
let jump_operand = self.read::<u32>(pc);
pc += size_of::<u32>();
let target_operand = self.read::<u32>(pc);
Expand Down Expand Up @@ -709,7 +709,8 @@ impl CodeBlock {
| Opcode::Reserved52
| Opcode::Reserved53
| Opcode::Reserved54
| Opcode::Reserved55 => unreachable!("Reserved opcodes are unrechable"),
| Opcode::Reserved55
| Opcode::Reserved56 => unreachable!("Reserved opcodes are unrechable"),
}
}

Expand Down
49 changes: 1 addition & 48 deletions boa_engine/src/vm/opcode/control_flow/break.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
vm::{call_frame::AbruptCompletionRecord, opcode::Operation, CompletionType},
Context, JsResult, JsValue,
Context, JsResult,
};

/// `Break` implements the Opcode Operation for `Opcode::Break`
Expand Down Expand Up @@ -52,50 +52,3 @@ impl Operation for Break {
Ok(CompletionType::Normal)
}
}

/// `BreakLabel` implements the Opcode Operation for `Opcode::BreakLabel`
///
/// Operation:
/// - Pop required environments and jump to address.
pub(crate) struct BreakLabel;

impl Operation for BreakLabel {
const NAME: &'static str = "BreakLabel";
const INSTRUCTION: &'static str = "INST - BreakLabel";

fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let jump_address = context.vm.read::<u32>();
let target_address = context.vm.read::<u32>();

let value = context.vm.stack.pop().unwrap_or(JsValue::undefined());
context.vm.push(value);

// 1. Iterate through Env stack looking for exit address.
let mut envs_to_pop = 0;
for i in (0..context.vm.frame().env_stack.len()).rev() {
let Some(env_entry) = context.vm.frame_mut().env_stack.get_mut(i) else {
break;
};

if jump_address == env_entry.exit_address()
|| (env_entry.is_finally_env() && jump_address == env_entry.start_address())
{
break;
}

envs_to_pop += env_entry.env_num();
context.vm.frame_mut().env_stack.pop();
}

let env_truncation_len = context.vm.environments.len().saturating_sub(envs_to_pop);
context.vm.environments.truncate(env_truncation_len);

// 2. Register target address in AbruptCompletionRecord.
let new_record = AbruptCompletionRecord::new_break().with_initial_target(target_address);
context.vm.frame_mut().abrupt_completion = Some(new_record);

// 3. Set program counter and finally return fields.
context.vm.frame_mut().pc = jump_address;
Ok(CompletionType::Normal)
}
}
9 changes: 2 additions & 7 deletions boa_engine/src/vm/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,13 +1166,6 @@ generate_impl! {
/// Stack: loop_return_value **=>**
Break,

/// Jumps to a label target location and pops the environments involved.
///
/// Operands: Jump Address: u32, Target address: u32
///
/// Stack: **=>**
BreakLabel,

/// Sets the `AbruptCompletionRecord` for a delayed continue
///
/// Operands: Jump Address: u32, Target address: u32,
Expand Down Expand Up @@ -1801,6 +1794,8 @@ generate_impl! {
Reserved54 => Reserved,
/// Reserved [`Opcode`].
Reserved55 => Reserved,
/// Reserved [`Opcode`].
Reserved56 => Reserved,
}
}

Expand Down

0 comments on commit bcb7631

Please sign in to comment.