KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > InsnUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 /**
28  * InsnUtils provides a set of static methods which serve to
29  * select vm instructions during code annotation.
30  */

31
32 public
33 class InsnUtils implements VMConstants {
34
35   /**
36    * Return the best instruction for loading the specified integer
37    * constant onto the stack - hopefully use short form
38    */

39   public static Insn integerConstant(int i, ConstantPool pool) {
40     if (i == 0)
41       return Insn.create(opc_iconst_0);
42     else if (i == 1)
43       return Insn.create(opc_iconst_1);
44     else if (i == 2)
45       return Insn.create(opc_iconst_2);
46     else if (i == 3)
47       return Insn.create(opc_iconst_3);
48     else if (i == 4)
49       return Insn.create(opc_iconst_4);
50     else if (i == 5)
51       return Insn.create(opc_iconst_5);
52     else if (i >= -128 && i < 128)
53       return Insn.create(opc_bipush, i);
54     return Insn.create(opc_ldc, pool.addInteger(i));
55   }
56
57   /**
58    * Return the best instruction for loading the specified long constant onto
59    * the stack.
60    */

61   public static Insn longConstant(long l, ConstantPool pool) {
62     if (l == 0)
63       return Insn.create(opc_lconst_0);
64     else if (l == 1)
65       return Insn.create(opc_lconst_1);
66     else
67       return Insn.create(opc_ldc2_w, pool.addLong(l));
68   }
69
70   /**
71    * Return the best instruction for loading the specified float constant onto
72    * the stack.
73    */

74   public static Insn floatConstant(float f, ConstantPool pool) {
75     if (f == 0)
76       return Insn.create(opc_fconst_0);
77     else if (f == 1)
78       return Insn.create(opc_fconst_1);
79     else if (f == 2)
80       return Insn.create(opc_fconst_2);
81     else
82       return Insn.create(opc_ldc, pool.addFloat(f));
83   }
84
85   /**
86    * Return the best instruction for loading the specified double constant onto
87    * the stack.
88    */

89   public static Insn doubleConstant(double d, ConstantPool pool) {
90     if (d == 0)
91       return Insn.create(opc_dconst_0);
92     else if (d == 1)
93       return Insn.create(opc_dconst_1);
94     else
95       return Insn.create(opc_ldc2_w, pool.addDouble(d));
96   }
97
98   /**
99    * Return the best instruction for storing a reference to a local
100    * variable slot
101    */

102   public static Insn aStore(int i, ConstantPool pool) {
103     if (i == 0)
104       return Insn.create(opc_astore_0);
105     else if (i == 1)
106       return Insn.create(opc_astore_1);
107     else if (i == 2)
108       return Insn.create(opc_astore_2);
109     else if (i == 3)
110       return Insn.create(opc_astore_3);
111     return Insn.create(opc_astore, i);
112   }
113
114   /**
115    * Return the best instruction for storing an int to a local
116    * variable slot
117    */

118   public static Insn iStore(int i, ConstantPool pool) {
119     if (i == 0)
120       return Insn.create(opc_istore_0);
121     else if (i == 1)
122       return Insn.create(opc_istore_1);
123     else if (i == 2)
124       return Insn.create(opc_istore_2);
125     else if (i == 3)
126       return Insn.create(opc_istore_3);
127     return Insn.create(opc_istore, i);
128   }
129
130   /**
131    * Return the best instruction for storing a float to a local
132    * variable slot
133    */

134   public static Insn fStore(int i, ConstantPool pool) {
135     if (i == 0)
136       return Insn.create(opc_fstore_0);
137     else if (i == 1)
138       return Insn.create(opc_fstore_1);
139     else if (i == 2)
140       return Insn.create(opc_fstore_2);
141     else if (i == 3)
142       return Insn.create(opc_fstore_3);
143     return Insn.create(opc_fstore, i);
144   }
145
146   /**
147    * Return the best instruction for storing a long to a local
148    * variable slot
149    */

150   public static Insn lStore(int i, ConstantPool pool) {
151     if (i == 0)
152       return Insn.create(opc_lstore_0);
153     else if (i == 1)
154       return Insn.create(opc_lstore_1);
155     else if (i == 2)
156       return Insn.create(opc_lstore_2);
157     else if (i == 3)
158       return Insn.create(opc_lstore_3);
159     return Insn.create(opc_lstore, i);
160   }
161
162   /**
163    * Return the best instruction for storing a double to a local
164    * variable slot
165    */

166   public static Insn dStore(int i, ConstantPool pool) {
167     if (i == 0)
168       return Insn.create(opc_dstore_0);
169     else if (i == 1)
170       return Insn.create(opc_dstore_1);
171     else if (i == 2)
172       return Insn.create(opc_dstore_2);
173     else if (i == 3)
174       return Insn.create(opc_dstore_3);
175     return Insn.create(opc_dstore, i);
176   }
177
178   /**
179    * Return the best instruction for loading a reference from a local
180    * variable slot
181    */

182   public static Insn aLoad(int i, ConstantPool pool) {
183     if (i == 0)
184       return Insn.create(opc_aload_0);
185     else if (i == 1)
186       return Insn.create(opc_aload_1);
187     else if (i == 2)
188       return Insn.create(opc_aload_2);
189     else if (i == 3)
190       return Insn.create(opc_aload_3);
191     return Insn.create(opc_aload, i);
192   }
193
194   /**
195    * Return the best instruction for loading an int from a local
196    * variable slot
197    */

198   public static Insn iLoad(int i, ConstantPool pool) {
199     if (i == 0)
200       return Insn.create(opc_iload_0);
201     else if (i == 1)
202       return Insn.create(opc_iload_1);
203     else if (i == 2)
204       return Insn.create(opc_iload_2);
205     else if (i == 3)
206       return Insn.create(opc_iload_3);
207     return Insn.create(opc_iload, i);
208   }
209
210   /**
211    * Return the best instruction for loading a float from a local
212    * variable slot
213    */

214   public static Insn fLoad(int i, ConstantPool pool) {
215     if (i == 0)
216       return Insn.create(opc_fload_0);
217     else if (i == 1)
218       return Insn.create(opc_fload_1);
219     else if (i == 2)
220       return Insn.create(opc_fload_2);
221     else if (i == 3)
222       return Insn.create(opc_fload_3);
223     return Insn.create(opc_fload, i);
224   }
225
226   /**
227    * Return the best instruction for loading a long from a local
228    * variable slot
229    */

230   public static Insn lLoad(int i, ConstantPool pool) {
231     if (i == 0)
232       return Insn.create(opc_lload_0);
233     else if (i == 1)
234       return Insn.create(opc_lload_1);
235     else if (i == 2)
236       return Insn.create(opc_lload_2);
237     else if (i == 3)
238       return Insn.create(opc_lload_3);
239     return Insn.create(opc_lload, i);
240   }
241
242   /**
243    * Return the best instruction for loading a double from a local
244    * variable slot
245    */

246   public static Insn dLoad(int i, ConstantPool pool) {
247     if (i == 0)
248       return Insn.create(opc_dload_0);
249     else if (i == 1)
250       return Insn.create(opc_dload_1);
251     else if (i == 2)
252       return Insn.create(opc_dload_2);
253     else if (i == 3)
254       return Insn.create(opc_dload_3);
255     return Insn.create(opc_dload, i);
256   }
257
258   /**
259    * Return the best instruction for loading a value from a local
260    * variable slot
261    */

262   public static Insn load(int tp, int i, ConstantPool pool) {
263     switch(tp) {
264     //@olsen: added these cases:
265
case T_BOOLEAN:
266     case T_CHAR:
267     case T_BYTE:
268     case T_SHORT:
269     //@olsen: end added cases
270
case T_INT:
271       return iLoad(i, pool);
272     case T_FLOAT:
273       return fLoad(i, pool);
274     case T_DOUBLE:
275       return dLoad(i, pool);
276     case T_LONG:
277       return lLoad(i, pool);
278     case TC_OBJECT:
279       return aLoad(i, pool);
280     default:
281         throw new InsnError("bad load type");//NOI18N
282
}
283   }
284
285
286   /**
287    * Return the best instruction for storing a value to a local
288    * variable slot
289    */

290   public static Insn store(int tp, int i, ConstantPool pool) {
291     switch(tp) {
292     //@olsen: added these cases:
293
case T_BOOLEAN:
294     case T_CHAR:
295     case T_BYTE:
296     case T_SHORT:
297     //@olsen: end added cases
298
case T_INT:
299       return iStore(i, pool);
300     case T_FLOAT:
301       return fStore(i, pool);
302     case T_DOUBLE:
303       return dStore(i, pool);
304     case T_LONG:
305       return lStore(i, pool);
306     case TC_OBJECT:
307       return aStore(i, pool);
308     default:
309         throw new InsnError("bad store type");//NOI18N
310
}
311   }
312 }
313
314
Popular Tags