changes + annotation generation
[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 isParamDesc(Descriptor desc) {
93     return mapParamDescToIdx.containsKey(desc);
94   }
95
96   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
97
98     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
99     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
100
101     Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
102     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
103       FlowEdge flowEdge = (FlowEdge) iterator.next();
104       if (flowEdge.getDst().equals(toNode)) {
105         return true;
106       } else {
107         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
108           return true;
109         }
110       }
111     }
112
113     return false;
114   }
115
116   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
117
118     FlowNode fromNode = getFlowNode(fromDescTuple);
119     FlowNode toNode = getFlowNode(toDescTuple);
120
121     // System.out.println("create an edge from " + fromNode + " to " + toNode);
122
123     int fromTupleSize = fromDescTuple.size();
124     NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
125     for (int i = 0; i < fromTupleSize; i++) {
126       Descriptor desc = fromDescTuple.get(i);
127       curTuple.add(desc);
128       addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
129     }
130
131     int toTupleSize = toDescTuple.size();
132     curTuple = new NTuple<Descriptor>();
133     for (int i = 0; i < toTupleSize; i++) {
134       Descriptor desc = toDescTuple.get(i);
135       curTuple.add(desc);
136       addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
137     }
138
139   }
140
141   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
142       NTuple<Descriptor> endTuple) {
143
144     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
145     fromNode.addOutEdge(edge);
146
147     // System.out.println("add a new edge=" + edge);
148
149   }
150
151   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
152     if (mapDescTupleToInferNode.containsKey(descTuple)) {
153       return mapDescTupleToInferNode.get(descTuple);
154     } else {
155       FlowNode node = createNewFlowNode(descTuple);
156       return node;
157     }
158   }
159
160   public FlowNode getThisVarNode() {
161     return thisVarNode;
162   }
163
164   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
165
166     if (!mapDescTupleToInferNode.containsKey(tuple)) {
167       FlowNode node = new FlowNode(tuple, isParameter(tuple));
168       mapDescTupleToInferNode.put(tuple, node);
169       nodeSet.add(node);
170
171       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
172
173       if (tuple.size() > 1) {
174         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
175         getFlowNode(baseTuple).addFieldNode(node);
176       }
177
178       // System.out.println("Creating new node=" + node);
179       return node;
180     } else {
181       return mapDescTupleToInferNode.get(tuple);
182     }
183
184   }
185
186   public void setReturnFlowNode(NTuple<Descriptor> tuple) {
187
188     if (!mapDescTupleToInferNode.containsKey(tuple)) {
189       createNewFlowNode(tuple);
190     }
191
192     FlowNode node = mapDescTupleToInferNode.get(tuple);
193     node.setReturn(true);
194
195     returnNodeSet.add(node);
196   }
197
198   public Set<FlowNode> getReturnNodeSet() {
199     return returnNodeSet;
200   }
201
202   public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
203     Set<FlowNode> set = new HashSet<FlowNode>();
204     getReachableFlowNodeSet(fn, set);
205     return set;
206   }
207
208   private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
209
210     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
211       FlowEdge edge = (FlowEdge) iterator.next();
212
213       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
214
215         FlowNode dstNode = getFlowNode(edge.getEndTuple());
216
217         if (!visited.contains(dstNode)) {
218           visited.add(dstNode);
219           getReachableFlowNodeSet(dstNode, visited);
220         }
221       }
222     }
223
224   }
225
226   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
227     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
228       FlowEdge edge = (FlowEdge) iterator.next();
229
230       if (fn.getDescTuple().equals(edge.getInitTuple())) {
231         FlowNode dstNode = getFlowNode(edge.getEndTuple());
232         NTuple<Location> dstTuple = getLocationTuple(dstNode);
233
234         if (!visited.contains(dstTuple)) {
235           visited.add(dstTuple);
236           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
237         }
238
239       }
240     }
241     return visited;
242   }
243
244   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
245     return getLocationTuple(getFlowNode(descTuple));
246   }
247
248   public NTuple<Location> getLocationTuple(FlowNode fn) {
249
250     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
251       NTuple<Descriptor> descTuple = fn.getDescTuple();
252       NTuple<Location> locTuple = new NTuple<Location>();
253       ClassDescriptor cd = null;
254
255       Descriptor localDesc = fn.getDescTuple().get(0);
256       if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
257         Location topLoc = new Location(md, Location.TOP);
258         topLoc.setLocDescriptor(LocationInference.TOPDESC);
259         locTuple.add(topLoc);
260       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
261         Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
262         globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
263         locTuple.add(globalLoc);
264       } else {
265         // normal case
266         for (int i = 0; i < descTuple.size(); i++) {
267           Descriptor curDesc = descTuple.get(i);
268           Location loc;
269           if (i == 0) {
270             loc = new Location(md, curDesc.getSymbol());
271             loc.setLocDescriptor(curDesc);
272             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
273           } else {
274             loc = new Location(cd, curDesc.getSymbol());
275             loc.setLocDescriptor(curDesc);
276             cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
277           }
278           locTuple.add(loc);
279         }
280       }
281
282       mapFlowNodeToLocTuple.put(fn, locTuple);
283     }
284     return mapFlowNodeToLocTuple.get(fn);
285   }
286
287   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
288     Set<FlowNode> set = new HashSet<FlowNode>();
289     getIncomingFlowNodeSet(node, set);
290     return set;
291   }
292
293   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
294
295     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
296       FlowNode curNode = (FlowNode) iterator.next();
297       Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
298
299       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
300         FlowEdge flowEdge = (FlowEdge) iterator2.next();
301
302         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
303           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
304
305           if (!visited.contains(incomingNode)) {
306             visited.add(incomingNode);
307             getIncomingFlowNodeSet(incomingNode, visited);
308           }
309         }
310       }
311     }
312
313   }
314
315   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
316
317     NTuple<Descriptor> dstTuple = fn.getDescTuple();
318
319     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
320
321     ClassDescriptor cd = null;
322
323     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
324       FlowNode node = (FlowNode) iterator.next();
325       Set<FlowEdge> edgeSet = node.getOutEdgeSet();
326       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
327         FlowEdge flowEdge = (FlowEdge) iterator2.next();
328         if (dstTuple.equals(flowEdge.getEndTuple())) {
329           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
330           NTuple<Location> locTuple = new NTuple<Location>();
331           for (int i = 0; i < initTuple.size(); i++) {
332             Descriptor d = initTuple.get(i);
333             Location loc;
334             if (i == 0) {
335               loc = new Location(md, d.getSymbol());
336               cd = ((VarDescriptor) d).getType().getClassDesc();
337             } else {
338               loc = new Location(cd, d.getSymbol());
339               cd = ((FieldDescriptor) d).getType().getClassDesc();
340             }
341             locTuple.add(loc);
342           }
343           set.add(locTuple);
344         }
345       }
346     }
347     return set;
348   }
349
350   public boolean isParameter(NTuple<Descriptor> tuple) {
351     // return true if a descriptor tuple is started with a parameter descriptor
352     Descriptor firstIdxDesc = tuple.get(0);
353     return mapParamDescToIdx.containsKey(firstIdxDesc);
354   }
355
356   public int getParamIdx(NTuple<Descriptor> tuple) {
357     Descriptor firstDesc = tuple.get(0);
358     return mapParamDescToIdx.get(firstDesc).intValue();
359   }
360
361   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
362       Set<FlowEdge> addedEdgeSet) throws IOException {
363
364     Set<FlowEdge> edgeSet = node.getOutEdgeSet();
365
366     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
367       FlowEdge flowEdge = iterator.next();
368
369       FlowNode u = flowEdge.getSrc();
370       FlowNode v = flowEdge.getDst();
371
372       if (u.getDescTuple().equals(flowEdge.getInitTuple())
373           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
374         // only draw an edge of the actual value flow
375
376         if (!addedEdgeSet.contains(flowEdge)) {
377
378           if (!addedNodeSet.contains(u)) {
379             drawNode(u, bw);
380             addedNodeSet.add(u);
381           }
382           if (!addedNodeSet.contains(v)) {
383             drawNode(v, bw);
384             addedNodeSet.add(v);
385           }
386
387           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
388           addedEdgeSet.add(flowEdge);
389         }
390       }
391
392     }
393
394   }
395
396   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
397     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
398   }
399
400   public void writeGraph() throws java.io.IOException {
401
402     String graphName = "flowgraph_" + md.toString();
403     graphName = graphName.replaceAll("[\\W]", "");
404
405     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
406     bw.write("digraph " + graphName + " {\n");
407     bw.write("compound=true;\n");
408
409     // then visit every flow node
410
411     Iterator<FlowNode> iter = nodeSet.iterator();
412
413     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
414     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
415
416     while (iter.hasNext()) {
417       FlowNode node = iter.next();
418
419       if (node.getDescTuple().size() == 1) {
420         // here, we just care about the local variable
421         if (node.getFieldNodeSet().size() > 0) {
422           drawSubgraph(node, bw, addedEdgeSet);
423         }
424       }
425       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
426
427     }
428
429     bw.write("}\n");
430     bw.close();
431
432   }
433
434   public boolean constainsNode(FlowNode node) {
435     return nodeSet.contains(node);
436   }
437
438   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
439       throws IOException {
440
441     bw.write("subgraph cluster_" + node.getID() + "{\n");
442     bw.write("label=\"" + node.getPrettyID() + "\";\n");
443
444     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
445     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
446       FlowNode fieldNode = (FlowNode) iterator.next();
447       if (fieldNode.getFieldNodeSet().size() > 0) {
448         drawSubgraph(fieldNode, bw, addedSet);
449       } else {
450         Descriptor desc = fieldNode.getDescTuple().getLastElement();
451         if (desc instanceof VarDescriptor) {
452           VarDescriptor varDesc = (VarDescriptor) desc;
453           if (varDesc.getType().isPrimitive()) {
454             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
455           }
456         } else if (desc instanceof FieldDescriptor) {
457           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
458           if (fieldDesc.getType().isPrimitive()) {
459             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
460           }
461         }
462       }
463     }
464
465     bw.write("}\n");
466   }
467 }