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;
39 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
41 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
42 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
43 this.nodeSet = new HashSet<FlowNode>();
44 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
45 this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
46 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
47 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
48 this.returnNodeSet = new HashSet<FlowNode>();
51 // create a node for 'this' varialbe
52 NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
53 thisDescTuple.add(md.getThis());
54 FlowNode thisNode = new FlowNode(thisDescTuple, true);
55 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
56 thisVarTuple.add(md.getThis());
57 createNewFlowNode(thisVarTuple);
58 thisVarNode = thisNode;
63 public Set<FlowNode> getNodeSet() {
67 public MethodDescriptor getMethodDescriptor() {
71 public Set<FlowNode> getParameterNodeSet() {
72 Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
73 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
74 FlowNode fn = (FlowNode) iterator.next();
75 if (fn.isParameter()) {
82 public void addNeighbor(FlowNode node, FlowNode neighbor) {
83 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
85 set = new HashSet<FlowNode>();
89 // System.out.println("add a new neighbor " + neighbor + " to " + node);
92 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
94 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
95 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
97 Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
98 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
99 FlowEdge flowEdge = (FlowEdge) iterator.next();
100 if (flowEdge.getDst().equals(toNode)) {
103 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
112 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
114 FlowNode fromNode = getFlowNode(fromDescTuple);
115 FlowNode toNode = getFlowNode(toDescTuple);
117 // System.out.println("create an edge from " + fromNode + " to " + toNode);
119 int fromTupleSize = fromDescTuple.size();
120 NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
121 for (int i = 0; i < fromTupleSize; i++) {
122 Descriptor desc = fromDescTuple.get(i);
124 addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
127 int toTupleSize = toDescTuple.size();
128 curTuple = new NTuple<Descriptor>();
129 for (int i = 0; i < toTupleSize; i++) {
130 Descriptor desc = toDescTuple.get(i);
132 addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
137 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
138 NTuple<Descriptor> endTuple) {
140 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
141 fromNode.addOutEdge(edge);
143 // System.out.println("add a new edge=" + edge);
147 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
148 if (mapDescTupleToInferNode.containsKey(descTuple)) {
149 return mapDescTupleToInferNode.get(descTuple);
151 FlowNode node = createNewFlowNode(descTuple);
156 public FlowNode getThisVarNode() {
160 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
162 if (!mapDescTupleToInferNode.containsKey(tuple)) {
164 FlowNode node = new FlowNode(tuple, isParameter(tuple));
165 mapDescTupleToInferNode.put(tuple, node);
168 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
170 if (tuple.size() > 1) {
171 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
172 getFlowNode(baseTuple).addFieldNode(node);
175 // System.out.println("Creating new node=" + node);
178 return mapDescTupleToInferNode.get(tuple);
183 public void setReturnFlowNode(NTuple<Descriptor> tuple) {
185 if (!mapDescTupleToInferNode.containsKey(tuple)) {
186 createNewFlowNode(tuple);
189 FlowNode node = mapDescTupleToInferNode.get(tuple);
190 node.setReturn(true);
192 returnNodeSet.add(node);
195 public Set<FlowNode> getReturnNodeSet() {
196 return returnNodeSet;
199 public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
200 Set<FlowNode> set = new HashSet<FlowNode>();
201 getReachableFlowNodeSet(fn, set);
205 private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
207 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
208 FlowEdge edge = (FlowEdge) iterator.next();
210 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
212 FlowNode dstNode = getFlowNode(edge.getEndTuple());
214 if (!visited.contains(dstNode)) {
215 visited.add(dstNode);
216 getReachableFlowNodeSet(dstNode, visited);
223 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
224 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
225 FlowEdge edge = (FlowEdge) iterator.next();
227 if (fn.getDescTuple().equals(edge.getInitTuple())) {
228 FlowNode dstNode = getFlowNode(edge.getEndTuple());
229 NTuple<Location> dstTuple = getLocationTuple(dstNode);
231 if (!visited.contains(dstTuple)) {
232 visited.add(dstTuple);
233 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
241 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
242 return getLocationTuple(getFlowNode(descTuple));
245 public NTuple<Location> getLocationTuple(FlowNode fn) {
247 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
248 NTuple<Descriptor> descTuple = fn.getDescTuple();
249 NTuple<Location> locTuple = new NTuple<Location>();
250 ClassDescriptor cd = null;
252 Descriptor localDesc = fn.getDescTuple().get(0);
253 if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
254 Location topLoc = new Location(md, Location.TOP);
255 locTuple.add(topLoc);
256 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
257 Location topLoc = new Location(md, LocationInference.GLOBALLOC);
258 locTuple.add(topLoc);
261 for (int i = 0; i < descTuple.size(); i++) {
262 Descriptor curDesc = descTuple.get(i);
265 loc = new Location(md, curDesc.getSymbol());
266 loc.setLocDescriptor(curDesc);
267 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
269 loc = new Location(cd, curDesc.getSymbol());
270 loc.setLocDescriptor(curDesc);
271 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
277 mapFlowNodeToLocTuple.put(fn, locTuple);
279 return mapFlowNodeToLocTuple.get(fn);
282 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
283 Set<FlowNode> set = new HashSet<FlowNode>();
284 getIncomingFlowNodeSet(node, set);
288 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
290 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
291 FlowNode curNode = (FlowNode) iterator.next();
292 Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
294 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
295 FlowEdge flowEdge = (FlowEdge) iterator2.next();
297 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
298 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
300 if (!visited.contains(incomingNode)) {
301 visited.add(incomingNode);
302 getIncomingFlowNodeSet(incomingNode, visited);
310 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
312 NTuple<Descriptor> dstTuple = fn.getDescTuple();
314 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
316 ClassDescriptor cd = null;
318 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
319 FlowNode node = (FlowNode) iterator.next();
320 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
321 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
322 FlowEdge flowEdge = (FlowEdge) iterator2.next();
323 if (dstTuple.equals(flowEdge.getEndTuple())) {
324 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
325 NTuple<Location> locTuple = new NTuple<Location>();
326 for (int i = 0; i < initTuple.size(); i++) {
327 Descriptor d = initTuple.get(i);
330 loc = new Location(md, d.getSymbol());
331 cd = ((VarDescriptor) d).getType().getClassDesc();
333 loc = new Location(cd, d.getSymbol());
334 cd = ((FieldDescriptor) d).getType().getClassDesc();
345 public boolean isParameter(NTuple<Descriptor> tuple) {
346 // return true if a descriptor tuple is started with a parameter descriptor
347 Descriptor firstIdxDesc = tuple.get(0);
348 return mapParamDescToIdx.containsKey(firstIdxDesc);
351 public int getParamIdx(NTuple<Descriptor> tuple) {
352 Descriptor firstDesc = tuple.get(0);
353 return mapParamDescToIdx.get(firstDesc).intValue();
356 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
357 Set<FlowEdge> addedEdgeSet) throws IOException {
359 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
361 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
362 FlowEdge flowEdge = iterator.next();
364 FlowNode u = flowEdge.getSrc();
365 FlowNode v = flowEdge.getDst();
367 if (u.getDescTuple().equals(flowEdge.getInitTuple())
368 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
369 // only draw an edge of the actual value flow
371 if (!addedEdgeSet.contains(flowEdge)) {
373 if (!addedNodeSet.contains(u)) {
377 if (!addedNodeSet.contains(v)) {
382 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
383 addedEdgeSet.add(flowEdge);
391 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
392 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
395 public void writeGraph() throws java.io.IOException {
397 String graphName = "flowgraph_" + md.toString();
398 graphName = graphName.replaceAll("[\\W]", "");
400 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
401 bw.write("digraph " + graphName + " {\n");
402 bw.write("compound=true;\n");
404 // then visit every flow node
406 Iterator<FlowNode> iter = nodeSet.iterator();
408 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
409 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
411 while (iter.hasNext()) {
412 FlowNode node = iter.next();
414 if (node.getDescTuple().size() == 1) {
415 // here, we just care about the local variable
416 if (node.getFieldNodeSet().size() > 0) {
417 drawSubgraph(node, bw, addedEdgeSet);
420 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
429 public boolean constainsNode(FlowNode node) {
430 return nodeSet.contains(node);
433 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
436 bw.write("subgraph cluster_" + node.getID() + "{\n");
437 bw.write("label=\"" + node.getPrettyID() + "\";\n");
439 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
440 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
441 FlowNode fieldNode = (FlowNode) iterator.next();
442 if (fieldNode.getFieldNodeSet().size() > 0) {
443 drawSubgraph(fieldNode, bw, addedSet);
445 Descriptor desc = fieldNode.getDescTuple().getLastElement();
446 if (desc instanceof VarDescriptor) {
447 VarDescriptor varDesc = (VarDescriptor) desc;
448 if (varDesc.getType().isPrimitive()) {
449 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
451 } else if (desc instanceof FieldDescriptor) {
452 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
453 if (fieldDesc.getType().isPrimitive()) {
454 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");