Instruction.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2021, 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/Span.h>
  9. #include <LibJS/Forward.h>
  10. #define ENUMERATE_BYTECODE_OPS(O) \
  11. O(Add) \
  12. O(Append) \
  13. O(AsyncIteratorClose) \
  14. O(Await) \
  15. O(BitwiseAnd) \
  16. O(BitwiseNot) \
  17. O(BitwiseOr) \
  18. O(BitwiseXor) \
  19. O(BlockDeclarationInstantiation) \
  20. O(Call) \
  21. O(CallWithArgumentArray) \
  22. O(ConcatString) \
  23. O(ContinuePendingUnwind) \
  24. O(CopyObjectExcludingProperties) \
  25. O(CreateLexicalEnvironment) \
  26. O(CreateVariable) \
  27. O(Decrement) \
  28. O(DeleteById) \
  29. O(DeleteByIdWithThis) \
  30. O(DeleteByValue) \
  31. O(DeleteByValueWithThis) \
  32. O(DeleteVariable) \
  33. O(Div) \
  34. O(EnterUnwindContext) \
  35. O(EnterObjectEnvironment) \
  36. O(Exp) \
  37. O(GetById) \
  38. O(GetByIdWithThis) \
  39. O(GetByValue) \
  40. O(GetByValueWithThis) \
  41. O(GetCalleeAndThisFromEnvironment) \
  42. O(GetIterator) \
  43. O(GetMethod) \
  44. O(GetNewTarget) \
  45. O(GetImportMeta) \
  46. O(GetObjectPropertyIterator) \
  47. O(GetPrivateById) \
  48. O(GetVariable) \
  49. O(GetGlobal) \
  50. O(GetLocal) \
  51. O(GreaterThan) \
  52. O(GreaterThanEquals) \
  53. O(HasPrivateId) \
  54. O(ImportCall) \
  55. O(In) \
  56. O(Increment) \
  57. O(InstanceOf) \
  58. O(IteratorClose) \
  59. O(IteratorNext) \
  60. O(IteratorResultDone) \
  61. O(IteratorResultValue) \
  62. O(IteratorToArray) \
  63. O(Jump) \
  64. O(JumpConditional) \
  65. O(JumpNullish) \
  66. O(JumpUndefined) \
  67. O(LeaveLexicalEnvironment) \
  68. O(LeaveUnwindContext) \
  69. O(LeftShift) \
  70. O(LessThan) \
  71. O(LessThanEquals) \
  72. O(Load) \
  73. O(LoadImmediate) \
  74. O(LooselyEquals) \
  75. O(LooselyInequals) \
  76. O(Mod) \
  77. O(Mul) \
  78. O(NewArray) \
  79. O(NewBigInt) \
  80. O(NewClass) \
  81. O(NewFunction) \
  82. O(NewObject) \
  83. O(NewRegExp) \
  84. O(NewString) \
  85. O(NewTypeError) \
  86. O(Not) \
  87. O(PushDeclarativeEnvironment) \
  88. O(PutById) \
  89. O(PutByIdWithThis) \
  90. O(PutByValue) \
  91. O(PutByValueWithThis) \
  92. O(PutPrivateById) \
  93. O(ResolveThisBinding) \
  94. O(ResolveSuperBase) \
  95. O(Return) \
  96. O(RightShift) \
  97. O(ScheduleJump) \
  98. O(SetVariable) \
  99. O(SetLocal) \
  100. O(Store) \
  101. O(StrictlyEquals) \
  102. O(StrictlyInequals) \
  103. O(Sub) \
  104. O(SuperCallWithArgumentArray) \
  105. O(Throw) \
  106. O(ThrowIfNotObject) \
  107. O(ThrowIfNullish) \
  108. O(ToNumeric) \
  109. O(Typeof) \
  110. O(TypeofVariable) \
  111. O(TypeofLocal) \
  112. O(UnaryMinus) \
  113. O(UnaryPlus) \
  114. O(UnsignedRightShift) \
  115. O(Yield)
  116. namespace JS::Bytecode {
  117. class alignas(void*) Instruction {
  118. public:
  119. constexpr static bool IsTerminator = false;
  120. enum class Type {
  121. #define __BYTECODE_OP(op) \
  122. op,
  123. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  124. #undef __BYTECODE_OP
  125. };
  126. bool is_terminator() const;
  127. Type type() const { return m_type; }
  128. size_t length() const;
  129. DeprecatedString to_deprecated_string(Bytecode::Executable const&) const;
  130. ThrowCompletionOr<void> execute(Bytecode::Interpreter&) const;
  131. static void destroy(Instruction&);
  132. protected:
  133. explicit Instruction(Type type)
  134. : m_type(type)
  135. {
  136. }
  137. private:
  138. Type m_type {};
  139. };
  140. class InstructionStreamIterator {
  141. public:
  142. explicit InstructionStreamIterator(ReadonlyBytes bytes)
  143. : m_bytes(bytes)
  144. {
  145. }
  146. size_t offset() const { return m_offset; }
  147. bool at_end() const { return m_offset >= m_bytes.size(); }
  148. void jump(size_t offset)
  149. {
  150. VERIFY(offset <= m_bytes.size());
  151. m_offset = offset;
  152. }
  153. Instruction const& operator*() const { return dereference(); }
  154. ALWAYS_INLINE void operator++()
  155. {
  156. VERIFY(!at_end());
  157. m_offset += dereference().length();
  158. }
  159. private:
  160. Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
  161. ReadonlyBytes m_bytes;
  162. size_t m_offset { 0 };
  163. };
  164. }