Interpreter.h 4.5 KB

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