add new compilation flag -ssjavainfer for the location inference.
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
index ee47d0418e346629432aaca27ee7fee82b6d5d13..bf1c6f12df1262f7c36be0b67cd2c4538465881a 100644 (file)
@@ -3,25 +3,32 @@ package Analysis.SSJava;
 import java.io.BufferedWriter;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
 import Analysis.CallGraph.CallGraph;
+import Analysis.Loops.GlobalFieldType;
 import Analysis.Loops.LoopOptimize;
 import Analysis.Loops.LoopTerminate;
 import IR.AnnotationDescriptor;
 import IR.ClassDescriptor;
 import IR.Descriptor;
+import IR.FieldDescriptor;
 import IR.MethodDescriptor;
 import IR.State;
+import IR.SymbolTable;
 import IR.TypeUtil;
 import IR.Flat.BuildFlat;
 import IR.Flat.FlatMethod;
-import IR.Tree.TreeNode;
+import IR.Flat.FlatNode;
 import Util.Pair;
 
 public class SSJavaAnalysis {
@@ -36,6 +43,8 @@ public class SSJavaAnalysis {
   public static final String DELTA = "DELTA";
   public static final String TERMINATE = "TERMINATE";
   public static final String DELEGATE = "DELEGATE";
+  public static final String DELEGATETHIS = "DELEGATETHIS";
+  public static final String TRUST = "TRUST";
 
   State state;
   TypeUtil tu;
@@ -64,6 +73,22 @@ public class SSJavaAnalysis {
   // set containing a class that has at least one annoated method
   Set<ClassDescriptor> annotationRequireClassSet;
 
+  // the set of method descriptor required to check the linear type property
+  Set<MethodDescriptor> linearTypeCheckMethodSet;
+
+  // the set of method descriptors annotated as "TRUST"
+  Set<MethodDescriptor> trustWorthyMDSet;
+
+  // points to method containing SSJAVA Loop
+  private MethodDescriptor methodContainingSSJavaLoop;
+
+  private FlatNode ssjavaLoopEntrance;
+
+  // keep the field ownership from the linear type checking
+  Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
+
+  Set<FlatNode> sameHeightWriteFlatNodeSet;
+
   CallGraph callgraph;
 
   LinearTypeCheck checker;
@@ -79,19 +104,109 @@ public class SSJavaAnalysis {
     this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
+    this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
     this.bf = bf;
+    this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
+    this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
+    this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
   }
 
   public void doCheck() {
-    doLinearTypeCheck();
-    System.exit(0);
     doMethodAnnotationCheck();
-    if (state.SSJAVADEBUG) {
-      debugPrint();
+    computeLinearTypeCheckMethodSet();
+    doLinearTypeCheck();
+    // if (state.SSJAVADEBUG) {
+    // debugPrint();
+    // }
+    if (state.SSJAVAINFER) {
+      inference();
+    } else {
+      parseLocationAnnotation();
+      doFlowDownCheck();
+      doDefinitelyWrittenCheck();
+      doLoopCheck();
+    }
+  }
+
+  private void inference() {
+    LocationInference inferEngine = new LocationInference(this, state);
+    inferEngine.inference();
+  }
+
+  private void doLoopCheck() {
+    GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
+    LoopOptimize lo = new LoopOptimize(gft, tu);
+
+    SymbolTable classtable = state.getClassSymbolTable();
+
+    List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
+    List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
+
+    toanalyzeList.addAll(classtable.getValueSet());
+    Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
+      public int compare(ClassDescriptor o1, ClassDescriptor o2) {
+        return o1.getClassName().compareTo(o2.getClassName());
+      }
+    });
+
+    for (int i = 0; i < toanalyzeList.size(); i++) {
+      ClassDescriptor cd = toanalyzeList.get(i);
+
+      SymbolTable methodtable = cd.getMethodTable();
+      toanalyzeMethodList.clear();
+      toanalyzeMethodList.addAll(methodtable.getValueSet());
+      Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
+        public int compare(MethodDescriptor o1, MethodDescriptor o2) {
+          return o1.getSymbol().compareTo(o2.getSymbol());
+        }
+      });
+
+      for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
+        MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
+        if (needTobeAnnotated(md)) {
+          lo.analyze(state.getMethodFlat(md));
+          doLoopTerminationCheck(lo, state.getMethodFlat(md));
+        }
+      }
+
     }
