Example usage for org.objectweb.asm Label Label

List of usage examples for org.objectweb.asm Label Label

Introduction

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

Prototype

public Label() 

Source Link

Document

Constructs a new label.

Usage

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

private void pushLocationStack() {
    assert context.isLocationStackNeeded() : context;
    if (context.isInternalLocation()) {
        mv.visitInsn(Opcodes.ACONST_NULL);
        return;/*from  w w  w .  j ava2s  . c  om*/
    }
    int locationStack = context.getLocationStack();
    if (context.isMethodBodyTracked()) {
        assert locationStack >= 0 : context;
        mv.loadLocal(locationStack);
        return;
    }
    if (locationStack < 0) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, TransformerUtil.LOCATION_STACK, "get",
                TransformerUtil.NOARG_RETURNS_STACK, false);
        return;
    }

    Label done = new Label();
    mv.loadLocal(locationStack);
    mv.dup();
    mv.ifNonNull(done);
    mv.pop();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, TransformerUtil.LOCATION_STACK, "get",
            TransformerUtil.NOARG_RETURNS_STACK, false);
    mv.dup();
    mv.storeLocal(locationStack);
    mv.visitLabel(done);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

/**
 * @see com.devexperts.aprof.LocationStack#addInvokedMethod(int)
 *//*from   ww w  .  j  a  va 2s .co  m*/
@Override
protected void visitStartInvokedMethod() {
    assert !context.isInternalLocation() : context;
    startFinally = new Label();
    pushLocationStack();
    mv.push(AProfRegistry.registerLocation(context.getLocation()));
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TransformerUtil.LOCATION_STACK, "addInvokedMethod",
            TransformerUtil.INT_VOID, false);
    mv.visitLabel(startFinally);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

