changes.
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.Set;
7 import java.util.StringTokenizer;
8 import java.util.Vector;
9
10 import Analysis.CallGraph.CallGraph;
11 import Analysis.Loops.LoopOptimize;
12 import Analysis.Loops.LoopTerminate;
13 import IR.AnnotationDescriptor;
14 import IR.ClassDescriptor;
15 import IR.Descriptor;
16 import IR.MethodDescriptor;
17 import IR.State;
18 import IR.TypeUtil;
19 import IR.Flat.BuildFlat;
20 import IR.Flat.FlatMethod;
21 import IR.Flat.TempDescriptor;
22
23 public class SSJavaAnalysis {
24
25   public static final String SSJAVA = "SSJAVA";
26   public static final String LATTICE = "LATTICE";
27   public static final String METHODDEFAULT = "METHODDEFAULT";
28   public static final String THISLOC = "THISLOC";
29   public static final String GLOBALLOC = "GLOBALLOC";
30   public static final String RETURNLOC = "RETURNLOC";
31   public static final String LOC = "LOC";
32   public static final String DELTA = "DELTA";
33   public static final String TERMINATE = "TERMINATE";
34
35   State state;
36   TypeUtil tu;
37   FlowDownCheck flowDownChecker;
38   MethodAnnotationCheck methodAnnotationChecker;
39
40   // set containing method requires to be annoated
41   Set<MethodDescriptor> annotationRequireSet;
42
43   // class -> field lattice
44   Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
45
46   // class -> default local variable lattice
47   Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
48
49   // method -> local variable lattice
50   Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
51
52   // method set that does not want to have loop termination analysis
53   Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
54
55   // map shared location to its descriptors
56   Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
57
58   // set containing a class that has at least one annoated method
59   Set<ClassDescriptor> annotationRequireClassSet;
60
61   CallGraph callgraph;
62
63   public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
64     this.state = state;
65     this.tu = tu;
66     this.callgraph = callgraph;
67     this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
68     this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
69     this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
70     this.annotationRequireSet = new HashSet<MethodDescriptor>();
71     this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
72     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
73     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
74   }
75
76   public void doCheck() {
77     doMethodAnnotationCheck();
78     if (state.SSJAVADEBUG) {
79       debugPrint();
80     }
81     parseLocationAnnotation();
82     doFlowDownCheck();
83     doDefinitelyWrittenCheck();
84     doSingleReferenceCheck();
85   }
86
87   public void debugPrint() {
88     System.out.println("SSJAVA: SSJava is checking the following methods:");
89     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
90       MethodDescriptor md = iterator.next();
91       System.out.print(" " + md);
92     }
93     System.out.println();
94   }
95
96   private void doMethodAnnotationCheck() {
97     methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
98     methodAnnotationChecker.methodAnnoatationCheck();
99     methodAnnotationChecker.methodAnnoataionInheritanceCheck();
100   }
101
102   public void doFlowDownCheck() {
103     flowDownChecker = new FlowDownCheck(this, state);
104     flowDownChecker.flowDownCheck();
105   }
106
107   public void doDefinitelyWrittenCheck() {
108     DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
109     checker.definitelyWrittenCheck();
110   }
111
112   public void doSingleReferenceCheck() {
113     SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
114     checker.singleReferenceCheck();
115   }
116
117   private void parseLocationAnnotation() {
118     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
119     while (it.hasNext()) {
120       ClassDescriptor cd = (ClassDescriptor) it.next();
121       // parsing location hierarchy declaration for the class
122       Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
123       for (int i = 0; i < classAnnotations.size(); i++) {
124         AnnotationDescriptor an = classAnnotations.elementAt(i);
125         String marker = an.getMarker();
126         if (marker.equals(LATTICE)) {
127           SSJavaLattice<String> locOrder =
128               new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
129           cd2lattice.put(cd, locOrder);
130           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
131         } else if (marker.equals(METHODDEFAULT)) {
132           MethodLattice<String> locOrder =
133               new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
134           cd2methodDefault.put(cd, locOrder);
135           parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
136         }
137       }
138
139       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
140         MethodDescriptor md = (MethodDescriptor) method_it.next();
141         // parsing location hierarchy declaration for the method
142
143         if (needTobeAnnotated(md)) {
144           Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
145           if (methodAnnotations != null) {
146             for (int i = 0; i < methodAnnotations.size(); i++) {
147               AnnotationDescriptor an = methodAnnotations.elementAt(i);
148               if (an.getMarker().equals(LATTICE)) {
149                 // developer explicitly defines method lattice
150                 MethodLattice<String> locOrder =
151                     new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
152                 md2lattice.put(md, locOrder);
153                 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
154               } else if (an.getMarker().equals(TERMINATE)) {
155                 // developer explicitly wants to skip loop termination analysis
156                 String value = an.getValue();
157                 int maxIteration = 0;
158                 if (value != null) {
159                   maxIteration = Integer.parseInt(value);
160                 }
161                 System.out.println("###md=" + md);
162                 skipLoopTerminate.put(md, new Integer(maxIteration));
163               }
164             }
165           }
166         }
167
168       }
169
170     }
171   }
172
173   private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
174       MethodLattice<String> locOrder) {
175
176     value = value.replaceAll(" ", ""); // remove all blank spaces
177
178     StringTokenizer tokenizer = new StringTokenizer(value, ",");
179
180     while (tokenizer.hasMoreTokens()) {
181       String orderElement = tokenizer.nextToken();
182       int idx = orderElement.indexOf("<");
183       if (idx > 0) {// relative order element
184         String lowerLoc = orderElement.substring(0, idx);
185         String higherLoc = orderElement.substring(idx + 1);
186         locOrder.put(higherLoc, lowerLoc);
187         if (locOrder.isIntroducingCycle(higherLoc)) {
188           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
189               + " introduces a cycle.");
190         }
191       } else if (orderElement.startsWith(THISLOC + "=")) {
192         String thisLoc = orderElement.substring(8);
193         locOrder.setThisLoc(thisLoc);
194       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
195         String globalLoc = orderElement.substring(10);
196         locOrder.setGlobalLoc(globalLoc);
197       } else if (orderElement.startsWith(RETURNLOC + "=")) {
198         String returnLoc = orderElement.substring(10);
199         locOrder.setReturnLoc(returnLoc);
200       } else if (orderElement.contains("*")) {
201         // spin loc definition
202         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
203       } else {
204         // single element
205         locOrder.put(orderElement);
206       }
207     }
208
209     // sanity checks
210     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
211       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
212           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
213     }
214
215     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
216       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
217           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
218     }
219   }
220
221   private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
222       SSJavaLattice<String> locOrder) {
223
224     value = value.replaceAll(" ", ""); // remove all blank spaces
225
226     StringTokenizer tokenizer = new StringTokenizer(value, ",");
227
228     while (tokenizer.hasMoreTokens()) {
229       String orderElement = tokenizer.nextToken();
230       int idx = orderElement.indexOf("<");
231
232       if (idx > 0) {// relative order element
233         String lowerLoc = orderElement.substring(0, idx);
234         String higherLoc = orderElement.substring(idx + 1);
235         locOrder.put(higherLoc, lowerLoc);
236         if (locOrder.isIntroducingCycle(higherLoc)) {
237           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
238               + " introduces a cycle.");
239         }
240       } else if (orderElement.contains("*")) {
241         // spin loc definition
242         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
243       } else {
244         // single element
245         locOrder.put(orderElement);
246       }
247     }
248
249     // sanity check
250     Set<String> spinLocSet = locOrder.getSpinLocSet();
251     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
252       String spinLoc = (String) iterator.next();
253       if (!locOrder.containsKey(spinLoc)) {
254         throw new Error("Spin location '" + spinLoc
255             + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
256       }
257     }
258   }
259
260   public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
261     return cd2lattice;
262   }
263
264   public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
265     return cd2methodDefault;
266   }
267
268   public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
269     return md2lattice;
270   }
271
272   public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
273     return cd2lattice.get(cd);
274   }
275
276   public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
277     return cd2methodDefault.get(cd);
278   }
279
280   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
281     if (md2lattice.containsKey(md)) {
282       return md2lattice.get(md);
283     } else {
284       return cd2methodDefault.get(md.getClassDesc());
285     }
286   }
287
288   public boolean needTobeAnnotated(MethodDescriptor md) {
289     return annotationRequireSet.contains(md);
290   }
291
292   public boolean needToBeAnnoated(ClassDescriptor cd) {
293     return annotationRequireClassSet.contains(cd);
294   }
295
296   public void addAnnotationRequire(ClassDescriptor cd) {
297     annotationRequireClassSet.add(cd);
298   }
299
300   public void addAnnotationRequire(MethodDescriptor md) {
301
302     ClassDescriptor cd = md.getClassDesc();
303     // if a method requires to be annotated, class containg that method also
304     // requires to be annotated
305     annotationRequireClassSet.add(cd);
306     annotationRequireSet.add(md);
307   }
308
309   public Set<MethodDescriptor> getAnnotationRequireSet() {
310     return annotationRequireSet;
311   }
312
313   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
314     LoopTerminate lt = new LoopTerminate();
315     if (needTobeAnnotated(fm.getMethod())) {
316       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
317     }
318   }
319
320   public void doLoopTerminationCheck(LoopOptimize lo) {
321     LoopTerminate lt = new LoopTerminate();
322     for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
323       MethodDescriptor md = (MethodDescriptor) iterator.next();
324       if (!skipLoopTerminate.containsKey(md)) {
325         FlatMethod fm = state.getMethodFlat(md);
326         lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
327       }
328     }
329
330   }
331
332   public CallGraph getCallGraph() {
333     return callgraph;
334   }
335
336   public SSJavaLattice<String> getLattice(Descriptor d) {
337
338     if (d instanceof MethodDescriptor) {
339       return getMethodLattice((MethodDescriptor) d);
340     } else {
341       return getClassLattice((ClassDescriptor) d);
342     }
343
344   }
345
346   public boolean isSharedLocation(Location loc) {
347     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
348     return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
349   }
350
351   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
352     Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
353     if (set == null) {
354       set = new HashSet<Descriptor>();
355       mapSharedLocation2DescriptorSet.put(loc, set);
356     }
357     set.add(d);
358   }
359
360 }