From bcb76316670d4dbeb4c9392494168268405ad075 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Tue, 20 Jun 2023 22:46:48 +0200 Subject: [PATCH] Remove `BreakLabel` opcode --- .../src/bytecompiler/statement/break.rs | 11 ++--- boa_engine/src/vm/code_block.rs | 4 +- boa_engine/src/vm/flowgraph/mod.rs | 5 +- .../src/vm/opcode/control_flow/break.rs | 49 +------------------ boa_engine/src/vm/opcode/mod.rs | 9 +--- 5 files changed, 11 insertions(+), 67 deletions(-) diff --git a/boa_engine/src/bytecompiler/statement/break.rs b/boa_engine/src/bytecompiler/statement/break.rs index 8a1a8d9a15c..eb51b429cd5 100644 --- a/boa_engine/src/bytecompiler/statement/break.rs +++ b/boa_engine/src/bytecompiler/statement/break.rs @@ -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); @@ -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); diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index 9c76c3dac92..67f14f1fb0e 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -328,7 +328,6 @@ impl CodeBlock { } Opcode::CopyDataProperties | Opcode::Break - | Opcode::BreakLabel | Opcode::Continue | Opcode::LoopStart | Opcode::IteratorLoopStart @@ -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"), } } } diff --git a/boa_engine/src/vm/flowgraph/mod.rs b/boa_engine/src/vm/flowgraph/mod.rs index 7dd74cbc0cd..6e632d66bb1 100644 --- a/boa_engine/src/vm/flowgraph/mod.rs +++ b/boa_engine/src/vm/flowgraph/mod.rs @@ -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::(pc); pc += size_of::(); let target_operand = self.read::(pc); @@ -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"), } } diff --git a/boa_engine/src/vm/opcode/control_flow/break.rs b/boa_engine/src/vm/opcode/control_flow/break.rs index 9e30179d2a2..f940fb77bb6 100644 --- a/boa_engine/src/vm/opcode/control_flow/break.rs +++ b/boa_engine/src/vm/opcode/control_flow/break.rs @@ -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` @@ -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 { - let jump_address = context.vm.read::(); - let target_address = context.vm.read::(); - - 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) - } -} diff --git a/boa_engine/src/vm/opcode/mod.rs b/boa_engine/src/vm/opcode/mod.rs index 57afe213ff3..29bf14fee8b 100644 --- a/boa_engine/src/vm/opcode/mod.rs +++ b/boa_engine/src/vm/opcode/mod.rs @@ -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, @@ -1801,6 +1794,8 @@ generate_impl! { Reserved54 => Reserved, /// Reserved [`Opcode`]. Reserved55 => Reserved, + /// Reserved [`Opcode`]. + Reserved56 => Reserved, } }