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_OPCODES_H_
      6 #define V8_WASM_OPCODES_H_
      7 
      8 #include "src/globals.h"
      9 #include "src/machine-type.h"
     10 #include "src/signature.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 namespace wasm {
     15 
     16 // Binary encoding of local types.
     17 enum LocalTypeCode {
     18   kLocalVoid = 0x40,
     19   kLocalI32 = 0x7f,
     20   kLocalI64 = 0x7e,
     21   kLocalF32 = 0x7d,
     22   kLocalF64 = 0x7c,
     23   kLocalS128 = 0x7b
     24 };
     25 
     26 // Type code for multi-value block types.
     27 static const uint8_t kMultivalBlock = 0x41;
     28 
     29 // We reuse the internal machine type to represent WebAssembly AST types.
     30 // A typedef improves readability without adding a whole new type system.
     31 typedef MachineRepresentation LocalType;
     32 const LocalType kAstStmt = MachineRepresentation::kNone;
     33 const LocalType kAstI32 = MachineRepresentation::kWord32;
     34 const LocalType kAstI64 = MachineRepresentation::kWord64;
     35 const LocalType kAstF32 = MachineRepresentation::kFloat32;
     36 const LocalType kAstF64 = MachineRepresentation::kFloat64;
     37 const LocalType kAstS128 = MachineRepresentation::kSimd128;
     38 // We use kTagged here because kNone is already used by kAstStmt.
     39 const LocalType kAstEnd = MachineRepresentation::kTagged;
     40 
     41 typedef Signature<LocalType> FunctionSig;
     42 std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
     43 
     44 typedef Vector<const char> WasmName;
     45 
     46 typedef int WasmCodePosition;
     47 const WasmCodePosition kNoCodePosition = -1;
     48 
     49 // Control expressions and blocks.
     50 #define FOREACH_CONTROL_OPCODE(V)      \
     51   V(Unreachable, 0x00, _)              \
     52   V(Nop, 0x01, _)                      \
     53   V(Block, 0x02, _)                    \
     54   V(Loop, 0x03, _)                     \
     55   V(If, 0x004, _)                      \
     56   V(Else, 0x05, _)                     \
     57   V(Try, 0x06, _ /* eh_prototype */)   \
     58   V(Catch, 0x07, _ /* eh_prototype */) \
     59   V(Throw, 0x08, _ /* eh_prototype */) \
     60   V(End, 0x0b, _)                      \
     61   V(Br, 0x0c, _)                       \
     62   V(BrIf, 0x0d, _)                     \
     63   V(BrTable, 0x0e, _)                  \
     64   V(Return, 0x0f, _)
     65 
     66 // Constants, locals, globals, and calls.
     67 #define FOREACH_MISC_OPCODE(V) \
     68   V(CallFunction, 0x10, _)     \
     69   V(CallIndirect, 0x11, _)     \
     70   V(Drop, 0x1a, _)             \
     71   V(Select, 0x1b, _)           \
     72   V(GetLocal, 0x20, _)         \
     73   V(SetLocal, 0x21, _)         \
     74   V(TeeLocal, 0x22, _)         \
     75   V(GetGlobal, 0x23, _)        \
     76   V(SetGlobal, 0x24, _)        \
     77   V(I32Const, 0x41, _)         \
     78   V(I64Const, 0x42, _)         \
     79   V(F32Const, 0x43, _)         \
     80   V(F64Const, 0x44, _)         \
     81   V(I8Const, 0xcb, _ /* TODO(titzer): V8 specific, remove */)
     82 
     83 // Load memory expressions.
     84 #define FOREACH_LOAD_MEM_OPCODE(V) \
     85   V(I32LoadMem, 0x28, i_i)         \
     86   V(I64LoadMem, 0x29, l_i)         \
     87   V(F32LoadMem, 0x2a, f_i)         \
     88   V(F64LoadMem, 0x2b, d_i)         \
     89   V(I32LoadMem8S, 0x2c, i_i)       \
     90   V(I32LoadMem8U, 0x2d, i_i)       \
     91   V(I32LoadMem16S, 0x2e, i_i)      \
     92   V(I32LoadMem16U, 0x2f, i_i)      \
     93   V(I64LoadMem8S, 0x30, l_i)       \
     94   V(I64LoadMem8U, 0x31, l_i)       \
     95   V(I64LoadMem16S, 0x32, l_i)      \
     96   V(I64LoadMem16U, 0x33, l_i)      \
     97   V(I64LoadMem32S, 0x34, l_i)      \
     98   V(I64LoadMem32U, 0x35, l_i)
     99 
    100 // Store memory expressions.
    101 #define FOREACH_STORE_MEM_OPCODE(V) \
    102   V(I32StoreMem, 0x36, i_ii)        \
    103   V(I64StoreMem, 0x37, l_il)        \
    104   V(F32StoreMem, 0x38, f_if)        \
    105   V(F64StoreMem, 0x39, d_id)        \
    106   V(I32StoreMem8, 0x3a, i_ii)       \
    107   V(I32StoreMem16, 0x3b, i_ii)      \
    108   V(I64StoreMem8, 0x3c, l_il)       \
    109   V(I64StoreMem16, 0x3d, l_il)      \
    110   V(I64StoreMem32, 0x3e, l_il)
    111 
    112 // Miscellaneous memory expressions
    113 #define FOREACH_MISC_MEM_OPCODE(V) \
    114   V(MemorySize, 0x3f, i_v)         \
    115   V(GrowMemory, 0x40, i_i)
    116 
    117 // Expressions with signatures.
    118 #define FOREACH_SIMPLE_OPCODE(V)  \
    119   V(I32Eqz, 0x45, i_i)            \
    120   V(I32Eq, 0x46, i_ii)            \
    121   V(I32Ne, 0x47, i_ii)            \
    122   V(I32LtS, 0x48, i_ii)           \
    123   V(I32LtU, 0x49, i_ii)           \
    124   V(I32GtS, 0x4a, i_ii)           \
    125   V(I32GtU, 0x4b, i_ii)           \
    126   V(I32LeS, 0x4c, i_ii)           \
    127   V(I32LeU, 0x4d, i_ii)           \
    128   V(I32GeS, 0x4e, i_ii)           \
    129   V(I32GeU, 0x4f, i_ii)           \
    130   V(I64Eqz, 0x50, i_l)            \
    131   V(I64Eq, 0x51, i_ll)            \
    132   V(I64Ne, 0x52, i_ll)            \
    133   V(I64LtS, 0x53, i_ll)           \
    134   V(I64LtU, 0x54, i_ll)           \
    135   V(I64GtS, 0x55, i_ll)           \
    136   V(I64GtU, 0x56, i_ll)           \
    137   V(I64LeS, 0x57, i_ll)           \
    138   V(I64LeU, 0x58, i_ll)           \
    139   V(I64GeS, 0x59, i_ll)           \
    140   V(I64GeU, 0x5a, i_ll)           \
    141   V(F32Eq, 0x5b, i_ff)            \
    142   V(F32Ne, 0x5c, i_ff)            \
    143   V(F32Lt, 0x5d, i_ff)            \
    144   V(F32Gt, 0x5e, i_ff)            \
    145   V(F32Le, 0x5f, i_ff)            \
    146   V(F32Ge, 0x60, i_ff)            \
    147   V(F64Eq, 0x61, i_dd)            \
    148   V(F64Ne, 0x62, i_dd)            \
    149   V(F64Lt, 0x63, i_dd)            \
    150   V(F64Gt, 0x64, i_dd)            \
    151   V(F64Le, 0x65, i_dd)            \
    152   V(F64Ge, 0x66, i_dd)            \
    153   V(I32Clz, 0x67, i_i)            \
    154   V(I32Ctz, 0x68, i_i)            \
    155   V(I32Popcnt, 0x69, i_i)         \
    156   V(I32Add, 0x6a, i_ii)           \
    157   V(I32Sub, 0x6b, i_ii)           \
    158   V(I32Mul, 0x6c, i_ii)           \
    159   V(I32DivS, 0x6d, i_ii)          \
    160   V(I32DivU, 0x6e, i_ii)          \
    161   V(I32RemS, 0x6f, i_ii)          \
    162   V(I32RemU, 0x70, i_ii)          \
    163   V(I32And, 0x71, i_ii)           \
    164   V(I32Ior, 0x72, i_ii)           \
    165   V(I32Xor, 0x73, i_ii)           \
    166   V(I32Shl, 0x74, i_ii)           \
    167   V(I32ShrS, 0x75, i_ii)          \
    168   V(I32ShrU, 0x76, i_ii)          \
    169   V(I32Rol, 0x77, i_ii)           \
    170   V(I32Ror, 0x78, i_ii)           \
    171   V(I64Clz, 0x79, l_l)            \
    172   V(I64Ctz, 0x7a, l_l)            \
    173   V(I64Popcnt, 0x7b, l_l)         \
    174   V(I64Add, 0x7c, l_ll)           \
    175   V(I64Sub, 0x7d, l_ll)           \
    176   V(I64Mul, 0x7e, l_ll)           \
    177   V(I64DivS, 0x7f, l_ll)          \
    178   V(I64DivU, 0x80, l_ll)          \
    179   V(I64RemS, 0x81, l_ll)          \
    180   V(I64RemU, 0x82, l_ll)          \
    181   V(I64And, 0x83, l_ll)           \
    182   V(I64Ior, 0x84, l_ll)           \
    183   V(I64Xor, 0x85, l_ll)           \
    184   V(I64Shl, 0x86, l_ll)           \
    185   V(I64ShrS, 0x87, l_ll)          \
    186   V(I64ShrU, 0x88, l_ll)          \
    187   V(I64Rol, 0x89, l_ll)           \
    188   V(I64Ror, 0x8a, l_ll)           \
    189   V(F32Abs, 0x8b, f_f)            \
    190   V(F32Neg, 0x8c, f_f)            \
    191   V(F32Ceil, 0x8d, f_f)           \
    192   V(F32Floor, 0x8e, f_f)          \
    193   V(F32Trunc, 0x8f, f_f)          \
    194   V(F32NearestInt, 0x90, f_f)     \
    195   V(F32Sqrt, 0x91, f_f)           \
    196   V(F32Add, 0x92, f_ff)           \
    197   V(F32Sub, 0x93, f_ff)           \
    198   V(F32Mul, 0x94, f_ff)           \
    199   V(F32Div, 0x95, f_ff)           \
    200   V(F32Min, 0x96, f_ff)           \
    201   V(F32Max, 0x97, f_ff)           \
    202   V(F32CopySign, 0x98, f_ff)      \
    203   V(F64Abs, 0x99, d_d)            \
    204   V(F64Neg, 0x9a, d_d)            \
    205   V(F64Ceil, 0x9b, d_d)           \
    206   V(F64Floor, 0x9c, d_d)          \
    207   V(F64Trunc, 0x9d, d_d)          \
    208   V(F64NearestInt, 0x9e, d_d)     \
    209   V(F64Sqrt, 0x9f, d_d)           \
    210   V(F64Add, 0xa0, d_dd)           \
    211   V(F64Sub, 0xa1, d_dd)           \
    212   V(F64Mul, 0xa2, d_dd)           \
    213   V(F64Div, 0xa3, d_dd)           \
    214   V(F64Min, 0xa4, d_dd)           \
    215   V(F64Max, 0xa5, d_dd)           \
    216   V(F64CopySign, 0xa6, d_dd)      \
    217   V(I32ConvertI64, 0xa7, i_l)     \
    218   V(I32SConvertF32, 0xa8, i_f)    \
    219   V(I32UConvertF32, 0xa9, i_f)    \
    220   V(I32SConvertF64, 0xaa, i_d)    \
    221   V(I32UConvertF64, 0xab, i_d)    \
    222   V(I64SConvertI32, 0xac, l_i)    \
    223   V(I64UConvertI32, 0xad, l_i)    \
    224   V(I64SConvertF32, 0xae, l_f)    \
    225   V(I64UConvertF32, 0xaf, l_f)    \
    226   V(I64SConvertF64, 0xb0, l_d)    \
    227   V(I64UConvertF64, 0xb1, l_d)    \
    228   V(F32SConvertI32, 0xb2, f_i)    \
    229   V(F32UConvertI32, 0xb3, f_i)    \
    230   V(F32SConvertI64, 0xb4, f_l)    \
    231   V(F32UConvertI64, 0xb5, f_l)    \
    232   V(F32ConvertF64, 0xb6, f_d)     \
    233   V(F64SConvertI32, 0xb7, d_i)    \
    234   V(F64UConvertI32, 0xb8, d_i)    \
    235   V(F64SConvertI64, 0xb9, d_l)    \
    236   V(F64UConvertI64, 0xba, d_l)    \
    237   V(F64ConvertF32, 0xbb, d_f)     \
    238   V(I32ReinterpretF32, 0xbc, i_f) \
    239   V(I64ReinterpretF64, 0xbd, l_d) \
    240   V(F32ReinterpretI32, 0xbe, f_i) \
    241   V(F64ReinterpretI64, 0xbf, d_l)
    242 
    243 // For compatibility with Asm.js.
    244 #define FOREACH_ASMJS_COMPAT_OPCODE(V) \
    245   V(F64Acos, 0xc0, d_d)                \
    246   V(F64Asin, 0xc1, d_d)                \
    247   V(F64Atan, 0xc2, d_d)                \
    248   V(F64Cos, 0xc3, d_d)                 \
    249   V(F64Sin, 0xc4, d_d)                 \
    250   V(F64Tan, 0xc5, d_d)                 \
    251   V(F64Exp, 0xc6, d_d)                 \
    252   V(F64Log, 0xc7, d_d)                 \
    253   V(F64Atan2, 0xc8, d_dd)              \
    254   V(F64Pow, 0xc9, d_dd)                \
    255   V(F64Mod, 0xca, d_dd)                \
    256   V(I32AsmjsDivS, 0xd0, i_ii)          \
    257   V(I32AsmjsDivU, 0xd1, i_ii)          \
    258   V(I32AsmjsRemS, 0xd2, i_ii)          \
    259   V(I32AsmjsRemU, 0xd3, i_ii)          \
    260   V(I32AsmjsLoadMem8S, 0xd4, i_i)      \
    261   V(I32AsmjsLoadMem8U, 0xd5, i_i)      \
    262   V(I32AsmjsLoadMem16S, 0xd6, i_i)     \
    263   V(I32AsmjsLoadMem16U, 0xd7, i_i)     \
    264   V(I32AsmjsLoadMem, 0xd8, i_i)        \
    265   V(F32AsmjsLoadMem, 0xd9, f_i)        \
    266   V(F64AsmjsLoadMem, 0xda, d_i)        \
    267   V(I32AsmjsStoreMem8, 0xdb, i_ii)     \
    268   V(I32AsmjsStoreMem16, 0xdc, i_ii)    \
    269   V(I32AsmjsStoreMem, 0xdd, i_ii)      \
    270   V(F32AsmjsStoreMem, 0xde, f_if)      \
    271   V(F64AsmjsStoreMem, 0xdf, d_id)      \
    272   V(I32AsmjsSConvertF32, 0xe0, i_f)    \
    273   V(I32AsmjsUConvertF32, 0xe1, i_f)    \
    274   V(I32AsmjsSConvertF64, 0xe2, i_d)    \
    275   V(I32AsmjsUConvertF64, 0xe3, i_d)
    276 
    277 #define FOREACH_SIMD_0_OPERAND_OPCODE(V) \
    278   V(F32x4Splat, 0xe500, s_f)             \
    279   V(F32x4ReplaceLane, 0xe502, s_sif)     \
    280   V(F32x4Abs, 0xe503, s_s)               \
    281   V(F32x4Neg, 0xe504, s_s)               \
    282   V(F32x4Sqrt, 0xe505, s_s)              \
    283   V(F32x4RecipApprox, 0xe506, s_s)       \
    284   V(F32x4SqrtApprox, 0xe507, s_s)        \
    285   V(F32x4Add, 0xe508, s_ss)              \
    286   V(F32x4Sub, 0xe509, s_ss)              \
    287   V(F32x4Mul, 0xe50a, s_ss)              \
    288   V(F32x4Div, 0xe50b, s_ss)              \
    289   V(F32x4Min, 0xe50c, s_ss)              \
    290   V(F32x4Max, 0xe50d, s_ss)              \
    291   V(F32x4MinNum, 0xe50e, s_ss)           \
    292   V(F32x4MaxNum, 0xe50f, s_ss)           \
    293   V(F32x4Eq, 0xe510, s_ss)               \
    294   V(F32x4Ne, 0xe511, s_ss)               \
    295   V(F32x4Lt, 0xe512, s_ss)               \
    296   V(F32x4Le, 0xe513, s_ss)               \
    297   V(F32x4Gt, 0xe514, s_ss)               \
    298   V(F32x4Ge, 0xe515, s_ss)               \
    299   V(F32x4Select, 0xe516, s_sss)          \
    300   V(F32x4Swizzle, 0xe517, s_s)           \
    301   V(F32x4Shuffle, 0xe518, s_ss)          \
    302   V(F32x4FromInt32x4, 0xe519, s_s)       \
    303   V(F32x4FromUint32x4, 0xe51a, s_s)      \
    304   V(I32x4Splat, 0xe51b, s_i)             \
    305   V(I32x4ReplaceLane, 0xe51d, s_sii)     \
    306   V(I32x4Neg, 0xe51e, s_s)               \
    307   V(I32x4Add, 0xe51f, s_ss)              \
    308   V(I32x4Sub, 0xe520, s_ss)              \
    309   V(I32x4Mul, 0xe521, s_ss)              \
    310   V(I32x4Min_s, 0xe522, s_ss)            \
    311   V(I32x4Max_s, 0xe523, s_ss)            \
    312   V(I32x4Shl, 0xe524, s_si)              \
    313   V(I32x4Shr_s, 0xe525, s_si)            \
    314   V(I32x4Eq, 0xe526, s_ss)               \
    315   V(I32x4Ne, 0xe527, s_ss)               \
    316   V(I32x4Lt_s, 0xe528, s_ss)             \
    317   V(I32x4Le_s, 0xe529, s_ss)             \
    318   V(I32x4Gt_s, 0xe52a, s_ss)             \
    319   V(I32x4Ge_s, 0xe52b, s_ss)             \
    320   V(I32x4Select, 0xe52c, s_sss)          \
    321   V(I32x4Swizzle, 0xe52d, s_s)           \
    322   V(I32x4Shuffle, 0xe52e, s_ss)          \
    323   V(I32x4FromFloat32x4, 0xe52f, s_s)     \
    324   V(I32x4Min_u, 0xe530, s_ss)            \
    325   V(I32x4Max_u, 0xe531, s_ss)            \
    326   V(I32x4Shr_u, 0xe532, s_ss)            \
    327   V(I32x4Lt_u, 0xe533, s_ss)             \
    328   V(I32x4Le_u, 0xe534, s_ss)             \
    329   V(I32x4Gt_u, 0xe535, s_ss)             \
    330   V(I32x4Ge_u, 0xe536, s_ss)             \
    331   V(Ui32x4FromFloat32x4, 0xe537, s_s)    \
    332   V(I16x8Splat, 0xe538, s_i)             \
    333   V(I16x8ReplaceLane, 0xe53a, s_sii)     \
    334   V(I16x8Neg, 0xe53b, s_s)               \
    335   V(I16x8Add, 0xe53c, s_ss)              \
    336   V(I16x8AddSaturate_s, 0xe53d, s_ss)    \
    337   V(I16x8Sub, 0xe53e, s_ss)              \
    338   V(I16x8SubSaturate_s, 0xe53f, s_ss)    \
    339   V(I16x8Mul, 0xe540, s_ss)              \
    340   V(I16x8Min_s, 0xe541, s_ss)            \
    341   V(I16x8Max_s, 0xe542, s_ss)            \
    342   V(I16x8Shl, 0xe543, s_si)              \
    343   V(I16x8Shr_s, 0xe544, s_si)            \
    344   V(I16x8Eq, 0xe545, s_ss)               \
    345   V(I16x8Ne, 0xe546, s_ss)               \
    346   V(I16x8Lt_s, 0xe547, s_ss)             \
    347   V(I16x8Le_s, 0xe548, s_ss)             \
    348   V(I16x8Gt_s, 0xe549, s_ss)             \
    349   V(I16x8Ge_s, 0xe54a, s_ss)             \
    350   V(I16x8Select, 0xe54b, s_sss)          \
    351   V(I16x8Swizzle, 0xe54c, s_s)           \
    352   V(I16x8Shuffle, 0xe54d, s_ss)          \
    353   V(I16x8AddSaturate_u, 0xe54e, s_ss)    \
    354   V(I16x8SubSaturate_u, 0xe54f, s_ss)    \
    355   V(I16x8Min_u, 0xe550, s_ss)            \
    356   V(I16x8Max_u, 0xe551, s_ss)            \
    357   V(I16x8Shr_u, 0xe552, s_si)            \
    358   V(I16x8Lt_u, 0xe553, s_ss)             \
    359   V(I16x8Le_u, 0xe554, s_ss)             \
    360   V(I16x8Gt_u, 0xe555, s_ss)             \
    361   V(I16x8Ge_u, 0xe556, s_ss)             \
    362   V(I8x16Splat, 0xe557, s_i)             \
    363   V(I8x16ReplaceLane, 0xe559, s_sii)     \
    364   V(I8x16Neg, 0xe55a, s_s)               \
    365   V(I8x16Add, 0xe55b, s_ss)              \
    366   V(I8x16AddSaturate_s, 0xe55c, s_ss)    \
    367   V(I8x16Sub, 0xe55d, s_ss)              \
    368   V(I8x16SubSaturate_s, 0xe55e, s_ss)    \
    369   V(I8x16Mul, 0xe55f, s_ss)              \
    370   V(I8x16Min_s, 0xe560, s_ss)            \
    371   V(I8x16Max_s, 0xe561, s_ss)            \
    372   V(I8x16Shl, 0xe562, s_si)              \
    373   V(I8x16Shr_s, 0xe563, s_si)            \
    374   V(I8x16Eq, 0xe564, s_ss)               \
    375   V(I8x16Neq, 0xe565, s_ss)              \
    376   V(I8x16Lt_s, 0xe566, s_ss)             \
    377   V(I8x16Le_s, 0xe567, s_ss)             \
    378   V(I8x16Gt_s, 0xe568, s_ss)             \
    379   V(I8x16Ge_s, 0xe569, s_ss)             \
    380   V(I8x16Select, 0xe56a, s_sss)          \
    381   V(I8x16Swizzle, 0xe56b, s_s)           \
    382   V(I8x16Shuffle, 0xe56c, s_ss)          \
    383   V(I8x16AddSaturate_u, 0xe56d, s_ss)    \
    384   V(I8x16Sub_saturate_u, 0xe56e, s_ss)   \
    385   V(I8x16Min_u, 0xe56f, s_ss)            \
    386   V(I8x16Max_u, 0xe570, s_ss)            \
    387   V(I8x16Shr_u, 0xe571, s_ss)            \
    388   V(I8x16Lt_u, 0xe572, s_ss)             \
    389   V(I8x16Le_u, 0xe573, s_ss)             \
    390   V(I8x16Gt_u, 0xe574, s_ss)             \
    391   V(I8x16Ge_u, 0xe575, s_ss)             \
    392   V(S128And, 0xe576, s_ss)               \
    393   V(S128Ior, 0xe577, s_ss)               \
    394   V(S128Xor, 0xe578, s_ss)               \
    395   V(S128Not, 0xe579, s_s)
    396 
    397 #define FOREACH_SIMD_1_OPERAND_OPCODE(V) \
    398   V(F32x4ExtractLane, 0xe501, _)         \
    399   V(I32x4ExtractLane, 0xe51c, _)         \
    400   V(I16x8ExtractLane, 0xe539, _)         \
    401   V(I8x16ExtractLane, 0xe558, _)
    402 
    403 #define FOREACH_ATOMIC_OPCODE(V)               \
    404   V(I32AtomicAdd8S, 0xe601, i_ii)              \
    405   V(I32AtomicAdd8U, 0xe602, i_ii)              \
    406   V(I32AtomicAdd16S, 0xe603, i_ii)             \
    407   V(I32AtomicAdd16U, 0xe604, i_ii)             \
    408   V(I32AtomicAdd32, 0xe605, i_ii)              \
    409   V(I32AtomicAnd8S, 0xe606, i_ii)              \
    410   V(I32AtomicAnd8U, 0xe607, i_ii)              \
    411   V(I32AtomicAnd16S, 0xe608, i_ii)             \
    412   V(I32AtomicAnd16U, 0xe609, i_ii)             \
    413   V(I32AtomicAnd32, 0xe60a, i_ii)              \
    414   V(I32AtomicCompareExchange8S, 0xe60b, i_ii)  \
    415   V(I32AtomicCompareExchange8U, 0xe60c, i_ii)  \
    416   V(I32AtomicCompareExchange16S, 0xe60d, i_ii) \
    417   V(I32AtomicCompareExchange16U, 0xe60e, i_ii) \
    418   V(I32AtomicCompareExchange32, 0xe60f, i_ii)  \
    419   V(I32AtomicExchange8S, 0xe610, i_ii)         \
    420   V(I32AtomicExchange8U, 0xe611, i_ii)         \
    421   V(I32AtomicExchange16S, 0xe612, i_ii)        \
    422   V(I32AtomicExchange16U, 0xe613, i_ii)        \
    423   V(I32AtomicExchange32, 0xe614, i_ii)         \
    424   V(I32AtomicOr8S, 0xe615, i_ii)               \
    425   V(I32AtomicOr8U, 0xe616, i_ii)               \
    426   V(I32AtomicOr16S, 0xe617, i_ii)              \
    427   V(I32AtomicOr16U, 0xe618, i_ii)              \
    428   V(I32AtomicOr32, 0xe619, i_ii)               \
    429   V(I32AtomicSub8S, 0xe61a, i_ii)              \
    430   V(I32AtomicSub8U, 0xe61b, i_ii)              \
    431   V(I32AtomicSub16S, 0xe61c, i_ii)             \
    432   V(I32AtomicSub16U, 0xe61d, i_ii)             \
    433   V(I32AtomicSub32, 0xe61e, i_ii)              \
    434   V(I32AtomicXor8S, 0xe61f, i_ii)              \
    435   V(I32AtomicXor8U, 0xe620, i_ii)              \
    436   V(I32AtomicXor16S, 0xe621, i_ii)             \
    437   V(I32AtomicXor16U, 0xe622, i_ii)             \
    438   V(I32AtomicXor32, 0xe623, i_ii)
    439 
    440 // All opcodes.
    441 #define FOREACH_OPCODE(V)          \
    442   FOREACH_CONTROL_OPCODE(V)        \
    443   FOREACH_MISC_OPCODE(V)           \
    444   FOREACH_SIMPLE_OPCODE(V)         \
    445   FOREACH_STORE_MEM_OPCODE(V)      \
    446   FOREACH_LOAD_MEM_OPCODE(V)       \
    447   FOREACH_MISC_MEM_OPCODE(V)       \
    448   FOREACH_ASMJS_COMPAT_OPCODE(V)   \
    449   FOREACH_SIMD_0_OPERAND_OPCODE(V) \
    450   FOREACH_SIMD_1_OPERAND_OPCODE(V) \
    451   FOREACH_ATOMIC_OPCODE(V)
    452 
    453 // All signatures.
    454 #define FOREACH_SIGNATURE(V)         \
    455   FOREACH_SIMD_SIGNATURE(V)          \
    456   V(i_ii, kAstI32, kAstI32, kAstI32) \
    457   V(i_i, kAstI32, kAstI32)           \
    458   V(i_v, kAstI32)                    \
    459   V(i_ff, kAstI32, kAstF32, kAstF32) \
    460   V(i_f, kAstI32, kAstF32)           \
    461   V(i_dd, kAstI32, kAstF64, kAstF64) \
    462   V(i_d, kAstI32, kAstF64)           \
    463   V(i_l, kAstI32, kAstI64)           \
    464   V(l_ll, kAstI64, kAstI64, kAstI64) \
    465   V(i_ll, kAstI32, kAstI64, kAstI64) \
    466   V(l_l, kAstI64, kAstI64)           \
    467   V(l_i, kAstI64, kAstI32)           \
    468   V(l_f, kAstI64, kAstF32)           \
    469   V(l_d, kAstI64, kAstF64)           \
    470   V(f_ff, kAstF32, kAstF32, kAstF32) \
    471   V(f_f, kAstF32, kAstF32)           \
    472   V(f_d, kAstF32, kAstF64)           \
    473   V(f_i, kAstF32, kAstI32)           \
    474   V(f_l, kAstF32, kAstI64)           \
    475   V(d_dd, kAstF64, kAstF64, kAstF64) \
    476   V(d_d, kAstF64, kAstF64)           \
    477   V(d_f, kAstF64, kAstF32)           \
    478   V(d_i, kAstF64, kAstI32)           \
    479   V(d_l, kAstF64, kAstI64)           \
    480   V(d_id, kAstF64, kAstI32, kAstF64) \
    481   V(f_if, kAstF32, kAstI32, kAstF32) \
    482   V(l_il, kAstI64, kAstI32, kAstI64)
    483 
    484 #define FOREACH_SIMD_SIGNATURE(V)                  \
    485   V(s_s, kAstS128, kAstS128)                       \
    486   V(s_f, kAstS128, kAstF32)                        \
    487   V(s_sif, kAstS128, kAstS128, kAstI32, kAstF32)   \
    488   V(s_ss, kAstS128, kAstS128, kAstS128)            \
    489   V(s_sss, kAstS128, kAstS128, kAstS128, kAstS128) \
    490   V(s_i, kAstS128, kAstI32)                        \
    491   V(s_sii, kAstS128, kAstS128, kAstI32, kAstI32)   \
    492   V(s_si, kAstS128, kAstS128, kAstI32)
    493 
    494 #define FOREACH_PREFIX(V) \
    495   V(Simd, 0xe5)           \
    496   V(Atomic, 0xe6)
    497 
    498 enum WasmOpcode {
    499 // Declare expression opcodes.
    500 #define DECLARE_NAMED_ENUM(name, opcode, sig) kExpr##name = opcode,
    501   FOREACH_OPCODE(DECLARE_NAMED_ENUM)
    502 #undef DECLARE_NAMED_ENUM
    503 #define DECLARE_PREFIX(name, opcode) k##name##Prefix = opcode,
    504       FOREACH_PREFIX(DECLARE_PREFIX)
    505 #undef DECLARE_PREFIX
    506 };
    507 
    508 // The reason for a trap.
    509 #define FOREACH_WASM_TRAPREASON(V) \
    510   V(TrapUnreachable)               \
    511   V(TrapMemOutOfBounds)            \
    512   V(TrapDivByZero)                 \
    513   V(TrapDivUnrepresentable)        \
    514   V(TrapRemByZero)                 \
    515   V(TrapFloatUnrepresentable)      \
    516   V(TrapFuncInvalid)               \
    517   V(TrapFuncSigMismatch)           \
    518   V(TrapInvalidIndex)
    519 
    520 enum TrapReason {
    521 #define DECLARE_ENUM(name) k##name,
    522   FOREACH_WASM_TRAPREASON(DECLARE_ENUM)
    523   kTrapCount
    524 #undef DECLARE_ENUM
    525 };
    526 
    527 // A collection of opcode-related static methods.
    528 class V8_EXPORT_PRIVATE WasmOpcodes {
    529  public:
    530   static const char* OpcodeName(WasmOpcode opcode);
    531   static const char* ShortOpcodeName(WasmOpcode opcode);
    532   static FunctionSig* Signature(WasmOpcode opcode);
    533   static FunctionSig* AsmjsSignature(WasmOpcode opcode);
    534   static FunctionSig* AtomicSignature(WasmOpcode opcode);
    535   static bool IsPrefixOpcode(WasmOpcode opcode);
    536 
    537   static int TrapReasonToMessageId(TrapReason reason);
    538   static const char* TrapReasonMessage(TrapReason reason);
    539 
    540   static byte MemSize(MachineType type) {
    541     return 1 << ElementSizeLog2Of(type.representation());
    542   }
    543 
    544   static byte MemSize(LocalType type) { return 1 << ElementSizeLog2Of(type); }
    545 
    546   static LocalTypeCode LocalTypeCodeFor(LocalType type) {
    547     switch (type) {
    548       case kAstI32:
    549         return kLocalI32;
    550       case kAstI64:
    551         return kLocalI64;
    552       case kAstF32:
    553         return kLocalF32;
    554       case kAstF64:
    555         return kLocalF64;
    556       case kAstS128:
    557         return kLocalS128;
    558       case kAstStmt:
    559         return kLocalVoid;
    560       default:
    561         UNREACHABLE();
    562         return kLocalVoid;
    563     }
    564   }
    565 
    566   static MachineType MachineTypeFor(LocalType type) {
    567     switch (type) {
    568       case kAstI32:
    569         return MachineType::Int32();
    570       case kAstI64:
    571         return MachineType::Int64();
    572       case kAstF32:
    573         return MachineType::Float32();
    574       case kAstF64:
    575         return MachineType::Float64();
    576       case kAstS128:
    577         return MachineType::Simd128();
    578       case kAstStmt:
    579         return MachineType::None();
    580       default:
    581         UNREACHABLE();
    582         return MachineType::None();
    583     }
    584   }
    585 
    586   static LocalType LocalTypeFor(MachineType type) {
    587     if (type == MachineType::Int8()) {
    588       return kAstI32;
    589     } else if (type == MachineType::Uint8()) {
    590       return kAstI32;
    591     } else if (type == MachineType::Int16()) {
    592       return kAstI32;
    593     } else if (type == MachineType::Uint16()) {
    594       return kAstI32;
    595     } else if (type == MachineType::Int32()) {
    596       return kAstI32;
    597     } else if (type == MachineType::Uint32()) {
    598       return kAstI32;
    599     } else if (type == MachineType::Int64()) {
    600       return kAstI64;
    601     } else if (type == MachineType::Uint64()) {
    602       return kAstI64;
    603     } else if (type == MachineType::Float32()) {
    604       return kAstF32;
    605     } else if (type == MachineType::Float64()) {
    606       return kAstF64;
    607     } else if (type == MachineType::Simd128()) {
    608       return kAstS128;
    609     } else {
    610       UNREACHABLE();
    611       return kAstI32;
    612     }
    613   }
    614 
    615   static WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
    616     if (type == MachineType::Int8()) {
    617       return store ? kExprI32StoreMem8 : kExprI32LoadMem8S;
    618     } else if (type == MachineType::Uint8()) {
    619       return store ? kExprI32StoreMem8 : kExprI32LoadMem8U;
    620     } else if (type == MachineType::Int16()) {
    621       return store ? kExprI32StoreMem16 : kExprI32LoadMem16S;
    622     } else if (type == MachineType::Uint16()) {
    623       return store ? kExprI32StoreMem16 : kExprI32LoadMem16U;
    624     } else if (type == MachineType::Int32()) {
    625       return store ? kExprI32StoreMem : kExprI32LoadMem;
    626     } else if (type == MachineType::Uint32()) {
    627       return store ? kExprI32StoreMem : kExprI32LoadMem;
    628     } else if (type == MachineType::Int64()) {
    629       return store ? kExprI64StoreMem : kExprI64LoadMem;
    630     } else if (type == MachineType::Uint64()) {
    631       return store ? kExprI64StoreMem : kExprI64LoadMem;
    632     } else if (type == MachineType::Float32()) {
    633       return store ? kExprF32StoreMem : kExprF32LoadMem;
    634     } else if (type == MachineType::Float64()) {
    635       return store ? kExprF64StoreMem : kExprF64LoadMem;
    636     } else {
    637       UNREACHABLE();
    638       return kExprNop;
    639     }
    640   }
    641 
    642   static char ShortNameOf(LocalType type) {
    643     switch (type) {
    644       case kAstI32:
    645         return 'i';
    646       case kAstI64:
    647         return 'l';
    648       case kAstF32:
    649         return 'f';
    650       case kAstF64:
    651         return 'd';
    652       case kAstS128:
    653         return 's';
    654       case kAstStmt:
    655         return 'v';
    656       case kAstEnd:
    657         return 'x';
    658       default:
    659         UNREACHABLE();
    660         return '?';
    661     }
    662   }
    663 
    664   static const char* TypeName(LocalType type) {
    665     switch (type) {
    666       case kAstI32:
    667         return "i32";
    668       case kAstI64:
    669         return "i64";
    670       case kAstF32:
    671         return "f32";
    672       case kAstF64:
    673         return "f64";
    674       case kAstS128:
    675         return "s128";
    676       case kAstStmt:
    677         return "<stmt>";
    678       case kAstEnd:
    679         return "<end>";
    680       default:
    681         return "<unknown>";
    682     }
    683   }
    684 };
    685 }  // namespace wasm
    686 }  // namespace internal
    687 }  // namespace v8
    688 
    689 #endif  // V8_WASM_OPCODES_H_
    690