Instruction.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(BitwiseAnd) \
  13. O(BitwiseNot) \
  14. O(BitwiseOr) \
  15. O(BitwiseXor) \
  16. O(Call) \
  17. O(ConcatString) \
  18. O(ContinuePendingUnwind) \
  19. O(CopyObjectExcludingProperties) \
  20. O(CreateEnvironment) \
  21. O(CreateVariable) \
  22. O(Decrement) \
  23. O(Div) \
  24. O(EnterUnwindContext) \
  25. O(EnterObjectEnvironment) \
  26. O(Exp) \
  27. O(FinishUnwind) \
  28. O(GetById) \
  29. O(GetByValue) \
  30. O(GetIterator) \
  31. O(GetVariable) \
  32. O(GreaterThan) \
  33. O(GreaterThanEquals) \
  34. O(In) \
  35. O(Increment) \
  36. O(InstanceOf) \
  37. O(IteratorNext) \
  38. O(IteratorResultDone) \
  39. O(IteratorResultValue) \
  40. O(IteratorToArray) \
  41. O(Jump) \
  42. O(JumpConditional) \
  43. O(JumpNullish) \
  44. O(JumpUndefined) \
  45. O(LeaveEnvironment) \
  46. O(LeaveUnwindContext) \
  47. O(LeftShift) \
  48. O(LessThan) \
  49. O(LessThanEquals) \
  50. O(Load) \
  51. O(LoadImmediate) \
  52. O(LooselyEquals) \
  53. O(LooselyInequals) \
  54. O(Mod) \
  55. O(Mul) \
  56. O(NewArray) \
  57. O(NewBigInt) \
  58. O(NewClass) \
  59. O(NewFunction) \
  60. O(NewObject) \
  61. O(NewRegExp) \
  62. O(NewString) \
  63. O(Not) \
  64. O(PushDeclarativeEnvironment) \
  65. O(PutById) \
  66. O(PutByValue) \
  67. O(ResolveThisBinding) \
  68. O(Return) \
  69. O(RightShift) \
  70. O(SetVariable) \
  71. O(Store) \
  72. O(StrictlyEquals) \
  73. O(StrictlyInequals) \
  74. O(Sub) \
  75. O(Throw) \
  76. O(Typeof) \
  77. O(UnaryMinus) \
  78. O(UnaryPlus) \
  79. O(UnsignedRightShift) \
  80. O(Yield)
  81. namespace JS::Bytecode {
  82. class Instruction {
  83. public:
  84. constexpr static bool IsTerminator = false;
  85. enum class Type {
  86. #define __BYTECODE_OP(op) \
  87. op,
  88. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  89. #undef __BYTECODE_OP
  90. };
  91. bool is_terminator() const;
  92. Type type() const { return m_type; }
  93. size_t length() const;
  94. String to_string(Bytecode::Executable const&) const;
  95. ThrowCompletionOr<void> execute(Bytecode::Interpreter&) const;
  96. void replace_references(BasicBlock const&, BasicBlock const&);
  97. static void destroy(Instruction&);
  98. protected:
  99. explicit Instruction(Type type)
  100. : m_type(type)
  101. {
  102. }
  103. private:
  104. Type m_type {};
  105. };
  106. class InstructionStreamIterator {
  107. public:
  108. explicit InstructionStreamIterator(ReadonlyBytes bytes)
  109. : m_bytes(bytes)
  110. {
  111. }
  112. size_t offset() const { return m_offset; }
  113. bool at_end() const { return m_offset >= m_bytes.size(); }
  114. void jump(size_t offset)
  115. {
  116. VERIFY(offset <= m_bytes.size());
  117. m_offset = offset;
  118. }
  119. Instruction const& operator*() const { return dereference(); }
  120. ALWAYS_INLINE void operator++()
  121. {
  122. VERIFY(!at_end());
  123. m_offset += dereference().length();
  124. }
  125. private:
  126. Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
  127. ReadonlyBytes m_bytes;
  128. size_t m_offset { 0 };
  129. };
  130. }