e81fe7d073b93b29fe094b4f6313b353ec9d724f
[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> returnNodeSet;
23   FlowNode thisVarNode;
24
25   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
26
27   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
28   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
29
30   // maps the composite representation of field/var descriptors to infer nodes
31   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
32
33   // maps a paramter descriptor to its index
34   Map<Descriptor, Integer> mapParamDescToIdx;
35
36   // DS for the lattice generation
37   Map<Integer, FlowNode> mapIdxToFlowNode;
38
39   public static int interseed = 0;
40
41   boolean debug = true;
42
43   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
44     this.md = md;
45     this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
46     this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
47     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
48     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
49     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
50     this.returnNodeSet = new HashSet<FlowNode>();
51     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
52     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
53
54     if (!md.isStatic()) {
55       // create a node for 'this' varialbe
56       // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
57       // thisDescTuple.add(md.getThis());
58
59       NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
60       thisVarTuple.add(md.getThis());
61       FlowNode thisNode = createNewFlowNode(thisVarTuple);
62       thisNode.setSkeleton(true);
63       thisVarNode = thisNode;
64     }
65
66     setupMapIdxToDesc();
67
68   }
69
70   public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
71     return mapDescTupleToInferNode;
72   }
73
74   public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
75     this.mapDescTupleToInferNode.putAll(in);
76   }
77
78   public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
79     return mapLocTupleToFlowNode;
80   }
81
82   public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
83     this.mapLocTupleToFlowNode.putAll(in);
84   }
85
86   public void setReturnNodeSet(Set<FlowNode> in) {
87     this.returnNodeSet.addAll(in);
88   }
89
90   public void setThisVarNode(FlowNode thisVarNode) {
91     this.thisVarNode = thisVarNode;
92   }
93
94   public Map<Descriptor, Integer> getMapParamDescToIdx() {
95     return mapParamDescToIdx;
96   }
97
98   public FlowNode createIntermediateNode(MethodDescriptor md) {
99
100     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
101     Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
102     tuple.add(interDesc);
103     interseed++;
104
105     NTuple<Location> locTuple = new NTuple<Location>();
106     Location interLoc = new Location(md, interDesc);
107     locTuple.add(interLoc);
108
109     FlowNode newNode = new FlowNode(locTuple);
110     newNode.setIntermediate(true);
111
112     mapDescTupleToInferNode.put(tuple, newNode);
113     // nodeSet.add(newNode);
114
115     System.out.println("create new intermediate node= " + newNode);
116
117     return newNode;
118   }
119
120   private void setupMapIdxToDesc() {
121
122     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
123     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
124       Descriptor paramDesc = (Descriptor) iterator.next();
125       int idx = mapParamDescToIdx.get(paramDesc);
126       NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
127       descTuple.add(paramDesc);
128       FlowNode paramNode = getFlowNode(descTuple);
129       mapIdxToFlowNode.put(idx, paramNode);
130       paramNode.setSkeleton(true);
131     }
132
133   }
134
135   public int getNumParameters() {
136     return mapIdxToFlowNode.keySet().size();
137   }
138
139   public FlowNode getParamFlowNode(int idx) {
140     return mapIdxToFlowNode.get(idx);
141   }
142
143   public Set<FlowNode> getNodeSet() {
144     Set<FlowNode> set = new HashSet<FlowNode>();
145     set.addAll(mapDescTupleToInferNode.values());
146     return set;
147   }
148
149   public MethodDescriptor getMethodDescriptor() {
150     return md;
151   }
152
153   public boolean isParamDesc(Descriptor desc) {
154
155     if (mapParamDescToIdx.containsKey(desc)) {
156       int idx = mapParamDescToIdx.get(desc);
157       if (!md.isStatic() && idx == 0) {
158         return false;
159       }
160       return true;
161     }
162
163     return false;
164   }
165
166   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
167
168     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
169     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
170
171     Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
172     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
173       FlowEdge flowEdge = (FlowEdge) iterator.next();
174       if (flowEdge.getDst().equals(toNode)) {
175         return true;
176       } else {
177         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
178           return true;
179         }
180       }
181     }
182
183     return false;
184   }
185
186   public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
187
188     Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
189
190     // returns the set of edges that share the same prefix of startNode
191     Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
192
193     for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
194       Set<FlowEdge> nodeEdgeSet = iter.next();
195       for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
196         FlowEdge edge = iter2.next();
197         if (edge.getInitTuple().get(0).equals(prefixDesc)) {
198           edgeSet.add(edge);
199         }
200       }
201     }
202
203     return edgeSet;
204   }
205
206   public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
207     if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
208       mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
209     }
210     return mapFlowNodeToOutEdgeSet.get(node);
211   }
212
213   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
214
215     FlowNode fromNode = getFlowNode(fromDescTuple);
216     FlowNode toNode = getFlowNode(toDescTuple);
217
218     // System.out.println("create an edge from " + fromNode + " to " + toNode);
219
220     int fromTupleSize = fromDescTuple.size();
221     NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
222     for (int i = 0; i < fromTupleSize; i++) {
223       Descriptor desc = fromDescTuple.get(i);
224       curFromTuple.add(desc);
225       int toTupleSize = toDescTuple.size();
226       NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
227       for (int k = 0; k < toTupleSize; k++) {
228         Descriptor toDesc = toDescTuple.get(k);
229         curToTuple.add(toDesc);
230         addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
231       }
232     }
233
234     // int fromTupleSize = fromDescTuple.size();
235     // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
236     // for (int i = 0; i < fromTupleSize; i++) {
237     // Descriptor desc = fromDescTuple.get(i);
238     // curTuple.add(desc);
239     // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
240     // }
241     //
242     // int toTupleSize = toDescTuple.size();
243     // curTuple = new NTuple<Descriptor>();
244     // for (int i = 0; i < toTupleSize; i++) {
245     // Descriptor desc = toDescTuple.get(i);
246     // curTuple.add(desc);
247     // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
248     // }
249
250   }
251
252   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
253       NTuple<Descriptor> endTuple) {
254
255     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
256     addOutEdge(fromNode, edge);
257
258     // System.out.println("add a new edge=" + edge);
259   }
260
261   private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
262     if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
263       mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
264     }
265     mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
266   }
267
268   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
269     if (!mapDescTupleToInferNode.containsKey(descTuple)) {
270       FlowNode node = createNewFlowNode(descTuple);
271       mapDescTupleToInferNode.put(descTuple, node);
272     }
273     return mapDescTupleToInferNode.get(descTuple);
274   }
275
276   public FlowNode getThisVarNode() {
277     return thisVarNode;
278   }
279
280   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
281
282     NTuple<Location> locTuple = translateToLocationTuple(tuple);
283     if (!mapDescTupleToInferNode.containsKey(tuple)) {
284
285       FlowNode node = new FlowNode(locTuple);
286       mapDescTupleToInferNode.put(tuple, node);
287       // nodeSet.add(node);
288
289       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
290
291       if (tuple.size() > 1) {
292         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
293         getFlowNode(baseTuple).addFieldNode(node);
294       }
295
296       // System.out.println("Creating new node=" + node);
297       return node;
298     } else {
299       return mapDescTupleToInferNode.get(tuple);
300     }
301
302   }
303
304   public void addReturnFlowNode(NTuple<Descriptor> tuple) {
305
306     if (!mapDescTupleToInferNode.containsKey(tuple)) {
307       createNewFlowNode(tuple);
308     }
309
310     FlowNode node = mapDescTupleToInferNode.get(tuple);
311     node.setReturn(true);
312
313     returnNodeSet.add(node);
314   }
315
316   public Set<FlowNode> getReturnNodeSet() {
317     return returnNodeSet;
318   }
319
320   public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
321     Set<FlowNode> set = new HashSet<FlowNode>();
322     recurLocalReachFlowNodeSet(fn, set);
323     return set;
324   }
325
326   private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
327
328     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
329     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
330       FlowEdge edge = (FlowEdge) iterator.next();
331       FlowNode dstNode = edge.getDst();
332
333       if (!visited.contains(dstNode)) {
334         visited.add(dstNode);
335         recurLocalReachFlowNodeSet(dstNode, visited);
336       }
337     }
338
339   }
340
341   private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
342
343     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
344     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
345       FlowEdge edge = (FlowEdge) iterator.next();
346
347       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
348         FlowNode dstNode = getFlowNode(edge.getEndTuple());
349         if (!visited.contains(dstNode)) {
350           visited.add(dstNode);
351           getReachFlowNodeSetFrom(dstNode, visited);
352         }
353       }
354     }
355
356   }
357
358   public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
359     Set<FlowNode> set = new HashSet<FlowNode>();
360     getReachFlowNodeSetFrom(fn, set);
361     return set;
362   }
363
364   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
365   //
366   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
367   // iterator.hasNext();) {
368   // FlowEdge edge = (FlowEdge) iterator.next();
369   //
370   // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
371   //
372   // FlowNode dstNode = getFlowNode(edge.getEndTuple());
373   //
374   // if (!visited.contains(dstNode)) {
375   // visited.add(dstNode);
376   // getReachFlowNodeSetFrom(dstNode, visited);
377   // }
378   // }
379   // }
380   //
381   // }
382
383   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
384
385     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
386
387     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
388       FlowEdge edge = (FlowEdge) iterator.next();
389
390       if (fn.getLocTuple().equals(edge.getInitTuple())) {
391         FlowNode dstNode = getFlowNode(edge.getEndTuple());
392         NTuple<Location> dstTuple = getLocationTuple(dstNode);
393
394         if (!visited.contains(dstTuple)) {
395           visited.add(dstTuple);
396           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
397         }
398
399       }
400     }
401     return visited;
402   }
403
404   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
405     return getLocationTuple(getFlowNode(descTuple));
406   }
407
408   public NTuple<Location> translateToLocationTuple(NTuple<Descriptor> descTuple) {
409
410     NTuple<Location> locTuple = new NTuple<Location>();
411     ClassDescriptor cd = null;
412
413     Descriptor localDesc = descTuple.get(0);
414
415     if (localDesc instanceof InterDescriptor) {
416       Location interLoc = new Location(md, localDesc);
417       locTuple.add(interLoc);
418     } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
419       Location topLoc = new Location(md, Location.TOP);
420       topLoc.setLocDescriptor(LocationInference.TOPDESC);
421       locTuple.add(topLoc);
422     } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
423       Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
424       globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
425       locTuple.add(globalLoc);
426     } else {
427       // normal case
428       for (int i = 0; i < descTuple.size(); i++) {
429         Descriptor curDesc = descTuple.get(i);
430         Location loc;
431         if (i == 0) {
432           loc = new Location(md, curDesc.getSymbol());
433           loc.setLocDescriptor(curDesc);
434           cd = ((VarDescriptor) curDesc).getType().getClassDesc();
435         } else {
436           loc = new Location(cd, curDesc.getSymbol());
437           loc.setLocDescriptor(curDesc);
438
439           if (curDesc instanceof FieldDescriptor) {
440             cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
441           } else {
442             cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
443           }
444
445         }
446         locTuple.add(loc);
447       }
448     }
449
450     return locTuple;
451   }
452
453   public NTuple<Location> getLocationTuple(FlowNode fn) {
454
455     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
456       NTuple<Descriptor> descTuple = fn.getDescTuple();
457       NTuple<Location> locTuple = new NTuple<Location>();
458       ClassDescriptor cd = null;
459
460       Descriptor localDesc = fn.getDescTuple().get(0);
461
462       if (fn.isIntermediate()) {
463         Location interLoc = new Location(md, localDesc.getSymbol());
464         interLoc.setLocDescriptor(localDesc);
465         locTuple.add(interLoc);
466       } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
467         Location topLoc = new Location(md, Location.TOP);
468         topLoc.setLocDescriptor(LocationInference.TOPDESC);
469         locTuple.add(topLoc);
470       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
471         Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
472         globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
473         locTuple.add(globalLoc);
474       } else {
475         // normal case
476         for (int i = 0; i < descTuple.size(); i++) {
477           Descriptor curDesc = descTuple.get(i);
478           Location loc;
479           if (i == 0) {
480             loc = new Location(md, curDesc.getSymbol());
481             loc.setLocDescriptor(curDesc);
482             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
483           } else {
484             loc = new Location(cd, curDesc.getSymbol());
485             loc.setLocDescriptor(curDesc);
486
487             if (curDesc instanceof FieldDescriptor) {
488               cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
489             } else {
490               cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
491             }
492
493           }
494           locTuple.add(loc);
495         }
496       }
497
498       mapFlowNodeToLocTuple.put(fn, locTuple);
499     }
500     return mapFlowNodeToLocTuple.get(fn);
501   }
502
503   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
504     Set<FlowNode> set = new HashSet<FlowNode>();
505     getIncomingFlowNodeSet(node, set);
506     return set;
507   }
508
509   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
510
511     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
512       FlowNode curNode = (FlowNode) iterator.next();
513       Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
514
515       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
516         FlowEdge flowEdge = (FlowEdge) iterator2.next();
517
518         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
519           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
520
521           if (!visited.contains(incomingNode)) {
522             visited.add(incomingNode);
523             getIncomingFlowNodeSet(incomingNode, visited);
524           }
525         }
526       }
527     }
528
529   }
530
531   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
532
533     NTuple<Descriptor> dstTuple = fn.getDescTuple();
534
535     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
536
537     ClassDescriptor cd = null;
538
539     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
540       FlowNode node = (FlowNode) iterator.next();
541
542       Set<FlowEdge> edgeSet = getOutEdgeSet(node);
543       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
544         FlowEdge flowEdge = (FlowEdge) iterator2.next();
545         if (dstTuple.equals(flowEdge.getEndTuple())) {
546           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
547           NTuple<Location> locTuple = new NTuple<Location>();
548           for (int i = 0; i < initTuple.size(); i++) {
549             Descriptor d = initTuple.get(i);
550             Location loc;
551             if (i == 0) {
552               loc = new Location(md, d.getSymbol());
553               cd = ((VarDescriptor) d).getType().getClassDesc();
554             } else {
555               loc = new Location(cd, d.getSymbol());
556               cd = ((FieldDescriptor) d).getType().getClassDesc();
557             }
558             locTuple.add(loc);
559           }
560           set.add(locTuple);
561         }
562       }
563     }
564     return set;
565   }
566
567   public boolean isParameter(NTuple<Descriptor> tuple) {
568     // return true if a descriptor tuple is started with a parameter descriptor
569     Descriptor firstIdxDesc = tuple.get(0);
570     return mapParamDescToIdx.containsKey(firstIdxDesc);
571   }
572
573   public int getParamIdx(NTuple<Descriptor> tuple) {
574     Descriptor firstIdxDesc = tuple.get(0);
575     return mapParamDescToIdx.get(firstIdxDesc);
576   }
577
578   public FlowGraph clone() {
579     FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
580
581     // clone.setNodeSet(getNodeSet());
582     clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
583     clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
584     clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
585
586     clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
587     clone.setReturnNodeSet(getReturnNodeSet());
588     clone.setThisVarNode(getThisVarNode());
589
590     return clone;
591   }
592
593   public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
594     return mapFlowNodeToLocTuple;
595   }
596
597   public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
598     this.mapFlowNodeToLocTuple.putAll(in);
599   }
600
601   public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
602     return mapFlowNodeToOutEdgeSet;
603   }
604
605   public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
606     Set<FlowNode> keySet = inMap.keySet();
607     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
608       FlowNode key = (FlowNode) iterator.next();
609       Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
610       newEdgeSet.addAll(inMap.get(key));
611       mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
612     }
613   }
614
615   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
616       Set<FlowEdge> addedEdgeSet) throws IOException {
617
618     Set<FlowEdge> edgeSet = getOutEdgeSet(node);
619
620     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
621       FlowEdge flowEdge = iterator.next();
622
623       FlowNode u = flowEdge.getSrc();
624       FlowNode v = flowEdge.getDst();
625
626       if (u.getLocTuple().equals(flowEdge.getInitTuple())
627           && v.getLocTuple().equals(flowEdge.getEndTuple())) {
628         // only draw an edge of the actual value flow
629
630         if (!addedEdgeSet.contains(flowEdge)) {
631
632           if (!addedNodeSet.contains(u)) {
633             drawNode(u, bw);
634             addedNodeSet.add(u);
635           }
636           if (!addedNodeSet.contains(v)) {
637             drawNode(v, bw);
638             addedNodeSet.add(v);
639           }
640
641           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
642           addedEdgeSet.add(flowEdge);
643         }
644       }
645
646     }
647
648   }
649
650   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
651     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
652   }
653
654   public void writeGraph() throws java.io.IOException {
655     writeGraph("");
656   }
657
658   public void writeGraph(String suffix) throws java.io.IOException {
659
660     String graphName = "flowgraph_" + md.toString() + suffix;
661     graphName = graphName.replaceAll("[\\W]", "");
662
663     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
664     bw.write("digraph " + graphName + " {\n");
665     bw.write("compound=true;\n");
666
667     // then visit every flow node
668
669     // Iterator<FlowNode> iter = nodeSet.iterator();
670     Iterator<FlowNode> iter = getNodeSet().iterator();
671
672     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
673     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
674
675     while (iter.hasNext()) {
676       FlowNode node = iter.next();
677
678       if (node.getLocTuple().size() == 1) {
679         // here, we just care about the local variable
680         if (node.getFieldNodeSet().size() > 0) {
681           drawSubgraph(node, bw, addedEdgeSet);
682         }
683       }
684       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
685
686     }
687
688     bw.write("}\n");
689     bw.close();
690
691   }
692
693   public boolean constainsNode(FlowNode node) {
694     return getNodeSet().contains(node);
695   }
696
697   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
698       throws IOException {
699
700     bw.write("subgraph cluster_" + node.getID() + "{\n");
701     bw.write("label=\"" + node.getPrettyID() + "\";\n");
702
703     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
704     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
705       FlowNode fieldNode = (FlowNode) iterator.next();
706       if (fieldNode.getFieldNodeSet().size() > 0) {
707         drawSubgraph(fieldNode, bw, addedSet);
708       } else {
709         Descriptor desc = fieldNode.getDescTuple().getLastElement();
710         if (desc instanceof VarDescriptor) {
711           VarDescriptor varDesc = (VarDescriptor) desc;
712           if (varDesc.getType().isPrimitive()) {
713             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
714           }
715         } else if (desc instanceof FieldDescriptor) {
716           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
717           if (fieldDesc.getType().isPrimitive()) {
718             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
719           }
720         }
721       }
722     }
723
724     bw.write("}\n");
725   }
726 }