changes.
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
index 1402a41f13e6b45e0c5f37413ade659790d1a482..752edfbde242092a01e65eac3b8d32a53148d1fc 100644 (file)
@@ -1,5 +1,8 @@
 package Analysis.SSJava;
 
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -18,7 +21,8 @@ import IR.State;
 import IR.TypeUtil;
 import IR.Flat.BuildFlat;
 import IR.Flat.FlatMethod;
-import IR.Flat.TempDescriptor;
+import IR.Tree.TreeNode;
+import Util.Pair;
 
 public class SSJavaAnalysis {
 
@@ -31,13 +35,16 @@ public class SSJavaAnalysis {
   public static final String LOC = "LOC";
   public static final String DELTA = "DELTA";
   public static final String TERMINATE = "TERMINATE";
+  public static final String DELEGATE = "DELEGATE";
+  public static final String DELEGATETHIS = "DELEGATETHIS";
 
   State state;
   TypeUtil tu;
   FlowDownCheck flowDownChecker;
   MethodAnnotationCheck methodAnnotationChecker;
+  BuildFlat bf;
 
-  // if a method has annotations, the mapping has true
+  // set containing method requires to be annoated
   Set<MethodDescriptor> annotationRequireSet;
 
   // class -> field lattice
@@ -49,15 +56,20 @@ public class SSJavaAnalysis {
   // method -> local variable lattice
   Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
 
-  // method set that does not have loop termination analysis
+  // method set that does not want to have loop termination analysis
   Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
 
   // map shared location to its descriptors
   Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
 
+  // set containing a class that has at least one annoated method
+  Set<ClassDescriptor> annotationRequireClassSet;
+
   CallGraph callgraph;
 
-  public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
+  LinearTypeCheck checker;
+
+  public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
     this.state = state;
     this.tu = tu;
     this.callgraph = callgraph;
@@ -65,11 +77,14 @@ public class SSJavaAnalysis {
     this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
     this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
     this.annotationRequireSet = new HashSet<MethodDescriptor>();
+    this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
+    this.bf = bf;
   }
 
   public void doCheck() {
+    doLinearTypeCheck();
     doMethodAnnotationCheck();
     if (state.SSJAVADEBUG) {
       debugPrint();
@@ -77,15 +92,20 @@ public class SSJavaAnalysis {
     parseLocationAnnotation();
     doFlowDownCheck();
     doDefinitelyWrittenCheck();
-    doSingleReferenceCheck();
+  }
+
+  private void doLinearTypeCheck() {
+    LinearTypeCheck checker = new LinearTypeCheck(this, state);
+    checker.linearTypeCheck();
   }
 
   public void debugPrint() {
     System.out.println("SSJAVA: SSJava is checking the following methods:");
     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
       MethodDescriptor md = iterator.next();
-      System.out.println("SSJAVA: " + md);
+      System.out.print(" " + md);
     }
+    System.out.println();
   }
 
   private void doMethodAnnotationCheck() {
@@ -104,11 +124,6 @@ public class SSJavaAnalysis {
     checker.definitelyWrittenCheck();
   }
 
-  public void doSingleReferenceCheck() {
-    SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
-    checker.singleReferenceCheck();
-  }
-
   private void parseLocationAnnotation() {
     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
     while (it.hasNext()) {
@@ -123,11 +138,17 @@ public class SSJavaAnalysis {
               new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
           cd2lattice.put(cd, locOrder);
           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
+
+          if (state.SSJAVADEBUG) {
+            // generate lattice dot file
+            writeLatticeDotFile(cd, locOrder);
+          }
+
         } else if (marker.equals(METHODDEFAULT)) {
           MethodLattice<String> locOrder =
               new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
           cd2methodDefault.put(cd, locOrder);
-          parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
+          parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
         }
       }
 
@@ -145,7 +166,7 @@ public class SSJavaAnalysis {
                 MethodLattice<String> locOrder =
                     new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
                 md2lattice.put(md, locOrder);
-                parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
+                parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
               } else if (an.getMarker().equals(TERMINATE)) {
                 // developer explicitly wants to skip loop termination analysis
                 String value = an.getValue();
@@ -153,6 +174,7 @@ public class SSJavaAnalysis {
                 if (value != null) {
                   maxIteration = Integer.parseInt(value);
                 }
+                System.out.println("###md=" + md);
                 skipLoopTerminate.put(md, new Integer(maxIteration));
               }
             }
@@ -164,7 +186,41 @@ public class SSJavaAnalysis {
     }
   }
 
-  private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
+  private void writeLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder) {
+
+    String className = cd.getSymbol().replaceAll("[\\W_]", "");
+
+    Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
+
+    try {
+      BufferedWriter bw = new BufferedWriter(new FileWriter(className + ".dot"));
+
+      bw.write("digraph " + className + " {\n");
+
+      for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
+        // pair is in the form of <higher, lower>
+        Pair<String, String> pair = (Pair<String, String>) iterator.next();
+
+        String highLocId = pair.getFirst();
+        if (locOrder.isSharedLoc(highLocId)) {
+          highLocId = "\"" + highLocId + "*\"";
+        }
+        String lowLocId = pair.getSecond();
+        if (locOrder.isSharedLoc(lowLocId)) {
+          lowLocId = "\"" + lowLocId + "*\"";
+        }
+        bw.write(highLocId + " -> " + lowLocId + ";\n");
+      }
+      bw.write("}\n");
+      bw.close();
+
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+  }
+
+  private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
       MethodLattice<String> locOrder) {
 
     value = value.replaceAll(" ", ""); // remove all blank spaces
@@ -188,9 +244,12 @@ public class SSJavaAnalysis {
       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
         String globalLoc = orderElement.substring(10);
         locOrder.setGlobalLoc(globalLoc);
-      } else if (orderElement.contains("*")) {
+      } else if (orderElement.startsWith(RETURNLOC + "=")) {
+        String returnLoc = orderElement.substring(10);
+        locOrder.setReturnLoc(returnLoc);
+      } else if (orderElement.endsWith("*")) {
         // spin loc definition
-        locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
+        locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
       } else {
         // single element
         locOrder.put(orderElement);
@@ -230,7 +289,7 @@ public class SSJavaAnalysis {
         }
       } else if (orderElement.contains("*")) {
         // spin loc definition
-        locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
+        locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
       } else {
         // single element
         locOrder.put(orderElement);
@@ -238,7 +297,7 @@ public class SSJavaAnalysis {
     }
 
     // sanity check
-    Set<String> spinLocSet = locOrder.getSpinLocSet();
+    Set<String> spinLocSet = locOrder.getSharedLocSet();
     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
       String spinLoc = (String) iterator.next();
       if (!locOrder.containsKey(spinLoc)) {
@@ -264,6 +323,10 @@ public class SSJavaAnalysis {
     return cd2lattice.get(cd);
   }
 
+  public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
+    return cd2methodDefault.get(cd);
+  }
+
   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
     if (md2lattice.containsKey(md)) {
       return md2lattice.get(md);
@@ -276,7 +339,20 @@ public class SSJavaAnalysis {
     return annotationRequireSet.contains(md);
   }
 
+  public boolean needToBeAnnoated(ClassDescriptor cd) {
+    return annotationRequireClassSet.contains(cd);
+  }
+
+  public void addAnnotationRequire(ClassDescriptor cd) {
+    annotationRequireClassSet.add(cd);
+  }
+
   public void addAnnotationRequire(MethodDescriptor md) {
+
+    ClassDescriptor cd = md.getClassDesc();
+    // if a method requires to be annotated, class containg that method also
+    // requires to be annotated
+    annotationRequireClassSet.add(cd);
     annotationRequireSet.add(md);
   }
 
@@ -286,7 +362,9 @@ public class SSJavaAnalysis {
 
   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
     LoopTerminate lt = new LoopTerminate();
-    lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
+    if (needTobeAnnotated(fm.getMethod())) {
+      lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
+    }
   }
 
   public void doLoopTerminationCheck(LoopOptimize lo) {
@@ -317,7 +395,7 @@ public class SSJavaAnalysis {
 
   public boolean isSharedLocation(Location loc) {
     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
-    return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
+    return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
   }
 
   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
@@ -329,4 +407,8 @@ public class SSJavaAnalysis {
     set.add(d);
   }
 
+  public BuildFlat getBuildFlat() {
+    return bf;
+  }
+
 }