changes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Map;
10 import java.util.Set;
11
12 import IR.ClassDescriptor;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.VarDescriptor;
17
18 public class FlowGraph {
19
20   MethodDescriptor md;
21
22   Set<FlowNode> nodeSet;
23   Set<FlowNode> returnNodeSet;
24   FlowNode thisVarNode;
25
26   // maps the composite representation of field/var descriptors to infer nodes
27   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
28
29   // maps an infer node to the set of neighbors which is pointed by the node
30   Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
31
32   // maps a paramter descriptor to its index
33   Map<Descriptor, Integer> mapParamDescToIdx;
34   boolean debug = true;
35
36   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
37     this.md = md;
38     this.nodeSet = new HashSet<FlowNode>();
39     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
40     this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
41     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
42     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
43     this.returnNodeSet = new HashSet<FlowNode>();
44
45     // create a node for 'this' varialbe
46     NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
47     thisDescTuple.add(md.getThis());
48     FlowNode thisNode = new FlowNode(thisDescTuple, true);
49     NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
50     thisVarTuple.add(md.getThis());
51     createNewFlowNode(thisVarTuple);
52     thisVarNode = thisNode;
53
54   }
55
56   public Set<FlowNode> getNodeSet() {
57     return nodeSet;
58   }
59
60   public MethodDescriptor getMethodDescriptor() {
61     return md;
62   }
63
64   public Set<FlowNode> getParameterNodeSet() {
65     Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
66     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
67       FlowNode fn = (FlowNode) iterator.next();
68       if (fn.isParameter()) {
69         paramNodeSet.add(fn);
70       }
71     }
72     return paramNodeSet;
73   }
74
75   public void addNeighbor(FlowNode node, FlowNode neighbor) {
76     Set<FlowNode> set = mapNodeToNeighborSet.get(node);
77     if (set == null) {
78       set = new HashSet<FlowNode>();
79     }
80     set.add(neighbor);
81
82     System.out.println("add a new neighbor " + neighbor + " to " + node);
83   }
84
85   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
86
87     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
88     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
89
90     Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
91     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
92       FlowEdge flowEdge = (FlowEdge) iterator.next();
93       if (flowEdge.getDst().equals(toNode)) {
94         return true;
95       } else {
96         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
97           return true;
98         }
99       }
100     }
101
102     return false;
103   }
104
105   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
106
107     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
108     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
109
110     System.out.println("create an edge from " + fromNode + " to " + toNode);
111
112     int fromTupleSize = fromDescTuple.size();
113     NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
114     for (int i = 0; i < fromTupleSize; i++) {
115       Descriptor desc = fromDescTuple.get(i);
116       curTuple.add(desc);
117       addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
118     }
119
120     int toTupleSize = toDescTuple.size();
121     curTuple = new NTuple<Descriptor>();
122     for (int i = 0; i < toTupleSize; i++) {
123       Descriptor desc = toDescTuple.get(i);
124       curTuple.add(desc);
125       addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
126     }
127
128   }
129
130   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
131       NTuple<Descriptor> endTuple) {
132
133     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
134     fromNode.addOutEdge(edge);
135
136     System.out.println("add a new edge=" + edge);
137
138   }
139
140   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
141     if (mapDescTupleToInferNode.containsKey(descTuple)) {
142       return mapDescTupleToInferNode.get(descTuple);
143     } else {
144       FlowNode node = createNewFlowNode(descTuple);
145       return node;
146     }
147   }
148
149   public FlowNode getThisVarNode() {
150     return thisVarNode;
151   }
152
153   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
154
155     if (!mapDescTupleToInferNode.containsKey(tuple)) {
156
157       FlowNode node = new FlowNode(tuple, isParamter(tuple));
158       mapDescTupleToInferNode.put(tuple, node);
159       nodeSet.add(node);
160
161       if (tuple.size() > 1) {
162         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
163         getFlowNode(baseTuple).addFieldNode(node);
164       }
165
166       System.out.println("Creating new node=" + node);
167       return node;
168     } else {
169       return mapDescTupleToInferNode.get(tuple);
170     }
171
172   }
173
174   public void setReturnFlowNode(NTuple<Descriptor> tuple) {
175
176     if (!mapDescTupleToInferNode.containsKey(tuple)) {
177       createNewFlowNode(tuple);
178     }
179
180     FlowNode node = mapDescTupleToInferNode.get(tuple);
181     node.setReturn(true);
182
183     returnNodeSet.add(node);
184   }
185
186   public Set<FlowNode> getReturnNodeSet() {
187     return returnNodeSet;
188   }
189
190   public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
191     Set<FlowNode> set = new HashSet<FlowNode>();
192     getReachableFlowNodeSet(fn, set);
193     return set;
194   }
195
196   private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
197
198     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
199       FlowEdge edge = (FlowEdge) iterator.next();
200
201       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
202
203         FlowNode dstNode = getFlowNode(edge.getEndTuple());
204
205         if (!visited.contains(dstNode)) {
206           visited.add(dstNode);
207           getReachableFlowNodeSet(dstNode, visited);
208         }
209       }
210     }
211
212   }
213
214   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
215     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
216       FlowEdge edge = (FlowEdge) iterator.next();
217
218       if (fn.getDescTuple().equals(edge.getInitTuple())) {
219         FlowNode dstNode = getFlowNode(edge.getEndTuple());
220         NTuple<Location> dstTuple = getLocationTuple(dstNode);
221
222         if (!visited.contains(dstTuple)) {
223           visited.add(dstTuple);
224           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
225         }
226
227       }
228     }
229     return visited;
230   }
231
232   public NTuple<Location> getLocationTuple(FlowNode fn) {
233
234     NTuple<Descriptor> descTuple = fn.getDescTuple();
235
236     NTuple<Location> locTuple = new NTuple<Location>();
237
238     ClassDescriptor cd = null;
239
240     for (int i = 0; i < descTuple.size(); i++) {
241       Descriptor d = descTuple.get(i);
242       Location loc;
243       if (i == 0) {
244         loc = new Location(md, d.getSymbol());
245         cd = ((VarDescriptor) d).getType().getClassDesc();
246       } else {
247         loc = new Location(cd, d.getSymbol());
248         cd = ((FieldDescriptor) d).getType().getClassDesc();
249       }
250       locTuple.add(loc);
251     }
252
253     return locTuple;
254   }
255
256   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
257     Set<FlowNode> set = new HashSet<FlowNode>();
258     getIncomingFlowNodeSet(node, set);
259     return set;
260   }
261
262   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
263
264     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
265       FlowNode curNode = (FlowNode) iterator.next();
266       Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
267
268       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
269         FlowEdge flowEdge = (FlowEdge) iterator2.next();
270
271         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
272           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
273
274           if (!visited.contains(incomingNode)) {
275             visited.add(incomingNode);
276             getIncomingFlowNodeSet(incomingNode, visited);
277           }
278         }
279       }
280     }
281
282   }
283
284   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
285
286     NTuple<Descriptor> dstTuple = fn.getDescTuple();
287
288     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
289
290     ClassDescriptor cd = null;
291
292     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
293       FlowNode node = (FlowNode) iterator.next();
294       Set<FlowEdge> edgeSet = node.getOutEdgeSet();
295       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
296         FlowEdge flowEdge = (FlowEdge) iterator2.next();
297         if (dstTuple.equals(flowEdge.getEndTuple())) {
298           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
299           NTuple<Location> locTuple = new NTuple<Location>();
300           for (int i = 0; i < initTuple.size(); i++) {
301             Descriptor d = initTuple.get(i);
302             Location loc;
303             if (i == 0) {
304               loc = new Location(md, d.getSymbol());
305               cd = ((VarDescriptor) d).getType().getClassDesc();
306             } else {
307               loc = new Location(cd, d.getSymbol());
308               cd = ((FieldDescriptor) d).getType().getClassDesc();
309             }
310             locTuple.add(loc);
311           }
312           set.add(locTuple);
313         }
314       }
315     }
316     return set;
317   }
318
319   public boolean isParamter(NTuple<Descriptor> tuple) {
320     // return true if a descriptor tuple is started with a parameter descriptor
321     Descriptor firstIdxDesc = tuple.get(0);
322     return mapParamDescToIdx.containsKey(firstIdxDesc);
323   }
324
325   public int getParamIdx(NTuple<Descriptor> tuple) {
326     Descriptor firstDesc = tuple.get(0);
327     return mapParamDescToIdx.get(firstDesc).intValue();
328   }
329
330   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
331       Set<FlowEdge> addedEdgeSet) throws IOException {
332
333     Set<FlowEdge> edgeSet = node.getOutEdgeSet();
334
335     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
336       FlowEdge flowEdge = iterator.next();
337
338       FlowNode u = flowEdge.getSrc();
339       FlowNode v = flowEdge.getDst();
340
341       if (u.getDescTuple().equals(flowEdge.getInitTuple())
342           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
343         // only draw an edge of the actual value flow
344
345         if (!addedEdgeSet.contains(flowEdge)) {
346
347           if (!addedNodeSet.contains(u)) {
348             drawNode(u, bw);
349             addedNodeSet.add(u);
350           }
351           if (!addedNodeSet.contains(v)) {
352             drawNode(v, bw);
353             addedNodeSet.add(v);
354           }
355
356           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
357           addedEdgeSet.add(flowEdge);
358         }
359       }
360
361     }
362
363   }
364
365   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
366     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
367   }
368
369   public void writeGraph() throws java.io.IOException {
370
371     String graphName = "flowgraph_" + md.toString();
372     graphName = graphName.replaceAll("[\\W]", "");
373
374     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
375     bw.write("digraph " + graphName + " {\n");
376     bw.write("compound=true;\n");
377
378     // then visit every flow node
379
380     Iterator<FlowNode> iter = nodeSet.iterator();
381
382     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
383     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
384
385     while (iter.hasNext()) {
386       FlowNode node = iter.next();
387
388       if (node.getDescTuple().size() == 1) {
389         // here, we just care about the local variable
390         if (node.getFieldNodeSet().size() > 0) {
391           drawSubgraph(node, bw, addedEdgeSet);
392         }
393       }
394       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
395
396     }
397
398     bw.write("}\n");
399     bw.close();
400
401   }
402
403   public boolean constainsNode(FlowNode node) {
404     return nodeSet.contains(node);
405   }
406
407   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
408       throws IOException {
409
410     bw.write("subgraph cluster_" + node.getID() + "{\n");
411     bw.write("label=\"" + node.getPrettyID() + "\";\n");
412
413     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
414     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
415       FlowNode fieldNode = (FlowNode) iterator.next();
416       if (fieldNode.getFieldNodeSet().size() > 0) {
417         drawSubgraph(fieldNode, bw, addedSet);
418       } else {
419         Descriptor desc = fieldNode.getDescTuple().getLastElement();
420         if (desc instanceof VarDescriptor) {
421           VarDescriptor varDesc = (VarDescriptor) desc;
422           if (varDesc.getType().isPrimitive()) {
423             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
424           }
425         } else if (desc instanceof FieldDescriptor) {
426           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
427           if (fieldDesc.getType().isPrimitive()) {
428             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
429           }
430         }
431       }
432     }
433
434     bw.write("}\n");
435   }
436 }