Example usage for org.objectweb.asm Opcodes ICONST_M1

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

Introduction

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

Prototype

int ICONST_M1

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

Click Source Link

Usage

From source file:org.jooby.internal.apitool.BytecodeRouteParser.java

License:Apache License

private Object paramValue(final ClassLoader loader, final ClassNode owner, final MethodNode method,
        final AbstractInsnNode n) {
    if (n instanceof LdcInsnNode) {

        Object cst = ((LdcInsnNode) n).cst;
        if (cst instanceof Type) {
            boolean typePresent = new Insn<>(method, n).next().filter(is(MethodInsnNode.class)).findFirst()
                    .map(MethodInsnNode.class::cast).filter(mutantToSomething().or(getOrCreateKotlinClass()))
                    .isPresent();/*  www . j  av a  2 s .  c o  m*/
            if (typePresent) {
                return null;
            }
            return loadType(loader, ((Type) cst).getClassName());
        }
        return cst;
    } else if (n instanceof InsnNode) {
        InsnNode insn = (InsnNode) n;
        switch (insn.getOpcode()) {
        case Opcodes.ICONST_0:
            return 0;
        case Opcodes.ICONST_1:
            return 1;
        case Opcodes.ICONST_2:
            return 2;
        case Opcodes.ICONST_3:
            return 3;
        case Opcodes.ICONST_4:
            return 4;
        case Opcodes.ICONST_5:
            return 5;
        case Opcodes.LCONST_0:
            return 0L;
        case Opcodes.LCONST_1:
            return 1L;
        case Opcodes.FCONST_0:
            return 0f;
        case Opcodes.FCONST_1:
            return 1f;
        case Opcodes.FCONST_2:
            return 2f;
        case Opcodes.DCONST_0:
            return 0d;
        case Opcodes.DCONST_1:
            return 1d;
        case Opcodes.ICONST_M1:
            return -1;
        case Opcodes.ACONST_NULL:
            return null;
        }
    } else if (n instanceof IntInsnNode) {
        IntInsnNode intinsn = (IntInsnNode) n;
        return intinsn.operand;
    } else if (n instanceof FieldInsnNode) {
        // toEnum
        FieldInsnNode finsn = (FieldInsnNode) n;
        if (finsn.getOpcode() == GETSTATIC) {
            java.lang.reflect.Type possibleEnum = loadType(loader, finsn.owner);
            if (MoreTypes.getRawType(possibleEnum).isEnum()) {
                return finsn.name;
            }
        }
    }
    return n;
}

From source file:org.mbte.groovypp.compiler.asm.LdcImproverMethodAdapter.java

License:Apache License

public void visitLdcInsn(Object cst) {
    if (cst instanceof Integer) {
        Integer value = (Integer) cst;
        switch (value) {
        case -1:/* w  ww.  ja  v  a2  s  .  c  o m*/
            super.visitInsn(Opcodes.ICONST_M1);
            break;
        case 0:
            super.visitInsn(Opcodes.ICONST_0);
            break;
        case 1:
            super.visitInsn(Opcodes.ICONST_1);
            break;
        case 2:
            super.visitInsn(Opcodes.ICONST_2);
            break;
        case 3:
            super.visitInsn(Opcodes.ICONST_3);
            break;
        case 4:
            super.visitInsn(Opcodes.ICONST_4);
            break;
        case 5:
            super.visitInsn(Opcodes.ICONST_5);
            break;
        default:
            if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
                super.visitIntInsn(Opcodes.BIPUSH, value);
            } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
                super.visitIntInsn(Opcodes.SIPUSH, value);
            } else {
                super.visitLdcInsn(Integer.valueOf(value));
            }
        }
    } else if (cst instanceof BigDecimal) {
        super.visitTypeInsn(NEW, "java/math/BigDecimal");
        super.visitInsn(DUP);
        super.visitLdcInsn(cst.toString());
        super.visitMethodInsn(INVOKESPECIAL, "java/math/BigDecimal", "<init>", "(Ljava/lang/String;)V");
    } else if (cst instanceof BigInteger) {
        super.visitTypeInsn(NEW, "java/math/BigInteger");
        super.visitInsn(DUP);
        super.visitLdcInsn(cst.toString());
        super.visitMethodInsn(INVOKESPECIAL, "java/math/BigInteger", "<init>", "(Ljava/lang/String;)V");
    } else if (cst instanceof Double) {
        Double aDouble = (Double) cst;
        if (aDouble == 1.0d)
            super.visitInsn(DCONST_1);
        else
            super.visitLdcInsn(cst);
    } else if (cst instanceof Long) {
        Long aLong = (Long) cst;
        if (aLong == 0L)
            super.visitInsn(LCONST_0);
        else if (aLong == 1L)
            super.visitInsn(LCONST_1);
        else
            super.visitLdcInsn(cst);
    } else if (cst instanceof Float) {
        Float aFloat = (Float) cst;
        if (aFloat == 1.0f)
            super.visitInsn(FCONST_1);
        else if (aFloat == 2.0f)
            super.visitInsn(FCONST_2);
        else
            super.visitLdcInsn(cst);
    } else if (cst == null) {
        super.visitInsn(ACONST_NULL);
    } else
        super.visitLdcInsn(cst);
}

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

