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