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
38   Map<Integer, FlowNode> mapIdxToFlowNode;
39
40   boolean debug = true;
41
42   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
43     this.md = md;
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>();
53
54     if (!md.isStatic()) {
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;
63     }
64
65     setupMapIdxToDesc();
66
67   }
68
69   private void setupMapIdxToDesc() {
70
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));
78     }
79
80   }
81
82   public FlowNode getParamFlowNode(int idx) {
83     return mapIdxToFlowNode.get(idx);
84   }
85
86   public Set<FlowNode> getNodeSet() {
87     return nodeSet;
88   }
89
90   public MethodDescriptor getMethodDescriptor() {
91     return md;
92   }
93
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()) {
99         paramNodeSet.add(fn);
100       }
101     }
102     return paramNodeSet;
103   }
104
105   public void addNeighbor(FlowNode node, FlowNode neighbor) {
106     Set<FlowNode> set = mapNodeToNeighborSet.get(node);
107     if (set == null) {
108       set = new HashSet<FlowNode>();
109     }
110     set.add(neighbor);
111
112     // System.out.println("add a new neighbor " + neighbor + " to " + node);
113   }
114
115   public boolean isParamDesc(Descriptor desc) {
116
117     if (mapParamDescToIdx.containsKey(desc)) {
118       int idx = mapParamDescToIdx.get(desc);
119       if (!md.isStatic() && idx == 0) {
120         return false;
121       }
122       return true;
123     }
124
125     return false;
126   }
127
128   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
129
130     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
131     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
132
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)) {
137         return true;
138       } else {
139         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
140           return true;
141         }
142       }
143     }
144
145     return false;
146   }
147
148   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
149
150     FlowNode fromNode = getFlowNode(fromDescTuple);
151     FlowNode toNode = getFlowNode(toDescTuple);
152
153     // System.out.println("create an edge from " + fromNode + " to " + toNode);
154
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);
159       curTuple.add(desc);
160       addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
161     }
162
163     int toTupleSize = toDescTuple.size();
164     curTuple = new NTuple<Descriptor>();
165     for (int i = 0; i < toTupleSize; i++) {
166       Descriptor desc = toDescTuple.get(i);
167       curTuple.add(desc);
168       addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
169     }
170
171   }
172
173   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
174       NTuple<Descriptor> endTuple) {
175
176     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
177     fromNode.addOutEdge(edge);
178
179     // System.out.println("add a new edge=" + edge);
180
181   }
182
183   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
184     if (mapDescTupleToInferNode.containsKey(descTuple)) {
185       return mapDescTupleToInferNode.get(descTuple);
186     } else {
187       FlowNode node = createNewFlowNode(descTuple);
188       return node;
189     }
190   }
191
192   public FlowNode getThisVarNode() {
193     return thisVarNode;
194   }
195
196   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
197
198     if (!mapDescTupleToInferNode.containsKey(tuple)) {
199       FlowNode node = new FlowNode(tuple, isParameter(tuple));
200       mapDescTupleToInferNode.put(tuple, node);
201       nodeSet.add(node);
202
203       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
204
205       if (tuple.size() > 1) {
206         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
207         getFlowNode(baseTuple).addFieldNode(node);
208       }
209
210       // System.out.println("Creating new node=" + node);
211       return node;
212     } else {
213       return mapDescTupleToInferNode.get(tuple);
214     }
215
216   }
217
218   public void addReturnFlowNode(NTuple<Descriptor> tuple) {
219
220     if (!mapDescTupleToInferNode.containsKey(tuple)) {
221       createNewFlowNode(tuple);
222     }
223
224     FlowNode node = mapDescTupleToInferNode.get(tuple);
225     node.setReturn(true);
226
227     returnNodeSet.add(node);
228   }
229
230   public Set<FlowNode> getReturnNodeSet() {
231     return returnNodeSet;
232   }
233
234   public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
235     Set<FlowNode> set = new HashSet<FlowNode>();
236     getReachableFlowNodeSet(fn, set);
237     return set;
238   }
239
240   private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
241
242     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
243       FlowEdge edge = (FlowEdge) iterator.next();
244
245       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
246
247         FlowNode dstNode = getFlowNode(edge.getEndTuple());
248
249         if (!visited.contains(dstNode)) {
250           visited.add(dstNode);
251           getReachableFlowNodeSet(dstNode, visited);
252         }
253       }
254     }
255
256   }
257
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();
261
262       if (fn.getDescTuple().equals(edge.getInitTuple())) {
263         FlowNode dstNode = getFlowNode(edge.getEndTuple());
264         NTuple<Location> dstTuple = getLocationTuple(dstNode);
265
266         if (!visited.contains(dstTuple)) {
267           visited.add(dstTuple);
268           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
269         }
270
271       }
272     }
273     return visited;
274   }
275
276   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
277     return getLocationTuple(getFlowNode(descTuple));
278   }
279
280   public NTuple<Location> getLocationTuple(FlowNode fn) {
281
282     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
283       NTuple<Descriptor> descTuple = fn.getDescTuple();
284       NTuple<Location> locTuple = new NTuple<Location>();
285       ClassDescriptor cd = null;
286
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);
296       } else {
297         // normal case
298         for (int i = 0; i < descTuple.size(); i++) {
299           Descriptor curDesc = descTuple.get(i);
300           Location loc;
301           if (i == 0) {
302             loc = new Location(md, curDesc.getSymbol());
303             loc.setLocDescriptor(curDesc);
304             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
305           } else {
306             loc = new Location(cd, curDesc.getSymbol());
307             loc.setLocDescriptor(curDesc);
308             cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
309           }
310           locTuple.add(loc);
311         }
312       }
313
314       mapFlowNodeToLocTuple.put(fn, locTuple);
315     }
316     return mapFlowNodeToLocTuple.get(fn);
317   }
318
319   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
320     Set<FlowNode> set = new HashSet<FlowNode>();
321     getIncomingFlowNodeSet(node, set);
322     return set;
323   }
324
325   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
326
327     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
328       FlowNode curNode = (FlowNode) iterator.next();
329       Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
330
331       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
332         FlowEdge flowEdge = (FlowEdge) iterator2.next();
333
334         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
335           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
336
337           if (!visited.contains(incomingNode)) {
338             visited.add(incomingNode);
339             getIncomingFlowNodeSet(incomingNode, visited);
340           }
341         }
342       }
343     }
344
345   }
346
347   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
348
349     NTuple<Descriptor> dstTuple = fn.getDescTuple();
350
351     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
352
353     ClassDescriptor cd = null;
354
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);
365             Location loc;
366             if (i == 0) {
367               loc = new Location(md, d.getSymbol());
368               cd = ((VarDescriptor) d).getType().getClassDesc();
369             } else {
370               loc = new Location(cd, d.getSymbol());
371               cd = ((FieldDescriptor) d).getType().getClassDesc();
372             }
373             locTuple.add(loc);
374           }
375           set.add(locTuple);
376         }
377       }
378     }
379     return set;
380   }
381
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);
386   }
387
388   public int getParamIdx(NTuple<Descriptor> tuple) {
389     Descriptor firstDesc = tuple.get(0);
390     return mapParamDescToIdx.get(firstDesc).intValue();
391   }
392
393   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
394       Set<FlowEdge> addedEdgeSet) throws IOException {
395
396     Set<FlowEdge> edgeSet = node.getOutEdgeSet();
397
398     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
399       FlowEdge flowEdge = iterator.next();
400
401       FlowNode u = flowEdge.getSrc();
402       FlowNode v = flowEdge.getDst();
403
404       if (u.getDescTuple().equals(flowEdge.getInitTuple())
405           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
406         // only draw an edge of the actual value flow
407
408         if (!addedEdgeSet.contains(flowEdge)) {
409
410           if (!addedNodeSet.contains(u)) {
411             drawNode(u, bw);
412             addedNodeSet.add(u);
413           }
414           if (!addedNodeSet.contains(v)) {
415             drawNode(v, bw);
416             addedNodeSet.add(v);
417           }
418
419           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
420           addedEdgeSet.add(flowEdge);
421         }
422       }
423
424     }
425
426   }
427
428   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
429     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
430   }
431
432   public void writeGraph() throws java.io.IOException {
433
434     String graphName = "flowgraph_" + md.toString();
435     graphName = graphName.replaceAll("[\\W]", "");
436
437     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
438     bw.write("digraph " + graphName + " {\n");
439     bw.write("compound=true;\n");
440
441     // then visit every flow node
442
443     Iterator<FlowNode> iter = nodeSet.iterator();
444
445     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
446     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
447
448     while (iter.hasNext()) {
449       FlowNode node = iter.next();
450
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);
455         }
456       }
457       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
458
459     }
460
461     bw.write("}\n");
462     bw.close();
463
464   }
465
466   public boolean constainsNode(FlowNode node) {
467     return nodeSet.contains(node);
468   }
469
470   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
471       throws IOException {
472
473     bw.write("subgraph cluster_" + node.getID() + "{\n");
474     bw.write("label=\"" + node.getPrettyID() + "\";\n");
475
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);
481       } else {
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");
487           }
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");
492           }
493         }
494       }
495     }
496
497     bw.write("}\n");
498   }
499 }