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_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 :
31 : // The table index field in an element segment was repurposed as a flags field.
32 : // To specify a table index, we have to set the flag value to 2, followed by
33 : // the table index.
34 : #define TABLE_INDEX0 U32V_1(ACTIVE_NO_INDEX)
35 : #define TABLE_INDEX(v) U32V_1(ACTIVE_WITH_INDEX), U32V_1(v)
36 :
37 : #define ZERO_ALIGNMENT 0
38 : #define ZERO_OFFSET 0
39 :
40 : #define BR_TARGET(v) U32V_1(v)
41 :
42 : #define MASK_7 ((1 << 7) - 1)
43 : #define MASK_14 ((1 << 14) - 1)
44 : #define MASK_21 ((1 << 21) - 1)
45 : #define MASK_28 ((1 << 28) - 1)
46 :
47 : #define U32V_1(x) static_cast<byte>((x)&MASK_7)
48 : #define U32V_2(x) \
49 : static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7)
50 : #define U32V_3(x) \
51 : static_cast<byte>((((x)) & MASK_7) | 0x80), \
52 : static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
53 : static_cast<byte>(((x) >> 14) & MASK_7)
54 : #define U32V_4(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) | 0x80), \
58 : static_cast<byte>(((x) >> 21) & MASK_7)
59 : #define U32V_5(x) \
60 : static_cast<byte>(((x)&MASK_7) | 0x80), \
61 : static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
62 : static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \
63 : static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \
64 : static_cast<byte>((((x) >> 28) & MASK_7))
65 :
66 : // Convenience macros for building Wasm bytecode directly into a byte array.
67 :
68 : //------------------------------------------------------------------------------
69 : // Control.
70 : //------------------------------------------------------------------------------
71 : #define WASM_NOP kExprNop
72 : #define WASM_END kExprEnd
73 :
74 : #define ARITY_0 0
75 : #define ARITY_1 1
76 : #define ARITY_2 2
77 : #define DEPTH_0 0
78 : #define DEPTH_1 1
79 : #define DEPTH_2 2
80 : #define ARITY_2 2
81 :
82 : #define WASM_BLOCK(...) kExprBlock, kLocalVoid, __VA_ARGS__, kExprEnd
83 : #define WASM_BLOCK_I(...) kExprBlock, kLocalI32, __VA_ARGS__, kExprEnd
84 : #define WASM_BLOCK_L(...) kExprBlock, kLocalI64, __VA_ARGS__, kExprEnd
85 : #define WASM_BLOCK_F(...) kExprBlock, kLocalF32, __VA_ARGS__, kExprEnd
86 : #define WASM_BLOCK_D(...) kExprBlock, kLocalF64, __VA_ARGS__, kExprEnd
87 :
88 : #define WASM_BLOCK_T(t, ...) \
89 : kExprBlock, static_cast<byte>(ValueTypes::ValueTypeCodeFor(t)), __VA_ARGS__, \
90 : kExprEnd
91 :
92 : #define WASM_BLOCK_X(index, ...) \
93 : kExprBlock, static_cast<byte>(index), __VA_ARGS__, kExprEnd
94 :
95 : #define WASM_INFINITE_LOOP kExprLoop, kLocalVoid, kExprBr, DEPTH_0, kExprEnd
96 :
97 : #define WASM_LOOP(...) kExprLoop, kLocalVoid, __VA_ARGS__, kExprEnd
98 : #define WASM_LOOP_I(...) kExprLoop, kLocalI32, __VA_ARGS__, kExprEnd
99 : #define WASM_LOOP_L(...) kExprLoop, kLocalI64, __VA_ARGS__, kExprEnd
100 : #define WASM_LOOP_F(...) kExprLoop, kLocalF32, __VA_ARGS__, kExprEnd
101 : #define WASM_LOOP_D(...) kExprLoop, kLocalF64, __VA_ARGS__, kExprEnd
102 :
103 : #define WASM_LOOP_T(t, ...) \
104 : kExprLoop, static_cast<byte>(ValueTypes::ValueTypeCodeFor(t)), __VA_ARGS__, \
105 : kExprEnd
106 :
107 : #define WASM_LOOP_X(index, ...) \
108 : kExprLoop, static_cast<byte>(index), __VA_ARGS__, kExprEnd
109 :
110 : #define WASM_IF(cond, ...) cond, kExprIf, kLocalVoid, __VA_ARGS__, kExprEnd
111 :
112 : #define WASM_IF_T(t, cond, ...) \
113 : cond, kExprIf, static_cast<byte>(ValueTypes::ValueTypeCodeFor(t)), \
114 : __VA_ARGS__, kExprEnd
115 :
116 : #define WASM_IF_X(index, cond, ...) \
117 : cond, kExprIf, static_cast<byte>(index), __VA_ARGS__, kExprEnd
118 :
119 : #define WASM_IF_ELSE(cond, tstmt, fstmt) \
120 : cond, kExprIf, kLocalVoid, tstmt, kExprElse, fstmt, kExprEnd
121 :
122 : #define WASM_IF_ELSE_I(cond, tstmt, fstmt) \
123 : cond, kExprIf, kLocalI32, tstmt, kExprElse, fstmt, kExprEnd
124 : #define WASM_IF_ELSE_L(cond, tstmt, fstmt) \
125 : cond, kExprIf, kLocalI64, tstmt, kExprElse, fstmt, kExprEnd
126 : #define WASM_IF_ELSE_F(cond, tstmt, fstmt) \
127 : cond, kExprIf, kLocalF32, tstmt, kExprElse, fstmt, kExprEnd
128 : #define WASM_IF_ELSE_D(cond, tstmt, fstmt) \
129 : cond, kExprIf, kLocalF64, tstmt, kExprElse, fstmt, kExprEnd
130 :
131 : #define WASM_IF_ELSE_T(t, cond, tstmt, fstmt) \
132 : cond, kExprIf, static_cast<byte>(ValueTypes::ValueTypeCodeFor(t)), tstmt, \
133 : kExprElse, fstmt, kExprEnd
134 :
135 : #define WASM_IF_ELSE_X(index, cond, tstmt, fstmt) \
136 : cond, kExprIf, static_cast<byte>(index), tstmt, kExprElse, fstmt, kExprEnd
137 :
138 : #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect
139 :
140 : #define WASM_RETURN0 kExprReturn
141 : #define WASM_RETURN1(val) val, kExprReturn
142 : #define WASM_RETURNN(count, ...) __VA_ARGS__, kExprReturn
143 :
144 : #define WASM_BR(depth) kExprBr, static_cast<byte>(depth)
145 : #define WASM_BR_IF(depth, cond) cond, kExprBrIf, static_cast<byte>(depth)
146 : #define WASM_BR_IFD(depth, val, cond) \
147 : val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
148 : #define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth)
149 : #define WASM_UNREACHABLE kExprUnreachable
150 :
151 : #define WASM_BR_TABLE(key, count, ...) \
152 : key, kExprBrTable, U32V_1(count), __VA_ARGS__
153 :
154 : #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8)
155 : #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8)
156 :
157 : //------------------------------------------------------------------------------
158 : // Misc expressions.
159 : //------------------------------------------------------------------------------
160 : #define WASM_ID(...) __VA_ARGS__
161 : #define WASM_ZERO kExprI32Const, 0
162 : #define WASM_ONE kExprI32Const, 1
163 :
164 : #define I32V_MIN(length) -(1 << (6 + (7 * ((length)-1))))
165 : #define I32V_MAX(length) ((1 << (6 + (7 * ((length)-1)))) - 1)
166 : #define I64V_MIN(length) -(1LL << (6 + (7 * ((length)-1))))
167 : #define I64V_MAX(length) ((1LL << (6 + 7 * ((length)-1))) - 1)
168 :
169 : #define I32V_IN_RANGE(value, length) \
170 : ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length))
171 : #define I64V_IN_RANGE(value, length) \
172 : ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length))
173 :
174 : #define WASM_NO_LOCALS 0
175 :
176 : namespace v8 {
177 : namespace internal {
178 : namespace wasm {
179 :
180 : inline void CheckI32v(int32_t value, int length) {
181 : DCHECK(length >= 1 && length <= 5);
182 : DCHECK(length == 5 || I32V_IN_RANGE(value, length));
183 : }
184 :
185 : inline void CheckI64v(int64_t value, int length) {
186 : DCHECK(length >= 1 && length <= 10);
187 : DCHECK(length == 10 || I64V_IN_RANGE(value, length));
188 : }
189 :
190 82207 : inline WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
191 82207 : switch (type.representation()) {
192 : case MachineRepresentation::kWord8:
193 : return store ? kExprI32StoreMem8
194 402 : : type.IsSigned() ? kExprI32LoadMem8S : kExprI32LoadMem8U;
195 : case MachineRepresentation::kWord16:
196 : return store ? kExprI32StoreMem16
197 342 : : type.IsSigned() ? kExprI32LoadMem16S : kExprI32LoadMem16U;
198 : case MachineRepresentation::kWord32:
199 41491 : return store ? kExprI32StoreMem : kExprI32LoadMem;
200 : case MachineRepresentation::kWord64:
201 31948 : return store ? kExprI64StoreMem : kExprI64LoadMem;
202 : case MachineRepresentation::kFloat32:
203 3862 : return store ? kExprF32StoreMem : kExprF32LoadMem;
204 : case MachineRepresentation::kFloat64:
205 4445 : return store ? kExprF64StoreMem : kExprF64LoadMem;
206 : case MachineRepresentation::kSimd128:
207 0 : return store ? kExprS128StoreMem : kExprS128LoadMem;
208 : default:
209 0 : UNREACHABLE();
210 : }
211 : }
212 :
213 : } // namespace wasm
214 : } // namespace internal
215 : } // namespace v8
216 :
217 : //------------------------------------------------------------------------------
218 : // Int32 Const operations
219 : //------------------------------------------------------------------------------
220 : #define WASM_I32V(val) kExprI32Const, U32V_5(val)
221 :
222 : #define WASM_I32V_1(val) \
223 : static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val)
224 : #define WASM_I32V_2(val) \
225 : static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val)
226 : #define WASM_I32V_3(val) \
227 : static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val)
228 : #define WASM_I32V_4(val) \
229 : static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val)
230 : #define WASM_I32V_5(val) \
231 : static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val)
232 :
233 : //------------------------------------------------------------------------------
234 : // Int64 Const operations
235 : //------------------------------------------------------------------------------
236 : #define WASM_I64V(val) \
237 : kExprI64Const, \
238 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
239 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
240 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
241 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
242 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
243 : static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
244 : static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
245 : static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
246 : static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
247 : static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
248 :
249 : #define WASM_I64V_1(val) \
250 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \
251 : static_cast<byte>(static_cast<int64_t>(val) & MASK_7)
252 : #define WASM_I64V_2(val) \
253 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), kExprI64Const), \
254 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
255 : static_cast<byte>((static_cast<int64_t>(val) >> 7) & MASK_7)
256 : #define WASM_I64V_3(val) \
257 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \
258 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
259 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
260 : static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7)
261 : #define WASM_I64V_4(val) \
262 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const), \
263 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
264 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
265 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
266 : static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7)
267 : #define WASM_I64V_5(val) \
268 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const), \
269 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
270 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
271 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
272 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
273 : static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7)
274 : #define WASM_I64V_6(val) \
275 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const), \
276 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
277 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
278 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
279 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
280 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
281 : static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7)
282 : #define WASM_I64V_7(val) \
283 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const), \
284 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
285 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
286 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
287 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
288 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
289 : static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
290 : static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7)
291 : #define WASM_I64V_8(val) \
292 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const), \
293 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
294 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
295 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
296 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
297 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
298 : static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
299 : static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
300 : static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7)
301 : #define WASM_I64V_9(val) \
302 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), kExprI64Const), \
303 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
304 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
305 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
306 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
307 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
308 : static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
309 : static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
310 : static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
311 : static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7)
312 : #define WASM_I64V_10(val) \
313 : static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \
314 : static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \
315 : static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
316 : static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
317 : static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
318 : static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
319 : static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
320 : static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
321 : static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
322 : static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
323 : static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
324 :
325 : #define WASM_F32(val) \
326 : kExprF32Const, \
327 : static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \
328 : static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \
329 : static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \
330 : static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24)
331 : #define WASM_F64(val) \
332 : kExprF64Const, \
333 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val))), \
334 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 8), \
335 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 16), \
336 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 24), \
337 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 32), \
338 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 40), \
339 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 48), \
340 : static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 56)
341 :
342 : #define WASM_REF_NULL kExprRefNull
343 :
344 : #define WASM_GET_LOCAL(index) kExprGetLocal, static_cast<byte>(index)
345 : #define WASM_SET_LOCAL(index, val) val, kExprSetLocal, static_cast<byte>(index)
346 : #define WASM_TEE_LOCAL(index, val) val, kExprTeeLocal, static_cast<byte>(index)
347 : #define WASM_DROP kExprDrop
348 : #define WASM_GET_GLOBAL(index) kExprGetGlobal, static_cast<byte>(index)
349 : #define WASM_SET_GLOBAL(index, val) \
350 : val, kExprSetGlobal, static_cast<byte>(index)
351 : #define WASM_LOAD_MEM(type, index) \
352 : index, \
353 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
354 : ZERO_ALIGNMENT, ZERO_OFFSET
355 : #define WASM_STORE_MEM(type, index, val) \
356 : index, val, \
357 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
358 : ZERO_ALIGNMENT, ZERO_OFFSET
359 : #define WASM_LOAD_MEM_OFFSET(type, offset, index) \
360 : index, \
361 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
362 : ZERO_ALIGNMENT, offset
363 : #define WASM_STORE_MEM_OFFSET(type, offset, index, val) \
364 : index, val, \
365 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
366 : ZERO_ALIGNMENT, offset
367 : #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment) \
368 : index, \
369 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, false)), \
370 : alignment, ZERO_OFFSET
371 : #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val) \
372 : index, val, \
373 : static_cast<byte>(v8::internal::wasm::LoadStoreOpcodeOf(type, true)), \
374 : alignment, ZERO_OFFSET
375 :
376 : #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index)
377 : #define WASM_CALL_FUNCTION(index, ...) \
378 : __VA_ARGS__, kExprCallFunction, static_cast<byte>(index)
379 :
380 : #define TABLE_ZERO 0
381 :
382 : // TODO(titzer): change usages of these macros to put func last.
383 : #define WASM_CALL_INDIRECT0(index, func) \
384 : func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
385 : #define WASM_CALL_INDIRECT1(index, func, a) \
386 : a, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
387 : #define WASM_CALL_INDIRECT2(index, func, a, b) \
388 : a, b, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
389 : #define WASM_CALL_INDIRECT3(index, func, a, b, c) \
390 : a, b, c, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
391 : #define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \
392 : a, b, c, d, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
393 : #define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \
394 : a, b, c, d, e, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
395 : #define WASM_CALL_INDIRECTN(arity, index, func, ...) \
396 : __VA_ARGS__, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
397 :
398 : #define WASM_NOT(x) x, kExprI32Eqz
399 : #define WASM_SEQ(...) __VA_ARGS__
400 :
401 : //------------------------------------------------------------------------------
402 : // Constructs that are composed of multiple bytecodes.
403 : //------------------------------------------------------------------------------
404 : #define WASM_WHILE(x, y) \
405 : kExprLoop, kLocalVoid, x, kExprIf, kLocalVoid, y, kExprBr, DEPTH_1, \
406 : kExprEnd, kExprEnd
407 : #define WASM_INC_LOCAL(index) \
408 : kExprGetLocal, static_cast<byte>(index), kExprI32Const, 1, kExprI32Add, \
409 : kExprTeeLocal, static_cast<byte>(index)
410 : #define WASM_INC_LOCAL_BYV(index, count) \
411 : kExprGetLocal, static_cast<byte>(index), kExprI32Const, \
412 : static_cast<byte>(count), kExprI32Add, kExprTeeLocal, \
413 : static_cast<byte>(index)
414 : #define WASM_INC_LOCAL_BY(index, count) \
415 : kExprGetLocal, static_cast<byte>(index), kExprI32Const, \
416 : static_cast<byte>(count), kExprI32Add, kExprSetLocal, \
417 : static_cast<byte>(index)
418 : #define WASM_UNOP(opcode, x) x, static_cast<byte>(opcode)
419 : #define WASM_BINOP(opcode, x, y) x, y, static_cast<byte>(opcode)
420 :
421 : //------------------------------------------------------------------------------
422 : // Int32 operations
423 : //------------------------------------------------------------------------------
424 : #define WASM_I32_ADD(x, y) x, y, kExprI32Add
425 : #define WASM_I32_SUB(x, y) x, y, kExprI32Sub
426 : #define WASM_I32_MUL(x, y) x, y, kExprI32Mul
427 : #define WASM_I32_DIVS(x, y) x, y, kExprI32DivS
428 : #define WASM_I32_DIVU(x, y) x, y, kExprI32DivU
429 : #define WASM_I32_REMS(x, y) x, y, kExprI32RemS
430 : #define WASM_I32_REMU(x, y) x, y, kExprI32RemU
431 : #define WASM_I32_AND(x, y) x, y, kExprI32And
432 : #define WASM_I32_IOR(x, y) x, y, kExprI32Ior
433 : #define WASM_I32_XOR(x, y) x, y, kExprI32Xor
434 : #define WASM_I32_SHL(x, y) x, y, kExprI32Shl
435 : #define WASM_I32_SHR(x, y) x, y, kExprI32ShrU
436 : #define WASM_I32_SAR(x, y) x, y, kExprI32ShrS
437 : #define WASM_I32_ROR(x, y) x, y, kExprI32Ror
438 : #define WASM_I32_ROL(x, y) x, y, kExprI32Rol
439 : #define WASM_I32_EQ(x, y) x, y, kExprI32Eq
440 : #define WASM_I32_NE(x, y) x, y, kExprI32Ne
441 : #define WASM_I32_LTS(x, y) x, y, kExprI32LtS
442 : #define WASM_I32_LES(x, y) x, y, kExprI32LeS
443 : #define WASM_I32_LTU(x, y) x, y, kExprI32LtU
444 : #define WASM_I32_LEU(x, y) x, y, kExprI32LeU
445 : #define WASM_I32_GTS(x, y) x, y, kExprI32GtS
446 : #define WASM_I32_GES(x, y) x, y, kExprI32GeS
447 : #define WASM_I32_GTU(x, y) x, y, kExprI32GtU
448 : #define WASM_I32_GEU(x, y) x, y, kExprI32GeU
449 : #define WASM_I32_CLZ(x) x, kExprI32Clz
450 : #define WASM_I32_CTZ(x) x, kExprI32Ctz
451 : #define WASM_I32_POPCNT(x) x, kExprI32Popcnt
452 : #define WASM_I32_EQZ(x) x, kExprI32Eqz
453 :
454 : //------------------------------------------------------------------------------
455 : // Asmjs Int32 operations
456 : //------------------------------------------------------------------------------
457 : #define WASM_I32_ASMJS_DIVS(x, y) x, y, kExprI32AsmjsDivS
458 : #define WASM_I32_ASMJS_REMS(x, y) x, y, kExprI32AsmjsRemS
459 : #define WASM_I32_ASMJS_DIVU(x, y) x, y, kExprI32AsmjsDivU
460 : #define WASM_I32_ASMJS_REMU(x, y) x, y, kExprI32AsmjsRemU
461 :
462 : //------------------------------------------------------------------------------
463 : // Int64 operations
464 : //------------------------------------------------------------------------------
465 : #define WASM_I64_ADD(x, y) x, y, kExprI64Add
466 : #define WASM_I64_SUB(x, y) x, y, kExprI64Sub
467 : #define WASM_I64_MUL(x, y) x, y, kExprI64Mul
468 : #define WASM_I64_DIVS(x, y) x, y, kExprI64DivS
469 : #define WASM_I64_DIVU(x, y) x, y, kExprI64DivU
470 : #define WASM_I64_REMS(x, y) x, y, kExprI64RemS
471 : #define WASM_I64_REMU(x, y) x, y, kExprI64RemU
472 : #define WASM_I64_AND(x, y) x, y, kExprI64And
473 : #define WASM_I64_IOR(x, y) x, y, kExprI64Ior
474 : #define WASM_I64_XOR(x, y) x, y, kExprI64Xor
475 : #define WASM_I64_SHL(x, y) x, y, kExprI64Shl
476 : #define WASM_I64_SHR(x, y) x, y, kExprI64ShrU
477 : #define WASM_I64_SAR(x, y) x, y, kExprI64ShrS
478 : #define WASM_I64_ROR(x, y) x, y, kExprI64Ror
479 : #define WASM_I64_ROL(x, y) x, y, kExprI64Rol
480 : #define WASM_I64_EQ(x, y) x, y, kExprI64Eq
481 : #define WASM_I64_NE(x, y) x, y, kExprI64Ne
482 : #define WASM_I64_LTS(x, y) x, y, kExprI64LtS
483 : #define WASM_I64_LES(x, y) x, y, kExprI64LeS
484 : #define WASM_I64_LTU(x, y) x, y, kExprI64LtU
485 : #define WASM_I64_LEU(x, y) x, y, kExprI64LeU
486 : #define WASM_I64_GTS(x, y) x, y, kExprI64GtS
487 : #define WASM_I64_GES(x, y) x, y, kExprI64GeS
488 : #define WASM_I64_GTU(x, y) x, y, kExprI64GtU
489 : #define WASM_I64_GEU(x, y) x, y, kExprI64GeU
490 : #define WASM_I64_CLZ(x) x, kExprI64Clz
491 : #define WASM_I64_CTZ(x) x, kExprI64Ctz
492 : #define WASM_I64_POPCNT(x) x, kExprI64Popcnt
493 : #define WASM_I64_EQZ(x) x, kExprI64Eqz
494 :
495 : //------------------------------------------------------------------------------
496 : // Float32 operations
497 : //------------------------------------------------------------------------------
498 : #define WASM_F32_ADD(x, y) x, y, kExprF32Add
499 : #define WASM_F32_SUB(x, y) x, y, kExprF32Sub
500 : #define WASM_F32_MUL(x, y) x, y, kExprF32Mul
501 : #define WASM_F32_DIV(x, y) x, y, kExprF32Div
502 : #define WASM_F32_MIN(x, y) x, y, kExprF32Min
503 : #define WASM_F32_MAX(x, y) x, y, kExprF32Max
504 : #define WASM_F32_ABS(x) x, kExprF32Abs
505 : #define WASM_F32_NEG(x) x, kExprF32Neg
506 : #define WASM_F32_COPYSIGN(x, y) x, y, kExprF32CopySign
507 : #define WASM_F32_CEIL(x) x, kExprF32Ceil
508 : #define WASM_F32_FLOOR(x) x, kExprF32Floor
509 : #define WASM_F32_TRUNC(x) x, kExprF32Trunc
510 : #define WASM_F32_NEARESTINT(x) x, kExprF32NearestInt
511 : #define WASM_F32_SQRT(x) x, kExprF32Sqrt
512 : #define WASM_F32_EQ(x, y) x, y, kExprF32Eq
513 : #define WASM_F32_NE(x, y) x, y, kExprF32Ne
514 : #define WASM_F32_LT(x, y) x, y, kExprF32Lt
515 : #define WASM_F32_LE(x, y) x, y, kExprF32Le
516 : #define WASM_F32_GT(x, y) x, y, kExprF32Gt
517 : #define WASM_F32_GE(x, y) x, y, kExprF32Ge
518 :
519 : //------------------------------------------------------------------------------
520 : // Float64 operations
521 : //------------------------------------------------------------------------------
522 : #define WASM_F64_ADD(x, y) x, y, kExprF64Add
523 : #define WASM_F64_SUB(x, y) x, y, kExprF64Sub
524 : #define WASM_F64_MUL(x, y) x, y, kExprF64Mul
525 : #define WASM_F64_DIV(x, y) x, y, kExprF64Div
526 : #define WASM_F64_MIN(x, y) x, y, kExprF64Min
527 : #define WASM_F64_MAX(x, y) x, y, kExprF64Max
528 : #define WASM_F64_ABS(x) x, kExprF64Abs
529 : #define WASM_F64_NEG(x) x, kExprF64Neg
530 : #define WASM_F64_COPYSIGN(x, y) x, y, kExprF64CopySign
531 : #define WASM_F64_CEIL(x) x, kExprF64Ceil
532 : #define WASM_F64_FLOOR(x) x, kExprF64Floor
533 : #define WASM_F64_TRUNC(x) x, kExprF64Trunc
534 : #define WASM_F64_NEARESTINT(x) x, kExprF64NearestInt
535 : #define WASM_F64_SQRT(x) x, kExprF64Sqrt
536 : #define WASM_F64_EQ(x, y) x, y, kExprF64Eq
537 : #define WASM_F64_NE(x, y) x, y, kExprF64Ne
538 : #define WASM_F64_LT(x, y) x, y, kExprF64Lt
539 : #define WASM_F64_LE(x, y) x, y, kExprF64Le
540 : #define WASM_F64_GT(x, y) x, y, kExprF64Gt
541 : #define WASM_F64_GE(x, y) x, y, kExprF64Ge
542 :
543 : //------------------------------------------------------------------------------
544 : // Type conversions.
545 : //------------------------------------------------------------------------------
546 : #define WASM_I32_SCONVERT_F32(x) x, kExprI32SConvertF32
547 : #define WASM_I32_SCONVERT_F64(x) x, kExprI32SConvertF64
548 : #define WASM_I32_UCONVERT_F32(x) x, kExprI32UConvertF32
549 : #define WASM_I32_UCONVERT_F64(x) x, kExprI32UConvertF64
550 : #define WASM_I32_CONVERT_I64(x) x, kExprI32ConvertI64
551 : #define WASM_I64_SCONVERT_F32(x) x, kExprI64SConvertF32
552 : #define WASM_I64_SCONVERT_F64(x) x, kExprI64SConvertF64
553 : #define WASM_I64_UCONVERT_F32(x) x, kExprI64UConvertF32
554 : #define WASM_I64_UCONVERT_F64(x) x, kExprI64UConvertF64
555 : #define WASM_I64_SCONVERT_I32(x) x, kExprI64SConvertI32
556 : #define WASM_I64_UCONVERT_I32(x) x, kExprI64UConvertI32
557 : #define WASM_F32_SCONVERT_I32(x) x, kExprF32SConvertI32
558 : #define WASM_F32_UCONVERT_I32(x) x, kExprF32UConvertI32
559 : #define WASM_F32_SCONVERT_I64(x) x, kExprF32SConvertI64
560 : #define WASM_F32_UCONVERT_I64(x) x, kExprF32UConvertI64
561 : #define WASM_F32_CONVERT_F64(x) x, kExprF32ConvertF64
562 : #define WASM_F32_REINTERPRET_I32(x) x, kExprF32ReinterpretI32
563 : #define WASM_F64_SCONVERT_I32(x) x, kExprF64SConvertI32
564 : #define WASM_F64_UCONVERT_I32(x) x, kExprF64UConvertI32
565 : #define WASM_F64_SCONVERT_I64(x) x, kExprF64SConvertI64
566 : #define WASM_F64_UCONVERT_I64(x) x, kExprF64UConvertI64
567 : #define WASM_F64_CONVERT_F32(x) x, kExprF64ConvertF32
568 : #define WASM_F64_REINTERPRET_I64(x) x, kExprF64ReinterpretI64
569 : #define WASM_I32_REINTERPRET_F32(x) x, kExprI32ReinterpretF32
570 : #define WASM_I64_REINTERPRET_F64(x) x, kExprI64ReinterpretF64
571 :
572 : //------------------------------------------------------------------------------
573 : // Numeric operations
574 : //------------------------------------------------------------------------------
575 : #define WASM_NUMERIC_OP(op) kNumericPrefix, static_cast<byte>(op)
576 : #define WASM_I32_SCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI32SConvertSatF32)
577 : #define WASM_I32_UCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI32UConvertSatF32)
578 : #define WASM_I32_SCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI32SConvertSatF64)
579 : #define WASM_I32_UCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI32UConvertSatF64)
580 : #define WASM_I64_SCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI64SConvertSatF32)
581 : #define WASM_I64_UCONVERT_SAT_F32(x) x, WASM_NUMERIC_OP(kExprI64UConvertSatF32)
582 : #define WASM_I64_SCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI64SConvertSatF64)
583 : #define WASM_I64_UCONVERT_SAT_F64(x) x, WASM_NUMERIC_OP(kExprI64UConvertSatF64)
584 :
585 : #define MEMORY_ZERO 0
586 :
587 : #define WASM_MEMORY_INIT(seg, dst, src, size) \
588 : dst, src, size, WASM_NUMERIC_OP(kExprMemoryInit), MEMORY_ZERO, U32V_1(seg)
589 : #define WASM_MEMORY_DROP(seg) WASM_NUMERIC_OP(kExprMemoryDrop), U32V_1(seg)
590 : #define WASM_MEMORY_COPY(dst, src, size) \
591 : dst, src, size, WASM_NUMERIC_OP(kExprMemoryCopy), MEMORY_ZERO
592 : #define WASM_MEMORY_FILL(dst, val, size) \
593 : dst, val, size, WASM_NUMERIC_OP(kExprMemoryFill), MEMORY_ZERO
594 : #define WASM_TABLE_INIT(seg, dst, src, size) \
595 : dst, src, size, WASM_NUMERIC_OP(kExprTableInit), TABLE_ZERO, U32V_1(seg)
596 : #define WASM_TABLE_DROP(seg) WASM_NUMERIC_OP(kExprTableDrop), U32V_1(seg)
597 : #define WASM_TABLE_COPY(dst, src, size) \
598 : dst, src, size, WASM_NUMERIC_OP(kExprTableCopy), TABLE_ZERO
599 :
600 : //------------------------------------------------------------------------------
601 : // Memory Operations.
602 : //------------------------------------------------------------------------------
603 : #define WASM_GROW_MEMORY(x) x, kExprMemoryGrow, 0
604 : #define WASM_MEMORY_SIZE kExprMemorySize, 0
605 :
606 : #define SIG_ENTRY_v_v kWasmFunctionTypeCode, 0, 0
607 : #define SIZEOF_SIG_ENTRY_v_v 3
608 :
609 : #define SIG_ENTRY_v_x(a) kWasmFunctionTypeCode, 1, a, 0
610 : #define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeCode, 2, a, b, 0
611 : #define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 0
612 : #define SIZEOF_SIG_ENTRY_v_x 4
613 : #define SIZEOF_SIG_ENTRY_v_xx 5
614 : #define SIZEOF_SIG_ENTRY_v_xxx 6
615 :
616 : #define SIG_ENTRY_x(r) kWasmFunctionTypeCode, 0, 1, r
617 : #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeCode, 1, a, 1, r
618 : #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeCode, 2, a, b, 1, r
619 : #define SIG_ENTRY_xx_xx(r, s, a, b) kWasmFunctionTypeCode, 2, a, b, 2, r, s
620 : #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 1, r
621 : #define SIZEOF_SIG_ENTRY_x 4
622 : #define SIZEOF_SIG_ENTRY_x_x 5
623 : #define SIZEOF_SIG_ENTRY_x_xx 6
624 : #define SIZEOF_SIG_ENTRY_xx_xx 7
625 : #define SIZEOF_SIG_ENTRY_x_xxx 7
626 :
627 : #define WASM_BRV(depth, ...) __VA_ARGS__, kExprBr, static_cast<byte>(depth)
628 : #define WASM_BRV_IF(depth, val, cond) \
629 : val, cond, kExprBrIf, static_cast<byte>(depth)
630 : #define WASM_BRV_IFD(depth, val, cond) \
631 : val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
632 : #define WASM_IFB(cond, ...) cond, kExprIf, kLocalVoid, __VA_ARGS__, kExprEnd
633 : #define WASM_BR_TABLEV(val, key, count, ...) \
634 : val, key, kExprBrTable, U32V_1(count), __VA_ARGS__
635 :
636 : //------------------------------------------------------------------------------
637 : // Atomic Operations.
638 : //------------------------------------------------------------------------------
639 : #define WASM_ATOMICS_OP(op) kAtomicPrefix, static_cast<byte>(op)
640 : #define WASM_ATOMICS_BINOP(op, x, y, representation) \
641 : x, y, WASM_ATOMICS_OP(op), \
642 : static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
643 : #define WASM_ATOMICS_TERNARY_OP(op, x, y, z, representation) \
644 : x, y, z, WASM_ATOMICS_OP(op), \
645 : static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
646 : #define WASM_ATOMICS_LOAD_OP(op, x, representation) \
647 : x, WASM_ATOMICS_OP(op), \
648 : static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
649 : #define WASM_ATOMICS_STORE_OP(op, x, y, representation) \
650 : x, y, WASM_ATOMICS_OP(op), \
651 : static_cast<byte>(ElementSizeLog2Of(representation)), ZERO_OFFSET
652 :
653 : //------------------------------------------------------------------------------
654 : // Sign Externsion Operations.
655 : //------------------------------------------------------------------------------
656 : #define WASM_I32_SIGN_EXT_I8(x) x, kExprI32SExtendI8
657 : #define WASM_I32_SIGN_EXT_I16(x) x, kExprI32SExtendI16
658 : #define WASM_I64_SIGN_EXT_I8(x) x, kExprI64SExtendI8
659 : #define WASM_I64_SIGN_EXT_I16(x) x, kExprI64SExtendI16
660 : #define WASM_I64_SIGN_EXT_I32(x) x, kExprI64SExtendI32
661 :
662 : #endif // V8_WASM_MACRO_GEN_H_
|