KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > pre > SootFilter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Florian Loitsch
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 package soot.jimple.toolkits.scalar.pre;
28 import soot.jimple.toolkits.graph.*;
29 import soot.toolkits.graph.*;
30 import soot.jimple.*;
31 import soot.*;
32 import java.util.*;
33
34 /**
35  * Allows easy filtering/wrapping of Soot objects. Operations that are done
36  * very often are grouped here.
37  */

38 public class SootFilter {
39
40   /**
41    * wraps a value into a EquivalentValue.
42    * returns <code>null</code> if <code>val</code> is null.
43    *
44    * @param val the Value to wrap.
45    * @return the EquivalentValue containing val.
46    */

47   public static EquivalentValue equiVal(Value val) {
48     if (val == null) return null;
49     return new EquivalentValue(val);
50   }
51
52   /**
53    * filters out the RHS of an assignmentStmt.
54    *
55    * @param unit a Unit from which to extract the RHS.
56    * @return the RHS-Value of <code>unit</code> or <code>null</code> if
57    * <code>unit</code> wasn't an assignment-stmt.
58    */

59   public static Value rhs(Unit unit) {
60     if (unit instanceof AssignStmt)
61       return ((AssignStmt)unit).getRightOp();
62     else
63       return null;
64   }
65
66   /**
67    * only lets binary expression through.
68    *
69    * @param val the Value to test for.
70    * @return <code>val</code> if it is a binary expression. otherwise
71    * <code>null</code>.
72    */

73   public static Value binop(Value val) {
74     if (val == null) return null;
75     if (val instanceof BinopExpr) return val;
76     return null;
77   }
78
79   /**
80    * only lets binary RHS through.
81    *
82    * @param unit the Unit to test for.
83    * @return the rhs of the current unit, if <code>unit</code> is an
84    * AssigStmt and its RHS is a binary expression. otherwise
85    * <code>null</code>.
86    */

87   public static Value binopRhs(Unit unit) {
88     return binop(rhs(unit));
89   }
90
91   /**
92    * only lets concrete references through. A concrete reference is either an
93    * array-ref or a field-ref.<br>
94    * returns <code>null</code> if <code>val</code> already was null.
95    *
96    * @param val the Value to test for.
97    * @return the <code>val</code> if it was a concrete reference. otherwise
98    * <code>null</code>.
99    */

100   public static Value concreteRef(Value val) {
101     if (val == null) return null;
102     if (val instanceof ConcreteRef) return val;
103     return null;
104   }
105
106   /**
107    * filters out Exception-throwing Values. This method is perhaps
108    * conservative.<br>
109    * returns <code>null</code> if <code>val</code> is null.
110    *
111    * @param val the Value to test for.
112    * @return <code>val</code> if val doesn't throw any exception, or
113    * <code>null</code> otherwise.
114    */

115   public static Value noExceptionThrowing(Value val) {
116     if (val == null) return null;
117     if (!throwsException(val))
118       return val;
119     else
120       return null;
121   }
122
123   /**
124    * filters out RHS that don't throw any exception.
125    *
126    * @param unit the Unit to test.
127    * @return the rhs, if <code>unit</code> is an assignment-stmt and can't
128    * throw any exception.
129    */

130   public static Value noExceptionThrowingRhs(Unit unit) {
131     return noExceptionThrowing(rhs(unit));
132   }
133
134   /**
135    * filters out RHS that aren't invokes.
136    *
137    * @param unit the Unit to look at.
138    * @return the RHS of <code>unit</code> if it is an assignment-stmt, and its
139    * RHS is not an invoke.
140    */

141   public static Value noInvokeRhs(Unit unit) {
142     return noInvoke(rhs(unit));
143   }
144
145   /**
146    * filters out Invokes.<br>
147    * returns <code>null</code> if <code>val</code> is null.
148    *
149    * @param val the Value to inspect
150    * @return <code>val</code>, if val is not an invoke, <code>null</code>
151    * otherwise.
152    */

153   public static Value noInvoke(Value val) {
154     if (val == null || isInvoke(val))
155       return null;
156     else
157       return val;
158   }
159
160   /**
161    * returns true, if <code>val</code> is an invoke.
162    *
163    * @param val the Value to inspect.
164    * @return true if <code>val</code> is an invoke.
165    */

166   public static boolean isInvoke(Value val) {
167     val = getEquivalentValueRoot(val);
168     if (val instanceof InvokeExpr)
169       return true;
170     return false;
171   }
172
173   /**
174    * filters out Locals.<br>
175    * returns <code>null</code> if <code>val</code> is null.
176    *
177    * @param val the Value to look at.
178    * @return <code>val</code>, if it is a Local, <code>null</code> otherwise.
179    */

180   public static Value local(Value val) {
181     if (val != null && isLocal(val))
182       return val;
183     else
184       return null;
185   }
186
187   /**
188    * only lets non-Locals through.<br>
189    * returns <code>null</code> if <code>val</code> is null.
190    *
191    * @param val the Value to look at.
192    * @return <code>val</code>, if it is not a Local, <code>null</code> otherwise.
193    */

194   public static Value noLocal(Value val) {
195     if (val != null && !isLocal(val))
196       return val;
197     else
198       return null;
199   }
200
201   /**
202    * returns true, if <code>val</code> is a Local.
203    */

204   public static boolean isLocal(Value val) {
205     return (getEquivalentValueRoot(val) instanceof Local);
206   }
207
208   /**
209    * returns the Value of an EquivalentValue. If there are several
210    * EquivalentValues stacked one into another, gets the deepest Value.<br>
211    * returns <code>null</code> if <code>val</code> is null.
212    *
213    * @param val the Value to inspect.
214    * @return val, if val is not an EquivalentValue, or the deepest Value
215    * otherwise.
216    */

217   public static Value getEquivalentValueRoot(Value val) {
218     if (val == null) return null;
219     /* extract the Value, if val is an EquivalentValue. One of the reasons, why
220      * testing for "instanceof" is sometimes not a good idea.*/

221     while (val instanceof EquivalentValue)
222       val = ((EquivalentValue)val).getValue();
223     return val;
224   }
225
226   /**
227    * a (probably) conservative way of telling, if a Value throws an exception
228    * or not.
229    */

230   public static boolean throwsException(Value val) {
231     val = getEquivalentValueRoot(val);
232
233     /* i really hope i did not forget any... */
234     if (val instanceof BinopExpr ||
235     val instanceof UnopExpr ||
236     val instanceof Local ||
237     val instanceof Constant) {
238       if (val instanceof DivExpr ||
239       val instanceof RemExpr ||
240       val instanceof LengthExpr)
241     return true;
242       return false;
243     }
244     return true;
245   }
246 }
247
Popular Tags