1 19 20 25 26 27 package soot.baf.toolkits.base; 28 29 import java.util.*; 30 import java.io.*; 31 32 import soot.*; 33 import soot.util.*; 34 import soot.baf.*; 35 import soot.toolkits.scalar.*; 36 import soot.toolkits.graph.*; 37 import soot.baf.internal.*; 38 39 48 49 public class PeepholeOptimizer extends BodyTransformer 50 { 51 public PeepholeOptimizer( Singletons.Global g ) {} 52 public static PeepholeOptimizer v() { return G.v().soot_baf_toolkits_base_PeepholeOptimizer(); } 53 54 private InputStream peepholeListingStream = null; 55 private final String packageName = "soot.baf.toolkits.base"; 56 57 private Map peepholeMap = new HashMap(); 58 59 60 61 62 protected void internalTransform(Body body, String phaseName, Map options) 63 { 64 boolean changed = true; 65 BufferedReader reader = null; 66 67 peepholeListingStream = PeepholeOptimizer.class.getResourceAsStream("peephole.dat"); 68 if (peepholeListingStream == null) 69 throw new RuntimeException ("could not open file peephole.dat!"); 70 reader = new BufferedReader(new InputStreamReader(peepholeListingStream)); 71 72 String line = null; 73 List peepholes = new LinkedList(); 74 try { 75 line = reader.readLine(); 76 while(line != null) { 77 if(line.length() > 0) 78 if(!(line.charAt(0) == '#')) 79 peepholes.add(line); 80 line = reader.readLine(); 81 } 82 } catch (IOException e) { 83 throw new RuntimeException ("IO error occured while reading file: " + 84 line + System.getProperty("line.separator") + e); 85 } 86 87 88 while(changed) { 89 changed = false; 90 91 Iterator it = peepholes.iterator(); 92 while(it.hasNext()) { 93 94 boolean peepholeWorked = true; 95 String peepholeName = (String ) it.next(); 96 97 while(peepholeWorked) { 98 peepholeWorked = false; 99 100 101 Class peepholeClass; 102 if((peepholeClass = (Class ) peepholeMap.get(peepholeName)) == null) { 103 try { 104 peepholeClass = (Class ) Class.forName(packageName + "." + peepholeName); 105 } catch (ClassNotFoundException e) { 106 throw new RuntimeException (e.toString()); 107 } 108 peepholeMap.put(peepholeName, peepholeClass); 109 } 110 111 Peephole p = null; 112 try { 113 p = (Peephole) peepholeClass.newInstance(); 114 } catch (IllegalAccessException e) { 115 throw new RuntimeException (e.toString()); 116 } catch (InstantiationException e) { 117 throw new RuntimeException (e.toString()); 118 } 119 if(p.apply(body)) { 120 peepholeWorked = true; 121 changed = true; 122 } 123 } 124 } 125 } 126 try 127 { 128 peepholeListingStream.close(); 129 } 130 catch (IOException e) 131 {} 132 } 133 } 134 | Popular Tags |