View Javadoc

1   // CodeCache.java, created Wed Jan 30 22:33:28 2002 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.HashMap;
7   import java.util.LinkedList;
8   import java.util.List;
9   import java.util.Map;
10  import joeq.Class.jq_Method;
11  
12  /***
13   *
14   * @author  John Whaley <jwhaley@alum.mit.edu>
15   * @version $Id: CodeCache.java 2457 2006-04-06 02:32:10Z mcmartin $
16   */
17  public class CodeCache {
18  
19      public static final CodeCache cache = new CodeCache();
20  
21      protected Map map = new HashMap();
22      protected Map bcmap = new HashMap();
23  
24      public static List/*<ControlFlowGraphVisitor>*/ passes = new LinkedList();
25      
26      
27      /*** Creates new CodeCache */
28      public CodeCache() { }
29  
30      public static ControlFlowGraph getCode(jq_Method m) { return cache._get(m); }
31      public static Map getBCMap(jq_Method m) { return cache._getmap(m); }
32      public static void free(ControlFlowGraph cfg) {
33          if (getCode(cfg.getMethod()) == cfg)
34              cache._delete(cfg.getMethod());
35      }
36  
37      public static boolean TRACE = false;
38      public static boolean AlwaysMap = false;
39      
40      public static void addDefaultPass(ControlFlowGraphVisitor pass) {
41          passes.add(pass);
42      }
43  
44      public static void clearDefaultPasses() {
45          passes.clear();        
46      }
47      
48      protected ControlFlowGraph _get(jq_Method m) {
49          ControlFlowGraph cfg = (ControlFlowGraph)map.get(m);
50          if (cfg == null) {
51              if (TRACE) System.out.println("Generating quads for "+m);
52              BytecodeToQuad b2q = new BytecodeToQuad(m);
53              cfg = b2q.convert();
54              map.put(m, cfg);
55              if (AlwaysMap) {
56                  bcmap.put(m, b2q.getQuadToBytecodeMap());
57              }
58              for (java.util.Iterator i = passes.iterator(); i.hasNext(); ) {
59                  ControlFlowGraphVisitor v = (ControlFlowGraphVisitor)i.next();
60                  v.visitCFG(cfg);
61              }
62              //System.out.println("Done creating representation for " + m);            
63          }
64          //if(m.getName().toString().)
65          return cfg;
66      }
67  
68      protected java.util.Map _getmap(jq_Method m) {
69          Map result = (Map) bcmap.get(m);
70          if (result == null) {
71              if (TRACE) System.out.println("Generating quads for "+m);
72              BytecodeToQuad b2q = new BytecodeToQuad(m);
73              ControlFlowGraph cfg = b2q.convert();
74              map.put(m, cfg);
75              result = b2q.getQuadToBytecodeMap();
76              bcmap.put(m, result);
77          }
78          return result;
79      }
80      
81      protected void _delete(jq_Method m) {
82          map.remove(m);
83          bcmap.remove(m);
84      }
85      
86      public void invalidateCache() {
87          map.clear();
88          bcmap.clear();
89      }
90      
91      static public void invalidate() {
92          cache.invalidateCache();
93      }
94  
95      public static void invalidateBCMap(jq_Method m) {
96          cache._invalidateBCMap(m);        
97      }
98  
99      private void _invalidateBCMap(jq_Method m) {
100         if(bcmap.get(m) != null) {
101             // System.err.println("Replacing " + m);
102         }
103         bcmap.put(m, null);        
104     }
105 }