only look at things reachable from main
[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   private void buildGraph() {
171     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
172     while(it.hasNext()) {
173       ClassDescriptor cn=(ClassDescriptor)it.next();
174       Iterator methodit=cn.getMethods();
175       //Iterator through methods
176       while(methodit.hasNext()) {
177         MethodDescriptor md=(MethodDescriptor)methodit.next();
178         analyzeMethod( (Object)md, state.getMethodFlat(md) );
179       }
180     }
181     it=state.getTaskSymbolTable().getDescriptorsIterator();
182     while(it.hasNext()) {
183       TaskDescriptor td=(TaskDescriptor)it.next();
184       analyzeMethod( (Object)td, state.getMethodFlat(td) );
185     }
186   }
187
188   protected void analyzeMethod(Object caller, FlatMethod fm) {
189     HashSet toexplore=new HashSet();
190     toexplore.add(fm);
191     HashSet explored=new HashSet();
192     //look at all the nodes in the flat representation
193     while(!toexplore.isEmpty()) {
194       FlatNode fn=(FlatNode)(toexplore.iterator()).next();
195       toexplore.remove(fn);
196       explored.add(fn);
197       for(int i=0; i<fn.numNext(); i++) {
198         FlatNode fnnext=fn.getNext(i);
199         if (!explored.contains(fnnext))
200           toexplore.add(fnnext);
201       }
202       if (fn.kind()==FKind.FlatCall) {
203         FlatCall fc=(FlatCall)fn;
204         MethodDescriptor calledmethod=fc.getMethod();
205         Set methodsthatcouldbecalled=fc.getThis()==null ? getMethods(calledmethod) :
206                                       getMethods(calledmethod, fc.getThis().getType());
207
208         // add caller -> callee maps
209         if( !mapCaller2CalleeSet.containsKey(caller) ) {
210           mapCaller2CalleeSet.put(caller, new HashSet() );
211         }
212         ((HashSet)mapCaller2CalleeSet.get(caller)).addAll(methodsthatcouldbecalled);
213
214         // add callee -> caller maps
215         Iterator calleeItr = methodsthatcouldbecalled.iterator();
216         while( calleeItr.hasNext() ) {
217           MethodDescriptor callee = (MethodDescriptor) calleeItr.next();
218           if( !mapCallee2CallerSet.containsKey(callee) ) {
219             mapCallee2CallerSet.put(callee, new HashSet() );
220           }
221           ((HashSet)mapCallee2CallerSet.get(callee)).add(caller);
222         }
223       }
224     }
225   }
226
227
228   public void writeVirtual2ImplemToDot(String graphName)  throws java.io.IOException {
229     HashSet labeledInDot = new HashSet();
230
231     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+".dot") );
232     bw.write("digraph "+graphName+" {\n");
233     Iterator mapItr =  mapVirtual2ImplementationSet.entrySet().iterator();
234     while( mapItr.hasNext() ) {
235       Map.Entry me        = (Map.Entry)mapItr.next();
236       MethodDescriptor virtual   = (MethodDescriptor) me.getKey();
237       HashSet implemSet = (HashSet)          me.getValue();
238
239       if( !labeledInDot.contains(virtual) ) {
240         labeledInDot.add(virtual);
241         bw.write("  "+virtual.getNum()+"[label=\""+virtual+"\"];\n");
242       }
243
244       Iterator implemItr = implemSet.iterator();
245       while( implemItr.hasNext() ) {
246         Descriptor implem = (Descriptor) implemItr.next();
247
248         if( !labeledInDot.contains(implem) ) {
249           labeledInDot.add(implem);
250           bw.write("  "+implem.getNum()+"[label=\""+implem+"\"];\n");
251         }
252
253         bw.write("  "+virtual.getNum()+"->"+implem.getNum()+";\n");
254       }
255     }
256     bw.write("}\n");
257     bw.close();
258   }
259
260
261   public void writeCaller2CalleesToDot(String graphName)  throws java.io.IOException {
262     // write out the call graph (should be equivalent) by
263     // using the callers mapping
264     HashSet labeledInDot = new HashSet();
265     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+"byCallers.dot") );
266     bw.write("digraph "+graphName+"byCallers {\n");
267     Iterator mapItr = mapCaller2CalleeSet.entrySet().iterator();
268     while( mapItr.hasNext() ) {
269       Map.Entry me      = (Map.Entry)mapItr.next();
270       Descriptor caller = (Descriptor) me.getKey();
271       HashSet calleeSet = (HashSet)    me.getValue();
272
273       if( !labeledInDot.contains(caller) ) {
274         labeledInDot.add(caller);
275         bw.write("  "+caller.getNum()+"[label=\"" +caller+"\"];\n");
276       }
277
278       Iterator calleeItr = calleeSet.iterator();
279       while( calleeItr.hasNext() ) {
280         MethodDescriptor callee = (MethodDescriptor) calleeItr.next();
281
282         if( !labeledInDot.contains(callee) ) {
283           labeledInDot.add(callee);
284           bw.write("  "+callee.getNum()+"[label=\""+callee+"\"];\n");
285         }
286
287         bw.write("  "+caller.getNum()+"->"+callee.getNum()+";\n");
288       }
289     }
290     bw.write("}\n");
291     bw.close();
292   }
293
294
295   public void writeCallee2CallersToDot(String graphName)  throws java.io.IOException {
296     // each task or method only needs to be labeled once
297     // in a dot file
298     HashSet labeledInDot = new HashSet();
299
300     // write out the call graph using the callees mapping
301     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName+"byCallees.dot") );
302     bw.write("digraph "+graphName+"byCallees {\n");
303     Iterator mapItr = mapCallee2CallerSet.entrySet().iterator();
304     while( mapItr.hasNext() ) {
305       Map.Entry me        = (Map.Entry)mapItr.next();
306       MethodDescriptor callee    = (MethodDescriptor) me.getKey();
307       HashSet callerSet = (HashSet)          me.getValue();
308
309       if( !labeledInDot.contains(callee) ) {
310         labeledInDot.add(callee);
311         bw.write("  "+callee.getNum()+"[label=\""+callee+"\"];\n");
312       }
313
314       Iterator callerItr = callerSet.iterator();
315       while( callerItr.hasNext() ) {
316         Descriptor caller = (Descriptor) callerItr.next();
317
318         if( !labeledInDot.contains(caller) ) {
319           labeledInDot.add(caller);
320           bw.write("  "+caller.getNum()+"[label=\""+caller+"\"];\n");
321         }
322
323         bw.write("  "+caller.getNum()+"->"+callee.getNum()+";\n");
324       }
325     }
326     bw.write("}\n");
327     bw.close();
328   }
329 }