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_MACRO_GEN_H_
6 #define V8_WASM_MACRO_GEN_H_
7
8 #include "src/wasm/wasm-opcodes.h"
9
10 #include "src/zone/zone-containers.h"
11
12 #define U32_LE(v) \
13 static_cast<byte>(v), static_cast<byte>((v) >> 8), \
14 static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24)
15
16 #define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8)
17
18 #define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion)
19
20 #define SIG_INDEX(v) U32V_1(v)
21 #define FUNC_INDEX(v) U32V_1(v)
22 #define EXCEPTION_INDEX(v) U32V_1(v)
23 #define NO_NAME U32V_1(0)
24 #define ENTRY_COUNT(v) U32V_1(v)
25
26 // Segment flags
27 #define ACTIVE_NO_INDEX 0
28 #define PASSIVE 1
29 #define ACTIVE_WITH_INDEX 2
30 #define DECLARATIVE 3
31 #define PASSIVE_WITH_ELEMENTS 5
32 #define ACTIVE_WITH_ELEMENTS 6
33 #define DECLARATIVE_WITH_ELEMENTS 7
34
35 // The table index field in an element segment was repurposed as a flags field.
36 // To specify a table index, we have to set the flag value to 2, followed by
37 // the table index.
38 #define TABLE_INDEX0 static_cast<byte>(ACTIVE_NO_INDEX)
39 #define TABLE_INDEX(v) static_cast<byte>(ACTIVE_WITH_INDEX), U32V_1(v)
40
41 #define ZERO_ALIGNMENT 0
42 #define ZERO_OFFSET 0
43
44 #define BR_TARGET(v) U32V_1(v)
45
46 #define MASK_7 ((1 << 7) - 1)
47 #define MASK_14 ((1 << 14) - 1)
48 #define MASK_21 ((1 << 21) - 1)
49 #define MASK_28 ((1 << 28) - 1)
50
51 #define U32V_1(x) static_cast<byte>((x)&MASK_7)
52 #define U32V_2(x) \
53 static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7)
54 #define U32V_3(x) \
55 static_cast<byte>((((x)) & MASK_7) | 0x80), \
56 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
57 static_cast<byte>(((x) >> 14) & MASK_7)
58 #define U32V_4(x) \
59 static_cast<byte>(((x)&MASK_7) | 0x80), \
60 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
61 static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \
62 static_cast<byte>(((x) >> 21) & MASK_7)
63 #define U32V_5(x) \
64 static_cast<byte>(((x)&MASK_7) | 0x80), \
65 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
66 static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \
67 static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \
68 static_cast<byte>((((x) >> 28) & MASK_7))
69
70 #define U64V_1(x) U32V_1(static_cast<uint32_t>(x))
71 #define U64V_2(x) U32V_2(static_cast<uint32_t>(x))
72 #define U64V_3(x) U32V_3(static_cast<uint32_t>(x))
73 #define U64V_4(x) U32V_4(static_cast<uint32_t>(x))
74 #define U64V_5(x) \
75 static_cast<uint8_t>((uint64_t{x} & MASK_7) | 0x80), \
76 static_cast<uint8_t>(((uint64_t{x} >> 7) & MASK_7) | 0x80), \
77 static_cast<uint8_t>(((uint64_t{x} >> 14) & MASK_7) | 0x80), \
78 static_cast<uint8_t>(((uint64_t{x} >> 21) & MASK_7) | 0x80), \
79 static_cast<uint8_t>(((uint64_t{x} >> 28) & MASK_7))
80 #define U64V_6(x) \
81 static_cast<uint8_t>((uint64_t{x} & MASK_7) | 0x80), \
82 static_cast<uint8_t>(((uint64_t{x} >> 7) & MASK_7) | 0x80), \
83 static_cast<uint8_t>(((uint64_t{x} >> 14) & MASK_7) | 0x80), \
84 static_cast<uint8_t>(((uint64_t{x} >> 21) & MASK_7) | 0x80), \
85 static_cast<uint8_t>(((uint64_t{x} >> 28) & MASK_7) | 0x80), \
86 static_cast<uint8_t>(((uint64_t{x} >> 35) & MASK_7))
87 #define U64V_10(x) \
88 static_cast<uint8_t>((uint64_t{x} & MASK_7) | 0x80), \
89 static_cast<uint8_t>(((uint64_t{x} >> 7) & MASK_7) | 0x80), \
90 static_cast<uint8_t>(((uint64_t{x} >> 14) & MASK_7) | 0x80), \
91 static_cast<uint8_t>(((uint64_t{x} >> 21) & MASK_7) | 0x80), \
92 static_cast<uint8_t>(((uint64_t{x} >> 28) & MASK_7) | 0x80), \
93 static_cast<uint8_t>(((uint64_t{x} >> 35) & MASK_7) | 0x80), \
94 static_cast<uint8_t>(((uint64_t{x} >> 42) & MASK_7) | 0x80), \
95 static_cast<uint8_t>(((uint64_t{x} >> 49) & MASK_7) | 0x80), \
96 static_cast<uint8_t>(((uint64_t{x} >> 56) & MASK_7) | 0x80), \
97 static_cast<uint8_t>((uint64_t{x} >> 63) & MASK_7)
98
99 // Convenience macros for building Wasm bytecode directly into a byte array.
100
101 //------------------------------------------------------------------------------
102 // Control.
103 //------------------------------------------------------------------------------
104 #define WASM_NOP kExprNop
105 #define WASM_END kExprEnd
106
107 #define ARITY_0 0
108 #define ARITY_1 1
109 #define ARITY_2 2
110 #define DEPTH_0 0
111 #define DEPTH_1 1
112 #define DEPTH_2 2
113
114 #define WASM_HEAP_TYPE(heap_type) static_cast<byte>((heap_type).code() & 0x7f)
115
116 #define WASM_REF_TYPE(type) \
117 (type).kind() == kRef ? kRefCode : kOptRefCode, \
118 WASM_HEAP_TYPE((type).heap_type())
119
120 #define WASM_BLOCK(...) kExprBlock, kVoidCode, __VA_ARGS__, kExprEnd
121 #define WASM_BLOCK_I(...) kExprBlock, kI32Code, __VA_ARGS__, kExprEnd
122 #define WASM_BLOCK_L(...) kExprBlock, kI64Code, __VA_ARGS__, kExprEnd
123 #define WASM_BLOCK_F(...) kExprBlock, kF32Code, __VA_ARGS__, kExprEnd
124 #define WASM_BLOCK_D(...) kExprBlock, kF64Code, __VA_ARGS__, kExprEnd
125 #define WASM_BLOCK_T(t, ...) \
126 kExprBlock, static_cast<byte>((t).value_type_code()), __VA_ARGS__, kExprEnd
127
128 #define WASM_BLOCK_R(type, ...) \
129 kExprBlock, WASM_REF_TYPE(type), __VA_ARGS__, kExprEnd
130
131 #define WASM_BLOCK_X(index, ...) \
132 kExprBlock, static_cast<byte>(index), __VA_ARGS__, kExprEnd
133
134 #define WASM_INFINITE_LOOP kExprLoop, kVoidCode, kExprBr, DEPTH_0, kExprEnd
135
136 #define WASM_LOOP(...) kExprLoop, kVoidCode, __VA_ARGS__, kExprEnd
137 #define WASM_LOOP_I(...) kExprLoop, kI32Code, __VA_ARGS__, kExprEnd
138 #define WASM_LOOP_L(...) kExprLoop, kI64Code, __VA_ARGS__, kExprEnd
139 #define WASM_LOOP_F(...) kExprLoop, kF32Code, __VA_ARGS__, kExprEnd
140 #define WASM_LOOP_D(...) kExprLoop, kF64Code, __VA_ARGS__, kExprEnd
141
142 #define WASM_LOOP_T(t, ...) \
143 kExprLoop, static_cast<byte>((t).value_type_code()), __VA_ARGS__, kExprEnd
144
145 #define WASM_LOOP_R(t, ...) kExprLoop, TYPE_IMM(t), __VA_ARGS__, kExprEnd
146
147 #define WASM_LOOP_X(index, ...) \
148 kExprLoop, static_cast<byte>(index), __VA_ARGS__, kExprEnd
149
150 #define WASM_IF(cond, ...) cond, kExprIf, kVoidCode, __VA_ARGS__, kExprEnd
151
152 #define WASM_IF_T(t, cond, ...) \
153 cond, kExprIf, static_cast<byte>((t).value_type_code()), __VA_ARGS__, kExprEnd
154
155 #define WASM_IF_R(t, cond, ...) \
156 cond, kExprIf, TYPE_IMM(t), __VA_ARGS__, kExprEnd
157
158 #define WASM_IF_X(index, cond, ...) \
159 cond, kExprIf, static_cast<byte>(index), __VA_ARGS__, kExprEnd
160
161 #define WASM_IF_ELSE(cond, tstmt, fstmt) \
162 cond, kExprIf, kVoidCode, tstmt, kExprElse, fstmt, kExprEnd
163
164 #define WASM_IF_ELSE_I(cond, tstmt, fstmt) \
165 cond, kExprIf, kI32Code, tstmt, kExprElse, fstmt, kExprEnd
166 #define WASM_IF_ELSE_L(cond, tstmt, fstmt) \
167 cond, kExprIf, kI64Code, tstmt, kExprElse, fstmt, kExprEnd
168 #define WASM_IF_ELSE_F(cond, tstmt, fstmt) \
169 cond, kExprIf, kF32Code, tstmt, kExprElse, fstmt, kExprEnd
170 #define WASM_IF_ELSE_D(cond, tstmt, fstmt) \
171 cond, kExprIf, kF64Code, tstmt, kExprElse, fstmt, kExprEnd
172
173 #define WASM_IF_ELSE_T(t, cond, tstmt, fstmt) \
174 cond, kExprIf, static_cast<byte>((t).value_type_code()), tstmt, kExprElse, \
175 fstmt, kExprEnd
176
177 #define WASM_IF_ELSE_R(t, cond, tstmt, fstmt) \
178 cond, kExprIf, WASM_REF_TYPE(t), tstmt, kExprElse, fstmt, kExprEnd
179
180 #define WASM_IF_ELSE_X(index, cond, tstmt, fstmt) \
181 cond, kExprIf, static_cast<byte>(index), tstmt, kExprElse, fstmt, kExprEnd
182
183 #define WASM_TRY_T(t, trystmt) \
184 kExprTry, static_cast<byte>((t).value_type_code()), trystmt, kExprEnd
185 #define WASM_TRY_CATCH_T(t, trystmt, catchstmt, except) \
186 kExprTry, static_cast<byte>((t).value_type_code()), trystmt, kExprCatch, \
187 except, catchstmt, kExprEnd
188 #define WASM_TRY_CATCH_CATCH_T(t, trystmt, except1, catchstmt1, except2, \
189 catchstmt2) \
190 kExprTry, static_cast<byte>((t).value_type_code()), trystmt, kExprCatch, \
191 except1, catchstmt1, kExprCatch, except2, catchstmt2, kExprEnd
192 #define WASM_TRY_CATCH_R(t, trystmt, catchstmt) \
193 kExprTry, WASM_REF_TYPE(t), trystmt, kExprCatch, catchstmt, kExprEnd
194 #define WASM_TRY_CATCH_ALL_T(t, trystmt, catchstmt) \
195 kExprTry, static_cast<byte>((t).value_type_code()), trystmt, kExprCatchAll, \
196 catchstmt, kExprEnd
197 #define WASM_TRY_DELEGATE(trystmt, depth) \
198 kExprTry, kVoidCode, trystmt, kExprDelegate, depth
199 #define WASM_TRY_DELEGATE_T(t, trystmt, depth) \
200 kExprTry, static_cast<byte>((t).value_type_code()), trystmt, kExprDelegate, \
201 depth
202
203 #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect
204 #define WASM_SELECT_I(tval, fval, cond) \
205 tval, fval, cond, kExprSelectWithType, U32V_1(1), kI32Code
206 #define WASM_SELECT_L(tval, fval, cond) \
207 tval, fval, cond, kExprSelectWithType, U32V_1(1), kI64Code
208 #define WASM_SELECT_F(tval, fval, cond) \
209 tval, fval, cond, kExprSelectWithType, U32V_1(1), kF32Code
210 #define WASM_SELECT_D(tval, fval, cond) \
211 tval, fval, cond, kExprSelectWithType, U32V_1(1), kF64Code
212 #define WASM_SELECT_R(tval, fval, cond) \
213 tval, fval, cond, kExprSelectWithType, U32V_1(1), kExternRefCode
214 #define WASM_SELECT_A(tval, fval, cond) \
215 tval, fval, cond, kExprSelectWithType, U32V_1(1), kFuncRefCode
216
217 #define WASM_BR(depth) kExprBr, static_cast<byte>(depth)
218 #define WASM_BR_IF(depth, cond) cond, kExprBrIf, static_cast<byte>(depth)
219 #define WASM_BR_IFD(depth, val, cond) \
220 val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
221 #define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth)
222 #define WASM_UNREACHABLE kExprUnreachable
223 #define WASM_RETURN(...) __VA_ARGS__, kExprReturn
224 #define WASM_RETURN0 kExprReturn
225
226 #define WASM_BR_TABLE(key, count, ...) \
227 key, kExprBrTable, U32V_1(count), __VA_ARGS__
228
229 #define WASM_THROW(index) kExprThrow, static_cast<byte>(index)
230
231 //------------------------------------------------------------------------------
232 // Misc expressions.
233 //------------------------------------------------------------------------------
234 #define WASM_STMTS(...) __VA_ARGS__
235 #define WASM_ZERO WASM_I32V_1(0)
236 #define WASM_ONE WASM_I32V_1(1)
237 #define WASM_ZERO64 WASM_I64V_1(0)
238 #define WASM_ONE64 WASM_I64V_1(1)
239
240 #define I32V_MIN(length) -(1 << (6 + (7 * ((length)-1))))
241 #define I32V_MAX(length) ((1 << (6 + (7 * ((length)-1)))) - 1)
242 #define I64V_MIN(length) -(1LL << (6 + (7 * ((length)-1))))
243 #define I64V_MAX(length) ((1LL << (6 + 7 * ((length)-1))) - 1)
244
245 #define I32V_IN_RANGE(value, length) \
246 ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length))
247 #define I64V_IN_RANGE(value, length) \
248 ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length))
249
250 #define WASM_NO_LOCALS 0
251
252 //------------------------------------------------------------------------------
253 // Helpers for encoding sections and other fields with length prefix.
254 //------------------------------------------------------------------------------
255
256 template <typename... Args>
257 std::integral_constant<size_t, sizeof...(Args)> CountArgsHelper(Args...);
258 #define COUNT_ARGS(...) (decltype(CountArgsHelper(__VA_ARGS__))::value)
259
260 template <size_t num>
261 struct CheckLEB1 : std::integral_constant<size_t, num> {
262 static_assert(num <= I32V_MAX(1), "LEB range check");
263 };
264 #define CHECK_LEB1(num) CheckLEB1<num>::value
265
266 #define ADD_COUNT(...) CHECK_LEB1(COUNT_ARGS(__VA_ARGS__)), __VA_ARGS__
267
268 #define SECTION(name, ...) k##name##SectionCode, ADD_COUNT(__VA_ARGS__)
269
270 namespace v8 {
271 namespace internal {
272 namespace wasm {
273
CheckI32v(int32_t value,int length)274 inline void CheckI32v(int32_t value, int length) {
275 DCHECK(length >= 1 && length <= 5);
276 DCHECK(length == 5 || I32V_IN_RANGE(value, length));
277 }
278
CheckI64v(int64_t value,int length)279 inline void CheckI64v(int64_t value, int length) {
280 DCHECK(length >= 1 && length <= 10);
281 DCHECK(length == 10 || I64V_IN_RANGE(value, length));
282 }
283
LoadStoreOpcodeOf(MachineType type,bool store)284 inline WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
285 switch (type.representation()) {
286 case MachineRepresentation::kWord8:
287 return store ? kExprI32StoreMem8
288 : type.IsSigned() ? kExprI32LoadMem8S : kExprI32LoadMem8U;
289 case MachineRepresentation::kWord16:
290 return store ? kExprI32StoreMem16
291 : type.IsSigned() ? kExprI32LoadMem16S : kExprI32LoadMem16U;
292 case MachineRepresentation::kWord32:
293 return store ? kExprI32StoreMem : kExprI32LoadMem;
294 case MachineRepresentation::kWord64:
295 return store ? kExprI64StoreMem : kExprI64LoadMem;
296 case MachineRepresentation::kFloat32:
297 return store ? kExprF32StoreMem : kExprF32LoadMem;
298 case MachineRepresentation::kFloat64:
299 return store ? kExprF64StoreMem : kExprF64LoadMem;
300 case MachineRepresentation::kSimd128:
301 return store ? kExprS128StoreMem : kExprS128LoadMem;
302 default:
303 UNREACHABLE();
304 }
305 }
306
307 } // namespace wasm
308 } // namespace internal
309 } // namespace v8
310
311 //------------------------------------------------------------------------------
312 // Int32 Const operations
313 //------------------------------------------------------------------------------
314 #define WASM_I32V(val) kExprI32Const, U32V_5(val)
315
316 #define WASM_I32V_1(val) \
317 static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val)
318 #define WASM_I32V_2(val) \
319 static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val)
320 #define WASM_I32V_3(val) \
321 static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val)
322 #define WASM_I32V_4(val) \
323 static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val)
324 #define WASM_I32V_5(val) \
325 static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val)
326
327 //------------------------------------------------------------------------------
328 // Int64 Const operations
329 //------------------------------------------------------------------------------
330 #define WASM_I64V(val) \
331 kExprI64Const, \
332 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
333 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
334 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
335 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
336 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
337 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
338 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
339 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
340 static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
341 static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
342
343 #define WASM_I64V_1(val) \
344 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \
345 static_cast<byte>(static_cast<int64_t>(val) & MASK_7)
346 #define WASM_I64V_2(val) \
347 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), kExprI64Const), \
348 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
349 static_cast<byte>((static_cast<int64_t>(val) >> 7) & MASK_7)
350 #define WASM_I64V_3(val) \
351 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \
352 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
353 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
354 static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7)
355 #define WASM_I64V_4(val) \
356 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const), \
357 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
358 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
359 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
360 static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7)
361 #define WASM_I64V_5(val) \
362 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const), \
363 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
364 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
365 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
366 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
367 static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7)
368 #define WASM_I64V_6(val) \
369 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const), \
370 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
371 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
372 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
373 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
374 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
375 static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7)
376 #define WASM_I64V_7(val) \
377 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const), \
378 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
379 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
380 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
381 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
382 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
383 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
384 static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7)
385 #define WASM_I64V_8(val) \
386 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const), \
387 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
388 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
389 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
390 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
391 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
392 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
393 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
394 static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7)
395 #define WASM_I64V_9(val) \
396 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), kExprI64Const), \
397 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
398 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
399 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
400 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
401 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
402 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
403 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
404 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
405 static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7)
406 #define WASM_I64V_10(val) \
407 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \
408 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
409 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
410 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
411 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
412 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
413 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
414 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
415 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
416 static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
417 static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
418
419 #define WASM_F32(val) \
420 kExprF32Const, \
421 static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \
422 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \
423 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \
424 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24)
425 #define WASM_F64(val) \
426 kExprF64Const, \
427 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val))), \
428 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 8), \
429 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 16), \
430 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 24), \
431 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 32), \
432 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 40), \
433 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 48), \
434 static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 56)
435
436 #define WASM_LOCAL_GET(index) kExprLocalGet, static_cast<byte>(index)
437 #define WASM_LOCAL_SET(index, val) val, kExprLocalSet, static_cast<byte>(index)
438 #define WASM_LOCAL_TEE(index, val) val, kExprLocalTee, static_cast<byte>(index)
439 #define WASM_DROP kExprDrop
440 #define WASM_GLOBAL_GET(index) kExprGlobalGet, static_cast<byte>(index)
441 #define WASM_GLOBAL_SET(index, val) \
442 val, kExprGlobalSet, static_cast<byte>(index)
443 #define WASM_TABLE_GET(table_index, index) \
444 index, kExprTableGet, static_cast<byte>(table_index)
445 #define WASM_TABLE_SET(table_index, index, val) \
446 index, val, kExprTableSet, static_cast<byte>(table_index)
447 #define WASM_LOAD_MEM(type, index) \
448 index, \
449 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
450 ZERO_ALIGNMENT, ZERO_OFFSET
451 #define WASM_STORE_MEM(type, index, val) \
452 index, val, \
453 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
454 ZERO_ALIGNMENT, ZERO_OFFSET
455 #define WASM_LOAD_MEM_OFFSET(type, offset, index) \
456 index, \
457 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
458 ZERO_ALIGNMENT, offset
459 #define WASM_STORE_MEM_OFFSET(type, offset, index, val) \
460 index, val, \
461 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
462 ZERO_ALIGNMENT, offset
463 #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment) \
464 index, \
465 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
466 alignment, ZERO_OFFSET
467 #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val) \
468 index, val, \
469 static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
470 alignment, ZERO_OFFSET
471 #define WASM_RETHROW(index) kExprRethrow, static_cast<byte>(index)
472
473 #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index)
474 #define WASM_CALL_FUNCTION(index, ...) \
475 __VA_ARGS__, kExprCallFunction, static_cast<byte>(index)
476
477 #define WASM_RETURN_CALL_FUNCTION0(index) \
478 kExprReturnCall, static_cast<byte>(index)
479 #define WASM_RETURN_CALL_FUNCTION(index, ...) \
480 __VA_ARGS__, kExprReturnCall, static_cast<byte>(index)
481
482 #define TABLE_ZERO 0
483
484 //------------------------------------------------------------------------------
485 // Heap-allocated object operations.
486 //------------------------------------------------------------------------------
487 #define WASM_GC_OP(op) kGCPrefix, static_cast<byte>(op)
488 #define WASM_STRUCT_NEW(index, ...) \
489 __VA_ARGS__, WASM_GC_OP(kExprStructNew), static_cast<byte>(index)
490 #define WASM_STRUCT_NEW_WITH_RTT(index, ...) \
491 __VA_ARGS__, WASM_GC_OP(kExprStructNewWithRtt), static_cast<byte>(index)
492 #define WASM_STRUCT_NEW_DEFAULT(index) \
493 WASM_GC_OP(kExprStructNewDefault), static_cast<byte>(index)
494 #define WASM_STRUCT_NEW_DEFAULT_WITH_RTT(index, rtt) \
495 rtt, WASM_GC_OP(kExprStructNewDefaultWithRtt), static_cast<byte>(index)
496 #define WASM_STRUCT_GET(typeidx, fieldidx, struct_obj) \
497 struct_obj, WASM_GC_OP(kExprStructGet), static_cast<byte>(typeidx), \
498 static_cast<byte>(fieldidx)
499 #define WASM_STRUCT_GET_S(typeidx, fieldidx, struct_obj) \
500 struct_obj, WASM_GC_OP(kExprStructGetS), static_cast<byte>(typeidx), \
501 static_cast<byte>(fieldidx)
502 #define WASM_STRUCT_GET_U(typeidx, fieldidx, struct_obj) \
503 struct_obj, WASM_GC_OP(kExprStructGetU), static_cast<byte>(typeidx), \
504 static_cast<byte>(fieldidx)
505 #define WASM_STRUCT_SET(typeidx, fieldidx, struct_obj, value) \
506 struct_obj, value, WASM_GC_OP(kExprStructSet), static_cast<byte>(typeidx), \
507 static_cast<byte>(fieldidx)
508 #define WASM_REF_NULL(type_encoding) kExprRefNull, type_encoding
509 #define WASM_REF_FUNC(index) kExprRefFunc, index
510 #define WASM_REF_IS_NULL(val) val, kExprRefIsNull
511 #define WASM_REF_AS_NON_NULL(val) val, kExprRefAsNonNull
512 #define WASM_REF_EQ(lhs, rhs) lhs, rhs, kExprRefEq
513 #define WASM_REF_TEST(ref, rtt) ref, rtt, WASM_GC_OP(kExprRefTest)
514 #define WASM_REF_TEST_STATIC(ref, typeidx) \
515 ref, WASM_GC_OP(kExprRefTestStatic), static_cast<byte>(typeidx)
516 #define WASM_REF_CAST(ref, rtt) ref, rtt, WASM_GC_OP(kExprRefCast)
517 #define WASM_REF_CAST_STATIC(ref, typeidx) \
518 ref, WASM_GC_OP(kExprRefCastStatic), static_cast<byte>(typeidx)
519 // Takes a reference value from the value stack to allow sequences of
520 // conditional branches.
521 #define WASM_BR_ON_CAST(depth, rtt) \
522 rtt, WASM_GC_OP(kExprBrOnCast), static_cast<byte>(depth)
523 #define WASM_BR_ON_CAST_STATIC(depth, typeidx) \
524 WASM_GC_OP(kExprBrOnCastStatic), static_cast<byte>(depth), \
525 static_cast<byte>(typeidx)
526 #define WASM_BR_ON_CAST_FAIL(depth, rtt) \
527 rtt, WASM_GC_OP(kExprBrOnCastFail), static_cast<byte>(depth)
528 #define WASM_BR_ON_CAST_STATIC_FAIL(depth, typeidx) \
529 WASM_GC_OP(kExprBrOnCastStaticFail), static_cast<byte>(depth), \
530 static_cast<byte>(typeidx)
531
532 #define WASM_REF_IS_FUNC(ref) ref, WASM_GC_OP(kExprRefIsFunc)
533 #define WASM_REF_IS_DATA(ref) ref, WASM_GC_OP(kExprRefIsData)
534 #define WASM_REF_IS_I31(ref) ref, WASM_GC_OP(kExprRefIsI31)
535 #define WASM_REF_AS_FUNC(ref) ref, WASM_GC_OP(kExprRefAsFunc)
536 #define WASM_REF_AS_DATA(ref) ref, WASM_GC_OP(kExprRefAsData)
537 #define WASM_REF_AS_I31(ref) ref, WASM_GC_OP(kExprRefAsI31)
538 #define WASM_BR_ON_FUNC(depth) \
539 WASM_GC_OP(kExprBrOnFunc), static_cast<byte>(depth)
540 #define WASM_BR_ON_DATA(depth) \
541 WASM_GC_OP(kExprBrOnData), static_cast<byte>(depth)
542 #define WASM_BR_ON_I31(depth) WASM_GC_OP(kExprBrOnI31), static_cast<byte>(depth)
543 #define WASM_BR_ON_NON_FUNC(depth) \
544 WASM_GC_OP(kExprBrOnNonFunc), static_cast<byte>(depth)
545 #define WASM_BR_ON_NON_DATA(depth) \
546 WASM_GC_OP(kExprBrOnNonData), static_cast<byte>(depth)
547 #define WASM_BR_ON_NON_I31(depth) \
548 WASM_GC_OP(kExprBrOnNonI31), static_cast<byte>(depth)
549
550 #define WASM_ARRAY_NEW(index, default_value, length) \
551 default_value, length, WASM_GC_OP(kExprArrayNew), static_cast<byte>(index)
552 #define WASM_ARRAY_NEW_WITH_RTT(index, default_value, length, rtt) \
553 default_value, length, rtt, WASM_GC_OP(kExprArrayNewWithRtt), \
554 static_cast<byte>(index)
555 #define WASM_ARRAY_NEW_DEFAULT(index, length) \
556 length, WASM_GC_OP(kExprArrayNewDefault), static_cast<byte>(index)
557 #define WASM_ARRAY_NEW_DEFAULT_WITH_RTT(index, length, rtt) \
558 length, rtt, WASM_GC_OP(kExprArrayNewDefaultWithRtt), static_cast<byte>(index)
559 #define WASM_ARRAY_GET(typeidx, array, index) \
560 array, index, WASM_GC_OP(kExprArrayGet), static_cast<byte>(typeidx)
561 #define WASM_ARRAY_GET_U(typeidx, array, index) \
562 array, index, WASM_GC_OP(kExprArrayGetU), static_cast<byte>(typeidx)
563 #define WASM_ARRAY_GET_S(typeidx, array, index) \
564 array, index, WASM_GC_OP(kExprArrayGetS), static_cast<byte>(typeidx)
565 #define WASM_ARRAY_SET(typeidx, array, index, value) \
566 array, index, value, WASM_GC_OP(kExprArraySet), static_cast<byte>(typeidx)
567 #define WASM_ARRAY_LEN(typeidx, array) \
568 array, WASM_GC_OP(kExprArrayLen), static_cast<byte>(typeidx)
569 #define WASM_ARRAY_COPY(dst_idx, src_idx, dst_array, dst_index, src_array, \
570 src_index, length) \
571 dst_array, dst_index, src_array, src_index, length, \
572 WASM_GC_OP(kExprArrayCopy), static_cast<byte>(dst_idx), \
573 static_cast<byte>(src_idx)
574 #define WASM_ARRAY_INIT(index, length, ...) \
575 __VA_ARGS__, WASM_GC_OP(kExprArrayInit), static_cast<byte>(index), \
576 static_cast<byte>(length)
577 #define WASM_ARRAY_INIT_STATIC(index, length, ...) \
578 __VA_ARGS__, WASM_GC_OP(kExprArrayInitStatic), static_cast<byte>(index), \
579 static_cast<byte>(length)
580
581 #define WASM_RTT_WITH_DEPTH(depth, typeidx) \
582 kRttWithDepthCode, U32V_1(depth), U32V_1(typeidx)
583 #define WASM_RTT(typeidx) kRttCode, U32V_1(typeidx)
584 #define WASM_RTT_CANON(typeidx) \
585 WASM_GC_OP(kExprRttCanon), static_cast<byte>(typeidx)
586 #define WASM_RTT_SUB(typeidx, supertype) \
587 supertype, WASM_GC_OP(kExprRttSub), static_cast<byte>(typeidx)
588 #define WASM_RTT_FRESH_SUB(typeidx, supertype) \
589 supertype, WASM_GC_OP(kExprRttFreshSub), static_cast<byte>(typeidx)
590
591 #define WASM_I31_NEW(val) val, WASM_GC_OP(kExprI31New)
592 #define WASM_I31_GET_S(val) val, WASM_GC_OP(kExprI31GetS)
593 #define WASM_I31_GET_U(val) val, WASM_GC_OP(kExprI31GetU)
594
595 #define WASM_BR_ON_NULL(depth, ref_object) \
596 ref_object, kExprBrOnNull, static_cast<byte>(depth)
597
598 #define WASM_BR_ON_NON_NULL(depth, ref_object) \
599 ref_object, kExprBrOnNonNull, static_cast<byte>(depth)
600
601 // Pass: sig_index, ...args, func_index
602 #define WASM_CALL_INDIRECT(sig_index, ...) \
603 __VA_ARGS__, kExprCallIndirect, static_cast<byte>(sig_index), TABLE_ZERO
604 #define WASM_CALL_INDIRECT_TABLE(table, sig_index, ...) \
605 __VA_ARGS__, kExprCallIndirect, static_cast<byte>(sig_index), \
606 static_cast<byte>(table)
607 #define WASM_RETURN_CALL_INDIRECT(sig_index, ...) \
608 __VA_ARGS__, kExprReturnCallIndirect, static_cast<byte>(sig_index), TABLE_ZERO
609
610 #define WASM_CALL_REF(func_ref, ...) __VA_ARGS__, func_ref, kExprCallRef
611
612 #define WASM_RETURN_CALL_REF(func_ref, ...) \
613 __VA_ARGS__, func_ref, kExprReturnCallRef
614
615 // shift locals by 1; let (locals[0]: local_type) = value in ...
616 #define WASM_LET_1_V(local_type, value, ...) \
617 value, kExprLet, kVoidCode, U32V_1(1), U32V_1(1), local_type, __VA_ARGS__, \
618 kExprEnd
619 #define WASM_LET_1_I(local_type, value, ...) \
620 value, kExprLet, kI32Code, U32V_1(1), U32V_1(1), local_type, __VA_ARGS__, \
621 kExprEnd
622 // shift locals by 2;
623 // let (locals[0]: local_type_1) = value_1,
624 // (locals[1]: local_type_2) = value_2
625 // in ...
626 #define WASM_LET_2_I(local_type_1, value_1, local_type_2, value_2, ...) \
627 value_1, value_2, kExprLet, kI32Code, U32V_1(2), U32V_1(1), local_type_1, \
628 U32V_1(1), local_type_2, __VA_ARGS__, kExprEnd
629
630 #define WASM_NOT(x) x, kExprI32Eqz
631 #define WASM_SEQ(...) __VA_ARGS__
632
633 //------------------------------------------------------------------------------
634 // Constructs that are composed of multiple bytecodes.
635 //------------------------------------------------------------------------------
636 #define WASM_WHILE(x, y) \
637 kExprLoop, kVoidCode, x, kExprIf, kVoidCode, y, kExprBr, DEPTH_1, kExprEnd, \
638 kExprEnd
639 #define WASM_INC_LOCAL(index) \
640 kExprLocalGet, static_cast<byte>(index), kExprI32Const, 1, kExprI32Add, \
641 kExprLocalTee, static_cast<byte>(index)
642 #define WASM_INC_LOCAL_BYV(index, count) \
643 kExprLocalGet, static_cast<byte>(index), kExprI32Const, \
644 static_cast<byte>(count), kExprI32Add, kExprLocalTee, \
645 static_cast<byte>(index)
646 #define WASM_INC_LOCAL_BY(index, count) \
647 kExprLocalGet, static_cast<byte>(index), kExprI32Const, \
648 static_cast<byte>(count), kExprI32Add, kExprLocalSet, \
649 static_cast<byte>(index)
650 #define WASM_UNOP(opcode, x) x, static_cast<byte>(opcode)
651 #define WASM_BINOP(opcode, x, y) x, y, static_cast<byte>(opcode)
652
653 //------------------------------------------------------------------------------
654 // Int32 operations
655 //------------------------------------------------------------------------------
656 #define WASM_I32_ADD(x, y) x, y, kExprI32Add
657 #define WASM_I32_SUB(x, y) x, y, kExprI32Sub
658 #define WASM_I32_MUL(x, y) x, y, kExprI32Mul
659 #define WASM_I32_DIVS(x, y) x, y, kExprI32DivS
660 #define WASM_I32_DIVU(x, y) x, y, kExprI32DivU
661 #define WASM_I32_REMS(x, y) x, y, kExprI32RemS
662 #define WASM_I32_REMU(x, y) x, y, kExprI32RemU
663 #define WASM_I32_AND(x, y) x, y, kExprI32And
664 #define WASM_I32_IOR(x, y) x, y, kExprI32Ior
665 #define WASM_I32_XOR(x, y) x, y, kExprI32Xor
666 #define WASM_I32_SHL(x, y) x, y, kExprI32Shl
667 #define WASM_I32_SHR(x, y) x, y, kExprI32ShrU
668 #define WASM_I32_SAR(x, y) x, y, kExprI32ShrS
669 #define WASM_I32_ROR(x, y) x, y, kExprI32Ror
670 #define WASM_I32_ROL(x, y) x, y, kExprI32Rol
671 #define WASM_I32_EQ(x, y) x, y, kExprI32Eq
672 #define WASM_I32_NE(x, y) x, y, kExprI32Ne
673 #define WASM_I32_LTS(x, y) x, y, kExprI32LtS
674 #define WASM_I32_LES(x, y) x, y, kExprI32LeS
675 #define WASM_I32_LTU(x, y) x, y, kExprI32LtU
676 #define WASM_I32_LEU(x, y) x, y, kExprI32LeU
677 #define WASM_I32_GTS(x, y) x, y, kExprI32GtS
678 #define WASM_I32_GES(x, y) x, y, kExprI32GeS
679 #define WASM_I32_GTU(x, y) x, y, kExprI32GtU
680 #define WASM_I32_GEU(x, y) x, y, kExprI32GeU
681 #define WASM_I32_CLZ(x) x, kExprI32Clz
682 #define WASM_I32_CTZ(x) x, kExprI32Ctz
683 #define WASM_I32_POPCNT(x) x, kExprI32Popcnt
684 #define WASM_I32_EQZ(x) x, kExprI32Eqz
685
686 //------------------------------------------------------------------------------
687 // Asmjs Int32 operations
688 //------------------------------------------------------------------------------
689 #define WASM_I32_ASMJS_DIVS(x, y) x, y, kExprI32AsmjsDivS
690 #define WASM_I32_ASMJS_REMS(x, y) x, y, kExprI32AsmjsRemS
691 #define WASM_I32_ASMJS_DIVU(x, y) x, y, kExprI32AsmjsDivU
692 #define WASM_I32_ASMJS_REMU(x, y) x, y, kExprI32AsmjsRemU
693
694 //------------------------------------------------------------------------------
695 // Int64 operations
696 //------------------------------------------------------------------------------
697 #define WASM_I64_ADD(x, y) x, y, kExprI64Add
698 #define WASM_I64_SUB(x, y) x, y, kExprI64Sub
699 #define WASM_I64_MUL(x, y) x, y, kExprI64Mul
700 #define WASM_I64_DIVS(x, y) x, y, kExprI64DivS
701 #define WASM_I64_DIVU(x, y) x, y, kExprI64DivU
702 #define WASM_I64_REMS(x, y) x, y, kExprI64RemS
703 #define WASM_I64_REMU(x, y) x, y, kExprI64RemU
704 #define WASM_I64_AND(x, y) x, y, kExprI64And
705 #define WASM_I64_IOR(x, y) x, y, kExprI64Ior
706 #define WASM_I64_XOR(x, y) x, y, kExprI64Xor
707 #define WASM_I64_SHL(x, y) x, y, kExprI64Shl
708 #define WASM_I64_SHR(x, y) x, y, kExprI64ShrU
709 #define WASM_I64_SAR(x, y) x, y, kExprI64ShrS
710 #define WASM_I64_ROR(x, y) x, y, kExprI64Ror
711 #define WASM_I64_ROL(x, y) x, y, kExprI64Rol
712 #define WASM_I64_EQ(x, y) x, y, kExprI64Eq
713 #define WASM_I64_NE(x, y) x, y, kExprI64Ne
714 #define WASM_I64_LTS(x, y) x, y, kExprI64LtS
715 #define WASM_I64_LES(x, y) x, y, kExprI64LeS
716 #define WASM_I64_LTU(x, y) x, y, kExprI64LtU
717 #define WASM_I64_LEU(x, y) x, y, kExprI64LeU
718 #define WASM_I64_GTS(x, y) x, y, kExprI64GtS
719 #define WASM_I64_GES(x, y) x, y, kExprI64GeS
720 #define WASM_I64_GTU(x, y) x, y, kExprI64GtU
721 #define WASM_I64_GEU(x, y) x, y, kExprI64GeU
722 #define WASM_I64_CLZ(x) x, kExprI64Clz
723 #define WASM_I64_CTZ(x) x, kExprI64Ctz
724 #define WASM_I64_POPCNT(x) x, kExprI64Popcnt
725 #define WASM_I64_EQZ(x) x, kExprI64Eqz
726
727 //------------------------------------------------------------------------------
728 // Float32 operations
729 //------------------------------------------------------------------------------
730 #define WASM_F32_ADD(x, y) x, y, kExprF32Add
731 #define WASM_F32_SUB(x, y) x, y, kExprF32Sub
732 #define WASM_F32_MUL(x, y) x, y, kExprF32Mul
733 #define WASM_F32_DIV(x, y) x, y, kExprF32Div
734 #define WASM_F32_MIN(x, y) x, y, kExprF32Min
735 #define WASM_F32_MAX(x, y) x, y, kExprF32Max
736 #define WASM_F32_ABS(x) x, kExprF32Abs
737 #define WASM_F32_NEG(x) x, kExprF32Neg
738 #define WASM_F32_COPYSIGN(x, y) x, y, kExprF32CopySign
739 #define WASM_F32_CEIL(x) x, kExprF32Ceil
740 #define WASM_F32_FLOOR(x) x, kExprF32Floor
741 #define WASM_F32_TRUNC(x) x, kExprF32Trunc
742 #define WASM_F32_NEARESTINT(x) x, kExprF32NearestInt
743 #define WASM_F32_SQRT(x) x, kExprF32Sqrt
744 #define WASM_F32_EQ(x, y) x, y, kExprF32Eq
745 #define WASM_F32_NE(x, y) x, y, kExprF32Ne
746 #define WASM_F32_LT(x, y) x, y, kExprF32Lt
747 #define WASM_F32_LE(x, y) x, y, kExprF32Le
748 #define WASM_F32_GT(x, y) x, y, kExprF32Gt
749 #define WASM_F32_GE(x, y) x, y, kExprF32Ge
750
751 //------------------------------------------------------------------------------
752 // Float64 operations
753 //------------------------------------------------------------------------------
754 #define WASM_F64_ADD(x, y) x, y, kExprF64Add
755 #define WASM_F64_SUB(x, y) x, y, kExprF64Sub
756 #define WASM_F64_MUL(x, y) x, y, kExprF64Mul
757 #define WASM_F64_DIV(x, y) x, y, kExprF64Div
758 #define WASM_F64_MIN(x, y) x, y, kExprF64Min
759 #define WASM_F64_MAX(x, y) x, y, kExprF64Max
760 #define WASM_F64_ABS(x) x, kExprF64Abs
761 #define WASM_F64_NEG(x) x, kExprF64Neg
762 #define WASM_F64_COPYSIGN(x, y) x, y, kExprF64CopySign
763 #define WASM_F64_CEIL(x) x, kExprF64Ceil
764 #define WASM_F64_FLOOR(x) x, kExprF64Floor
765 #define WASM_F64_TRUNC(x) x, kExprF64Trunc
766 #define WASM_F64_NEARESTINT(x) x, kExprF64NearestInt
767 #define WASM_F64_SQRT(x) x, kExprF64Sqrt
768 #define WASM_F64_EQ(x, y) x, y, kExprF64Eq
769 #define WASM_F64_NE(x, y) x, y, kExprF64Ne
770 #define WASM_F64_LT(x, y) x, y, kExprF64Lt
771 #define WASM_F64_LE(x, y) x, y, kExprF64Le
772 #define WASM_F64_GT(x, y) x, y, kExprF64Gt
773 #define WASM_F64_GE(x, y) x, y, kExprF64Ge
774
775 //------------------------------------------------------------------------------
776 // Type conversions.
777 //------------------------------------------------------------------------------
778 #define WASM_I32_SCONVERT_F32(x) x, kExprI32SConvertF32
779 #define WASM_I32_SCONVERT_F64(x) x, kExprI32SConvertF64
780 #define WASM_I32_UCONVERT_F32(x) x, kExprI32UConvertF32
781 #define WASM_I32_UCONVERT_F64(x) x, kExprI32UConvertF64
782 #define WASM_I32_CONVERT_I64(x) x, kExprI32ConvertI64
783 #define WASM_I64_SCONVERT_F32(x) x, kExprI64SConvertF32
784 #define WASM_I64_SCONVERT_F64(x) x, kExprI64SConvertF64
785 #define WASM_I64_UCONVERT_F32(x) x, kExprI64UConvertF32
786 #define WASM_I64_UCONVERT_F64(x) x, kExprI64UConvertF64
787 #define WASM_I64_SCONVERT_I32(x) x, kExprI64SConvertI32
788 #define WASM_I64_UCONVERT_I32(x) x, kExprI64UConvertI32
789 #define WASM_F32_SCONVERT_I32(x) x, kExprF32SConvertI32
790 #define WASM_F32_UCONVERT_I32(x) x, kExprF32UConvertI32
791 #define WASM_F32_SCONVERT_I64(x) x, kExprF32SConvertI64
792 #define WASM_F32_UCONVERT_I64(x) x, kExprF32UConvertI64
793 #define WASM_F32_CONVERT_F64(x) x, kExprF32ConvertF64
794 #define WASM_F32_REINTERPRET_I32(x) x, kExprF32ReinterpretI32
795 #define WASM_F64_SCONVERT_I32(x) x, kExprF64SConvertI32
796 #define WASM_F64_UCONVERT_I32(x) x, kExprF64UConvertI32
797 #define WASM_F64_SCONVERT_I64(x) x, kExprF64SConvertI64
798 #define WASM_F64_UCONVERT_I64(x) x, kExprF64UConvertI64
799 #define WASM_F64_CONVERT_F32(x) x, kExprF64ConvertF32
800 #define WASM_F64_REINTERPRET_I64(x) x, kExprF64ReinterpretI64
801 #define WASM_I32_REINTERPRET_F32(x) x, kExprI32ReinterpretF32
802 #define WASM_I64_REINTERPRET_F64(x) x, kExprI64ReinterpretF64
803
804 //------------------------------------------------------------------------------
805 // Numeric operations
806 //------------------------------------------------------------------------------
807 #define WASM_NUMERIC_OP(op) kNumericPrefix, static_cast<byte>(op)
808 #define WASM_I32_SCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI32SConvertSatF32)
809 #define WASM_I32_UCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI32UConvertSatF32)
810 #define WASM_I32_SCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI32SConvertSatF64)
811 #define WASM_I32_UCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI32UConvertSatF64)
812 #define WASM_I64_SCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI64SConvertSatF32)
813 #define WASM_I64_UCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI64UConvertSatF32)
814 #define WASM_I64_SCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI64SConvertSatF64)
815 #define WASM_I64_UCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI64UConvertSatF64)
816
817 #define MEMORY_ZERO 0
818
819 #define WASM_MEMORY_INIT(seg, dst, src, size) \
820 dst, src, size, WASM_NUMERIC_OP(kExprMemoryInit), U32V_1(seg), MEMORY_ZERO
821 #define WASM_DATA_DROP(seg) WASM_NUMERIC_OP(kExprDataDrop), U32V_1(seg)
822 #define WASM_MEMORY_COPY(dst, src, size) \
823 dst, src, size, WASM_NUMERIC_OP(kExprMemoryCopy), MEMORY_ZERO, MEMORY_ZERO
824 #define WASM_MEMORY_FILL(dst, val, size) \
825 dst, val, size, WASM_NUMERIC_OP(kExprMemoryFill), MEMORY_ZERO
826 #define WASM_TABLE_INIT(table, seg, dst, src, size) \
827 dst, src, size, WASM_NUMERIC_OP(kExprTableInit), U32V_1(seg), \
828 static_cast<byte>(table)
829 #define WASM_ELEM_DROP(seg) WASM_NUMERIC_OP(kExprElemDrop), U32V_1(seg)
830 #define WASM_TABLE_COPY(table_dst, table_src, dst, src, size) \
831 dst, src, size, WASM_NUMERIC_OP(kExprTableCopy), \
832 static_cast<byte>(table_dst), static_cast<byte>(table_src)
833 #define WASM_TABLE_GROW(table, initial_value, delta) \
834 initial_value, delta, WASM_NUMERIC_OP(kExprTableGrow), \
835 static_cast<byte>(table)
836 #define WASM_TABLE_SIZE(table) \
837 WASM_NUMERIC_OP(kExprTableSize), static_cast<byte>(table)
838 #define WASM_TABLE_FILL(table, times, value, start) \
839 times, value, start, WASM_NUMERIC_OP(kExprTableFill), static_cast<byte>(table)
840
841 //------------------------------------------------------------------------------
842 // Memory Operations.
843 //------------------------------------------------------------------------------
844 #define WASM_MEMORY_GROW(x) x, kExprMemoryGrow, 0
845 #define WASM_MEMORY_SIZE kExprMemorySize, 0
846
847 #define SIG_ENTRY_v_v kWasmFunctionTypeCode, 0, 0
848 #define SIZEOF_SIG_ENTRY_v_v 3
849
850 #define SIG_ENTRY_v_x(a) kWasmFunctionTypeCode, 1, a, 0
851 #define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeCode, 2, a, b, 0
852 #define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 0
853 #define SIZEOF_SIG_ENTRY_v_x 4
854 #define SIZEOF_SIG_ENTRY_v_xx 5
855 #define SIZEOF_SIG_ENTRY_v_xxx 6
856
857 #define SIG_ENTRY_x(r) kWasmFunctionTypeCode, 0, 1, r
858 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeCode, 1, a, 1, r
859 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeCode, 2, a, b, 1, r
860 #define SIG_ENTRY_xx_xx(r, s, a, b) kWasmFunctionTypeCode, 2, a, b, 2, r, s
861 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 1, r
862 #define SIZEOF_SIG_ENTRY_x 4
863 #define SIZEOF_SIG_ENTRY_x_x 5
864 #define SIZEOF_SIG_ENTRY_x_xx 6
865 #define SIZEOF_SIG_ENTRY_xx_xx 7
866 #define SIZEOF_SIG_ENTRY_x_xxx 7
867
868 #define WASM_BRV(depth, ...) __VA_ARGS__, kExprBr, static_cast<byte>(depth)
869 #define WASM_BRV_IF(depth, val, cond) \
870 val, cond, kExprBrIf, static_cast<byte>(depth)
871 #define WASM_BRV_IFD(depth, val, cond) \
872 val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
873
874 //------------------------------------------------------------------------------
875 // Atomic Operations.
876 //------------------------------------------------------------------------------
877 #define WASM_ATOMICS_OP(op) kAtomicPrefix, static_cast<byte>(op)
878 #define WASM_ATOMICS_BINOP(op, x, y, representation) \
879 x, y, WASM_ATOMICS_OP(op), \
880 static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
881 #define WASM_ATOMICS_TERNARY_OP(op, x, y, z, representation) \
882 x, y, z, WASM_ATOMICS_OP(op), \
883 static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
884 #define WASM_ATOMICS_LOAD_OP(op, x, representation) \
885 x, WASM_ATOMICS_OP(op), \
886 static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
887 #define WASM_ATOMICS_STORE_OP(op, x, y, representation) \
888 x, y, WASM_ATOMICS_OP(op), \
889 static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
890 #define WASM_ATOMICS_WAIT(op, index, value, timeout, offset) \
891 index, value, timeout, WASM_ATOMICS_OP(op), ZERO_ALIGNMENT, offset
892 #define WASM_ATOMICS_FENCE WASM_ATOMICS_OP(kExprAtomicFence), ZERO_OFFSET
893
894 //------------------------------------------------------------------------------
895 // Sign Extension Operations.
896 //------------------------------------------------------------------------------
897 #define WASM_I32_SIGN_EXT_I8(x) x, kExprI32SExtendI8
898 #define WASM_I32_SIGN_EXT_I16(x) x, kExprI32SExtendI16
899 #define WASM_I64_SIGN_EXT_I8(x) x, kExprI64SExtendI8
900 #define WASM_I64_SIGN_EXT_I16(x) x, kExprI64SExtendI16
901 #define WASM_I64_SIGN_EXT_I32(x) x, kExprI64SExtendI32
902
903 //------------------------------------------------------------------------------
904 // SIMD Operations.
905 //------------------------------------------------------------------------------
906 #define TO_BYTE(val) static_cast<byte>(val)
907 // Encode all simd ops as a 2-byte LEB.
908 #define WASM_SIMD_OP(op) kSimdPrefix, U32V_2(op & 0xff)
909 #define WASM_SIMD_OPN(op, ...) __VA_ARGS__, WASM_SIMD_OP(op)
910 #define WASM_SIMD_SPLAT(Type, ...) __VA_ARGS__, WASM_SIMD_OP(kExpr##Type##Splat)
911 #define WASM_SIMD_UNOP(op, x) x, WASM_SIMD_OP(op)
912 #define WASM_SIMD_BINOP(op, x, y) x, y, WASM_SIMD_OP(op)
913 #define WASM_SIMD_SHIFT_OP(op, x, y) x, y, WASM_SIMD_OP(op)
914 #define WASM_SIMD_CONCAT_OP(op, bytes, x, y) \
915 x, y, WASM_SIMD_OP(op), TO_BYTE(bytes)
916 #define WASM_SIMD_SELECT(format, x, y, z) x, y, z, WASM_SIMD_OP(kExprS128Select)
917 #define WASM_SIMD_CONSTANT(v) \
918 WASM_SIMD_OP(kExprS128Const), TO_BYTE(v[0]), TO_BYTE(v[1]), TO_BYTE(v[2]), \
919 TO_BYTE(v[3]), TO_BYTE(v[4]), TO_BYTE(v[5]), TO_BYTE(v[6]), \
920 TO_BYTE(v[7]), TO_BYTE(v[8]), TO_BYTE(v[9]), TO_BYTE(v[10]), \
921 TO_BYTE(v[11]), TO_BYTE(v[12]), TO_BYTE(v[13]), TO_BYTE(v[14]), \
922 TO_BYTE(v[15])
923
924 #define WASM_SIMD_F64x2_SPLAT(x) WASM_SIMD_SPLAT(F64x2, x)
925 #define WASM_SIMD_F64x2_EXTRACT_LANE(lane, x) \
926 x, WASM_SIMD_OP(kExprF64x2ExtractLane), TO_BYTE(lane)
927 #define WASM_SIMD_F64x2_REPLACE_LANE(lane, x, y) \
928 x, y, WASM_SIMD_OP(kExprF64x2ReplaceLane), TO_BYTE(lane)
929
930 #define WASM_SIMD_F32x4_SPLAT(x) WASM_SIMD_SPLAT(F32x4, x)
931 #define WASM_SIMD_F32x4_EXTRACT_LANE(lane, x) \
932 x, WASM_SIMD_OP(kExprF32x4ExtractLane), TO_BYTE(lane)
933 #define WASM_SIMD_F32x4_REPLACE_LANE(lane, x, y) \
934 x, y, WASM_SIMD_OP(kExprF32x4ReplaceLane), TO_BYTE(lane)
935
936 #define WASM_SIMD_I64x2_SPLAT(x) WASM_SIMD_SPLAT(I64x2, x)
937 #define WASM_SIMD_I64x2_EXTRACT_LANE(lane, x) \
938 x, WASM_SIMD_OP(kExprI64x2ExtractLane), TO_BYTE(lane)
939 #define WASM_SIMD_I64x2_REPLACE_LANE(lane, x, y) \
940 x, y, WASM_SIMD_OP(kExprI64x2ReplaceLane), TO_BYTE(lane)
941
942 #define WASM_SIMD_I32x4_SPLAT(x) WASM_SIMD_SPLAT(I32x4, x)
943 #define WASM_SIMD_I32x4_EXTRACT_LANE(lane, x) \
944 x, WASM_SIMD_OP(kExprI32x4ExtractLane), TO_BYTE(lane)
945 #define WASM_SIMD_I32x4_REPLACE_LANE(lane, x, y) \
946 x, y, WASM_SIMD_OP(kExprI32x4ReplaceLane), TO_BYTE(lane)
947
948 #define WASM_SIMD_I16x8_SPLAT(x) WASM_SIMD_SPLAT(I16x8, x)
949 #define WASM_SIMD_I16x8_EXTRACT_LANE(lane, x) \
950 x, WASM_SIMD_OP(kExprI16x8ExtractLaneS), TO_BYTE(lane)
951 #define WASM_SIMD_I16x8_EXTRACT_LANE_U(lane, x) \
952 x, WASM_SIMD_OP(kExprI16x8ExtractLaneU), TO_BYTE(lane)
953 #define WASM_SIMD_I16x8_REPLACE_LANE(lane, x, y) \
954 x, y, WASM_SIMD_OP(kExprI16x8ReplaceLane), TO_BYTE(lane)
955
956 #define WASM_SIMD_I8x16_SPLAT(x) WASM_SIMD_SPLAT(I8x16, x)
957 #define WASM_SIMD_I8x16_EXTRACT_LANE(lane, x) \
958 x, WASM_SIMD_OP(kExprI8x16ExtractLaneS), TO_BYTE(lane)
959 #define WASM_SIMD_I8x16_EXTRACT_LANE_U(lane, x) \
960 x, WASM_SIMD_OP(kExprI8x16ExtractLaneU), TO_BYTE(lane)
961 #define WASM_SIMD_I8x16_REPLACE_LANE(lane, x, y) \
962 x, y, WASM_SIMD_OP(kExprI8x16ReplaceLane), TO_BYTE(lane)
963
964 #define WASM_SIMD_I8x16_SHUFFLE_OP(opcode, m, x, y) \
965 x, y, WASM_SIMD_OP(opcode), TO_BYTE(m[0]), TO_BYTE(m[1]), TO_BYTE(m[2]), \
966 TO_BYTE(m[3]), TO_BYTE(m[4]), TO_BYTE(m[5]), TO_BYTE(m[6]), \
967 TO_BYTE(m[7]), TO_BYTE(m[8]), TO_BYTE(m[9]), TO_BYTE(m[10]), \
968 TO_BYTE(m[11]), TO_BYTE(m[12]), TO_BYTE(m[13]), TO_BYTE(m[14]), \
969 TO_BYTE(m[15])
970
971 #define WASM_SIMD_LOAD_MEM(index) \
972 index, WASM_SIMD_OP(kExprS128LoadMem), ZERO_ALIGNMENT, ZERO_OFFSET
973 #define WASM_SIMD_LOAD_MEM_OFFSET(offset, index) \
974 index, WASM_SIMD_OP(kExprS128LoadMem), ZERO_ALIGNMENT, offset
975 #define WASM_SIMD_STORE_MEM(index, val) \
976 index, val, WASM_SIMD_OP(kExprS128StoreMem), ZERO_ALIGNMENT, ZERO_OFFSET
977 #define WASM_SIMD_STORE_MEM_OFFSET(offset, index, val) \
978 index, val, WASM_SIMD_OP(kExprS128StoreMem), ZERO_ALIGNMENT, offset
979
980 #define WASM_SIMD_F64x2_QFMA(a, b, c) a, b, c, WASM_SIMD_OP(kExprF64x2Qfma)
981 #define WASM_SIMD_F64x2_QFMS(a, b, c) a, b, c, WASM_SIMD_OP(kExprF64x2Qfms)
982 #define WASM_SIMD_F32x4_QFMA(a, b, c) a, b, c, WASM_SIMD_OP(kExprF32x4Qfma)
983 #define WASM_SIMD_F32x4_QFMS(a, b, c) a, b, c, WASM_SIMD_OP(kExprF32x4Qfms)
984
985 // Like WASM_SIMD_LOAD_MEM but needs the load opcode.
986 #define WASM_SIMD_LOAD_OP(opcode, index) \
987 index, WASM_SIMD_OP(opcode), ZERO_ALIGNMENT, ZERO_OFFSET
988 #define WASM_SIMD_LOAD_OP_OFFSET(opcode, index, offset) \
989 index, WASM_SIMD_OP(opcode), ZERO_ALIGNMENT, offset
990 #define WASM_SIMD_LOAD_OP_ALIGNMENT(opcode, index, alignment) \
991 index, WASM_SIMD_OP(opcode), alignment, ZERO_OFFSET
992
993 // Load a Simd lane from a numeric pointer. We need this because lanes are
994 // reversed in big endian. Note: a Simd value has {kSimd128Size / sizeof(*ptr)}
995 // lanes.
996 #ifdef V8_TARGET_BIG_ENDIAN
997 #define LANE(ptr, index) ptr[kSimd128Size / sizeof(*ptr) - (index)-1]
998 #else
999 #define LANE(ptr, index) ptr[index]
1000 #endif
1001
1002 //------------------------------------------------------------------------------
1003 // Compilation Hints.
1004 //------------------------------------------------------------------------------
1005 #define COMPILE_STRATEGY_DEFAULT (0x00)
1006 #define COMPILE_STRATEGY_LAZY (0x01)
1007 #define COMPILE_STRATEGY_EAGER (0x02)
1008 #define BASELINE_TIER_DEFAULT (0x00 << 2)
1009 #define BASELINE_TIER_BASELINE (0x01 << 2)
1010 #define BASELINE_TIER_OPTIMIZED (0x02 << 2)
1011 #define TOP_TIER_DEFAULT (0x00 << 4)
1012 #define TOP_TIER_BASELINE (0x01 << 4)
1013 #define TOP_TIER_OPTIMIZED (0x02 << 4)
1014
1015 #endif // V8_WASM_MACRO_GEN_H_
1016