Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | }; |
| 23 | |
| 24 | // Binary encoding of memory types. |
| 25 | enum MemTypeCode { |
| 26 | kMemI8 = 0, |
| 27 | kMemU8 = 1, |
| 28 | kMemI16 = 2, |
| 29 | kMemU16 = 3, |
| 30 | kMemI32 = 4, |
| 31 | kMemU32 = 5, |
| 32 | kMemI64 = 6, |
| 33 | kMemU64 = 7, |
| 34 | kMemF32 = 8, |
| 35 | kMemF64 = 9 |
| 36 | }; |
| 37 | |
| 38 | // We reuse the internal machine type to represent WebAssembly AST types. |
| 39 | // A typedef improves readability without adding a whole new type system. |
| 40 | typedef MachineRepresentation LocalType; |
| 41 | const LocalType kAstStmt = MachineRepresentation::kNone; |
| 42 | const LocalType kAstI32 = MachineRepresentation::kWord32; |
| 43 | const LocalType kAstI64 = MachineRepresentation::kWord64; |
| 44 | const LocalType kAstF32 = MachineRepresentation::kFloat32; |
| 45 | const LocalType kAstF64 = MachineRepresentation::kFloat64; |
| 46 | // We use kTagged here because kNone is already used by kAstStmt. |
| 47 | const LocalType kAstEnd = MachineRepresentation::kTagged; |
| 48 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | typedef Signature<LocalType> FunctionSig; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 50 | std::ostream& operator<<(std::ostream& os, const FunctionSig& function); |
| 51 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 52 | struct WasmName { |
| 53 | const char* name; |
| 54 | uint32_t length; |
| 55 | }; |
| 56 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 57 | // TODO(titzer): Renumber all the opcodes to fill in holes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | |
| 59 | // Control expressions and blocks. |
| 60 | #define FOREACH_CONTROL_OPCODE(V) \ |
| 61 | V(Nop, 0x00, _) \ |
| 62 | V(Block, 0x01, _) \ |
| 63 | V(Loop, 0x02, _) \ |
| 64 | V(If, 0x03, _) \ |
| 65 | V(IfElse, 0x04, _) \ |
| 66 | V(Select, 0x05, _) \ |
| 67 | V(Br, 0x06, _) \ |
| 68 | V(BrIf, 0x07, _) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 69 | V(BrTable, 0x08, _) \ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | V(Return, 0x14, _) \ |
| 71 | V(Unreachable, 0x15, _) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | |
| 73 | // Constants, locals, globals, and calls. |
| 74 | #define FOREACH_MISC_OPCODE(V) \ |
| 75 | V(I8Const, 0x09, _) \ |
| 76 | V(I32Const, 0x0a, _) \ |
| 77 | V(I64Const, 0x0b, _) \ |
| 78 | V(F64Const, 0x0c, _) \ |
| 79 | V(F32Const, 0x0d, _) \ |
| 80 | V(GetLocal, 0x0e, _) \ |
| 81 | V(SetLocal, 0x0f, _) \ |
| 82 | V(LoadGlobal, 0x10, _) \ |
| 83 | V(StoreGlobal, 0x11, _) \ |
| 84 | V(CallFunction, 0x12, _) \ |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 85 | V(CallIndirect, 0x13, _) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 86 | V(CallImport, 0x1F, _) \ |
| 87 | V(DeclLocals, 0x1E, _) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | |
| 89 | // Load memory expressions. |
| 90 | #define FOREACH_LOAD_MEM_OPCODE(V) \ |
| 91 | V(I32LoadMem8S, 0x20, i_i) \ |
| 92 | V(I32LoadMem8U, 0x21, i_i) \ |
| 93 | V(I32LoadMem16S, 0x22, i_i) \ |
| 94 | V(I32LoadMem16U, 0x23, i_i) \ |
| 95 | V(I64LoadMem8S, 0x24, l_i) \ |
| 96 | V(I64LoadMem8U, 0x25, l_i) \ |
| 97 | V(I64LoadMem16S, 0x26, l_i) \ |
| 98 | V(I64LoadMem16U, 0x27, l_i) \ |
| 99 | V(I64LoadMem32S, 0x28, l_i) \ |
| 100 | V(I64LoadMem32U, 0x29, l_i) \ |
| 101 | V(I32LoadMem, 0x2a, i_i) \ |
| 102 | V(I64LoadMem, 0x2b, l_i) \ |
| 103 | V(F32LoadMem, 0x2c, f_i) \ |
| 104 | V(F64LoadMem, 0x2d, d_i) |
| 105 | |
| 106 | // Store memory expressions. |
| 107 | #define FOREACH_STORE_MEM_OPCODE(V) \ |
| 108 | V(I32StoreMem8, 0x2e, i_ii) \ |
| 109 | V(I32StoreMem16, 0x2f, i_ii) \ |
| 110 | V(I64StoreMem8, 0x30, l_il) \ |
| 111 | V(I64StoreMem16, 0x31, l_il) \ |
| 112 | V(I64StoreMem32, 0x32, l_il) \ |
| 113 | V(I32StoreMem, 0x33, i_ii) \ |
| 114 | V(I64StoreMem, 0x34, l_il) \ |
| 115 | V(F32StoreMem, 0x35, f_if) \ |
| 116 | V(F64StoreMem, 0x36, d_id) |
| 117 | |
| 118 | // Load memory expressions. |
| 119 | #define FOREACH_MISC_MEM_OPCODE(V) \ |
| 120 | V(MemorySize, 0x3b, i_v) \ |
| 121 | V(GrowMemory, 0x39, i_i) |
| 122 | |
| 123 | // Expressions with signatures. |
| 124 | #define FOREACH_SIMPLE_OPCODE(V) \ |
| 125 | V(I32Add, 0x40, i_ii) \ |
| 126 | V(I32Sub, 0x41, i_ii) \ |
| 127 | V(I32Mul, 0x42, i_ii) \ |
| 128 | V(I32DivS, 0x43, i_ii) \ |
| 129 | V(I32DivU, 0x44, i_ii) \ |
| 130 | V(I32RemS, 0x45, i_ii) \ |
| 131 | V(I32RemU, 0x46, i_ii) \ |
| 132 | V(I32And, 0x47, i_ii) \ |
| 133 | V(I32Ior, 0x48, i_ii) \ |
| 134 | V(I32Xor, 0x49, i_ii) \ |
| 135 | V(I32Shl, 0x4a, i_ii) \ |
| 136 | V(I32ShrU, 0x4b, i_ii) \ |
| 137 | V(I32ShrS, 0x4c, i_ii) \ |
| 138 | V(I32Eq, 0x4d, i_ii) \ |
| 139 | V(I32Ne, 0x4e, i_ii) \ |
| 140 | V(I32LtS, 0x4f, i_ii) \ |
| 141 | V(I32LeS, 0x50, i_ii) \ |
| 142 | V(I32LtU, 0x51, i_ii) \ |
| 143 | V(I32LeU, 0x52, i_ii) \ |
| 144 | V(I32GtS, 0x53, i_ii) \ |
| 145 | V(I32GeS, 0x54, i_ii) \ |
| 146 | V(I32GtU, 0x55, i_ii) \ |
| 147 | V(I32GeU, 0x56, i_ii) \ |
| 148 | V(I32Clz, 0x57, i_i) \ |
| 149 | V(I32Ctz, 0x58, i_i) \ |
| 150 | V(I32Popcnt, 0x59, i_i) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 151 | V(I32Eqz, 0x5a, i_i) \ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | V(I64Add, 0x5b, l_ll) \ |
| 153 | V(I64Sub, 0x5c, l_ll) \ |
| 154 | V(I64Mul, 0x5d, l_ll) \ |
| 155 | V(I64DivS, 0x5e, l_ll) \ |
| 156 | V(I64DivU, 0x5f, l_ll) \ |
| 157 | V(I64RemS, 0x60, l_ll) \ |
| 158 | V(I64RemU, 0x61, l_ll) \ |
| 159 | V(I64And, 0x62, l_ll) \ |
| 160 | V(I64Ior, 0x63, l_ll) \ |
| 161 | V(I64Xor, 0x64, l_ll) \ |
| 162 | V(I64Shl, 0x65, l_ll) \ |
| 163 | V(I64ShrU, 0x66, l_ll) \ |
| 164 | V(I64ShrS, 0x67, l_ll) \ |
| 165 | V(I64Eq, 0x68, i_ll) \ |
| 166 | V(I64Ne, 0x69, i_ll) \ |
| 167 | V(I64LtS, 0x6a, i_ll) \ |
| 168 | V(I64LeS, 0x6b, i_ll) \ |
| 169 | V(I64LtU, 0x6c, i_ll) \ |
| 170 | V(I64LeU, 0x6d, i_ll) \ |
| 171 | V(I64GtS, 0x6e, i_ll) \ |
| 172 | V(I64GeS, 0x6f, i_ll) \ |
| 173 | V(I64GtU, 0x70, i_ll) \ |
| 174 | V(I64GeU, 0x71, i_ll) \ |
| 175 | V(I64Clz, 0x72, l_l) \ |
| 176 | V(I64Ctz, 0x73, l_l) \ |
| 177 | V(I64Popcnt, 0x74, l_l) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 178 | V(I64Eqz, 0xba, i_l) \ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 179 | V(F32Add, 0x75, f_ff) \ |
| 180 | V(F32Sub, 0x76, f_ff) \ |
| 181 | V(F32Mul, 0x77, f_ff) \ |
| 182 | V(F32Div, 0x78, f_ff) \ |
| 183 | V(F32Min, 0x79, f_ff) \ |
| 184 | V(F32Max, 0x7a, f_ff) \ |
| 185 | V(F32Abs, 0x7b, f_f) \ |
| 186 | V(F32Neg, 0x7c, f_f) \ |
| 187 | V(F32CopySign, 0x7d, f_ff) \ |
| 188 | V(F32Ceil, 0x7e, f_f) \ |
| 189 | V(F32Floor, 0x7f, f_f) \ |
| 190 | V(F32Trunc, 0x80, f_f) \ |
| 191 | V(F32NearestInt, 0x81, f_f) \ |
| 192 | V(F32Sqrt, 0x82, f_f) \ |
| 193 | V(F32Eq, 0x83, i_ff) \ |
| 194 | V(F32Ne, 0x84, i_ff) \ |
| 195 | V(F32Lt, 0x85, i_ff) \ |
| 196 | V(F32Le, 0x86, i_ff) \ |
| 197 | V(F32Gt, 0x87, i_ff) \ |
| 198 | V(F32Ge, 0x88, i_ff) \ |
| 199 | V(F64Add, 0x89, d_dd) \ |
| 200 | V(F64Sub, 0x8a, d_dd) \ |
| 201 | V(F64Mul, 0x8b, d_dd) \ |
| 202 | V(F64Div, 0x8c, d_dd) \ |
| 203 | V(F64Min, 0x8d, d_dd) \ |
| 204 | V(F64Max, 0x8e, d_dd) \ |
| 205 | V(F64Abs, 0x8f, d_d) \ |
| 206 | V(F64Neg, 0x90, d_d) \ |
| 207 | V(F64CopySign, 0x91, d_dd) \ |
| 208 | V(F64Ceil, 0x92, d_d) \ |
| 209 | V(F64Floor, 0x93, d_d) \ |
| 210 | V(F64Trunc, 0x94, d_d) \ |
| 211 | V(F64NearestInt, 0x95, d_d) \ |
| 212 | V(F64Sqrt, 0x96, d_d) \ |
| 213 | V(F64Eq, 0x97, i_dd) \ |
| 214 | V(F64Ne, 0x98, i_dd) \ |
| 215 | V(F64Lt, 0x99, i_dd) \ |
| 216 | V(F64Le, 0x9a, i_dd) \ |
| 217 | V(F64Gt, 0x9b, i_dd) \ |
| 218 | V(F64Ge, 0x9c, i_dd) \ |
| 219 | V(I32SConvertF32, 0x9d, i_f) \ |
| 220 | V(I32SConvertF64, 0x9e, i_d) \ |
| 221 | V(I32UConvertF32, 0x9f, i_f) \ |
| 222 | V(I32UConvertF64, 0xa0, i_d) \ |
| 223 | V(I32ConvertI64, 0xa1, i_l) \ |
| 224 | V(I64SConvertF32, 0xa2, l_f) \ |
| 225 | V(I64SConvertF64, 0xa3, l_d) \ |
| 226 | V(I64UConvertF32, 0xa4, l_f) \ |
| 227 | V(I64UConvertF64, 0xa5, l_d) \ |
| 228 | V(I64SConvertI32, 0xa6, l_i) \ |
| 229 | V(I64UConvertI32, 0xa7, l_i) \ |
| 230 | V(F32SConvertI32, 0xa8, f_i) \ |
| 231 | V(F32UConvertI32, 0xa9, f_i) \ |
| 232 | V(F32SConvertI64, 0xaa, f_l) \ |
| 233 | V(F32UConvertI64, 0xab, f_l) \ |
| 234 | V(F32ConvertF64, 0xac, f_d) \ |
| 235 | V(F32ReinterpretI32, 0xad, f_i) \ |
| 236 | V(F64SConvertI32, 0xae, d_i) \ |
| 237 | V(F64UConvertI32, 0xaf, d_i) \ |
| 238 | V(F64SConvertI64, 0xb0, d_l) \ |
| 239 | V(F64UConvertI64, 0xb1, d_l) \ |
| 240 | V(F64ConvertF32, 0xb2, d_f) \ |
| 241 | V(F64ReinterpretI64, 0xb3, d_l) \ |
| 242 | V(I32ReinterpretF32, 0xb4, i_f) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 243 | V(I64ReinterpretF64, 0xb5, l_d) \ |
| 244 | V(I32Ror, 0xb6, i_ii) \ |
| 245 | V(I32Rol, 0xb7, i_ii) \ |
| 246 | V(I64Ror, 0xb8, l_ll) \ |
| 247 | V(I64Rol, 0xb9, l_ll) |
| 248 | |
| 249 | // For compatibility with Asm.js. |
| 250 | #define FOREACH_ASMJS_COMPAT_OPCODE(V) \ |
| 251 | V(F64Acos, 0xc0, d_d) \ |
| 252 | V(F64Asin, 0xc1, d_d) \ |
| 253 | V(F64Atan, 0xc2, d_d) \ |
| 254 | V(F64Cos, 0xc3, d_d) \ |
| 255 | V(F64Sin, 0xc4, d_d) \ |
| 256 | V(F64Tan, 0xc5, d_d) \ |
| 257 | V(F64Exp, 0xc6, d_d) \ |
| 258 | V(F64Log, 0xc7, d_d) \ |
| 259 | V(F64Atan2, 0xc8, d_dd) \ |
| 260 | V(F64Pow, 0xc9, d_dd) \ |
| 261 | V(F64Mod, 0xca, d_dd) |
| 262 | |
| 263 | // TODO(titzer): sketch of asm-js compatibility bytecodes |
| 264 | /* V(I32AsmjsDivS, 0xd0, i_ii) \ */ |
| 265 | /* V(I32AsmjsDivU, 0xd1, i_ii) \ */ |
| 266 | /* V(I32AsmjsRemS, 0xd2, i_ii) \ */ |
| 267 | /* V(I32AsmjsRemU, 0xd3, i_ii) \ */ |
| 268 | /* V(I32AsmjsLoad8S, 0xd4, i_i) \ */ |
| 269 | /* V(I32AsmjsLoad8U, 0xd5, i_i) \ */ |
| 270 | /* V(I32AsmjsLoad16S, 0xd6, i_i) \ */ |
| 271 | /* V(I32AsmjsLoad16U, 0xd7, i_i) \ */ |
| 272 | /* V(I32AsmjsLoad, 0xd8, i_i) \ */ |
| 273 | /* V(F32AsmjsLoad, 0xd9, f_i) \ */ |
| 274 | /* V(F64AsmjsLoad, 0xda, d_i) \ */ |
| 275 | /* V(I32AsmjsStore8, 0xdb, i_i) \ */ |
| 276 | /* V(I32AsmjsStore16, 0xdc, i_i) \ */ |
| 277 | /* V(I32AsmjsStore, 0xdd, i_ii) \ */ |
| 278 | /* V(F32AsmjsStore, 0xde, i_if) \ */ |
| 279 | /* V(F64AsmjsStore, 0xdf, i_id) \ */ |
| 280 | /* V(I32SAsmjsConvertF32, 0xe0, i_f) \ */ |
| 281 | /* V(I32UAsmjsConvertF32, 0xe1, i_f) \ */ |
| 282 | /* V(I32SAsmjsConvertF64, 0xe2, i_d) \ */ |
| 283 | /* V(I32SAsmjsConvertF64, 0xe3, i_d) */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 284 | |
| 285 | // All opcodes. |
| 286 | #define FOREACH_OPCODE(V) \ |
| 287 | FOREACH_CONTROL_OPCODE(V) \ |
| 288 | FOREACH_MISC_OPCODE(V) \ |
| 289 | FOREACH_SIMPLE_OPCODE(V) \ |
| 290 | FOREACH_STORE_MEM_OPCODE(V) \ |
| 291 | FOREACH_LOAD_MEM_OPCODE(V) \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 292 | FOREACH_MISC_MEM_OPCODE(V) \ |
| 293 | FOREACH_ASMJS_COMPAT_OPCODE(V) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 294 | |
| 295 | // All signatures. |
| 296 | #define FOREACH_SIGNATURE(V) \ |
| 297 | V(i_ii, kAstI32, kAstI32, kAstI32) \ |
| 298 | V(i_i, kAstI32, kAstI32) \ |
| 299 | V(i_v, kAstI32) \ |
| 300 | V(i_ff, kAstI32, kAstF32, kAstF32) \ |
| 301 | V(i_f, kAstI32, kAstF32) \ |
| 302 | V(i_dd, kAstI32, kAstF64, kAstF64) \ |
| 303 | V(i_d, kAstI32, kAstF64) \ |
| 304 | V(i_l, kAstI32, kAstI64) \ |
| 305 | V(l_ll, kAstI64, kAstI64, kAstI64) \ |
| 306 | V(i_ll, kAstI32, kAstI64, kAstI64) \ |
| 307 | V(l_l, kAstI64, kAstI64) \ |
| 308 | V(l_i, kAstI64, kAstI32) \ |
| 309 | V(l_f, kAstI64, kAstF32) \ |
| 310 | V(l_d, kAstI64, kAstF64) \ |
| 311 | V(f_ff, kAstF32, kAstF32, kAstF32) \ |
| 312 | V(f_f, kAstF32, kAstF32) \ |
| 313 | V(f_d, kAstF32, kAstF64) \ |
| 314 | V(f_i, kAstF32, kAstI32) \ |
| 315 | V(f_l, kAstF32, kAstI64) \ |
| 316 | V(d_dd, kAstF64, kAstF64, kAstF64) \ |
| 317 | V(d_d, kAstF64, kAstF64) \ |
| 318 | V(d_f, kAstF64, kAstF32) \ |
| 319 | V(d_i, kAstF64, kAstI32) \ |
| 320 | V(d_l, kAstF64, kAstI64) \ |
| 321 | V(d_id, kAstF64, kAstI32, kAstF64) \ |
| 322 | V(f_if, kAstF32, kAstI32, kAstF32) \ |
| 323 | V(l_il, kAstI64, kAstI32, kAstI64) |
| 324 | |
| 325 | enum WasmOpcode { |
| 326 | // Declare expression opcodes. |
| 327 | #define DECLARE_NAMED_ENUM(name, opcode, sig) kExpr##name = opcode, |
| 328 | FOREACH_OPCODE(DECLARE_NAMED_ENUM) |
| 329 | #undef DECLARE_NAMED_ENUM |
| 330 | }; |
| 331 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 332 | // The reason for a trap. |
| 333 | enum TrapReason { |
| 334 | kTrapUnreachable, |
| 335 | kTrapMemOutOfBounds, |
| 336 | kTrapDivByZero, |
| 337 | kTrapDivUnrepresentable, |
| 338 | kTrapRemByZero, |
| 339 | kTrapFloatUnrepresentable, |
| 340 | kTrapFuncInvalid, |
| 341 | kTrapFuncSigMismatch, |
| 342 | kTrapCount |
| 343 | }; |
| 344 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 345 | // A collection of opcode-related static methods. |
| 346 | class WasmOpcodes { |
| 347 | public: |
| 348 | static bool IsSupported(WasmOpcode opcode); |
| 349 | static const char* OpcodeName(WasmOpcode opcode); |
| 350 | static FunctionSig* Signature(WasmOpcode opcode); |
| 351 | |
| 352 | static byte MemSize(MachineType type) { |
| 353 | return 1 << ElementSizeLog2Of(type.representation()); |
| 354 | } |
| 355 | |
| 356 | static LocalTypeCode LocalTypeCodeFor(LocalType type) { |
| 357 | switch (type) { |
| 358 | case kAstI32: |
| 359 | return kLocalI32; |
| 360 | case kAstI64: |
| 361 | return kLocalI64; |
| 362 | case kAstF32: |
| 363 | return kLocalF32; |
| 364 | case kAstF64: |
| 365 | return kLocalF64; |
| 366 | case kAstStmt: |
| 367 | return kLocalVoid; |
| 368 | default: |
| 369 | UNREACHABLE(); |
| 370 | return kLocalVoid; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static MemTypeCode MemTypeCodeFor(MachineType type) { |
| 375 | if (type == MachineType::Int8()) { |
| 376 | return kMemI8; |
| 377 | } else if (type == MachineType::Uint8()) { |
| 378 | return kMemU8; |
| 379 | } else if (type == MachineType::Int16()) { |
| 380 | return kMemI16; |
| 381 | } else if (type == MachineType::Uint16()) { |
| 382 | return kMemU16; |
| 383 | } else if (type == MachineType::Int32()) { |
| 384 | return kMemI32; |
| 385 | } else if (type == MachineType::Uint32()) { |
| 386 | return kMemU32; |
| 387 | } else if (type == MachineType::Int64()) { |
| 388 | return kMemI64; |
| 389 | } else if (type == MachineType::Uint64()) { |
| 390 | return kMemU64; |
| 391 | } else if (type == MachineType::Float32()) { |
| 392 | return kMemF32; |
| 393 | } else if (type == MachineType::Float64()) { |
| 394 | return kMemF64; |
| 395 | } else { |
| 396 | UNREACHABLE(); |
| 397 | return kMemI32; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static MachineType MachineTypeFor(LocalType type) { |
| 402 | switch (type) { |
| 403 | case kAstI32: |
| 404 | return MachineType::Int32(); |
| 405 | case kAstI64: |
| 406 | return MachineType::Int64(); |
| 407 | case kAstF32: |
| 408 | return MachineType::Float32(); |
| 409 | case kAstF64: |
| 410 | return MachineType::Float64(); |
| 411 | case kAstStmt: |
| 412 | return MachineType::None(); |
| 413 | default: |
| 414 | UNREACHABLE(); |
| 415 | return MachineType::None(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static LocalType LocalTypeFor(MachineType type) { |
| 420 | if (type == MachineType::Int8()) { |
| 421 | return kAstI32; |
| 422 | } else if (type == MachineType::Uint8()) { |
| 423 | return kAstI32; |
| 424 | } else if (type == MachineType::Int16()) { |
| 425 | return kAstI32; |
| 426 | } else if (type == MachineType::Uint16()) { |
| 427 | return kAstI32; |
| 428 | } else if (type == MachineType::Int32()) { |
| 429 | return kAstI32; |
| 430 | } else if (type == MachineType::Uint32()) { |
| 431 | return kAstI32; |
| 432 | } else if (type == MachineType::Int64()) { |
| 433 | return kAstI64; |
| 434 | } else if (type == MachineType::Uint64()) { |
| 435 | return kAstI64; |
| 436 | } else if (type == MachineType::Float32()) { |
| 437 | return kAstF32; |
| 438 | } else if (type == MachineType::Float64()) { |
| 439 | return kAstF64; |
| 440 | } else { |
| 441 | UNREACHABLE(); |
| 442 | return kAstI32; |
| 443 | } |
| 444 | } |
| 445 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | static WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) { |
| 447 | if (type == MachineType::Int8()) { |
| 448 | return store ? kExprI32StoreMem8 : kExprI32LoadMem8S; |
| 449 | } else if (type == MachineType::Uint8()) { |
| 450 | return store ? kExprI32StoreMem8 : kExprI32LoadMem8U; |
| 451 | } else if (type == MachineType::Int16()) { |
| 452 | return store ? kExprI32StoreMem16 : kExprI32LoadMem16S; |
| 453 | } else if (type == MachineType::Uint16()) { |
| 454 | return store ? kExprI32StoreMem16 : kExprI32LoadMem16U; |
| 455 | } else if (type == MachineType::Int32()) { |
| 456 | return store ? kExprI32StoreMem : kExprI32LoadMem; |
| 457 | } else if (type == MachineType::Uint32()) { |
| 458 | return store ? kExprI32StoreMem : kExprI32LoadMem; |
| 459 | } else if (type == MachineType::Int64()) { |
| 460 | return store ? kExprI64StoreMem : kExprI64LoadMem; |
| 461 | } else if (type == MachineType::Uint64()) { |
| 462 | return store ? kExprI64StoreMem : kExprI64LoadMem; |
| 463 | } else if (type == MachineType::Float32()) { |
| 464 | return store ? kExprF32StoreMem : kExprF32LoadMem; |
| 465 | } else if (type == MachineType::Float64()) { |
| 466 | return store ? kExprF64StoreMem : kExprF64LoadMem; |
| 467 | } else { |
| 468 | UNREACHABLE(); |
| 469 | return kExprNop; |
| 470 | } |
| 471 | } |
| 472 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 473 | static char ShortNameOf(LocalType type) { |
| 474 | switch (type) { |
| 475 | case kAstI32: |
| 476 | return 'i'; |
| 477 | case kAstI64: |
| 478 | return 'l'; |
| 479 | case kAstF32: |
| 480 | return 'f'; |
| 481 | case kAstF64: |
| 482 | return 'd'; |
| 483 | case kAstStmt: |
| 484 | return 'v'; |
| 485 | case kAstEnd: |
| 486 | return 'x'; |
| 487 | default: |
| 488 | UNREACHABLE(); |
| 489 | return '?'; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static const char* TypeName(LocalType type) { |
| 494 | switch (type) { |
| 495 | case kAstI32: |
| 496 | return "i32"; |
| 497 | case kAstI64: |
| 498 | return "i64"; |
| 499 | case kAstF32: |
| 500 | return "f32"; |
| 501 | case kAstF64: |
| 502 | return "f64"; |
| 503 | case kAstStmt: |
| 504 | return "<stmt>"; |
| 505 | case kAstEnd: |
| 506 | return "<end>"; |
| 507 | default: |
| 508 | return "<unknown>"; |
| 509 | } |
| 510 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 511 | |
| 512 | static const char* TrapReasonName(TrapReason reason) { |
| 513 | switch (reason) { |
| 514 | case kTrapUnreachable: |
| 515 | return "unreachable"; |
| 516 | case kTrapMemOutOfBounds: |
| 517 | return "memory access out of bounds"; |
| 518 | case kTrapDivByZero: |
| 519 | return "divide by zero"; |
| 520 | case kTrapDivUnrepresentable: |
| 521 | return "divide result unrepresentable"; |
| 522 | case kTrapRemByZero: |
| 523 | return "remainder by zero"; |
| 524 | case kTrapFloatUnrepresentable: |
| 525 | return "integer result unrepresentable"; |
| 526 | case kTrapFuncInvalid: |
| 527 | return "invalid function"; |
| 528 | case kTrapFuncSigMismatch: |
| 529 | return "function signature mismatch"; |
| 530 | default: |
| 531 | return "<?>"; |
| 532 | } |
| 533 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 534 | }; |
| 535 | } // namespace wasm |
| 536 | } // namespace internal |
| 537 | } // namespace v8 |
| 538 | |
| 539 | #endif // V8_WASM_OPCODES_H_ |