KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > internal > JIfStmt


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 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
28
29
30
31 package soot.jimple.internal;
32
33 import soot.*;
34 import soot.tagkit.*;
35 import soot.jimple.*;
36 import soot.baf.*;
37 import soot.jimple.*;
38 import soot.util.*;
39 import java.util.*;
40
41 public class JIfStmt extends AbstractStmt implements IfStmt
42 {
43     ValueBox conditionBox;
44     UnitBox targetBox;
45
46     List targetBoxes;
47
48     public JIfStmt(Value condition, Unit target)
49     {
50         this(Jimple.v().newConditionExprBox(condition),
51              Jimple.v().newStmtBox(target));
52     }
53
54     public JIfStmt(Value condition, UnitBox target)
55     {
56         this(Jimple.v().newConditionExprBox(condition),
57              target);
58     }
59
60
61
62     protected JIfStmt(ValueBox conditionBox, UnitBox targetBox)
63     {
64         this.conditionBox = conditionBox;
65         this.targetBox = targetBox;
66
67         targetBoxes = new ArrayList();
68         targetBoxes.add(this.targetBox);
69         targetBoxes = Collections.unmodifiableList(targetBoxes);
70     }
71     
72     public Object JavaDoc clone()
73     {
74         return new JIfStmt(Jimple.cloneIfNecessary(getCondition()), getTarget());
75     }
76     
77     public String JavaDoc toString()
78     {
79         Unit t = getTarget();
80         String JavaDoc target = "(branch)";
81         if(!t.branches())
82             target = t.toString();
83         return Jimple.v().IF + " " + getCondition().toString() + " " + Jimple.v().GOTO + " " + target;
84     }
85     
86     public void toString(UnitPrinter up) {
87         up.literal(Jimple.v().IF);
88         up.literal(" ");
89         conditionBox.toString(up);
90         up.literal(" ");
91         up.literal(Jimple.v().GOTO);
92         up.literal(" ");
93         targetBox.toString(up);
94     }
95     
96     public Value getCondition()
97     {
98         return conditionBox.getValue();
99     }
100     public void setCondition(Value condition)
101     {
102         conditionBox.setValue(condition);
103     }
104
105     public ValueBox getConditionBox()
106     {
107         return conditionBox;
108     }
109
110     public Stmt getTarget()
111     {
112         return (Stmt) targetBox.getUnit();
113     }
114
115     public void setTarget(Unit target)
116     {
117         targetBox.setUnit(target);
118     }
119
120     public UnitBox getTargetBox()
121     {
122         return targetBox;
123     }
124
125     public List getUseBoxes()
126     {
127         List useBoxes = new ArrayList();
128
129         useBoxes.addAll(conditionBox.getValue().getUseBoxes());
130         useBoxes.add(conditionBox);
131
132         return useBoxes;
133     }
134
135     public List getUnitBoxes()
136     {
137         return targetBoxes;
138     }
139
140     public void apply(Switch sw)
141     {
142         ((StmtSwitch) sw).caseIfStmt(this);
143     }
144
145     public void convertToBaf(final JimpleToBafContext context, final List out)
146     {
147         Value cond = getCondition();
148
149         final Value op1 = ((BinopExpr) cond).getOp1();
150         final Value op2 = ((BinopExpr) cond).getOp2();
151
152     context.setCurrentUnit(this);
153
154         // Handle simple subcase where op1 is null
155
if(op2 instanceof NullConstant || op1 instanceof NullConstant)
156           {
157             if(op2 instanceof NullConstant)
158               ((ConvertToBaf)op1).convertToBaf(context, out);
159             else
160               ((ConvertToBaf)op2).convertToBaf(context, out);
161             Unit u;
162
163             if(cond instanceof EqExpr)
164               u = Baf.v().newIfNullInst
165                       (Baf.v().newPlaceholderInst(getTarget()));
166             else if (cond instanceof NeExpr)
167               u = Baf.v().newIfNonNullInst
168                       (Baf.v().newPlaceholderInst(getTarget()));
169             else
170               throw new RuntimeException JavaDoc("invalid condition");
171
172             out.add(u);
173         Iterator it = getTags().iterator();
174         while(it.hasNext()) {
175         u.addTag((Tag) it.next());
176         }
177             return;
178           }
179
180         // Handle simple subcase where op2 is 0
181
if(op2 instanceof IntConstant && ((IntConstant) op2).value == 0)
182           {
183               ((ConvertToBaf)op1).convertToBaf(context, out);
184                 
185               cond.apply(new AbstractJimpleValueSwitch()
186               {
187                     private void add(Unit u)
188                     {
189                 out.add(u);
190             Iterator it = getTags().iterator();
191             while(it.hasNext()) {
192                 u.addTag((Tag) it.next());
193                         }
194                     }
195
196                     public void caseEqExpr(EqExpr expr)
197                     {
198             add(Baf.v().newIfEqInst(Baf.v().newPlaceholderInst(getTarget())));
199                     }
200         
201                     public void caseNeExpr(NeExpr expr)
202                     {
203             add(Baf.v().newIfNeInst(Baf.v().newPlaceholderInst(getTarget())));
204                     }
205         
206                     public void caseLtExpr(LtExpr expr)
207                     {
208             add(Baf.v().newIfLtInst(Baf.v().newPlaceholderInst(getTarget())));
209                     }
210                     
211                     public void caseLeExpr(LeExpr expr)
212                     {
213             add(Baf.v().newIfLeInst(Baf.v().newPlaceholderInst(getTarget())));
214                     }
215         
216                     public void caseGtExpr(GtExpr expr)
217                     {
218             add(Baf.v().newIfGtInst(Baf.v().newPlaceholderInst(getTarget())));
219                     }
220         
221                     public void caseGeExpr(GeExpr expr)
222                     {
223             add(Baf.v().newIfGeInst(Baf.v().newPlaceholderInst(getTarget())));
224                     }
225         
226                     public void defaultCase(Value v)
227                     {
228                         throw new RuntimeException JavaDoc("invalid condition " + v);
229                     }
230                 });
231                  
232                 return;
233             }
234         
235         // Handle simple subcase where op1 is 0 (flip directions)
236
if(op1 instanceof IntConstant && ((IntConstant) op1).value == 0)
237             {
238                 ((ConvertToBaf)op2).convertToBaf(context, out);
239                 
240                 cond.apply(new AbstractJimpleValueSwitch()
241                 {
242                     private void add(Unit u)
243                     {
244                 out.add(u);
245             Iterator it = getTags().iterator();
246             while(it.hasNext()) {
247                 u.addTag((Tag) it.next());
248                         }
249                     }
250
251                     public void caseEqExpr(EqExpr expr)
252                     {
253             add(Baf.v().newIfEqInst(Baf.v().newPlaceholderInst(getTarget())));
254                     }
255         
256                     public void caseNeExpr(NeExpr expr)
257                     {
258             add(Baf.v().newIfNeInst(Baf.v().newPlaceholderInst(getTarget())));
259                     }
260         
261                     public void caseLtExpr(LtExpr expr)
262                     {
263             add(Baf.v().newIfGtInst(Baf.v().newPlaceholderInst(getTarget())));
264                     }
265                     
266                     public void caseLeExpr(LeExpr expr)
267                     {
268             add(Baf.v().newIfGeInst (Baf.v().newPlaceholderInst(getTarget())));
269                     }
270         
271                     public void caseGtExpr(GtExpr expr)
272                     {
273             add(Baf.v().newIfLtInst(Baf.v().newPlaceholderInst(getTarget())));
274                     }
275         
276                     public void caseGeExpr(GeExpr expr)
277                     {
278             add(Baf.v().newIfLeInst(Baf.v().newPlaceholderInst(getTarget())));
279                     }
280         
281                     public void defaultCase(Value v)
282                     {
283                         throw new RuntimeException JavaDoc("invalid condition " + v);
284                     }
285                 });
286                  
287                 return;
288             }
289         
290         ((ConvertToBaf)op1).convertToBaf(context, out);
291         ((ConvertToBaf)op2).convertToBaf(context, out);
292     
293
294         cond.apply(new AbstractJimpleValueSwitch() {
295             private void add(Unit u)
296             {
297                 out.add(u);
298                 Iterator it = getTags().iterator();
299                 while(it.hasNext()) {
300                     u.addTag((Tag) it.next());
301                 }
302             }
303
304             public void caseEqExpr(EqExpr expr)
305         {
306                 add(Baf.v().newIfCmpEqInst(op1.getType(),
307                           Baf.v().newPlaceholderInst(getTarget())));
308             }
309
310             public void caseNeExpr(NeExpr expr)
311             {
312                 add(Baf.v().newIfCmpNeInst(op1.getType(),
313                        Baf.v().newPlaceholderInst(getTarget())));
314             }
315
316             public void caseLtExpr(LtExpr expr)
317             {
318                 add(Baf.v().newIfCmpLtInst(op1.getType(),
319                        Baf.v().newPlaceholderInst(getTarget())));
320             }
321
322             public void caseLeExpr(LeExpr expr)
323             {
324                 add(Baf.v().newIfCmpLeInst(op1.getType(),
325                        Baf.v().newPlaceholderInst(getTarget())));
326             }
327
328             public void caseGtExpr(GtExpr expr)
329             {
330                 add(Baf.v().newIfCmpGtInst(op1.getType(),
331                        Baf.v().newPlaceholderInst(getTarget())));
332             }
333
334             public void caseGeExpr(GeExpr expr)
335             {
336                 add(Baf.v().newIfCmpGeInst(op1.getType(),
337                        Baf.v().newPlaceholderInst(getTarget())));
338             }
339         });
340     
341     }
342
343
344     public boolean fallsThrough(){return true;}
345     public boolean branches(){return true;}
346
347 }
348
Popular Tags