KickJava   Java API By Example, From Geeks To Geeks.

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


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.tagkit.*;
22 import soot.*;
23 import java.util.*;
24 import soot.toolkits.graph.*;
25 import soot.jimple.toolkits.callgraph.*;
26 import soot.jimple.*;
27 import java.io.*;
28
29 public class FieldRWTagger extends BodyTransformer
30 {
31     public FieldRWTagger( Singletons.Global g ) {}
32     public static FieldRWTagger v() { return G.v().soot_jimple_toolkits_pointer_FieldRWTagger(); }
33
34     public int numRWs = 0;
35     public int numWRs = 0;
36     public int numRRs = 0;
37     public int numWWs = 0;
38     public int numNatives = 0;
39     public Date startTime = null;
40     boolean optionDontTag = false;
41     boolean optionNaive = false;
42     private CallGraph cg;
43
44     protected class UniqueRWSets {
45     protected ArrayList l = new ArrayList();
46     RWSet getUnique( RWSet s ) {
47         if( s == null ) return s;
48         for( Iterator retIt = l.iterator(); retIt.hasNext(); ) {
49             final RWSet ret = (RWSet) retIt.next();
50         if( ret.isEquivTo( s ) ) return ret;
51         }
52         l.add( s );
53         return s;
54     }
55     Iterator iterator() {
56         return l.iterator();
57     }
58     short indexOf( RWSet s ) {
59         short i = 0;
60         for( Iterator retIt = l.iterator(); retIt.hasNext(); ) {
61             final RWSet ret = (RWSet) retIt.next();
62         if( ret.isEquivTo( s ) ) return i;
63         i++;
64         }
65         return -1;
66     }
67     }
68
69     protected void initializationStuff( String JavaDoc phaseName ) {
70     if( G.v().Union_factory == null ) {
71         G.v().Union_factory = new UnionFactory() {
72         public Union newUnion() { return FullObjectSet.v(); }
73         };
74     }
75     if( startTime == null ) {
76         startTime = new Date();
77     }
78         cg = Scene.v().getCallGraph();
79     }
80     protected Object JavaDoc keyFor( Stmt s ) {
81     if( s.containsInvokeExpr() ) {
82         if( optionNaive ) throw new RuntimeException JavaDoc( "shouldn't get here" );
83             Iterator it = cg.edgesOutOf( s );
84         if( !it.hasNext() ) {
85         return Collections.EMPTY_LIST;
86         }
87             ArrayList ret = new ArrayList();
88             while( it.hasNext() ) {
89                 ret.add( it.next() );
90             }
91             return ret;
92     } else {
93         return s;
94     }
95     }
96     protected void internalTransform(Body body, String JavaDoc phaseName, Map options)
97     {
98     initializationStuff( phaseName );
99     SideEffectAnalysis sea = new SideEffectAnalysis(
100                 DumbPointerAnalysis.v(), Scene.v().getCallGraph() );
101         sea.findNTRWSets( body.getMethod() );
102     HashMap stmtToReadSet = new HashMap();
103     HashMap stmtToWriteSet = new HashMap();
104     UniqueRWSets sets = new UniqueRWSets();
105     optionDontTag = PhaseOptions.getBoolean( options, "dont-tag" );
106     boolean justDoTotallyConservativeThing =
107         body.getMethod().getName().equals( "<clinit>" );
108     for( Iterator stmtIt = body.getUnits().iterator(); stmtIt.hasNext(); ) {
109         final Stmt stmt = (Stmt) stmtIt.next();
110             if( !stmt.containsInvokeExpr() ) continue;
111         if( justDoTotallyConservativeThing ) {
112         stmtToReadSet.put( stmt, sets.getUnique( new FullRWSet() ) );
113         stmtToWriteSet.put( stmt, sets.getUnique( new FullRWSet() ) );
114         continue;
115         }
116         Object JavaDoc key = keyFor( stmt );
117         if( !stmtToReadSet.containsKey( key ) ) {
118         stmtToReadSet.put( key,
119             sets.getUnique( sea.readSet( body.getMethod(), stmt ) ) );
120         stmtToWriteSet.put( key,
121             sets.getUnique( sea.writeSet( body.getMethod(), stmt ) ) );
122         }
123     }
124     /*
125     DependenceGraph graph = new DependenceGraph();
126     for( Iterator outerIt = sets.iterator(); outerIt.hasNext(); ) {
127         final RWSet outer = (RWSet) outerIt.next();
128
129         for( Iterator innerIt = sets.iterator(); innerIt.hasNext(); ) {
130
131             final RWSet inner = (RWSet) innerIt.next();
132         if( inner == outer ) break;
133         if( outer.hasNonEmptyIntersection( inner ) ) {
134             graph.addEdge( sets.indexOf( outer ), sets.indexOf( inner ) );
135         }
136         }
137     }
138     if( !optionDontTag ) {
139         body.getMethod().addTag( graph );
140     }
141     for( Iterator stmtIt = body.getUnits().iterator(); stmtIt.hasNext(); ) {
142         final Stmt stmt = (Stmt) stmtIt.next();
143         Object key;
144         if( optionNaive && stmt.containsInvokeExpr() ) {
145         key = stmt;
146         } else {
147         key = keyFor( stmt );
148         }
149         RWSet read = (RWSet) stmtToReadSet.get( key );
150         RWSet write = (RWSet) stmtToWriteSet.get( key );
151         if( read != null || write != null ) {
152         DependenceTag tag = new DependenceTag();
153         if( read != null && read.getCallsNative() ) {
154             tag.setCallsNative();
155             numNatives++;
156         } else if( write != null && write.getCallsNative() ) {
157             tag.setCallsNative();
158             numNatives++;
159         }
160         tag.setRead( sets.indexOf( read ) );
161         tag.setWrite( sets.indexOf( write ) );
162         if( !optionDontTag ) stmt.addTag( tag );
163
164         // The loop below is just fro calculating stats.
165         if( !justDoTotallyConservativeThing ) {
166             for( Iterator innerIt = body.getUnits().iterator(); innerIt.hasNext(); ) {
167                 final Stmt inner = (Stmt) innerIt.next();
168             Object ikey;
169             if( optionNaive && inner.containsInvokeExpr() ) {
170                 ikey = inner;
171             } else {
172                 ikey = keyFor( inner );
173             }
174             RWSet innerRead = (RWSet) stmtToReadSet.get( ikey );
175             RWSet innerWrite = (RWSet) stmtToWriteSet.get( ikey );
176             if( graph.areAdjacent( sets.indexOf( read ),
177                     sets.indexOf( innerWrite ) ) ) numRWs++;
178             if( graph.areAdjacent( sets.indexOf( write ),
179                     sets.indexOf( innerRead ) ) ) numWRs++;
180             if( inner == stmt ) continue;
181             if( graph.areAdjacent( sets.indexOf( write ),
182                     sets.indexOf( innerWrite ) ) ) numWWs++;
183             if( graph.areAdjacent( sets.indexOf( read ),
184                     sets.indexOf( innerRead ) ) ) numRRs++;
185             }
186         }
187         }
188     }
189         */

190     }
191 }
192
193
194
Popular Tags