• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WASM_OPCODES_H_
6 #define V8_WASM_OPCODES_H_
7 
8 #include "src/globals.h"
9 #include "src/machine-type.h"
10 #include "src/runtime/runtime.h"
11 #include "src/signature.h"
12 
13 namespace v8 {
14 namespace internal {
15 namespace wasm {
16 
17 // Binary encoding of local types.
18 enum ValueTypeCode {
19   kLocalVoid = 0x40,
20   kLocalI32 = 0x7f,
21   kLocalI64 = 0x7e,
22   kLocalF32 = 0x7d,
23   kLocalF64 = 0x7c,
24   kLocalS128 = 0x7b,
25   kLocalS1x4 = 0x7a,
26   kLocalS1x8 = 0x79,
27   kLocalS1x16 = 0x78
28 };
29 
30 // Type code for multi-value block types.
31 static const uint8_t kMultivalBlock = 0x41;
32 
33 // We reuse the internal machine type to represent WebAssembly types.
34 // A typedef improves readability without adding a whole new type system.
35 typedef MachineRepresentation ValueType;
36 const ValueType kWasmStmt = MachineRepresentation::kNone;
37 const ValueType kWasmI32 = MachineRepresentation::kWord32;
38 const ValueType kWasmI64 = MachineRepresentation::kWord64;
39 const ValueType kWasmF32 = MachineRepresentation::kFloat32;
40 const ValueType kWasmF64 = MachineRepresentation::kFloat64;
41 const ValueType kWasmS128 = MachineRepresentation::kSimd128;
42 const ValueType kWasmS1x4 = MachineRepresentation::kSimd1x4;
43 const ValueType kWasmS1x8 = MachineRepresentation::kSimd1x8;
44 const ValueType kWasmS1x16 = MachineRepresentation::kSimd1x16;
45 const ValueType kWasmVar = MachineRepresentation::kTagged;
46 
47 typedef Signature<ValueType> FunctionSig;
48 std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
49 
50 typedef Vector<const char> WasmName;
51 
52 typedef int WasmCodePosition;
53 const WasmCodePosition kNoCodePosition = -1;
54 
55 // Control expressions and blocks.
56 #define FOREACH_CONTROL_OPCODE(V)      \
57   V(Unreachable, 0x00, _)              \
58   V(Nop, 0x01, _)                      \
59   V(Block, 0x02, _)                    \
60   V(Loop, 0x03, _)                     \
61   V(If, 0x004, _)                      \
62   V(Else, 0x05, _)                     \
63   V(Try, 0x06, _ /* eh_prototype */)   \
64   V(Catch, 0x07, _ /* eh_prototype */) \
65   V(Throw, 0x08, _ /* eh_prototype */) \
66   V(End, 0x0b, _)                      \
67   V(Br, 0x0c, _)                       \
68   V(BrIf, 0x0d, _)                     \
69   V(BrTable, 0x0e, _)                  \
70   V(Return, 0x0f, _)
71 
72 // Constants, locals, globals, and calls.
73 #define FOREACH_MISC_OPCODE(V) \
74   V(CallFunction, 0x10, _)     \
75   V(CallIndirect, 0x11, _)     \
76   V(Drop, 0x1a, _)             \
77   V(Select, 0x1b, _)           \
78   V(GetLocal, 0x20, _)         \
79   V(SetLocal, 0x21, _)         \
80   V(TeeLocal, 0x22, _)         \
81   V(GetGlobal, 0x23, _)        \
82   V(SetGlobal, 0x24, _)        \
83   V(I32Const, 0x41, _)         \
84   V(I64Const, 0x42, _)         \
85   V(F32Const, 0x43, _)         \
86   V(F64Const, 0x44, _)
87 
88 // Load memory expressions.
89 #define FOREACH_LOAD_MEM_OPCODE(V) \
90   V(I32LoadMem, 0x28, i_i)         \
91   V(I64LoadMem, 0x29, l_i)         \
92   V(F32LoadMem, 0x2a, f_i)         \
93   V(F64LoadMem, 0x2b, d_i)         \
94   V(I32LoadMem8S, 0x2c, i_i)       \
95   V(I32LoadMem8U, 0x2d, i_i)       \
96   V(I32LoadMem16S, 0x2e, i_i)      \
97   V(I32LoadMem16U, 0x2f, i_i)      \
98   V(I64LoadMem8S, 0x30, l_i)       \
99   V(I64LoadMem8U, 0x31, l_i)       \
100   V(I64LoadMem16S, 0x32, l_i)      \
101   V(I64LoadMem16U, 0x33, l_i)      \
102   V(I64LoadMem32S, 0x34, l_i)      \
103   V(I64LoadMem32U, 0x35, l_i)
104 
105 // Store memory expressions.
106 #define FOREACH_STORE_MEM_OPCODE(V) \
107   V(I32StoreMem, 0x36, i_ii)        \
108   V(I64StoreMem, 0x37, l_il)        \
109   V(F32StoreMem, 0x38, f_if)        \
110   V(F64StoreMem, 0x39, d_id)        \
111   V(I32StoreMem8, 0x3a, i_ii)       \
112   V(I32StoreMem16, 0x3b, i_ii)      \
113   V(I64StoreMem8, 0x3c, l_il)       \
114   V(I64StoreMem16, 0x3d, l_il)      \
115   V(I64StoreMem32, 0x3e, l_il)
116 
117 // Miscellaneous memory expressions
118 #define FOREACH_MISC_MEM_OPCODE(V) \
119   V(MemorySize, 0x3f, i_v)         \
120   V(GrowMemory, 0x40, i_i)
121 
122 // Expressions with signatures.
123 #define FOREACH_SIMPLE_OPCODE(V)  \
124   V(I32Eqz, 0x45, i_i)            \
125   V(I32Eq, 0x46, i_ii)            \
126   V(I32Ne, 0x47, i_ii)            \
127   V(I32LtS, 0x48, i_ii)           \
128   V(I32LtU, 0x49, i_ii)           \
129   V(I32GtS, 0x4a, i_ii)           \
130   V(I32GtU, 0x4b, i_ii)           \
131   V(I32LeS, 0x4c, i_ii)           \
132   V(I32LeU, 0x4d, i_ii)           \
133   V(I32GeS, 0x4e, i_ii)           \
134   V(I32GeU, 0x4f, i_ii)           \
135   V(I64Eqz, 0x50, i_l)            \
136   V(I64Eq, 0x51, i_ll)            \
137   V(I64Ne, 0x52, i_ll)            \
138   V(I64LtS, 0x53, i_ll)           \
139   V(I64LtU, 0x54, i_ll)           \
140   V(I64GtS, 0x55, i_ll)           \
141   V(I64GtU, 0x56, i_ll)           \
142   V(I64LeS, 0x57, i_ll)           \
143   V(I64LeU, 0x58, i_ll)           \
144   V(I64GeS, 0x59, i_ll)           \
145   V(I64GeU, 0x5a, i_ll)           \
146   V(F32Eq, 0x5b, i_ff)            \
147   V(F32Ne, 0x5c, i_ff)            \
148   V(F32Lt, 0x5d, i_ff)            \
149   V(F32Gt, 0x5e, i_ff)            \
150   V(F32Le, 0x5f, i_ff)            \
151   V(F32Ge, 0x60, i_ff)            \
152   V(F64Eq, 0x61, i_dd)            \
153   V(F64Ne, 0x62, i_dd)            \
154   V(F64Lt, 0x63, i_dd)            \
155   V(F64Gt, 0x64, i_dd)            \
156   V(F64Le, 0x65, i_dd)            \
157   V(F64Ge, 0x66, i_dd)            \
158   V(I32Clz, 0x67, i_i)            \
159   V(I32Ctz, 0x68, i_i)            \
160   V(I32Popcnt, 0x69, i_i)         \
161   V(I32Add, 0x6a, i_ii)           \
162   V(I32Sub, 0x6b, i_ii)           \
163   V(I32Mul, 0x6c, i_ii)           \
164   V(I32DivS, 0x6d, i_ii)          \
165   V(I32DivU, 0x6e, i_ii)          \
166   V(I32RemS, 0x6f, i_ii)          \
167   V(I32RemU, 0x70, i_ii)          \
168   V(I32And, 0x71, i_ii)           \
169   V(I32Ior, 0x72, i_ii)           \
170   V(I32Xor, 0x73, i_ii)           \
171   V(I32Shl, 0x74, i_ii)           \
172   V(I32ShrS, 0x75, i_ii)          \
173   V(I32ShrU, 0x76, i_ii)          \
174   V(I32Rol, 0x77, i_ii)           \
175   V(I32Ror, 0x78, i_ii)           \
176   V(I64Clz, 0x79, l_l)            \
177   V(I64Ctz, 0x7a, l_l)            \
178   V(I64Popcnt, 0x7b, l_l)         \
179   V(I64Add, 0x7c, l_ll)           \
180   V(I64Sub, 0x7d, l_ll)           \
181   V(I64Mul, 0x7e, l_ll)           \
182   V(I64DivS, 0x7f, l_ll)          \
183   V(I64DivU, 0x80, l_ll)          \
184   V(I64RemS, 0x81, l_ll)          \
185   V(I64RemU, 0x82, l_ll)          \
186   V(I64And, 0x83, l_ll)           \
187   V(I64Ior, 0x84, l_ll)           \
188   V(I64Xor, 0x85, l_ll)           \
189   V(I64Shl, 0x86, l_ll)           \
190   V(I64ShrS, 0x87, l_ll)          \
191   V(I64ShrU, 0x88, l_ll)          \
192   V(I64Rol, 0x89, l_ll)           \
193   V(I64Ror, 0x8a, l_ll)           \
194   V(F32Abs, 0x8b, f_f)            \
195   V(F32Neg, 0x8c, f_f)            \
196   V(F32Ceil, 0x8d, f_f)           \
197   V(F32Floor, 0x8e, f_f)          \
198   V(F32Trunc, 0x8f, f_f)          \
199   V(F32NearestInt, 0x90, f_f)     \
200   V(F32Sqrt, 0x91, f_f)           \
201   V(F32Add, 0x92, f_ff)           \
202   V(F32Sub, 0x93, f_ff)           \
203   V(F32Mul, 0x94, f_ff)           \
204   V(F32Div, 0x95, f_ff)           \
205   V(F32Min, 0x96, f_ff)           \
206   V(F32Max, 0x97, f_ff)           \
207   V(F32CopySign, 0x98, f_ff)      \
208   V(F64Abs, 0x99, d_d)            \
209   V(F64Neg, 0x9a, d_d)            \
210   V(F64Ceil, 0x9b, d_d)           \
211   V(F64Floor, 0x9c, d_d)          \
212   V(F64Trunc, 0x9d, d_d)          \
213   V(F64NearestInt, 0x9e, d_d)     \
214   V(F64Sqrt, 0x9f, d_d)           \
215   V(F64Add, 0xa0, d_dd)           \
216   V(F64Sub, 0xa1, d_dd)           \
217   V(F64Mul, 0xa2, d_dd)           \
218   V(F64Div, 0xa3, d_dd)           \
219   V(F64Min, 0xa4, d_dd)           \
220   V(F64Max, 0xa5, d_dd)           \
221   V(F64CopySign, 0xa6, d_dd)      \
222   V(I32ConvertI64, 0xa7, i_l)     \
223   V(I32SConvertF32, 0xa8, i_f)    \
224   V(I32UConvertF32, 0xa9, i_f)    \
225   V(I32SConvertF64, 0xaa, i_d)    \
226   V(I32UConvertF64, 0xab, i_d)    \
227   V(I64SConvertI32, 0xac, l_i)    \
228   V(I64UConvertI32, 0xad, l_i)    \
229   V(I64SConvertF32, 0xae, l_f)    \
230   V(I64UConvertF32, 0xaf, l_f)    \
231   V(I64SConvertF64, 0xb0, l_d)    \
232   V(I64UConvertF64, 0xb1, l_d)    \
233   V(F32SConvertI32, 0xb2, f_i)    \
234   V(F32UConvertI32, 0xb3, f_i)    \
235   V(F32SConvertI64, 0xb4, f_l)    \
236   V(F32UConvertI64, 0xb5, f_l)    \
237   V(F32ConvertF64, 0xb6, f_d)     \
238   V(F64SConvertI32, 0xb7, d_i)    \
239   V(F64UConvertI32, 0xb8, d_i)    \
240   V(F64SConvertI64, 0xb9, d_l)    \
241   V(F64UConvertI64, 0xba, d_l)    \
242   V(F64ConvertF32, 0xbb, d_f)     \
243   V(I32ReinterpretF32, 0xbc, i_f) \
244   V(I64ReinterpretF64, 0xbd, l_d) \
245   V(F32ReinterpretI32, 0xbe, f_i) \
246   V(F64ReinterpretI64, 0xbf, d_l)
247 
248 // For compatibility with Asm.js.
249 #define FOREACH_ASMJS_COMPAT_OPCODE(V) \
250   V(F64Acos, 0xc0, d_d)                \
251   V(F64Asin, 0xc1, d_d)                \
252   V(F64Atan, 0xc2, d_d)                \
253   V(F64Cos, 0xc3, d_d)                 \
254   V(F64Sin, 0xc4, d_d)                 \
255   V(F64Tan, 0xc5, d_d)                 \
256   V(F64Exp, 0xc6, d_d)                 \
257   V(F64Log, 0xc7, d_d)                 \
258   V(F64Atan2, 0xc8, d_dd)              \
259   V(F64Pow, 0xc9, d_dd)                \
260   V(F64Mod, 0xca, d_dd)                \
261   V(I32AsmjsDivS, 0xd0, i_ii)          \
262   V(I32AsmjsDivU, 0xd1, i_ii)          \
263   V(I32AsmjsRemS, 0xd2, i_ii)          \
264   V(I32AsmjsRemU, 0xd3, i_ii)          \
265   V(I32AsmjsLoadMem8S, 0xd4, i_i)      \
266   V(I32AsmjsLoadMem8U, 0xd5, i_i)      \
267   V(I32AsmjsLoadMem16S, 0xd6, i_i)     \
268   V(I32AsmjsLoadMem16U, 0xd7, i_i)     \
269   V(I32AsmjsLoadMem, 0xd8, i_i)        \
270   V(F32AsmjsLoadMem, 0xd9, f_i)        \
271   V(F64AsmjsLoadMem, 0xda, d_i)        \
272   V(I32AsmjsStoreMem8, 0xdb, i_ii)     \
273   V(I32AsmjsStoreMem16, 0xdc, i_ii)    \
274   V(I32AsmjsStoreMem, 0xdd, i_ii)      \
275   V(F32AsmjsStoreMem, 0xde, f_if)      \
276   V(F64AsmjsStoreMem, 0xdf, d_id)      \
277   V(I32AsmjsSConvertF32, 0xe0, i_f)    \
278   V(I32AsmjsUConvertF32, 0xe1, i_f)    \
279   V(I32AsmjsSConvertF64, 0xe2, i_d)    \
280   V(I32AsmjsUConvertF64, 0xe3, i_d)
281 
282 #define FOREACH_SIMD_0_OPERAND_OPCODE(V) \
283   V(F32x4Splat, 0xe500, s_f)             \
284   V(F32x4Abs, 0xe503, s_s)               \
285   V(F32x4Neg, 0xe504, s_s)               \
286   V(F32x4Sqrt, 0xe505, s_s)              \
287   V(F32x4RecipApprox, 0xe506, s_s)       \
288   V(F32x4SqrtApprox, 0xe507, s_s)        \
289   V(F32x4Add, 0xe508, s_ss)              \
290   V(F32x4Sub, 0xe509, s_ss)              \
291   V(F32x4Mul, 0xe50a, s_ss)              \
292   V(F32x4Div, 0xe50b, s_ss)              \
293   V(F32x4Min, 0xe50c, s_ss)              \
294   V(F32x4Max, 0xe50d, s_ss)              \
295   V(F32x4MinNum, 0xe50e, s_ss)           \
296   V(F32x4MaxNum, 0xe50f, s_ss)           \
297   V(F32x4Eq, 0xe510, s1x4_ss)            \
298   V(F32x4Ne, 0xe511, s1x4_ss)            \
299   V(F32x4Lt, 0xe512, s1x4_ss)            \
300   V(F32x4Le, 0xe513, s1x4_ss)            \
301   V(F32x4Gt, 0xe514, s1x4_ss)            \
302   V(F32x4Ge, 0xe515, s1x4_ss)            \
303   V(F32x4SConvertI32x4, 0xe519, s_s)     \
304   V(F32x4UConvertI32x4, 0xe51a, s_s)     \
305   V(I32x4Splat, 0xe51b, s_i)             \
306   V(I32x4Neg, 0xe51e, s_s)               \
307   V(I32x4Add, 0xe51f, s_ss)              \
308   V(I32x4Sub, 0xe520, s_ss)              \
309   V(I32x4Mul, 0xe521, s_ss)              \
310   V(I32x4MinS, 0xe522, s_ss)             \
311   V(I32x4MaxS, 0xe523, s_ss)             \
312   V(I32x4Eq, 0xe526, s1x4_ss)            \
313   V(I32x4Ne, 0xe527, s1x4_ss)            \
314   V(I32x4LtS, 0xe528, s1x4_ss)           \
315   V(I32x4LeS, 0xe529, s1x4_ss)           \
316   V(I32x4GtS, 0xe52a, s1x4_ss)           \
317   V(I32x4GeS, 0xe52b, s1x4_ss)           \
318   V(I32x4SConvertF32x4, 0xe52f, s_s)     \
319   V(I32x4MinU, 0xe530, s_ss)             \
320   V(I32x4MaxU, 0xe531, s_ss)             \
321   V(I32x4LtU, 0xe533, s1x4_ss)           \
322   V(I32x4LeU, 0xe534, s1x4_ss)           \
323   V(I32x4GtU, 0xe535, s1x4_ss)           \
324   V(I32x4GeU, 0xe536, s1x4_ss)           \
325   V(I32x4UConvertF32x4, 0xe537, s_s)     \
326   V(I16x8Splat, 0xe538, s_i)             \
327   V(I16x8Neg, 0xe53b, s_s)               \
328   V(I16x8Add, 0xe53c, s_ss)              \
329   V(I16x8AddSaturateS, 0xe53d, s_ss)     \
330   V(I16x8Sub, 0xe53e, s_ss)              \
331   V(I16x8SubSaturateS, 0xe53f, s_ss)     \
332   V(I16x8Mul, 0xe540, s_ss)              \
333   V(I16x8MinS, 0xe541, s_ss)             \
334   V(I16x8MaxS, 0xe542, s_ss)             \
335   V(I16x8Eq, 0xe545, s1x8_ss)            \
336   V(I16x8Ne, 0xe546, s1x8_ss)            \
337   V(I16x8LtS, 0xe547, s1x8_ss)           \
338   V(I16x8LeS, 0xe548, s1x8_ss)           \
339   V(I16x8GtS, 0xe549, s1x8_ss)           \
340   V(I16x8GeS, 0xe54a, s1x8_ss)           \
341   V(I16x8AddSaturateU, 0xe54e, s_ss)     \
342   V(I16x8SubSaturateU, 0xe54f, s_ss)     \
343   V(I16x8MinU, 0xe550, s_ss)             \
344   V(I16x8MaxU, 0xe551, s_ss)             \
345   V(I16x8LtU, 0xe553, s1x8_ss)           \
346   V(I16x8LeU, 0xe554, s1x8_ss)           \
347   V(I16x8GtU, 0xe555, s1x8_ss)           \
348   V(I16x8GeU, 0xe556, s1x8_ss)           \
349   V(I8x16Splat, 0xe557, s_i)             \
350   V(I8x16Neg, 0xe55a, s_s)               \
351   V(I8x16Add, 0xe55b, s_ss)              \
352   V(I8x16AddSaturateS, 0xe55c, s_ss)     \
353   V(I8x16Sub, 0xe55d, s_ss)              \
354   V(I8x16SubSaturateS, 0xe55e, s_ss)     \
355   V(I8x16Mul, 0xe55f, s_ss)              \
356   V(I8x16MinS, 0xe560, s_ss)             \
357   V(I8x16MaxS, 0xe561, s_ss)             \
358   V(I8x16Eq, 0xe564, s1x16_ss)           \
359   V(I8x16Ne, 0xe565, s1x16_ss)           \
360   V(I8x16LtS, 0xe566, s1x16_ss)          \
361   V(I8x16LeS, 0xe567, s1x16_ss)          \
362   V(I8x16GtS, 0xe568, s1x16_ss)          \
363   V(I8x16GeS, 0xe569, s1x16_ss)          \
364   V(I8x16AddSaturateU, 0xe56d, s_ss)     \
365   V(I8x16SubSaturateU, 0xe56e, s_ss)     \
366   V(I8x16MinU, 0xe56f, s_ss)             \
367   V(I8x16MaxU, 0xe570, s_ss)             \
368   V(I8x16LtU, 0xe572, s1x16_ss)          \
369   V(I8x16LeU, 0xe573, s1x16_ss)          \
370   V(I8x16GtU, 0xe574, s1x16_ss)          \
371   V(I8x16GeU, 0xe575, s1x16_ss)          \
372   V(S128And, 0xe576, s_ss)               \
373   V(S128Or, 0xe577, s_ss)                \
374   V(S128Xor, 0xe578, s_ss)               \
375   V(S128Not, 0xe579, s_s)                \
376   V(S32x4Select, 0xe52c, s_s1x4ss)       \
377   V(S32x4Swizzle, 0xe52d, s_s)           \
378   V(S32x4Shuffle, 0xe52e, s_ss)          \
379   V(S16x8Select, 0xe54b, s_s1x8ss)       \
380   V(S16x8Swizzle, 0xe54c, s_s)           \
381   V(S16x8Shuffle, 0xe54d, s_ss)          \
382   V(S8x16Select, 0xe56a, s_s1x16ss)      \
383   V(S8x16Swizzle, 0xe56b, s_s)           \
384   V(S8x16Shuffle, 0xe56c, s_ss)
385 
386 #define FOREACH_SIMD_1_OPERAND_OPCODE(V) \
387   V(F32x4ExtractLane, 0xe501, _)         \
388   V(F32x4ReplaceLane, 0xe502, _)         \
389   V(I32x4ExtractLane, 0xe51c, _)         \
390   V(I32x4ReplaceLane, 0xe51d, _)         \
391   V(I32x4Shl, 0xe524, _)                 \
392   V(I32x4ShrS, 0xe525, _)                \
393   V(I32x4ShrU, 0xe532, _)                \
394   V(I16x8ExtractLane, 0xe539, _)         \
395   V(I16x8ReplaceLane, 0xe53a, _)         \
396   V(I16x8Shl, 0xe543, _)                 \
397   V(I16x8ShrS, 0xe544, _)                \
398   V(I16x8ShrU, 0xe552, _)                \
399   V(I8x16ExtractLane, 0xe558, _)         \
400   V(I8x16ReplaceLane, 0xe559, _)         \
401   V(I8x16Shl, 0xe562, _)                 \
402   V(I8x16ShrS, 0xe563, _)                \
403   V(I8x16ShrU, 0xe571, _)
404 
405 #define FOREACH_ATOMIC_OPCODE(V)               \
406   V(I32AtomicAdd8S, 0xe601, i_ii)              \
407   V(I32AtomicAdd8U, 0xe602, i_ii)              \
408   V(I32AtomicAdd16S, 0xe603, i_ii)             \
409   V(I32AtomicAdd16U, 0xe604, i_ii)             \
410   V(I32AtomicAdd, 0xe605, i_ii)                \
411   V(I32AtomicAnd8S, 0xe606, i_ii)              \
412   V(I32AtomicAnd8U, 0xe607, i_ii)              \
413   V(I32AtomicAnd16S, 0xe608, i_ii)             \
414   V(I32AtomicAnd16U, 0xe609, i_ii)             \
415   V(I32AtomicAnd, 0xe60a, i_ii)                \
416   V(I32AtomicCompareExchange8S, 0xe60b, i_ii)  \
417   V(I32AtomicCompareExchange8U, 0xe60c, i_ii)  \
418   V(I32AtomicCompareExchange16S, 0xe60d, i_ii) \
419   V(I32AtomicCompareExchange16U, 0xe60e, i_ii) \
420   V(I32AtomicCompareExchange, 0xe60f, i_ii)    \
421   V(I32AtomicExchange8S, 0xe610, i_ii)         \
422   V(I32AtomicExchange8U, 0xe611, i_ii)         \
423   V(I32AtomicExchange16S, 0xe612, i_ii)        \
424   V(I32AtomicExchange16U, 0xe613, i_ii)        \
425   V(I32AtomicExchange, 0xe614, i_ii)           \
426   V(I32AtomicOr8S, 0xe615, i_ii)               \
427   V(I32AtomicOr8U, 0xe616, i_ii)               \
428   V(I32AtomicOr16S, 0xe617, i_ii)              \
429   V(I32AtomicOr16U, 0xe618, i_ii)              \
430   V(I32AtomicOr, 0xe619, i_ii)                 \
431   V(I32AtomicSub8S, 0xe61a, i_ii)              \
432   V(I32AtomicSub8U, 0xe61b, i_ii)              \
433   V(I32AtomicSub16S, 0xe61c, i_ii)             \
434   V(I32AtomicSub16U, 0xe61d, i_ii)             \
435   V(I32AtomicSub, 0xe61e, i_ii)                \
436   V(I32AtomicXor8S, 0xe61f, i_ii)              \
437   V(I32AtomicXor8U, 0xe620, i_ii)              \
438   V(I32AtomicXor16S, 0xe621, i_ii)             \
439   V(I32AtomicXor16U, 0xe622, i_ii)             \
440   V(I32AtomicXor, 0xe623, i_ii)
441 
442 // All opcodes.
443 #define FOREACH_OPCODE(V)          \
444   FOREACH_CONTROL_OPCODE(V)        \
445   FOREACH_MISC_OPCODE(V)           \
446   FOREACH_SIMPLE_OPCODE(V)         \
447   FOREACH_STORE_MEM_OPCODE(V)      \
448   FOREACH_LOAD_MEM_OPCODE(V)       \
449   FOREACH_MISC_MEM_OPCODE(V)       \
450   FOREACH_ASMJS_COMPAT_OPCODE(V)   \
451   FOREACH_SIMD_0_OPERAND_OPCODE(V) \
452   FOREACH_SIMD_1_OPERAND_OPCODE(V) \
453   FOREACH_ATOMIC_OPCODE(V)
454 
455 // All signatures.
456 #define FOREACH_SIGNATURE(V)            \
457   FOREACH_SIMD_SIGNATURE(V)             \
458   V(i_ii, kWasmI32, kWasmI32, kWasmI32) \
459   V(i_i, kWasmI32, kWasmI32)            \
460   V(i_v, kWasmI32)                      \
461   V(i_ff, kWasmI32, kWasmF32, kWasmF32) \
462   V(i_f, kWasmI32, kWasmF32)            \
463   V(i_dd, kWasmI32, kWasmF64, kWasmF64) \
464   V(i_d, kWasmI32, kWasmF64)            \
465   V(i_l, kWasmI32, kWasmI64)            \
466   V(l_ll, kWasmI64, kWasmI64, kWasmI64) \
467   V(i_ll, kWasmI32, kWasmI64, kWasmI64) \
468   V(l_l, kWasmI64, kWasmI64)            \
469   V(l_i, kWasmI64, kWasmI32)            \
470   V(l_f, kWasmI64, kWasmF32)            \
471   V(l_d, kWasmI64, kWasmF64)            \
472   V(f_ff, kWasmF32, kWasmF32, kWasmF32) \
473   V(f_f, kWasmF32, kWasmF32)            \
474   V(f_d, kWasmF32, kWasmF64)            \
475   V(f_i, kWasmF32, kWasmI32)            \
476   V(f_l, kWasmF32, kWasmI64)            \
477   V(d_dd, kWasmF64, kWasmF64, kWasmF64) \
478   V(d_d, kWasmF64, kWasmF64)            \
479   V(d_f, kWasmF64, kWasmF32)            \
480   V(d_i, kWasmF64, kWasmI32)            \
481   V(d_l, kWasmF64, kWasmI64)            \
482   V(d_id, kWasmF64, kWasmI32, kWasmF64) \
483   V(f_if, kWasmF32, kWasmI32, kWasmF32) \
484   V(l_il, kWasmI64, kWasmI32, kWasmI64)
485 
486 #define FOREACH_SIMD_SIGNATURE(V)                         \
487   V(s_s, kWasmS128, kWasmS128)                            \
488   V(s_f, kWasmS128, kWasmF32)                             \
489   V(s_ss, kWasmS128, kWasmS128, kWasmS128)                \
490   V(s1x4_ss, kWasmS1x4, kWasmS128, kWasmS128)             \
491   V(s1x8_ss, kWasmS1x8, kWasmS128, kWasmS128)             \
492   V(s1x16_ss, kWasmS1x16, kWasmS128, kWasmS128)           \
493   V(s_i, kWasmS128, kWasmI32)                             \
494   V(s_si, kWasmS128, kWasmS128, kWasmI32)                 \
495   V(i_s, kWasmI32, kWasmS128)                             \
496   V(s_s1x4ss, kWasmS128, kWasmS1x4, kWasmS128, kWasmS128) \
497   V(s_s1x8ss, kWasmS128, kWasmS1x8, kWasmS128, kWasmS128) \
498   V(s_s1x16ss, kWasmS128, kWasmS1x16, kWasmS128, kWasmS128)
499 
500 #define FOREACH_PREFIX(V) \
501   V(Simd, 0xe5)           \
502   V(Atomic, 0xe6)
503 
504 enum WasmOpcode {
505 // Declare expression opcodes.
506 #define DECLARE_NAMED_ENUM(name, opcode, sig) kExpr##name = opcode,
507   FOREACH_OPCODE(DECLARE_NAMED_ENUM)
508 #undef DECLARE_NAMED_ENUM
509 #define DECLARE_PREFIX(name, opcode) k##name##Prefix = opcode,
510       FOREACH_PREFIX(DECLARE_PREFIX)
511 #undef DECLARE_PREFIX
512 };
513 
514 // The reason for a trap.
515 #define FOREACH_WASM_TRAPREASON(V) \
516   V(TrapUnreachable)               \
517   V(TrapMemOutOfBounds)            \
518   V(TrapDivByZero)                 \
519   V(TrapDivUnrepresentable)        \
520   V(TrapRemByZero)                 \
521   V(TrapFloatUnrepresentable)      \
522   V(TrapFuncInvalid)               \
523   V(TrapFuncSigMismatch)
524 
525 enum TrapReason {
526 #define DECLARE_ENUM(name) k##name,
527   FOREACH_WASM_TRAPREASON(DECLARE_ENUM)
528   kTrapCount
529 #undef DECLARE_ENUM
530 };
531 
532 // A collection of opcode-related static methods.
533 class V8_EXPORT_PRIVATE WasmOpcodes {
534  public:
535   static const char* OpcodeName(WasmOpcode opcode);
536   static FunctionSig* Signature(WasmOpcode opcode);
537   static FunctionSig* AsmjsSignature(WasmOpcode opcode);
538   static FunctionSig* AtomicSignature(WasmOpcode opcode);
539   static bool IsPrefixOpcode(WasmOpcode opcode);
540 
541   static int TrapReasonToMessageId(TrapReason reason);
542   static const char* TrapReasonMessage(TrapReason reason);
543 
MemSize(MachineType type)544   static byte MemSize(MachineType type) {
545     return 1 << ElementSizeLog2Of(type.representation());
546   }
547 
MemSize(ValueType type)548   static byte MemSize(ValueType type) { return 1 << ElementSizeLog2Of(type); }
549 
ValueTypeCodeFor(ValueType type)550   static ValueTypeCode ValueTypeCodeFor(ValueType type) {
551     switch (type) {
552       case kWasmI32:
553         return kLocalI32;
554       case kWasmI64:
555         return kLocalI64;
556       case kWasmF32:
557         return kLocalF32;
558       case kWasmF64:
559         return kLocalF64;
560       case kWasmS128:
561         return kLocalS128;
562       case kWasmS1x4:
563         return kLocalS1x4;
564       case kWasmS1x8:
565         return kLocalS1x8;
566       case kWasmS1x16:
567         return kLocalS1x16;
568       case kWasmStmt:
569         return kLocalVoid;
570       default:
571         UNREACHABLE();
572         return kLocalVoid;
573     }
574   }
575 
MachineTypeFor(ValueType type)576   static MachineType MachineTypeFor(ValueType type) {
577     switch (type) {
578       case kWasmI32:
579         return MachineType::Int32();
580       case kWasmI64:
581         return MachineType::Int64();
582       case kWasmF32:
583         return MachineType::Float32();
584       case kWasmF64:
585         return MachineType::Float64();
586       case kWasmS128:
587         return MachineType::Simd128();
588       case kWasmS1x4:
589         return MachineType::Simd1x4();
590       case kWasmS1x8:
591         return MachineType::Simd1x8();
592       case kWasmS1x16:
593         return MachineType::Simd1x16();
594       case kWasmStmt:
595         return MachineType::None();
596       default:
597         UNREACHABLE();
598         return MachineType::None();
599     }
600   }
601 
ValueTypeFor(MachineType type)602   static ValueType ValueTypeFor(MachineType type) {
603     if (type == MachineType::Int8()) {
604       return kWasmI32;
605     } else if (type == MachineType::Uint8()) {
606       return kWasmI32;
607     } else if (type == MachineType::Int16()) {
608       return kWasmI32;
609     } else if (type == MachineType::Uint16()) {
610       return kWasmI32;
611     } else if (type == MachineType::Int32()) {
612       return kWasmI32;
613     } else if (type == MachineType::Uint32()) {
614       return kWasmI32;
615     } else if (type == MachineType::Int64()) {
616       return kWasmI64;
617     } else if (type == MachineType::Uint64()) {
618       return kWasmI64;
619     } else if (type == MachineType::Float32()) {
620       return kWasmF32;
621     } else if (type == MachineType::Float64()) {
622       return kWasmF64;
623     } else if (type == MachineType::Simd128()) {
624       return kWasmS128;
625     } else if (type == MachineType::Simd1x4()) {
626       return kWasmS1x4;
627     } else if (type == MachineType::Simd1x8()) {
628       return kWasmS1x8;
629     } else if (type == MachineType::Simd1x16()) {
630       return kWasmS1x16;
631     } else {
632       UNREACHABLE();
633       return kWasmI32;
634     }
635   }
636 
LoadStoreOpcodeOf(MachineType type,bool store)637   static WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
638     if (type == MachineType::Int8()) {
639       return store ? kExprI32StoreMem8 : kExprI32LoadMem8S;
640     } else if (type == MachineType::Uint8()) {
641       return store ? kExprI32StoreMem8 : kExprI32LoadMem8U;
642     } else if (type == MachineType::Int16()) {
643       return store ? kExprI32StoreMem16 : kExprI32LoadMem16S;
644     } else if (type == MachineType::Uint16()) {
645       return store ? kExprI32StoreMem16 : kExprI32LoadMem16U;
646     } else if (type == MachineType::Int32()) {
647       return store ? kExprI32StoreMem : kExprI32LoadMem;
648     } else if (type == MachineType::Uint32()) {
649       return store ? kExprI32StoreMem : kExprI32LoadMem;
650     } else if (type == MachineType::Int64()) {
651       return store ? kExprI64StoreMem : kExprI64LoadMem;
652     } else if (type == MachineType::Uint64()) {
653       return store ? kExprI64StoreMem : kExprI64LoadMem;
654     } else if (type == MachineType::Float32()) {
655       return store ? kExprF32StoreMem : kExprF32LoadMem;
656     } else if (type == MachineType::Float64()) {
657       return store ? kExprF64StoreMem : kExprF64LoadMem;
658     } else {
659       UNREACHABLE();
660       return kExprNop;
661     }
662   }
663 
ShortNameOf(ValueType type)664   static char ShortNameOf(ValueType type) {
665     switch (type) {
666       case kWasmI32:
667         return 'i';
668       case kWasmI64:
669         return 'l';
670       case kWasmF32:
671         return 'f';
672       case kWasmF64:
673         return 'd';
674       case kWasmS128:
675       case kWasmS1x4:
676       case kWasmS1x8:
677       case kWasmS1x16:
678         return 's';
679       case kWasmStmt:
680         return 'v';
681       case kWasmVar:
682         return '*';
683       default:
684         return '?';
685     }
686   }
687 
TypeName(ValueType type)688   static const char* TypeName(ValueType type) {
689     switch (type) {
690       case kWasmI32:
691         return "i32";
692       case kWasmI64:
693         return "i64";
694       case kWasmF32:
695         return "f32";
696       case kWasmF64:
697         return "f64";
698       case kWasmS128:
699         return "s128";
700       case kWasmS1x4:
701         return "s1x4";
702       case kWasmS1x8:
703         return "s1x8";
704       case kWasmS1x16:
705         return "s1x16";
706       case kWasmStmt:
707         return "<stmt>";
708       case kWasmVar:
709         return "<var>";
710       default:
711         return "<unknown>";
712     }
713   }
714 };
715 }  // namespace wasm
716 }  // namespace internal
717 }  // namespace v8
718 
719 #endif  // V8_WASM_OPCODES_H_
720