generate annotated source code again but it's still not the correct one...
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
index 146af4e6215a5bc2b8696df6c15177c10a15a79f..46a22966b3fa55f5d5c45a74e5533bf62303d738 100644 (file)
@@ -6,10 +6,13 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
@@ -39,6 +42,7 @@ public class SSJavaAnalysis {
   public static final String THISLOC = "THISLOC";
   public static final String GLOBALLOC = "GLOBALLOC";
   public static final String RETURNLOC = "RETURNLOC";
+  public static final String PCLOC = "PCLOC";
   public static final String LOC = "LOC";
   public static final String DELTA = "DELTA";
   public static final String TERMINATE = "TERMINATE";
@@ -46,6 +50,9 @@ public class SSJavaAnalysis {
   public static final String DELEGATETHIS = "DELEGATETHIS";
   public static final String TRUST = "TRUST";
 
+  public static final String TOP = "_top_";
+  public static final String BOTTOM = "_bottom_";
+
   State state;
   TypeUtil tu;
   FlowDownCheck flowDownChecker;
@@ -79,6 +86,9 @@ public class SSJavaAnalysis {
   // the set of method descriptors annotated as "TRUST"
   Set<MethodDescriptor> trustWorthyMDSet;
 
+  // method -> the initial program counter location
+  Map<MethodDescriptor, CompositeLocation> md2pcLoc;
+
   // points to method containing SSJAVA Loop
   private MethodDescriptor methodContainingSSJavaLoop;
 
@@ -93,6 +103,13 @@ public class SSJavaAnalysis {
 
   LinearTypeCheck checker;
 
+  // maps a descriptor to its known dependents: namely
+  // methods or tasks that call the descriptor's method
+  // AND are part of this analysis (reachable from main)
+  private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
+
+  private LinkedList<MethodDescriptor> sortedDescriptors;
+
   public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
     this.state = state;
     this.tu = tu;
@@ -109,17 +126,26 @@ public class SSJavaAnalysis {
     this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
     this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
     this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
+    this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
+    this.sortedDescriptors = new LinkedList<MethodDescriptor>();
+    this.md2pcLoc = new HashMap<MethodDescriptor, CompositeLocation>();
   }
 
   public void doCheck() {
     doMethodAnnotationCheck();
-    computeLinearTypeCheckMethodSet();
-    doLinearTypeCheck();
+
+    if (state.SSJAVA && !state.SSJAVAINFER) {
+      computeLinearTypeCheckMethodSet();
+      doLinearTypeCheck();
+      init();
+    }
+
     if (state.SSJAVADEBUG) {
-      // debugPrint();
+      // debug_printAnnotationRequiredSet();
     }
     if (state.SSJAVAINFER) {
-      // inference();
+      inference();
+      System.exit(0);
     } else {
       parseLocationAnnotation();
       doFlowDownCheck();
@@ -128,6 +154,18 @@ public class SSJavaAnalysis {
     }
   }
 
+  public void init() {
+    // perform topological sort over the set of methods accessed by the main
+    // event loop
+    Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
+    methodDescriptorsToAnalyze.addAll(getAnnotationRequireSet());
+    sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
+  }
+
+  public LinkedList<MethodDescriptor> getSortedDescriptors() {
+    return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
+  }
+
   private void inference() {
     LocationInference inferEngine = new LocationInference(this, state);
     inferEngine.inference();
@@ -214,11 +252,11 @@ public class SSJavaAnalysis {
     checker.linearTypeCheck();
   }
 
-  public void debugPrint() {
+  public void debug_printAnnotationRequiredSet() {
     System.out.println("SSJAVA: SSJava is checking the following methods:");
     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
       MethodDescriptor md = iterator.next();
-      System.out.print(" " + md);
+      System.out.println(md);
     }
     System.out.println();
   }
@@ -227,6 +265,10 @@ public class SSJavaAnalysis {
     methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
     methodAnnotationChecker.methodAnnoatationCheck();
     methodAnnotationChecker.methodAnnoataionInheritanceCheck();
+    if (state.SSJAVAINFER) {
+      annotationRequireClassSet.add(methodContainingSSJavaLoop.getClassDesc());
+      annotationRequireSet.add(methodContainingSSJavaLoop);
+    }
     state.setAnnotationRequireSet(annotationRequireSet);
   }
 
@@ -251,18 +293,18 @@ public class SSJavaAnalysis {
         String marker = an.getMarker();
         if (marker.equals(LATTICE)) {
           SSJavaLattice<String> locOrder =
-              new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
+              new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
           cd2lattice.put(cd, locOrder);
           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
 
           if (state.SSJAVADEBUG) {
             // generate lattice dot file
-            writeLatticeDotFile(cd, locOrder);
+            writeLatticeDotFile(cd, null, locOrder);
           }
 
         } else if (marker.equals(METHODDEFAULT)) {
           MethodLattice<String> locOrder =
-              new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
+              new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
           cd2methodDefault.put(cd, locOrder);
           parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
         }
@@ -280,7 +322,7 @@ public class SSJavaAnalysis {
               if (an.getMarker().equals(LATTICE)) {
                 // developer explicitly defines method lattice
                 MethodLattice<String> locOrder =
-                    new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
+                    new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
                 md2lattice.put(md, locOrder);
                 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
               } else if (an.getMarker().equals(TERMINATE)) {
@@ -301,36 +343,51 @@ public class SSJavaAnalysis {
     }
   }
 
-  private void writeLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder) {
+  public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
+      SSJavaLattice<T> locOrder) {
 
-    String className = cd.getSymbol().replaceAll("[\\W_]", "");
+    String fileName = "lattice_";
+    if (md != null) {
+      fileName +=
+          cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.getSymbol().replaceAll("[\\W_]", "");
+    } else {
+      fileName += cd.getSymbol().replaceAll("[\\W_]", "");
+    }
 
-    Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
+    Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
 
-    try {
-      BufferedWriter bw = new BufferedWriter(new FileWriter(className + ".dot"));
+    if (pairSet.size() > 0) {
+      try {
+        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
 
-      bw.write("digraph " + className + " {\n");
+        bw.write("digraph " + fileName + " {\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();
+        for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
+          // pair is in the form of <higher, lower>
+          Pair<T, T> pair = (Pair<T, T>) iterator.next();
 
-        String highLocId = pair.getFirst();
-        if (locOrder.isSharedLoc(highLocId)) {
-          highLocId = "\"" + highLocId + "*\"";
-        }
-        String lowLocId = pair.getSecond();
-        if (locOrder.isSharedLoc(lowLocId)) {
-          lowLocId = "\"" + lowLocId + "*\"";
+          T highLocId = pair.getFirst();
+          String highLocStr, lowLocStr;
+          if (locOrder.isSharedLoc(highLocId)) {
+            highLocStr = "\"" + highLocId + "*\"";
+          } else {
+            highLocStr = highLocId.toString();
+          }
+          T lowLocId = pair.getSecond();
+          if (locOrder.isSharedLoc(lowLocId)) {
+            lowLocStr = "\"" + lowLocId + "*\"";
+          } else {
+            lowLocStr = lowLocId.toString();
+          }
+          bw.write(highLocStr + " -> " + lowLocStr + ";\n");
         }
-        bw.write(highLocId + " -> " + lowLocId + ";\n");
+        bw.write("}\n");
+        bw.close();
+
+      } catch (IOException e) {
+        e.printStackTrace();
       }
-      bw.write("}\n");
-      bw.close();
 
-    } catch (IOException e) {
-      e.printStackTrace();
     }
 
   }
@@ -456,6 +513,19 @@ public class SSJavaAnalysis {
     }
   }
 
+  public CompositeLocation getPCLocation(MethodDescriptor md) {
+    if (!md2pcLoc.containsKey(md)) {
+      // by default, the initial pc location is TOP
+      CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
+      md2pcLoc.put(md, pcLoc);
+    }
+    return md2pcLoc.get(md);
+  }
+
+  public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
+    md2pcLoc.put(md, pcLoc);
+  }
+
   public boolean needToCheckLinearType(MethodDescriptor md) {
     return linearTypeCheckMethodSet.contains(md);
   }
@@ -575,4 +645,78 @@ public class SSJavaAnalysis {
     return this.sameHeightWriteFlatNodeSet.contains(fn);
   }
 
+  public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
+
+    Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
+
+    LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
+
+    Iterator<MethodDescriptor> itr = toSort.iterator();
+    while (itr.hasNext()) {
+      MethodDescriptor d = itr.next();
+
+      if (!discovered.contains(d)) {
+        dfsVisit(d, toSort, sorted, discovered);
+      }
+    }
+
+    return sorted;
+  }
+
+  // While we're doing DFS on call graph, remember
+  // dependencies for efficient queuing of methods
+  // during interprocedural analysis:
+  //
+  // a dependent of a method decriptor d for this analysis is:
+  // 1) a method or task that invokes d
+  // 2) in the descriptorsToAnalyze set
+  private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
+      LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
+
+    discovered.add(md);
+
+    Iterator itr2 = callgraph.getCalleeSet(md).iterator();
+    while (itr2.hasNext()) {
+      MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
+      addDependent(dCallee, md);
+    }
+
+    Iterator itr = callgraph.getCallerSet(md).iterator();
+    while (itr.hasNext()) {
+      MethodDescriptor dCaller = (MethodDescriptor) itr.next();
+      // only consider callers in the original set to analyze
+      if (!toSort.contains(dCaller)) {
+        continue;
+      }
+      if (!discovered.contains(dCaller)) {
+        addDependent(md, // callee
+            dCaller // caller
+        );
+
+        dfsVisit(dCaller, toSort, sorted, discovered);
+      }
+    }
+
+    // for leaf-nodes last now!
+    sorted.addLast(md);
+  }
+
+  public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
+    Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
+    if (deps == null) {
+      deps = new HashSet<MethodDescriptor>();
+    }
+    deps.add(caller);
+    mapDescriptorToSetDependents.put(callee, deps);
+  }
+
+  public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
+    Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
+    if (deps == null) {
+      deps = new HashSet<MethodDescriptor>();
+      mapDescriptorToSetDependents.put(callee, deps);
+    }
+    return deps;
+  }
+
 }