KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > baf > toolkits > base > PeepholeOptimizer


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrice Pominville
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

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 /**
40  * Driver class to run peepholes on the Baf IR. The peepholes applied
41  * must implement the Peephole interface. Peepholes are loaded dynamically
42  * by the soot runtime; the runtime reads the file peephole.dat, in order to
43  * determine which peepholes to apply.
44  *
45  * @see Peephole
46  * @see ExamplePeephole
47  */

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 JavaDoc packageName = "soot.baf.toolkits.base";
56
57     private Map peepholeMap = new HashMap();
58
59     /** The method that drives the optimizations. */
60     /* This is the public interface to PeepholeOptimizer */
61   
62     protected void internalTransform(Body body, String JavaDoc 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 JavaDoc("could not open file peephole.dat!");
70         reader = new BufferedReader(new InputStreamReader(peepholeListingStream));
71
72         String JavaDoc 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 JavaDoc("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 JavaDoc peepholeName = (String JavaDoc) it.next();
96                 
97                 while(peepholeWorked) {
98                     peepholeWorked = false;
99
100                 
101                     Class JavaDoc peepholeClass;
102                     if((peepholeClass = (Class JavaDoc) peepholeMap.get(peepholeName)) == null) {
103                         try {
104                             peepholeClass = (Class JavaDoc) Class.forName(packageName + "." + peepholeName);
105                         } catch (ClassNotFoundException JavaDoc e) {
106                             throw new RuntimeException JavaDoc(e.toString());
107                         }
108                         peepholeMap.put(peepholeName, peepholeClass);
109                     }
110                     
111                     Peephole p = null;
112                     try {
113                         p = (Peephole) peepholeClass.newInstance();
114                     } catch (IllegalAccessException JavaDoc e) {
115                         throw new RuntimeException JavaDoc(e.toString());
116                     } catch (InstantiationException JavaDoc e) {
117                         throw new RuntimeException JavaDoc(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