KickJava   Java API By Example, From Geeks To Geeks.

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


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.dava.toolkits.base.AST.analysis.*;
23 import soot.dava.internal.javaRep.*;
24 import soot.dava.internal.AST.*;
25 import soot.*;
26 import soot.jimple.*;
27 import soot.grimp.internal.*;
28
29 /*
30   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
31   this is done by overriding the caseASTStatementSequenceNode
32 */

33 public class BooleanConditionSimplification extends DepthFirstAdapter{
34
35     public BooleanConditionSimplification(boolean verbose){
36     super(verbose);
37     }
38
39
40     public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){
41     }
42
43     public BooleanConditionSimplification(){
44     }
45     /*
46       The method checks whether a particular ASTBinaryCondition
47       is a comparison of a local with a boolean
48       If so the ASTBinaryCondition is replaced by a ASTUnaryCondition
49     */

50     public void outASTIfNode(ASTIfNode node){
51     ASTCondition condition = node.get_Condition();
52     if(condition instanceof ASTBinaryCondition){
53         ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr();
54         Value unary = checkBooleanUse(condExpr);
55         if(unary != null){
56         node.set_Condition(new ASTUnaryCondition(unary));
57         }
58     }
59     }
60
61     public void outASTIfElseNode(ASTIfElseNode node){
62     ASTCondition condition = node.get_Condition();
63     if(condition instanceof ASTBinaryCondition){
64         ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr();
65         Value unary = checkBooleanUse(condExpr);
66         if(unary != null){
67         node.set_Condition(new ASTUnaryCondition(unary));
68         }
69     }
70     }
71
72     public void outASTWhileNode(ASTWhileNode node){
73     ASTCondition condition = node.get_Condition();
74     if(condition instanceof ASTBinaryCondition){
75         ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr();
76         Value unary = checkBooleanUse(condExpr);
77         if(unary != null){
78         node.set_Condition(new ASTUnaryCondition(unary));
79         }
80     }
81     }
82
83     public void outASTDoWhileNode(ASTDoWhileNode node){
84     ASTCondition condition = node.get_Condition();
85     if(condition instanceof ASTBinaryCondition){
86         ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr();
87         Value unary = checkBooleanUse(condExpr);
88         if(unary != null){
89         node.set_Condition(new ASTUnaryCondition(unary));
90         }
91     }
92     }
93
94     private Value checkBooleanUse(ConditionExpr condition){
95     //check whether the condition qualifies as a boolean use
96
if(condition instanceof NeExpr || condition instanceof EqExpr){
97         Value op1 = condition.getOp1();
98         Value op2 = condition.getOp2();
99         if(op1 instanceof DIntConstant){
100         Type op1Type = ((DIntConstant)op1).type;
101         if( op1Type instanceof BooleanType){
102             return decideCondition(op2,((DIntConstant)op1).toString(),condition);
103         }
104         }
105         else if(op2 instanceof DIntConstant){
106         Type op2Type = ((DIntConstant)op2).type;
107         if( op2Type instanceof BooleanType){
108             return decideCondition(op1,((DIntConstant)op2).toString(),condition);
109         }
110         }
111         
112         else
113         return null;//meaning no Value used as boolean found
114
}
115     return null; //meaning no local used as boolean found
116
}
117
118     /*
119       Used to decide what the condition should be if we are converting from ConditionExpr to Value
120       A != false/0 --> A
121       A != true/1 --> !A
122       A == false/0 --> !A
123       A == true/1 --> A
124     */

125     private Value decideCondition(Value A, String JavaDoc truthString, ConditionExpr condition){
126     int truthValue =0;
127     boolean notEqual=false;
128
129     //find out whether we are dealing with a false or true
130
if(truthString.compareTo("false")==0)
131         truthValue=0;
132     else if(truthString.compareTo("true")==0)
133         truthValue=1;
134     else
135         throw new RuntimeException JavaDoc();
136
137     
138
139     //find out whether the comparison operator is != or ==
140
if(condition instanceof NeExpr ){
141         notEqual=true;
142     }
143     else if( condition instanceof EqExpr){
144         notEqual=false;
145     }
146     else
147         throw new RuntimeException JavaDoc();
148
149
150     //decide and return
151
if( notEqual && truthValue==0){ //A != false -->A
152
return A;
153     }
154     else if(notEqual && truthValue==1){//A != true --> !A
155
if(A instanceof DNotExpr){//A is actually !B
156
return ((DNotExpr)A).getOp();
157         }
158         else
159         return (new DNotExpr(A));
160     }
161     else if(!notEqual && truthValue==0){//A == false --> !A
162
if(A instanceof DNotExpr){//A is actually !B
163
return ((DNotExpr)A).getOp();
164         }
165         else
166         return new DNotExpr(A);
167     }
168     else if(!notEqual && truthValue==1){//A == true --> A
169
return A;
170     }
171     else
172         throw new RuntimeException JavaDoc();
173     }
174
175 }
Popular Tags