X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FMethodGen.java;h=854b76a6b25635606dec35bddaac2361d2c2f5d1;hp=8051a9ef344ab6a29ca8e518760d96b9d1f5b48b;hb=76a1336320be6b7dbae29acd341659ed648f9487;hpb=269385cf9a9f82b99097e249d47794c9cba109ae diff --git a/src/org/ibex/classgen/MethodGen.java b/src/org/ibex/classgen/MethodGen.java index 8051a9e..854b76a 100644 --- a/src/org/ibex/classgen/MethodGen.java +++ b/src/org/ibex/classgen/MethodGen.java @@ -5,12 +5,12 @@ import java.util.*; /** A class representing a method in a generated classfile @see ClassFile#addMethod */ -public class MethodGen implements CGConst { +public class MethodGen extends Type.Class.Method.Body implements CGConst { private final static boolean EMIT_NOPS = false; private static final int NO_CODE = -1; - private final Type.Class.Method method; + public final Type.Class.Method method; private final int flags; private final ClassFile.AttrGen attrs; private final ClassFile.AttrGen codeAttrs; @@ -25,10 +25,12 @@ public class MethodGen implements CGConst { private byte[] op; private Object[] arg; private ConstantPool.Ent[] cparg; - + + // Constructors ////////////////////////////////////////////////////////////////////////////// MethodGen(Type.Class.Method method, int flags) { + method.super(); if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags"); this.method = method; this.flags = flags; @@ -42,10 +44,16 @@ public class MethodGen implements CGConst { } MethodGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException { - this.flags = in.readShort(); + this(in.readShort(), cp.getUtf8KeyByIndex(in.readShort()), c, in, cp); } + + private MethodGen(short flags, String name, Type.Class c, DataInput in, ConstantPool cp) throws IOException { + this(flags, name, c.method(name+cp.getUtf8KeyByIndex(in.readShort())), c, in, cp); } + private MethodGen(short flags, String name, Type.Class.Method m, + Type.Class c, DataInput in, ConstantPool cp) throws IOException { + m.super(); + this.flags = flags; if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new ClassFile.ClassReadExn("invalid flags"); - String name = cp.getUtf8KeyByIndex(in.readShort()); - this.method = c.method(name+cp.getUtf8KeyByIndex(in.readShort())); + this.method = m; this.attrs = new ClassFile.AttrGen(in,cp); if ((flags & (NATIVE|ABSTRACT))==0) { @@ -142,12 +150,14 @@ public class MethodGen implements CGConst { ConstantPool.Ent ent = cp.getByIndex(in.readUnsignedShort()); if (ent.tag != CONSTANT_INTERFACEMETHODREF) throw new ClassFile.ClassReadExn("illegal argument to bytecode"); arg = ((ConstantPool.InterfaceMethodKey)ent.key()).method; - if (in.readByte() == 0 || in.readByte() != 0) throw new ClassFile.ClassReadExn("illegal count or 0 arg to invokeinterface"); + if (in.readByte() == 0 || in.readByte() != 0) + throw new ClassFile.ClassReadExn("illegal count or 0 arg to invokeinterface"); break; } default: if ((opdata&OP_CPENT_FLAG)!=0) { - ConstantPool.Ent ent = cp.getByIndex(argLength == 2 ? in.readUnsignedShort() : argLength == 1 ? in.readUnsignedByte() : -1); + ConstantPool.Ent ent = + cp.getByIndex(argLength == 2 ? in.readUnsignedShort() : argLength == 1 ? in.readUnsignedByte() : -1); int tag = ent.tag; Object key = ent.key(); switch(op) { @@ -163,25 +173,32 @@ public class MethodGen implements CGConst { case CONSTANT_CLASS: break; default: - throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + Integer.toString(op&0xff,16)); + throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + + Integer.toString(op&0xff,16)); } break; case GETSTATIC: case PUTSTATIC: case GETFIELD: case PUTFIELD: - if (tag != CONSTANT_FIELDREF) throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + Integer.toString(op&0xff,16)); + if (tag != CONSTANT_FIELDREF) + throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + + Integer.toString(op&0xff,16)); break; case INVOKEVIRTUAL: case INVOKESPECIAL: case INVOKESTATIC: - if (tag != CONSTANT_METHODREF) throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + Integer.toString(op&0xff,16)); + if (tag != CONSTANT_METHODREF) + throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + + Integer.toString(op&0xff,16)); break; case NEW: case ANEWARRAY: case CHECKCAST: case INSTANCEOF: - if (tag != CONSTANT_CLASS) throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + Integer.toString(op&0xff,16)); + if (tag != CONSTANT_CLASS) + throw new ClassFile.ClassReadExn("illegal argument to bytecode 0x" + + Integer.toString(op&0xff,16)); break; default: throw new Error("should never happen"); @@ -211,7 +228,8 @@ public class MethodGen implements CGConst { Switch si = (Switch) arg[i]; int pos = map[si.getDefaultTarget()]; - if (pos < 0) throw new ClassFile.ClassReadExn("default target points to invalid bytecode: " + si.getDefaultTarget()); + if (pos < 0) + throw new ClassFile.ClassReadExn("default target points to invalid bytecode: " + si.getDefaultTarget()); si.setDefaultTarget(pos); for(int j=0;jop (IF_ICMPGT -> IF_ICMPLE, IFNE -> IFEQ, etc) - @exception IllegalArgumentException if op isn't an IF* instruction */ - public static byte negate(byte op) { - switch(op) { - 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 IllegalArgumentException("Can't negate " + Integer.toHexString(op)); - } - } - private Object resolveTarget(Object arg) { int target; if (arg instanceof PhantomTarget) { @@ -956,7 +954,7 @@ public class MethodGen implements CGConst { // Debugging ////////////////////////////////////////////////////////////////////////////// - public void debugToString(StringBuffer sb, String constructorName) { + public void debugBodyToString(StringBuffer sb) { // This is intentionally a local variable so it can be removed by gcclass final String[] OP_NAMES = new String[]{ "nop", "aconst_null", "iconst_m1", "iconst_0", "iconst_1", "iconst_2", @@ -1003,35 +1001,46 @@ public class MethodGen implements CGConst { "", "", "", "", "", "", "", "", "", "" }; - - sb.append(" ").append(ClassFile.flagsToString(flags,false)); - sb.append(method.debugToString()); - if (thrownExceptions.size() > 0) { - sb.append("throws"); - for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();) - sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(","); - sb.setLength(sb.length()-1); - sb.append(" "); + for(int i=0;iop (IF_ICMPGT -> IF_ICMPLE, IFNE -> IFEQ, etc) + @exception IllegalArgumentException if op isn't an IF* instruction */ + public static byte negate(byte op) { + switch(op) { + 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 IllegalArgumentException("Can't negate " + Integer.toHexString(op)); } } + }