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.println("SSJAVA: " + md);
95 private void doMethodAnnotationCheck() {
96 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
97 methodAnnotationChecker.methodAnnoatationCheck();
98 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
101 public void doFlowDownCheck() {
102 flowDownChecker = new FlowDownCheck(this, state);
103 flowDownChecker.flowDownCheck();
106 public void doDefinitelyWrittenCheck() {
107 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
108 checker.definitelyWrittenCheck();
111 public void doSingleReferenceCheck() {
112 SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
113 checker.singleReferenceCheck();
116 private void parseLocationAnnotation() {
117 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
118 while (it.hasNext()) {
119 ClassDescriptor cd = (ClassDescriptor) it.next();
120 // parsing location hierarchy declaration for the class
121 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
122 for (int i = 0; i < classAnnotations.size(); i++) {
123 AnnotationDescriptor an = classAnnotations.elementAt(i);
124 String marker = an.getMarker();
125 if (marker.equals(LATTICE)) {
126 SSJavaLattice<String> locOrder =
127 new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
128 cd2lattice.put(cd, locOrder);
129 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
130 } else if (marker.equals(METHODDEFAULT)) {
131 MethodLattice<String> locOrder =
132 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
133 cd2methodDefault.put(cd, locOrder);
134 parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
138 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
139 MethodDescriptor md = (MethodDescriptor) method_it.next();
140 // parsing location hierarchy declaration for the method
142 if (needTobeAnnotated(md)) {
143 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
144 if (methodAnnotations != null) {
145 for (int i = 0; i < methodAnnotations.size(); i++) {
146 AnnotationDescriptor an = methodAnnotations.elementAt(i);
147 if (an.getMarker().equals(LATTICE)) {
148 // developer explicitly defines method lattice
149 MethodLattice<String> locOrder =
150 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
151 md2lattice.put(md, locOrder);
152 parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
153 } else if (an.getMarker().equals(TERMINATE)) {
154 // developer explicitly wants to skip loop termination analysis
155 String value = an.getValue();
156 int maxIteration = 0;
158 maxIteration = Integer.parseInt(value);
160 System.out.println("###md=" + md);
161 skipLoopTerminate.put(md, new Integer(maxIteration));
172 private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
173 MethodLattice<String> locOrder) {
175 value = value.replaceAll(" ", ""); // remove all blank spaces
177 StringTokenizer tokenizer = new StringTokenizer(value, ",");
179 while (tokenizer.hasMoreTokens()) {
180 String orderElement = tokenizer.nextToken();
181 int idx = orderElement.indexOf("<");
182 if (idx > 0) {// relative order element
183 String lowerLoc = orderElement.substring(0, idx);
184 String higherLoc = orderElement.substring(idx + 1);
185 locOrder.put(higherLoc, lowerLoc);
186 if (locOrder.isIntroducingCycle(higherLoc)) {
187 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
188 + " introduces a cycle.");
190 } else if (orderElement.startsWith(THISLOC + "=")) {
191 String thisLoc = orderElement.substring(8);
192 locOrder.setThisLoc(thisLoc);
193 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
194 String globalLoc = orderElement.substring(10);
195 locOrder.setGlobalLoc(globalLoc);
196 } else if (orderElement.contains("*")) {
197 // spin loc definition
198 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
201 locOrder.put(orderElement);
206 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
207 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
208 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
211 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
212 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
213 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
217 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
218 SSJavaLattice<String> locOrder) {
220 value = value.replaceAll(" ", ""); // remove all blank spaces
222 StringTokenizer tokenizer = new StringTokenizer(value, ",");
224 while (tokenizer.hasMoreTokens()) {
225 String orderElement = tokenizer.nextToken();
226 int idx = orderElement.indexOf("<");
228 if (idx > 0) {// relative order element
229 String lowerLoc = orderElement.substring(0, idx);
230 String higherLoc = orderElement.substring(idx + 1);
231 locOrder.put(higherLoc, lowerLoc);
232 if (locOrder.isIntroducingCycle(higherLoc)) {
233 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
234 + " introduces a cycle.");
236 } else if (orderElement.contains("*")) {
237 // spin loc definition
238 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
241 locOrder.put(orderElement);
246 Set<String> spinLocSet = locOrder.getSpinLocSet();
247 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
248 String spinLoc = (String) iterator.next();
249 if (!locOrder.containsKey(spinLoc)) {
250 throw new Error("Spin location '" + spinLoc
251 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
256 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
260 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
261 return cd2methodDefault;
264 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
268 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
269 return cd2lattice.get(cd);
272 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
273 return cd2methodDefault.get(cd);
276 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
277 if (md2lattice.containsKey(md)) {
278 return md2lattice.get(md);
280 return cd2methodDefault.get(md.getClassDesc());
284 public boolean needTobeAnnotated(MethodDescriptor md) {
285 return annotationRequireSet.contains(md);
288 public boolean needToBeAnnoated(ClassDescriptor cd) {
289 return annotationRequireClassSet.contains(cd);
292 public void addAnnotationRequire(ClassDescriptor cd) {
293 annotationRequireClassSet.add(cd);
296 public void addAnnotationRequire(MethodDescriptor md) {
298 ClassDescriptor cd = md.getClassDesc();
299 // if a method requires to be annotated, class containg that method also
300 // requires to be annotated
301 annotationRequireClassSet.add(cd);
302 annotationRequireSet.add(md);
305 public Set<MethodDescriptor> getAnnotationRequireSet() {
306 return annotationRequireSet;
309 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
310 LoopTerminate lt = new LoopTerminate();
311 if (needTobeAnnotated(fm.getMethod())) {
312 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
316 public void doLoopTerminationCheck(LoopOptimize lo) {
317 LoopTerminate lt = new LoopTerminate();
318 for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
319 MethodDescriptor md = (MethodDescriptor) iterator.next();
320 if (!skipLoopTerminate.containsKey(md)) {
321 FlatMethod fm = state.getMethodFlat(md);
322 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
328 public CallGraph getCallGraph() {
332 public SSJavaLattice<String> getLattice(Descriptor d) {
334 if (d instanceof MethodDescriptor) {
335 return getMethodLattice((MethodDescriptor) d);
337 return getClassLattice((ClassDescriptor) d);
342 public boolean isSharedLocation(Location loc) {
343 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
344 return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
347 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
348 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
350 set = new HashSet<Descriptor>();
351 mapSharedLocation2DescriptorSet.put(loc, set);