changes.
[IRC.git] / Robust / src / Analysis / SSJava / BuildLattice.java
index eca8dfaa06d8878871bd682f4de70154d1b5d5f2..eb38f2709aea6053819aab8cef5e4e4799b1c9bf 100644 (file)
@@ -14,11 +14,19 @@ public class BuildLattice {
   public static int seed = 0;
   private LocationInference infer;
 
+  private final HNode topNode;
+  private final HNode bottomNode;
+
   public BuildLattice(LocationInference infer) {
     this.infer = infer;
+    topNode = new HNode(infer.ssjava.TOP);
+    bottomNode = new HNode(infer.ssjava.BOTTOM);
   }
 
-  public SSJavaLattice<String> buildLattice(HierarchyGraph inputGraph) {
+  public SSJavaLattice<String> buildLattice(Descriptor desc) {
+
+    HierarchyGraph inputGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
+    LocationSummary locSummary = infer.getLocationSummary(desc);
 
     BasisSet basisSet = inputGraph.computeBasisSet();
     debug_print(inputGraph);
@@ -26,13 +34,13 @@ public class BuildLattice {
     Family family = generateFamily(basisSet);
     Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = coveringGraph(basisSet, family);
 
-    SSJavaLattice<String> lattice = buildLattice(basisSet, inputGraph, mapImSucc);
+    SSJavaLattice<String> lattice = buildLattice(basisSet, inputGraph, locSummary, mapImSucc);
     return lattice;
 
   }
 
   private SSJavaLattice<String> buildLattice(BasisSet basisSet, HierarchyGraph inputGraph,
-      Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
+      LocationSummary locSummary, Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
 
     SSJavaLattice<String> lattice =
         new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
@@ -45,11 +53,28 @@ public class BuildLattice {
 
       String higherName = generateElementName(basisSet, inputGraph, mapFToLocName, higher);
 
+      HNode higherNode = inputGraph.getHNode(higherName);
+      if (higherNode != null && higherNode.isSharedNode()) {
+        lattice.addSharedLoc(higherName);
+      }
+      Set<Descriptor> descSet = inputGraph.getDescSetOfNode(higherNode);
+      for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
+        Descriptor d = (Descriptor) iterator2.next();
+        locSummary.addMapHNodeNameToLocationName(d.getSymbol(), higherName);
+      }
+      // locSummary.addMapHNodeNameToLocationName(higherName, higherName);
+
       Set<Set<Integer>> lowerSet = mapImSucc.get(higher);
       for (Iterator iterator2 = lowerSet.iterator(); iterator2.hasNext();) {
         Set<Integer> lower = (Set<Integer>) iterator2.next();
 
         String lowerName = generateElementName(basisSet, inputGraph, mapFToLocName, lower);
+        locSummary.addMapHNodeNameToLocationName(lowerName, lowerName);
+
+        HNode lowerNode = inputGraph.getHNode(higherName);
+        if (lowerNode != null && lowerNode.isSharedNode()) {
+          lattice.addSharedLoc(lowerName);
+        }
 
         if (higher.size() == 0) {
           // empty case
@@ -65,12 +90,24 @@ public class BuildLattice {
     return lattice;
   }
 
-  public HNode getCombinationNodeInSCGraph(Descriptor desc, HNode simpleGraphNode) {
+  public HNode getCombinationNodeInSCGraph(Descriptor desc, HNode nodeFromSimpleGraph) {
+
+    HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
+
+    if (nodeFromSimpleGraph.isSkeleton()) {
+      return scGraph.getCurrentHNode(nodeFromSimpleGraph);
+    }
 
     Set<HNode> combineSkeletonNodeSet =
-        infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(simpleGraphNode);
+        infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(nodeFromSimpleGraph);
     HNode combinationNodeInSCGraph =
-        infer.getSkeletonCombinationHierarchyGraph(desc).getCombinationNode(combineSkeletonNodeSet);
+        infer.getSkeletonCombinationHierarchyGraph(desc).getMapCombineNodeSetToCombinationNode()
+            .get(combineSkeletonNodeSet);
+
+    // Set<HNode> combineSkeletonNodeSet =
+    // infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(simpleGraphNode);
+    // HNode combinationNodeInSCGraph =
+    // infer.getSkeletonCombinationHierarchyGraph(desc).getCombinationNode(combineSkeletonNodeSet);
     return combinationNodeInSCGraph;
   }
 
@@ -82,19 +119,19 @@ public class BuildLattice {
 
     HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
     HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
+    LocationSummary locSummary = infer.getLocationSummary(desc);
 
     SSJavaLattice<String> lattice = skeletonLattice.clone();
 
-    Map<TripleItem, String> mapIntermediateLocation = new HashMap<TripleItem, String>();
-
     Set<HNode> visited = new HashSet<HNode>();
 
     Set<HNode> nodeSet = simpleGraph.getNodeSet();
 
-    // expand a combination node
     Map<TripleItem, String> mapIntermediateLoc = new HashMap<TripleItem, String>();
+
     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
       HNode node = (HNode) iterator.next();
+      System.out.println("node=" + node);
       if (node.isSkeleton() && (!visited.contains(node))) {
         visited.add(node);
 
@@ -104,53 +141,110 @@ public class BuildLattice {
 
           if (!outNode.isSkeleton()) {
             if (outNode.isCombinationNode()) {
+              // expand the combination node 'outNode'
               // here we need to expand the corresponding combination location in the lattice
               HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, outNode);
 
               Set<HNode> combineSkeletonNodeSet =
                   simpleGraph.getCombineSetByCombinationNode(outNode);
+
+              // System.out.println("combineSkeletonNodeSet=" + combineSkeletonNodeSet);
+
               Set<HNode> combinationNodeSet =
                   simpleGraph.getCombinationNodeSetByCombineNodeSet(combineSkeletonNodeSet);
+
+              // System.out.println("combinationNodeSet=" + combinationNodeSet);
+
               Set<HNode> endNodeSetFromSimpleGraph =
                   simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(outNode,
                       combinationNodeSet);
+              // System.out.println("-endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
               Set<HNode> endCombNodeSet = new HashSet<HNode>();
               for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
                 HNode endNode = (HNode) iterator3.next();
                 endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
               }
               visited.add(outNode);
+
               // follows the straight line up to another skeleton/combination node
               if (endCombNodeSet.size() > 0) {
+                endCombNodeSet =
+                    removeTransitivelyReachToNode(desc, combinationNodeInSCGraph, endCombNodeSet);
                 recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
-                    mapIntermediateLoc, 1, outNode);
+                    mapIntermediateLoc, 1, locSummary, outNode);
               }
 
             } else {
               // we have a node that is neither combination or skeleton node
-              HNode startNode = node;
+              System.out.println("%%%skeleton node=" + node + "  outNode=" + outNode);
+              HNode startNode = scGraph.getCurrentHNode(node);
+
+              // if (node.getDescriptor() != null) {
+              // // node is a skeleton node and it might be merged into another node in the SC
+              // graph.
+              // startNode = scGraph.getHNode(node.getDescriptor());
+              // } else {
+              // // this node has already been merged before the SC graph.
+              // startNode = node;
+              // }
+
               Set<HNode> endNodeSetFromSimpleGraph =
                   simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(outNode, null);
 
+              System.out.println("endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph
+                  + "   from=" + outNode);
               Set<HNode> endCombNodeSet = new HashSet<HNode>();
               for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
                 HNode endNode = (HNode) iterator3.next();
                 endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
               }
-
+              System.out.println("endCombNodeSet=" + endCombNodeSet);
               visited.add(outNode);
               if (endCombNodeSet.size() > 0) {
                 // follows the straight line up to another skeleton/combination node
-                recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
-                    mapIntermediateLoc, 1, outNode);
-
+                endCombNodeSet = removeTransitivelyReachToNode(desc, startNode, endCombNodeSet);
+              } else if (endCombNodeSet.size() == 0) {
+                // the outNode is (directly/transitively) connected to the bottom node
+                // therefore, we just add a dummy bottom HNode to the endCombNodeSet.
+                endCombNodeSet.add(bottomNode);
               }
-
+              recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
+                  mapIntermediateLoc, 1, locSummary, outNode);
             }
 
           }
 
         }
+      } else if (!node.isSkeleton() && !node.isCombinationNode() && !node.isMergeNode()
+          && !visited.contains(node)) {
+
+        // an intermediate node 'node' may be located between "TOP" location and a skeleton node
+        int sizeIncomingNode = simpleGraph.getIncomingNodeSet(node).size();
+
+        if (sizeIncomingNode == 0) {
+          // this node will be directly connected to the TOP location
+          // start adding the following nodes from this node
+
+          Set<HNode> endNodeSetFromSimpleGraph =
+              simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(node, null);
+
+          Set<HNode> endCombNodeSet = new HashSet<HNode>();
+          for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
+            HNode endNode = (HNode) iterator3.next();
+            endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
+          }
+
+          HNode startNode = topNode;
+          visited.add(startNode);
+          if (endCombNodeSet.size() > 0) {
+            // follows the straight line up to another skeleton/combination node
+            // endCombNodeSet = removeTransitivelyReachToNode(desc, node, endCombNodeSet);
+            recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
+                mapIntermediateLoc, 1, locSummary, node);
+          }
+
+        }
+
       }
     }
 
