OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef V8_INTERPRETER_BYTECODE_TRAITS_H_ |
| 6 #define V8_INTERPRETER_BYTECODE_TRAITS_H_ |
| 7 |
| 8 #include "src/interpreter/bytecodes.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 namespace interpreter { |
| 13 namespace internal { |
| 14 |
| 15 // Template helpers to deduce the number of operands each bytecode has. |
| 16 #define OPERAND_TERM OperandType::kNone, OperandType::kNone, OperandType::kNone |
| 17 |
| 18 template <OperandType> |
| 19 struct OperandTraits {}; |
| 20 |
| 21 #define DECLARE_OPERAND_SIZE(Name, Size) \ |
| 22 template <> \ |
| 23 struct OperandTraits<OperandType::k##Name> { \ |
| 24 static const int kSize = static_cast<int>(Size); \ |
| 25 }; |
| 26 OPERAND_TYPE_LIST(DECLARE_OPERAND_SIZE) |
| 27 #undef DECLARE_OPERAND_SIZE |
| 28 |
| 29 |
| 30 template <OperandType... Args> |
| 31 struct BytecodeTraits {}; |
| 32 |
| 33 template <OperandType operand_0, OperandType operand_1, OperandType operand_2, |
| 34 OperandType operand_3> |
| 35 struct BytecodeTraits<operand_0, operand_1, operand_2, operand_3, |
| 36 OPERAND_TERM> { |
| 37 static OperandType GetOperandType(int i) { |
| 38 DCHECK(0 <= i && i < kOperandCount); |
| 39 const OperandType kOperands[] = {operand_0, operand_1, operand_2, |
| 40 operand_3}; |
| 41 return kOperands[i]; |
| 42 } |
| 43 |
| 44 static inline int GetOperandOffset(int i) { |
| 45 DCHECK(0 <= i && i < kOperandCount); |
| 46 const int kOffset0 = 1; |
| 47 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; |
| 48 const int kOffset2 = kOffset1 + OperandTraits<operand_1>::kSize; |
| 49 const int kOffset3 = kOffset2 + OperandTraits<operand_2>::kSize; |
| 50 const int kOperandOffsets[] = {kOffset0, kOffset1, kOffset2, kOffset3}; |
| 51 return kOperandOffsets[i]; |
| 52 } |
| 53 |
| 54 static const int kOperandCount = 4; |
| 55 static const int kSize = |
| 56 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize + |
| 57 OperandTraits<operand_2>::kSize + OperandTraits<operand_3>::kSize; |
| 58 }; |
| 59 |
| 60 |
| 61 template <OperandType operand_0, OperandType operand_1, OperandType operand_2> |
| 62 struct BytecodeTraits<operand_0, operand_1, operand_2, OPERAND_TERM> { |
| 63 static inline OperandType GetOperandType(int i) { |
| 64 DCHECK(0 <= i && i <= 2); |
| 65 const OperandType kOperands[] = {operand_0, operand_1, operand_2}; |
| 66 return kOperands[i]; |
| 67 } |
| 68 |
| 69 static inline int GetOperandOffset(int i) { |
| 70 DCHECK(0 <= i && i < kOperandCount); |
| 71 const int kOffset0 = 1; |
| 72 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; |
| 73 const int kOffset2 = kOffset1 + OperandTraits<operand_1>::kSize; |
| 74 const int kOperandOffsets[] = {kOffset0, kOffset1, kOffset2}; |
| 75 return kOperandOffsets[i]; |
| 76 } |
| 77 |
| 78 static const int kOperandCount = 3; |
| 79 static const int kSize = |
| 80 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize + |
| 81 OperandTraits<operand_2>::kSize; |
| 82 }; |
| 83 |
| 84 template <OperandType operand_0, OperandType operand_1> |
| 85 struct BytecodeTraits<operand_0, operand_1, OPERAND_TERM> { |
| 86 static inline OperandType GetOperandType(int i) { |
| 87 DCHECK(0 <= i && i < kOperandCount); |
| 88 const OperandType kOperands[] = {operand_0, operand_1}; |
| 89 return kOperands[i]; |
| 90 } |
| 91 |
| 92 static inline int GetOperandOffset(int i) { |
| 93 DCHECK(0 <= i && i < kOperandCount); |
| 94 const int kOffset0 = 1; |
| 95 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; |
| 96 const int kOperandOffsets[] = {kOffset0, kOffset1}; |
| 97 return kOperandOffsets[i]; |
| 98 } |
| 99 |
| 100 static const int kOperandCount = 2; |
| 101 static const int kSize = |
| 102 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize; |
| 103 }; |
| 104 |
| 105 template <OperandType operand_0> |
| 106 struct BytecodeTraits<operand_0, OPERAND_TERM> { |
| 107 static inline OperandType GetOperandType(int i) { |
| 108 DCHECK(i == 0); |
| 109 return operand_0; |
| 110 } |
| 111 |
| 112 static inline int GetOperandOffset(int i) { |
| 113 DCHECK(i == 0); |
| 114 return 1; |
| 115 } |
| 116 |
| 117 static const int kOperandCount = 1; |
| 118 static const int kSize = 1 + OperandTraits<operand_0>::kSize; |
| 119 }; |
| 120 |
| 121 template <> |
| 122 struct BytecodeTraits<OperandType::kNone, OPERAND_TERM> { |
| 123 static inline OperandType GetOperandType(int i) { |
| 124 UNREACHABLE(); |
| 125 return OperandType::kNone; |
| 126 } |
| 127 |
| 128 static inline int GetOperandOffset(int i) { |
| 129 UNREACHABLE(); |
| 130 return 1; |
| 131 } |
| 132 |
| 133 static const int kOperandCount = 0; |
| 134 static const int kSize = 1 + OperandTraits<OperandType::kNone>::kSize; |
| 135 }; |
| 136 |
| 137 } // namespace internal |
| 138 } // namespace interpreter |
| 139 } // namespace internal |
| 140 } // namespace v8 |
| 141 |
| 142 #endif // V8_INTERPRETER_BYTECODE_TRAITS_H_ |
OLD | NEW |