Instruction.h 6.3 KB

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