KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > transformations > ForLoopCreator


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.toolkits.base.AST.transformations;
21
22 import soot.*;
23 import java.util.*;
24 import soot.dava.*;
25 import soot.dava.internal.SET.*;
26 import soot.dava.internal.AST.*;
27 import soot.dava.toolkits.base.AST.analysis.*;
28
29
30 /*
31   Nomair A. Naeem 21-FEB-2005
32
33   In the depthFirstAdaptor children of a ASTNode
34   are gotten in three ways
35   a, ASTStatementSequenceNode uses one way see caseASTStatementSequenceNode in DepthFirstAdapter
36   b, ASTTryNode uses another way see caseASTTryNode in DepthFirstAdapter
37   c, All other nodes use normalRetrieving method to retrieve the children
38
39   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
40   this is done by overriding the caseASTStatementSequenceNode
41
42 */

43
44 public class ForLoopCreator extends DepthFirstAdapter{
45
46     public ForLoopCreator(){
47     }
48
49     public ForLoopCreator(boolean verbose){
50     super(verbose);
51     }
52
53     
54     public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){
55     }
56
57     /*
58       Note the ASTNode in this case can be any of the following:
59
60       ASTMethodNode ASTSwitchNode ASTIfNode ASTIfElseNode
61       ASTUnconditionalWhileNode ASTWhileNode ASTDoWhileNode ASTForLoopNode
62       ASTLabeledBlockNode ASTSynchronizedBlockNode
63     */

