Instruction.h 4.1 KB

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