now it works fine with three benchmarks
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.StringTokenizer;
18 import java.util.Vector;
19
20 import Analysis.CallGraph.CallGraph;
21 import Analysis.Loops.GlobalFieldType;
22 import Analysis.Loops.LoopOptimize;
23 import Analysis.Loops.LoopTerminate;
24 import IR.AnnotationDescriptor;
25 import IR.ClassDescriptor;
26 import IR.Descriptor;
27 import IR.FieldDescriptor;
28 import IR.MethodDescriptor;
29 import IR.State;
30 import IR.SymbolTable;
31 import IR.TypeUtil;
32 import IR.Flat.BuildFlat;
33 import IR.Flat.FlatMethod;
34 import IR.Flat.FlatNode;
35 import Util.Pair;
36
37 public class SSJavaAnalysis {
38
39   public static final String SSJAVA = "SSJAVA";
40   public static final String LATTICE = "LATTICE";
41   public static final String METHODDEFAULT = "METHODDEFAULT";
42   public static final String THISLOC = "THISLOC";
43   public static final String GLOBALLOC = "GLOBALLOC";
44   public static final String RETURNLOC = "RETURNLOC";
45   public static final String PCLOC = "PCLOC";
46   public static final String LOC = "LOC";
47   public static final String DELTA = "DELTA";
48   public static final String TERMINATE = "TERMINATE";
49   public static final String DELEGATE = "DELEGATE";
50   public static final String DELEGATETHIS = "DELEGATETHIS";
51   public static final String TRUST = "TRUST";
52
53   public static final String TOP = "_top_";
54   public static final String BOTTOM = "_bottom_";
55
56   State state;
57   TypeUtil tu;
58   FlowDownCheck flowDownChecker;
59   MethodAnnotationCheck methodAnnotationChecker;
60   BuildFlat bf;
61
62   // set containing method requires to be annoated
63   Set<MethodDescriptor> annotationRequireSet;
64
65   // class -> field lattice
66   Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
67
68   // class -> default local variable lattice
69   Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
70
71   // method -> local variable lattice
72   Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
73
74   // method set that does not want to have loop termination analysis
75   Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
76
77   // map shared location to its descriptors
78   Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
79
80   // set containing a class that has at least one annoated method
81   Set<ClassDescriptor> annotationRequireClassSet;
82
83   // the set of method descriptor required to check the linear type property
84   Set<MethodDescriptor> linearTypeCheckMethodSet;
85
86   // the set of method descriptors annotated as "TRUST"
87   Set<MethodDescriptor> trustWorthyMDSet;
88
89   // method -> the initial program counter location
90   Map<MethodDescriptor, CompositeLocation> md2pcLoc;
91
92   // points to method containing SSJAVA Loop
93   private MethodDescriptor methodContainingSSJavaLoop;
94
95   private FlatNode ssjavaLoopEntrance;
96
97   // keep the field ownership from the linear type checking
98   Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
99
100   Set<FlatNode> sameHeightWriteFlatNodeSet;
101
102   CallGraph callgraph;
103
104   LinearTypeCheck checker;
105
106   // maps a descriptor to its known dependents: namely
107   // methods or tasks that call the descriptor's method
108   // AND are part of this analysis (reachable from main)
109   private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
110
111   private LinkedList<MethodDescriptor> sortedDescriptors;
112
113   private Map<Location, Set<Descriptor>> mapSharedLocToDescSet;
114
115   public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
116     this.state = state;
117     this.tu = tu;
118     this.callgraph = callgraph;
119     this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
120     this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
121     this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
122     this.annotationRequireSet = new HashSet<MethodDescriptor>();
123     this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
124     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
125     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
126     this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
127     this.bf = bf;
128     this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
129     this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
130     this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
131     this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
132     this.sortedDescriptors = new LinkedList<MethodDescriptor>();
133     this.md2pcLoc = new HashMap<MethodDescriptor, CompositeLocation>();
134     this.mapSharedLocToDescSet = new HashMap<Location, Set<Descriptor>>();
135   }
136
137   public void doCheck() {
138     doMethodAnnotationCheck();
139
140     if (state.SSJAVA && !state.SSJAVAINFER) {
141       init();
142       computeLinearTypeCheckMethodSet();
143       doLinearTypeCheck();
144     }
145
146     if (state.SSJAVADEBUG) {
147       // debug_printAnnotationRequiredSet();
148     }
149     if (state.SSJAVAINFER) {
150       inference();
151       System.exit(0);
152     } else {
153       parseLocationAnnotation();
154       doFlowDownCheck();
155       doDefinitelyWrittenCheck();
156       doLoopCheck();
157     }
158
159     for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
160       MethodDescriptor md = (MethodDescriptor) iterator.next();
161       MethodLattice<String> locOrder = getMethodLattice(md);
162       writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
163       // System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
164       // + locOrder.getKeySet().size());
165     }
166   }
167
168   public void init() {
169     // perform topological sort over the set of methods accessed by the main
170     // event loop
171     Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
172     methodDescriptorsToAnalyze.addAll(getAnnotationRequireSet());
173     sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
174   }
175
176   public LinkedList<MethodDescriptor> getSortedDescriptors() {
177     return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
178   }
179
180   public void addSharedDesc(Location loc, Descriptor fd) {
181     if (!mapSharedLocToDescSet.containsKey(loc)) {
182       mapSharedLocToDescSet.put(loc, new HashSet<Descriptor>());
183     }
184     mapSharedLocToDescSet.get(loc).add(fd);
185   }
186
187   public Set<Descriptor> getSharedDescSet(Location loc) {
188     return mapSharedLocToDescSet.get(loc);
189   }
190
191   private void inference() {
192     LocationInference inferEngine = new LocationInference(this, state, tu);
193     inferEngine.inference();
194   }
195
196   private void doLoopCheck() {
197     GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
198     LoopOptimize lo = new LoopOptimize(gft, tu);
199
200     SymbolTable classtable = state.getClassSymbolTable();
201
202     List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
203     List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
204
205     toanalyzeList.addAll(classtable.getValueSet());
206     Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
207       public int compare(ClassDescriptor o1, ClassDescriptor o2) {
208         return o1.getClassName().compareTo(o2.getClassName());
209       }
210     });
211
212     for (int i = 0; i < toanalyzeList.size(); i++) {
213       ClassDescriptor cd = toanalyzeList.get(i);
214
215       SymbolTable methodtable = cd.getMethodTable();
216       toanalyzeMethodList.clear();
217       toanalyzeMethodList.addAll(methodtable.getValueSet());
218       Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
219         public int compare(MethodDescriptor o1, MethodDescriptor o2) {
220           return o1.getSymbol().compareTo(o2.getSymbol());
221         }
222       });
223
224       for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
225         MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
226         if (needTobeAnnotated(md)) {
227           lo.analyze(state.getMethodFlat(md));
228           doLoopTerminationCheck(lo, state.getMethodFlat(md));
229         }
230       }
231
232     }
233
234   }
235
236   public void addTrustMethod(MethodDescriptor md) {
237     trustWorthyMDSet.add(md);
238   }
239
240   public boolean isTrustMethod(MethodDescriptor md) {
241     return trustWorthyMDSet.contains(md);
242   }
243
244   private void computeLinearTypeCheckMethodSet() {
245
246     Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
247     linearTypeCheckMethodSet.addAll(allCalledSet);
248
249     Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
250
251     for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
252       MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
253       Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
254       trustedSet.add(trustMethod);
255       trustedSet.addAll(calledFromTrustMethodSet);
256     }
257
258     linearTypeCheckMethodSet.removeAll(trustedSet);
259
260     // if a method is called only by trusted method, no need to check linear
261     // type & flow down rule
262     for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
263       MethodDescriptor md = (MethodDescriptor) iterator.next();
264       Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
265       if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
266         linearTypeCheckMethodSet.add(md);
267       }
268     }
269     
270     linearTypeCheckMethodSet.addAll(sortedDescriptors);
271
272   }
273
274   private void doLinearTypeCheck() {
275     LinearTypeCheck checker = new LinearTypeCheck(this, state);
276     checker.linearTypeCheck();
277   }
278
279   public void debug_printAnnotationRequiredSet() {
280     System.out.println("SSJAVA: SSJava is checking the following methods:");
281     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
282       MethodDescriptor md = iterator.next();
283       System.out.println(md);
284     }
285     System.out.println();
286   }
287
288   private void doMethodAnnotationCheck() {
289     methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
290     methodAnnotationChecker.methodAnnoatationCheck();
291     methodAnnotationChecker.methodAnnoataionInheritanceCheck();
292     if (state.SSJAVAINFER) {
293       annotationRequireClassSet.add(methodContainingSSJavaLoop.getClassDesc());
294       annotationRequireSet.add(methodContainingSSJavaLoop);
295     }
296     state.setAnnotationRequireSet(annotationRequireSet);
297   }
298
299   public void doFlowDownCheck() {
300     flowDownChecker = new FlowDownCheck(this, state);
301     flowDownChecker.flowDownCheck();
302   }
303
304   public void doDefinitelyWrittenCheck() {
305     DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
306     checker.definitelyWrittenCheck();
307   }
308
309   private void parseLocationAnnotation() {
310     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
311     while (it.hasNext()) {
312       ClassDescriptor cd = (ClassDescriptor) it.next();
313       // parsing location hierarchy declaration for the class
314       Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
315       for (int i = 0; i < classAnnotations.size(); i++) {
316         AnnotationDescriptor an = classAnnotations.elementAt(i);
317         String marker = an.getMarker();
318         if (marker.equals(LATTICE)) {
319           SSJavaLattice<String> locOrder =
320               new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
321           cd2lattice.put(cd, locOrder);
322           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
323
324           if (state.SSJAVADEBUG) {
325             // generate lattice dot file
326             writeLatticeDotFile(cd, null, locOrder);
327             System.out.println("~~~\t" + cd + "\t" + locOrder.getKeySet().size());
328           }
329
330         } else if (marker.equals(METHODDEFAULT)) {
331           MethodLattice<String> locOrder =
332               new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
333           cd2methodDefault.put(cd, locOrder);
334           parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
335           // writeLatticeDotFile(cd, null, locOrder, "METHOD_DEFAULT");
336         }
337       }
338
339       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
340         MethodDescriptor md = (MethodDescriptor) method_it.next();
341         // parsing location hierarchy declaration for the method
342
343         if (needTobeAnnotated(md)) {
344           Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
345           if (methodAnnotations != null) {
346             for (int i = 0; i < methodAnnotations.size(); i++) {
347               AnnotationDescriptor an = methodAnnotations.elementAt(i);
348               if (an.getMarker().equals(LATTICE)) {
349                 // developer explicitly defines method lattice
350                 MethodLattice<String> locOrder =
351                     new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
352                 md2lattice.put(md, locOrder);
353                 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
354                 writeLatticeDotFile(cd, md, locOrder, "");
355               } else if (an.getMarker().equals(TERMINATE)) {
356                 // developer explicitly wants to skip loop termination analysis
357                 String value = an.getValue();
358                 int maxIteration = 0;
359                 if (value != null) {
360                   maxIteration = Integer.parseInt(value);
361                 }
362                 skipLoopTerminate.put(md, new Integer(maxIteration));
363               }
364             }
365           }
366         }
367
368       }
369
370     }
371   }
372
373   public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
374       SSJavaLattice<T> locOrder) {
375     writeLatticeDotFile(cd, md, locOrder, "");
376
377   }
378
379   public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
380       SSJavaLattice<T> locOrder, String nameSuffix) {
381
382     String fileName = "lattice_";
383     if (md != null) {
384       fileName +=
385           cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
386     } else {
387       fileName += cd.getSymbol().replaceAll("[\\W_]", "");
388     }
389
390     fileName += nameSuffix;
391
392     Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
393
394     if (pairSet.size() > 0) {
395       try {
396         BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
397
398         bw.write("digraph " + fileName + " {\n");
399
400         for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
401           // pair is in the form of <higher, lower>
402           Pair<T, T> pair = (Pair<T, T>) iterator.next();
403
404           T highLocId = pair.getFirst();
405           String highLocStr, lowLocStr;
406           if (locOrder.isSharedLoc(highLocId)) {
407             highLocStr = "\"" + highLocId + "*\"";
408           } else {
409             highLocStr = highLocId.toString();
410           }
411           T lowLocId = pair.getSecond();
412           if (locOrder.isSharedLoc(lowLocId)) {
413             lowLocStr = "\"" + lowLocId + "*\"";
414           } else {
415             lowLocStr = lowLocId.toString();
416           }
417           bw.write(highLocStr + " -> " + lowLocStr + ";\n");
418         }
419         bw.write("}\n");
420         bw.close();
421
422       } catch (IOException e) {
423         e.printStackTrace();
424       }
425
426     }
427
428   }
429
430   private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
431       MethodLattice<String> locOrder) {
432
433     value = value.replaceAll(" ", ""); // remove all blank spaces
434
435     StringTokenizer tokenizer = new StringTokenizer(value, ",");
436
437     while (tokenizer.hasMoreTokens()) {
438       String orderElement = tokenizer.nextToken();
439       int idx = orderElement.indexOf("<");
440       if (idx > 0) {// relative order element
441         String lowerLoc = orderElement.substring(0, idx);
442         String higherLoc = orderElement.substring(idx + 1);
443         locOrder.put(higherLoc, lowerLoc);
444         if (locOrder.isIntroducingCycle(higherLoc)) {
445           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
446               + " introduces a cycle.");
447         }
448       } else if (orderElement.startsWith(THISLOC + "=")) {
449         String thisLoc = orderElement.substring(8);
450         locOrder.setThisLoc(thisLoc);
451       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
452         String globalLoc = orderElement.substring(10);
453         locOrder.setGlobalLoc(globalLoc);
454       } else if (orderElement.startsWith(RETURNLOC + "=")) {
455         String returnLoc = orderElement.substring(10);
456         locOrder.setReturnLoc(returnLoc);
457       } else if (orderElement.endsWith("*")) {
458         // spin loc definition
459         locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
460       } else {
461         // single element
462         locOrder.put(orderElement);
463       }
464     }
465
466     // sanity checks
467     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
468       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
469           + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
470     }
471
472     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
473       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
474           + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
475     }
476   }
477
478   private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
479       SSJavaLattice<String> locOrder) {
480
481     value = value.replaceAll(" ", ""); // remove all blank spaces
482
483     StringTokenizer tokenizer = new StringTokenizer(value, ",");
484
485     while (tokenizer.hasMoreTokens()) {
486       String orderElement = tokenizer.nextToken();
487       int idx = orderElement.indexOf("<");
488
489       if (idx > 0) {// relative order element
490         String lowerLoc = orderElement.substring(0, idx);
491         String higherLoc = orderElement.substring(idx + 1);
492         locOrder.put(higherLoc, lowerLoc);
493         if (locOrder.isIntroducingCycle(higherLoc)) {
494           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
495               + " introduces a cycle in the class lattice " + cd);
496         }
497       } else if (orderElement.contains("*")) {
498         // spin loc definition
499         locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
500       } else {
501         // single element
502         locOrder.put(orderElement);
503       }
504     }
505
506     // sanity check
507     Set<String> spinLocSet = locOrder.getSharedLocSet();
508     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
509       String spinLoc = (String) iterator.next();
510       if (!locOrder.containsKey(spinLoc)) {
511         throw new Error("Spin location '" + spinLoc
512             + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
513       }
514     }
515   }
516
517   public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
518     return cd2lattice;
519   }
520
521   public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
522     return cd2methodDefault;
523   }
524
525   public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
526     return md2lattice;
527   }
528
529   public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
530     return cd2lattice.get(cd);
531   }
532
533   public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
534     return cd2methodDefault.get(cd);
535   }
536
537   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
538     if (md2lattice.containsKey(md)) {
539       return md2lattice.get(md);
540     } else {
541
542       if (cd2methodDefault.containsKey(md.getClassDesc())) {
543         return cd2methodDefault.get(md.getClassDesc());
544       } else {
545         throw new Error("Method Lattice of " + md + " is not defined.");
546       }
547
548     }
549   }
550
551   public CompositeLocation getPCLocation(MethodDescriptor md) {
552     if (!md2pcLoc.containsKey(md)) {
553       // by default, the initial pc location is TOP
554       CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
555       md2pcLoc.put(md, pcLoc);
556     }
557     return md2pcLoc.get(md);
558   }
559
560   public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
561     md2pcLoc.put(md, pcLoc);
562   }
563
564   public boolean needToCheckLinearType(MethodDescriptor md) {
565     return linearTypeCheckMethodSet.contains(md);
566   }
567
568   public boolean needTobeAnnotated(MethodDescriptor md) {
569     return annotationRequireSet.contains(md);
570   }
571
572   public boolean needToBeAnnoated(ClassDescriptor cd) {
573     return annotationRequireClassSet.contains(cd);
574   }
575
576   public void addAnnotationRequire(ClassDescriptor cd) {
577     annotationRequireClassSet.add(cd);
578   }
579
580   public void addAnnotationRequire(MethodDescriptor md) {
581
582     ClassDescriptor cd = md.getClassDesc();
583     // if a method requires to be annotated, class containg that method also
584     // requires to be annotated
585     if (!isSSJavaUtil(cd)) {
586       annotationRequireClassSet.add(cd);
587       annotationRequireSet.add(md);
588     }
589   }
590
591   public Set<MethodDescriptor> getAnnotationRequireSet() {
592     return annotationRequireSet;
593   }
594
595   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
596     LoopTerminate lt = new LoopTerminate(this, state);
597     if (needTobeAnnotated(fm.getMethod())) {
598       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
599     }
600   }
601
602   public CallGraph getCallGraph() {
603     return callgraph;
604   }
605
606   public SSJavaLattice<String> getLattice(Descriptor d) {
607
608     if (d instanceof MethodDescriptor) {
609       return getMethodLattice((MethodDescriptor) d);
610     } else {
611       return getClassLattice((ClassDescriptor) d);
612     }
613
614   }
615
616   public boolean isSharedLocation(Location loc) {
617     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
618     return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
619   }
620
621   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
622     Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
623     if (set == null) {
624       set = new HashSet<Descriptor>();
625       mapSharedLocation2DescriptorSet.put(loc, set);
626     }
627     set.add(d);
628   }
629
630   public BuildFlat getBuildFlat() {
631     return bf;
632   }
633
634   public MethodDescriptor getMethodContainingSSJavaLoop() {
635     return methodContainingSSJavaLoop;
636   }
637
638   public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
639     this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
640   }
641
642   public boolean isSSJavaUtil(ClassDescriptor cd) {
643     if (cd.getSymbol().equals("SSJAVA")) {
644       return true;
645     }
646     return false;
647   }
648
649   public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
650
651     Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
652     if (fieldSet == null) {
653       fieldSet = new HashSet<FieldDescriptor>();
654       mapMethodToOwnedFieldSet.put(md, fieldSet);
655     }
656     fieldSet.add(field);
657   }
658
659   public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
660     Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
661     if (fieldSet != null) {
662       return fieldSet.contains(field);
663     }
664     return false;
665   }
666
667   public FlatNode getSSJavaLoopEntrance() {
668     return ssjavaLoopEntrance;
669   }
670
671   public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
672     this.ssjavaLoopEntrance = ssjavaLoopEntrance;
673   }
674
675   public void addSameHeightWriteFlatNode(FlatNode fn) {
676     this.sameHeightWriteFlatNodeSet.add(fn);
677   }
678
679   public boolean isSameHeightWrite(FlatNode fn) {
680     return this.sameHeightWriteFlatNodeSet.contains(fn);
681   }
682
683   public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
684
685     Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
686
687     LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
688
689     Iterator<MethodDescriptor> itr = toSort.iterator();
690     while (itr.hasNext()) {
691       MethodDescriptor d = itr.next();
692
693       if (!discovered.contains(d)) {
694         dfsVisit(d, toSort, sorted, discovered);
695       }
696     }
697
698     return sorted;
699   }
700
701   // While we're doing DFS on call graph, remember
702   // dependencies for efficient queuing of methods
703   // during interprocedural analysis:
704   //
705   // a dependent of a method decriptor d for this analysis is:
706   // 1) a method or task that invokes d
707   // 2) in the descriptorsToAnalyze set
708   private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
709       LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
710
711     discovered.add(md);
712
713     Iterator itr2 = callgraph.getCalleeSet(md).iterator();
714     while (itr2.hasNext()) {
715       MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
716       addDependent(dCallee, md);
717     }
718
719     Iterator itr = callgraph.getCallerSet(md).iterator();
720     while (itr.hasNext()) {
721       MethodDescriptor dCaller = (MethodDescriptor) itr.next();
722       // only consider callers in the original set to analyze
723       if (!toSort.contains(dCaller)) {
724         continue;
725       }
726       if (!discovered.contains(dCaller)) {
727         addDependent(md, // callee
728             dCaller // caller
729         );
730
731         dfsVisit(dCaller, toSort, sorted, discovered);
732       }
733     }
734
735     // for leaf-nodes last now!
736     sorted.addLast(md);
737   }
738
739   public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
740     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
741     if (deps == null) {
742       deps = new HashSet<MethodDescriptor>();
743     }
744     deps.add(caller);
745     mapDescriptorToSetDependents.put(callee, deps);
746   }
747
748   public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
749     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
750     if (deps == null) {
751       deps = new HashSet<MethodDescriptor>();
752       mapDescriptorToSetDependents.put(callee, deps);
753     }
754     return deps;
755   }
756
757 }