| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-07-02 12:53:10 +02:00
										 |  |  |     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<Value> registers; | 
					
						
							|  |  |  |     Vector<GCPtr<Environment>> saved_lexical_environments; | 
					
						
							| 
									
										
										
										
											2022-11-25 23:15:44 +00:00
										 |  |  |     Vector<UnwindInfo> unwind_contexts; | 
					
						
							| 
									
										
										
										
											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-07-08 17:43:26 +02:00
										 |  |  |     ThrowCompletionOr<Value> run(Realm& realm, Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr) | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-22 15:59:18 +02:00
										 |  |  |         auto value_and_frame = run_and_return_frame(realm, 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-07-20 10:46:42 +02:00
										 |  |  |     ValueAndFrame run_and_return_frame(Realm&, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     void jump(Label const& label) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_pending_jump = &label.block(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-25 16:15:34 +01:00
										 |  |  |     void schedule_jump(Label const& label) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_scheduled_jump = &label.block(); | 
					
						
							|  |  |  |         VERIFY(unwind_contexts().last().finalizer); | 
					
						
							|  |  |  |         jump(Label { *unwind_contexts().last().finalizer }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-25 17:15:29 +01:00
										 |  |  |     void do_return(Value return_value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_return_value = return_value; | 
					
						
							|  |  |  |         m_saved_exception = {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-04 12:07:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 15:04:38 +02:00
										 |  |  |     void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target); | 
					
						
							|  |  |  |     void leave_unwind_context(); | 
					
						
							| 
									
										
										
										
											2022-02-07 14:36:45 +01:00
										 |  |  |     ThrowCompletionOr<void> continue_pending_unwind(Label const& resume_label); | 
					
						
							| 
									
										
										
										
											2021-06-10 15:04:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06-17 13:16:35 +02:00
										 |  |  |     size_t pc() const; | 
					
						
							|  |  |  |     DeprecatedString debug_position() const; | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-28 23:59:43 +02:00
										 |  |  |     Optional<Value>& this_value() { return m_this_value; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 12:53:10 +02:00
										 |  |  |     void visit_edges(Cell::Visitor&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											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-07-20 10:46:42 +02:00
										 |  |  |     Span<Value> registers() { return m_current_call_frame; } | 
					
						
							|  |  |  |     ReadonlySpan<Value> registers() const { return m_current_call_frame; } | 
					
						
							| 
									
										
										
										
											2023-07-01 13:56:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-20 10:46:42 +02:00
										 |  |  |     void push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*>, size_t register_count); | 
					
						
							|  |  |  |     [[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; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     Optional<BasicBlock const*> m_pending_jump; | 
					
						
							| 
									
										
										
										
											2022-11-25 16:15:34 +01:00
										 |  |  |     BasicBlock const* m_scheduled_jump { nullptr }; | 
					
						
							| 
									
										
										
										
											2023-07-28 23:59:43 +02:00
										 |  |  |     Optional<Value> m_this_value; | 
					
						
							| 
									
										
										
										
											2023-07-02 12:53:10 +02:00
										 |  |  |     Optional<Value> m_return_value; | 
					
						
							|  |  |  |     Optional<Value> m_saved_exception; | 
					
						
							| 
									
										
										
										
											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 }; | 
					
						
							|  |  |  |     InstructionStreamIterator* m_pc { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 13:34:46 +02:00
										 |  |  | extern bool g_dump_bytecode; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 15:59:18 +02:00
										 |  |  | ThrowCompletionOr<NonnullOwnPtr<Bytecode::Executable>> compile(VM&, ASTNode const& no, JS::FunctionKind kind, DeprecatedFlyString const& name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |