KickJava   Java API By Example, From Geeks To Geeks.

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


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 soot.jimple.*;
24 import java.util.*;
25 import soot.dava.internal.SET.*;
26 import soot.dava.internal.AST.*;
27 import soot.dava.internal.javaRep.*;
28 import soot.dava.internal.asg.*;
29 import soot.dava.toolkits.base.AST.analysis.*;
30
31
32 /*
33   Nomair A. Naeem 18-FEB-2005
34
35   The class is responsible to do the following transformation on the AST
36   
37   if(A){ if(A && B){
38       if(B){ Body1
39          Body1 -----> }
40       } Body2
41   }
42   Body2
43
44   The most important thing to check is that there is only one If Statement inside the
45   outer if and that there is NO other statement.
46
47   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
48   this is done by overriding the caseASTStatementSequenceNode
49 */

50
51 public class AndAggregator extends DepthFirstAdapter{
52
53     public AndAggregator(){
54     }
55     public AndAggregator(boolean verbose){
56     super(verbose);
57     }
58
59     public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){
60     }
61
62     public void outASTIfNode(ASTIfNode node){
63     List bodies = node.get_SubBodies();
64     if(bodies.size()==1){ //this should always be one since there is only one body of an if statement
65
List body = (List)bodies.get(0);
66         //this is the if body check to see if this is a single if Node
67
if(body.size()==1){//size is good
68
ASTNode bodyNode = (ASTNode)body.get(0);
69         if(bodyNode instanceof ASTIfNode){
70             /*
71               We can do AndAggregation at this point
72               node contains the outer if
73               (ASTIfNode)bodyNode is the inner if
74             */

75
76             ASTCondition outerCond = node.get_Condition();
77             ASTCondition innerCond = ((ASTIfNode)bodyNode).get_Condition();
78
79             SETNodeLabel outerLabel = ((ASTIfNode)node).get_Label();
80             SETNodeLabel innerLabel = ((ASTIfNode)bodyNode).get_Label();
81             
82             SETNodeLabel newLabel = null;
83             if(outerLabel.toString()==null && innerLabel.toString()==null){
84             newLabel = outerLabel;
85             }
86             else if(outerLabel.toString()!=null && innerLabel.toString()==null){
87             newLabel = outerLabel;
88             }
89             else if(outerLabel.toString()==null && innerLabel.toString()!=null){
90             newLabel = innerLabel;
91             }
92             else if(outerLabel.toString()!=null && innerLabel.toString()!=null){
93             newLabel=outerLabel;
94             //however we have to change all occurance of inner label to point to that
95
//of outerlabel now
96
changeUses(outerLabel.toString(),innerLabel.toString(),bodyNode);
97             }
98
99             //aggregate the conditions
100
ASTCondition newCond = new ASTAndCondition(outerCond,innerCond);
101
102             //Get the body of the inner Node that will be the overall body
103
List newBodyList = ((ASTIfNode)bodyNode).get_SubBodies();
104
105             //retireve the actual body List
106
if(newBodyList.size()==1){//should always be one since this is body of IF
107
List newBody = (List)newBodyList.get(0);
108             node.replace(newLabel,newCond,newBody);
109             //System.out.println("ANDDDDDD AGGREGATING !!!");
110
G.v().ASTTransformations_modified=true;
111             }
112         }
113         else{//not an if node
114
}
115         }
116         else{ //IfBody has more than 1 nodes cant do AND aggregation
117
}
118     }
119     }
120
121     private void changeUses(String JavaDoc to, String JavaDoc from, ASTNode node){
122     //remember this method is only called when "to" and "from" are both non null
123
List subBodies = node.get_SubBodies();
124     Iterator it = subBodies.iterator();
125     while(it.hasNext()){
126         //going over all subBodies
127
if (node instanceof ASTStatementSequenceNode){
128         //check for abrupt stmts
129

130         ASTStatementSequenceNode stmtSeq = (ASTStatementSequenceNode)node;
131         List statements = stmtSeq.getStatements();
132         Iterator stmtIt = statements.iterator();
133         while(stmtIt.hasNext()){
134             AugmentedStmt as = (AugmentedStmt)stmtIt.next();
135             Stmt s = as.get_Stmt();
136
137
138             if(s instanceof DAbruptStmt){
139             DAbruptStmt abStmt = (DAbruptStmt)s;
140             if(abStmt.is_Break() || abStmt.is_Continue()){
141                 SETNodeLabel label = abStmt.getLabel();
142                 String JavaDoc labelBroken = label.toString();
143                 
144                 if(labelBroken != null){//stmt breaks some label
145
if(labelBroken.compareTo(from)==0){
146                     //have to replace the "from" label to "to" label
147
label.set_Name(to);
148                 }
149                 }
150             }
151             }
152         }
153         }
154         else{
155         //need to recursively call changeUses
156
List subBodyNodes=null;
157
158         if(node instanceof ASTTryNode){
159             ASTTryNode.container subBody = (ASTTryNode.container)it.next();
160             subBodyNodes=(List)subBody.o;
161         }
162         else{
163             subBodyNodes = (List)it.next();
164         }
165         Iterator nodesIt = subBodyNodes.iterator();
166         while(nodesIt.hasNext()){
167             changeUses(to,from,(ASTNode)nodesIt.next());
168         }
169         }
170
171     }//going through subBodies
172

173     }
174
175 }
Popular Tags