License:Open Source License

/**
 * Encodes the Java code for a given Java operator expression.
 *   /*from w w  w . ja va2 s  . c o m*/
 * @param operatorExpr the java operator expression  
 * @param context
 * @return JavaTypeName
 * @throws JavaGenerationException
 */
private static JavaTypeName encodeOperatorExpr(JavaExpression.OperatorExpression operatorExpr,
        GenerationContext context) throws JavaGenerationException {

    MethodVisitor mv = context.getMethodVisitor();

    JavaOperator operator = operatorExpr.getJavaOperator();
    String symbol = operator.getSymbol();
    JavaTypeName valueType = operator.getValueType();

    // Now carry out the operation according to its type.
    if (operator.isArithmeticOp()) {

        // Add the instructions to evaluate the first argument.
        JavaTypeName arg1Type = encodeExpr(operatorExpr.getArgument(0), context);

        if (operatorExpr instanceof OperatorExpression.Unary) {

            if (symbol.equals("-")) { // number negation

                mv.visitInsn(getNegateOpCode(arg1Type));
                return arg1Type;
            }

            throw new JavaGenerationException("Unknown unary arithmetic operator " + symbol + ".");
        }

        // Add an instruction to widen the argument if necessary.
        int wideningOpCode = getWideningOpCode(arg1Type, valueType);
        if (wideningOpCode != Opcodes.NOP) {
            mv.visitInsn(wideningOpCode);
        }

        // Add the instructions to evaluate the second argument.
        JavaTypeName arg2Type = encodeExpr(operatorExpr.getArgument(1), context);

        // Add an instruction to widen the argument if necessary.
        wideningOpCode = getWideningOpCode(arg2Type, valueType);
        if (wideningOpCode != Opcodes.NOP) {
            mv.visitInsn(wideningOpCode);
        }

        // Evaluate.
        mv.visitInsn(getArithmeticBinaryOpCode(symbol, valueType));

        return valueType;
    }

    if (operator.isBitOp()) {
        // Add the instructions to evaluate the first argument.
        JavaTypeName arg1Type = encodeExpr(operatorExpr.getArgument(0), context);

        // Add an instruction to widen the argument if necessary.
        int wideningOpCode = getWideningOpCode(arg1Type, valueType);
        if (wideningOpCode != Opcodes.NOP) {
            mv.visitInsn(wideningOpCode);
        }

        if (operatorExpr instanceof OperatorExpression.Unary) {
            if (symbol.equals("~")) { // number negation
                if (valueType == JavaTypeName.INT) {
                    mv.visitInsn(Opcodes.ICONST_M1);
                    mv.visitInsn(Opcodes.IXOR);
                    return valueType;
                } else if (valueType == JavaTypeName.LONG) {
                    encodePushLongValue(new Long(-1), context);
                    mv.visitInsn(Opcodes.LXOR);
                    return valueType;
                }
            }

            throw new JavaGenerationException("Unknown unary arithmetic operator " + symbol + ".");
        }

        // Add the instructions to evaluate the second argument.
        JavaTypeName arg2Type = encodeExpr(operatorExpr.getArgument(1), context);

        // If this is >>, >>>, or << we may need to narrow the second argument to an int
        if (symbol.equals(">>") || symbol.equals(">>>") || symbol.equals("<<")) {
            if (arg2Type == JavaTypeName.LONG) {
                mv.visitInsn(Opcodes.L2I);
            }
        } else {
            // Add an instruction to widen the argument if necessary.
            wideningOpCode = getWideningOpCode(arg2Type, valueType);
            if (wideningOpCode != Opcodes.NOP) {
                mv.visitInsn(wideningOpCode);
            }
        }

        // Evaluate.
        mv.visitInsn(getArithmeticBinaryOpCode(symbol, valueType));

        return valueType;

    }

    if (operator.isLogicalOp() || operator.isRelationalOp()) {

        // Logical op:    {"!", "&&", "||"}
        // Relational op: {">", ">=", "<", "<=", "==", "!="}

        Label trueContinuation = new Label();
        Label falseContinuation = new Label();

        encodeBooleanValuedOperatorHelper(operatorExpr, context, trueContinuation, falseContinuation);
        return encodeThenTrueElseFalse(trueContinuation, falseContinuation, context);
    }

    if (operator == JavaOperator.STRING_CONCATENATION) {

        // Create an uninitialized StringBuilder, duplicate the reference (so we can invoke the initializer).
        mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
        mv.visitInsn(Opcodes.DUP);

        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");

        // append the first arg.
        JavaTypeName firstArgType = encodeExpr(operatorExpr.getArgument(0), context);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                getAppendJVMDescriptor(firstArgType));

        // Append the results of evaluation of the second arg.
        // Note that, conveniently, StringBuilder has an append() method for all the different types.
        JavaTypeName secondArgType = encodeExpr(operatorExpr.getArgument(1), context);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                getAppendJVMDescriptor(secondArgType));

        // Call toString() on the result.           
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                "()Ljava/lang/String;");

        return JavaTypeName.STRING;
    }

    return encodeTernaryOperatorExpr((OperatorExpression.Ternary) operatorExpr, context);
}

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

License:Open Source License

@Test
public void test_iconst() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.ICONST_0));
    assertStack(programState, new Constraint[][] {
            { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });

    programState = execute(new Instruction(Opcodes.ICONST_1));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO,
            BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });

    int[] opCodesConst = new int[] { Opcodes.ICONST_M1, Opcodes.ICONST_2, Opcodes.ICONST_3, Opcodes.ICONST_4,
            Opcodes.ICONST_5 };/*from  ww  w. j av a2s . co  m*/
    for (int opcode : opCodesConst) {
        programState = execute(new Instruction(opcode));
        assertStack(programState, new Constraint[][] {
                { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
    }
}

From source file:org.spongepowered.asm.util.ASMHelper.java

License:MIT License

/**
 * Gets an instruction that pushes a integer onto the stack.  The
 * instruction uses the smallest push possible (ICONST_*, BIPUSH, SIPUSH or
 * Integer constant)./*from   www  . j  ava2  s .co  m*/
 *
 * @param c the integer to push onto the stack
 * @return insn node to insert
 */
public static AbstractInsnNode pushIntConstant(int c) {
    if (c == -1) {
        return new InsnNode(Opcodes.ICONST_M1);
    } else if (c >= 0 && c <= 5) {
        return new InsnNode(intConstants[c]);
    } else if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
        return new IntInsnNode(Opcodes.BIPUSH, c);
    } else if (c >= Short.MIN_VALUE && c <= Short.MAX_VALUE) {
        return new IntInsnNode(Opcodes.SIPUSH, c);
    } else {
        return new LdcInsnNode(c);
    }
}

From source file:org.spongepowered.despector.emitter.bytecode.instruction.BytecodeIntConstantEmitter.java

License:Open Source License

@Override
public void emit(BytecodeEmitterContext ctx, IntConstant arg, TypeSignature type) {
    MethodVisitor mv = ctx.getMethodVisitor();
    int val = arg.getConstant();
    if (val == -1) {
        mv.visitInsn(Opcodes.ICONST_M1);
    } else if (val == 0) {
        mv.visitInsn(Opcodes.ICONST_0);/*from w ww  . jav a 2s .c  o m*/
    } else if (val == 1) {
        mv.visitInsn(Opcodes.ICONST_1);
    } else if (val == 2) {
        mv.visitInsn(Opcodes.ICONST_2);
    } else if (val == 3) {
        mv.visitInsn(Opcodes.ICONST_3);
    } else if (val == 4) {
        mv.visitInsn(Opcodes.ICONST_4);
    } else if (val == 5) {
        mv.visitInsn(Opcodes.ICONST_5);
    } else if (val <= Byte.MAX_VALUE && val >= Byte.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, val);
    } else if (val <= Short.MAX_VALUE && val >= Short.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, val);
    } else {
        mv.visitLdcInsn(val);
    }
    ctx.updateStack(1);
}

