001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1999 Patrice Pominville
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.baf.toolkits.base;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: import soot.*;
032:
033: /**
034: * Driver class to run peepholes on the Baf IR. The peepholes applied
035: * must implement the Peephole interface. Peepholes are loaded dynamically
036: * by the soot runtime; the runtime reads the file peephole.dat, in order to
037: * determine which peepholes to apply.
038: *
039: * @see Peephole
040: * @see ExamplePeephole
041: */
042:
043: public class PeepholeOptimizer extends BodyTransformer {
044: public PeepholeOptimizer(Singletons.Global g) {
045: }
046:
047: public static PeepholeOptimizer v() {
048: return G.v().soot_baf_toolkits_base_PeepholeOptimizer();
049: }
050:
051: private InputStream peepholeListingStream = null;
052: private final String packageName = "soot.baf.toolkits.base";
053:
054: private final Map<String, Class> peepholeMap = new HashMap<String, Class>();
055:
056: /** The method that drives the optimizations. */
057: /* This is the public interface to PeepholeOptimizer */
058:
059: protected void internalTransform(Body body, String phaseName,
060: Map options) {
061: boolean changed = true;
062: BufferedReader reader = null;
063:
064: peepholeListingStream = PeepholeOptimizer.class
065: .getResourceAsStream("peephole.dat");
066: if (peepholeListingStream == null)
067: throw new RuntimeException(
068: "could not open file peephole.dat!");
069: reader = new BufferedReader(new InputStreamReader(
070: peepholeListingStream));
071:
072: String line = null;
073: List<String> peepholes = new LinkedList<String>();
074: try {
075: line = reader.readLine();
076: while (line != null) {
077: if (line.length() > 0)
078: if (!(line.charAt(0) == '#'))
079: peepholes.add(line);
080: line = reader.readLine();
081: }
082: } catch (IOException e) {
083: throw new RuntimeException(
084: "IO error occured while reading file: " + line
085: + System.getProperty("line.separator") + e);
086: }
087:
088: while (changed) {
089: changed = false;
090:
091: Iterator<String> it = peepholes.iterator();
092: while (it.hasNext()) {
093:
094: boolean peepholeWorked = true;
095: String peepholeName = it.next();
096:
097: while (peepholeWorked) {
098: peepholeWorked = false;
099:
100: Class peepholeClass;
101: if ((peepholeClass = peepholeMap.get(peepholeName)) == null) {
102: try {
103: peepholeClass = Class.forName(packageName
104: + "." + 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: peepholeListingStream.close();
128: } catch (IOException e) {
129: }
130: }
131: }
|