Interpreter.h 3.2 KB

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