1 package Analysis.CallGraph;
3 import IR.Flat.FlatMethod;
4 import IR.Flat.FlatNode;
5 import IR.Flat.FlatCall;
8 import IR.ClassDescriptor;
9 import IR.MethodDescriptor;
10 import IR.TaskDescriptor;
11 import IR.TypeDescriptor;
15 public class CallGraph {
16 protected State state;
18 // MethodDescriptor maps to HashSet<MethodDescriptor>
19 protected Hashtable mapVirtual2ImplementationSet;
21 // MethodDescriptor or TaskDescriptor maps to HashSet<MethodDescriptor>
22 protected Hashtable mapCaller2CalleeSet;
24 // MethodDescriptor maps to HashSet<MethodDescriptor or TaskDescriptor>
25 protected Hashtable mapCallee2CallerSet;
27 protected CallGraph() {}
29 public CallGraph(State state) {
31 mapVirtual2ImplementationSet = new Hashtable();
32 mapCaller2CalleeSet = new Hashtable();
33 mapCallee2CallerSet = new Hashtable();
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);
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);
56 Set s = (Set) mapCaller2CalleeSet.get(d);
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();
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)
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);
95 public Set getMethods(MethodDescriptor md, TypeDescriptor type) {
96 return getMethods(md);
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();
104 Set s=(Set)mapVirtual2ImplementationSet.get(md);
106 for(Iterator it=s.iterator(); it.hasNext();) {
107 MethodDescriptor md2=(MethodDescriptor)it.next();
108 ns.addAll(getMethods(md2));
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);
119 public Set getMethodCalls(MethodDescriptor md) {
120 return getMethodCalls( (Descriptor) md);
123 public Set getMethodCalls(Descriptor d) {
124 assert d instanceof MethodDescriptor ||
125 d instanceof TaskDescriptor;
127 HashSet ns=new HashSet();
129 return getMoreMethodCalls(ns, d);
132 private Set getMoreMethodCalls(HashSet found, Descriptor d) {
133 HashSet ns=new HashSet();
136 Set s=(Set)mapCaller2CalleeSet.get(d);
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));
147 /** Returns all methods transitively callable from d */
149 public Set getAllMethods(Descriptor d) {
150 HashSet tovisit=new HashSet();
152 HashSet callable=new HashSet();
153 while(!tovisit.isEmpty()) {
154 Descriptor md=(Descriptor)tovisit.iterator().next();
156 Set s=(Set)mapCaller2CalleeSet.get(md);
158 for(Iterator it=s.iterator(); it.hasNext();) {
159 MethodDescriptor md2=(MethodDescriptor)it.next();
160 if( !callable.contains(md2) ) {
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) );
181 it=state.getTaskSymbolTable().getDescriptorsIterator();
182 while(it.hasNext()) {
183 TaskDescriptor td=(TaskDescriptor)it.next();
184 analyzeMethod( (Object)td, state.getMethodFlat(td) );
188 protected void analyzeMethod(Object caller, FlatMethod fm) {
189 HashSet toexplore=new HashSet();
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);
197 for(int i=0; i<fn.numNext(); i++) {
198 FlatNode fnnext=fn.getNext(i);
199 if (!explored.contains(fnnext))
200 toexplore.add(fnnext);
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());
208 // add caller -> callee maps
209 if( !mapCaller2CalleeSet.containsKey(caller) ) {
210 mapCaller2CalleeSet.put(caller, new HashSet() );
212 ((HashSet)mapCaller2CalleeSet.get(caller)).addAll(methodsthatcouldbecalled);
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() );
221 ((HashSet)mapCallee2CallerSet.get(callee)).add(caller);
228 public void writeVirtual2ImplemToDot(String graphName) throws java.io.IOException {
229 HashSet labeledInDot = new HashSet();
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();
239 if( !labeledInDot.contains(virtual) ) {
240 labeledInDot.add(virtual);
241 bw.write(" "+virtual.getNum()+"[label=\""+virtual+"\"];\n");
244 Iterator implemItr = implemSet.iterator();
245 while( implemItr.hasNext() ) {
246 Descriptor implem = (Descriptor) implemItr.next();
248 if( !labeledInDot.contains(implem) ) {
249 labeledInDot.add(implem);
250 bw.write(" "+implem.getNum()+"[label=\""+implem+"\"];\n");
253 bw.write(" "+virtual.getNum()+"->"+implem.getNum()+";\n");
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();
273 if( !labeledInDot.contains(caller) ) {
274 labeledInDot.add(caller);
275 bw.write(" "+caller.getNum()+"[label=\"" +caller+"\"];\n");
278 Iterator calleeItr = calleeSet.iterator();
279 while( calleeItr.hasNext() ) {
280 MethodDescriptor callee = (MethodDescriptor) calleeItr.next();
282 if( !labeledInDot.contains(callee) ) {
283 labeledInDot.add(callee);
284 bw.write(" "+callee.getNum()+"[label=\""+callee+"\"];\n");
287 bw.write(" "+caller.getNum()+"->"+callee.getNum()+";\n");
295 public void writeCallee2CallersToDot(String graphName) throws java.io.IOException {
296 // each task or method only needs to be labeled once
298 HashSet labeledInDot = new HashSet();
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();
309 if( !labeledInDot.contains(callee) ) {
310 labeledInDot.add(callee);
311 bw.write(" "+callee.getNum()+"[label=\""+callee+"\"];\n");
314 Iterator callerItr = callerSet.iterator();
315 while( callerItr.hasNext() ) {
316 Descriptor caller = (Descriptor) callerItr.next();
318 if( !labeledInDot.contains(caller) ) {
319 labeledInDot.add(caller);
320 bw.write(" "+caller.getNum()+"[label=\""+caller+"\"];\n");
323 bw.write(" "+caller.getNum()+"->"+callee.getNum()+";\n");