| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-04 12:07:38 +02:00
										 |  |  | #include <LibJS/Bytecode/Label.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | #include <LibJS/Bytecode/Register.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Generator { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     static OwnPtr<Block> generate(ASTNode const&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Register allocate_register(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2021-06-04 11:38:17 +02:00
										 |  |  |     OpType& emit(Args&&... args) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |         void* slot = next_slot(); | 
					
						
							|  |  |  |         grow(sizeof(OpType)); | 
					
						
							|  |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							|  |  |  |         return *static_cast<OpType*>(slot); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename OpType, typename... Args> | 
					
						
							|  |  |  |     OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         void* slot = next_slot(); | 
					
						
							|  |  |  |         grow(sizeof(OpType) + extra_register_slots * sizeof(Register)); | 
					
						
							|  |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							|  |  |  |         return *static_cast<OpType*>(slot); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 12:07:38 +02:00
										 |  |  |     Label make_label() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     void begin_continuable_scope(); | 
					
						
							|  |  |  |     void end_continuable_scope(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Label nearest_continuable_scope() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Generator(); | 
					
						
							|  |  |  |     ~Generator(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     void grow(size_t); | 
					
						
							|  |  |  |     void* next_slot(); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OwnPtr<Block> m_block; | 
					
						
							|  |  |  |     u32 m_next_register { 1 }; | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     Vector<Label> m_continuable_scopes; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |