-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTInterpreter.cpp
More file actions
279 lines (248 loc) · 8.4 KB
/
ASTInterpreter.cpp
File metadata and controls
279 lines (248 loc) · 8.4 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//==--- tools/clang-check/ClangInterpreter.cpp - Clang Interpreter tool --------------===//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
using namespace clang;
#include "Environment.h"
class InterpreterVisitor :
public EvaluatedExprVisitor<InterpreterVisitor> {
public:
explicit InterpreterVisitor(const ASTContext &context, Environment * env)
: EvaluatedExprVisitor(context), mEnv(env) {}
virtual ~InterpreterVisitor() {}
virtual void VisitIntegerLiteral(IntegerLiteral * intliteral) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit IntegerLiteral\n";
mEnv->intliteral(intliteral);
}
virtual void VisitCharacterLiteral(CharacterLiteral * Character ){
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit CharacterLiteral\n";
mEnv->Character(Character);
}
// process BinaryOperator,e.g. assignment, add and etc.
virtual void VisitBinaryOperator (BinaryOperator * bop) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit BinaryOperator\n";
//VisitStmt : 分析表达式,分析该节点下所有子树节点,依次进行深度优先遍历的递归调用去获取函数的值,有些子节点比如说VisitIntegerLiteral下不会再有子树,则不需要visit
VisitStmt(bop);
// llvm::errs() << "[+] visitStmt BinaryOperator done\n";
mEnv->binop(bop);
}
// process DeclRefExpr, e.g. refered decl expr
virtual void VisitDeclRefExpr(DeclRefExpr * expr) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit DeclRefExpr\n";
VisitStmt(expr);
// llvm::errs() << "[+] visitStmt VisitDeclRefExpr done\n";
mEnv->declref(expr);
}
// process CastExpr
// virtual void VisitCastExpr(CastExpr * expr) {
// llvm::errs() << "[+] visit CastExpr\n";
// VisitStmt(expr);
// llvm::errs() << "[+] visitStmt VisitCastExpr done\n";
// mEnv->cast(expr);
// }
// process CallExpr,e.g. function call
virtual void VisitCallExpr(CallExpr * call) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit CallExpr\n";
VisitStmt(call);
mEnv->call(call);
if (FunctionDecl *callee = call->getDirectCallee()){
if ((!callee->getName().equals("GET")) && (!callee->getName().equals("PRINT")) &&
(!callee->getName().equals("MALLOC")) && (!callee->getName().equals("FREE"))){
Stmt *body=callee->getBody();
if(body && isa<CompoundStmt>(body) )
{
//visit the function body
Visit(body);
int64_t retvalue = mEnv->getReturn();
mEnv->mStack_pop_back();
mEnv->mStack_bindStmt(call, retvalue);
}
}
}
}
virtual void VisitIfStmt(IfStmt *ifstmt) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit IfStmt\n";
//get the condition expr and visit relevant node in ast
Expr *expr=ifstmt->getCond();
Visit(expr);
//cout<<expr->getStmtClassName()<<endl;
//BinaryOperator * bop = dyn_cast<BinaryOperator>(expr);
//get the bool value of condition expr
bool cond=mEnv->getcond(expr);
//if condition value is true, visit then block,else visit else block
if(cond)
{
Visit(ifstmt->getThen()); // must use the Visit, not the VisitStmt
}
else
{
Stmt *else_block=ifstmt->getElse();
//if else block really exists
if(else_block)
Visit(else_block);
}
}
//process WhileStmt
virtual void VisitWhileStmt(WhileStmt *whilestmt) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit WhileStmt\n";
//get the condition expr of WhileStmt in ast,and visit relevant node
Expr *expr = whilestmt->getCond();
Visit(expr);
//BinaryOperator *bop = dyn_cast<BinaryOperator>(expr);
//get the condition value of WhileStmt,if it is true, visit the body of WhileStmt
bool cond=mEnv->getcond(expr);
Stmt *body=whilestmt->getBody();
while(cond)
{
if( body && isa<CompoundStmt>(body) )
{
VisitStmt(whilestmt->getBody());
}
//update the condition value
Visit(expr);
cond=mEnv->getcond(expr);
}
}
//process ForStmt
//https://clang.llvm.org/doxygen/Stmt_8h_source.html#l2451
virtual void VisitForStmt(ForStmt *forstmt){
// llvm::errs() << "[+] visit VisitForStmt\n";
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit forstmt\n";
if(Stmt *init = forstmt->getInit()){
Visit(init);
}
for(; mEnv->Expr_GetVal(forstmt->getCond()); Visit(forstmt->getInc())){ //getCond 返回值是bool,而不是void,不能使用Visit,只能直接利用expr对该语句进行解析from ‘void’ to ‘bool’
Stmt *body=forstmt->getBody();
if(body && isa<CompoundStmt>(body)){
Visit(forstmt->getBody());
}
}
}
// process return stmt.
virtual void VisitReturnStmt(ReturnStmt *returnStmt)
{
//isCurFunctionReturn
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit ReturnStmt\n";
Visit(returnStmt->getRetValue());
mEnv->returnstmt(returnStmt);
}
// process DeclStmt,e.g. int a; int a=c+d; and etc.
virtual void VisitDeclStmt(DeclStmt * declstmt) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit DeclStmt\n";
mEnv->decl(declstmt);
}
//process UnaryExprOrTypeTraitExpr, e.g. sizeof and etc.
virtual void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *uop)
{
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit VisitUnaryExprOrTypeTraitExpr\n";
VisitStmt(uop);
mEnv->unarysizeof(uop);
}
virtual void VisitParenExpr(ParenExpr * pexpr) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit VisitParenExpr\n";
VisitStmt(pexpr);
mEnv->parenexpr(pexpr);
}
//process UnaryOperator, e.g. -, * and etc.
virtual void VisitUnaryOperator (UnaryOperator * uop) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit VisitUnaryOperator\n";
VisitStmt(uop);
mEnv->unaryop(uop);
}
virtual void VisitCastExpr(CastExpr * expr) {
if(mEnv->haveReturn()){
return;
}
llvm::errs() << "[+] visit VisitCastExpr\n";
VisitStmt(expr);
mEnv->cast(expr);
}
virtual void VisitArraySubscriptExpr(ArraySubscriptExpr *ase) {
VisitStmt(ase);
mEnv->arrayexpr(ase);
}
// process ArraySubscriptExpr, e.g. int [2]
// virtual void VisitArraySubscriptExpr(ArraySubscriptExpr *arrayexpr)
// {
// if(mEnv->haveReturn()){
// return;
// }
// // llvm::errs() << "[+] visit VisitArraySubscriptExpr\n";
// return ;
// // VisitStmt(arrayexpr);
// // mEnv->array(arrayexpr);
// }
private:
Environment * mEnv;
};
class InterpreterConsumer : public ASTConsumer {
public:
explicit InterpreterConsumer(const ASTContext& context) : mEnv(),
mVisitor(context, &mEnv) {
}
virtual ~InterpreterConsumer() {}
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
TranslationUnitDecl * decl = Context.getTranslationUnitDecl();
mEnv.init(decl);
FunctionDecl * entry = mEnv.getEntry();
mVisitor.VisitStmt(entry->getBody());
}
private:
Environment mEnv;
InterpreterVisitor mVisitor;
};
class InterpreterClassAction : public ASTFrontendAction {
public:
virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
return std::unique_ptr<clang::ASTConsumer>(
new InterpreterConsumer(Compiler.getASTContext()));
}
};
int main (int argc, char ** argv) {
if (argc > 1) {
clang::tooling::runToolOnCode(std::unique_ptr<clang::FrontendAction>(new InterpreterClassAction), argv[1]);
}
}