| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/AST.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | #include <LibJS/Bytecode/BasicBlock.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Bytecode/Generator.h>
 | 
					
						
							|  |  |  | #include <LibJS/Bytecode/Instruction.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-11 01:35:01 +04:30
										 |  |  | #include <LibJS/Bytecode/Op.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Bytecode/Register.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generator::Generator() | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     : m_string_table(make<StringTable>()) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generator::~Generator() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  | Executable Generator::generate(ASTNode const& node, bool is_in_generator_function) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Generator generator; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     generator.switch_to_basic_block(generator.make_block()); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     if (is_in_generator_function) { | 
					
						
							|  |  |  |         generator.enter_generator_context(); | 
					
						
							|  |  |  |         // Immediately yield with no value.
 | 
					
						
							|  |  |  |         auto& start_block = generator.make_block(); | 
					
						
							|  |  |  |         generator.emit<Bytecode::Op::Yield>(Label { start_block }); | 
					
						
							|  |  |  |         generator.switch_to_basic_block(start_block); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |     node.generate_bytecode(generator); | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  |     if (is_in_generator_function) { | 
					
						
							|  |  |  |         // Terminate all unterminated blocks with yield return
 | 
					
						
							|  |  |  |         for (auto& block : generator.m_root_basic_blocks) { | 
					
						
							|  |  |  |             if (block.is_terminated()) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             generator.switch_to_basic_block(block); | 
					
						
							|  |  |  |             generator.emit<Bytecode::Op::LoadImmediate>(js_undefined()); | 
					
						
							|  |  |  |             generator.emit<Bytecode::Op::Yield>(nullptr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  |     return { move(generator.m_root_basic_blocks), move(generator.m_string_table), generator.m_next_register }; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | void Generator::grow(size_t additional_size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     VERIFY(m_current_basic_block); | 
					
						
							|  |  |  |     m_current_basic_block->grow(additional_size); | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void* Generator::next_slot() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     VERIFY(m_current_basic_block); | 
					
						
							|  |  |  |     return m_current_basic_block->next_slot(); | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | Register Generator::allocate_register() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VERIFY(m_next_register != NumericLimits<u32>::max()); | 
					
						
							|  |  |  |     return Register { m_next_register++ }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | Label Generator::nearest_continuable_scope() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_continuable_scopes.last(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | void Generator::begin_continuable_scope(Label continue_target) | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     m_continuable_scopes.append(continue_target); | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Generator::end_continuable_scope() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_continuable_scopes.take_last(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  | Label Generator::nearest_breakable_scope() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_breakable_scopes.last(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void Generator::begin_breakable_scope(Label breakable_target) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_breakable_scopes.append(breakable_target); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  | void Generator::end_breakable_scope() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_breakable_scopes.take_last(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |