Line data Source code
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 : #include "src/wasm/wasm-opcodes.h"
6 :
7 : #include <array>
8 :
9 : #include "src/base/template-utils.h"
10 : #include "src/messages.h"
11 : #include "src/runtime/runtime.h"
12 : #include "src/signature.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace wasm {
17 :
18 : #define CASE_OP(name, str) \
19 : case kExpr##name: \
20 : return str;
21 : #define CASE_I32_OP(name, str) CASE_OP(I32##name, "i32." str)
22 : #define CASE_I64_OP(name, str) CASE_OP(I64##name, "i64." str)
23 : #define CASE_F32_OP(name, str) CASE_OP(F32##name, "f32." str)
24 : #define CASE_F64_OP(name, str) CASE_OP(F64##name, "f64." str)
25 : #define CASE_REF_OP(name, str) CASE_OP(Ref##name, "ref." str)
26 : #define CASE_F32x4_OP(name, str) CASE_OP(F32x4##name, "f32x4." str)
27 : #define CASE_I32x4_OP(name, str) CASE_OP(I32x4##name, "i32x4." str)
28 : #define CASE_I16x8_OP(name, str) CASE_OP(I16x8##name, "i16x8." str)
29 : #define CASE_I8x16_OP(name, str) CASE_OP(I8x16##name, "i8x16." str)
30 : #define CASE_S128_OP(name, str) CASE_OP(S128##name, "s128." str)
31 : #define CASE_S32x4_OP(name, str) CASE_OP(S32x4##name, "s32x4." str)
32 : #define CASE_S16x8_OP(name, str) CASE_OP(S16x8##name, "s16x8." str)
33 : #define CASE_S8x16_OP(name, str) CASE_OP(S8x16##name, "s8x16." str)
34 : #define CASE_S1x4_OP(name, str) CASE_OP(S1x4##name, "s1x4." str)
35 : #define CASE_S1x8_OP(name, str) CASE_OP(S1x8##name, "s1x8." str)
36 : #define CASE_S1x16_OP(name, str) CASE_OP(S1x16##name, "s1x16." str)
37 : #define CASE_INT_OP(name, str) CASE_I32_OP(name, str) CASE_I64_OP(name, str)
38 : #define CASE_FLOAT_OP(name, str) CASE_F32_OP(name, str) CASE_F64_OP(name, str)
39 : #define CASE_ALL_OP(name, str) CASE_FLOAT_OP(name, str) CASE_INT_OP(name, str)
40 : #define CASE_SIMD_OP(name, str) \
41 : CASE_F32x4_OP(name, str) CASE_I32x4_OP(name, str) CASE_I16x8_OP(name, str) \
42 : CASE_I8x16_OP(name, str)
43 : #define CASE_SIMDI_OP(name, str) \
44 : CASE_I32x4_OP(name, str) CASE_I16x8_OP(name, str) CASE_I8x16_OP(name, str)
45 : #define CASE_SIGN_OP(TYPE, name, str) \
46 : CASE_##TYPE##_OP(name##S, str "_s") CASE_##TYPE##_OP(name##U, str "_u")
47 : #define CASE_UNSIGNED_OP(TYPE, name, str) CASE_##TYPE##_OP(name##U, str "_u")
48 : #define CASE_ALL_SIGN_OP(name, str) \
49 : CASE_FLOAT_OP(name, str) CASE_SIGN_OP(INT, name, str)
50 : #define CASE_CONVERT_OP(name, RES, SRC, src_suffix, str) \
51 : CASE_##RES##_OP(U##name##SRC, str "_u/" src_suffix) \
52 : CASE_##RES##_OP(S##name##SRC, str "_s/" src_suffix)
53 : #define CASE_CONVERT_SAT_OP(name, RES, SRC, src_suffix, str) \
54 : CASE_##RES##_OP(U##name##Sat##SRC, str "_u:sat/" src_suffix) \
55 : CASE_##RES##_OP(S##name##Sat##SRC, str "_s:sat/" src_suffix)
56 : #define CASE_L32_OP(name, str) \
57 : CASE_SIGN_OP(I32, name##8, str "8") \
58 : CASE_SIGN_OP(I32, name##16, str "16") \
59 : CASE_I32_OP(name, str "32")
60 : #define CASE_U32_OP(name, str) \
61 : CASE_I32_OP(name, str "32") \
62 : CASE_UNSIGNED_OP(I32, name##8, str "8") \
63 : CASE_UNSIGNED_OP(I32, name##16, str "16")
64 : #define CASE_UNSIGNED_ALL_OP(name, str) \
65 : CASE_U32_OP(name, str) \
66 : CASE_I64_OP(name, str "64") \
67 : CASE_UNSIGNED_OP(I64, name##8, str "8") \
68 : CASE_UNSIGNED_OP(I64, name##16, str "16") \
69 : CASE_UNSIGNED_OP(I64, name##32, str "32")
70 :
71 59988 : const char* WasmOpcodes::OpcodeName(WasmOpcode opcode) {
72 59988 : switch (opcode) {
73 : // clang-format off
74 :
75 : // Standard opcodes
76 69 : CASE_INT_OP(Eqz, "eqz")
77 553 : CASE_ALL_OP(Eq, "eq")
78 145 : CASE_ALL_OP(Ne, "ne")
79 269 : CASE_ALL_OP(Add, "add")
80 265 : CASE_ALL_OP(Sub, "sub")
81 265 : CASE_ALL_OP(Mul, "mul")
82 265 : CASE_ALL_SIGN_OP(Lt, "lt")
83 145 : CASE_ALL_SIGN_OP(Gt, "gt")
84 265 : CASE_ALL_SIGN_OP(Le, "le")
85 145 : CASE_ALL_SIGN_OP(Ge, "ge")
86 45 : CASE_INT_OP(Clz, "clz")
87 213 : CASE_INT_OP(Ctz, "ctz")
88 45 : CASE_INT_OP(Popcnt, "popcnt")
89 265 : CASE_ALL_SIGN_OP(Div, "div")
90 268 : CASE_SIGN_OP(INT, Rem, "rem")
91 269 : CASE_INT_OP(And, "and")
92 268 : CASE_INT_OP(Ior, "or")
93 268 : CASE_INT_OP(Xor, "xor")
94 268 : CASE_INT_OP(Shl, "shl")
95 268 : CASE_SIGN_OP(INT, Shr, "shr")
96 145 : CASE_INT_OP(Rol, "rol")
97 145 : CASE_INT_OP(Ror, "ror")
98 45 : CASE_FLOAT_OP(Abs, "abs")
99 95 : CASE_FLOAT_OP(Neg, "neg")
100 45 : CASE_FLOAT_OP(Ceil, "ceil")
101 165 : CASE_FLOAT_OP(Floor, "floor")
102 45 : CASE_FLOAT_OP(Trunc, "trunc")
103 45 : CASE_FLOAT_OP(NearestInt, "nearest")
104 45 : CASE_FLOAT_OP(Sqrt, "sqrt")
105 145 : CASE_FLOAT_OP(Min, "min")
106 145 : CASE_FLOAT_OP(Max, "max")
107 145 : CASE_FLOAT_OP(CopySign, "copysign")
108 1 : CASE_REF_OP(Null, "null")
109 1 : CASE_REF_OP(IsNull, "is_null")
110 45 : CASE_I32_OP(ConvertI64, "wrap/i64")
111 65 : CASE_CONVERT_OP(Convert, INT, F32, "f32", "trunc")
112 65 : CASE_CONVERT_OP(Convert, INT, F64, "f64", "trunc")
113 69 : CASE_CONVERT_OP(Convert, I64, I32, "i32", "extend")
114 65 : CASE_CONVERT_OP(Convert, F32, I32, "i32", "convert")
115 45 : CASE_CONVERT_OP(Convert, F32, I64, "i64", "convert")
116 65 : CASE_F32_OP(ConvertF64, "demote/f64")
117 73 : CASE_CONVERT_OP(Convert, F64, I32, "i32", "convert")
118 45 : CASE_CONVERT_OP(Convert, F64, I64, "i64", "convert")
119 65 : CASE_F64_OP(ConvertF32, "promote/f32")
120 45 : CASE_I32_OP(ReinterpretF32, "reinterpret/f32")
121 45 : CASE_I64_OP(ReinterpretF64, "reinterpret/f64")
122 45 : CASE_F32_OP(ReinterpretI32, "reinterpret/i32")
123 45 : CASE_F64_OP(ReinterpretI64, "reinterpret/i64")
124 1 : CASE_INT_OP(SExtendI8, "sign_extend8")
125 1 : CASE_INT_OP(SExtendI16, "sign_extend16")
126 1 : CASE_I64_OP(SExtendI32, "sign_extend32")
127 1 : CASE_OP(Unreachable, "unreachable")
128 33 : CASE_OP(Nop, "nop")
129 24 : CASE_OP(Block, "block")
130 12 : CASE_OP(Loop, "loop")
131 170 : CASE_OP(If, "if")
132 1 : CASE_OP(Else, "else")
133 1 : CASE_OP(End, "end")
134 9 : CASE_OP(Br, "br")
135 237 : CASE_OP(BrIf, "br_if")
136 122 : CASE_OP(BrTable, "br_table")
137 1 : CASE_OP(Return, "return")
138 240 : CASE_OP(CallFunction, "call")
139 269 : CASE_OP(CallIndirect, "call_indirect")
140 10 : CASE_OP(ReturnCall, "return_call")
141 1 : CASE_OP(ReturnCallIndirect, "return_call_indirect")
142 29 : CASE_OP(Drop, "drop")
143 164 : CASE_OP(Select, "select")
144 25901 : CASE_OP(GetLocal, "get_local")
145 621 : CASE_OP(SetLocal, "set_local")
146 751 : CASE_OP(TeeLocal, "tee_local")
147 1 : CASE_OP(GetGlobal, "get_global")
148 316 : CASE_OP(SetGlobal, "set_global")
149 5 : CASE_OP(GetTable, "get_table")
150 5 : CASE_OP(SetTable, "set_table")
151 2961 : CASE_ALL_OP(Const, "const")
152 1 : CASE_OP(MemorySize, "memory.size")
153 26 : CASE_OP(MemoryGrow, "memory.grow")
154 25 : CASE_ALL_OP(LoadMem, "load")
155 25 : CASE_SIGN_OP(INT, LoadMem8, "load8")
156 25 : CASE_SIGN_OP(INT, LoadMem16, "load16")
157 25 : CASE_SIGN_OP(I64, LoadMem32, "load32")
158 1 : CASE_S128_OP(LoadMem, "load128")
159 53 : CASE_ALL_OP(StoreMem, "store")
160 57 : CASE_INT_OP(StoreMem8, "store8")
161 57 : CASE_INT_OP(StoreMem16, "store16")
162 49 : CASE_I64_OP(StoreMem32, "store32")
163 1 : CASE_S128_OP(StoreMem, "store128")
164 :
165 : // Exception handling opcodes.
166 1 : CASE_OP(Try, "try")
167 1 : CASE_OP(Catch, "catch")
168 3 : CASE_OP(Throw, "throw")
169 4 : CASE_OP(Rethrow, "rethrow")
170 2 : CASE_OP(BrOnExn, "br_on_exn")
171 :
172 : // asm.js-only opcodes.
173 21 : CASE_F64_OP(Acos, "acos")
174 21 : CASE_F64_OP(Asin, "asin")
175 21 : CASE_F64_OP(Atan, "atan")
176 21 : CASE_F64_OP(Cos, "cos")
177 21 : CASE_F64_OP(Sin, "sin")
178 21 : CASE_F64_OP(Tan, "tan")
179 21 : CASE_F64_OP(Exp, "exp")
180 21 : CASE_F64_OP(Log, "log")
181 121 : CASE_F64_OP(Atan2, "atan2")
182 121 : CASE_F64_OP(Pow, "pow")
183 121 : CASE_F64_OP(Mod, "mod")
184 25 : CASE_F32_OP(AsmjsLoadMem, "asmjs_load")
185 25 : CASE_F64_OP(AsmjsLoadMem, "asmjs_load")
186 21 : CASE_L32_OP(AsmjsLoadMem, "asmjs_load")
187 125 : CASE_I32_OP(AsmjsStoreMem, "asmjs_store")
188 121 : CASE_F32_OP(AsmjsStoreMem, "asmjs_store")
189 121 : CASE_F64_OP(AsmjsStoreMem, "asmjs_store")
190 121 : CASE_I32_OP(AsmjsStoreMem8, "asmjs_store8")
191 121 : CASE_I32_OP(AsmjsStoreMem16, "asmjs_store16")
192 165 : CASE_SIGN_OP(I32, AsmjsDiv, "asmjs_div")
193 165 : CASE_SIGN_OP(I32, AsmjsRem, "asmjs_rem")
194 25 : CASE_I32_OP(AsmjsSConvertF32, "asmjs_convert_s/f32")
195 25 : CASE_I32_OP(AsmjsUConvertF32, "asmjs_convert_u/f32")
196 25 : CASE_I32_OP(AsmjsSConvertF64, "asmjs_convert_s/f64")
197 25 : CASE_I32_OP(AsmjsUConvertF64, "asmjs_convert_u/f64")
198 :
199 : // Numeric Opcodes.
200 5 : CASE_CONVERT_SAT_OP(Convert, I32, F32, "f32", "trunc")
201 5 : CASE_CONVERT_SAT_OP(Convert, I32, F64, "f64", "trunc")
202 5 : CASE_CONVERT_SAT_OP(Convert, I64, F32, "f32", "trunc")
203 5 : CASE_CONVERT_SAT_OP(Convert, I64, F64, "f64", "trunc")
204 1 : CASE_OP(MemoryInit, "memory.init")
205 1 : CASE_OP(DataDrop, "data.drop")
206 1 : CASE_OP(MemoryCopy, "memory.copy")
207 1 : CASE_OP(MemoryFill, "memory.fill")
208 1 : CASE_OP(TableInit, "table.init")
209 1 : CASE_OP(ElemDrop, "elem.drop")
210 1 : CASE_OP(TableCopy, "table.copy")
211 :
212 : // SIMD opcodes.
213 1 : CASE_SIMD_OP(Splat, "splat")
214 1 : CASE_SIMD_OP(Neg, "neg")
215 1 : CASE_SIMD_OP(Eq, "eq")
216 1 : CASE_SIMD_OP(Ne, "ne")
217 1 : CASE_SIMD_OP(Add, "add")
218 1 : CASE_SIMD_OP(Sub, "sub")
219 1 : CASE_SIMD_OP(Mul, "mul")
220 1 : CASE_F32x4_OP(Abs, "abs")
221 1 : CASE_F32x4_OP(AddHoriz, "add_horizontal")
222 1 : CASE_F32x4_OP(RecipApprox, "recip_approx")
223 1 : CASE_F32x4_OP(RecipSqrtApprox, "recip_sqrt_approx")
224 1 : CASE_F32x4_OP(Min, "min")
225 1 : CASE_F32x4_OP(Max, "max")
226 1 : CASE_F32x4_OP(Lt, "lt")
227 1 : CASE_F32x4_OP(Le, "le")
228 1 : CASE_F32x4_OP(Gt, "gt")
229 1 : CASE_F32x4_OP(Ge, "ge")
230 1 : CASE_CONVERT_OP(Convert, F32x4, I32x4, "i32", "convert")
231 1 : CASE_CONVERT_OP(Convert, I32x4, F32x4, "f32", "convert")
232 1 : CASE_CONVERT_OP(Convert, I32x4, I16x8Low, "i32", "convert")
233 1 : CASE_CONVERT_OP(Convert, I32x4, I16x8High, "i32", "convert")
234 1 : CASE_CONVERT_OP(Convert, I16x8, I32x4, "i32", "convert")
235 1 : CASE_CONVERT_OP(Convert, I16x8, I8x16Low, "i32", "convert")
236 1 : CASE_CONVERT_OP(Convert, I16x8, I8x16High, "i32", "convert")
237 1 : CASE_CONVERT_OP(Convert, I8x16, I16x8, "i32", "convert")
238 1 : CASE_F32x4_OP(ExtractLane, "extract_lane")
239 1 : CASE_F32x4_OP(ReplaceLane, "replace_lane")
240 1 : CASE_SIMDI_OP(ExtractLane, "extract_lane")
241 1 : CASE_SIMDI_OP(ReplaceLane, "replace_lane")
242 1 : CASE_SIGN_OP(SIMDI, Min, "min")
243 1 : CASE_SIGN_OP(SIMDI, Max, "max")
244 1 : CASE_SIGN_OP(SIMDI, Lt, "lt")
245 1 : CASE_SIGN_OP(SIMDI, Le, "le")
246 1 : CASE_SIGN_OP(SIMDI, Gt, "gt")
247 1 : CASE_SIGN_OP(SIMDI, Ge, "ge")
248 1 : CASE_SIGN_OP(SIMDI, Shr, "shr")
249 1 : CASE_SIMDI_OP(Shl, "shl")
250 1 : CASE_I32x4_OP(AddHoriz, "add_horizontal")
251 1 : CASE_I16x8_OP(AddHoriz, "add_horizontal")
252 1 : CASE_SIGN_OP(I16x8, AddSaturate, "add_saturate")
253 1 : CASE_SIGN_OP(I8x16, AddSaturate, "add_saturate")
254 1 : CASE_SIGN_OP(I16x8, SubSaturate, "sub_saturate")
255 1 : CASE_SIGN_OP(I8x16, SubSaturate, "sub_saturate")
256 1 : CASE_S128_OP(And, "and")
257 1 : CASE_S128_OP(Or, "or")
258 1 : CASE_S128_OP(Xor, "xor")
259 1 : CASE_S128_OP(Not, "not")
260 1 : CASE_S128_OP(Select, "select")
261 1 : CASE_S8x16_OP(Shuffle, "shuffle")
262 1 : CASE_S1x4_OP(AnyTrue, "any_true")
263 1 : CASE_S1x4_OP(AllTrue, "all_true")
264 1 : CASE_S1x8_OP(AnyTrue, "any_true")
265 1 : CASE_S1x8_OP(AllTrue, "all_true")
266 1 : CASE_S1x16_OP(AnyTrue, "any_true")
267 1 : CASE_S1x16_OP(AllTrue, "all_true")
268 :
269 : // Atomic operations.
270 1 : CASE_OP(AtomicWake, "atomic_wake")
271 1 : CASE_INT_OP(AtomicWait, "atomic_wait")
272 5 : CASE_UNSIGNED_ALL_OP(AtomicLoad, "atomic_load")
273 5 : CASE_UNSIGNED_ALL_OP(AtomicStore, "atomic_store")
274 1 : CASE_UNSIGNED_ALL_OP(AtomicAdd, "atomic_add")
275 1 : CASE_UNSIGNED_ALL_OP(AtomicSub, "atomic_sub")
276 1 : CASE_UNSIGNED_ALL_OP(AtomicAnd, "atomic_and")
277 1 : CASE_UNSIGNED_ALL_OP(AtomicOr, "atomic_or")
278 1 : CASE_UNSIGNED_ALL_OP(AtomicXor, "atomic_xor")
279 1 : CASE_UNSIGNED_ALL_OP(AtomicExchange, "atomic_xchng")
280 1 : CASE_UNSIGNED_ALL_OP(AtomicCompareExchange, "atomic_cmpxchng")
281 :
282 0 : default : return "unknown";
283 : // clang-format on
284 : }
285 : }
286 :
287 : #undef CASE_OP
288 : #undef CASE_I32_OP
289 : #undef CASE_I64_OP
290 : #undef CASE_F32_OP
291 : #undef CASE_F64_OP
292 : #undef CASE_REF_OP
293 : #undef CASE_F32x4_OP
294 : #undef CASE_I32x4_OP
295 : #undef CASE_I16x8_OP
296 : #undef CASE_I8x16_OP
297 : #undef CASE_S128_OP
298 : #undef CASE_S32x4_OP
299 : #undef CASE_S16x8_OP
300 : #undef CASE_S8x16_OP
301 : #undef CASE_S1x4_OP
302 : #undef CASE_S1x8_OP
303 : #undef CASE_S1x16_OP
304 : #undef CASE_INT_OP
305 : #undef CASE_FLOAT_OP
306 : #undef CASE_ALL_OP
307 : #undef CASE_SIMD_OP
308 : #undef CASE_SIMDI_OP
309 : #undef CASE_SIGN_OP
310 : #undef CASE_UNSIGNED_OP
311 : #undef CASE_UNSIGNED_ALL_OP
312 : #undef CASE_ALL_SIGN_OP
313 : #undef CASE_CONVERT_OP
314 : #undef CASE_CONVERT_SAT_OP
315 : #undef CASE_L32_OP
316 : #undef CASE_U32_OP
317 :
318 62635174 : bool WasmOpcodes::IsPrefixOpcode(WasmOpcode opcode) {
319 62635174 : switch (opcode) {
320 : #define CHECK_PREFIX(name, opcode) case k##name##Prefix:
321 : FOREACH_PREFIX(CHECK_PREFIX)
322 : #undef CHECK_PREFIX
323 : return true;
324 : default:
325 56902790 : return false;
326 : }
327 : }
328 :
329 0 : bool WasmOpcodes::IsControlOpcode(WasmOpcode opcode) {
330 0 : switch (opcode) {
331 : #define CHECK_OPCODE(name, opcode, _) case kExpr##name:
332 : FOREACH_CONTROL_OPCODE(CHECK_OPCODE)
333 : #undef CHECK_OPCODE
334 : return true;
335 : default:
336 0 : return false;
337 : }
338 : }
339 :
340 2072087 : bool WasmOpcodes::IsUnconditionalJump(WasmOpcode opcode) {
341 2072087 : switch (opcode) {
342 : case kExprUnreachable:
343 : case kExprBr:
344 : case kExprBrTable:
345 : case kExprReturn:
346 : return true;
347 : default:
348 2041535 : return false;
349 : }
350 : }
351 :
352 88 : bool WasmOpcodes::IsSignExtensionOpcode(WasmOpcode opcode) {
353 88 : switch (opcode) {
354 : case kExprI32SExtendI8:
355 : case kExprI32SExtendI16:
356 : case kExprI64SExtendI8:
357 : case kExprI64SExtendI16:
358 : case kExprI64SExtendI32:
359 : return true;
360 : default:
361 32 : return false;
362 : }
363 : }
364 :
365 580 : bool WasmOpcodes::IsAnyRefOpcode(WasmOpcode opcode) {
366 580 : switch (opcode) {
367 : case kExprRefNull:
368 : case kExprRefIsNull:
369 : return true;
370 : default:
371 548 : return false;
372 : }
373 : }
374 :
375 352 : bool WasmOpcodes::IsThrowingOpcode(WasmOpcode opcode) {
376 : // TODO(8729): Trapping opcodes are not yet considered to be throwing.
377 352 : switch (opcode) {
378 : case kExprThrow:
379 : case kExprRethrow:
380 : case kExprCallFunction:
381 : case kExprCallIndirect:
382 : return true;
383 : default:
384 316 : return false;
385 : }
386 : }
387 :
388 0 : std::ostream& operator<<(std::ostream& os, const FunctionSig& sig) {
389 0 : if (sig.return_count() == 0) os << "v";
390 0 : for (auto ret : sig.returns()) {
391 : os << ValueTypes::ShortNameOf(ret);
392 : }
393 0 : os << "_";
394 0 : if (sig.parameter_count() == 0) os << "v";
395 0 : for (auto param : sig.parameters()) {
396 : os << ValueTypes::ShortNameOf(param);
397 : }
398 0 : return os;
399 : }
400 :
401 157965 : bool IsJSCompatibleSignature(const FunctionSig* sig, bool has_bigint_feature) {
402 157965 : if (sig->return_count() > 1) {
403 : return false;
404 : }
405 328802 : for (auto type : sig->all()) {
406 171669 : if (!has_bigint_feature && type == kWasmI64) {
407 : return false;
408 : }
409 :
410 170837 : if (type == kWasmS128) return false;
411 : }
412 : return true;
413 : }
414 :
415 : namespace {
416 :
417 : #define DECLARE_SIG_ENUM(name, ...) kSigEnum_##name,
418 : enum WasmOpcodeSig : byte {
419 : kSigEnum_None,
420 : FOREACH_SIGNATURE(DECLARE_SIG_ENUM)
421 : };
422 : #undef DECLARE_SIG_ENUM
423 : #define DECLARE_SIG(name, ...) \
424 : constexpr ValueType kTypes_##name[] = {__VA_ARGS__}; \
425 : constexpr int kReturnsCount_##name = kTypes_##name[0] == kWasmStmt ? 0 : 1; \
426 : constexpr FunctionSig kSig_##name( \
427 : kReturnsCount_##name, static_cast<int>(arraysize(kTypes_##name)) - 1, \
428 : kTypes_##name + (1 - kReturnsCount_##name));
429 : FOREACH_SIGNATURE(DECLARE_SIG)
430 : #undef DECLARE_SIG
431 :
432 : #define DECLARE_SIG_ENTRY(name, ...) &kSig_##name,
433 : constexpr const FunctionSig* kCachedSigs[] = {
434 : nullptr, FOREACH_SIGNATURE(DECLARE_SIG_ENTRY)};
435 : #undef DECLARE_SIG_ENTRY
436 :
437 : // gcc 4.7 - 4.9 has a bug which causes the constexpr attribute to get lost when
438 : // passing functions (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52892). Hence
439 : // encapsulate these constexpr functions in functors.
440 : // TODO(clemensh): Remove this once we require gcc >= 5.0.
441 :
442 : struct GetShortOpcodeSigIndex {
443 : constexpr WasmOpcodeSig operator()(byte opcode) const {
444 : #define CASE(name, opc, sig) opcode == opc ? kSigEnum_##sig:
445 : return FOREACH_SIMPLE_OPCODE(CASE) FOREACH_SIMPLE_PROTOTYPE_OPCODE(CASE)
446 : kSigEnum_None;
447 : #undef CASE
448 : }
449 : };
450 :
451 : struct GetAsmJsOpcodeSigIndex {
452 : constexpr WasmOpcodeSig operator()(byte opcode) const {
453 : #define CASE(name, opc, sig) opcode == opc ? kSigEnum_##sig:
454 : return FOREACH_ASMJS_COMPAT_OPCODE(CASE) kSigEnum_None;
455 : #undef CASE
456 : }
457 : };
458 :
459 : struct GetSimdOpcodeSigIndex {
460 : constexpr WasmOpcodeSig operator()(byte opcode) const {
461 : #define CASE(name, opc, sig) opcode == (opc & 0xFF) ? kSigEnum_##sig:
462 : return FOREACH_SIMD_0_OPERAND_OPCODE(CASE) FOREACH_SIMD_MEM_OPCODE(CASE)
463 : kSigEnum_None;
464 : #undef CASE
465 : }
466 : };
467 :
468 : struct GetAtomicOpcodeSigIndex {
469 : constexpr WasmOpcodeSig operator()(byte opcode) const {
470 : #define CASE(name, opc, sig) opcode == (opc & 0xFF) ? kSigEnum_##sig:
471 : return FOREACH_ATOMIC_OPCODE(CASE) kSigEnum_None;
472 : #undef CASE
473 : }
474 : };
475 :
476 : struct GetNumericOpcodeSigIndex {
477 : constexpr WasmOpcodeSig operator()(byte opcode) const {
478 : #define CASE(name, opc, sig) opcode == (opc & 0xFF) ? kSigEnum_##sig:
479 : return FOREACH_NUMERIC_OPCODE(CASE) kSigEnum_None;
480 : #undef CASE
481 : }
482 : };
483 :
484 : constexpr std::array<WasmOpcodeSig, 256> kShortSigTable =
485 : base::make_array<256>(GetShortOpcodeSigIndex{});
486 : constexpr std::array<WasmOpcodeSig, 256> kSimpleAsmjsExprSigTable =
487 : base::make_array<256>(GetAsmJsOpcodeSigIndex{});
488 : constexpr std::array<WasmOpcodeSig, 256> kSimdExprSigTable =
489 : base::make_array<256>(GetSimdOpcodeSigIndex{});
490 : constexpr std::array<WasmOpcodeSig, 256> kAtomicExprSigTable =
491 : base::make_array<256>(GetAtomicOpcodeSigIndex{});
492 : constexpr std::array<WasmOpcodeSig, 256> kNumericExprSigTable =
493 : base::make_array<256>(GetNumericOpcodeSigIndex{});
494 :
495 : } // namespace
496 :
497 2096121 : FunctionSig* WasmOpcodes::Signature(WasmOpcode opcode) {
498 2096121 : switch (opcode >> 8) {
499 : case 0:
500 2041686 : return const_cast<FunctionSig*>(kCachedSigs[kShortSigTable[opcode]]);
501 : case kSimdPrefix:
502 : return const_cast<FunctionSig*>(
503 6148 : kCachedSigs[kSimdExprSigTable[opcode & 0xFF]]);
504 : case kAtomicPrefix:
505 : return const_cast<FunctionSig*>(
506 47743 : kCachedSigs[kAtomicExprSigTable[opcode & 0xFF]]);
507 : case kNumericPrefix:
508 : return const_cast<FunctionSig*>(
509 544 : kCachedSigs[kNumericExprSigTable[opcode & 0xFF]]);
510 : default:
511 0 : UNREACHABLE(); // invalid prefix.
512 : return nullptr;
513 : }
514 : }
515 :
516 1780731 : FunctionSig* WasmOpcodes::AsmjsSignature(WasmOpcode opcode) {
517 : DCHECK_GT(kSimpleAsmjsExprSigTable.size(), opcode);
518 : return const_cast<FunctionSig*>(
519 1780731 : kCachedSigs[kSimpleAsmjsExprSigTable[opcode]]);
520 : }
521 :
522 : // Define constexpr arrays.
523 : constexpr uint8_t LoadType::kLoadSizeLog2[];
524 : constexpr ValueType LoadType::kValueType[];
525 : constexpr MachineType LoadType::kMemType[];
526 : constexpr uint8_t StoreType::kStoreSizeLog2[];
527 : constexpr ValueType StoreType::kValueType[];
528 : constexpr MachineRepresentation StoreType::kMemRep[];
529 :
530 768 : MessageTemplate WasmOpcodes::TrapReasonToMessageId(TrapReason reason) {
531 768 : switch (reason) {
532 : #define TRAPREASON_TO_MESSAGE(name) \
533 : case k##name: \
534 : return MessageTemplate::kWasm##name;
535 : FOREACH_WASM_TRAPREASON(TRAPREASON_TO_MESSAGE)
536 : #undef TRAPREASON_TO_MESSAGE
537 : default:
538 : return MessageTemplate::kNone;
539 : }
540 : }
541 :
542 0 : const char* WasmOpcodes::TrapReasonMessage(TrapReason reason) {
543 0 : return MessageFormatter::TemplateString(TrapReasonToMessageId(reason));
544 : }
545 : } // namespace wasm
546 : } // namespace internal
547 178779 : } // namespace v8
|