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 // maps the composite representation of field/var descriptors to infer nodes
27 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
29 // maps an infer node to the set of neighbors which is pointed by the node
30 Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
32 // maps a paramter descriptor to its index
33 Map<Descriptor, Integer> mapParamDescToIdx;
36 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
38 this.nodeSet = new HashSet<FlowNode>();
39 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
40 this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
41 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
42 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
43 this.returnNodeSet = new HashSet<FlowNode>();
45 // create a node for 'this' varialbe
46 NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
47 thisDescTuple.add(md.getThis());
48 FlowNode thisNode = new FlowNode(thisDescTuple, true);
49 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
50 thisVarTuple.add(md.getThis());
51 createNewFlowNode(thisVarTuple);
52 thisVarNode = thisNode;
56 public Set<FlowNode> getNodeSet() {
60 public MethodDescriptor getMethodDescriptor() {
64 public Set<FlowNode> getParameterNodeSet() {
65 Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
66 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
67 FlowNode fn = (FlowNode) iterator.next();
68 if (fn.isParameter()) {
75 public void addNeighbor(FlowNode node, FlowNode neighbor) {
76 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
78 set = new HashSet<FlowNode>();
82 System.out.println("add a new neighbor " + neighbor + " to " + node);
85 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
87 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
88 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
90 Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
91 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
92 FlowEdge flowEdge = (FlowEdge) iterator.next();
93 if (flowEdge.getDst().equals(toNode)) {
96 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
105 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
107 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
108 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
110 System.out.println("create an edge from " + fromNode + " to " + toNode);
112 int fromTupleSize = fromDescTuple.size();
113 NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
114 for (int i = 0; i < fromTupleSize; i++) {
115 Descriptor desc = fromDescTuple.get(i);
117 addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
120 int toTupleSize = toDescTuple.size();
121 curTuple = new NTuple<Descriptor>();
122 for (int i = 0; i < toTupleSize; i++) {
123 Descriptor desc = toDescTuple.get(i);
125 addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
130 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
131 NTuple<Descriptor> endTuple) {
133 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
134 fromNode.addOutEdge(edge);
136 System.out.println("add a new edge=" + edge);
140 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
141 if (mapDescTupleToInferNode.containsKey(descTuple)) {
142 return mapDescTupleToInferNode.get(descTuple);
144 FlowNode node = createNewFlowNode(descTuple);
149 public FlowNode getThisVarNode() {
153 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
155 if (!mapDescTupleToInferNode.containsKey(tuple)) {
157 FlowNode node = new FlowNode(tuple, isParamter(tuple));
158 mapDescTupleToInferNode.put(tuple, node);
161 if (tuple.size() > 1) {
162 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
163 getFlowNode(baseTuple).addFieldNode(node);
166 System.out.println("Creating new node=" + node);
169 return mapDescTupleToInferNode.get(tuple);
174 public void setReturnFlowNode(NTuple<Descriptor> tuple) {
176 if (!mapDescTupleToInferNode.containsKey(tuple)) {
177 createNewFlowNode(tuple);
180 FlowNode node = mapDescTupleToInferNode.get(tuple);
181 node.setReturn(true);
183 returnNodeSet.add(node);
186 public Set<FlowNode> getReturnNodeSet() {
187 return returnNodeSet;
190 public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
191 Set<FlowNode> set = new HashSet<FlowNode>();
192 getReachableFlowNodeSet(fn, set);
196 private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
198 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
199 FlowEdge edge = (FlowEdge) iterator.next();
201 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
203 FlowNode dstNode = getFlowNode(edge.getEndTuple());
205 if (!visited.contains(dstNode)) {
206 visited.add(dstNode);
207 getReachableFlowNodeSet(dstNode, visited);
214 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
215 for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
216 FlowEdge edge = (FlowEdge) iterator.next();
218 if (fn.getDescTuple().equals(edge.getInitTuple())) {
219 FlowNode dstNode = getFlowNode(edge.getEndTuple());
220 NTuple<Location> dstTuple = getLocationTuple(dstNode);
222 if (!visited.contains(dstTuple)) {
223 visited.add(dstTuple);
224 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
232 public NTuple<Location> getLocationTuple(FlowNode fn) {
234 NTuple<Descriptor> descTuple = fn.getDescTuple();
236 NTuple<Location> locTuple = new NTuple<Location>();
238 ClassDescriptor cd = null;
240 for (int i = 0; i < descTuple.size(); i++) {
241 Descriptor d = descTuple.get(i);
244 loc = new Location(md, d.getSymbol());
245 cd = ((VarDescriptor) d).getType().getClassDesc();
247 loc = new Location(cd, d.getSymbol());
248 cd = ((FieldDescriptor) d).getType().getClassDesc();
256 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
257 Set<FlowNode> set = new HashSet<FlowNode>();
258 getIncomingFlowNodeSet(node, set);
262 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
264 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
265 FlowNode curNode = (FlowNode) iterator.next();
266 Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
268 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
269 FlowEdge flowEdge = (FlowEdge) iterator2.next();
271 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
272 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
274 if (!visited.contains(incomingNode)) {
275 visited.add(incomingNode);
276 getIncomingFlowNodeSet(incomingNode, visited);
284 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
286 NTuple<Descriptor> dstTuple = fn.getDescTuple();
288 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
290 ClassDescriptor cd = null;
292 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
293 FlowNode node = (FlowNode) iterator.next();
294 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
295 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
296 FlowEdge flowEdge = (FlowEdge) iterator2.next();
297 if (dstTuple.equals(flowEdge.getEndTuple())) {
298 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
299 NTuple<Location> locTuple = new NTuple<Location>();
300 for (int i = 0; i < initTuple.size(); i++) {
301 Descriptor d = initTuple.get(i);
304 loc = new Location(md, d.getSymbol());
305 cd = ((VarDescriptor) d).getType().getClassDesc();
307 loc = new Location(cd, d.getSymbol());
308 cd = ((FieldDescriptor) d).getType().getClassDesc();
319 public boolean isParamter(NTuple<Descriptor> tuple) {
320 // return true if a descriptor tuple is started with a parameter descriptor
321 Descriptor firstIdxDesc = tuple.get(0);
322 return mapParamDescToIdx.containsKey(firstIdxDesc);
325 public int getParamIdx(NTuple<Descriptor> tuple) {
326 Descriptor firstDesc = tuple.get(0);
327 return mapParamDescToIdx.get(firstDesc).intValue();
330 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
331 Set<FlowEdge> addedEdgeSet) throws IOException {
333 Set<FlowEdge> edgeSet = node.getOutEdgeSet();
335 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
336 FlowEdge flowEdge = iterator.next();
338 FlowNode u = flowEdge.getSrc();
339 FlowNode v = flowEdge.getDst();
341 if (u.getDescTuple().equals(flowEdge.getInitTuple())
342 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
343 // only draw an edge of the actual value flow
345 if (!addedEdgeSet.contains(flowEdge)) {
347 if (!addedNodeSet.contains(u)) {
351 if (!addedNodeSet.contains(v)) {
356 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
357 addedEdgeSet.add(flowEdge);
365 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
366 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
369 public void writeGraph() throws java.io.IOException {
371 String graphName = "flowgraph_" + md.toString();
372 graphName = graphName.replaceAll("[\\W]", "");
374 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
375 bw.write("digraph " + graphName + " {\n");
376 bw.write("compound=true;\n");
378 // then visit every flow node
380 Iterator<FlowNode> iter = nodeSet.iterator();
382 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
383 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
385 while (iter.hasNext()) {
386 FlowNode node = iter.next();
388 if (node.getDescTuple().size() == 1) {
389 // here, we just care about the local variable
390 if (node.getFieldNodeSet().size() > 0) {
391 drawSubgraph(node, bw, addedEdgeSet);
394 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
403 public boolean constainsNode(FlowNode node) {
404 return nodeSet.contains(node);
407 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
410 bw.write("subgraph cluster_" + node.getID() + "{\n");
411 bw.write("label=\"" + node.getPrettyID() + "\";\n");
413 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
414 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
415 FlowNode fieldNode = (FlowNode) iterator.next();
416 if (fieldNode.getFieldNodeSet().size() > 0) {
417 drawSubgraph(fieldNode, bw, addedSet);
419 Descriptor desc = fieldNode.getDescTuple().getLastElement();
420 if (desc instanceof VarDescriptor) {
421 VarDescriptor varDesc = (VarDescriptor) desc;
422 if (varDesc.getType().isPrimitive()) {
423 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
425 } else if (desc instanceof FieldDescriptor) {
426 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
427 if (fieldDesc.getType().isPrimitive()) {
428 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");