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;
15 import IR.MethodDescriptor;
18 import IR.Flat.FlatMethod;
20 public class SSJavaAnalysis {
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";
34 FlowDownCheck flowDownChecker;
35 MethodAnnotationCheck methodAnnotationChecker;
37 // if a method has annotations, the mapping has true
38 Set<MethodDescriptor> annotationRequireSet;
40 // class -> field lattice
41 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
43 // class -> default local variable lattice
44 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
46 // method -> local variable lattice
47 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
49 // method set that does not have loop termination analysis
50 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
54 public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
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>();
65 public void doCheck() {
66 doMethodAnnotationCheck();
67 if (state.SSJAVADEBUG) {
70 parseLocationAnnotation();
72 doDefinitelyWrittenCheck();
73 doSingleReferenceCheck();
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);
84 private void doMethodAnnotationCheck() {
85 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
86 methodAnnotationChecker.methodAnnoatationCheck();
87 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
90 public void doFlowDownCheck() {
91 flowDownChecker = new FlowDownCheck(this, state);
92 flowDownChecker.flowDownCheck();
95 public void doDefinitelyWrittenCheck() {
96 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
97 checker.definitelyWrittenCheck();
100 public void doSingleReferenceCheck() {
101 SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
102 checker.singleReferenceCheck();
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);
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
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;
147 maxIteration = Integer.parseInt(value);
149 skipLoopTerminate.put(md, new Integer(maxIteration));
160 private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
161 MethodLattice<String> locOrder) {
163 value = value.replaceAll(" ", ""); // remove all blank spaces
165 StringTokenizer tokenizer = new StringTokenizer(value, ",");
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.");
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));
189 locOrder.put(orderElement);
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());
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());
205 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
206 SSJavaLattice<String> locOrder) {
208 value = value.replaceAll(" ", ""); // remove all blank spaces
210 StringTokenizer tokenizer = new StringTokenizer(value, ",");
212 while (tokenizer.hasMoreTokens()) {
213 String orderElement = tokenizer.nextToken();
214 int idx = orderElement.indexOf("<");
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.");
224 } else if (orderElement.contains("*")) {
225 // spin loc definition
226 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
229 locOrder.put(orderElement);
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());
244 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
248 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
249 return cd2methodDefault;
252 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
256 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
257 return cd2lattice.get(cd);
260 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
261 if (md2lattice.containsKey(md)) {
262 return md2lattice.get(md);
264 return cd2methodDefault.get(md.getClassDesc());
268 public boolean needTobeAnnotated(MethodDescriptor md) {
269 return annotationRequireSet.contains(md);
272 public void addAnnotationRequire(MethodDescriptor md) {
273 annotationRequireSet.add(md);
276 public Set<MethodDescriptor> getAnnotationRequireSet() {
277 return annotationRequireSet;
280 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
281 LoopTerminate lt = new LoopTerminate();
282 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
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));
297 public CallGraph getCallGraph() {