From source file:org.spongepowered.mod.asm.util.ASMUtils.java

License:MIT License

public static AbstractInsnNode getPushIntConstant(int c) {
    if (c == -1) {
        return new InsnNode(Opcodes.ICONST_M1);
    } else if (c >= 0 && c <= 5) {
        return new InsnNode(intConstants[c]);
    } else if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
        return new IntInsnNode(Opcodes.BIPUSH, c);
    } else if (c >= Short.MIN_VALUE && c <= Short.MAX_VALUE) {
        return new IntInsnNode(Opcodes.SIPUSH, c);
    } else {/*www . j a va 2s. c o  m*/
        return new LdcInsnNode(c);
    }
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildConstructor(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*from   w  w w  . jav  a 2s  .c  o  m*/

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I");

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildEndMethod(ClassVisitor cv, String className, Dfa dfa) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "end", "()" + Type.getDescriptor(Matcher.class), null,
            null);//from   w  ww .  j a  v  a  2s.c o m

    stateLabels = new Label[dfa.getStates().size()];
    Arrays.setAll(stateLabels, i -> new Label());
    int[] keys = new int[dfa.getStates().size()];
    Arrays.setAll(keys, IntUnaryOperator.identity());

    saveLabel = new Label();
    errorLabel = new Label();

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    mv.visitLookupSwitchInsn(errorLabel, keys, stateLabels);

    for (int i = 0; i < dfa.getStates().size(); ++i) {
        mv.visitLabel(stateLabels[i]);
        DfaTransition transition = dfa.getStates().get(i).getTransition(-1);
        if (transition == null) {
            mv.visitJumpInsn(Opcodes.GOTO, errorLabel);
        } else {
            DfaState target = transition.getTarget();
            mv.visitIntInsn(Opcodes.SIPUSH, transition.getTarget().getIndex());
            mv.visitVarInsn(Opcodes.ISTORE, 1);
            mv.visitIntInsn(Opcodes.SIPUSH, !target.isTerminal() ? -1 : target.getDomains()[0]);
            mv.visitVarInsn(Opcodes.ISTORE, 2);
            debug(mv, "DFA: " + i + " .-> " + target.getIndex() + " " + Arrays.toString(target.getDomains()));
            mv.visitJumpInsn(Opcodes.GOTO, saveLabel);
        }
    }

    mv.visitLabel(errorLabel);
    debug(mv, "DFA: error");
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 1);
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    mv.visitLabel(saveLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildWorkerMethod(ClassVisitor cv, String className, Dfa dfa) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "feed",
            "(Ljava/lang/String;IIZ)" + Type.getDescriptor(Matcher.class), null, null);
    mv.visitCode();//from  ww w  .  j a v  a 2  s. c o m

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ISTORE, 5);

    errorLabel = new Label();
    saveLabel = new Label();
    loopLabel = new Label();
    continueLabel = new Label();

    mv.visitLabel(loopLabel);
    generateLengthGuard(mv);

    stateLabels = new Label[dfa.getStates().size()];
    Arrays.setAll(stateLabels, i -> new Label());
    int[] keys = new int[dfa.getStates().size()];
    Arrays.setAll(keys, IntUnaryOperator.identity());

    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitLookupSwitchInsn(errorLabel, keys, stateLabels);

    mv.visitLabel(continueLabel);
    mv.visitIincInsn(2, 1);
    mv.visitJumpInsn(Opcodes.GOTO, loopLabel);

    mv.visitLabel(errorLabel);
    debug(mv, "DFA: error");
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 5);

    mv.visitLabel(saveLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);

    for (int i = 0; i < dfa.getStates().size(); ++i) {
        mv.visitLabel(stateLabels[i]);
        DfaState state = dfa.getStates().get(i);
        generateTransitions(state, mv);
    }

    mv.visitMaxs(3, 6);
    mv.visitEnd();
}