KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > Evaluator


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Phong Co
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28 package soot.jimple.toolkits.scalar;
29
30 import soot.util.*;
31 import soot.*;
32 import soot.jimple.*;
33 import java.io.*;
34 import java.util.*;
35
36
37 public class Evaluator {
38
39     public static boolean isValueConstantValued(Value op) {
40
41         if (op instanceof Constant)
42             return true;
43         else if ((op instanceof UnopExpr)) {
44             if (isValueConstantValued(((UnopExpr)op).getOp()))
45                 return true;
46         }
47         else if (op instanceof BinopExpr)
48         {
49             /* Handle weird cases. */
50             if (op instanceof DivExpr || op instanceof RemExpr)
51             {
52                 if (!isValueConstantValued(((BinopExpr)op).getOp1()) ||
53                     !isValueConstantValued(((BinopExpr)op).getOp2()))
54                     return false;
55
56                 Value c1 = getConstantValueOf(((BinopExpr)op).getOp1());
57                 Value c2 = getConstantValueOf(((BinopExpr)op).getOp2());
58
59                 /* check for a 0 value. If so, punt. */
60                 if (c2 instanceof IntConstant && ((IntConstant)c2).value == 0)
61                     return false;
62
63                 if (c2 instanceof LongConstant &&
64                          ((LongConstant)c2).value == 0)
65                     return false;
66             }
67
68             if (isValueConstantValued(((BinopExpr)op).getOp1()) &&
69                 isValueConstantValued(((BinopExpr)op).getOp2()))
70                 return true;
71         }
72         return false;
73     } // isValueConstantValued
74

75     /** Returns the constant value of <code>op</code> if it is easy
76      * to find the constant value; else returns <code>null</code>. */

77     public static Value getConstantValueOf(Value op) {
78         
79         if (!isValueConstantValued(op))
80             return null;
81         
82         if (op instanceof Constant)
83             return op;
84         else if (op instanceof UnopExpr) {
85             Value c = getConstantValueOf(((UnopExpr)op).getOp());
86             if (op instanceof NegExpr)
87                 return ((NumericConstant)c).negate();
88         }
89         else if (op instanceof BinopExpr) {
90             Value c1 = getConstantValueOf(((BinopExpr)op).getOp1());
91             Value c2 = getConstantValueOf(((BinopExpr)op).getOp2());
92             if (op instanceof AddExpr)
93                 return ((NumericConstant)c1).add((NumericConstant)c2);
94             else if (op instanceof SubExpr)
95                 return ((NumericConstant)c1).subtract((NumericConstant)c2);
96             else if (op instanceof MulExpr)
97                 return ((NumericConstant)c1).multiply((NumericConstant)c2);
98             // punting handled by isValueConstantValued().
99
else if (op instanceof DivExpr)
100                 return ((NumericConstant)c1).divide((NumericConstant)c2);
101             else if (op instanceof RemExpr)
102                 return ((NumericConstant)c1).remainder((NumericConstant)c2);
103             else if (op instanceof EqExpr || op instanceof NeExpr)
104             {
105                 if (c1 instanceof NumericConstant)
106                 {
107                     if (op instanceof EqExpr)
108                         return ((NumericConstant)c1).equalEqual
109                             ((NumericConstant)c2);
110                     else if (op instanceof NeExpr)
111                         return ((NumericConstant)c1).notEqual
112                             ((NumericConstant)c2);
113                 }
114                 else if (c1 instanceof StringConstant)
115                 {
116                     boolean equality = ((StringConstant)c1).equals
117                         ((StringConstant)c2);
118
119                     boolean truth = (op instanceof EqExpr) ? equality :
120                         !equality;
121
122                     // Yeah, this variable name sucks, but I couldn't resist.
123
IntConstant beauty = IntConstant.v(truth ? 1 : 0);
124                     return beauty;
125                 }
126                 else if (c1 instanceof NullConstant)
127                     return IntConstant.v
128                         (((NullConstant)c1).equals(c2) ? 1 : 0);
129                 throw new RuntimeException JavaDoc
130                     ("constant neither numeric nor string");
131             }
132             else if (op instanceof GtExpr)
133                 return ((NumericConstant)c1).greaterThan((NumericConstant)c2);
134             else if (op instanceof GeExpr)
135                 return ((NumericConstant)c1).greaterThanOrEqual((NumericConstant)c2);
136             else if (op instanceof LtExpr)
137                 return ((NumericConstant)c1).lessThan((NumericConstant)c2);
138             else if (op instanceof LeExpr)
139                 return ((NumericConstant)c1).lessThanOrEqual((NumericConstant)c2);
140             else if (op instanceof AndExpr)
141                 return ((ArithmeticConstant)c1).and((ArithmeticConstant)c2);
142             else if (op instanceof OrExpr)
143                 return ((ArithmeticConstant)c1).or((ArithmeticConstant)c2);
144             else if (op instanceof XorExpr)
145                 return ((ArithmeticConstant)c1).xor((ArithmeticConstant)c2);
146             else if (op instanceof ShlExpr)
147                 return ((ArithmeticConstant)c1).shiftLeft((ArithmeticConstant)c2);
148             else if (op instanceof ShrExpr)
149                 return ((ArithmeticConstant)c1).shiftRight((ArithmeticConstant)c2);
150             else if (op instanceof UshrExpr)
151                 return ((ArithmeticConstant)c1).unsignedShiftRight((ArithmeticConstant)c2);
152             else if (op instanceof CmpExpr) {
153                 if ((c1 instanceof LongConstant) &&
154                     (c2 instanceof LongConstant))
155                     return ((LongConstant)c1).cmp((LongConstant)c2);
156                 else throw new IllegalArgumentException JavaDoc(
157                                   "CmpExpr: LongConstant(s) expected");
158             }
159             else if ((op instanceof CmpgExpr) || (op instanceof CmplExpr)) {
160                 if ((c1 instanceof RealConstant) &&
161                     (c2 instanceof RealConstant)) {
162                     
163                     if(op instanceof CmpgExpr)
164                         return ((RealConstant) c1).cmpg((RealConstant) c2);
165                     else if(op instanceof CmplExpr)
166                         return ((RealConstant) c1).cmpl((RealConstant) c2);
167                         
168                 }
169                 else throw new IllegalArgumentException JavaDoc(
170                                   "CmpExpr: RealConstant(s) expected");
171             }
172             else
173                 throw new RuntimeException JavaDoc("unknown binop: " + op);
174         }
175
176         throw new RuntimeException JavaDoc("couldn't getConstantValueOf of: " + op);
177     } // getConstantValueOf
178

179 } // Evaluator
180

181
182
183
184
185
Popular Tags