changes on inference
authoryeom <yeom>
Thu, 22 Mar 2012 18:44:29 +0000 (18:44 +0000)
committeryeom <yeom>
Thu, 22 Mar 2012 18:44:29 +0000 (18:44 +0000)
Robust/src/Analysis/SSJava/InferGraph.java [new file with mode: 0644]
Robust/src/Analysis/SSJava/InferNode.java [new file with mode: 0644]
Robust/src/Analysis/SSJava/LocationInference.java
Robust/src/Analysis/SSJava/SSJavaAnalysis.java

diff --git a/Robust/src/Analysis/SSJava/InferGraph.java b/Robust/src/Analysis/SSJava/InferGraph.java
new file mode 100644 (file)
index 0000000..e49b62d
--- /dev/null
@@ -0,0 +1,71 @@
+package Analysis.SSJava;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import IR.Descriptor;
+
+public class InferGraph {
+
+  Set<InferNode> nodeSet;
+
+  // having one location for the top location
+  private static final int topLocationID = 1;
+
+  // unique ID seed
+  private static int uniqueID = 10000;
+
+  // maps descriptors (field and local var descriptors) to its unique integer id
+  Map<Descriptor, Integer> mapDescriptorToUniqueID;
+
+  // maps field/var descriptros to infer nodes
+  Map<Descriptor, InferNode> mapDescToInferNode;
+
+  boolean debug = true;
+
+  public InferGraph() {
+    nodeSet = new HashSet<InferNode>();
+    mapDescToInferNode = new HashMap<Descriptor, InferNode>();
+    mapDescriptorToUniqueID = new HashMap<Descriptor, Integer>();
+  }
+
+  public void addValueFlowEdge(Descriptor fromDesc, Descriptor toDesc) {
+
+  }
+
+  public InferNode getInferNode(Descriptor desc) {
+    if (mapDescToInferNode.containsKey(desc)) {
+
+    }
+    return null;
+  }
+
+  public void assignTopLocationToDescriptor(Descriptor desc) {
+    mapDescriptorToUniqueID.put(desc, Integer.valueOf((topLocationID)));
+    debug_uniqueid_print(desc);
+  }
+
+  public void assignUniqueIDtoDescriptor(Descriptor desc) {
+    mapDescriptorToUniqueID.put(desc, getUniqueID());
+    debug_uniqueid_print(desc);
+  }
+
+  private int getUniqueID() {
+    return uniqueID++;
+  }
+
+  private void debug_uniqueid_print(Descriptor d) {
+    if (debug) {
+      int id = mapDescriptorToUniqueID.get(d).intValue();
+      System.out.print(d + " -> ");
+      if (id == topLocationID) {
+        System.out.println("TOP");
+      } else {
+        System.out.println(id);
+      }
+
+    }
+  }
+}
diff --git a/Robust/src/Analysis/SSJava/InferNode.java b/Robust/src/Analysis/SSJava/InferNode.java
new file mode 100644 (file)
index 0000000..b00b4e0
--- /dev/null
@@ -0,0 +1,26 @@
+package Analysis.SSJava;
+
+import IR.Descriptor;
+
+public class InferNode {
+
+  private Descriptor desc;
+  private int id;
+
+  public Descriptor getDesc() {
+    return desc;
+  }
+
+  public void setDesc(Descriptor desc) {
+    this.desc = desc;
+  }
+
+  public int getId() {
+    return id;
+  }
+
+  public void setId(int id) {
+    this.id = id;
+  }
+
+}
index 267948ad964c299f37def083a835963617634124..2bb1330bdd5d46ea3a58227c8ec2384c737e12fe 100644 (file)
@@ -3,19 +3,40 @@ package Analysis.SSJava;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import IR.ClassDescriptor;
 
 import IR.ClassDescriptor;
+import IR.Descriptor;
 import IR.FieldDescriptor;
 import IR.MethodDescriptor;
 import IR.State;
 import IR.SymbolTable;
 import IR.VarDescriptor;
 import IR.FieldDescriptor;
 import IR.MethodDescriptor;
 import IR.State;
 import IR.SymbolTable;
 import IR.VarDescriptor;
+import IR.Tree.ArrayAccessNode;
+import IR.Tree.AssignmentNode;
+import IR.Tree.BlockExpressionNode;
 import IR.Tree.BlockNode;
 import IR.Tree.BlockStatementNode;
 import IR.Tree.BlockNode;
 import IR.Tree.BlockStatementNode;
+import IR.Tree.CastNode;
+import IR.Tree.CreateObjectNode;
 import IR.Tree.DeclarationNode;
 import IR.Tree.DeclarationNode;
+import IR.Tree.ExpressionNode;
+import IR.Tree.FieldAccessNode;
+import IR.Tree.IfStatementNode;
 import IR.Tree.Kind;
 import IR.Tree.Kind;
