-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftable.cpp
186 lines (146 loc) · 5.47 KB
/
ftable.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* function table */
#include "ftable.h"
#include <llvm/IR/Constants.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/SourceMgr.h>
#include <set>
#include <unordered_set>
#include <vector>
using namespace llvm;
const bool DEBUG = false;
bool readFunctionTable(Module *m, FunctionTableTy& tbl) {
tbl.clear();
GlobalVariable *ft = m->getGlobalVariable("R_FunTab", true);
if (!ft) {
errs() << "ERROR: Can't resolve FunTab\n";
return false;
}
PointerType *pTy = dyn_cast<PointerType>(ft->getType());
if (!pTy) {
errs() << "ERROR: invalid function table (not pointer type)\n";
return false;
}
ArrayType *aTy = dyn_cast<ArrayType>(pTy->getElementType());
if (!aTy) {
errs() << "ERROR: invalid function table (pointer not to array type)\n";
return false;
}
unsigned nfuns = aTy->getNumElements();
if (DEBUG) outs() << "Function table has " << std::to_string(nfuns) << " entries.\n";
if (!ft->hasInitializer()) {
errs() << "ERROR: invalid function table (does not have initializer)\n";
return false;
}
ConstantArray *ca = dyn_cast<ConstantArray>(ft->getInitializer());
if (!ca) {
errs() << "ERROR: invalid function table (not constant array)\n";
return false;
}
tbl.reserve(nfuns - 1);
for(unsigned i = 0; i < nfuns - 1; i++) {
ConstantStruct *cstr = dyn_cast<ConstantStruct>(ca->getAggregateElement(i));
if (!cstr) {
errs() << "ERROR: invalid function table (array not of structures): " << i << "\n";
return false;
}
if (cstr->getType()->getName() != "struct.FUNTAB") {
errs() << "ERROR: array of structures not struct.FUNTAB\n";
return false;
}
ConstantExpr *nameExp = dyn_cast<ConstantExpr>(cstr->getAggregateElement(0U));
Function *fun = dyn_cast<Function>(cstr->getAggregateElement(1U));
ConstantInt *offsetC = dyn_cast<ConstantInt>(cstr->getAggregateElement(2U));
ConstantInt *evalC = dyn_cast<ConstantInt>(cstr->getAggregateElement(3U));
ConstantInt *arityC = dyn_cast<ConstantInt>(cstr->getAggregateElement(4U));
int64_t offset = offsetC->getSExtValue();
int64_t eval = evalC->getSExtValue();
int64_t arity = arityC->getSExtValue();
GlobalVariable* nameStr = cast<GlobalVariable>(nameExp->getOperand(0));
ConstantDataArray* nameInit = cast<ConstantDataArray>(nameStr->getInitializer());
std::string name = nameInit->getAsCString();
if (DEBUG) errs() << " name: " << name << " function: " << fun->getName() << " offset: " << std::to_string(offset)
<< " eval: " << std::to_string(eval) << " arity: " << std::to_string(arity) << "\n";
tbl.push_back({FunctionEntry(name, fun, offset, eval, arity)});
}
if (!isa<ConstantAggregateZero>(ca->getAggregateElement(nfuns-1))) {
errs() << "ERROR: last entry of function table is not zero\n";
return false;
}
return true;
}
void dumpFunctions(FunctionTableTy& funtab, std::string type, bool isInternal, bool isPrimitive, bool isBuiltin, bool isSpecial) {
unsigned cnt;
cnt = 0;
errs() << type << ":\n";
for(FunctionTableTy::iterator fi = funtab.begin(), fe = funtab.end(); fi != fe; ++fi) {
FunctionEntry& f = *fi;
if (f.isInternal() == isInternal && f.isPrimitive() == isPrimitive && f.isBuiltin() == isBuiltin && f.isSpecial() == isSpecial) {
cnt++;
errs() << " " << f.rname << " (" << f.fun->getName() << ":" << f.arity << " " /* << (ensuresArity(f.fun) ? "CHECKED" : "IGNORED") */ << ")\n";
}
}
errs() << " (in total " << std::to_string(cnt) << " " << type << ")\n";
}
void dumpFunctionTable(FunctionTableTy& funtab) {
dumpFunctions(funtab, "internal builtins", true, false, true, false);
dumpFunctions(funtab, "internal specials", true, false, false, true);
dumpFunctions(funtab, "primitive builtins", false, true, true, false);
dumpFunctions(funtab, "primitive specials", false, true, false, true);
}
int maxArity(Function* fun, FunctionTableTy& funtab) {
int res = -1;
for(FunctionTableTy::iterator fi = funtab.begin(), fe = funtab.end(); fi != fe; ++fi) {
FunctionEntry& e = *fi;
if (e.fun == fun && e.arity > res) {
res = e.arity;
}
}
return res;
}
IntSetTy uniqueFunctionArities(Function *fun, FunctionTableTy& funtab) {
IntSetTy arities;
for(FunctionTableTy::iterator fi = funtab.begin(), fe = funtab.end(); fi != fe; ++fi) {
FunctionEntry& e = *fi;
if (e.fun == fun) {
arities.insert(e.arity);
}
}
return arities;
}
std::string dumpFunctionArities(IntSetTy arities, int effectiveArity) {
assert(arities.size() > 0);
if (arities.size() == 1) {
int a = *arities.begin();
if (a != effectiveArity) {
return "[!" + std::to_string(a) + "]";
} else {
return "[" + std::to_string(a) + "]"; // perhaps could omit this output
}
}
std::string res = "[";
bool first = true;
for(IntSetTy::iterator ai = arities.begin(), ae = arities.end(); ai != ae; ++ai) {
int a = *ai;
if (first) {
first = false;
} else {
res += ",";
}
res += std::to_string(a);
}
return res + "]";
}
bool isDoFunction(Function *fun, FunctionTableTy& funtab) {
for(FunctionTableTy::iterator fi = funtab.begin(), fe = funtab.end(); fi != fe; ++fi) {
FunctionEntry& e = *fi;
Function *fun = e.fun;
if (e.fun == fun) {
return true;
}
}
return false;
}