| 
									
										
										
										
											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/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-06 07:51:14 +02:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | #include <AK/Span.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  | #include <LibJS/Bytecode/Executable.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  | #include <LibJS/SourceRange.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  | #define ENUMERATE_BYTECODE_OPS(O)      \
 | 
					
						
							|  |  |  |     O(Add)                             \ | 
					
						
							| 
									
										
										
										
											2024-05-11 22:54:41 +00:00
										 |  |  |     O(AddPrivateName)                  \ | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     O(ArrayAppend)                     \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(AsyncIteratorClose)              \ | 
					
						
							|  |  |  |     O(Await)                           \ | 
					
						
							|  |  |  |     O(BitwiseAnd)                      \ | 
					
						
							|  |  |  |     O(BitwiseNot)                      \ | 
					
						
							|  |  |  |     O(BitwiseOr)                       \ | 
					
						
							|  |  |  |     O(BitwiseXor)                      \ | 
					
						
							|  |  |  |     O(BlockDeclarationInstantiation)   \ | 
					
						
							|  |  |  |     O(Call)                            \ | 
					
						
							| 
									
										
										
										
											2024-10-31 22:47:30 +01:00
										 |  |  |     O(CallBuiltin)                     \ | 
					
						
							|  |  |  |     O(CallConstruct)                   \ | 
					
						
							|  |  |  |     O(CallDirectEval)                  \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(CallWithArgumentArray)           \ | 
					
						
							| 
									
										
										
										
											2023-11-11 23:19:46 +01:00
										 |  |  |     O(Catch)                           \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(ConcatString)                    \ | 
					
						
							|  |  |  |     O(ContinuePendingUnwind)           \ | 
					
						
							|  |  |  |     O(CopyObjectExcludingProperties)   \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:06:12 +02:00
										 |  |  |     O(CreateArguments)                 \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(CreateLexicalEnvironment)        \ | 
					
						
							| 
									
										
										
										
											2024-05-11 22:54:41 +00:00
										 |  |  |     O(CreatePrivateEnvironment)        \ | 
					
						
							| 
									
										
										
										
											2024-05-05 22:06:55 +02:00
										 |  |  |     O(CreateRestParams)                \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:06:12 +02:00
										 |  |  |     O(CreateVariable)                  \ | 
					
						
							|  |  |  |     O(CreateVariableEnvironment)       \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(Decrement)                       \ | 
					
						
							|  |  |  |     O(DeleteById)                      \ | 
					
						
							|  |  |  |     O(DeleteByIdWithThis)              \ | 
					
						
							|  |  |  |     O(DeleteByValue)                   \ | 
					
						
							|  |  |  |     O(DeleteByValueWithThis)           \ | 
					
						
							|  |  |  |     O(DeleteVariable)                  \ | 
					
						
							|  |  |  |     O(Div)                             \ | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     O(Dump)                            \ | 
					
						
							|  |  |  |     O(End)                             \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(EnterObjectEnvironment)          \ | 
					
						
							| 
									
										
										
										
											2024-04-11 11:58:18 +02:00
										 |  |  |     O(EnterUnwindContext)              \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(Exp)                             \ | 
					
						
							|  |  |  |     O(GetById)                         \ | 
					
						
							|  |  |  |     O(GetByIdWithThis)                 \ | 
					
						
							|  |  |  |     O(GetByValue)                      \ | 
					
						
							|  |  |  |     O(GetByValueWithThis)              \ | 
					
						
							|  |  |  |     O(GetCalleeAndThisFromEnvironment) \ | 
					
						
							| 
									
										
										
										
											2025-03-31 09:32:39 +01:00
										 |  |  |     O(GetCompletionFields)             \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:06:12 +02:00
										 |  |  |     O(GetGlobal)                       \ | 
					
						
							|  |  |  |     O(GetImportMeta)                   \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(GetIterator)                     \ | 
					
						
							| 
									
										
										
										
											2024-05-20 11:53:28 +02:00
										 |  |  |     O(GetLength)                       \ | 
					
						
							|  |  |  |     O(GetLengthWithThis)               \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(GetMethod)                       \ | 
					
						
							|  |  |  |     O(GetNewTarget)                    \ | 
					
						
							| 
									
										
										
										
											2023-12-07 10:44:41 +01:00
										 |  |  |     O(GetNextMethodFromIteratorRecord) \ | 
					
						
							| 
									
										
										
										
											2024-04-11 11:58:18 +02:00
										 |  |  |     O(GetObjectFromIteratorRecord)     \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(GetObjectPropertyIterator)       \ | 
					
						
							|  |  |  |     O(GetPrivateById)                  \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:32:04 +02:00
										 |  |  |     O(GetBinding)                      \ | 
					
						
							| 
									
										
										
										
											2025-05-04 01:41:49 +02:00
										 |  |  |     O(GetInitializedBinding)           \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(GreaterThan)                     \ | 
					
						
							|  |  |  |     O(GreaterThanEquals)               \ | 
					
						
							|  |  |  |     O(HasPrivateId)                    \ | 
					
						
							|  |  |  |     O(ImportCall)                      \ | 
					
						
							|  |  |  |     O(In)                              \ | 
					
						
							|  |  |  |     O(Increment)                       \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:30:30 +02:00
										 |  |  |     O(InitializeLexicalBinding)        \ | 
					
						
							|  |  |  |     O(InitializeVariableBinding)       \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(InstanceOf)                      \ | 
					
						
							|  |  |  |     O(IteratorClose)                   \ | 
					
						
							|  |  |  |     O(IteratorNext)                    \ | 
					
						
							| 
									
										
										
										
											2025-05-01 16:05:24 +03:00
										 |  |  |     O(IteratorNextUnpack)              \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(IteratorToArray)                 \ | 
					
						
							|  |  |  |     O(Jump)                            \ | 
					
						
							| 
									
										
										
										
											2024-05-06 10:15:17 +02:00
										 |  |  |     O(JumpFalse)                       \ | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |     O(JumpGreaterThan)                 \ | 
					
						
							|  |  |  |     O(JumpGreaterThanEquals)           \ | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     O(JumpIf)                          \ | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |     O(JumpLessThan)                    \ | 
					
						
							|  |  |  |     O(JumpLessThanEquals)              \ | 
					
						
							|  |  |  |     O(JumpLooselyEquals)               \ | 
					
						
							|  |  |  |     O(JumpLooselyInequals)             \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(JumpNullish)                     \ | 
					
						
							| 
									
										
										
										
											2024-05-09 15:13:31 +02:00
										 |  |  |     O(JumpStrictlyEquals)              \ | 
					
						
							|  |  |  |     O(JumpStrictlyInequals)            \ | 
					
						
							| 
									
										
										
										
											2024-05-06 10:15:17 +02:00
										 |  |  |     O(JumpTrue)                        \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(JumpUndefined)                   \ | 
					
						
							| 
									
										
										
										
											2024-04-11 11:58:18 +02:00
										 |  |  |     O(LeaveFinally)                    \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(LeaveLexicalEnvironment)         \ | 
					
						
							| 
									
										
										
										
											2024-05-11 22:54:41 +00:00
										 |  |  |     O(LeavePrivateEnvironment)         \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(LeaveUnwindContext)              \ | 
					
						
							|  |  |  |     O(LeftShift)                       \ | 
					
						
							|  |  |  |     O(LessThan)                        \ | 
					
						
							|  |  |  |     O(LessThanEquals)                  \ | 
					
						
							|  |  |  |     O(LooselyEquals)                   \ | 
					
						
							|  |  |  |     O(LooselyInequals)                 \ | 
					
						
							|  |  |  |     O(Mod)                             \ | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     O(Mov)                             \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(Mul)                             \ | 
					
						
							|  |  |  |     O(NewArray)                        \ | 
					
						
							|  |  |  |     O(NewClass)                        \ | 
					
						
							|  |  |  |     O(NewFunction)                     \ | 
					
						
							|  |  |  |     O(NewObject)                       \ | 
					
						
							| 
									
										
										
										
											2023-11-17 22:07:23 +02:00
										 |  |  |     O(NewPrimitiveArray)               \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(NewRegExp)                       \ | 
					
						
							|  |  |  |     O(NewTypeError)                    \ | 
					
						
							|  |  |  |     O(Not)                             \ | 
					
						
							| 
									
										
										
										
											2024-05-18 17:25:43 +02:00
										 |  |  |     O(PrepareYield)                    \ | 
					
						
							| 
									
										
										
										
											2024-02-20 11:45:01 +01:00
										 |  |  |     O(PostfixDecrement)                \ | 
					
						
							|  |  |  |     O(PostfixIncrement)                \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(PutById)                         \ | 
					
						
							|  |  |  |     O(PutByIdWithThis)                 \ | 
					
						
							| 
									
										
										
										
											2024-11-01 22:00:32 +01:00
										 |  |  |     O(PutBySpread)                     \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(PutByValue)                      \ | 
					
						
							|  |  |  |     O(PutByValueWithThis)              \ | 
					
						
							|  |  |  |     O(PutPrivateById)                  \ | 
					
						
							|  |  |  |     O(ResolveSuperBase)                \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:06:12 +02:00
										 |  |  |     O(ResolveThisBinding)              \ | 
					
						
							| 
									
										
										
										
											2024-04-11 11:07:35 +02:00
										 |  |  |     O(RestoreScheduledJump)            \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(Return)                          \ | 
					
						
							|  |  |  |     O(RightShift)                      \ | 
					
						
							|  |  |  |     O(ScheduleJump)                    \ | 
					
						
							| 
									
										
										
										
											2025-03-31 09:32:39 +01:00
										 |  |  |     O(SetCompletionType)               \ | 
					
						
							| 
									
										
										
										
											2025-05-01 23:58:38 +02:00
										 |  |  |     O(SetGlobal)                       \ | 
					
						
							| 
									
										
										
										
											2024-05-14 11:30:30 +02:00
										 |  |  |     O(SetLexicalBinding)               \ | 
					
						
							|  |  |  |     O(SetVariableBinding)              \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(StrictlyEquals)                  \ | 
					
						
							|  |  |  |     O(StrictlyInequals)                \ | 
					
						
							|  |  |  |     O(Sub)                             \ | 
					
						
							|  |  |  |     O(SuperCallWithArgumentArray)      \ | 
					
						
							|  |  |  |     O(Throw)                           \ | 
					
						
							|  |  |  |     O(ThrowIfNotObject)                \ | 
					
						
							|  |  |  |     O(ThrowIfNullish)                  \ | 
					
						
							| 
									
										
										
										
											2024-02-04 08:00:54 +01:00
										 |  |  |     O(ThrowIfTDZ)                      \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(Typeof)                          \ | 
					
						
							| 
									
										
										
										
											2024-06-14 09:37:26 +02:00
										 |  |  |     O(TypeofBinding)                   \ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:33:58 +02:00
										 |  |  |     O(UnaryMinus)                      \ | 
					
						
							|  |  |  |     O(UnaryPlus)                       \ | 
					
						
							|  |  |  |     O(UnsignedRightShift)              \ | 
					
						
							| 
									
										
										
										
											2021-10-24 14:38:57 +02:00
										 |  |  |     O(Yield) | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 16:47:42 +02:00
										 |  |  | class alignas(void*) Instruction { | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     constexpr static bool IsTerminator = false; | 
					
						
							| 
									
										
										
										
											2024-05-06 10:42:52 +02:00
										 |  |  |     static constexpr bool IsVariableLength = false; | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     enum class Type { | 
					
						
							|  |  |  | #define __BYTECODE_OP(op) \
 | 
					
						
							|  |  |  |     op, | 
					
						
							|  |  |  |         ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) | 
					
						
							|  |  |  | #undef __BYTECODE_OP
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Type type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2024-05-06 11:09:09 +02:00
										 |  |  |     size_t length() const; | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     ByteString to_byte_string(Bytecode::Executable const&) const; | 
					
						
							| 
									
										
										
										
											2024-05-06 06:44:08 +02:00
										 |  |  |     void visit_labels(Function<void(Label&)> visitor); | 
					
						
							| 
									
										
										
										
											2024-05-12 18:49:03 +02:00
										 |  |  |     void visit_operands(Function<void(Operand&)> visitor); | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     static void destroy(Instruction&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2024-05-06 11:09:09 +02:00
										 |  |  |     explicit Instruction(Type type) | 
					
						
							|  |  |  |         : m_type(type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 06:44:08 +02:00
										 |  |  |     void visit_labels_impl(Function<void(Label&)>) { } | 
					
						
							| 
									
										
										
										
											2024-05-12 18:49:03 +02:00
										 |  |  |     void visit_operands_impl(Function<void(Operand&)>) { } | 
					
						
							| 
									
										
										
										
											2024-05-06 06:44:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Type m_type {}; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class InstructionStreamIterator { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-10-30 22:06:27 +01:00
										 |  |  |     InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr, size_t offset = 0) | 
					
						
							| 
									
										
										
										
											2023-09-26 16:03:42 +02:00
										 |  |  |         : m_begin(bytes.data()) | 
					
						
							|  |  |  |         , m_end(bytes.data() + bytes.size()) | 
					
						
							| 
									
										
										
										
											2023-10-30 22:06:27 +01:00
										 |  |  |         , m_ptr(bytes.data() + offset) | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  |         , m_executable(executable) | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 16:03:42 +02:00
										 |  |  |     size_t offset() const { return m_ptr - m_begin; } | 
					
						
							|  |  |  |     bool at_end() const { return m_ptr >= m_end; } | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Instruction const& operator*() const { return dereference(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE void operator++() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-26 16:03:42 +02:00
										 |  |  |         m_ptr += dereference().length(); | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 16:53:55 +02:00
										 |  |  |     UnrealizedSourceRange source_range() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-03 08:18:10 +02:00
										 |  |  |     Executable const* executable() const { return m_executable; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-09-26 16:03:42 +02:00
										 |  |  |     Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_ptr); } | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 16:03:42 +02:00
										 |  |  |     u8 const* m_begin { nullptr }; | 
					
						
							|  |  |  |     u8 const* m_end { nullptr }; | 
					
						
							|  |  |  |     u8 const* m_ptr { nullptr }; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<Executable const> m_executable; | 
					
						
							| 
									
										
										
										
											2021-10-25 13:37:02 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |