Instruction.h 6.7 KB

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