KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > pointer > PASideEffectTester


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
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.jimple.toolkits.pointer;
21
22 import soot.*;
23 import soot.jimple.*;
24 import java.util.*;
25
26 // ArrayRef,
27
// CaughtExceptionRef,
28
// FieldRef,
29
// IdentityRef,
30
// InstanceFieldRef,
31
// InstanceInvokeExpr,
32
// Local,
33
// StaticFieldRef
34

35 public class PASideEffectTester implements SideEffectTester
36 {
37     PointsToAnalysis pa = Scene.v().getPointsToAnalysis();
38     SideEffectAnalysis sea = Scene.v().getSideEffectAnalysis();
39     HashMap unitToRead;
40     HashMap unitToWrite;
41     HashMap localToReachingObjects;
42     SootMethod currentMethod;
43
44     public PASideEffectTester() {
45     if( G.v().Union_factory == null ) {
46         G.v().Union_factory = new UnionFactory() {
47         public Union newUnion() { return FullObjectSet.v(); }
48         };
49     }
50     }
51
52     /** Call this when starting to analyze a new method to setup the cache. */
53     public void newMethod( SootMethod m ) {
54     unitToRead = new HashMap();
55     unitToWrite = new HashMap();
56     localToReachingObjects = new HashMap();
57     currentMethod = m;
58     sea.findNTRWSets( currentMethod );
59     }
60
61     protected RWSet readSet( Unit u ) {
62     RWSet ret = (RWSet) unitToRead.get( u );
63     if( ret == null ) {
64         unitToRead.put( u, ret = sea.readSet( currentMethod, (Stmt) u ) );
65     }
66     return ret;
67     }
68
69     protected RWSet writeSet( Unit u ) {
70     RWSet ret = (RWSet) unitToWrite.get( u );
71     if( ret == null ) {
72         unitToWrite.put( u, ret = sea.writeSet( currentMethod, (Stmt) u ) );
73     }
74     return ret;
75     }
76     
77     protected PointsToSet reachingObjects( Local l ) {
78     PointsToSet ret = (PointsToSet) localToReachingObjects.get( l );
79     if( ret == null ) {
80         localToReachingObjects.put( l,
81             ret = pa.reachingObjects( l ) );
82     }
83     return ret;
84     }
85
86     /** Returns true if the unit can read from v.
87      * Does not deal with expressions; deals with Refs. */

88     public boolean unitCanReadFrom(Unit u, Value v)
89     {
90     return valueTouchesRWSet( readSet( u ), v, u.getUseBoxes() );
91     }
92
93     /** Returns true if the unit can read from v.
94      * Does not deal with expressions; deals with Refs. */

95     public boolean unitCanWriteTo(Unit u, Value v)
96     {
97     return valueTouchesRWSet( writeSet( u ), v, u.getDefBoxes() );
98     }
99
100     protected boolean valueTouchesRWSet(RWSet s, Value v, List boxes)
101     {
102         for( Iterator useIt = v.getUseBoxes().iterator(); useIt.hasNext(); ) {
103             final ValueBox use = (ValueBox) useIt.next();
104             if( valueTouchesRWSet( s, use.getValue(), boxes ) ) return true;
105         }
106         // This doesn't really make any sense, but we need to return something.
107
if (v instanceof Constant)
108             return false;
109
110         if (v instanceof Expr)
111             throw new RuntimeException JavaDoc("can't deal with expr");
112
113     for( Iterator boxIt = boxes.iterator(); boxIt.hasNext(); ) {
114
115         final ValueBox box = (ValueBox) boxIt.next();
116         Value boxed = box.getValue();
117         if( boxed.equivTo( v ) ) return true;
118     }
119
120     if (v instanceof Local) {
121         return false;
122     }
123
124     if( v instanceof InstanceFieldRef ) {
125         InstanceFieldRef ifr = (InstanceFieldRef) v;
126         if( s == null ) return false;
127         PointsToSet o1 = s.getBaseForField( ifr.getField() );
128         if( o1 == null ) return false;
129         PointsToSet o2 = reachingObjects( (Local) ifr.getBase() );
130         if( o2 == null ) return false;
131         return o1.hasNonEmptyIntersection( o2 );
132     }
133
134     if( v instanceof ArrayRef ) {
135         ArrayRef ar = (ArrayRef) v;
136         if( s == null ) return false;
137         PointsToSet o1 = s.getBaseForField( PointsToAnalysis.ARRAY_ELEMENTS_NODE );
138         if( o1 == null ) return false;
139         PointsToSet o2 = reachingObjects( (Local) ar.getBase() );
140         if( o2 == null ) return false;
141         return o1.hasNonEmptyIntersection( o2 );
142     }
143
144     if( v instanceof StaticFieldRef ) {
145         StaticFieldRef sfr = (StaticFieldRef) v;
146         if( s == null ) return false;
147         return s.getGlobals().contains( sfr.getField() );
148     }
149
150     throw new RuntimeException JavaDoc( "Forgot to handle value "+v );
151     }
152 }
153
Popular Tags