001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai and Patrick Lam
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-2003.
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;
027:
028: import java.util.*;
029: import soot.util.*;
030: import soot.options.Options;
031:
032: /** Maintains the pair (phaseName, singleton) needed for a
033: * transformation. */
034: public class Transform implements HasPhaseOptions {
035: final private boolean DEBUG = true;
036: String phaseName;
037: Transformer t;
038:
039: public Transform(String phaseName, Transformer t) {
040: this .phaseName = phaseName;
041: this .t = t;
042: }
043:
044: public String getPhaseName() {
045: return phaseName;
046: }
047:
048: public Transformer getTransformer() {
049: return t;
050: }
051:
052: private String declaredOpts;
053: private String defaultOpts;
054:
055: public String getDeclaredOptions() {
056: if (declaredOpts != null)
057: return declaredOpts;
058: return Options.getDeclaredOptionsForPhase(phaseName);
059: }
060:
061: public String getDefaultOptions() {
062: if (defaultOpts != null)
063: return defaultOpts;
064: return Options.getDefaultOptionsForPhase(phaseName);
065: }
066:
067: /** Allows user-defined phases to have options other than just enabled
068: * without having to mess with the XML.
069: * Call this method with a space-separated list of options declared
070: * for this Transform. Only declared options may be passed to this
071: * transform as a phase option. */
072: public void setDeclaredOptions(String options) {
073: declaredOpts = options;
074: }
075:
076: /** Allows user-defined phases to have options other than just
077: * enabled without having to mess with the XML. Call this method
078: * with a space-separated list of option:value pairs that this
079: * Transform is to use as default parameters (eg
080: * `enabled:off'). */
081: public void setDefaultOptions(String options) {
082: defaultOpts = options;
083: }
084:
085: public void apply() {
086: Map options = PhaseOptions.v().getPhaseOptions(phaseName);
087: if (PhaseOptions.getBoolean(options, "enabled")) {
088: if (Options.v().verbose()) {
089: G.v().out.println("Applying phase " + phaseName
090: + " to the scene.");
091: }
092: }
093: if (DEBUG)
094: PhaseDumper.v().dumpBefore(getPhaseName());
095:
096: ((SceneTransformer) t).transform(phaseName, options);
097:
098: if (DEBUG)
099: PhaseDumper.v().dumpAfter(getPhaseName());
100: }
101:
102: public void apply(Body b) {
103: Map options = PhaseOptions.v().getPhaseOptions(phaseName);
104: if (PhaseOptions.getBoolean(options, "enabled")) {
105: if (Options.v().verbose()) {
106: G.v().out.println("Applying phase " + phaseName
107: + " to " + b.getMethod() + ".");
108: }
109: }
110: if (DEBUG)
111: PhaseDumper.v().dumpBefore(b, getPhaseName());
112:
113: ((BodyTransformer) t).transform(b, phaseName, options);
114:
115: if (DEBUG)
116: PhaseDumper.v().dumpAfter(b, getPhaseName());
117: }
118: }
|