e69578ff024615a67334e138675b2de4345b23f3
[IRC.git] / Robust / src / Analysis / CallGraph / CallGraph.java
1 package Analysis.CallGraph;
2 import IR.State;
3 import IR.Flat.FlatMethod;
4 import IR.Flat.FlatNode;
5 import IR.Flat.FlatCall;
6 import IR.Flat.FKind;
7 import IR.Descriptor;
8 import IR.ClassDescriptor;
9 import IR.MethodDescriptor;
10 import IR.TaskDescriptor;
11 import IR.TypeDescriptor;
12 import java.util.*;
13 import java.io.*;
14
15 public class CallGraph {
16   protected State state;
17
18   // MethodDescriptor maps to HashSet<MethodDescriptor>
19   protected Hashtable mapVirtual2ImplementationSet;
20
21   // MethodDescriptor or TaskDescriptor maps to HashSet<MethodDescriptor>
22   protected Hashtable mapCaller2CalleeSet;
23
24   // MethodDescriptor maps to HashSet<MethodDescriptor or TaskDescriptor>
25   protected Hashtable mapCallee2CallerSet;
26
27   protected CallGraph() {}
28
29   public CallGraph(State state) {
30     this.state=state;
31     mapVirtual2ImplementationSet = new Hashtable();
32     mapCaller2CalleeSet          = new Hashtable();
33     mapCallee2CallerSet          = new Hashtable();
34     buildVirtualMap();
35     buildGraph();
36   }
37
38   // this method returns the set of Descriptors
39   // (MethodDescriptors and/or TaskDescriptors)
40   //  that call the given method
41   public Set getCallerSet(MethodDescriptor md) {
42     Set s = (Set) mapCallee2CallerSet.get(md);
43     
44     if( s == null ) {
45       return new HashSet();
46     }
47     return s;
48   }
49
50   // this method returns the set of MethodDescriptors that
51   // are called by the given method or task
52   public Set getCalleeSet(Descriptor d) {
53     assert(d instanceof MethodDescriptor) ||
54     (d instanceof TaskDescriptor);
55
56     Set s = (Set) mapCaller2CalleeSet.get(d);
57
58     if( s == null ) {
59       return new HashSet();
60     }
61     return s;
62   }
63
64   // build a mapping of virtual methods to all
65   // possible implementations of that method
66   protected void buildVirtualMap() {
67     //Iterator through classes
68     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
69     while(it.hasNext()) {
70       ClassDescriptor cn=(ClassDescriptor)it.next();
71       Iterator methodit=cn.getMethods();
72       //Iterator through methods
73       while(methodit.hasNext()) {
74         MethodDescriptor md=(MethodDescriptor)methodit.next();
75         if (md.isStatic()||md.getReturnType()==null)
76           continue;
77         ClassDescriptor superdesc=cn.getSuperDesc();
78         if (superdesc!=null) {
79           Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
80           boolean foundmatch=false;
81           for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
82             MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
83             if (md.matches(matchmd)) {
84               if (!mapVirtual2ImplementationSet.containsKey(matchmd))
85                 mapVirtual2ImplementationSet.put(matchmd,new HashSet());
86               ((HashSet)mapVirtual2ImplementationSet.get(matchmd)).add(md);
87               break;
88             }
89           }
90         }
91       }
92     }
93   }
94
95   public Set getMethods(MethodDescriptor md, TypeDescriptor type) {
96     return getMethods(md);
97   }
98
99   /** Given a call to MethodDescriptor, lists the methods which
100       could actually be called due to virtual dispatch. */
101   public Set getMethods(MethodDescriptor md) {
102     HashSet ns=new HashSet();
103     ns.add(md);
104     Set s=(Set)mapVirtual2ImplementationSet.get(md);
105     if (s!=null)
106       for(Iterator it=s.iterator(); it.hasNext();) {
107         MethodDescriptor md2=(MethodDescriptor)it.next();
108         ns.addAll(getMethods(md2));
109       }
110     return ns;
111   }
112
113   /** Given a call to MethodDescriptor, lists the methods which
114       could actually be call by that method. */
115   public Set getMethodCalls(TaskDescriptor td) {
116     return getMethodCalls( (Descriptor) td);
117   }
118
119   public Set getMethodCalls(MethodDescriptor md) {
120     return getMethodCalls( (Descriptor) md);
121   }
122
123   public Set getMethodCalls(Descriptor d) {
124     assert d instanceof MethodDescriptor ||
125     d instanceof TaskDescriptor;
126
127     HashSet ns=new HashSet();
128     ns.add(d);
129     return getMoreMethodCalls(ns, d);
130   }
131
132   private Set getMoreMethodCalls(HashSet found, Descriptor d) {
133     HashSet ns=new HashSet();
134     ns.add(d);
135     found.add(d);
136     Set s=(Set)mapCaller2CalleeSet.get(d);
137     if (s!=null)
138       for(Iterator it=s.iterator(); it.hasNext();) {
139         MethodDescriptor md=(MethodDescriptor)it.next();
140         if( !found.contains(md) ) {
141           ns.addAll(getMoreMethodCalls(found, md));
142         }
143       }
144     return ns;
145   }
146
147   /** Returns all methods transitively callable from d */
148
149   public Set getAllMethods(Descriptor d) {
150     HashSet tovisit=new HashSet();
151     tovisit.add(d);
152     HashSet callable=new HashSet();
153     while(!tovisit.isEmpty()) {
154       Descriptor md=(Descriptor)tovisit.iterator().next();
155       tovisit.remove(md);
156       Set s=(Set)mapCaller2CalleeSet.get(md);
157       if (s!=null) {
158         for(Iterator it=s.iterator(); it.hasNext();) {
159           MethodDescriptor md2=(MethodDescriptor)it.next();
160           if( !callable.contains(md2) ) {
161             callable.add(md2);
162             tovisit.add(md2);
163           }
164         }
165       }
166     }
167     return callable;
168   }
169   
170   // Returns a set of methods containing SESEs and located at the first   
171   // in transitive call chain starting from d 
172   public Set getFirstReachableMethodContainingSESE(Descriptor d,
173       Set<MethodDescriptor> methodsContainingSESEs) {
174     HashSet tovisit = new HashSet();
175     tovisit.add(d);
176     HashSet callable = new HashSet();
177     while (!tovisit.isEmpty()) {
178       Descriptor md = (Descriptor) tovisit.iterator().next();
179       tovisit.remove(md);
180       Set s = (Set) mapCaller2CalleeSet.get(md);
181       if (s != null) {
182         for (Iterator it = s.iterator(); it.hasNext();) {
183           MethodDescriptor md2 = (MethodDescriptor) it.next();
184           if (!callable.contains(md2)) {
185             callable.add(md2);
186             if (!methodsContainingSESEs.contains(md2)) {
187               // if current method has sese, do not need to go down
188               tovisit.add(md2);
189             }
190           }
191         }
192       }
193     }
194     callable.retainAll(methodsContainingSESEs);
195     return callable;
196   }
197   
198
199   private void buildGraph() {
200     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
201     while(it.hasNext()) {
202       ClassDescriptor cn=(ClassDescriptor)it.next();
203       Iterator methodit=cn.getMethods();
204       //Iterator through methods
205       while(methodit.hasNext()) {
206         MethodDescriptor md=(MethodDescriptor)methodit.next();
207         analyzeMethod( (Object)md, state.getMethodFlat(md) );
208       }
209     }
210     it=state.getTaskSymbolTable().getDescriptorsIterator();
211     while(it.hasNext()) {
212       TaskDescriptor td=(TaskDescriptor)it.next();
213       analyzeMethod( (Object)td, state.getMethodFlat(td) );
214     }
215   }
216
217   protected void analyzeMethod(Object caller, FlatMethod fm) {
218     HashSet toexplore=new HashSet();
219     toexplore.add(fm);
220     HashSet explored=new HashSet();
221     //look at all the nodes in the flat representation
222     while(!toexplore.isEmpty()) {
223       FlatNode fn=(FlatNode)(toexplore.iterator()).next();
224       toexplore.remove(fn);
225       explored.add(fn);
226       for(int i=0; i<fn.numNext(); i++) {
227         FlatNode fnnext=fn.getNext(i);
228         if (!explored.contains(fnnext))
229           toexplore.add(fnnext);
230       }
231       if (fn.kind()==FKind.FlatCall) {
232         FlatCall fc=(FlatCall)fn;
233         MethodDescriptor calledmethod=fc.getMethod();
234         Set methodsthatcouldbecalled=fc.getThis()==null ? getMethods(calledmethod) :
235                                       getMethods(calledmethod, fc.getThis().getType());
236
237         // add caller -> callee maps
238         if( !mapCaller2CalleeSet.containsKey(caller) ) {
239           mapCaller2CalleeSet.put(caller, new HashSet() );
240         }
241         ((HashSet)mapCaller2CalleeSet.get(caller)).addAll(methodsthatcouldbecalled);
242
243         // add callee -> caller maps
244         Iterator calleeItr = methodsthatcouldbecalled.iterator();
245         while( calleeItr.hasNext() ) {
246           MethodDescriptor callee = (MethodDescriptor) calleeItr.next();
247           if( !mapCallee2CallerSet.containsKey(callee) ) {
248             mapCallee2CallerSet.put(callee, new HashSet() );
249           }
250           ((HashSet)mapCallee2CallerSet.get(callee)).add(caller);
251         }
252       }
253     }
254   }
255
256
257   public void writeVirtual2ImplemToDot(String graphName)  throws java.io.IOException {
258     HashSet labeledInDot = new HashSet();
259
260     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+".dot") );
261     bw.write("digraph "+graphName+" {\n");
262     Iterator mapItr =  mapVirtual2ImplementationSet.entrySet().iterator();
263     while( mapItr.hasNext() ) {
264       Map.Entry me        = (Map.Entry)mapItr.next();
265       MethodDescriptor virtual   = (MethodDescriptor) me.getKey();
266       HashSet implemSet = (HashSet)          me.getValue();
267
268       if( !labeledInDot.contains(virtual) ) {
269         labeledInDot.add(virtual);
270         bw.write("  "+virtual.getNum()+"[label=\""+virtual+"\"];\n");
271       }
272
273       Iterator implemItr = implemSet.iterator();
274       while( implemItr.hasNext() ) {
275         Descriptor implem = (Descriptor) implemItr.next();
276
277         if( !labeledInDot.contains(implem) ) {
278           labeledInDot.add(implem);
279           bw.write("  "+implem.getNum()+"[label=\""+implem+"\"];\n");
280         }
281
282         bw.write("  "+virtual.getNum()+"->"+implem.getNum()+";\n");
283       }
284     }
285     bw.write("}\n");
286     bw.close();
287   }
288
289
290   public void writeCaller2CalleesToDot(String graphName)  throws java.io.IOException {
291     // write out the call graph (should be equivalent) by
292     // using the callers mapping
293     HashSet labeledInDot = new HashSet();
294     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+"byCallers.dot") );
295     bw.write("digraph "+graphName+"byCallers {\n");
296     Iterator mapItr = mapCaller2CalleeSet.entrySet().iterator();
297     while( mapItr.hasNext() ) {
298       Map.Entry me      = (Map.Entry)mapItr.next();
299       Descriptor caller = (Descriptor) me.getKey();
300       HashSet calleeSet = (HashSet)    me.getValue();
301
302       if( !labeledInDot.contains(caller) ) {
303         labeledInDot.add(caller);
304         bw.write("  "+caller.getNum()+"[label=\"" +caller+"\"];\n");
305       }
306
307       Iterator calleeItr = calleeSet.iterator();
308       while( calleeItr.hasNext() ) {
309         MethodDescriptor callee = (MethodDescriptor) calleeItr.next();
310
311         if( !labeledInDot.contains(callee) ) {
312           labeledInDot.add(callee);
313           bw.write("  "+callee.getNum()+"[label=\""+callee+"\"];\n");
314         }
315
316         bw.write("  "+caller.getNum()+"->"+callee.getNum()+";\n");
317       }
318     }
319     bw.write("}\n");
320     bw.close();
321   }
322
323
324   public void writeCallee2CallersToDot(String graphName)  throws java.io.IOException {
325     // each task or method only needs to be labeled once
326     // in a dot file
327     HashSet labeledInDot = new HashSet();
328
329     // write out the call graph using the callees mapping
330     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+"byCallees.dot") );
331     bw.write("digraph "+graphName+"byCallees {\n");
332     Iterator mapItr = mapCallee2CallerSet.entrySet().iterator();
333     while( mapItr.hasNext() ) {
334       Map.Entry me        = (Map.Entry)mapItr.next();
335       MethodDescriptor callee    = (MethodDescriptor) me.getKey();
336       HashSet callerSet = (HashSet)          me.getValue();
337
338       if( !labeledInDot.contains(callee) ) {
339         labeledInDot.add(callee);
340         bw.write("  "+callee.getNum()+"[label=\""+callee+"\"];\n");
341       }
342
343       Iterator callerItr = callerSet.iterator();
344       while( callerItr.hasNext() ) {
345         Descriptor caller = (Descriptor) callerItr.next();
346
347         if( !labeledInDot.contains(caller) ) {
348           labeledInDot.add(caller);
349           bw.write("  "+caller.getNum()+"[label=\""+caller+"\"];\n");
350         }
351
352         bw.write("  "+caller.getNum()+"->"+callee.getNum()+";\n");
353       }
354     }
355     bw.write("}\n");
356     bw.close();
357   }
358 }