Example usage for org.objectweb.asm Opcodes ARRAYLENGTH

List of usage examples for org.objectweb.asm Opcodes ARRAYLENGTH

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ARRAYLENGTH.

Prototype

int ARRAYLENGTH

To view the source code for org.objectweb.asm Opcodes ARRAYLENGTH.

Click Source Link

Usage

From source file:com.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

/**
 * Reflection-based allocation (@see java.lang.reflect.Array#newInstance) is
 * triggered with a static method call (INVOKESTATIC), so we hook it here.
 * Class initialization is triggered with a constructor call (INVOKESPECIAL)
 * so we hook that here too as a proxy for the new bytecode which leaves an
 * uninitialized object on the stack that we're not allowed to touch.
 * {@link java.lang.Object#clone} is also a call to INVOKESPECIAL,
 * and is hooked here.  {@link java.lang.Class#newInstance} and
 * {@link java.lang.reflect.Constructor#newInstance} are both
 * INVOKEVIRTUAL calls, so they are hooked here, as well.
 *///www  . j  a  v  a  2  s. com
@Override
public void visitMethodInsn(int opcode, String owner, String name, String signature, boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC &&
    // Array does its own native allocation.  Grr.
            owner.equals("java/lang/reflect/Array") && name.equals("newInstance")) {
        if (signature.equals("(Ljava/lang/Class;I)Ljava/lang/Object;")) {

            Label beginScopeLabel = new Label();
            Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            // stack: ... class count
            int countIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, countIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... class count
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj
            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... newobj newobj count
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj count newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        } else if (signature.equals("(Ljava/lang/Class;[I)Ljava/lang/Object;")) {
            Label beginScopeLabel = new Label();
            Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
            // stack: ... class dimsArray
            pushProductOfIntArrayOnStack();
            // -> stack: ... class dimsArray product
            int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, productIndex);
            // -> stack: ... class dimsArray

            super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
            // -> stack: ... class dimsArray
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj

            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, productIndex);
            // -> stack: ... newobj newobj product
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj product newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        }
    }

    if (opcode == Opcodes.INVOKEVIRTUAL) {
        if ("clone".equals(name) && owner.startsWith("[")) {
            super.visitMethodInsn(opcode, owner, name, signature, itf);

            int i = 0;
            while (i < owner.length()) {
                if (owner.charAt(i) != '[') {
                    break;
                }
                i++;
            }
            if (i > 1) {
                // -> stack: ... newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... arrayref
                calculateArrayLengthAndDispatch(owner.substring(i), i);
            } else {
                // -> stack: ... newobj
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... newobj newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... newobj arrayref
                super.visitInsn(Opcodes.ARRAYLENGTH);
                // -> stack: ... newobj length
                super.visitInsn(Opcodes.SWAP);
                // -> stack: ... length newobj
                invokeRecordAllocation(owner.substring(i));
            }
            return;
        }
    }

    if (opcode == Opcodes.INVOKESPECIAL) {
        if (!"clone".equals(name) || !"java/lang/Object".equals(owner)) {
            if ("<init>".equals(name) && outstandingAllocs > 0) {
                // Tricky because superclass initializers mean there can be more calls
                // to <init> than calls to NEW; hence outstandingAllocs.
                --outstandingAllocs;

                // Most of the time (i.e. in bytecode generated by javac) it is the case
                // that following an <init> call the top of the stack has a reference ot
                // the newly-initialized object.  But nothing in the JVM Spec requires
                // this, so we need to play games with the stack to make an explicit
                // extra copy (and then discard it).

                dupStackElementBeforeSignatureArgs(signature);
                super.visitMethodInsn(opcode, owner, name, signature, itf);
                super.visitLdcInsn(-1);
                super.visitInsn(Opcodes.SWAP);
                invokeRecordAllocation(owner);
                super.visitInsn(Opcodes.POP);
                return;
            }
        }
    }

    super.visitMethodInsn(opcode, owner, name, signature, itf);

}

From source file:com.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(String typeName, int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function./*from ww  w  .  ja v a2 s.  c o m*/

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.RETURN:
        break;// ww  w . j a  v a 2 s  .c om
    case Opcodes.ACONST_NULL:
        push(InferredType.NULL);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(InferredType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(InferredType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.IALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        pop(2);
        push(InferredType.INT);
        break;
    case Opcodes.LALOAD:
    case Opcodes.D2L:
        pop(2);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.L2D:
        pop(2);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.AALOAD:
        InferredType arrayType = pop(2);
        InferredType elementType = arrayType.getElementTypeIfArrayOrThrow();
        push(elementType);
        break;
    case Opcodes.IASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.FASTORE:
    case Opcodes.AASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    case Opcodes.POP:
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    case Opcodes.POP2:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
        pop(2);
        break;
    case Opcodes.DUP:
        push(top());
        break;
    case Opcodes.DUP_X1: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP_X2: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2: {
        InferredType top = pop();
        InferredType next = pop();
        push(next);
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X1: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(next);
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X2: {
        InferredType t1 = pop();
        InferredType t2 = pop();
        InferredType t3 = pop();
        InferredType t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    }
    case Opcodes.SWAP: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        break;
    }
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.D2I:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
        pop(2);
        push(InferredType.INT);
        break;

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
        pop(4);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;

    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2L:
    case Opcodes.F2L:
        pop();
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2F:
        pop();
        push(InferredType.FLOAT);
        break;

    case Opcodes.LCMP:
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
        pop(4);
        push(InferredType.INT);
        break;

    case Opcodes.I2D:
    case Opcodes.F2D:
        pop();
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.F2I:
    case Opcodes.ARRAYLENGTH:
        pop();
        push(InferredType.INT);
        break;
    case Opcodes.FALOAD:
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.L2F:
    case Opcodes.D2F:
        pop(2);
        push(InferredType.FLOAT);
        break;

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        pop(4);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitInsn(opcode);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void pushProductOfIntArrayOnStack() {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();

    final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
    final int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final Label loopLabel = new Label();
    final Label endLabel = new Label();

    super.visitLabel(beginScopeLabel);

    // stack: ... intArray
    super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
    // -> stack: ...

    // counter = 0
    super.visitInsn(Opcodes.ICONST_0);
    super.visitVarInsn(Opcodes.ISTORE, counterIndex);
    // product = 1
    super.visitInsn(Opcodes.ICONST_1);
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // loop:// w ww.  j  ava2  s. co  m
    super.visitLabel(loopLabel);
    // if index >= arraylength goto end:
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitInsn(Opcodes.ARRAYLENGTH);
    super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
    // product = product * max(array[counter],1)
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitInsn(Opcodes.IALOAD);
    super.visitInsn(Opcodes.DUP);
    final Label nonZeroDimension = new Label();
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    super.visitInsn(Opcodes.POP);
    super.visitInsn(Opcodes.ICONST_1);
    super.visitLabel(nonZeroDimension);
    super.visitVarInsn(Opcodes.ILOAD, productIndex);
    super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // iinc counter 1
    super.visitIincInsn(counterIndex, 1);
    // goto loop
    super.visitJumpInsn(Opcodes.GOTO, loopLabel);
    // end:
    super.visitLabel(endLabel);
    // re-push dimensions array
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    // push product
    super.visitVarInsn(Opcodes.ILOAD, productIndex);

    super.visitLabel(endScopeLabel);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

/**
 * Reflection-based allocation (@see java.lang.reflect.Array#newInstance) is
 * triggered with a static method call (INVOKESTATIC), so we hook it here.
 * Class initialization is triggered with a constructor call (INVOKESPECIAL)
 * so we hook that here too as a proxy for the new bytecode which leaves an
 * uninitialized object on the stack that we're not allowed to touch.
 * {@link java.lang.Object#clone} is also a call to INVOKESPECIAL,
 * and is hooked here.  {@link java.lang.Class#newInstance} and
 * {@link java.lang.reflect.Constructor#newInstance} are both
 * INVOKEVIRTUAL calls, so they are hooked here, as well.
 *///  w  ww  .  j a  v  a2 s  .c o m
@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String signature,
        final boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC &&
    // Array does its own native allocation.  Grr.
            owner.equals("java/lang/reflect/Array") && name.equals("newInstance")) {
        if (signature.equals("(Ljava/lang/Class;I)Ljava/lang/Object;")) {

            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            // stack: ... class count
            final int countIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, countIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... class count
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj
            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... newobj newobj count
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj count newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        } else if (signature.equals("(Ljava/lang/Class;[I)Ljava/lang/Object;")) {
            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
            // stack: ... class dimsArray
            pushProductOfIntArrayOnStack();
            // -> stack: ... class dimsArray product
            final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, productIndex);
            // -> stack: ... class dimsArray

            super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
            // -> stack: ... class dimsArray
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj

            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, productIndex);
            // -> stack: ... newobj newobj product
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj product newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        }
    }

    if (opcode == Opcodes.INVOKEVIRTUAL) {
        if ("clone".equals(name) && owner.startsWith("[")) {
            super.visitMethodInsn(opcode, owner, name, signature, itf);

            int i = 0;
            while (i < owner.length()) {
                if (owner.charAt(i) != '[') {
                    break;
                }
                i++;
            }
            if (i > 1) {
                // -> stack: ... newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... arrayref
                calculateArrayLengthAndDispatch(owner.substring(i), i);
            } else {
                // -> stack: ... newobj
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... newobj newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... newobj arrayref
                super.visitInsn(Opcodes.ARRAYLENGTH);
                // -> stack: ... newobj length
                super.visitInsn(Opcodes.SWAP);
                // -> stack: ... length newobj
                invokeRecordAllocation(owner.substring(i));
            }
            return;
        } else if ("newInstance".equals(name)) {
            if ("java/lang/Class".equals(owner) && "()Ljava/lang/Object;".equals(signature)) {
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... Class Class
                super.visitMethodInsn(opcode, owner, name, signature, itf);
                // -> stack: ... Class newobj
                super.visitInsn(Opcodes.DUP_X1);
                // -> stack: ... newobj Class newobj
                super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, CLASS_RECORDER_SIG,
                        false);
                // -> stack: ... newobj
                return;
            } else if ("java/lang/reflect/Constructor".equals(owner)
                    && "([Ljava/lang/Object;)Ljava/lang/Object;".equals(signature)) {
                buildRecorderFromObject(opcode, owner, name, signature, itf);
                return;
            }
        }
    }

    if (opcode == Opcodes.INVOKESPECIAL) {
        if ("clone".equals(name) && "java/lang/Object".equals(owner)) {
            buildRecorderFromObject(opcode, owner, name, signature, itf);
            return;
        } else if ("<init>".equals(name) && outstandingAllocs > 0) {
            // Tricky because superclass initializers mean there can be more calls
            // to <init> than calls to NEW; hence outstandingAllocs.
            --outstandingAllocs;

            // Most of the time (i.e. in bytecode generated by javac) it is the case
            // that following an <init> call the top of the stack has a reference ot
            // the newly-initialized object.  But nothing in the JVM Spec requires
            // this, so we need to play games with the stack to make an explicit
            // extra copy (and then discard it).

            dupStackElementBeforeSignatureArgs(signature);
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            super.visitLdcInsn(-1);
            super.visitInsn(Opcodes.SWAP);
            invokeRecordAllocation(owner);
            super.visitInsn(Opcodes.POP);
            return;
        }
    }

    super.visitMethodInsn(opcode, owner, name, signature, itf);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(final String typeName, final int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function.//from w ww.j ava2s  .c  om

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    final Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        final Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.ACONST_NULL:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Load(lineNumber, new Constant(null, JavaType.OBJECT)));
            }/* w w  w  . j  a  v  a 2 s .c o  m*/
        });
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        loadConstant(opcode - Opcodes.ICONST_M1 - 1, JavaType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        loadConstant(opcode - Opcodes.LCONST_0, JavaType.LONG);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        loadConstant(opcode - Opcodes.FCONST_0, JavaType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        loadConstant(opcode - Opcodes.DCONST_0, JavaType.DOUBLE);
        break;
    case Opcodes.IALOAD:
        recordArrayLoad(JavaType.INT);
        break;
    case Opcodes.LALOAD:
        recordArrayLoad(JavaType.LONG);
        break;
    case Opcodes.FALOAD:
        recordArrayLoad(JavaType.FLOAT);
        break;
    case Opcodes.DALOAD:
        recordArrayLoad(JavaType.DOUBLE);
        break;
    case Opcodes.AALOAD:
        recordArrayLoad(JavaType.OBJECT);
        break;
    case Opcodes.BALOAD:
        recordArrayLoad(JavaType.BYTE);
        break;
    case Opcodes.CALOAD:
        recordArrayLoad(JavaType.CHAR);
        break;
    case Opcodes.SALOAD:
        recordArrayLoad(JavaType.SHORT);
        break;

    case Opcodes.IASTORE:
        recordArrayStore(JavaType.INT);
        break;
    case Opcodes.LASTORE:
        recordArrayStore(JavaType.LONG);
        break;
    case Opcodes.FASTORE:
        recordArrayStore(JavaType.FLOAT);
        break;
    case Opcodes.DASTORE:
        recordArrayStore(JavaType.DOUBLE);
        break;
    case Opcodes.AASTORE:
        recordArrayStore(JavaType.OBJECT);
        break;
    case Opcodes.BASTORE:
        recordArrayStore(JavaType.BYTE);
        break;
    case Opcodes.CASTORE:
        recordArrayStore(JavaType.CHAR);
        break;
    case Opcodes.SASTORE:
        recordArrayStore(JavaType.SHORT);
        break;
    case Opcodes.POP:
    case Opcodes.POP2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Pop(lineNumber, opcode - Opcodes.POP + 1));
            }
        });
        break;
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
        recorder.add(new Runnable() {
            public void run() {
                int offset = opcode - Opcodes.DUP;
                block.addOp(new Duplicate(lineNumber, offset));
            }
        });
        break;
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Duplicate2(lineNumber, opcode - Opcodes.DUP2));
            }
        });
        break;
    case Opcodes.SWAP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Swap(lineNumber));
            }
        });
        break;
    case Opcodes.IRETURN:
        _return(JavaType.INT);
        break;
    case Opcodes.FRETURN:
        _return(JavaType.FLOAT);
        break;
    case Opcodes.ARETURN:
        _return(JavaType.OBJECT);
        break;
    case Opcodes.LRETURN:
        _return(JavaType.LONG);
        break;
    case Opcodes.DRETURN:
        _return(JavaType.DOUBLE);
        break;
    case Opcodes.ATHROW:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Throw(lineNumber));
            }
        });
        break;
    case Opcodes.RETURN:
        _return(JavaType.VOID);
        break;
    case Opcodes.LCMP:
        operation("cmp", JavaType.LONG, JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.FCMPL:
        operation("cmpl", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.FCMPG:
        operation("cmpg", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.DCMPL:
        operation("cmpl", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.DCMPG:
        operation("cmpg", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.LSHL:
        operation("shl", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LSHR:
        operation("shr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LUSHR:
        operation("ushr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LADD:
        operation("add", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LSUB:
        operation("sub", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LDIV:
        operation("div", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LREM:
        operation("rem", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LAND:
        operation("and", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LOR:
        operation("or", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LXOR:
        operation("xor", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LMUL:
        operation("mul", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.FADD:
        operation("add", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FSUB:
        operation("sub", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FMUL:
        operation("mul", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FREM:
        operation("rem", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FDIV:
        operation("div", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.ISHL:
        operation("shl", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISHR:
        operation("shr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IUSHR:
        operation("ushr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IADD:
        operation("add", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISUB:
        operation("sub", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IMUL:
        operation("mul", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IDIV:
        operation("div", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IREM:
        operation("rem", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IAND:
        operation("and", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IOR:
        operation("or", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IXOR:
        operation("xor", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.DSUB:
        operation("sub", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DADD:
        operation("add", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DMUL:
        operation("mul", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DDIV:
        operation("div", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DREM:
        operation("rem", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.L2I:
        convert(JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.L2F:
        convert(JavaType.LONG, JavaType.FLOAT);
        break;
    case Opcodes.L2D:
        convert(JavaType.LONG, JavaType.DOUBLE);
        break;
    case Opcodes.LNEG:
        operation("neg", JavaType.LONG, null, JavaType.LONG);
        break;
    case Opcodes.F2I:
        convert(JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.F2L:
        convert(JavaType.FLOAT, JavaType.LONG);
        break;
    case Opcodes.FNEG:
        operation("neg", JavaType.FLOAT, null, JavaType.FLOAT);
        break;
    case Opcodes.F2D:
        convert(JavaType.FLOAT, JavaType.DOUBLE);
        break;
    case Opcodes.D2I:
        convert(JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.D2L:
        convert(JavaType.DOUBLE, JavaType.LONG);
        break;
    case Opcodes.D2F:
        convert(JavaType.DOUBLE, JavaType.FLOAT);
        break;
    case Opcodes.DNEG:
        operation("neg", JavaType.DOUBLE, null, JavaType.DOUBLE);
        break;
    case Opcodes.I2L:
        convert(JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.I2F:
        convert(JavaType.INT, JavaType.FLOAT);
        break;
    case Opcodes.I2D:
        convert(JavaType.INT, JavaType.DOUBLE);
        break;
    case Opcodes.I2B:
        convert(JavaType.INT, JavaType.BYTE);
        break;
    case Opcodes.I2C:
        convert(JavaType.INT, JavaType.CHAR);
        break;
    case Opcodes.I2S:
        convert(JavaType.INT, JavaType.SHORT);
        break;
    case Opcodes.INEG:
        operation("neg", JavaType.INT, null, JavaType.INT);
        break;
    case Opcodes.ARRAYLENGTH:
        operation("arraylength", JavaType.OBJECT.toArray(), null, JavaType.INT);
        break;
    case Opcodes.MONITORENTER:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorEnter(lineNumber));
            }
        });
        break;
    case Opcodes.MONITOREXIT:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorExit(lineNumber));
            }
        });
        break;
    case Opcodes.NOP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "NOP", null, null, null));
            }
        });
    }
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;//from   ww  w.j  a  va  2  s.  co m

    case Opcodes.ACONST_NULL: {
        Operand op = new Operand();
        op.setNull(true);
        push(op);
    }
        break;

    case Opcodes.ICONST_M1: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(-1));
        push(op);
    }
        break;

    case Opcodes.ICONST_0: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.ICONST_1: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.ICONST_2: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(2));
        push(op);
    }
        break;

    case Opcodes.ICONST_3: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(3));
        push(op);
    }
        break;

    case Opcodes.ICONST_4: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(4));
        push(op);
    }
        break;

    case Opcodes.ICONST_5: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(5));
        push(op);
    }
        break;

    case Opcodes.LCONST_0: {
        Operand op = new Operand();
        op.setConstant(Long.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.LCONST_1: {
        Operand op = new Operand();
        op.setConstant(Long.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.FCONST_0: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.FCONST_1: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.FCONST_2: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(2));
        push(op);
    }
        break;

    case Opcodes.DCONST_0: {
        Operand op = new Operand();
        op.setConstant(Double.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.DCONST_1: {
        Operand op = new Operand();
        op.setConstant(Double.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.IALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.AALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("Ljava/lang/Object;");
        push(op);
    }
        break;

    case Opcodes.BALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("B");
        push(op);
    }
        break;

    case Opcodes.CALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("C");
        push(op);
    }
        break;

    case Opcodes.SALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("S");
        push(op);
    }
        break;

    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
        if (stack.size() < 2) {
            stack.clear();
        } else {
            Operand value = stack.remove(stack.size() - 1);
            Operand reg = stack.remove(stack.size() - 1);
            registers.put(Integer.valueOf(reg.getRegister()), value);
        }
        break;

    case Opcodes.POP:
        pop();
        break;

    case Opcodes.POP2:
        pop2();
        break;

    case Opcodes.DUP:
        if (!stack.isEmpty()) {
            Operand op = stack.get(stack.size() - 1);
            push(op);
        }
        break;

    case Opcodes.DUP_X1:
        if (stack.size() >= 2) {
            Operand op = stack.get(stack.size() - 1);
            stack.add(stack.size() - 2, op);
        }
        break;

    case Opcodes.DUP_X2:
        if (stack.size() >= 2) {
            Operand op = stack.get(stack.size() - 2);
            String sig = op.getSignature();
            op = stack.get(stack.size() - 1);
            if ("J".equals(sig) || "D".equals(sig)) {
                stack.add(stack.size() - 2, op);
            } else if (stack.size() >= 3) {
                stack.add(stack.size() - 3, op);
            }
        }
        break;

    case Opcodes.DUP2:
        if (stack.size() >= 2) {
            stack.add(stack.get(stack.size() - 2));
            stack.add(stack.get(stack.size() - 2));
        }
        break;

    case Opcodes.DUP2_X1:
        if (stack.size() >= 1) {
            Operand op = stack.get(stack.size() - 1);
            String sig = op.getSignature();
            if ("J".equals(sig) || "D".equals(sig)) {
                if (stack.size() >= 3) {
                    stack.add(stack.size() - 3, op);
                    op = stack.get(stack.size() - 2);
                    stack.add(stack.size() - 4, op);
                }
            } else {
                if (stack.size() >= 2) {
                    stack.add(stack.size() - 2, op);
                }
            }
        }
        break;

    case Opcodes.DUP2_X2:
        if (stack.size() >= 1) {
            Operand op = stack.get(stack.size() - 1);
            String sig = op.getSignature();
            if ("J".equals(sig) || "D".equals(sig)) {
                if (stack.size() >= 2) {
                    op = stack.get(stack.size() - 2);
                    sig = op.getSignature();
                    if ("J".equals(sig) || "D".equals(sig)) {
                        op = stack.get(stack.size() - 1);
                        stack.add(stack.size() - 2, op);
                    } else {
                        if (stack.size() >= 3) {
                            op = stack.get(stack.size() - 1);
                            stack.add(stack.size() - 3, op);
                        }
                    }
                }
            } else {
                if (stack.size() >= 3) {
                    op = stack.get(stack.size() - 3);
                    sig = op.getSignature();
                    if ("J".equals(sig) || "D".equals(sig)) {
                        op = stack.get(stack.size() - 2);
                        stack.add(stack.size() - 3, op);
                        op = stack.get(stack.size() - 1);
                        stack.add(stack.size() - 3, op);
                    } else {
                        if (stack.size() >= 4) {
                            op = stack.get(stack.size() - 2);
                            stack.add(stack.size() - 4, op);
                            op = stack.get(stack.size() - 1);
                            stack.add(stack.size() - 4, op);
                        }
                    }
                }
            }
        }
        break;

    case Opcodes.SWAP:
        if (stack.size() >= 2) {
            Operand op = stack.remove(stack.size() - 1);
            stack.add(stack.size() - 1, op);
        }
        break;

    case Opcodes.IADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.ISUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.INEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.ISHL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSHL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.ISHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IUSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LUSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IAND: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LAND: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IXOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LXOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.I2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Integer) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.I2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Integer) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.I2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Integer) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.L2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Long) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.L2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Long) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.L2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Long) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.F2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Float) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.F2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Float) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.F2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Float) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.D2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Double) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.D2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Double) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.D2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Double) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.I2B: {
        Operand bop = new Operand();
        bop.setSignature("B");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                bop.setConstant(Byte.valueOf(((Integer) o).byteValue()));
            }
        }
        push(bop);
    }
        break;

    case Opcodes.I2C: {
        Operand cop = new Operand();
        cop.setSignature("C");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                cop.setConstant(Character.valueOf((char) ((Integer) o).intValue()));
            }
        }
        push(cop);
    }
        break;

    case Opcodes.I2S: {
        Operand sop = new Operand();
        sop.setSignature("S");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                sop.setConstant(Short.valueOf((short) ((Integer) o).intValue()));
            }
        }
        push(sop);
    }
        break;

    case Opcodes.LCMP:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
    case Opcodes.DCMPL:
    case Opcodes.DCMPG: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
        pop();
        break;

    case Opcodes.RETURN:
        //nop
        break;

    case Opcodes.ARRAYLENGTH: {
        pop();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    }
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * For each element in an object array, performs an action.
 * @param counterVar parameter used to keep track of count in loop
 * @param arrayLenVar parameter used to keep track of array length
 * @param array object array instruction list -- must leave an array on top of the stack
 * @param action action to perform on each element -- element will be at top of stack and must be consumed by these instructions
 * @return instructions instruction list to perform some action if two ints are equal
 * @throws NullPointerException if any argument is {@code null}
 *//*from w  w  w.  j a  v a  2s .c o m*/
public static InsnList forEach(Variable counterVar, Variable arrayLenVar, InsnList array, InsnList action) {
    Validate.notNull(counterVar);
    Validate.notNull(arrayLenVar);
    Validate.notNull(array);
    Validate.notNull(action);
    Validate.isTrue(counterVar.getType().equals(Type.INT_TYPE));
    Validate.isTrue(arrayLenVar.getType().equals(Type.INT_TYPE));

    InsnList ret = new InsnList();

    LabelNode doneLabelNode = new LabelNode();
    LabelNode loopLabelNode = new LabelNode();

    // put zero in to counterVar
    ret.add(new LdcInsnNode(0)); // int
    ret.add(new VarInsnNode(Opcodes.ISTORE, counterVar.getIndex())); //

    // load array we'll be traversing over
    ret.add(array); // object[]

    // put array length in to arrayLenVar
    ret.add(new InsnNode(Opcodes.DUP)); // object[], object[]
    ret.add(new InsnNode(Opcodes.ARRAYLENGTH)); // object[], int
    ret.add(new VarInsnNode(Opcodes.ISTORE, arrayLenVar.getIndex())); // object[]

    // loopLabelNode: test if counterVar == arrayLenVar, if it does then jump to doneLabelNode
    ret.add(loopLabelNode);
    ret.add(new VarInsnNode(Opcodes.ILOAD, counterVar.getIndex())); // object[], int
    ret.add(new VarInsnNode(Opcodes.ILOAD, arrayLenVar.getIndex())); // object[], int, int
    ret.add(new JumpInsnNode(Opcodes.IF_ICMPEQ, doneLabelNode)); // object[]

    // load object from object[]
    ret.add(new InsnNode(Opcodes.DUP)); // object[], object[]
    ret.add(new VarInsnNode(Opcodes.ILOAD, counterVar.getIndex())); // object[], object[], int
    ret.add(new InsnNode(Opcodes.AALOAD)); // object[], object

    // call action
    ret.add(action); // object[]

    // increment counter var and goto loopLabelNode
    ret.add(new IincInsnNode(counterVar.getIndex(), 1)); // object[]
    ret.add(new JumpInsnNode(Opcodes.GOTO, loopLabelNode)); // object[]

    // doneLabelNode: pop object[] off of stack
    ret.add(doneLabelNode);
    ret.add(new InsnNode(Opcodes.POP)); //

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Gets the size of an array and puts it on to the stack.
 * @param arrayRef instructions to get the array -- must leave an array on the stack
 * @return instructions to get the size of the array -- will size an int on the stack
 * @throws NullPointerException if any argument is {@code null}
 *//*  w w  w.  jav a 2s.c om*/
public static InsnList loadArrayLength(InsnList arrayRef) {
    Validate.notNull(arrayRef);

    InsnList ret = new InsnList();
    ret.add(arrayRef);
    ret.add(new InsnNode(Opcodes.ARRAYLENGTH));

    return ret;
}