KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
33   Nomair A. Naeem 03-MARCH-2005
34
35 PATTERN 1:
36   if(cond1){ if(cond1){
37      break label_1 break label_1
38   } -----> }
39   else{ Body1
40     Body1
41   }
42
43
44 PATTERN 2:
45   if(!cond1){ if(cond1){
46      Body1 break label_1
47   } -----> }
48   else{ Body1
49     break label_1
50   }
51
52   The assumption is that if cond1 is true there is an abrupt edge and Body1
53   will not be executed. So this works only if the else in the original
54   has an abrupt control flow. This can be break/continue/return
55
56
57   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
58   this is done by overriding the caseASTStatementSequenceNode
59 */

60 public class IfElseBreaker{
61     ASTIfNode newIfNode;
62     List remainingBody;
63
64     public IfElseBreaker(){
65     newIfNode=null;
66     remainingBody=null;
67     }
68
69     public boolean isIfElseBreakingPossiblePatternOne(ASTIfElseNode node){
70     List ifBody = node.getIfBody();
71     if(ifBody.size()!=1){
72         //we are only interested if size is one
73
return false;
74     }
75     
76     ASTNode onlyNode=(ASTNode)ifBody.get(0);
77     boolean check = checkStmt(onlyNode,node);
78     if(!check){
79         return false;
80     }
81
82     //breaking is possible
83
//break and store
84
newIfNode = new ASTIfNode(((ASTLabeledNode)node).get_Label(),node.get_Condition(),ifBody);
85     remainingBody = node.getElseBody();
86     
87     return true;
88     }
89
90
91     public boolean isIfElseBreakingPossiblePatternTwo(ASTIfElseNode node){
92     List elseBody = node.getElseBody();
93     if(elseBody.size()!=1){
94         //we are only interested if size is one
95
return false;
96     }
97     
98     ASTNode onlyNode=(ASTNode)elseBody.get(0);
99     boolean check = checkStmt(onlyNode,node);
100     if(!check){
101         return false;
102     }
103     //breaking is possible
104

105     ASTCondition cond = node.get_Condition();
106     //flip
107
cond.flip();
108
109     newIfNode = new ASTIfNode(((ASTLabeledNode)node).get_Label(),cond,elseBody);
110     remainingBody = node.getIfBody();
111     
112     return true;
113     }
114
115
116
117
118     private boolean checkStmt(ASTNode onlyNode,ASTIfElseNode node){
119     if(!(onlyNode instanceof ASTStatementSequenceNode)){
120         //only interested in StmtSeq nodes
121
return false;
122     }
123     
124     ASTStatementSequenceNode stmtNode=(ASTStatementSequenceNode)onlyNode;
125     List statements = stmtNode.getStatements();
126     if(statements.size()!=1){
127         //need one stmt only
128
return false;
129     }
130     
131     AugmentedStmt as = (AugmentedStmt)statements.get(0);
132     Stmt stmt = as.get_Stmt();
133
134     if(!(stmt instanceof DAbruptStmt)){
135         //interested in abrupt stmts only
136
return false;
137     }
138     DAbruptStmt abStmt = (DAbruptStmt)stmt;
139     if(!(abStmt.is_Break() || abStmt.is_Continue())){
140         //interested in breaks and continues only
141
return false;
142     }
143
144     //make sure that the break is not that of the if
145
//unliekly but good to check
146
SETNodeLabel ifLabel = ((ASTLabeledNode)node).get_Label();
147     
148     if (ifLabel!=null){
149         if(ifLabel.toString()!=null){
150         if(abStmt.is_Break()){
151             String JavaDoc breakLabel = abStmt.getLabel().toString();
152             if(breakLabel!=null){
153             if(breakLabel.compareTo(ifLabel.toString())==0){
154                 //is a break of this label
155
return false;
156             }
157             }
158         }
159         }
160     }
161     return true;
162     }
163
164
165
166
167
168     /*
169       The purpose of this method is to replace the ASTIfElseNode
170       given by the var nodeNumber with the new ASTIfNode
171       and to add the remianing list of bodies after this ASTIfNode
172
173       The new body is then returned;
174
175     */

176     public List createNewBody(List oldSubBody, int nodeNumber){
177     if(newIfNode == null)
178         return null;
179
180     List newSubBody = new ArrayList();
181
182     if(oldSubBody.size()<= nodeNumber){
183         //something is wrong since the oldSubBody has lesser nodes than nodeNumber
184
return null;
185     }
186
187     Iterator oldIt = oldSubBody.iterator();
188     int index=0;
189     while(index!=nodeNumber){
190         newSubBody.add(oldIt.next());
191         index++;
192     }
193
194     //check to see that the next is an ASTIfElseNode
195
ASTNode temp = (ASTNode)oldIt.next();
196     if(!(temp instanceof ASTIfElseNode))
197         return null;
198     
199     newSubBody.add(newIfNode);
200
201     newSubBody.addAll(remainingBody);
202
203
204     //copy any remaining nodes
205
while(oldIt.hasNext()){
206         newSubBody.add(oldIt.next());
207     }
208     
209     return newSubBody;
210     }
211 }
212
Popular Tags