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 #include "src/wasm/wasm-opcodes.h"
6 #include "src/signature.h"
7
8 namespace v8 {
9 namespace internal {
10 namespace wasm {
11
12 typedef Signature<LocalType> FunctionSig;
13
OpcodeName(WasmOpcode opcode)14 const 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
28 #define DECLARE_SIG_ENUM(name, ...) kSigEnum_##name,
29
30
31 enum WasmOpcodeSig { FOREACH_SIGNATURE(DECLARE_SIG_ENUM) };
32
33
34 // TODO(titzer): not static-initializer safe. Wrap in LazyInstance.
35 #define DECLARE_SIG(name, ...) \
36 static LocalType kTypes_##name[] = {__VA_ARGS__}; \
37 static const FunctionSig kSig_##name( \
38 1, static_cast<int>(arraysize(kTypes_##name)) - 1, kTypes_##name);
39
40 FOREACH_SIGNATURE(DECLARE_SIG)
41
42 #define DECLARE_SIG_ENTRY(name, ...) &kSig_##name,
43
44 static const FunctionSig* kSimpleExprSigs[] = {
45 nullptr, FOREACH_SIGNATURE(DECLARE_SIG_ENTRY)};
46
47 static byte kSimpleExprSigTable[256];
48
49
50 // Initialize the signature table.
InitSigTable()51 static void InitSigTable() {
52 #define SET_SIG_TABLE(name, opcode, sig) \
53 kSimpleExprSigTable[opcode] = static_cast<int>(kSigEnum_##sig) + 1;
54 FOREACH_SIMPLE_OPCODE(SET_SIG_TABLE);
55 #undef SET_SIG_TABLE
56 }
57
58
Signature(WasmOpcode opcode)59 FunctionSig* WasmOpcodes::Signature(WasmOpcode opcode) {
60 // TODO(titzer): use LazyInstance to make this thread safe.
61 if (kSimpleExprSigTable[kExprI32Add] == 0) InitSigTable();
62 return const_cast<FunctionSig*>(
63 kSimpleExprSigs[kSimpleExprSigTable[static_cast<byte>(opcode)]]);
64 }
65
66
67 // TODO(titzer): pull WASM_64 up to a common header.
68 #if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64
69 #define WASM_64 1
70 #else
71 #define WASM_64 0
72 #endif
73
74
IsSupported(WasmOpcode opcode)75 bool WasmOpcodes::IsSupported(WasmOpcode opcode) {
76 #if !WASM_64
77 switch (opcode) {
78 // Opcodes not supported on 32-bit platforms.
79 case kExprI64Add:
80 case kExprI64Sub:
81 case kExprI64Mul:
82 case kExprI64DivS:
83 case kExprI64DivU:
84 case kExprI64RemS:
85 case kExprI64RemU:
86 case kExprI64And:
87 case kExprI64Ior:
88 case kExprI64Xor:
89 case kExprI64Shl:
90 case kExprI64ShrU:
91 case kExprI64ShrS:
92 case kExprI64Eq:
93 case kExprI64Ne:
94 case kExprI64LtS:
95 case kExprI64LeS:
96 case kExprI64LtU:
97 case kExprI64LeU:
98 case kExprI64GtS:
99 case kExprI64GeS:
100 case kExprI64GtU:
101 case kExprI64GeU:
102
103 case kExprI32ConvertI64:
104 case kExprI64SConvertI32:
105 case kExprI64UConvertI32:
106
107 case kExprF64ReinterpretI64:
108 case kExprI64ReinterpretF64:
109
110 case kExprI64Clz:
111 case kExprI64Ctz:
112 case kExprI64Popcnt:
113
114 case kExprF32SConvertI64:
115 case kExprF32UConvertI64:
116 case kExprF64SConvertI64:
117 case kExprF64UConvertI64:
118 case kExprI64SConvertF32:
119 case kExprI64SConvertF64:
120 case kExprI64UConvertF32:
121 case kExprI64UConvertF64:
122
123 return false;
124 default:
125 return true;
126 }
127 #else
128 return true;
129 #endif
130 }
131 } // namespace wasm
132 } // namespace internal
133 } // namespace v8
134