KickJava   Java API By Example, From Geeks To Geeks.

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


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.jimple.*;
25 import soot.dava.internal.SET.*;
26 import soot.dava.internal.AST.*;
27 import soot.dava.internal.asg.*;
28 import soot.dava.internal.javaRep.*;
29 import soot.dava.toolkits.base.AST.analysis.*;
30 /*
31   Nomair A. Naeem 18-FEB-2005
32
33     PATTERN ONE:
34        label_1:
35            while(cond1){
36                if(cond2){ while(cond1 && !cond2){
37                    Body
38                } }
39            } //if body is a break label_1
40     
41
42         PATTERN TWO:
43           lable_1:
44               while(true){ while(!cond){
45                 if(cond){ }
46                     Body Body(minus the break)
47                 }
48               }
49               This pattern can be applied only if
50           1: the loop is an UnconditionalLoopNode
51           2, The body should be a statement sequence (could do the general
52              case but its too complex
53           2, The body contains an abrupt edge out of the loop
54
55   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
56   this is done by overriding the caseASTStatementSequenceNode
57 */

58
59 public class StrengthenByIf{
60     /*
61       We know this method is called when there is a while node which has a body
62       consisting entirely of one ASTIfNode
63     */

64     public static List getNewNode(ASTNode loopNode, ASTIfNode ifNode){
65     List ifBody = ifNode.getIfBody();
66     String JavaDoc label = isItOnlyBreak(ifBody);
67     if(label != null){
68         //only one break statement and it is breaking some label
69

70         //make sure its breaking the label on the loop
71
if(((ASTLabeledNode)loopNode).get_Label().toString()!=null){
72
73         if(((ASTLabeledNode)loopNode).get_Label().toString().compareTo(label)==0){
74             //the if has a single break breaking the loop
75
//pattern 1 matched
76

77             if(loopNode instanceof ASTWhileNode){
78             ASTCondition outerCond = ((ASTWhileNode)loopNode).get_Condition();
79             //flip the inner condition
80
ASTCondition innerCond = ifNode.get_Condition();
81             innerCond.flip();
82             //aggregate the two conditions
83
ASTCondition newCond = new ASTAndCondition(outerCond,innerCond);
84             //make empty body
85
List newWhileBody = new ArrayList();
86             //SETNodeLabel newLabel = ((ASTWhileNode)loopNode).get_Label();
87

88             // dont need any label name since the body of the while is empty
89
SETNodeLabel newLabel = new SETNodeLabel();
90
91             //make new ASTWhileNode
92
List toReturn = new ArrayList();
93             toReturn.add(new ASTWhileNode(newLabel,newCond,newWhileBody));
94             return toReturn;
95             
96             }
97             else if(loopNode instanceof ASTDoWhileNode){
98             /*
99               What to do when the ASTDoWhileNode only has one
100               body which is a break of the whileNode???
101             */

102             return null;
103             }
104             else if (loopNode instanceof ASTUnconditionalLoopNode){
105             /*
106               An UnconditionalLoopNode has a single If Condition
107               which breaks the loop
108               In this case
109               Create an ASTWhileLoop Node with the flipped Condition
110               of the If statement
111             */

112
113             //flip the inner condition
114
ASTCondition innerCond = ifNode.get_Condition();
115             innerCond.flip();
116
117             //make empty body
118
List newWhileBody = new ArrayList();
119             //SETNodeLabel newLabel = ((ASTUnconditionalLoopNode)loopNode).get_Label();
120

121             // dont need any label name since the body of the while is empty
122
SETNodeLabel newLabel = new SETNodeLabel();
123             
124             //make new ASTWhileNode
125
List toReturn = new ArrayList();
126             toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody));
127             return toReturn;
128             }
129         }//if the labels match
130
}
131     }//the first Pattern was a match
132
else if(loopNode instanceof ASTUnconditionalLoopNode && ifBody.size()==1){
133         //try the UnconditionalLoopNode pattern
134

135         //we need one stmtSeq Node
136
ASTNode tempNode = (ASTNode)ifBody.get(0);
137         if(tempNode instanceof ASTStatementSequenceNode){
138         //a stmtSeq
139
List statements = ((ASTStatementSequenceNode)tempNode).getStatements();
140         Iterator stIt = statements.iterator();
141         while(stIt.hasNext()){
142             AugmentedStmt as = (AugmentedStmt)stIt.next();
143             Stmt stmt = as.get_Stmt();
144             if(stmt instanceof DAbruptStmt && !(stIt.hasNext())){
145             //this is an abrupt stmt and the last stmt
146
DAbruptStmt abStmt = (DAbruptStmt)stmt;
147             if(abStmt.is_Break()){
148                 //last statement and that too a break
149
String JavaDoc loopLabel = ((ASTLabeledNode)loopNode).get_Label().toString();
150                 String JavaDoc breakLabel= abStmt.getLabel().toString();
151                 if(loopLabel != null && breakLabel!=null){
152                 if(loopLabel.compareTo(breakLabel)==0){
153                     
154                     //pattern matched
155
//flip the inner condition
156
ASTCondition innerCond = ifNode.get_Condition();
157                     innerCond.flip();
158                     
159                     //make empty body
160
List newWhileBody = new ArrayList();
161                     SETNodeLabel newLabel = ((ASTUnconditionalLoopNode)loopNode).get_Label();
162                     
163                     //make new ASTWhileNode
164
List toReturn = new ArrayList();
165                     toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody));
166                     
167                     // Add the statementSequenceNode AFTER the whileNode except for the laststmt
168
Iterator tempIt = statements.iterator();
169                     List newStmts = new ArrayList();
170                     while(tempIt.hasNext()){
171                     Object JavaDoc tempStmt = tempIt.next();
172                     if(tempIt.hasNext()){
173                         newStmts.add(tempStmt);
174                     }
175                     }
176                     toReturn.add(new ASTStatementSequenceNode(newStmts));
177                     return toReturn;
178                 }//labels are the same
179
}//labels are non null
180
}//is a break stmt
181
}
182             else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt){
183             if(!(stIt.hasNext())){
184                 //return obj/return;
185
//flip cond
186
ASTCondition innerCond = ifNode.get_Condition();
187                 innerCond.flip();
188                 
189                 //make empty body
190
List newWhileBody = new ArrayList();
191                 //SETNodeLabel newLabel = ((ASTUnconditionalLoopNode)loopNode).get_Label();
192

193
194                 // dont need any label name since the body of the while is empty
195
SETNodeLabel newLabel = new SETNodeLabel();
196
197                 //make new ASTWhileNode
198
List toReturn = new ArrayList();
199                 toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody));
200                 
201                 // Add the statementSequenceNode AFTER the whileNode except for the laststmt
202
Iterator tempIt = statements.iterator();
203                 List newStmts = new ArrayList();
204                 while(tempIt.hasNext()){
205                 newStmts.add(tempIt.next());
206                 }
207                 toReturn.add(new ASTStatementSequenceNode(newStmts));
208                 return toReturn;
209             }
210             }
211         }//end of going through statements
212
}//end of stmtSEq node
213
}//end of else if
214
return null;
215     }//end of method
216

