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 skipLoopTerminate.put(md, new Integer(maxIteration));
167 private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
168 MethodLattice<String> locOrder) {
170 value = value.replaceAll(" ", ""); // remove all blank spaces
172 StringTokenizer tokenizer = new StringTokenizer(value, ",");
174 while (tokenizer.hasMoreTokens()) {
175 String orderElement = tokenizer.nextToken();
176 int idx = orderElement.indexOf("<");
177 if (idx > 0) {// relative order element
178 String lowerLoc = orderElement.substring(0, idx);
179 String higherLoc = orderElement.substring(idx + 1);
180 locOrder.put(higherLoc, lowerLoc);
181 if (locOrder.isIntroducingCycle(higherLoc)) {
182 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
183 + " introduces a cycle.");
185 } else if (orderElement.startsWith(THISLOC + "=")) {
186 String thisLoc = orderElement.substring(8);
187 locOrder.setThisLoc(thisLoc);
188 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
189 String globalLoc = orderElement.substring(10);
190 locOrder.setGlobalLoc(globalLoc);
191 } else if (orderElement.contains("*")) {
192 // spin loc definition
193 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
196 locOrder.put(orderElement);
201 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
202 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
203 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
206 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
207 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
208 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
212 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
213 SSJavaLattice<String> locOrder) {
215 value = value.replaceAll(" ", ""); // remove all blank spaces
217 StringTokenizer tokenizer = new StringTokenizer(value, ",");
219 while (tokenizer.hasMoreTokens()) {
220 String orderElement = tokenizer.nextToken();
221 int idx = orderElement.indexOf("<");
223 if (idx > 0) {// relative order element
224 String lowerLoc = orderElement.substring(0, idx);
225 String higherLoc = orderElement.substring(idx + 1);
226 locOrder.put(higherLoc, lowerLoc);
227 if (locOrder.isIntroducingCycle(higherLoc)) {
228 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
229 + " introduces a cycle.");
231 } else if (orderElement.contains("*")) {
232 // spin loc definition
233 locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
236 locOrder.put(orderElement);
241 Set<String> spinLocSet = locOrder.getSpinLocSet();
242 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
243 String spinLoc = (String) iterator.next();
244 if (!locOrder.containsKey(spinLoc)) {
245 throw new Error("Spin location '" + spinLoc
246 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
251 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
255 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
256 return cd2methodDefault;
259 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
263 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
264 return cd2lattice.get(cd);
267 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
268 if (md2lattice.containsKey(md)) {
269 return md2lattice.get(md);
271 return cd2methodDefault.get(md.getClassDesc());
275 public boolean needTobeAnnotated(MethodDescriptor md) {
276 return annotationRequireSet.contains(md);
279 public void addAnnotationRequire(MethodDescriptor md) {
280 annotationRequireSet.add(md);
283 public Set<MethodDescriptor> getAnnotationRequireSet() {
284 return annotationRequireSet;
287 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
288 LoopTerminate lt = new LoopTerminate();
289 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
292 public void doLoopTerminationCheck(LoopOptimize lo) {
293 LoopTerminate lt = new LoopTerminate();
294 for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
295 MethodDescriptor md = (MethodDescriptor) iterator.next();
296 if (!skipLoopTerminate.containsKey(md)) {
297 FlatMethod fm = state.getMethodFlat(md);
298 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
304 public CallGraph getCallGraph() {
308 public SSJavaLattice<String> getLattice(Descriptor d) {
310 if (d instanceof MethodDescriptor) {
311 return getMethodLattice((MethodDescriptor) d);
313 return getClassLattice((ClassDescriptor) d);
318 public boolean isSharedLocation(Location loc) {
319 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
320 return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
323 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
324 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
326 set = new HashSet<Descriptor>();
327 mapSharedLocation2DescriptorSet.put(loc, set);