1 salcianu 1.1.2.1 // CachingBBConverter.java, created Wed Mar 8 15:57:12 2000 by salcianu 2 cananian 1.1.2.2 // Copyright (C) 2000 Alexandru SALCIANU <salcianu@retezat.lcs.mit.edu> 3 salcianu 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 salcianu 1.1.2.1 package harpoon.Util.BasicBlocks; 5 salcianu 1.1.2.1 6 salcianu 1.1.2.1 import java.util.Map; 7 salcianu 1.1.2.1 import java.util.HashMap; 8 salcianu 1.1.2.1 9 salcianu 1.1.2.1 import harpoon.ClassFile.HMethod; 10 salcianu 1.1.2.1 import harpoon.ClassFile.HCodeFactory; 11 salcianu 1.1.2.3 import harpoon.Analysis.BasicBlock; 12 salcianu 1.1.2.3 import harpoon.Analysis.BasicBlockFactoryInterf; 13 salcianu 1.1.2.1 14 salcianu 1.1.2.1 /** 15 salcianu 1.1.2.1 * <code>CachingBBConverter</code> provides some caching for the 16 salcianu 1.1.2.1 * <code>BBConverter</code>. This is THE class to use if you need to obtain 17 salcianu 1.1.2.1 * the <code>BasicBlock</code> view of the same method multiple times and 18 salcianu 1.1.2.1 * don't want to litter all your code with caching mechanisms. 19 salcianu 1.1.2.1 * 20 cananian 1.1.2.2 * @author Alexandru SALCIANU <salcianu@retezat.lcs.mit.edu> 21 cananian 1.2 * @version $Id: CachingBBConverter.java,v 1.2 2002/02/25 21:08:59 cananian Exp $ 22 salcianu 1.1.2.1 */ 23 salcianu 1.1.2.1 public class CachingBBConverter extends BBConverter{ 24 salcianu 1.1.2.1 25 salcianu 1.1.2.1 Map cache; 26 salcianu 1.1.2.1 27 salcianu 1.1.2.1 /** Creates a <code>CachingBBConverter</code>. */ 28 salcianu 1.1.2.1 public CachingBBConverter(HCodeFactory hcf){ 29 salcianu 1.1.2.1 super(hcf); 30 salcianu 1.1.2.1 cache = new HashMap(); 31 salcianu 1.1.2.1 } 32 salcianu 1.1.2.1 33 salcianu 1.1.2.1 /** Converts the code of the method <code>hm</code> to 34 salcianu 1.1.2.1 <code>BasicBlock.Factory</code>, a basic block view of 35 salcianu 1.1.2.1 <code>hm</code>'s code. The code of the method is obtained from 36 salcianu 1.1.2.1 the <code>HCodeFactory</code> that was passed to the constructor of 37 salcianu 1.1.2.1 <code>this</code>.<br> 38 salcianu 1.1.2.1 <b>Note</b>: the results are cached. */ 39 salcianu 1.1.2.3 public BasicBlockFactoryInterf convert2bb(HMethod hm){ 40 salcianu 1.1.2.3 BasicBlockFactoryInterf bb = (BasicBlockFactoryInterf) cache.get(hm); 41 salcianu 1.1.2.1 if(bb == null){ 42 salcianu 1.1.2.1 bb = super.convert2bb(hm); 43 salcianu 1.1.2.1 cache.put(hm,bb); 44 salcianu 1.1.2.1 } 45 salcianu 1.1.2.1 return bb; 46 salcianu 1.1.2.1 } 47 salcianu 1.1.2.1 48 salcianu 1.1.2.1 /** Remove from the internal cache the result for <code>hm</code>. 49 salcianu 1.1.2.1 This is useful if <code>hm</code> was modified and a new 50 salcianu 1.1.2.1 <code>BasicBlock</code> view needs to be generated for it. */ 51 salcianu 1.1.2.1 public void clear(HMethod hm){ 52 salcianu 1.1.2.1 cache.remove(hm); 53 salcianu 1.1.2.1 } 54 salcianu 1.1.2.1 55 salcianu 1.1.2.1 /** Completely clears the internal cache. */ 56 salcianu 1.1.2.1 public void clear(){ 57 salcianu 1.1.2.1 cache.clear(); 58 salcianu 1.1.2.1 } 59 cananian 1.2 }