KickJava   Java API By Example, From Geeks To Geeks.

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


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 SideEffectTagger extends BodyTransformer
30 {
31     public SideEffectTagger( Singletons.Global g ) {}
32     public static SideEffectTagger v() { return G.v().soot_jimple_toolkits_pointer_SideEffectTagger(); }
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 optionNaive = false;
41     private CallGraph cg;
42     
43     protected class UniqueRWSets {
44     protected ArrayList l = new ArrayList();
45     RWSet getUnique( RWSet s ) {
46         if( s == null ) return s;
47         for( Iterator retIt = l.iterator(); retIt.hasNext(); ) {
48             final RWSet ret = (RWSet) retIt.next();
49         if( ret.isEquivTo( s ) ) return ret;
50         }
51         l.add( s );
52         return s;
53     }
54     Iterator iterator() {
55         return l.iterator();
56     }
57     short indexOf( RWSet s ) {
58         short i = 0;
59         for( Iterator retIt = l.iterator(); retIt.hasNext(); ) {
60             final RWSet ret = (RWSet) retIt.next();
61         if( ret.isEquivTo( s ) ) return i;
62         i++;
63         }
64         return -1;
65     }
66     }
67
68     protected void initializationStuff( String JavaDoc phaseName ) {
69         G.v().Union_factory = new UnionFactory() {
70         //ReallyCheapRasUnion ru = new ReallyCheapRasUnion();
71
//public Union newUnion() { return new RasUnion(); }
72
public Union newUnion() { return new MemoryEfficientRasUnion(); }
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 = Scene.v().getSideEffectAnalysis();
100     optionNaive = PhaseOptions.getBoolean( options, "naive" );
101     if( !optionNaive ) {
102         sea.findNTRWSets( body.getMethod() );
103     }
104     HashMap stmtToReadSet = new HashMap();
105     HashMap stmtToWriteSet = new HashMap();
106     UniqueRWSets sets = new UniqueRWSets();
107     boolean justDoTotallyConservativeThing =
108         body.getMethod().getName().equals( "<clinit>" );
109     for( Iterator stmtIt = body.getUnits().iterator(); stmtIt.hasNext(); ) {
110         final Stmt stmt = (Stmt) stmtIt.next();
111         if( justDoTotallyConservativeThing
112         || ( optionNaive && stmt.containsInvokeExpr() ) ) {
113         stmtToReadSet.put( stmt, sets.getUnique( new FullRWSet() ) );
114         stmtToWriteSet.put( stmt, sets.getUnique( new FullRWSet() ) );
115         continue;
116         }
117         Object JavaDoc key = keyFor( stmt );
118         if( !stmtToReadSet.containsKey( key ) ) {
119         stmtToReadSet.put( key,
120             sets.getUnique( sea.readSet( body.getMethod(), stmt ) ) );
121         stmtToWriteSet.put( key,
122             sets.getUnique( sea.writeSet( body.getMethod(), stmt ) ) );
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                     //G.v().out.println( "inner set is: "+inner );
135
//G.v().out.println( "outer set is: "+outer );
136
graph.addEdge( sets.indexOf( outer ), sets.indexOf( inner ) );
137         }
138         }
139     }
140         body.getMethod().addTag( graph );
141     for( Iterator stmtIt = body.getUnits().iterator(); stmtIt.hasNext(); ) {
142         final Stmt stmt = (Stmt) stmtIt.next();
143         Object JavaDoc 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                 stmt.addTag( tag );
163
164         // The loop below is just for calculating stats.
165
/*
166         if( !justDoTotallyConservativeThing ) {
167             for( Iterator innerIt = body.getUnits().iterator(); innerIt.hasNext(); ) {
168                 final Stmt inner = (Stmt) innerIt.next();
169             Object ikey;
170             if( optionNaive && inner.containsInvokeExpr() ) {
171                 ikey = inner;
172             } else {
173                 ikey = keyFor( inner );
174             }
175             RWSet innerRead = (RWSet) stmtToReadSet.get( ikey );
176             RWSet innerWrite = (RWSet) stmtToWriteSet.get( ikey );
177             if( graph.areAdjacent( sets.indexOf( read ),
178                     sets.indexOf( innerWrite ) ) ) numRWs++;
179             if( graph.areAdjacent( sets.indexOf( write ),
180                     sets.indexOf( innerRead ) ) ) numWRs++;
181             if( inner == stmt ) continue;
182             if( graph.areAdjacent( sets.indexOf( write ),
183                     sets.indexOf( innerWrite ) ) ) numWWs++;
184             if( graph.areAdjacent( sets.indexOf( read ),
185                     sets.indexOf( innerRead ) ) ) numRRs++;
186             }
187         }
188                 */

189         }
190     }
191     }
192 }
193
194
195
Popular Tags