/* * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include namespace JS::Bytecode { class InstructionStreamIterator; class PassManager; struct CallFrame { static NonnullOwnPtr create(size_t register_count); void operator delete(void* ptr) { free(ptr); } void visit_edges(Cell::Visitor& visitor) { for (auto const& value : registers()) visitor.visit(value); for (auto const& environment : saved_lexical_environments) visitor.visit(environment); for (auto& context : unwind_contexts) { visitor.visit(context.lexical_environment); } } Vector> saved_lexical_environments; Vector unwind_contexts; Vector previously_scheduled_jumps; Span registers() { return { register_values, register_count }; } ReadonlySpan registers() const { return { register_values, register_count }; } size_t register_count { 0 }; Value register_values[]; }; class Interpreter { public: explicit Interpreter(VM&); ~Interpreter(); [[nodiscard]] Realm& realm() { return *m_realm; } [[nodiscard]] Object& global_object() { return *m_global_object; } [[nodiscard]] DeclarativeEnvironment& global_declarative_environment() { return *m_global_declarative_environment; } VM& vm() { return m_vm; } VM const& vm() const { return m_vm; } ThrowCompletionOr run(Script&, JS::GCPtr lexical_environment_override = nullptr); ThrowCompletionOr run(SourceTextModule&); ThrowCompletionOr run(Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr) { auto value_and_frame = run_and_return_frame(executable, entry_point); return move(value_and_frame.value); } struct ValueAndFrame { ThrowCompletionOr value; OwnPtr frame; }; ValueAndFrame run_and_return_frame(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr); ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); } ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); } Value& reg(Register const& r) { return registers()[r.index()]; } Value reg(Register const& r) const { return registers()[r.index()]; } [[nodiscard]] Value get(Operand) const; void set(Operand, Value); auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; } auto& unwind_contexts() { return call_frame().unwind_contexts; } void do_return(Value value) { reg(Register::return_value()) = value; reg(Register::exception()) = {}; } void enter_unwind_context(); void leave_unwind_context(); void catch_exception(Operand dst); void enter_object_environment(Object&); Executable& current_executable() { return *m_current_executable; } Executable const& current_executable() const { return *m_current_executable; } BasicBlock const& current_block() const { return *m_current_block; } Optional instruction_stream_iterator() const { return m_pc; } void visit_edges(Cell::Visitor&); Span registers() { return m_current_call_frame; } ReadonlySpan registers() const { return m_current_call_frame; } private: void run_bytecode(); CallFrame& call_frame() { return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; }); } CallFrame const& call_frame() const { return const_cast(this)->call_frame(); } void push_call_frame(Variant, CallFrame*>); [[nodiscard]] Variant, CallFrame*> pop_call_frame(); VM& m_vm; Vector, CallFrame*>> m_call_frames; Span m_current_call_frame; BasicBlock const* m_scheduled_jump { nullptr }; Executable* m_current_executable { nullptr }; BasicBlock const* m_current_block { nullptr }; Realm* m_realm { nullptr }; Object* m_global_object { nullptr }; DeclarativeEnvironment* m_global_declarative_environment { nullptr }; Optional m_pc {}; }; extern bool g_dump_bytecode; ThrowCompletionOr> compile(VM&, ASTNode const&, ReadonlySpan, JS::FunctionKind kind, DeprecatedFlyString const& name); }