Interpreter.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. class PassManager;
  17. struct RegisterWindow {
  18. void visit_edges(Cell::Visitor& visitor)
  19. {
  20. for (auto const& value : registers)
  21. visitor.visit(value);
  22. for (auto const& environment : saved_lexical_environments)
  23. visitor.visit(environment);
  24. for (auto const& environment : saved_variable_environments)
  25. visitor.visit(environment);
  26. for (auto& context : unwind_contexts) {
  27. visitor.visit(context.lexical_environment);
  28. visitor.visit(context.variable_environment);
  29. }
  30. }
  31. Vector<Value> registers;
  32. Vector<GCPtr<Environment>> saved_lexical_environments;
  33. Vector<GCPtr<Environment>> saved_variable_environments;
  34. Vector<UnwindInfo> unwind_contexts;
  35. };
  36. class Interpreter {
  37. public:
  38. [[nodiscard]] static bool enabled();
  39. static void set_enabled(bool);
  40. static void set_optimizations_enabled(bool);
  41. explicit Interpreter(VM&);
  42. ~Interpreter();
  43. Realm& realm();
  44. VM& vm() { return m_vm; }
  45. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  46. ThrowCompletionOr<Value> run(SourceTextModule&);
  47. ThrowCompletionOr<Value> run(Realm& realm, Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  48. {
  49. auto value_and_frame = run_and_return_frame(realm, executable, entry_point);
  50. return move(value_and_frame.value);
  51. }
  52. struct ValueAndFrame {
  53. ThrowCompletionOr<Value> value;
  54. OwnPtr<RegisterWindow> frame;
  55. };
  56. ValueAndFrame run_and_return_frame(Realm&, Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point, RegisterWindow* = nullptr);
  57. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  58. Value& reg(Register const& r) { return registers()[r.index()]; }
  59. auto& saved_lexical_environment_stack() { return window().saved_lexical_environments; }
  60. auto& saved_variable_environment_stack() { return window().saved_variable_environments; }
  61. auto& unwind_contexts() { return window().unwind_contexts; }
  62. void jump(Label const& label)
  63. {
  64. m_pending_jump = &label.block();
  65. }
  66. void schedule_jump(Label const& label)
  67. {
  68. m_scheduled_jump = &label.block();
  69. VERIFY(unwind_contexts().last().finalizer);
  70. jump(Label { *unwind_contexts().last().finalizer });
  71. }
  72. void do_return(Value return_value)
  73. {
  74. m_return_value = return_value;
  75. m_saved_exception = {};
  76. }
  77. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  78. void leave_unwind_context();
  79. ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label);
  80. Executable const& current_executable() { return *m_current_executable; }
  81. BasicBlock const& current_block() const { return *m_current_block; }
  82. size_t pc() const;
  83. DeprecatedString debug_position() const;
  84. static Bytecode::PassManager& optimization_pipeline();
  85. VM::InterpreterExecutionScope ast_interpreter_scope(Realm&);
  86. void visit_edges(Cell::Visitor&);
  87. private:
  88. RegisterWindow& window()
  89. {
  90. return m_register_windows.last().visit([](auto& x) -> RegisterWindow& { return *x; });
  91. }
  92. RegisterWindow const& window() const
  93. {
  94. return const_cast<Interpreter*>(this)->window();
  95. }
  96. Span<Value> registers() { return m_current_register_window; }
  97. ReadonlySpan<Value> registers() const { return m_current_register_window; }
  98. void push_register_window(Variant<NonnullOwnPtr<RegisterWindow>, RegisterWindow*>, size_t register_count);
  99. [[nodiscard]] Variant<NonnullOwnPtr<RegisterWindow>, RegisterWindow*> pop_register_window();
  100. VM& m_vm;
  101. Vector<Variant<NonnullOwnPtr<RegisterWindow>, RegisterWindow*>> m_register_windows;
  102. Span<Value> m_current_register_window;
  103. Optional<BasicBlock const*> m_pending_jump;
  104. BasicBlock const* m_scheduled_jump { nullptr };
  105. Optional<Value> m_return_value;
  106. Optional<Value> m_saved_return_value;
  107. Optional<Value> m_saved_exception;
  108. Executable const* m_current_executable { nullptr };
  109. OwnPtr<JS::Interpreter> m_ast_interpreter;
  110. BasicBlock const* m_current_block { nullptr };
  111. InstructionStreamIterator* m_pc { nullptr };
  112. };
  113. extern bool g_dump_bytecode;
  114. ThrowCompletionOr<NonnullOwnPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name);
  115. }