View Javadoc

1   // CalculateSize.java, created Mar 21, 2004 1:26:40 AM 2004 by jwhaley
2   // Copyright (C) 2004 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.Iterator;
7   import java.util.List;
8   import joeq.Class.jq_Class;
9   import joeq.Main.Helper;
10  import joeq.Runtime.Reflection;
11  
12  /***
13   * CalculateSize
14   * 
15   * @author jwhaley
16   * @version $Id: CalculateSize.java 1931 2004-09-22 22:17:47Z joewhaley $
17   */
18  public class CalculateSize extends QuadVisitor.EmptyVisitor implements ControlFlowGraphVisitor, BasicBlockVisitor {
19  
20      static final int ARRAYLIST_OVERHEAD = 7 * 4;
21      static final int HASHMAP_OVERHEAD = 7 * 4;
22      
23      int cfgSize;
24      int quadSize;
25      int rfSize;
26      
27      /* (non-Javadoc)
28       * @see joeq.Compiler.Quad.ControlFlowGraphVisitor#visitCFG(joeq.Compiler.Quad.ControlFlowGraph)
29       */
30      public void visitCFG(ControlFlowGraph cfg) {
31          jq_Class c = (jq_Class) Reflection.getTypeOf(cfg);
32          c.prepare();
33          cfgSize += c.getInstanceSize();
34          List ehs = cfg.getExceptionHandlers();
35          cfgSize += ARRAYLIST_OVERHEAD;
36          cfgSize += 4 * ehs.size();
37          for (Iterator i = ehs.iterator(); i.hasNext(); ) {
38              ExceptionHandler eh = (ExceptionHandler) i.next();
39              c = (jq_Class) Reflection.getTypeOf(eh);
40              c.prepare();
41              cfgSize += c.getInstanceSize();
42              List bbs = eh.getHandledBasicBlocks();
43              cfgSize += ARRAYLIST_OVERHEAD + 4*bbs.size();
44          }
45          if (cfg.jsr_map != null) {
46              cfgSize += HASHMAP_OVERHEAD;
47              cfgSize += cfg.jsr_map.size() * 24; // 24 for each Entry
48              cfgSize += cfg.jsr_map.size() * 100; // for Entry[] table;
49          }
50          RegisterFactory rf = cfg.getRegisterFactory();
51          c = (jq_Class) Reflection.getTypeOf(rf);
52          c.prepare();
53          rfSize += c.getInstanceSize();
54          rfSize += ARRAYLIST_OVERHEAD;
55          rfSize += 4 * rf.size();
56          rfSize += HASHMAP_OVERHEAD;
57          rfSize += rf.numberOfLocalRegisters() * 24; // 24 for each Entry
58          rfSize += rf.numberOfLocalRegisters() * 100; // for Entry[] table;
59          rfSize += HASHMAP_OVERHEAD;
60          rfSize += rf.numberOfStackRegisters() * 24; // 24 for each Entry
61          rfSize += rf.numberOfStackRegisters() * 100; // for Entry[] table;
62          cfg.visitBasicBlocks(this);
63          System.out.println(cfg.getMethod()+" "+toString());
64      }
65  
66      /* (non-Javadoc)
67       * @see joeq.Compiler.Quad.BasicBlockVisitor#visitBasicBlock(joeq.Compiler.Quad.BasicBlock)
68       */
69      public void visitBasicBlock(BasicBlock bb) {
70          jq_Class c = (jq_Class) Reflection.getTypeOf(bb);
71          c.prepare();
72          cfgSize += c.getInstanceSize();
73          cfgSize += ARRAYLIST_OVERHEAD;
74          cfgSize += 4 * bb.getNumberOfPredecessors();
75          cfgSize += ARRAYLIST_OVERHEAD;
76          cfgSize += 4 * bb.getNumberOfSuccessors();
77          cfgSize += ARRAYLIST_OVERHEAD;
78          cfgSize += 4 * bb.size();
79          // todo: sharing of exception handler lists?
80          ExceptionHandlerList ehl = bb.getExceptionHandlers();
81          while (ehl != null) {
82              c = (jq_Class) Reflection.getTypeOf(ehl);
83              c.prepare();
84              cfgSize += c.getInstanceSize();
85              ExceptionHandler eh = ehl.getHandler();
86              if (eh != null) {
87                  c = (jq_Class) Reflection.getTypeOf(eh);
88                  c.prepare();
89                  cfgSize += c.getInstanceSize();
90                  cfgSize += ARRAYLIST_OVERHEAD;
91                  cfgSize += 4 * eh.getHandledBasicBlocks().size();
92              }
93              ehl = ehl.getParent();
94          }
95          bb.visitQuads(this);
96      }
97      
98      /* (non-Javadoc)
99       * @see joeq.Compiler.Quad.QuadVisitor#visitQuad(joeq.Compiler.Quad.Quad)
100      */
101     public void visitQuad(Quad obj) {
102         super.visitQuad(obj);
103         jq_Class c = (jq_Class) Reflection.getTypeOf(obj);
104         c.prepare();
105         quadSize += c.getInstanceSize();
106         for (Iterator i = obj.getAllOperands().iterator(); i.hasNext(); ) {
107             Operand o = (Operand) i.next();
108             c = (jq_Class) Reflection.getTypeOf(o);
109             c.prepare();
110             quadSize += c.getInstanceSize();
111         }
112     }
113 
114     public String toString() {
115         return cfgSize+" + "+quadSize+" + "+rfSize+" = "+(cfgSize+quadSize+rfSize);
116     }
117     
118     public static void main(String[] args) {
119         CalculateSize cs = new CalculateSize();
120         for (int i = 0; i < args.length; ++i) {
121             jq_Class c = (jq_Class) Helper.load(args[0]);
122             Helper.runPass(c, (ControlFlowGraphVisitor)cs);
123         }
124         System.out.println(cs.toString());
125     }
126     
127 }