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:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

public static void pop(ClassNode type, MethodVisitor mv) {
    if (type == double_TYPE || type == long_TYPE)
        mv.visitInsn(Opcodes.POP2);//  ww w  .j a  v  a2  s.co  m
    else
        mv.visitInsn(Opcodes.POP);
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**    
 * @param type//w w  w  . j a  va 2s .  c o m
 * @return the POP or POP2 instruction, depending on type.
 */
private static int getPopOpCode(JavaTypeName type) {

    switch (type.getTag()) {
    case JavaTypeName.BOOLEAN_TAG:
    case JavaTypeName.BYTE_TAG:
    case JavaTypeName.SHORT_TAG:
    case JavaTypeName.CHAR_TAG:
    case JavaTypeName.INT_TAG:
    case JavaTypeName.FLOAT_TAG:
    case JavaTypeName.ARRAY_TAG:
    case JavaTypeName.OBJECT_TAG:
        return Opcodes.POP;

    case JavaTypeName.LONG_TAG:
    case JavaTypeName.DOUBLE_TAG:
        return Opcodes.POP2;

    case JavaTypeName.VOID_TAG:
    default: {
        throw new IllegalArgumentException("invalid type: " + type);
    }
    }
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

private static boolean encodeSwitchStatement(JavaStatement.SwitchStatement switchStatement,
        GenerationContext context) throws JavaGenerationException {

    MethodVisitor mv = context.getMethodVisitor();

    // Append the instructions to evaluate the condition, leaving the result on the operand stack.
    JavaExpression condition = switchStatement.getCondition();
    encodeExpr(condition, context);//from   w  w w.j  a  va  2 s  .  c o  m

    // Follow (what seems to be) javac's rule for deciding between a lookup switch and a table switch:
    //      Use a table switch if allowed by maxTableSize, where
    //          maxTableSize = (# cases - 2) * 5  , (# cases) doesn't include the default.
    //
    // eg. if there are 2 case alternatives (+ default): 
    //          maxTableSize = ((2 - 2) * 5) = 0, so always use a lookup switch.
    //     if there are cases {1, 2, 3, 4, 15}:
    //          maxTableSize = ((5 - 2) * 5) = 15.  A table would contain 15 entries {1, 2, .., 15}, so use a table switch.
    //     if there are cases {1, 2, 3, 4, 16}:
    //          A table would contain 16 entries {1, 2, .., 16}, so use a lookup switch.
    List<IntCaseGroup> caseGroups = switchStatement.getCaseGroups();
    int nTotalCases = 0;

    // Calculate the first and last values.
    int firstValue = Integer.MAX_VALUE;
    int lastValue = Integer.MIN_VALUE;
    for (int caseN = 0; caseN < caseGroups.size(); ++caseN) {
        SwitchStatement.IntCaseGroup switchCaseGroup = caseGroups.get(caseN);
        for (int i = 0, nCaseLabels = switchCaseGroup.getNCaseLabels(); i < nCaseLabels; i++) {
            nTotalCases++;
            int caseLabel = switchCaseGroup.getNthCaseLabel(i);
            if (firstValue > caseLabel) {
                firstValue = caseLabel;
            }

            if (lastValue < caseLabel) {
                lastValue = caseLabel;
            }
        }
    }

    if (nTotalCases == 0) {
        // If the switch statement contains no cases, javac's behaviour is to generate a pop instruction
        // to pop the condition value off the operand stack.
        mv.visitInsn(Opcodes.POP);

    } else {

        int nSpannedCases = lastValue - firstValue + 1;

        int maxTableSize = (nTotalCases - 2) * 5;
        boolean useTableSwitch = !(nSpannedCases > maxTableSize);

        // Create the default label and declare the other case labels.
        Label defaultLabel = new Label();

        // (SwitchStatement.IntCaseGroup->Label) A map from case group to label.
        Map<IntCaseGroup, Label> caseGroupToLabelMap = new HashMap<IntCaseGroup, Label>();

        if (!useTableSwitch) {
            // A lookup switch.  Each case match gets its own label.

            // Map from case label to label, sorted by case label.
            SortedMap<Integer, Label> caseLabelToLabelMap = new TreeMap<Integer, Label>();

            // A count of the number of case labels.
            int nCaseLabels = 0;

            // Create the labels, and populate the map.
            for (int caseN = 0; caseN < caseGroups.size(); ++caseN) {
                SwitchStatement.IntCaseGroup switchCaseGroup = caseGroups.get(caseN);

                // The label for this case group.
                Label label = new Label();
                caseGroupToLabelMap.put(switchCaseGroup, label);

                for (int i = 0, nCaseGroupLabels = switchCaseGroup
                        .getNCaseLabels(); i < nCaseGroupLabels; i++) {
                    int caseLabel = switchCaseGroup.getNthCaseLabel(i);
                    caseLabelToLabelMap.put(Integer.valueOf(caseLabel), label);
                    nCaseLabels++;
                }
            }

            // Create the array for case matches and the corresponding labels.
            int[] caseMatches = new int[nCaseLabels];
            Label[] labels = new Label[nCaseLabels];

            int index = 0;
            for (final Map.Entry<Integer, Label> entry : caseLabelToLabelMap.entrySet()) {
                Integer caseLabelInteger = entry.getKey();
                caseMatches[index] = caseLabelInteger.intValue();
                labels[index] = entry.getValue();
                index++;
            }

            // Visit the lookup switch instruction.
            mv.visitLookupSwitchInsn(defaultLabel, caseMatches, labels);

        } else {
            // A table switch 
            // The cases which aren't given should be set to the default case.

            Label[] labels = new Label[nSpannedCases];

            // Initially set all elements to the default label, if they won't all be set to something else later.
            if (nSpannedCases != nTotalCases) {
                Arrays.fill(labels, defaultLabel);
            }

            // For each switch case provided, create a new label.
            for (int caseN = 0; caseN < caseGroups.size(); ++caseN) {
                SwitchStatement.IntCaseGroup switchCaseGroup = caseGroups.get(caseN);

                // The label for this case group.
                Label label = new Label();
                caseGroupToLabelMap.put(switchCaseGroup, label);

                for (int i = 0, nCaseGroupLabels = switchCaseGroup
                        .getNCaseLabels(); i < nCaseGroupLabels; i++) {
                    int caseLabel = switchCaseGroup.getNthCaseLabel(i);
                    int labelArrayIndex = caseLabel - firstValue;

                    labels[labelArrayIndex] = label;
                }
            }

            // Visit the table switch instruction.
            mv.visitTableSwitchInsn(firstValue, lastValue, defaultLabel, labels);
        }

        // Iterate over the cases.       
        for (int caseN = 0; caseN < caseGroups.size(); ++caseN) {
            SwitchStatement.IntCaseGroup switchCase = caseGroups.get(caseN);
            JavaStatement caseStatementGroup = switchCase.getStatement();

            // Add a local scope..
            GenerationContext innerContext = new GenerationContext(context);

            // Get the label to visit..
            Label labelToVisit = caseGroupToLabelMap.get(switchCase);

            //mark the location of the code for the i'th case.
            mv.visitLabel(labelToVisit);

            // get the instructions for the case statement.
            boolean caseStatementIsTerminating = encodeStatement(caseStatementGroup, innerContext);
            context.addJumpReturnLabelInfo(innerContext);

            // We don't (yet) handle non-terminating code in a case block.
            if (!caseStatementIsTerminating) {
                throw new JavaGenerationException("Can't (yet) handle non-terminating code in a case block.");
            }
        }

        JavaStatement defaultStatementGroup = switchStatement.getDefaultStatement();
        //mark the location of the default statements (even if there are none, this means just progress to the next instruction..)
        mv.visitLabel(defaultLabel);
        if (defaultStatementGroup != null) {
            // Add a local scope..
            GenerationContext innerContext = new GenerationContext(context);

            boolean defaultStatementIsTerminating = encodeStatement(defaultStatementGroup, innerContext);
            context.addJumpReturnLabelInfo(innerContext);

            // We don't (yet) handle non-terminating code in a case block.
            if (!defaultStatementIsTerminating) {
                throw new JavaGenerationException("Can't (yet) handle non-terminating code in a case block.");
            }
        }
    }

    // Note: we should add GOTO instructions to each case block to jump to after the switch statement if necessary.
    //   But for now it's not necessary since all cases are terminated.
    boolean isTerminating = true;

    return isTerminating;
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.AODMutator1.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:

    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            mv.visitInsn(Opcodes.POP);
        } else {//from  w w w  .j  a  va  2 s .com
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            mv.visitInsn(Opcodes.POP2);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    default:
        mv.visitInsn(opcode);
        break;
    }
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.AODMutator2.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.INT_TYPE);
            mv.visitVarInsn(Opcodes.ISTORE, storage);
            mv.visitInsn(Opcodes.POP);
            mv.visitVarInsn(Opcodes.ILOAD, storage);
            /*/*from   w ww  .jav  a2 s .c om*/
             * Alternative : mv.visitInsn(Opcodes.SWAP);
             * mv.visitInsn(Opcodes.POP);
             * mv.visitVarInsn(Opcodes.ILOAD,storage);
             */
        } else {
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.FLOAT_TYPE);
            mv.visitVarInsn(Opcodes.FSTORE, storage);
            mv.visitInsn(Opcodes.POP);
            mv.visitVarInsn(Opcodes.FLOAD, storage);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.LONG_TYPE);
            mv.visitVarInsn(Opcodes.LSTORE, storage);
            mv.visitInsn(Opcodes.POP2);
            mv.visitVarInsn(Opcodes.LLOAD, storage);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.DOUBLE_TYPE);
            mv.visitVarInsn(Opcodes.DSTORE, storage);
            mv.visitInsn(Opcodes.POP2);
            mv.visitVarInsn(Opcodes.DLOAD, storage);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    default:
        mv.visitInsn(opcode);
        break;
    }
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.OBBNMutator2.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.IAND:
    case Opcodes.IOR:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            mv.visitInsn(Opcodes.POP);
        } else {/*from   www  .jav  a 2  s.  c  o m*/
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.LAND:
    case Opcodes.LOR:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            mv.visitInsn(Opcodes.POP2);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    default:
        mv.visitInsn(opcode);
        break;
    }
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.OBBNMutator3.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.IAND:
    case Opcodes.IOR:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.INT_TYPE);
            mv.visitVarInsn(Opcodes.ISTORE, storage);
            mv.visitInsn(Opcodes.POP);
            mv.visitVarInsn(Opcodes.ILOAD, storage);
        } else {//from   www. jav  a2  s. c o m
            mv.visitInsn(opcode);
        }
        break;
    case Opcodes.LAND:
    case Opcodes.LOR:
        if (this.shouldMutate(OpcodeToType.typeOfOpcode(opcode))) {
            int storage = this.newLocal(Type.LONG_TYPE);
            mv.visitVarInsn(Opcodes.LSTORE, storage);
            mv.visitInsn(Opcodes.POP2);
            mv.visitVarInsn(Opcodes.LLOAD, storage);
        } else {
            mv.visitInsn(opcode);
        }
        break;
    default:
        mv.visitInsn(opcode);
        break;
    }
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation freturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opcode, final MethodVisitor mv) {
            // Strategy translated from jumble BCEL code
            // The following is complicated by the problem of NaNs. By default
            // the new value is -(x + 1), but this doesn't work for NaNs. But
            // for a NaN x != x is true, and we use this to detect them.
            mv.visitInsn(Opcodes.DUP);//w  ww. j  a v a  2  s. c o m
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.FCMPG);
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, l1);
            mv.visitInsn(Opcodes.POP);
            mv.visitInsn(Opcodes.FCONST_0);
            mv.visitLabel(l1);
            mv.visitInsn(Opcodes.FCONST_1);
            mv.visitInsn(Opcodes.FADD);
            mv.visitInsn(Opcodes.FNEG);
            mv.visitInsn(Opcodes.FRETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "replaced return of float value with -(x + 1) for " + methodInfo.getDescription();
        }

    };
}

From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java

License:Open Source License

@Test
public void test_pop() throws Exception {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    ProgramState programState = execute(new Instruction(Opcodes.POP),
            ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2));
    assertThat(programState.peekValue()).isEqualTo(sv1);
    programState = execute(new Instruction(Opcodes.POP));
    assertEmptyStack(programState);//from  w w  w.  j  a  va2  s  .c  o m
}

From source file:org.zoeey.ztpl.compiler.ByteCodeHelper.java

License:LGPL

/**
 * Writer/*from  www .j  av a 2s.c  o  m*/
 * @param str ?
 */
public void writeStr(String str) {
    mv.visitVarInsn(Opcodes.ALOAD, VAR_INDEX_WRITER);
    mv.visitLdcInsn(str);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/Writer"//
            , "append", "(Ljava/lang/CharSequence;)Ljava/io/Writer;");
    mv.visitInsn(Opcodes.POP);
}