Interpreter.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <LibJS/Bytecode/Label.h>
  9. #include <LibJS/Bytecode/Register.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Heap/Handle.h>
  13. #include <LibJS/Runtime/Exception.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode {
  16. using RegisterWindow = Vector<Value>;
  17. class Interpreter {
  18. public:
  19. explicit Interpreter(GlobalObject&);
  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. VM& vm() { return m_vm; }
  25. Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);
  26. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  27. Value& reg(Register const& r) { return registers()[r.index()]; }
  28. [[nodiscard]] RegisterWindow snapshot_frame() const { return m_register_windows.last(); }
  29. void enter_frame(RegisterWindow const& frame)
  30. {
  31. ++m_manually_entered_frames;
  32. m_register_windows.append(make<RegisterWindow>(frame));
  33. }
  34. void leave_frame()
  35. {
  36. VERIFY(m_manually_entered_frames);
  37. --m_manually_entered_frames;
  38. m_register_windows.take_last();
  39. }
  40. void jump(Label const& label)
  41. {
  42. m_pending_jump = &label.block();
  43. }
  44. void do_return(Value return_value) { m_return_value = return_value; }
  45. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  46. void leave_unwind_context();
  47. void continue_pending_unwind(Label const& resume_label);
  48. Executable const& current_executable() { return *m_current_executable; }
  49. private:
  50. RegisterWindow& registers() { return m_register_windows.last(); }
  51. VM& m_vm;
  52. GlobalObject& m_global_object;
  53. NonnullOwnPtrVector<RegisterWindow> m_register_windows;
  54. Optional<BasicBlock const*> m_pending_jump;
  55. Value m_return_value;
  56. size_t m_manually_entered_frames { 0 };
  57. Executable const* m_current_executable { nullptr };
  58. Vector<UnwindInfo> m_unwind_contexts;
  59. Handle<Exception> m_saved_exception;
  60. };
  61. }