changes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
1 package Analysis.SSJava;
2
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;
9 import java.util.Map;
10 import java.util.Set;
11
12 import IR.ClassDescriptor;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.VarDescriptor;
17
18 public class FlowGraph {
19
20   MethodDescriptor md;
21
22   Set<FlowNode> nodeSet;
23   Set<FlowNode> returnNodeSet;
24   FlowNode thisVarNode;
25
26   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
27   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
28
29   // maps the composite representation of field/var descriptors to infer nodes
30   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
31
32   // maps an infer node to the set of neighbors which is pointed by the node
33   Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
34
35   // maps a paramter descriptor to its index
36   Map<Descriptor, Integer> mapParamDescToIdx;
37   boolean debug = true;
38
39   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
40     this.md = md;
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>();
49
50     if (!md.isStatic()) {
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;
59     }
60
61   }
62
63   public Set<FlowNode> getNodeSet() {
64     return nodeSet;
65   }
66
67   public MethodDescriptor getMethodDescriptor() {
68     return md;
69   }
70
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()) {
76         paramNodeSet.add(fn);
77       }
78     }
79     return paramNodeSet;
80   }
81
82   public void addNeighbor(FlowNode node, FlowNode neighbor) {
83     Set<FlowNode> set = mapNodeToNeighborSet.get(node);
84     if (set == null) {
85       set = new HashSet<FlowNode>();
86     }
87     set.add(neighbor);
88
89     System.out.println("add a new neighbor " + neighbor + " to " + node);
90   }
91
92   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
93
94     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
95     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
96
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)) {
101         return true;
102       } else {
103         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
104           return true;
105         }
106       }
107     }
108
109     return false;
110   }
111
112   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
113
114     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
115     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
116
117     System.out.println("create an edge from " + fromNode + " to " + toNode);
118
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);
123       curTuple.add(desc);
124       addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
125     }
126
127     int toTupleSize = toDescTuple.size();
128     curTuple = new NTuple<Descriptor>();
129     for (int i = 0; i < toTupleSize; i++) {
130       Descriptor desc = toDescTuple.get(i);
131       curTuple.add(desc);
132       addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
133     }
134
135   }
136
137   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
138       NTuple<Descriptor> endTuple) {
139
140     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
141     fromNode.addOutEdge(edge);
142
143     System.out.println("add a new edge=" + edge);
144
145   }
146
147   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
148     if (mapDescTupleToInferNode.containsKey(descTuple)) {
149       return mapDescTupleToInferNode.get(descTuple);
150     } else {
151       FlowNode node = createNewFlowNode(descTuple);
152       return node;
153     }
154   }
155
156   public FlowNode getThisVarNode() {
157     return thisVarNode;
158   }
159
160   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
161
162     if (!mapDescTupleToInferNode.containsKey(tuple)) {
163
164       FlowNode node = new FlowNode(tuple, isParamter(tuple));
165       mapDescTupleToInferNode.put(tuple, node);
166       nodeSet.add(node);
167
168       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
169
170       if (tuple.size() > 1) {
171         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
172         getFlowNode(baseTuple).addFieldNode(node);
173       }
174
175       System.out.println("Creating new node=" + node);
176       return node;
177     } else {
178       return mapDescTupleToInferNode.get(tuple);
179     }
180
181   }
182
183   public void setReturnFlowNode(NTuple<Descriptor> tuple) {
184
185     if (!mapDescTupleToInferNode.containsKey(tuple)) {
186       createNewFlowNode(tuple);
187     }
188
189     FlowNode node = mapDescTupleToInferNode.get(tuple);
190     node.setReturn(true);
191
192     returnNodeSet.add(node);
193   }
194
195   public Set<FlowNode> getReturnNodeSet() {
196     return returnNodeSet;
197   }
198
199   public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
200     Set<FlowNode> set = new HashSet<FlowNode>();
201     getReachableFlowNodeSet(fn, set);
202     return set;
203   }
204
205   private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
206
207     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
208       FlowEdge edge = (FlowEdge) iterator.next();
209
210       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
211
212         FlowNode dstNode = getFlowNode(edge.getEndTuple());
213
214         if (!visited.contains(dstNode)) {
215           visited.add(dstNode);
216           getReachableFlowNodeSet(dstNode, visited);
217         }
218       }
219     }
220
221   }
222
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();
226
227       if (fn.getDescTuple().equals(edge.getInitTuple())) {
228         FlowNode dstNode = getFlowNode(edge.getEndTuple());
229         NTuple<Location> dstTuple = getLocationTuple(dstNode);
230
231         if (!visited.contains(dstTuple)) {
232           visited.add(dstTuple);
233           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
234         }
235
236       }
237     }
238     return visited;
239   }
240
241   public NTuple<Location> getLocationTuple(FlowNode fn) {
242
243     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
244       NTuple<Descriptor> descTuple = fn.getDescTuple();
245       NTuple<Location> locTuple = new NTuple<Location>();
246       ClassDescriptor cd = null;
247
248       for (int i = 0; i < descTuple.size(); i++) {
249         Descriptor curDesc = descTuple.get(i);
250         Location loc;
251         if (i == 0) {
252           loc = new Location(md, curDesc.getSymbol());
253           loc.setLocDescriptor(md);
254           cd = ((VarDescriptor) curDesc).getType().getClassDesc();
255         } else {
256           loc = new Location(cd, curDesc.getSymbol());
257           loc.setLocDescriptor(curDesc);
258           cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
259         }
260         locTuple.add(loc);
261       }
262       mapFlowNodeToLocTuple.put(fn, locTuple);
263     }
264     return mapFlowNodeToLocTuple.get(fn);
265   }
266
267   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
268     Set<FlowNode> set = new HashSet<FlowNode>();
269     getIncomingFlowNodeSet(node, set);
270     return set;
271   }
272
273   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
274
275     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
276       FlowNode curNode = (FlowNode) iterator.next();
277       Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
278
279       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
280         FlowEdge flowEdge = (FlowEdge) iterator2.next();
281
282         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
283           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
284
285           if (!visited.contains(incomingNode)) {
286             visited.add(incomingNode);
287             getIncomingFlowNodeSet(incomingNode, visited);
288           }
289         }
290       }
291     }
292
293   }
294
295   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
296
297     NTuple<Descriptor> dstTuple = fn.getDescTuple();
298
299     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
300
301     ClassDescriptor cd = null;
302
303     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
304       FlowNode node = (FlowNode) iterator.next();
305       Set<FlowEdge> edgeSet = node.getOutEdgeSet();
306       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
307         FlowEdge flowEdge = (FlowEdge) iterator2.next();
308         if (dstTuple.equals(flowEdge.getEndTuple())) {
309           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
310           NTuple<Location> locTuple = new NTuple<Location>();
311           for (int i = 0; i < initTuple.size(); i++) {
312             Descriptor d = initTuple.get(i);
313             Location loc;
314             if (i == 0) {
315               loc = new Location(md, d.getSymbol());
316               cd = ((VarDescriptor) d).getType().getClassDesc();
317             } else {
318               loc = new Location(cd, d.getSymbol());
319               cd = ((FieldDescriptor) d).getType().getClassDesc();
320             }
321             locTuple.add(loc);
322           }
323           set.add(locTuple);
324         }
325       }
326     }
327     return set;
328   }
329
330   public boolean isParamter(NTuple<Descriptor> tuple) {
331     // return true if a descriptor tuple is started with a parameter descriptor
332     Descriptor firstIdxDesc = tuple.get(0);
333     return mapParamDescToIdx.containsKey(firstIdxDesc);
334   }
335
336   public int getParamIdx(NTuple<Descriptor> tuple) {
337     Descriptor firstDesc = tuple.get(0);
338     return mapParamDescToIdx.get(firstDesc).intValue();
339   }
340
341   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
342       Set<FlowEdge> addedEdgeSet) throws IOException {
343
344     Set<FlowEdge> edgeSet = node.getOutEdgeSet();
345
346     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
347       FlowEdge flowEdge = iterator.next();
348
349       FlowNode u = flowEdge.getSrc();
350       FlowNode v = flowEdge.getDst();
351
352       if (u.getDescTuple().equals(flowEdge.getInitTuple())
353           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
354         // only draw an edge of the actual value flow
355
356         if (!addedEdgeSet.contains(flowEdge)) {
357
358           if (!addedNodeSet.contains(u)) {
359             drawNode(u, bw);
360             addedNodeSet.add(u);
361           }
362           if (!addedNodeSet.contains(v)) {
363             drawNode(v, bw);
364             addedNodeSet.add(v);
365           }
366
367           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
368           addedEdgeSet.add(flowEdge);
369         }
370       }
371
372     }
373
374   }
375
376   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
377     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
378   }
379
380   public void writeGraph() throws java.io.IOException {
381
382     String graphName = "flowgraph_" + md.toString();
383     graphName = graphName.replaceAll("[\\W]", "");
384
385     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
386     bw.write("digraph " + graphName + " {\n");
387     bw.write("compound=true;\n");
388
389     // then visit every flow node
390
391     Iterator<FlowNode> iter = nodeSet.iterator();
392
393     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
394     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
395
396     while (iter.hasNext()) {
397       FlowNode node = iter.next();
398
399       if (node.getDescTuple().size() == 1) {
400         // here, we just care about the local variable
401         if (node.getFieldNodeSet().size() > 0) {
402           drawSubgraph(node, bw, addedEdgeSet);
403         }
404       }
405       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
406
407     }
408
409     bw.write("}\n");
410     bw.close();
411
412   }
413
414   public boolean constainsNode(FlowNode node) {
415     return nodeSet.contains(node);
416   }
417
418   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
419       throws IOException {
420
421     bw.write("subgraph cluster_" + node.getID() + "{\n");
422     bw.write("label=\"" + node.getPrettyID() + "\";\n");
423
424     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
425     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
426       FlowNode fieldNode = (FlowNode) iterator.next();
427       if (fieldNode.getFieldNodeSet().size() > 0) {
428         drawSubgraph(fieldNode, bw, addedSet);
429       } else {
430         Descriptor desc = fieldNode.getDescTuple().getLastElement();
431         if (desc instanceof VarDescriptor) {
432           VarDescriptor varDesc = (VarDescriptor) desc;
433           if (varDesc.getType().isPrimitive()) {
434             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
435           }
436         } else if (desc instanceof FieldDescriptor) {
437           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
438           if (fieldDesc.getType().isPrimitive()) {
439             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
440           }
441         }
442       }
443     }
444
445     bw.write("}\n");
446   }
447 }