@Override
protected void visitEndInvokedMethod() {
    Label endFinally = new Label();
    mv.visitTryCatchBlock(startFinally, endFinally, endFinally, null);
    mv.visitLabel(endFinally);/* w w  w .j  a va 2s  .  c  om*/
    int var = mv.newLocal(Type.getType(Object.class));
    mv.storeLocal(var);
    visitReturnFromInvokedMethod();
    mv.loadLocal(var);
    mv.throwException();
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

@Override
protected void visitTrackedMethodInsn(int opcode, String owner, String name, String desc, boolean intf) {
    assert !context.isInternalLocation() : context;
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    Label done = new Label();
    visitMarkInvocationPoint();/*from   w  ww  .  j a  v a2s .  c o  m*/
    mv.visitTryCatchBlock(start, end, handler, null);
    mv.visitLabel(start);
    mv.visitMethodInsn(opcode, owner, name, desc, intf);
    mv.visitLabel(end);
    visitUnmarkInvocationPoint();
    mv.goTo(done);
    mv.visitLabel(handler);
    int var = mv.newLocal(Type.getType(Object.class));
    mv.storeLocal(var);
    visitUnmarkInvocationPoint();
    mv.loadLocal(var);
    mv.throwException();
    mv.visitLabel(done);
}

From source file:com.dragome.callbackevictor.serverside.bytecode.transformation.asm.ContinuationMethodAdapter.java

License:Apache License

public void visitCode() {
    mv.visitCode();/*from   w  w  w . j  a  va 2  s .  c o m*/

    int fsize = labels.size();
    Label[] restoreLabels = new Label[fsize];
    for (int i = 0; i < restoreLabels.length; i++) {
        restoreLabels[i] = new Label();
    }

    // verify if restoring
    Label l0 = new Label();

    // PC: StackRecorder stackRecorder = StackRecorder.get();
    mv.visitMethodInsn(INVOKESTATIC, STACK_RECORDER, "get", "()L" + STACK_RECORDER + ";", false);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, stackRecorderVar);
    mv.visitLabel(startLabel);

    // PC: if (stackRecorder != null && !stackRecorder.isRestoring) {  
    mv.visitJumpInsn(IFNULL, l0);
    mv.visitVarInsn(ALOAD, stackRecorderVar);
    mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isRestoring", "Z");
    mv.visitJumpInsn(IFEQ, l0);

    mv.visitVarInsn(ALOAD, stackRecorderVar);
    // PC:    stackRecorder.popInt();
    mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Int", "()I", false);
    mv.visitTableSwitchInsn(0, fsize - 1, l0, restoreLabels);

    // switch cases
    for (int i = 0; i < fsize; i++) {
        Label frameLabel = labels.get(i);
        mv.visitLabel(restoreLabels[i]);

        MethodInsnNode mnode = nodes.get(i);
        Frame frame = analyzer.getFrames()[canalyzer.getIndex(mnode)];

        // for each local variable store the value in locals popping it from the stack!
        // locals
        int lsize = frame.getLocals();
        for (int j = lsize - 1; j >= 0; j--) {
            BasicValue value = (BasicValue) frame.getLocal(j);
            if (isNull(value)) {
                mv.visitInsn(ACONST_NULL);
                mv.visitVarInsn(ASTORE, j);
            } else if (value == BasicValue.UNINITIALIZED_VALUE) {
                // TODO ??
            } else if (value == BasicValue.RETURNADDRESS_VALUE) {
                // TODO ??
            } else {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                Type type = value.getType();
                if (value.isReference()) {
                    mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object",
                            "()Ljava/lang/Object;", false);
                    Type t = value.getType();
                    String desc = t.getDescriptor();
                    if (desc.charAt(0) == '[') {
                        mv.visitTypeInsn(CHECKCAST, desc);
                    } else {
                        mv.visitTypeInsn(CHECKCAST, t.getInternalName());
                    }
                    mv.visitVarInsn(ASTORE, j);

                } else {
                    mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type),
                            "()" + type.getDescriptor(), false);
                    mv.visitVarInsn(type.getOpcode(ISTORE), j);
                }
            }
        }

        if (frame instanceof MonitoringFrame) {
            int[] monitoredLocals = ((MonitoringFrame) frame).getMonitored();
            // System.out.println(System.identityHashCode(frame)+" AMonitored locals "+monitoredLocals.length);
            for (int monitoredLocal : monitoredLocals) {
                // System.out.println(System.identityHashCode(frame)+" AMonitored local "+monitoredLocals[j]);
                mv.visitVarInsn(ALOAD, monitoredLocal);
                mv.visitInsn(MONITORENTER);
            }
        }

        // stack
        int argSize = Type.getArgumentTypes(mnode.desc).length;
        int ownerSize = mnode.getOpcode() == INVOKESTATIC ? 0 : 1; // TODO
        int initSize = mnode.name.equals("<init>") ? 2 : 0;
        int ssize = frame.getStackSize();
        for (int j = 0; j < ssize - argSize - ownerSize - initSize; j++) {
            BasicValue value = (BasicValue) frame.getStack(j);
            if (isNull(value)) {
                mv.visitInsn(ACONST_NULL);
            } else if (value == BasicValue.UNINITIALIZED_VALUE) {
                // TODO ??
            } else if (value == BasicValue.RETURNADDRESS_VALUE) {
                // TODO ??
            } else if (value.isReference()) {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object", "()Ljava/lang/Object;",
                        false);
                mv.visitTypeInsn(CHECKCAST, value.getType().getInternalName());
            } else {
                Type type = value.getType();
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type),
                        "()" + type.getDescriptor(), false);
            }
        }

        boolean hasMethodRef = false;
        if (mnode.getOpcode() != INVOKESTATIC) {
            // Load the object whose method we are calling  
            BasicValue value = ((BasicValue) frame.getStack(ssize - argSize - 1));
            if (isNull(value)) {
                // If user code causes NPE, then we keep this behavior: load null to get NPE at runtime 
                mv.visitInsn(ACONST_NULL);
            } else {
                hasMethodRef = true;

                mv.visitVarInsn(ALOAD, stackRecorderVar);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Reference",
                        "()Ljava/lang/Object;", false);
                mv.visitTypeInsn(CHECKCAST, value.getType().getInternalName());

            }
        }

        // Create null types for the parameters of the method invocation
        // RS:
        if (hasMethodRef && canalyzer._continueReflection && mnode.name.contains("invoke")
                && mnode.owner.contains("java/lang/reflect/Method")) {
            ContinuationMethodAnalyzer.MyVariables vars = canalyzer._reflectMapping.get(mnode);
            mv.visitVarInsn(ALOAD, vars.objectVar());
            mv.visitVarInsn(ALOAD, vars.argsVar());
            //mv.visitVarInsn(ALOAD, 2);
            //mv.visitVarInsn(ALOAD, 4);
        }
        //RS:
        else {
            for (Type paramType : Type.getArgumentTypes(mnode.desc)) {
                pushDefault(paramType);
            }
        }

        // continue to the next method
        mv.visitJumpInsn(GOTO, frameLabel);
    }

    // PC: }
    // end of start block
    mv.visitLabel(l0);
}

