KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > baf > internal > BLookupSwitchInst


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam, Patrick Pominville and Raja Vallee-Rai
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 package soot.baf.internal;
27 import soot.jimple.Constant;
28 import soot.util.*;
29 import java.util.*;
30 import soot.*;
31 import soot.baf.*;
32
33 public class BLookupSwitchInst extends AbstractInst implements LookupSwitchInst
34 {
35     UnitBox defaultTargetBox;
36     List lookupValues;
37     UnitBox[] targetBoxes;
38     List unitBoxes;
39
40     public BLookupSwitchInst(Unit defaultTarget,
41                              List lookupValues, List targets)
42     {
43         this.defaultTargetBox = Baf.v().newInstBox(defaultTarget);
44
45         this.targetBoxes = new UnitBox[targets.size()];
46
47         for(int i = 0; i < targetBoxes.length; i++)
48             this.targetBoxes[i] = Baf.v().newInstBox((Unit) targets.get(i));
49
50         this.lookupValues = new ArrayList();
51         this.lookupValues.addAll(lookupValues);
52
53         // Build up unitBoxes
54
{
55             unitBoxes = new ArrayList();
56
57             for(int i = 0; i < targetBoxes.length; i++)
58                 unitBoxes.add(targetBoxes[i]);
59
60             unitBoxes.add(defaultTargetBox);
61             unitBoxes = Collections.unmodifiableList(unitBoxes);
62         }
63     }
64
65     public Object JavaDoc clone()
66     {
67         List list = new ArrayList();
68         for(int i =0; i< targetBoxes.length; i++) {
69
70             list.add(targetBoxes[i].getUnit());
71         }
72
73         
74         return new BLookupSwitchInst(defaultTargetBox.getUnit(), lookupValues, list);
75     }
76
77
78     public int getInCount()
79     {
80         return 1;
81     }
82
83     public int getInMachineCount()
84     {
85         return 1;
86     }
87     
88     public int getOutCount()
89     {
90         return 0;
91     }
92
93     public int getOutMachineCount()
94     {
95         return 0;
96     }
97     
98     public Unit getDefaultTarget()
99     {
100         return defaultTargetBox.getUnit();
101     }
102
103     public void setDefaultTarget(Unit defaultTarget)
104     {
105         defaultTargetBox.setUnit(defaultTarget);
106     }
107
108     public UnitBox getDefaultTargetBox()
109     {
110         return defaultTargetBox;
111     }
112
113     public void setLookupValues(List lookupValues)
114     {
115         this.lookupValues = new ArrayList();
116         this.lookupValues.addAll(lookupValues);
117     }
118
119     public void setLookupValue(int index, int value)
120     {
121         this.lookupValues.set(index, new Integer JavaDoc(value));
122     }
123
124     public int getLookupValue(int index)
125     {
126         return ((Integer JavaDoc) lookupValues.get(index)).intValue();
127     }
128
129     public List getLookupValues()
130     {
131         return Collections.unmodifiableList(lookupValues);
132     }
133
134     public int getTargetCount() { return targetBoxes.length; }
135     
136     public Unit getTarget(int index)
137     {
138         return targetBoxes[index].getUnit();
139     }
140
141     public void setTarget(int index, Unit target)
142     {
143         targetBoxes[index].setUnit(target);
144     }
145
146     public void setTargets(List targets)
147     {
148         for(int i = 0; i < targets.size(); i++)
149             targetBoxes[i].setUnit((Unit) targets.get(i));
150     }
151
152     public UnitBox getTargetBox(int index)
153     {
154         return targetBoxes[index];
155     }
156
157     public List getTargets()
158     {
159         List targets = new ArrayList();
160
161         for(int i = 0; i < targetBoxes.length; i++)
162             targets.add(targetBoxes[i].getUnit());
163
164         return targets;
165     }
166
167     public String JavaDoc getName() { return "lookupswitch"; }
168
169     public String JavaDoc toString()
170     {
171         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
172         String JavaDoc endOfLine = " ";
173         
174         buffer.append("lookupswitch" + endOfLine);
175             
176         buffer.append("{" + endOfLine);
177         
178         for(int i = 0; i < lookupValues.size(); i++)
179         {
180             buffer.append(" case " + lookupValues.get(i) + ": goto " +
181                     getTarget(i) + ";" + endOfLine);
182         }
183
184         buffer.append(" default: goto " + getDefaultTarget() + ";" + endOfLine);
185         buffer.append("}");
186
187         return buffer.toString();
188     }
189
190     public void toString(UnitPrinter up) {
191         up.literal( "lookupswitch" );
192         up.newline();
193         up.literal("{");
194         up.newline();
195         
196         for(int i = 0; i < lookupValues.size(); i++)
197         {
198             up.literal(" case ");
199             up.constant( (Constant) lookupValues.get(i) );
200             up.literal(": goto ");
201             targetBoxes[i].toString(up);
202             up.literal(";");
203             up.newline();
204         }
205
206         up.literal(" default: goto ");
207         defaultTargetBox.toString(up);
208         up.literal(";");
209         up.newline();
210         up.literal("}");
211     }
212
213     public List getUnitBoxes()
214     {
215         return unitBoxes;
216     }
217
218     public void apply(Switch sw)
219     {
220         ((InstSwitch) sw).caseLookupSwitchInst(this);
221     }
222
223     public boolean fallsThrough()
224     {
225         return false;
226     }
227     public boolean branches()
228     {
229         return true;
230     }
231
232
233 }
234
Popular Tags