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.VarDescriptor;
18 public class FlowGraph {
22 Set<FlowNode> nodeSet;
23 Set<FlowNode> returnNodeSet;
26 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
27 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
29 // maps the composite representation of field/var descriptors to infer nodes
30 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
32 // maps an infer node to the set of neighbors which is pointed by the node
33 Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
35 // maps a paramter descriptor to its index
36 Map<Descriptor, Integer> mapParamDescToIdx;
38 Map<Integer, FlowNode> mapIdxToFlowNode;
42 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
44 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
45 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
46 this.nodeSet = new HashSet<FlowNode>();
47 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
48 this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<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>();
55 // create a node for 'this' varialbe
56 NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
57 thisDescTuple.add(md.getThis());
58 FlowNode thisNode = new FlowNode(thisDescTuple, true);
59 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
60 thisVarTuple.add(md.getThis());
61 createNewFlowNode(thisVarTuple);
62 thisVarNode = thisNode;
69 private void setupMapIdxToDesc() {
71 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
72 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
73 Descriptor paramDesc = (Descriptor) iterator.next();
74 int idx = mapParamDescToIdx.get(paramDesc);
75 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
76 descTuple.add(paramDesc);
77 mapIdxToFlowNode.put(idx, getFlowNode(descTuple));
82 public FlowNode getParamFlowNode(int idx) {
83 return mapIdxToFlowNode.get(idx);
86 public Set<FlowNode> getNodeSet() {
90 public MethodDescriptor getMethodDescriptor() {
94 public Set<FlowNode> getParameterNodeSet() {
95 Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
96 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
97 FlowNode fn = (FlowNode) iterator.next();
98 if (fn.isParameter()) {
105 public void addNeighbor(FlowNode node, FlowNode neighbor) {
106 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
108 set = new HashSet<FlowNode>();
112 // System.out.println("add a new neighbor " + neighbor + " to " + node);
115 public boolean isParamDesc(Descriptor desc) {
117 if (mapParamDescToIdx.containsKey(desc)) {
118 int idx = mapParamDescToIdx.get(desc);
119 if (!md.isStatic() && idx == 0) {
128 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
130 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
131 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
133 Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
134 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
135 FlowEdge flowEdge = (FlowEdge) iterator.next();
136 if (flowEdge.getDst().equals(toNode)) {
139 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
148 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
150 FlowNode fromNode = getFlowNode(fromDescTuple);
151 FlowNode toNode = getFlowNode(toDescTuple);
153 // System.out.println("create an edge from " + fromNode + " to " + toNode);
155 int fromTupleSize = fromDescTuple.size();
156 NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
157 for (int i = 0; i < fromTupleSize; i++) {
158 Descriptor desc = fromDescTuple.get(i);
160 addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
163 int toTupleSize = toDescTuple.size();
164 curTuple = new NTuple<Descriptor>();
165 for (int i = 0; i < toTupleSize; i++) {
166 Descriptor desc = toDescTuple.get(i);
168 addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
173 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
174 NTuple<Descriptor> endTuple) {
176 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
177 fromNode.addOutEdge(edge);
179 // System.out.println("add a new edge=" + edge);
183 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
184 if (mapDescTupleToInferNode.containsKey(descTuple)) {
185 return mapDescTupleToInferNode.get(descTuple);
187 FlowNode node = createNewFlowNode(descTuple);
192 public FlowNode getThisVarNode() {
196 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
198 if (!mapDescTupleToInferNode.containsKey(tuple)) {
199 FlowNode node = new FlowNode(tuple, isParameter(tuple));
200 mapDescTupleToInferNode.put(tuple, node);
203 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
205 if (tuple.size() > 1) {
206 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
207 getFlowNode(baseTuple).addFieldNode(node);
210 // System.out.println("Creating new node=" + node);
213 return mapDescTupleToInferNode.get(tuple);
218 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
220 if (!mapDescTupleToInferNode.containsKey(tuple)) {
221 createNewFlowNode(tuple);
224 FlowNode node = mapDescTupleToInferNode.get(tuple);
225 node.setReturn(true);
227 returnNodeSet.add(node);
230 public Set<FlowNode> getReturnNodeSet() {
231 return returnNodeSet;
234 public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
235 Set<FlowNode> set = new HashSet<FlowNode>();
236 getReachableFlowNodeSet(fn, set);
240 private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
242 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
243 FlowEdge edge = (FlowEdge) iterator.next();
245 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
247 FlowNode dstNode = getFlowNode(edge.getEndTuple());
249 if (!visited.contains(dstNode)) {
250 visited.add(dstNode);
251 getReachableFlowNodeSet(dstNode, visited);
258 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
259 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
260 FlowEdge edge = (FlowEdge) iterator.next();
262 if (fn.getDescTuple().equals(edge.getInitTuple())) {
263 FlowNode dstNode = getFlowNode(edge.getEndTuple());
264 NTuple<Location> dstTuple = getLocationTuple(dstNode);
266 if (!visited.contains(dstTuple)) {
267 visited.add(dstTuple);
268 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
276 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
277 return getLocationTuple(getFlowNode(descTuple));
280 public NTuple<Location> getLocationTuple(FlowNode fn) {
282 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
283 NTuple<Descriptor> descTuple = fn.getDescTuple();
284 NTuple<Location> locTuple = new NTuple<Location>();
285 ClassDescriptor cd = null;
287 Descriptor localDesc = fn.getDescTuple().get(0);
288 if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
289 Location topLoc = new Location(md, Location.TOP);
290 topLoc.setLocDescriptor(LocationInference.TOPDESC);
291 locTuple.add(topLoc);
292 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
293 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
294 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
295 locTuple.add(globalLoc);
298 for (int i = 0; i < descTuple.size(); i++) {
299 Descriptor curDesc = descTuple.get(i);
302 loc = new Location(md, curDesc.getSymbol());
303 loc.setLocDescriptor(curDesc);
304 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
306 loc = new Location(cd, curDesc.getSymbol());
307 loc.setLocDescriptor(curDesc);
308 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
314 mapFlowNodeToLocTuple.put(fn, locTuple);
316 return mapFlowNodeToLocTuple.get(fn);
319 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
320 Set<FlowNode> set = new HashSet<FlowNode>();
321 getIncomingFlowNodeSet(node, set);
325 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
327 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
328 FlowNode curNode = (FlowNode) iterator.next();
329 Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
331 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
332 FlowEdge flowEdge = (FlowEdge) iterator2.next();
334 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
335 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
337 if (!visited.contains(incomingNode)) {
338 visited.add(incomingNode);
339 getIncomingFlowNodeSet(incomingNode, visited);
347 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
349 NTuple<Descriptor> dstTuple = fn.getDescTuple();
351 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
353 ClassDescriptor cd = null;
355 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
356 FlowNode node = (FlowNode) iterator.next();
357 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
358 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
359 FlowEdge flowEdge = (FlowEdge) iterator2.next();
360 if (dstTuple.equals(flowEdge.getEndTuple())) {
361 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
362 NTuple<Location> locTuple = new NTuple<Location>();
363 for (int i = 0; i < initTuple.size(); i++) {
364 Descriptor d = initTuple.get(i);
367 loc = new Location(md, d.getSymbol());
368 cd = ((VarDescriptor) d).getType().getClassDesc();
370 loc = new Location(cd, d.getSymbol());
371 cd = ((FieldDescriptor) d).getType().getClassDesc();
382 public boolean isParameter(NTuple<Descriptor> tuple) {
383 // return true if a descriptor tuple is started with a parameter descriptor
384 Descriptor firstIdxDesc = tuple.get(0);
385 return mapParamDescToIdx.containsKey(firstIdxDesc);
388 public int getParamIdx(NTuple<Descriptor> tuple) {
389 Descriptor firstDesc = tuple.get(0);
390 return mapParamDescToIdx.get(firstDesc).intValue();
393 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
394 Set<FlowEdge> addedEdgeSet) throws IOException {
396 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
398 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
399 FlowEdge flowEdge = iterator.next();
401 FlowNode u = flowEdge.getSrc();
402 FlowNode v = flowEdge.getDst();
404 if (u.getDescTuple().equals(flowEdge.getInitTuple())
405 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
406 // only draw an edge of the actual value flow
408 if (!addedEdgeSet.contains(flowEdge)) {
410 if (!addedNodeSet.contains(u)) {
414 if (!addedNodeSet.contains(v)) {
419 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
420 addedEdgeSet.add(flowEdge);
428 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
429 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
432 public void writeGraph() throws java.io.IOException {
434 String graphName = "flowgraph_" + md.toString();
435 graphName = graphName.replaceAll("[\\W]", "");
437 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
438 bw.write("digraph " + graphName + " {\n");
439 bw.write("compound=true;\n");
441 // then visit every flow node
443 Iterator<FlowNode> iter = nodeSet.iterator();
445 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
446 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
448 while (iter.hasNext()) {
449 FlowNode node = iter.next();
451 if (node.getDescTuple().size() == 1) {
452 // here, we just care about the local variable
453 if (node.getFieldNodeSet().size() > 0) {
454 drawSubgraph(node, bw, addedEdgeSet);
457 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
466 public boolean constainsNode(FlowNode node) {
467 return nodeSet.contains(node);
470 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
473 bw.write("subgraph cluster_" + node.getID() + "{\n");
474 bw.write("label=\"" + node.getPrettyID() + "\";\n");
476 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
477 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
478 FlowNode fieldNode = (FlowNode) iterator.next();
479 if (fieldNode.getFieldNodeSet().size() > 0) {
480 drawSubgraph(fieldNode, bw, addedSet);
482 Descriptor desc = fieldNode.getDescTuple().getLastElement();
483 if (desc instanceof VarDescriptor) {
484 VarDescriptor varDesc = (VarDescriptor) desc;
485 if (varDesc.getType().isPrimitive()) {
486 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
488 } else if (desc instanceof FieldDescriptor) {
489 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
490 if (fieldDesc.getType().isPrimitive()) {
491 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");