Example usage for org.objectweb.asm Opcodes SIPUSH

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

Introduction

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

Prototype

int SIPUSH

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

Click Source Link

Usage

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

License:Open Source License

/**
 * Encodes instructions to push an int value onto the operand stack.
 * May need to box the argument 'value' if it is sufficiently large that
 * it needs to go into the constant pool.
 * @param value to be pushed /*from   w  w  w .j  a  v  a  2  s .c  o m*/
 * @param context
 * @return JavaTypeName the type of the result on the operand stack. 
 */
private static JavaTypeName encodePushIntValue(int value, GenerationContext context) {

    MethodVisitor mv = context.getMethodVisitor();

    if (value >= -1 && value <= 5) {
        // Use ICONST_n
        mv.visitInsn(Opcodes.ICONST_0 + value);

    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        // Use BIPUSH
        mv.visitIntInsn(Opcodes.BIPUSH, (byte) value);

    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        // Use SIPUSH
        mv.visitIntInsn(Opcodes.SIPUSH, (short) value);

    } else {
        // If everything fails create a Constant pool entry
        mv.visitLdcInsn(Integer.valueOf(value));
    }

    return JavaTypeName.INT;
}

From source file:org.pitest.coverage.codeassist.CoverageMethodVisitor.java

License:Apache License

private void pushConstant(final int value) {
    switch (value) {
    case 0:/*from   w  w  w .  j a  v a 2s .c o m*/
        this.mv.visitInsn(ICONST_0);
        break;
    case 1:
        this.mv.visitInsn(ICONST_1);
        break;
    case 2:
        this.mv.visitInsn(ICONST_2);
        break;
    case 3:
        this.mv.visitInsn(ICONST_3);
        break;
    case 4:
        this.mv.visitInsn(ICONST_4);
        break;
    case 5:
        this.mv.visitInsn(ICONST_5);
        break;
    default:
        if (value <= Byte.MAX_VALUE) {
            this.mv.visitIntInsn(Opcodes.BIPUSH, value);
        } else if (value <= Short.MAX_VALUE) {
            this.mv.visitIntInsn(Opcodes.SIPUSH, value);
        } else {
            this.mv.visitLdcInsn(value);
        }
    }
}

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

License:Open Source License

@Test
public void test_sipush() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.SIPUSH, 42));
    assertStack(programState,//from  ww  w .j a va  2  s  .co m
            new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });

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

    programState = execute(new Instruction(Opcodes.SIPUSH, 0));
    assertStack(programState, new Constraint[][] {
            { DivisionByZeroCheck.ZeroConstraint.ZERO, ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE } });
}

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)./*w  w  w . j  av a 2s.c  o 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);//  w w w  .  j  a v a  2s  .c  om
    } 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.ASMHelper.java

License:MIT License

/**
 * Generate a new method "int name()", which returns a constant value.
 *
 * @param clazz Class to add method to/*  www  .  ja v  a2 s.  co m*/
 * @param name Name of method
 * @param retval Return value of method
 */
public static void generateIntegerMethodConst(ClassNode clazz, String name, short retval) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()I",
            null, null);
    InsnList code = method.instructions;

    // Probably doesn't make a huge difference, but use BIPUSH if the value is small enough.
    if (retval >= Byte.MIN_VALUE && retval <= Byte.MAX_VALUE) {
        code.add(new IntInsnNode(Opcodes.BIPUSH, retval));
    } else {
        code.add(new IntInsnNode(Opcodes.SIPUSH, retval));
    }
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}

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 {/*from  w  w  w.j a  va  2 s  .c om*/
        return new LdcInsnNode(c);
    }
}

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);/*w w w. ja  va  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 generateTransition(MethodVisitor mv, DfaState source, DfaState target) {
    if (source.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_M1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }/*from  w w w. jav  a  2  s. c  o  m*/
    mv.visitIntInsn(Opcodes.SIPUSH, target.getIndex());
    mv.visitVarInsn(Opcodes.ISTORE, 5);
    if (target.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitIntInsn(Opcodes.SIPUSH, target.getDomains()[0]);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }

    debug(mv, "DFA: " + source.getIndex() + " -> " + target.getIndex() + " "
            + Arrays.toString(target.getDomains()));
    if (target.isTerminal()) {
        Label noReluctant = new Label();
        mv.visitVarInsn(Opcodes.ILOAD, 4);
        mv.visitJumpInsn(Opcodes.IFEQ, noReluctant);
        mv.visitIincInsn(2, 1);
        debug(mv, "DFA reached terminal state");
        mv.visitJumpInsn(Opcodes.GOTO, saveLabel);
        mv.visitLabel(noReluctant);
    }
    if (source.getIndex() + 1 == target.getIndex()) {
        mv.visitIincInsn(2, 1);
        generateLengthGuard(mv);
        mv.visitJumpInsn(Opcodes.GOTO, stateLabels[target.getIndex()]);
    } else {
        mv.visitJumpInsn(Opcodes.GOTO, continueLabel);
    }
}

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

License:LGPL

/**
 * /*from   w  w w . j  av a2  s.  co m*/
 * @param num
 * @return
 */
private ByteCodeHelper visitInt(int num) {
    /**
     * 
     * @param num
     */
    int label = 0;
    switch (num) {
    case -1:
        mv.visitInsn(Opcodes.ICONST_M1);
        break;
    case 0:
        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 (num <= Byte.MAX_VALUE && num >= Byte.MIN_VALUE) {
            mv.visitIntInsn(Opcodes.BIPUSH, num);
        } else if (num <= Short.MAX_VALUE && num >= Short.MIN_VALUE) {
            mv.visitIntInsn(Opcodes.SIPUSH, num);
        } else if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE) {
            mv.visitLdcInsn(num);
            /**
             *  ??
             */
        } else if (num <= Long.MAX_VALUE && num >= Long.MIN_VALUE) {
            mv.visitLdcInsn(num);
            label = 1;
        } else {
            // todo throw something
        }
    }
    if (label == 1) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
    } else {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
    }
    return this;
}