forked from WAVM/WAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatorPrinter.h
More file actions
97 lines (89 loc) · 3.26 KB
/
Copy pathOperatorPrinter.h
File metadata and controls
97 lines (89 loc) · 3.26 KB
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
#pragma once
#include "Inline/BasicTypes.h"
#include "Inline/Serialization.h"
#include "IR.h"
#include "Module.h"
#include "Operators.h"
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); \
}
ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
std::string unknown(Opcode opcode)
{
return "<unknown opcode " + std::to_string((Uptr)opcode) + ">";
}
private:
const Module& module;
const FunctionDef& functionDef;
std::string describeImm(NoImm) { return ""; }
std::string describeImm(ControlStructureImm imm) { return std::string(" : ") + asString(imm.resultType); }
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 = " [";
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(CallImm 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(CallIndirectImm imm)
{
const std::string typeString = imm.type.index >= module.types.size()
? "<invalid type index>"
: asString(module.types[imm.type.index]);
return " " + typeString;
}
template<Uptr naturalAlignmentLog2>
std::string describeImm(LoadOrStoreImm<naturalAlignmentLog2> imm)
{
return " align=" + std::to_string(1<<imm.alignmentLog2) + " offset=" + std::to_string(imm.offset);
}
std::string describeImm(MemoryImm) { return ""; }
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 = " [";
const char* prefix = "";
for(Uptr laneIndex = 0;laneIndex < numLanes;++laneIndex)
{
result += prefix
+ (imm.laneIndices[laneIndex] < numLanes ? 'a' : 'b')
+ std::to_string(imm.laneIndices[laneIndex]);
prefix = ",";
}
return result;
}
template<Uptr naturalAlignmentLog2>
std::string describeImm(AtomicLoadOrStoreImm<naturalAlignmentLog2> imm)
{
return " align=" + std::to_string(1<<imm.alignmentLog2) + " offset=" + std::to_string(imm.offset);
}
std::string describeImm(CatchImm) { return ""; }
std::string describeImm(ThrowImm) { return ""; }
std::string describeImm(RethrowImm) { return ""; }
};
}