From a08712838aa5568d4c03c310e7aa28d5c8fb6910 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Tue, 27 Jun 2023 06:50:49 +0200 Subject: [PATCH] Remove some opcodes --- .../src/bytecompiler/statement/labelled.rs | 8 +- boa_engine/src/bytecompiler/statement/try.rs | 15 +- boa_engine/src/vm/code_block.rs | 8 +- boa_engine/src/vm/flowgraph/mod.rs | 17 +-- .../src/vm/opcode/control_flow/finally.rs | 139 ------------------ .../src/vm/opcode/control_flow/labelled.rs | 37 ----- boa_engine/src/vm/opcode/control_flow/mod.rs | 4 - boa_engine/src/vm/opcode/mod.rs | 27 +--- 8 files changed, 20 insertions(+), 235 deletions(-) delete mode 100644 boa_engine/src/vm/opcode/control_flow/finally.rs delete mode 100644 boa_engine/src/vm/opcode/control_flow/labelled.rs diff --git a/boa_engine/src/bytecompiler/statement/labelled.rs b/boa_engine/src/bytecompiler/statement/labelled.rs index bab66e8d7a4..860a13b4ac0 100644 --- a/boa_engine/src/bytecompiler/statement/labelled.rs +++ b/boa_engine/src/bytecompiler/statement/labelled.rs @@ -1,7 +1,4 @@ -use crate::{ - bytecompiler::{ByteCompiler, NodeKind}, - vm::Opcode, -}; +use crate::bytecompiler::{ByteCompiler, NodeKind}; use boa_ast::{ statement::{Labelled, LabelledItem}, Statement, @@ -11,7 +8,6 @@ impl ByteCompiler<'_, '_> { /// Compile a [`Labelled`] `boa_ast` node pub(crate) fn compile_labelled(&mut self, labelled: &Labelled, use_expr: bool) { let labelled_loc = self.next_opcode_location(); - let end_label = self.emit_opcode_with_operand(Opcode::LabelledStart); self.push_labelled_control_info(labelled.label(), labelled_loc, use_expr); match labelled.item() { @@ -38,8 +34,6 @@ impl ByteCompiler<'_, '_> { } } - let labelled_end = self.next_opcode_location(); - self.patch_jump_with_target(end_label, labelled_end); self.pop_labelled_control_info(); } } diff --git a/boa_engine/src/bytecompiler/statement/try.rs b/boa_engine/src/bytecompiler/statement/try.rs index c5d1fb11f2f..1909bb0376f 100644 --- a/boa_engine/src/bytecompiler/statement/try.rs +++ b/boa_engine/src/bytecompiler/statement/try.rs @@ -1,5 +1,5 @@ use crate::{ - bytecompiler::{jump_control::JumpControlInfoFlags, ByteCompiler, Label}, + bytecompiler::{jump_control::JumpControlInfoFlags, ByteCompiler}, vm::{BindingOpcode, Opcode}, }; use boa_ast::{ @@ -25,6 +25,7 @@ impl ByteCompiler<'_, '_> { self.emit_opcode(Opcode::TryEnd); + // TODO: simplify when there is finally but no catch. if has_finally { self.emit_opcode(Opcode::PushZero); if has_catch { @@ -50,7 +51,6 @@ impl ByteCompiler<'_, '_> { // Pop and push control loops post FinallyStart, as FinallyStart resets flow control variables. // Handle finally header operations let finally_start = self.next_opcode_location(); - let finally_end = self.emit_opcode_with_operand(Opcode::FinallyStart); self.jump_info .last_mut() @@ -58,7 +58,7 @@ impl ByteCompiler<'_, '_> { .flags |= JumpControlInfoFlags::IN_FINALLY; self.patch_jump_with_target(finally_loc, finally_start); // Compile finally statement body - self.compile_finally_stmt(finally, finally_end, has_catch); + self.compile_finally_stmt(finally, has_catch); self.pop_try_control_info(finally_start); } else { @@ -103,12 +103,7 @@ impl ByteCompiler<'_, '_> { } } - pub(crate) fn compile_finally_stmt( - &mut self, - finally: &Finally, - finally_end_label: Label, - has_catch: bool, - ) { + pub(crate) fn compile_finally_stmt(&mut self, finally: &Finally, has_catch: bool) { // TODO: We could probably remove the Get/SetReturnValue if we check that there is no break/continues statements. self.emit_opcode(Opcode::GetReturnValue); self.compile_block(finally.block(), true); @@ -122,7 +117,5 @@ impl ByteCompiler<'_, '_> { // Rethrow error if error happend! self.emit_opcode(Opcode::ReThrow); self.patch_jump(has_throw_exit); - - self.patch_jump(finally_end_label); } } diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index 8bca6192c13..d66061d2922 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -315,8 +315,6 @@ impl CodeBlock { | Opcode::JumpIfFalse | Opcode::JumpIfNotUndefined | Opcode::JumpIfNullOrUndefined - | Opcode::FinallyStart - | Opcode::LabelledStart | Opcode::Case | Opcode::Default | Opcode::LogicalAnd @@ -539,7 +537,6 @@ impl CodeBlock { | Opcode::LoopContinue | Opcode::LoopStart | Opcode::IteratorLoopStart - | Opcode::LabelledEnd | Opcode::CreateForInIterator | Opcode::GetIterator | Opcode::GetAsyncIterator @@ -637,7 +634,10 @@ impl CodeBlock { | Opcode::Reserved51 | Opcode::Reserved52 | Opcode::Reserved53 - | Opcode::Reserved54 => unreachable!("Reserved opcodes are unrechable"), + | Opcode::Reserved54 + | Opcode::Reserved55 + | Opcode::Reserved56 + | Opcode::Reserved57 => unreachable!("Reserved opcodes are unrechable"), } } } diff --git a/boa_engine/src/vm/flowgraph/mod.rs b/boa_engine/src/vm/flowgraph/mod.rs index 04f3032064e..a1f1e34c0fc 100644 --- a/boa_engine/src/vm/flowgraph/mod.rs +++ b/boa_engine/src/vm/flowgraph/mod.rs @@ -130,14 +130,6 @@ impl CodeBlock { EdgeStyle::Line, ); } - Opcode::LabelledStart => { - let end_address = self.read::(pc); - pc += size_of::(); - - let label = format!("{opcode_str} {end_address}"); - graph.add_node(previous_pc, NodeShape::None, label.into(), Color::Red); - graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); - } Opcode::TemplateLookup | Opcode::TemplateCreate => { let start_address = self.read::(pc); pc += size_of::(); @@ -272,8 +264,7 @@ impl CodeBlock { | Opcode::Call | Opcode::New | Opcode::SuperCall - | Opcode::ConcatToString - | Opcode::FinallyStart => { + | Opcode::ConcatToString => { pc += size_of::(); graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); @@ -584,7 +575,6 @@ impl CodeBlock { | Opcode::LoopContinue | Opcode::LoopStart | Opcode::IteratorLoopStart - | Opcode::LabelledEnd | Opcode::CreateForInIterator | Opcode::GetIterator | Opcode::GetAsyncIterator @@ -707,7 +697,10 @@ impl CodeBlock { | Opcode::Reserved51 | Opcode::Reserved52 | Opcode::Reserved53 - | Opcode::Reserved54 => unreachable!("Reserved opcodes are unrechable"), + | Opcode::Reserved54 + | Opcode::Reserved55 + | Opcode::Reserved56 + | Opcode::Reserved57 => unreachable!("Reserved opcodes are unrechable"), } } diff --git a/boa_engine/src/vm/opcode/control_flow/finally.rs b/boa_engine/src/vm/opcode/control_flow/finally.rs deleted file mode 100644 index dabf5e1c8cc..00000000000 --- a/boa_engine/src/vm/opcode/control_flow/finally.rs +++ /dev/null @@ -1,139 +0,0 @@ -use crate::{ - vm::{opcode::Operation, CompletionType}, - Context, JsResult, -}; - -/// `FinallyStart` implements the Opcode Operation for `Opcode::FinallyStart` -/// -/// Operation: -/// - Start of a finally block. -#[derive(Debug, Clone, Copy)] -pub(crate) struct FinallyStart; - -impl Operation for FinallyStart { - const NAME: &'static str = "FinallyStart"; - const INSTRUCTION: &'static str = "INST - FinallyStart"; - - fn execute(context: &mut Context<'_>) -> JsResult { - let _exit = context.vm.read::(); - Ok(CompletionType::Normal) - } -} - -/// `FinallyEnd` implements the Opcode Operation for `Opcode::FinallyEnd` -/// -/// Operation: -/// - End of a finally block. -#[derive(Debug, Clone, Copy)] -pub(crate) struct FinallyEnd; - -impl Operation for FinallyEnd { - const NAME: &'static str = "FinallyEnd"; - const INSTRUCTION: &'static str = "INST - FinallyEnd"; - - fn execute(_context: &mut Context<'_>) -> JsResult { - // let finally_candidates = context - // .vm - // .frame() - // .env_stack - // .iter() - // .filter(|env| env.is_finally_env() && context.vm.frame().pc < env.start_address()); - - // let next_finally = match finally_candidates.last() { - // Some(env) => env.start_address(), - // _ => u32::MAX, - // }; - - // let mut envs_to_pop = 0; - // match context.vm.frame().abrupt_completion { - // Some(record) if next_finally < record.target() => { - // context.vm.frame_mut().pc = next_finally; - - // while let Some(env_entry) = context.vm.frame().env_stack.last() { - // if next_finally <= env_entry.exit_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); - // } - // Some(record) if record.is_break() && context.vm.frame().pc < record.target() => { - // // handle the continuation of an abrupt break. - // context.vm.frame_mut().pc = record.target(); - // while let Some(env_entry) = context.vm.frame().env_stack.last() { - // if record.target() == env_entry.exit_address() { - // break; - // } - - // envs_to_pop += env_entry.env_num(); - // context.vm.frame_mut().env_stack.pop(); - // } - - // context.vm.frame_mut().abrupt_completion = None; - - // let env_truncation_len = context.vm.environments.len().saturating_sub(envs_to_pop); - // context.vm.environments.truncate(env_truncation_len); - // } - // Some(record) if record.is_continue() && context.vm.frame().pc > record.target() => { - // // Handle the continuation of an abrupt continue - // context.vm.frame_mut().pc = record.target(); - // while let Some(env_entry) = context.vm.frame().env_stack.last() { - // if env_entry.start_address() == record.target() { - // break; - // } - // envs_to_pop += env_entry.env_num(); - // context.vm.frame_mut().env_stack.pop(); - // } - - // context.vm.frame_mut().abrupt_completion = None; - // let env_truncation_len = context.vm.environments.len().saturating_sub(envs_to_pop); - // context.vm.environments.truncate(env_truncation_len); - // } - // Some(record) if record.is_return() => { - // return Ok(CompletionType::Return); - // } - // Some(record) - // if record.is_throw_with_target() && context.vm.frame().pc < record.target() => - // { - // context.vm.frame_mut().pc = record.target(); - // while let Some(env_entry) = context.vm.frame_mut().env_stack.pop() { - // envs_to_pop += env_entry.env_num(); - // if env_entry.start_address() == record.target() { - // break; - // } - // } - // context.vm.frame_mut().abrupt_completion = None; - // let env_truncation_len = context.vm.environments.len().saturating_sub(envs_to_pop); - // context.vm.environments.truncate(env_truncation_len); - // } - // Some(record) if !record.is_throw_with_target() => { - // let current_stack = context - // .vm - // .frame_mut() - // .env_stack - // .pop() - // .expect("Popping current finally stack."); - - // let env_truncation_len = context - // .vm - // .environments - // .len() - // .saturating_sub(current_stack.env_num()); - // context.vm.environments.truncate(env_truncation_len); - - // let err = JsError::from_opaque(context.vm.pop()); - // context.vm.err = Some(err); - // return Ok(CompletionType::Throw); - // } - // _ => { - // context.vm.frame_mut().env_stack.pop(); - // } - // } - - Ok(CompletionType::Normal) - } -} diff --git a/boa_engine/src/vm/opcode/control_flow/labelled.rs b/boa_engine/src/vm/opcode/control_flow/labelled.rs deleted file mode 100644 index afc8e96d8be..00000000000 --- a/boa_engine/src/vm/opcode/control_flow/labelled.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::{ - vm::{opcode::Operation, CompletionType}, - Context, JsResult, -}; - -/// `LabelledStart` implements the Opcode Operation for `Opcode::LabelledStart` -/// -/// Operation: -/// - Start of a labelled block. -#[derive(Debug, Clone, Copy)] -pub(crate) struct LabelledStart; - -impl Operation for LabelledStart { - const NAME: &'static str = "LabelledStart"; - const INSTRUCTION: &'static str = "INST - LabelledStart"; - - fn execute(context: &mut Context<'_>) -> JsResult { - let _end = context.vm.read::(); - Ok(CompletionType::Normal) - } -} - -/// `LabelledEnd` implements the Opcode Operation for `Opcode::LabelledEnd` -/// -/// Operation: -/// - Clean up environments at the end of labelled block. -#[derive(Debug, Clone, Copy)] -pub(crate) struct LabelledEnd; - -impl Operation for LabelledEnd { - const NAME: &'static str = "LabelledEnd"; - const INSTRUCTION: &'static str = "INST - LabelledEnd"; - - fn execute(_context: &mut Context<'_>) -> JsResult { - Ok(CompletionType::Normal) - } -} diff --git a/boa_engine/src/vm/opcode/control_flow/mod.rs b/boa_engine/src/vm/opcode/control_flow/mod.rs index fa3a4d7637c..697e5a99ad0 100644 --- a/boa_engine/src/vm/opcode/control_flow/mod.rs +++ b/boa_engine/src/vm/opcode/control_flow/mod.rs @@ -1,13 +1,9 @@ pub(crate) mod r#break; pub(crate) mod r#continue; -pub(crate) mod finally; -pub(crate) mod labelled; pub(crate) mod r#return; pub(crate) mod throw; pub(crate) mod r#try; -pub(crate) use finally::*; -pub(crate) use labelled::*; pub(crate) use r#break::*; pub(crate) use r#continue::*; pub(crate) use r#return::*; diff --git a/boa_engine/src/vm/opcode/mod.rs b/boa_engine/src/vm/opcode/mod.rs index bdeaf64865e..fac892f8322 100644 --- a/boa_engine/src/vm/opcode/mod.rs +++ b/boa_engine/src/vm/opcode/mod.rs @@ -1166,13 +1166,6 @@ generate_impl! { /// Stack: **=>** TryEnd, - /// Start of a finally block. - /// - /// Operands: - /// - /// Stack: **=>** - FinallyStart, - /// Unconditional Jump /// /// Operands: Jump Address: u32 @@ -1410,20 +1403,6 @@ generate_impl! { /// Stack: **=>** value LoopEnd, - /// Push labelled start marker. - /// - /// Operands: Exit Address: u32, - /// - /// Stack: **=>** - LabelledStart, - - /// Clean up environments at the end of a labelled block. - /// - /// Operands: - /// - /// Stack: **=>** - LabelledEnd, - /// Creates the ForInIterator of an object. /// /// Stack: object **=>** @@ -1802,6 +1781,12 @@ generate_impl! { Reserved53 => Reserved, /// Reserved [`Opcode`]. Reserved54 => Reserved, + /// Reserved [`Opcode`]. + Reserved55 => Reserved, + /// Reserved [`Opcode`]. + Reserved56 => Reserved, + /// Reserved [`Opcode`]. + Reserved57 => Reserved, } }