From d02e7b38119107a8bc42af8e23d8e6d491b18c78 Mon Sep 17 00:00:00 2001 From: Sahan Fernando Date: Fri, 4 Jun 2021 21:54:20 +1000 Subject: [PATCH] LibWasm: Move Wasm::BytecodeInterpreter into its own header --- Tests/LibWasm/test-wasm.cpp | 2 +- .../AbstractMachine/AbstractMachine.cpp | 3 +- .../AbstractMachine/BytecodeInterpreter.cpp | 999 ++++++++++++++++++ .../AbstractMachine/BytecodeInterpreter.h | 70 ++ .../LibWasm/AbstractMachine/Interpreter.cpp | 990 ----------------- .../LibWasm/AbstractMachine/Interpreter.h | 56 - Userland/Libraries/LibWasm/CMakeLists.txt | 1 + Userland/Utilities/wasm.cpp | 2 +- 8 files changed, 1074 insertions(+), 1049 deletions(-) create mode 100644 Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp create mode 100644 Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 086203251b..7b6f3c6aa5 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include TEST_ROOT("Userland/Libraries/LibWasm/Tests"); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp index bb7315afd8..80ec5a619c 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp @@ -4,9 +4,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "Interpreter.h" #include +#include #include +#include #include namespace Wasm { diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp new file mode 100644 index 0000000000..699aed649a --- /dev/null +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp @@ -0,0 +1,999 @@ +/* + * Copyright (c) 2021, Ali Mohammad Pur + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Wasm { + +#define TRAP_IF_NOT(x) \ + do { \ + if (trap_if_not(x)) { \ + dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \ + return; \ + } \ + } while (false) + +#define TRAP_IF_NOT_NORETURN(x) \ + do { \ + if (trap_if_not(x)) { \ + dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \ + } \ + } while (false) + +void BytecodeInterpreter::interpret(Configuration& configuration) +{ + m_do_trap = false; + auto& instructions = configuration.frame().expression().instructions(); + auto max_ip_value = InstructionPointer { instructions.size() }; + auto& current_ip_value = configuration.ip(); + + while (current_ip_value < max_ip_value) { + auto& instruction = instructions[current_ip_value.value()]; + auto old_ip = current_ip_value; + interpret(configuration, current_ip_value, instruction); + if (m_do_trap) + return; + if (current_ip_value == old_ip) // If no jump occurred + ++current_ip_value; + } +} + +void BytecodeInterpreter::branch_to_label(Configuration& configuration, LabelIndex index) +{ + dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value()); + auto label = configuration.nth_label(index.value()); + TRAP_IF_NOT(label.has_value()); + dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label->continuation().value(), label->arity()); + auto results = pop_values(configuration, label->arity()); + + size_t drop_count = index.value() + 1; + for (; !configuration.stack().is_empty();) { + auto& entry = configuration.stack().peek(); + if (entry.has