Interpreter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, RegisterWindow* = nullptr);
  41. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  42. Value& reg(Register const& r) { return registers()[r.index()]; }
  43. auto& saved_lexical_environment_stack() { return window().saved_lexical_environments; }
  44. auto& saved_variable_environment_stack() { return window().saved_variable_environments; }
  45. void jump(Label const& label)
  46. {
  47. m_pending_jump = &label.block();
  48. }
  49. void do_return(Value return_value) { m_return_value = return_value; }
  50. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  51. void leave_unwind_context();
  52. ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label);
  53. Executable const& current_executable() { return *m_current_executable; }
  54. enum class OptimizationLevel {
  55. Default,
  56. __Count,
  57. };
  58. static Bytecode::PassManager& optimization_pipeline(OptimizationLevel = OptimizationLevel::Default);
  59. VM::InterpreterExecutionScope ast_interpreter_scope();
  60. private:
  61. RegisterWindow& window()
  62. {
  63. return m_register_windows.last().visit([](auto& x) -> RegisterWindow& { return *x; });
  64. }
  65. RegisterWindow const& window() const
  66. {
  67. return const_cast<Interpreter*>(this)->window();
  68. }
  69. MarkedVector<Value>& registers() { return window().registers; }
  70. static AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> s_optimization_pipelines;
  71. VM& m_vm;
  72. GlobalObject& m_global_object;
  73. Realm& m_realm;
  74. Vector<Variant<NonnullOwnPtr<RegisterWindow>, RegisterWindow*>> m_register_windows;
  75. Optional<BasicBlock const*> m_pending_jump;
  76. Value m_return_value;
  77. Executable const* m_current_executable { nullptr };
  78. Vector<UnwindInfo> m_unwind_contexts;
  79. Handle<Value> m_saved_exception;
  80. OwnPtr<JS::Interpreter> m_ast_interpreter;
  81. };
  82. extern bool g_dump_bytecode;
  83. }