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 // if a method has annotations, the mapping has true
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 have loop termination analysis
53 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
55 // map shared location to its descriptors
56 Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
60 public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
63 this.callgraph = callgraph;
64 this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
65 this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
66 this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
67 this.annotationRequireSet = new HashSet<MethodDescriptor>();
68 this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
69 this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
72 public void doCheck() {
73 doMethodAnnotationCheck();
74 if (state.SSJAVADEBUG) {
77 parseLocationAnnotation();
79 doDefinitelyWrittenCheck();
80 doSingleReferenceCheck();
83 public void debugPrint() {
84 System.out.println("SSJAVA: SSJava is checking the following methods:");
85 for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
86 MethodDescriptor md = iterator.next();
87 System.out.println("SSJAVA: " + md);
91 private void doMethodAnnotationCheck() {
92 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
93 methodAnnotationChecker.methodAnnoatationCheck();
94 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
97 public void doFlowDownCheck() {
98 flowDownChecker = new FlowDownCheck(this, state);
99 flowDownChecker.flowDownCheck();
102 public void doDefinitelyWrittenCheck() {
103 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
104 checker.definitelyWrittenCheck();
107 public void doSingleReferenceCheck() {
108 SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
109 checker.singleReferenceCheck();
112 private void parseLocationAnnotation() {
113 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
114 while (it.hasNext()) {
115 ClassDescriptor cd = (ClassDescriptor) it.next();
116 // parsing location hierarchy declaration for the class
117 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
118 for (int i = 0; i < classAnnotations.size(); i++) {
119 AnnotationDescriptor an = classAnnotations.elementAt(i);
120 String marker = an.getMarker();
121 if (marker.equals(LATTICE)) {
122 SSJavaLattice<String> locOrder =
123 new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
124 cd2lattice.put(cd, locOrder);
125 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
126 } else if (marker.equals(METHODDEFAULT)) {
127 MethodLattice<String> locOrder =
128 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
129 cd2methodDefault.put(cd, locOrder);
130 parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
134 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
135 MethodDescriptor md = (MethodDescriptor) method_it.next();
136 // parsing location hierarchy declaration for the method
138 if (needTobeAnnotated(md)) {
139 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
140 if (methodAnnotations != null) {
141 for (int i = 0; i < methodAnnotations.size(); i++) {
142 AnnotationDescriptor an = methodAnnotations.elementAt(i);
143 if (an.getMarker().equals(LATTICE)) {
144 // developer explicitly defines method lattice
145 MethodLattice<String> locOrder =
146 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
147 md2lattice.put(md, locOrder);
148 parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
149 } else if (an.getMarker().equals(TERMINATE)) {
150 // developer explicitly wants to skip loop termination analysis
151 String value = an.getValue();
152 int maxIteration = 0;
154 maxIteration = Integer.parseInt(value);
156 System.out.println("###md=" + md);
157 skipLoopTerminate.put(md, new Integer(maxIteration));
168 private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
169 MethodLattice<String> locOrder) {
171 value = value.replaceAll(" ", ""); // remove all blank spaces
173 StringTokenizer tokenizer = new StringTokenizer(value, ",");
175 while (tokenizer.hasMoreTokens()) {
176 String orderElement = tokenizer.nextToken();
177 int idx = orderElement.indexOf("<");
178 if (idx > 0) {// relative order element
179 String lowerLoc = orderElement.substring(0, idx);
180 String higherLoc = orderElement.substring(idx + 1);
181 locOrder.put(higherLoc, lowerLoc);
182 if (locOrder.isIntroducingCycle(higherLoc)) {
183 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
184 + " introduces a cycle.");
186 } else if (orderElement.startsWith(THISLOC + "=")) {
187 String thisLoc = orderElement.substring(8);
188 locOrder.setThisLoc(thisLoc);
189 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
190 String globalLoc = orderElement.substring(10);
191 locOrder.setGlobalLoc(globalLoc);
192 } else if (orderElement.contains("*")) {
193 // spin loc definition
194 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
197 locOrder.put(orderElement);
202 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
203 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
204 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
207 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
208 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
209 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
213 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
214 SSJavaLattice<String> locOrder) {
216 value = value.replaceAll(" ", ""); // remove all blank spaces
218 StringTokenizer tokenizer = new StringTokenizer(value, ",");
220 while (tokenizer.hasMoreTokens()) {
221 String orderElement = tokenizer.nextToken();
222 int idx = orderElement.indexOf("<");
224 if (idx > 0) {// relative order element
225 String lowerLoc = orderElement.substring(0, idx);
226 String higherLoc = orderElement.substring(idx + 1);
227 locOrder.put(higherLoc, lowerLoc);
228 if (locOrder.isIntroducingCycle(higherLoc)) {
229 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
230 + " introduces a cycle.");
232 } else if (orderElement.contains("*")) {
233 // spin loc definition
234 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
237 locOrder.put(orderElement);
242 Set<String> spinLocSet = locOrder.getSpinLocSet();
243 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
244 String spinLoc = (String) iterator.next();
245 if (!locOrder.containsKey(spinLoc)) {
246 throw new Error("Spin location '" + spinLoc
247 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
252 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
256 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
257 return cd2methodDefault;
260 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
264 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
265 return cd2lattice.get(cd);
268 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
269 if (md2lattice.containsKey(md)) {
270 return md2lattice.get(md);
272 return cd2methodDefault.get(md.getClassDesc());
276 public boolean needTobeAnnotated(MethodDescriptor md) {
277 return annotationRequireSet.contains(md);
280 public void addAnnotationRequire(MethodDescriptor md) {
281 annotationRequireSet.add(md);
284 public Set<MethodDescriptor> getAnnotationRequireSet() {
285 return annotationRequireSet;
288 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
289 LoopTerminate lt = new LoopTerminate();
290 if (needTobeAnnotated(fm.getMethod())) {
291 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
295 public void doLoopTerminationCheck(LoopOptimize lo) {
296 LoopTerminate lt = new LoopTerminate();
297 for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
298 MethodDescriptor md = (MethodDescriptor) iterator.next();
299 if (!skipLoopTerminate.containsKey(md)) {
300 FlatMethod fm = state.getMethodFlat(md);
301 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
307 public CallGraph getCallGraph() {
311 public SSJavaLattice<String> getLattice(Descriptor d) {
313 if (d instanceof MethodDescriptor) {
314 return getMethodLattice((MethodDescriptor) d);
316 return getClassLattice((ClassDescriptor) d);
321 public boolean isSharedLocation(Location loc) {
322 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
323 return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
326 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
327 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
329 set = new HashSet<Descriptor>();
330 mapSharedLocation2DescriptorSet.put(loc, set);