Interpreter.h 4.3 KB

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