Example usage for org.objectweb.asm.tree InsnList add

List of usage examples for org.objectweb.asm.tree InsnList add

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList add.

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(ArrayLoadInst i, InsnList insns) {
    load(i.getArray(), insns);/* w  ww .j  av  a 2 s .  c  om*/
    load(i.getIndex(), insns);
    if (i.getType() instanceof ReferenceType)
        insns.add(new InsnNode(Opcodes.AALOAD));
    else if (i.getType().equals(booleanType) || i.getType().equals(byteType))
        insns.add(new InsnNode(Opcodes.BALOAD));
    else if (i.getType().equals(charType))
        insns.add(new InsnNode(Opcodes.CALOAD));
    else if (i.getType().equals(shortType))
        insns.add(new InsnNode(Opcodes.SALOAD));
    else if (i.getType().equals(intType))
        insns.add(new InsnNode(Opcodes.IALOAD));
    else if (i.getType().equals(longType))
        insns.add(new InsnNode(Opcodes.LALOAD));
    else if (i.getType().equals(floatType))
        insns.add(new InsnNode(Opcodes.FALOAD));
    else if (i.getType().equals(doubleType))
        insns.add(new InsnNode(Opcodes.DALOAD));
    else
        throw new AssertionError(i);
    store(i, insns);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(ArrayStoreInst i, InsnList insns) {
    load(i.getArray(), insns);/*from   w ww .java 2 s .c  om*/
    load(i.getIndex(), insns);
    load(i.getData(), insns);
    //TODO: what if the array is a null constant?
    RegularType componentType = ((ArrayType) i.getArray().getType()).getComponentType();
    if (componentType instanceof ReferenceType)
        insns.add(new InsnNode(Opcodes.AASTORE));
    else if (componentType.equals(booleanType) || componentType.equals(byteType))
        insns.add(new InsnNode(Opcodes.BASTORE));
    else if (componentType.equals(charType))
        insns.add(new InsnNode(Opcodes.CASTORE));
    else if (componentType.equals(shortType))
        insns.add(new InsnNode(Opcodes.SASTORE));
    else if (componentType.equals(intType))
        insns.add(new InsnNode(Opcodes.IASTORE));
    else if (componentType.equals(longType))
        insns.add(new InsnNode(Opcodes.LASTORE));
    else if (componentType.equals(floatType))
        insns.add(new InsnNode(Opcodes.FASTORE));
    else if (componentType.equals(doubleType))
        insns.add(new InsnNode(Opcodes.DASTORE));
    else
        throw new AssertionError(i);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(BinaryInst i, InsnList insns) {
    load(i.getOperand(0), insns);// ww w. j  ava 2s.c  om
    load(i.getOperand(1), insns);
    int opcode = 0;
    if (i.getOperand(0).getType().isSubtypeOf(intType)) {
        switch (i.getOperation()) {
        case ADD:
            opcode = Opcodes.IADD;
            break;
        case SUB:
            opcode = Opcodes.ISUB;
            break;
        case MUL:
            opcode = Opcodes.IMUL;
            break;
        case DIV:
            opcode = Opcodes.IDIV;
            break;
        case REM:
            opcode = Opcodes.IREM;
            break;
        case SHL:
            opcode = Opcodes.ISHL;
            break;
        case SHR:
            opcode = Opcodes.ISHR;
            break;
        case USHR:
            opcode = Opcodes.ISHR;
            break;
        case AND:
            opcode = Opcodes.IAND;
            break;
        case OR:
            opcode = Opcodes.IOR;
            break;
        case XOR:
            opcode = Opcodes.IXOR;
            break;
        default:
            throw new AssertionError(i);
        }
    } else if (i.getOperand(0).getType().equals(longType)) {
        switch (i.getOperation()) {
        case ADD:
            opcode = Opcodes.LADD;
            break;
        case SUB:
            opcode = Opcodes.LSUB;
            break;
        case MUL:
            opcode = Opcodes.LMUL;
            break;
        case DIV:
            opcode = Opcodes.LDIV;
            break;
        case REM:
            opcode = Opcodes.LREM;
            break;
        case SHL:
            opcode = Opcodes.LSHL;
            break;
        case SHR:
            opcode = Opcodes.LSHR;
            break;
        case USHR:
            opcode = Opcodes.LSHR;
            break;
        case AND:
            opcode = Opcodes.LAND;
            break;
        case OR:
            opcode = Opcodes.LOR;
            break;
        case XOR:
            opcode = Opcodes.LXOR;
            break;
        case CMP:
            opcode = Opcodes.LCMP;
            break;
        default:
            throw new AssertionError(i);
        }
    } else if (i.getOperand(0).getType().equals(floatType)) {
        switch (i.getOperation()) {
        case ADD:
            opcode = Opcodes.FADD;
            break;
        case SUB:
            opcode = Opcodes.FSUB;
            break;
        case MUL:
            opcode = Opcodes.FMUL;
            break;
        case DIV:
            opcode = Opcodes.FDIV;
            break;
        case REM:
            opcode = Opcodes.FREM;
            break;
        case CMP:
            opcode = Opcodes.FCMPL;
            break;
        case CMPG:
            opcode = Opcodes.FCMPG;
            break;
        default:
            throw new AssertionError(i);
        }
    } else if (i.getOperand(0).getType().equals(doubleType)) {
        switch (i.getOperation()) {
        case ADD:
            opcode = Opcodes.DADD;
            break;
        case SUB:
            opcode = Opcodes.DSUB;
            break;
        case MUL:
            opcode = Opcodes.DMUL;
            break;
        case DIV:
            opcode = Opcodes.DDIV;
            break;
        case REM:
            opcode = Opcodes.DREM;
            break;
        case CMP:
            opcode = Opcodes.DCMPL;
            break;
        case CMPG:
            opcode = Opcodes.DCMPG;
            break;
        default:
            throw new AssertionError(i);
        }
    } else
        throw new AssertionError(i);
    insns.add(new InsnNode(opcode));
    store(i, insns);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(BranchInst i, InsnList insns) {
    //TODO: accessor methods on BranchInst
    Value left = i.getOperand(0), right = i.getOperand(1);
    BasicBlock target = (BasicBlock) i.getOperand(2), fallthrough = (BasicBlock) i.getOperand(3);
    if (!method.basicBlocks().contains(target))
        throw new IllegalArgumentException("Branch targets block not in method: " + i);
    if (!method.basicBlocks().contains(fallthrough))
        throw new IllegalArgumentException("Branch falls through to block not in method: " + i);
    load(i.getOperand(0), insns);/*from  ww w.ja  v  a2s.co m*/
    load(i.getOperand(1), insns);
    //TODO: long, float, doubles need to go through CMP inst first
    int opcode;
    if (left.getType() instanceof ReferenceType || left.getType() instanceof VoidType) {
        switch (i.getSense()) {
        case EQ:
            opcode = Opcodes.IF_ACMPEQ;
            break;
        case NE:
            opcode = Opcodes.IF_ACMPNE;
            break;
        default:
            throw new AssertionError(i);
        }
    } else if (left.getType().isSubtypeOf(intType)) {
        switch (i.getSense()) {
        case EQ:
            opcode = Opcodes.IF_ICMPEQ;
            break;
        case NE:
            opcode = Opcodes.IF_ICMPNE;
            break;
        case LT:
            opcode = Opcodes.IF_ICMPLT;
            break;
        case GT:
            opcode = Opcodes.IF_ICMPGT;
            break;
        case LE:
            opcode = Opcodes.IF_ICMPLE;
            break;
        case GE:
            opcode = Opcodes.IF_ICMPGE;
            break;
        default:
            throw new AssertionError(i);
        }
    } else
        throw new AssertionError(i);
    insns.add(new JumpInsnNode(opcode, labels.get(target)));
    insns.add(new JumpInsnNode(Opcodes.GOTO, labels.get(fallthrough)));
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(CallInst i, InsnList insns) {
    Method m = i.getMethod();//ww w. j  av a2s.  co m
    boolean callingSuperCtor = false;
    if (m.isConstructor()) {
        //If we're calling super(), load this.
        //TODO: this will get confused if we call a superclass constructor
        //for any reason other than our own initialization!
        if (method.isConstructor() && method.getParent().getSuperclass().equals(m.getParent())) {
            load(method.arguments().get(0), insns);
            callingSuperCtor = true;
        } else {
            insns.add(new TypeInsnNode(Opcodes.NEW, internalName(m.getType().getReturnType().getKlass())));
            insns.add(new InsnNode(Opcodes.DUP));
        }
    }
    int opcode;
    if (m.modifiers().contains(Modifier.STATIC))
        opcode = Opcodes.INVOKESTATIC;
    else if (m.isConstructor() || m.getAccess().equals(Access.PRIVATE) ||
    //We're calling a superclass method we've overridden.
            (Iterables.contains(method.getParent().superclasses(), m.getParent()))
                    && method.getParent().getMethodByVirtual(m.getName(), m.getType()) != m)
        opcode = Opcodes.INVOKESPECIAL;
    else if (m.getParent().modifiers().contains(Modifier.INTERFACE))
        //TODO: may not be correct?
        opcode = Opcodes.INVOKEINTERFACE;
    else
        opcode = Opcodes.INVOKEVIRTUAL;

    String owner = internalName(m.getParent());
    //hack to make cloning arrays work
    if (opcode == Opcodes.INVOKESPECIAL && m.getName().equals("clone")
            && i.getArgument(0).getType() instanceof ArrayType) {
        opcode = Opcodes.INVOKEVIRTUAL;
        owner = internalName(((ArrayType) i.getArgument(0).getType()).getKlass());
    }

    for (Value v : i.arguments())
        load(v, insns);
    insns.add(new MethodInsnNode(opcode, owner, m.getName(), i.callDescriptor()));

    if (!(i.getType() instanceof VoidType) && !callingSuperCtor)
        store(i, insns);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(CastInst i, InsnList insns) {
    load(i.getOperand(0), insns);/*from   w  w w.  j a v  a  2s  . c  o  m*/
    if (i.getType() instanceof ReferenceType) {
        insns.add(new TypeInsnNode(Opcodes.CHECKCAST, internalName(((ReferenceType) i.getType()).getKlass())));
    } else {
        PrimitiveType from = (PrimitiveType) i.getOperand(0).getType();
        PrimitiveType to = (PrimitiveType) i.getType();
        for (int op : from.getCastOpcode(to))
            insns.add(new InsnNode(op));
    }
    store(i, insns);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(InstanceofInst i, InsnList insns) {
    load(i.getOperand(0), insns);//from  w  w w.  j  av a 2 s .com
    insns.add(new TypeInsnNode(Opcodes.INSTANCEOF, internalName(i.getTestType().getKlass())));
    store(i, insns);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(JumpInst i, InsnList insns) {
    BasicBlock target = (BasicBlock) i.getOperand(0);
    if (!method.basicBlocks().contains(target))
        throw new IllegalArgumentException("Jump to block not in method: " + i);
    insns.add(new JumpInsnNode(Opcodes.GOTO, labels.get(target)));
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(LoadInst i, InsnList insns) {
    Value location = i.getLocation();// w  ww  . j  a  v a2s .c o  m
    if (location instanceof LocalVariable) {
        load(location, insns);
        store(i, insns);
    } else {
        Field f = (Field) location;
        if (!f.isStatic())
            load(i.getInstance(), insns);
        insns.add(new FieldInsnNode(f.isStatic() ? Opcodes.GETSTATIC : Opcodes.GETFIELD,
                internalName(f.getParent()), f.getName(), f.getType().getFieldType().getDescriptor()));
        store(i, insns);
    }
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(NewArrayInst i, InsnList insns) {
    ArrayType t = i.getType();/*from w w w. j a va2s  .  com*/
    if (t.getDimensions() == 1) {
        load(i.getOperand(0), insns);
        RegularType ct = t.getComponentType();
        if (ct instanceof PrimitiveType) {
            if (ct.equals(booleanType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_BOOLEAN));
            else if (ct.equals(byteType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_BYTE));
            else if (ct.equals(charType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_CHAR));
            else if (ct.equals(shortType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_SHORT));
            else if (ct.equals(intType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT));
            else if (ct.equals(longType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_LONG));
            else if (ct.equals(floatType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_FLOAT));
            else if (ct.equals(doubleType))
                insns.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_DOUBLE));
        } else {
            insns.add(new TypeInsnNode(Opcodes.ANEWARRAY, internalName(ct.getKlass())));
        }
    } else {
        for (Value v : i.operands())
            load(v, insns);
        insns.add(new MultiANewArrayInsnNode(t.getDescriptor(), i.getNumOperands()));
    }
    store(i, insns);
}