2021-06-03 10:46:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-10-30 22:06:27 +01:00
|
|
|
#include <LibJS/Bytecode/Executable.h>
|
2021-06-04 12:07:38 +02:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Bytecode/Register.h>
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
#include <LibJS/Heap/Cell.h>
|
2023-06-22 15:59:18 +02:00
|
|
|
#include <LibJS/Runtime/FunctionKind.h>
|
2022-02-11 22:38:21 +03:30
|
|
|
#include <LibJS/Runtime/VM.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
2023-06-17 13:16:35 +02:00
|
|
|
class InstructionStreamIterator;
|
|
|
|
|
2023-07-20 10:46:42 +02:00
|
|
|
struct CallFrame {
|
2023-11-27 17:27:51 +01:00
|
|
|
static NonnullOwnPtr<CallFrame> create(size_t register_count);
|
|
|
|
|
|
|
|
void operator delete(void* ptr) { free(ptr); }
|
|
|
|
|
2023-07-02 12:53:10 +02:00
|
|
|
void visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
2023-11-27 17:27:51 +01:00
|
|
|
for (auto const& value : registers())
|
2023-07-02 12:53:10 +02:00
|
|
|
visitor.visit(value);
|
|
|
|
for (auto const& environment : saved_lexical_environments)
|
|
|
|
visitor.visit(environment);
|
|
|
|
for (auto& context : unwind_contexts) {
|
|
|
|
visitor.visit(context.lexical_environment);
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 17:27:51 +01:00
|
|
|
|
2023-07-02 12:53:10 +02:00
|
|
|
Vector<GCPtr<Environment>> saved_lexical_environments;
|
2022-11-25 23:15:44 +00:00
|
|
|
Vector<UnwindInfo> unwind_contexts;
|
2023-10-21 22:46:40 +02:00
|
|
|
Vector<BasicBlock const*> previously_scheduled_jumps;
|
2023-11-27 17:27:51 +01:00
|
|
|
|
|
|
|
Span<Value> registers() { return { register_values, register_count }; }
|
|
|
|
ReadonlySpan<Value> registers() const { return { register_values, register_count }; }
|
|
|
|
|
|
|
|
size_t register_count { 0 };
|
|
|
|
Value register_values[];
|
2022-02-12 19:48:45 +03:30
|
|
|
};
|
2021-06-05 15:53:36 +02:00
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
class Interpreter {
|
|
|
|
public:
|
2023-06-22 15:59:18 +02:00
|
|
|
explicit Interpreter(VM&);
|
2021-06-03 10:46:30 +02:00
|
|
|
~Interpreter();
|
|
|
|
|
2023-06-22 15:59:18 +02:00
|
|
|
Realm& realm();
|
2021-06-03 18:26:13 +02:00
|
|
|
VM& vm() { return m_vm; }
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2023-06-15 12:36:57 +02:00
|
|
|
ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
|
|
|
|
ThrowCompletionOr<Value> run(SourceTextModule&);
|
|
|
|
|
2023-09-21 09:26:32 +02:00
|
|
|
ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr)
|
2021-11-11 00:34:44 +03:30
|
|
|
{
|
2023-09-21 09:26:32 +02:00
|
|
|
auto value_and_frame = run_and_return_frame(executable, entry_point);
|
2022-02-11 22:38:21 +03:30
|
|
|
return move(value_and_frame.value);
|
2021-11-11 00:34:44 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueAndFrame {
|
2021-11-11 04:11:56 +03:30
|
|
|
ThrowCompletionOr<Value> value;
|
2023-07-20 10:46:42 +02:00
|
|
|
OwnPtr<CallFrame> frame;
|
2021-11-11 00:34:44 +03:30
|
|
|
};
|
2023-09-21 09:26:32 +02:00
|
|
|
ValueAndFrame run_and_return_frame(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr);
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2021-06-07 20:58:36 -07:00
|
|
|
ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
|
2023-07-21 17:30:07 +02:00
|
|
|
ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
|
2021-06-05 15:53:36 +02:00
|
|
|
Value& reg(Register const& r) { return registers()[r.index()]; }
|
2021-06-11 01:38:30 +04:30
|
|
|
|
2023-07-20 10:46:42 +02:00
|
|
|
auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; }
|
|
|
|
auto& unwind_contexts() { return call_frame().unwind_contexts; }
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2023-09-26 15:32:46 +02:00
|
|
|
void do_return(Value value)
|
2022-12-25 17:15:29 +01:00
|
|
|
{
|
2023-09-26 15:32:46 +02:00
|
|
|
reg(Register::return_value()) = value;
|
2023-09-26 11:57:42 +02:00
|
|
|
reg(Register::exception()) = {};
|
2022-12-25 17:15:29 +01:00
|
|
|
}
|
2021-06-04 12:07:38 +02:00
|
|
|
|
2023-10-20 00:33:51 +02:00
|
|
|
void enter_unwind_context();
|
2021-06-10 15:04:38 +02:00
|
|
|
void leave_unwind_context();
|
2023-11-11 23:19:46 +01:00
|
|
|
void catch_exception();
|
2021-06-10 15:04:38 +02:00
|
|
|
|
2023-11-12 00:12:21 +01:00
|
|
|
void enter_object_environment(Object&);
|
|
|
|
|
2023-07-08 17:43:26 +02:00
|
|
|
Executable& current_executable() { return *m_current_executable; }
|
|
|
|
Executable const& current_executable() const { return *m_current_executable; }
|
2022-10-30 11:58:46 +01:00
|
|
|
BasicBlock const& current_block() const { return *m_current_block; }
|
2023-11-01 00:39:28 +01:00
|
|
|
Optional<InstructionStreamIterator const&> instruction_stream_iterator() const { return m_pc; }
|
2021-06-09 10:02:01 +02:00
|
|
|
|
2023-07-02 12:53:10 +02:00
|
|
|
void visit_edges(Cell::Visitor&);
|
|
|
|
|
2023-10-14 14:37:48 +02:00
|
|
|
Span<Value> registers() { return m_current_call_frame; }
|
|
|
|
ReadonlySpan<Value> registers() const { return m_current_call_frame; }
|
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
private:
|
2023-09-26 16:41:42 +02:00
|
|
|
void run_bytecode();
|
|
|
|
|
2023-07-20 10:46:42 +02:00
|
|
|
CallFrame& call_frame()
|
2022-04-15 20:20:51 +04:30
|
|
|
{
|
2023-07-20 10:46:42 +02:00
|
|
|
return m_call_frames.last().visit([](auto& x) -> CallFrame& { return *x; });
|
2022-04-15 20:20:51 +04:30
|
|
|
}
|
|
|
|
|
2023-07-20 10:46:42 +02:00
|
|
|
CallFrame const& call_frame() const
|
2022-04-15 20:20:51 +04:30
|
|
|
{
|
2023-07-20 10:46:42 +02:00
|
|
|
return const_cast<Interpreter*>(this)->call_frame();
|
2022-04-15 20:20:51 +04:30
|
|
|
}
|
|
|
|
|
2023-11-27 17:27:51 +01:00
|
|
|
void push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*>);
|
2023-07-20 10:46:42 +02:00
|
|
|
[[nodiscard]] Variant<NonnullOwnPtr<CallFrame>, CallFrame*> pop_call_frame();
|
2021-06-05 15:53:36 +02:00
|
|
|
|
2021-06-03 18:26:13 +02:00
|
|
|
VM& m_vm;
|
2023-07-20 10:46:42 +02:00
|
|
|
Vector<Variant<NonnullOwnPtr<CallFrame>, CallFrame*>> m_call_frames;
|
|
|
|
Span<Value> m_current_call_frame;
|
2022-11-25 16:15:34 +01:00
|
|
|
BasicBlock const* m_scheduled_jump { nullptr };
|
2023-07-08 17:43:26 +02:00
|
|
|
Executable* m_current_executable { nullptr };
|
2022-10-30 11:58:46 +01:00
|
|
|
BasicBlock const* m_current_block { nullptr };
|
2023-09-01 16:53:55 +02:00
|
|
|
Optional<InstructionStreamIterator&> m_pc {};
|
2021-06-03 10:46:30 +02:00
|
|
|
};
|
|
|
|
|
2021-10-24 13:34:46 +02:00
|
|
|
extern bool g_dump_bytecode;
|
|
|
|
|
2023-11-27 13:23:59 +01:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name);
|
2023-06-22 15:59:18 +02:00
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
}
|