From 9fe38245b24dcf864c9c6aff8dce597819566d0b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 26 Sep 2023 16:41:42 +0200 Subject: [PATCH] LibJS: Move bytecode interpreter's inner loop to its own function --- .../Libraries/LibJS/Bytecode/Interpreter.cpp | 36 +++++++++++-------- .../Libraries/LibJS/Bytecode/Interpreter.h | 2 ++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 848659a5ad..9fb98b56b9 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -161,22 +161,8 @@ ThrowCompletionOr Interpreter::run(SourceTextModule& module) return js_undefined(); } -Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executable, BasicBlock const* entry_point, CallFrame* in_frame) +void Interpreter::run_bytecode() { - dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable); - - TemporaryChange restore_executable { m_current_executable, &executable }; - TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast(nullptr) }; - - VERIFY(!vm().execution_context_stack().is_empty()); - - TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() }; - - if (in_frame) - push_call_frame(in_frame, executable.number_of_registers); - else - push_call_frame(make(), executable.number_of_registers); - for (;;) { auto pc = InstructionStreamIterator { m_current_block->instruction_stream(), m_current_executable }; TemporaryChange temp_change { m_pc, Optional(pc) }; @@ -185,6 +171,7 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executa bool will_jump = false; bool will_return = false; bool will_yield = false; + while (!pc.at_end()) { auto& instruction = *pc; auto ran_or_error = instruction.execute(*this); @@ -260,6 +247,25 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executa if (will_return) break; } +} + +Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executable, BasicBlock const* entry_point, CallFrame* in_frame) +{ + dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable); + + TemporaryChange restore_executable { m_current_executable, &executable }; + TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast(nullptr) }; + + VERIFY(!vm().execution_context_stack().is_empty()); + + TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() }; + + if (in_frame) + push_call_frame(in_frame, executable.number_of_registers); + else + push_call_frame(make(), executable.number_of_registers); + + run_bytecode(); dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable); diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h index fe9843494d..eae8bf7748 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h @@ -92,6 +92,8 @@ public: void visit_edges(Cell::Visitor&); private: + void run_bytecode(); + CallFrame& call_frame() { return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; });