| 
									
										
										
										
											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 <AK/NonnullOwnPtrVector.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | #include <AK/SinglyLinkedList.h>
 | 
					
						
							|  |  |  | #include <LibJS/Bytecode/BasicBlock.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-24 13:30:49 +02:00
										 |  |  | #include <LibJS/Bytecode/Executable.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-24 15:34:30 +02:00
										 |  |  | #include <LibJS/Bytecode/IdentifierTable.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-04 12:07:38 +02:00
										 |  |  | #include <LibJS/Bytecode/Label.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-11 01:35:01 +04:30
										 |  |  | #include <LibJS/Bytecode/Op.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | #include <LibJS/Bytecode/Register.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  | #include <LibJS/Bytecode/StringTable.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Generator { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     static Executable generate(ASTNode const&, bool is_in_generator_function = false); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Register allocate_register(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 01:35:01 +04:30
										 |  |  |     void ensure_enough_space(size_t size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Make sure there's always enough space for a single jump at the end.
 | 
					
						
							|  |  |  |         if (!m_current_basic_block->can_grow(size + sizeof(Op::Jump))) { | 
					
						
							|  |  |  |             auto& new_block = make_block(); | 
					
						
							|  |  |  |             emit<Op::Jump>().set_targets( | 
					
						
							|  |  |  |                 Label { new_block }, | 
					
						
							|  |  |  |                 {}); | 
					
						
							|  |  |  |             switch_to_basic_block(new_block); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |     OpType& emit(Args&&... args) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         VERIFY(!is_current_block_terminated()); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:35:01 +04:30
										 |  |  |         // If the block doesn't have enough space, switch to another block
 | 
					
						
							|  |  |  |         if constexpr (!OpType::IsTerminator) | 
					
						
							|  |  |  |             ensure_enough_space(sizeof(OpType)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         void* slot = next_slot(); | 
					
						
							|  |  |  |         grow(sizeof(OpType)); | 
					
						
							|  |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         if constexpr (OpType::IsTerminator) | 
					
						
							|  |  |  |             m_current_basic_block->terminate({}); | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         return *static_cast<OpType*>(slot); | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |     OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args) | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         VERIFY(!is_current_block_terminated()); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:35:01 +04:30
										 |  |  |         // If the block doesn't have enough space, switch to another block
 | 
					
						
							|  |  |  |         if constexpr (!OpType::IsTerminator) | 
					
						
							|  |  |  |             ensure_enough_space(sizeof(OpType) + extra_register_slots * sizeof(Register)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         void* slot = next_slot(); | 
					
						
							|  |  |  |         grow(sizeof(OpType) + extra_register_slots * sizeof(Register)); | 
					
						
							|  |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         if constexpr (OpType::IsTerminator) | 
					
						
							|  |  |  |             m_current_basic_block->terminate({}); | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         return *static_cast<OpType*>(slot); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-25 15:16:22 +02:00
										 |  |  |     void emit_load_from_reference(JS::ASTNode const&); | 
					
						
							|  |  |  |     void emit_store_to_reference(JS::ASTNode const&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     void begin_continuable_scope(Label continue_target); | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     void end_continuable_scope(); | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  |     void begin_breakable_scope(Label breakable_target); | 
					
						
							|  |  |  |     void end_breakable_scope(); | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     [[nodiscard]] Label nearest_continuable_scope() const; | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  |     [[nodiscard]] Label nearest_breakable_scope() const; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     void switch_to_basic_block(BasicBlock& block) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_current_basic_block = █ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 15:04:38 +02:00
										 |  |  |     [[nodiscard]] BasicBlock& current_block() { return *m_current_basic_block; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     BasicBlock& make_block(String name = {}) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (name.is_empty()) | 
					
						
							|  |  |  |             name = String::number(m_next_block++); | 
					
						
							|  |  |  |         m_root_basic_blocks.append(BasicBlock::create(name)); | 
					
						
							|  |  |  |         return m_root_basic_blocks.last(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_current_block_terminated() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_current_basic_block->is_terminated(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 15:14:14 +02:00
										 |  |  |     StringTableIndex intern_string(String string) | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-24 15:14:14 +02:00
										 |  |  |         return m_string_table->insert(move(string)); | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 15:34:30 +02:00
										 |  |  |     IdentifierTableIndex intern_identifier(FlyString string) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_identifier_table->insert(move(string)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     bool is_in_generator_function() const { return m_is_in_generator_function; } | 
					
						
							|  |  |  |     void enter_generator_context() { m_is_in_generator_function = true; } | 
					
						
							|  |  |  |     void leave_generator_context() { m_is_in_generator_function = false; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Generator(); | 
					
						
							|  |  |  |     ~Generator(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |     void grow(size_t); | 
					
						
							|  |  |  |     void* next_slot(); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     BasicBlock* m_current_basic_block { nullptr }; | 
					
						
							|  |  |  |     NonnullOwnPtrVector<BasicBlock> m_root_basic_blocks; | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     NonnullOwnPtr<StringTable> m_string_table; | 
					
						
							| 
									
										
										
										
											2021-10-24 15:34:30 +02:00
										 |  |  |     NonnullOwnPtr<IdentifierTable> m_identifier_table; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:07:53 +02:00
										 |  |  |     u32 m_next_register { 2 }; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     u32 m_next_block { 1 }; | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     bool m_is_in_generator_function { false }; | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     Vector<Label> m_continuable_scopes; | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  |     Vector<Label> m_breakable_scopes; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |