Interpreter.h 4.7 KB

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