64     public void normalRetrieving(ASTNode node){
65     if(node instanceof ASTSwitchNode){
66         dealWithSwitchNode((ASTSwitchNode)node);
67         return;
68     }
69
70     //from the Node get the subBodes
71
Iterator sbit = node.get_SubBodies().iterator();
72
73     //onlyASTIfElseNode has 2 subBodies but we need to deal with that
74
int subBodyNumber=0;
75     while (sbit.hasNext()) {
76         List subBody = (List)sbit.next();
77         Iterator it = subBody.iterator();
78
79         int nodeNumber=0;
80         //go over the ASTNodes in this subBody and apply
81
while (it.hasNext()){
82         ASTNode temp = (ASTNode) it.next();
83         if(temp instanceof ASTStatementSequenceNode){
84             //need to check if this is followed by a while node
85
if(it.hasNext()){
86             //there is a next node
87
ASTNode temp1 = (ASTNode)subBody.get(nodeNumber+1);
88             if(temp1 instanceof ASTWhileNode){
89                 //a statement sequence node followed by a while node
90

91                 ForLoopCreationHelper helper =
92                 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1);
93
94                 if(helper.checkPattern()){
95                 //pattern matched
96

97                 List newBody = helper.createNewBody(subBody,nodeNumber);
98                 if(newBody!= null){
99                     if(node instanceof ASTIfElseNode){
100                     if(subBodyNumber==0){
101                         //the if body was modified
102
List subBodies = node.get_SubBodies();
103                         List ifElseBody = (List)subBodies.get(1);
104                         ((ASTIfElseNode)node).replaceBody(newBody,ifElseBody);
105                         G.v().ASTTransformations_modified = true;
106                         //System.out.println("FOR LOOP CREATED");
107
return;
108                     }
109                     else if(subBodyNumber==1){
110                         //else body was modified
111
List subBodies = node.get_SubBodies();
112                         List ifBody = (List)subBodies.get(0);
113                         ((ASTIfElseNode)node).replaceBody(ifBody,newBody);
114                         G.v().ASTTransformations_modified = true;
115                         //System.out.println("FOR LOOP CREATED");
116
return;
117                     }
118                     else{
119                         throw new RuntimeException JavaDoc("Please report benchmark to programmer.");
120                     }
121                     }
122                     else{
123                     if(node instanceof ASTMethodNode){
124                         ((ASTMethodNode)node).replaceBody(newBody);
125                         G.v().ASTTransformations_modified = true;
126                         //System.out.println("FOR LOOP CREATED");
127
return;
128                         
129                     }
130                     else if(node instanceof ASTSynchronizedBlockNode){
131                         ((ASTSynchronizedBlockNode)node).replaceBody(newBody);
132                         G.v().ASTTransformations_modified = true;
133                         //System.out.println("FOR LOOP CREATED");
134
return;
135                     }
136                     else if(node instanceof ASTLabeledBlockNode){
137                         ((ASTLabeledBlockNode)node).replaceBody(newBody);
138                         G.v().ASTTransformations_modified = true;
139                         //System.out.println("FOR LOOP CREATED");
140
return;
141                     }
142                     else if(node instanceof ASTUnconditionalLoopNode){
143                         ((ASTUnconditionalLoopNode)node).replaceBody(newBody);
144                         G.v().ASTTransformations_modified = true;
145                         //System.out.println("FOR LOOP CREATED");
146
return;
147                     }
148                     else if(node instanceof ASTIfNode){
149                         ((ASTIfNode)node).replaceBody(newBody);
150                         G.v().ASTTransformations_modified = true;
151                         //System.out.println("FOR LOOP CREATED");
152
return;
153                     }
154                     else if(node instanceof ASTWhileNode){
155                         ((ASTWhileNode)node).replaceBody(newBody);
156                         G.v().ASTTransformations_modified = true;
157                         //System.out.println("FOR LOOP CREATED");
158
return;
159                     }
160                     else if(node instanceof ASTDoWhileNode){
161                         ((ASTDoWhileNode)node).replaceBody(newBody);
162                         G.v().ASTTransformations_modified = true;
163                         //System.out.println("FOR LOOP CREATED");
164
return;
165                     }
166                     else if(node instanceof ASTForLoopNode){
167                         ((ASTForLoopNode)node).replaceBody(newBody);
168                         G.v().ASTTransformations_modified = true;
169                         //System.out.println("FOR LOOP CREATED");
170
return;
171                     }
172                     else {
173                        throw new RuntimeException JavaDoc("Please report benchmark to programmer.");
174                     }
175                     }
176                 }//newBody was not null
177
}//for loop creation pattern matched
178
}//the next node was a whilenode
179
}//there is a next node
180
}//temp is a stmtSeqNode
181
temp.apply(this);
182         nodeNumber++;
183         }//end of going over nodes
184
subBodyNumber++;
185     }//end of going over subBodies
186
}
187
188     public void caseASTTryNode(ASTTryNode node){
189     inASTTryNode(node);
190
191     //get try body
192
List tryBody = node.get_TryBody();
193     Iterator it = tryBody.iterator();
194
195     int nodeNumber=0;
196     //go over the ASTNodes and apply
197
while (it.hasNext()){
198         ASTNode temp = (ASTNode) it.next();
199         if(temp instanceof ASTStatementSequenceNode){
200         //need to check if this is followed by a while node
201
if(it.hasNext()){
202             //there is a next node
203
ASTNode temp1 = (ASTNode)tryBody.get(nodeNumber+1);
204             if(temp1 instanceof ASTWhileNode){
205             //a statement sequence node followed by a while node
206

207             ForLoopCreationHelper helper =
208                 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1);
209                 
210             if(helper.checkPattern()){
211                 //pattern matched
212

213                 List newBody = helper.createNewBody(tryBody,nodeNumber);
214                 if(newBody!= null){
215                 //something did not go wrong
216
node.replaceTryBody(newBody);
217                 G.v().ASTTransformations_modified = true;
218                 //System.out.println("FOR LOOP CREATED");
219
return;
220                 }//newBody was not null
221
}//for loop creation pattern matched
222
}//the next node was a whilenode
223
}//there is a next node
224
}//temp is a stmtSeqNode
225
temp.apply(this);
226         nodeNumber++;
227     }//end of while going through tryBody
228

