BytecodeInterpreter.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StackInfo.h>
  8. #include <LibWasm/AbstractMachine/Configuration.h>
  9. #include <LibWasm/AbstractMachine/Interpreter.h>
  10. namespace Wasm {
  11. struct BytecodeInterpreter : public Interpreter {
  12. explicit BytecodeInterpreter(StackInfo const& stack_info)
  13. : m_stack_info(stack_info)
  14. {
  15. }
  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. struct CallFrameHandle {
  28. explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration)
  29. : m_configuration_handle(configuration)
  30. , m_interpreter(interpreter)
  31. {
  32. }
  33. ~CallFrameHandle() = default;
  34. Configuration::CallFrameHandle m_configuration_handle;
  35. BytecodeInterpreter& m_interpreter;
  36. };
  37. protected:
  38. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&);
  39. void branch_to_label(Configuration&, LabelIndex);
  40. template<typename ReadT, typename PushT>
  41. void load_and_push(Configuration&, Instruction const&);
  42. template<typename PopT, typename StoreT>
  43. void pop_and_store(Configuration&, Instruction const&);
  44. void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, i32 base);
  45. void call_address(Configuration&, FunctionAddress);
  46. template<typename PopType, typename PushType, typename Operator>
  47. void binary_numeric_operation(Configuration&);
  48. template<typename PopType, typename PushType, typename Operator>
  49. void unary_operation(Configuration&);
  50. template<typename V, typename T>
  51. MakeUnsigned<T> checked_unsigned_truncate(V);
  52. template<typename V, typename T>
  53. MakeSigned<T> checked_signed_truncate(V);
  54. template<typename T>
  55. T read_value(ReadonlyBytes data);
  56. Vector<Value> pop_values(Configuration& configuration, size_t count);
  57. ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
  58. {
  59. if (!value)
  60. m_trap = Trap { reason };
  61. return !m_trap.has<Empty>();
  62. }
  63. Variant<Trap, JS::Completion, Empty> m_trap;
  64. StackInfo const& m_stack_info;
  65. };
  66. struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
  67. DebuggerBytecodeInterpreter(StackInfo const& stack_info)
  68. : BytecodeInterpreter(stack_info)
  69. {
  70. }
  71. virtual ~DebuggerBytecodeInterpreter() override = default;
  72. Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook;
  73. Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook;
  74. private:
  75. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override;
  76. };
  77. }