blob: 7cb9c00449c7f301424be47589e9177ed69c18af [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
11namespace v8 {
12namespace internal {
13namespace wasm {
14
15// Binary encoding of local types.
16enum LocalTypeCode {
17 kLocalVoid = 0,
18 kLocalI32 = 1,
19 kLocalI64 = 2,
20 kLocalF32 = 3,
21 kLocalF64 = 4
22};
23
24// Binary encoding of memory types.
25enum 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.
40typedef MachineRepresentation LocalType;
41const LocalType kAstStmt = MachineRepresentation::kNone;
42const LocalType kAstI32 = MachineRepresentation::kWord32;
43const LocalType kAstI64 = MachineRepresentation::kWord64;
44const LocalType kAstF32 = MachineRepresentation::kFloat32;
45const LocalType kAstF64 = MachineRepresentation::kFloat64;
46// We use kTagged here because kNone is already used by kAstStmt.
47const LocalType kAstEnd = MachineRepresentation::kTagged;
48
49// Functionality related to encoding memory accesses.
50struct MemoryAccess {
51 // Atomicity annotations for access to the memory and globals.
52 enum Atomicity {
53 kNone = 0, // non-atomic
54 kSequential = 1, // sequential consistency
55 kAcquire = 2, // acquire semantics
56 kRelease = 3 // release semantics
57 };
58
59 // Alignment annotations for memory accesses.
60 enum Alignment { kAligned = 0, kUnaligned = 1 };
61
62 // Bitfields for the various annotations for memory accesses.
63 typedef BitField<Alignment, 7, 1> AlignmentField;
64 typedef BitField<Atomicity, 5, 2> AtomicityField;
65 typedef BitField<bool, 4, 1> OffsetField;
66};
67
68typedef Signature<LocalType> FunctionSig;
Ben Murdoch097c5b22016-05-18 11:27:45 +010069std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
70
71// TODO(titzer): Renumber all the opcodes to fill in holes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072
73// Control expressions and blocks.
74#define FOREACH_CONTROL_OPCODE(V) \
75 V(Nop, 0x00, _) \
76 V(Block, 0x01, _) \
77 V(Loop, 0x02, _) \
78 V(If, 0x03, _) \
79 V(IfElse, 0x04, _) \
80 V(Select, 0x05, _) \
81 V(Br, 0x06, _) \
82 V(BrIf, 0x07, _) \
83 V(TableSwitch, 0x08, _) \
84 V(Return, 0x14, _) \
85 V(Unreachable, 0x15, _)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086
87// Constants, locals, globals, and calls.
88#define FOREACH_MISC_OPCODE(V) \
89 V(I8Const, 0x09, _) \
90 V(I32Const, 0x0a, _) \
91 V(I64Const, 0x0b, _) \
92 V(F64Const, 0x0c, _) \
93 V(F32Const, 0x0d, _) \
94 V(GetLocal, 0x0e, _) \
95 V(SetLocal, 0x0f, _) \
96 V(LoadGlobal, 0x10, _) \
97 V(StoreGlobal, 0x11, _) \
98 V(CallFunction, 0x12, _) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010099 V(CallIndirect, 0x13, _) \
100 V(CallImport, 0x1F, _)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101
102// Load memory expressions.
103#define FOREACH_LOAD_MEM_OPCODE(V) \
104 V(I32LoadMem8S, 0x20, i_i) \
105 V(I32LoadMem8U, 0x21, i_i) \
106 V(I32LoadMem16S, 0x22, i_i) \
107 V(I32LoadMem16U, 0x23, i_i) \
108 V(I64LoadMem8S, 0x24, l_i) \
109 V(I64LoadMem8U, 0x25, l_i) \
110 V(I64LoadMem16S, 0x26, l_i) \
111 V(I64LoadMem16U, 0x27, l_i) \
112 V(I64LoadMem32S, 0x28, l_i) \
113 V(I64LoadMem32U, 0x29, l_i) \
114 V(I32LoadMem, 0x2a, i_i) \
115 V(I64LoadMem, 0x2b, l_i) \
116 V(F32LoadMem, 0x2c, f_i) \
117 V(F64LoadMem, 0x2d, d_i)
118
119// Store memory expressions.
120#define FOREACH_STORE_MEM_OPCODE(V) \
121 V(I32StoreMem8, 0x2e, i_ii) \
122 V(I32StoreMem16, 0x2f, i_ii) \
123 V(I64StoreMem8, 0x30, l_il) \
124 V(I64StoreMem16, 0x31, l_il) \
125 V(I64StoreMem32, 0x32, l_il) \
126 V(I32StoreMem, 0x33, i_ii) \
127 V(I64StoreMem, 0x34, l_il) \
128 V(F32StoreMem, 0x35, f_if) \
129 V(F64StoreMem, 0x36, d_id)
130
131// Load memory expressions.
132#define FOREACH_MISC_MEM_OPCODE(V) \
133 V(MemorySize, 0x3b, i_v) \
134 V(GrowMemory, 0x39, i_i)
135
136// Expressions with signatures.
137#define FOREACH_SIMPLE_OPCODE(V) \
138 V(I32Add, 0x40, i_ii) \
139 V(I32Sub, 0x41, i_ii) \
140 V(I32Mul, 0x42, i_ii) \
141 V(I32DivS, 0x43, i_ii) \
142 V(I32DivU, 0x44, i_ii) \
143 V(I32RemS, 0x45, i_ii) \
144 V(I32RemU, 0x46, i_ii) \
145 V(I32And, 0x47, i_ii) \
146 V(I32Ior, 0x48, i_ii) \
147 V(I32Xor, 0x49, i_ii) \
148 V(I32Shl, 0x4a, i_ii) \
149 V(I32ShrU, 0x4b, i_ii) \
150 V(I32ShrS, 0x4c, i_ii) \
151 V(I32Eq, 0x4d, i_ii) \
152 V(I32Ne, 0x4e, i_ii) \
153 V(I32LtS, 0x4f, i_ii) \
154 V(I32LeS, 0x50, i_ii) \
155 V(I32LtU, 0x51, i_ii) \
156 V(I32LeU, 0x52, i_ii) \
157 V(I32GtS, 0x53, i_ii) \
158 V(I32GeS, 0x54, i_ii) \
159 V(I32GtU, 0x55, i_ii) \
160 V(I32GeU, 0x56, i_ii) \
161 V(I32Clz, 0x57, i_i) \
162 V(I32Ctz, 0x58, i_i) \
163 V(I32Popcnt, 0x59, i_i) \
164 V(BoolNot, 0x5a, i_i) \
165 V(I64Add, 0x5b, l_ll) \
166 V(I64Sub, 0x5c, l_ll) \
167 V(I64Mul, 0x5d, l_ll) \
168 V(I64DivS, 0x5e, l_ll) \
169 V(I64DivU, 0x5f, l_ll) \
170 V(I64RemS, 0x60, l_ll) \
171 V(I64RemU, 0x61, l_ll) \
172 V(I64And, 0x62, l_ll) \
173 V(I64Ior, 0x63, l_ll) \
174 V(I64Xor, 0x64, l_ll) \
175 V(I64Shl, 0x65, l_ll) \
176 V(I64ShrU, 0x66, l_ll) \
177 V(I64ShrS, 0x67, l_ll) \
178 V(I64Eq, 0x68, i_ll) \
179 V(I64Ne, 0x69, i_ll) \
180 V(I64LtS, 0x6a, i_ll) \
181 V(I64LeS, 0x6b, i_ll) \
182 V(I64LtU, 0x6c, i_ll) \
183 V(I64LeU, 0x6d, i_ll) \
184 V(I64GtS, 0x6e, i_ll) \
185 V(I64GeS, 0x6f, i_ll) \
186 V(I64GtU, 0x70, i_ll) \
187 V(I64GeU, 0x71, i_ll) \
188 V(I64Clz, 0x72, l_l) \
189 V(I64Ctz, 0x73, l_l) \
190 V(I64Popcnt, 0x74, l_l) \
191 V(F32Add, 0x75, f_ff) \
192 V(F32Sub, 0x76, f_ff) \
193 V(F32Mul, 0x77, f_ff) \
194 V(F32Div, 0x78, f_ff) \
195 V(F32Min, 0x79, f_ff) \
196 V(F32Max, 0x7a, f_ff) \
197 V(F32Abs, 0x7b, f_f) \
198 V(F32Neg, 0x7c, f_f) \
199 V(F32CopySign, 0x7d, f_ff) \
200 V(F32Ceil, 0x7e, f_f) \
201 V(F32Floor, 0x7f, f_f) \
202 V(F32Trunc, 0x80, f_f) \
203 V(F32NearestInt, 0x81, f_f) \
204 V(F32Sqrt, 0x82, f_f) \
205 V(F32Eq, 0x83, i_ff) \
206 V(F32Ne, 0x84, i_ff) \
207 V(F32Lt, 0x85, i_ff) \
208 V(F32Le, 0x86, i_ff) \
209 V(F32Gt, 0x87, i_ff) \
210 V(F32Ge, 0x88, i_ff) \
211 V(F64Add, 0x89, d_dd) \
212 V(F64Sub, 0x8a, d_dd) \
213 V(F64Mul, 0x8b, d_dd) \
214 V(F64Div, 0x8c, d_dd) \
215 V(F64Min, 0x8d, d_dd) \
216 V(F64Max, 0x8e, d_dd) \
217 V(F64Abs, 0x8f, d_d) \
218 V(F64Neg, 0x90, d_d) \
219 V(F64CopySign, 0x91, d_dd) \
220 V(F64Ceil, 0x92, d_d) \
221 V(F64Floor, 0x93, d_d) \
222 V(F64Trunc, 0x94, d_d) \
223 V(F64NearestInt, 0x95, d_d) \
224 V(F64Sqrt, 0x96, d_d) \
225 V(F64Eq, 0x97, i_dd) \
226 V(F64Ne, 0x98, i_dd) \
227 V(F64Lt, 0x99, i_dd) \
228 V(F64Le, 0x9a, i_dd) \
229 V(F64Gt, 0x9b, i_dd) \
230 V(F64Ge, 0x9c, i_dd) \
231 V(I32SConvertF32, 0x9d, i_f) \
232 V(I32SConvertF64, 0x9e, i_d) \
233 V(I32UConvertF32, 0x9f, i_f) \
234 V(I32UConvertF64, 0xa0, i_d) \
235 V(I32ConvertI64, 0xa1, i_l) \
236 V(I64SConvertF32, 0xa2, l_f) \
237 V(I64SConvertF64, 0xa3, l_d) \
238 V(I64UConvertF32, 0xa4, l_f) \
239 V(I64UConvertF64, 0xa5, l_d) \
240 V(I64SConvertI32, 0xa6, l_i) \
241 V(I64UConvertI32, 0xa7, l_i) \
242 V(F32SConvertI32, 0xa8, f_i) \
243 V(F32UConvertI32, 0xa9, f_i) \
244 V(F32SConvertI64, 0xaa, f_l) \
245 V(F32UConvertI64, 0xab, f_l) \
246 V(F32ConvertF64, 0xac, f_d) \
247 V(F32ReinterpretI32, 0xad, f_i) \
248 V(F64SConvertI32, 0xae, d_i) \
249 V(F64UConvertI32, 0xaf, d_i) \
250 V(F64SConvertI64, 0xb0, d_l) \
251 V(F64UConvertI64, 0xb1, d_l) \
252 V(F64ConvertF32, 0xb2, d_f) \
253 V(F64ReinterpretI64, 0xb3, d_l) \
254 V(I32ReinterpretF32, 0xb4, i_f) \
255 V(I64ReinterpretF64, 0xb5, l_d)
256
257// All opcodes.
258#define FOREACH_OPCODE(V) \
259 FOREACH_CONTROL_OPCODE(V) \
260 FOREACH_MISC_OPCODE(V) \
261 FOREACH_SIMPLE_OPCODE(V) \
262 FOREACH_STORE_MEM_OPCODE(V) \
263 FOREACH_LOAD_MEM_OPCODE(V) \
264 FOREACH_MISC_MEM_OPCODE(V)
265
266// All signatures.
267#define FOREACH_SIGNATURE(V) \
268 V(i_ii, kAstI32, kAstI32, kAstI32) \
269 V(i_i, kAstI32, kAstI32) \
270 V(i_v, kAstI32) \
271 V(i_ff, kAstI32, kAstF32, kAstF32) \
272 V(i_f, kAstI32, kAstF32) \
273 V(i_dd, kAstI32, kAstF64, kAstF64) \
274 V(i_d, kAstI32, kAstF64) \
275 V(i_l, kAstI32, kAstI64) \
276 V(l_ll, kAstI64, kAstI64, kAstI64) \
277 V(i_ll, kAstI32, kAstI64, kAstI64) \
278 V(l_l, kAstI64, kAstI64) \
279 V(l_i, kAstI64, kAstI32) \
280 V(l_f, kAstI64, kAstF32) \
281 V(l_d, kAstI64, kAstF64) \
282 V(f_ff, kAstF32, kAstF32, kAstF32) \
283 V(f_f, kAstF32, kAstF32) \
284 V(f_d, kAstF32, kAstF64) \
285 V(f_i, kAstF32, kAstI32) \
286 V(f_l, kAstF32, kAstI64) \
287 V(d_dd, kAstF64, kAstF64, kAstF64) \
288 V(d_d, kAstF64, kAstF64) \
289 V(d_f, kAstF64, kAstF32) \
290 V(d_i, kAstF64, kAstI32) \
291 V(d_l, kAstF64, kAstI64) \
292 V(d_id, kAstF64, kAstI32, kAstF64) \
293 V(f_if, kAstF32, kAstI32, kAstF32) \
294 V(l_il, kAstI64, kAstI32, kAstI64)
295
296enum WasmOpcode {
297// Declare expression opcodes.
298#define DECLARE_NAMED_ENUM(name, opcode, sig) kExpr##name = opcode,
299 FOREACH_OPCODE(DECLARE_NAMED_ENUM)
300#undef DECLARE_NAMED_ENUM
301};
302
303// A collection of opcode-related static methods.
304class WasmOpcodes {
305 public:
306 static bool IsSupported(WasmOpcode opcode);
307 static const char* OpcodeName(WasmOpcode opcode);
308 static FunctionSig* Signature(WasmOpcode opcode);
309
310 static byte MemSize(MachineType type) {
311 return 1 << ElementSizeLog2Of(type.representation());
312 }
313
314 static LocalTypeCode LocalTypeCodeFor(LocalType type) {
315 switch (type) {
316 case kAstI32:
317 return kLocalI32;
318 case kAstI64:
319 return kLocalI64;
320 case kAstF32:
321 return kLocalF32;
322 case kAstF64:
323 return kLocalF64;
324 case kAstStmt:
325 return kLocalVoid;
326 default:
327 UNREACHABLE();
328 return kLocalVoid;
329 }
330 }
331
332 static MemTypeCode MemTypeCodeFor(MachineType type) {
333 if (type == MachineType::Int8()) {
334 return kMemI8;
335 } else if (type == MachineType::Uint8()) {
336 return kMemU8;
337 } else if (type == MachineType::Int16()) {
338 return kMemI16;
339 } else if (type == MachineType::Uint16()) {
340 return kMemU16;
341 } else if (type == MachineType::Int32()) {
342 return kMemI32;
343 } else if (type == MachineType::Uint32()) {
344 return kMemU32;
345 } else if (type == MachineType::Int64()) {
346 return kMemI64;
347 } else if (type == MachineType::Uint64()) {
348 return kMemU64;
349 } else if (type == MachineType::Float32()) {
350 return kMemF32;
351 } else if (type == MachineType::Float64()) {
352 return kMemF64;
353 } else {
354 UNREACHABLE();
355 return kMemI32;
356 }
357 }
358
359 static MachineType MachineTypeFor(LocalType type) {
360 switch (type) {
361 case kAstI32:
362 return MachineType::Int32();
363 case kAstI64:
364 return MachineType::Int64();
365 case kAstF32:
366 return MachineType::Float32();
367 case kAstF64:
368 return MachineType::Float64();
369 case kAstStmt:
370 return MachineType::None();
371 default:
372 UNREACHABLE();
373 return MachineType::None();
374 }
375 }
376
377 static LocalType LocalTypeFor(MachineType type) {
378 if (type == MachineType::Int8()) {
379 return kAstI32;
380 } else if (type == MachineType::Uint8()) {
381 return kAstI32;
382 } else if (type == MachineType::Int16()) {
383 return kAstI32;
384 } else if (type == MachineType::Uint16()) {
385 return kAstI32;
386 } else if (type == MachineType::Int32()) {
387 return kAstI32;
388 } else if (type == MachineType::Uint32()) {
389 return kAstI32;
390 } else if (type == MachineType::Int64()) {
391 return kAstI64;
392 } else if (type == MachineType::Uint64()) {
393 return kAstI64;
394 } else if (type == MachineType::Float32()) {
395 return kAstF32;
396 } else if (type == MachineType::Float64()) {
397 return kAstF64;
398 } else {
399 UNREACHABLE();
400 return kAstI32;
401 }
402 }
403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000404 static WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
405 if (type == MachineType::Int8()) {
406 return store ? kExprI32StoreMem8 : kExprI32LoadMem8S;
407 } else if (type == MachineType::Uint8()) {
408 return store ? kExprI32StoreMem8 : kExprI32LoadMem8U;
409 } else if (type == MachineType::Int16()) {
410 return store ? kExprI32StoreMem16 : kExprI32LoadMem16S;
411 } else if (type == MachineType::Uint16()) {
412 return store ? kExprI32StoreMem16 : kExprI32LoadMem16U;
413 } else if (type == MachineType::Int32()) {
414 return store ? kExprI32StoreMem : kExprI32LoadMem;
415 } else if (type == MachineType::Uint32()) {
416 return store ? kExprI32StoreMem : kExprI32LoadMem;
417 } else if (type == MachineType::Int64()) {
418 return store ? kExprI64StoreMem : kExprI64LoadMem;
419 } else if (type == MachineType::Uint64()) {
420 return store ? kExprI64StoreMem : kExprI64LoadMem;
421 } else if (type == MachineType::Float32()) {
422 return store ? kExprF32StoreMem : kExprF32LoadMem;
423 } else if (type == MachineType::Float64()) {
424 return store ? kExprF64StoreMem : kExprF64LoadMem;
425 } else {
426 UNREACHABLE();
427 return kExprNop;
428 }
429 }
430
431 static byte LoadStoreAccessOf(bool with_offset) {
432 return MemoryAccess::OffsetField::encode(with_offset);
433 }
434
435 static char ShortNameOf(LocalType type) {
436 switch (type) {
437 case kAstI32:
438 return 'i';
439 case kAstI64:
440 return 'l';
441 case kAstF32:
442 return 'f';
443 case kAstF64:
444 return 'd';
445 case kAstStmt:
446 return 'v';
447 case kAstEnd:
448 return 'x';
449 default:
450 UNREACHABLE();
451 return '?';
452 }
453 }
454
455 static const char* TypeName(LocalType type) {
456 switch (type) {
457 case kAstI32:
458 return "i32";
459 case kAstI64:
460 return "i64";
461 case kAstF32:
462 return "f32";
463 case kAstF64:
464 return "f64";
465 case kAstStmt:
466 return "<stmt>";
467 case kAstEnd:
468 return "<end>";
469 default:
470 return "<unknown>";
471 }
472 }
473};
474} // namespace wasm
475} // namespace internal
476} // namespace v8
477
478#endif // V8_WASM_OPCODES_H_