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