KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
32   Nomair A. Naeem 18-FEB-2005
33
34     PATTERN ONE:
35        label_1:
36            while(cond1){ label_1:
37                if(cond2){ while(cond1 && cond2){
38                    Body 1 Body1
39                } }
40            else{
41              break label_1
42            }
43            }
44     
45        The important thing is that Body2 should contain an abrupt
46        edge out of the while loop. If Body2 is just a break and nothing
47        else the body2 in the transformed version is empty
48
49
50     PATTERN TWO:
51        label_1:
52            while(true){ label_1:
53                if(cond2){ while(cond2){
54                    Body 1 Body1
55                } }
56            else{ Body2
57                Body 2
58            }
59            }
60
61
62
63   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
64   this is done by overriding the caseASTStatementSequenceNode
65 */

66
67 public class StrengthenByIfElse{
68     /*
69       We know this method is called when there is a loop node which has a body
70       consisting entirely of one ASTIfElseNode
71     */

72     public static List getNewNode(ASTNode loopNode, ASTIfElseNode ifElseNode){
73     //make sure that elsebody has only a stmtseq node
74
List elseBody = ((ASTIfElseNode)ifElseNode).getElseBody();
75     if(elseBody.size()!=1){
76         //this is more than one we need one stmtSeq Node
77
return null;
78     }
79     ASTNode tempNode = (ASTNode)elseBody.get(0);
80     if(!(tempNode instanceof ASTStatementSequenceNode)){
81         //not a stmtSeq
82
return null;
83     }
84
85     List statements = ((ASTStatementSequenceNode)tempNode).getStatements();
86     Iterator stmtIt = statements.iterator();
87     while(stmtIt.hasNext()){
88         AugmentedStmt as = (AugmentedStmt)stmtIt.next();
89         Stmt stmt = as.get_Stmt();
90         if(stmt instanceof DAbruptStmt){
91         //this is a abrupt stmt
92
DAbruptStmt abStmt = (DAbruptStmt)stmt;
93         if(!(abStmt.is_Break())){
94             //we need a break
95
return null;
96         }
97         else{
98             if(stmtIt.hasNext()){
99             //a break should be the last stmt
100
return null;
101             }
102             SETNodeLabel label = abStmt.getLabel();
103             String JavaDoc labelBroken=label.toString();
104             String JavaDoc loopLabel= ((ASTLabeledNode)loopNode).get_Label().toString();
105             if(labelBroken != null && loopLabel != null){//stmt breaks some label
106
if(labelBroken.compareTo(loopLabel)==0){
107                 //we have found a break breaking this label
108

109                 //make sure that if the orignal was an ASTWhileNode then there was
110
//ONLY a break statement
111
if(loopNode instanceof ASTWhileNode){
112                 if(statements.size()!=1){
113                     //more than 1 statement
114
return null;
115                 }
116                 }
117
118
119                 //pattern matched
120
ASTWhileNode newWhileNode = makeWhileNode(ifElseNode,loopNode);
121                 if(newWhileNode == null){
122                 return null;
123                 }
124                 List toReturn = new ArrayList();
125                 toReturn.add(newWhileNode);
126
127                                 
128                 // Add the statementSequenceNode AFTER the whileNode except for the laststmt
129
if(statements.size()!=1){
130                 //size 1 means that the only stmt is a break stmt
131

132                 Iterator tempIt = statements.iterator();
133                 List newStmts = new ArrayList();
134                 while(tempIt.hasNext()){
135                     Object JavaDoc tempStmt = tempIt.next();
136                     if(tempIt.hasNext()){
137                     newStmts.add(tempStmt);
138                     }
139                 }
140                 toReturn.add(new ASTStatementSequenceNode(newStmts));
141                 }
142                 return toReturn;
143
144             }//labels matched
145
}//non null labels
146
}//end of break stmt
147
}//stmt is an abrupt stmt
148
else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt){
149         if(!(loopNode instanceof ASTUnconditionalLoopNode)){
150             //this pattern is only possible for while(true)
151
return null;
152         }
153
154         if(stmtIt.hasNext()){
155             //returns should come in the end
156
return null;
157         }
158
159
160         //pattern matched
161
ASTWhileNode newWhileNode = makeWhileNode(ifElseNode,loopNode);
162         if(newWhileNode == null){
163             return null;
164         }
165         List toReturn = new ArrayList();
166         toReturn.add(newWhileNode);
167
168         // Add the statementSequenceNode AFTER the whileNode
169
Iterator tempIt = statements.iterator();
170         List newStmts = new ArrayList();
171         while(tempIt.hasNext()){
172             Object JavaDoc tempStmt = tempIt.next();
173             newStmts.add(tempStmt);
174         }
175         toReturn.add(new ASTStatementSequenceNode(newStmts));
176         return toReturn;
177         }//if stmt was a return stmt
178
}//going through the stmts
179
return null;
180     }//end of method
181

182
183     private static ASTWhileNode makeWhileNode(ASTIfElseNode ifElseNode, ASTNode loopNode){
184     ASTCondition outerCond = null;
185     ASTCondition innerCond = ifElseNode.get_Condition();
186     ASTCondition newCond = null;
187
188     if(loopNode instanceof ASTWhileNode){
189         outerCond=((ASTWhileNode)loopNode).get_Condition();
190         newCond = new ASTAndCondition(outerCond,innerCond);
191     }
192     else if (loopNode instanceof ASTUnconditionalLoopNode){
193         newCond = innerCond;
194     }
195     else{
196         //not dealing with the case of ASTDoWhileNode
197
return null;
198     }
199     List loopBody = ifElseNode.getIfBody();
200     SETNodeLabel newLabel = ((ASTLabeledNode)loopNode).get_Label();
201                 
202     //make new ASTWhileNode
203
return new ASTWhileNode(newLabel,newCond,loopBody);
204     }
205
206 }//end class
Popular Tags