@@ -158,12 +252,63 @@ public class BuildLattice {
 
   }
 
+  private Set<HNode> removeTransitivelyReachToNode(Descriptor desc, HNode startNode,
+      Set<HNode> endNodeSet) {
+
+    // if an end node is not directly connected to the start node in the SC graph
+    // replace it with a directly connected one which transitively reaches to it.
+
+    HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
+    Set<HNode> newEndNodeSet = new HashSet<HNode>();
+
+    for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
+      HNode endNode = (HNode) iterator.next();
+      if (scGraph.isDirectlyConnectedTo(startNode, endNode)) {
+        newEndNodeSet.add(endNode);
+      } else {
+        HNode newEndNode =
+            getDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode);
+        // System.out.println("#### old END NODE=" + endNode + " --->" + newEndNode);
+        newEndNodeSet.add(newEndNode);
+      }
+    }
+
+    // System.out.println("removeTransitivelyReachToNode=" + endNodeSet + "  newSet=" +
+    // newEndNodeSet);
+
+    return newEndNodeSet;
+
+  }
+
+  private HNode getDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
+      HNode startNode, HNode endNode) {
+    Set<HNode> connected = new HashSet<HNode>();
+    recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode, connected);
+    return connected.iterator().next();
+  }
+
+  private void recurDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
+      HNode startNode, HNode curNode, Set<HNode> connected) {
+
+    Set<HNode> inNodeSet = scGraph.getIncomingNodeSet(curNode);
+    for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
+      HNode inNode = (HNode) iterator.next();
+      if (inNode.equals(startNode)) {
+        connected.add(curNode);
+      } else {
+        // System.out.println("inNode=" + inNode);
+        recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, inNode, connected);
+      }
+    }
+
+  }
+
   private void recurDFSNormalNode(Descriptor desc, SSJavaLattice<String> lattice, HNode startNode,
       Set<HNode> endNodeSet, Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc,
-      int idx, HNode curNode) {
+      int idx, LocationSummary locSummary, HNode curNode) {
 
     TripleItem item = new TripleItem(startNode, endNodeSet, idx);
-
+    // System.out.println("item=" + item);
     if (!mapIntermediateLoc.containsKey(item)) {
       // need to create a new intermediate location in the lattice
       String newLocName = "ILOC" + (seed++);
@@ -188,15 +333,22 @@ public class BuildLattice {
     }
 
     String locName = mapIntermediateLoc.get(item);
-
     HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
+
+    Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
+    for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
+      Descriptor d = (Descriptor) iterator.next();
+      locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
+    }
+    // locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
+
     Set<HNode> outSet = graph.getOutgoingNodeSet(curNode);
     for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
       HNode outNode = (HNode) iterator2.next();
       if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
         visited.add(outNode);
         recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
-            idx + 1, outNode);
+            idx + 1, locSummary, outNode);
       }
     }
 
