| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 18:49:52 +01:00
										 |  |  |     [[nodiscard]] Realm& realm() { return *m_realm; } | 
					
						
							|  |  |  |     [[nodiscard]] Object& global_object() { return *m_global_object; } | 
					
						
							|  |  |  |     [[nodiscard]] DeclarativeEnvironment& global_declarative_environment() { return *m_global_declarative_environment; } | 
					
						
							| 
									
										
										
										
											2021-06-03 18:26:13 +02:00
										 |  |  |     VM& vm() { return m_vm; } | 
					
						
							| 
									
										
										
										
											2024-02-02 10:19:17 +01:00
										 |  |  |     VM const& vm() const { 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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |         auto result_and_return_register = run_executable(executable, entry_point); | 
					
						
							|  |  |  |         return move(result_and_return_register.value); | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |     struct ResultAndReturnRegister { | 
					
						
							| 
									
										
										
										
											2021-11-11 04:11:56 +03:30
										 |  |  |         ThrowCompletionOr<Value> value; | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |         Value return_register_value; | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |     ResultAndReturnRegister run_executable(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point); | 
					
						
							| 
									
										
										
										
											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()); } | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |     Value& reg(Register const& r) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return vm().running_execution_context().registers[r.index()]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Value reg(Register const& r) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return vm().running_execution_context().registers[r.index()]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-02 10:19:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     [[nodiscard]] Value get(Operand) const; | 
					
						
							|  |  |  |     void set(Operand, Value); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     void catch_exception(Operand dst); | 
					
						
							| 
									
										
										
										
											2024-04-11 11:07:35 +02:00
										 |  |  |     void restore_scheduled_jump(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 19:33:49 +02:00
										 |  |  |     Vector<Value>& registers() { return vm().running_execution_context().registers; } | 
					
						
							|  |  |  |     Vector<Value> const& registers() const { return vm().running_execution_context().registers; } | 
					
						
							| 
									
										
										
										
											2023-10-14 14:37:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-09-26 16:41:42 +02:00
										 |  |  |     void run_bytecode(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:26:13 +02:00
										 |  |  |     VM& m_vm; | 
					
						
							| 
									
										
										
										
											2022-11-25 16:15:34 +01:00
										 |  |  |     BasicBlock const* m_scheduled_jump { nullptr }; | 
					
						
							| 
									
										
										
										
											2024-04-05 13:47:41 -07:00
										 |  |  |     GCPtr<Executable> m_current_executable { nullptr }; | 
					
						
							| 
									
										
										
										
											2022-10-30 11:58:46 +01:00
										 |  |  |     BasicBlock const* m_current_block { nullptr }; | 
					
						
							| 
									
										
										
										
											2024-04-05 13:47:41 -07:00
										 |  |  |     GCPtr<Realm> m_realm { nullptr }; | 
					
						
							|  |  |  |     GCPtr<Object> m_global_object { nullptr }; | 
					
						
							|  |  |  |     GCPtr<DeclarativeEnvironment> m_global_declarative_environment { 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  | ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const&, ReadonlySpan<FunctionParameter>, JS::FunctionKind kind, DeprecatedFlyString const& name); | 
					
						
							| 
									
										
										
										
											2023-06-22 15:59:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |