Interpreter.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <LibWasm/AbstractMachine/Configuration.h>
  8. namespace Wasm {
  9. struct Interpreter {
  10. virtual ~Interpreter() = default;
  11. virtual void interpret(Configuration&) = 0;
  12. virtual bool did_trap() const = 0;
  13. virtual void clear_trap() = 0;
  14. };
  15. struct BytecodeInterpreter : public Interpreter {
  16. virtual void interpret(Configuration&) override;
  17. virtual ~BytecodeInterpreter() override = default;
  18. virtual bool did_trap() const override { return m_do_trap; }
  19. virtual void clear_trap() override { m_do_trap = false; }
  20. struct CallFrameHandle {
  21. explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration)
  22. : m_configuration_handle(configuration)
  23. , m_interpreter(interpreter)
  24. {
  25. }
  26. ~CallFrameHandle() = default;
  27. Configuration::CallFrameHandle m_configuration_handle;
  28. BytecodeInterpreter& m_interpreter;
  29. };
  30. protected:
  31. virtual void interpret(Configuration&, InstructionPointer&, const Instruction&);
  32. void branch_to_label(Configuration&, LabelIndex);
  33. template<typename ReadT, typename PushT>
  34. void load_and_push(Configuration&, const Instruction&);
  35. void store_to_memory(Configuration&, const Instruction&, ReadonlyBytes data);
  36. void call_address(Configuration&, FunctionAddress);
  37. template<typename V, typename T>
  38. MakeUnsigned<T> checked_unsigned_truncate(V);
  39. template<typename V, typename T>
  40. MakeSigned<T> checked_signed_truncate(V);
  41. template<typename T>
  42. T read_value(ReadonlyBytes data);
  43. Vector<Value> pop_values(Configuration& configuration, size_t count);
  44. bool trap_if_not(bool value)
  45. {
  46. if (!value)
  47. m_do_trap = true;
  48. return m_do_trap;
  49. }
  50. bool m_do_trap { false };
  51. };
  52. struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
  53. virtual ~DebuggerBytecodeInterpreter() override = default;
  54. Function<bool(Configuration&, InstructionPointer&, const Instruction&)> pre_interpret_hook;
  55. Function<bool(Configuration&, InstructionPointer&, const Instruction&, const Interpreter&)> post_interpret_hook;
  56. private:
  57. virtual void interpret(Configuration&, InstructionPointer&, const Instruction&) override;
  58. };
  59. }