BytecodeInterpreter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ByteString trap_reason() const override
  20. {
  21. return m_trap.visit(
  22. [](Empty) -> ByteString { VERIFY_NOT_REACHED(); },
  23. [](Trap const& trap) { return trap.reason; },
  24. [](JS::Completion const& completion) { return completion.value()->to_string_without_side_effects().to_byte_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. template<size_t N>
  45. void pop_and_store_lane_n(Configuration&, Instruction const&);
  46. template<size_t M, size_t N, template<typename> typename SetSign>
  47. void load_and_push_mxn(Configuration&, Instruction const&);
  48. template<size_t N>
  49. void load_and_push_lane_n(Configuration&, Instruction const&);
  50. template<size_t N>
  51. void load_and_push_zero_n(Configuration&, Instruction const&);
  52. template<size_t M>
  53. void load_and_push_m_splat(Configuration&, Instruction const&);
  54. template<size_t M, template<size_t> typename NativeType>
  55. void set_top_m_splat(Configuration&, NativeType<M>);
  56. template<size_t M, template<size_t> typename NativeType>
  57. void pop_and_push_m_splat(Configuration&, Instruction const&);
  58. template<typename M, template<typename> typename SetSign, typename VectorType = Native128ByteVectorOf<M, SetSign>>
  59. Optional<VectorType> pop_vector(Configuration&);
  60. template<typename M, template<typename> typename SetSign, typename VectorType = Native128ByteVectorOf<M, SetSign>>
  61. Optional<VectorType> peek_vector(Configuration&);
  62. void store_to_memory(Configuration&, Instruction::MemoryArgument const&, ReadonlyBytes data, u32 base);
  63. void call_address(Configuration&, FunctionAddress);
  64. template<typename PopTypeLHS, typename PushType, typename Operator, typename PopTypeRHS = PopTypeLHS, typename... Args>
  65. void binary_numeric_operation(Configuration&, Args&&...);
  66. template<typename PopType, typename PushType, typename Operator, typename... Args>
  67. void unary_operation(Configuration&, Args&&...);
  68. template<typename T>
  69. T read_value(ReadonlyBytes data);
  70. Vector<Value> pop_values(Configuration& configuration, size_t count);
  71. ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
  72. {
  73. if (!value)
  74. m_trap = Trap { reason };
  75. return !m_trap.has<Empty>();
  76. }
  77. Variant<Trap, JS::Completion, Empty> m_trap;
  78. StackInfo const& m_stack_info;
  79. };
  80. struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
  81. DebuggerBytecodeInterpreter(StackInfo const& stack_info)
  82. : BytecodeInterpreter(stack_info)
  83. {
  84. }
  85. virtual ~DebuggerBytecodeInterpreter() override = default;
  86. Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook;
  87. Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook;
  88. private:
  89. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override;
  90. };
  91. }