| 
									
										
										
										
											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-06-04 12:07:38 +02:00
										 |  |  | #include <LibJS/Bytecode/Label.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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 09:11:20 +02:00
										 |  |  | struct Executable { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     NonnullOwnPtrVector<BasicBlock> basic_blocks; | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     NonnullOwnPtr<StringTable> string_table; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     size_t number_of_registers { 0 }; | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String const& get_string(StringTableIndex index) const { return string_table->get(index); } | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | class Generator { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-09 09:11:20 +02:00
										 |  |  |     static Executable generate(ASTNode const&); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Register allocate_register(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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-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-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-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-09 06:49:58 +04:30
										 |  |  |     [[nodiscard]] Label nearest_continuable_scope() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void switch_to_basic_block(BasicBlock& block) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_current_basic_block = █ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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-06-09 10:02:01 +02:00
										 |  |  |     StringTableIndex intern_string(StringView const& string) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_string_table->insert(string); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06-09 06:49:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     u32 m_next_register { 1 }; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     u32 m_next_block { 1 }; | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     Vector<Label> m_continuable_scopes; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |