Example usage for org.objectweb.asm MethodVisitor visitVarInsn

List of usage examples for org.objectweb.asm MethodVisitor visitVarInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitVarInsn.

Prototype

public void visitVarInsn(final int opcode, final int var) 

Source Link

Document

Visits a local variable instruction.

Usage

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitNewAndDup(MethodVisitor mv, Frame frame, int stackIndex, MethodInsnNode min) {
    int arguments = frame.getStackSize() - stackIndex - 1;
    int neededLocals = 0;
    for (int i = arguments; i >= 1; i--) {
        BasicValue v = (BasicValue) frame.getStack(stackIndex + i);
        mv.visitVarInsn(v.getType().getOpcode(Opcodes.ISTORE), lvarStack + NUM_LOCALS + neededLocals);
        neededLocals += v.getSize();/*from ww  w .jav a 2s .co  m*/
    }
    db.log(LogLevel.DEBUG, "Inserting NEW & DUP for constructor call %s%s with %d arguments (%d locals)",
            min.owner, min.desc, arguments, neededLocals);
    if (additionalLocals < neededLocals) {
        additionalLocals = neededLocals;
    }
    ((NewValue) frame.getStack(stackIndex - 1)).insn.accept(mv);
    ((NewValue) frame.getStack(stackIndex)).insn.accept(mv);
    for (int i = 1; i <= arguments; i++) {
        BasicValue v = (BasicValue) frame.getStack(stackIndex + i);
        neededLocals -= v.getSize();
        mv.visitVarInsn(v.getType().getOpcode(Opcodes.ILOAD), lvarStack + NUM_LOCALS + neededLocals);
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPopMethod(MethodVisitor mv) {
    //        emitVerifyInstrumentation(mv);

    final Label lbl = new Label();
    if (DUAL) {//from   ww  w .  j  a v a 2 s .c  o m
        mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
        mv.visitJumpInsn(Opcodes.IFNULL, lbl);
    }

    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "popMethod", "()V");

    if (DUAL)
        mv.visitLabel(lbl);
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitStoreState(MethodVisitor mv, int idx, FrameInfo fi, int numArgsToPreserve) {
    Frame f = frames[fi.endInstruction];

    if (fi.lBefore != null)
        fi.lBefore.accept(mv);//from  w w  w  . j  a  v a2  s  . c  om

    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    emitConst(mv, idx);
    emitConst(mv, fi.numSlots);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "pushMethod", "(II)V");

    // store operand stack
    for (int i = f.getStackSize(); i-- > 0;) {
        BasicValue v = (BasicValue) f.getStack(i);
        if (!isOmitted(v)) {
            if (!isNullType(v)) {
                int slotIdx = fi.stackSlotIndices[i];
                assert slotIdx >= 0 && slotIdx < fi.numSlots;
                emitStoreValue(mv, v, lvarStack, slotIdx, -1);
            } else {
                db.log(LogLevel.DEBUG, "NULL stack entry: type=%s size=%d", v.getType(), v.getSize());
                mv.visitInsn(Opcodes.POP);
            }
        }
    }

    // store local vars
    for (int i = firstLocal; i < f.getLocals(); i++) {
        BasicValue v = (BasicValue) f.getLocal(i);
        if (!isNullType(v)) {
            mv.visitVarInsn(v.getType().getOpcode(Opcodes.ILOAD), i);
            int slotIdx = fi.localSlotIndices[i];
            assert slotIdx >= 0 && slotIdx < fi.numSlots;
            emitStoreValue(mv, v, lvarStack, slotIdx, i);
        }
    }

    // restore last numArgsToPreserve operands
    for (int i = f.getStackSize() - numArgsToPreserve; i < f.getStackSize(); i++) {
        BasicValue v = (BasicValue) f.getStack(i);
        if (!isOmitted(v)) {
            if (!isNullType(v)) {
                int slotIdx = fi.stackSlotIndices[i];
                assert slotIdx >= 0 && slotIdx < fi.numSlots;
                emitRestoreValue(mv, v, lvarStack, slotIdx, -1);
            } else {
                mv.visitInsn(Opcodes.ACONST_NULL);
            }
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitRestoreState(MethodVisitor mv, int idx, FrameInfo fi, int numArgsPreserved) {
    Frame f = frames[fi.endInstruction];

    // restore local vars
    for (int i = firstLocal; i < f.getLocals(); i++) {
        BasicValue v = (BasicValue) f.getLocal(i);
        if (!isNullType(v)) {
            int slotIdx = fi.localSlotIndices[i];
            assert slotIdx >= 0 && slotIdx < fi.numSlots;
            emitRestoreValue(mv, v, lvarStack, slotIdx, i);
            mv.visitVarInsn(v.getType().getOpcode(Opcodes.ISTORE), i);
        } else if (v != BasicValue.UNINITIALIZED_VALUE) {
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitVarInsn(Opcodes.ASTORE, i);
        }//from  w w  w  .java2 s  .  c  o  m
    }

    // restore operand stack
    for (int i = 0; i < f.getStackSize() - numArgsPreserved; i++) {
        BasicValue v = (BasicValue) f.getStack(i);
        if (!isOmitted(v)) {
            if (!isNullType(v)) {
                int slotIdx = fi.stackSlotIndices[i];
                assert slotIdx >= 0 && slotIdx < fi.numSlots;
                emitRestoreValue(mv, v, lvarStack, slotIdx, -1);
            } else {
                mv.visitInsn(Opcodes.ACONST_NULL);
            }
        }
    }

    if (fi.lAfter != null) {
        fi.lAfter.accept(mv);
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPostRestore(MethodVisitor mv) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "postRestore", "()V");
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPreemptionPoint(MethodVisitor mv, int type) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    switch (type) {
    case 0:/* w w  w.j  ava 2s. c o  m*/
        mv.visitInsn(Opcodes.ICONST_0);
        break;
    case 1:
        mv.visitInsn(Opcodes.ICONST_1);
        break;
    case 2:
        mv.visitInsn(Opcodes.ICONST_2);
        break;
    case 3:
        mv.visitInsn(Opcodes.ICONST_3);
        break;
    default:
        throw new AssertionError("Unsupported type: " + type);
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "preemptionPoint", "(I)V");
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitStoreValue(MethodVisitor mv, BasicValue v, int lvarStack, int idx, int lvar)
        throws InternalError, IndexOutOfBoundsException {
    String desc;/*from w  ww.ja  va  2  s .  c  o  m*/

    switch (v.getType().getSort()) {
    case Type.OBJECT:
    case Type.ARRAY:
        desc = "(Ljava/lang/Object;L" + STACK_NAME + ";I)V";
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        desc = "(IL" + STACK_NAME + ";I)V";
        break;
    case Type.FLOAT:
        desc = "(FL" + STACK_NAME + ";I)V";
        break;
    case Type.LONG:
        desc = "(JL" + STACK_NAME + ";I)V";
        break;
    case Type.DOUBLE:
        desc = "(DL" + STACK_NAME + ";I)V";
        break;
    default:
        throw new InternalError("Unexpected type: " + v.getType());
    }

    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    //        if (v.getType().getSort() == Type.OBJECT || v.getType().getSort() == Type.ARRAY)
    //            println(mv, "STORE " + (lvar >= 0 ? ("VAR " + lvar + ": ") : "OPRND: "));
    emitConst(mv, idx);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, STACK_NAME, "push", desc);
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitRestoreValue(MethodVisitor mv, BasicValue v, int lvarStack, int idx, int lvar) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    emitConst(mv, idx);/*from   ww  w  .j  av  a  2s .  co m*/

    switch (v.getType().getSort()) {
    case Type.OBJECT:
        String internalName = v.getType().getInternalName();
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getObject", "(I)Ljava/lang/Object;");
        if (!internalName.equals("java/lang/Object")) // don't cast to Object ;)
            mv.visitTypeInsn(Opcodes.CHECKCAST, internalName);
        //                println(mv, "RESTORE " + (lvar >= 0 ? ("VAR " + lvar + ": ") : "OPRND: "));
        break;
    case Type.ARRAY:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getObject", "(I)Ljava/lang/Object;");
        mv.visitTypeInsn(Opcodes.CHECKCAST, v.getType().getDescriptor());
        break;
    case Type.BYTE:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2B);
        break;
    case Type.SHORT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2S);
        break;
    case Type.CHAR:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2C);
        break;
    case Type.BOOLEAN:
    case Type.INT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        break;
    case Type.FLOAT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getFloat", "(I)F");
        break;
    case Type.LONG:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getLong", "(I)J");
        break;
    case Type.DOUBLE:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getDouble", "(I)D");
        break;
    default:
        throw new InternalError("Unexpected type: " + v.getType());
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void println(MethodVisitor mv, String prefix, int refVar) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);//from www . j  a  v  a 2 s.c o  m
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" " + prefix);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" var " + refVar + ":");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");

    mv.visitVarInsn(Opcodes.ALOAD, refVar);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}

From source file:com.alibaba.hotswap.processor.constructor.ConstructorVisitor.java

License:Open Source License

private void storeArgs(GeneratorAdapter adapter, MethodVisitor hotswapInit, MethodMeta methodMeta) {
    Type[] argTypes = Type.getArgumentTypes(methodMeta.desc);

    if (argTypes.length == 0) {
        return;/*from   ww w.  j ava  2 s.c om*/
    }

    adapter.loadArg(2);// Object[]

    int nextIndex = 4;
    for (int i = 0; i < argTypes.length; i++) {
        adapter.dup();
        adapter.push(i);
        adapter.arrayLoad(Type.getType(Object.class));// Object[i]
        adapter.unbox(argTypes[i]);
        hotswapInit.visitVarInsn(argTypes[i].getOpcode(Opcodes.ISTORE), nextIndex);
        nextIndex += argTypes[i].getSize();
    }

    adapter.pop();
}