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