286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.generic;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/A
286N/A/**
286N/A * Instances of this class may be used, e.g., to generate typed
286N/A * versions of instructions. Its main purpose is to be used as the
286N/A * byte code generating backend of a compiler. You can subclass it to
286N/A * add your own create methods.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A * @see Constants
286N/A */
286N/Apublic class InstructionFactory
286N/A implements InstructionConstants, java.io.Serializable
286N/A{
286N/A protected ClassGen cg;
286N/A protected ConstantPoolGen cp;
286N/A
286N/A public InstructionFactory(ClassGen cg, ConstantPoolGen cp) {
286N/A this.cg = cg;
286N/A this.cp = cp;
286N/A }
286N/A
286N/A /** Initialize with ClassGen object
286N/A */
286N/A public InstructionFactory(ClassGen cg) {
286N/A this(cg, cg.getConstantPool());
286N/A }
286N/A
286N/A /** Initialize just with ConstantPoolGen object
286N/A */
286N/A public InstructionFactory(ConstantPoolGen cp) {
286N/A this(null, cp);
286N/A }
286N/A
286N/A /** Create an invoke instruction.
286N/A *
286N/A * @param class_name name of the called class
286N/A * @param name name of the called method
286N/A * @param ret_type return type of method
286N/A * @param arg_types argument types of method
286N/A * @param kind how to invoke, i.e., INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL,
286N/A * or INVOKESPECIAL
286N/A * @see Constants
286N/A */
286N/A public InvokeInstruction createInvoke(String class_name, String name, Type ret_type,
286N/A Type[] arg_types, short kind) {
286N/A int index;
286N/A int nargs = 0;
286N/A String signature = Type.getMethodSignature(ret_type, arg_types);
286N/A
286N/A for(int i=0; i < arg_types.length; i++) // Count size of arguments
286N/A nargs += arg_types[i].getSize();
286N/A
286N/A if(kind == Constants.INVOKEINTERFACE)
286N/A index = cp.addInterfaceMethodref(class_name, name, signature);
286N/A else
286N/A index = cp.addMethodref(class_name, name, signature);
286N/A
286N/A switch(kind) {
286N/A case Constants.INVOKESPECIAL: return new INVOKESPECIAL(index);
286N/A case Constants.INVOKEVIRTUAL: return new INVOKEVIRTUAL(index);
286N/A case Constants.INVOKESTATIC: return new INVOKESTATIC(index);
286N/A case Constants.INVOKEINTERFACE: return new INVOKEINTERFACE(index, nargs + 1);
286N/A default:
286N/A throw new RuntimeException("Oops: Unknown invoke kind:" + kind);
286N/A }
286N/A }
286N/A
286N/A /** Create a call to the most popular System.out.println() method.
286N/A *
286N/A * @param s the string to print
286N/A */
286N/A public InstructionList createPrintln(String s) {
286N/A InstructionList il = new InstructionList();
286N/A int out = cp.addFieldref("java.lang.System", "out",
286N/A "Ljava/io/PrintStream;");
286N/A int println = cp.addMethodref("java.io.PrintStream", "println",
286N/A "(Ljava/lang/String;)V");
286N/A
286N/A il.append(new GETSTATIC(out));
286N/A il.append(new PUSH(cp, s));
286N/A il.append(new INVOKEVIRTUAL(println));
286N/A
286N/A return il;
286N/A }
286N/A
286N/A /** Uses PUSH to push a constant value onto the stack.
286N/A * @param value must be of type Number, Boolean, Character or String
286N/A */
286N/A public Instruction createConstant(Object value) {
286N/A PUSH push;
286N/A
286N/A if(value instanceof Number)
286N/A push = new PUSH(cp, (Number)value);
286N/A else if(value instanceof String)
286N/A push = new PUSH(cp, (String)value);
286N/A else if(value instanceof Boolean)
286N/A push = new PUSH(cp, (Boolean)value);
286N/A else if(value instanceof Character)
286N/A push = new PUSH(cp, (Character)value);
286N/A else
286N/A throw new ClassGenException("Illegal type: " + value.getClass());
286N/A
286N/A return push.getInstruction();
286N/A }
286N/A
286N/A private static class MethodObject {
286N/A Type[] arg_types;
286N/A Type result_type;
286N/A String[] arg_names;
286N/A String class_name;
286N/A String name;
286N/A int access;
286N/A
286N/A MethodObject(String c, String n, Type r, Type[] a, int acc) {
286N/A class_name = c;
286N/A name = n;
286N/A result_type = r;
286N/A arg_types = a;
286N/A access = acc;
286N/A }
286N/A }
286N/A
286N/A private InvokeInstruction createInvoke(MethodObject m, short kind) {
286N/A return createInvoke(m.class_name, m.name, m.result_type, m.arg_types, kind);
286N/A }
286N/A
286N/A private static MethodObject[] append_mos = {
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.STRING }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.OBJECT }, Constants.ACC_PUBLIC),
286N/A null, null, // indices 2, 3
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.BOOLEAN }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.CHAR }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.FLOAT }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.DOUBLE }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.INT }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER, // No append(byte)
286N/A new Type[] { Type.INT }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER, // No append(short)
286N/A new Type[] { Type.INT }, Constants.ACC_PUBLIC),
286N/A new MethodObject("java.lang.StringBuffer", "append", Type.STRINGBUFFER,
286N/A new Type[] { Type.LONG }, Constants.ACC_PUBLIC)
286N/A };
286N/A
286N/A private static final boolean isString(Type type) {
286N/A return ((type instanceof ObjectType) &&
286N/A ((ObjectType)type).getClassName().equals("java.lang.String"));
286N/A }
286N/A
286N/A public Instruction createAppend(Type type) {
286N/A byte t = type.getType();
286N/A
286N/A if(isString(type))
286N/A return createInvoke(append_mos[0], Constants.INVOKEVIRTUAL);
286N/A
286N/A switch(t) {
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_CHAR:
286N/A case Constants.T_FLOAT:
286N/A case Constants.T_DOUBLE:
286N/A case Constants.T_BYTE:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_INT:
286N/A case Constants.T_LONG
286N/A : return createInvoke(append_mos[t], Constants.INVOKEVIRTUAL);
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT:
286N/A return createInvoke(append_mos[1], Constants.INVOKEVIRTUAL);
286N/A default:
286N/A throw new RuntimeException("Oops: No append for this type? " + type);
286N/A }
286N/A }
286N/A
286N/A /** Create a field instruction.
286N/A *
286N/A * @param class_name name of the accessed class
286N/A * @param name name of the referenced field
286N/A * @param type type of field
286N/A * @param kind how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC
286N/A * @see Constants
286N/A */
286N/A public FieldInstruction createFieldAccess(String class_name, String name, Type type, short kind) {
286N/A int index;
286N/A String signature = type.getSignature();
286N/A
286N/A index = cp.addFieldref(class_name, name, signature);
286N/A
286N/A switch(kind) {
286N/A case Constants.GETFIELD: return new GETFIELD(index);
286N/A case Constants.PUTFIELD: return new PUTFIELD(index);
286N/A case Constants.GETSTATIC: return new GETSTATIC(index);
286N/A case Constants.PUTSTATIC: return new PUTSTATIC(index);
286N/A
286N/A default:
286N/A throw new RuntimeException("Oops: Unknown getfield kind:" + kind);
286N/A }
286N/A }
286N/A
286N/A /** Create reference to `this'
286N/A */
286N/A public static Instruction createThis() {
286N/A return new ALOAD(0);
286N/A }
286N/A
286N/A /** Create typed return
286N/A */
286N/A public static ReturnInstruction createReturn(Type type) {
286N/A switch(type.getType()) {
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return ARETURN;
286N/A case Constants.T_INT:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_CHAR:
286N/A case Constants.T_BYTE: return IRETURN;
286N/A case Constants.T_FLOAT: return FRETURN;
286N/A case Constants.T_DOUBLE: return DRETURN;
286N/A case Constants.T_LONG: return LRETURN;
286N/A case Constants.T_VOID: return RETURN;
286N/A
286N/A default:
286N/A throw new RuntimeException("Invalid type: " + type);
286N/A }
286N/A }
286N/A
286N/A private static final ArithmeticInstruction createBinaryIntOp(char first, String op) {
286N/A switch(first) {
286N/A case '-' : return ISUB;
286N/A case '+' : return IADD;
286N/A case '%' : return IREM;
286N/A case '*' : return IMUL;
286N/A case '/' : return IDIV;
286N/A case '&' : return IAND;
286N/A case '|' : return IOR;
286N/A case '^' : return IXOR;
286N/A case '<' : return ISHL;
286N/A case '>' : return op.equals(">>>")? (ArithmeticInstruction)IUSHR :
286N/A (ArithmeticInstruction)ISHR;
286N/A default: throw new RuntimeException("Invalid operand " + op);
286N/A }
286N/A }
286N/A
286N/A private static final ArithmeticInstruction createBinaryLongOp(char first, String op) {
286N/A switch(first) {
286N/A case '-' : return LSUB;
286N/A case '+' : return LADD;
286N/A case '%' : return LREM;
286N/A case '*' : return LMUL;
286N/A case '/' : return LDIV;
286N/A case '&' : return LAND;
286N/A case '|' : return LOR;
286N/A case '^' : return LXOR;
286N/A case '<' : return LSHL;
286N/A case '>' : return op.equals(">>>")? (ArithmeticInstruction)LUSHR :
286N/A (ArithmeticInstruction)LSHR;
286N/A default: throw new RuntimeException("Invalid operand " + op);
286N/A }
286N/A }
286N/A
286N/A private static final ArithmeticInstruction createBinaryFloatOp(char op) {
286N/A switch(op) {
286N/A case '-' : return FSUB;
286N/A case '+' : return FADD;
286N/A case '*' : return FMUL;
286N/A case '/' : return FDIV;
286N/A default: throw new RuntimeException("Invalid operand " + op);
286N/A }
286N/A }
286N/A
286N/A private static final ArithmeticInstruction createBinaryDoubleOp(char op) {
286N/A switch(op) {
286N/A case '-' : return DSUB;
286N/A case '+' : return DADD;
286N/A case '*' : return DMUL;
286N/A case '/' : return DDIV;
286N/A default: throw new RuntimeException("Invalid operand " + op);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Create binary operation for simple basic types, such as int and float.
286N/A *
286N/A * @param op operation, such as "+", "*", "<<", etc.
286N/A */
286N/A public static ArithmeticInstruction createBinaryOperation(String op, Type type) {
286N/A char first = op.toCharArray()[0];
286N/A
286N/A switch(type.getType()) {
286N/A case Constants.T_BYTE:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_INT:
286N/A case Constants.T_CHAR: return createBinaryIntOp(first, op);
286N/A case Constants.T_LONG: return createBinaryLongOp(first, op);
286N/A case Constants.T_FLOAT: return createBinaryFloatOp(first);
286N/A case Constants.T_DOUBLE: return createBinaryDoubleOp(first);
286N/A default: throw new RuntimeException("Invalid type " + type);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @param size size of operand, either 1 (int, e.g.) or 2 (double)
286N/A */
286N/A public static StackInstruction createPop(int size) {
286N/A return (size == 2)? (StackInstruction)POP2 :
286N/A (StackInstruction)POP;
286N/A }
286N/A
286N/A /**
286N/A * @param size size of operand, either 1 (int, e.g.) or 2 (double)
286N/A */
286N/A public static StackInstruction createDup(int size) {
286N/A return (size == 2)? (StackInstruction)DUP2 :
286N/A (StackInstruction)DUP;
286N/A }
286N/A
286N/A /**
286N/A * @param size size of operand, either 1 (int, e.g.) or 2 (double)
286N/A */
286N/A public static StackInstruction createDup_2(int size) {
286N/A return (size == 2)? (StackInstruction)DUP2_X2 :
286N/A (StackInstruction)DUP_X2;
286N/A }
286N/A
286N/A /**
286N/A * @param size size of operand, either 1 (int, e.g.) or 2 (double)
286N/A */
286N/A public static StackInstruction createDup_1(int size) {
286N/A return (size == 2)? (StackInstruction)DUP2_X1 :
286N/A (StackInstruction)DUP_X1;
286N/A }
286N/A
286N/A /**
286N/A * @param index index of local variable
286N/A */
286N/A public static LocalVariableInstruction createStore(Type type, int index) {
286N/A switch(type.getType()) {
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_CHAR:
286N/A case Constants.T_BYTE:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_INT: return new ISTORE(index);
286N/A case Constants.T_FLOAT: return new FSTORE(index);
286N/A case Constants.T_DOUBLE: return new DSTORE(index);
286N/A case Constants.T_LONG: return new LSTORE(index);
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return new ASTORE(index);
286N/A default: throw new RuntimeException("Invalid type " + type);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @param index index of local variable
286N/A */
286N/A public static LocalVariableInstruction createLoad(Type type, int index) {
286N/A switch(type.getType()) {
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_CHAR:
286N/A case Constants.T_BYTE:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_INT: return new ILOAD(index);
286N/A case Constants.T_FLOAT: return new FLOAD(index);
286N/A case Constants.T_DOUBLE: return new DLOAD(index);
286N/A case Constants.T_LONG: return new LLOAD(index);
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return new ALOAD(index);
286N/A default: throw new RuntimeException("Invalid type " + type);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @param type type of elements of array, i.e., array.getElementType()
286N/A */
286N/A public static ArrayInstruction createArrayLoad(Type type) {
286N/A switch(type.getType()) {
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_BYTE: return BALOAD;
286N/A case Constants.T_CHAR: return CALOAD;
286N/A case Constants.T_SHORT: return SALOAD;
286N/A case Constants.T_INT: return IALOAD;
286N/A case Constants.T_FLOAT: return FALOAD;
286N/A case Constants.T_DOUBLE: return DALOAD;
286N/A case Constants.T_LONG: return LALOAD;
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return AALOAD;
286N/A default: throw new RuntimeException("Invalid type " + type);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @param type type of elements of array, i.e., array.getElementType()
286N/A */
286N/A public static ArrayInstruction createArrayStore(Type type) {
286N/A switch(type.getType()) {
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_BYTE: return BASTORE;
286N/A case Constants.T_CHAR: return CASTORE;
286N/A case Constants.T_SHORT: return SASTORE;
286N/A case Constants.T_INT: return IASTORE;
286N/A case Constants.T_FLOAT: return FASTORE;
286N/A case Constants.T_DOUBLE: return DASTORE;
286N/A case Constants.T_LONG: return LASTORE;
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return AASTORE;
286N/A default: throw new RuntimeException("Invalid type " + type);
286N/A }
286N/A }
286N/A
286N/A
286N/A /** Create conversion operation for two stack operands, this may be an I2C, instruction, e.g.,
286N/A * if the operands are basic types and CHECKCAST if they are reference types.
286N/A */
286N/A public Instruction createCast(Type src_type, Type dest_type) {
286N/A if((src_type instanceof BasicType) && (dest_type instanceof BasicType)) {
286N/A byte dest = dest_type.getType();
286N/A byte src = src_type.getType();
286N/A
286N/A if(dest == Constants.T_LONG && (src == Constants.T_CHAR || src == Constants.T_BYTE ||
286N/A src == Constants.T_SHORT))
286N/A src = Constants.T_INT;
286N/A
286N/A String[] short_names = { "C", "F", "D", "B", "S", "I", "L" };
286N/A
286N/A String name = "com.sun.org.apache.bcel.internal.generic." + short_names[src - Constants.T_CHAR] +
286N/A "2" + short_names[dest - Constants.T_CHAR];
286N/A
286N/A Instruction i = null;
286N/A try {
286N/A i = (Instruction)java.lang.Class.forName(name).newInstance();
286N/A } catch(Exception e) {
286N/A throw new RuntimeException("Could not find instruction: " + name);
286N/A }
286N/A
286N/A return i;
286N/A } else if((src_type instanceof ReferenceType) && (dest_type instanceof ReferenceType)) {
286N/A if(dest_type instanceof ArrayType)
286N/A return new CHECKCAST(cp.addArrayClass((ArrayType)dest_type));
286N/A else
286N/A return new CHECKCAST(cp.addClass(((ObjectType)dest_type).getClassName()));
286N/A }
286N/A else
286N/A throw new RuntimeException("Can not cast " + src_type + " to " + dest_type);
286N/A }
286N/A
286N/A public GETFIELD createGetField(String class_name, String name, Type t) {
286N/A return new GETFIELD(cp.addFieldref(class_name, name, t.getSignature()));
286N/A }
286N/A
286N/A public GETSTATIC createGetStatic(String class_name, String name, Type t) {
286N/A return new GETSTATIC(cp.addFieldref(class_name, name, t.getSignature()));
286N/A }
286N/A
286N/A public PUTFIELD createPutField(String class_name, String name, Type t) {
286N/A return new PUTFIELD(cp.addFieldref(class_name, name, t.getSignature()));
286N/A }
286N/A
286N/A public PUTSTATIC createPutStatic(String class_name, String name, Type t) {
286N/A return new PUTSTATIC(cp.addFieldref(class_name, name, t.getSignature()));
286N/A }
286N/A
286N/A public CHECKCAST createCheckCast(ReferenceType t) {
286N/A if(t instanceof ArrayType)
286N/A return new CHECKCAST(cp.addArrayClass((ArrayType)t));
286N/A else
286N/A return new CHECKCAST(cp.addClass((ObjectType)t));
286N/A }
286N/A
286N/A public INSTANCEOF createInstanceOf(ReferenceType t) {
286N/A if(t instanceof ArrayType)
286N/A return new INSTANCEOF(cp.addArrayClass((ArrayType)t));
286N/A else
286N/A return new INSTANCEOF(cp.addClass((ObjectType)t));
286N/A }
286N/A
286N/A public NEW createNew(ObjectType t) {
286N/A return new NEW(cp.addClass(t));
286N/A }
286N/A
286N/A public NEW createNew(String s) {
286N/A return createNew(new ObjectType(s));
286N/A }
286N/A
286N/A /** Create new array of given size and type.
286N/A * @return an instruction that creates the corresponding array at runtime, i.e. is an AllocationInstruction
286N/A */
286N/A public Instruction createNewArray(Type t, short dim) {
286N/A if(dim == 1) {
286N/A if(t instanceof ObjectType)
286N/A return new ANEWARRAY(cp.addClass((ObjectType)t));
286N/A else if(t instanceof ArrayType)
286N/A return new ANEWARRAY(cp.addArrayClass((ArrayType)t));
286N/A else
286N/A return new NEWARRAY(((BasicType)t).getType());
286N/A } else {
286N/A ArrayType at;
286N/A
286N/A if(t instanceof ArrayType)
286N/A at = (ArrayType)t;
286N/A else
286N/A at = new ArrayType(t, dim);
286N/A
286N/A return new MULTIANEWARRAY(cp.addArrayClass(at), dim);
286N/A }
286N/A }
286N/A
286N/A /** Create "null" value for reference types, 0 for basic types like int
286N/A */
286N/A public static Instruction createNull(Type type) {
286N/A switch(type.getType()) {
286N/A case Constants.T_ARRAY:
286N/A case Constants.T_OBJECT: return ACONST_NULL;
286N/A case Constants.T_INT:
286N/A case Constants.T_SHORT:
286N/A case Constants.T_BOOLEAN:
286N/A case Constants.T_CHAR:
286N/A case Constants.T_BYTE: return ICONST_0;
286N/A case Constants.T_FLOAT: return FCONST_0;
286N/A case Constants.T_DOUBLE: return DCONST_0;
286N/A case Constants.T_LONG: return LCONST_0;
286N/A case Constants.T_VOID: return NOP;
286N/A
286N/A default:
286N/A throw new RuntimeException("Invalid type: " + type);
286N/A }
286N/A }
286N/A
286N/A /** Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH.
286N/A * For those you should use the SWITCH compound instruction.
286N/A */
286N/A public static BranchInstruction createBranchInstruction(short opcode, InstructionHandle target) {
286N/A switch(opcode) {
286N/A case Constants.IFEQ: return new IFEQ(target);
286N/A case Constants.IFNE: return new IFNE(target);
286N/A case Constants.IFLT: return new IFLT(target);
286N/A case Constants.IFGE: return new IFGE(target);
286N/A case Constants.IFGT: return new IFGT(target);
286N/A case Constants.IFLE: return new IFLE(target);
286N/A case Constants.IF_ICMPEQ: return new IF_ICMPEQ(target);
286N/A case Constants.IF_ICMPNE: return new IF_ICMPNE(target);
286N/A case Constants.IF_ICMPLT: return new IF_ICMPLT(target);
286N/A case Constants.IF_ICMPGE: return new IF_ICMPGE(target);
286N/A case Constants.IF_ICMPGT: return new IF_ICMPGT(target);
286N/A case Constants.IF_ICMPLE: return new IF_ICMPLE(target);
286N/A case Constants.IF_ACMPEQ: return new IF_ACMPEQ(target);
286N/A case Constants.IF_ACMPNE: return new IF_ACMPNE(target);
286N/A case Constants.GOTO: return new GOTO(target);
286N/A case Constants.JSR: return new JSR(target);
286N/A case Constants.IFNULL: return new IFNULL(target);
286N/A case Constants.IFNONNULL: return new IFNONNULL(target);
286N/A case Constants.GOTO_W: return new GOTO_W(target);
286N/A case Constants.JSR_W: return new JSR_W(target);
286N/A default:
286N/A throw new RuntimeException("Invalid opcode: " + opcode);
286N/A }
286N/A }
286N/A
286N/A public void setClassGen(ClassGen c) { cg = c; }
286N/A public ClassGen getClassGen() { return cg; }
286N/A public void setConstantPool(ConstantPoolGen c) { cp = c; }
286N/A public ConstantPoolGen getConstantPool() { return cp; }
286N/A}