217
218
219
220     /*
221       Given a body of a node the method checks for the following:
222       1, the body has only one node
223       2, the node is a statementSequenceNode
224       3, There is only one statement in the stmt seq node
225       4, the stmt is a break stmt
226
227       If the conditions are true the label of the break stmt is returned
228       otherwise null is returned
229     */

230     private static String JavaDoc isItOnlyBreak(List body){
231     if(body.size()!=1){
232         //this is more than one we need one stmtSeq Node
233
return null;
234     }
235     ASTNode tempNode = (ASTNode)body.get(0);
236     if(!(tempNode instanceof ASTStatementSequenceNode)){
237         //not a stmtSeq
238
return null;
239     }
240       
241     List statements = ((ASTStatementSequenceNode)tempNode).getStatements();
242     if(statements.size()!=1){
243         //we need one break
244
return null;
245     }
246     AugmentedStmt as = (AugmentedStmt)statements.get(0);
247     Stmt stmt = as.get_Stmt();
248     if(!(stmt instanceof DAbruptStmt)){
249         //this is not a break stmt
250
return null;
251     }
252     DAbruptStmt abStmt = (DAbruptStmt)stmt;
253     if(!(abStmt.is_Break())){
254         //we need a break
255
return null;
256     }
257     return abStmt.getLabel().toString();
258     }
259 }
260
261
262
Popular Tags