| 
									
										
										
										
											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-09 06:49:58 +04:30
										 |  |  | #include "Generator.h"
 | 
					
						
							| 
									
										
										
										
											2021-06-13 20:40:20 +04:30
										 |  |  | #include "PassManager.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>
 | 
					
						
							| 
									
										
										
										
											2021-06-10 15:04:38 +02:00
										 |  |  | #include <LibJS/Heap/Handle.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Exception.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  | using RegisterWindow = Vector<Value>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | class Interpreter { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  |     Interpreter(GlobalObject&, Realm&); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     ~Interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  |     // FIXME: Remove this thing once we don't need it anymore!
 | 
					
						
							|  |  |  |     static Interpreter* current(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     GlobalObject& global_object() { return m_global_object; } | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  |     Realm& realm() { return m_realm; } | 
					
						
							| 
									
										
										
										
											2021-06-03 18:26:13 +02:00
										 |  |  |     VM& vm() { return m_vm; } | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 04:11:56 +03:30
										 |  |  |     ThrowCompletionOr<Value> run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr) | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     { | 
					
						
							|  |  |  |         auto value_and_frame = run_and_return_frame(executable, entry_point); | 
					
						
							|  |  |  |         return value_and_frame.value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct ValueAndFrame { | 
					
						
							| 
									
										
										
										
											2021-11-11 04:11:56 +03:30
										 |  |  |         ThrowCompletionOr<Value> value; | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |         OwnPtr<RegisterWindow> frame; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ValueAndFrame run_and_return_frame(Bytecode::Executable const&, 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()); } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     [[nodiscard]] RegisterWindow snapshot_frame() const { return m_register_windows.last(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void enter_frame(RegisterWindow const& frame) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |         m_manually_entered_frames.append(true); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |         m_register_windows.append(make<RegisterWindow>(frame)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     NonnullOwnPtr<RegisterWindow> pop_frame() | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |         VERIFY(!m_manually_entered_frames.is_empty()); | 
					
						
							|  |  |  |         VERIFY(m_manually_entered_frames.last()); | 
					
						
							|  |  |  |         m_manually_entered_frames.take_last(); | 
					
						
							|  |  |  |         return m_register_windows.take_last(); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  |     void do_return(Value return_value) { m_return_value = return_value; } | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  |     void continue_pending_unwind(Label const& resume_label); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     Executable const& current_executable() { return *m_current_executable; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 20:40:20 +04:30
										 |  |  |     enum class OptimizationLevel { | 
					
						
							|  |  |  |         Default, | 
					
						
							|  |  |  |         __Count, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     static Bytecode::PassManager& optimization_pipeline(OptimizationLevel = OptimizationLevel::Default); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  |     RegisterWindow& registers() { return m_register_windows.last(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 20:40:20 +04:30
										 |  |  |     static AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> s_optimization_pipelines; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:26:13 +02:00
										 |  |  |     VM& m_vm; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     GlobalObject& m_global_object; | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  |     Realm& m_realm; | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  |     NonnullOwnPtrVector<RegisterWindow> m_register_windows; | 
					
						
							| 
									
										
										
										
											2021-11-11 00:34:44 +03:30
										 |  |  |     Vector<bool> m_manually_entered_frames; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     Optional<BasicBlock const*> m_pending_jump; | 
					
						
							| 
									
										
										
										
											2021-06-05 15:53:36 +02:00
										 |  |  |     Value m_return_value; | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     Executable const* m_current_executable { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-06-10 15:04:38 +02:00
										 |  |  |     Vector<UnwindInfo> m_unwind_contexts; | 
					
						
							|  |  |  |     Handle<Exception> m_saved_exception; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 13:34:46 +02:00
										 |  |  | extern bool g_dump_bytecode; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |