View Javadoc

1   // ControlFlowGraphNavigator.java, created Sat Mar 29  0:56:01 2003 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Compiler.Quad;
5   
6   import java.util.Arrays;
7   import java.util.Collection;
8   import java.util.HashSet;
9   import java.util.Iterator;
10  import java.util.List;
11  import joeq.Class.jq_Class;
12  import joeq.Class.jq_Method;
13  import joeq.Class.jq_Type;
14  import joeq.Main.HostedVM;
15  import jwutil.collections.AppendList;
16  import jwutil.graphs.Navigator;
17  import jwutil.graphs.SCCTopSortedGraph;
18  import jwutil.graphs.SCComponent;
19  
20  /***
21   * @author John Whaley <jwhaley@alum.mit.edu>
22   * @version $Id: ControlFlowGraphNavigator.java 1931 2004-09-22 22:17:47Z joewhaley $
23   */
24  public class ControlFlowGraphNavigator implements Navigator {
25  
26      protected ControlFlowGraph cfg;
27  
28      /*** Construct a new ControlFlowGraphNavigator for the given
29       * control flow graph.
30       * @param cfg control flow graph
31       */
32      public ControlFlowGraphNavigator(ControlFlowGraph cfg) {
33          this.cfg = cfg;
34      }
35      protected ControlFlowGraphNavigator() {}
36      
37      /*** Singleton object for a control flow graph navigator that
38       * does not take into account exception edges.
39       */
40      public static final ControlFlowGraphNavigator INSTANCE = new ControlFlowGraphNavigator();
41  
42      /* (non-Javadoc)
43       * @see joeq.Util.Graphs.Navigator#next(java.lang.Object)
44       */
45      public Collection next(Object node) {
46          BasicBlock bb = (BasicBlock) node;
47          List result = bb.getSuccessors();
48          List eh = bb.getExceptionHandlerEntries();
49          if (cfg == null || eh.isEmpty()) return result;
50          else return new AppendList(result, eh);
51      }
52  
53      /* (non-Javadoc)
54       * @see joeq.Util.Graphs.Navigator#prev(java.lang.Object)
55       */
56      public Collection prev(Object node) {
57          BasicBlock bb = (BasicBlock) node;
58          List result = bb.getPredecessors();
59          if (cfg == null || !bb.isExceptionHandlerEntry()) return result;
60          Iterator ex_handlers = cfg.getExceptionHandlersMatchingEntry(bb);
61          while (ex_handlers.hasNext()) {
62              ExceptionHandler eh = (ExceptionHandler)ex_handlers.next();
63              result = new AppendList(result, eh.getHandledBasicBlocks());
64          }
65          return result;
66      }
67  
68      // test function
69      public static void main(String[] args) {
70          HostedVM.initialize();
71          HashSet set = new HashSet();
72          for (int i=0; i<args.length; ++i) {
73              String s = args[i];
74              jq_Class c = (jq_Class) jq_Type.parseType(s);
75              c.load();
76              set.addAll(Arrays.asList(c.getDeclaredStaticMethods()));
77          }
78          for (Iterator i=set.iterator(); i.hasNext(); ) {
79              jq_Method m = (jq_Method) i.next();
80              if (m.getBytecode() == null) continue;
81              System.out.println("Method "+m);
82              ControlFlowGraph cfg = CodeCache.getCode(m);
83              ControlFlowGraphNavigator v = new ControlFlowGraphNavigator(cfg);
84              SCComponent c = SCComponent.buildSCC(cfg.entry(), v);
85              SCCTopSortedGraph g = SCCTopSortedGraph.topSort(c);
86              for (Iterator j=g.getFirst().listTopSort().iterator(); j.hasNext(); ) {
87                  SCComponent d = (SCComponent) j.next();
88                  System.out.println(d);
89              }
90          }
91      }
92  
93  }