+import IR.Tree.LiteralNode;
+import IR.Tree.LoopNode;
+import IR.Tree.MethodInvokeNode;
+import IR.Tree.NameNode;
+import IR.Tree.OpNode;
+import IR.Tree.ReturnNode;
+import IR.Tree.SubBlockNode;
+import IR.Tree.SwitchBlockNode;
+import IR.Tree.SwitchStatementNode;
+import IR.Tree.TertiaryNode;
 
 public class LocationInference {
 
 
 public class LocationInference {
 
@@ -25,11 +46,16 @@ public class LocationInference {
   List<ClassDescriptor> toanalyzeList;
   List<MethodDescriptor> toanalyzeMethodList;
 
   List<ClassDescriptor> toanalyzeList;
   List<MethodDescriptor> toanalyzeMethodList;
 
+  boolean debug = true;
+
+  InferGraph graph;
+
   public LocationInference(SSJavaAnalysis ssjava, State state) {
     this.ssjava = ssjava;
     this.state = state;
     this.toanalyzeList = new ArrayList<ClassDescriptor>();
     this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
   public LocationInference(SSJavaAnalysis ssjava, State state) {
     this.ssjava = ssjava;
     this.state = state;
     this.toanalyzeList = new ArrayList<ClassDescriptor>();
     this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
+    this.graph = new InferGraph();
   }
 
   public void setupToAnalyze() {
   }
 
   public void setupToAnalyze() {
@@ -80,96 +106,168 @@ public class LocationInference {
         analyzeFieldDeclaration(cd, fd);
       } else {
         // for static final, assign top location by default
         analyzeFieldDeclaration(cd, fd);
       } else {
         // for static final, assign top location by default
+        graph.assignTopLocationToDescriptor(fd);
       }
     }
   }
 
   private void analyzeFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
       }
     }
   }
 
   private void analyzeFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
-    // assign a unique ID to field
+    graph.assignUniqueIDtoDescriptor(fd);
   }
 
   public void inference() {
 
   }
 
   public void inference() {
 
+    // 1) assign a unique id to every field & variable
     setupToAnalyze();
 
     while (!toAnalyzeIsEmpty()) {
       ClassDescriptor cd = toAnalyzeNext();
 
     setupToAnalyze();
 
     while (!toAnalyzeIsEmpty()) {
       ClassDescriptor cd = toAnalyzeNext();
 
+      System.out.println("SSJAVA: Location Inference on the class: " + cd);
       checkDeclarationInClass(cd);
 
       setupToAnalazeMethod(cd);
       while (!toAnalyzeMethodIsEmpty()) {
         MethodDescriptor md = toAnalyzeMethodNext();
 
       checkDeclarationInClass(cd);
 
       setupToAnalazeMethod(cd);
       while (!toAnalyzeMethodIsEmpty()) {
         MethodDescriptor md = toAnalyzeMethodNext();
 
-        // need to analyze method declaration for assigning unique id to
-        // parameters(including 'this' variable)
+        if (ssjava.needTobeAnnotated(md)) {
+          // assigns unique id to the method parameters
+          assignUniqueIDMethodParamteres(cd, md);
+
+          if (state.SSJAVADEBUG) {
+            System.out.println("SSJAVA: Location Inference on the method: " + md);
+          }
+          assignUniqueIDMethodBody(cd, md);
+        }
+      }
+    }
+
+    // 2) construct value flow graph
+
+    setupToAnalyze();
+
+    while (!toAnalyzeIsEmpty()) {
+      ClassDescriptor cd = toAnalyzeNext();
 
 
+      setupToAnalazeMethod(cd);
+      while (!toAnalyzeMethodIsEmpty()) {
+        MethodDescriptor md = toAnalyzeMethodNext();
         if (ssjava.needTobeAnnotated(md)) {
           if (state.SSJAVADEBUG) {
         if (ssjava.needTobeAnnotated(md)) {
           if (state.SSJAVADEBUG) {
-            System.out.println("SSJAVA: Location Inference: " + md);
+            System.out.println("SSJAVA: Constructing a flow graph: " + md);
           }
           }
-          analyzeMethodBody(cd, md, null);
+          analyzeMethodBody(cd, md);
         }
       }
     }
 
   }
 
         }
       }
     }
 
   }
 
-  private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md,
-      CompositeLocation constraints) {
+  private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
     BlockNode bn = state.getMethodBody(md);
     BlockNode bn = state.getMethodBody(md);
-    analyzeBlockNode(md, md.getParameterTable(), bn);
+    // checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
   }
 
   }
 
