OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERPRETER_BYTECODE_TRAITS_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_TRAITS_H_ |
6 #define V8_INTERPRETER_BYTECODE_TRAITS_H_ | 6 #define V8_INTERPRETER_BYTECODE_TRAITS_H_ |
7 | 7 |
8 #include "src/interpreter/bytecodes.h" | 8 #include "src/interpreter/bytecodes.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 namespace interpreter { | 12 namespace interpreter { |
13 | 13 |
14 // TODO(rmcilroy): consider simplifying this to avoid the template magic. | 14 // TODO(rmcilroy): consider simplifying this to avoid the template magic. |
15 | 15 |
16 // Template helpers to deduce the number of operands each bytecode has. | 16 // Template helpers to deduce the number of operands each bytecode has. |
17 #define OPERAND_TERM OperandType::kNone, OperandType::kNone, OperandType::kNone | 17 #define OPERAND_TERM OperandType::kNone, OperandType::kNone, OperandType::kNone |
18 | 18 |
| 19 template <OperandTypeInfo> |
| 20 struct OperandTypeInfoTraits { |
| 21 static const bool kIsScalable = false; |
| 22 static const bool kIsUnsigned = false; |
| 23 static const OperandSize kUnscaledSize = OperandSize::kNone; |
| 24 }; |
| 25 |
| 26 #define DECLARE_OPERAND_TYPE_INFO(Name, Scalable, Unsigned, BaseSize) \ |
| 27 template <> \ |
| 28 struct OperandTypeInfoTraits<OperandTypeInfo::k##Name> { \ |
| 29 static const bool kIsScalable = Scalable; \ |
| 30 static const bool kIsUnsigned = Unsigned; \ |
| 31 static const OperandSize kUnscaledSize = BaseSize; \ |
| 32 }; |
| 33 OPERAND_TYPE_INFO_LIST(DECLARE_OPERAND_TYPE_INFO) |
| 34 #undef DECLARE_OPERAND_TYPE_INFO |
| 35 |
19 template <OperandType> | 36 template <OperandType> |
20 struct OperandTraits {}; | 37 struct OperandTraits { |
| 38 typedef OperandTypeInfoTraits<OperandTypeInfo::kNone> TypeInfo; |
| 39 }; |
21 | 40 |
22 #define DECLARE_OPERAND_SIZE(Name, Size) \ | 41 #define DECLARE_OPERAND_TYPE_TRAITS(Name, InfoType) \ |
23 template <> \ | 42 template <> \ |
24 struct OperandTraits<OperandType::k##Name> { \ | 43 struct OperandTraits<OperandType::k##Name> { \ |
25 static const OperandSize kSizeType = Size; \ | 44 typedef OperandTypeInfoTraits<InfoType> TypeInfo; \ |
26 static const int kSize = static_cast<int>(Size); \ | |
27 }; | 45 }; |
28 OPERAND_TYPE_LIST(DECLARE_OPERAND_SIZE) | 46 OPERAND_TYPE_LIST(DECLARE_OPERAND_TYPE_TRAITS) |
29 #undef DECLARE_OPERAND_SIZE | 47 #undef DECLARE_OPERAND_TYPE_TRAITS |
30 | 48 |
31 template <OperandType> | 49 template <OperandType> |
32 struct RegisterOperandTraits { | 50 struct RegisterOperandTraits { |
33 static const int kIsRegisterOperand = 0; | 51 static const int kIsRegisterOperand = 0; |
34 }; | 52 }; |
35 | 53 |
36 #define DECLARE_REGISTER_OPERAND(Name, _) \ | 54 #define DECLARE_REGISTER_OPERAND(Name, _) \ |
37 template <> \ | 55 template <> \ |
38 struct RegisterOperandTraits<OperandType::k##Name> { \ | 56 struct RegisterOperandTraits<OperandType::k##Name> { \ |
39 static const int kIsRegisterOperand = 1; \ | 57 static const int kIsRegisterOperand = 1; \ |
40 }; | 58 }; |
41 REGISTER_OPERAND_TYPE_LIST(DECLARE_REGISTER_OPERAND) | 59 REGISTER_OPERAND_TYPE_LIST(DECLARE_REGISTER_OPERAND) |
42 #undef DECLARE_REGISTER_OPERAND | 60 #undef DECLARE_REGISTER_OPERAND |
43 | 61 |
44 template <OperandType... Args> | 62 template <OperandType... Args> |
45 struct BytecodeTraits {}; | 63 struct BytecodeTraits {}; |
46 | 64 |
47 template <OperandType operand_0, OperandType operand_1, OperandType operand_2, | 65 template <OperandType operand_0, OperandType operand_1, OperandType operand_2, |
48 OperandType operand_3> | 66 OperandType operand_3> |
49 struct BytecodeTraits<operand_0, operand_1, operand_2, operand_3, | 67 struct BytecodeTraits<operand_0, operand_1, operand_2, operand_3, |
50 OPERAND_TERM> { | 68 OPERAND_TERM> { |
51 static OperandType GetOperandType(int i) { | 69 static OperandType GetOperandType(int i) { |
52 DCHECK(0 <= i && i < kOperandCount); | 70 DCHECK(0 <= i && i < kOperandCount); |
53 const OperandType kOperands[] = {operand_0, operand_1, operand_2, | 71 const OperandType kOperands[] = {operand_0, operand_1, operand_2, |
54 operand_3}; | 72 operand_3}; |
55 return kOperands[i]; | 73 return kOperands[i]; |
56 } | 74 } |
57 | 75 |
58 static inline OperandSize GetOperandSize(int i) { | |
59 DCHECK(0 <= i && i < kOperandCount); | |
60 const OperandSize kOperandSizes[] = | |
61 {OperandTraits<operand_0>::kSizeType, | |
62 OperandTraits<operand_1>::kSizeType, | |
63 OperandTraits<operand_2>::kSizeType, | |
64 OperandTraits<operand_3>::kSizeType}; | |
65 return kOperandSizes[i]; | |
66 } | |
67 | |
68 static inline int GetOperandOffset(int i) { | |
69 DCHECK(0 <= i && i < kOperandCount); | |
70 const int kOffset0 = 1; | |
71 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; | |
72 const int kOffset2 = kOffset1 + OperandTraits<operand_1>::kSize; | |
73 const int kOffset3 = kOffset2 + OperandTraits<operand_2>::kSize; | |
74 const int kOperandOffsets[] = {kOffset0, kOffset1, kOffset2, kOffset3}; | |
75 return kOperandOffsets[i]; | |
76 } | |
77 | |
78 template <OperandType ot> | 76 template <OperandType ot> |
79 static inline bool HasAnyOperandsOfType() { | 77 static inline bool HasAnyOperandsOfType() { |
80 return operand_0 == ot || operand_1 == ot || operand_2 == ot || | 78 return operand_0 == ot || operand_1 == ot || operand_2 == ot || |
81 operand_3 == ot; | 79 operand_3 == ot; |
82 } | 80 } |
83 | 81 |
| 82 static inline bool IsScalable() { |
| 83 return (OperandTraits<operand_0>::TypeInfo::kIsScalable | |
| 84 OperandTraits<operand_1>::TypeInfo::kIsScalable | |
| 85 OperandTraits<operand_2>::TypeInfo::kIsScalable | |
| 86 OperandTraits<operand_3>::TypeInfo::kIsScalable); |
| 87 } |
| 88 |
84 static const int kOperandCount = 4; | 89 static const int kOperandCount = 4; |
85 static const int kRegisterOperandCount = | 90 static const int kRegisterOperandCount = |
86 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 91 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
87 RegisterOperandTraits<operand_1>::kIsRegisterOperand + | 92 RegisterOperandTraits<operand_1>::kIsRegisterOperand + |
88 RegisterOperandTraits<operand_2>::kIsRegisterOperand + | 93 RegisterOperandTraits<operand_2>::kIsRegisterOperand + |
89 RegisterOperandTraits<operand_3>::kIsRegisterOperand; | 94 RegisterOperandTraits<operand_3>::kIsRegisterOperand; |
90 static const int kRegisterOperandBitmap = | 95 static const int kRegisterOperandBitmap = |
91 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 96 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
92 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) + | 97 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) + |
93 (RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2) + | 98 (RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2) + |
94 (RegisterOperandTraits<operand_3>::kIsRegisterOperand << 3); | 99 (RegisterOperandTraits<operand_3>::kIsRegisterOperand << 3); |
95 static const int kSize = | |
96 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize + | |
97 OperandTraits<operand_2>::kSize + OperandTraits<operand_3>::kSize; | |
98 }; | 100 }; |
99 | 101 |
100 template <OperandType operand_0, OperandType operand_1, OperandType operand_2> | 102 template <OperandType operand_0, OperandType operand_1, OperandType operand_2> |
101 struct BytecodeTraits<operand_0, operand_1, operand_2, OPERAND_TERM> { | 103 struct BytecodeTraits<operand_0, operand_1, operand_2, OPERAND_TERM> { |
102 static inline OperandType GetOperandType(int i) { | 104 static inline OperandType GetOperandType(int i) { |
103 DCHECK(0 <= i && i <= 2); | 105 DCHECK(0 <= i && i <= 2); |
104 const OperandType kOperands[] = {operand_0, operand_1, operand_2}; | 106 const OperandType kOperands[] = {operand_0, operand_1, operand_2}; |
105 return kOperands[i]; | 107 return kOperands[i]; |
106 } | 108 } |
107 | 109 |
108 static inline OperandSize GetOperandSize(int i) { | |
109 DCHECK(0 <= i && i < kOperandCount); | |
110 const OperandSize kOperandSizes[] = | |
111 {OperandTraits<operand_0>::kSizeType, | |
112 OperandTraits<operand_1>::kSizeType, | |
113 OperandTraits<operand_2>::kSizeType}; | |
114 return kOperandSizes[i]; | |
115 } | |
116 | |
117 static inline int GetOperandOffset(int i) { | |
118 DCHECK(0 <= i && i < kOperandCount); | |
119 const int kOffset0 = 1; | |
120 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; | |
121 const int kOffset2 = kOffset1 + OperandTraits<operand_1>::kSize; | |
122 const int kOperandOffsets[] = {kOffset0, kOffset1, kOffset2}; | |
123 return kOperandOffsets[i]; | |
124 } | |
125 | |
126 template <OperandType ot> | 110 template <OperandType ot> |
127 static inline bool HasAnyOperandsOfType() { | 111 static inline bool HasAnyOperandsOfType() { |
128 return operand_0 == ot || operand_1 == ot || operand_2 == ot; | 112 return operand_0 == ot || operand_1 == ot || operand_2 == ot; |
129 } | 113 } |
130 | 114 |
| 115 static inline bool IsScalable() { |
| 116 return (OperandTraits<operand_0>::TypeInfo::kIsScalable | |
| 117 OperandTraits<operand_1>::TypeInfo::kIsScalable | |
| 118 OperandTraits<operand_2>::TypeInfo::kIsScalable); |
| 119 } |
| 120 |
131 static const int kOperandCount = 3; | 121 static const int kOperandCount = 3; |
132 static const int kRegisterOperandCount = | 122 static const int kRegisterOperandCount = |
133 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 123 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
134 RegisterOperandTraits<operand_1>::kIsRegisterOperand + | 124 RegisterOperandTraits<operand_1>::kIsRegisterOperand + |
135 RegisterOperandTraits<operand_2>::kIsRegisterOperand; | 125 RegisterOperandTraits<operand_2>::kIsRegisterOperand; |
136 static const int kRegisterOperandBitmap = | 126 static const int kRegisterOperandBitmap = |
137 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 127 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
138 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) + | 128 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) + |
139 (RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2); | 129 (RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2); |
140 static const int kSize = | |
141 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize + | |
142 OperandTraits<operand_2>::kSize; | |
143 }; | 130 }; |
144 | 131 |
145 template <OperandType operand_0, OperandType operand_1> | 132 template <OperandType operand_0, OperandType operand_1> |
146 struct BytecodeTraits<operand_0, operand_1, OPERAND_TERM> { | 133 struct BytecodeTraits<operand_0, operand_1, OPERAND_TERM> { |
147 static inline OperandType GetOperandType(int i) { | 134 static inline OperandType GetOperandType(int i) { |
148 DCHECK(0 <= i && i < kOperandCount); | 135 DCHECK(0 <= i && i < kOperandCount); |
149 const OperandType kOperands[] = {operand_0, operand_1}; | 136 const OperandType kOperands[] = {operand_0, operand_1}; |
150 return kOperands[i]; | 137 return kOperands[i]; |
151 } | 138 } |
152 | 139 |
153 static inline OperandSize GetOperandSize(int i) { | |
154 DCHECK(0 <= i && i < kOperandCount); | |
155 const OperandSize kOperandSizes[] = | |
156 {OperandTraits<operand_0>::kSizeType, | |
157 OperandTraits<operand_1>::kSizeType}; | |
158 return kOperandSizes[i]; | |
159 } | |
160 | |
161 static inline int GetOperandOffset(int i) { | |
162 DCHECK(0 <= i && i < kOperandCount); | |
163 const int kOffset0 = 1; | |
164 const int kOffset1 = kOffset0 + OperandTraits<operand_0>::kSize; | |
165 const int kOperandOffsets[] = {kOffset0, kOffset1}; | |
166 return kOperandOffsets[i]; | |
167 } | |
168 | |
169 template <OperandType ot> | 140 template <OperandType ot> |
170 static inline bool HasAnyOperandsOfType() { | 141 static inline bool HasAnyOperandsOfType() { |
171 return operand_0 == ot || operand_1 == ot; | 142 return operand_0 == ot || operand_1 == ot; |
172 } | 143 } |
173 | 144 |
| 145 static inline bool IsScalable() { |
| 146 return (OperandTraits<operand_0>::TypeInfo::kIsScalable | |
| 147 OperandTraits<operand_1>::TypeInfo::kIsScalable); |
| 148 } |
| 149 |
174 static const int kOperandCount = 2; | 150 static const int kOperandCount = 2; |
175 static const int kRegisterOperandCount = | 151 static const int kRegisterOperandCount = |
176 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 152 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
177 RegisterOperandTraits<operand_1>::kIsRegisterOperand; | 153 RegisterOperandTraits<operand_1>::kIsRegisterOperand; |
178 static const int kRegisterOperandBitmap = | 154 static const int kRegisterOperandBitmap = |
179 RegisterOperandTraits<operand_0>::kIsRegisterOperand + | 155 RegisterOperandTraits<operand_0>::kIsRegisterOperand + |
180 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1); | 156 (RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1); |
181 static const int kSize = | |
182 1 + OperandTraits<operand_0>::kSize + OperandTraits<operand_1>::kSize; | |
183 }; | 157 }; |
184 | 158 |
185 template <OperandType operand_0> | 159 template <OperandType operand_0> |
186 struct BytecodeTraits<operand_0, OPERAND_TERM> { | 160 struct BytecodeTraits<operand_0, OPERAND_TERM> { |
187 static inline OperandType GetOperandType(int i) { | 161 static inline OperandType GetOperandType(int i) { |
188 DCHECK(i == 0); | 162 DCHECK(i == 0); |
189 return operand_0; | 163 return operand_0; |
190 } | 164 } |
191 | 165 |
192 static inline OperandSize GetOperandSize(int i) { | |
193 DCHECK(i == 0); | |
194 return OperandTraits<operand_0>::kSizeType; | |
195 } | |
196 | |
197 static inline int GetOperandOffset(int i) { | |
198 DCHECK(i == 0); | |
199 return 1; | |
200 } | |
201 | |
202 template <OperandType ot> | 166 template <OperandType ot> |
203 static inline bool HasAnyOperandsOfType() { | 167 static inline bool HasAnyOperandsOfType() { |
204 return operand_0 == ot; | 168 return operand_0 == ot; |
205 } | 169 } |
206 | 170 |
| 171 static inline bool IsScalable() { |
| 172 return OperandTraits<operand_0>::TypeInfo::kIsScalable; |
| 173 } |
| 174 |
207 static const int kOperandCount = 1; | 175 static const int kOperandCount = 1; |
208 static const int kRegisterOperandCount = | 176 static const int kRegisterOperandCount = |
209 RegisterOperandTraits<operand_0>::kIsRegisterOperand; | 177 RegisterOperandTraits<operand_0>::kIsRegisterOperand; |
210 static const int kRegisterOperandBitmap = | 178 static const int kRegisterOperandBitmap = |
211 RegisterOperandTraits<operand_0>::kIsRegisterOperand; | 179 RegisterOperandTraits<operand_0>::kIsRegisterOperand; |
212 static const int kSize = 1 + OperandTraits<operand_0>::kSize; | |
213 }; | 180 }; |
214 | 181 |
215 template <> | 182 template <> |
216 struct BytecodeTraits<OperandType::kNone, OPERAND_TERM> { | 183 struct BytecodeTraits<OperandType::kNone, OPERAND_TERM> { |
217 static inline OperandType GetOperandType(int i) { | 184 static inline OperandType GetOperandType(int i) { |
218 UNREACHABLE(); | 185 UNREACHABLE(); |
219 return OperandType::kNone; | 186 return OperandType::kNone; |
220 } | 187 } |
221 | 188 |
222 static inline OperandSize GetOperandSize(int i) { | |
223 UNREACHABLE(); | |
224 return OperandSize::kNone; | |
225 } | |
226 | |
227 static inline int GetOperandOffset(int i) { | |
228 UNREACHABLE(); | |
229 return 1; | |
230 } | |
231 | |
232 template <OperandType ot> | 189 template <OperandType ot> |
233 static inline bool HasAnyOperandsOfType() { | 190 static inline bool HasAnyOperandsOfType() { |
234 return false; | 191 return false; |
235 } | 192 } |
236 | 193 |
| 194 static inline bool IsScalable() { return false; } |
| 195 |
237 static const int kOperandCount = 0; | 196 static const int kOperandCount = 0; |
238 static const int kRegisterOperandCount = 0; | 197 static const int kRegisterOperandCount = 0; |
239 static const int kRegisterOperandBitmap = 0; | 198 static const int kRegisterOperandBitmap = 0; |
240 static const int kSize = 1 + OperandTraits<OperandType::kNone>::kSize; | |
241 }; | 199 }; |
242 | 200 |
243 } // namespace interpreter | 201 } // namespace interpreter |
244 } // namespace internal | 202 } // namespace internal |
245 } // namespace v8 | 203 } // namespace v8 |
246 | 204 |
247 #endif // V8_INTERPRETER_BYTECODE_TRAITS_H_ | 205 #endif // V8_INTERPRETER_BYTECODE_TRAITS_H_ |
OLD | NEW |