KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > Pack


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai and Patrick Lam
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;
28
29 import soot.util.*;
30 import java.util.*;
31
32 /** A wrapper object for a pack of optimizations.
33  * Provides chain-like operations, except that the key is the phase name. */

34 public abstract class Pack implements HasPhaseOptions
35 {
36     final private boolean DEBUG = true;
37     private String JavaDoc name;
38     public String JavaDoc getPhaseName() { return name; }
39     public Pack( String JavaDoc name ) {
40         this.name = name;
41     }
42     Chain opts = new HashChain();
43     
44     public Iterator iterator() { return opts.iterator(); }
45
46     public void add(Transform t) {
47         PhaseOptions.v().getPM().notifyAddPack();
48         if( get( t.getPhaseName() ) != null ) {
49             throw new RuntimeException JavaDoc( "Phase "+t.getPhaseName()+" already "
50                     +"in pack" );
51         }
52         opts.add(t);
53     }
54
55     public void insertAfter(Transform t, String JavaDoc phaseName)
56     {
57         PhaseOptions.v().getPM().notifyAddPack();
58         Iterator it = opts.iterator();
59         while (it.hasNext())
60         {
61             Transform tr = (Transform)it.next();
62             if (tr.getPhaseName().equals(phaseName))
63             {
64                 opts.insertAfter(t, tr);
65                 return;
66             }
67         }
68         throw new RuntimeException JavaDoc("phase "+phaseName+" not found!");
69     }
70
71     public void insertBefore(Transform t, String JavaDoc phaseName)
72     {
73         PhaseOptions.v().getPM().notifyAddPack();
74         Iterator it = opts.iterator();
75         while (it.hasNext())
76         {
77             Transform tr = (Transform)it.next();
78             if (tr.getPhaseName().equals(phaseName))
79             {
80                 opts.insertBefore(t, tr);
81                 return;
82             }
83         }
84         throw new RuntimeException JavaDoc("phase "+phaseName+" not found!");
85     }
86
87     public Transform get( String JavaDoc phaseName ) {
88         for( Iterator trIt = opts.iterator(); trIt.hasNext(); ) {
89             final Transform tr = (Transform) trIt.next();
90             if( tr.getPhaseName().equals(phaseName) ) {
91                 return tr;
92             }
93         }
94         return null;
95     }
96
97     protected void internalApply() {
98         throw new RuntimeException JavaDoc("wrong type of pack");
99     }
100
101     protected void internalApply(Body b) {
102         throw new RuntimeException JavaDoc("wrong type of pack");
103     }
104
105     public final void apply() {
106         Map options = PhaseOptions.v().getPhaseOptions( this );
107         if( !PhaseOptions.getBoolean( options, "enabled" ) ) return;
108     if (DEBUG)
109         PhaseDumper.v().dumpBefore(getPhaseName());
110         internalApply();
111     if (DEBUG)
112         PhaseDumper.v().dumpAfter(getPhaseName());
113     }
114
115     public final void apply(Body b) {
116         Map options = PhaseOptions.v().getPhaseOptions( this );
117         if( !PhaseOptions.getBoolean( options, "enabled" ) ) return;
118     if (DEBUG)
119         PhaseDumper.v().dumpBefore(b, getPhaseName());
120         internalApply(b);
121     if (DEBUG)
122         PhaseDumper.v().dumpAfter(b, getPhaseName());
123     }
124
125     public String JavaDoc getDeclaredOptions() { return soot.options.Options.getDeclaredOptionsForPhase( getPhaseName() ); }
126     public String JavaDoc getDefaultOptions() { return soot.options.Options.getDefaultOptionsForPhase( getPhaseName() ); }
127 }
128
Popular Tags