-  private CompositeLocation analyzeBlockNode(MethodDescriptor md, SymbolTable nametable,
-      BlockNode bn) {
+  private void assignUniqueIDMethodParamteres(ClassDescriptor cd, MethodDescriptor md) {
+
+    List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
+    for (int i = 0; i < md.numParameters(); i++) {
+      // process annotations on method parameters
+      VarDescriptor vd = (VarDescriptor) md.getParameter(i);
+      graph.assignUniqueIDtoDescriptor(vd);
+    }
+
+  }
+
+  private void assignUniqueIDMethodBody(ClassDescriptor cd, MethodDescriptor md) {
+    BlockNode bn = state.getMethodBody(md);
+    assignUniqueIDBlockNode(md, md.getParameterTable(), bn);
+  }
+
+  private void assignUniqueIDBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
 
     bn.getVarTable().setParent(nametable);
     for (int i = 0; i < bn.size(); i++) {
       BlockStatementNode bsn = bn.get(i);
 
     bn.getVarTable().setParent(nametable);
     for (int i = 0; i < bn.size(); i++) {
       BlockStatementNode bsn = bn.get(i);
-      analyzeBlockStatementNode(md, bn.getVarTable(), bsn);
+      assignUniqueIDBlockStatementNode(md, bn.getVarTable(), bsn);
     }
     }
-    return new CompositeLocation();
 
   }
 
 
   }
 
-  private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
+  private void assignUniqueIDBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
       BlockStatementNode bsn) {
 
     switch (bsn.kind()) {
       BlockStatementNode bsn) {
 
     switch (bsn.kind()) {
-    case Kind.BlockExpressionNode:
-      // checkBlockExpressionNode(md,(BlockExpressionNode)bsn);
-      break;
 
     case Kind.DeclarationNode:
 
     case Kind.DeclarationNode:
-      analyzeDeclarationNode(md, nametable, (DeclarationNode) bsn);
+      assignUniqueIDDeclarationNode(md, nametable, (DeclarationNode) bsn);
       break;
 
     case Kind.IfStatementNode:
       break;
 
     case Kind.IfStatementNode:
+      assignUniqueIDIfStatementNode(md, nametable, (IfStatementNode) bsn);
       break;
 
     case Kind.LoopNode:
       break;
 
     case Kind.LoopNode:
-      break;
-
-    case Kind.ReturnNode:
+      assignUniqueIDLoopNode(md, nametable, (LoopNode) bsn);
       break;
 
     case Kind.SubBlockNode:
       break;
 
     case Kind.SubBlockNode:
+      assignUniqueIDSubBlockNode(md, nametable, (SubBlockNode) bsn);
       break;
 
     case Kind.ContinueBreakNode:
       break;
 
     case Kind.SwitchStatementNode:
       break;
 
     case Kind.ContinueBreakNode:
       break;
 
     case Kind.SwitchStatementNode:
-      break;
+      assignUniqueIDSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
+    }
+
+  }
 
 
+  private void assignUniqueIDSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
+      SwitchStatementNode ssn) {
+    BlockNode sbn = ssn.getSwitchBody();
+    for (int i = 0; i < sbn.size(); i++) {
+      SwitchBlockNode node = (SwitchBlockNode) sbn.get(i);
+      assignUniqueIDBlockNode(md, nametable, node.getSwitchBlockStatement());
     }
   }
 
     }
   }
 
-  private void analyzeDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
+  private void assignUniqueIDSubBlockNode(MethodDescriptor md, SymbolTable nametable,
+      SubBlockNode sbn) {
+    assignUniqueIDBlockNode(md, nametable, sbn.getBlockNode());
+  }
 
 
-    VarDescriptor vd = dn.getVarDescriptor();
+  private void assignUniqueIDLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
 
 
+    if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
+      assignUniqueIDBlockNode(md, nametable, ln.getBody());
+    } else {
+      // check 'for loop' case
+      BlockNode bn = ln.getInitializer();
+      bn.getVarTable().setParent(nametable);
+      assignUniqueIDBlockNode(md, bn.getVarTable(), ln.getUpdate());
+      assignUniqueIDBlockNode(md, bn.getVarTable(), ln.getBody());
+    }
+
+  }
+
+  private void assignUniqueIDIfStatementNode(MethodDescriptor md, SymbolTable nametable,
+      IfStatementNode isn) {
+
+    assignUniqueIDBlockNode(md, nametable, isn.getTrueBlock());
+
+    if (isn.getFalseBlock() != null) {
+      assignUniqueIDBlockNode(md, nametable, isn.getFalseBlock());
+    }
+
+  }
+
+  private void assignUniqueIDDeclarationNode(MethodDescriptor md, SymbolTable nametable,
+      DeclarationNode dn) {
+
+    VarDescriptor vd = dn.getVarDescriptor();
+    graph.assignUniqueIDtoDescriptor(vd);
   }
 
 }
   }
 
 }
index 146af4e6215a5bc2b8696df6c15177c10a15a79f..31075ece9d5fbde878f7fd9d855a95b08fcc2cf3 100644 (file)
@@ -119,7 +119,7 @@ public class SSJavaAnalysis {
       // debugPrint();
     }
     if (state.SSJAVAINFER) {
       // debugPrint();
     }
     if (state.SSJAVAINFER) {
-      // inference();
+       inference();
     } else {
       parseLocationAnnotation();
       doFlowDownCheck();
     } else {
       parseLocationAnnotation();
       doFlowDownCheck();