BytecodeInterpreter.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. virtual void interpret(Configuration&) override;
  13. virtual ~BytecodeInterpreter() override = default;
  14. virtual bool did_trap() const override { return !m_trap.has<Empty>(); }
  15. virtual DeprecatedString trap_reason() const override
  16. {
  17. return m_trap.visit(
  18. [](Empty) -> DeprecatedString { VERIFY_NOT_REACHED(); },
  19. [](Trap const& trap) { return trap.reason; },
  20. [](JS::Completion const& completion) { return completion.value()->to_string_without_side_effects().release_value().to_deprecated_string(); });
  21. }
  22. virtual void clear_trap() override { m_trap = Empty {}; }
  23. struct CallFrameHandle {
  24. explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration)
  25. : m_configuration_handle(configuration)
  26. , m_interpreter(interpreter)
  27. {
  28. }
  29. ~CallFrameHandle() = default;
  30. Configuration::CallFrameHandle m_configuration_handle;
  31. BytecodeInterpreter& m_interpreter;
  32. };
  33. protected:
  34. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&);
  35. void branch_to_label(Configuration&, LabelIndex);
  36. template<typename ReadT, typename PushT>
  37. void load_and_push(Configuration&, Instruction const&);
  38. template<typename PopT, typename StoreT>
  39. void pop_and_store(Configuration&, Instruction const&);
  40. void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, i32 base);
  41. void call_address(Configuration&, FunctionAddress);
  42. template<typename PopType, typename PushType, typename Operator>
  43. void binary_numeric_operation(Configuration&);
  44. template<typename PopType, typename PushType, typename Operator>
  45. void unary_operation(Configuration&);
  46. template<typename V, typename T>
  47. MakeUnsigned<T> checked_unsigned_truncate(V);
  48. template<typename V, typename T>
  49. MakeSigned<T> checked_signed_truncate(V);
  50. template<typename T>
  51. T read_value(ReadonlyBytes data);
  52. Vector<Value> pop_values(Configuration& configuration, size_t count);
  53. ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
  54. {
  55. if (!value)
  56. m_trap = Trap { reason };
  57. return !m_trap.has<Empty>();
  58. }
  59. Variant<Trap, JS::Completion, Empty> m_trap;
  60. StackInfo m_stack_info;
  61. };
  62. struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
  63. virtual ~DebuggerBytecodeInterpreter() override = default;
  64. Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook;
  65. Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook;
  66. private:
  67. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override;
  68. };
  69. }