KickJava   Java API By Example, From Geeks To Geeks.

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


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