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.NameDescriptor;
17 import IR.VarDescriptor;
18
19 public class FlowGraph {
20
21   MethodDescriptor md;
22
23   Set<FlowNode> nodeSet;
24   Set<FlowNode> returnNodeSet;
25   FlowNode thisVarNode;
26
27   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
28   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
29
30   // maps the composite representation of field/var descriptors to infer nodes
31   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
32
33   // maps an infer node to the set of neighbors which is pointed by the node
34   Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
35
36   // maps a paramter descriptor to its index
37   Map<Descriptor, Integer> mapParamDescToIdx;
38
39   Map<Integer, FlowNode> mapIdxToFlowNode;
40
41   public static int interseed = 0;
42
43   boolean debug = true;
44
45   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
46     this.md = md;
47     this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
48     this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
49     this.nodeSet = new HashSet<FlowNode>();
50     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
51     this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
52     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
53     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
54     this.returnNodeSet = new HashSet<FlowNode>();
55     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
56
57     if (!md.isStatic()) {
58       // create a node for 'this' varialbe
59       NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
60       thisDescTuple.add(md.getThis());
61       FlowNode thisNode = new FlowNode(thisDescTuple, true);
62       NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
63       thisVarTuple.add(md.getThis());
64       createNewFlowNode(thisVarTuple);
65       thisVarNode = thisNode;
66     }
67
68     setupMapIdxToDesc();
69
70   }
71
72   public FlowNode createIntermediateNode() {
73     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
74     Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
75     tuple.add(interDesc);
76     interseed++;
77     FlowNode node = createNewFlowNode(tuple, true);
78     return node;
79   }
80
81   private void setupMapIdxToDesc() {
82
83     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
84     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
85       Descriptor paramDesc = (Descriptor) iterator.next();
86       int idx = mapParamDescToIdx.get(paramDesc);
87       NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
88       descTuple.add(paramDesc);
89       mapIdxToFlowNode.put(idx, getFlowNode(descTuple));
90     }
91
92   }
93
94   public FlowNode getParamFlowNode(int idx) {
95     return mapIdxToFlowNode.get(idx);
96   }
97
98   public Set<FlowNode> getNodeSet() {
99     return nodeSet;
100   }
101
102   public MethodDescriptor getMethodDescriptor() {
103     return md;
104   }
105
106   public Set<FlowNode> getParameterNodeSet() {
107     Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
108     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
109       FlowNode fn = (FlowNode) iterator.next();
110       if (fn.isParameter()) {
111         paramNodeSet.add(fn);
112       }
113     }
114     return paramNodeSet;
115   }
116
117   public void addNeighbor(FlowNode node, FlowNode neighbor) {
118     Set<FlowNode> set = mapNodeToNeighborSet.get(node);
119     if (set == null) {
120       set = new HashSet<FlowNode>();
121     }
122     set.add(neighbor);
123
124     // System.out.println("add a new neighbor " + neighbor + " to " + node);
125   }
126
127   public boolean isParamDesc(Descriptor desc) {
128
129     if (mapParamDescToIdx.containsKey(desc)) {
130       int idx = mapParamDescToIdx.get(desc);
131       if (!md.isStatic() && idx == 0) {
132         return false;
133       }
134       return true;
135     }
136
137     return false;
138   }
139
140   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
141
142     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
143     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
144
145     Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
146     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
147       FlowEdge flowEdge = (FlowEdge) iterator.next();
148       if (flowEdge.getDst().equals(toNode)) {
149         return true;
150       } else {
151         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
152           return true;
153         }
154       }
155     }
156
157     return false;
158   }
159
160   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
161
162     FlowNode fromNode = getFlowNode(fromDescTuple);
163     FlowNode toNode = getFlowNode(toDescTuple);
164
165     // System.out.println("create an edge from " + fromNode + " to " + toNode);
166
167     int fromTupleSize = fromDescTuple.size();
168     NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
169     for (int i = 0; i < fromTupleSize; i++) {
170       Descriptor desc = fromDescTuple.get(i);
171       curTuple.add(desc);
172       addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
173     }
174
175     int toTupleSize = toDescTuple.size();
176     curTuple = new NTuple<Descriptor>();
177     for (int i = 0; i < toTupleSize; i++) {
178       Descriptor desc = toDescTuple.get(i);
179       curTuple.add(desc);
180       addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
181     }
182
183   }
184
185   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
186       NTuple<Descriptor> endTuple) {
187
188     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
189     fromNode.addOutEdge(edge);
190
191     // System.out.println("add a new edge=" + edge);
192
193   }
194
195   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
196     if (mapDescTupleToInferNode.containsKey(descTuple)) {
197       return mapDescTupleToInferNode.get(descTuple);
198     } else {
199       FlowNode node = createNewFlowNode(descTuple);
200       return node;
201     }
202   }
203
204   public FlowNode getThisVarNode() {
205     return thisVarNode;
206   }
207
208   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
209     return createNewFlowNode(tuple, false);
210   }
211
212   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple, boolean isIntermediate) {
213
214     if (!mapDescTupleToInferNode.containsKey(tuple)) {
215       FlowNode node = new FlowNode(tuple, isParameter(tuple));
216       node.setIntermediate(isIntermediate);
217       mapDescTupleToInferNode.put(tuple, node);
218       nodeSet.add(node);
219
220       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
221
222       if (tuple.size() > 1) {
223         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
224         getFlowNode(baseTuple).addFieldNode(node);
225       }
226
227       // System.out.println("Creating new node=" + node);
228       return node;
229     } else {
230       return mapDescTupleToInferNode.get(tuple);
231     }
232
233   }
234
235   public void addReturnFlowNode(NTuple<Descriptor> tuple) {
236
237     if (!mapDescTupleToInferNode.containsKey(tuple)) {
238       createNewFlowNode(tuple);
239     }
240
241     FlowNode node = mapDescTupleToInferNode.get(tuple);
242     node.setReturn(true);
243
244     returnNodeSet.add(node);
245   }
246
247   public Set<FlowNode> getReturnNodeSet() {
248     return returnNodeSet;
249   }
250
251   public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
252     Set<FlowNode> set = new HashSet<FlowNode>();
253     getReachableFlowNodeSet(fn, set);
254     return set;
255   }
256
257   private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
258
259     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
260       FlowEdge edge = (FlowEdge) iterator.next();
261
262       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
263
264         FlowNode dstNode = getFlowNode(edge.getEndTuple());
265
266         if (!visited.contains(dstNode)) {
267           visited.add(dstNode);
268           getReachableFlowNodeSet(dstNode, visited);
269         }
270       }
271     }
272
273   }
274
275   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
276     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
277       FlowEdge edge = (FlowEdge) iterator.next();
278
279       if (fn.getDescTuple().equals(edge.getInitTuple())) {
280         FlowNode dstNode = getFlowNode(edge.getEndTuple());
281         NTuple<Location> dstTuple = getLocationTuple(dstNode);
282
283         if (!visited.contains(dstTuple)) {
284           visited.add(dstTuple);
285           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
286         }
287
288       }
289     }
290     return visited;
291   }
292
293   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
294     return getLocationTuple(getFlowNode(descTuple));
295   }
296
297   public NTuple<Location> getLocationTuple(FlowNode fn) {
298
299     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
300       NTuple<Descriptor> descTuple = fn.getDescTuple();
301       NTuple<Location> locTuple = new NTuple<Location>();
302       ClassDescriptor cd = null;
303
304       Descriptor localDesc = fn.getDescTuple().get(0);
305
306       if (fn.isIntermediate()) {
307         Location interLoc = new Location(md, localDesc.getSymbol());
308         interLoc.setLocDescriptor(localDesc);
309         locTuple.add(interLoc);
310       } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
311         Location topLoc = new Location(md, Location.TOP);
312         topLoc.setLocDescriptor(LocationInference.TOPDESC);
313         locTuple.add(topLoc);
314       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
315         Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
316         globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
317         locTuple.add(globalLoc);
318       } else {
319         // normal case
320         for (int i = 0; i < descTuple.size(); i++) {
321           Descriptor curDesc = descTuple.get(i);
322           Location loc;
323           if (i == 0) {
324             loc = new Location(md, curDesc.getSymbol());
325             loc.setLocDescriptor(curDesc);
326             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
327           } else {
328             loc = new Location(cd, curDesc.getSymbol());
329             loc.setLocDescriptor(curDesc);
330             cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
331           }
332           locTuple.add(loc);
333         }
334       }
335
336       mapFlowNodeToLocTuple.put(fn, locTuple);
337     }
338     return mapFlowNodeToLocTuple.get(fn);
339   }
340
341   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
342     Set<FlowNode> set = new HashSet<FlowNode>();
343     getIncomingFlowNodeSet(node, set);
344     return set;
345   }
346
347   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
348
349     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
350       FlowNode curNode = (FlowNode) iterator.next();
351       Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
352
353       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
354         FlowEdge flowEdge = (FlowEdge) iterator2.next();
355
356         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
357           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
358
359           if (!visited.contains(incomingNode)) {
360             visited.add(incomingNode);
361             getIncomingFlowNodeSet(incomingNode, visited);
362           }
363         }
364       }
365     }
366
367   }
368
369   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
370
371     NTuple<Descriptor> dstTuple = fn.getDescTuple();
372
373     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
374
375     ClassDescriptor cd = null;
376
377     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
378       FlowNode node = (FlowNode) iterator.next();
379       Set<FlowEdge> edgeSet = node.getOutEdgeSet();
380       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
381         FlowEdge flowEdge = (FlowEdge) iterator2.next();
382         if (dstTuple.equals(flowEdge.getEndTuple())) {
383           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
384           NTuple<Location> locTuple = new NTuple<Location>();
385           for (int i = 0; i < initTuple.size(); i++) {
386             Descriptor d = initTuple.get(i);
387             Location loc;
388             if (i == 0) {
389               loc = new Location(md, d.getSymbol());
390               cd = ((VarDescriptor) d).getType().getClassDesc();
391             } else {
392               loc = new Location(cd, d.getSymbol());
393               cd = ((FieldDescriptor) d).getType().getClassDesc();
394             }
395             locTuple.add(loc);
396           }
397           set.add(locTuple);
398         }
399       }
400     }
401     return set;
402   }
403
404   public boolean isParameter(NTuple<Descriptor> tuple) {
405     // return true if a descriptor tuple is started with a parameter descriptor
406     Descriptor firstIdxDesc = tuple.get(0);
407     return mapParamDescToIdx.containsKey(firstIdxDesc);
408   }
409
410   public int getParamIdx(NTuple<Descriptor> tuple) {
411     Descriptor firstDesc = tuple.get(0);
412     return mapParamDescToIdx.get(firstDesc).intValue();
413   }
414
415   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
416       Set<FlowEdge> addedEdgeSet) throws IOException {
417
418     Set<FlowEdge> edgeSet = node.getOutEdgeSet();
419
420     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
421       FlowEdge flowEdge = iterator.next();
422
423       FlowNode u = flowEdge.getSrc();
424       FlowNode v = flowEdge.getDst();
425
426       if (u.getDescTuple().equals(flowEdge.getInitTuple())
427           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
428         // only draw an edge of the actual value flow
429
430         if (!addedEdgeSet.contains(flowEdge)) {
431
432           if (!addedNodeSet.contains(u)) {
433             drawNode(u, bw);
434             addedNodeSet.add(u);
435           }
436           if (!addedNodeSet.contains(v)) {
437             drawNode(v, bw);
438             addedNodeSet.add(v);
439           }
440
441           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
442           addedEdgeSet.add(flowEdge);
443         }
444       }
445
446     }
447
448   }
449
450   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
451     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
452   }
453
454   public void writeGraph() throws java.io.IOException {
455
456     String graphName = "flowgraph_" + md.toString();
457     graphName = graphName.replaceAll("[\\W]", "");
458
459     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
460     bw.write("digraph " + graphName + " {\n");
461     bw.write("compound=true;\n");
462
463     // then visit every flow node
464
465     Iterator<FlowNode> iter = nodeSet.iterator();
466
467     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
468     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
469
470     while (iter.hasNext()) {
471       FlowNode node = iter.next();
472
473       if (node.getDescTuple().size() == 1) {
474         // here, we just care about the local variable
475         if (node.getFieldNodeSet().size() > 0) {
476           drawSubgraph(node, bw, addedEdgeSet);
477         }
478       }
479       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
480
481     }
482
483     bw.write("}\n");
484     bw.close();
485
486   }
487
488   public boolean constainsNode(FlowNode node) {
489     return nodeSet.contains(node);
490   }
491
492   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
493       throws IOException {
494
495     bw.write("subgraph cluster_" + node.getID() + "{\n");
496     bw.write("label=\"" + node.getPrettyID() + "\";\n");
497
498     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
499     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
500       FlowNode fieldNode = (FlowNode) iterator.next();
501       if (fieldNode.getFieldNodeSet().size() > 0) {
502         drawSubgraph(fieldNode, bw, addedSet);
503       } else {
504         Descriptor desc = fieldNode.getDescTuple().getLastElement();
505         if (desc instanceof VarDescriptor) {
506           VarDescriptor varDesc = (VarDescriptor) desc;
507           if (varDesc.getType().isPrimitive()) {
508             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
509           }
510         } else if (desc instanceof FieldDescriptor) {
511           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
512           if (fieldDesc.getType().isPrimitive()) {
513             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
514           }
515         }
516       }
517     }
518
519     bw.write("}\n");
520   }
521 }