| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  | #include <LibJS/Bytecode/Executable.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Bytecode/Instruction.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | #include <LibJS/Bytecode/Op.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | void Instruction::destroy(Instruction& instruction) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | #define __BYTECODE_OP(op)                        \
 | 
					
						
							|  |  |  |     case Type::op:                               \ | 
					
						
							|  |  |  |         static_cast<Op::op&>(instruction).~op(); \ | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (instruction.type()) { | 
					
						
							|  |  |  |         ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __BYTECODE_OP
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 06:44:08 +02:00
										 |  |  | void Instruction::visit_labels(Function<void(JS::Bytecode::Label&)> visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #define __BYTECODE_OP(op)                                             \
 | 
					
						
							|  |  |  |     case Type::op:                                                    \ | 
					
						
							|  |  |  |         static_cast<Op::op&>(*this).visit_labels_impl(move(visitor)); \ | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (type()) { | 
					
						
							|  |  |  |         ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __BYTECODE_OP
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-12 18:49:03 +02:00
										 |  |  | void Instruction::visit_operands(Function<void(JS::Bytecode::Operand&)> visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #define __BYTECODE_OP(op)                                               \
 | 
					
						
							|  |  |  |     case Type::op:                                                      \ | 
					
						
							|  |  |  |         static_cast<Op::op&>(*this).visit_operands_impl(move(visitor)); \ | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (type()) { | 
					
						
							|  |  |  |         ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __BYTECODE_OP
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 11:09:09 +02:00
										 |  |  | template<typename Op> | 
					
						
							|  |  |  | concept HasVariableLength = Op::IsVariableLength; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename Op> | 
					
						
							|  |  |  | concept HasFixedLength = !Op::IsVariableLength; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<HasVariableLength Op> | 
					
						
							|  |  |  | size_t get_length_impl(Op const& op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return op.length_impl(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Function template for types without a length_impl method
 | 
					
						
							|  |  |  | template<HasFixedLength Op> | 
					
						
							|  |  |  | size_t get_length_impl(Op const&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return sizeof(Op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t Instruction::length() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #define __BYTECODE_OP(op)                                   \
 | 
					
						
							|  |  |  |     case Type::op: {                                        \ | 
					
						
							|  |  |  |         auto& typed_op = static_cast<Op::op const&>(*this); \ | 
					
						
							|  |  |  |         return get_length_impl(typed_op);                   \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (type()) { | 
					
						
							|  |  |  |         ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __BYTECODE_OP
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  | UnrealizedSourceRange InstructionStreamIterator::source_range() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VERIFY(m_executable); | 
					
						
							| 
									
										
										
										
											2024-05-06 07:51:14 +02:00
										 |  |  |     auto record = m_executable->source_map.get(offset()).value(); | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         .source_code = m_executable->source_code, | 
					
						
							|  |  |  |         .start_offset = record.source_start_offset, | 
					
						
							|  |  |  |         .end_offset = record.source_end_offset, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 10:19:17 +01:00
										 |  |  | Operand::Operand(Register reg) | 
					
						
							|  |  |  |     : m_type(Type::Register) | 
					
						
							|  |  |  |     , m_index(reg.index()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |