blob: 5e705f41ba12d4b854444dfc80bd6ac9dcce6635 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#pragma once
#include "WAVM/IR/IR.h"
#include "WAVM/IR/Module.h"
#include "WAVM/IR/Operators.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Serialization.h"
namespace WAVM { namespace IR {
struct OperatorPrinter
{
typedef std::string Result;
OperatorPrinter(const Module& inModule, const FunctionDef& inFunctionDef)
: module(inModule), functionDef(inFunctionDef)
{
}
#define VISIT_OPCODE(encoding, name, nameString, Imm, ...) \
std::string name(Imm imm = {}) { return std::string(nameString) + describeImm(imm); }
WAVM_ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
private:
const Module& module;
const FunctionDef& functionDef;
std::string describeImm(NoImm) { return ""; }
std::string describeImm(ControlStructureImm imm)
{
const FunctionType type = resolveBlockType(module, imm.type);
return std::string(" : ") + asString(type.params()) + " -> " + asString(type.results());
}
std::string describeImm(SelectImm imm) { return std::string(" ") + asString(imm.type); }
std::string describeImm(BranchImm imm) { return " " + std::to_string(imm.targetDepth); }
std::string describeImm(BranchTableImm imm)
{
std::string result = " " + std::to_string(imm.defaultTargetDepth);
const char* prefix = " [";
WAVM_ASSERT(imm.branchTableIndex < functionDef.branchTables.size());
for(auto depth : functionDef.branchTables[imm.branchTableIndex])
{
result += prefix + std::to_string(depth);
prefix = ",";
}
result += "]";
return result;
}
template<typename NativeValue> std::string describeImm(LiteralImm<NativeValue> imm)
{
return " " + asString(imm.value);
}
template<bool isGlobal> std::string describeImm(GetOrSetVariableImm<isGlobal> imm)
{
return " " + std::to_string(imm.variableIndex);
}
std::string describeImm(FunctionImm imm)
{
const std::string typeString
= imm.functionIndex >= module.functions.size()
? "<invalid function index>"
: asString(module.types[module.functions.getType(imm.functionIndex).index]);
return " " + std::to_string(imm.functionIndex) + " " + typeString;
}
std::string describeImm(FunctionRefImm imm)
{
return describeImm(FunctionImm{imm.functionIndex});
}
std::string describeImm(CallIndirectImm imm)
{
const std::string typeString = imm.type.index >= module.types.size()
? "<invalid type index>"
: asString(module.types[imm.type.index]);
return " " + typeString;
}
std::string describeImm(BaseLoadOrStoreImm imm)
{
return " " + std::to_string(imm.memoryIndex) + " offset=" + std::to_string(imm.offset)
+ " align=" + std::to_string(1 << imm.alignmentLog2);
}
std::string describeImm(MemoryImm imm) { return " " + std::to_string(imm.memoryIndex); }
std::string describeImm(MemoryCopyImm imm)
{
return " " + std::to_string(imm.sourceMemoryIndex) + " "
+ std::to_string(imm.destMemoryIndex);
}
std::string describeImm(TableImm imm) { return " " + std::to_string(imm.tableIndex); }
std::string describeImm(TableCopyImm imm)
{
return " " + std::to_string(imm.sourceTableIndex) + " "
+ std::to_string(imm.destTableIndex);
}
template<Uptr numLanes> std::string describeImm(LaneIndexImm<numLanes> imm)
{
return " " + std::to_string(imm.laneIndex);
}
template<Uptr numLanes> std::string describeImm(ShuffleImm<numLanes> imm)
{
std::string result = " ";
char prefix = '[';
for(Uptr laneIndex = 0; laneIndex < numLanes; ++laneIndex)
{
result += prefix;
result += imm.laneIndices[laneIndex] < numLanes ? 'a' : 'b';
result += imm.laneIndices[laneIndex] < numLanes
? std::to_string(imm.laneIndices[laneIndex])
: std::to_string(imm.laneIndices[laneIndex] - numLanes);
prefix = ',';
}
result += ']';
return result;
}
std::string describeImm(AtomicFenceImm imm)
{
switch(imm.order)
{
case MemoryOrder::sequentiallyConsistent: return " seqcst";
default: WAVM_UNREACHABLE();
};
}
std::string describeImm(ExceptionTypeImm) { return ""; }
std::string describeImm(DelegateImm) { return ""; }
std::string describeImm(RethrowImm) { return ""; }
std::string describeImm(DataSegmentAndMemImm imm)
{
return std::to_string(imm.dataSegmentIndex) + " " + std::to_string(imm.memoryIndex);
}
std::string describeImm(DataSegmentImm imm)
{
return " " + std::to_string(imm.dataSegmentIndex);
}
std::string describeImm(ElemSegmentAndTableImm imm)
{
return " " + std::to_string(imm.elemSegmentIndex) + " "
+ std::to_string(imm.tableIndex);
}
std::string describeImm(ElemSegmentImm imm)
{
return " " + std::to_string(imm.elemSegmentIndex);
}
std::string describeImm(ReferenceTypeImm imm)
{
return std::string(" ") + asString(imm.referenceType);
}
};
}}
|