From source file:com.dragome.callbackevictor.serverside.bytecode.transformation.asm.ContinuationMethodAdapter.java

License:Apache License

public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    mv.visitMethodInsn(opcode, owner, name, desc, itf);

    if (currentFrame != null) {
        Label fl = new Label();

        mv.visitVarInsn(ALOAD, stackRecorderVar);
        mv.visitJumpInsn(IFNULL, fl);//www .  j a  v a 2s .  co m
        mv.visitVarInsn(ALOAD, stackRecorderVar);
        mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isCapturing", "Z");
        mv.visitJumpInsn(IFEQ, fl);

        // save stack
        Type returnType = Type.getReturnType(desc);
        boolean hasReturn = returnType != Type.VOID_TYPE;
        if (hasReturn) {
            mv.visitInsn(returnType.getSize() == 1 ? POP : POP2);
        }

        Type[] params = Type.getArgumentTypes(desc);
        int argSize = params.length;
        int ownerSize = opcode == INVOKESTATIC ? 0 : 1; // TODO
        int ssize = currentFrame.getStackSize() - argSize - ownerSize;
        for (int i = ssize - 1; i >= 0; i--) {
            BasicValue value = (BasicValue) currentFrame.getStack(i);
            if (isNull(value)) {
                mv.visitInsn(POP);
            } else if (value == BasicValue.UNINITIALIZED_VALUE) {
                // TODO ??
            } else if (value.isReference()) {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                mv.visitInsn(SWAP);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object",
                        "(Ljava/lang/Object;)V", false);
            } else {
                Type type = value.getType();
                if (type.getSize() > 1) {
                    mv.visitInsn(ACONST_NULL); // dummy stack entry
                    mv.visitVarInsn(ALOAD, stackRecorderVar);
                    mv.visitInsn(DUP2_X2); // swap2 for long/double
                    mv.visitInsn(POP2);
                    mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type),
                            "(" + type.getDescriptor() + ")V", false);
                    mv.visitInsn(POP); // remove dummy stack entry
                } else {
                    mv.visitVarInsn(ALOAD, stackRecorderVar);
                    mv.visitInsn(SWAP);
                    mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type),
                            "(" + type.getDescriptor() + ")V", false);
                }
            }
        }

        if (!isStatic) {
            //RS:
            if (canalyzer._continueReflection && name.equals("invoke")
                    && owner.startsWith("java/lang/reflect/Method")) {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                ContinuationMethodAnalyzer.MyVariables vars = canalyzer._reflectMapping
                        .get(nodes.get(currentIndex));
                //               System.out.println("Class:" + canalyzer.className + "method:" + stackRecorderVar + ":" + name + ":" + owner + ":" + desc + ":" + vars.methodVar());
                mv.visitVarInsn(ALOAD, vars.methodVar());
                //mv.visitVarInsn(ALOAD, 3);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "replace" + "Reference",
                        "(Ljava/lang/Object;)V", false);
            }
            //RS:
            mv.visitVarInsn(ALOAD, stackRecorderVar);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Reference",
                    "(Ljava/lang/Object;)V", false);
        }

        // save locals
        int fsize = currentFrame.getLocals();
        for (int j = 0; j < fsize; j++) {
            BasicValue value = (BasicValue) currentFrame.getLocal(j);
            if (isNull(value)) {
                // no need to save null
            } else if (value == BasicValue.UNINITIALIZED_VALUE) {
                // no need to save uninitialized objects
            } else if (value.isReference()) {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                mv.visitVarInsn(ALOAD, j);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object",
                        "(Ljava/lang/Object;)V", false);
            } else {
                mv.visitVarInsn(ALOAD, stackRecorderVar);
                Type type = value.getType();
                mv.visitVarInsn(type.getOpcode(ILOAD), j);
                mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type),
                        "(" + type.getDescriptor() + ")V", false);
            }
        }

        mv.visitVarInsn(ALOAD, stackRecorderVar);
        if (currentIndex >= 128) {
            // if > 127 then it's a SIPUSH, not a BIPUSH...
            mv.visitIntInsn(SIPUSH, currentIndex);
        } else {
            // TODO optimize to iconst_0...
            mv.visitIntInsn(BIPUSH, currentIndex);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V", false);

        if (currentFrame instanceof MonitoringFrame) {
            int[] monitoredLocals = ((MonitoringFrame) currentFrame).getMonitored();
            // System.out.println(System.identityHashCode(currentFrame)+" Monitored locals "+monitoredLocals.length);
            for (int monitoredLocal : monitoredLocals) {
                // System.out.println(System.identityHashCode(currentFrame)+" Monitored local "+monitoredLocals[j]);
                mv.visitVarInsn(ALOAD, monitoredLocal);
                mv.visitInsn(MONITOREXIT);
            }
        }

        Type methodReturnType = Type.getReturnType(methodDesc);
        pushDefault(methodReturnType);
        mv.visitInsn(methodReturnType.getOpcode(IRETURN));
        mv.visitLabel(fl);

        currentIndex++;
        currentFrame = null;
    }
}