-    parseLocationAnnotation();
-    doFlowDownCheck();
-    doDefinitelyWrittenCheck();
+
+  }
+
+  public void addTrustMethod(MethodDescriptor md) {
+    trustWorthyMDSet.add(md);
+  }
+
+  public boolean isTrustMethod(MethodDescriptor md) {
+    return trustWorthyMDSet.contains(md);
+  }
+
+  private void computeLinearTypeCheckMethodSet() {
+
+    Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
+    linearTypeCheckMethodSet.addAll(allCalledSet);
+
+    Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
+
+    for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
+      MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
+      Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
+      trustedSet.add(trustMethod);
+      trustedSet.addAll(calledFromTrustMethodSet);
+    }
+
+    linearTypeCheckMethodSet.removeAll(trustedSet);
+
+    // if a method is called only by trusted method, no need to check linear
+    // type & flow down rule
+    for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
+      MethodDescriptor md = (MethodDescriptor) iterator.next();
+      Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
+      if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
+        linearTypeCheckMethodSet.add(md);
+      }
+    }
+
   }
 
   private void doLinearTypeCheck() {
@@ -174,7 +289,6 @@ public class SSJavaAnalysis {
                 if (value != null) {
                   maxIteration = Integer.parseInt(value);
                 }
-                System.out.println("###md=" + md);
                 skipLoopTerminate.put(md, new Integer(maxIteration));
               }
             }
@@ -259,12 +373,12 @@ public class SSJavaAnalysis {
     // sanity checks
     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
-          + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
+          + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
     }
 
     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
-          + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
+          + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
     }
   }
 
@@ -331,10 +445,20 @@ public class SSJavaAnalysis {
     if (md2lattice.containsKey(md)) {
       return md2lattice.get(md);
     } else {
-      return cd2methodDefault.get(md.getClassDesc());
+
+      if (cd2methodDefault.containsKey(md.getClassDesc())) {
+        return cd2methodDefault.get(md.getClassDesc());
+      } else {
+        throw new Error("Method Lattice of " + md + " is not defined.");
+      }
+
     }
   }
 
+  public boolean needToCheckLinearType(MethodDescriptor md) {
+    return linearTypeCheckMethodSet.contains(md);
+  }
+
   public boolean needTobeAnnotated(MethodDescriptor md) {
     return annotationRequireSet.contains(md);
   }
@@ -352,8 +476,10 @@ public class SSJavaAnalysis {
     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);
+    if (!isSSJavaUtil(cd)) {
+      annotationRequireClassSet.add(cd);
+      annotationRequireSet.add(md);
+    }
   }
 
   public Set<MethodDescriptor> getAnnotationRequireSet() {
@@ -361,24 +487,12 @@ public class SSJavaAnalysis {
   }
 
   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
-    LoopTerminate lt = new LoopTerminate();
+    LoopTerminate lt = new LoopTerminate(this, state);
     if (needTobeAnnotated(fm.getMethod())) {
       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
     }
   }
 
-  public void doLoopTerminationCheck(LoopOptimize lo) {
-    LoopTerminate lt = new LoopTerminate();
-    for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
-      MethodDescriptor md = (MethodDescriptor) iterator.next();
-      if (!skipLoopTerminate.containsKey(md)) {
-        FlatMethod fm = state.getMethodFlat(md);
-        lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
-      }
-    }
-
-  }
-
   public CallGraph getCallGraph() {
     return callgraph;
   }
@@ -411,4 +525,53 @@ public class SSJavaAnalysis {
     return bf;
   }
 
+  public MethodDescriptor getMethodContainingSSJavaLoop() {
+    return methodContainingSSJavaLoop;
+  }
+
+  public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
+    this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
+  }
+
+  public boolean isSSJavaUtil(ClassDescriptor cd) {
+    if (cd.getSymbol().equals("SSJAVA")) {
+      return true;
+    }
+    return false;
+  }
+
+  public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
+
+    Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
+    if (fieldSet == null) {
+      fieldSet = new HashSet<FieldDescriptor>();
+      mapMethodToOwnedFieldSet.put(md, fieldSet);
+    }
+    fieldSet.add(field);
+  }
+
+  public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
+    Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
+    if (fieldSet != null) {
+      return fieldSet.contains(field);
+    }
+    return false;
+  }
+
+  public FlatNode getSSJavaLoopEntrance() {
+    return ssjavaLoopEntrance;
+  }
+
+  public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
+    this.ssjavaLoopEntrance = ssjavaLoopEntrance;
+  }
+
+  public void addSameHeightWriteFlatNode(FlatNode fn) {
+    this.sameHeightWriteFlatNodeSet.add(fn);
+  }
+
+  public boolean isSameHeightWrite(FlatNode fn) {
+    return this.sameHeightWriteFlatNodeSet.contains(fn);
+  }
+
 }