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;
19 public class FlowGraph {
23 Set<FlowNode> nodeSet;
24 Set<FlowNode> returnNodeSet;
27 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
28 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
30 // maps the composite representation of field/var descriptors to infer nodes
31 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
33 // maps an infer node to the set of neighbors which is pointed by the node
34 Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
36 // maps a paramter descriptor to its index
37 Map<Descriptor, Integer> mapParamDescToIdx;
39 Map<Integer, FlowNode> mapIdxToFlowNode;
41 public static int interseed = 0;
45 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
47 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
48 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
49 this.nodeSet = new HashSet<FlowNode>();
50 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
51 this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
52 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
53 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
54 this.returnNodeSet = new HashSet<FlowNode>();
55 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
58 // create a node for 'this' varialbe
59 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
60 // thisDescTuple.add(md.getThis());
62 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
63 thisVarTuple.add(md.getThis());
64 FlowNode thisNode = createNewFlowNode(thisVarTuple);
65 thisNode.setSkeleton(true);
66 thisVarNode = thisNode;
73 public Map<Descriptor, Integer> getMapParamDescToIdx() {
74 return mapParamDescToIdx;
77 public FlowNode createIntermediateNode() {
78 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
79 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
83 FlowNode newNode = new FlowNode(tuple);
84 newNode.setIntermediate(true);
86 mapDescTupleToInferNode.put(tuple, newNode);
89 System.out.println("create new intermediate node= " + newNode);
94 private void setupMapIdxToDesc() {
96 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
97 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
98 Descriptor paramDesc = (Descriptor) iterator.next();
99 int idx = mapParamDescToIdx.get(paramDesc);
100 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
101 descTuple.add(paramDesc);
102 FlowNode paramNode = getFlowNode(descTuple);
103 mapIdxToFlowNode.put(idx, paramNode);
104 paramNode.setSkeleton(true);
109 public int getNumParameters() {
110 return mapIdxToFlowNode.keySet().size();
113 public FlowNode getParamFlowNode(int idx) {
114 return mapIdxToFlowNode.get(idx);
117 public Set<FlowNode> getNodeSet() {
121 public MethodDescriptor getMethodDescriptor() {
125 public void addNeighbor(FlowNode node, FlowNode neighbor) {
126 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
128 set = new HashSet<FlowNode>();
132 // System.out.println("add a new neighbor " + neighbor + " to " + node);
135 public boolean isParamDesc(Descriptor desc) {
137 if (mapParamDescToIdx.containsKey(desc)) {
138 int idx = mapParamDescToIdx.get(desc);
139 if (!md.isStatic() && idx == 0) {
148 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
150 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
151 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
153 Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
154 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
155 FlowEdge flowEdge = (FlowEdge) iterator.next();
156 if (flowEdge.getDst().equals(toNode)) {
159 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
168 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
170 FlowNode fromNode = getFlowNode(fromDescTuple);
171 FlowNode toNode = getFlowNode(toDescTuple);
173 System.out.println("create an edge from " + fromNode + " to " + toNode);
175 int fromTupleSize = fromDescTuple.size();
176 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
177 for (int i = 0; i < fromTupleSize; i++) {
178 Descriptor desc = fromDescTuple.get(i);
179 curFromTuple.add(desc);
180 int toTupleSize = toDescTuple.size();
181 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
182 for (int k = 0; k < toTupleSize; k++) {
183 Descriptor toDesc = toDescTuple.get(k);
184 curToTuple.add(toDesc);
185 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
189 // int fromTupleSize = fromDescTuple.size();
190 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
191 // for (int i = 0; i < fromTupleSize; i++) {
192 // Descriptor desc = fromDescTuple.get(i);
193 // curTuple.add(desc);
194 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
197 // int toTupleSize = toDescTuple.size();
198 // curTuple = new NTuple<Descriptor>();
199 // for (int i = 0; i < toTupleSize; i++) {
200 // Descriptor desc = toDescTuple.get(i);
201 // curTuple.add(desc);
202 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
207 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
208 NTuple<Descriptor> endTuple) {
210 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
211 fromNode.addOutEdge(edge);
213 System.out.println("add a new edge=" + edge);
216 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
217 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
218 FlowNode node = createNewFlowNode(descTuple);
219 mapDescTupleToInferNode.put(descTuple, node);
221 return mapDescTupleToInferNode.get(descTuple);
224 public FlowNode getThisVarNode() {
228 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
230 if (!mapDescTupleToInferNode.containsKey(tuple)) {
231 FlowNode node = new FlowNode(tuple);
232 mapDescTupleToInferNode.put(tuple, node);
235 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
237 if (tuple.size() > 1) {
238 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
239 getFlowNode(baseTuple).addFieldNode(node);
242 // System.out.println("Creating new node=" + node);
245 return mapDescTupleToInferNode.get(tuple);
250 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
252 if (!mapDescTupleToInferNode.containsKey(tuple)) {
253 createNewFlowNode(tuple);
256 FlowNode node = mapDescTupleToInferNode.get(tuple);
257 node.setReturn(true);
259 returnNodeSet.add(node);
262 public Set<FlowNode> getReturnNodeSet() {
263 return returnNodeSet;
266 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
267 Set<FlowNode> set = new HashSet<FlowNode>();
268 recurLocalReachFlowNodeSet(fn, set);
272 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
274 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
275 FlowEdge edge = (FlowEdge) iterator.next();
276 FlowNode dstNode = edge.getDst();
278 if (!visited.contains(dstNode)) {
279 visited.add(dstNode);
280 recurLocalReachFlowNodeSet(dstNode, visited);
286 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
288 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
289 FlowEdge edge = (FlowEdge) iterator.next();
291 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
292 FlowNode dstNode = getFlowNode(edge.getEndTuple());
293 if (!visited.contains(dstNode)) {
294 visited.add(dstNode);
295 getReachFlowNodeSetFrom(dstNode, visited);
302 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
303 Set<FlowNode> set = new HashSet<FlowNode>();
304 getReachFlowNodeSetFrom(fn, set);
308 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
310 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
311 // iterator.hasNext();) {
312 // FlowEdge edge = (FlowEdge) iterator.next();
314 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
316 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
318 // if (!visited.contains(dstNode)) {
319 // visited.add(dstNode);
320 // getReachFlowNodeSetFrom(dstNode, visited);
327 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
328 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
329 FlowEdge edge = (FlowEdge) iterator.next();
331 if (fn.getDescTuple().equals(edge.getInitTuple())) {
332 FlowNode dstNode = getFlowNode(edge.getEndTuple());
333 NTuple<Location> dstTuple = getLocationTuple(dstNode);
335 if (!visited.contains(dstTuple)) {
336 visited.add(dstTuple);
337 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
345 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
346 return getLocationTuple(getFlowNode(descTuple));
349 public NTuple<Location> getLocationTuple(FlowNode fn) {
351 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
352 NTuple<Descriptor> descTuple = fn.getDescTuple();
353 NTuple<Location> locTuple = new NTuple<Location>();
354 ClassDescriptor cd = null;
356 Descriptor localDesc = fn.getDescTuple().get(0);
358 if (fn.isIntermediate()) {
359 Location interLoc = new Location(md, localDesc.getSymbol());
360 interLoc.setLocDescriptor(localDesc);
361 locTuple.add(interLoc);
362 } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
363 Location topLoc = new Location(md, Location.TOP);
364 topLoc.setLocDescriptor(LocationInference.TOPDESC);
365 locTuple.add(topLoc);
366 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
367 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
368 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
369 locTuple.add(globalLoc);
372 for (int i = 0; i < descTuple.size(); i++) {
373 Descriptor curDesc = descTuple.get(i);
376 loc = new Location(md, curDesc.getSymbol());
377 loc.setLocDescriptor(curDesc);
378 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
380 loc = new Location(cd, curDesc.getSymbol());
381 loc.setLocDescriptor(curDesc);
382 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
388 mapFlowNodeToLocTuple.put(fn, locTuple);
390 return mapFlowNodeToLocTuple.get(fn);
393 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
394 Set<FlowNode> set = new HashSet<FlowNode>();
395 getIncomingFlowNodeSet(node, set);
399 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
401 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
402 FlowNode curNode = (FlowNode) iterator.next();
403 Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
405 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
406 FlowEdge flowEdge = (FlowEdge) iterator2.next();
408 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
409 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
411 if (!visited.contains(incomingNode)) {
412 visited.add(incomingNode);
413 getIncomingFlowNodeSet(incomingNode, visited);
421 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
423 NTuple<Descriptor> dstTuple = fn.getDescTuple();
425 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
427 ClassDescriptor cd = null;
429 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
430 FlowNode node = (FlowNode) iterator.next();
431 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
432 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
433 FlowEdge flowEdge = (FlowEdge) iterator2.next();
434 if (dstTuple.equals(flowEdge.getEndTuple())) {
435 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
436 NTuple<Location> locTuple = new NTuple<Location>();
437 for (int i = 0; i < initTuple.size(); i++) {
438 Descriptor d = initTuple.get(i);
441 loc = new Location(md, d.getSymbol());
442 cd = ((VarDescriptor) d).getType().getClassDesc();
444 loc = new Location(cd, d.getSymbol());
445 cd = ((FieldDescriptor) d).getType().getClassDesc();
456 public boolean isParameter(NTuple<Descriptor> tuple) {
457 // return true if a descriptor tuple is started with a parameter descriptor
458 Descriptor firstIdxDesc = tuple.get(0);
459 return mapParamDescToIdx.containsKey(firstIdxDesc);
462 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
463 Set<FlowEdge> addedEdgeSet) throws IOException {
465 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
467 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
468 FlowEdge flowEdge = iterator.next();
470 FlowNode u = flowEdge.getSrc();
471 FlowNode v = flowEdge.getDst();
473 if (u.getDescTuple().equals(flowEdge.getInitTuple())
474 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
475 // only draw an edge of the actual value flow
477 if (!addedEdgeSet.contains(flowEdge)) {
479 if (!addedNodeSet.contains(u)) {
483 if (!addedNodeSet.contains(v)) {
488 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
489 addedEdgeSet.add(flowEdge);
497 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
498 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
501 public void writeGraph() throws java.io.IOException {
503 String graphName = "flowgraph_" + md.toString();
504 graphName = graphName.replaceAll("[\\W]", "");
506 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
507 bw.write("digraph " + graphName + " {\n");
508 bw.write("compound=true;\n");
510 // then visit every flow node
512 Iterator<FlowNode> iter = nodeSet.iterator();
514 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
515 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
517 while (iter.hasNext()) {
518 FlowNode node = iter.next();
520 if (node.getDescTuple().size() == 1) {
521 // here, we just care about the local variable
522 if (node.getFieldNodeSet().size() > 0) {
523 drawSubgraph(node, bw, addedEdgeSet);
526 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
535 public boolean constainsNode(FlowNode node) {
536 return nodeSet.contains(node);
539 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
542 bw.write("subgraph cluster_" + node.getID() + "{\n");
543 bw.write("label=\"" + node.getPrettyID() + "\";\n");
545 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
546 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
547 FlowNode fieldNode = (FlowNode) iterator.next();
548 if (fieldNode.getFieldNodeSet().size() > 0) {
549 drawSubgraph(fieldNode, bw, addedSet);
551 Descriptor desc = fieldNode.getDescTuple().getLastElement();
552 if (desc instanceof VarDescriptor) {
553 VarDescriptor varDesc = (VarDescriptor) desc;
554 if (varDesc.getType().isPrimitive()) {
555 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
557 } else if (desc instanceof FieldDescriptor) {
558 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
559 if (fieldDesc.getType().isPrimitive()) {
560 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");