01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 2003 Ondrej Lhotak
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * 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.spark.fieldrw;
21:
22: import soot.*;
23: import soot.util.*;
24: import java.util.*;
25: import soot.jimple.toolkits.callgraph.*;
26: import soot.jimple.*;
27:
28: public class FieldTagger extends BodyTransformer {
29: public FieldTagger(Singletons.Global g) {
30: }
31:
32: public static FieldTagger v() {
33: return G.v().soot_jimple_spark_fieldrw_FieldTagger();
34: }
35:
36: private final HashSet<SootMethod> processedMethods = new HashSet<SootMethod>();
37: private final HashMultiMap methodToWrite = new HashMultiMap();
38: private final HashMultiMap methodToRead = new HashMultiMap();
39:
40: protected void ensureProcessed(SootMethod m) {
41: if (processedMethods.contains(m))
42: return;
43: processedMethods.add(m);
44: if (!m.isConcrete())
45: return;
46: if (m.isPhantom())
47: return;
48: for (Iterator sIt = m.retrieveActiveBody().getUnits()
49: .iterator(); sIt.hasNext();) {
50: final Stmt s = (Stmt) sIt.next();
51: if (s instanceof AssignStmt) {
52: AssignStmt as = (AssignStmt) s;
53: Value l = as.getLeftOp();
54: if (l instanceof FieldRef) {
55: methodToWrite.put(m, ((FieldRef) l).getField());
56: }
57: Value r = as.getRightOp();
58: if (r instanceof FieldRef) {
59: methodToRead.put(m, ((FieldRef) r).getField());
60: }
61: }
62: }
63: }
64:
65: protected void internalTransform(Body body, String phaseName,
66: Map options) {
67: int threshold = PhaseOptions.getInt(options, "threshold");
68:
69: ensureProcessed(body.getMethod());
70:
71: CallGraph cg = Scene.v().getCallGraph();
72: TransitiveTargets tt = new TransitiveTargets(cg);
73: statement: for (Iterator sIt = body.getUnits().iterator(); sIt
74: .hasNext();) {
75: final Stmt s = (Stmt) sIt.next();
76: HashSet read = new HashSet();
77: HashSet write = new HashSet();
78: Iterator<MethodOrMethodContext> it = tt.iterator(s);
79: while (it.hasNext()) {
80: SootMethod target = (SootMethod) it.next();
81: ensureProcessed(target);
82: if (target.isNative())
83: continue statement;
84: if (target.isPhantom())
85: continue statement;
86: read.addAll(methodToRead.get(target));
87: write.addAll(methodToWrite.get(target));
88: if (read.size() + write.size() > threshold) {
89: continue statement;
90: }
91: }
92: s.addTag(new FieldReadTag(read));
93: s.addTag(new FieldWriteTag(write));
94: }
95: }
96: }
|