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