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