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());
61 FlowNode thisNode = new FlowNode(thisDescTuple, true);
62 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
63 thisVarTuple.add(md.getThis());
64 createNewFlowNode(thisVarTuple);
65 thisVarNode = thisNode;
72 public FlowNode createIntermediateNode() {
73 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
74 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
77 FlowNode node = createNewFlowNode(tuple, true);
81 private void setupMapIdxToDesc() {
83 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
84 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
85 Descriptor paramDesc = (Descriptor) iterator.next();
86 int idx = mapParamDescToIdx.get(paramDesc);
87 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
88 descTuple.add(paramDesc);
89 mapIdxToFlowNode.put(idx, getFlowNode(descTuple));
94 public FlowNode getParamFlowNode(int idx) {
95 return mapIdxToFlowNode.get(idx);
98 public Set<FlowNode> getNodeSet() {
102 public MethodDescriptor getMethodDescriptor() {
106 public Set<FlowNode> getParameterNodeSet() {
107 Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
108 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
109 FlowNode fn = (FlowNode) iterator.next();
110 if (fn.isParameter()) {
111 paramNodeSet.add(fn);
117 public void addNeighbor(FlowNode node, FlowNode neighbor) {
118 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
120 set = new HashSet<FlowNode>();
124 // System.out.println("add a new neighbor " + neighbor + " to " + node);
127 public boolean isParamDesc(Descriptor desc) {
129 if (mapParamDescToIdx.containsKey(desc)) {
130 int idx = mapParamDescToIdx.get(desc);
131 if (!md.isStatic() && idx == 0) {
140 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
142 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
143 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
145 Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
146 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
147 FlowEdge flowEdge = (FlowEdge) iterator.next();
148 if (flowEdge.getDst().equals(toNode)) {
151 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
160 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
162 FlowNode fromNode = getFlowNode(fromDescTuple);
163 FlowNode toNode = getFlowNode(toDescTuple);
165 // System.out.println("create an edge from " + fromNode + " to " + toNode);
167 int fromTupleSize = fromDescTuple.size();
168 NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
169 for (int i = 0; i < fromTupleSize; i++) {
170 Descriptor desc = fromDescTuple.get(i);
172 addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
175 int toTupleSize = toDescTuple.size();
176 curTuple = new NTuple<Descriptor>();
177 for (int i = 0; i < toTupleSize; i++) {
178 Descriptor desc = toDescTuple.get(i);
180 addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
185 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
186 NTuple<Descriptor> endTuple) {
188 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
189 fromNode.addOutEdge(edge);
191 // System.out.println("add a new edge=" + edge);
195 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
196 if (mapDescTupleToInferNode.containsKey(descTuple)) {
197 return mapDescTupleToInferNode.get(descTuple);
199 FlowNode node = createNewFlowNode(descTuple);
204 public FlowNode getThisVarNode() {
208 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
209 return createNewFlowNode(tuple, false);
212 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple, boolean isIntermediate) {
214 if (!mapDescTupleToInferNode.containsKey(tuple)) {
215 FlowNode node = new FlowNode(tuple, isParameter(tuple));
216 node.setIntermediate(isIntermediate);
217 mapDescTupleToInferNode.put(tuple, node);
220 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
222 if (tuple.size() > 1) {
223 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
224 getFlowNode(baseTuple).addFieldNode(node);
227 // System.out.println("Creating new node=" + node);
230 return mapDescTupleToInferNode.get(tuple);
235 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
237 if (!mapDescTupleToInferNode.containsKey(tuple)) {
238 createNewFlowNode(tuple);
241 FlowNode node = mapDescTupleToInferNode.get(tuple);
242 node.setReturn(true);
244 returnNodeSet.add(node);
247 public Set<FlowNode> getReturnNodeSet() {
248 return returnNodeSet;
251 public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
252 Set<FlowNode> set = new HashSet<FlowNode>();
253 getReachableFlowNodeSet(fn, set);
257 private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
259 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
260 FlowEdge edge = (FlowEdge) iterator.next();
262 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
264 FlowNode dstNode = getFlowNode(edge.getEndTuple());
266 if (!visited.contains(dstNode)) {
267 visited.add(dstNode);
268 getReachableFlowNodeSet(dstNode, visited);
275 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
276 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
277 FlowEdge edge = (FlowEdge) iterator.next();
279 if (fn.getDescTuple().equals(edge.getInitTuple())) {
280 FlowNode dstNode = getFlowNode(edge.getEndTuple());
281 NTuple<Location> dstTuple = getLocationTuple(dstNode);
283 if (!visited.contains(dstTuple)) {
284 visited.add(dstTuple);
285 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
293 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
294 return getLocationTuple(getFlowNode(descTuple));
297 public NTuple<Location> getLocationTuple(FlowNode fn) {
299 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
300 NTuple<Descriptor> descTuple = fn.getDescTuple();
301 NTuple<Location> locTuple = new NTuple<Location>();
302 ClassDescriptor cd = null;
304 Descriptor localDesc = fn.getDescTuple().get(0);
306 if (fn.isIntermediate()) {
307 Location interLoc = new Location(md, localDesc.getSymbol());
308 interLoc.setLocDescriptor(localDesc);
309 locTuple.add(interLoc);
310 } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
311 Location topLoc = new Location(md, Location.TOP);
312 topLoc.setLocDescriptor(LocationInference.TOPDESC);
313 locTuple.add(topLoc);
314 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
315 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
316 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
317 locTuple.add(globalLoc);
320 for (int i = 0; i < descTuple.size(); i++) {
321 Descriptor curDesc = descTuple.get(i);
324 loc = new Location(md, curDesc.getSymbol());
325 loc.setLocDescriptor(curDesc);
326 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
328 loc = new Location(cd, curDesc.getSymbol());
329 loc.setLocDescriptor(curDesc);
330 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
336 mapFlowNodeToLocTuple.put(fn, locTuple);
338 return mapFlowNodeToLocTuple.get(fn);
341 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
342 Set<FlowNode> set = new HashSet<FlowNode>();
343 getIncomingFlowNodeSet(node, set);
347 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
349 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
350 FlowNode curNode = (FlowNode) iterator.next();
351 Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
353 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
354 FlowEdge flowEdge = (FlowEdge) iterator2.next();
356 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
357 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
359 if (!visited.contains(incomingNode)) {
360 visited.add(incomingNode);
361 getIncomingFlowNodeSet(incomingNode, visited);
369 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
371 NTuple<Descriptor> dstTuple = fn.getDescTuple();
373 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
375 ClassDescriptor cd = null;
377 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
378 FlowNode node = (FlowNode) iterator.next();
379 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
380 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
381 FlowEdge flowEdge = (FlowEdge) iterator2.next();
382 if (dstTuple.equals(flowEdge.getEndTuple())) {
383 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
384 NTuple<Location> locTuple = new NTuple<Location>();
385 for (int i = 0; i < initTuple.size(); i++) {
386 Descriptor d = initTuple.get(i);
389 loc = new Location(md, d.getSymbol());
390 cd = ((VarDescriptor) d).getType().getClassDesc();
392 loc = new Location(cd, d.getSymbol());
393 cd = ((FieldDescriptor) d).getType().getClassDesc();
404 public boolean isParameter(NTuple<Descriptor> tuple) {
405 // return true if a descriptor tuple is started with a parameter descriptor
406 Descriptor firstIdxDesc = tuple.get(0);
407 return mapParamDescToIdx.containsKey(firstIdxDesc);
410 public int getParamIdx(NTuple<Descriptor> tuple) {
411 Descriptor firstDesc = tuple.get(0);
412 return mapParamDescToIdx.get(firstDesc).intValue();
415 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
416 Set<FlowEdge> addedEdgeSet) throws IOException {
418 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
420 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
421 FlowEdge flowEdge = iterator.next();
423 FlowNode u = flowEdge.getSrc();
424 FlowNode v = flowEdge.getDst();
426 if (u.getDescTuple().equals(flowEdge.getInitTuple())
427 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
428 // only draw an edge of the actual value flow
430 if (!addedEdgeSet.contains(flowEdge)) {
432 if (!addedNodeSet.contains(u)) {
436 if (!addedNodeSet.contains(v)) {
441 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
442 addedEdgeSet.add(flowEdge);
450 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
451 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
454 public void writeGraph() throws java.io.IOException {
456 String graphName = "flowgraph_" + md.toString();
457 graphName = graphName.replaceAll("[\\W]", "");
459 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
460 bw.write("digraph " + graphName + " {\n");
461 bw.write("compound=true;\n");
463 // then visit every flow node
465 Iterator<FlowNode> iter = nodeSet.iterator();
467 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
468 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
470 while (iter.hasNext()) {
471 FlowNode node = iter.next();
473 if (node.getDescTuple().size() == 1) {
474 // here, we just care about the local variable
475 if (node.getFieldNodeSet().size() > 0) {
476 drawSubgraph(node, bw, addedEdgeSet);
479 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
488 public boolean constainsNode(FlowNode node) {
489 return nodeSet.contains(node);
492 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
495 bw.write("subgraph cluster_" + node.getID() + "{\n");
496 bw.write("label=\"" + node.getPrettyID() + "\";\n");
498 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
499 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
500 FlowNode fieldNode = (FlowNode) iterator.next();
501 if (fieldNode.getFieldNodeSet().size() > 0) {
502 drawSubgraph(fieldNode, bw, addedSet);
504 Descriptor desc = fieldNode.getDescTuple().getLastElement();
505 if (desc instanceof VarDescriptor) {
506 VarDescriptor varDesc = (VarDescriptor) desc;
507 if (varDesc.getType().isPrimitive()) {
508 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
510 } else if (desc instanceof FieldDescriptor) {
511 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
512 if (fieldDesc.getType().isPrimitive()) {
513 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");