Example usage for org.objectweb.asm Opcodes ICONST_1

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

Introduction

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

Prototype

int ICONST_1

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

Click Source Link

Usage

From source file:org.jboss.byteman.rule.expression.TwiddleExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    // compile the operand and then bit twiddle it
    Expression oper = getOperand(0);
    Type operType = oper.getType();

    int currentStack = compileContext.getStackCount();
    int expected = 0;

    oper.compile(mv, compileContext);//from  www  .  j  a v a 2  s .c  om
    compileContext.addStackCount((operType.getNBytes() > 4 ? 2 : 1));
    compileTypeConversion(operType, type, mv, compileContext);
    if (type == Type.B) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2B);
    } else if (type == Type.S) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2S);
    } else if (type == Type.C) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2C);
    } else if (type == Type.I) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
    } else if (type == Type.J) {
        expected = 2;
        mv.visitInsn(Opcodes.LCONST_1);
        mv.visitInsn(Opcodes.LXOR);
    }

    // check the stack height is what we expect
    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("MinusExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + currentStack + expected);
    }
}

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();/* ww w . j  a  v  a2s  .  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.lambdamatic.analyzer.ast.LambdaExpressionReader.java

License:Open Source License

/**
 * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null}
 * if the instruction is not a full statement (in that case, the instruction is stored in the
 * given Expression {@link Stack})./*  www. ja  va  2s  .c  o m*/
 * 
 * @param insnNode the instruction to read
 * @param expressionStack the expression stack to put on or pop from.
 * @param localVariables the local variables
 * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created
 *         after reading the current instruction.
 * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode
 *      instruction listings on Wikipedia</a>
 */
private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack,
        final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) {
    final List<Statement> statements = new ArrayList<>();
    final AbstractInsnNode insnNode = insnCursor.getCurrent();
    switch (insnNode.getOpcode()) {
    // return a reference from a method
    case Opcodes.ARETURN:
        // return an integer from a method
    case Opcodes.IRETURN:
        statements.add(new ReturnStatement(expressionStack.pop()));
        break;
    // return void from method
    case Opcodes.RETURN:
        // wrap all pending expressions into ExpressionStatements
        while (!expressionStack.isEmpty()) {
            final Expression pendingExpression = expressionStack.pop();
            statements.add(new ExpressionStatement(pendingExpression));
        }
        break;
    // push a null reference onto the stack
    case Opcodes.ACONST_NULL:
        expressionStack.add(new NullLiteral());
        break;
    // load the int value 0 onto the stack
    case Opcodes.ICONST_0:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(0));
        break;
    // load the int value 1 onto the stack
    case Opcodes.ICONST_1:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(1));
        break;
    // load the int value 2 onto the stack
    case Opcodes.ICONST_2:
        expressionStack.add(new NumberLiteral(2));
        break;
    // load the int value 3 onto the stack
    case Opcodes.ICONST_3:
        expressionStack.add(new NumberLiteral(3));
        break;
    // load the int value 4 onto the stack
    case Opcodes.ICONST_4:
        expressionStack.add(new NumberLiteral(4));
        break;
    // load the int value 5 onto the stack
    case Opcodes.ICONST_5:
        expressionStack.add(new NumberLiteral(5));
        break;
    // push the long 0 onto the stack
    case Opcodes.LCONST_0:
        expressionStack.add(new NumberLiteral(0L));
        break;
    // push the long 1 onto the stack
    case Opcodes.LCONST_1:
        expressionStack.add(new NumberLiteral(1L));
        break;
    // push the 0.0f onto the stack
    case Opcodes.FCONST_0:
        expressionStack.add(new NumberLiteral(0f));
        break;
    // push the 1.0f onto the stack
    case Opcodes.FCONST_1:
        expressionStack.add(new NumberLiteral(1f));
        break;
    // push the 2.0f onto the stack
    case Opcodes.FCONST_2:
        expressionStack.add(new NumberLiteral(2f));
        break;
    // push the constant 0.0 onto the stack
    case Opcodes.DCONST_0:
        expressionStack.add(new NumberLiteral(0d));
        break;
    // push the constant 1.0 onto the stack
    case Opcodes.DCONST_1:
        expressionStack.add(new NumberLiteral(1d));
        break;
    // compare two longs values
    case Opcodes.LCMP:
        // compare two doubles
    case Opcodes.DCMPL:
        // compare two doubles
    case Opcodes.DCMPG:
        // compare two floats
    case Opcodes.FCMPL:
        // compare two floats
    case Opcodes.FCMPG:
        statements.addAll(
                readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables));
        break;
    // add 2 ints
    case Opcodes.IADD:
        expressionStack.add(readOperation(Operator.ADD, expressionStack));
        break;
    // int subtract
    case Opcodes.ISUB:
        expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack));
        break;
    // multiply 2 integers
    case Opcodes.IMUL:
        expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack));
        break;
    // divide 2 integers
    case Opcodes.IDIV:
        expressionStack.add(readOperation(Operator.DIVIDE, expressionStack));
        break;
    // negate int
    case Opcodes.INEG:
        expressionStack.add(inverseInteger(expressionStack));
        break;
    // discard the top value on the stack
    case Opcodes.POP:
        statements.add(new ExpressionStatement(expressionStack.pop()));
        break;
    // duplicate the value on top of the stack
    case Opcodes.DUP:
        expressionStack.push(expressionStack.peek());
        break;
    // insert a copy of the top value into the stack two values from the top.
    case Opcodes.DUP_X1:
        expressionStack.add(expressionStack.size() - 2, expressionStack.peek());
        break;
    // store into a reference in an array
    case Opcodes.AASTORE:
        readArrayStoreInstruction(insnNode, expressionStack);
        break;
    // converts Float to Double -> ignored.
    case Opcodes.F2D:
        break;
    default:
        throw new AnalyzeException(
                "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported.");
    }
    return statements;
}

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://from   w  w  w. j ava 2 s.  com
            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.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

