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