Configuration.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/AbstractMachine.h>
  8. namespace Wasm {
  9. class Configuration {
  10. public:
  11. explicit Configuration(Store& store)
  12. : m_store(store)
  13. {
  14. }
  15. void set_frame(Frame frame)
  16. {
  17. Label label(frame.arity(), frame.expression().instructions().size(), m_value_stack.size());
  18. frame.label_index() = m_label_stack.size();
  19. m_frame_stack.append(move(frame));
  20. m_label_stack.append(label);
  21. }
  22. ALWAYS_INLINE auto& frame() const { return m_frame_stack.last(); }
  23. ALWAYS_INLINE auto& frame() { return m_frame_stack.last(); }
  24. ALWAYS_INLINE auto& ip() const { return m_ip; }
  25. ALWAYS_INLINE auto& ip() { return m_ip; }
  26. ALWAYS_INLINE auto& depth() const { return m_depth; }
  27. ALWAYS_INLINE auto& depth() { return m_depth; }
  28. ALWAYS_INLINE auto& value_stack() const { return m_value_stack; }
  29. ALWAYS_INLINE auto& value_stack() { return m_value_stack; }
  30. ALWAYS_INLINE auto& label_stack() const { return m_label_stack; }
  31. ALWAYS_INLINE auto& label_stack() { return m_label_stack; }
  32. ALWAYS_INLINE auto& store() const { return m_store; }
  33. ALWAYS_INLINE auto& store() { return m_store; }
  34. struct CallFrameHandle {
  35. explicit CallFrameHandle(Configuration& configuration)
  36. : ip(configuration.ip())
  37. , configuration(configuration)
  38. {
  39. configuration.depth()++;
  40. }
  41. ~CallFrameHandle()
  42. {
  43. configuration.unwind({}, *this);
  44. }
  45. InstructionPointer ip { 0 };
  46. Configuration& configuration;
  47. };
  48. void unwind(Badge<CallFrameHandle>, CallFrameHandle const&);
  49. Result call(Interpreter&, FunctionAddress, Vector<Value> arguments);
  50. Result execute(Interpreter&);
  51. void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
  52. bool should_limit_instruction_count() const { return m_should_limit_instruction_count; }
  53. void dump_stack();
  54. private:
  55. Store& m_store;
  56. Vector<Value> m_value_stack;
  57. Vector<Label> m_label_stack;
  58. Vector<Frame> m_frame_stack;
  59. size_t m_depth { 0 };
  60. InstructionPointer m_ip;
  61. bool m_should_limit_instruction_count { false };
  62. };
  63. }