Interpreter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Bytecode/Label.h>
  8. #include <LibJS/Bytecode/Register.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Heap/Cell.h>
  11. #include <LibJS/Runtime/FunctionKind.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS::Bytecode {
  15. class InstructionStreamIterator;
  16. struct CallFrame {
  17. void visit_edges(Cell::Visitor& visitor)
  18. {
  19. for (auto const& value : registers)
  20. visitor.visit(value);
  21. for (auto const& environment : saved_lexical_environments)
  22. visitor.visit(environment);
  23. for (auto& context : unwind_contexts) {
  24. visitor.visit(context.lexical_environment);
  25. }
  26. }
  27. Vector<Value> registers;
  28. Vector<GCPtr<Environment>> saved_lexical_environments;
  29. Vector<UnwindInfo> unwind_contexts;
  30. };
  31. class Interpreter {
  32. public:
  33. [[nodiscard]] static bool enabled();
  34. static void set_enabled(bool);
  35. explicit Interpreter(VM&);
  36. ~Interpreter();
  37. Realm& realm();
  38. VM& vm() { return m_vm; }
  39. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  40. ThrowCompletionOr<Value> run(SourceTextModule&);
  41. ThrowCompletionOr<Value> run(Realm& realm, Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  42. {
  43. auto value_and_frame = run_and_return_frame(realm, executable, entry_point);
  44. return move(value_and_frame.value);
  45. }
  46. struct ValueAndFrame {
  47. ThrowCompletionOr<Value> value;
  48. OwnPtr<CallFrame> frame;
  49. };
  50. ValueAndFrame run_and_return_frame(Realm&, Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr);
  51. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  52. ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
  53. Value& reg(Register const& r) { return registers()[r.index()]; }
  54. auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; }
  55. auto& unwind_contexts() { return call_frame().unwind_contexts; }
  56. void jump(Label const& label)
  57. {
  58. m_pending_jump = &label.block();
  59. }
  60. void schedule_jump(Label const& label)
  61. {
  62. m_scheduled_jump = &label.block();
  63. VERIFY(unwind_contexts().last().finalizer);
  64. jump(Label { *unwind_contexts().last().finalizer });
  65. }
  66. void do_return(Value return_value)
  67. {
  68. m_return_value = return_value;
  69. m_saved_exception = {};
  70. }
  71. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  72. void leave_unwind_context();
  73. ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label);
  74. Executable& current_executable() { return *m_current_executable; }
  75. Executable const& current_executable() const { return *m_current_executable; }
  76. BasicBlock const& current_block() const { return *m_current_block; }
  77. size_t pc() const;
  78. DeprecatedString debug_position() const;
  79. VM::InterpreterExecutionScope ast_interpreter_scope(Realm&);
  80. Optional<Value>& this_value() { return m_this_value; }
  81. void visit_edges(Cell::Visitor&);
  82. private:
  83. CallFrame& call_frame()
  84. {
  85. return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; });
  86. }
  87. CallFrame const& call_frame() const
  88. {
  89. return const_cast<Interpreter*>(this)->call_frame();
  90. }
  91. Span<Value> registers() { return m_current_call_frame; }
  92. ReadonlySpan<Value> registers() const { return m_current_call_frame; }
  93. void push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*>, size_t register_count);
  94. [[nodiscard]] Variant<NonnullOwnPtr<CallFrame>, CallFrame*> pop_call_frame();
  95. VM& m_vm;
  96. Vector<Variant<NonnullOwnPtr<CallFrame>, CallFrame*>> m_call_frames;
  97. Span<Value> m_current_call_frame;
  98. Optional<BasicBlock const*> m_pending_jump;
  99. BasicBlock const* m_scheduled_jump { nullptr };
  100. Optional<Value> m_this_value;
  101. Optional<Value> m_return_value;
  102. Optional<Value> m_saved_exception;
  103. Executable* m_current_executable { nullptr };
  104. OwnPtr<JS::Interpreter> m_ast_interpreter;
  105. BasicBlock const* m_current_block { nullptr };
  106. InstructionStreamIterator* m_pc { nullptr };
  107. };
  108. extern bool g_dump_bytecode;
  109. ThrowCompletionOr<NonnullOwnPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name);
  110. }