changes.
[IRC.git] / Robust / src / Analysis / Disjoint / EffectsAnalysis.java
1 package Analysis.Disjoint;
2
3 import java.util.Hashtable;
4 import java.util.Iterator;
5
6 import IR.FieldDescriptor;
7 import IR.Flat.FlatCall;
8 import IR.Flat.FlatMethod;
9 import IR.Flat.TempDescriptor;
10
11 /////////////////////////////////////////////
12 // 
13 //  Effects analysis computes read/write/strong
14 //  update and other sorts of effects for the
15 //  scope of a method or rblock.  The effects
16 //  are associated with the heap roots through
17 //  which a reference to the effect target was
18 //  obtained.
19 //
20 //  The effects analysis piggy-backs
21 //  on the disjoint reachability analysis,
22 //  if requested, to support OoOJava and
23 //  potentially other analysis clients.
24 //
25 /////////////////////////////////////////////
26
27 public class EffectsAnalysis {
28
29   private Hashtable<FlatMethod, EffectSet> mapFlatmethodToEffectset;
30
31   // private Hashtable<MethodContext, MethodEffects>
32   // mapMethodContextToMethodEffects;
33   // boolean methodeffects = false;
34
35   public EffectsAnalysis(boolean methodeffects) {
36     mapFlatmethodToEffectset = new Hashtable<FlatMethod, EffectSet>();
37   }
38
39   public void analyzeFlatFieldNode(FlatMethod fmContaining, ReachGraph rg, TempDescriptor rhs, FieldDescriptor fld) {
40
41     VariableNode vn = rg.td2vn.get(rhs);
42
43     EffectSet effectSet = mapFlatmethodToEffectset.get(fmContaining);
44     if (effectSet == null) {
45       effectSet = new EffectSet();
46     }
47
48     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
49       RefEdge edge = iterator.next();
50       TaintSet taintSet = edge.getTaints();
51       AllocSite affectedAlloc = edge.getDst().getAllocSite();
52       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
53         Taint taint = taintSetIter.next();
54
55 //        Effect effect = new Effect(taint.getParamIndex(), taint.getAllocSite(), affectedAlloc, Effect.read, fld);
56 //        effectSet.addMethodEffect(taint.getParamIndex(), effect);
57
58       }
59     }
60
61     mapFlatmethodToEffectset.put(fmContaining, effectSet);
62
63   }
64
65   public void analyzeFlatSetFieldNode(FlatMethod fmContaining, ReachGraph rg, TempDescriptor lhs, FieldDescriptor fld) {
66
67     VariableNode vn = rg.td2vn.get(lhs);
68
69     EffectSet effectSet = mapFlatmethodToEffectset.get(fmContaining);
70     if (effectSet == null) {
71       effectSet = new EffectSet();
72     }
73
74     for (Iterator<RefEdge> iterator = vn.iteratorToReferencees(); iterator.hasNext();) {
75       RefEdge edge = iterator.next();
76       TaintSet taintSet = edge.getTaints();
77       AllocSite affectedAlloc = edge.getDst().getAllocSite();
78       for (Iterator<Taint> taintSetIter = taintSet.iterator(); taintSetIter.hasNext();) {
79         Taint taint = taintSetIter.next();
80
81 //        Effect effect = new Effect(taint.getParamIndex(), taint.getAllocSite(), affectedAlloc, Effect.write, fld);
82 //        effectSet.addMethodEffect(taint.getParamIndex(), effect);
83
84       }
85     }
86
87     mapFlatmethodToEffectset.put(fmContaining, effectSet);
88     
89   }
90
91   public EffectSet getEffectSet(FlatMethod fm) {
92     return mapFlatmethodToEffectset.get(fm);
93   }
94
95   /*
96    * public MethodEffects getMethodEffectsByMethodContext(MethodContext mc){
97    * return mapMethodContextToMethodEffects.get(mc); }
98    * 
99    * public void createNewMapping(MethodContext mcNew) { if(!methodeffects)
100    * return; if (!mapMethodContextToMethodEffects.containsKey(mcNew)) {
101    * MethodEffects meNew = new MethodEffects();
102    * mapMethodContextToMethodEffects.put(mcNew, meNew); } }
103    */
104
105   /*
106    * public void analyzeFlatCall(OwnershipGraph calleeOG, MethodContext
107    * calleeMC, MethodContext callerMC, FlatCall fc) { if(!methodeffects) return;
108    * MethodEffects me = mapMethodContextToMethodEffects.get(callerMC);
109    * MethodEffects meFlatCall = mapMethodContextToMethodEffects .get(calleeMC);
110    * me.analyzeFlatCall(calleeOG, fc, callerMC, meFlatCall);
111    * mapMethodContextToMethodEffects.put(callerMC, me); }
112    */
113
114   /*
115    * public void analyzeFlatFieldNode(MethodContext mc, OwnershipGraph og,
116    * TempDescriptor srcDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
117    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
118    * me.analyzeFlatFieldNode(og, srcDesc, fieldDesc);
119    * mapMethodContextToMethodEffects.put(mc, me); }
120    * 
121    * public void analyzeFlatSetFieldNode(MethodContext mc, OwnershipGraph og,
122    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
123    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
124    * me.analyzeFlatSetFieldNode(og, dstDesc, fieldDesc);
125    * mapMethodContextToMethodEffects.put(mc, me); }
126    * 
127    * public void analyzeFlatSetElementNode(MethodContext mc, OwnershipGraph og,
128    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
129    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
130    * me.analyzeFlatSetElementNode(og, dstDesc, fieldDesc);
131    * mapMethodContextToMethodEffects.put(mc, me); }
132    * 
133    * public void analyzeFlatElementNode(MethodContext mc, OwnershipGraph og,
134    * TempDescriptor dstDesc, FieldDescriptor fieldDesc) { if(!methodeffects)
135    * return; MethodEffects me = mapMethodContextToMethodEffects.get(mc);
136    * me.analyzeFlatElementNode(og, dstDesc, fieldDesc);
137    * mapMethodContextToMethodEffects.put(mc, me); }
138    * 
139    * 
140    * public void writeMethodEffectsResult() throws IOException {
141    * 
142    * try { BufferedWriter bw = new BufferedWriter(new FileWriter(
143    * "MethodEffects_report.txt"));
144    * 
145    * Set<MethodContext> mcSet = mapMethodContextToMethodEffects.keySet();
146    * Iterator<MethodContext> mcIter = mcSet.iterator(); while (mcIter.hasNext())
147    * { MethodContext mc = mcIter.next(); MethodDescriptor md =
148    * (MethodDescriptor) mc.getDescriptor();
149    * 
150    * int startIdx = 0; if (!md.isStatic()) { startIdx = 1; }
151    * 
152    * MethodEffects me = mapMethodContextToMethodEffects.get(mc); EffectsSet
153    * effectsSet = me.getEffects();
154    * 
155    * bw.write("Method " + mc + " :\n"); for (int i = startIdx; i <
156    * md.numParameters() + startIdx; i++) {
157    * 
158    * String paramName = md.getParamName(i - startIdx);
159    * 
160    * Set<EffectsKey> effectSet = effectsSet.getReadingSet(i); String keyStr =
161    * "{"; if (effectSet != null) { Iterator<EffectsKey> effectIter =
162    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
163    * effectIter.next(); keyStr += " " + key; } } keyStr += " }";
164    * bw.write("  Paramter " + paramName + " ReadingSet=" + keyStr + "\n");
165    * 
166    * effectSet = effectsSet.getWritingSet(new Integer(i)); keyStr = "{"; if
167    * (effectSet != null) { Iterator<EffectsKey> effectIter =
168    * effectSet.iterator(); while (effectIter.hasNext()) { EffectsKey key =
169    * effectIter.next(); keyStr += " " + key; } }
170    * 
171    * keyStr += " }"; bw.write("  Paramter " + paramName + " WritingngSet=" +
172    * keyStr + "\n");
173    * 
174    * } bw.write("\n");
175    * 
176    * }
177    * 
178    * bw.close(); } catch (IOException e) { System.err.println(e); }
179    * 
180    * }
181    */
182 }