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. explicit Interpreter(Realm&);
  25. ~Interpreter();
  26. // FIXME: Remove this thing once we don't need it anymore!
  27. static Interpreter* current();
  28. Realm& realm() { return m_realm; }
  29. VM& vm() { return m_vm; }
  30. ThrowCompletionOr<Value> run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  31. {
  32. auto value_and_frame = run_and_return_frame(executable, entry_point);
  33. return move(value_and_frame.value);
  34. }
  35. struct ValueAndFrame {
  36. ThrowCompletionOr<Value> value;
  37. OwnPtr<RegisterWindow> frame;
  38. };
  39. ValueAndFrame run_and_return_frame(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point, RegisterWindow* = nullptr);
  40. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  41. Value& reg(Register const& r) { return registers()[r.index()]; }
  42. auto& saved_lexical_environment_stack() { return window().saved_lexical_environments; }
  43. auto& saved_variable_environment_stack() { return window().saved_variable_environments; }
  44. void jump(Label const& label)
  45. {
  46. m_pending_jump = &label.block();
  47. }
  48. void do_return(Value return_value) { m_return_value = return_value; }
  49. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  50. void leave_unwind_context();
  51. ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label);
  52. Executable const& current_executable() { return *m_current_executable; }
  53. enum class OptimizationLevel {
  54. None,
  55. Optimize,
  56. __Count,
  57. Default = None,
  58. };
  59. static Bytecode::PassManager& optimization_pipeline(OptimizationLevel = OptimizationLevel::Default);
  60. VM::InterpreterExecutionScope ast_interpreter_scope();
  61. private:
  62. RegisterWindow& window()
  63. {
  64. return m_register_windows.last().visit([](auto& x) -> RegisterWindow& { return *x; });
  65. }
  66. RegisterWindow const& window() const
  67. {
  68. return const_cast<Interpreter*>(this)->window();
  69. }
  70. MarkedVector<Value>& registers() { return window().registers; }
  71. static AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> s_optimization_pipelines;
  72. VM& m_vm;
  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. }