• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_WASM_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 IMPORT_SIG_INDEX(v) U32V_1(v)
21 #define FUNC_INDEX(v) U32V_1(v)
22 #define TABLE_INDEX(v) U32V_1(v)
23 #define NO_NAME U32V_1(0)
24 #define NAME_LENGTH(v) U32V_1(v)
25 #define ENTRY_COUNT(v) U32V_1(v)
26 
27 #define ZERO_ALIGNMENT 0
28 #define ZERO_OFFSET 0
29 
30 #define BR_TARGET(v) U32V_1(v)
31 
32 #define MASK_7 ((1 << 7) - 1)
33 #define MASK_14 ((1 << 14) - 1)
34 #define MASK_21 ((1 << 21) - 1)
35 #define MASK_28 ((1 << 28) - 1)
36 
37 #define U32V_1(x) static_cast<byte>((x)&MASK_7)
38 #define U32V_2(x) \
39   static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7)
40 #define U32V_3(x)                                      \
41   static_cast<byte>((((x)) & MASK_7) | 0x80),          \
42       static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \
43       static_cast<byte>(((x) >> 14) & MASK_7)
44 #define U32V_4(x)                                       \
45   static_cast<byte>(((x)&MASK_7) | 0x80),               \
46       static_cast<byte>((((x) >> 7) & MASK_7) | 0x80),  \
47       static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \
48       static_cast<byte>(((x) >> 21) & MASK_7)
49 #define U32V_5(x)                                       \
50   static_cast<byte>(((x)&MASK_7) | 0x80),               \
51       static_cast<byte>((((x) >> 7) & MASK_7) | 0x80),  \
52       static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \
53       static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \
54       static_cast<byte>((((x) >> 28) & MASK_7))
55 
56 // Convenience macros for building Wasm bytecode directly into a byte array.
57 
58 //------------------------------------------------------------------------------
59 // Control.
60 //------------------------------------------------------------------------------
61 #define WASM_NOP kExprNop
62 #define WASM_END kExprEnd
63 
64 #define ARITY_0 0
65 #define ARITY_1 1
66 #define ARITY_2 2
67 #define DEPTH_0 0
68 #define DEPTH_1 1
69 #define DEPTH_2 2
70 #define ARITY_2 2
71 
72 #define WASM_BLOCK(...) kExprBlock, kLocalVoid, __VA_ARGS__, kExprEnd
73 
74 #define WASM_BLOCK_T(t, ...)                                       \
75   kExprBlock, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t)), \
76       __VA_ARGS__, kExprEnd
77 
78 #define WASM_BLOCK_TT(t1, t2, ...)                                       \
79   kExprBlock, kMultivalBlock, 0,                                         \
80       static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t1)),              \
81       static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t2)), __VA_ARGS__, \
82       kExprEnd
83 
84 #define WASM_BLOCK_I(...) kExprBlock, kLocalI32, __VA_ARGS__, kExprEnd
85 #define WASM_BLOCK_L(...) kExprBlock, kLocalI64, __VA_ARGS__, kExprEnd
86 #define WASM_BLOCK_F(...) kExprBlock, kLocalF32, __VA_ARGS__, kExprEnd
87 #define WASM_BLOCK_D(...) kExprBlock, kLocalF64, __VA_ARGS__, kExprEnd
88 
89 #define WASM_INFINITE_LOOP kExprLoop, kLocalVoid, kExprBr, DEPTH_0, kExprEnd
90 
91 #define WASM_LOOP(...) kExprLoop, kLocalVoid, __VA_ARGS__, kExprEnd
92 #define WASM_LOOP_I(...) kExprLoop, kLocalI32, __VA_ARGS__, kExprEnd
93 #define WASM_LOOP_L(...) kExprLoop, kLocalI64, __VA_ARGS__, kExprEnd
94 #define WASM_LOOP_F(...) kExprLoop, kLocalF32, __VA_ARGS__, kExprEnd
95 #define WASM_LOOP_D(...) kExprLoop, kLocalF64, __VA_ARGS__, kExprEnd
96 
97 #define WASM_IF(cond, tstmt) cond, kExprIf, kLocalVoid, tstmt, kExprEnd
98 
99 #define WASM_IF_ELSE(cond, tstmt, fstmt) \
100   cond, kExprIf, kLocalVoid, tstmt, kExprElse, fstmt, kExprEnd
101 
102 #define WASM_IF_ELSE_T(t, cond, tstmt, fstmt)                                \
103   cond, kExprIf, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t)), tstmt, \
104       kExprElse, fstmt, kExprEnd
105 
106 #define WASM_IF_ELSE_TT(t1, t2, cond, tstmt, fstmt)                           \
107   cond, kExprIf, kMultivalBlock, 0,                                           \
108       static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t1)),                   \
109       static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(t2)), tstmt, kExprElse, \
110       fstmt, kExprEnd
111 
112 #define WASM_IF_ELSE_I(cond, tstmt, fstmt) \
113   cond, kExprIf, kLocalI32, tstmt, kExprElse, fstmt, kExprEnd
114 #define WASM_IF_ELSE_L(cond, tstmt, fstmt) \
115   cond, kExprIf, kLocalI64, tstmt, kExprElse, fstmt, kExprEnd
116 #define WASM_IF_ELSE_F(cond, tstmt, fstmt) \
117   cond, kExprIf, kLocalF32, tstmt, kExprElse, fstmt, kExprEnd
118 #define WASM_IF_ELSE_D(cond, tstmt, fstmt) \
119   cond, kExprIf, kLocalF64, tstmt, kExprElse, fstmt, kExprEnd
120 
121 #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect
122 
123 #define WASM_RETURN0 kExprReturn
124 #define WASM_RETURN1(val) val, kExprReturn
125 #define WASM_RETURNN(count, ...) __VA_ARGS__, kExprReturn
126 
127 #define WASM_BR(depth) kExprBr, static_cast<byte>(depth)
128 #define WASM_BR_IF(depth, cond) cond, kExprBrIf, static_cast<byte>(depth)
129 #define WASM_BR_IFD(depth, val, cond) \
130   val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
131 #define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth)
132 #define WASM_UNREACHABLE kExprUnreachable
133 
134 #define WASM_BR_TABLE(key, count, ...) \
135   key, kExprBrTable, U32V_1(count), __VA_ARGS__
136 
137 #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8)
138 #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8)
139 
140 //------------------------------------------------------------------------------
141 // Misc expressions.
142 //------------------------------------------------------------------------------
143 #define WASM_ID(...) __VA_ARGS__
144 #define WASM_ZERO kExprI32Const, 0
145 #define WASM_ONE kExprI32Const, 1
146 
147 #define I32V_MIN(length) -(1 << (6 + (7 * ((length) - 1))))
148 #define I32V_MAX(length) ((1 << (6 + (7 * ((length) - 1)))) - 1)
149 #define I64V_MIN(length) -(1LL << (6 + (7 * ((length) - 1))))
150 #define I64V_MAX(length) ((1LL << (6 + 7 * ((length) - 1))) - 1)
151 
152 #define I32V_IN_RANGE(value, length) \
153   ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length))
154 #define I64V_IN_RANGE(value, length) \
155   ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length))
156 
157 #define WASM_NO_LOCALS 0
158 
159 namespace v8 {
160 namespace internal {
161 namespace wasm {
162 
CheckI32v(int32_t value,int length)163 inline void CheckI32v(int32_t value, int length) {
164   DCHECK(length >= 1 && length <= 5);
165   DCHECK(length == 5 || I32V_IN_RANGE(value, length));
166 }
167 
CheckI64v(int64_t value,int length)168 inline void CheckI64v(int64_t value, int length) {
169   DCHECK(length >= 1 && length <= 10);
170   DCHECK(length == 10 || I64V_IN_RANGE(value, length));
171 }
172 
173 // A helper for encoding local declarations prepended to the body of a
174 // function.
175 // TODO(titzer): move this to an appropriate header.
176 class LocalDeclEncoder {
177  public:
178   explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr)
sig(s)179       : sig(s), local_decls(zone), total(0) {}
180 
181   // Prepend local declarations by creating a new buffer and copying data
182   // over. The new buffer must be delete[]'d by the caller.
Prepend(Zone * zone,const byte ** start,const byte ** end)183   void Prepend(Zone* zone, const byte** start, const byte** end) const {
184     size_t size = (*end - *start);
185     byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
186     size_t pos = Emit(buffer);
187     memcpy(buffer + pos, *start, size);
188     pos += size;
189     *start = buffer;
190     *end = buffer + pos;
191   }
192 
Emit(byte * buffer)193   size_t Emit(byte* buffer) const {
194     size_t pos = 0;
195     pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size()));
196     for (size_t i = 0; i < local_decls.size(); ++i) {
197       pos = WriteUint32v(buffer, pos, local_decls[i].first);
198       buffer[pos++] = WasmOpcodes::ValueTypeCodeFor(local_decls[i].second);
199     }
200     DCHECK_EQ(Size(), pos);
201     return pos;
202   }
203 
204   // Add locals declarations to this helper. Return the index of the newly added
205   // local(s), with an optional adjustment for the parameters.
AddLocals(uint32_t count,ValueType type)206   uint32_t AddLocals(uint32_t count, ValueType type) {
207     uint32_t result =
208         static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
209     total += count;
210     if (local_decls.size() > 0 && local_decls.back().second == type) {
211       count += local_decls.back().first;
212       local_decls.pop_back();
213     }
214     local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
215     return result;
216   }
217 
Size()218   size_t Size() const {
219     size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size()));
220     for (auto p : local_decls) size += 1 + SizeofUint32v(p.first);
221     return size;
222   }
223 
has_sig()224   bool has_sig() const { return sig != nullptr; }
get_sig()225   FunctionSig* get_sig() const { return sig; }
set_sig(FunctionSig * s)226   void set_sig(FunctionSig* s) { sig = s; }
227 
228  private:
229   FunctionSig* sig;
230   ZoneVector<std::pair<uint32_t, ValueType>> local_decls;
231   size_t total;
232 
SizeofUint32v(uint32_t val)233   size_t SizeofUint32v(uint32_t val) const {
234     size_t size = 1;
235     while (true) {
236       byte b = val & MASK_7;
237       if (b == val) return size;
238       size++;
239       val = val >> 7;
240     }
241   }
242 
243   // TODO(titzer): lift encoding of u32v to a common place.
WriteUint32v(byte * buffer,size_t pos,uint32_t val)244   size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const {
245     while (true) {
246       byte b = val & MASK_7;
247       if (b == val) {
248         buffer[pos++] = b;
249         break;
250       }
251       buffer[pos++] = 0x80 | b;
252       val = val >> 7;
253     }
254     return pos;
255   }
256 };
257 }  // namespace wasm
258 }  // namespace internal
259 }  // namespace v8
260 
261 //------------------------------------------------------------------------------
262 // Int32 Const operations
263 //------------------------------------------------------------------------------
264 #define WASM_I32V(val) kExprI32Const, U32V_5(val)
265 
266 #define WASM_I32V_1(val) \
267   static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val)
268 #define WASM_I32V_2(val) \
269   static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val)
270 #define WASM_I32V_3(val) \
271   static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val)
272 #define WASM_I32V_4(val) \
273   static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val)
274 #define WASM_I32V_5(val) \
275   static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val)
276 
277 //------------------------------------------------------------------------------
278 // Int64 Const operations
279 //------------------------------------------------------------------------------
280 #define WASM_I64V(val)                                                        \
281   kExprI64Const,                                                              \
282       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
283       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
284       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
285       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
286       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
287       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
288       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
289       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
290       static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
291       static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
292 
293 #define WASM_I64V_1(val)                                                     \
294   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \
295       static_cast<byte>(static_cast<int64_t>(val) & MASK_7)
296 #define WASM_I64V_2(val)                                                     \
297   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), kExprI64Const), \
298       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),        \
299       static_cast<byte>((static_cast<int64_t>(val) >> 7) & MASK_7)
300 #define WASM_I64V_3(val)                                                     \
301   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \
302       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),        \
303       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
304       static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7)
305 #define WASM_I64V_4(val)                                                      \
306   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const),  \
307       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
308       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
309       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
310       static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7)
311 #define WASM_I64V_5(val)                                                      \
312   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const),  \
313       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
314       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
315       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
316       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
317       static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7)
318 #define WASM_I64V_6(val)                                                      \
319   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const),  \
320       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
321       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
322       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
323       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
324       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
325       static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7)
326 #define WASM_I64V_7(val)                                                      \
327   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const),  \
328       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
329       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
330       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
331       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
332       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
333       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
334       static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7)
335 #define WASM_I64V_8(val)                                                      \
336   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const),  \
337       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
338       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
339       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
340       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
341       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
342       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
343       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
344       static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7)
345 #define WASM_I64V_9(val)                                                      \
346   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), kExprI64Const),  \
347       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
348       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
349       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
350       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
351       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
352       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
353       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
354       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
355       static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7)
356 #define WASM_I64V_10(val)                                                     \
357   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \
358       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
359       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
360       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
361       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
362       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
363       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
364       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
365       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
366       static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
367       static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
368 
369 #define WASM_F32(val)                                                       \
370   kExprF32Const,                                                            \
371       static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))),        \
372       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8),  \
373       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \
374       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24)
375 #define WASM_F64(val)                                                        \
376   kExprF64Const,                                                             \
377       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val))),       \
378       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 8),  \
379       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 16), \
380       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 24), \
381       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 32), \
382       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 40), \
383       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 48), \
384       static_cast<byte>(bit_cast<uint64_t>(static_cast<double>(val)) >> 56)
385 #define WASM_GET_LOCAL(index) kExprGetLocal, static_cast<byte>(index)
386 #define WASM_SET_LOCAL(index, val) val, kExprSetLocal, static_cast<byte>(index)
387 #define WASM_TEE_LOCAL(index, val) val, kExprTeeLocal, static_cast<byte>(index)
388 #define WASM_DROP kExprDrop
389 #define WASM_GET_GLOBAL(index) kExprGetGlobal, static_cast<byte>(index)
390 #define WASM_SET_GLOBAL(index, val) \
391   val, kExprSetGlobal, static_cast<byte>(index)
392 #define WASM_LOAD_MEM(type, index)                                             \
393   index, static_cast<byte>(                                                    \
394              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
395       ZERO_ALIGNMENT, ZERO_OFFSET
396 #define WASM_STORE_MEM(type, index, val)                                   \
397   index, val,                                                              \
398       static_cast<byte>(                                                   \
399           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
400       ZERO_ALIGNMENT, ZERO_OFFSET
401 #define WASM_LOAD_MEM_OFFSET(type, offset, index)                              \
402   index, static_cast<byte>(                                                    \
403              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
404       ZERO_ALIGNMENT, static_cast<byte>(offset)
405 #define WASM_STORE_MEM_OFFSET(type, offset, index, val)                    \
406   index, val,                                                              \
407       static_cast<byte>(                                                   \
408           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
409       ZERO_ALIGNMENT, static_cast<byte>(offset)
410 #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment)                        \
411   index, static_cast<byte>(                                                    \
412              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
413       alignment, ZERO_OFFSET
414 #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val)              \
415   index, val,                                                              \
416       static_cast<byte>(                                                   \
417           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
418       alignment, ZERO_OFFSET
419 
420 #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index)
421 #define WASM_CALL_FUNCTION(index, ...) \
422   __VA_ARGS__, kExprCallFunction, static_cast<byte>(index)
423 
424 #define TABLE_ZERO 0
425 
426 // TODO(titzer): change usages of these macros to put func last.
427 #define WASM_CALL_INDIRECT0(index, func) \
428   func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
429 #define WASM_CALL_INDIRECT1(index, func, a) \
430   a, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
431 #define WASM_CALL_INDIRECT2(index, func, a, b) \
432   a, b, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
433 #define WASM_CALL_INDIRECT3(index, func, a, b, c) \
434   a, b, c, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
435 #define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \
436   a, b, c, d, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
437 #define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \
438   a, b, c, d, e, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
439 #define WASM_CALL_INDIRECTN(arity, index, func, ...) \
440   __VA_ARGS__, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
441 
442 #define WASM_NOT(x) x, kExprI32Eqz
443 #define WASM_SEQ(...) __VA_ARGS__
444 
445 //------------------------------------------------------------------------------
446 // Constructs that are composed of multiple bytecodes.
447 //------------------------------------------------------------------------------
448 #define WASM_WHILE(x, y)                                              \
449   kExprLoop, kLocalVoid, x, kExprIf, kLocalVoid, y, kExprBr, DEPTH_1, \
450       kExprEnd, kExprEnd
451 #define WASM_INC_LOCAL(index)                                             \
452   kExprGetLocal, static_cast<byte>(index), kExprI32Const, 1, kExprI32Add, \
453       kExprTeeLocal, static_cast<byte>(index)
454 #define WASM_INC_LOCAL_BYV(index, count)                    \
455   kExprGetLocal, static_cast<byte>(index), kExprI32Const,   \
456       static_cast<byte>(count), kExprI32Add, kExprTeeLocal, \
457       static_cast<byte>(index)
458 #define WASM_INC_LOCAL_BY(index, count)                     \
459   kExprGetLocal, static_cast<byte>(index), kExprI32Const,   \
460       static_cast<byte>(count), kExprI32Add, kExprSetLocal, \
461       static_cast<byte>(index)
462 #define WASM_UNOP(opcode, x) x, static_cast<byte>(opcode)
463 #define WASM_BINOP(opcode, x, y) x, y, static_cast<byte>(opcode)
464 
465 //------------------------------------------------------------------------------
466 // Int32 operations
467 //------------------------------------------------------------------------------
468 #define WASM_I32_ADD(x, y) x, y, kExprI32Add
469 #define WASM_I32_SUB(x, y) x, y, kExprI32Sub
470 #define WASM_I32_MUL(x, y) x, y, kExprI32Mul
471 #define WASM_I32_DIVS(x, y) x, y, kExprI32DivS
472 #define WASM_I32_DIVU(x, y) x, y, kExprI32DivU
473 #define WASM_I32_REMS(x, y) x, y, kExprI32RemS
474 #define WASM_I32_REMU(x, y) x, y, kExprI32RemU
475 #define WASM_I32_AND(x, y) x, y, kExprI32And
476 #define WASM_I32_IOR(x, y) x, y, kExprI32Ior
477 #define WASM_I32_XOR(x, y) x, y, kExprI32Xor
478 #define WASM_I32_SHL(x, y) x, y, kExprI32Shl
479 #define WASM_I32_SHR(x, y) x, y, kExprI32ShrU
480 #define WASM_I32_SAR(x, y) x, y, kExprI32ShrS
481 #define WASM_I32_ROR(x, y) x, y, kExprI32Ror
482 #define WASM_I32_ROL(x, y) x, y, kExprI32Rol
483 #define WASM_I32_EQ(x, y) x, y, kExprI32Eq
484 #define WASM_I32_NE(x, y) x, y, kExprI32Ne
485 #define WASM_I32_LTS(x, y) x, y, kExprI32LtS
486 #define WASM_I32_LES(x, y) x, y, kExprI32LeS
487 #define WASM_I32_LTU(x, y) x, y, kExprI32LtU
488 #define WASM_I32_LEU(x, y) x, y, kExprI32LeU
489 #define WASM_I32_GTS(x, y) x, y, kExprI32GtS
490 #define WASM_I32_GES(x, y) x, y, kExprI32GeS
491 #define WASM_I32_GTU(x, y) x, y, kExprI32GtU
492 #define WASM_I32_GEU(x, y) x, y, kExprI32GeU
493 #define WASM_I32_CLZ(x) x, kExprI32Clz
494 #define WASM_I32_CTZ(x) x, kExprI32Ctz
495 #define WASM_I32_POPCNT(x) x, kExprI32Popcnt
496 #define WASM_I32_EQZ(x) x, kExprI32Eqz
497 
498 //------------------------------------------------------------------------------
499 // Asmjs Int32 operations
500 //------------------------------------------------------------------------------
501 #define WASM_I32_ASMJS_DIVS(x, y) x, y, kExprI32AsmjsDivS
502 #define WASM_I32_ASMJS_REMS(x, y) x, y, kExprI32AsmjsRemS
503 #define WASM_I32_ASMJS_DIVU(x, y) x, y, kExprI32AsmjsDivU
504 #define WASM_I32_ASMJS_REMU(x, y) x, y, kExprI32AsmjsRemU
505 
506 //------------------------------------------------------------------------------
507 // Int64 operations
508 //------------------------------------------------------------------------------
509 #define WASM_I64_ADD(x, y) x, y, kExprI64Add
510 #define WASM_I64_SUB(x, y) x, y, kExprI64Sub
511 #define WASM_I64_MUL(x, y) x, y, kExprI64Mul
512 #define WASM_I64_DIVS(x, y) x, y, kExprI64DivS
513 #define WASM_I64_DIVU(x, y) x, y, kExprI64DivU
514 #define WASM_I64_REMS(x, y) x, y, kExprI64RemS
515 #define WASM_I64_REMU(x, y) x, y, kExprI64RemU
516 #define WASM_I64_AND(x, y) x, y, kExprI64And
517 #define WASM_I64_IOR(x, y) x, y, kExprI64Ior
518 #define WASM_I64_XOR(x, y) x, y, kExprI64Xor
519 #define WASM_I64_SHL(x, y) x, y, kExprI64Shl
520 #define WASM_I64_SHR(x, y) x, y, kExprI64ShrU
521 #define WASM_I64_SAR(x, y) x, y, kExprI64ShrS
522 #define WASM_I64_ROR(x, y) x, y, kExprI64Ror
523 #define WASM_I64_ROL(x, y) x, y, kExprI64Rol
524 #define WASM_I64_EQ(x, y) x, y, kExprI64Eq
525 #define WASM_I64_NE(x, y) x, y, kExprI64Ne
526 #define WASM_I64_LTS(x, y) x, y, kExprI64LtS
527 #define WASM_I64_LES(x, y) x, y, kExprI64LeS
528 #define WASM_I64_LTU(x, y) x, y, kExprI64LtU
529 #define WASM_I64_LEU(x, y) x, y, kExprI64LeU
530 #define WASM_I64_GTS(x, y) x, y, kExprI64GtS
531 #define WASM_I64_GES(x, y) x, y, kExprI64GeS
532 #define WASM_I64_GTU(x, y) x, y, kExprI64GtU
533 #define WASM_I64_GEU(x, y) x, y, kExprI64GeU
534 #define WASM_I64_CLZ(x) x, kExprI64Clz
535 #define WASM_I64_CTZ(x) x, kExprI64Ctz
536 #define WASM_I64_POPCNT(x) x, kExprI64Popcnt
537 #define WASM_I64_EQZ(x) x, kExprI64Eqz
538 
539 //------------------------------------------------------------------------------
540 // Float32 operations
541 //------------------------------------------------------------------------------
542 #define WASM_F32_ADD(x, y) x, y, kExprF32Add
543 #define WASM_F32_SUB(x, y) x, y, kExprF32Sub
544 #define WASM_F32_MUL(x, y) x, y, kExprF32Mul
545 #define WASM_F32_DIV(x, y) x, y, kExprF32Div
546 #define WASM_F32_MIN(x, y) x, y, kExprF32Min
547 #define WASM_F32_MAX(x, y) x, y, kExprF32Max
548 #define WASM_F32_ABS(x) x, kExprF32Abs
549 #define WASM_F32_NEG(x) x, kExprF32Neg
550 #define WASM_F32_COPYSIGN(x, y) x, y, kExprF32CopySign
551 #define WASM_F32_CEIL(x) x, kExprF32Ceil
552 #define WASM_F32_FLOOR(x) x, kExprF32Floor
553 #define WASM_F32_TRUNC(x) x, kExprF32Trunc
554 #define WASM_F32_NEARESTINT(x) x, kExprF32NearestInt
555 #define WASM_F32_SQRT(x) x, kExprF32Sqrt
556 #define WASM_F32_EQ(x, y) x, y, kExprF32Eq
557 #define WASM_F32_NE(x, y) x, y, kExprF32Ne
558 #define WASM_F32_LT(x, y) x, y, kExprF32Lt
559 #define WASM_F32_LE(x, y) x, y, kExprF32Le
560 #define WASM_F32_GT(x, y) x, y, kExprF32Gt
561 #define WASM_F32_GE(x, y) x, y, kExprF32Ge
562 
563 //------------------------------------------------------------------------------
564 // Float64 operations
565 //------------------------------------------------------------------------------
566 #define WASM_F64_ADD(x, y) x, y, kExprF64Add
567 #define WASM_F64_SUB(x, y) x, y, kExprF64Sub
568 #define WASM_F64_MUL(x, y) x, y, kExprF64Mul
569 #define WASM_F64_DIV(x, y) x, y, kExprF64Div
570 #define WASM_F64_MIN(x, y) x, y, kExprF64Min
571 #define WASM_F64_MAX(x, y) x, y, kExprF64Max
572 #define WASM_F64_ABS(x) x, kExprF64Abs
573 #define WASM_F64_NEG(x) x, kExprF64Neg
574 #define WASM_F64_COPYSIGN(x, y) x, y, kExprF64CopySign
575 #define WASM_F64_CEIL(x) x, kExprF64Ceil
576 #define WASM_F64_FLOOR(x) x, kExprF64Floor
577 #define WASM_F64_TRUNC(x) x, kExprF64Trunc
578 #define WASM_F64_NEARESTINT(x) x, kExprF64NearestInt
579 #define WASM_F64_SQRT(x) x, kExprF64Sqrt
580 #define WASM_F64_EQ(x, y) x, y, kExprF64Eq
581 #define WASM_F64_NE(x, y) x, y, kExprF64Ne
582 #define WASM_F64_LT(x, y) x, y, kExprF64Lt
583 #define WASM_F64_LE(x, y) x, y, kExprF64Le
584 #define WASM_F64_GT(x, y) x, y, kExprF64Gt
585 #define WASM_F64_GE(x, y) x, y, kExprF64Ge
586 
587 //------------------------------------------------------------------------------
588 // Type conversions.
589 //------------------------------------------------------------------------------
590 #define WASM_I32_SCONVERT_F32(x) x, kExprI32SConvertF32
591 #define WASM_I32_SCONVERT_F64(x) x, kExprI32SConvertF64
592 #define WASM_I32_UCONVERT_F32(x) x, kExprI32UConvertF32
593 #define WASM_I32_UCONVERT_F64(x) x, kExprI32UConvertF64
594 #define WASM_I32_CONVERT_I64(x) x, kExprI32ConvertI64
595 #define WASM_I64_SCONVERT_F32(x) x, kExprI64SConvertF32
596 #define WASM_I64_SCONVERT_F64(x) x, kExprI64SConvertF64
597 #define WASM_I64_UCONVERT_F32(x) x, kExprI64UConvertF32
598 #define WASM_I64_UCONVERT_F64(x) x, kExprI64UConvertF64
599 #define WASM_I64_SCONVERT_I32(x) x, kExprI64SConvertI32
600 #define WASM_I64_UCONVERT_I32(x) x, kExprI64UConvertI32
601 #define WASM_F32_SCONVERT_I32(x) x, kExprF32SConvertI32
602 #define WASM_F32_UCONVERT_I32(x) x, kExprF32UConvertI32
603 #define WASM_F32_SCONVERT_I64(x) x, kExprF32SConvertI64
604 #define WASM_F32_UCONVERT_I64(x) x, kExprF32UConvertI64
605 #define WASM_F32_CONVERT_F64(x) x, kExprF32ConvertF64
606 #define WASM_F32_REINTERPRET_I32(x) x, kExprF32ReinterpretI32
607 #define WASM_F64_SCONVERT_I32(x) x, kExprF64SConvertI32
608 #define WASM_F64_UCONVERT_I32(x) x, kExprF64UConvertI32
609 #define WASM_F64_SCONVERT_I64(x) x, kExprF64SConvertI64
610 #define WASM_F64_UCONVERT_I64(x) x, kExprF64UConvertI64
611 #define WASM_F64_CONVERT_F32(x) x, kExprF64ConvertF32
612 #define WASM_F64_REINTERPRET_I64(x) x, kExprF64ReinterpretI64
613 #define WASM_I32_REINTERPRET_F32(x) x, kExprI32ReinterpretF32
614 #define WASM_I64_REINTERPRET_F64(x) x, kExprI64ReinterpretF64
615 
616 //------------------------------------------------------------------------------
617 // Memory Operations.
618 //------------------------------------------------------------------------------
619 #define WASM_GROW_MEMORY(x) x, kExprGrowMemory, 0
620 #define WASM_MEMORY_SIZE kExprMemorySize, 0
621 
622 //------------------------------------------------------------------------------
623 // Simd Operations.
624 //------------------------------------------------------------------------------
625 // TODO(bbudge) Migrate these into tests.
626 #define WASM_SIMD_F32x4_SPLAT(x) \
627   x, kSimdPrefix, static_cast<byte>(kExprF32x4Splat)
628 #define WASM_SIMD_F32x4_EXTRACT_LANE(lane, x)               \
629   x, kSimdPrefix, static_cast<byte>(kExprF32x4ExtractLane), \
630       static_cast<byte>(lane)
631 #define WASM_SIMD_F32x4_REPLACE_LANE(lane, x, y)               \
632   x, y, kSimdPrefix, static_cast<byte>(kExprF32x4ReplaceLane), \
633       static_cast<byte>(lane)
634 #define WASM_SIMD_F32x4_ADD(x, y) \
635   x, y, kSimdPrefix, static_cast<byte>(kExprF32x4Add)
636 #define WASM_SIMD_F32x4_SUB(x, y) \
637   x, y, kSimdPrefix, static_cast<byte>(kExprF32x4Sub)
638 
639 #define WASM_SIMD_I32x4_SPLAT(x) \
640   x, kSimdPrefix, static_cast<byte>(kExprI32x4Splat)
641 #define WASM_SIMD_I32x4_EXTRACT_LANE(lane, x)               \
642   x, kSimdPrefix, static_cast<byte>(kExprI32x4ExtractLane), \
643       static_cast<byte>(lane)
644 #define WASM_SIMD_I32x4_REPLACE_LANE(lane, x, y)               \
645   x, y, kSimdPrefix, static_cast<byte>(kExprI32x4ReplaceLane), \
646       static_cast<byte>(lane)
647 #define WASM_SIMD_I32x4_ADD(x, y) \
648   x, y, kSimdPrefix, static_cast<byte>(kExprI32x4Add)
649 #define WASM_SIMD_I32x4_SUB(x, y) \
650   x, y, kSimdPrefix, static_cast<byte>(kExprI32x4Sub)
651 
652 #define SIG_ENTRY_v_v kWasmFunctionTypeForm, 0, 0
653 #define SIZEOF_SIG_ENTRY_v_v 3
654 
655 #define SIG_ENTRY_v_x(a) kWasmFunctionTypeForm, 1, a, 0
656 #define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeForm, 2, a, b, 0
657 #define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 0
658 #define SIZEOF_SIG_ENTRY_v_x 4
659 #define SIZEOF_SIG_ENTRY_v_xx 5
660 #define SIZEOF_SIG_ENTRY_v_xxx 6
661 
662 #define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r
663 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r
664 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r
665 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r
666 #define SIZEOF_SIG_ENTRY_x 4
667 #define SIZEOF_SIG_ENTRY_x_x 5
668 #define SIZEOF_SIG_ENTRY_x_xx 6
669 #define SIZEOF_SIG_ENTRY_x_xxx 7
670 
671 #define WASM_BRV(depth, val) val, kExprBr, static_cast<byte>(depth)
672 #define WASM_BRV_IF(depth, val, cond) \
673   val, cond, kExprBrIf, static_cast<byte>(depth)
674 #define WASM_BRV_IFD(depth, val, cond) \
675   val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
676 #define WASM_IFB(cond, ...) cond, kExprIf, kLocalVoid, __VA_ARGS__, kExprEnd
677 #define WASM_BR_TABLEV(val, key, count, ...) \
678   val, key, kExprBrTable, U32V_1(count), __VA_ARGS__
679 
680 #endif  // V8_WASM_MACRO_GEN_H_
681