Interpreter.h 2.6 KB

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