Interpreter.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. static NonnullOwnPtr<CallFrame> create(size_t register_count);
  19. void operator delete(void* ptr) { free(ptr); }
  20. void visit_edges(Cell::Visitor& visitor)
  21. {
  22. for (auto const& value : registers())
  23. visitor.visit(value);
  24. for (auto const& environment : saved_lexical_environments)
  25. visitor.visit(environment);
  26. for (auto& context : unwind_contexts) {
  27. visitor.visit(context.lexical_environment);
  28. }
  29. }
  30. Vector<GCPtr<Environment>> saved_lexical_environments;
  31. Vector<UnwindInfo> unwind_contexts;
  32. Vector<BasicBlock const*> previously_scheduled_jumps;
  33. Span<Value> registers() { return { register_values, register_count }; }
  34. ReadonlySpan<Value> registers() const { return { register_values, register_count }; }
  35. size_t register_count { 0 };
  36. Value register_values[];
  37. };
  38. class Interpreter {
  39. public:
  40. explicit Interpreter(VM&);
  41. ~Interpreter();
  42. Realm& realm();
  43. VM& vm() { return m_vm; }
  44. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  45. ThrowCompletionOr<Value> run(SourceTextModule&);
  46. ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  47. {
  48. auto value_and_frame = run_and_return_frame(executable, entry_point);
  49. return move(value_and_frame.value);
  50. }
  51. struct ValueAndFrame {
  52. ThrowCompletionOr<Value> value;
  53. OwnPtr<CallFrame> frame;
  54. };
  55. ValueAndFrame run_and_return_frame(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr);
  56. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  57. ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
  58. Value& reg(Register const& r) { return registers()[r.index()]; }
  59. auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; }
  60. auto& unwind_contexts() { return call_frame().unwind_contexts; }
  61. void do_return(Value value)
  62. {
  63. reg(Register::return_value()) = value;
  64. reg(Register::exception()) = {};
  65. }
  66. void enter_unwind_context();
  67. void leave_unwind_context();
  68. void catch_exception();
  69. void enter_object_environment(Object&);
  70. Executable& current_executable() { return *m_current_executable; }
  71. Executable const& current_executable() const { return *m_current_executable; }
  72. BasicBlock const& current_block() const { return *m_current_block; }
  73. Optional<InstructionStreamIterator const&> instruction_stream_iterator() const { return m_pc; }
  74. void visit_edges(Cell::Visitor&);
  75. Span<Value> registers() { return m_current_call_frame; }
  76. ReadonlySpan<Value> registers() const { return m_current_call_frame; }
  77. private:
  78. void run_bytecode();
  79. CallFrame& call_frame()
  80. {
  81. return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; });
  82. }
  83. CallFrame const& call_frame() const
  84. {
  85. return const_cast<Interpreter*>(this)->call_frame();
  86. }
  87. void push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*>);
  88. [[nodiscard]] Variant<NonnullOwnPtr<CallFrame>, CallFrame*> pop_call_frame();
  89. VM& m_vm;
  90. Vector<Variant<NonnullOwnPtr<CallFrame>, CallFrame*>> m_call_frames;
  91. Span<Value> m_current_call_frame;
  92. BasicBlock const* m_scheduled_jump { nullptr };
  93. Executable* m_current_executable { nullptr };
  94. BasicBlock const* m_current_block { nullptr };
  95. Optional<InstructionStreamIterator&> m_pc {};
  96. };
  97. extern bool g_dump_bytecode;
  98. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name);
  99. }