1 package Analysis.SSJava;
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;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
17 import IR.VarDescriptor;
18 import IR.Tree.MethodInvokeNode;
20 public class FlowGraph {
24 Set<FlowNode> returnNodeSet;
27 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
28 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
30 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
31 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
33 // maps the composite representation of field/var descriptors to infer nodes
34 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
36 // maps a paramter descriptor to its index
37 Map<Descriptor, Integer> mapParamDescToIdx;
39 // DS for the lattice generation
40 Map<Integer, FlowNode> mapIdxToFlowNode;
42 Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
44 Map<Descriptor, ClassDescriptor> mapIntersectionDescToEnclosingDescriptor;
46 public static int interseed = 0;
50 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
52 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
53 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
54 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
55 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
56 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
57 this.returnNodeSet = new HashSet<FlowNode>();
58 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
59 this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
60 this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
61 this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
62 this.mapIntersectionDescToEnclosingDescriptor = new HashMap<Descriptor, ClassDescriptor>();
65 // create a node for 'this' varialbe
66 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
67 // thisDescTuple.add(md.getThis());
69 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
70 thisVarTuple.add(md.getThis());
71 FlowNode thisNode = createNewFlowNode(thisVarTuple);
72 thisNode.setSkeleton(true);
73 thisVarNode = thisNode;
80 public void addMapInterLocNodeToEnclosingDescriptor(Descriptor interDesc, ClassDescriptor desc) {
81 System.out.println("##### INTERLOC=" + interDesc + " enclosing desc=" + desc);
82 mapIntersectionDescToEnclosingDescriptor.put(interDesc, desc);
85 public ClassDescriptor getEnclosingDescriptor(Descriptor interDesc) {
86 return mapIntersectionDescToEnclosingDescriptor.get(interDesc);
89 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
90 return mapDescTupleToInferNode;
93 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
94 this.mapDescTupleToInferNode.putAll(in);
97 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
98 return mapLocTupleToFlowNode;
101 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
102 this.mapLocTupleToFlowNode.putAll(in);
105 public void setReturnNodeSet(Set<FlowNode> in) {
106 this.returnNodeSet.addAll(in);
109 public void setThisVarNode(FlowNode thisVarNode) {
110 this.thisVarNode = thisVarNode;
113 public Map<Descriptor, Integer> getMapParamDescToIdx() {
114 return mapParamDescToIdx;
117 public FlowNode createIntermediateNode() {
118 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
119 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
120 tuple.add(interDesc);
123 FlowNode newNode = new FlowNode(tuple);
124 newNode.setIntermediate(true);
126 mapDescTupleToInferNode.put(tuple, newNode);
127 // nodeSet.add(newNode);
129 System.out.println("create new intermediate node= " + newNode);
134 public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
135 return mapMethodInvokeNodeToFlowReturnNode.get(min);
138 public FlowReturnNode createReturnNode(MethodInvokeNode min) {
139 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
140 NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
143 FlowReturnNode newNode = new FlowReturnNode(tuple, min);
144 mapDescTupleToInferNode.put(tuple, newNode);
145 mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
146 // nodeSet.add(newNode);
148 System.out.println("create new set node= " + newNode);
153 private void setupMapIdxToDesc() {
155 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
156 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
157 Descriptor paramDesc = (Descriptor) iterator.next();
158 int idx = mapParamDescToIdx.get(paramDesc);
159 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
160 descTuple.add(paramDesc);
161 FlowNode paramNode = getFlowNode(descTuple);
162 mapIdxToFlowNode.put(idx, paramNode);
163 paramNode.setSkeleton(true);
168 public int getNumParameters() {
169 return mapIdxToFlowNode.keySet().size();
172 public FlowNode getParamFlowNode(int idx) {
173 return mapIdxToFlowNode.get(idx);
176 public Set<FlowEdge> getEdgeSet() {
177 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
179 Set<FlowNode> nodeSet = getNodeSet();
180 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
181 FlowNode flowNode = (FlowNode) iterator.next();
182 edgeSet.addAll(getOutEdgeSet(flowNode));
184 System.out.println("EDGESET=" + edgeSet);
188 public Set<FlowNode> getParamFlowNodeSet() {
189 Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
190 setParamFlowNode.addAll(mapIdxToFlowNode.values());
191 return setParamFlowNode;
194 public Set<FlowNode> getNodeSet() {
195 Set<FlowNode> set = new HashSet<FlowNode>();
196 set.addAll(mapDescTupleToInferNode.values());
197 System.out.println("NODESET=" + set);
201 public MethodDescriptor getMethodDescriptor() {
205 public boolean isParamDesc(Descriptor desc) {
207 if (mapParamDescToIdx.containsKey(desc)) {
208 int idx = mapParamDescToIdx.get(desc);
209 if (!md.isStatic() && idx == 0) {
218 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
220 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
221 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
223 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
224 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
225 FlowEdge flowEdge = (FlowEdge) iterator.next();
226 if (flowEdge.getDst().equals(toNode)) {
229 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
238 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
240 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
242 // returns the set of edges that share the same prefix of startNode
243 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
245 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
246 Set<FlowEdge> nodeEdgeSet = iter.next();
247 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
248 FlowEdge edge = iter2.next();
249 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
258 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
259 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
260 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
262 return mapFlowNodeToOutEdgeSet.get(node);
265 public Set<FlowEdge> getInEdgeSet(FlowNode node) {
266 if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
267 mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
269 return mapFlowNodeToInEdgeSet.get(node);
272 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
274 FlowNode fromNode = getFlowNode(fromDescTuple);
275 FlowNode toNode = getFlowNode(toDescTuple);
277 if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
281 // System.out.println("create an edge from " + fromNode + " to " + toNode);
283 int fromTupleSize = fromDescTuple.size();
284 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
285 for (int i = 0; i < fromTupleSize; i++) {
286 Descriptor desc = fromDescTuple.get(i);
287 curFromTuple.add(desc);
288 int toTupleSize = toDescTuple.size();
289 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
290 for (int k = 0; k < toTupleSize; k++) {
291 Descriptor toDesc = toDescTuple.get(k);
292 curToTuple.add(toDesc);
293 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
297 // int fromTupleSize = fromDescTuple.size();
298 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
299 // for (int i = 0; i < fromTupleSize; i++) {
300 // Descriptor desc = fromDescTuple.get(i);
301 // curTuple.add(desc);
302 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
305 // int toTupleSize = toDescTuple.size();
306 // curTuple = new NTuple<Descriptor>();
307 // for (int i = 0; i < toTupleSize; i++) {
308 // Descriptor desc = toDescTuple.get(i);
309 // curTuple.add(desc);
310 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
315 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
316 NTuple<Descriptor> endTuple) {
318 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
319 addOutEdge(fromNode, edge);
320 addInEdge(toNode, edge);
322 // System.out.println("add a new edge=" + edge);
325 private void addInEdge(FlowNode toNode, FlowEdge edge) {
326 getInEdgeSet(toNode).add(edge);
329 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
330 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
331 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
333 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
336 public boolean contains(NTuple<Descriptor> descTuple) {
337 return mapDescTupleToInferNode.containsKey(descTuple);
340 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
341 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
342 FlowNode node = createNewFlowNode(descTuple);
343 mapDescTupleToInferNode.put(descTuple, node);
345 return mapDescTupleToInferNode.get(descTuple);
348 public FlowNode getThisVarNode() {
352 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
354 // System.out.println("createNewFlowNode=" + tuple);
355 if (!mapDescTupleToInferNode.containsKey(tuple)) {
356 FlowNode node = new FlowNode(tuple);
357 mapDescTupleToInferNode.put(tuple, node);
358 // nodeSet.add(node);
360 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
362 if (tuple.size() > 1) {
363 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
364 getFlowNode(baseTuple).addFieldNode(node);
367 // System.out.println("Creating new node=" + node);
370 return mapDescTupleToInferNode.get(tuple);
375 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
377 if (!mapDescTupleToInferNode.containsKey(tuple)) {
378 createNewFlowNode(tuple);
381 FlowNode node = mapDescTupleToInferNode.get(tuple);
382 returnNodeSet.add(node);
385 public Set<FlowNode> getReturnNodeSet() {
386 return returnNodeSet;
389 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
390 Set<FlowNode> set = new HashSet<FlowNode>();
391 recurLocalReachFlowNodeSet(fn, set);
395 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
397 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
398 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
399 FlowEdge edge = (FlowEdge) iterator.next();
400 FlowNode originalDstNode = edge.getDst();
402 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
403 if (originalDstNode instanceof FlowReturnNode) {
404 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
405 Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
406 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
407 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
408 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
409 dstNodeSet.add(getFlowNode(rtuple));
412 dstNodeSet.add(originalDstNode);
415 for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
416 FlowNode dstNode = (FlowNode) iterator2.next();
417 if (!visited.contains(dstNode)) {
418 visited.add(dstNode);
419 recurLocalReachFlowNodeSet(dstNode, visited);
427 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
429 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
430 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
431 FlowEdge edge = (FlowEdge) iterator.next();
433 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
434 FlowNode dstNode = getFlowNode(edge.getEndTuple());
435 if (!visited.contains(dstNode)) {
436 visited.add(dstNode);
437 getReachFlowNodeSetFrom(dstNode, visited);
444 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
445 Set<FlowNode> set = new HashSet<FlowNode>();
446 getReachFlowNodeSetFrom(fn, set);
450 public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
451 Set<FlowNode> reachableSet = new HashSet<FlowNode>();
453 Set<FlowNode> nodeSet = getNodeSet();
454 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
455 FlowNode originalSrcNode = (FlowNode) iterator.next();
457 Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
458 if (originalSrcNode instanceof FlowReturnNode) {
459 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
460 Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
461 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
462 // System.out.println("#rnode=" + rnode + " rtupleSet=" + rtupleSet);
463 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
464 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
465 if (rtuple.startsWith(prefix)) {
466 // System.out.println("rtuple=" + rtuple + " give it to recur=" + originalSrcNode);
467 recurReachableSetFrom(originalSrcNode, reachableSet);
471 if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
472 recurReachableSetFrom(originalSrcNode, reachableSet);
481 public Set<NTuple<Descriptor>> getReturnTupleSet(Set<NTuple<Descriptor>> in) {
483 Set<NTuple<Descriptor>> normalTupleSet = new HashSet<NTuple<Descriptor>>();
484 for (Iterator iterator2 = in.iterator(); iterator2.hasNext();) {
485 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator2.next();
486 FlowNode tupleNode = getFlowNode(tuple);
487 if (tupleNode instanceof FlowReturnNode) {
488 normalTupleSet.addAll(getReturnTupleSet(((FlowReturnNode) tupleNode).getReturnTupleSet()));
490 normalTupleSet.add(tuple);
493 return normalTupleSet;
496 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
498 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
499 // iterator.hasNext();) {
500 // FlowEdge edge = (FlowEdge) iterator.next();
502 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
504 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
506 // if (!visited.contains(dstNode)) {
507 // visited.add(dstNode);
508 // getReachFlowNodeSetFrom(dstNode, visited);
515 private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
517 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
518 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
519 FlowEdge edge = (FlowEdge) iterator.next();
520 FlowNode dstNode = getFlowNode(edge.getEndTuple());
521 if (!reachableSet.contains(dstNode)) {
522 reachableSet.add(dstNode);
523 recurReachableSetFrom(dstNode, reachableSet);
529 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
531 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
533 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
534 FlowEdge edge = (FlowEdge) iterator.next();
536 if (fn.getDescTuple().equals(edge.getInitTuple())) {
537 FlowNode dstNode = getFlowNode(edge.getEndTuple());
538 NTuple<Location> dstTuple = getLocationTuple(dstNode);
540 if (!visited.contains(dstTuple)) {
541 visited.add(dstTuple);
542 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
550 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
551 return getLocationTuple(getFlowNode(descTuple));
554 public NTuple<Location> getLocationTuple(FlowNode fn) {
556 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
557 NTuple<Descriptor> descTuple = fn.getDescTuple();
558 NTuple<Location> locTuple = new NTuple<Location>();
559 ClassDescriptor cd = null;
561 Descriptor localDesc = fn.getDescTuple().get(0);
563 if (fn.isIntermediate() && fn.getDescTuple().size() == 1) {
564 Location interLoc = new Location(md, localDesc.getSymbol());
565 interLoc.setLocDescriptor(localDesc);
566 locTuple.add(interLoc);
567 } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
568 Location topLoc = new Location(md, Location.TOP);
569 topLoc.setLocDescriptor(LocationInference.TOPDESC);
570 locTuple.add(topLoc);
571 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
572 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
573 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
574 locTuple.add(globalLoc);
577 for (int i = 0; i < descTuple.size(); i++) {
578 Descriptor curDesc = descTuple.get(i);
581 loc = new Location(md, curDesc.getSymbol());
582 loc.setLocDescriptor(curDesc);
583 if (curDesc instanceof VarDescriptor) {
584 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
585 } else if (curDesc instanceof InterDescriptor) {
586 cd = mapIntersectionDescToEnclosingDescriptor.get(curDesc);
588 // otherwise it should be the last element
592 loc = new Location(cd, curDesc.getSymbol());
593 loc.setLocDescriptor(curDesc);
595 if (curDesc instanceof FieldDescriptor) {
596 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
597 } else if (curDesc instanceof LocationDescriptor) {
598 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
600 // otherwise it should be the last element of the tuple
609 mapFlowNodeToLocTuple.put(fn, locTuple);
611 return mapFlowNodeToLocTuple.get(fn);
614 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
615 Set<FlowNode> set = new HashSet<FlowNode>();
616 getIncomingFlowNodeSet(node, set);
620 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
622 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
623 FlowNode curNode = (FlowNode) iterator.next();
624 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
626 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
627 FlowEdge flowEdge = (FlowEdge) iterator2.next();
629 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
630 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
632 if (incomingNode instanceof FlowReturnNode) {
633 FlowReturnNode rnode = (FlowReturnNode) incomingNode;
634 Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
635 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(nodeTupleSet);
636 for (Iterator iterator3 = rtupleSet.iterator(); iterator3.hasNext();) {
637 NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
638 FlowNode fn = getFlowNode(nodeTuple);
639 if (!visited.contains(fn)) {
641 getIncomingFlowNodeSet(fn, visited);
645 if (!visited.contains(incomingNode)) {
646 visited.add(incomingNode);
647 getIncomingFlowNodeSet(incomingNode, visited);
657 public boolean isParameter(NTuple<Descriptor> tuple) {
658 // return true if a descriptor tuple is started with a parameter descriptor
659 Descriptor firstIdxDesc = tuple.get(0);
660 return mapParamDescToIdx.containsKey(firstIdxDesc);
663 public int getParamIdx(NTuple<Descriptor> tuple) {
664 Descriptor firstIdxDesc = tuple.get(0);
665 return mapParamDescToIdx.get(firstIdxDesc);
668 public FlowGraph clone() {
669 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
671 // clone.setNodeSet(getNodeSet());
672 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
673 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
674 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
676 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
677 clone.setReturnNodeSet(getReturnNodeSet());
678 clone.setThisVarNode(getThisVarNode());
683 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
684 return mapFlowNodeToLocTuple;
687 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
688 this.mapFlowNodeToLocTuple.putAll(in);
691 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
692 return mapFlowNodeToOutEdgeSet;
695 public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
697 Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
699 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
700 FlowNode curNode = (FlowNode) iterator.next();
701 Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
703 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
704 FlowEdge outEdge = (FlowEdge) iterator2.next();
706 if (outEdge.getEndTuple().startsWith(prefix)) {
707 incomingNodeSet.add(curNode);
708 recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
715 return incomingNodeSet;
719 private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
721 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
723 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
724 FlowEdge inEdge = (FlowEdge) iterator.next();
726 FlowNode inNode = getFlowNode(inEdge.getInitTuple());
727 if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
729 recurIncomingNodeSetByPrefix(prefix, inNode, visited);
735 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
736 Set<FlowNode> keySet = inMap.keySet();
737 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
738 FlowNode key = (FlowNode) iterator.next();
739 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
740 newEdgeSet.addAll(inMap.get(key));
741 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
745 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
746 Set<FlowEdge> addedEdgeSet) throws IOException {
748 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
750 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
751 FlowEdge flowEdge = iterator.next();
753 FlowNode u = flowEdge.getSrc();
754 FlowNode v = flowEdge.getDst();
756 if (u.getDescTuple().equals(flowEdge.getInitTuple())
757 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
758 // only draw an edge of the actual value flow
760 if (!addedEdgeSet.contains(flowEdge)) {
762 if (!addedNodeSet.contains(u)) {
766 if (!addedNodeSet.contains(v)) {
771 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
772 addedEdgeSet.add(flowEdge);
780 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
781 if (node instanceof FlowReturnNode) {
782 FlowReturnNode rnode = (FlowReturnNode) node;
783 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
785 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
790 public void writeGraph() throws java.io.IOException {
794 public void writeGraph(String suffix) throws java.io.IOException {
796 String graphName = "flowgraph_" + md.toString() + suffix;
797 graphName = graphName.replaceAll("[\\W]", "");
799 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
800 bw.write("digraph " + graphName + " {\n");
801 bw.write("compound=true;\n");
803 // then visit every flow node
805 // Iterator<FlowNode> iter = nodeSet.iterator();
806 Iterator<FlowNode> iter = getNodeSet().iterator();
808 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
809 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
811 while (iter.hasNext()) {
812 FlowNode node = iter.next();
814 if (node.getDescTuple().size() == 1) {
815 // here, we just care about the local variable
816 if (node.getFieldNodeSet().size() > 0) {
817 drawSubgraph(node, bw, addedEdgeSet);
820 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
829 public boolean constainsNode(FlowNode node) {
830 return getNodeSet().contains(node);
833 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
836 bw.write("subgraph cluster_" + node.getID() + "{\n");
837 bw.write("label=\"" + node.getPrettyID() + "\";\n");
839 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
840 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
841 FlowNode fieldNode = (FlowNode) iterator.next();
842 if (fieldNode.getFieldNodeSet().size() > 0) {
843 drawSubgraph(fieldNode, bw, addedSet);
845 Descriptor desc = fieldNode.getDescTuple().getLastElement();
846 if (desc instanceof VarDescriptor) {
847 VarDescriptor varDesc = (VarDescriptor) desc;
848 if (varDesc.getType().isPrimitive()) {
849 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
851 } else if (desc instanceof FieldDescriptor) {
852 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
853 if (fieldDesc.getType().isPrimitive()) {
854 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
863 public void removeEdge(NTuple<Descriptor> from, NTuple<Descriptor> to) {
865 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
866 Set<FlowEdge> edgeSet = getOutEdgeSet(getFlowNode(from));
868 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
869 FlowEdge flowEdge = (FlowEdge) iterator.next();
870 if (flowEdge.getInitTuple().equals(from) && flowEdge.getEndTuple().equals(to)) {
871 toberemoved.add(flowEdge);
875 edgeSet.removeAll(toberemoved);
879 public void removeNode(FlowNode node) {
881 NTuple<Descriptor> tuple = node.getCurrentDescTuple();
883 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
884 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
885 FlowEdge flowEdge = (FlowEdge) iterator.next();
886 if (flowEdge.getEndTuple().equals(tuple)) {
888 Set<FlowEdge> outSet = getOutEdgeSet(getFlowNode(flowEdge.getInitTuple()));
889 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
890 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
891 FlowEdge outEdge = (FlowEdge) iterator2.next();
892 if (outEdge.getEndTuple().equals(tuple)) {
893 toberemoved.add(outEdge);
896 outSet.removeAll(toberemoved);
900 Set<FlowEdge> outEdgeSet = getOutEdgeSet(node);
901 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
902 FlowEdge flowEdge = (FlowEdge) iterator.next();
903 if (flowEdge.getInitTuple().equals(tuple)) {
905 Set<FlowEdge> inSet = getInEdgeSet(getFlowNode(flowEdge.getEndTuple()));
906 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
907 for (Iterator iterator2 = inSet.iterator(); iterator2.hasNext();) {
908 FlowEdge inEdge = (FlowEdge) iterator2.next();
909 if (inEdge.getInitTuple().equals(tuple)) {
910 toberemoved.add(inEdge);
913 inSet.removeAll(toberemoved);
917 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
918 FlowNode flowNode = (FlowNode) iterator.next();
919 Set<FlowEdge> outSet = getOutEdgeSet(flowNode);
920 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
921 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
922 FlowEdge flowEdge = (FlowEdge) iterator2.next();
923 if (flowEdge.getInitTuple().equals(tuple) || flowEdge.getEndTuple().equals(tuple)) {
924 toberemoved.add(flowEdge);
927 outSet.removeAll(toberemoved);
930 mapFlowNodeToOutEdgeSet.remove(node);
931 mapFlowNodeToInEdgeSet.remove(node);
932 System.out.println("REMOVING NODE=" + mapDescTupleToInferNode.get(tuple) + " from tuple="
934 mapDescTupleToInferNode.remove(tuple);
935 System.out.println("mapDescTupleToInferNode=" + mapDescTupleToInferNode);