Source code: com/techtrader/modules/tools/bytecode/WideInstruction.java
1 package com.techtrader.modules.tools.bytecode;
2
3
4 import java.io.IOException;
5 import java.io.DataInput;
6 import java.io.DataOutput;
7
8 import com.techtrader.modules.tools.bytecode.visitor.BCVisitor;
9
10
11 /**
12 * Representation of the WIDE instruction, which is used to allow other
13 * instructions to index values beyond what they can normally index baed
14 * on the length of their arguments.
15 *
16 * @author Abe White
17 */
18 public class WideInstruction
19 extends IIncInstruction
20 {
21 private int _ins = 0;
22
23
24 protected WideInstruction (Code owner)
25 {
26 super (owner);
27 _opcode = WIDE;
28 }
29
30
31 /**
32 * Set the type of instruction this wide instruction modifies.
33 *
34 * @return this Instruction, for method chaining
35 */
36 public WideInstruction iinc ()
37 {
38 _ins = IINC;
39 return this;
40 }
41
42
43 /**
44 * Set the type of instruction this wide instruction modifies.
45 *
46 * @return this Instruction, for method chaining
47 */
48 public WideInstruction ret ()
49 {
50 _ins = RET;
51 return this;
52 }
53
54
55 /**
56 * Set the type of instruction this wide instruction modifies.
57 *
58 * @return this Instruction, for method chaining
59 */
60 public WideInstruction iload ()
61 {
62 _ins = ILOAD;
63 return this;
64 }
65
66
67 /**
68 * Set the type of instruction this wide instruction modifies.
69 *
70 * @return this Instruction, for method chaining
71 */
72 public WideInstruction fload ()
73 {
74 _ins = FLOAD;
75 return this;
76 }
77
78
79 /**
80 * Set the type of instruction this wide instruction modifies.
81 *
82 * @return this Instruction, for method chaining
83 */
84 public WideInstruction aload ()
85 {
86 _ins = ALOAD;
87 return this;
88 }
89
90
91 /**
92 * Set the type of instruction this wide instruction modifies.
93 *
94 * @return this Instruction, for method chaining
95 */
96 public WideInstruction lload ()
97 {
98 _ins = LLOAD;
99 return this;
100 }
101
102
103 /**
104 * Set the type of instruction this wide instruction modifies.
105 *
106 * @return this Instruction, for method chaining
107 */
108 public WideInstruction dload ()
109 {
110 _ins = DLOAD;
111 return this;
112 }
113
114
115 /**
116 * Set the type of instruction this wide instruction modifies.
117 *
118 * @return this Instruction, for method chaining
119 */
120 public WideInstruction istore ()
121 {
122 _ins = ISTORE;
123 return this;
124 }
125
126
127 /**
128 * Set the type of instruction this wide instruction modifies.
129 *
130 * @return this Instruction, for method chaining
131 */
132 public WideInstruction fstore ()
133 {
134 _ins = FSTORE;
135 return this;
136 }
137
138
139 /**
140 * Set the type of instruction this wide instruction modifies.
141 *
142 * @return this Instruction, for method chaining
143 */
144 public WideInstruction astore ()
145 {
146 _ins = ASTORE;
147 return this;
148 }
149
150
151 /**
152 * Set the type of instruction this wide instruction modifies.
153 *
154 * @return this Instruction, for method chaining
155 */
156 public WideInstruction lstore ()
157 {
158 _ins = LSTORE;
159 return this;
160 }
161
162
163 /**
164 * Set the type of instruction this wide instruction modifies.
165 *
166 * @return this Instruction, for method chaining
167 */
168 public WideInstruction dstore ()
169 {
170 _ins = DSTORE;
171 return this;
172 }
173
174
175 /**
176 * Set the code of the instruction to modify. Should be one of:
177 * IINC, RET, ILOAD, ALOAD, FLOAD, DLOAD, LLOAD, ISTORE, ASTORE, FSTORE,
178 * DSTORE, LSTORE.
179 *
180 * @return this Instruction, for method chaining
181 */
182 public WideInstruction setInstruction (int code)
183 {
184 _ins = code;
185 return this;
186 }
187
188
189 /**
190 * Get the code of the instruction to modify; this will return one
191 * of the constants defined in {@link Constants}.
192 */
193 public int getInstruction ()
194 {
195 return _ins;
196 }
197
198
199 protected void copy (Instruction orig)
200 {
201 super.copy (orig);
202 setInstruction (((WideInstruction) orig).getInstruction ());
203 }
204
205
206 public int getLength ()
207 {
208 // don't call super
209
210 // opcode, ins, index
211 int length = 1 + 1 + 2;
212
213 // increment
214 if (_ins == IINC)
215 length += 2;
216
217 return length;
218 }
219
220
221 public int getStackChange ()
222 {
223 switch (_ins)
224 {
225 case ILOAD:
226 case FLOAD:
227 case ALOAD:
228 return 1;
229
230 case LLOAD:
231 case DLOAD:
232 return 2;
233
234 case ISTORE:
235 case FSTORE:
236 case ASTORE:
237 return -1;
238
239 case LSTORE:
240 case DSTORE:
241 return -2;
242
243 default:
244 return 0;
245 }
246 }
247
248
249 protected void readData (DataInput in)
250 throws IOException
251 {
252 // don't call super
253
254 _ins = in.readUnsignedByte ();
255 setIndex (in.readUnsignedShort ());
256 if (_ins == IINC)
257 setIncrement (in.readUnsignedShort ());
258 }
259
260
261 protected void writeData (DataOutput out)
262 throws IOException
263 {
264 // don't call super
265
266 out.writeByte (_ins);
267 out.writeShort (getIndex ());
268 if (_ins == IINC)
269 out.writeShort (getIncrement ());
270 }
271
272
273 public void acceptVisit (BCVisitor visit)
274 {
275 visit.enterWideInstruction (this);
276 visit.exitWideInstruction (this);
277 }
278 }