KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > NaiveSideEffectTester


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2000 Patrick Lam
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;
28
29 import soot.*;
30 import java.util.*;
31
32 /** Provides naive side effect information.
33  * Relies on no context information; instead, does the least
34  * conservative thing possible even in the possible presence of badness.
35  *
36  * Possible weakness of SideEffectTester: consider a Box. We don't
37  * have a name for "what-is-inside-the-box" and so we can't
38  * ask questions about it. But perhaps we need only ask questions
39  * about the box itself; the sideeffect tester can deal with
40  * that internally. */

41
42 // ArrayRef,
43
// CaughtExceptionRef,
44
// FieldRef,
45
// IdentityRef,
46
// InstanceFieldRef,
47
// InstanceInvokeExpr,
48
// Local,
49
// StaticFieldRef
50

51 public class NaiveSideEffectTester implements SideEffectTester
52 {
53     public void newMethod( SootMethod m ) {
54     }
55
56     /** Returns true if the unit can read from v.
57      * Does not deal with expressions; deals with Refs. */

58     public boolean unitCanReadFrom(Unit u, Value v)
59     {
60         Stmt s = (Stmt)u;
61
62         // This doesn't really make any sense, but we need to return something.
63
if (v instanceof Constant)
64             return false;
65
66         if (v instanceof Expr)
67             throw new RuntimeException JavaDoc("can't deal with expr");
68
69         // If it's an invoke, then only locals are safe.
70
if (s.containsInvokeExpr())
71         {
72         if (!(v instanceof Local))
73         return true;
74         }
75
76         // otherwise, use boxes tell all.
77
Iterator useIt = u.getUseBoxes().iterator();
78         while (useIt.hasNext())
79         {
80             Value use = (Value)useIt.next();
81
82             if (use.equivTo(v))
83                 return true;
84
85             Iterator vUseIt = v.getUseBoxes().iterator();
86             while (vUseIt.hasNext())
87             {
88                 if (use.equivTo(vUseIt.next()))
89                     return true;
90             }
91         }
92         return false;
93     }
94
95     public boolean unitCanWriteTo(Unit u, Value v)
96     {
97         Stmt s = (Stmt)u;
98
99         if (v instanceof Constant)
100             return false;
101
102         if (v instanceof Expr)
103             throw new RuntimeException JavaDoc("can't deal with expr");
104
105         // If it's an invoke, then only locals are safe.
106
if (s.containsInvokeExpr())
107         {
108             if (!(v instanceof Local))
109                 return true;
110         }
111
112         // otherwise, def boxes tell all.
113
Iterator defIt = u.getDefBoxes().iterator();
114         while (defIt.hasNext())
115         {
116             Value def = ((ValueBox)(defIt.next())).getValue();
117             Iterator useIt = v.getUseBoxes().iterator();
118             while (useIt.hasNext())
119             {
120                 Value use = ((ValueBox)useIt.next()).getValue();
121                 if (def.equivTo(use))
122                   return true;
123             }
124             // also handle the container of all these useboxes!
125
if (def.equivTo(v))
126                 return true;
127
128             // deal with aliasing - handle case where they
129
// are a read to the same field, regardless of
130
// base object.
131
if (v instanceof InstanceFieldRef &&
132                 def instanceof InstanceFieldRef)
133             {
134                 if (((InstanceFieldRef)v).getField() ==
135                     ((InstanceFieldRef)def).getField())
136                     return true;
137             }
138         }
139         return false;
140     }
141 }
142
Popular Tags