229
230
231
232     Map exceptionMap = node.get_ExceptionMap();
233     Map paramMap = node.get_ParamMap();
234     //get catch list and apply on the following
235
// a, type of exception caught
236
// b, local of exception
237
// c, catchBody
238
List catchList = node.get_CatchList();
239     Iterator itBody=null;
240         it = catchList.iterator();
241     while (it.hasNext()) {
242         ASTTryNode.container catchBody = (ASTTryNode.container)it.next();
243         
244         SootClass sootClass = ((SootClass)exceptionMap.get(catchBody));
245         Type type = sootClass.getType();
246         
247         //apply on type of exception
248
caseType(type);
249
250         //apply on local of exception
251
Local local = (Local)paramMap.get(catchBody);
252         decideCaseExprOrRef(local);
253
254         //apply on catchBody
255
List body = (List)catchBody.o;
256         itBody = body.iterator();
257
258         nodeNumber=0;
259         //go over the ASTNodes and apply
260
while (itBody.hasNext()){
261         ASTNode temp = (ASTNode) itBody.next();
262
263         if(temp instanceof ASTStatementSequenceNode){
264             //need to check if this is followed by a while node
265
if(itBody.hasNext()){
266             //there is a next node
267
ASTNode temp1 = (ASTNode)body.get(nodeNumber+1);
268             if(temp1 instanceof ASTWhileNode){
269                 //a statement sequence node followed by a while node
270

271                 ForLoopCreationHelper helper =
272                 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1);
273                 
274                 if(helper.checkPattern()){
275                 //pattern matched
276

277                 List newBody = helper.createNewBody(body,nodeNumber);
278                 if(newBody!= null){
279                     //something did not go wrong
280
catchBody.replaceBody(newBody);
281                     G.v().ASTTransformations_modified = true;
282                     //System.out.println("FOR LOOP CREATED");
283
return;
284                 }//newBody was not null
285
}//for loop creation pattern matched
286
}//the next node was a whilenode
287
}//there is a next node
288
}//temp is a stmtSeqNode
289

290         temp.apply(this);
291         nodeNumber++;
292         }
293     }
294     
295     outASTTryNode(node);
296     }
297
298
299     private void dealWithSwitchNode(ASTSwitchNode node){
300     //do a depthfirst on elements of the switchNode
301

302     List indexList = node.getIndexList();
303     Map index2BodyList = node.getIndex2BodyList();
304
305     Iterator it = indexList.iterator();
306     while (it.hasNext()) {//going through all the cases of the switch statement
307
Object JavaDoc currentIndex = it.next();
308         List body = (List) index2BodyList.get( currentIndex);
309         
310         if (body != null){
311         //this body is a list of ASTNodes
312

313         Iterator itBody = body.iterator();
314         int nodeNumber=0;
315         //go over the ASTNodes and apply
316
while (itBody.hasNext()){
317             ASTNode temp = (ASTNode) itBody.next();
318
319             if(temp instanceof ASTStatementSequenceNode){
320             //need to check if this is followed by a while node
321
if(itBody.hasNext()){
322                 //there is a next node
323
ASTNode temp1 = (ASTNode)body.get(nodeNumber+1);
324                 if(temp1 instanceof ASTWhileNode){
325                 //a statement sequence node followed by a while node
326

327                 ForLoopCreationHelper helper =
328                     new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1);
329                 
330                 if(helper.checkPattern()){
331                     //pattern matched
332

333                     List newBody = helper.createNewBody(body,nodeNumber);
334                     if(newBody!= null){
335                     //something did not go wrong
336

337                     //put this body in the Map
338
index2BodyList.put(currentIndex,newBody);
339                     //replace in actual switchNode
340
node.replaceIndex2BodyList(index2BodyList);
341
342                     G.v().ASTTransformations_modified = true;
343                     //System.out.println("FOR LOOP CREATED");
344
return;
345                     }//newBody was not null
346
}//for loop creation pattern matched
347
}//the next node was a whilenode
348
}//there is a next node
349
}//temp is a stmtSeqNode
350
temp.apply(this);
351             nodeNumber++;
352         }
353         }
354     }
355     }
356 }
Popular Tags