BytecodeInterpreter.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_value(); }
  15. virtual String trap_reason() const override { return m_trap.value().reason; }
  16. virtual void clear_trap() override { m_trap.clear(); }
  17. struct CallFrameHandle {
  18. explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration)
  19. : m_configuration_handle(configuration)
  20. , m_interpreter(interpreter)
  21. {
  22. }
  23. ~CallFrameHandle() = default;
  24. Configuration::CallFrameHandle m_configuration_handle;
  25. BytecodeInterpreter& m_interpreter;
  26. };
  27. protected:
  28. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&);
  29. void branch_to_label(Configuration&, LabelIndex);
  30. template<typename ReadT, typename PushT>
  31. void load_and_push(Configuration&, Instruction const&);
  32. void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data);
  33. void call_address(Configuration&, FunctionAddress);
  34. template<typename V, typename T>
  35. MakeUnsigned<T> checked_unsigned_truncate(V);
  36. template<typename V, typename T>
  37. MakeSigned<T> checked_signed_truncate(V);
  38. template<typename T>
  39. T read_value(ReadonlyBytes data);
  40. Vector<Value> pop_values(Configuration& configuration, size_t count);
  41. ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
  42. {
  43. if (!value)
  44. m_trap = Trap { reason };
  45. return m_trap.has_value();
  46. }
  47. Optional<Trap> m_trap;
  48. StackInfo m_stack_info;
  49. };
  50. struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
  51. virtual ~DebuggerBytecodeInterpreter() override = default;
  52. Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook;
  53. Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook;
  54. private:
  55. virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override;
  56. };
  57. }