Interpreter.h 3.1 KB

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