From: brian Date: Wed, 26 May 2004 15:08:28 +0000 (+0000) Subject: tons of stuff X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=commitdiff_plain;h=bc9112573cba51be5e7d285ccd3e496be4278c63;hp=cb0bae954e7dd0067e77c436c78ee19ace52d9e3 tons of stuff darcs-hash:20040526150828-24bed-51c91e71b36335ed81c3fcb7a6ac068d907fef4c.gz --- diff --git a/src/org/ibex/classgen/AttrGen.java b/src/org/ibex/classgen/AttrGen.java index 81d94b3..758d9f0 100644 --- a/src/org/ibex/classgen/AttrGen.java +++ b/src/org/ibex/classgen/AttrGen.java @@ -24,7 +24,7 @@ public class AttrGen { for(Enumeration e = ht.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); byte[] val = (byte[]) ht.get(name); - o.writeShort(cp.getUtf8(name).index); + o.writeShort(cp.getUtf8Index(name)); o.writeInt(val.length); o.write(val); } diff --git a/src/org/ibex/classgen/CGConst.java b/src/org/ibex/classgen/CGConst.java index 0dde25c..cfef6b7 100644 --- a/src/org/ibex/classgen/CGConst.java +++ b/src/org/ibex/classgen/CGConst.java @@ -1,17 +1,16 @@ package org.ibex.classgen; public interface CGConst { - public static final int ACC_PUBLIC = (byte) 0x0001; - public static final int ACC_FINAL = (byte) 0x0010; - // Class only public static final int ACC_INTERFACE = (byte) 0x0200; public static final int ACC_SUPER = (byte) 0x0020; // Field/Method only + public static final int ACC_PUBLIC = (byte) 0x0001; public static final int ACC_PRIVATE = (byte) 0x0002; public static final int ACC_PROTECTED = (byte) 0x0004; public static final int ACC_STATIC = (byte) 0x0008; + public static final int ACC_FINAL = (byte) 0x0010; // Class/Method only public static final int ACC_ABSTRACT = (byte) 0x0400; @@ -26,205 +25,233 @@ public interface CGConst { public static final int ACC_TRANSIENT = (byte) 0x0080; // Instructions - public static final byte NOP = (byte) 0x0; - public static final byte ACONST_NULL = (byte) 0x01; - public static final byte ICONST_M1 = (byte) 0x02; - public static final byte ICONST_0 = (byte) 0x03; - public static final byte ICONST_1 = (byte) 0x04; - public static final byte ICONST_2 = (byte) 0x05; - public static final byte ICONST_3 = (byte) 0x06; - public static final byte ICONST_4 = (byte) 0x07; - public static final byte ICONST_5 = (byte) 0x08; - public static final byte LCONST_0 = (byte) 0x09; - public static final byte LCONST_1 = (byte) 0x0A; - public static final byte FCONST_0 = (byte) 0x0B; - public static final byte FCONST_1 = (byte) 0x0C; - public static final byte FCONST_2 = (byte) 0x0D; - public static final byte DCONST_0 = (byte) 0x0E; - public static final byte DCONST_1 = (byte) 0x0F; - public static final byte BIPUSH = (byte) 0x10; - public static final byte SIPUSH = (byte) 0x11; - public static final byte LDC = (byte) 0x12; - public static final byte LDC_W = (byte) 0x13; - public static final byte LDC2_W = (byte) 0x14; - public static final byte ILOAD = (byte) 0x15; - public static final byte LLOAD = (byte) 0x16; - public static final byte FLOAD = (byte) 0x17; - public static final byte DLOAD = (byte) 0x18; - public static final byte ALOAD = (byte) 0x19; - public static final byte ILOAD_0 = (byte) 0x1A; - public static final byte ILOAD_1 = (byte) 0x1B; - public static final byte ILOAD_2 = (byte) 0x1C; - public static final byte ILOAD_3 = (byte) 0x1D; - public static final byte LLOAD_0 = (byte) 0x1E; - public static final byte LLOAD_1 = (byte) 0x1F; - public static final byte LLOAD_2 = (byte) 0x20; - public static final byte LLOAD_3 = (byte) 0x21; - public static final byte FLOAD_0 = (byte) 0x22; - public static final byte FLOAD_1 = (byte) 0x23; - public static final byte FLOAD_2 = (byte) 0x24; - public static final byte FLOAD_3 = (byte) 0x25; - public static final byte DLOAD_0 = (byte) 0x26; - public static final byte DLOAD_1 = (byte) 0x27; - public static final byte DLOAD_2 = (byte) 0x28; - public static final byte DLOAD_3 = (byte) 0x29; - public static final byte ALOAD_0 = (byte) 0x2A; - public static final byte ALOAD_1 = (byte) 0x2B; - public static final byte ALOAD_2 = (byte) 0x2C; - public static final byte ALOAD_3 = (byte) 0x2D; - public static final byte IALOAD = (byte) 0x2E; - public static final byte LALOAD = (byte) 0x2F; - public static final byte FALOAD = (byte) 0x30; - public static final byte DALOAD = (byte) 0x31; - public static final byte AALOAD = (byte) 0x32; - public static final byte BALOAD = (byte) 0x33; - public static final byte CALOAD = (byte) 0x34; - public static final byte SALOAD = (byte) 0x35; - public static final byte ISTORE = (byte) 0x36; - public static final byte LSTORE = (byte) 0x37; - public static final byte FSTORE = (byte) 0x38; - public static final byte DSTORE = (byte) 0x39; - public static final byte ASTORE = (byte) 0x3A; - public static final byte ISTORE_0 = (byte) 0x3B; - public static final byte ISTORE_1 = (byte) 0x3C; - public static final byte ISTORE_2 = (byte) 0x3D; - public static final byte ISTORE_3 = (byte) 0x3E; - public static final byte LSTORE_0 = (byte) 0x3F; - public static final byte LSTORE_1 = (byte) 0x40; - public static final byte LSTORE_2 = (byte) 0x41; - public static final byte LSTORE_3 = (byte) 0x42; - public static final byte FSTORE_0 = (byte) 0x43; - public static final byte FSTORE_1 = (byte) 0x44; - public static final byte FSTORE_2 = (byte) 0x45; - public static final byte FSTORE_3 = (byte) 0x46; - public static final byte DSTORE_0 = (byte) 0x47; - public static final byte DSTORE_1 = (byte) 0x48; - public static final byte DSTORE_2 = (byte) 0x49; - public static final byte DSTORE_3 = (byte) 0x4A; - public static final byte ASTORE_0 = (byte) 0x4B; - public static final byte ASTORE_1 = (byte) 0x4C; - public static final byte ASTORE_2 = (byte) 0x4D; - public static final byte ASTORE_3 = (byte) 0x4E; - public static final byte IASTORE = (byte) 0x4F; - public static final byte LASTORE = (byte) 0x50; - public static final byte FASTORE = (byte) 0x51; - public static final byte DASTORE = (byte) 0x52; - public static final byte AASTORE = (byte) 0x53; - public static final byte BASTORE = (byte) 0x54; - public static final byte CASTORE = (byte) 0x55; - public static final byte SASTORE = (byte) 0x56; - public static final byte POP = (byte) 0x57; - public static final byte POP2 = (byte) 0x58; - public static final byte DUP = (byte) 0x59; - public static final byte DUP_X1 = (byte) 0x5A; - public static final byte DUP_X2 = (byte) 0x5B; - public static final byte DUP2 = (byte) 0x5C; - public static final byte DUP2_X1 = (byte) 0x5D; - public static final byte DUP2_X2 = (byte) 0x5E; - public static final byte SWAP = (byte) 0x5F; - public static final byte IADD = (byte) 0x60; - public static final byte LADD = (byte) 0x61; - public static final byte FADD = (byte) 0x62; - public static final byte DADD = (byte) 0x63; - public static final byte ISUB = (byte) 0x64; - public static final byte LSUB = (byte) 0x65; - public static final byte FSUB = (byte) 0x66; - public static final byte DSUB = (byte) 0x67; - public static final byte IMUL = (byte) 0x68; - public static final byte LMUL = (byte) 0x69; - public static final byte FMUL = (byte) 0x6A; - public static final byte DMUL = (byte) 0x6B; - public static final byte IDIV = (byte) 0x6C; - public static final byte LDIV = (byte) 0x6D; - public static final byte FDIV = (byte) 0x6E; - public static final byte DDIV = (byte) 0x6F; - public static final byte IREM = (byte) 0x70; - public static final byte LREM = (byte) 0x71; - public static final byte FREM = (byte) 0x72; - public static final byte DREM = (byte) 0x73; - public static final byte INET = (byte) 0x74; - public static final byte LNEG = (byte) 0x75; - public static final byte FNEG = (byte) 0x76; - public static final byte DNEG = (byte) 0x77; - public static final byte ISHL = (byte) 0x78; - public static final byte LSHL = (byte) 0x79; - public static final byte ISHR = (byte) 0x7A; - public static final byte LSHR = (byte) 0x7B; - public static final byte IUSHR = (byte) 0x7C; - public static final byte LUSHR = (byte) 0x7D; - public static final byte IAND = (byte) 0x7E; - public static final byte LAND = (byte) 0x7F; - public static final byte IOR = (byte) 0x80; - public static final byte LOR = (byte) 0x81; - public static final byte IXOR = (byte) 0x82; - public static final byte LXOR = (byte) 0x83; - public static final byte IINC = (byte) 0x84; - public static final byte I2L = (byte) 0x85; - public static final byte I2F = (byte) 0x86; - public static final byte I2D = (byte) 0x87; - public static final byte L2I = (byte) 0x88; - public static final byte L2F = (byte) 0x89; - public static final byte L2D = (byte) 0x8A; - public static final byte F2I = (byte) 0x8B; - public static final byte F2L = (byte) 0x8C; - public static final byte F2D = (byte) 0x8D; - public static final byte D2I = (byte) 0x8E; - public static final byte D2L = (byte) 0x8F; - public static final byte D2F = (byte) 0x90; - public static final byte I2B = (byte) 0x91; - public static final byte I2C = (byte) 0x92; - public static final byte I2S = (byte) 0x93; - public static final byte LCMP = (byte) 0x94; - public static final byte FCMPL = (byte) 0x95; - public static final byte FCMPG = (byte) 0x96; - public static final byte DCMPL = (byte) 0x97; - public static final byte DCMPG = (byte) 0x98; - public static final byte IFEQ = (byte) 0x99; - public static final byte IFNE = (byte) 0x9A; - public static final byte IFLT = (byte) 0x9B; - public static final byte IFGE = (byte) 0x9C; - public static final byte IFGT = (byte) 0x9D; - public static final byte IFLE = (byte) 0x9E; - public static final byte IF_ICMPEQ = (byte) 0x9F; - public static final byte IF_ICMPNE = (byte) 0xA0; - public static final byte IF_ICMPLT = (byte) 0xA1; - public static final byte IF_ICMPGE = (byte) 0xA2; - public static final byte IF_ICMPGT = (byte) 0xA3; - public static final byte IF_ICMPLE = (byte) 0xA4; - public static final byte IF_ACMPEQ = (byte) 0xA5; - public static final byte IF_ACMPNE = (byte) 0xA6; - public static final byte GOTO = (byte) 0xA7; - public static final byte JSR = (byte) 0xA8; - public static final byte RET = (byte) 0xA9; - public static final byte TABLESWITCH = (byte) 0xAA; - public static final byte LOOKUPSWITCH = (byte) 0xAB; - public static final byte IRETURN = (byte) 0xAC; - public static final byte LRETURN = (byte) 0xAD; - public static final byte FRETURN = (byte) 0xAE; - public static final byte DRETURN = (byte) 0xAF; - public static final byte ARETURN = (byte) 0xB0; - public static final byte RETURN = (byte) 0xB1; - public static final byte GETSTATIC = (byte) 0xB2; - public static final byte PUTSTATIC = (byte) 0xB3; - public static final byte GETFIELD = (byte) 0xB4; - public static final byte PUTFIELD = (byte) 0xB5; - public static final byte INVOKEVIRTUAL = (byte) 0xB6; - public static final byte INVOKESPECIAL = (byte) 0xB7; - public static final byte INVOKESTATIC = (byte) 0xB8; - public static final byte INVOKEINTERFACE = (byte) 0xB9; - public static final byte NEW = (byte) 0xBB; - public static final byte NEWARRAY = (byte) 0xBC; - public static final byte ANEWARRAY = (byte) 0xBD; - public static final byte ARRAYLENGTH = (byte) 0xBE; - public static final byte ATHROW = (byte) 0xBF; - public static final byte CHECKCAST = (byte) 0xC0; - public static final byte INSTANCEOF = (byte) 0xC1; - public static final byte MONITORENTER = (byte) 0xC2; - public static final byte MONITOREXIT = (byte) 0xC3; - public static final byte WIDE = (byte) 0xC4; - public static final byte MULTIANEWARRAY = (byte) 0xC5; - public static final byte IFNULL = (byte) 0xC6; - public static final byte IFNONNULL = (byte) 0xC7; - public static final byte GOTO_W = (byte) 0xC8; - public static final byte JSR_W = (byte) 0xC9; + + // NOTE: DO NOT modify or remove the number in the comments + + public static final byte NOP = (byte) 0x0; // 0 + public static final byte ACONST_NULL = (byte) 0x01; // 0 + public static final byte ICONST_M1 = (byte) 0x02; // 0 + public static final byte ICONST_0 = (byte) 0x03; // 0 + public static final byte ICONST_1 = (byte) 0x04; // 0 + public static final byte ICONST_2 = (byte) 0x05; // 0 + public static final byte ICONST_3 = (byte) 0x06; // 0 + public static final byte ICONST_4 = (byte) 0x07; // 0 + public static final byte ICONST_5 = (byte) 0x08; // 0 + public static final byte LCONST_0 = (byte) 0x09; // 0 + public static final byte LCONST_1 = (byte) 0x0A; // 0 + public static final byte FCONST_0 = (byte) 0x0B; // 0 + public static final byte FCONST_1 = (byte) 0x0C; // 0 + public static final byte FCONST_2 = (byte) 0x0D; // 0 + public static final byte DCONST_0 = (byte) 0x0E; // 0 + public static final byte DCONST_1 = (byte) 0x0F; // 0 + public static final byte BIPUSH = (byte) 0x10; // 1 + public static final byte SIPUSH = (byte) 0x11; // 2 + public static final byte LDC = (byte) 0x12; // 1C + public static final byte LDC_W = (byte) 0x13; // 2C + public static final byte LDC2_W = (byte) 0x14; // 2C + public static final byte ILOAD = (byte) 0x15; // 1 + public static final byte LLOAD = (byte) 0x16; // 1 + public static final byte FLOAD = (byte) 0x17; // 1 + public static final byte DLOAD = (byte) 0x18; // 1 + public static final byte ALOAD = (byte) 0x19; // 1 + public static final byte ILOAD_0 = (byte) 0x1A; // 0 + public static final byte ILOAD_1 = (byte) 0x1B; // 0 + public static final byte ILOAD_2 = (byte) 0x1C; // 0 + public static final byte ILOAD_3 = (byte) 0x1D; // 0 + public static final byte LLOAD_0 = (byte) 0x1E; // 0 + public static final byte LLOAD_1 = (byte) 0x1F; // 0 + public static final byte LLOAD_2 = (byte) 0x20; // 0 + public static final byte LLOAD_3 = (byte) 0x21; // 0 + public static final byte FLOAD_0 = (byte) 0x22; // 0 + public static final byte FLOAD_1 = (byte) 0x23; // 0 + public static final byte FLOAD_2 = (byte) 0x24; // 0 + public static final byte FLOAD_3 = (byte) 0x25; // 0 + public static final byte DLOAD_0 = (byte) 0x26; // 0 + public static final byte DLOAD_1 = (byte) 0x27; // 0 + public static final byte DLOAD_2 = (byte) 0x28; // 0 + public static final byte DLOAD_3 = (byte) 0x29; // 0 + public static final byte ALOAD_0 = (byte) 0x2A; // 0 + public static final byte ALOAD_1 = (byte) 0x2B; // 0 + public static final byte ALOAD_2 = (byte) 0x2C; // 0 + public static final byte ALOAD_3 = (byte) 0x2D; // 0 + public static final byte IALOAD = (byte) 0x2E; // 0 + public static final byte LALOAD = (byte) 0x2F; // 0 + public static final byte FALOAD = (byte) 0x30; // 0 + public static final byte DALOAD = (byte) 0x31; // 0 + public static final byte AALOAD = (byte) 0x32; // 0 + public static final byte BALOAD = (byte) 0x33; // 0 + public static final byte CALOAD = (byte) 0x34; // 0 + public static final byte SALOAD = (byte) 0x35; // 0 + public static final byte ISTORE = (byte) 0x36; // 0 + public static final byte LSTORE = (byte) 0x37; // 9 + public static final byte FSTORE = (byte) 0x38; // 0 + public static final byte DSTORE = (byte) 0x39; // 0 + public static final byte ASTORE = (byte) 0x3A; // 0 + public static final byte ISTORE_0 = (byte) 0x3B; // 0 + public static final byte ISTORE_1 = (byte) 0x3C; // 0 + public static final byte ISTORE_2 = (byte) 0x3D; // 0 + public static final byte ISTORE_3 = (byte) 0x3E; // 0 + public static final byte LSTORE_0 = (byte) 0x3F; // 0 + public static final byte LSTORE_1 = (byte) 0x40; // 0 + public static final byte LSTORE_2 = (byte) 0x41; // 0 + public static final byte LSTORE_3 = (byte) 0x42; // 0 + public static final byte FSTORE_0 = (byte) 0x43; // 0 + public static final byte FSTORE_1 = (byte) 0x44; // 0 + public static final byte FSTORE_2 = (byte) 0x45; // 0 + public static final byte FSTORE_3 = (byte) 0x46; // 0 + public static final byte DSTORE_0 = (byte) 0x47; // 0 + public static final byte DSTORE_1 = (byte) 0x48; // 0 + public static final byte DSTORE_2 = (byte) 0x49; // 0 + public static final byte DSTORE_3 = (byte) 0x4A; // 0 + public static final byte ASTORE_0 = (byte) 0x4B; // 0 + public static final byte ASTORE_1 = (byte) 0x4C; // 0 + public static final byte ASTORE_2 = (byte) 0x4D; // 0 + public static final byte ASTORE_3 = (byte) 0x4E; // 0 + public static final byte IASTORE = (byte) 0x4F; // 0 + public static final byte LASTORE = (byte) 0x50; // 0 + public static final byte FASTORE = (byte) 0x51; // 0 + public static final byte DASTORE = (byte) 0x52; // 0 + public static final byte AASTORE = (byte) 0x53; // 0 + public static final byte BASTORE = (byte) 0x54; // 0 + public static final byte CASTORE = (byte) 0x55; // 0 + public static final byte SASTORE = (byte) 0x56; // 0 + public static final byte POP = (byte) 0x57; // 0 + public static final byte POP2 = (byte) 0x58; // 0 + public static final byte DUP = (byte) 0x59; // 0 + public static final byte DUP_X1 = (byte) 0x5A; // 0 + public static final byte DUP_X2 = (byte) 0x5B; // 0 + public static final byte DUP2 = (byte) 0x5C; // 0 + public static final byte DUP2_X1 = (byte) 0x5D; // 0 + public static final byte DUP2_X2 = (byte) 0x5E; // 0 + public static final byte SWAP = (byte) 0x5F; // 0 + public static final byte IADD = (byte) 0x60; // 0 + public static final byte LADD = (byte) 0x61; // 0 + public static final byte FADD = (byte) 0x62; // 0 + public static final byte DADD = (byte) 0x63; // 0 + public static final byte ISUB = (byte) 0x64; // 0 + public static final byte LSUB = (byte) 0x65; // 0 + public static final byte FSUB = (byte) 0x66; // 0 + public static final byte DSUB = (byte) 0x67; // 0 + public static final byte IMUL = (byte) 0x68; // 0 + public static final byte LMUL = (byte) 0x69; // 0 + public static final byte FMUL = (byte) 0x6A; // 0 + public static final byte DMUL = (byte) 0x6B; // 0 + public static final byte IDIV = (byte) 0x6C; // 0 + public static final byte LDIV = (byte) 0x6D; // 0 + public static final byte FDIV = (byte) 0x6E; // 0 + public static final byte DDIV = (byte) 0x6F; // 0 + public static final byte IREM = (byte) 0x70; // 0 + public static final byte LREM = (byte) 0x71; // 0 + public static final byte FREM = (byte) 0x72; // 0 + public static final byte DREM = (byte) 0x73; // 0 + public static final byte INEG = (byte) 0x74; // 0 + public static final byte LNEG = (byte) 0x75; // 0 + public static final byte FNEG = (byte) 0x76; // 0 + public static final byte DNEG = (byte) 0x77; // 0 + public static final byte ISHL = (byte) 0x78; // 0 + public static final byte LSHL = (byte) 0x79; // 0 + public static final byte ISHR = (byte) 0x7A; // 0 + public static final byte LSHR = (byte) 0x7B; // 0 + public static final byte IUSHR = (byte) 0x7C; // 0 + public static final byte LUSHR = (byte) 0x7D; // 0 + public static final byte IAND = (byte) 0x7E; // 0 + public static final byte LAND = (byte) 0x7F; // 0 + public static final byte IOR = (byte) 0x80; // 0 + public static final byte LOR = (byte) 0x81; // 0 + public static final byte IXOR = (byte) 0x82; // 0 + public static final byte LXOR = (byte) 0x83; // 0 + public static final byte IINC = (byte) 0x84; // 2 + public static final byte I2L = (byte) 0x85; // 0 + public static final byte I2F = (byte) 0x86; // 0 + public static final byte I2D = (byte) 0x87; // 0 + public static final byte L2I = (byte) 0x88; // 0 + public static final byte L2F = (byte) 0x89; // 0 + public static final byte L2D = (byte) 0x8A; // 0 + public static final byte F2I = (byte) 0x8B; // 0 + public static final byte F2L = (byte) 0x8C; // 0 + public static final byte F2D = (byte) 0x8D; // 0 + public static final byte D2I = (byte) 0x8E; // 0 + public static final byte D2L = (byte) 0x8F; // 0 + public static final byte D2F = (byte) 0x90; // 0 + public static final byte I2B = (byte) 0x91; // 0 + public static final byte I2C = (byte) 0x92; // 0 + public static final byte I2S = (byte) 0x93; // 0 + public static final byte LCMP = (byte) 0x94; // 0 + public static final byte FCMPL = (byte) 0x95; // 0 + public static final byte FCMPG = (byte) 0x96; // 0 + public static final byte DCMPL = (byte) 0x97; // 0 + public static final byte DCMPG = (byte) 0x98; // 0 + public static final byte IFEQ = (byte) 0x99; // 2B + public static final byte IFNE = (byte) 0x9A; // 2B + public static final byte IFLT = (byte) 0x9B; // 2B + public static final byte IFGE = (byte) 0x9C; // 2B + public static final byte IFGT = (byte) 0x9D; // 2B + public static final byte IFLE = (byte) 0x9E; // 2B + public static final byte IF_ICMPEQ = (byte) 0x9F; // 2B + public static final byte IF_ICMPNE = (byte) 0xA0; // 2B + public static final byte IF_ICMPLT = (byte) 0xA1; // 2B + public static final byte IF_ICMPGE = (byte) 0xA2; // 2B + public static final byte IF_ICMPGT = (byte) 0xA3; // 2B + public static final byte IF_ICMPLE = (byte) 0xA4; // 2B + public static final byte IF_ACMPEQ = (byte) 0xA5; // 2B + public static final byte IF_ACMPNE = (byte) 0xA6; // 2B + public static final byte GOTO = (byte) 0xA7; // 2B + public static final byte JSR = (byte) 0xA8; // 2B + public static final byte RET = (byte) 0xA9; // 1 + public static final byte TABLESWITCH = (byte) 0xAA; // V + public static final byte LOOKUPSWITCH = (byte) 0xAB; // V + public static final byte IRETURN = (byte) 0xAC; // 0 + public static final byte LRETURN = (byte) 0xAD; // 0 + public static final byte FRETURN = (byte) 0xAE; // 0 + public static final byte DRETURN = (byte) 0xAF; // 0 + public static final byte ARETURN = (byte) 0xB0; // 0 + public static final byte RETURN = (byte) 0xB1; // 0 + public static final byte GETSTATIC = (byte) 0xB2; // 2C + public static final byte PUTSTATIC = (byte) 0xB3; // 2C + public static final byte GETFIELD = (byte) 0xB4; // 2C + public static final byte PUTFIELD = (byte) 0xB5; // 2C + public static final byte INVOKEVIRTUAL = (byte) 0xB6; // 2C + public static final byte INVOKESPECIAL = (byte) 0xB7; // 2C + public static final byte INVOKESTATIC = (byte) 0xB8; // 2C + public static final byte INVOKEINTERFACE = (byte) 0xB9; // 2C + public static final byte NEW = (byte) 0xBB; // 2C + public static final byte NEWARRAY = (byte) 0xBC; // 1 + public static final byte ANEWARRAY = (byte) 0xBD; // 2C + public static final byte ARRAYLENGTH = (byte) 0xBE; // 0 + public static final byte ATHROW = (byte) 0xBF; // 0 + public static final byte CHECKCAST = (byte) 0xC0; // 2C + public static final byte INSTANCEOF = (byte) 0xC1; // 2C + public static final byte MONITORENTER = (byte) 0xC2; // 0 + public static final byte MONITOREXIT = (byte) 0xC3; // 0 + public static final byte WIDE = (byte) 0xC4; // V + public static final byte MULTIANEWARRAY = (byte) 0xC5; // 3 + public static final byte IFNULL = (byte) 0xC6; // 2B + public static final byte IFNONNULL = (byte) 0xC7; // 2B + public static final byte GOTO_W = (byte) 0xC8; // 4B + public static final byte JSR_W = (byte) 0xC9; // 4B +} + +// DO NOT REMOVE THIS + +/* +#!/usr/bin/perl -w +@ARGV || push @ARGV,$0; +my @a = (); +while(<>) { + chomp; + next unless(m|byte ([A-Z0-9_]+) = .*?([0-9xA-F]+);\s+//\s*(.*)$|i); + my ($name,$num) = ($1,hex($2)); + $_ = $3; + my $n = 1; + $n |= (s/^(\d+)// ? $1 : (s/^V//||die,7)) << 1; + $n |= (1<<4) if(s/^C//); + $n |= (1<<5) if(s/^B//); + die if(/./); + $a[$num] = $n; +} +print "private static final byte[] OP_DATA = {\n\t"; +for(my $i=0;$i<256;$i++) { + printf "0x%02x%s", $a[$i]||2, $i==255?"\n};\n":($i%16)==15?",\n\t":", "; } +__END__ +*/ diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index 91b002e..fc13c4d 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -5,9 +5,12 @@ import java.io.*; import org.ibex.classgen.util.*; -public class CPGen { +// FEATURE: Add a "hit count" to each entry and optimize the table + +class CPGen { private Hashtable entries = new Hashtable(); private int nextIndex = 1; // 0 is reserved + private int count; private boolean sealed; CPGen() { } @@ -17,8 +20,14 @@ public class CPGen { */ abstract static class Ent implements Sort.Comparable { int index; - public abstract int tag(); - public void dump(DataOutput o) throws IOException { o.writeByte(tag()); } + int tag; + + Ent(int tag) { this.tag = tag; } + + int getIndex() { return index; } + + void dump(DataOutput o) throws IOException { o.writeByte(tag); } + public int compareTo(Object o) { if(!(o instanceof Ent)) return 1; int oi = ((Ent)o).index; @@ -28,30 +37,33 @@ public class CPGen { } } - abstract static class OneU2Ent extends Ent { int i; public void dump(DataOutput o) throws IOException { super.dump(o); o.writeShort(i); } } - abstract static class OneU4Ent extends Ent { int i; public void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); } } - abstract static class TwoU2Ent extends OneU2Ent { int i2; public void dump(DataOutput o) throws IOException { super.dump(o); o.writeShort(i2); } } - abstract static class TwoU4Ent extends OneU4Ent { int i2; public void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i2); } } + static class OneU4Ent extends Ent { + int i; + OneU4Ent(int tag) { super(tag); } + void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); } + } - static class IntEnt extends OneU4Ent { public int tag() { return 3; } } // word1: bytes - static class FloatEnt extends OneU4Ent { public int tag() { return 4; } } // word1: bytes - static class LongEnt extends TwoU4Ent { public int tag() { return 5; } } // word1/2: bytes - static class DoubleEnt extends TwoU4Ent { public int tag() { return 6; } } // word1/2: bytes - static class ClassEnt extends OneU2Ent { public int tag() { return 7; } } // word1: name_index - static class StringEnt extends OneU2Ent { public int tag() { return 8; } } // word1: string_index - static class FieldRefEnt extends TwoU2Ent { public int tag() { return 9; } } // word1: class_index word2: name_and_type_index - static class MethodRefEnt extends TwoU2Ent { public int tag() { return 10; } } // word1: class_index word2: name_and_type_index - static class IMethodRefEnt extends TwoU2Ent { public int tag() { return 11; } } // word1: class_index word2: name_and_type_index - static class NameAndTypeEnt extends TwoU2Ent { public int tag() { return 12; } } // word1: name_index word2: descriptor_index + static class LongEnt extends Ent { + long l; + LongEnt(int tag) { super(tag); } + void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); } + } - static class Utf8Ent extends Ent { - String s; - public int tag() { return 1; } - public void dump(DataOutput o) throws IOException { + static class CPRefEnt extends Ent { + Ent e1; + Ent e2; + CPRefEnt(int tag) { super(tag); } + void dump(DataOutput o) throws IOException { super.dump(o); - o.writeUTF(s); + o.writeShort(e1.index); + if(e2 != null) o.writeShort(e2.index); } - public String toString() { return "Utf8: " + s; } + } + + static class Utf8Ent extends Ent { + String s; + Utf8Ent() { super(1); } + void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); } } /* @@ -63,34 +75,7 @@ public class CPGen { public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); } public int hashCode() { return ~s.hashCode(); } } - - public static class NameAndType { - String name; - String type; - public NameAndType(String name, String type) { this.name = name; this.type = type; } - public boolean equals(Object o_) { - if(!(o_ instanceof NameAndType)) return false; - NameAndType o = (NameAndType) o_; - return o.name.equals(name) && o.type.equals(type); - } - public int hashCode() { return name.hashCode() ^ type.hashCode(); } - } - - static abstract class FieldMethodRef { - Type.Object klass; - NameAndType nameAndType; - public FieldMethodRef(Type.Object klass, NameAndType nameAndType) { this.klass = klass; this.nameAndType = nameAndType; } - public boolean equals(Object o_) { - if(!(o_ instanceof FieldMethodRef)) return false; - FieldMethodRef o = (FieldMethodRef) o_; - return o.klass.equals(klass) && o.nameAndType.equals(nameAndType); - } - } - - public static class FieldRef extends FieldMethodRef { public FieldRef (Type.Object c, NameAndType t) { super(c,t); } } - public static class MethodRef extends FieldMethodRef { public MethodRef (Type.Object c, NameAndType t) { super(c,t); } } - public static class IMethodRef extends FieldMethodRef { public IMethodRef(Type.Object c, NameAndType t) { super(c,t); } } - + /* * Methods */ @@ -98,10 +83,21 @@ public class CPGen { public final Ent get(Object o) { return (Ent) entries.get(o); } public final Ent getUtf8(String s) { return get(new Utf8Key(s)); } + public final int getIndex(Object o) { + Ent e = get(o); + if(e == null) throw new IllegalStateException("entry not found"); + return e.getIndex(); + } + public final int getUtf8Index(String s) { + Ent e = getUtf8(s); + if(e == null) throw new IllegalStateException("entry not found"); + return e.getIndex(); + } - public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndType(name,descriptor)); } + public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); } public final Ent addUtf8(String s) { return add(new Utf8Key(s)); } + // FEATURE: Don't resolve indexes until dump (for optimize) public final Ent add(Object o) { if(sealed) throw new IllegalStateException("constant pool is sealed"); @@ -111,58 +107,55 @@ public class CPGen { if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full"); if(o instanceof Type.Object) { - ClassEnt ce = new ClassEnt(); - ce.i = addUtf8(((Type.Object)o).internalForm()).index; + CPRefEnt ce = new CPRefEnt(7); + ce.e1 = addUtf8(((Type.Object)o).internalForm()); ent = ce; } else if(o instanceof String) { - StringEnt se = new StringEnt(); - se.i = addUtf8((String)o).index; - ent = se; + CPRefEnt ce = new CPRefEnt(8); + ce.e1 = addUtf8((String)o); + ent = ce; } else if(o instanceof Integer) { - IntEnt ie = new IntEnt(); - ie.i = ((Integer)o).intValue(); - ent = ie; + OneU4Ent ue = new OneU4Ent(3); + ue.i = ((Integer)o).intValue(); + ent = ue; } else if(o instanceof Float) { - FloatEnt fe = new FloatEnt(); - fe.i = Float.floatToIntBits(((Float)o).floatValue()); - ent = fe; + OneU4Ent ue = new OneU4Ent(4); + ue.i = Float.floatToIntBits(((Float)o).floatValue()); + ent = ue; } else if(o instanceof Long) { - LongEnt le = new LongEnt(); - long l = ((Long)o).longValue(); - le.i = (int)(l>>>32); - le.i2 = (int)l; + LongEnt le = new LongEnt(5); + le.l = ((Long)o).longValue(); ent = le; } else if(o instanceof Double) { - DoubleEnt de = new DoubleEnt(); - long l = Double.doubleToLongBits(((Double)o).doubleValue()); - de.i = (int)(l>>>32); - de.i2 = (int)l; - ent = de; + LongEnt le = new LongEnt(6); + le.l = Double.doubleToLongBits(((Double)o).doubleValue()); + ent = le; } else if(o instanceof Utf8Key) { Utf8Ent ue = new Utf8Ent(); ue.s = ((Utf8Key)o).s; ent = ue; - } else if(o instanceof NameAndType) { - NameAndTypeEnt ne = new NameAndTypeEnt(); - NameAndType key = (NameAndType) o; - ne.i = addUtf8(key.name).index; - ne.i2 = addUtf8(key.type).index; - ent = ne; - } else if(o instanceof FieldMethodRef) { - FieldMethodRef key = (FieldMethodRef) o; - TwoU2Ent fme; - if(o instanceof MethodRef) fme = new MethodRefEnt(); - else if(o instanceof IMethodRef) fme = new IMethodRefEnt(); - else if(o instanceof FieldRef) fme = new FieldRefEnt(); - else throw new Error("should never happen"); - fme.i = add(key.klass).index; - fme.i2 = add(key.nameAndType).index; - ent = fme; + } else if(o instanceof ClassGen.NameAndType) { + CPRefEnt ce = new CPRefEnt(12); + ClassGen.NameAndType key = (ClassGen.NameAndType) o; + ce.e1 = addUtf8(key.name); + ce.e2 = addUtf8(key.type); + ent = ce; + } else if(o instanceof ClassGen.FieldMethodRef) { + ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o; + int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0; + if(tag == 0) throw new Error("should never happen"); + CPRefEnt ce = new CPRefEnt(tag); + ce.e1 = add(key.klass); + ce.e2 = add(key.nameAndType); + ent = ce; } else { throw new IllegalArgumentException("Unknown type passed to add"); } ent.index = nextIndex++; + if(ent instanceof LongEnt) nextIndex++; + count++; + entries.put(o,ent); return ent; } @@ -170,13 +163,14 @@ public class CPGen { public int size() { return nextIndex; } public void dump(DataOutput o) throws IOException { - Ent[] ents = new Ent[nextIndex-1]; + Ent[] ents = new Ent[count]; int i=0; Enumeration e = entries.keys(); while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement()); + if(i != count) throw new Error("should never happen"); Sort.sort(ents); for(i=0;i= 256) op = LDC_W; break; + + public final void set(int pos, byte op, boolean b) { set(pos,op,b?1:0); } + public final void set(int pos, byte op, int n) { + if(op == LDC) { + switch(n) { + case -1: set(pos,ICONST_M1); return; + case 0: set(pos,ICONST_0); return; + case 1: set(pos,ICONST_1); return; + case 2: set(pos,ICONST_2); return; + case 3: set(pos,ICONST_3); return; + case 4: set(pos,ICONST_4); return; + case 5: set(pos,ICONST_5); return; + } + if(n >= -128 && n <= 127) set(pos,BIPUSH,N(n)); + else if(n >= -32767 && n <= 32767) set(pos,SIPUSH,N(n)); + else set(pos,LDC,cp.add(N(n))); + } else { + set(pos,op,N(n)); } - this.op[pos] = op; - this.arg[pos] = arg; } + + private void set(int pos, byte op, Object arg) { + int iarg = arg instanceof Integer ? ((Integer)arg).intValue() : -1; - public final void setPushConst(int pos, int n) { - switch(n) { - case -1: set(pos,ICONST_M1); break; - case 0: set(pos,ICONST_0); break; - case 1: set(pos,ICONST_1); break; - case 2: set(pos,ICONST_2); break; - case 3: set(pos,ICONST_3); break; - case 4: set(pos,ICONST_4); break; - case 5: set(pos,ICONST_5); break; + switch(op) { + case ILOAD: case ISTORE: case LLOAD: case LSTORE: case FLOAD: + case FSTORE: case DLOAD: case DSTORE: case ALOAD: case ASTORE: + { + if(iarg >= 0 && iarg <= 3) { + int base = -1; + switch(op) { + case ILOAD: base = ILOAD_0; break; + case ISTORE: base = ISTORE_0; break; + case LLOAD: base = LLOAD_0; break; + case LSTORE: base = LSTORE_0; break; + case FLOAD: base = FLOAD_0; break; + case FSTORE: base = FSTORE_0; break; + case DLOAD: base = DLOAD_0; break; + case DSTORE: base = DSTORE_0; break; + case ALOAD: base = ALOAD_0; break; + case ASTORE: base = ASTORE_0; break; + } + op = (byte)(base + iarg); + } else { + if(iarg > maxLocals) maxLocals = iarg; + } + } + case LDC: + if(arg instanceof Integer) { set(pos,op,iarg); return; } + if(arg instanceof Boolean) { set(pos,op,((Boolean)arg).booleanValue()); return; } + if(arg instanceof Long) { + long l = ((Long)arg).longValue(); + if(l == 0L) { set(pos,LCONST_0); return; } + if(l == 1L) { set(pos,LCONST_1); return; } + } + + if(arg instanceof Long || arg instanceof Double) op = LDC2_W; + // fall through default: - if(n >= -128 && n <= 127) set(pos,BIPUSH,n); - if(n >= -32767 && n <= 32767) set(pos,SIPUSH,n); - setLDC(pos,N(n)); + if(OP_CPENT(op) && !(arg instanceof CPGen.Ent)) arg = cp.add(arg); break; } - } - public final void setPushConst(int pos, long l) { - if(l==0) set(pos,LCONST_0); - else if(l==1) set(pos,LCONST_1); - else setLDC(pos,N(l)); + _set(pos,op,arg); } - public final void setPushConst(int pos, float f) { - if(f == 1.0f) set(pos,FCONST_0); - else if(f == 1.0f) set(pos,FCONST_1); - else if(f == 2.0f) set(pos,FCONST_2); - else setLDC(pos,N(f)); - } - public final void setPushConst(int pos, double d) { - if(d == 1.0) set(pos,DCONST_0); - else if(d == 2.0) set(pos,DCONST_1); - else setLDC(pos,N(d)); - } - public final void setPushConst(int pos, Object o) { - if(o instanceof Integer) setPushConst(pos,((Integer)o).intValue()); - else if(o instanceof Long) setPushConst(pos,((Long)o).longValue()); - else if(o instanceof Float) setPushConst(pos,((Float)o).floatValue()); - else if(o instanceof Double) setPushConst(pos,((Double)o).doubleValue()); - else setLDC(pos,o); - } + public final void _set(int pos, byte op, Object arg) { + if(capacity == -1) throw new IllegalStateException("method can't have code"); + if(size == -1) throw new IllegalStateException("method is finalized"); + if(!OP_VALID(op)) throw new IllegalArgumentException("unknown bytecode"); - private void setLDC(int pos, Object o) { set(pos,LDC,cp.add(o)); } + this.op[pos] = op; + this.arg[pos] = arg; + } + + public static class SI { + public final Object[] targets; + public Object defaultTarget; - public final CPGen.Ent methodRef(Type.Object c, String name, Type ret, Type[] args) { - return methodRef(c,name,MethodGen.descriptor(ret,args)); + SI(int size) { targets = new Object[size]; } + public void setTarget(int pos, Object val) { targets[pos] = val; } + public void setTarget(int pos, int val) { targets[pos] = N(val); } + public void setDefaultTarget(int val) { setDefaultTarget(N(val)); } + public void setDefaultTarget(Object o) { defaultTarget = o; } + public int size() { return targets.length; } + + public int getTarget(int pos) { return ((Integer)targets[pos]).intValue(); } + public int getDefaultTarget() { return ((Integer)defaultTarget).intValue(); } } - public final CPGen.Ent methodRef(Type.Object c, String name, String descriptor) { - return cp.add(new CPGen.MethodRef(c,new CPGen.NameAndType(name,descriptor))); + + public static class TSI extends SI { + public final int lo; + public final int hi; + public int defaultTarget = -1; + public TSI(int lo, int hi) { + super(hi-lo+1); + this.lo = lo; + this.hi = hi; + } + public void setTargetForVal(int val, Object o) { setTarget(val-lo,o); } + public void setTargetForVal(int val, int n) { setTarget(val-lo,n); } } - public final CPGen.Ent fieldRef(Type.Object c, String name, Type type) { - return fieldRef(c,name,type.getDescriptor()); + + public static class LSI extends SI { + public final int[] vals; + public LSI(int size) { + super(size); + this.vals = new int[size]; + } + public final void setVal(int pos, int val) { vals[pos] = val; } } - public final CPGen.Ent fieldRef(Type.Object c, String name, String descriptor) { - return cp.add(new CPGen.FieldRef(c,new CPGen.NameAndType(name,descriptor))); - } + public static class Pair { + public int i1; + public int i2; + public Pair(int i1, int i2) { this.i1 = i1; this.i2 = i2; } + } + public void setMaxLocals(int maxLocals) { this.maxLocals = maxLocals; } public void setMaxStack(int maxStack) { this.maxStack = maxStack; } @@ -159,6 +233,20 @@ public class MethodGen implements CGConst { } } + private Object resolveTarget(Object arg) { + int target; + if(arg instanceof PhantomTarget) { + target = ((PhantomTarget)arg).getTarget(); + if(target == -1) throw new IllegalStateException("unresolved phantom target"); + arg = N(target); + } else { + target = ((Integer)arg).intValue(); + } + if(target < 0 || target >= size) + throw new IllegalStateException("invalid target address"); + return arg; + } + private void _finish() throws IOException { if(size == -1) return; @@ -169,17 +257,51 @@ public class MethodGen implements CGConst { int[] maxpc = pc; int p,i; - // Pass1 - Calculate maximum pc of each bytecode + // Pass1 - Calculate maximum pc of each bytecode, widen some insns, resolve any unresolved jumps, etc for(i=0,p=0;i= 256) this.op[i] = op = LDC_W; + // fall through + default: + if((j = OP_ARG_SIZE(op)) == -1) throw new Error("shouldn't be here"); + p += 1 + j; + break; } } + // Pass2 - Widen instructions if they can possibly be too short for(i=0;i 32767) op[i] = op[i] == GOTO ? GOTO_W : JSR_W; + break; } } } + // Pass3 - Calculate actual pc for(i=0,p=0;i 255 || pair[1] < -128 || pair[1] > 127) throw new ClassGen.Exn("overflow of iinc arg"); - o.writeByte(pair[0]); - o.writeByte(pair[1]); - continue; + Pair pair = (Pair) arg; + if(pair.i1 > 255 || pair.i2 < -128 || pair.i2 > 127) throw new ClassGen.Exn("overflow of iinc arg"); + o.writeByte(pair.i1); + o.writeByte(pair.i2); } - case IF_ICMPLT: - case GOTO: - case GOTO_W: - case JSR: - case JSR_W: - outArg = pc[iarg] - pc[i]; - if(outArg < -32768 || outArg > 32767) throw new ClassGen.Exn("overflow of s2 offset"); + case TABLESWITCH: + case LOOKUPSWITCH: { + SI si = (SI) arg; + int mypc = pc[i]; + for(p = pc[i]+1;(p&3)!=0;p++) o.writeByte(0); + o.writeInt(pc[si.getDefaultTarget()] - mypc); + if(op == LOOKUPSWITCH) { + int[] vals = ((LSI)si).vals; + o.writeInt(si.size()); + for(int j=0;j 32767) throw new ClassGen.Exn("overflow of s2 offset"); + o.writeShort(v); + } else if(OP_CPENT(op)) { + int v = ((CPGen.Ent)arg).getIndex(); + if(argLength == 1) o.writeByte(v); + else if(argLength == 2) o.writeShort(v); + else throw new Error("should never happen"); + } else if(argLength == -1) { + throw new Error("should never happen - variable length instruction not explicitly handled"); + } else { + if(!wasInt) throw new IllegalStateException("Invalid argument given for " + Integer.toHexString(op&0xff)); + if(argLength == 1) { + if(iarg < -128 || iarg >= 256) throw new ClassGen.Exn("overflow of s/u1 option"); + o.writeByte(iarg); + } else if(argLength == 2) { + if(iarg < -32767 || iarg >= 65536) throw new ClassGen.Exn("overflow of s/u2 option"); + o.writeShort(iarg); + } else { + throw new Error("should never happen"); + } + } break; } - - if(argLength == 1) o.writeByte(outArg); - else if(argLength == 2) o.writeShort(outArg); - else throw new Error("should never happen"); } - o.writeShort(0); // FIXME: Exception table + if(baos.size() - 8 != codeSize) throw new Error("we didn't output what we were supposed to"); + + o.writeShort(exnTable.size()); + for(Enumeration e = exnTable.keys();e.hasMoreElements();) + ((ExnTableEnt)exnTable.get(e.nextElement())).dump(o,pc,codeSize); + o.writeShort(codeAttrs.size()); codeAttrs.dump(o); @@ -257,71 +433,80 @@ public class MethodGen implements CGConst { byte[] codeAttribute = baos.toByteArray(); attrs.add("Code",codeAttribute); + + baos.reset(); + o.writeShort(thrownExceptions.size()); + for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();) + o.writeShort(((CPGen.Ent)thrownExceptions.get(e.nextElement())).getIndex()); + attrs.add("Exceptions",baos.toByteArray()); + size = -1; } public void dump(DataOutput o) throws IOException { o.writeShort(flags); - o.writeShort(nameIndex); - o.writeShort(descriptorIndex); + o.writeShort(cp.getUtf8Index(name)); + o.writeShort(cp.getUtf8Index(getDescriptor())); o.writeShort(attrs.size()); attrs.dump(o); } - private static int opArgLength(byte op) { + public static byte negate(byte op) { switch(op) { - case NOP: - case ICONST_M1: - case ICONST_0: - case ICONST_1: - case ICONST_2: - case ICONST_3: - case ICONST_4: - case ICONST_5: - case LCONST_0: - case LCONST_1: - case FCONST_0: - case FCONST_1: - case FCONST_2: - case DCONST_0: - case DCONST_1: - case ILOAD_0: - case ILOAD_1: - case ILOAD_2: - case ILOAD_3: - case ISTORE_0: - case ISTORE_1: - case ISTORE_2: - case ISTORE_3: - case RETURN: - return 0; - case LDC: - case BIPUSH: - case ILOAD: - case ISTORE: - return 1; - case LDC_W: - case SIPUSH: - case GETSTATIC: - case PUTSTATIC: - case GETFIELD: - case PUTFIELD: - case INVOKESTATIC: - case INVOKEVIRTUAL: - case INVOKESPECIAL: - case IINC: - case GOTO: - case JSR: - case IF_ICMPLT: - return 2; + case IFEQ: return IFNE; + case IFNE: return IFEQ; + case IFLT: return IFGE; + case IFGE: return IFLT; + case IFGT: return IFLE; + case IFLE: return IFGT; + case IF_ICMPEQ: return IF_ICMPNE; + case IF_ICMPNE: return IF_ICMPEQ; + case IF_ICMPLT: return IF_ICMPGE; + case IF_ICMPGE: return IF_ICMPLT; + case IF_ICMPGT: return IF_ICMPLE; + case IF_ICMPLE: return IF_ICMPGT; + case IF_ACMPEQ: return IF_ACMPNE; + case IF_ACMPNE: return IF_ACMPEQ; + default: - throw new ClassGen.Exn("unknown bytecode " + Integer.toHexString(op&0xff)); + throw new IllegalArgumentException("Can't negate " + Integer.toHexString(op)); } } - + + public static class PhantomTarget { + private int target = -1; + public void setTarget(int target) { this.target = target; } + public int getTarget() { return target; } + } + private static Integer N(int n) { return new Integer(n); } private static Long N(long n) { return new Long(n); } private static Float N(float f) { return new Float(f); } private static Double N(double d) { return new Double(d); } private static int max(int a, int b) { return a > b ? a : b; } + + private static final boolean OP_VALID(byte op) { return (OP_DATA[op&0xff] & 1) != 0; } + private static final int OP_ARG_SIZE(byte op) { int n = ((OP_DATA[op&0xff]>>1)&3); return n == 7 ? -1 : n; } + private static final boolean OP_CPENT(byte op) { return (OP_DATA[op&0xff]&(1<<4)) != 0; } + private static final boolean OP_BRANCH(byte op) { return (OP_DATA[op&0xff]&(1<<5)) != 0; } + + // Run perl -x src/org/ibex/classgen/CGConst.java to generate this + private static final byte[] OP_DATA = { + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x03, 0x05, 0x13, 0x15, 0x15, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x13, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, + 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x03, 0x0f, 0x0f, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x02, 0x15, 0x03, 0x15, 0x01, 0x01, + 0x15, 0x15, 0x01, 0x01, 0x0f, 0x07, 0x25, 0x25, 0x29, 0x29, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 + }; } diff --git a/src/org/ibex/classgen/MethodRef.java b/src/org/ibex/classgen/MethodRef.java new file mode 100644 index 0000000..cb546bd --- /dev/null +++ b/src/org/ibex/classgen/MethodRef.java @@ -0,0 +1,24 @@ +package org.ibex.classgen; + +public class MethodRef extends ClassGen.FieldMethodRef { + public MethodRef(Type.Object c, ClassGen.NameAndType t) { super(c,t); } + public MethodRef(Type.Object c, String name, String descriptor) { + this(c,new ClassGen.NameAndType(name,descriptor)); + } + public MethodRef(Type.Object c, String name, Type ret, Type[] args) { + this(c,name,getDescriptor(ret,args)); + } + public MethodRef(String s, String name, Type ret, Type[] args) { + this(new Type.Object(s),name,ret,args); + } + + static String getDescriptor(Type ret, Type[] args) { + StringBuffer sb = new StringBuffer(args.length*4); + sb.append("("); + for(int i=0;i