changes toward intraprocedural analysis
[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                 skipLoopTerminate.put(md, new Integer(maxIteration));
157               }
158             }
159           }
160         }
161
162       }
163
164     }
165   }
166
167   private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
168       MethodLattice<String> locOrder) {
169
170     value = value.replaceAll(" ", ""); // remove all blank spaces
171
172     StringTokenizer tokenizer = new StringTokenizer(value, ",");
173
174     while (tokenizer.hasMoreTokens()) {
175       String orderElement = tokenizer.nextToken();
176       int idx = orderElement.indexOf("<");
177       if (idx > 0) {// relative order element
178         String lowerLoc = orderElement.substring(0, idx);
179         String higherLoc = orderElement.substring(idx + 1);
180         locOrder.put(higherLoc, lowerLoc);
181         if (locOrder.isIntroducingCycle(higherLoc)) {
182           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
183               + " introduces a cycle.");
184         }
185       } else if (orderElement.startsWith(THISLOC + "=")) {
186         String thisLoc = orderElement.substring(8);
187         locOrder.setThisLoc(thisLoc);
188       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
189         String globalLoc = orderElement.substring(10);
190         locOrder.setGlobalLoc(globalLoc);
191       } else if (orderElement.contains("*")) {
192         // spin loc definition
193         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
194       } else {
195         // single element
196         locOrder.put(orderElement);
197       }
198     }
199
200     // sanity checks
201     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
202       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
203           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
204     }
205
206     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
207       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
208           + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
209     }
210   }
211
212   private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
213       SSJavaLattice<String> locOrder) {
214
215     value = value.replaceAll(" ", ""); // remove all blank spaces
216
217     StringTokenizer tokenizer = new StringTokenizer(value, ",");
218
219     while (tokenizer.hasMoreTokens()) {
220       String orderElement = tokenizer.nextToken();
221       int idx = orderElement.indexOf("<");
222
223       if (idx > 0) {// relative order element
224         String lowerLoc = orderElement.substring(0, idx);
225         String higherLoc = orderElement.substring(idx + 1);
226         locOrder.put(higherLoc, lowerLoc);
227         if (locOrder.isIntroducingCycle(higherLoc)) {
228           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
229               + " introduces a cycle.");
230         }
231       } else if (orderElement.contains("*")) {
232         // spin loc definition
233         locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
234       } else {
235         // single element
236         locOrder.put(orderElement);
237       }
238     }
239
240     // sanity check
241     Set<String> spinLocSet = locOrder.getSpinLocSet();
242     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
243       String spinLoc = (String) iterator.next();
244       if (!locOrder.containsKey(spinLoc)) {
245         throw new Error("Spin location '" + spinLoc
246             + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
247       }
248     }
249   }
250
251   public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
252     return cd2lattice;
253   }
254
255   public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
256     return cd2methodDefault;
257   }
258
259   public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
260     return md2lattice;
261   }
262
263   public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
264     return cd2lattice.get(cd);
265   }
266
267   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
268     if (md2lattice.containsKey(md)) {
269       return md2lattice.get(md);
270     } else {
271       return cd2methodDefault.get(md.getClassDesc());
272     }
273   }
274
275   public boolean needTobeAnnotated(MethodDescriptor md) {
276     return annotationRequireSet.contains(md);
277   }
278
279   public void addAnnotationRequire(MethodDescriptor md) {
280     annotationRequireSet.add(md);
281   }
282
283   public Set<MethodDescriptor> getAnnotationRequireSet() {
284     return annotationRequireSet;
285   }
286
287   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
288     LoopTerminate lt = new LoopTerminate();
289     lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
290   }
291
292   public void doLoopTerminationCheck(LoopOptimize lo) {
293     LoopTerminate lt = new LoopTerminate();
294     for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
295       MethodDescriptor md = (MethodDescriptor) iterator.next();
296       if (!skipLoopTerminate.containsKey(md)) {
297         FlatMethod fm = state.getMethodFlat(md);
298         lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
299       }
300     }
301
302   }
303
304   public CallGraph getCallGraph() {
305     return callgraph;
306   }
307
308   public SSJavaLattice<String> getLattice(Descriptor d) {
309
310     if (d instanceof MethodDescriptor) {
311       return getMethodLattice((MethodDescriptor) d);
312     } else {
313       return getClassLattice((ClassDescriptor) d);
314     }
315
316   }
317
318   public boolean isSharedLocation(Location loc) {
319     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
320     return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
321   }
322
323   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
324     Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
325     if (set == null) {
326       set = new HashSet<Descriptor>();
327       mapSharedLocation2DescriptorSet.put(loc, set);
328     }
329     set.add(d);
330   }
331
332 }