KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > shimple > ShimpleBody


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Navindra Umanee <navindra@cs.mcgill.ca>
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 package soot.shimple;
21
22 import soot.*;
23 import soot.options.*;
24 import soot.jimple.*;
25 import soot.jimple.internal.*;
26 import soot.jimple.toolkits.scalar.*;
27 import soot.shimple.internal.*;
28 import soot.shimple.toolkits.scalar.*;
29 import soot.toolkits.scalar.*;
30 import soot.util.*;
31 import java.util.*;
32
33 // * <p> We decided to hide all the intelligence in
34
// * internal.ShimpleBodyBuilder for clarity of API. Eventually we will
35
// * likely switch to an explicit Strategy pattern that will allow us to
36
// * select different SSA behaviours and algorithms.
37
/**
38  * Implementation of the Body class for the SSA Shimple IR.
39  * This class provides methods for maintaining SSA form as well as
40  * eliminating SSA form.
41  *
42  * @author Navindra Umanee
43  * @see soot.shimple.internal.ShimpleBodyBuilder
44  * @see <a
45  * HREF="http://citeseer.nj.nec.com/cytron91efficiently.html">Efficiently
46  * Computing Static Single Assignment Form and the Control Dependence
47  * Graph</a>
48  **/

49 public class ShimpleBody extends StmtBody
50 {
51     /**
52      * Holds our options map...
53      **/

54     protected ShimpleOptions options;
55
56     protected ShimpleBodyBuilder sbb;
57     
58     protected boolean isExtendedSSA = false;
59     
60     /**
61      * Construct an empty ShimpleBody associated with m.
62      **/

63     ShimpleBody(SootMethod m, Map options)
64     {
65         super(m);
66
67         // must happen before SPatchingChain gets created
68
this.options = new ShimpleOptions(options);
69         setSSA(true);
70         isExtendedSSA = this.options.extended();
71         
72         unitChain = new SPatchingChain(this, new HashChain());
73         sbb = new ShimpleBodyBuilder(this);
74     }
75
76     /**
77      * Constructs a ShimpleBody from the given Body and options.
78      *
79      * <p> Currently available option is "naive-phi-elimination",
80      * typically in the "shimple" phase (eg, -p shimple
81      * naive-phi-elimination) which can be useful for understanding
82      * the effect of analyses.
83      **/

84     ShimpleBody(Body body, Map options)
85     {
86         super(body.getMethod());
87
88         if (!(body instanceof JimpleBody || body instanceof ShimpleBody))
89             throw new RuntimeException JavaDoc("Cannot construct ShimpleBody from given Body type.");
90
91         if(Options.v().verbose())
92             G.v().out.println("[" + getMethod().getName() + "] Constructing ShimpleBody...");
93
94         // must happen before SPatchingChain gets created
95
this.options = new ShimpleOptions(options);
96
97         unitChain = new SPatchingChain(this, new HashChain());
98         importBodyContentsFrom(body);
99
100         /* Shimplise body */
101         sbb = new ShimpleBodyBuilder(this);
102
103         if(body instanceof ShimpleBody)
104             rebuild(true);
105         else
106             rebuild(false);
107     }
108     
109     /**
110      * Recompute SSA form.
111      *
112      * <p> Note: assumes presence of Phi nodes in body that require
113      * elimination. If you *know* there are no Phi nodes present,
114      * you may prefer to use rebuild(false) in order to skip some
115      * transformations during the Phi elimination process.
116      **/

117     public void rebuild()
118     {
119         rebuild(true);
120     }
121
122     /**
123      * Rebuild SSA form.
124      *
125      * <p> If there are Phi nodes already present in the body, it is
126      * imperative that we specify this so that the algorithm can
127      * eliminate them before rebuilding SSA.
128      *
129      * <p> The eliminate Phi nodes stage is harmless, but if you
130      * *know* that no Phi nodes are present and you wish to avoid the
131      * transformations involved in eliminating Phi nodes, use
132      * rebuild(false).
133      **/

134     public void rebuild(boolean hasPhiNodes)
135     {
136         isExtendedSSA = options.extended();
137         sbb.transform();
138         setSSA(true);
139     }
140     
141     /**
142      * Returns an equivalent unbacked JimpleBody of the current Body
143      * by eliminating the Phi nodes.
144      *
145      * <p> Currently available option is "naive-phi-elimination",
146      * typically specified in the "shimple" phase (eg, -p shimple
147      * naive-phi-elimination) which skips the dead code elimination
148      * and register allocation phase before eliminating Phi nodes.
149      * This can be useful for understanding the effect of analyses.
150      *
151      * <p> Remember to setActiveBody() if necessary in your
152      * SootMethod.
153      *
154      * @see #eliminatePhiNodes()
155      **/

156     public JimpleBody toJimpleBody()
157     {
158         ShimpleBody sBody = (ShimpleBody) this.clone();
159
160         sBody.eliminateNodes();
161         JimpleBody jBody = Jimple.v().newBody(sBody.getMethod());
162         jBody.importBodyContentsFrom(sBody);
163         return jBody;
164     }
165
166     /**
167      * Remove Phi nodes from body. SSA form is no longer a given
168      * once done.
169      *
170      * <p> Currently available option is "naive-phi-elimination",
171      * typically specified in the "shimple" phase (eg, -p shimple
172      * naive-phi-elimination) which skips the dead code elimination
173      * and register allocation phase before eliminating Phi nodes.
174      * This can be useful for understanding the effect of analyses.
175      *
176      * @see #toJimpleBody()
177      **/

178     public void eliminatePhiNodes()
179     {
180         sbb.preElimOpt();
181         sbb.eliminatePhiNodes();
182         sbb.postElimOpt();
183         setSSA(false);
184     }
185
186     public void eliminatePiNodes()
187     {
188         sbb.eliminatePiNodes();
189     }
190     
191     public void eliminateNodes()
192     {
193         sbb.preElimOpt();
194         sbb.eliminatePhiNodes();
195         if(isExtendedSSA)
196             sbb.eliminatePiNodes();
197         sbb.postElimOpt();
198         setSSA(false);
199     }
200     
201
202     /**
203      * Returns a copy of the current ShimpleBody.
204      **/

205     public Object JavaDoc clone()
206     {
207         Body b = Shimple.v().newBody(getMethod());
208         b.importBodyContentsFrom(this);
209         return b;
210     }
211
212     /**
213      * Set isSSA boolean to indicate whether a ShimpleBody is still in SSA
214      * form or not. Could be useful for book-keeping purposes.
215      **/

216     protected boolean isSSA = false;
217
218     /**
219      * Sets a flag that indicates whether ShimpleBody is still in SSA
220      * form after a transformation or not. It is often up to the user
221      * to indicate if a body is no longer in SSA form.
222      **/

223     public void setSSA(boolean isSSA)
224     {
225         this.isSSA = isSSA;
226     }
227
228     /**
229      * Returns value of, optional, user-maintained SSA boolean.
230      *
231      * @see #setSSA(boolean)
232      **/

233     public boolean isSSA()
234     {
235         return isSSA;
236     }
237
238     public boolean isExtendedSSA()
239     {
240         return isExtendedSSA;
241     }
242     
243     /**
244      * Returns the Shimple options applicable to this body.
245      **/

246     public ShimpleOptions getOptions()
247     {
248         return options;
249     }
250
251     /**
252      * Make sure the locals in this body all have unique String names.
253      * If the standard-local-names option is specified to Shimple,
254      * this results in the LocalNameStandardizer being applied.
255      * Otherwise, renaming is kept to a minimum and an underscore
256      * notation is used to differentiate locals previously of the same
257      * name.
258      *
259      * @see soot.jimple.toolkits.scalar.LocalNameStandardizer
260      **/

261     public void makeUniqueLocalNames()
262     {
263         sbb.makeUniqueLocalNames();
264     }
265 }
266
Popular Tags