001: /*
002: * Copyright 2004 Brian S O'Neill
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.cojen.classfile;
018:
019: /**
020: * A collection of opcode constants for creating class file byte code. These
021: * opcodes are defined in chapter 6 of
022: * <i>The Java Virual Machine Specification</i>.
023: *
024: * @author Brian S O'Neill
025: * @see cojen.classfile.attribute.CodeAttr
026: */
027: public class Opcode {
028: public final static byte NOP = (byte) 0;
029: public final static byte ACONST_NULL = (byte) 1;
030: public final static byte ICONST_M1 = (byte) 2;
031: public final static byte ICONST_0 = (byte) 3;
032: public final static byte ICONST_1 = (byte) 4;
033: public final static byte ICONST_2 = (byte) 5;
034: public final static byte ICONST_3 = (byte) 6;
035: public final static byte ICONST_4 = (byte) 7;
036: public final static byte ICONST_5 = (byte) 8;
037: public final static byte LCONST_0 = (byte) 9;
038: public final static byte LCONST_1 = (byte) 10;
039: public final static byte FCONST_0 = (byte) 11;
040: public final static byte FCONST_1 = (byte) 12;
041: public final static byte FCONST_2 = (byte) 13;
042: public final static byte DCONST_0 = (byte) 14;
043: public final static byte DCONST_1 = (byte) 15;
044: public final static byte BIPUSH = (byte) 16;
045: public final static byte SIPUSH = (byte) 17;
046: public final static byte LDC = (byte) 18;
047: public final static byte LDC_W = (byte) 19;
048: public final static byte LDC2_W = (byte) 20;
049: public final static byte ILOAD = (byte) 21;
050: public final static byte LLOAD = (byte) 22;
051: public final static byte FLOAD = (byte) 23;
052: public final static byte DLOAD = (byte) 24;
053: public final static byte ALOAD = (byte) 25;
054: public final static byte ILOAD_0 = (byte) 26;
055: public final static byte ILOAD_1 = (byte) 27;
056: public final static byte ILOAD_2 = (byte) 28;
057: public final static byte ILOAD_3 = (byte) 29;
058: public final static byte LLOAD_0 = (byte) 30;
059: public final static byte LLOAD_1 = (byte) 31;
060: public final static byte LLOAD_2 = (byte) 32;
061: public final static byte LLOAD_3 = (byte) 33;
062: public final static byte FLOAD_0 = (byte) 34;
063: public final static byte FLOAD_1 = (byte) 35;
064: public final static byte FLOAD_2 = (byte) 36;
065: public final static byte FLOAD_3 = (byte) 37;
066: public final static byte DLOAD_0 = (byte) 38;
067: public final static byte DLOAD_1 = (byte) 39;
068: public final static byte DLOAD_2 = (byte) 40;
069: public final static byte DLOAD_3 = (byte) 41;
070: public final static byte ALOAD_0 = (byte) 42;
071: public final static byte ALOAD_1 = (byte) 43;
072: public final static byte ALOAD_2 = (byte) 44;
073: public final static byte ALOAD_3 = (byte) 45;
074: public final static byte IALOAD = (byte) 46;
075: public final static byte LALOAD = (byte) 47;
076: public final static byte FALOAD = (byte) 48;
077: public final static byte DALOAD = (byte) 49;
078: public final static byte AALOAD = (byte) 50;
079: public final static byte BALOAD = (byte) 51;
080: public final static byte CALOAD = (byte) 52;
081: public final static byte SALOAD = (byte) 53;
082: public final static byte ISTORE = (byte) 54;
083: public final static byte LSTORE = (byte) 55;
084: public final static byte FSTORE = (byte) 56;
085: public final static byte DSTORE = (byte) 57;
086: public final static byte ASTORE = (byte) 58;
087: public final static byte ISTORE_0 = (byte) 59;
088: public final static byte ISTORE_1 = (byte) 60;
089: public final static byte ISTORE_2 = (byte) 61;
090: public final static byte ISTORE_3 = (byte) 62;
091: public final static byte LSTORE_0 = (byte) 63;
092: public final static byte LSTORE_1 = (byte) 64;
093: public final static byte LSTORE_2 = (byte) 65;
094: public final static byte LSTORE_3 = (byte) 66;
095: public final static byte FSTORE_0 = (byte) 67;
096: public final static byte FSTORE_1 = (byte) 68;
097: public final static byte FSTORE_2 = (byte) 69;
098: public final static byte FSTORE_3 = (byte) 70;
099: 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 getMnemonic(byte opcode)
236: throws IllegalArgumentException {
237: try {
238: return Mnemonic.m[opcode & 0xff];
239: } catch (ArrayIndexOutOfBoundsException e) {
240: throw new IllegalArgumentException("Opcode not valid: "
241: + 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(
290: "Opcode not an if instruction: "
291: + getMnemonic(opcode));
292: }
293: }
294:
295: /**
296: * Returns true if the given opcode can throw an exception at runtime.
297: */
298: public final static boolean canThrowException(byte opcode) {
299: switch (opcode) {
300: default:
301: return false;
302: case IALOAD:
303: case LALOAD:
304: case FALOAD:
305: case DALOAD:
306: case AALOAD:
307: case BALOAD:
308: case CALOAD:
309: case SALOAD:
310: case IASTORE:
311: case LASTORE:
312: case FASTORE:
313: case DASTORE:
314: case AASTORE:
315: case BASTORE:
316: case CASTORE:
317: case SASTORE:
318: case IDIV:
319: case LDIV:
320: case IREM:
321: case LREM:
322: case GETFIELD:
323: case PUTFIELD:
324: case INVOKEVIRTUAL:
325: case INVOKESPECIAL:
326: case INVOKESTATIC:
327: case INVOKEINTERFACE:
328: case NEWARRAY:
329: case ANEWARRAY:
330: case ARRAYLENGTH:
331: case ATHROW:
332: case CHECKCAST:
333: case MONITORENTER:
334: case MONITOREXIT:
335: case MULTIANEWARRAY:
336: return true;
337: }
338: }
339:
340: private static class Mnemonic {
341: public static final String[] m = { "nop", "aconst_null",
342: "iconst_m1", "iconst_0", "iconst_1", "iconst_2",
343: "iconst_3", "iconst_4", "iconst_5", "lconst_0",
344: "lconst_1", "fconst_0", "fconst_1", "fconst_2",
345: "dconst_0", "dconst_1", "bipush", "sipush", "ldc",
346: "ldc_w", "ldc2_w", "iload", "lload", "fload", "dload",
347: "aload", "iload_0", "iload_1", "iload_2", "iload_3",
348: "lload_0", "lload_1", "lload_2", "lload_3", "fload_0",
349: "fload_1", "fload_2", "fload_3", "dload_0", "dload_1",
350: "dload_2", "dload_3", "aload_0", "aload_1", "aload_2",
351: "aload_3", "iaload", "laload", "faload", "daload",
352: "aaload", "baload", "caload", "saload", "istore",
353: "lstore", "fstore", "dstore", "astore", "istore_0",
354: "istore_1", "istore_2", "istore_3", "lstore_0",
355: "lstore_1", "lstore_2", "lstore_3", "fstore_0",
356: "fstore_1", "fstore_2", "fstore_3", "dstore_0",
357: "dstore_1", "dstore_2", "dstore_3", "astore_0",
358: "astore_1", "astore_2", "astore_3", "iastore",
359: "lastore", "fastore", "dastore", "aastore", "bastore",
360: "castore", "sastore", "pop", "pop2", "dup", "dup_x1",
361: "dup_x2", "dup2", "dup2_x1", "dup2_x2", "swap", "iadd",
362: "ladd", "fadd", "dadd", "isub", "lsub", "fsub", "dsub",
363: "imul", "lmul", "fmul", "dmul", "idiv", "ldiv", "fdiv",
364: "ddiv", "irem", "lrem", "frem", "drem", "ineg", "lneg",
365: "fneg", "dneg", "ishl", "lshl", "ishr", "lshr",
366: "iushr", "lushr", "iand", "land", "ior", "lor", "ixor",
367: "lxor", "iinc", "i2l", "i2f", "i2d", "l2i", "l2f",
368: "l2d", "f2i", "f2l", "f2d", "d2i", "d2l", "d2f", "i2b",
369: "i2c", "i2s", "lcmp", "fcmpl", "fcmpg", "dcmpl",
370: "dcmpg", "ifeq", "ifne", "iflt", "ifge", "ifgt",
371: "ifle", "if_icmpeq", "if_icmpne", "if_icmplt",
372: "if_icmpge", "if_icmpgt", "if_icmple", "if_acmpeq",
373: "if_acmpne", "goto", "jsr", "ret", "tableswitch",
374: "lookupswitch", "ireturn", "lreturn", "freturn",
375: "dreturn", "areturn", "return", "getstatic",
376: "putstatic", "getfield", "putfield", "invokevirtual",
377: "invokespecial", "invokestatic", "invokeinterface",
378: "unused", "new", "newarray", "anewarray",
379: "arraylength", "athrow", "checkcast", "instanceof",
380: "monitorenter", "monitorexit", "wide",
381: "multianewarray", "ifnull", "ifnonnull", "goto_w",
382: "jsr_w", "breakpoint", };
383: }
384: }
|