-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPITypeCheckingConsumer.cpp
More file actions
293 lines (193 loc) · 6.96 KB
/
MPITypeCheckingConsumer.cpp
File metadata and controls
293 lines (193 loc) · 6.96 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
280
281
282
283
284
285
286
287
288
289
#include "MPITypeCheckingConsumer.h"
using namespace clang;
using namespace std;
using namespace llvm;
int labelIndex=0;
MPITypeCheckingConsumer::MPITypeCheckingConsumer(CompilerInstance *ci, int numOfProc){
init(ci,numOfProc);
}
MPITypeCheckingConsumer::MPITypeCheckingConsumer(CompilerInstance *ci) {
init(ci, N);
}
void MPITypeCheckingConsumer::init(CompilerInstance *ci, int numOfProc){
this->ci=ci;
this->visitStart=false;
this->numOfProcs=numOfProc;
this->setOfWorldCommGroup.insert(WORLD);
this->commManager=new CommManager(ci, numOfProc);
this->mpiTree=new MPITree(new MPINode(new CommNode(ST_NODE_ROOT,Condition(true))));
this->mpiSimulator=new MPISimulator(this->commManager,this->mpiTree);
this->protocolGen=new ProtocolGenerator(this->mpiTree,this->commManager->getParamRoleMapping());
}
bool MPITypeCheckingConsumer::HandleTopLevelDecl(DeclGroupRef d)
{
DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++)
{
this->TraverseDecl(*it);
if(isa<VarDecl>(*it)){
VarDecl *var=cast<VarDecl>(*it);
string varName=var->getDeclName().getAsString();
cout<<"Find the var "<<varName<<endl;
this->commManager->insertVarName(varName);
}
if(isa<FunctionDecl>(*it)){
FunctionDecl *func=cast<FunctionDecl>(*it);
if(func->isMain()){
this->mainFunc=func;
}
}
}
return true;
}
void MPITypeCheckingConsumer::HandleTranslationUnit(ASTContext &Ctx) {
cout<<"all the parts have been parsed!"<<endl;
int numOfErrs=ci->getDiagnosticClient().getNumErrors();
if(numOfErrs>0){
throw exception();
}
this->visitStart=true;
this->VisitFunctionDecl(this->mainFunc);
//after the main function has been visited,
//the comm tree and roles will have been constructed,
//and the traversal of the comm tree can start
this->mpiSimulator->simulate();
this->protocolGen->generateTheProtocols();
}
bool MPITypeCheckingConsumer::checkWhetherTheDeclHasBeenVisitedBefore(FunctionDecl *decl){
list<string>::iterator findIt=find(funcsList.begin(),funcsList.end(),decl->getQualifiedNameAsString());
if(findIt!=funcsList.end())
return true;
else
return false;
}
void MPITypeCheckingConsumer::analyzeDecl(FunctionDecl *funcDecl){
if (this->checkWhetherTheDeclHasBeenVisitedBefore(funcDecl))
{
string errInfo= "The decl "+decl2str(&ci->getSourceManager(),ci->getLangOpts(),funcDecl)
+ " has already started in the past and has NOT finished yet! A possible deadlock is detected. Program analysis stops here.";
throw new MPI_TypeChecking_Error(errInfo);
}
//analyze the body of the function
/***********************************************************************/
if(funcDecl->hasBody()){
this->funcsList.push_back(funcDecl->getQualifiedNameAsString());
cout<<"add "<<funcDecl->getQualifiedNameAsString()<<" to list "<<endl;
}
this->VisitFunctionDecl(funcDecl);
}
void MPITypeCheckingConsumer::removeFuncFromList(){
if(!this->funcsList.empty()){
string popped=this->funcsList.back();
cout<<"The decl with name "<<popped<<" is going to be removed."<<endl;
this->funcsList.pop_back();
}
}
string MPITypeCheckingConsumer::getVarInIncExpr(Expr *inc){
if (isa<UnaryOperator>(inc)){
UnaryOperator *uOP=cast<UnaryOperator>(inc);
return expr2str(&ci->getSourceManager(),ci->getLangOpts(),uOP->getSubExpr());
}
if (isa<BinaryOperator>(inc))
{
BinaryOperator *binOP=cast<BinaryOperator>(inc);
Expr *lhs=binOP->getLHS();
return expr2str(&ci->getSourceManager(),ci->getLangOpts(),lhs);
}
return "";
}
bool MPITypeCheckingConsumer::isChangingByOneUnit(Expr *inc){
if (isa<UnaryOperator>(inc)){
UnaryOperator *uOP=cast<UnaryOperator>(inc);
return uOP->isIncrementDecrementOp();
}
if (isa<BinaryOperator>(inc))
{
BinaryOperator *binOP=cast<BinaryOperator>(inc);
Expr *lhs=binOP->getLHS();
Expr *rhs=binOP->getRHS();
string op=binOP->getOpcodeStr();
if(binOP->isCompoundAssignmentOp()){
if(op!="+=" && op!="-=")
return false;
APSInt result;
if(rhs->EvaluateAsInt(result,ci->getASTContext())){
int num=atoi(result.toString(10).c_str());
if(num==1)
return true;
}
}
}
return false;
}
void MPITypeCheckingConsumer::handleLoop(){
CommNode *theLoopNode=this->mpiSimulator->getCurNode();
CommNode *curP=theLoopNode->getParent();
if (curP->isMaster())
{
theLoopNode->setInfo("LOOP_"+convertIntToStr(labelIndex++));
theLoopNode->setMaster();
MPINode *theLoopMPINode;
if (theLoopNode->getNodeType()==ST_NODE_FOREACH)
theLoopMPINode=new MPIForEachNode((ForEachNode*)theLoopNode);
else
theLoopMPINode=new MPINode(theLoopNode);
this->mpiTree->insertNode(theLoopMPINode);
this->mpiSimulator->insertPosAndMPINodeTuple(theLoopNode->getPosIndex(),theLoopMPINode);
}
}
string decl2str(SourceManager *sm, LangOptions lopt,Decl *d) {
clang::SourceLocation b(d->getLocStart()), _e(d->getLocEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, *sm, lopt));
return std::string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}
string decl2str(SourceManager *sm, LangOptions lopt,const Decl *d) {
clang::SourceLocation b(d->getLocStart()), _e(d->getLocEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, *sm, lopt));
return std::string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}
string stmt2str(SourceManager *sm, LangOptions lopt,Stmt *stmt) {
SourceLocation b(stmt->getLocStart()), _e(stmt->getLocEnd());
SourceLocation e(Lexer::getLocForEndOfToken(_e, 0, *sm, lopt));
return string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}
string stmt2str(SourceManager *sm, LangOptions lopt,const Stmt *stmt) {
SourceLocation b(stmt->getLocStart()), _e(stmt->getLocEnd());
SourceLocation e(Lexer::getLocForEndOfToken(_e, 0, *sm, lopt));
return string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}
string expr2str(SourceManager *sm, LangOptions lopt,clang::Expr *expr){
SourceLocation b(expr->getLocStart()), _e(expr->getLocEnd());
SourceLocation e(Lexer::getLocForEndOfToken(_e, 0, *sm, lopt));
const char* endChar= sm->getCharacterData(e);
int offset=0;
endChar--;
while(*endChar!=',' && *endChar!='(' && *endChar!=';'
&& *endChar!='='){
offset++;
endChar--;
}
string out=string(endChar+1,offset);
return delSpaces(out);
}
string delSpaces(string &str){
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
str.erase(std::remove(str.begin(), str.end(), '\t'), str.end());
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
return str;
}
void checkIdTable(clang::CompilerInstance *ci){
clang::IdentifierTable *idTable =&(ci->getASTContext().Idents);
clang::IdentifierTable::iterator it;
for (it=idTable->begin(); it!=idTable->end(); ++it){
clang::StringRef str=it->getKey();
cout << "the id is: "<<str.str() << endl;
clang::IdentifierInfo *idInfo;
idInfo = &idTable->get(str);
//cout << "the len is "<< idInfo->getLength()<<" and "<< (idInfo->getTokenID()) <<endl;
}
}