Instruction.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/Function.h>
  9. #include <AK/Span.h>
  10. #include <LibJS/Bytecode/Executable.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/SourceRange.h>
  13. #define ENUMERATE_BYTECODE_OPS(O) \
  14. O(Add) \
  15. O(AddPrivateName) \
  16. O(ArrayAppend) \
  17. O(AsyncIteratorClose) \
  18. O(Await) \
  19. O(BitwiseAnd) \
  20. O(BitwiseNot) \
  21. O(BitwiseOr) \
  22. O(BitwiseXor) \
  23. O(BlockDeclarationInstantiation) \
  24. O(Call) \
  25. O(CallWithArgumentArray) \
  26. O(Catch) \
  27. O(ConcatString) \
  28. O(ContinuePendingUnwind) \
  29. O(CopyObjectExcludingProperties) \
  30. O(CreateArguments) \
  31. O(CreateLexicalEnvironment) \
  32. O(CreatePrivateEnvironment) \
  33. O(CreateRestParams) \
  34. O(CreateVariable) \
  35. O(CreateVariableEnvironment) \
  36. O(Decrement) \
  37. O(DeleteById) \
  38. O(DeleteByIdWithThis) \
  39. O(DeleteByValue) \
  40. O(DeleteByValueWithThis) \
  41. O(DeleteVariable) \
  42. O(Div) \
  43. O(Dump) \
  44. O(End) \
  45. O(EnterObjectEnvironment) \
  46. O(EnterUnwindContext) \
  47. O(Exp) \
  48. O(GetArgument) \
  49. O(GetById) \
  50. O(GetByIdWithThis) \
  51. O(GetByValue) \
  52. O(GetByValueWithThis) \
  53. O(GetCalleeAndThisFromEnvironment) \
  54. O(GetGlobal) \
  55. O(GetImportMeta) \
  56. O(GetIterator) \
  57. O(GetLength) \
  58. O(GetLengthWithThis) \
  59. O(GetMethod) \
  60. O(GetNewTarget) \
  61. O(GetNextMethodFromIteratorRecord) \
  62. O(GetObjectFromIteratorRecord) \
  63. O(GetObjectPropertyIterator) \
  64. O(GetPrivateById) \
  65. O(GetBinding) \
  66. O(GreaterThan) \
  67. O(GreaterThanEquals) \
  68. O(HasPrivateId) \
  69. O(ImportCall) \
  70. O(In) \
  71. O(Increment) \
  72. O(InitializeLexicalBinding) \
  73. O(InitializeVariableBinding) \
  74. O(InstanceOf) \
  75. O(IteratorClose) \
  76. O(IteratorNext) \
  77. O(IteratorToArray) \
  78. O(Jump) \
  79. O(JumpFalse) \
  80. O(JumpGreaterThan) \
  81. O(JumpGreaterThanEquals) \
  82. O(JumpIf) \
  83. O(JumpLessThan) \
  84. O(JumpLessThanEquals) \
  85. O(JumpLooselyEquals) \
  86. O(JumpLooselyInequals) \
  87. O(JumpNullish) \
  88. O(JumpStrictlyEquals) \
  89. O(JumpStrictlyInequals) \
  90. O(JumpTrue) \
  91. O(JumpUndefined) \
  92. O(LeaveFinally) \
  93. O(LeaveLexicalEnvironment) \
  94. O(LeavePrivateEnvironment) \
  95. O(LeaveUnwindContext) \
  96. O(LeftShift) \
  97. O(LessThan) \
  98. O(LessThanEquals) \
  99. O(LooselyEquals) \
  100. O(LooselyInequals) \
  101. O(Mod) \
  102. O(Mov) \
  103. O(Mul) \
  104. O(NewArray) \
  105. O(NewClass) \
  106. O(NewFunction) \
  107. O(NewObject) \
  108. O(NewPrimitiveArray) \
  109. O(NewRegExp) \
  110. O(NewTypeError) \
  111. O(Not) \
  112. O(PrepareYield) \
  113. O(PostfixDecrement) \
  114. O(PostfixIncrement) \
  115. O(PutById) \
  116. O(PutByIdWithThis) \
  117. O(PutByValue) \
  118. O(PutByValueWithThis) \
  119. O(PutPrivateById) \
  120. O(ResolveSuperBase) \
  121. O(ResolveThisBinding) \
  122. O(RestoreScheduledJump) \
  123. O(Return) \
  124. O(RightShift) \
  125. O(ScheduleJump) \
  126. O(SetArgument) \
  127. O(SetLexicalBinding) \
  128. O(SetVariableBinding) \
  129. O(StrictlyEquals) \
  130. O(StrictlyInequals) \
  131. O(Sub) \
  132. O(SuperCallWithArgumentArray) \
  133. O(Throw) \
  134. O(ThrowIfNotObject) \
  135. O(ThrowIfNullish) \
  136. O(ThrowIfTDZ) \
  137. O(Typeof) \
  138. O(TypeofBinding) \
  139. O(UnaryMinus) \
  140. O(UnaryPlus) \
  141. O(UnsignedRightShift) \
  142. O(Yield)
  143. namespace JS::Bytecode {
  144. class alignas(void*) Instruction {
  145. public:
  146. constexpr static bool IsTerminator = false;
  147. static constexpr bool IsVariableLength = false;
  148. enum class Type {
  149. #define __BYTECODE_OP(op) \
  150. op,
  151. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  152. #undef __BYTECODE_OP
  153. };
  154. Type type() const { return m_type; }
  155. size_t length() const;
  156. ByteString to_byte_string(Bytecode::Executable const&) const;
  157. void visit_labels(Function<void(Label&)> visitor);
  158. void visit_operands(Function<void(Operand&)> visitor);
  159. static void destroy(Instruction&);
  160. protected:
  161. explicit Instruction(Type type)
  162. : m_type(type)
  163. {
  164. }
  165. void visit_labels_impl(Function<void(Label&)>) { }
  166. void visit_operands_impl(Function<void(Operand&)>) { }
  167. private:
  168. Type m_type {};
  169. };
  170. class InstructionStreamIterator {
  171. public:
  172. InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr, size_t offset = 0)
  173. : m_begin(bytes.data())
  174. , m_end(bytes.data() + bytes.size())
  175. , m_ptr(bytes.data() + offset)
  176. , m_executable(executable)
  177. {
  178. }
  179. size_t offset() const { return m_ptr - m_begin; }
  180. bool at_end() const { return m_ptr >= m_end; }
  181. Instruction const& operator*() const { return dereference(); }
  182. ALWAYS_INLINE void operator++()
  183. {
  184. m_ptr += dereference().length();
  185. }
  186. UnrealizedSourceRange source_range() const;
  187. RefPtr<SourceCode> source_code() const;
  188. Executable const* executable() const { return m_executable; }
  189. private:
  190. Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_ptr); }
  191. u8 const* m_begin { nullptr };
  192. u8 const* m_end { nullptr };
  193. u8 const* m_ptr { nullptr };
  194. GCPtr<Executable const> m_executable;
  195. };
  196. }