KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > Opcode


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile;
18
19 /**
20  * A collection of opcode constants for creating class file byte code. These
21  * opcodes are defined in chapter 6 of
22  * <i>The Java Virual Machine Specification</i>.
23  *
24  * @author Brian S O'Neill
25  * @see cojen.classfile.attribute.CodeAttr
26  */

27 public class Opcode {
28     public final static byte NOP = (byte)0;
29     public final static byte ACONST_NULL = (byte)1;
30     public final static byte ICONST_M1 = (byte)2;
31     public final static byte ICONST_0 = (byte)3;
32     public final static byte ICONST_1 = (byte)4;
33     public final static byte ICONST_2 = (byte)5;
34     public final static byte ICONST_3 = (byte)6;
35     public final static byte ICONST_4 = (byte)7;
36     public final static byte ICONST_5 = (byte)8;
37     public final static byte LCONST_0 = (byte)9;
38     public final static byte LCONST_1 = (byte)10;
39     public final static byte FCONST_0 = (byte)11;
40     public final static byte FCONST_1 = (byte)12;
41     public final static byte FCONST_2 = (byte)13;
42     public final static byte DCONST_0 = (byte)14;
43     public final static byte DCONST_1 = (byte)15;
44     public final static byte BIPUSH = (byte)16;
45     public final static byte SIPUSH = (byte)17;
46     public final static byte LDC = (byte)18;
47     public final static byte LDC_W = (byte)19;
48     public final static byte LDC2_W = (byte)20;
49     public final static byte ILOAD = (byte)21;
50     public final static byte LLOAD = (byte)22;
51     public final static byte FLOAD = (byte)23;
52     public final static byte DLOAD = (byte)24;
53     public final static byte ALOAD = (byte)25;
54     public final static byte ILOAD_0 = (byte)26;
55     public final static byte ILOAD_1 = (byte)27;
56     public final static byte ILOAD_2 = (byte)28;
57     public final static byte ILOAD_3 = (byte)29;
58     public final static byte LLOAD_0 = (byte)30;
59     public final static byte LLOAD_1 = (byte)31;
60     public final static byte LLOAD_2 = (byte)32;
61     public final static byte LLOAD_3 = (byte)33;
62     public final static byte FLOAD_0 = (byte)34;
63     public final static byte FLOAD_1 = (byte)35;
64     public final static byte FLOAD_2 = (byte)36;
65     public final static byte FLOAD_3 = (byte)37;
66     public final static byte DLOAD_0 = (byte)38;
67     public final static byte DLOAD_1 = (byte)39;
68     public final static byte DLOAD_2 = (byte)40;
69     public final static byte DLOAD_3 = (byte)41;
70     public final static byte ALOAD_0 = (byte)42;
71     public final static byte ALOAD_1 = (byte)43;
72     public final static byte ALOAD_2 = (byte)44;
73     public final static byte ALOAD_3 = (byte)45;
74     public final static byte IALOAD = (byte)46;
75     public final static byte LALOAD = (byte)47;
76     public final static byte FALOAD = (byte)48;
77     public final static byte DALOAD = (byte)49;
78     public final static byte AALOAD = (byte)50;
79     public final static byte BALOAD = (byte)51;
80     public final static byte CALOAD = (byte)52;
81     public final static byte SALOAD = (byte)53;
82     public final static byte ISTORE = (byte)54;
83     public final static byte LSTORE = (byte)55;
84     public final static byte FSTORE = (byte)56;
85     public final static byte DSTORE = (byte)57;
86     public final static byte ASTORE = (byte)58;
87     public final static byte ISTORE_0 = (byte)59;
88     public final static byte ISTORE_1 = (byte)60;
89     public final static byte ISTORE_2 = (byte)61;
90     public final static byte ISTORE_3 = (byte)62;
91     public final static byte LSTORE_0 = (byte)63;
92     public final static byte LSTORE_1 = (byte)64;
93     public final static byte LSTORE_2 = (byte)65;
94     public final static byte LSTORE_3 = (byte)66;
95     public final static byte FSTORE_0 = (byte)67;
96     public final static byte FSTORE_1 = (byte)68;
97     public final static byte FSTORE_2 = (byte)69;
98     public final static byte FSTORE_3 = (byte)70;
99     public final static byte DSTORE_0 = (byte)71;
100     public final static byte DSTORE_1 = (byte)72;
101     public final static byte DSTORE_2 = (byte)73;
102     public final static byte DSTORE_3 = (byte)74;
103     public final static byte ASTORE_0 = (byte)75;
104     public final static byte ASTORE_1 = (byte)76;
105     public final static byte ASTORE_2 = (byte)77;
106     public final static byte ASTORE_3 = (byte)78;
107     public final static byte IASTORE = (byte)79;
108     public final static byte LASTORE = (byte)80;
109     public final static byte FASTORE = (byte)81;
110     public final static byte DASTORE = (byte)82;
111     public final static byte AASTORE = (byte)83;
112     public final static byte BASTORE = (byte)84;
113     public final static byte CASTORE = (byte)85;
114     public final static byte SASTORE = (byte)86;
115     public final static byte POP = (byte)87;
116     public final static byte POP2 = (byte)88;
117     public final static byte DUP = (byte)89;
118     public final static byte DUP_X1 = (byte)90;
119     public final static byte DUP_X2 = (byte)91;
120     public final static byte DUP2 = (byte)92;
121     public final static byte DUP2_X1 = (byte)93;
122     public final static byte DUP2_X2 = (byte)94;
123     public final static byte SWAP = (byte)95;
124     public final static byte IADD = (byte)96;
125     public final static byte LADD = (byte)97;
126     public final static byte FADD = (byte)98;
127     public final static byte DADD = (byte)99;
128     public final static byte ISUB = (byte)100;
129     public final static byte LSUB = (byte)101;
130     public final static byte FSUB = (byte)102;
131     public final static byte DSUB = (byte)103;
132     public final static byte IMUL = (byte)104;
133     public final static byte LMUL = (byte)105;
134     public final static byte FMUL = (byte)106;
135     public final static byte DMUL = (byte)107;
136     public final static byte IDIV = (byte)108;
137     public final static byte LDIV = (byte)109;
138     public final static byte FDIV = (byte)110;
139     public final static byte DDIV = (byte)111;
140     public final static byte IREM = (byte)112;
141     public final static byte LREM = (byte)113;
142     public final static byte FREM = (byte)114;
143     public final static byte DREM = (byte)115;
144     public final static byte INEG = (byte)116;
145     public final static byte LNEG = (byte)117;
146     public final static byte FNEG = (byte)118;
147     public final static byte DNEG = (byte)119;
148     public final static byte ISHL = (byte)120;
149     public final static byte LSHL = (byte)121;
150     public final static byte ISHR = (byte)122;
151     public final static byte LSHR = (byte)123;
152     public final static byte IUSHR = (byte)124;
153     public final static byte LUSHR = (byte)125;
154     public final static byte IAND = (byte)126;
155     public final static byte LAND = (byte)127;
156     public final static byte IOR = (byte)128;
157     public final static byte LOR = (byte)129;
158     public final static byte IXOR = (byte)130;
159     public final static byte LXOR = (byte)131;
160     public final static byte IINC = (byte)132;
161     public final static byte I2L = (byte)133;
162     public final static byte I2F = (byte)134;
163     public final static byte I2D = (byte)135;
164     public final static byte L2I = (byte)136;
165     public final static byte L2F = (byte)137;
166     public final static byte L2D = (byte)138;
167     public final static byte F2I = (byte)139;
168     public final static byte F2L = (byte)140;
169     public final static byte F2D = (byte)141;
170     public final static byte D2I = (byte)142;
171     public final static byte D2L = (byte)143;
172     public final static byte D2F = (byte)144;
173     public final static byte I2B = (byte)145;
174     public final static byte I2C = (byte)146;
175     public final static byte I2S = (byte)147;
176     public final static byte LCMP = (byte)148;
177     public final static byte FCMPL = (byte)149;
178     public final static byte FCMPG = (byte)150;
179     public final static byte DCMPL = (byte)151;
180     public final static byte DCMPG = (byte)152;
181     public final static byte IFEQ = (byte)153;
182     public final static byte IFNE = (byte)154;
183     public final static byte IFLT = (byte)155;
184     public final static byte IFGE = (byte)156;
185     public final static byte IFGT = (byte)157;
186     public final static byte IFLE = (byte)158;
187     public final static byte IF_ICMPEQ = (byte)159;
188     public final static byte IF_ICMPNE = (byte)160;
189     public final static byte IF_ICMPLT = (byte)161;
190     public final static byte IF_ICMPGE = (byte)162;
191     public final static byte IF_ICMPGT = (byte)163;
192     public final static byte IF_ICMPLE = (byte)164;
193     public final static byte IF_ACMPEQ = (byte)165;
194     public final static byte IF_ACMPNE = (byte)166;
195     public final static byte GOTO = (byte)167;
196     public final static byte JSR = (byte)168;
197     public final static byte RET = (byte)169;
198     public final static byte TABLESWITCH = (byte)170;
199     public final static byte LOOKUPSWITCH = (byte)171;
200     public final static byte IRETURN = (byte)172;
201     public final static byte LRETURN = (byte)173;
202     public final static byte FRETURN = (byte)174;
203     public final static byte DRETURN = (byte)175;
204     public final static byte ARETURN = (byte)176;
205     public final static byte RETURN = (byte)177;
206     public final static byte GETSTATIC = (byte)178;
207     public final static byte PUTSTATIC = (byte)179;
208     public final static byte GETFIELD = (byte)180;
209     public final static byte PUTFIELD = (byte)181;
210     public final static byte INVOKEVIRTUAL = (byte)182;
211     public final static byte INVOKESPECIAL = (byte)183;
212     public final static byte INVOKESTATIC = (byte)184;
213     public final static byte INVOKEINTERFACE = (byte)185;
214     public final static byte UNUSED = (byte)186;
215     public final static byte NEW = (byte)187;
216     public final static byte NEWARRAY = (byte)188;
217     public final static byte ANEWARRAY = (byte)189;
218     public final static byte ARRAYLENGTH = (byte)190;
219     public final static byte ATHROW = (byte)191;
220     public final static byte CHECKCAST = (byte)192;
221     public final static byte INSTANCEOF = (byte)193;
222     public final static byte MONITORENTER = (byte)194;
223     public final static byte MONITOREXIT = (byte)195;
224     public final static byte WIDE = (byte)196;
225     public final static byte MULTIANEWARRAY = (byte)197;
226     public final static byte IFNULL = (byte)198;
227     public final static byte IFNONNULL = (byte)199;
228     public final static byte GOTO_W = (byte)200;
229     public final static byte JSR_W = (byte)201;
230     public final static byte BREAKPOINT = (byte)202;
231
232     /**
233      * @exception IllegalArgumentException if opcode is invalid
234      */

235     public final static String JavaDoc getMnemonic(byte opcode)
236         throws IllegalArgumentException JavaDoc {
237         try {
238             return Mnemonic.m[opcode & 0xff];
239         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
240             throw new IllegalArgumentException JavaDoc
241                 ("Opcode not valid: " + opcode);
242         }
243     }
244
245     /**
246      * Reverses the condition for an "if" opcode. For example, IFEQ is changed
247      * to IFNE.
248      */

249     public final static byte reverseIfOpcode(byte opcode) {
250         // Actually, because the numbers assigned to the "if" opcodes
251
// were so cleverly chosen, all I really need to do is toggle
252
// bit 0. I'm not going to do that because I still need to check if
253
// an invalid opcode was passed in.
254

255         switch (opcode) {
256         case IF_ACMPEQ:
257             return IF_ACMPNE;
258         case IF_ACMPNE:
259             return IF_ACMPEQ;
260         case IF_ICMPEQ:
261             return IF_ICMPNE;
262         case IF_ICMPNE:
263             return IF_ICMPEQ;
264         case IF_ICMPLT:
265             return IF_ICMPGE;
266         case IF_ICMPGE:
267             return IF_ICMPLT;
268         case IF_ICMPGT:
269             return IF_ICMPLE;
270         case IF_ICMPLE:
271             return IF_ICMPGT;
272         case IFEQ:
273             return IFNE;
274         case IFNE:
275             return IFEQ;
276         case IFLT:
277             return IFGE;
278         case IFGE:
279             return IFLT;
280         case IFGT:
281             return IFLE;
282         case IFLE:
283             return IFGT;
284         case IFNONNULL:
285             return IFNULL;
286         case IFNULL:
287             return IFNONNULL;
288         default:
289             throw new IllegalArgumentException JavaDoc
290                 ("Opcode not an if instruction: " + getMnemonic(opcode));
291         }
292     }
293
294     /**
295      * Returns true if the given opcode can throw an exception at runtime.
296      */

297     public final static boolean canThrowException(byte opcode) {
298         switch (opcode) {
299         default:
300             return false;
301         case IALOAD:
302         case LALOAD:
303         case FALOAD:
304         case DALOAD:
305         case AALOAD:
306         case BALOAD:
307         case CALOAD:
308         case SALOAD:
309         case IASTORE:
310         case LASTORE:
311         case FASTORE:
312         case DASTORE:
313         case AASTORE:
314         case BASTORE:
315         case CASTORE:
316         case SASTORE:
317         case IDIV:
318         case LDIV:
319         case IREM:
320         case LREM:
321         case GETFIELD:
322         case PUTFIELD:
323         case INVOKEVIRTUAL:
324         case INVOKESPECIAL:
325         case INVOKESTATIC:
326         case INVOKEINTERFACE:
327         case NEWARRAY:
328         case ANEWARRAY:
329         case ARRAYLENGTH:
330         case ATHROW:
331         case CHECKCAST:
332         case MONITORENTER:
333         case MONITOREXIT:
334         case MULTIANEWARRAY:
335             return true;
336         }
337     }
338
339     private static class Mnemonic {
340         public static final String JavaDoc[] m = {
341             "nop",
342             "aconst_null",
343             "iconst_m1",
344             "iconst_0",
345             "iconst_1",
346             "iconst_2",
347             "iconst_3",
348             "iconst_4",
349             "iconst_5",
350             "lconst_0",
351             "lconst_1",
352             "fconst_0",
353             "fconst_1",
354             "fconst_2",
355             "dconst_0",
356             "dconst_1",
357             "bipush",
358             "sipush",
359             "ldc",
360             "ldc_w",
361             "ldc2_w",
362             "iload",
363             "lload",
364             "fload",
365             "dload",
366             "aload",
367             "iload_0",
368             "iload_1",
369             "iload_2",
370             "iload_3",
371             "lload_0",
372             "lload_1",
373             "lload_2",
374             "lload_3",
375             "fload_0",
376             "fload_1",
377             "fload_2",
378             "fload_3",
379             "dload_0",
380             "dload_1",
381             "dload_2",
382             "dload_3",
383             "aload_0",
384             "aload_1",
385             "aload_2",
386             "aload_3",
387             "iaload",
388             "laload",
389             "faload",
390             "daload",
391             "aaload",
392             "baload",
393             "caload",
394             "saload",
395             "istore",
396             "lstore",
397             "fstore",
398             "dstore",
399             "astore",
400             "istore_0",
401             "istore_1",
402             "istore_2",
403             "istore_3",
404             "lstore_0",
405             "lstore_1",
406             "lstore_2",
407             "lstore_3",
408             "fstore_0",
409             "fstore_1",
410             "fstore_2",
411             "fstore_3",
412             "dstore_0",
413             "dstore_1",
414             "dstore_2",
415             "dstore_3",
416             "astore_0",
417             "astore_1",
418             "astore_2",
419             "astore_3",
420             "iastore",
421             "lastore",
422             "fastore",
423             "dastore",
424             "aastore",
425             "bastore",
426             "castore",
427             "sastore",
428             "pop",
429             "pop2",
430             "dup",
431             "dup_x1",
432             "dup_x2",
433             "dup2",
434             "dup2_x1",
435             "dup2_x2",
436             "swap",
437             "iadd",
438             "ladd",
439             "fadd",
440             "dadd",
441             "isub",
442             "lsub",
443             "fsub",
444             "dsub",
445             "imul",
446             "lmul",
447             "fmul",
448             "dmul",
449             "idiv",
450             "ldiv",
451             "fdiv",
452             "ddiv",
453             "irem",
454             "lrem",
455             "frem",
456             "drem",
457             "ineg",
458             "lneg",
459             "fneg",
460             "dneg",
461             "ishl",
462             "lshl",
463             "ishr",
464             "lshr",
465             "iushr",
466             "lushr",
467             "iand",
468             "land",
469             "ior",
470             "lor",
471             "ixor",
472             "lxor",
473             "iinc",
474             "i2l",
475             "i2f",
476             "i2d",
477             "l2i",
478             "l2f",
479             "l2d",
480             "f2i",
481             "f2l",
482             "f2d",
483             "d2i",
484             "d2l",
485             "d2f",
486             "i2b",
487             "i2c",
488             "i2s",
489             "lcmp",
490             "fcmpl",
491             "fcmpg",
492             "dcmpl",
493             "dcmpg",
494             "ifeq",
495             "ifne",
496             "iflt",
497             "ifge",
498             "ifgt",
499             "ifle",
500             "if_icmpeq",
501             "if_icmpne",
502             "if_icmplt",
503             "if_icmpge",
504             "if_icmpgt",
505             "if_icmple",
506             "if_acmpeq",
507             "if_acmpne",
508             "goto",
509             "jsr",
510             "ret",
511             "tableswitch",
512             "lookupswitch",
513             "ireturn",
514             "lreturn",
515             "freturn",
516             "dreturn",
517             "areturn",
518             "return",
519             "getstatic",
520             "putstatic",
521             "getfield",
522             "putfield",
523             "invokevirtual",
524             "invokespecial",
525             "invokestatic",
526             "invokeinterface",
527             "unused",
528             "new",
529             "newarray",
530             "anewarray",
531             "arraylength",
532             "athrow",
533             "checkcast",
534             "instanceof",
535             "monitorenter",
536             "monitorexit",
537             "wide",
538             "multianewarray",
539             "ifnull",
540             "ifnonnull",
541             "goto_w",
542             "jsr_w",
543             "breakpoint",
544         };
545     }
546 }
547
Popular Tags