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.Loops.LoopOptimize;
11 import Analysis.Loops.LoopTerminate;
12 import IR.AnnotationDescriptor;
13 import IR.ClassDescriptor;
14 import IR.MethodDescriptor;
17 import IR.Flat.FlatMethod;
19 public class SSJavaAnalysis {
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";
33 FlowDownCheck flowDownChecker;
34 MethodAnnotationCheck methodAnnotationChecker;
36 // if a method has annotations, the mapping has true
37 Set<MethodDescriptor> annotationRequireSet;
39 // class -> field lattice
40 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
42 // class -> default local variable lattice
43 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
45 // method -> local variable lattice
46 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
48 // method set that does not have loop termination analysis
49 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
51 public SSJavaAnalysis(State state, TypeUtil 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>();
61 public void doCheck() {
62 doMethodAnnotationCheck();
63 if (state.SSJAVADEBUG) {
66 parseLocationAnnotation();
68 doDefinitelyWrittenCheck();
69 doSingleReferenceCheck();
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);
80 private void doMethodAnnotationCheck() {
81 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
82 methodAnnotationChecker.methodAnnoatationCheck();
83 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
86 public void doFlowDownCheck() {
87 flowDownChecker = new FlowDownCheck(this, state);
88 flowDownChecker.flowDownCheck();
91 public void doDefinitelyWrittenCheck() {
92 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(state);
93 checker.definitelyWrittenCheck();
96 public void doSingleReferenceCheck() {
97 SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
98 checker.singleReferenceCheck();
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);
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
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;
143 maxIteration = Integer.parseInt(value);
145 skipLoopTerminate.put(md, new Integer(maxIteration));
156 private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
157 MethodLattice<String> locOrder) {
159 value = value.replaceAll(" ", ""); // remove all blank spaces
161 StringTokenizer tokenizer = new StringTokenizer(value, ",");
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.");
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));
185 locOrder.put(orderElement);
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());
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());
201 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
202 SSJavaLattice<String> locOrder) {
204 value = value.replaceAll(" ", ""); // remove all blank spaces
206 StringTokenizer tokenizer = new StringTokenizer(value, ",");
208 while (tokenizer.hasMoreTokens()) {
209 String orderElement = tokenizer.nextToken();
210 int idx = orderElement.indexOf("<");
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.");
220 } else if (orderElement.contains("*")) {
221 // spin loc definition
222 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
225 locOrder.put(orderElement);
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());
240 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
244 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
245 return cd2methodDefault;
248 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
252 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
253 return cd2lattice.get(cd);
256 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
257 if (md2lattice.containsKey(md)) {
258 return md2lattice.get(md);
260 return cd2methodDefault.get(md.getClassDesc());
264 public boolean needTobeAnnotated(MethodDescriptor md) {
265 return annotationRequireSet.contains(md);
268 public void addAnnotationRequire(MethodDescriptor md) {
269 annotationRequireSet.add(md);
272 public Set<MethodDescriptor> getAnnotationRequireSet() {
273 return annotationRequireSet;
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));