Example usage for org.objectweb.asm MethodVisitor visitIntInsn

List of usage examples for org.objectweb.asm MethodVisitor visitIntInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitIntInsn.

Prototype

public void visitIntInsn(final int opcode, final int operand) 

Source Link

Document

Visits an instruction with a single int operand.

Usage

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testMultiply() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);//from  w w  w .j  ava  2  s.  c  o m
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IMUL);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a * b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testDiv() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);/*from   w w w.j a  va  2 s.  co m*/
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IDIV);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a / b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testRem() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);/*from ww w . j a  va  2  s .c  o  m*/
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IREM);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a % b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testShr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);// w  ww. j ava2 s. co  m
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(ISHR);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a shr b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testUshr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);/*from   w  w w  .j a  v  a  2 s  .  c o m*/
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IUSHR);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a ushr b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testShl() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);//from   w  w  w  .  jav  a  2s .  c  om
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(ISHL);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n" + "    i = a shl b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testFloatCompare() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZFF)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);/*  w w w.j  a v a  2s.c  o m*/
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitIntInsn(FLOAD, 1);
    mv.visitIntInsn(FLOAD, 2);
    mv.visitInsn(FCMPG);
    mv.visitJumpInsn(IFGT, l1);
    mv.visitInsn(ICONST_1);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l2);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "Z", null, start, end, 0);
    mv.visitLocalVariable("a", "F", null, start, end, 1);
    mv.visitLocalVariable("b", "F", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Boolean, a: Float, b: Float) {\n" + "    i = a <= b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.RangeTests.java

License:Open Source License

@Test
public void testIfRange() {
    TestMethodBuilder builder = new TestMethodBuilder("decimalDigitValue", "(C)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    mv.visitLabel(start);// w w w  .jav a 2  s.c o  m
    Label end = new Label();
    mv.visitIntInsn(BIPUSH, 48);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IF_ICMPGT, l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 57);
    mv.visitJumpInsn(IF_ICMPGT, l1);
    mv.visitInsn(ICONST_0);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_1);
    mv.visitLabel(l2);
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("Out of range");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitIntInsn(BIPUSH, 48);
    mv.visitInsn(ISUB);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("c", "C", null, start, end, 0);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "decimalDigitValue");
    String good = "fun decimalDigitValue(c: Char): Int {\n" + "    if (param1 !in '0'..'9') {\n"
            + "        throw IllegalArgumentException(\"Out of range\")\n" + "    }\n\n" + "    return c - 48\n"
            + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.springframework.migrationanalyzer.contributions.bytecode.DelegatingMethodVisitor.java

License:Apache License

@Override
public void visitIntInsn(int opcode, int operand) {
    for (MethodVisitor delegate : this.delegates) {
        delegate.visitIntInsn(opcode, operand);
    }//from   www . jav  a 2  s  . co  m
}

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   www  .  j  a  va2s  .  c  om*/

    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();
}