protected void pushConstant(boolean value, MethodVisitor mv) {
    if (value) {//from  w  w w. j  a va 2s . co m
        mv.visitInsn(Opcodes.ICONST_1);
    } else {
        mv.visitInsn(Opcodes.ICONST_0);
    }
}

From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

public void pushConstant(int value, MethodVisitor mv) {
    switch (value) {
    case 0://from  w w w  . j  a  va2  s.  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;
    case 4:
        mv.visitInsn(Opcodes.ICONST_4);
        break;
    case 5:
        mv.visitInsn(Opcodes.ICONST_5);
        break;
    default:
        if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
            mv.visitIntInsn(Opcodes.BIPUSH, value);
        } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
            mv.visitIntInsn(Opcodes.SIPUSH, value);
        } else {
            mv.visitLdcInsn(Integer.valueOf(value));
        }
    }
}

From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

/**
 * negate a boolean on stack. true->false, false->true
 * @param mv/*from www .j a va 2s  . c  o m*/
 */
public void negateBoolean(MethodVisitor mv) {
    // code to negate the primitive boolean
    Label endLabel = new Label();
    Label falseLabel = new Label();
    mv.visitJumpInsn(Opcodes.IFNE, falseLabel);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitJumpInsn(Opcodes.GOTO, endLabel);
    mv.visitLabel(falseLabel);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitLabel(endLabel);
}

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

License:Open Source License

/**
 * Add the <clinit> method. This initializes the static fields that have initializers.
 * @param classRep// w  w w . j a  v a2s .c o m
 * @param cv
 * @param initializeForAsserts
 * @throws JavaGenerationException
 */
private static void encodeClassInitializer(JavaClassRep classRep, ClassVisitor cv, boolean initializeForAsserts)
        throws JavaGenerationException {

    // Add initializers for the statically-initialized fields.
    final int nFields = classRep.getNFieldDeclarations();

    if (!classRep.hasInitializedStaticField() && !initializeForAsserts) {
        //we don't need to bother adding a static initializer if there are no static fields to initialize.
        //note that javac also has this optimization.
        return;
    }

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);

    // Start the method's code.
    mv.visitCode();

    final JavaTypeName classRepTypeName = classRep.getClassName();

    /*
     * If this class contains assert statements we need to initialize the static final synthetic boolean field
     * '$assertionsDisabled'.
     * This is done by loading the Class constant for the top level class. The method 'desiredAssertionStatus()'
     * is then invoked on this Class object and the returned value is used to initialize $assertionsDisabled. 
     */
    if (initializeForAsserts) {
        // Get the fully-qualified internal class  name.    
        final String className = classRepTypeName.getJVMInternalName();

        // Get the top level class name.
        String topLevelClassName = getTopLevelClassJVMInternalName(className);

        // Load the Class constant for the top level class
        mv.visitLdcInsn(Type.getType("L" + topLevelClassName + ";"));

        // Now that we have the Class constant for the top level class we invoke the method
        // desiredAssertionStatus on it and use the resulting value to initialize the static field $assertionsDisabled in this class.
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "desiredAssertionStatus", "()Z");
        Label l0 = new Label();
        mv.visitJumpInsn(Opcodes.IFNE, l0);
        mv.visitInsn(Opcodes.ICONST_1);
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitLabel(l1);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, className, "$assertionsDisabled", "Z");
    }

    for (int i = 0; i < nFields; ++i) {

        JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i);
        JavaExpression initializer = fieldDecl.getInitializer();

        if (initializer != null && Modifier.isStatic(fieldDecl.getModifiers())) {

            GenerationContext context = new GenerationContext(new HashMap<String, JavaTypeName>(),
                    new HashMap<String, Integer>(), 0, mv, classRepTypeName);

            //evaluate the initializer
            encodeExpr(initializer, context);

            //do the assignment
            mv.visitFieldInsn(Opcodes.PUTSTATIC, classRep.getClassName().getJVMInternalName(),
                    fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor());
        }
    }

    // return.
    mv.visitInsn(Opcodes.RETURN);

    //mark the end of the method with a call to visitMaxs
    mv.visitMaxs(0, 0);

    // End the method.
    mv.visitEnd();
}

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

License:Open Source License

private static JavaTypeName encodeThenTrueElseFalse(Label trueContinuation, Label falseContinuation,
        GenerationContext context) {/*w w w. j av a 2  s .com*/

    MethodVisitor mv = context.getMethodVisitor();

    mv.visitLabel(trueContinuation);
    mv.visitInsn(Opcodes.ICONST_1);
    Label nextLabel = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, nextLabel);
    mv.visitLabel(falseContinuation);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitLabel(nextLabel);

    return JavaTypeName.BOOLEAN;
}

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

License:Open Source License

/**
 * Encodes instructions to push a boolean value onto the operand stack.
 * @param value to be pushed    //from  w w  w .j  a  v  a  2s  .  c  o  m
 * @param context
 * @return JavaTypeName the type of the result on the operand stack.      
 */
private static JavaTypeName encodePushBooleanValue(Boolean value, GenerationContext context) {
    MethodVisitor mv = context.getMethodVisitor();

    boolean v = value.booleanValue();
    if (v) {
        mv.visitInsn(Opcodes.ICONST_1);
    } else {
        mv.visitInsn(Opcodes.ICONST_0);
    }

    return JavaTypeName.BOOLEAN;
}