Interpreter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Bytecode/Executable.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/Runtime/FunctionKind.h>
  13. #include <LibJS/Runtime/VM.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode {
  16. class InstructionStreamIterator;
  17. struct CallFrame {
  18. void visit_edges(Cell::Visitor& visitor)
  19. {
  20. for (auto const& value : registers)
  21. visitor.visit(value);
  22. for (auto const& environment : saved_lexical_environments)
  23. visitor.visit(environment);
  24. for (auto& context : unwind_contexts) {
  25. visitor.visit(context.lexical_environment);
  26. }
  27. }
  28. Vector<Value> registers;
  29. Vector<GCPtr<Environment>> saved_lexical_environments;
  30. Vector<UnwindInfo> unwind_contexts;
  31. Vector<BasicBlock const*> previously_scheduled_jumps;
  32. };
  33. class Interpreter {
  34. public:
  35. explicit Interpreter(VM&);
  36. ~Interpreter();
  37. Realm& realm();
  38. VM& vm() { return m_vm; }
  39. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  40. ThrowCompletionOr<Value> run(SourceTextModule&);
  41. ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  42. {
  43. auto value_and_frame = run_and_return_frame(executable, entry_point);
  44. return move(value_and_frame.value);
  45. }
  46. struct ValueAndFrame {
  47. ThrowCompletionOr<Value> value;
  48. OwnPtr<CallFrame> frame;
  49. };
  50. ValueAndFrame run_and_return_frame(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr);
  51. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  52. ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
  53. Value& reg(Register const& r) { return registers()[r.index()]; }
  54. auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; }
  55. auto& unwind_contexts() { return call_frame().unwind_contexts; }
  56. void do_return(Value value)
  57. {
  58. reg(Register::return_value()) = value;
  59. reg(Register::exception()) = {};
  60. }
  61. void enter_unwind_context();
  62. void leave_unwind_context();
  63. Executable& current_executable() { return *m_current_executable; }
  64. Executable const& current_executable() const { return *m_current_executable; }
  65. BasicBlock const& current_block() const { return *m_current_block; }
  66. Optional<InstructionStreamIterator const&> instruction_stream_iterator() const { return m_pc; }
  67. void visit_edges(Cell::Visitor&);
  68. Span<Value> registers() { return m_current_call_frame; }
  69. ReadonlySpan<Value> registers() const { return m_current_call_frame; }
  70. private:
  71. void run_bytecode();
  72. CallFrame& call_frame()
  73. {
  74. return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; });
  75. }
  76. CallFrame const& call_frame() const
  77. {
  78. return const_cast<Interpreter*>(this)->call_frame();
  79. }
  80. void push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*>, size_t register_count);
  81. [[nodiscard]] Variant<NonnullOwnPtr<CallFrame>, CallFrame*> pop_call_frame();
  82. VM& m_vm;
  83. Vector<Variant<NonnullOwnPtr<CallFrame>, CallFrame*>> m_call_frames;
  84. Span<Value> m_current_call_frame;
  85. BasicBlock const* m_scheduled_jump { nullptr };
  86. Executable* m_current_executable { nullptr };
  87. BasicBlock const* m_current_block { nullptr };
  88. Optional<InstructionStreamIterator&> m_pc {};
  89. };
  90. extern bool g_dump_bytecode;
  91. ThrowCompletionOr<NonnullRefPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name);
  92. }