Home | History | Annotate | Download | only in wasm
      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-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) U16_LE(v)
     21 // TODO(binji): make SIG_INDEX match this.
     22 #define IMPORT_SIG_INDEX(v) U32V_1(v)
     23 #define FUNC_INDEX(v) U32V_1(v)
     24 #define NO_NAME U32V_1(0)
     25 #define NAME_LENGTH(v) U32V_1(v)
     26 
     27 #define ZERO_ALIGNMENT 0
     28 #define ZERO_OFFSET 0
     29 
     30 #define BR_TARGET(v) U32_LE(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 
     63 #define ARITY_0 0
     64 #define ARITY_1 1
     65 #define DEPTH_0 0
     66 #define DEPTH_1 1
     67 
     68 #define WASM_BLOCK(count, ...) kExprBlock, __VA_ARGS__, kExprEnd
     69 #define WASM_INFINITE_LOOP kExprLoop, kExprBr, ARITY_0, DEPTH_0, kExprEnd
     70 #define WASM_LOOP(count, ...) kExprLoop, __VA_ARGS__, kExprEnd
     71 #define WASM_IF(cond, tstmt) cond, kExprIf, tstmt, kExprEnd
     72 #define WASM_IF_ELSE(cond, tstmt, fstmt) \
     73   cond, kExprIf, tstmt, kExprElse, fstmt, kExprEnd
     74 #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect
     75 #define WASM_BR(depth) kExprBr, ARITY_0, static_cast<byte>(depth)
     76 #define WASM_BR_IF(depth, cond) \
     77   cond, kExprBrIf, ARITY_0, static_cast<byte>(depth)
     78 #define WASM_BRV(depth, val) val, kExprBr, ARITY_1, static_cast<byte>(depth)
     79 #define WASM_BRV_IF(depth, val, cond) \
     80   val, cond, kExprBrIf, ARITY_1, static_cast<byte>(depth)
     81 #define WASM_BREAK(depth) kExprBr, ARITY_0, static_cast<byte>(depth + 1)
     82 #define WASM_CONTINUE(depth) kExprBr, ARITY_0, static_cast<byte>(depth)
     83 #define WASM_BREAKV(depth, val) \
     84   val, kExprBr, ARITY_1, static_cast<byte>(depth + 1)
     85 #define WASM_RETURN0 kExprReturn, ARITY_0
     86 #define WASM_RETURN1(val) val, kExprReturn, ARITY_1
     87 #define WASM_RETURNN(count, ...) __VA_ARGS__, kExprReturn, count
     88 #define WASM_UNREACHABLE kExprUnreachable
     89 
     90 #define WASM_BR_TABLE(key, count, ...) \
     91   key, kExprBrTable, ARITY_0, U32V_1(count), __VA_ARGS__
     92 
     93 #define WASM_BR_TABLEV(val, key, count, ...) \
     94   val, key, kExprBrTable, ARITY_1, U32V_1(count), __VA_ARGS__
     95 
     96 #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8)
     97 #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8)
     98 
     99 //------------------------------------------------------------------------------
    100 // Misc expressions.
    101 //------------------------------------------------------------------------------
    102 #define WASM_ID(...) __VA_ARGS__
    103 #define WASM_ZERO kExprI8Const, 0
    104 #define WASM_ONE kExprI8Const, 1
    105 #define WASM_I8(val) kExprI8Const, static_cast<byte>(val)
    106 
    107 #define I32V_MIN(length) -(1 << (6 + (7 * ((length) - 1))))
    108 #define I32V_MAX(length) ((1 << (6 + (7 * ((length) - 1)))) - 1)
    109 #define I64V_MIN(length) -(1LL << (6 + (7 * ((length) - 1))))
    110 #define I64V_MAX(length) ((1LL << (6 + 7 * ((length) - 1))) - 1)
    111 
    112 #define I32V_IN_RANGE(value, length) \
    113   ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length))
    114 #define I64V_IN_RANGE(value, length) \
    115   ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length))
    116 
    117 #define WASM_NO_LOCALS 0
    118 
    119 namespace v8 {
    120 namespace internal {
    121 namespace wasm {
    122 
    123 inline void CheckI32v(int32_t value, int length) {
    124   DCHECK(length >= 1 && length <= 5);
    125   DCHECK(length == 5 || I32V_IN_RANGE(value, length));
    126 }
    127 
    128 inline void CheckI64v(int64_t value, int length) {
    129   DCHECK(length >= 1 && length <= 10);
    130   DCHECK(length == 10 || I64V_IN_RANGE(value, length));
    131 }
    132 
    133 // A helper for encoding local declarations prepended to the body of a
    134 // function.
    135 // TODO(titzer): move this to an appropriate header.
    136 class LocalDeclEncoder {
    137  public:
    138   explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr)
    139       : sig(s), local_decls(zone), total(0) {}
    140 
    141   // Prepend local declarations by creating a new buffer and copying data
    142   // over. The new buffer must be delete[]'d by the caller.
    143   void Prepend(Zone* zone, const byte** start, const byte** end) const {
    144     size_t size = (*end - *start);
    145     byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
    146     size_t pos = Emit(buffer);
    147     memcpy(buffer + pos, *start, size);
    148     pos += size;
    149     *start = buffer;
    150     *end = buffer + pos;
    151   }
    152 
    153   size_t Emit(byte* buffer) const {
    154     size_t pos = 0;
    155     pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size()));
    156     for (size_t i = 0; i < local_decls.size(); ++i) {
    157       pos = WriteUint32v(buffer, pos, local_decls[i].first);
    158       buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second);
    159     }
    160     DCHECK_EQ(Size(), pos);
    161     return pos;
    162   }
    163 
    164   // Add locals declarations to this helper. Return the index of the newly added
    165   // local(s), with an optional adjustment for the parameters.
    166   uint32_t AddLocals(uint32_t count, LocalType type) {
    167     uint32_t result =
    168         static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
    169     total += count;
    170     if (local_decls.size() > 0 && local_decls.back().second == type) {
    171       count += local_decls.back().first;
    172       local_decls.pop_back();
    173     }
    174     local_decls.push_back(std::pair<uint32_t, LocalType>(count, type));
    175     return result;
    176   }
    177 
    178   size_t Size() const {
    179     size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size()));
    180     for (auto p : local_decls) size += 1 + SizeofUint32v(p.first);
    181     return size;
    182   }
    183 
    184   bool has_sig() const { return sig != nullptr; }
    185   FunctionSig* get_sig() const { return sig; }
    186   void set_sig(FunctionSig* s) { sig = s; }
    187 
    188  private:
    189   FunctionSig* sig;
    190   ZoneVector<std::pair<uint32_t, LocalType>> local_decls;
    191   size_t total;
    192 
    193   size_t SizeofUint32v(uint32_t val) const {
    194     size_t size = 1;
    195     while (true) {
    196       byte b = val & MASK_7;
    197       if (b == val) return size;
    198       size++;
    199       val = val >> 7;
    200     }
    201   }
    202 
    203   // TODO(titzer): lift encoding of u32v to a common place.
    204   size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const {
    205     while (true) {
    206       byte b = val & MASK_7;
    207       if (b == val) {
    208         buffer[pos++] = b;
    209         break;
    210       }
    211       buffer[pos++] = 0x80 | b;
    212       val = val >> 7;
    213     }
    214     return pos;
    215   }
    216 };
    217 }  // namespace wasm
    218 }  // namespace internal
    219 }  // namespace v8
    220 
    221 //------------------------------------------------------------------------------
    222 // Int32 Const operations
    223 //------------------------------------------------------------------------------
    224 #define WASM_I32V(val) kExprI32Const, U32V_5(val)
    225 
    226 #define WASM_I32V_1(val) \
    227   static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val)
    228 #define WASM_I32V_2(val) \
    229   static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val)
    230 #define WASM_I32V_3(val) \
    231   static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val)
    232 #define WASM_I32V_4(val) \
    233   static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val)
    234 #define WASM_I32V_5(val) \
    235   static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val)
    236 
    237 //------------------------------------------------------------------------------
    238 // Int64 Const operations
    239 //------------------------------------------------------------------------------
    240 #define WASM_I64V(val)                                                        \
    241   kExprI64Const,                                                              \
    242       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    243       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    244       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    245       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    246       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    247       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
    248       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
    249       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
    250       static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
    251       static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
    252 
    253 #define WASM_I64V_1(val)                                                     \
    254   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \
    255       static_cast<byte>(static_cast<int64_t>(val) & MASK_7)
    256 #define WASM_I64V_2(val)                                                     \
    257   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), 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)
    260 #define WASM_I64V_3(val)                                                     \
    261   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \
    262       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),        \
    263       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \
    264       static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7)
    265 #define WASM_I64V_4(val)                                                      \
    266   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const),  \
    267       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    268       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    269       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    270       static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7)
    271 #define WASM_I64V_5(val)                                                      \
    272   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const),  \
    273       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    274       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    275       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    276       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    277       static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7)
    278 #define WASM_I64V_6(val)                                                      \
    279   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const),  \
    280       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    281       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    282       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    283       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    284       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    285       static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7)
    286 #define WASM_I64V_7(val)                                                      \
    287   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const),  \
    288       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    289       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    290       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    291       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    292       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    293       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
    294       static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7)
    295 #define WASM_I64V_8(val)                                                      \
    296   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const),  \
    297       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    298       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    299       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    300       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    301       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    302       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
    303       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
    304       static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7)
    305 #define WASM_I64V_9(val)                                                      \
    306   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), 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) | 0x80), \
    311       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    312       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
    313       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
    314       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
    315       static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7)
    316 #define WASM_I64V_10(val)                                                     \
    317   static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \
    318       static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80),         \
    319       static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80),  \
    320       static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \
    321       static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \
    322       static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \
    323       static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \
    324       static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \
    325       static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \
    326       static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \
    327       static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7)
    328 
    329 #define WASM_F32(val)                                                       \
    330   kExprF32Const,                                                            \
    331       static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))),        \
    332       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8),  \
    333       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \
    334       static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24)
    335 #define WASM_F64(val)                                        \
    336   kExprF64Const, static_cast<byte>(bit_cast<uint64_t>(val)), \
    337       static_cast<byte>(bit_cast<uint64_t>(val) >> 8),       \
    338       static_cast<byte>(bit_cast<uint64_t>(val) >> 16),      \
    339       static_cast<byte>(bit_cast<uint64_t>(val) >> 24),      \
    340       static_cast<byte>(bit_cast<uint64_t>(val) >> 32),      \
    341       static_cast<byte>(bit_cast<uint64_t>(val) >> 40),      \
    342       static_cast<byte>(bit_cast<uint64_t>(val) >> 48),      \
    343       static_cast<byte>(bit_cast<uint64_t>(val) >> 56)
    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_LOAD_GLOBAL(index) kExprLoadGlobal, static_cast<byte>(index)
    347 #define WASM_STORE_GLOBAL(index, val) \
    348   val, kExprStoreGlobal, static_cast<byte>(index)
    349 #define WASM_LOAD_MEM(type, index)                                             \
    350   index, static_cast<byte>(                                                    \
    351              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    352       ZERO_ALIGNMENT, ZERO_OFFSET
    353 #define WASM_STORE_MEM(type, index, val)                                   \
    354   index, val,                                                              \
    355       static_cast<byte>(                                                   \
    356           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    357       ZERO_ALIGNMENT, ZERO_OFFSET
    358 #define WASM_LOAD_MEM_OFFSET(type, offset, index)                              \
    359   index, static_cast<byte>(                                                    \
    360              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    361       ZERO_ALIGNMENT, static_cast<byte>(offset)
    362 #define WASM_STORE_MEM_OFFSET(type, offset, index, val)                    \
    363   index, val,                                                              \
    364       static_cast<byte>(                                                   \
    365           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    366       ZERO_ALIGNMENT, static_cast<byte>(offset)
    367 #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment)                        \
    368   index, static_cast<byte>(                                                    \
    369              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    370       alignment, ZERO_OFFSET
    371 #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val)              \
    372   index, val,                                                              \
    373       static_cast<byte>(                                                   \
    374           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    375       alignment, ZERO_OFFSET
    376 
    377 #define WASM_CALL_FUNCTION0(index) \
    378   kExprCallFunction, 0, static_cast<byte>(index)
    379 #define WASM_CALL_FUNCTION1(index, a) \
    380   a, kExprCallFunction, 1, static_cast<byte>(index)
    381 #define WASM_CALL_FUNCTION2(index, a, b) \
    382   a, b, kExprCallFunction, 2, static_cast<byte>(index)
    383 #define WASM_CALL_FUNCTION3(index, a, b, c) \
    384   a, b, c, kExprCallFunction, 3, static_cast<byte>(index)
    385 #define WASM_CALL_FUNCTION4(index, a, b, c, d) \
    386   a, b, c, d, kExprCallFunction, 4, static_cast<byte>(index)
    387 #define WASM_CALL_FUNCTION5(index, a, b, c, d, e) \
    388   kExprCallFunction, 5, static_cast<byte>(index)
    389 #define WASM_CALL_FUNCTIONN(arity, index, ...) \
    390   __VA_ARGS__, kExprCallFunction, arity, static_cast<byte>(index)
    391 
    392 #define WASM_CALL_IMPORT0(index) kExprCallImport, 0, static_cast<byte>(index)
    393 #define WASM_CALL_IMPORT1(index, a) \
    394   a, kExprCallImport, 1, static_cast<byte>(index)
    395 #define WASM_CALL_IMPORT2(index, a, b) \
    396   a, b, kExprCallImport, 2, static_cast<byte>(index)
    397 #define WASM_CALL_IMPORT3(index, a, b, c) \
    398   a, b, c, kExprCallImport, 3, static_cast<byte>(index)
    399 #define WASM_CALL_IMPORT4(index, a, b, c, d) \
    400   a, b, c, d, kExprCallImport, 4, static_cast<byte>(index)
    401 #define WASM_CALL_IMPORT5(index, a, b, c, d, e) \
    402   a, b, c, d, e, kExprCallImport, 5, static_cast<byte>(index)
    403 #define WASM_CALL_IMPORTN(arity, index, ...) \
    404   __VA_ARGS__, kExprCallImport, U32V_1(arity), static_cast<byte>(index),
    405 
    406 #define WASM_CALL_INDIRECT0(index, func) \
    407   func, kExprCallIndirect, 0, static_cast<byte>(index)
    408 #define WASM_CALL_INDIRECT1(index, func, a) \
    409   func, a, kExprCallIndirect, 1, static_cast<byte>(index)
    410 #define WASM_CALL_INDIRECT2(index, func, a, b) \
    411   func, a, b, kExprCallIndirect, 2, static_cast<byte>(index)
    412 #define WASM_CALL_INDIRECT3(index, func, a, b, c) \
    413   func, a, b, c, kExprCallIndirect, 3, static_cast<byte>(index)
    414 #define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \
    415   func, a, b, c, d, kExprCallIndirect, 4, static_cast<byte>(index)
    416 #define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \
    417   func, a, b, c, d, e, kExprCallIndirect, 5, static_cast<byte>(index)
    418 #define WASM_CALL_INDIRECTN(arity, index, func, ...) \
    419   func, __VA_ARGS__, kExprCallIndirect, U32V_1(arity), static_cast<byte>(index)
    420 
    421 #define WASM_NOT(x) x, kExprI32Eqz
    422 #define WASM_SEQ(...) __VA_ARGS__
    423 
    424 //------------------------------------------------------------------------------
    425 // Constructs that are composed of multiple bytecodes.
    426 //------------------------------------------------------------------------------
    427 #define WASM_WHILE(x, y) \
    428   kExprLoop, x, kExprIf, y, kExprBr, ARITY_1, DEPTH_1, kExprEnd, kExprEnd
    429 #define WASM_INC_LOCAL(index)                                            \
    430   kExprGetLocal, static_cast<byte>(index), kExprI8Const, 1, kExprI32Add, \
    431       kExprSetLocal, static_cast<byte>(index)
    432 #define WASM_INC_LOCAL_BY(index, count)                     \
    433   kExprGetLocal, static_cast<byte>(index), kExprI8Const,    \
    434       static_cast<byte>(count), kExprI32Add, kExprSetLocal, \
    435       static_cast<byte>(index)
    436 #define WASM_UNOP(opcode, x) x, static_cast<byte>(opcode)
    437 #define WASM_BINOP(opcode, x, y) x, y, static_cast<byte>(opcode)
    438 
    439 //------------------------------------------------------------------------------
    440 // Int32 operations
    441 //------------------------------------------------------------------------------
    442 #define WASM_I32_ADD(x, y) x, y, kExprI32Add
    443 #define WASM_I32_SUB(x, y) x, y, kExprI32Sub
    444 #define WASM_I32_MUL(x, y) x, y, kExprI32Mul
    445 #define WASM_I32_DIVS(x, y) x, y, kExprI32DivS
    446 #define WASM_I32_DIVU(x, y) x, y, kExprI32DivU
    447 #define WASM_I32_REMS(x, y) x, y, kExprI32RemS
    448 #define WASM_I32_REMU(x, y) x, y, kExprI32RemU
    449 #define WASM_I32_AND(x, y) x, y, kExprI32And
    450 #define WASM_I32_IOR(x, y) x, y, kExprI32Ior
    451 #define WASM_I32_XOR(x, y) x, y, kExprI32Xor
    452 #define WASM_I32_SHL(x, y) x, y, kExprI32Shl
    453 #define WASM_I32_SHR(x, y) x, y, kExprI32ShrU
    454 #define WASM_I32_SAR(x, y) x, y, kExprI32ShrS
    455 #define WASM_I32_ROR(x, y) x, y, kExprI32Ror
    456 #define WASM_I32_ROL(x, y) x, y, kExprI32Rol
    457 #define WASM_I32_EQ(x, y) x, y, kExprI32Eq
    458 #define WASM_I32_NE(x, y) x, y, kExprI32Ne
    459 #define WASM_I32_LTS(x, y) x, y, kExprI32LtS
    460 #define WASM_I32_LES(x, y) x, y, kExprI32LeS
    461 #define WASM_I32_LTU(x, y) x, y, kExprI32LtU
    462 #define WASM_I32_LEU(x, y) x, y, kExprI32LeU
    463 #define WASM_I32_GTS(x, y) x, y, kExprI32GtS
    464 #define WASM_I32_GES(x, y) x, y, kExprI32GeS
    465 #define WASM_I32_GTU(x, y) x, y, kExprI32GtU
    466 #define WASM_I32_GEU(x, y) x, y, kExprI32GeU
    467 #define WASM_I32_CLZ(x) x, kExprI32Clz
    468 #define WASM_I32_CTZ(x) x, kExprI32Ctz
    469 #define WASM_I32_POPCNT(x) x, kExprI32Popcnt
    470 #define WASM_I32_EQZ(x) x, kExprI32Eqz
    471 
    472 //------------------------------------------------------------------------------
    473 // Int64 operations
    474 //------------------------------------------------------------------------------
    475 #define WASM_I64_ADD(x, y) x, y, kExprI64Add
    476 #define WASM_I64_SUB(x, y) x, y, kExprI64Sub
    477 #define WASM_I64_MUL(x, y) x, y, kExprI64Mul
    478 #define WASM_I64_DIVS(x, y) x, y, kExprI64DivS
    479 #define WASM_I64_DIVU(x, y) x, y, kExprI64DivU
    480 #define WASM_I64_REMS(x, y) x, y, kExprI64RemS
    481 #define WASM_I64_REMU(x, y) x, y, kExprI64RemU
    482 #define WASM_I64_AND(x, y) x, y, kExprI64And
    483 #define WASM_I64_IOR(x, y) x, y, kExprI64Ior
    484 #define WASM_I64_XOR(x, y) x, y, kExprI64Xor
    485 #define WASM_I64_SHL(x, y) x, y, kExprI64Shl
    486 #define WASM_I64_SHR(x, y) x, y, kExprI64ShrU
    487 #define WASM_I64_SAR(x, y) x, y, kExprI64ShrS
    488 #define WASM_I64_ROR(x, y) x, y, kExprI64Ror
    489 #define WASM_I64_ROL(x, y) x, y, kExprI64Rol
    490 #define WASM_I64_EQ(x, y) x, y, kExprI64Eq
    491 #define WASM_I64_NE(x, y) x, y, kExprI64Ne
    492 #define WASM_I64_LTS(x, y) x, y, kExprI64LtS
    493 #define WASM_I64_LES(x, y) x, y, kExprI64LeS
    494 #define WASM_I64_LTU(x, y) x, y, kExprI64LtU
    495 #define WASM_I64_LEU(x, y) x, y, kExprI64LeU
    496 #define WASM_I64_GTS(x, y) x, y, kExprI64GtS
    497 #define WASM_I64_GES(x, y) x, y, kExprI64GeS
    498 #define WASM_I64_GTU(x, y) x, y, kExprI64GtU
    499 #define WASM_I64_GEU(x, y) x, y, kExprI64GeU
    500 #define WASM_I64_CLZ(x) x, kExprI64Clz
    501 #define WASM_I64_CTZ(x) x, kExprI64Ctz
    502 #define WASM_I64_POPCNT(x) x, kExprI64Popcnt
    503 #define WASM_I64_EQZ(x) x, kExprI64Eqz
    504 
    505 //------------------------------------------------------------------------------
    506 // Float32 operations
    507 //------------------------------------------------------------------------------
    508 #define WASM_F32_ADD(x, y) x, y, kExprF32Add
    509 #define WASM_F32_SUB(x, y) x, y, kExprF32Sub
    510 #define WASM_F32_MUL(x, y) x, y, kExprF32Mul
    511 #define WASM_F32_DIV(x, y) x, y, kExprF32Div
    512 #define WASM_F32_MIN(x, y) x, y, kExprF32Min
    513 #define WASM_F32_MAX(x, y) x, y, kExprF32Max
    514 #define WASM_F32_ABS(x) x, kExprF32Abs
    515 #define WASM_F32_NEG(x) x, kExprF32Neg
    516 #define WASM_F32_COPYSIGN(x, y) x, y, kExprF32CopySign
    517 #define WASM_F32_CEIL(x) x, kExprF32Ceil
    518 #define WASM_F32_FLOOR(x) x, kExprF32Floor
    519 #define WASM_F32_TRUNC(x) x, kExprF32Trunc
    520 #define WASM_F32_NEARESTINT(x) x, kExprF32NearestInt
    521 #define WASM_F32_SQRT(x) x, kExprF32Sqrt
    522 #define WASM_F32_EQ(x, y) x, y, kExprF32Eq
    523 #define WASM_F32_NE(x, y) x, y, kExprF32Ne
    524 #define WASM_F32_LT(x, y) x, y, kExprF32Lt
    525 #define WASM_F32_LE(x, y) x, y, kExprF32Le
    526 #define WASM_F32_GT(x, y) x, y, kExprF32Gt
    527 #define WASM_F32_GE(x, y) x, y, kExprF32Ge
    528 
    529 //------------------------------------------------------------------------------
    530 // Float64 operations
    531 //------------------------------------------------------------------------------
    532 #define WASM_F64_ADD(x, y) x, y, kExprF64Add
    533 #define WASM_F64_SUB(x, y) x, y, kExprF64Sub
    534 #define WASM_F64_MUL(x, y) x, y, kExprF64Mul
    535 #define WASM_F64_DIV(x, y) x, y, kExprF64Div
    536 #define WASM_F64_MIN(x, y) x, y, kExprF64Min
    537 #define WASM_F64_MAX(x, y) x, y, kExprF64Max
    538 #define WASM_F64_ABS(x) x, kExprF64Abs
    539 #define WASM_F64_NEG(x) x, kExprF64Neg
    540 #define WASM_F64_COPYSIGN(x, y) x, y, kExprF64CopySign
    541 #define WASM_F64_CEIL(x) x, kExprF64Ceil
    542 #define WASM_F64_FLOOR(x) x, kExprF64Floor
    543 #define WASM_F64_TRUNC(x) x, kExprF64Trunc
    544 #define WASM_F64_NEARESTINT(x) x, kExprF64NearestInt
    545 #define WASM_F64_SQRT(x) x, kExprF64Sqrt
    546 #define WASM_F64_EQ(x, y) x, y, kExprF64Eq
    547 #define WASM_F64_NE(x, y) x, y, kExprF64Ne
    548 #define WASM_F64_LT(x, y) x, y, kExprF64Lt
    549 #define WASM_F64_LE(x, y) x, y, kExprF64Le
    550 #define WASM_F64_GT(x, y) x, y, kExprF64Gt
    551 #define WASM_F64_GE(x, y) x, y, kExprF64Ge
    552 
    553 //------------------------------------------------------------------------------
    554 // Type conversions.
    555 //------------------------------------------------------------------------------
    556 #define WASM_I32_SCONVERT_F32(x) x, kExprI32SConvertF32
    557 #define WASM_I32_SCONVERT_F64(x) x, kExprI32SConvertF64
    558 #define WASM_I32_UCONVERT_F32(x) x, kExprI32UConvertF32
    559 #define WASM_I32_UCONVERT_F64(x) x, kExprI32UConvertF64
    560 #define WASM_I32_CONVERT_I64(x) x, kExprI32ConvertI64
    561 #define WASM_I64_SCONVERT_F32(x) x, kExprI64SConvertF32
    562 #define WASM_I64_SCONVERT_F64(x) x, kExprI64SConvertF64
    563 #define WASM_I64_UCONVERT_F32(x) x, kExprI64UConvertF32
    564 #define WASM_I64_UCONVERT_F64(x) x, kExprI64UConvertF64
    565 #define WASM_I64_SCONVERT_I32(x) x, kExprI64SConvertI32
    566 #define WASM_I64_UCONVERT_I32(x) x, kExprI64UConvertI32
    567 #define WASM_F32_SCONVERT_I32(x) x, kExprF32SConvertI32
    568 #define WASM_F32_UCONVERT_I32(x) x, kExprF32UConvertI32
    569 #define WASM_F32_SCONVERT_I64(x) x, kExprF32SConvertI64
    570 #define WASM_F32_UCONVERT_I64(x) x, kExprF32UConvertI64
    571 #define WASM_F32_CONVERT_F64(x) x, kExprF32ConvertF64
    572 #define WASM_F32_REINTERPRET_I32(x) x, kExprF32ReinterpretI32
    573 #define WASM_F64_SCONVERT_I32(x) x, kExprF64SConvertI32
    574 #define WASM_F64_UCONVERT_I32(x) x, kExprF64UConvertI32
    575 #define WASM_F64_SCONVERT_I64(x) x, kExprF64SConvertI64
    576 #define WASM_F64_UCONVERT_I64(x) x, kExprF64UConvertI64
    577 #define WASM_F64_CONVERT_F32(x) x, kExprF64ConvertF32
    578 #define WASM_F64_REINTERPRET_I64(x) x, kExprF64ReinterpretI64
    579 #define WASM_I32_REINTERPRET_F32(x) x, kExprI32ReinterpretF32
    580 #define WASM_I64_REINTERPRET_F64(x) x, kExprI64ReinterpretF64
    581 
    582 #define SIG_ENTRY_v_v kWasmFunctionTypeForm, 0, 0
    583 #define SIZEOF_SIG_ENTRY_v_v 3
    584 
    585 #define SIG_ENTRY_v_x(a) kWasmFunctionTypeForm, 1, a, 0
    586 #define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeForm, 2, a, b, 0
    587 #define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 0
    588 #define SIZEOF_SIG_ENTRY_v_x 4
    589 #define SIZEOF_SIG_ENTRY_v_xx 5
    590 #define SIZEOF_SIG_ENTRY_v_xxx 6
    591 
    592 #define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r
    593 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r
    594 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r
    595 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r
    596 #define SIZEOF_SIG_ENTRY_x 4
    597 #define SIZEOF_SIG_ENTRY_x_x 5
    598 #define SIZEOF_SIG_ENTRY_x_xx 6
    599 #define SIZEOF_SIG_ENTRY_x_xxx 7
    600 
    601 #endif  // V8_WASM_MACRO_GEN_H_
    602