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