changes: generated annotated code but it still causes type errors + re-formatting...
[IRC.git] / Robust / src / Analysis / SSJava / DefinitelyWrittenCheck.java
1 package Analysis.SSJava;
2
3 import java.util.Enumeration;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.Set;
9 import java.util.Stack;
10
11 import Analysis.Liveness;
12 import Analysis.CallGraph.CallGraph;
13 import Analysis.Loops.LoopFinder;
14 import IR.Descriptor;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
17 import IR.Operation;
18 import IR.State;
19 import IR.TypeDescriptor;
20 import IR.TypeExtension;
21 import IR.Flat.FKind;
22 import IR.Flat.FlatCall;
23 import IR.Flat.FlatElementNode;
24 import IR.Flat.FlatFieldNode;
25 import IR.Flat.FlatLiteralNode;
26 import IR.Flat.FlatMethod;
27 import IR.Flat.FlatNew;
28 import IR.Flat.FlatNode;
29 import IR.Flat.FlatOpNode;
30 import IR.Flat.FlatSetElementNode;
31 import IR.Flat.FlatSetFieldNode;
32 import IR.Flat.TempDescriptor;
33 import IR.Tree.Modifiers;
34
35 public class DefinitelyWrittenCheck {
36
37   SSJavaAnalysis ssjava;
38   State state;
39   CallGraph callGraph;
40
41   Liveness liveness;
42
43   int debugcount = 0;
44
45   // maps a flat node to its WrittenSet: this keeps all heap path overwritten
46   // previously.
47   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
48
49   // maps a temp descriptor to its heap path
50   // each temp descriptor has a unique heap path since we do not allow any
51   // alias.
52   private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
53
54   // maps a temp descriptor to its composite location
55   private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
56
57   // maps a flat method to the READ that is the set of heap path that is
58   // expected to be written before method invocation
59   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
60
61   // maps a flat method to the must-write set that is the set of heap path that
62   // is overwritten on every possible path during method invocation
63   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
64
65   // maps a flat method to the DELETE SET that is a set of heap path to shared
66   // locations that are
67   // written to but not overwritten by the higher value
68   private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
69
70   // maps a flat method to the S SET that is a set of heap path to shared
71   // locations that are overwritten by the higher value
72   private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToSharedLocMap;
73
74   // maps a flat method to the may-wirte set that is the set of heap path that
75   // might be written to
76   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
77
78   // maps a call site to the read set contributed by all callees
79   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
80
81   // maps a call site to the must write set contributed by all callees
82   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
83
84   // maps a call site to the may read set contributed by all callees
85   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
86
87   // points to method containing SSJAVA Loop
88   private MethodDescriptor methodContainingSSJavaLoop;
89
90   // maps a flatnode to definitely written analysis mapping M
91   private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
92
93   // maps shared location to the set of descriptors which belong to the shared
94   // location
95
96   // keep current descriptors to visit in fixed-point interprocedural analysis,
97   private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
98
99   // when analyzing flatcall, need to re-schedule set of callee
100   private Set<MethodDescriptor> calleesToEnqueue;
101
102   private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
103
104   public static final String arrayElementFieldName = "___element_";
105   static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
106
107   // maps a method descriptor to the merged incoming caller's current
108   // reading status
109   // it is for setting clearance flag when all read set is overwritten
110   private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
111
112   private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
113
114   private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
115   private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
116
117   private LoopFinder ssjavaLoop;
118   private Set<FlatNode> loopIncElements;
119
120   private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
121   private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
122   private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
123   private SharedLocMap calleeUnionBoundDeleteSet;
124   private SharedLocMap calleeIntersectBoundSharedSet;
125
126   Set<TempDescriptor> liveInTempSetToEventLoop;
127
128   private Hashtable<Descriptor, Location> mapDescToLocation;
129
130   private TempDescriptor LOCAL;
131
132   public static int MAXAGE = 1;
133
134   public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
135     this.state = state;
136     this.ssjava = ssjava;
137     this.callGraph = ssjava.getCallGraph();
138     this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
139     this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
140     this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
141     this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
142     this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
143     this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
144     this.mapFlatNodetoEventLoopMap =
145         new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
146     this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
147     this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
148     this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
149
150     this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
151     this.calleesToEnqueue = new HashSet<MethodDescriptor>();
152     this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
153     this.LOCAL = new TempDescriptor("LOCAL");
154     this.mapDescToLocation = new Hashtable<Descriptor, Location>();
155     this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
156     this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
157     this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
158     this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
159     this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
160     this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
161     this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
162     this.calleeUnionBoundDeleteSet = new SharedLocMap();
163     this.calleeIntersectBoundSharedSet = new SharedLocMap();
164     this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
165     this.mapMethodToSharedLocCoverSet =
166         new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
167     this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
168     this.liveness = new Liveness();
169     this.liveInTempSetToEventLoop = new HashSet<TempDescriptor>();
170   }
171
172   public void definitelyWrittenCheck() {
173     if (!ssjava.getAnnotationRequireSet().isEmpty()) {
174       initialize();
175
176       methodReadWriteSetAnalysis();
177       computeSharedCoverSet();
178
179       sharedLocAnalysis();
180
181       eventLoopAnalysis();
182
183     }
184   }
185
186   private void sharedLocAnalysis() {
187
188     // perform method READ/OVERWRITE analysis
189     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
190
191     // current descriptors to visit in fixed-point interprocedural analysis,
192     // prioritized by
193     // dependency in the call graph
194     methodDescriptorsToVisitStack.clear();
195
196     descriptorListToAnalyze.removeFirst();
197
198     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
199     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
200
201     while (!descriptorListToAnalyze.isEmpty()) {
202       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
203       methodDescriptorsToVisitStack.add(md);
204     }
205
206     // analyze scheduled methods until there are no more to visit
207     while (!methodDescriptorsToVisitStack.isEmpty()) {
208       // start to analyze leaf node
209       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
210       FlatMethod fm = state.getMethodFlat(md);
211
212       SharedLocMap sharedLocMap = new SharedLocMap();
213       SharedLocMap deleteSet = new SharedLocMap();
214
215       sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
216       SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
217       SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
218
219       if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
220         mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
221         mapFlatMethodToDeleteSet.put(fm, deleteSet);
222
223         // results for callee changed, so enqueue dependents caller for
224         // further
225         // analysis
226         Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
227         while (depsItr.hasNext()) {
228           MethodDescriptor methodNext = depsItr.next();
229           if (!methodDescriptorsToVisitStack.contains(methodNext)
230               && methodDescriptorToVistSet.contains(methodNext)) {
231             methodDescriptorsToVisitStack.add(methodNext);
232           }
233
234         }
235
236       }
237
238     }
239
240     sharedLoc_analyzeEventLoop();
241
242   }
243
244   private void sharedLoc_analyzeEventLoop() {
245     if (state.SSJAVADEBUG) {
246       System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
247     }
248     SharedLocMap sharedLocMap = new SharedLocMap();
249     SharedLocMap deleteSet = new SharedLocMap();
250     sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
251         ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, true);
252
253   }
254
255   private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
256       SharedLocMap deleteSet) {
257     if (state.SSJAVADEBUG) {
258       System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
259     }
260
261     sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
262
263   }
264
265   private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
266       SharedLocMap deleteSet, boolean isEventLoopBody) {
267
268     // intraprocedural analysis
269     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
270     flatNodesToVisit.add(startNode);
271
272     while (!flatNodesToVisit.isEmpty()) {
273       FlatNode fn = flatNodesToVisit.iterator().next();
274       flatNodesToVisit.remove(fn);
275
276       SharedLocMap currSharedSet = new SharedLocMap();
277       SharedLocMap currDeleteSet = new SharedLocMap();
278
279       for (int i = 0; i < fn.numPrev(); i++) {
280         FlatNode prevFn = fn.getPrev(i);
281         SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
282         if (inSharedLoc != null) {
283           mergeSharedLocMap(currSharedSet, inSharedLoc);
284         }
285
286         SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
287         if (inDeleteLoc != null) {
288           mergeDeleteSet(currDeleteSet, inDeleteLoc);
289         }
290       }
291
292       sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
293           isEventLoopBody);
294
295       SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
296       SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
297
298       if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet))) {
299         mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
300         mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
301         for (int i = 0; i < fn.numNext(); i++) {
302           FlatNode nn = fn.getNext(i);
303           if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
304             flatNodesToVisit.add(nn);
305           }
306
307         }
308       }
309
310     }
311
312   }
313
314   private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
315       SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
316       boolean isEventLoopBody) {
317
318     MethodDescriptor md = fm.getMethod();
319
320     SharedLocMap killSet = new SharedLocMap();
321     SharedLocMap genSet = new SharedLocMap();
322
323     TempDescriptor lhs;
324     TempDescriptor rhs;
325     FieldDescriptor fld;
326
327     switch (fn.kind()) {
328
329     case FKind.FlatOpNode: {
330
331       if (isEventLoopBody) {
332         FlatOpNode fon = (FlatOpNode) fn;
333
334         if (fon.getOp().getOp() == Operation.ASSIGN) {
335           lhs = fon.getDest();
336           rhs = fon.getLeft();
337
338           if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
339               && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
340
341             if (mapHeapPath.containsKey(rhs)) {
342               Location dstLoc = getLocation(lhs);
343               if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
344                 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
345                 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
346
347                 Location srcLoc = getLocation(lhs);
348
349                 // computing gen/kill set
350                 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
351
352                 if (!ssjava.isSameHeightWrite(fn)) {
353                   computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
354                   updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
355                 } else {
356                   computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
357                   updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
358                 }
359
360               }
361             } else {
362               break;
363             }
364
365           }
366
367         }
368
369       }
370
371     }
372       break;
373
374     case FKind.FlatSetFieldNode:
375     case FKind.FlatSetElementNode: {
376
377       Location fieldLoc;
378       if (fn.kind() == FKind.FlatSetFieldNode) {
379         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
380         lhs = fsfn.getDst();
381         fld = fsfn.getField();
382         rhs = fsfn.getSrc();
383         fieldLoc = (Location) fld.getType().getExtension();
384       } else {
385         break;
386       }
387
388       if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
389         // if the field belongs to the local lattice, no reason to calculate
390         // shared location
391         break;
392       }
393
394       NTuple<Location> fieldLocTuple = new NTuple<Location>();
395       if (fld.isStatic()) {
396         if (fld.isFinal()) {
397           // in this case, fld has TOP location
398           Location topLocation = Location.createTopLocation(md);
399           fieldLocTuple.add(topLocation);
400         } else {
401           fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
402           if (fn.kind() == FKind.FlatSetFieldNode) {
403             fieldLocTuple.add((Location) fld.getType().getExtension());
404           }
405         }
406
407       } else {
408         fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
409         if (fn.kind() == FKind.FlatSetFieldNode) {
410           fieldLocTuple.add((Location) fld.getType().getExtension());
411         }
412       }
413
414       // shared loc extension
415       Location srcLoc = getLocation(rhs);
416       if (ssjava.isSharedLocation(fieldLoc)) {
417         // only care the case that loc(f) is shared location
418         // write(field)
419
420         // NTuple<Location> fieldLocTuple = new NTuple<Location>();
421         // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
422         // fieldLocTuple.add(fieldLoc);
423
424         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
425         fldHeapPath.addAll(computePath(lhs));
426         if (fn.kind() == FKind.FlatSetFieldNode) {
427           fldHeapPath.add(fld);
428         }
429
430         // computing gen/kill set
431         computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
432
433         if (!ssjava.isSameHeightWrite(fn)) {
434           computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
435           updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
436         } else {
437           computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
438           updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
439         }
440
441       }
442
443     }
444       break;
445
446     case FKind.FlatCall: {
447       FlatCall fc = (FlatCall) fn;
448
449       bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
450
451       // computing gen/kill set
452       generateKILLSetForFlatCall(curr, killSet);
453       generateGENSetForFlatCall(curr, genSet);
454
455     }
456       break;
457
458     case FKind.FlatExit: {
459       // merge the current delete/shared loc mapping
460       mergeSharedLocMap(sharedLocMap, curr);
461       mergeDeleteSet(deleteSet, currDeleteSet);
462
463     }
464       break;
465
466     }
467
468     computeNewMapping(curr, killSet, genSet);
469
470   }
471
472   private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
473
474     Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
475     for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
476       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
477       genSet.addWrite(locTupleKey, curr.get(locTupleKey));
478       genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
479
480       genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
481     }
482
483   }
484
485   private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
486
487     Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
488     for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
489       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
490       killSet.addWrite(locTupleKey, curr.get(locTupleKey));
491     }
492
493   }
494
495   private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
496
497     Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
498
499     for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
500       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
501
502       Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
503       currDeleteSet.addWrite(locTupleKey, inSet);
504
505     }
506   }
507
508   private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
509     curr.kill(killSet);
510     curr.gen(genSet);
511   }
512
513   private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
514       NTuple<Descriptor> hp) {
515     currDeleteSet.removeWrite(locTuple, hp);
516   }
517
518   private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
519       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
520     currDeleteSet.addWrite(locTuple, hp);
521   }
522
523   private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
524       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
525     Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
526
527     if (currWriteSet != null) {
528       genSet.addWrite(locTuple, currWriteSet);
529     }
530
531     genSet.addWrite(locTuple, hp);
532   }
533
534   private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
535       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
536     Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
537
538     if (currWriteSet != null) {
539       genSet.addWrite(locTuple, currWriteSet);
540     }
541     genSet.removeWrite(locTuple, hp);
542   }
543
544   private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
545       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
546
547     Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
548     if (writeSet != null) {
549       killSet.addWrite(locTuple, writeSet);
550     }
551
552   }
553
554   private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
555
556     Set<NTuple<Location>> locTupleKeySet = in.keySet();
557     for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
558       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
559
560       Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
561       Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
562       if (currSet == null) {
563         currSet = new HashSet<NTuple<Descriptor>>();
564         currSet.addAll(inSet);
565         currSharedSet.addWrite(locTupleKey, currSet);
566       }
567       currSet.retainAll(inSet);
568     }
569
570   }
571
572   private void computeSharedCoverSet() {
573     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
574
575     // current descriptors to visit in fixed-point interprocedural analysis,
576     // prioritized by
577     // dependency in the call graph
578     methodDescriptorsToVisitStack.clear();
579
580     descriptorListToAnalyze.removeFirst();
581
582     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
583     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
584
585     while (!descriptorListToAnalyze.isEmpty()) {
586       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
587       methodDescriptorsToVisitStack.add(md);
588     }
589
590     // analyze scheduled methods until there are no more to visit
591     while (!methodDescriptorsToVisitStack.isEmpty()) {
592       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
593       FlatMethod fm = state.getMethodFlat(md);
594       computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
595     }
596
597     computeSharedCoverSetForEventLoop();
598
599   }
600
601   private void computeSharedCoverSetForEventLoop() {
602     computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
603   }
604
605   private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
606
607     // System.out.println("computeSharedCoverSet_analyzeMethod=" + fm);
608     MethodDescriptor md = fm.getMethod();
609
610     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
611
612     Set<FlatNode> visited = new HashSet<FlatNode>();
613
614     if (onlyVisitSSJavaLoop) {
615       flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
616     } else {
617       flatNodesToVisit.add(fm);
618     }
619
620     while (!flatNodesToVisit.isEmpty()) {
621       FlatNode fn = flatNodesToVisit.iterator().next();
622       flatNodesToVisit.remove(fn);
623       visited.add(fn);
624
625       computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
626
627       for (int i = 0; i < fn.numNext(); i++) {
628         FlatNode nn = fn.getNext(i);
629
630         if (!visited.contains(nn)) {
631           if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
632             flatNodesToVisit.add(nn);
633           }
634         }
635
636       }
637
638     }
639
640   }
641
642   private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
643       boolean isEventLoopBody) {
644     TempDescriptor lhs;
645     TempDescriptor rhs;
646     FieldDescriptor fld;
647
648     switch (fn.kind()) {
649
650     case FKind.FlatLiteralNode: {
651       FlatLiteralNode fln = (FlatLiteralNode) fn;
652       lhs = fln.getDst();
653
654       NTuple<Location> lhsLocTuple = new NTuple<Location>();
655       lhsLocTuple.add(Location.createTopLocation(md));
656       mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
657
658       if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
659           && !lhs.getSymbol().startsWith("srctmp")) {
660         // only need to care about composite location case here
661         if (lhs.getType().getExtension() instanceof SSJavaType) {
662           CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
663           Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
664         }
665       }
666
667     }
668       break;
669
670     case FKind.FlatOpNode: {
671       FlatOpNode fon = (FlatOpNode) fn;
672       // for a normal assign node, need to propagate lhs's location path to
673       // rhs
674       if (fon.getOp().getOp() == Operation.ASSIGN) {
675         rhs = fon.getLeft();
676         lhs = fon.getDest();
677
678         if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
679             && !lhs.getSymbol().startsWith("rightop")) {
680
681           if (mapHeapPath.containsKey(rhs)) {
682             NTuple<Location> rhsLocTuple = new NTuple<Location>();
683             NTuple<Location> lhsLocTuple = new NTuple<Location>();
684             if (mapDescriptorToLocationPath.containsKey(rhs)) {
685               mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
686               lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
687             } else {
688               // rhs side
689               if (rhs.getType().getExtension() != null
690                   && rhs.getType().getExtension() instanceof SSJavaType) {
691
692                 if (((SSJavaType) rhs.getType().getExtension()).getCompLoc() != null) {
693                   rhsLocTuple.addAll(((SSJavaType) rhs.getType().getExtension()).getCompLoc()
694                       .getTuple());
695                 }
696
697               } else {
698                 NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
699                 if (locTuple != null) {
700                   rhsLocTuple.addAll(locTuple);
701                 }
702               }
703               if (rhsLocTuple.size() > 0) {
704                 mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
705               }
706
707               // lhs side
708               if (lhs.getType().getExtension() != null
709                   && lhs.getType().getExtension() instanceof SSJavaType) {
710                 lhsLocTuple.addAll(((SSJavaType) lhs.getType().getExtension()).getCompLoc()
711                     .getTuple());
712                 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
713               } else if (mapDescriptorToLocationPath.get(rhs) != null) {
714                 // propagate rhs's location to lhs
715                 lhsLocTuple.addAll(mapDescriptorToLocationPath.get(rhs));
716                 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
717               }
718             }
719
720             if (isEventLoopBody && lhs.getType().isPrimitive()
721                 && !lhs.getSymbol().startsWith("srctmp")) {
722
723               NTuple<Descriptor> lhsHeapPath = computePath(lhs);
724
725               if (lhsLocTuple != null) {
726                 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
727               }
728
729             }
730           } else {
731             break;
732           }
733
734         }
735
736       }
737     }
738       break;
739
740     case FKind.FlatSetFieldNode:
741     case FKind.FlatSetElementNode: {
742
743       // x.f=y;
744
745       if (fn.kind() == FKind.FlatSetFieldNode) {
746         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
747         lhs = fsfn.getDst();
748         fld = fsfn.getField();
749         rhs = fsfn.getSrc();
750       } else {
751         FlatSetElementNode fsen = (FlatSetElementNode) fn;
752         lhs = fsen.getDst();
753         rhs = fsen.getSrc();
754         TypeDescriptor td = lhs.getType().dereference();
755         fld = getArrayField(td);
756       }
757
758       NTuple<Location> lhsLocTuple = new NTuple<Location>();
759       if (fld.isStatic()) {
760         if (fld.isFinal()) {
761           // in this case, fld has TOP location
762           Location topLocation = Location.createTopLocation(md);
763           lhsLocTuple.add(topLocation);
764         } else {
765           lhsLocTuple.addAll(deriveGlobalLocationTuple(md));
766         }
767       } else {
768         lhsLocTuple.addAll(deriveLocationTuple(md, lhs));
769       }
770
771       mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
772
773       NTuple<Location> fieldLocTuple = new NTuple<Location>();
774       fieldLocTuple.addAll(lhsLocTuple);
775
776       if (fn.kind() == FKind.FlatSetFieldNode) {
777         fieldLocTuple.add((Location) fld.getType().getExtension());
778       }
779
780       if (mapHeapPath.containsKey(lhs)) {
781         // fields reachable from the param can have heap path entry.
782         NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
783         lhsHeapPath.addAll(mapHeapPath.get(lhs));
784
785         Location fieldLocation;
786         if (fn.kind() == FKind.FlatSetFieldNode) {
787           fieldLocation = getLocation(fld);
788         } else {
789           fieldLocation = getLocation(lhsHeapPath.get(getArrayBaseDescriptorIdx(lhsHeapPath)));
790         }
791
792         // Location fieldLocation = getLocation(lhs);
793         if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
794           // if the field belongs to the local lattice, no reason to calculate
795           // shared location
796           break;
797         }
798
799         if (ssjava.isSharedLocation(fieldLocation)) {
800
801           NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
802           fieldHeapPath.addAll(computePath(lhs));
803           if (fn.kind() == FKind.FlatSetFieldNode) {
804             fieldHeapPath.add(fld);
805           }
806
807           addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
808
809         }
810       }
811
812     }
813       break;
814
815     case FKind.FlatElementNode:
816     case FKind.FlatFieldNode: {
817
818       // x=y.f;
819
820       if (fn.kind() == FKind.FlatFieldNode) {
821         FlatFieldNode ffn = (FlatFieldNode) fn;
822         lhs = ffn.getDst();
823         rhs = ffn.getSrc();
824         fld = ffn.getField();
825       } else {
826         FlatElementNode fen = (FlatElementNode) fn;
827         lhs = fen.getDst();
828         rhs = fen.getSrc();
829         TypeDescriptor td = rhs.getType().dereference();
830         fld = getArrayField(td);
831       }
832
833       NTuple<Location> locTuple = new NTuple<Location>();
834
835       if (fld.isStatic()) {
836
837         if (fld.isFinal()) {
838           // in this case, fld has TOP location
839           Location topLocation = Location.createTopLocation(md);
840           locTuple.add(topLocation);
841         } else {
842           locTuple.addAll(deriveGlobalLocationTuple(md));
843           if (fn.kind() == FKind.FlatFieldNode) {
844             locTuple.add((Location) fld.getType().getExtension());
845           }
846         }
847
848       } else {
849         locTuple.addAll(deriveLocationTuple(md, rhs));
850         if (fn.kind() == FKind.FlatFieldNode) {
851           locTuple.add((Location) fld.getType().getExtension());
852         }
853       }
854
855       mapDescriptorToLocationPath.put(lhs, locTuple);
856
857     }
858       break;
859
860     case FKind.FlatCall: {
861
862       FlatCall fc = (FlatCall) fn;
863
864       bindLocationPathCallerArgWithCalleeParam(md, fc);
865
866     }
867       break;
868
869     case FKind.FlatNew: {
870
871       FlatNew fnew = (FlatNew) fn;
872       TempDescriptor dst = fnew.getDst();
873       NTuple<Location> locTuple = deriveLocationTuple(md, dst);
874
875       if (locTuple != null) {
876         NTuple<Location> dstLocTuple = new NTuple<Location>();
877         dstLocTuple.addAll(locTuple);
878         mapDescriptorToLocationPath.put(dst, dstLocTuple);
879       }
880
881     }
882       break;
883     }
884   }
885
886   private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
887       NTuple<Descriptor> heapPath) {
888
889     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
890     if (map == null) {
891       map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
892       mapMethodToSharedLocCoverSet.put(md, map);
893     }
894
895     Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
896     if (writeSet == null) {
897       writeSet = new HashSet<NTuple<Descriptor>>();
898       map.put(locTuple, writeSet);
899     }
900     writeSet.add(heapPath);
901
902   }
903
904   private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
905
906     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
907       // ssjava util case!
908       // have write effects on the first argument
909       TempDescriptor arg = fc.getArg(0);
910       NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
911       NTuple<Descriptor> argHeapPath = computePath(arg);
912       addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
913     } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
914
915       // if arg is not primitive type, we need to propagate maywritten set to
916       // the caller's location path
917
918       MethodDescriptor mdCallee = fc.getMethod();
919       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
920       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
921
922       // create mapping from arg idx to its heap paths
923       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
924           new Hashtable<Integer, NTuple<Descriptor>>();
925
926       // create mapping from arg idx to its location paths
927       Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
928           new Hashtable<Integer, NTuple<Location>>();
929
930       if (fc.getThis() != null) {
931
932         if (mapHeapPath.containsKey(fc.getThis())) {
933
934           // setup heap path for 'this'
935           NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
936           thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
937           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
938
939           // setup location path for 'this'
940           NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
941           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
942
943         }
944       }
945
946       for (int i = 0; i < fc.numArgs(); i++) {
947         TempDescriptor arg = fc.getArg(i);
948         // create mapping arg to loc path
949
950         if (mapHeapPath.containsKey(arg)) {
951           // setup heap path
952           NTuple<Descriptor> argHeapPath = mapHeapPath.get(arg);
953           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
954           // setup loc path
955           NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
956           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
957         }
958
959       }
960
961       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
962         MethodDescriptor callee = (MethodDescriptor) iterator.next();
963         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
964
965         // binding caller's args and callee's params
966
967         Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
968             new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
969
970         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
971             new Hashtable<Integer, TempDescriptor>();
972         int offset = 0;
973         if (calleeFlatMethod.getMethod().isStatic()) {
974           // static method does not have implicit 'this' arg
975           offset = 1;
976         }
977
978         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
979           TempDescriptor param = calleeFlatMethod.getParameter(i);
980           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
981
982           NTuple<Descriptor> calleeHeapPath = computePath(param);
983
984           NTuple<Descriptor> argHeapPath =
985               mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
986
987           if (argHeapPath != null) {
988             mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
989
990           }
991
992         }
993
994         Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
995         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
996           Integer idx = (Integer) iterator2.next();
997
998           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
999
1000           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1001           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1002
1003           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1004           NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1005
1006           if (!calleeParam.getType().isPrimitive()) {
1007             createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1008                 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1009                 mapParamHeapPathToCallerArgHeapPath);
1010           }
1011         }
1012
1013       }
1014
1015     }
1016
1017   }
1018
1019   private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1020       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1021
1022     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1023         new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1024
1025     Set<NTuple<Location>> keySet = map.keySet();
1026
1027     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1028       NTuple<Location> key = (NTuple<Location>) iterator.next();
1029       if (key.startsWith(in)) {
1030         matchedMapping.put(key, map.get(key));
1031       }
1032     }
1033
1034     return matchedMapping;
1035
1036   }
1037
1038   private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1039       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1040       NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1041       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1042
1043     // propagate may-written-set associated with the key that is started with
1044     // calleepath to the caller
1045     // 1) makes a new key by combining caller path and callee path(except local
1046     // loc element of param)
1047     // 2) create new mapping of may-written-set of callee path to caller path
1048
1049     // extract all may written effect accessed through callee param path
1050     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1051         mapMethodToSharedLocCoverSet.get(callee);
1052
1053     if (calleeMapping == null) {
1054       return;
1055     }
1056
1057     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1058         mapMethodToSharedLocCoverSet.get(caller);
1059
1060     if (callerMapping == null) {
1061       callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1062       mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1063     }
1064
1065     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1066         getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1067
1068     Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1069
1070     for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1071       NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1072
1073       Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1074
1075       if (calleeMayWriteSet != null) {
1076
1077         Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1078
1079         Set<NTuple<Descriptor>> boundSet =
1080             convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1081                 mapParamHeapPathToCallerArgHeapPath);
1082
1083         boundMayWriteSet.addAll(boundSet);
1084
1085         NTuple<Location> newKey = new NTuple<Location>();
1086         newKey.addAll(callerArgLocPath);
1087         // need to replace the local location with the caller's path so skip the
1088         // local location of the parameter
1089         for (int i = 1; i < calleeKey.size(); i++) {
1090           newKey.add(calleeKey.get(i));
1091         }
1092
1093         callerMapping.union(newKey, boundMayWriteSet);
1094       }
1095
1096     }
1097
1098   }
1099
1100   private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1101       NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1102       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1103       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1104
1105     Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1106
1107     // replace callee's param path with caller's arg path
1108     for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1109       NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1110
1111       NTuple<Descriptor> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1112
1113       NTuple<Descriptor> callerArgHeapPath =
1114           mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1115
1116       NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1117       boundHeapPath.addAll(callerArgHeapPath);
1118
1119       for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1120         boundHeapPath.add(calleeWriteHeapPath.get(i));
1121       }
1122
1123       boundSet.add(boundHeapPath);
1124
1125     }
1126
1127     return boundSet;
1128   }
1129
1130   private Location getLocation(Descriptor d) {
1131
1132     if (d instanceof FieldDescriptor) {
1133       TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1134       if (te != null) {
1135         return (Location) te;
1136       }
1137     } else {
1138       assert d instanceof TempDescriptor;
1139       TempDescriptor td = (TempDescriptor) d;
1140
1141       TypeExtension te = td.getType().getExtension();
1142       if (te != null) {
1143         if (te instanceof SSJavaType) {
1144           SSJavaType ssType = (SSJavaType) te;
1145           if (ssType.getCompLoc() != null) {
1146             CompositeLocation comp = ssType.getCompLoc();
1147             return comp.get(comp.getSize() - 1);
1148           } else {
1149             return null;
1150           }
1151         } else {
1152           return (Location) te;
1153         }
1154       }
1155     }
1156
1157     return mapDescToLocation.get(d);
1158   }
1159
1160   private void eventLoopAnalysis() {
1161     // perform second stage analysis: intraprocedural analysis ensure that
1162     // all
1163     // variables are definitely written in-between the same read
1164
1165     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1166     flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1167
1168     while (!flatNodesToVisit.isEmpty()) {
1169       FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1170       flatNodesToVisit.remove(fn);
1171
1172       Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1173
1174       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1175           new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1176       for (int i = 0; i < fn.numPrev(); i++) {
1177         FlatNode nn = fn.getPrev(i);
1178         Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1179         if (in != null) {
1180           union(curr, in);
1181         }
1182       }
1183
1184       eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1185
1186       // if a new result, schedule forward nodes for analysis
1187       if (!curr.equals(prev)) {
1188         mapFlatNodetoEventLoopMap.put(fn, curr);
1189
1190         for (int i = 0; i < fn.numNext(); i++) {
1191           FlatNode nn = fn.getNext(i);
1192           if (loopIncElements.contains(nn)) {
1193             flatNodesToVisit.add(nn);
1194           }
1195
1196         }
1197       }
1198     }
1199   }
1200
1201   private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1202       Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1203
1204     Set<NTuple<Descriptor>> inKeySet = in.keySet();
1205     for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1206       NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1207       Set<WriteAge> inSet = in.get(inKey);
1208
1209       Set<WriteAge> currSet = curr.get(inKey);
1210
1211       if (currSet == null) {
1212         currSet = new HashSet<WriteAge>();
1213         curr.put(inKey, currSet);
1214       }
1215       currSet.addAll(inSet);
1216     }
1217
1218   }
1219
1220   private void eventLoopAnalysis_nodeAction(FlatNode fn,
1221       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1222
1223     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1224         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1225     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1226         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1227
1228     if (fn.equals(loopEntrance)) {
1229       // it reaches loop entrance: changes all flag to true
1230       Set<NTuple<Descriptor>> keySet = curr.keySet();
1231       for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1232         NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1233         Set<WriteAge> writeAgeSet = curr.get(key);
1234
1235         Set<WriteAge> incSet = new HashSet<WriteAge>();
1236         incSet.addAll(writeAgeSet);
1237         writeAgeSet.clear();
1238
1239         for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1240           WriteAge writeAge = (WriteAge) iterator2.next();
1241           WriteAge newWriteAge = writeAge.copy();
1242           newWriteAge.inc();
1243           writeAgeSet.add(newWriteAge);
1244         }
1245
1246       }
1247
1248     } else {
1249       TempDescriptor lhs;
1250       TempDescriptor rhs;
1251       FieldDescriptor fld;
1252
1253       switch (fn.kind()) {
1254
1255       case FKind.FlatOpNode: {
1256         FlatOpNode fon = (FlatOpNode) fn;
1257         lhs = fon.getDest();
1258         rhs = fon.getLeft();
1259
1260         if (fon.getOp().getOp() == Operation.ASSIGN) {
1261
1262           if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1263               && !lhs.getSymbol().startsWith("rightop")) {
1264
1265             boolean hasWriteEffect = false;
1266
1267             if (rhs.getType().getExtension() instanceof SSJavaType
1268                 && lhs.getType().getExtension() instanceof SSJavaType) {
1269
1270               CompositeLocation rhsCompLoc =
1271                   ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1272
1273               CompositeLocation lhsCompLoc =
1274                   ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1275
1276               if (lhsCompLoc != rhsCompLoc) {
1277                 // have a write effect!
1278                 hasWriteEffect = true;
1279               }
1280
1281             } else if (lhs.getType().isImmutable()) {
1282               hasWriteEffect = true;
1283             }
1284
1285             if (hasWriteEffect && mapHeapPath.containsKey(lhs)) {
1286               // write(lhs)
1287               NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1288               lhsHeapPath.addAll(mapHeapPath.get(lhs));
1289
1290               Location lhsLoc = getLocation(lhs);
1291               if (ssjava.isSharedLocation(lhsLoc)) {
1292
1293                 NTuple<Descriptor> varHeapPath = computePath(lhs);
1294                 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1295
1296                 Set<NTuple<Descriptor>> writtenSet =
1297                     mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1298
1299                 if (isCovered(varLocTuple, writtenSet)) {
1300                   computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1301                   computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1302                 } else {
1303                   computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1304                 }
1305
1306               } else {
1307
1308                 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1309                 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1310               }
1311
1312               Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1313               checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1314             }
1315
1316           }
1317
1318         }
1319
1320       }
1321         break;
1322
1323       case FKind.FlatFieldNode:
1324       case FKind.FlatElementNode: {
1325
1326         if (fn.kind() == FKind.FlatFieldNode) {
1327           FlatFieldNode ffn = (FlatFieldNode) fn;
1328           lhs = ffn.getDst();
1329           rhs = ffn.getSrc();
1330           fld = ffn.getField();
1331         } else {
1332           FlatElementNode fen = (FlatElementNode) fn;
1333           lhs = fen.getDst();
1334           rhs = fen.getSrc();
1335           TypeDescriptor td = rhs.getType().dereference();
1336           fld = getArrayField(td);
1337         }
1338
1339         // read field
1340         NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1341         NTuple<Descriptor> fldHeapPath;
1342         if (srcHeapPath != null) {
1343           fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1344         } else {
1345           // if srcHeapPath is null, it is static reference
1346           fldHeapPath = new NTuple<Descriptor>();
1347           fldHeapPath.add(rhs);
1348         }
1349         fldHeapPath.add(fld);
1350
1351         Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1352
1353         checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1354
1355       }
1356         break;
1357
1358       case FKind.FlatSetFieldNode:
1359       case FKind.FlatSetElementNode: {
1360
1361         if (fn.kind() == FKind.FlatSetFieldNode) {
1362           FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1363           lhs = fsfn.getDst();
1364           fld = fsfn.getField();
1365         } else {
1366           FlatSetElementNode fsen = (FlatSetElementNode) fn;
1367           lhs = fsen.getDst();
1368           rhs = fsen.getSrc();
1369           TypeDescriptor td = lhs.getType().dereference();
1370           fld = getArrayField(td);
1371         }
1372
1373         // set up heap path
1374         NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1375         if (lhsHeapPath != null) {
1376           // write(field)
1377           NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1378           if (fn.kind() == FKind.FlatSetFieldNode) {
1379             fldHeapPath.add(fld);
1380           }
1381
1382           // shared loc extension
1383           Location fieldLoc;
1384           if (fn.kind() == FKind.FlatSetFieldNode) {
1385             fieldLoc = (Location) fld.getType().getExtension();
1386           } else {
1387             NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1388             fieldLoc = locTuple.get(locTuple.size() - 1);
1389           }
1390
1391           if (ssjava.isSharedLocation(fieldLoc)) {
1392
1393             NTuple<Location> fieldLocTuple = new NTuple<Location>();
1394             fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1395             if (fn.kind() == FKind.FlatSetFieldNode) {
1396               fieldLocTuple.add(fieldLoc);
1397             }
1398
1399             Set<NTuple<Descriptor>> writtenSet =
1400                 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1401
1402             if (isCovered(fieldLocTuple, writtenSet)) {
1403               computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1404               computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1405             } else {
1406               computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1407             }
1408
1409           } else {
1410             computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1411             computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1412           }
1413
1414         }
1415
1416       }
1417         break;
1418
1419       case FKind.FlatCall: {
1420         FlatCall fc = (FlatCall) fn;
1421
1422         SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1423         generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1424         generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1425
1426       }
1427         break;
1428
1429       }
1430
1431       computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1432       if (fn instanceof FlatCall) {
1433         checkManyRead((FlatCall) fn, curr);
1434       }
1435
1436     }
1437
1438   }
1439
1440   private void computeGENSetForSharedNonCoverWrite(
1441       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1442       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1443
1444     Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1445     if (writeAgeSet == null) {
1446       writeAgeSet = new HashSet<WriteAge>();
1447       genSet.put(heapPath, writeAgeSet);
1448     }
1449
1450     writeAgeSet.add(new WriteAge(1));
1451
1452   }
1453
1454   private void computeGENSetForSharedAllCoverWrite(
1455       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1456       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1457
1458     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1459       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1460
1461       Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1462       writeAgeSet.add(new WriteAge(0));
1463
1464       genSet.put(writeHeapPath, writeAgeSet);
1465     }
1466
1467   }
1468
1469   private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1470       Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1471
1472     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1473       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1474       Set<WriteAge> writeSet = curr.get(writeHeapPath);
1475       if (writeSet != null) {
1476         killSet.put(writeHeapPath, writeSet);
1477       }
1478     }
1479
1480   }
1481
1482   private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1483
1484     if (inSet == null) {
1485       return false;
1486     }
1487
1488     Set<NTuple<Descriptor>> coverSet =
1489         mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1490
1491     return inSet.containsAll(coverSet);
1492   }
1493
1494   private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1495
1496     Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1497
1498     for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1499       NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1500       Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1501       checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1502     }
1503
1504   }
1505
1506   private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1507
1508     if (writeAgeSet != null) {
1509       for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1510         WriteAge writeAge = (WriteAge) iterator.next();
1511         if (writeAge.getAge() > MAXAGE) {
1512           generateErrorMessage(path, fn);
1513         }
1514       }
1515     }
1516   }
1517
1518   private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1519
1520     Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1521     if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1522
1523       NTuple<Location> locPathTuple = getLocationTuple(path);
1524       Set<NTuple<Descriptor>> coverSet =
1525           mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1526       throw new Error("Shared memory locations, which is reachable through references " + path
1527           + ", are not completely overwritten by the higher values at "
1528           + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1529           + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1530
1531     } else {
1532       throw new Error(
1533           "Memory location, which is reachable through references "
1534               + path
1535               + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1536               + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1537               + fn.getNumLine());
1538     }
1539
1540   }
1541
1542   private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1543       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1544
1545     Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1546
1547     for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1548       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1549
1550       if (!isSharedLocation(heapPath)) {
1551         addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1552       } else {
1553         // if the current heap path is shared location
1554
1555         NTuple<Location> locTuple = getLocationTuple(heapPath);
1556
1557         Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1558
1559         if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1560           // if it is covered, add all of heap paths belong to the same shared
1561           // loc with write age 0
1562
1563           for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1564             NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1565             addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1566           }
1567
1568         } else {
1569           // if not covered, add write age 1 to the heap path that is
1570           // may-written but not covered
1571           addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1572         }
1573
1574       }
1575
1576     }
1577
1578   }
1579
1580   private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1581       Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1582
1583     Set<WriteAge> currSet = map.get(heapPath);
1584     if (currSet == null) {
1585       currSet = new HashSet<WriteAge>();
1586       map.put(heapPath, currSet);
1587     }
1588
1589     currSet.add(age);
1590   }
1591
1592   private void generateKILLSetForFlatCall(FlatCall fc,
1593       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1594       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1595
1596     Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1597
1598     for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1599       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1600
1601       if (isSharedLocation(heapPath)) {
1602         NTuple<Location> locTuple = getLocationTuple(heapPath);
1603
1604         if (isCovered(locTuple, sharedLocMap.get(locTuple)) && curr.containsKey(heapPath)) {
1605           // if it is shared loc and corresponding shared loc has been covered
1606           KILLSet.put(heapPath, curr.get(heapPath));
1607         }
1608       } else {
1609
1610         for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1611           NTuple<Descriptor> key = e.nextElement();
1612           if (key.startsWith(heapPath)) {
1613             KILLSet.put(key, curr.get(key));
1614           }
1615         }
1616
1617       }
1618
1619     }
1620
1621   }
1622
1623   private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1624
1625     for (int i = heapPath.size() - 1; i >= 0; i--) {
1626       if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1627         return i;
1628       }
1629     }
1630
1631     return -1;
1632
1633   }
1634
1635   private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1636
1637     Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1638
1639     return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1640
1641   }
1642
1643   private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1644
1645     NTuple<Location> locTuple = new NTuple<Location>();
1646
1647     locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1648
1649     for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1650       locTuple.add(getLocation(heapPath.get(i)));
1651     }
1652
1653     return locTuple;
1654   }
1655
1656   private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1657       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1658       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1659
1660     for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1661       NTuple<Descriptor> key = e.nextElement();
1662
1663       Set<WriteAge> writeAgeSet = curr.get(key);
1664       if (writeAgeSet == null) {
1665         writeAgeSet = new HashSet<WriteAge>();
1666         curr.put(key, writeAgeSet);
1667       }
1668       writeAgeSet.removeAll(KILLSet.get(key));
1669     }
1670
1671     for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1672       NTuple<Descriptor> key = e.nextElement();
1673
1674       Set<WriteAge> currWriteAgeSet = curr.get(key);
1675       if (currWriteAgeSet == null) {
1676         currWriteAgeSet = new HashSet<WriteAge>();
1677         curr.put(key, currWriteAgeSet);
1678       }
1679       currWriteAgeSet.addAll(GENSet.get(key));
1680     }
1681
1682   }
1683
1684   private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1685       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1686
1687     // generate write age 0 for the field being written to
1688     Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1689     writeAgeSet.add(new WriteAge(0));
1690     GENSet.put(fldHeapPath, writeAgeSet);
1691
1692   }
1693
1694   private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1695       NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1696
1697     // removes all of heap path that starts with prefix 'hp'
1698     // since any reference overwrite along heap path gives overwriting side
1699     // effects on the value
1700
1701     Set<NTuple<Descriptor>> keySet = curr.keySet();
1702     for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1703       NTuple<Descriptor> key = iter.next();
1704       if (key.startsWith(hp)) {
1705         KILLSet.put(key, curr.get(key));
1706       }
1707     }
1708
1709   }
1710
1711   private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1712     // compute all possible callee set
1713     // transform all READ/WRITE set from the any possible
1714     // callees to the caller
1715     calleeUnionBoundReadSet.clear();
1716     calleeIntersectBoundMustWriteSet.clear();
1717     calleeUnionBoundMayWriteSet.clear();
1718
1719     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1720       // ssjava util case!
1721       // have write effects on the first argument
1722       TempDescriptor arg = fc.getArg(0);
1723       NTuple<Descriptor> argHeapPath = computePath(arg);
1724       calleeIntersectBoundMustWriteSet.add(argHeapPath);
1725       calleeUnionBoundMayWriteSet.add(argHeapPath);
1726     } else {
1727       MethodDescriptor mdCallee = fc.getMethod();
1728       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1729       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1730
1731       // create mapping from arg idx to its heap paths
1732       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1733           new Hashtable<Integer, NTuple<Descriptor>>();
1734
1735       // arg idx is starting from 'this' arg
1736       if (fc.getThis() != null) {
1737         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1738         if (thisHeapPath != null) {
1739           // if 'this' does not have heap path, it is local reference
1740           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1741         }
1742       }
1743
1744       for (int i = 0; i < fc.numArgs(); i++) {
1745         TempDescriptor arg = fc.getArg(i);
1746         NTuple<Descriptor> argHeapPath = computePath(arg);
1747         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1748       }
1749
1750       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1751         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1752         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1753
1754         // binding caller's args and callee's params
1755
1756         Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1757         if (calleeReadSet == null) {
1758           calleeReadSet = new HashSet<NTuple<Descriptor>>();
1759           mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1760         }
1761
1762         Set<NTuple<Descriptor>> calleeMustWriteSet =
1763             mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1764
1765         if (calleeMustWriteSet == null) {
1766           calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1767           mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1768         }
1769
1770         Set<NTuple<Descriptor>> calleeMayWriteSet =
1771             mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1772
1773         if (calleeMayWriteSet == null) {
1774           calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1775           mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1776         }
1777
1778         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1779             new Hashtable<Integer, TempDescriptor>();
1780         int offset = 0;
1781         if (calleeFlatMethod.getMethod().isStatic()) {
1782           // static method does not have implicit 'this' arg
1783           offset = 1;
1784         }
1785         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1786           TempDescriptor param = calleeFlatMethod.getParameter(i);
1787           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1788         }
1789
1790         Set<NTuple<Descriptor>> calleeBoundReadSet =
1791             bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1792         // union of the current read set and the current callee's
1793         // read set
1794         calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1795
1796         Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1797             bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1798         // intersection of the current overwrite set and the current
1799         // callee's
1800         // overwrite set
1801         merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1802
1803         Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1804             bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1805         calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1806       }
1807
1808     }
1809
1810   }
1811
1812   private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1813       FlatCall fc) {
1814
1815     calleeIntersectBoundSharedSet.clear();
1816     calleeUnionBoundDeleteSet.clear();
1817
1818     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1819       // ssjava util case!
1820       // have write effects on the first argument
1821       TempDescriptor arg = fc.getArg(0);
1822       NTuple<Descriptor> argHeapPath = computePath(arg);
1823
1824       // convert heap path to location path
1825       NTuple<Location> argLocTuple = new NTuple<Location>();
1826       argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1827       for (int i = 1; i < argHeapPath.size(); i++) {
1828         argLocTuple.add(getLocation(argHeapPath.get(i)));
1829       }
1830
1831       calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1832
1833     } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1834
1835       // if arg is not primitive type, we need to propagate maywritten set to
1836       // the caller's location path
1837
1838       MethodDescriptor mdCallee = fc.getMethod();
1839       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1840       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1841
1842       // create mapping from arg idx to its heap paths
1843       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1844           new Hashtable<Integer, NTuple<Descriptor>>();
1845
1846       // arg idx is starting from 'this' arg
1847       if (fc.getThis() != null) {
1848         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1849         if (thisHeapPath == null) {
1850           // method is called without creating new flat node representing 'this'
1851           thisHeapPath = new NTuple<Descriptor>();
1852           thisHeapPath.add(fc.getThis());
1853         }
1854
1855         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1856       }
1857
1858       for (int i = 0; i < fc.numArgs(); i++) {
1859         TempDescriptor arg = fc.getArg(i);
1860         NTuple<Descriptor> argHeapPath = computePath(arg);
1861         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1862       }
1863
1864       // create mapping from arg idx to its location paths
1865       Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1866           new Hashtable<Integer, NTuple<Location>>();
1867
1868       // arg idx is starting from 'this' arg
1869       if (fc.getThis() != null) {
1870         NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1871         if (thisLocationPath != null) {
1872           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1873         }
1874       }
1875
1876       for (int i = 0; i < fc.numArgs(); i++) {
1877         TempDescriptor arg = fc.getArg(i);
1878         NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1879         if (argLocationPath != null) {
1880           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1881         }
1882       }
1883
1884       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1885         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1886         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1887
1888         // binding caller's args and callee's params
1889
1890         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1891             new Hashtable<Integer, TempDescriptor>();
1892         int offset = 0;
1893         if (calleeFlatMethod.getMethod().isStatic()) {
1894           // static method does not have implicit 'this' arg
1895           offset = 1;
1896         }
1897         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1898           TempDescriptor param = calleeFlatMethod.getParameter(i);
1899           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1900         }
1901
1902         Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1903         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1904           Integer idx = (Integer) iterator2.next();
1905           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1906           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1907
1908           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1909           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1910           SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1911           SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1912
1913           if (calleeDeleteSet != null) {
1914             createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1915                 calleeLocationPath, calleeDeleteSet);
1916           }
1917
1918           if (calleeSharedLocMap != null) {
1919             createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1920                 calleeLocationPath, calleeSharedLocMap);
1921           }
1922
1923         }
1924
1925       }
1926     }
1927
1928   }
1929
1930   private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1931       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1932       SharedLocMap calleeDeleteSet) {
1933
1934     SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1935
1936     Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1937     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1938       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1939       Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1940       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1941         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1942         calleeUnionBoundDeleteSet.addWrite(
1943             bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1944             bindHeapPath(callerArgHeapPath, calleeHeapPath));
1945       }
1946     }
1947
1948   }
1949
1950   private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1951       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1952       SharedLocMap calleeSharedLocMap) {
1953
1954     SharedLocMap calleeParamSharedSet =
1955         calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
1956
1957     Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
1958     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1959       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1960       Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
1961       Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
1962       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1963         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1964         boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
1965       }
1966       calleeIntersectBoundSharedSet.intersect(
1967           bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
1968     }
1969
1970   }
1971
1972   private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
1973     NTuple<Location> locPath = new NTuple<Location>();
1974     locPath.addAll(start);
1975     for (int i = 1; i < end.size(); i++) {
1976       locPath.add(end.get(i));
1977     }
1978     return locPath;
1979   }
1980
1981   private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
1982     NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1983     heapPath.addAll(start);
1984     for (int i = 1; i < end.size(); i++) {
1985       heapPath.add(end.get(i));
1986     }
1987     return heapPath;
1988   }
1989
1990   private void initialize() {
1991     // First, identify ssjava loop entrace
1992
1993     // no need to analyze method having ssjava loop
1994     methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
1995
1996     FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
1997     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1998     flatNodesToVisit.add(fm);
1999
2000     LoopFinder loopFinder = new LoopFinder(fm);
2001
2002     while (!flatNodesToVisit.isEmpty()) {
2003       FlatNode fn = flatNodesToVisit.iterator().next();
2004       flatNodesToVisit.remove(fn);
2005
2006       String label = (String) state.fn2labelMap.get(fn);
2007       if (label != null) {
2008
2009         if (label.equals(ssjava.SSJAVA)) {
2010           ssjava.setSSJavaLoopEntrance(fn);
2011           break;
2012         }
2013       }
2014
2015       for (int i = 0; i < fn.numNext(); i++) {
2016         FlatNode nn = fn.getNext(i);
2017         flatNodesToVisit.add(nn);
2018       }
2019     }
2020
2021     assert ssjava.getSSJavaLoopEntrance() != null;
2022
2023     // assume that ssjava loop is top-level loop in method, not nested loop
2024     Set nestedLoop = loopFinder.nestedLoops();
2025     for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2026       LoopFinder lf = (LoopFinder) loopIter.next();
2027       if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2028         ssjavaLoop = lf;
2029       }
2030     }
2031
2032     assert ssjavaLoop != null;
2033
2034     loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2035
2036     // perform topological sort over the set of methods accessed by the main
2037     // event loop
2038     // Set<MethodDescriptor> methodDescriptorsToAnalyze = new
2039     // HashSet<MethodDescriptor>();
2040     // methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2041     // sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2042
2043     liveInTempSetToEventLoop =
2044         liveness.getLiveInTemps(state.getMethodFlat(methodContainingSSJavaLoop),
2045             ssjava.getSSJavaLoopEntrance());
2046   }
2047
2048   private void methodReadWriteSetAnalysis() {
2049     // perform method READ/OVERWRITE analysis
2050     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2051
2052     // current descriptors to visit in fixed-point interprocedural analysis,
2053     // prioritized by
2054     // dependency in the call graph
2055     methodDescriptorsToVisitStack.clear();
2056
2057     descriptorListToAnalyze.removeFirst();
2058
2059     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2060     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2061
2062     while (!descriptorListToAnalyze.isEmpty()) {
2063       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2064       methodDescriptorsToVisitStack.add(md);
2065     }
2066
2067     // analyze scheduled methods until there are no more to visit
2068     while (!methodDescriptorsToVisitStack.isEmpty()) {
2069       // start to analyze leaf node
2070       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2071       FlatMethod fm = state.getMethodFlat(md);
2072
2073       Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2074       Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2075       Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2076
2077       methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2078
2079       Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2080       Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2081       Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2082
2083       if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2084           .equals(prevMayWrite))) {
2085         mapFlatMethodToReadSet.put(fm, readSet);
2086         mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2087         mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2088
2089         // results for callee changed, so enqueue dependents caller for
2090         // further
2091         // analysis
2092         Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2093         while (depsItr.hasNext()) {
2094           MethodDescriptor methodNext = depsItr.next();
2095           if (!methodDescriptorsToVisitStack.contains(methodNext)
2096               && methodDescriptorToVistSet.contains(methodNext)) {
2097             methodDescriptorsToVisitStack.add(methodNext);
2098           }
2099
2100         }
2101
2102       }
2103
2104     }
2105
2106     methodReadWriteSetAnalysisToEventLoopBody();
2107
2108   }
2109
2110   private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2111       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2112     if (state.SSJAVADEBUG) {
2113       System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2114     }
2115
2116     methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2117
2118   }
2119
2120   private void methodReadWriteSetAnalysisToEventLoopBody() {
2121
2122     // perform method read/write analysis for Event Loop Body
2123
2124     FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2125
2126     if (state.SSJAVADEBUG) {
2127       System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2128           + flatMethodContainingSSJavaLoop);
2129     }
2130
2131     Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2132     Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2133     Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2134
2135     mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2136     mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2137     mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2138
2139     for (Iterator iterator = liveInTempSetToEventLoop.iterator(); iterator.hasNext();) {
2140       TempDescriptor liveIn = (TempDescriptor) iterator.next();
2141       NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2142       heapPath.add(liveIn);
2143       mapHeapPath.put(liveIn, heapPath);
2144     }
2145
2146     methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2147         mayWriteSet, true);
2148
2149   }
2150
2151   private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2152       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2153       boolean isEventLoopBody) {
2154
2155     // intraprocedural analysis
2156     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2157     flatNodesToVisit.add(startNode);
2158
2159     while (!flatNodesToVisit.isEmpty()) {
2160       FlatNode fn = flatNodesToVisit.iterator().next();
2161       flatNodesToVisit.remove(fn);
2162
2163       Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2164
2165       for (int i = 0; i < fn.numPrev(); i++) {
2166         FlatNode prevFn = fn.getPrev(i);
2167         Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2168         if (in != null) {
2169           merge(currMustWriteSet, in);
2170         }
2171       }
2172
2173       methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2174           isEventLoopBody);
2175
2176       Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2177
2178       if (!currMustWriteSet.equals(mustSetPrev)) {
2179         mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2180         for (int i = 0; i < fn.numNext(); i++) {
2181           FlatNode nn = fn.getNext(i);
2182           if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2183             flatNodesToVisit.add(nn);
2184           }
2185
2186         }
2187       }
2188
2189     }
2190
2191   }
2192
2193   private void methodReadWriteSet_nodeActions(FlatNode fn,
2194       Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2195       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2196       boolean isEventLoopBody) {
2197
2198     TempDescriptor lhs;
2199     TempDescriptor rhs;
2200     FieldDescriptor fld;
2201
2202     switch (fn.kind()) {
2203     case FKind.FlatMethod: {
2204
2205       // set up initial heap paths for method parameters
2206       FlatMethod fm = (FlatMethod) fn;
2207       for (int i = 0; i < fm.numParameters(); i++) {
2208         TempDescriptor param = fm.getParameter(i);
2209         NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2210         heapPath.add(param);
2211         mapHeapPath.put(param, heapPath);
2212       }
2213     }
2214       break;
2215
2216     case FKind.FlatOpNode: {
2217       FlatOpNode fon = (FlatOpNode) fn;
2218       // for a normal assign node, need to propagate lhs's heap path to
2219       // rhs
2220
2221       if (fon.getOp().getOp() == Operation.ASSIGN) {
2222         rhs = fon.getLeft();
2223         lhs = fon.getDest();
2224
2225         NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2226
2227         // if (lhs.getType().isPrimitive()) {
2228         // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2229         // lhsHeapPath.add(lhs);
2230         // mapHeapPath.put(lhs, lhsHeapPath);
2231         // } else
2232
2233         if (rhsHeapPath != null && (!lhs.getType().isPrimitive())) {
2234           mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2235         } else {
2236           break;
2237           // if (isEventLoopBody) {
2238           // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2239           // lhsHeapPath.add(rhs);
2240           // mapHeapPath.put(lhs, lhsHeapPath);
2241           // } else {
2242           // break;
2243           // }
2244         }
2245
2246         // shared loc extension
2247         if (isEventLoopBody) {
2248           if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2249
2250             if (rhs.getType().getExtension() instanceof Location
2251                 && lhs.getType().getExtension() instanceof CompositeLocation) {
2252               // rhs is field!
2253               Location rhsLoc = (Location) rhs.getType().getExtension();
2254
2255               CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2256               Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2257
2258               NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2259               for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2260                 heapPath.add(rhsHeapPath.get(i));
2261               }
2262
2263               NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2264               writeHeapPath.addAll(heapPath);
2265               writeHeapPath.add(lhs);
2266
2267             }
2268           }
2269         }
2270
2271       }
2272     }
2273       break;
2274
2275     case FKind.FlatElementNode:
2276     case FKind.FlatFieldNode: {
2277
2278       // x=y.f;
2279
2280       if (fn.kind() == FKind.FlatFieldNode) {
2281         FlatFieldNode ffn = (FlatFieldNode) fn;
2282         lhs = ffn.getDst();
2283         rhs = ffn.getSrc();
2284         fld = ffn.getField();
2285       } else {
2286         FlatElementNode fen = (FlatElementNode) fn;
2287         lhs = fen.getDst();
2288         rhs = fen.getSrc();
2289         TypeDescriptor td = rhs.getType().dereference();
2290         fld = getArrayField(td);
2291       }
2292
2293       if (fld.isFinal()) {
2294         // if field is final no need to check
2295         break;
2296       }
2297
2298       // set up heap path
2299       NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2300       if (srcHeapPath != null) {
2301         // if lhs srcHeapPath is null, it means that it is not reachable from
2302         // callee's parameters. so just ignore it
2303
2304         NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2305         if (fn.kind() == FKind.FlatFieldNode) {
2306           readingHeapPath.add(fld);
2307         }
2308
2309         mapHeapPath.put(lhs, readingHeapPath);
2310
2311         // read (x.f)
2312         if (fld.getType().isImmutable()) {
2313           // if WT doesnot have hp(x.f), add hp(x.f) to READ
2314           if (!currMustWriteSet.contains(readingHeapPath)) {
2315             readSet.add(readingHeapPath);
2316           }
2317         }
2318
2319         // no need to kill hp(x.f) from WT
2320       }
2321
2322     }
2323       break;
2324
2325     case FKind.FlatSetFieldNode:
2326     case FKind.FlatSetElementNode: {
2327
2328       // x.f=y;
2329
2330       if (fn.kind() == FKind.FlatSetFieldNode) {
2331         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2332         lhs = fsfn.getDst();
2333         fld = fsfn.getField();
2334         rhs = fsfn.getSrc();
2335       } else {
2336         FlatSetElementNode fsen = (FlatSetElementNode) fn;
2337         lhs = fsen.getDst();
2338         rhs = fsen.getSrc();
2339         TypeDescriptor td = lhs.getType().dereference();
2340         fld = getArrayField(td);
2341       }
2342
2343       // set up heap path
2344       NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2345
2346       if (lhsHeapPath != null) {
2347         // if lhs heap path is null, it means that it is not reachable from
2348         // callee's parameters. so just ignore it
2349         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2350         if (fn.kind() != FKind.FlatSetElementNode) {
2351           fldHeapPath.add(fld);
2352         }
2353         // mapHeapPath.put(fld, fldHeapPath);
2354
2355         // write(x.f)
2356         // need to add hp(y) to WT
2357         if (fn.kind() != FKind.FlatSetElementNode) {
2358           currMustWriteSet.add(fldHeapPath);
2359         }
2360         mayWriteSet.add(fldHeapPath);
2361
2362       }
2363
2364     }
2365       break;
2366
2367     case FKind.FlatCall: {
2368
2369       FlatCall fc = (FlatCall) fn;
2370
2371       bindHeapPathCallerArgWithCalleeParam(fc);
2372
2373       Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2374       boundReadSet.addAll(calleeUnionBoundReadSet);
2375
2376       Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2377       boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2378
2379       Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2380       boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2381
2382       mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2383       mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2384       mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2385
2386       // add heap path, which is an element of READ_bound set and is not
2387       // an
2388       // element of WT set, to the caller's READ set
2389       for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2390         NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2391         if (!currMustWriteSet.contains(read)) {
2392           readSet.add(read);
2393         }
2394       }
2395
2396       // add heap path, which is an element of OVERWRITE_bound set, to the
2397       // caller's WT set
2398       for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2399         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2400         currMustWriteSet.add(write);
2401       }
2402
2403       // add heap path, which is an element of WRITE_BOUND set, to the
2404       // caller's writeSet
2405       for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2406         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2407         mayWriteSet.add(write);
2408       }
2409
2410     }
2411       break;
2412
2413     case FKind.FlatExit: {
2414       // merge the current written set with OVERWRITE set
2415       merge(mustWriteSet, currMustWriteSet);
2416     }
2417       break;
2418
2419     }
2420
2421   }
2422
2423   static public FieldDescriptor getArrayField(TypeDescriptor td) {
2424     FieldDescriptor fd = mapTypeToArrayField.get(td);
2425     if (fd == null) {
2426       fd =
2427           new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2428               false);
2429       mapTypeToArrayField.put(td, fd);
2430     }
2431     return fd;
2432   }
2433
2434   private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2435     if (curr.isEmpty()) {
2436       // set has a special initial value which covers all possible
2437       // elements
2438       // For the first time of intersection, we can take all previous set
2439       curr.addAll(in);
2440     } else {
2441       // otherwise, current set is the intersection of the two sets
2442       curr.retainAll(in);
2443     }
2444
2445   }
2446
2447   // combine two heap path
2448   private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2449     NTuple<Descriptor> combined = new NTuple<Descriptor>();
2450
2451     for (int i = 0; i < callerIn.size(); i++) {
2452       combined.add(callerIn.get(i));
2453     }
2454
2455     // the first element of callee's heap path represents parameter
2456     // so we skip the first one since it is already added from caller's heap
2457     // path
2458     for (int i = 1; i < calleeIn.size(); i++) {
2459       combined.add(calleeIn.get(i));
2460     }
2461
2462     return combined;
2463   }
2464
2465   private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2466       Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2467       Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2468
2469     Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2470
2471     Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2472     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2473       Integer idx = (Integer) iterator.next();
2474
2475       NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2476       TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2477       for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2478         NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2479         if (element.startsWith(calleeParam)) {
2480           NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2481           boundedCalleeSet.add(boundElement);
2482         }
2483
2484       }
2485
2486     }
2487     return boundedCalleeSet;
2488
2489   }
2490
2491   private NTuple<Descriptor> computePath(Descriptor td) {
2492     // generate proper path fot input td
2493     // if td is local variable, it just generate one element tuple path
2494     if (mapHeapPath.containsKey(td)) {
2495       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2496       rtrHeapPath.addAll(mapHeapPath.get(td));
2497       return rtrHeapPath;
2498     } else {
2499       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2500       rtrHeapPath.add(td);
2501       return rtrHeapPath;
2502     }
2503   }
2504
2505   private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2506     String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2507     Location thisLoc = new Location(md, thisLocIdentifier);
2508     NTuple<Location> locTuple = new NTuple<Location>();
2509     locTuple.add(thisLoc);
2510     return locTuple;
2511   }
2512
2513   private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2514     String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2515     Location globalLoc = new Location(md, globalLocIdentifier);
2516     NTuple<Location> locTuple = new NTuple<Location>();
2517     locTuple.add(globalLoc);
2518     return locTuple;
2519   }
2520
2521   private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2522
2523     assert td.getType() != null;
2524
2525     if (mapDescriptorToLocationPath.containsKey(td)) {
2526       NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2527       NTuple<Location> rtrPath = new NTuple<Location>();
2528       rtrPath.addAll(locPath);
2529       return rtrPath;
2530     } else {
2531       if (td.getSymbol().startsWith("this")) {
2532         NTuple<Location> thisPath = deriveThisLocationTuple(md);
2533         NTuple<Location> rtrPath = new NTuple<Location>();
2534         rtrPath.addAll(thisPath);
2535         return rtrPath;
2536       } else {
2537
2538         if (td.getType().getExtension() != null) {
2539           SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2540           if (ssJavaType.getCompLoc() != null) {
2541             NTuple<Location> rtrPath = new NTuple<Location>();
2542             rtrPath.addAll(ssJavaType.getCompLoc().getTuple());
2543             return rtrPath;
2544           }
2545         }
2546
2547         return null;
2548
2549       }
2550     }
2551   }
2552 }