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