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