Serenity Operating System
at master 88 lines 3.1 kB view raw
1/* 2 * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/StackInfo.h> 10#include <LibWasm/AbstractMachine/Configuration.h> 11#include <LibWasm/AbstractMachine/Interpreter.h> 12 13namespace Wasm { 14 15struct BytecodeInterpreter : public Interpreter { 16 virtual void interpret(Configuration&) override; 17 virtual ~BytecodeInterpreter() override = default; 18 virtual bool did_trap() const override { return !m_trap.has<Empty>(); } 19 virtual DeprecatedString trap_reason() const override 20 { 21 return m_trap.visit( 22 [](Empty) -> DeprecatedString { VERIFY_NOT_REACHED(); }, 23 [](Trap const& trap) { return trap.reason; }, 24 [](JS::Completion const& completion) { return completion.value()->to_string_without_side_effects().release_value().to_deprecated_string(); }); 25 } 26 virtual void clear_trap() override { m_trap = Empty {}; } 27 28 struct CallFrameHandle { 29 explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration) 30 : m_configuration_handle(configuration) 31 , m_interpreter(interpreter) 32 { 33 } 34 35 ~CallFrameHandle() = default; 36 37 Configuration::CallFrameHandle m_configuration_handle; 38 BytecodeInterpreter& m_interpreter; 39 }; 40 41protected: 42 virtual void interpret(Configuration&, InstructionPointer&, Instruction const&); 43 void branch_to_label(Configuration&, LabelIndex); 44 template<typename ReadT, typename PushT> 45 void load_and_push(Configuration&, Instruction const&); 46 template<typename PopT, typename StoreT> 47 void pop_and_store(Configuration&, Instruction const&); 48 void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, i32 base); 49 void call_address(Configuration&, FunctionAddress); 50 51 template<typename PopType, typename PushType, typename Operator> 52 void binary_numeric_operation(Configuration&); 53 54 template<typename PopType, typename PushType, typename Operator> 55 void unary_operation(Configuration&); 56 57 template<typename V, typename T> 58 MakeUnsigned<T> checked_unsigned_truncate(V); 59 60 template<typename V, typename T> 61 MakeSigned<T> checked_signed_truncate(V); 62 63 template<typename T> 64 T read_value(ReadonlyBytes data); 65 66 Vector<Value> pop_values(Configuration& configuration, size_t count); 67 ALWAYS_INLINE bool trap_if_not(bool value, StringView reason) 68 { 69 if (!value) 70 m_trap = Trap { reason }; 71 return !m_trap.has<Empty>(); 72 } 73 74 Variant<Trap, JS::Completion, Empty> m_trap; 75 StackInfo m_stack_info; 76}; 77 78struct DebuggerBytecodeInterpreter : public BytecodeInterpreter { 79 virtual ~DebuggerBytecodeInterpreter() override = default; 80 81 Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook; 82 Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook; 83 84private: 85 virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override; 86}; 87 88}