21ae3920f1146231f9a47a998c2d44ee79f54cda
[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.println("SSJAVA: " + md);
92     }
93   }
94
95   private void doMethodAnnotationCheck() {
96     methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
97     methodAnnotationChecker.methodAnnoatationCheck();
98     methodAnnotationChecker.methodAnnoataionInheritanceCheck();
99   }
100
101   public void doFlowDownCheck() {
102     flowDownChecker = new FlowDownCheck(this, state);
103     flowDownChecker.flowDownCheck();
104   }
105
106   public void doDefinitelyWrittenCheck() {
107     DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
108     checker.definitelyWrittenCheck();
109   }
110
111   public void doSingleReferenceCheck() {
112     SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
113     checker.singleReferenceCheck();
114   }
115
116   private void parseLocationAnnotation() {
117     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
118     while (it.hasNext()) {
119       ClassDescriptor cd = (ClassDescriptor) it.next();
120       // parsing location hierarchy declaration for the class
121       Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
122       for (int i = 0; i < classAnnotations.size(); i++) {
123         AnnotationDescriptor an = classAnnotations.elementAt(i);
124         String marker = an.getMarker();
125         if (marker.equals(LATTICE)) {
126           SSJavaLattice<String> locOrder =
127               new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
128           cd2lattice.put(cd, locOrder);
129           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
130         } else if (marker.equals(METHODDEFAULT)) {
131           MethodLattice<String> locOrder =
132               new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
133           cd2methodDefault.put(cd, locOrder);
134           parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
135         }
136       }
137
138       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
139         MethodDescriptor md = (MethodDescriptor) method_it.next();
140         // parsing location hierarchy declaration for the method
141
142         if (needTobeAnnotated(md)) {
143           Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
144           if (methodAnnotations != null) {
145             for (int i = 0; i < methodAnnotations.size(); i++) {
146               AnnotationDescriptor an = methodAnnotations.elementAt(i);
147               if (an.getMarker().equals(LATTICE)) {
148                 // developer explicitly defines method lattice
149                 MethodLattice<String> locOrder =
150                     new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
151                 md2lattice.put(md, locOrder);
152                 parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
153               } else if (an.getMarker().equals(TERMINATE)) {
154                 // developer explicitly wants to skip loop termination analysis
155                 String value = an.getValue();
156                 int maxIteration = 0;
157                 if (value != null) {
158                   maxIteration = Integer.parseInt(value);
159                 }
160                 System.out.println("###md=" + md);
161                 skipLoopTerminate.put(md, new Integer(maxIteration));
162               }
163             }
164           }
165         }
166
167       }
168
169     }
170   }
171
172   private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
173       MethodLattice<String> locOrder) {
174
175     value = value.replaceAll(" ", ""); // remove all blank spaces
176
177     StringTokenizer tokenizer = new StringTokenizer(value, ",");
178
179     while (tokenizer.hasMoreTokens()) {
180       String orderElement = tokenizer.nextToken();
181       int idx = orderElement.indexOf("<");
182       if (idx > 0) {// relative order element
183         String lowerLoc = orderElement.substring(0, idx);
184         String higherLoc = orderElement.substring(idx + 1);
185         locOrder.put(higherLoc, lowerLoc);
186         if (locOrder.isIntroducingCycle(higherLoc)) {
187           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
188               + " introduces a cycle.");
189         }
190       } else if (orderElement.startsWith(THISLOC + "=")) {
191         String thisLoc = orderElement.substring(8);
192         locOrder.setThisLoc(thisLoc);
193       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
194         String globalLoc = orderElement.substring(10);
195         locOrder.setGlobalLoc(globalLoc);
196       } else if (orderElement.contains("*")) {
197         // spin loc definition
198         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
199       } else {
200         // single element
201         locOrder.put(orderElement);
202       }
203     }
204
205     // sanity checks
206     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
207       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
208           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
209     }
210
211     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
212       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
213           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
214     }
215   }
216
217   private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
218       SSJavaLattice<String> locOrder) {
219
220     value = value.replaceAll(" ", ""); // remove all blank spaces
221
222     StringTokenizer tokenizer = new StringTokenizer(value, ",");
223
224     while (tokenizer.hasMoreTokens()) {
225       String orderElement = tokenizer.nextToken();
226       int idx = orderElement.indexOf("<");
227
228       if (idx > 0) {// relative order element
229         String lowerLoc = orderElement.substring(0, idx);
230         String higherLoc = orderElement.substring(idx + 1);
231         locOrder.put(higherLoc, lowerLoc);
232         if (locOrder.isIntroducingCycle(higherLoc)) {
233           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
234               + " introduces a cycle.");
235         }
236       } else if (orderElement.contains("*")) {
237         // spin loc definition
238         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
239       } else {
240         // single element
241         locOrder.put(orderElement);
242       }
243     }
244
245     // sanity check
246     Set<String> spinLocSet = locOrder.getSpinLocSet();
247     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
248       String spinLoc = (String) iterator.next();
249       if (!locOrder.containsKey(spinLoc)) {
250         throw new Error("Spin location '" + spinLoc
251             + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
252       }
253     }
254   }
255
256   public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
257     return cd2lattice;
258   }
259
260   public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
261     return cd2methodDefault;
262   }
263
264   public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
265     return md2lattice;
266   }
267
268   public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
269     return cd2lattice.get(cd);
270   }
271
272   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
273     if (md2lattice.containsKey(md)) {
274       return md2lattice.get(md);
275     } else {
276       return cd2methodDefault.get(md.getClassDesc());
277     }
278   }
279
280   public boolean needTobeAnnotated(MethodDescriptor md) {
281     return annotationRequireSet.contains(md);
282   }
283
284   public boolean needToBeAnnoated(ClassDescriptor cd) {
285     return annotationRequireClassSet.contains(cd);
286   }
287
288   public void addAnnotationRequire(MethodDescriptor md) {
289
290     // if a method requires to be annotated, class containg that method also
291     // requires to be annotated
292     annotationRequireClassSet.add(md.getClassDesc());
293     annotationRequireSet.add(md);
294   }
295
296   public Set<MethodDescriptor> getAnnotationRequireSet() {
297     return annotationRequireSet;
298   }
299
300   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
301     LoopTerminate lt = new LoopTerminate();
302     if (needTobeAnnotated(fm.getMethod())) {
303       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
304     }
305   }
306
307   public void doLoopTerminationCheck(LoopOptimize lo) {
308     LoopTerminate lt = new LoopTerminate();
309     for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
310       MethodDescriptor md = (MethodDescriptor) iterator.next();
311       if (!skipLoopTerminate.containsKey(md)) {
312         FlatMethod fm = state.getMethodFlat(md);
313         lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
314       }
315     }
316
317   }
318
319   public CallGraph getCallGraph() {
320     return callgraph;
321   }
322
323   public SSJavaLattice<String> getLattice(Descriptor d) {
324
325     if (d instanceof MethodDescriptor) {
326       return getMethodLattice((MethodDescriptor) d);
327     } else {
328       return getClassLattice((ClassDescriptor) d);
329     }
330
331   }
332
333   public boolean isSharedLocation(Location loc) {
334     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
335     return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
336   }
337
338   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
339     Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
340     if (set == null) {
341       set = new HashSet<Descriptor>();
342       mapSharedLocation2DescriptorSet.put(loc, set);
343     }
344     set.add(d);
345   }
346
347 }