Interpreter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);
  28. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  29. Value& reg(Register const& r) { return registers()[r.index()]; }
  30. [[nodiscard]] RegisterWindow snapshot_frame() const { return m_register_windows.last(); }
  31. void enter_frame(RegisterWindow const& frame)
  32. {
  33. ++m_manually_entered_frames;
  34. m_register_windows.append(make<RegisterWindow>(frame));
  35. }
  36. void leave_frame()
  37. {
  38. VERIFY(m_manually_entered_frames);
  39. --m_manually_entered_frames;
  40. m_register_windows.take_last();
  41. }
  42. void jump(Label const& label)
  43. {
  44. m_pending_jump = &label.block();
  45. }
  46. void do_return(Value return_value) { m_return_value = return_value; }
  47. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  48. void leave_unwind_context();
  49. void continue_pending_unwind(Label const& resume_label);
  50. Executable const& current_executable() { return *m_current_executable; }
  51. enum class OptimizationLevel {
  52. Default,
  53. __Count,
  54. };
  55. static Bytecode::PassManager& optimization_pipeline(OptimizationLevel = OptimizationLevel::Default);
  56. private:
  57. RegisterWindow& registers() { return m_register_windows.last(); }
  58. static AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> s_optimization_pipelines;
  59. VM& m_vm;
  60. GlobalObject& m_global_object;
  61. Realm& m_realm;
  62. NonnullOwnPtrVector<RegisterWindow> m_register_windows;
  63. Optional<BasicBlock const*> m_pending_jump;
  64. Value m_return_value;
  65. size_t m_manually_entered_frames { 0 };
  66. Executable const* m_current_executable { nullptr };
  67. Vector<UnwindInfo> m_unwind_contexts;
  68. Handle<Exception> m_saved_exception;
  69. };
  70. extern bool g_dump_bytecode;
  71. }