| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | #include <AK/SinglyLinkedList.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  | #include <LibJS/AST.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | #include <LibJS/Bytecode/BasicBlock.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-12 19:54:08 +03:30
										 |  |  | #include <LibJS/Bytecode/CodeGenerationError.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>
 | 
					
						
							| 
									
										
										
										
											2021-11-11 00:46:07 +03:30
										 |  |  | #include <LibJS/Runtime/FunctionKind.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-13 10:49:07 +02:00
										 |  |  | #include <LibRegex/Regex.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Generator { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:34:36 +01:00
										 |  |  |     VM& vm() { return m_vm; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 19:48:45 +03:30
										 |  |  |     enum class SurroundingScopeKind { | 
					
						
							|  |  |  |         Global, | 
					
						
							|  |  |  |         Function, | 
					
						
							|  |  |  |         Block, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-05-05 22:06:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 10:57:06 +02:00
										 |  |  |     enum class MustPropagateCompletion { | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     static CodeGenerationErrorOr<GC::Ref<Executable>> generate_from_ast_node(VM&, ASTNode const&, FunctionKind = FunctionKind::Normal); | 
					
						
							|  |  |  |     static CodeGenerationErrorOr<GC::Ref<Executable>> generate_from_function(VM&, ECMAScriptFunctionObject const& function); | 
					
						
							| 
									
										
										
										
											2024-05-05 22:06:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     CodeGenerationErrorOr<void> emit_function_declaration_instantiation(ECMAScriptFunctionObject const& function); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     [[nodiscard]] ScopedOperand allocate_register(); | 
					
						
							|  |  |  |     [[nodiscard]] ScopedOperand local(u32 local_index); | 
					
						
							|  |  |  |     [[nodiscard]] ScopedOperand accumulator(); | 
					
						
							| 
									
										
										
										
											2024-05-31 20:41:29 +02:00
										 |  |  |     [[nodiscard]] ScopedOperand this_value(); | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void free_register(Register); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     void set_local_initialized(u32 local_index); | 
					
						
							|  |  |  |     [[nodiscard]] bool is_local_initialized(u32 local_index) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  |     class SourceLocationScope { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         SourceLocationScope(Generator&, ASTNode const& node); | 
					
						
							|  |  |  |         ~SourceLocationScope(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         Generator& m_generator; | 
					
						
							|  |  |  |         ASTNode const* m_previous_node { nullptr }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-19 23:18:54 +02:00
										 |  |  |     class UnwindContext { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         UnwindContext(Generator&, Optional<Label> finalizer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UnwindContext const* previous() const { return m_previous_context; } | 
					
						
							|  |  |  |         void set_handler(Label handler) { m_handler = handler; } | 
					
						
							|  |  |  |         Optional<Label> handler() const { return m_handler; } | 
					
						
							|  |  |  |         Optional<Label> finalizer() const { return m_finalizer; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ~UnwindContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         Generator& m_generator; | 
					
						
							|  |  |  |         Optional<Label> m_finalizer; | 
					
						
							|  |  |  |         Optional<Label> m_handler {}; | 
					
						
							|  |  |  |         UnwindContext const* m_previous_context { nullptr }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2024-05-06 23:00:19 +02:00
										 |  |  |     requires(requires { OpType(declval<Args>()...); }) | 
					
						
							| 
									
										
										
										
											2023-09-28 12:48:29 +02:00
										 |  |  |     void emit(Args&&... args) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         VERIFY(!is_current_block_terminated()); | 
					
						
							| 
									
										
										
										
											2023-09-28 09:29:42 +02:00
										 |  |  |         size_t slot_offset = m_current_basic_block->size(); | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |         m_current_basic_block->set_last_instruction_start_offset(slot_offset); | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         grow(sizeof(OpType)); | 
					
						
							| 
									
										
										
										
											2023-09-28 09:29:42 +02:00
										 |  |  |         void* slot = m_current_basic_block->data() + slot_offset; | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         if constexpr (OpType::IsTerminator) | 
					
						
							| 
									
										
										
										
											2024-03-06 08:36:19 +01:00
										 |  |  |             m_current_basic_block->terminate({}); | 
					
						
							| 
									
										
										
										
											2024-05-06 07:51:14 +02:00
										 |  |  |         m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() }); | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |     template<typename OpType, typename ExtraSlotType, typename... Args> | 
					
						
							| 
									
										
										
										
											2024-05-06 23:00:19 +02:00
										 |  |  |     requires(requires { OpType(declval<Args>()...); }) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |     void emit_with_extra_slots(size_t extra_slot_count, Args&&... args) | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         VERIFY(!is_current_block_terminated()); | 
					
						
							| 
									
										
										
										
											2022-09-09 16:47:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |         size_t size_to_allocate = round_up_to_power_of_two(sizeof(OpType) + extra_slot_count * sizeof(ExtraSlotType), alignof(void*)); | 
					
						
							| 
									
										
										
										
											2023-09-28 09:29:42 +02:00
										 |  |  |         size_t slot_offset = m_current_basic_block->size(); | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |         m_current_basic_block->set_last_instruction_start_offset(slot_offset); | 
					
						
							| 
									
										
										
										
											2022-09-09 16:47:42 +02:00
										 |  |  |         grow(size_to_allocate); | 
					
						
							| 
									
										
										
										
											2023-09-28 09:29:42 +02:00
										 |  |  |         void* slot = m_current_basic_block->data() + slot_offset; | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |         new (slot) OpType(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |         if constexpr (OpType::IsTerminator) | 
					
						
							| 
									
										
										
										
											2024-03-06 08:36:19 +01:00
										 |  |  |             m_current_basic_block->terminate({}); | 
					
						
							| 
									
										
										
										
											2024-05-06 07:51:14 +02:00
										 |  |  |         m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() }); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2024-05-06 23:00:19 +02:00
										 |  |  |     requires(requires { OpType(declval<Args>()...); }) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |     void emit_with_extra_operand_slots(size_t extra_operand_slots, Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         emit_with_extra_slots<OpType, Operand>(extra_operand_slots, forward<Args>(args)...); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename OpType, typename... Args> | 
					
						
							| 
									
										
										
										
											2024-05-06 23:00:19 +02:00
										 |  |  |     requires(requires { OpType(declval<Args>()...); }) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:37:28 +01:00
										 |  |  |     void emit_with_extra_value_slots(size_t extra_operand_slots, Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         emit_with_extra_slots<OpType, Value>(extra_operand_slots, forward<Args>(args)...); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-28 00:56:57 +01:00
										 |  |  |     void emit_mov(ScopedOperand const& dst, ScopedOperand const& src) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Optimize away when the source is the destination
 | 
					
						
							|  |  |  |         if (dst != src) | 
					
						
							|  |  |  |             emit<Op::Mov>(dst, src); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void emit_mov(Operand const& dst, Operand const& src) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         emit<Op::Mov>(dst, src); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |     void emit_jump_if(ScopedOperand const& condition, Label true_target, Label false_target); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     struct ReferenceOperands { | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |         Optional<ScopedOperand> base {};                                 // [[Base]]
 | 
					
						
							|  |  |  |         Optional<ScopedOperand> referenced_name {};                      // [[ReferencedName]] as an operand
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |         Optional<IdentifierTableIndex> referenced_identifier {};         // [[ReferencedName]] as an identifier
 | 
					
						
							|  |  |  |         Optional<IdentifierTableIndex> referenced_private_identifier {}; // [[ReferencedName]] as a private identifier
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |         Optional<ScopedOperand> this_value {};                           // [[ThisValue]]
 | 
					
						
							|  |  |  |         Optional<ScopedOperand> loaded_value {};                         // Loaded value, if we've performed a load.
 | 
					
						
							| 
									
										
										
										
											2023-10-04 15:22:07 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     CodeGenerationErrorOr<ReferenceOperands> emit_load_from_reference(JS::ASTNode const&, Optional<ScopedOperand> preferred_dst = {}); | 
					
						
							|  |  |  |     CodeGenerationErrorOr<void> emit_store_to_reference(JS::ASTNode const&, ScopedOperand value); | 
					
						
							|  |  |  |     CodeGenerationErrorOr<void> emit_store_to_reference(ReferenceOperands const&, ScopedOperand value); | 
					
						
							|  |  |  |     CodeGenerationErrorOr<Optional<ScopedOperand>> emit_delete_reference(JS::ASTNode const&); | 
					
						
							| 
									
										
										
										
											2021-10-25 15:16:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     CodeGenerationErrorOr<ReferenceOperands> emit_super_reference(MemberExpression const&); | 
					
						
							| 
									
										
										
										
											2023-07-06 17:02:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 11:30:30 +02:00
										 |  |  |     void emit_set_variable(JS::Identifier const& identifier, ScopedOperand value, Bytecode::Op::BindingInitializationMode initialization_mode = Bytecode::Op::BindingInitializationMode::Set, Bytecode::Op::EnvironmentMode mode = Bytecode::Op::EnvironmentMode::Lexical); | 
					
						
							| 
									
										
										
										
											2023-07-05 02:17:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     void push_home_object(ScopedOperand); | 
					
						
							| 
									
										
										
										
											2023-06-16 10:25:05 +02:00
										 |  |  |     void pop_home_object(); | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     void emit_new_function(ScopedOperand dst, JS::FunctionExpression const&, Optional<IdentifierTableIndex> lhs_name); | 
					
						
							| 
									
										
										
										
											2023-06-23 14:27:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     CodeGenerationErrorOr<Optional<ScopedOperand>> emit_named_evaluation_if_anonymous_function(Expression const&, Optional<IdentifierTableIndex> lhs_name, Optional<ScopedOperand> preferred_dst = {}); | 
					
						
							| 
									
										
										
										
											2023-06-16 10:25:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     void begin_continuable_scope(Label continue_target, Vector<FlyString> const& language_label_set); | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  |     void end_continuable_scope(); | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     void begin_breakable_scope(Label breakable_target, Vector<FlyString> const& language_label_set); | 
					
						
							| 
									
										
										
										
											2021-06-10 20:28:43 +08:00
										 |  |  |     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; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-26 23:57:51 -04:00
										 |  |  |     BasicBlock& make_block(String name = {}) | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     { | 
					
						
							|  |  |  |         if (name.is_empty()) | 
					
						
							| 
									
										
										
										
											2024-10-14 10:05:01 +02:00
										 |  |  |             name = String::number(m_next_block++); | 
					
						
							| 
									
										
										
										
											2024-05-06 08:06:56 +02:00
										 |  |  |         auto block = BasicBlock::create(m_root_basic_blocks.size(), name); | 
					
						
							| 
									
										
										
										
											2023-10-19 23:18:54 +02:00
										 |  |  |         if (auto const* context = m_current_unwind_context) { | 
					
						
							|  |  |  |             if (context->handler().has_value()) | 
					
						
							| 
									
										
										
										
											2024-05-06 08:06:56 +02:00
										 |  |  |                 block->set_handler(*m_root_basic_blocks[context->handler().value().basic_block_index()]); | 
					
						
							| 
									
										
										
										
											2023-10-19 23:18:54 +02:00
										 |  |  |             if (m_current_unwind_context->finalizer().has_value()) | 
					
						
							| 
									
										
										
										
											2024-05-06 08:06:56 +02:00
										 |  |  |                 block->set_finalizer(*m_root_basic_blocks[context->finalizer().value().basic_block_index()]); | 
					
						
							| 
									
										
										
										
											2023-10-19 23:18:54 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         m_root_basic_blocks.append(move(block)); | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |         return *m_root_basic_blocks.last(); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_current_block_terminated() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_current_basic_block->is_terminated(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-06 13:33:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05: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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 10:49:07 +02:00
										 |  |  |     RegexTableIndex intern_regex(ParsedRegex regex) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_regex_table->insert(move(regex)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     IdentifierTableIndex intern_identifier(FlyString string) | 
					
						
							| 
									
										
										
										
											2021-10-24 15:34:30 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return m_identifier_table->insert(move(string)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 11:26:10 -04:00
										 |  |  |     Optional<IdentifierTableIndex> intern_identifier_for_expression(Expression const& expression); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 21:44:36 +01:00
										 |  |  |     bool is_in_generator_or_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::Generator || m_enclosing_function_kind == FunctionKind::AsyncGenerator; } | 
					
						
							|  |  |  |     bool is_in_generator_function() const { return m_enclosing_function_kind == FunctionKind::Generator || m_enclosing_function_kind == FunctionKind::AsyncGenerator; } | 
					
						
							|  |  |  |     bool is_in_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::AsyncGenerator; } | 
					
						
							| 
									
										
										
										
											2023-07-14 21:45:41 +01:00
										 |  |  |     bool is_in_async_generator_function() const { return m_enclosing_function_kind == FunctionKind::AsyncGenerator; } | 
					
						
							| 
									
										
										
										
											2021-06-11 01:38:30 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 19:48:45 +03:30
										 |  |  |     enum class BindingMode { | 
					
						
							|  |  |  |         Lexical, | 
					
						
							|  |  |  |         Var, | 
					
						
							|  |  |  |         Global, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     struct LexicalScope { | 
					
						
							|  |  |  |         SurroundingScopeKind kind; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-11 13:25:08 +02:00
										 |  |  |     // Returns true if a lexical environment was created.
 | 
					
						
							|  |  |  |     bool emit_block_declaration_instantiation(ScopeNode const&); | 
					
						
							| 
									
										
										
										
											2022-03-14 02:34:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 16:34:47 +02:00
										 |  |  |     void begin_variable_scope(); | 
					
						
							| 
									
										
										
										
											2022-03-14 02:34:01 +00:00
										 |  |  |     void end_variable_scope(); | 
					
						
							| 
									
										
										
										
											2022-02-12 19:48:45 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |     enum class BlockBoundaryType { | 
					
						
							|  |  |  |         Break, | 
					
						
							|  |  |  |         Continue, | 
					
						
							|  |  |  |         Unwind, | 
					
						
							| 
									
										
										
										
											2022-11-13 20:56:53 +01:00
										 |  |  |         ReturnToFinally, | 
					
						
							| 
									
										
										
										
											2024-04-11 11:58:18 +02:00
										 |  |  |         LeaveFinally, | 
					
						
							| 
									
										
										
										
											2022-03-14 02:34:01 +00:00
										 |  |  |         LeaveLexicalEnvironment, | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |     }; | 
					
						
							|  |  |  |     template<typename OpType> | 
					
						
							| 
									
										
										
										
											2022-11-25 16:42:29 +01:00
										 |  |  |     void perform_needed_unwinds() | 
					
						
							|  |  |  |     requires(OpType::IsTerminator && !IsSame<OpType, Op::Jump>) | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |     { | 
					
						
							|  |  |  |         for (size_t i = m_boundaries.size(); i > 0; --i) { | 
					
						
							|  |  |  |             auto boundary = m_boundaries[i - 1]; | 
					
						
							| 
									
										
										
										
											2022-11-13 20:56:53 +01:00
										 |  |  |             using enum BlockBoundaryType; | 
					
						
							|  |  |  |             switch (boundary) { | 
					
						
							|  |  |  |             case Unwind: | 
					
						
							| 
									
										
										
										
											2022-11-25 16:42:29 +01:00
										 |  |  |                 if constexpr (IsSame<OpType, Bytecode::Op::Throw>) | 
					
						
							|  |  |  |                     return; | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |                 emit<Bytecode::Op::LeaveUnwindContext>(); | 
					
						
							| 
									
										
										
										
											2022-11-13 20:56:53 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case LeaveLexicalEnvironment: | 
					
						
							| 
									
										
										
										
											2023-06-16 16:43:24 +02:00
										 |  |  |                 emit<Bytecode::Op::LeaveLexicalEnvironment>(); | 
					
						
							| 
									
										
										
										
											2022-11-13 20:56:53 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case Break: | 
					
						
							|  |  |  |             case Continue: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case ReturnToFinally: | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2024-04-11 11:58:18 +02:00
										 |  |  |             case LeaveFinally: | 
					
						
							|  |  |  |                 emit<Bytecode::Op::LeaveFinally>(); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2022-11-13 20:56:53 +01:00
										 |  |  |             }; | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 22:38:43 +02:00
										 |  |  |     bool is_in_finalizer() const { return m_boundaries.contains_slow(BlockBoundaryType::LeaveFinally); } | 
					
						
							| 
									
										
										
										
											2024-05-12 11:03:26 +02:00
										 |  |  |     bool must_enter_finalizer() const { return m_boundaries.contains_slow(BlockBoundaryType::ReturnToFinally); } | 
					
						
							| 
									
										
										
										
											2024-05-06 22:38:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-25 16:15:34 +01:00
										 |  |  |     void generate_break(); | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     void generate_break(FlyString const& break_label); | 
					
						
							| 
									
										
										
										
											2022-11-25 16:15:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-25 16:35:39 +01:00
										 |  |  |     void generate_continue(); | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     void generate_continue(FlyString const& continue_label); | 
					
						
							| 
									
										
										
										
											2022-06-11 23:09:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-12 11:03:26 +02:00
										 |  |  |     template<typename OpType> | 
					
						
							|  |  |  |     void emit_return(ScopedOperand value) | 
					
						
							|  |  |  |     requires(IsOneOf<OpType, Op::Return, Op::Yield>) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         perform_needed_unwinds<OpType>(); | 
					
						
							|  |  |  |         if (must_enter_finalizer()) { | 
					
						
							|  |  |  |             VERIFY(m_current_basic_block->finalizer() != nullptr); | 
					
						
							|  |  |  |             // Compare to:
 | 
					
						
							|  |  |  |             // *  Interpreter::do_return
 | 
					
						
							|  |  |  |             // *  Interpreter::run_bytecode::handle_ContinuePendingUnwind
 | 
					
						
							|  |  |  |             // *  Return::execute_impl
 | 
					
						
							|  |  |  |             // *  Yield::execute_impl
 | 
					
						
							| 
									
										
										
										
											2024-05-18 17:25:43 +02:00
										 |  |  |             if constexpr (IsSame<OpType, Op::Yield>) | 
					
						
							|  |  |  |                 emit<Bytecode::Op::PrepareYield>(Operand(Register::saved_return_value()), value); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 emit<Bytecode::Op::Mov>(Operand(Register::saved_return_value()), value); | 
					
						
							| 
									
										
										
										
											2024-05-12 11:03:26 +02:00
										 |  |  |             emit<Bytecode::Op::Mov>(Operand(Register::exception()), add_constant(Value {})); | 
					
						
							|  |  |  |             emit<Bytecode::Op::Jump>(Label { *m_current_basic_block->finalizer() }); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if constexpr (IsSame<OpType, Op::Return>) | 
					
						
							|  |  |  |             emit<Op::Return>(value); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             emit<Op::Yield>(nullptr, value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |     void start_boundary(BlockBoundaryType type) { m_boundaries.append(type); } | 
					
						
							|  |  |  |     void end_boundary(BlockBoundaryType type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_boundaries.last() == type); | 
					
						
							|  |  |  |         m_boundaries.take_last(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-01 10:00:32 +02:00
										 |  |  |     [[nodiscard]] ScopedOperand copy_if_needed_to_preserve_evaluation_order(ScopedOperand const&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     [[nodiscard]] ScopedOperand get_this(Optional<ScopedOperand> preferred_dst = {}); | 
					
						
							| 
									
										
										
										
											2024-05-07 12:16:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     void emit_get_by_id(ScopedOperand dst, ScopedOperand base, IdentifierTableIndex property_identifier, Optional<IdentifierTableIndex> base_identifier = {}); | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     void emit_get_by_id_with_this(ScopedOperand dst, ScopedOperand base, IdentifierTableIndex, ScopedOperand this_value); | 
					
						
							| 
									
										
										
										
											2023-07-08 17:43:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     void emit_iterator_value(ScopedOperand dst, ScopedOperand result); | 
					
						
							|  |  |  |     void emit_iterator_complete(ScopedOperand dst, ScopedOperand result); | 
					
						
							| 
									
										
										
										
											2023-12-07 16:12:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 04:06:59 +02:00
										 |  |  |     [[nodiscard]] size_t next_global_variable_cache() { return m_next_global_variable_cache++; } | 
					
						
							| 
									
										
										
										
											2023-11-08 20:51:26 +01:00
										 |  |  |     [[nodiscard]] size_t next_property_lookup_cache() { return m_next_property_lookup_cache++; } | 
					
						
							| 
									
										
										
										
											2023-07-12 04:06:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     enum class DeduplicateConstant { | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-05-14 14:28:22 +02:00
										 |  |  |     [[nodiscard]] ScopedOperand add_constant(Value); | 
					
						
							| 
									
										
										
										
											2024-02-02 09:52:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-31 21:49:46 +02:00
										 |  |  |     [[nodiscard]] Value get_constant(ScopedOperand const& operand) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(operand.operand().is_constant()); | 
					
						
							|  |  |  |         return m_constants[operand.operand().index()]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 00:25:54 +02:00
										 |  |  |     UnwindContext const* current_unwind_context() const { return m_current_unwind_context; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     [[nodiscard]] bool is_finished() const { return m_finished; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 10:57:06 +02:00
										 |  |  |     [[nodiscard]] bool must_propagate_completion() const { return m_must_propagate_completion; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:34:36 +01:00
										 |  |  |     VM& m_vm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     static CodeGenerationErrorOr<GC::Ref<Executable>> compile(VM&, ASTNode const&, FunctionKind, GC::Ptr<ECMAScriptFunctionObject const>, MustPropagateCompletion, Vector<FlyString> local_variable_names); | 
					
						
							| 
									
										
										
										
											2024-05-05 22:06:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 19:44:10 +12:00
										 |  |  |     enum class JumpType { | 
					
						
							|  |  |  |         Continue, | 
					
						
							|  |  |  |         Break, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     void generate_scoped_jump(JumpType); | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |     void generate_labelled_jump(JumpType, FlyString const& label); | 
					
						
							| 
									
										
										
										
											2023-07-19 19:44:10 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     Generator(VM&, GC::Ptr<ECMAScriptFunctionObject const>, MustPropagateCompletion); | 
					
						
							| 
									
										
										
										
											2022-03-14 10:25:06 -06:00
										 |  |  |     ~Generator() = default; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  |     void grow(size_t); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |     // Returns true if a fused instruction was emitted.
 | 
					
						
							|  |  |  |     [[nodiscard]] bool fuse_compare_and_jump(ScopedOperand const& condition, Label true_target, Label false_target); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-11 23:09:37 +01:00
										 |  |  |     struct LabelableScope { | 
					
						
							|  |  |  |         Label bytecode_target; | 
					
						
							| 
									
										
										
										
											2025-03-18 18:08:02 -05:00
										 |  |  |         Vector<FlyString> language_label_set; | 
					
						
							| 
									
										
										
										
											2022-06-11 23:09:37 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     BasicBlock* m_current_basic_block { nullptr }; | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  |     ASTNode const* m_current_ast_node { nullptr }; | 
					
						
							| 
									
										
										
										
											2023-10-19 23:18:54 +02:00
										 |  |  |     UnwindContext const* m_current_unwind_context { nullptr }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |     Vector<NonnullOwnPtr<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; | 
					
						
							| 
									
										
										
										
											2023-07-13 10:49:07 +02:00
										 |  |  |     NonnullOwnPtr<RegexTable> m_regex_table; | 
					
						
							| 
									
										
										
										
											2024-12-26 14:32:52 +01:00
										 |  |  |     GC::RootVector<Value> m_constants; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 14:28:22 +02:00
										 |  |  |     mutable Optional<ScopedOperand> m_true_constant; | 
					
						
							|  |  |  |     mutable Optional<ScopedOperand> m_false_constant; | 
					
						
							|  |  |  |     mutable Optional<ScopedOperand> m_null_constant; | 
					
						
							|  |  |  |     mutable Optional<ScopedOperand> m_undefined_constant; | 
					
						
							|  |  |  |     mutable Optional<ScopedOperand> m_empty_constant; | 
					
						
							|  |  |  |     mutable HashMap<i32, ScopedOperand> m_int32_constants; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     ScopedOperand m_accumulator; | 
					
						
							| 
									
										
										
										
											2024-05-31 20:41:29 +02:00
										 |  |  |     ScopedOperand m_this_value; | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     Vector<Register> m_free_registers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 14:42:30 +02:00
										 |  |  |     u32 m_next_register { Register::reserved_register_count }; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     u32 m_next_block { 1 }; | 
					
						
							| 
									
										
										
										
											2023-07-08 17:43:26 +02:00
										 |  |  |     u32 m_next_property_lookup_cache { 0 }; | 
					
						
							| 
									
										
										
										
											2023-07-12 04:06:59 +02:00
										 |  |  |     u32 m_next_global_variable_cache { 0 }; | 
					
						
							| 
									
										
										
										
											2022-01-15 00:30:02 +01:00
										 |  |  |     FunctionKind m_enclosing_function_kind { FunctionKind::Normal }; | 
					
						
							| 
									
										
										
										
											2022-06-11 23:09:37 +01:00
										 |  |  |     Vector<LabelableScope> m_continuable_scopes; | 
					
						
							|  |  |  |     Vector<LabelableScope> m_breakable_scopes; | 
					
						
							| 
									
										
										
										
											2022-03-13 11:51:02 +03:30
										 |  |  |     Vector<BlockBoundaryType> m_boundaries; | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  |     Vector<ScopedOperand> m_home_objects; | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     HashTable<u32> m_initialized_locals; | 
					
						
							| 
									
										
										
										
											2024-05-07 21:36:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_finished { false }; | 
					
						
							| 
									
										
										
										
											2024-05-14 10:57:06 +02:00
										 |  |  |     bool m_must_propagate_completion { true }; | 
					
						
							| 
									
										
										
										
											2024-05-31 20:32:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<ECMAScriptFunctionObject const> m_function; | 
					
						
							| 
									
										
										
										
											2024-07-09 11:37:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Optional<IdentifierTableIndex> m_length_identifier; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |