bug fixes
[IRC.git] / Robust / src / Analysis / Pointer / Graph.java
1 package Analysis.Pointer;
2 import java.util.*;
3 import Analysis.Disjoint.PointerMethod;
4 import Analysis.Pointer.AllocFactory.AllocNode;
5 import IR.Flat.*;
6 import java.io.PrintWriter;
7
8 public class Graph {
9   /* This is field is set is this Graph is just a delta on the parent
10    * graph. */
11
12   Graph parent;
13   HashMap<AllocNode, MySet<Edge>> nodeMap;
14   HashMap<TempDescriptor, MySet<Edge>> varMap;
15   HashMap<AllocNode, MySet<Edge>> backMap;
16   MySet<Edge> strongUpdateSet;
17
18   /* Need this information for mapping in callee results */
19   MySet<Edge> reachEdge;
20   HashSet<AllocNode> reachNode;
21   MySet<Edge> externalEdgeSet;
22
23   /* Need this information for mapping in callee results */
24   HashSet<AllocNode> nodeAges;
25   HashMap<AllocNode, Boolean> oldNodes;
26
27   /* Need this information for mapping in callee results */
28   HashSet<AllocNode> callNodeAges;
29   HashSet<AllocNode> callOldNodes;
30
31   public Graph(Graph parent) {
32     nodeMap=new HashMap<AllocNode, MySet<Edge>>();
33     varMap=new HashMap<TempDescriptor, MySet<Edge>>();
34     nodeAges=new HashSet<AllocNode>();
35     oldNodes=new HashMap<AllocNode, Boolean>();
36     this.parent=parent;
37   }
38
39   public MySet<Edge> getBackEdges(AllocNode node) {
40     MySet<Edge> edgeset=new MySet<Edge>();
41     edgeset.addAll(backMap.get(node));
42     edgeset.addAll(parent.backMap.get(node));
43     return edgeset;
44   }
45
46   public boolean containsNode(AllocNode node) {
47     return nodeAges.contains(node)||parent!=null&&parent.nodeAges.contains(node);
48   }
49
50   public Edge getMatch(Edge old) {
51     if (old.srcvar!=null) {
52       MySet<Edge> edges=varMap.get(old.srcvar);
53       if (edges==null)
54         edges=parent.varMap.get(old.srcvar);
55       return edges.get(old);
56     } else {
57       MySet<Edge> edges=nodeMap.get(old.src);
58       if (edges==null)
59         edges=parent.nodeMap.get(old.src);
60       return edges.get(old);
61     }
62   }
63   
64   
65
66   public MySet<Edge> getEdges(TempDescriptor tmp) {
67     if (varMap.containsKey(tmp))
68       return varMap.get(tmp);
69     else if (parent!=null&&parent.varMap.containsKey(tmp))
70       return parent.varMap.get(tmp);
71     else return emptySet;
72   }
73
74   public MySet<Edge> getEdges(AllocNode node) {
75     if (nodeMap.containsKey(node))
76       return nodeMap.get(node);
77     else if (parent!=null&&parent.nodeMap.containsKey(node))
78       return parent.nodeMap.get(node);
79     else return emptySet;
80   }
81
82   public static MySet<Edge> emptySet=new MySet<Edge>();
83
84   public void printGraph(PrintWriter output, String name) {
85     output.println("digraph \""+name+"\" {");
86     output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
87     output.println("\tedge [fontsize=6];");
88     outputTempEdges(output, varMap, null);
89     if (parent!=null)
90       outputTempEdges(output, parent.varMap, varMap);
91     outputHeapEdges(output, nodeMap, null);
92     if (parent!=null)
93       outputHeapEdges(output, parent.nodeMap, nodeMap);
94     output.println("}\n");
95   }
96
97   private void outputTempEdges(PrintWriter output, HashMap<TempDescriptor, MySet<Edge>> varMap, 
98                                HashMap<TempDescriptor, MySet<Edge>> childvarMap) {
99     for(Map.Entry<TempDescriptor, MySet<Edge>> entry:varMap.entrySet()) {
100       TempDescriptor tmp=entry.getKey();
101       if (childvarMap!=null&&childvarMap.containsKey(tmp))
102         continue;
103       for(Edge e:entry.getValue()) {
104         AllocNode n=e.dst;
105         output.println("\t"+tmp.getSymbol()+"->"+n.getID()+";");
106       }
107     }
108   }
109
110   private void outputHeapEdges(PrintWriter output, HashMap<AllocNode, MySet<Edge>> nodeMap, 
111                                HashMap<AllocNode, MySet<Edge>> childNodeMap) {
112     for(Map.Entry<AllocNode, MySet<Edge>> entry:nodeMap.entrySet()) {
113       AllocNode node=entry.getKey();
114       if (childNodeMap!=null&&childNodeMap.containsKey(node))
115         continue;
116       for(Edge e:entry.getValue()) {
117         AllocNode n=e.dst;
118         String src=node.getID();
119         String dst=n.getID();
120         String field=e.fd!=null?e.fd.getSymbol():"[]";
121         output.println("\t"+src+"->"+dst+"[label=\""+field+"\"];");
122       }
123     }
124   }
125 }