bug fixes
[IRC.git] / Robust / src / Analysis / SSJava / DefinitelyWrittenCheck.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7 import java.util.Set;
8 import java.util.Stack;
9
10 import Analysis.CallGraph.CallGraph;
11 import IR.Descriptor;
12 import IR.FieldDescriptor;
13 import IR.MethodDescriptor;
14 import IR.Operation;
15 import IR.State;
16 import IR.TypeDescriptor;
17 import IR.Flat.FKind;
18 import IR.Flat.FlatCall;
19 import IR.Flat.FlatFieldNode;
20 import IR.Flat.FlatLiteralNode;
21 import IR.Flat.FlatMethod;
22 import IR.Flat.FlatNode;
23 import IR.Flat.FlatOpNode;
24 import IR.Flat.FlatSetFieldNode;
25 import IR.Flat.TempDescriptor;
26
27 public class DefinitelyWrittenCheck {
28
29   SSJavaAnalysis ssjava;
30   State state;
31   CallGraph callGraph;
32
33   // maps a descriptor to its known dependents: namely
34   // methods or tasks that call the descriptor's method
35   // AND are part of this analysis (reachable from main)
36   private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
37
38   // maps a flat node to its WrittenSet: this keeps all heap path overwritten
39   // previously.
40   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToWrittenSet;
41
42   // maps a temp descriptor to its heap path
43   // each temp descriptor has a unique heap path since we do not allow any
44   // alias.
45   private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
46
47   // maps a flat method to the READ that is the set of heap path that is
48   // expected to be written before method invocation
49   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToRead;
50
51   // maps a flat method to the OVERWRITE that is the set of heap path that is
52   // overwritten on every possible path during method invocation
53   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToOverWrite;
54
55   // points to method containing SSJAVA Loop
56   private MethodDescriptor methodContainingSSJavaLoop;
57
58   // maps a flatnode to definitely written analysis mapping M
59   private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>> definitelyWrittenResults;
60
61   private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
62   private Set<NTuple<Descriptor>> calleeIntersectBoundOverWriteSet;
63
64   public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
65     this.state = state;
66     this.ssjava = ssjava;
67     this.callGraph = ssjava.getCallGraph();
68     this.mapFlatNodeToWrittenSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
69     this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
70     this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
71     this.mapFlatMethodToRead = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
72     this.mapFlatMethodToOverWrite = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
73     this.definitelyWrittenResults =
74         new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>>();
75     this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
76     this.calleeIntersectBoundOverWriteSet = new HashSet<NTuple<Descriptor>>();
77   }
78
79   public void definitelyWrittenCheck() {
80     if (!ssjava.getAnnotationRequireSet().isEmpty()) {
81       methodReadOverWriteAnalysis();
82       writtenAnalyis();
83     }
84   }
85
86   private void writtenAnalyis() {
87     // perform second stage analysis: intraprocedural analysis ensure that
88     // all
89     // variables are definitely written in-between the same read
90
91     // First, identify ssjava loop entrace
92     FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
93     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
94     flatNodesToVisit.add(fm);
95
96     FlatNode entrance = null;
97
98     while (!flatNodesToVisit.isEmpty()) {
99       FlatNode fn = flatNodesToVisit.iterator().next();
100       flatNodesToVisit.remove(fn);
101
102       String label = (String) state.fn2labelMap.get(fn);
103       if (label != null) {
104
105         if (label.equals(ssjava.SSJAVA)) {
106           entrance = fn;
107           break;
108         }
109       }
110
111       for (int i = 0; i < fn.numNext(); i++) {
112         FlatNode nn = fn.getNext(i);
113         flatNodesToVisit.add(nn);
114       }
115     }
116
117     assert entrance != null;
118
119     writtenAnalysis_analyzeLoop(entrance);
120
121   }
122
123   private void writtenAnalysis_analyzeLoop(FlatNode entrance) {
124
125     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
126     flatNodesToVisit.add(entrance);
127
128     while (!flatNodesToVisit.isEmpty()) {
129       FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
130       flatNodesToVisit.remove(fn);
131
132       Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> prev =
133           definitelyWrittenResults.get(fn);
134
135       Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr =
136           new Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>();
137       for (int i = 0; i < fn.numPrev(); i++) {
138         FlatNode nn = fn.getPrev(i);
139         Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> dwIn =
140             definitelyWrittenResults.get(nn);
141         if (dwIn != null) {
142           merge(curr, dwIn);
143         }
144       }
145
146       writtenAnalysis_nodeAction(fn, curr, entrance);
147
148       // if a new result, schedule forward nodes for analysis
149       if (!curr.equals(prev)) {
150         definitelyWrittenResults.put(fn, curr);
151
152         for (int i = 0; i < fn.numNext(); i++) {
153           FlatNode nn = fn.getNext(i);
154           flatNodesToVisit.add(nn);
155         }
156       }
157     }
158   }
159
160   private void writtenAnalysis_nodeAction(FlatNode fn,
161       Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr, FlatNode loopEntrance) {
162     if (fn.equals(loopEntrance)) {
163       // it reaches loop entrance: changes all flag to true
164       Set<NTuple<Descriptor>> keySet = curr.keySet();
165       for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
166         NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
167         Hashtable<FlatNode, Boolean> pair = curr.get(key);
168         if (pair != null) {
169           Set<FlatNode> pairKeySet = pair.keySet();
170           for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
171             FlatNode pairKey = (FlatNode) iterator2.next();
172             pair.put(pairKey, Boolean.TRUE);
173           }
174         }
175       }
176     } else {
177       TempDescriptor lhs;
178       TempDescriptor rhs;
179       FieldDescriptor fld;
180
181       switch (fn.kind()) {
182       case FKind.FlatOpNode: {
183         FlatOpNode fon = (FlatOpNode) fn;
184         lhs = fon.getDest();
185         rhs = fon.getLeft();
186
187         NTuple<Descriptor> rhsHeapPath = computePath(rhs);
188         if (!rhs.getType().isImmutable()) {
189           mapHeapPath.put(lhs, rhsHeapPath);
190         } else {
191           if (fon.getOp().getOp() == Operation.ASSIGN) {
192             // read(rhs)
193             readValue(fn, rhsHeapPath, curr);
194           }
195           // write(lhs)
196           NTuple<Descriptor> lhsHeapPath = computePath(lhs);
197           removeHeapPath(curr, lhsHeapPath);
198         }
199       }
200         break;
201
202       case FKind.FlatLiteralNode: {
203         FlatLiteralNode fln = (FlatLiteralNode) fn;
204         lhs = fln.getDst();
205
206         // write(lhs)
207         NTuple<Descriptor> lhsHeapPath = computePath(lhs);
208         removeHeapPath(curr, lhsHeapPath);
209
210       }
211         break;
212
213       case FKind.FlatFieldNode:
214       case FKind.FlatElementNode: {
215
216         FlatFieldNode ffn = (FlatFieldNode) fn;
217         lhs = ffn.getDst();
218         rhs = ffn.getSrc();
219         fld = ffn.getField();
220
221         // read field
222         NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
223         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
224         fldHeapPath.add(fld);
225
226         if (fld.getType().isImmutable()) {
227           readValue(fn, fldHeapPath, curr);
228         }
229
230         // propagate rhs's heap path to the lhs
231         mapHeapPath.put(lhs, fldHeapPath);
232
233       }
234         break;
235
236       case FKind.FlatSetFieldNode:
237       case FKind.FlatSetElementNode: {
238
239         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
240         lhs = fsfn.getDst();
241         fld = fsfn.getField();
242
243         // write(field)
244         NTuple<Descriptor> lhsHeapPath = computePath(lhs);
245         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
246         fldHeapPath.add(fld);
247         removeHeapPath(curr, fldHeapPath);
248
249       }
250         break;
251
252       case FKind.FlatCall: {
253
254         FlatCall fc = (FlatCall) fn;
255         bindHeapPathCallerArgWithCaleeParam(fc);
256
257         // add <hp,statement,false> in which hp is an element of
258         // READ_bound set
259         // of callee: callee has 'read' requirement!
260         for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
261           NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
262
263           Hashtable<FlatNode, Boolean> gen = curr.get(read);
264           if (gen == null) {
265             gen = new Hashtable<FlatNode, Boolean>();
266             curr.put(read, gen);
267           }
268           Boolean currentStatus = gen.get(fn);
269           if (currentStatus == null) {
270             gen.put(fn, Boolean.FALSE);
271           } else {
272             checkFlag(currentStatus.booleanValue(), fn);
273           }
274         }
275
276         // removes <hp,statement,flag> if hp is an element of
277         // OVERWRITE_bound
278         // set of callee. it means that callee will overwrite it
279         for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
280           NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
281           removeHeapPath(curr, write);
282         }
283       }
284         break;
285
286       }
287     }
288
289   }
290
291   private void readValue(FlatNode fn, NTuple<Descriptor> hp,
292       Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr) {
293     Hashtable<FlatNode, Boolean> gen = curr.get(hp);
294     if (gen == null) {
295       gen = new Hashtable<FlatNode, Boolean>();
296       curr.put(hp, gen);
297     }
298     Boolean currentStatus = gen.get(fn);
299     if (currentStatus == null) {
300       gen.put(fn, Boolean.FALSE);
301     } else {
302       checkFlag(currentStatus.booleanValue(), fn);
303     }
304
305   }
306
307   private void removeHeapPath(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
308       NTuple<Descriptor> hp) {
309
310     // removes all of heap path that starts with prefix 'hp'
311     // since any reference overwrite along heap path gives overwriting side
312     // effects on the value
313
314     Set<NTuple<Descriptor>> keySet = curr.keySet();
315     for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
316       NTuple<Descriptor> key = iter.next();
317       if (key.startsWith(hp)) {
318         curr.put(key, new Hashtable<FlatNode, Boolean>());
319       }
320     }
321
322   }
323
324   private void bindHeapPathCallerArgWithCaleeParam(FlatCall fc) {
325     // compute all possible callee set
326     // transform all READ/OVERWRITE set from the any possible
327     // callees to the
328     // caller
329
330     calleeUnionBoundReadSet.clear();
331     calleeIntersectBoundOverWriteSet.clear();
332
333     MethodDescriptor mdCallee = fc.getMethod();
334     FlatMethod fmCallee = state.getMethodFlat(mdCallee);
335     Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
336     TypeDescriptor typeDesc = fc.getThis().getType();
337     setPossibleCallees.addAll(callGraph.getMethods(mdCallee, typeDesc));
338
339     // create mapping from arg idx to its heap paths
340     Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
341         new Hashtable<Integer, NTuple<Descriptor>>();
342
343     // arg idx is starting from 'this' arg
344     NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
345     thisHeapPath.add(fc.getThis());
346     mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
347
348     for (int i = 0; i < fc.numArgs(); i++) {
349       TempDescriptor arg = fc.getArg(i);
350       NTuple<Descriptor> argHeapPath = computePath(arg);
351       mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
352     }
353
354     for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
355       MethodDescriptor callee = (MethodDescriptor) iterator.next();
356       FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
357
358       // binding caller's args and callee's params
359
360       Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToRead.get(calleeFlatMethod);
361       if (calleeReadSet == null) {
362         calleeReadSet = new HashSet<NTuple<Descriptor>>();
363         mapFlatMethodToRead.put(calleeFlatMethod, calleeReadSet);
364       }
365       Set<NTuple<Descriptor>> calleeOverWriteSet = mapFlatMethodToOverWrite.get(calleeFlatMethod);
366       if (calleeOverWriteSet == null) {
367         calleeOverWriteSet = new HashSet<NTuple<Descriptor>>();
368         mapFlatMethodToOverWrite.put(calleeFlatMethod, calleeOverWriteSet);
369       }
370
371       Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
372           new Hashtable<Integer, TempDescriptor>();
373       for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
374         TempDescriptor param = calleeFlatMethod.getParameter(i);
375         mapParamIdx2ParamTempDesc.put(Integer.valueOf(i), param);
376       }
377
378       Set<NTuple<Descriptor>> calleeBoundReadSet =
379           bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
380       // union of the current read set and the current callee's
381       // read set
382       calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
383       Set<NTuple<Descriptor>> calleeBoundWriteSet =
384           bindSet(calleeOverWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
385       // intersection of the current overwrite set and the current
386       // callee's
387       // overwrite set
388       merge(calleeIntersectBoundOverWriteSet, calleeBoundWriteSet);
389     }
390
391   }
392
393   private void checkFlag(boolean booleanValue, FlatNode fn) {
394     if (booleanValue) {
395       throw new Error(
396           "There is a variable who comes back to the same read statement without being overwritten at the out-most iteration at "
397               + methodContainingSSJavaLoop.getClassDesc().getSourceFileName()
398               + "::"
399               + fn.getNumLine());
400     }
401   }
402
403   private void merge(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
404       Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> in) {
405
406     Set<NTuple<Descriptor>> inKeySet = in.keySet();
407     for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
408       NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
409       Hashtable<FlatNode, Boolean> inPair = in.get(inKey);
410
411       Set<FlatNode> pairKeySet = inPair.keySet();
412       for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
413         FlatNode pairKey = (FlatNode) iterator2.next();
414         Boolean inFlag = inPair.get(pairKey);
415
416         Hashtable<FlatNode, Boolean> currPair = curr.get(inKey);
417         if (currPair == null) {
418           currPair = new Hashtable<FlatNode, Boolean>();
419           curr.put(inKey, currPair);
420         }
421
422         Boolean currFlag = currPair.get(pairKey);
423         // by default, flag is set by false
424         if (currFlag == null) {
425           currFlag = Boolean.FALSE;
426         }
427         currFlag = Boolean.valueOf(inFlag.booleanValue() | currFlag.booleanValue());
428         currPair.put(pairKey, currFlag);
429       }
430
431     }
432
433   }
434
435   private void methodReadOverWriteAnalysis() {
436     // perform method READ/OVERWRITE analysis
437     Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
438     methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
439
440     LinkedList<MethodDescriptor> sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
441
442     // no need to analyze method having ssjava loop
443     methodContainingSSJavaLoop = sortedDescriptors.removeFirst();
444
445     // current descriptors to visit in fixed-point interprocedural analysis,
446     // prioritized by
447     // dependency in the call graph
448     Stack<MethodDescriptor> methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
449
450     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
451     methodDescriptorToVistSet.addAll(sortedDescriptors);
452
453     while (!sortedDescriptors.isEmpty()) {
454       MethodDescriptor md = sortedDescriptors.removeFirst();
455       methodDescriptorsToVisitStack.add(md);
456     }
457
458     // analyze scheduled methods until there are no more to visit
459     while (!methodDescriptorsToVisitStack.isEmpty()) {
460       // start to analyze leaf node
461       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
462       FlatMethod fm = state.getMethodFlat(md);
463
464       Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
465       Set<NTuple<Descriptor>> overWriteSet = new HashSet<NTuple<Descriptor>>();
466
467       methodReadOverWrite_analyzeMethod(fm, readSet, overWriteSet);
468
469       Set<NTuple<Descriptor>> prevRead = mapFlatMethodToRead.get(fm);
470       Set<NTuple<Descriptor>> prevOverWrite = mapFlatMethodToOverWrite.get(fm);
471
472       if (!(readSet.equals(prevRead) && overWriteSet.equals(prevOverWrite))) {
473         mapFlatMethodToRead.put(fm, readSet);
474         mapFlatMethodToOverWrite.put(fm, overWriteSet);
475
476         // results for callee changed, so enqueue dependents caller for
477         // further
478         // analysis
479         Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
480         while (depsItr.hasNext()) {
481           MethodDescriptor methodNext = depsItr.next();
482           if (!methodDescriptorsToVisitStack.contains(methodNext)
483               && methodDescriptorToVistSet.contains(methodNext)) {
484             methodDescriptorsToVisitStack.add(methodNext);
485           }
486
487         }
488
489       }
490
491     }
492
493   }
494
495   private void methodReadOverWrite_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
496       Set<NTuple<Descriptor>> overWriteSet) {
497     if (state.SSJAVADEBUG) {
498       System.out.println("Definitely written Analyzing: " + fm);
499     }
500
501     // intraprocedural analysis
502     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
503     Set<FlatNode> visited = new HashSet<FlatNode>();
504     flatNodesToVisit.add(fm);
505
506     while (!flatNodesToVisit.isEmpty()) {
507       FlatNode fn = flatNodesToVisit.iterator().next();
508       flatNodesToVisit.remove(fn);
509       visited.add(fn);
510
511       Set<NTuple<Descriptor>> curr = new HashSet<NTuple<Descriptor>>();
512
513       for (int i = 0; i < fn.numPrev(); i++) {
514         FlatNode prevFn = fn.getPrev(i);
515         Set<NTuple<Descriptor>> in = mapFlatNodeToWrittenSet.get(prevFn);
516         if (in != null) {
517           merge(curr, in);
518         }
519       }
520
521       methodReadOverWrite_nodeActions(fn, curr, readSet, overWriteSet);
522
523       mapFlatNodeToWrittenSet.put(fn, curr);
524
525       for (int i = 0; i < fn.numNext(); i++) {
526         FlatNode nn = fn.getNext(i);
527         if (!visited.contains(nn)) {
528           flatNodesToVisit.add(nn);
529         }
530       }
531
532     }
533
534   }
535
536   private void methodReadOverWrite_nodeActions(FlatNode fn, Set<NTuple<Descriptor>> writtenSet,
537       Set<NTuple<Descriptor>> readSet, Set<NTuple<Descriptor>> overWriteSet) {
538     TempDescriptor lhs;
539     TempDescriptor rhs;
540     FieldDescriptor fld;
541
542     switch (fn.kind()) {
543     case FKind.FlatMethod: {
544
545       // set up initial heap paths for method parameters
546       FlatMethod fm = (FlatMethod) fn;
547       for (int i = 0; i < fm.numParameters(); i++) {
548         TempDescriptor param = fm.getParameter(i);
549         NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
550         heapPath.add(param);
551         mapHeapPath.put(param, heapPath);
552       }
553     }
554       break;
555
556     case FKind.FlatOpNode: {
557       FlatOpNode fon = (FlatOpNode) fn;
558       // for a normal assign node, need to propagate lhs's heap path to
559       // rhs
560       if (fon.getOp().getOp() == Operation.ASSIGN) {
561         rhs = fon.getLeft();
562         lhs = fon.getDest();
563
564         NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
565         if (rhsHeapPath != null) {
566           mapHeapPath.put(lhs, mapHeapPath.get(rhs));
567         }
568
569       }
570     }
571       break;
572
573     case FKind.FlatFieldNode:
574     case FKind.FlatElementNode: {
575
576       // y=x.f;
577
578       FlatFieldNode ffn = (FlatFieldNode) fn;
579       lhs = ffn.getDst();
580       rhs = ffn.getSrc();
581       fld = ffn.getField();
582
583       // set up heap path
584       NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
585       NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
586       readingHeapPath.add(fld);
587       mapHeapPath.put(lhs, readingHeapPath);
588
589       // read (x.f)
590       if (fld.getType().isImmutable()) {
591         // if WT doesnot have hp(x.f), add hp(x.f) to READ
592         if (!writtenSet.contains(readingHeapPath)) {
593           readSet.add(readingHeapPath);
594         }
595       }
596
597       // need to kill hp(x.f) from WT
598       writtenSet.remove(readingHeapPath);
599
600     }
601       break;
602
603     case FKind.FlatSetFieldNode:
604     case FKind.FlatSetElementNode: {
605
606       // x.f=y;
607       FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
608       lhs = fsfn.getDst();
609       fld = fsfn.getField();
610       rhs = fsfn.getSrc();
611
612       // set up heap path
613       NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
614       NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
615       newHeapPath.add(fld);
616       mapHeapPath.put(fld, newHeapPath);
617
618       // write(x.f)
619       // need to add hp(y) to WT
620       writtenSet.add(newHeapPath);
621
622     }
623       break;
624
625     case FKind.FlatCall: {
626
627       FlatCall fc = (FlatCall) fn;
628
629       bindHeapPathCallerArgWithCaleeParam(fc);
630
631       // add heap path, which is an element of READ_bound set and is not
632       // an
633       // element of WT set, to the caller's READ set
634       for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
635         NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
636         if (!writtenSet.contains(read)) {
637           readSet.add(read);
638         }
639       }
640       writtenSet.removeAll(calleeUnionBoundReadSet);
641
642       // add heap path, which is an element of OVERWRITE_bound set, to the
643       // caller's WT set
644       for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
645         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
646         writtenSet.add(write);
647       }
648
649     }
650       break;
651
652     case FKind.FlatExit: {
653       // merge the current written set with OVERWRITE set
654       merge(overWriteSet, writtenSet);
655     }
656       break;
657
658     }
659
660   }
661
662   private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
663     if (curr.isEmpty()) {
664       // WrittenSet has a special initial value which covers all possible
665       // elements
666       // For the first time of intersection, we can take all previous set
667       curr.addAll(in);
668     } else {
669       // otherwise, current set is the intersection of the two sets
670       curr.retainAll(in);
671     }
672
673   }
674
675   // combine two heap path
676   private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
677     NTuple<Descriptor> combined = new NTuple<Descriptor>();
678
679     for (int i = 0; i < callerIn.size(); i++) {
680       combined.add(callerIn.get(i));
681     }
682
683     // the first element of callee's heap path represents parameter
684     // so we skip the first one since it is already added from caller's heap
685     // path
686     for (int i = 1; i < calleeIn.size(); i++) {
687       combined.add(calleeIn.get(i));
688     }
689
690     return combined;
691   }
692
693   private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
694       Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
695       Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
696
697     Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
698
699     Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
700     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
701       Integer idx = (Integer) iterator.next();
702
703       NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
704       TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
705
706       for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
707         NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
708         if (element.startsWith(calleeParam)) {
709           NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
710           boundedCalleeSet.add(boundElement);
711         }
712
713       }
714
715     }
716     return boundedCalleeSet;
717
718   }
719
720   // Borrowed it from disjoint analysis
721   private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
722
723     Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
724
725     LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
726
727     Iterator<MethodDescriptor> itr = toSort.iterator();
728     while (itr.hasNext()) {
729       MethodDescriptor d = itr.next();
730
731       if (!discovered.contains(d)) {
732         dfsVisit(d, toSort, sorted, discovered);
733       }
734     }
735
736     return sorted;
737   }
738
739   // While we're doing DFS on call graph, remember
740   // dependencies for efficient queuing of methods
741   // during interprocedural analysis:
742   //
743   // a dependent of a method decriptor d for this analysis is:
744   // 1) a method or task that invokes d
745   // 2) in the descriptorsToAnalyze set
746   private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
747       LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
748
749     discovered.add(md);
750
751     // otherwise call graph guides DFS
752     Iterator itr = callGraph.getCallerSet(md).iterator();
753     while (itr.hasNext()) {
754       MethodDescriptor dCaller = (MethodDescriptor) itr.next();
755
756       // only consider callers in the original set to analyze
757       if (!toSort.contains(dCaller)) {
758         continue;
759       }
760
761       if (!discovered.contains(dCaller)) {
762         addDependent(md, // callee
763             dCaller // caller
764         );
765
766         dfsVisit(dCaller, toSort, sorted, discovered);
767       }
768     }
769
770     // for leaf-nodes last now!
771     sorted.addLast(md);
772   }
773
774   // a dependent of a method decriptor d for this analysis is:
775   // 1) a method or task that invokes d
776   // 2) in the descriptorsToAnalyze set
777   private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
778     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
779     if (deps == null) {
780       deps = new HashSet<MethodDescriptor>();
781     }
782     deps.add(caller);
783     mapDescriptorToSetDependents.put(callee, deps);
784   }
785
786   private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
787     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
788     if (deps == null) {
789       deps = new HashSet<MethodDescriptor>();
790       mapDescriptorToSetDependents.put(callee, deps);
791     }
792     return deps;
793   }
794
795   private NTuple<Descriptor> computePath(TempDescriptor td) {
796     // generate proper path fot input td
797     // if td is local variable, it just generate one element tuple path
798     if (mapHeapPath.containsKey(td)) {
799       return mapHeapPath.get(td);
800     } else {
801       NTuple<Descriptor> path = new NTuple<Descriptor>();
802       path.add(td);
803       return path;
804     }
805   }
806
807 }