blob: 736c4d960926bdedd1ef144b2d9b77660d484b19 [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#include "src/wasm/wasm-opcodes.h"
6#include "src/signature.h"
7
8namespace v8 {
9namespace internal {
10namespace wasm {
11
12typedef Signature<LocalType> FunctionSig;
13
14const char* WasmOpcodes::OpcodeName(WasmOpcode opcode) {
15 switch (opcode) {
16#define DECLARE_NAME_CASE(name, opcode, sig) \
17 case kExpr##name: \
18 return "Expr" #name;
19 FOREACH_OPCODE(DECLARE_NAME_CASE)
20#undef DECLARE_NAME_CASE
21 default:
22 break;
23 }
24 return "Unknown";
25}
26
27
Ben Murdoch097c5b22016-05-18 11:27:45 +010028std::ostream& operator<<(std::ostream& os, const FunctionSig& sig) {
29 if (sig.return_count() == 0) os << "v";
30 for (size_t i = 0; i < sig.return_count(); i++) {
31 os << WasmOpcodes::ShortNameOf(sig.GetReturn(i));
32 }
33 os << "_";
34 if (sig.parameter_count() == 0) os << "v";
35 for (size_t i = 0; i < sig.parameter_count(); i++) {
36 os << WasmOpcodes::ShortNameOf(sig.GetParam(i));
37 }
38 return os;
39}
40
41
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042#define DECLARE_SIG_ENUM(name, ...) kSigEnum_##name,
43
44
45enum WasmOpcodeSig { FOREACH_SIGNATURE(DECLARE_SIG_ENUM) };
46
47
48// TODO(titzer): not static-initializer safe. Wrap in LazyInstance.
49#define DECLARE_SIG(name, ...) \
50 static LocalType kTypes_##name[] = {__VA_ARGS__}; \
51 static const FunctionSig kSig_##name( \
52 1, static_cast<int>(arraysize(kTypes_##name)) - 1, kTypes_##name);
53
54FOREACH_SIGNATURE(DECLARE_SIG)
55
56#define DECLARE_SIG_ENTRY(name, ...) &kSig_##name,
57
58static const FunctionSig* kSimpleExprSigs[] = {
59 nullptr, FOREACH_SIGNATURE(DECLARE_SIG_ENTRY)};
60
61static byte kSimpleExprSigTable[256];
62
63
64// Initialize the signature table.
65static void InitSigTable() {
66#define SET_SIG_TABLE(name, opcode, sig) \
67 kSimpleExprSigTable[opcode] = static_cast<int>(kSigEnum_##sig) + 1;
68 FOREACH_SIMPLE_OPCODE(SET_SIG_TABLE);
Ben Murdochda12d292016-06-02 14:46:10 +010069 FOREACH_ASMJS_COMPAT_OPCODE(SET_SIG_TABLE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070#undef SET_SIG_TABLE
71}
72
73
74FunctionSig* WasmOpcodes::Signature(WasmOpcode opcode) {
75 // TODO(titzer): use LazyInstance to make this thread safe.
76 if (kSimpleExprSigTable[kExprI32Add] == 0) InitSigTable();
77 return const_cast<FunctionSig*>(
78 kSimpleExprSigs[kSimpleExprSigTable[static_cast<byte>(opcode)]]);
79}
80
81
82// TODO(titzer): pull WASM_64 up to a common header.
83#if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64
84#define WASM_64 1
85#else
86#define WASM_64 0
87#endif
88
89
90bool WasmOpcodes::IsSupported(WasmOpcode opcode) {
91#if !WASM_64
92 switch (opcode) {
93 // Opcodes not supported on 32-bit platforms.
94 case kExprI64Add:
95 case kExprI64Sub:
96 case kExprI64Mul:
97 case kExprI64DivS:
98 case kExprI64DivU:
99 case kExprI64RemS:
100 case kExprI64RemU:
101 case kExprI64And:
102 case kExprI64Ior:
103 case kExprI64Xor:
104 case kExprI64Shl:
105 case kExprI64ShrU:
106 case kExprI64ShrS:
Ben Murdochda12d292016-06-02 14:46:10 +0100107 case kExprI64Ror:
108 case kExprI64Rol:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 case kExprI64Eq:
110 case kExprI64Ne:
111 case kExprI64LtS:
112 case kExprI64LeS:
113 case kExprI64LtU:
114 case kExprI64LeU:
115 case kExprI64GtS:
116 case kExprI64GeS:
117 case kExprI64GtU:
118 case kExprI64GeU:
119
120 case kExprI32ConvertI64:
121 case kExprI64SConvertI32:
122 case kExprI64UConvertI32:
123
124 case kExprF64ReinterpretI64:
125 case kExprI64ReinterpretF64:
126
127 case kExprI64Clz:
128 case kExprI64Ctz:
129 case kExprI64Popcnt:
130
131 case kExprF32SConvertI64:
132 case kExprF32UConvertI64:
133 case kExprF64SConvertI64:
134 case kExprF64UConvertI64:
135 case kExprI64SConvertF32:
136 case kExprI64SConvertF64:
137 case kExprI64UConvertF32:
138 case kExprI64UConvertF64:
139
140 return false;
141 default:
142 return true;
143 }
144#else
145 return true;
146#endif
147}
148} // namespace wasm
149} // namespace internal
150} // namespace v8