@@ -204,36 +356,45 @@ public class BuildLattice {
 
   private void recurDFS(Descriptor desc, SSJavaLattice<String> lattice,
       HNode combinationNodeInSCGraph, Set<HNode> endNodeSet, Set<HNode> visited,
-      Map<TripleItem, String> mapIntermediateLoc, int idx, HNode curNode) {
+      Map<TripleItem, String> mapIntermediateLoc, int idx, LocationSummary locSummary, HNode curNode) {
 
     TripleItem item = new TripleItem(combinationNodeInSCGraph, endNodeSet, idx);
 
     if (!mapIntermediateLoc.containsKey(item)) {
       // need to create a new intermediate location in the lattice
-      String newLocName = "ILOC" + (seed++);
       String above;
       if (idx == 1) {
-        above = combinationNodeInSCGraph.getName();
+        String newLocName = combinationNodeInSCGraph.getName();
+        mapIntermediateLoc.put(item, newLocName);
       } else {
+        String newLocName = "ILOC" + (seed++);
         int prevIdx = idx - 1;
         TripleItem prevItem = new TripleItem(combinationNodeInSCGraph, endNodeSet, prevIdx);
         above = mapIntermediateLoc.get(prevItem);
-      }
-
-      Set<String> belowSet = new HashSet<String>();
-      for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
-        HNode endNode = (HNode) iterator.next();
-        belowSet.add(endNode.getName());
-      }
-      lattice.insertNewLocationBetween(above, belowSet, newLocName);
 
-      mapIntermediateLoc.put(item, newLocName);
+        Set<String> belowSet = new HashSet<String>();
+        for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
+          HNode endNode = (HNode) iterator.next();
+          belowSet.add(endNode.getName());
+        }
+        lattice.insertNewLocationBetween(above, belowSet, newLocName);
+        mapIntermediateLoc.put(item, newLocName);
 
-      String locName = mapIntermediateLoc.get(item);
+      }
 
     }
 
     HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
