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 : #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 the module header.
18 : const uint32_t kWasmMagic = 0x6d736100;
19 : const uint32_t kWasmVersion = 0x01;
20 :
21 : // Binary encoding of local types.
22 : enum ValueTypeCode {
23 : kLocalVoid = 0x40,
24 : kLocalI32 = 0x7f,
25 : kLocalI64 = 0x7e,
26 : kLocalF32 = 0x7d,
27 : kLocalF64 = 0x7c,
28 : kLocalS128 = 0x7b
29 : };
30 :
31 : // We reuse the internal machine type to represent WebAssembly types.
32 : // A typedef improves readability without adding a whole new type system.
33 : using ValueType = MachineRepresentation;
34 : constexpr ValueType kWasmStmt = MachineRepresentation::kNone;
35 : constexpr ValueType kWasmI32 = MachineRepresentation::kWord32;
36 : constexpr ValueType kWasmI64 = MachineRepresentation::kWord64;
37 : constexpr ValueType kWasmF32 = MachineRepresentation::kFloat32;
38 : constexpr ValueType kWasmF64 = MachineRepresentation::kFloat64;
39 : constexpr ValueType kWasmS128 = MachineRepresentation::kSimd128;
40 : constexpr ValueType kWasmVar = MachineRepresentation::kTagged;
41 :
42 : using FunctionSig = Signature<ValueType>;
43 : std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
44 : bool IsJSCompatibleSignature(const FunctionSig* sig);
45 :
46 : using WasmName = Vector<const char>;
47 :
48 : using WasmCodePosition = int;
49 : constexpr WasmCodePosition kNoCodePosition = -1;
50 :
51 : // Control expressions and blocks.
52 : #define FOREACH_CONTROL_OPCODE(V) \
53 : V(Unreachable, 0x00, _) \
54 : V(Nop, 0x01, _) \
55 : V(Block, 0x02, _) \
56 : V(Loop, 0x03, _) \
57 : V(If, 0x004, _) \
58 : V(Else, 0x05, _) \
59 : V(Try, 0x06, _ /* eh_prototype */) \
60 : V(Catch, 0x07, _ /* eh_prototype */) \
61 : V(Throw, 0x08, _ /* eh_prototype */) \
62 : V(Rethrow, 0x09, _ /* eh_prototype */) \
63 : V(CatchAll, 0x0a, _ /* eh prototype */) \
64 : V(End, 0x0b, _) \
65 : V(Br, 0x0c, _) \
66 : V(BrIf, 0x0d, _) \
67 : V(BrTable, 0x0e, _) \
68 : V(Return, 0x0f, _)
69 :
70 : // Constants, locals, globals, and calls.
71 : #define FOREACH_MISC_OPCODE(V) \
72 : V(CallFunction, 0x10, _) \
73 : V(CallIndirect, 0x11, _) \
74 : V(Drop, 0x1a, _) \
75 : V(Select, 0x1b, _) \
76 : V(GetLocal, 0x20, _) \
77 : V(SetLocal, 0x21, _) \
78 : V(TeeLocal, 0x22, _) \
79 : V(GetGlobal, 0x23, _) \
80 : V(SetGlobal, 0x24, _) \
81 : V(I32Const, 0x41, _) \
82 : V(I64Const, 0x42, _) \
83 : V(F32Const, 0x43, _) \
84 : V(F64Const, 0x44, _)
85 :
86 : // Load memory expressions.
87 : #define FOREACH_LOAD_MEM_OPCODE(V) \
88 : V(I32LoadMem, 0x28, i_i) \
89 : V(I64LoadMem, 0x29, l_i) \
90 : V(F32LoadMem, 0x2a, f_i) \
91 : V(F64LoadMem, 0x2b, d_i) \
92 : V(I32LoadMem8S, 0x2c, i_i) \
93 : V(I32LoadMem8U, 0x2d, i_i) \
94 : V(I32LoadMem16S, 0x2e, i_i) \
95 : V(I32LoadMem16U, 0x2f, i_i) \
96 : V(I64LoadMem8S, 0x30, l_i) \
97 : V(I64LoadMem8U, 0x31, l_i) \
98 : V(I64LoadMem16S, 0x32, l_i) \
99 : V(I64LoadMem16U, 0x33, l_i) \
100 : V(I64LoadMem32S, 0x34, l_i) \
101 : V(I64LoadMem32U, 0x35, l_i)
102 :
103 : // Store memory expressions.
104 : #define FOREACH_STORE_MEM_OPCODE(V) \
105 : V(I32StoreMem, 0x36, i_ii) \
106 : V(I64StoreMem, 0x37, l_il) \
107 : V(F32StoreMem, 0x38, f_if) \
108 : V(F64StoreMem, 0x39, d_id) \
109 : V(I32StoreMem8, 0x3a, i_ii) \
110 : V(I32StoreMem16, 0x3b, i_ii) \
111 : V(I64StoreMem8, 0x3c, l_il) \
112 : V(I64StoreMem16, 0x3d, l_il) \
113 : V(I64StoreMem32, 0x3e, l_il)
114 :
115 : // Miscellaneous memory expressions
116 : #define FOREACH_MISC_MEM_OPCODE(V) \
117 : V(MemorySize, 0x3f, i_v) \
118 : V(GrowMemory, 0x40, i_i)
119 :
120 : // Expressions with signatures.
121 : #define FOREACH_SIMPLE_OPCODE(V) \
122 : V(I32Eqz, 0x45, i_i) \
123 : V(I32Eq, 0x46, i_ii) \
124 : V(I32Ne, 0x47, i_ii) \
125 : V(I32LtS, 0x48, i_ii) \
126 : V(I32LtU, 0x49, i_ii) \
127 : V(I32GtS, 0x4a, i_ii) \
128 : V(I32GtU, 0x4b, i_ii) \
129 : V(I32LeS, 0x4c, i_ii) \
130 : V(I32LeU, 0x4d, i_ii) \
131 : V(I32GeS, 0x4e, i_ii) \
132 : V(I32GeU, 0x4f, i_ii) \
133 : V(I64Eqz, 0x50, i_l) \
134 : V(I64Eq, 0x51, i_ll) \
135 : V(I64Ne, 0x52, i_ll) \
136 : V(I64LtS, 0x53, i_ll) \
137 : V(I64LtU, 0x54, i_ll) \
138 : V(I64GtS, 0x55, i_ll) \
139 : V(I64GtU, 0x56, i_ll) \
140 : V(I64LeS, 0x57, i_ll) \
141 : V(I64LeU, 0x58, i_ll) \
142 : V(I64GeS, 0x59, i_ll) \
143 : V(I64GeU, 0x5a, i_ll) \
144 : V(F32Eq, 0x5b, i_ff) \
145 : V(F32Ne, 0x5c, i_ff) \
146 : V(F32Lt, 0x5d, i_ff) \
147 : V(F32Gt, 0x5e, i_ff) \
148 : V(F32Le, 0x5f, i_ff) \
149 : V(F32Ge, 0x60, i_ff) \
150 : V(F64Eq, 0x61, i_dd) \
151 : V(F64Ne, 0x62, i_dd) \
152 : V(F64Lt, 0x63, i_dd) \
153 : V(F64Gt, 0x64, i_dd) \
154 : V(F64Le, 0x65, i_dd) \
155 : V(F64Ge, 0x66, i_dd) \
156 : V(I32Clz, 0x67, i_i) \
157 : V(I32Ctz, 0x68, i_i) \
158 : V(I32Popcnt, 0x69, i_i) \
159 : V(I32Add, 0x6a, i_ii) \
160 : V(I32Sub, 0x6b, i_ii) \
161 : V(I32Mul, 0x6c, i_ii) \
162 : V(I32DivS, 0x6d, i_ii) \
163 : V(I32DivU, 0x6e, i_ii) \
164 : V(I32RemS, 0x6f, i_ii) \
165 : V(I32RemU, 0x70, i_ii) \
166 : V(I32And, 0x71, i_ii) \
167 : V(I32Ior, 0x72, i_ii) \
168 : V(I32Xor, 0x73, i_ii) \
169 : V(I32Shl, 0x74, i_ii) \
170 : V(I32ShrS, 0x75, i_ii) \
171 : V(I32ShrU, 0x76, i_ii) \
172 : V(I32Rol, 0x77, i_ii) \
173 : V(I32Ror, 0x78, i_ii) \
174 : V(I64Clz, 0x79, l_l) \
175 : V(I64Ctz, 0x7a, l_l) \
176 : V(I64Popcnt, 0x7b, l_l) \
177 : V(I64Add, 0x7c, l_ll) \
178 : V(I64Sub, 0x7d, l_ll) \
179 : V(I64Mul, 0x7e, l_ll) \
180 : V(I64DivS, 0x7f, l_ll) \
181 : V(I64DivU, 0x80, l_ll) \
182 : V(I64RemS, 0x81, l_ll) \
183 : V(I64RemU, 0x82, l_ll) \
184 : V(I64And, 0x83, l_ll) \
185 : V(I64Ior, 0x84, l_ll) \
186 : V(I64Xor, 0x85, l_ll) \
187 : V(I64Shl, 0x86, l_ll) \
188 : V(I64ShrS, 0x87, l_ll) \
189 : V(I64ShrU, 0x88, l_ll) \
190 : V(I64Rol, 0x89, l_ll) \
191 : V(I64Ror, 0x8a, l_ll) \
192 : V(F32Abs, 0x8b, f_f) \
193 : V(F32Neg, 0x8c, f_f) \
194 : V(F32Ceil, 0x8d, f_f) \
195 : V(F32Floor, 0x8e, f_f) \
196 : V(F32Trunc, 0x8f, f_f) \
197 : V(F32NearestInt, 0x90, f_f) \
198 : V(F32Sqrt, 0x91, f_f) \
199 : V(F32Add, 0x92, f_ff) \
200 : V(F32Sub, 0x93, f_ff) \
201 : V(F32Mul, 0x94, f_ff) \
202 : V(F32Div, 0x95, f_ff) \
203 : V(F32Min, 0x96, f_ff) \
204 : V(F32Max, 0x97, f_ff) \
205 : V(F32CopySign, 0x98, f_ff) \
206 : V(F64Abs, 0x99, d_d) \
207 : V(F64Neg, 0x9a, d_d) \
208 : V(F64Ceil, 0x9b, d_d) \
209 : V(F64Floor, 0x9c, d_d) \
210 : V(F64Trunc, 0x9d, d_d) \
211 : V(F64NearestInt, 0x9e, d_d) \
212 : V(F64Sqrt, 0x9f, d_d) \
213 : V(F64Add, 0xa0, d_dd) \
214 : V(F64Sub, 0xa1, d_dd) \
215 : V(F64Mul, 0xa2, d_dd) \
216 : V(F64Div, 0xa3, d_dd) \
217 : V(F64Min, 0xa4, d_dd) \
218 : V(F64Max, 0xa5, d_dd) \
219 : V(F64CopySign, 0xa6, d_dd) \
220 : V(I32ConvertI64, 0xa7, i_l) \
221 : V(I32SConvertF32, 0xa8, i_f) \
222 : V(I32UConvertF32, 0xa9, i_f) \
223 : V(I32SConvertF64, 0xaa, i_d) \
224 : V(I32UConvertF64, 0xab, i_d) \
225 : V(I64SConvertI32, 0xac, l_i) \
226 : V(I64UConvertI32, 0xad, l_i) \
227 : V(I64SConvertF32, 0xae, l_f) \
228 : V(I64UConvertF32, 0xaf, l_f) \
229 : V(I64SConvertF64, 0xb0, l_d) \
230 : V(I64UConvertF64, 0xb1, l_d) \
231 : V(F32SConvertI32, 0xb2, f_i) \
232 : V(F32UConvertI32, 0xb3, f_i) \
233 : V(F32SConvertI64, 0xb4, f_l) \
234 : V(F32UConvertI64, 0xb5, f_l) \
235 : V(F32ConvertF64, 0xb6, f_d) \
236 : V(F64SConvertI32, 0xb7, d_i) \
237 : V(F64UConvertI32, 0xb8, d_i) \
238 : V(F64SConvertI64, 0xb9, d_l) \
239 : V(F64UConvertI64, 0xba, d_l) \
240 : V(F64ConvertF32, 0xbb, d_f) \
241 : V(I32ReinterpretF32, 0xbc, i_f) \
242 : V(I64ReinterpretF64, 0xbd, l_d) \
243 : V(F32ReinterpretI32, 0xbe, f_i) \
244 : V(F64ReinterpretI64, 0xbf, d_l)
245 :
246 : // For compatibility with Asm.js.
247 : #define FOREACH_ASMJS_COMPAT_OPCODE(V) \
248 : V(F64Acos, 0xc2, d_d) \
249 : V(F64Asin, 0xc3, d_d) \
250 : V(F64Atan, 0xc4, d_d) \
251 : V(F64Cos, 0xc5, d_d) \
252 : V(F64Sin, 0xc6, d_d) \
253 : V(F64Tan, 0xc7, d_d) \
254 : V(F64Exp, 0xc8, d_d) \
255 : V(F64Log, 0xc9, d_d) \
256 : V(F64Atan2, 0xca, d_dd) \
257 : V(F64Pow, 0xcb, d_dd) \
258 : V(F64Mod, 0xcc, d_dd) \
259 : V(I32AsmjsDivS, 0xd0, i_ii) \
260 : V(I32AsmjsDivU, 0xd1, i_ii) \
261 : V(I32AsmjsRemS, 0xd2, i_ii) \
262 : V(I32AsmjsRemU, 0xd3, i_ii) \
263 : V(I32AsmjsLoadMem8S, 0xd4, i_i) \
264 : V(I32AsmjsLoadMem8U, 0xd5, i_i) \
265 : V(I32AsmjsLoadMem16S, 0xd6, i_i) \
266 : V(I32AsmjsLoadMem16U, 0xd7, i_i) \
267 : V(I32AsmjsLoadMem, 0xd8, i_i) \
268 : V(F32AsmjsLoadMem, 0xd9, f_i) \
269 : V(F64AsmjsLoadMem, 0xda, d_i) \
270 : V(I32AsmjsStoreMem8, 0xdb, i_ii) \
271 : V(I32AsmjsStoreMem16, 0xdc, i_ii) \
272 : V(I32AsmjsStoreMem, 0xdd, i_ii) \
273 : V(F32AsmjsStoreMem, 0xde, f_if) \
274 : V(F64AsmjsStoreMem, 0xdf, d_id) \
275 : V(I32AsmjsSConvertF32, 0xe0, i_f) \
276 : V(I32AsmjsUConvertF32, 0xe1, i_f) \
277 : V(I32AsmjsSConvertF64, 0xe2, i_d) \
278 : V(I32AsmjsUConvertF64, 0xe3, i_d)
279 :
280 : #define FOREACH_SIMD_0_OPERAND_OPCODE(V) \
281 : V(F32x4Splat, 0xfd00, s_f) \
282 : V(F32x4Abs, 0xfd03, s_s) \
283 : V(F32x4Neg, 0xfd04, s_s) \
284 : V(F32x4RecipApprox, 0xfd06, s_s) \
285 : V(F32x4RecipSqrtApprox, 0xfd07, s_s) \
286 : V(F32x4Add, 0xfd08, s_ss) \
287 : V(F32x4AddHoriz, 0xfdb9, s_ss) \
288 : V(F32x4Sub, 0xfd09, s_ss) \
289 : V(F32x4Mul, 0xfd0a, s_ss) \
290 : V(F32x4Min, 0xfd0c, s_ss) \
291 : V(F32x4Max, 0xfd0d, s_ss) \
292 : V(F32x4Eq, 0xfd10, s_ss) \
293 : V(F32x4Ne, 0xfd11, s_ss) \
294 : V(F32x4Lt, 0xfd12, s_ss) \
295 : V(F32x4Le, 0xfd13, s_ss) \
296 : V(F32x4Gt, 0xfd14, s_ss) \
297 : V(F32x4Ge, 0xfd15, s_ss) \
298 : V(F32x4SConvertI32x4, 0xfd19, s_s) \
299 : V(F32x4UConvertI32x4, 0xfd1a, s_s) \
300 : V(I32x4Splat, 0xfd1b, s_i) \
301 : V(I32x4Neg, 0xfd1e, s_s) \
302 : V(I32x4Add, 0xfd1f, s_ss) \
303 : V(I32x4AddHoriz, 0xfdba, s_ss) \
304 : V(I32x4Sub, 0xfd20, s_ss) \
305 : V(I32x4Mul, 0xfd21, s_ss) \
306 : V(I32x4MinS, 0xfd22, s_ss) \
307 : V(I32x4MaxS, 0xfd23, s_ss) \
308 : V(I32x4Eq, 0xfd26, s_ss) \
309 : V(I32x4Ne, 0xfd27, s_ss) \
310 : V(I32x4LtS, 0xfd28, s_ss) \
311 : V(I32x4LeS, 0xfd29, s_ss) \
312 : V(I32x4GtS, 0xfd2a, s_ss) \
313 : V(I32x4GeS, 0xfd2b, s_ss) \
314 : V(I32x4SConvertF32x4, 0xfd2f, s_s) \
315 : V(I32x4UConvertF32x4, 0xfd37, s_s) \
316 : V(I32x4SConvertI16x8Low, 0xfd94, s_s) \
317 : V(I32x4SConvertI16x8High, 0xfd95, s_s) \
318 : V(I32x4UConvertI16x8Low, 0xfd96, s_s) \
319 : V(I32x4UConvertI16x8High, 0xfd97, s_s) \
320 : V(I32x4MinU, 0xfd30, s_ss) \
321 : V(I32x4MaxU, 0xfd31, s_ss) \
322 : V(I32x4LtU, 0xfd33, s_ss) \
323 : V(I32x4LeU, 0xfd34, s_ss) \
324 : V(I32x4GtU, 0xfd35, s_ss) \
325 : V(I32x4GeU, 0xfd36, s_ss) \
326 : V(I16x8Splat, 0xfd38, s_i) \
327 : V(I16x8Neg, 0xfd3b, s_s) \
328 : V(I16x8Add, 0xfd3c, s_ss) \
329 : V(I16x8AddSaturateS, 0xfd3d, s_ss) \
330 : V(I16x8AddHoriz, 0xfdbb, s_ss) \
331 : V(I16x8Sub, 0xfd3e, s_ss) \
332 : V(I16x8SubSaturateS, 0xfd3f, s_ss) \
333 : V(I16x8Mul, 0xfd40, s_ss) \
334 : V(I16x8MinS, 0xfd41, s_ss) \
335 : V(I16x8MaxS, 0xfd42, s_ss) \
336 : V(I16x8Eq, 0xfd45, s_ss) \
337 : V(I16x8Ne, 0xfd46, s_ss) \
338 : V(I16x8LtS, 0xfd47, s_ss) \
339 : V(I16x8LeS, 0xfd48, s_ss) \
340 : V(I16x8GtS, 0xfd49, s_ss) \
341 : V(I16x8GeS, 0xfd4a, s_ss) \
342 : V(I16x8AddSaturateU, 0xfd4e, s_ss) \
343 : V(I16x8SubSaturateU, 0xfd4f, s_ss) \
344 : V(I16x8MinU, 0xfd50, s_ss) \
345 : V(I16x8MaxU, 0xfd51, s_ss) \
346 : V(I16x8LtU, 0xfd53, s_ss) \
347 : V(I16x8LeU, 0xfd54, s_ss) \
348 : V(I16x8GtU, 0xfd55, s_ss) \
349 : V(I16x8GeU, 0xfd56, s_ss) \
350 : V(I16x8SConvertI32x4, 0xfd98, s_ss) \
351 : V(I16x8UConvertI32x4, 0xfd99, s_ss) \
352 : V(I16x8SConvertI8x16Low, 0xfd9a, s_s) \
353 : V(I16x8SConvertI8x16High, 0xfd9b, s_s) \
354 : V(I16x8UConvertI8x16Low, 0xfd9c, s_s) \
355 : V(I16x8UConvertI8x16High, 0xfd9d, s_s) \
356 : V(I8x16Splat, 0xfd57, s_i) \
357 : V(I8x16Neg, 0xfd5a, s_s) \
358 : V(I8x16Add, 0xfd5b, s_ss) \
359 : V(I8x16AddSaturateS, 0xfd5c, s_ss) \
360 : V(I8x16Sub, 0xfd5d, s_ss) \
361 : V(I8x16SubSaturateS, 0xfd5e, s_ss) \
362 : V(I8x16Mul, 0xfd5f, s_ss) \
363 : V(I8x16MinS, 0xfd60, s_ss) \
364 : V(I8x16MaxS, 0xfd61, s_ss) \
365 : V(I8x16Eq, 0xfd64, s_ss) \
366 : V(I8x16Ne, 0xfd65, s_ss) \
367 : V(I8x16LtS, 0xfd66, s_ss) \
368 : V(I8x16LeS, 0xfd67, s_ss) \
369 : V(I8x16GtS, 0xfd68, s_ss) \
370 : V(I8x16GeS, 0xfd69, s_ss) \
371 : V(I8x16AddSaturateU, 0xfd6d, s_ss) \
372 : V(I8x16SubSaturateU, 0xfd6e, s_ss) \
373 : V(I8x16MinU, 0xfd6f, s_ss) \
374 : V(I8x16MaxU, 0xfd70, s_ss) \
375 : V(I8x16LtU, 0xfd72, s_ss) \
376 : V(I8x16LeU, 0xfd73, s_ss) \
377 : V(I8x16GtU, 0xfd74, s_ss) \
378 : V(I8x16GeU, 0xfd75, s_ss) \
379 : V(I8x16SConvertI16x8, 0xfd9e, s_ss) \
380 : V(I8x16UConvertI16x8, 0xfd9f, s_ss) \
381 : V(S128And, 0xfd76, s_ss) \
382 : V(S128Or, 0xfd77, s_ss) \
383 : V(S128Xor, 0xfd78, s_ss) \
384 : V(S128Not, 0xfd79, s_s) \
385 : V(S128Select, 0xfd2c, s_sss) \
386 : V(S1x4AnyTrue, 0xfd84, i_s) \
387 : V(S1x4AllTrue, 0xfd85, i_s) \
388 : V(S1x8AnyTrue, 0xfd8a, i_s) \
389 : V(S1x8AllTrue, 0xfd8b, i_s) \
390 : V(S1x16AnyTrue, 0xfd90, i_s) \
391 : V(S1x16AllTrue, 0xfd91, i_s)
392 :
393 : #define FOREACH_SIMD_1_OPERAND_OPCODE(V) \
394 : V(F32x4ExtractLane, 0xfd01, _) \
395 : V(F32x4ReplaceLane, 0xfd02, _) \
396 : V(I32x4ExtractLane, 0xfd1c, _) \
397 : V(I32x4ReplaceLane, 0xfd1d, _) \
398 : V(I32x4Shl, 0xfd24, _) \
399 : V(I32x4ShrS, 0xfd25, _) \
400 : V(I32x4ShrU, 0xfd32, _) \
401 : V(I16x8ExtractLane, 0xfd39, _) \
402 : V(I16x8ReplaceLane, 0xfd3a, _) \
403 : V(I16x8Shl, 0xfd43, _) \
404 : V(I16x8ShrS, 0xfd44, _) \
405 : V(I16x8ShrU, 0xfd52, _) \
406 : V(I8x16ExtractLane, 0xfd58, _) \
407 : V(I8x16ReplaceLane, 0xfd59, _) \
408 : V(I8x16Shl, 0xfd62, _) \
409 : V(I8x16ShrS, 0xfd63, _) \
410 : V(I8x16ShrU, 0xfd71, _)
411 :
412 : #define FOREACH_SIMD_MASK_OPERAND_OPCODE(V) V(S8x16Shuffle, 0xfd6b, s_ss)
413 :
414 : #define FOREACH_SIMD_MEM_OPCODE(V) \
415 : V(S128LoadMem, 0xfd80, s_i) \
416 : V(S128StoreMem, 0xfd81, s_is)
417 :
418 : #define FOREACH_ATOMIC_OPCODE(V) \
419 : V(I32AtomicLoad, 0xfe10, i_i) \
420 : V(I32AtomicLoad8U, 0xfe12, i_i) \
421 : V(I32AtomicLoad16U, 0xfe13, i_i) \
422 : V(I32AtomicStore, 0xfe17, i_ii) \
423 : V(I32AtomicStore8U, 0xfe19, i_ii) \
424 : V(I32AtomicStore16U, 0xfe1a, i_ii) \
425 : V(I32AtomicAdd, 0xfe1e, i_ii) \
426 : V(I32AtomicAdd8U, 0xfe20, i_ii) \
427 : V(I32AtomicAdd16U, 0xfe21, i_ii) \
428 : V(I32AtomicSub, 0xfe25, i_ii) \
429 : V(I32AtomicSub8U, 0xfe27, i_ii) \
430 : V(I32AtomicSub16U, 0xfe28, i_ii) \
431 : V(I32AtomicAnd, 0xfe2c, i_ii) \
432 : V(I32AtomicAnd8U, 0xfe2e, i_ii) \
433 : V(I32AtomicAnd16U, 0xfe2f, i_ii) \
434 : V(I32AtomicOr, 0xfe33, i_ii) \
435 : V(I32AtomicOr8U, 0xfe35, i_ii) \
436 : V(I32AtomicOr16U, 0xfe36, i_ii) \
437 : V(I32AtomicXor, 0xfe3a, i_ii) \
438 : V(I32AtomicXor8U, 0xfe3c, i_ii) \
439 : V(I32AtomicXor16U, 0xfe3d, i_ii) \
440 : V(I32AtomicExchange, 0xfe41, i_ii) \
441 : V(I32AtomicExchange8U, 0xfe43, i_ii) \
442 : V(I32AtomicExchange16U, 0xfe44, i_ii) \
443 : V(I32AtomicCompareExchange, 0xfe48, i_iii) \
444 : V(I32AtomicCompareExchange8U, 0xfe4a, i_iii) \
445 : V(I32AtomicCompareExchange16U, 0xfe4b, i_iii)
446 :
447 : // All opcodes.
448 : #define FOREACH_OPCODE(V) \
449 : FOREACH_CONTROL_OPCODE(V) \
450 : FOREACH_MISC_OPCODE(V) \
451 : FOREACH_SIMPLE_OPCODE(V) \
452 : FOREACH_STORE_MEM_OPCODE(V) \
453 : FOREACH_LOAD_MEM_OPCODE(V) \
454 : FOREACH_MISC_MEM_OPCODE(V) \
455 : FOREACH_ASMJS_COMPAT_OPCODE(V) \
456 : FOREACH_SIMD_0_OPERAND_OPCODE(V) \
457 : FOREACH_SIMD_1_OPERAND_OPCODE(V) \
458 : FOREACH_SIMD_MASK_OPERAND_OPCODE(V) \
459 : FOREACH_SIMD_MEM_OPCODE(V) \
460 : FOREACH_ATOMIC_OPCODE(V)
461 :
462 : // All signatures.
463 : #define FOREACH_SIGNATURE(V) \
464 : FOREACH_SIMD_SIGNATURE(V) \
465 : V(i_ii, kWasmI32, kWasmI32, kWasmI32) \
466 : V(i_i, kWasmI32, kWasmI32) \
467 : V(i_v, kWasmI32) \
468 : V(i_ff, kWasmI32, kWasmF32, kWasmF32) \
469 : V(i_f, kWasmI32, kWasmF32) \
470 : V(i_dd, kWasmI32, kWasmF64, kWasmF64) \
471 : V(i_d, kWasmI32, kWasmF64) \
472 : V(i_l, kWasmI32, kWasmI64) \
473 : V(l_ll, kWasmI64, kWasmI64, kWasmI64) \
474 : V(i_ll, kWasmI32, kWasmI64, kWasmI64) \
475 : V(l_l, kWasmI64, kWasmI64) \
476 : V(l_i, kWasmI64, kWasmI32) \
477 : V(l_f, kWasmI64, kWasmF32) \
478 : V(l_d, kWasmI64, kWasmF64) \
479 : V(f_ff, kWasmF32, kWasmF32, kWasmF32) \
480 : V(f_f, kWasmF32, kWasmF32) \
481 : V(f_d, kWasmF32, kWasmF64) \
482 : V(f_i, kWasmF32, kWasmI32) \
483 : V(f_l, kWasmF32, kWasmI64) \
484 : V(d_dd, kWasmF64, kWasmF64, kWasmF64) \
485 : V(d_d, kWasmF64, kWasmF64) \
486 : V(d_f, kWasmF64, kWasmF32) \
487 : V(d_i, kWasmF64, kWasmI32) \
488 : V(d_l, kWasmF64, kWasmI64) \
489 : V(d_id, kWasmF64, kWasmI32, kWasmF64) \
490 : V(f_if, kWasmF32, kWasmI32, kWasmF32) \
491 : V(l_il, kWasmI64, kWasmI32, kWasmI64) \
492 : V(i_iii, kWasmI32, kWasmI32, kWasmI32, kWasmI32)
493 :
494 : #define FOREACH_SIMD_SIGNATURE(V) \
495 : V(s_s, kWasmS128, kWasmS128) \
496 : V(s_f, kWasmS128, kWasmF32) \
497 : V(s_ss, kWasmS128, kWasmS128, kWasmS128) \
498 : V(s_i, kWasmS128, kWasmI32) \
499 : V(s_si, kWasmS128, kWasmS128, kWasmI32) \
500 : V(i_s, kWasmI32, kWasmS128) \
501 : V(s_sss, kWasmS128, kWasmS128, kWasmS128, kWasmS128)
502 :
503 : #define FOREACH_PREFIX(V) \
504 : V(Simd, 0xfd) \
505 : V(Atomic, 0xfe)
506 :
507 : enum WasmOpcode {
508 : // Declare expression opcodes.
509 : #define DECLARE_NAMED_ENUM(name, opcode, sig) kExpr##name = opcode,
510 : FOREACH_OPCODE(DECLARE_NAMED_ENUM)
511 : #undef DECLARE_NAMED_ENUM
512 : #define DECLARE_PREFIX(name, opcode) k##name##Prefix = opcode,
513 : FOREACH_PREFIX(DECLARE_PREFIX)
514 : #undef DECLARE_PREFIX
515 : };
516 :
517 : // The reason for a trap.
518 : #define FOREACH_WASM_TRAPREASON(V) \
519 : V(TrapUnreachable) \
520 : V(TrapMemOutOfBounds) \
521 : V(TrapDivByZero) \
522 : V(TrapDivUnrepresentable) \
523 : V(TrapRemByZero) \
524 : V(TrapFloatUnrepresentable) \
525 : V(TrapFuncInvalid) \
526 : V(TrapFuncSigMismatch)
527 :
528 : enum TrapReason {
529 : #define DECLARE_ENUM(name) k##name,
530 : FOREACH_WASM_TRAPREASON(DECLARE_ENUM)
531 : kTrapCount
532 : #undef DECLARE_ENUM
533 : };
534 :
535 : // A collection of opcode-related static methods.
536 : class V8_EXPORT_PRIVATE WasmOpcodes {
537 : public:
538 : static const char* OpcodeName(WasmOpcode opcode);
539 : static FunctionSig* Signature(WasmOpcode opcode);
540 : static FunctionSig* AsmjsSignature(WasmOpcode opcode);
541 : static FunctionSig* AtomicSignature(WasmOpcode opcode);
542 : static bool IsPrefixOpcode(WasmOpcode opcode);
543 : static bool IsControlOpcode(WasmOpcode opcode);
544 : // Check whether the given opcode always jumps, i.e. all instructions after
545 : // this one in the current block are dead. Returns false for |end|.
546 : static bool IsUnconditionalJump(WasmOpcode opcode);
547 :
548 : static int TrapReasonToMessageId(TrapReason reason);
549 : static const char* TrapReasonMessage(TrapReason reason);
550 :
551 : static byte MemSize(MachineType type) {
552 938025 : return 1 << ElementSizeLog2Of(type.representation());
553 : }
554 :
555 : static byte MemSize(ValueType type) { return 1 << ElementSizeLog2Of(type); }
556 :
557 31733 : static ValueTypeCode ValueTypeCodeFor(ValueType type) {
558 31733 : switch (type) {
559 : case kWasmI32:
560 : return kLocalI32;
561 : case kWasmI64:
562 204 : return kLocalI64;
563 : case kWasmF32:
564 1592 : return kLocalF32;
565 : case kWasmF64:
566 5108 : return kLocalF64;
567 : case kWasmS128:
568 996 : return kLocalS128;
569 : case kWasmStmt:
570 18 : return kLocalVoid;
571 : default:
572 0 : UNREACHABLE();
573 : }
574 : }
575 :
576 916983 : static MachineType MachineTypeFor(ValueType type) {
577 916983 : 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 kWasmStmt:
589 : return MachineType::None();
590 : default:
591 0 : UNREACHABLE();
592 : }
593 : }
594 :
595 76452 : static ValueType ValueTypeFor(MachineType type) {
596 76452 : switch (type.representation()) {
597 : case MachineRepresentation::kWord8:
598 : case MachineRepresentation::kWord16:
599 : case MachineRepresentation::kWord32:
600 : return kWasmI32;
601 : case MachineRepresentation::kWord64:
602 : return kWasmI64;
603 : case MachineRepresentation::kFloat32:
604 : return kWasmF32;
605 : case MachineRepresentation::kFloat64:
606 : return kWasmF64;
607 : case MachineRepresentation::kSimd128:
608 : return kWasmS128;
609 : default:
610 0 : UNREACHABLE();
611 : }
612 : }
613 :
614 : static char ShortNameOf(ValueType type) {
615 : switch (type) {
616 : case kWasmI32:
617 : return 'i';
618 : case kWasmI64:
619 : return 'l';
620 : case kWasmF32:
621 : return 'f';
622 : case kWasmF64:
623 : return 'd';
624 : case kWasmS128:
625 : return 's';
626 : case kWasmStmt:
627 : return 'v';
628 : case kWasmVar:
629 : return '*';
630 : default:
631 : return '?';
632 : }
633 : }
634 :
635 43070 : static const char* TypeName(ValueType type) {
636 43070 : switch (type) {
637 : case kWasmI32:
638 : return "i32";
639 : case kWasmI64:
640 10482 : return "i64";
641 : case kWasmF32:
642 10380 : return "f32";
643 : case kWasmF64:
644 8066 : return "f64";
645 : case kWasmS128:
646 0 : return "s128";
647 : case kWasmStmt:
648 302 : return "<stmt>";
649 : case kWasmVar:
650 0 : return "<var>";
651 : default:
652 0 : return "<unknown>";
653 : }
654 : }
655 : };
656 :
657 : // Representation of an initializer expression.
658 : struct WasmInitExpr {
659 : enum WasmInitKind {
660 : kNone,
661 : kGlobalIndex,
662 : kI32Const,
663 : kI64Const,
664 : kF32Const,
665 : kF64Const
666 : } kind;
667 :
668 : union {
669 : int32_t i32_const;
670 : int64_t i64_const;
671 : float f32_const;
672 : double f64_const;
673 : uint32_t global_index;
674 : } val;
675 :
676 889191 : WasmInitExpr() : kind(kNone) {}
677 2817 : explicit WasmInitExpr(int32_t v) : kind(kI32Const) { val.i32_const = v; }
678 : explicit WasmInitExpr(int64_t v) : kind(kI64Const) { val.i64_const = v; }
679 84 : explicit WasmInitExpr(float v) : kind(kF32Const) { val.f32_const = v; }
680 571 : explicit WasmInitExpr(double v) : kind(kF64Const) { val.f64_const = v; }
681 18 : WasmInitExpr(WasmInitKind kind, uint32_t global_index) : kind(kGlobalIndex) {
682 18 : val.global_index = global_index;
683 : }
684 : };
685 :
686 : } // namespace wasm
687 : } // namespace internal
688 : } // namespace v8
689 :
690 : #endif // V8_WASM_OPCODES_H_
|