KickJava   Java API By Example, From Geeks To Geeks.

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


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 import soot.*;
22 import soot.jimple.*;
23 import soot.jimple.toolkits.callgraph.*;
24 import java.util.*;
25 import soot.util.*;
26
27 /** Generates side-effect information from a PointsToAnalysis. */
28 public class SideEffectAnalysis {
29     PointsToAnalysis pa;
30     CallGraph cg;
31     Map methodToNTReadSet = new HashMap();
32     Map methodToNTWriteSet = new HashMap();
33     int rwsetcount = 0;
34     TransitiveTargets tt;
35
36     public void findNTRWSets( SootMethod method ) {
37     if( methodToNTReadSet.containsKey( method )
38         && methodToNTWriteSet.containsKey( method ) ) return;
39     
40     MethodRWSet read = null;
41     MethodRWSet write = null;
42     for( Iterator sIt = method.retrieveActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
43         final Stmt s = (Stmt) sIt.next();
44             RWSet ntr = ntReadSet( method, s );
45             if( ntr != null ) {
46                 if( read == null ) read = new MethodRWSet();
47                 read.union( ntr );
48             }
49             RWSet ntw = ntWriteSet( method, s );
50             if( ntw != null ) {
51                 if( write == null ) write = new MethodRWSet();
52                 write.union( ntw );
53             }
54     }
55     methodToNTReadSet.put( method, read );
56     methodToNTWriteSet.put( method, write );
57     SootClass c = method.getDeclaringClass();
58     }
59
60     public RWSet nonTransitiveReadSet( SootMethod method ) {
61     findNTRWSets( method );
62     return (RWSet) methodToNTReadSet.get( method );
63     }
64
65     public RWSet nonTransitiveWriteSet( SootMethod method ) {
66     findNTRWSets( method );
67     return (RWSet) methodToNTWriteSet.get( method );
68     }
69
70     public SideEffectAnalysis( PointsToAnalysis pa, CallGraph cg ) {
71     this.pa = pa;
72     this.cg = cg;
73         this.tt = new TransitiveTargets( cg );
74     }
75
76     private RWSet ntReadSet( SootMethod method, Stmt stmt ) {
77     if( stmt instanceof AssignStmt ) {
78         AssignStmt a = (AssignStmt) stmt;
79         Value r = a.getRightOp();
80         return addValue( r, method, stmt );
81     }
82         return null;
83     }
84     public RWSet readSet( SootMethod method, Stmt stmt ) {
85     RWSet ret = null;
86         Iterator targets = tt.iterator( stmt );
87         while( targets.hasNext() ) {
88             SootMethod target = (SootMethod) targets.next();
89             if( target.isNative() ) {
90                 if( ret == null ) ret = new SiteRWSet();
91                 ret.setCallsNative();
92             } else if( target.isConcrete() ) {
93                 RWSet ntr = nonTransitiveReadSet(target);
94                 if( ntr != null ) {
95                     if( ret == null ) ret = new SiteRWSet();
96                     ret.union( ntr );
97                 }
98             }
99         }
100         if( ret == null ) return ntReadSet( method, stmt );
101         ret.union( ntReadSet( method, stmt ) );
102         return ret;
103     }
104
105     private RWSet ntWriteSet( SootMethod method, Stmt stmt ) {
106         if( stmt instanceof AssignStmt ) {
107         AssignStmt a = (AssignStmt) stmt;
108         Value l = a.getLeftOp();
109         return addValue( l, method, stmt );
110     }
111         return null;
112     }
113     public RWSet writeSet( SootMethod method, Stmt stmt ) {
114     RWSet ret = null;
115         Iterator targets = tt.iterator( stmt );
116         while( targets.hasNext() ) {
117             SootMethod target = (SootMethod) targets.next();
118             if( target.isNative() ) {
119                 if( ret == null ) ret = new SiteRWSet();
120                 ret.setCallsNative();
121             } else if( target.isConcrete() ) {
122                 RWSet ntw = nonTransitiveWriteSet(target);
123                 if( ntw != null ) {
124                     if( ret == null ) ret = new SiteRWSet();
125                     ret.union( ntw );
126                 }
127             }
128     }
129         if( ret == null ) return ntWriteSet( method, stmt );
130         ret.union( ntWriteSet( method, stmt ) );
131     return ret;
132     }
133
134     protected RWSet addValue( Value v, SootMethod m, Stmt s ) {
135     RWSet ret = null;
136     if( v instanceof InstanceFieldRef ) {
137         InstanceFieldRef ifr = (InstanceFieldRef) v;
138         PointsToSet base = pa.reachingObjects( (Local) ifr.getBase() );
139         ret = new StmtRWSet();
140         ret.addFieldRef( base, ifr.getField() );
141     } else if( v instanceof StaticFieldRef ) {
142         StaticFieldRef sfr = (StaticFieldRef) v;
143         ret = new StmtRWSet();
144         ret.addGlobal( sfr.getField() );
145     } else if( v instanceof ArrayRef ) {
146         ArrayRef ar = (ArrayRef) v;
147         PointsToSet base = pa.reachingObjects( (Local) ar.getBase() );
148         ret = new StmtRWSet();
149         ret.addFieldRef( base, PointsToAnalysis.ARRAY_ELEMENTS_NODE );
150     }
151     return ret;
152     }
153
154     public String JavaDoc toString() {
155         return "SideEffectAnalysis: PA="+pa+" CG="+cg;
156     }
157 }
158
159
Popular Tags