+    String locName = mapIntermediateLoc.get(item);
+    Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
+    for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
+      Descriptor d = (Descriptor) iterator.next();
+      locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
+    }
+
+    // System.out.println("-TripleItem=" + item);
+    // System.out.println("-curNode=" + curNode.getName() + " locName=" + locName);
+
     Set<HNode> outSet = graph.getOutgoingNodeSet(curNode);
     for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
       HNode outNode = (HNode) iterator2.next();
@@ -241,7 +402,7 @@ public class BuildLattice {
         if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
           visited.add(outNode);
           recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
-              mapIntermediateLoc, idx + 1, outNode);
+              mapIntermediateLoc, idx + 1, locSummary, outNode);
         }
       }
     }
@@ -316,7 +477,7 @@ public class BuildLattice {
       resetCount(mapFtoCount, family);
     }
 
-    System.out.println("mapImSucc=" + mapImSucc);
+    // System.out.println("mapImSucc=" + mapImSucc);
 
     return mapImSucc;
   }
@@ -381,8 +542,8 @@ public class BuildLattice {
 
   private void debug_print(HierarchyGraph inputGraph) {
     System.out.println("\nBuild Lattice:" + inputGraph.getName());
-    System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
-    System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
+    // System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
+    // System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
   }
 
 }
@@ -426,14 +587,21 @@ class TripleItem {
   }
 
   public int hashCode() {
-    return higherNode.hashCode() + lowerNodeSet.hashCode() + idx;
+
+    int h = 0;
+    if (higherNode != null) {
+      h = higherNode.hashCode();
+    }
+
+    return h + lowerNodeSet.hashCode() + idx;
   }
 
   public boolean equals(Object obj) {
 
     if (obj instanceof TripleItem) {
       TripleItem in = (TripleItem) obj;
-      if (higherNode.equals(in.higherNode) && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx) {
+      if ((higherNode == null || (higherNode != null && higherNode.equals(in.higherNode)))
+          && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx) {
         return true;
       }
     }