Example usage for org.objectweb.asm Opcodes POP

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

Introduction

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

Prototype

int POP

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

Click Source Link

Usage

From source file:com.sun.fortress.compiler.OverloadSet.java

License:Open Source License

private void joinStackNoUnion(MethodVisitor mv, Label lookahead) {
    Label try2 = new Label();
    Label next = new Label();
    Label cleanup = new Label();

    mv.visitInsn(Opcodes.DUP2); //#2 <: #1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME,
            Naming.RTTI_SUBTYPE_METHOD_SIG);
    mv.visitJumpInsn(Opcodes.IFEQ, try2); //if no, try opposite
    mv.visitInsn(Opcodes.POP); // want #1 (lower on the stack)
    mv.visitJumpInsn(Opcodes.GOTO, next); //done
    mv.visitLabel(try2);
    mv.visitInsn(Opcodes.SWAP);/*from  w  w w.java2  s .co m*/
    mv.visitInsn(Opcodes.DUP2); // #1 <: #2
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME,
            Naming.RTTI_SUBTYPE_METHOD_SIG);
    mv.visitJumpInsn(Opcodes.IFEQ, cleanup); //if no, fail dispatch
    mv.visitInsn(Opcodes.POP); // want #2 (now lower on the stack)
    mv.visitJumpInsn(Opcodes.GOTO, next); //done
    mv.visitLabel(cleanup);
    mv.visitInsn(Opcodes.POP2);
    mv.visitJumpInsn(Opcodes.GOTO, lookahead);
    mv.visitLabel(next);
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression list(Location loc, final List<BytecodeExpression> args) {
    List<TypeWidget> types = Lists.newArrayList();
    for (BytecodeExpression e : args) {
        types.add(e.getType());//w ww  . j  a  v a2  s  .  c o m
    }
    final TypeWidget unified = unify(types).boxed();
    final ListTypeWidget out = new ListTypeWidget(NotNullableTypeWidget.create(unified));
    return new BaseTypeExpression(out) {
        @Override
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            code.exec(out.construct(constant(args.size())));
            for (BytecodeExpression expr : args) {
                Label skip = new Label();
                mv.visitInsn(Opcodes.DUP);
                code.exec(expr);
                final TypeWidget type = expr.getType();
                boolean nullable = code.cast(unified, type, skip);
                mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Collection.class), "add",
                        Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)), true);
                if (nullable) {
                    // we're either going to POP the DUPed List OR the result of add
                    mv.visitLabel(skip);
                }
                mv.visitInsn(Opcodes.POP);
            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression fallback(Location loc, final BytecodeExpression primary,
        final BytecodeExpression caught) {
    TypeWidget unified = unify(primary.getType(), caught.getType());
    return new BaseTypeExpression(unified) {
        @Override/*from ww w.ja  va 2  s. co m*/
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            final Label start = new Label();
            final Label endCatch = new Label();
            final Label handler = new Label();
            Label done = new Label();
            // this probably should not be catching throwable and instead should be catching Exception
            // or permit certain Errors through only
            mv.visitTryCatchBlock(start, endCatch, handler, "java/lang/Throwable");
            mv.visitLabel(start);
            code.exec(primary);
            Label isNull = new Label();
            boolean maybeNull = code.cast(getType(), primary.getType(), isNull);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(endCatch);
            mv.visitLabel(handler);
            mv.visitInsn(Opcodes.POP);
            if (maybeNull) {
                mv.visitLabel(isNull);
            }
            code.exec(caught);
            code.cast(getType(), caught.getType());
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void pop(TypeWidget typeWidget) {
    switch (typeWidget.getJVMType().getSize()) {
    case 0:/*from  ww  w  .ja  v a 2 s .  c  o  m*/
        return;
    case 1:
        getMethodVisitor().visitInsn(Opcodes.POP);
        return;
    case 2:
        getMethodVisitor().visitInsn(Opcodes.POP2);
        return;
    default:
        throw new UnsupportedOperationException("Unexpected JVM type width: " + typeWidget.getJVMType());
    }
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void swap(TypeWidget top, TypeWidget previous) {
    MethodVisitor mv = getMethodVisitor();
    switch (top.getJVMType().getSize()) {
    case 1:/*  w w  w .ja  v a  2  s .com*/
        switch (previous.getJVMType().getSize()) {
        case 1:
            // 1, 1
            mv.visitInsn(Opcodes.SWAP);
            return;
        case 2:
            // 2, 1
            // {value3, value2}, value1  value1, {value3, value2}, value1
            mv.visitInsn(Opcodes.DUP_X2);
            // value1, {value3, value2}
            mv.visitInsn(Opcodes.POP);
            return;
        }
        break;
    case 2:
        switch (previous.getJVMType().getSize()) {
        case 1:
            // 1, 2
            // value3, {value2, value1}  {value2, value1}, value3, {value2, value1}
            mv.visitInsn(Opcodes.DUP2_X1);
            // {value2, value1}, value3
            mv.visitInsn(Opcodes.POP2);
            return;
        case 2:
            // {value4, value3}, {value2, value1}  {value2, value1}, {value4, value3}, {value2, value1}
            mv.visitInsn(Opcodes.DUP2_X2);
            // {value2, value1}, {value4, value3}
            mv.visitInsn(Opcodes.POP2);
            return;
        }
        break;
    }
    throw new UnsupportedOperationException();
}

From source file:cuchaz.enigma.CompiledSourceTypeLoader.java

License:Open Source License

private void removeRedundantClassCalls(ClassNode node) {
    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP/*from  www  .  j  a  v a2s.  c  o m*/
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }
}

From source file:cuchaz.enigma.TranslatingTypeLoader.java

License:Open Source License

private byte[] loadType(String className) {

    // NOTE: don't know if class name is obf or deobf
    ClassEntry classEntry = new ClassEntry(className);
    ClassEntry obfClassEntry = this.obfuscatingTranslator.getTranslatedClass(classEntry);

    // is this an inner class referenced directly? (ie trying to load b instead of a$b)
    if (!obfClassEntry.isInnerClass()) {
        List<ClassEntry> classChain = this.jarIndex.getObfClassChain(obfClassEntry);
        if (classChain.size() > 1) {
            System.err.println(String.format("WARNING: no class %s after inner class reconstruction. Try %s",
                    className, obfClassEntry.buildClassEntry(classChain)));
            return null;
        }// www .jav a2 s  .  c om
    }

    // is this a class we should even know about?
    if (!this.jarIndex.containsObfClass(obfClassEntry)) {
        return null;
    }

    // DEBUG
    //System.out.println(String.format("Looking for %s (obf: %s)", classEntry.getName(), obfClassEntry.getName()));

    // find the class in the jar
    ClassNode node = findClassInJar(obfClassEntry);
    if (node == null) {
        // couldn't find it
        return null;
    }

    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }

    ClassWriter writer = new ClassWriter(0);
    transformInto(node, writer);

    // we have a transformed class!
    return writer.toByteArray();
}

From source file:de.fhkoeln.gm.cui.javahardener.CheckNullMethodVisitor.java

License:Open Source License

/**
 * Surrounds the original invoke call with an IFNULL check.
 * If the object is null a default value will be pushed to the stack.
 * //  ww w.j a  v  a 2 s  .  c  o  m
 * This method duplicates the current object reference with DUP before
 * checking it with IFNULL. The original instance will be removed with POP
 * before {@link #pushDefault(Type) the default value will pushed}.
 * 
 * @param opcode
 * @param owner
 * @param name
 * @param desc
 */
private void invokeMethodWithoutArguments(int opcode, String owner, String name, String desc) {
    Label fallback = new Label();
    Label behind = new Label();

    // We surround the original call with an IFNULL check:
    super.visitInsn(Opcodes.DUP); // Duplicate stack pointer of the current object
    super.visitJumpInsn(Opcodes.IFNULL, fallback); // Skip method call if reference is null
    super.visitMethodInsn(opcode, owner, name, desc); // Original method call
    super.visitJumpInsn(Opcodes.GOTO, behind); // Jump over the reference is null path

    // But if not we need add a default value to the stack:
    super.visitLabel(fallback); // Reference is null path
    super.visitInsn(Opcodes.POP); // Pop the dup value from stack
    pushDefault(Type.getReturnType(desc));

    super.visitLabel(behind); // Label to jump of the reference is null path
}

From source file:de.fhkoeln.gm.cui.javahardener.CheckNullMethodVisitor.java

License:Open Source License

/**
 * Surrounds the original invoke call with an IFNULL check.
 * If the object is null a default value will be pushed to the stack.
 * /*w ww .  j  a  va2s .c  o  m*/
 * This method duplicates the current object reference with DUP2 and POP (to
 * remove the argument from the stack) before checking it with IFNULL.
 * The original instance reference and the argument will be removed with
 * POP2 before {@link #pushDefault(Type) the default value will pushed}.
 * 
 * Notice: This works only for 32 bit arguments: Objects, integers, etc.
 * but doesn't work with long or double values.
 * 
 * @param opcode
 * @param owner
 * @param name
 * @param desc
 */
private void invokeMethodWithOneArgument(int opcode, String owner, String name, String desc) {
    // Instead of the original invoke call:
    //super.visitMethodInsn(opcode, owner, name, desc);

    Label fallback = new Label();
    Label behind = new Label();

    // We surround the original call with an IFNULL check:
    super.visitInsn(Opcodes.DUP2); // Duplicate stack pointer of the current object AND the argument
    super.visitInsn(Opcodes.POP); // Remove the argument again
    super.visitJumpInsn(Opcodes.IFNULL, fallback); // Skip method call if reference is null
    super.visitMethodInsn(opcode, owner, name, desc); // Original method call
    super.visitJumpInsn(Opcodes.GOTO, behind); // Jump over the reference is null path

    // But if not we need add a default value to the stack:
    super.visitLabel(fallback); // Reference is null path
    super.visitInsn(Opcodes.POP2); // Pop the dup value from stack AND the argument
    pushDefault(Type.getReturnType(desc));

    super.visitLabel(behind); // Label to jump of the reference is null path
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityCollision.java

License:Creative Commons License

private static byte[] transformWorld(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_WORLD_GET_COLLIDING_BB);

    InsnList needle = new InsnList();
    LabelNode ln = new LabelNode();
    needle.add(ln);// ww  w  .ja  v a 2s  .  c o m
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_AABB_EXPAND, false));
    needle.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_GET_ENTITIES_EXCLUDE, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_SAPUTILS_EVENT_BUS));
    injectList.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_COLLIDING_ENTITY_CHECK_EVENT));
    injectList.add(new InsnNode(Opcodes.DUP));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_COLLENTITYCHKEVT_INIT, false));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_EVENT_BUS_POST, false));
    injectList.add(new InsnNode(Opcodes.POP));

    method.instructions.insert(insertPoint, injectList);

    // insert entity-sensitive bounding box method

    needle = new InsnList();
    ln = new LabelNode();
    needle.add(ln);
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 11));
    needle.add(new VarInsnNode(Opcodes.ILOAD, 12));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    needle.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ENTITY_GET_BOUNDING_BOX, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 11));
    injectList.add(new VarInsnNode(Opcodes.ILOAD, 12));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    injectList.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}