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/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 
     63 #define ARITY_0 0
     64 #define ARITY_1 1
     65 #define ARITY_2 2
     66 #define DEPTH_0 0
     67 #define DEPTH_1 1
     68 #define DEPTH_2 2
     69 #define ARITY_2 2
     70 
     71 #define WASM_BLOCK(...) kExprBlock, kLocalVoid, __VA_ARGS__, kExprEnd
     72 
     73 #define WASM_BLOCK_T(t, ...)                                       \
     74   kExprBlock, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t)), \
     75       __VA_ARGS__, kExprEnd
     76 
     77 #define WASM_BLOCK_TT(t1, t2, ...)                                       \
     78   kExprBlock, kMultivalBlock, 0,                                         \
     79       static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t1)),              \
     80       static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t2)), __VA_ARGS__, \
     81       kExprEnd
     82 
     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_INFINITE_LOOP kExprLoop, kLocalVoid, kExprBr, DEPTH_0, kExprEnd
     89 
     90 #define WASM_LOOP(...) kExprLoop, kLocalVoid, __VA_ARGS__, kExprEnd
     91 #define WASM_LOOP_I(...) kExprLoop, kLocalI32, __VA_ARGS__, kExprEnd
     92 #define WASM_LOOP_L(...) kExprLoop, kLocalI64, __VA_ARGS__, kExprEnd
     93 #define WASM_LOOP_F(...) kExprLoop, kLocalF32, __VA_ARGS__, kExprEnd
     94 #define WASM_LOOP_D(...) kExprLoop, kLocalF64, __VA_ARGS__, kExprEnd
     95 
     96 #define WASM_IF(cond, tstmt) cond, kExprIf, kLocalVoid, tstmt, kExprEnd
     97 
     98 #define WASM_IF_ELSE(cond, tstmt, fstmt) \
     99   cond, kExprIf, kLocalVoid, tstmt, kExprElse, fstmt, kExprEnd
    100 
    101 #define WASM_IF_ELSE_T(t, cond, tstmt, fstmt)                                \
    102   cond, kExprIf, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t)), tstmt, \
    103       kExprElse, fstmt, kExprEnd
    104 
    105 #define WASM_IF_ELSE_TT(t1, t2, cond, tstmt, fstmt)                           \
    106   cond, kExprIf, kMultivalBlock, 0,                                           \
    107       static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t1)),                   \
    108       static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(t2)), tstmt, kExprElse, \
    109       fstmt, kExprEnd
    110 
    111 #define WASM_IF_ELSE_I(cond, tstmt, fstmt) \
    112   cond, kExprIf, kLocalI32, tstmt, kExprElse, fstmt, kExprEnd
    113 #define WASM_IF_ELSE_L(cond, tstmt, fstmt) \
    114   cond, kExprIf, kLocalI64, tstmt, kExprElse, fstmt, kExprEnd
    115 #define WASM_IF_ELSE_F(cond, tstmt, fstmt) \
    116   cond, kExprIf, kLocalF32, tstmt, kExprElse, fstmt, kExprEnd
    117 #define WASM_IF_ELSE_D(cond, tstmt, fstmt) \
    118   cond, kExprIf, kLocalF64, tstmt, kExprElse, fstmt, kExprEnd
    119 
    120 #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect
    121 
    122 #define WASM_RETURN0 kExprReturn
    123 #define WASM_RETURN1(val) val, kExprReturn
    124 #define WASM_RETURNN(count, ...) __VA_ARGS__, kExprReturn
    125 
    126 #define WASM_BR(depth) kExprBr, static_cast<byte>(depth)
    127 #define WASM_BR_IF(depth, cond) cond, kExprBrIf, static_cast<byte>(depth)
    128 #define WASM_BR_IFD(depth, val, cond) \
    129   val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
    130 #define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth)
    131 #define WASM_UNREACHABLE kExprUnreachable
    132 
    133 #define WASM_BR_TABLE(key, count, ...) \
    134   key, kExprBrTable, U32V_1(count), __VA_ARGS__
    135 
    136 #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8)
    137 #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8)
    138 
    139 //------------------------------------------------------------------------------
    140 // Misc expressions.
    141 //------------------------------------------------------------------------------
    142 #define WASM_ID(...) __VA_ARGS__
    143 #define WASM_ZERO kExprI8Const, 0
    144 #define WASM_ONE kExprI8Const, 1
    145 #define WASM_I8(val) kExprI8Const, static_cast<byte>(val)
    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 
    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 
    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)
    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.
    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 
    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::LocalTypeCodeFor(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.
    206   uint32_t AddLocals(uint32_t count, LocalType 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, LocalType>(count, type));
    215     return result;
    216   }
    217 
    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 
    224   bool has_sig() const { return sig != nullptr; }
    225   FunctionSig* get_sig() const { return sig; }
    226   void set_sig(FunctionSig* s) { sig = s; }
    227 
    228  private:
    229   FunctionSig* sig;
    230   ZoneVector<std::pair<uint32_t, LocalType>> local_decls;
    231   size_t total;
    232 
    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.
    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, static_cast<byte>(bit_cast<uint64_t>(val)), \
    377       static_cast<byte>(bit_cast<uint64_t>(val) >> 8),       \
    378       static_cast<byte>(bit_cast<uint64_t>(val) >> 16),      \
    379       static_cast<byte>(bit_cast<uint64_t>(val) >> 24),      \
    380       static_cast<byte>(bit_cast<uint64_t>(val) >> 32),      \
    381       static_cast<byte>(bit_cast<uint64_t>(val) >> 40),      \
    382       static_cast<byte>(bit_cast<uint64_t>(val) >> 48),      \
    383       static_cast<byte>(bit_cast<uint64_t>(val) >> 56)
    384 #define WASM_GET_LOCAL(index) kExprGetLocal, static_cast<byte>(index)
    385 #define WASM_SET_LOCAL(index, val) val, kExprSetLocal, static_cast<byte>(index)
    386 #define WASM_TEE_LOCAL(index, val) val, kExprTeeLocal, static_cast<byte>(index)
    387 #define WASM_DROP kExprDrop
    388 #define WASM_GET_GLOBAL(index) kExprGetGlobal, static_cast<byte>(index)
    389 #define WASM_SET_GLOBAL(index, val) \
    390   val, kExprSetGlobal, static_cast<byte>(index)
    391 #define WASM_LOAD_MEM(type, index)                                             \
    392   index, static_cast<byte>(                                                    \
    393              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    394       ZERO_ALIGNMENT, ZERO_OFFSET
    395 #define WASM_STORE_MEM(type, index, val)                                   \
    396   index, val,                                                              \
    397       static_cast<byte>(                                                   \
    398           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    399       ZERO_ALIGNMENT, ZERO_OFFSET
    400 #define WASM_LOAD_MEM_OFFSET(type, offset, index)                              \
    401   index, static_cast<byte>(                                                    \
    402              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    403       ZERO_ALIGNMENT, static_cast<byte>(offset)
    404 #define WASM_STORE_MEM_OFFSET(type, offset, index, val)                    \
    405   index, val,                                                              \
    406       static_cast<byte>(                                                   \
    407           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    408       ZERO_ALIGNMENT, static_cast<byte>(offset)
    409 #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment)                        \
    410   index, static_cast<byte>(                                                    \
    411              v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
    412       alignment, ZERO_OFFSET
    413 #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val)              \
    414   index, val,                                                              \
    415       static_cast<byte>(                                                   \
    416           v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
    417       alignment, ZERO_OFFSET
    418 
    419 #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index)
    420 #define WASM_CALL_FUNCTION(index, ...) \
    421   __VA_ARGS__, kExprCallFunction, static_cast<byte>(index)
    422 
    423 #define TABLE_ZERO 0
    424 
    425 // TODO(titzer): change usages of these macros to put func last.
    426 #define WASM_CALL_INDIRECT0(index, func) \
    427   func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    428 #define WASM_CALL_INDIRECT1(index, func, a) \
    429   a, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    430 #define WASM_CALL_INDIRECT2(index, func, a, b) \
    431   a, b, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    432 #define WASM_CALL_INDIRECT3(index, func, a, b, c) \
    433   a, b, c, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    434 #define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \
    435   a, b, c, d, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    436 #define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \
    437   a, b, c, d, e, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    438 #define WASM_CALL_INDIRECTN(arity, index, func, ...) \
    439   __VA_ARGS__, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
    440 
    441 #define WASM_NOT(x) x, kExprI32Eqz
    442 #define WASM_SEQ(...) __VA_ARGS__
    443 
    444 //------------------------------------------------------------------------------
    445 // Constructs that are composed of multiple bytecodes.
    446 //------------------------------------------------------------------------------
    447 #define WASM_WHILE(x, y)                                              \
    448   kExprLoop, kLocalVoid, x, kExprIf, kLocalVoid, y, kExprBr, DEPTH_1, \
    449       kExprEnd, kExprEnd
    450 #define WASM_INC_LOCAL(index)                                            \
    451   kExprGetLocal, static_cast<byte>(index), kExprI8Const, 1, kExprI32Add, \
    452       kExprTeeLocal, static_cast<byte>(index)
    453 #define WASM_INC_LOCAL_BYV(index, count)                    \
    454   kExprGetLocal, static_cast<byte>(index), kExprI8Const,    \
    455       static_cast<byte>(count), kExprI32Add, kExprTeeLocal, \
    456       static_cast<byte>(index)
    457 #define WASM_INC_LOCAL_BY(index, count)                     \
    458   kExprGetLocal, static_cast<byte>(index), kExprI8Const,    \
    459       static_cast<byte>(count), kExprI32Add, kExprSetLocal, \
    460       static_cast<byte>(index)
    461 #define WASM_UNOP(opcode, x) x, static_cast<byte>(opcode)
    462 #define WASM_BINOP(opcode, x, y) x, y, static_cast<byte>(opcode)
    463 
    464 //------------------------------------------------------------------------------
    465 // Int32 operations
    466 //------------------------------------------------------------------------------
    467 #define WASM_I32_ADD(x, y) x, y, kExprI32Add
    468 #define WASM_I32_SUB(x, y) x, y, kExprI32Sub
    469 #define WASM_I32_MUL(x, y) x, y, kExprI32Mul
    470 #define WASM_I32_DIVS(x, y) x, y, kExprI32DivS
    471 #define WASM_I32_DIVU(x, y) x, y, kExprI32DivU
    472 #define WASM_I32_REMS(x, y) x, y, kExprI32RemS
    473 #define WASM_I32_REMU(x, y) x, y, kExprI32RemU
    474 #define WASM_I32_AND(x, y) x, y, kExprI32And
    475 #define WASM_I32_IOR(x, y) x, y, kExprI32Ior
    476 #define WASM_I32_XOR(x, y) x, y, kExprI32Xor
    477 #define WASM_I32_SHL(x, y) x, y, kExprI32Shl
    478 #define WASM_I32_SHR(x, y) x, y, kExprI32ShrU
    479 #define WASM_I32_SAR(x, y) x, y, kExprI32ShrS
    480 #define WASM_I32_ROR(x, y) x, y, kExprI32Ror
    481 #define WASM_I32_ROL(x, y) x, y, kExprI32Rol
    482 #define WASM_I32_EQ(x, y) x, y, kExprI32Eq
    483 #define WASM_I32_NE(x, y) x, y, kExprI32Ne
    484 #define WASM_I32_LTS(x, y) x, y, kExprI32LtS
    485 #define WASM_I32_LES(x, y) x, y, kExprI32LeS
    486 #define WASM_I32_LTU(x, y) x, y, kExprI32LtU
    487 #define WASM_I32_LEU(x, y) x, y, kExprI32LeU
    488 #define WASM_I32_GTS(x, y) x, y, kExprI32GtS
    489 #define WASM_I32_GES(x, y) x, y, kExprI32GeS
    490 #define WASM_I32_GTU(x, y) x, y, kExprI32GtU
    491 #define WASM_I32_GEU(x, y) x, y, kExprI32GeU
    492 #define WASM_I32_CLZ(x) x, kExprI32Clz
    493 #define WASM_I32_CTZ(x) x, kExprI32Ctz
    494 #define WASM_I32_POPCNT(x) x, kExprI32Popcnt
    495 #define WASM_I32_EQZ(x) x, kExprI32Eqz
    496 
    497 //------------------------------------------------------------------------------
    498 // Asmjs Int32 operations
    499 //------------------------------------------------------------------------------
    500 #define WASM_I32_ASMJS_DIVS(x, y) x, y, kExprI32AsmjsDivS
    501 #define WASM_I32_ASMJS_REMS(x, y) x, y, kExprI32AsmjsRemS
    502 #define WASM_I32_ASMJS_DIVU(x, y) x, y, kExprI32AsmjsDivU
    503 #define WASM_I32_ASMJS_REMU(x, y) x, y, kExprI32AsmjsRemU
    504 
    505 //------------------------------------------------------------------------------
    506 // Int64 operations
    507 //------------------------------------------------------------------------------
    508 #define WASM_I64_ADD(x, y) x, y, kExprI64Add
    509 #define WASM_I64_SUB(x, y) x, y, kExprI64Sub
    510 #define WASM_I64_MUL(x, y) x, y, kExprI64Mul
    511 #define WASM_I64_DIVS(x, y) x, y, kExprI64DivS
    512 #define WASM_I64_DIVU(x, y) x, y, kExprI64DivU
    513 #define WASM_I64_REMS(x, y) x, y, kExprI64RemS
    514 #define WASM_I64_REMU(x, y) x, y, kExprI64RemU
    515 #define WASM_I64_AND(x, y) x, y, kExprI64And
    516 #define WASM_I64_IOR(x, y) x, y, kExprI64Ior
    517 #define WASM_I64_XOR(x, y) x, y, kExprI64Xor
    518 #define WASM_I64_SHL(x, y) x, y, kExprI64Shl
    519 #define WASM_I64_SHR(x, y) x, y, kExprI64ShrU
    520 #define WASM_I64_SAR(x, y) x, y, kExprI64ShrS
    521 #define WASM_I64_ROR(x, y) x, y, kExprI64Ror
    522 #define WASM_I64_ROL(x, y) x, y, kExprI64Rol
    523 #define WASM_I64_EQ(x, y) x, y, kExprI64Eq
    524 #define WASM_I64_NE(x, y) x, y, kExprI64Ne
    525 #define WASM_I64_LTS(x, y) x, y, kExprI64LtS
    526 #define WASM_I64_LES(x, y) x, y, kExprI64LeS
    527 #define WASM_I64_LTU(x, y) x, y, kExprI64LtU
    528 #define WASM_I64_LEU(x, y) x, y, kExprI64LeU
    529 #define WASM_I64_GTS(x, y) x, y, kExprI64GtS
    530 #define WASM_I64_GES(x, y) x, y, kExprI64GeS
    531 #define WASM_I64_GTU(x, y) x, y, kExprI64GtU
    532 #define WASM_I64_GEU(x, y) x, y, kExprI64GeU
    533 #define WASM_I64_CLZ(x) x, kExprI64Clz
    534 #define WASM_I64_CTZ(x) x, kExprI64Ctz
    535 #define WASM_I64_POPCNT(x) x, kExprI64Popcnt
    536 #define WASM_I64_EQZ(x) x, kExprI64Eqz
    537 
    538 //------------------------------------------------------------------------------
    539 // Float32 operations
    540 //------------------------------------------------------------------------------
    541 #define WASM_F32_ADD(x, y) x, y, kExprF32Add
    542 #define WASM_F32_SUB(x, y) x, y, kExprF32Sub
    543 #define WASM_F32_MUL(x, y) x, y, kExprF32Mul
    544 #define WASM_F32_DIV(x, y) x, y, kExprF32Div
    545 #define WASM_F32_MIN(x, y) x, y, kExprF32Min
    546 #define WASM_F32_MAX(x, y) x, y, kExprF32Max
    547 #define WASM_F32_ABS(x) x, kExprF32Abs
    548 #define WASM_F32_NEG(x) x, kExprF32Neg
    549 #define WASM_F32_COPYSIGN(x, y) x, y, kExprF32CopySign
    550 #define WASM_F32_CEIL(x) x, kExprF32Ceil
    551 #define WASM_F32_FLOOR(x) x, kExprF32Floor
    552 #define WASM_F32_TRUNC(x) x, kExprF32Trunc
    553 #define WASM_F32_NEARESTINT(x) x, kExprF32NearestInt
    554 #define WASM_F32_SQRT(x) x, kExprF32Sqrt
    555 #define WASM_F32_EQ(x, y) x, y, kExprF32Eq
    556 #define WASM_F32_NE(x, y) x, y, kExprF32Ne
    557 #define WASM_F32_LT(x, y) x, y, kExprF32Lt
    558 #define WASM_F32_LE(x, y) x, y, kExprF32Le
    559 #define WASM_F32_GT(x, y) x, y, kExprF32Gt
    560 #define WASM_F32_GE(x, y) x, y, kExprF32Ge
    561 
    562 //------------------------------------------------------------------------------
    563 // Float64 operations
    564 //------------------------------------------------------------------------------
    565 #define WASM_F64_ADD(x, y) x, y, kExprF64Add
    566 #define WASM_F64_SUB(x, y) x, y, kExprF64Sub
    567 #define WASM_F64_MUL(x, y) x, y, kExprF64Mul
    568 #define WASM_F64_DIV(x, y) x, y, kExprF64Div
    569 #define WASM_F64_MIN(x, y) x, y, kExprF64Min
    570 #define WASM_F64_MAX(x, y) x, y, kExprF64Max
    571 #define WASM_F64_ABS(x) x, kExprF64Abs
    572 #define WASM_F64_NEG(x) x, kExprF64Neg
    573 #define WASM_F64_COPYSIGN(x, y) x, y, kExprF64CopySign
    574 #define WASM_F64_CEIL(x) x, kExprF64Ceil
    575 #define WASM_F64_FLOOR(x) x, kExprF64Floor
    576 #define WASM_F64_TRUNC(x) x, kExprF64Trunc
    577 #define WASM_F64_NEARESTINT(x) x, kExprF64NearestInt
    578 #define WASM_F64_SQRT(x) x, kExprF64Sqrt
    579 #define WASM_F64_EQ(x, y) x, y, kExprF64Eq
    580 #define WASM_F64_NE(x, y) x, y, kExprF64Ne
    581 #define WASM_F64_LT(x, y) x, y, kExprF64Lt
    582 #define WASM_F64_LE(x, y) x, y, kExprF64Le
    583 #define WASM_F64_GT(x, y) x, y, kExprF64Gt
    584 #define WASM_F64_GE(x, y) x, y, kExprF64Ge
    585 
    586 //------------------------------------------------------------------------------
    587 // Type conversions.
    588 //------------------------------------------------------------------------------
    589 #define WASM_I32_SCONVERT_F32(x) x, kExprI32SConvertF32
    590 #define WASM_I32_SCONVERT_F64(x) x, kExprI32SConvertF64
    591 #define WASM_I32_UCONVERT_F32(x) x, kExprI32UConvertF32
    592 #define WASM_I32_UCONVERT_F64(x) x, kExprI32UConvertF64
    593 #define WASM_I32_CONVERT_I64(x) x, kExprI32ConvertI64
    594 #define WASM_I64_SCONVERT_F32(x) x, kExprI64SConvertF32
    595 #define WASM_I64_SCONVERT_F64(x) x, kExprI64SConvertF64
    596 #define WASM_I64_UCONVERT_F32(x) x, kExprI64UConvertF32
    597 #define WASM_I64_UCONVERT_F64(x) x, kExprI64UConvertF64
    598 #define WASM_I64_SCONVERT_I32(x) x, kExprI64SConvertI32
    599 #define WASM_I64_UCONVERT_I32(x) x, kExprI64UConvertI32
    600 #define WASM_F32_SCONVERT_I32(x) x, kExprF32SConvertI32
    601 #define WASM_F32_UCONVERT_I32(x) x, kExprF32UConvertI32
    602 #define WASM_F32_SCONVERT_I64(x) x, kExprF32SConvertI64
    603 #define WASM_F32_UCONVERT_I64(x) x, kExprF32UConvertI64
    604 #define WASM_F32_CONVERT_F64(x) x, kExprF32ConvertF64
    605 #define WASM_F32_REINTERPRET_I32(x) x, kExprF32ReinterpretI32
    606 #define WASM_F64_SCONVERT_I32(x) x, kExprF64SConvertI32
    607 #define WASM_F64_UCONVERT_I32(x) x, kExprF64UConvertI32
    608 #define WASM_F64_SCONVERT_I64(x) x, kExprF64SConvertI64
    609 #define WASM_F64_UCONVERT_I64(x) x, kExprF64UConvertI64
    610 #define WASM_F64_CONVERT_F32(x) x, kExprF64ConvertF32
    611 #define WASM_F64_REINTERPRET_I64(x) x, kExprF64ReinterpretI64
    612 #define WASM_I32_REINTERPRET_F32(x) x, kExprI32ReinterpretF32
    613 #define WASM_I64_REINTERPRET_F64(x) x, kExprI64ReinterpretF64
    614 
    615 //------------------------------------------------------------------------------
    616 // Memory Operations.
    617 //------------------------------------------------------------------------------
    618 #define WASM_GROW_MEMORY(x) x, kExprGrowMemory, 0
    619 #define WASM_MEMORY_SIZE kExprMemorySize, 0
    620 
    621 //------------------------------------------------------------------------------
    622 // Simd Operations.
    623 //------------------------------------------------------------------------------
    624 #define WASM_SIMD_I32x4_SPLAT(x) x, kSimdPrefix, kExprI32x4Splat & 0xff
    625 #define WASM_SIMD_I32x4_EXTRACT_LANE(lane, x) \
    626   x, kSimdPrefix, kExprI32x4ExtractLane & 0xff, static_cast<byte>(lane)
    627 #define WASM_SIMD_I32x4_ADD(x, y) x, y, kSimdPrefix, kExprI32x4Add & 0xff
    628 #define WASM_SIMD_F32x4_SPLAT(x) x, kSimdPrefix, kExprF32x4Splat & 0xff
    629 #define WASM_SIMD_F32x4_EXTRACT_LANE(lane, x) \
    630   x, kSimdPrefix, kExprF32x4ExtractLane & 0xff, static_cast<byte>(lane)
    631 #define WASM_SIMD_F32x4_ADD(x, y) x, y, kSimdPrefix, kExprF32x4Add & 0xff
    632 
    633 #define SIG_ENTRY_v_v kWasmFunctionTypeForm, 0, 0
    634 #define SIZEOF_SIG_ENTRY_v_v 3
    635 
    636 #define SIG_ENTRY_v_x(a) kWasmFunctionTypeForm, 1, a, 0
    637 #define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeForm, 2, a, b, 0
    638 #define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 0
    639 #define SIZEOF_SIG_ENTRY_v_x 4
    640 #define SIZEOF_SIG_ENTRY_v_xx 5
    641 #define SIZEOF_SIG_ENTRY_v_xxx 6
    642 
    643 #define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r
    644 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r
    645 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r
    646 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r
    647 #define SIZEOF_SIG_ENTRY_x 4
    648 #define SIZEOF_SIG_ENTRY_x_x 5
    649 #define SIZEOF_SIG_ENTRY_x_xx 6
    650 #define SIZEOF_SIG_ENTRY_x_xxx 7
    651 
    652 #define WASM_BRV(depth, val) val, kExprBr, static_cast<byte>(depth)
    653 #define WASM_BRV_IF(depth, val, cond) \
    654   val, cond, kExprBrIf, static_cast<byte>(depth)
    655 #define WASM_BRV_IFD(depth, val, cond) \
    656   val, cond, kExprBrIf, static_cast<byte>(depth), kExprDrop
    657 #define WASM_IFB(cond, ...) cond, kExprIf, kLocalVoid, __VA_ARGS__, kExprEnd
    658 #define WASM_BR_TABLEV(val, key, count, ...) \
    659   val, key, kExprBrTable, U32V_1(count), __VA_ARGS__
    660 
    661 #endif  // V8_WASM_MACRO_GEN_H_
    662