1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
7 import java.util.StringTokenizer;
8 import java.util.Vector;
10 import Analysis.CallGraph.CallGraph;
11 import Analysis.Loops.LoopOptimize;
12 import Analysis.Loops.LoopTerminate;
13 import IR.AnnotationDescriptor;
14 import IR.ClassDescriptor;
16 import IR.MethodDescriptor;
19 import IR.Flat.BuildFlat;
20 import IR.Flat.FlatMethod;
21 import IR.Flat.TempDescriptor;
23 public class SSJavaAnalysis {
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";
37 FlowDownCheck flowDownChecker;
38 MethodAnnotationCheck methodAnnotationChecker;
40 // set containing method requires to be annoated
41 Set<MethodDescriptor> annotationRequireSet;
43 // class -> field lattice
44 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
46 // class -> default local variable lattice
47 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
49 // method -> local variable lattice
50 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
52 // method set that does not want to have loop termination analysis
53 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
55 // map shared location to its descriptors
56 Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
58 // set containing a class that has at least one annoated method
59 Set<ClassDescriptor> annotationRequireClassSet;
63 public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
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>>();
76 public void doCheck() {
77 doMethodAnnotationCheck();
78 if (state.SSJAVADEBUG) {
81 parseLocationAnnotation();
83 doDefinitelyWrittenCheck();
84 doSingleReferenceCheck();
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.print(" " + md);
96 private void doMethodAnnotationCheck() {
97 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
98 methodAnnotationChecker.methodAnnoatationCheck();
99 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
102 public void doFlowDownCheck() {
103 flowDownChecker = new FlowDownCheck(this, state);
104 flowDownChecker.flowDownCheck();
107 public void doDefinitelyWrittenCheck() {
108 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
109 checker.definitelyWrittenCheck();
112 public void doSingleReferenceCheck() {
113 SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
114 checker.singleReferenceCheck();
117 private void parseLocationAnnotation() {
118 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
119 while (it.hasNext()) {
120 ClassDescriptor cd = (ClassDescriptor) it.next();
121 // parsing location hierarchy declaration for the class
122 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
123 for (int i = 0; i < classAnnotations.size(); i++) {
124 AnnotationDescriptor an = classAnnotations.elementAt(i);
125 String marker = an.getMarker();
126 if (marker.equals(LATTICE)) {
127 SSJavaLattice<String> locOrder =
128 new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
129 cd2lattice.put(cd, locOrder);
130 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
131 } else if (marker.equals(METHODDEFAULT)) {
132 MethodLattice<String> locOrder =
133 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
134 cd2methodDefault.put(cd, locOrder);
135 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
139 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
140 MethodDescriptor md = (MethodDescriptor) method_it.next();
141 // parsing location hierarchy declaration for the method
143 if (needTobeAnnotated(md)) {
144 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
145 if (methodAnnotations != null) {
146 for (int i = 0; i < methodAnnotations.size(); i++) {
147 AnnotationDescriptor an = methodAnnotations.elementAt(i);
148 if (an.getMarker().equals(LATTICE)) {
149 // developer explicitly defines method lattice
150 MethodLattice<String> locOrder =
151 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
152 md2lattice.put(md, locOrder);
153 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
154 } else if (an.getMarker().equals(TERMINATE)) {
155 // developer explicitly wants to skip loop termination analysis
156 String value = an.getValue();
157 int maxIteration = 0;
159 maxIteration = Integer.parseInt(value);
161 System.out.println("###md=" + md);
162 skipLoopTerminate.put(md, new Integer(maxIteration));
173 private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
174 MethodLattice<String> locOrder) {
176 value = value.replaceAll(" ", ""); // remove all blank spaces
178 StringTokenizer tokenizer = new StringTokenizer(value, ",");
180 while (tokenizer.hasMoreTokens()) {
181 String orderElement = tokenizer.nextToken();
182 int idx = orderElement.indexOf("<");
183 if (idx > 0) {// relative order element
184 String lowerLoc = orderElement.substring(0, idx);
185 String higherLoc = orderElement.substring(idx + 1);
186 locOrder.put(higherLoc, lowerLoc);
187 if (locOrder.isIntroducingCycle(higherLoc)) {
188 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
189 + " introduces a cycle.");
191 } else if (orderElement.startsWith(THISLOC + "=")) {
192 String thisLoc = orderElement.substring(8);
193 locOrder.setThisLoc(thisLoc);
194 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
195 String globalLoc = orderElement.substring(10);
196 locOrder.setGlobalLoc(globalLoc);
197 } else if (orderElement.startsWith(RETURNLOC + "=")) {
198 String returnLoc = orderElement.substring(10);
199 locOrder.setReturnLoc(returnLoc);
200 } else if (orderElement.contains("*")) {
201 // spin loc definition
202 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
205 locOrder.put(orderElement);
210 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
211 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
212 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
215 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
216 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
217 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
221 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
222 SSJavaLattice<String> locOrder) {
224 value = value.replaceAll(" ", ""); // remove all blank spaces
226 StringTokenizer tokenizer = new StringTokenizer(value, ",");
228 while (tokenizer.hasMoreTokens()) {
229 String orderElement = tokenizer.nextToken();
230 int idx = orderElement.indexOf("<");
232 if (idx > 0) {// relative order element
233 String lowerLoc = orderElement.substring(0, idx);
234 String higherLoc = orderElement.substring(idx + 1);
235 locOrder.put(higherLoc, lowerLoc);
236 if (locOrder.isIntroducingCycle(higherLoc)) {
237 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
238 + " introduces a cycle.");
240 } else if (orderElement.contains("*")) {
241 // spin loc definition
242 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
245 locOrder.put(orderElement);
250 Set<String> spinLocSet = locOrder.getSpinLocSet();
251 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
252 String spinLoc = (String) iterator.next();
253 if (!locOrder.containsKey(spinLoc)) {
254 throw new Error("Spin location '" + spinLoc
255 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
260 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
264 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
265 return cd2methodDefault;
268 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
272 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
273 return cd2lattice.get(cd);
276 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
277 return cd2methodDefault.get(cd);
280 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
281 if (md2lattice.containsKey(md)) {
282 return md2lattice.get(md);
284 return cd2methodDefault.get(md.getClassDesc());
288 public boolean needTobeAnnotated(MethodDescriptor md) {
289 return annotationRequireSet.contains(md);
292 public boolean needToBeAnnoated(ClassDescriptor cd) {
293 return annotationRequireClassSet.contains(cd);
296 public void addAnnotationRequire(ClassDescriptor cd) {
297 annotationRequireClassSet.add(cd);
300 public void addAnnotationRequire(MethodDescriptor md) {
302 ClassDescriptor cd = md.getClassDesc();
303 // if a method requires to be annotated, class containg that method also
304 // requires to be annotated
305 annotationRequireClassSet.add(cd);
306 annotationRequireSet.add(md);
309 public Set<MethodDescriptor> getAnnotationRequireSet() {
310 return annotationRequireSet;
313 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
314 LoopTerminate lt = new LoopTerminate();
315 if (needTobeAnnotated(fm.getMethod())) {
316 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
320 public void doLoopTerminationCheck(LoopOptimize lo) {
321 LoopTerminate lt = new LoopTerminate();
322 for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
323 MethodDescriptor md = (MethodDescriptor) iterator.next();
324 if (!skipLoopTerminate.containsKey(md)) {
325 FlatMethod fm = state.getMethodFlat(md);
326 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
332 public CallGraph getCallGraph() {
336 public SSJavaLattice<String> getLattice(Descriptor d) {
338 if (d instanceof MethodDescriptor) {
339 return getMethodLattice((MethodDescriptor) d);
341 return getClassLattice((ClassDescriptor) d);
346 public boolean isSharedLocation(Location loc) {
347 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
348 return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
351 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
352 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
354 set = new HashSet<Descriptor>();
355 mapSharedLocation2DescriptorSet.put(loc, set);