Interpreter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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<Environment*> saved_lexical_environments;
  20. MarkedVector<Environment*> saved_variable_environments;
  21. };
  22. class Interpreter {
  23. public:
  24. Interpreter(GlobalObject&, Realm&);
  25. ~Interpreter();
  26. // FIXME: Remove this thing once we don't need it anymore!
  27. static Interpreter* current();
  28. GlobalObject& global_object() { return m_global_object; }
  29. Realm& realm() { return m_realm; }
  30. VM& vm() { return m_vm; }
  31. ThrowCompletionOr<Value> run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  32. {
  33. auto value_and_frame = run_and_return_frame(executable, entry_point);
  34. return move(value_and_frame.value);
  35. }
  36. struct ValueAndFrame {
  37. ThrowCompletionOr<Value> value;
  38. OwnPtr<RegisterWindow> frame;
  39. };
  40. ValueAndFrame run_and_return_frame(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point);
  41. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  42. Value& reg(Register const& r) { return registers()[r.index()]; }
  43. [[nodiscard]] RegisterWindow snapshot_frame() const { return m_register_windows.last(); }
  44. auto& saved_lexical_environment_stack() { return m_register_windows.last().saved_lexical_environments; }
  45. auto& saved_variable_environment_stack() { return m_register_windows.last().saved_variable_environments; }
  46. void enter_frame(RegisterWindow const& frame)
  47. {
  48. m_manually_entered_frames.append(true);
  49. m_register_windows.append(make<RegisterWindow>(frame));
  50. }
  51. NonnullOwnPtr<RegisterWindow> pop_frame()
  52. {
  53. VERIFY(!m_manually_entered_frames.is_empty());
  54. VERIFY(m_manually_entered_frames.last());
  55. m_manually_entered_frames.take_last();
  56. return m_register_windows.take_last();
  57. }
  58. void jump(Label const& label)
  59. {
  60. m_pending_jump = &label.block();
  61. }
  62. void do_return(Value return_value) { m_return_value = return_value; }
  63. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  64. void leave_unwind_context();
  65. ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label);
  66. Executable const& current_executable() { return *m_current_executable; }
  67. enum class OptimizationLevel {
  68. Default,
  69. __Count,
  70. };
  71. static Bytecode::PassManager& optimization_pipeline(OptimizationLevel = OptimizationLevel::Default);
  72. VM::InterpreterExecutionScope ast_interpreter_scope();
  73. private:
  74. MarkedVector<Value>& registers() { return m_register_windows.last().registers; }
  75. static AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> s_optimization_pipelines;
  76. VM& m_vm;
  77. GlobalObject& m_global_object;
  78. Realm& m_realm;
  79. NonnullOwnPtrVector<RegisterWindow> m_register_windows;
  80. Vector<bool> m_manually_entered_frames;
  81. Optional<BasicBlock const*> m_pending_jump;
  82. Value m_return_value;
  83. Executable const* m_current_executable { nullptr };
  84. Vector<UnwindInfo> m_unwind_contexts;
  85. Handle<Value> m_saved_exception;
  86. OwnPtr<JS::Interpreter> m_ast_interpreter;
  87. };
  88. extern bool g_dump_bytecode;
  89. }