From source file:com.dragome.callbackevictor.serverside.bytecode.transformation.asm.ContinuationMethodAdapter.java

License:Apache License

public void visitMaxs(int maxStack, int maxLocals) {
    Label endLabel = new Label();
    mv.visitLabel(endLabel);//from ww  w.  ja  v a2s .c  o  m

    mv.visitLocalVariable("__stackRecorder", "L" + STACK_RECORDER + ";", null, startLabel, endLabel,
            stackRecorderVar);

    mv.visitMaxs(0, 0);
}

From source file:com.dragome.callbackevictor.serverside.bytecode.transformation.asm.ContinuationMethodAnalyzer.java

License:Apache License

public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    MethodInsnNode mnode = new MethodInsnNode(opcode, owner, name, desc, itf);
    if (opcode == INVOKESPECIAL || "<init>".equals(name)) {
        methods.add(mnode);//from w  w w  .ja va  2 s . co  m
    }
    if (needsFrameGuard(opcode, owner, name, desc) /* && transformer.inScope(owner, name)*/) {
        Label label = new Label();
        super.visitLabel(label);
        LabelNode labelNode = new LabelNode(label);
        instructions.add(labelNode);
        labels.add(label);
        nodes.add(mnode);

        //RS:
        if (_continueReflection && name.equals("invoke") && owner.startsWith("java/lang/reflect/Method")) {
            int mthd = ((Integer) _variables.get(_variables.size() - 3)).intValue();
            int obj = ((Integer) _variables.get(_variables.size() - 2)).intValue();
            int args = ((Integer) _variables.get(_variables.size() - 1)).intValue();
            MyVariables vars = new MyVariables(mthd, obj, args);
            _reflectMapping.put(mnode, vars);
        }
        //RS:
    }
    instructions.add(mnode);
}

From source file:com.dragome.methodlogger.serverside.MethodLoggerAdapter.java

License:Apache License

protected void onMethodEnter() {
    if (!isStatic()) {
        l0 = new Label();
        super.visitLabel(l0);

        super.visitVarInsn(Opcodes.ALOAD, 0);
        super.visitLdcInsn(name);
        super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/dragome/methodlogger/enhancers/MethodInvocationLogger",
                "onMethodEnter", "(Ljava/lang/Object;Ljava/lang/String;)V", false);
    }//from ww w  .  j ava2  s  .co  m
    super.onMethodEnter();
}

From source file:com.facebook.presto.bytecode.instruction.LabelNode.java

License:Apache License

public LabelNode() {
    this.label = new Label();
    this.name = "label@" + label.hashCode();
}