blob: da6c1615dce292ee74cfd6166ed900e2a20a6436 [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"
Ben Murdochc5610432016-08-08 18:44:38 +01006#include "src/messages.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/signature.h"
8
9namespace v8 {
10namespace internal {
11namespace wasm {
12
13typedef Signature<LocalType> FunctionSig;
14
15const char* WasmOpcodes::OpcodeName(WasmOpcode opcode) {
16 switch (opcode) {
17#define DECLARE_NAME_CASE(name, opcode, sig) \
18 case kExpr##name: \
19 return "Expr" #name;
20 FOREACH_OPCODE(DECLARE_NAME_CASE)
21#undef DECLARE_NAME_CASE
22 default:
23 break;
24 }
25 return "Unknown";
26}
27
Ben Murdochc5610432016-08-08 18:44:38 +010028const char* WasmOpcodes::ShortOpcodeName(WasmOpcode opcode) {
29 switch (opcode) {
30#define DECLARE_NAME_CASE(name, opcode, sig) \
31 case kExpr##name: \
32 return #name;
33 FOREACH_OPCODE(DECLARE_NAME_CASE)
34#undef DECLARE_NAME_CASE
35 default:
36 break;
37 }
38 return "Unknown";
39}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
Ben Murdoch097c5b22016-05-18 11:27:45 +010041std::ostream& operator<<(std::ostream& os, const FunctionSig& sig) {
42 if (sig.return_count() == 0) os << "v";
Ben Murdoch61f157c2016-09-16 13:49:30 +010043 for (size_t i = 0; i < sig.return_count(); ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010044 os << WasmOpcodes::ShortNameOf(sig.GetReturn(i));
45 }
46 os << "_";
47 if (sig.parameter_count() == 0) os << "v";
Ben Murdoch61f157c2016-09-16 13:49:30 +010048 for (size_t i = 0; i < sig.parameter_count(); ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010049 os << WasmOpcodes::ShortNameOf(sig.GetParam(i));
50 }
51 return os;
52}
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054#define DECLARE_SIG_ENUM(name, ...) kSigEnum_##name,
55
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056enum WasmOpcodeSig { FOREACH_SIGNATURE(DECLARE_SIG_ENUM) };
57
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058// TODO(titzer): not static-initializer safe. Wrap in LazyInstance.
59#define DECLARE_SIG(name, ...) \
60 static LocalType kTypes_##name[] = {__VA_ARGS__}; \
61 static const FunctionSig kSig_##name( \
62 1, static_cast<int>(arraysize(kTypes_##name)) - 1, kTypes_##name);
63
64FOREACH_SIGNATURE(DECLARE_SIG)
65
66#define DECLARE_SIG_ENTRY(name, ...) &kSig_##name,
67
68static const FunctionSig* kSimpleExprSigs[] = {
69 nullptr, FOREACH_SIGNATURE(DECLARE_SIG_ENTRY)};
70
71static byte kSimpleExprSigTable[256];
72
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073// Initialize the signature table.
74static void InitSigTable() {
75#define SET_SIG_TABLE(name, opcode, sig) \
76 kSimpleExprSigTable[opcode] = static_cast<int>(kSigEnum_##sig) + 1;
77 FOREACH_SIMPLE_OPCODE(SET_SIG_TABLE);
Ben Murdochda12d292016-06-02 14:46:10 +010078 FOREACH_ASMJS_COMPAT_OPCODE(SET_SIG_TABLE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079#undef SET_SIG_TABLE
80}
81
Ben Murdochc5610432016-08-08 18:44:38 +010082class SigTable {
83 public:
84 SigTable() {
85 // TODO(ahaas): Move {InitSigTable} into the class.
86 InitSigTable();
87 }
88 FunctionSig* Signature(WasmOpcode opcode) const {
89 return const_cast<FunctionSig*>(
90 kSimpleExprSigs[kSimpleExprSigTable[static_cast<byte>(opcode)]]);
91 }
92};
93
94static base::LazyInstance<SigTable>::type sig_table = LAZY_INSTANCE_INITIALIZER;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095
96FunctionSig* WasmOpcodes::Signature(WasmOpcode opcode) {
Ben Murdochc5610432016-08-08 18:44:38 +010097 return sig_table.Get().Signature(opcode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098}
99
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100// TODO(titzer): pull WASM_64 up to a common header.
101#if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64
102#define WASM_64 1
103#else
104#define WASM_64 0
105#endif
106
Ben Murdochc5610432016-08-08 18:44:38 +0100107int WasmOpcodes::TrapReasonToMessageId(TrapReason reason) {
108 switch (reason) {
109#define TRAPREASON_TO_MESSAGE(name) \
110 case k##name: \
111 return MessageTemplate::kWasm##name;
112 FOREACH_WASM_TRAPREASON(TRAPREASON_TO_MESSAGE)
113#undef TRAPREASON_TO_MESSAGE
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 default:
Ben Murdochc5610432016-08-08 18:44:38 +0100115 return MessageTemplate::kNone;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 }
Ben Murdochc5610432016-08-08 18:44:38 +0100117}
118
119const char* WasmOpcodes::TrapReasonMessage(TrapReason reason) {
120 return MessageTemplate::TemplateString(TrapReasonToMessageId(reason));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121}
122} // namespace wasm
123} // namespace internal
124} // namespace v8