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 mapIntersectionDescToEnclosingDescriptor.put(interDesc, desc);
84 public ClassDescriptor getEnclosingDescriptor(Descriptor interDesc) {
85 return mapIntersectionDescToEnclosingDescriptor.get(interDesc);
88 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
89 return mapDescTupleToInferNode;
92 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
93 this.mapDescTupleToInferNode.putAll(in);
96 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
97 return mapLocTupleToFlowNode;
100 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
101 this.mapLocTupleToFlowNode.putAll(in);
104 public void setReturnNodeSet(Set<FlowNode> in) {
105 this.returnNodeSet.addAll(in);
108 public void setThisVarNode(FlowNode thisVarNode) {
109 this.thisVarNode = thisVarNode;
112 public Map<Descriptor, Integer> getMapParamDescToIdx() {
113 return mapParamDescToIdx;
116 public FlowNode createIntermediateNode() {
117 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
118 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
119 tuple.add(interDesc);
122 FlowNode newNode = new FlowNode(tuple);
123 newNode.setIntermediate(true);
125 mapDescTupleToInferNode.put(tuple, newNode);
126 // nodeSet.add(newNode);
128 // System.out.println("create new intsermediate node= " + newNode);
133 public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
134 return mapMethodInvokeNodeToFlowReturnNode.get(min);
137 public FlowReturnNode createReturnNode(MethodInvokeNode min) {
138 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
139 NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
142 FlowReturnNode newNode = new FlowReturnNode(tuple, min);
143 mapDescTupleToInferNode.put(tuple, newNode);
144 mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
145 // nodeSet.add(newNode);
147 // System.out.println("create new set node= " + newNode);
152 private void setupMapIdxToDesc() {
154 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
155 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
156 Descriptor paramDesc = (Descriptor) iterator.next();
157 int idx = mapParamDescToIdx.get(paramDesc);
158 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
159 descTuple.add(paramDesc);
160 FlowNode paramNode = getFlowNode(descTuple);
161 mapIdxToFlowNode.put(idx, paramNode);
162 paramNode.setSkeleton(true);
167 public int getNumParameters() {
168 return mapIdxToFlowNode.keySet().size();
171 public FlowNode getParamFlowNode(int idx) {
172 return mapIdxToFlowNode.get(idx);
175 public Set<FlowEdge> getEdgeSet() {
176 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
178 Set<FlowNode> nodeSet = getNodeSet();
179 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
180 FlowNode flowNode = (FlowNode) iterator.next();
181 edgeSet.addAll(getOutEdgeSet(flowNode));
186 public Set<FlowNode> getParamFlowNodeSet() {
187 Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
188 setParamFlowNode.addAll(mapIdxToFlowNode.values());
189 return setParamFlowNode;
192 public Set<FlowNode> getNodeSet() {
193 Set<FlowNode> set = new HashSet<FlowNode>();
194 set.addAll(mapDescTupleToInferNode.values());
198 public MethodDescriptor getMethodDescriptor() {
202 public boolean isParamDesc(Descriptor desc) {
204 if (mapParamDescToIdx.containsKey(desc)) {
205 int idx = mapParamDescToIdx.get(desc);
206 if (!md.isStatic() && idx == 0) {
215 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
217 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
218 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
220 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
221 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
222 FlowEdge flowEdge = (FlowEdge) iterator.next();
223 if (flowEdge.getDst().equals(toNode)) {
226 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
235 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
237 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
239 // returns the set of edges that share the same prefix of startNode
240 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
242 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
243 Set<FlowEdge> nodeEdgeSet = iter.next();
244 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
245 FlowEdge edge = iter2.next();
246 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
255 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
256 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
257 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
259 return mapFlowNodeToOutEdgeSet.get(node);
262 public Set<FlowEdge> getInEdgeSet(FlowNode node) {
263 if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
264 mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
266 return mapFlowNodeToInEdgeSet.get(node);
269 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
271 FlowNode fromNode = getFlowNode(fromDescTuple);
272 FlowNode toNode = getFlowNode(toDescTuple);
274 if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
278 // System.out.println("create an edge from " + fromNode + " to " + toNode);
280 int fromTupleSize = fromDescTuple.size();
281 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
282 for (int i = 0; i < fromTupleSize; i++) {
283 Descriptor desc = fromDescTuple.get(i);
284 curFromTuple.add(desc);
285 int toTupleSize = toDescTuple.size();
286 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
287 for (int k = 0; k < toTupleSize; k++) {
288 Descriptor toDesc = toDescTuple.get(k);
289 curToTuple.add(toDesc);
290 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
294 // int fromTupleSize = fromDescTuple.size();
295 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
296 // for (int i = 0; i < fromTupleSize; i++) {
297 // Descriptor desc = fromDescTuple.get(i);
298 // curTuple.add(desc);
299 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
302 // int toTupleSize = toDescTuple.size();
303 // curTuple = new NTuple<Descriptor>();
304 // for (int i = 0; i < toTupleSize; i++) {
305 // Descriptor desc = toDescTuple.get(i);
306 // curTuple.add(desc);
307 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
312 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
313 NTuple<Descriptor> endTuple) {
315 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
316 addOutEdge(fromNode, edge);
317 addInEdge(toNode, edge);
319 // System.out.println("add a new edge=" + edge);
322 private void addInEdge(FlowNode toNode, FlowEdge edge) {
323 getInEdgeSet(toNode).add(edge);
326 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
327 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
328 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
330 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
333 public boolean contains(NTuple<Descriptor> descTuple) {
334 return mapDescTupleToInferNode.containsKey(descTuple);
337 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
338 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
339 FlowNode node = createNewFlowNode(descTuple);
340 mapDescTupleToInferNode.put(descTuple, node);
342 return mapDescTupleToInferNode.get(descTuple);
345 public FlowNode getThisVarNode() {
349 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
351 // System.out.println("createNewFlowNode=" + tuple);
352 if (!mapDescTupleToInferNode.containsKey(tuple)) {
353 FlowNode node = new FlowNode(tuple);
354 mapDescTupleToInferNode.put(tuple, node);
355 // nodeSet.add(node);
357 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
359 if (tuple.size() > 1) {
360 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
361 getFlowNode(baseTuple).addFieldNode(node);
364 // System.out.println("Creating new node=" + node);
367 return mapDescTupleToInferNode.get(tuple);
372 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
374 if (!mapDescTupleToInferNode.containsKey(tuple)) {
375 createNewFlowNode(tuple);
378 FlowNode node = mapDescTupleToInferNode.get(tuple);
379 returnNodeSet.add(node);
382 public Set<FlowNode> getReturnNodeSet() {
383 return returnNodeSet;
386 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
387 Set<FlowNode> set = new HashSet<FlowNode>();
388 recurLocalReachFlowNodeSet(fn, set);
392 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
394 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
395 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
396 FlowEdge edge = (FlowEdge) iterator.next();
397 FlowNode originalDstNode = edge.getDst();
399 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
400 if (originalDstNode instanceof FlowReturnNode) {
401 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
402 Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
403 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
404 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
405 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
406 dstNodeSet.add(getFlowNode(rtuple));
409 dstNodeSet.add(originalDstNode);
412 for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
413 FlowNode dstNode = (FlowNode) iterator2.next();
414 if (!visited.contains(dstNode)) {
415 visited.add(dstNode);
416 recurLocalReachFlowNodeSet(dstNode, visited);
424 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
426 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
427 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
428 FlowEdge edge = (FlowEdge) iterator.next();
430 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
431 FlowNode dstNode = getFlowNode(edge.getEndTuple());
432 if (!visited.contains(dstNode)) {
433 visited.add(dstNode);
434 getReachFlowNodeSetFrom(dstNode, visited);
441 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
442 Set<FlowNode> set = new HashSet<FlowNode>();
443 getReachFlowNodeSetFrom(fn, set);
447 public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
448 Set<FlowNode> reachableSet = new HashSet<FlowNode>();
450 Set<FlowNode> nodeSet = getNodeSet();
451 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
452 FlowNode originalSrcNode = (FlowNode) iterator.next();
454 Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
455 if (originalSrcNode instanceof FlowReturnNode) {
456 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
457 Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
458 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
459 // System.out.println("#rnode=" + rnode + " rtupleSet=" + rtupleSet);
460 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
461 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
462 if (rtuple.startsWith(prefix)) {
463 // System.out.println("rtuple=" + rtuple + " give it to recur=" + originalSrcNode);
464 recurReachableSetFrom(originalSrcNode, reachableSet);
468 if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
469 recurReachableSetFrom(originalSrcNode, reachableSet);
478 public Set<NTuple<Descriptor>> getReturnTupleSet(Set<NTuple<Descriptor>> in) {
480 Set<NTuple<Descriptor>> normalTupleSet = new HashSet<NTuple<Descriptor>>();
481 for (Iterator iterator2 = in.iterator(); iterator2.hasNext();) {
482 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator2.next();
483 FlowNode tupleNode = getFlowNode(tuple);
484 if (tupleNode instanceof FlowReturnNode) {
485 normalTupleSet.addAll(getReturnTupleSet(((FlowReturnNode) tupleNode).getReturnTupleSet()));
487 normalTupleSet.add(tuple);
490 return normalTupleSet;
493 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
495 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
496 // iterator.hasNext();) {
497 // FlowEdge edge = (FlowEdge) iterator.next();
499 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
501 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
503 // if (!visited.contains(dstNode)) {
504 // visited.add(dstNode);
505 // getReachFlowNodeSetFrom(dstNode, visited);
512 private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
514 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
515 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
516 FlowEdge edge = (FlowEdge) iterator.next();
517 FlowNode dstNode = getFlowNode(edge.getEndTuple());
518 if (!reachableSet.contains(dstNode)) {
519 reachableSet.add(dstNode);
520 recurReachableSetFrom(dstNode, reachableSet);
526 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
528 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
530 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
531 FlowEdge edge = (FlowEdge) iterator.next();
533 if (fn.getDescTuple().equals(edge.getInitTuple())) {
534 FlowNode dstNode = getFlowNode(edge.getEndTuple());
535 NTuple<Location> dstTuple = getLocationTuple(dstNode);
537 if (!visited.contains(dstTuple)) {
538 visited.add(dstTuple);
539 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
547 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
548 return getLocationTuple(getFlowNode(descTuple));
551 public NTuple<Location> getLocationTuple(FlowNode fn) {
553 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
554 NTuple<Descriptor> descTuple = fn.getDescTuple();
555 NTuple<Location> locTuple = new NTuple<Location>();
556 ClassDescriptor cd = null;
558 Descriptor localDesc = fn.getDescTuple().get(0);
560 if (fn.isIntermediate() && fn.getDescTuple().size() == 1) {
561 Location interLoc = new Location(md, localDesc.getSymbol());
562 interLoc.setLocDescriptor(localDesc);
563 locTuple.add(interLoc);
564 } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
565 Location topLoc = new Location(md, Location.TOP);
566 topLoc.setLocDescriptor(LocationInference.TOPDESC);
567 locTuple.add(topLoc);
568 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
569 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
570 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
571 locTuple.add(globalLoc);
574 for (int i = 0; i < descTuple.size(); i++) {
575 Descriptor curDesc = descTuple.get(i);
578 loc = new Location(md, curDesc.getSymbol());
579 loc.setLocDescriptor(curDesc);
580 if (curDesc instanceof VarDescriptor) {
581 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
582 } else if (curDesc instanceof InterDescriptor) {
583 cd = mapIntersectionDescToEnclosingDescriptor.get(curDesc);
585 // otherwise it should be the last element
589 loc = new Location(cd, curDesc.getSymbol());
590 loc.setLocDescriptor(curDesc);
592 if (curDesc instanceof FieldDescriptor) {
593 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
594 } else if (curDesc instanceof LocationDescriptor) {
595 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
597 // otherwise it should be the last element of the tuple
606 mapFlowNodeToLocTuple.put(fn, locTuple);
608 return mapFlowNodeToLocTuple.get(fn);
611 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
612 Set<FlowNode> set = new HashSet<FlowNode>();
613 getIncomingFlowNodeSet(node, set);
617 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
619 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
620 FlowNode curNode = (FlowNode) iterator.next();
621 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
623 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
624 FlowEdge flowEdge = (FlowEdge) iterator2.next();
626 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
627 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
629 if (incomingNode instanceof FlowReturnNode) {
630 FlowReturnNode rnode = (FlowReturnNode) incomingNode;
631 Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
632 Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(nodeTupleSet);
633 for (Iterator iterator3 = rtupleSet.iterator(); iterator3.hasNext();) {
634 NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
635 FlowNode fn = getFlowNode(nodeTuple);
636 if (!visited.contains(fn)) {
638 getIncomingFlowNodeSet(fn, visited);
642 if (!visited.contains(incomingNode)) {
643 visited.add(incomingNode);
644 getIncomingFlowNodeSet(incomingNode, visited);
654 public boolean isParameter(NTuple<Descriptor> tuple) {
655 // return true if a descriptor tuple is started with a parameter descriptor
656 Descriptor firstIdxDesc = tuple.get(0);
657 return mapParamDescToIdx.containsKey(firstIdxDesc);
660 public int getParamIdx(NTuple<Descriptor> tuple) {
661 Descriptor firstIdxDesc = tuple.get(0);
662 return mapParamDescToIdx.get(firstIdxDesc);
665 public FlowGraph clone() {
666 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
668 // clone.setNodeSet(getNodeSet());
669 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
670 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
671 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
673 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
674 clone.setReturnNodeSet(getReturnNodeSet());
675 clone.setThisVarNode(getThisVarNode());
680 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
681 return mapFlowNodeToLocTuple;
684 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
685 this.mapFlowNodeToLocTuple.putAll(in);
688 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
689 return mapFlowNodeToOutEdgeSet;
692 public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
694 Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
696 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
697 FlowNode curNode = (FlowNode) iterator.next();
698 Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
700 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
701 FlowEdge outEdge = (FlowEdge) iterator2.next();
703 if (outEdge.getEndTuple().startsWith(prefix)) {
704 incomingNodeSet.add(curNode);
705 recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
712 return incomingNodeSet;
716 private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
718 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
720 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
721 FlowEdge inEdge = (FlowEdge) iterator.next();
723 FlowNode inNode = getFlowNode(inEdge.getInitTuple());
724 if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
726 recurIncomingNodeSetByPrefix(prefix, inNode, visited);
732 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
733 Set<FlowNode> keySet = inMap.keySet();
734 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
735 FlowNode key = (FlowNode) iterator.next();
736 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
737 newEdgeSet.addAll(inMap.get(key));
738 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
742 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
743 Set<FlowEdge> addedEdgeSet) throws IOException {
745 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
747 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
748 FlowEdge flowEdge = iterator.next();
750 FlowNode u = flowEdge.getSrc();
751 FlowNode v = flowEdge.getDst();
753 if (u.getDescTuple().equals(flowEdge.getInitTuple())
754 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
755 // only draw an edge of the actual value flow
757 if (!addedEdgeSet.contains(flowEdge)) {
759 if (!addedNodeSet.contains(u)) {
763 if (!addedNodeSet.contains(v)) {
768 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
769 addedEdgeSet.add(flowEdge);
777 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
778 if (node instanceof FlowReturnNode) {
779 FlowReturnNode rnode = (FlowReturnNode) node;
780 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
782 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
787 public void writeGraph() throws java.io.IOException {
791 public void writeGraph(String suffix) throws java.io.IOException {
793 String graphName = "flowgraph_" + md.toString() + suffix;
794 graphName = graphName.replaceAll("[\\W]", "");
796 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
797 bw.write("digraph " + graphName + " {\n");
798 bw.write("compound=true;\n");
800 // then visit every flow node
802 // Iterator<FlowNode> iter = nodeSet.iterator();
803 Iterator<FlowNode> iter = getNodeSet().iterator();
805 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
806 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
808 while (iter.hasNext()) {
809 FlowNode node = iter.next();
811 if (node.getDescTuple().size() == 1) {
812 // here, we just care about the local variable
813 if (node.getFieldNodeSet().size() > 0) {
814 drawSubgraph(node, bw, addedEdgeSet);
817 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
826 public boolean constainsNode(FlowNode node) {
827 return getNodeSet().contains(node);
830 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
833 bw.write("subgraph cluster_" + node.getID() + "{\n");
834 bw.write("label=\"" + node.getPrettyID() + "\";\n");
836 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
837 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
838 FlowNode fieldNode = (FlowNode) iterator.next();
839 if (fieldNode.getFieldNodeSet().size() > 0) {
840 drawSubgraph(fieldNode, bw, addedSet);
842 Descriptor desc = fieldNode.getDescTuple().getLastElement();
843 if (desc instanceof VarDescriptor) {
844 VarDescriptor varDesc = (VarDescriptor) desc;
845 if (varDesc.getType().isPrimitive()) {
846 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
848 } else if (desc instanceof FieldDescriptor) {
849 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
850 if (fieldDesc.getType().isPrimitive()) {
851 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
860 public void removeEdge(NTuple<Descriptor> from, NTuple<Descriptor> to) {
862 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
863 Set<FlowEdge> edgeSet = getOutEdgeSet(getFlowNode(from));
865 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
866 FlowEdge flowEdge = (FlowEdge) iterator.next();
867 if (flowEdge.getInitTuple().equals(from) && flowEdge.getEndTuple().equals(to)) {
868 toberemoved.add(flowEdge);
872 edgeSet.removeAll(toberemoved);
876 public void updateTuple(FlowNode node, NTuple<Descriptor> newTuple) {
878 NTuple<Descriptor> curTuple = node.getCurrentDescTuple();
879 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
880 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
881 FlowEdge flowEdge = (FlowEdge) iterator.next();
882 if (flowEdge.getEndTuple().equals(curTuple)) {
883 flowEdge.setEndTuple(newTuple);
887 Set<FlowEdge> outEdgeSet = getOutEdgeSet(node);
888 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
889 FlowEdge flowEdge = (FlowEdge) iterator.next();
890 if (flowEdge.getInitTuple().equals(curTuple)) {
891 flowEdge.setInitTuple(newTuple);
895 node.setBaseTuple(newTuple);
899 public void removeNode(FlowNode node) {
901 NTuple<Descriptor> tuple = node.getCurrentDescTuple();
903 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
904 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
905 FlowEdge flowEdge = (FlowEdge) iterator.next();
906 if (flowEdge.getEndTuple().equals(tuple)) {
908 Set<FlowEdge> outSet = getOutEdgeSet(getFlowNode(flowEdge.getInitTuple()));
909 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
910 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
911 FlowEdge outEdge = (FlowEdge) iterator2.next();
912 if (outEdge.getEndTuple().equals(tuple)) {
913 toberemoved.add(outEdge);
916 outSet.removeAll(toberemoved);
920 Set<FlowEdge> outEdgeSet = getOutEdgeSet(node);
921 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
922 FlowEdge flowEdge = (FlowEdge) iterator.next();
923 if (flowEdge.getInitTuple().equals(tuple)) {
925 Set<FlowEdge> inSet = getInEdgeSet(getFlowNode(flowEdge.getEndTuple()));
926 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
927 for (Iterator iterator2 = inSet.iterator(); iterator2.hasNext();) {
928 FlowEdge inEdge = (FlowEdge) iterator2.next();
929 if (inEdge.getInitTuple().equals(tuple)) {
930 toberemoved.add(inEdge);
933 inSet.removeAll(toberemoved);
937 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
938 FlowNode flowNode = (FlowNode) iterator.next();
939 Set<FlowEdge> outSet = getOutEdgeSet(flowNode);
940 Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
941 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
942 FlowEdge flowEdge = (FlowEdge) iterator2.next();
943 if (flowEdge.getInitTuple().equals(tuple) || flowEdge.getEndTuple().equals(tuple)) {
944 toberemoved.add(flowEdge);
947 outSet.removeAll(toberemoved);
950 mapFlowNodeToOutEdgeSet.remove(node);
951 mapFlowNodeToInEdgeSet.remove(node);
952 System.out.println("REMOVING NODE=" + mapDescTupleToInferNode.get(tuple) + " from tuple="
954 mapDescTupleToInferNode.remove(tuple);
955 System.out.println("mapDescTupleToInferNode=" + mapDescTupleToInferNode);