Example usage for org.objectweb.asm.tree InsnList get

List of usage examples for org.objectweb.asm.tree InsnList get

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList get.

Prototype

public AbstractInsnNode get(final int index) 

Source Link

Document

Returns the instruction whose index is given.

Usage

From source file:de.tuberlin.uebb.jbop.optimizer.var.FinalFieldInlinerTest.java

License:Open Source License

/**
 * Tests that finalFieldInliner is is working correctly for field-Chains with single-arrays.
 * /*from   www.  ja  v  a 2  s .  c  o m*/
 * @throws Exception
 *           the exception
 */
@Test
public void testFinalFieldInlinerFieldChainArray() throws Exception {
    // INIT
    final ClassNodeBuilder builderC = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.C")
            .addField("d", "[D").withModifiers(ACC_PRIVATE, ACC_FINAL).withAnnotation(ImmutableArray.class)
            .initArrayWith(1.0).//
            toClass();
    final ClassNodeBuilder builderB = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.B")
            .addField("c", Type.getDescriptor(builderC.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderA = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.A")
            .addField("b", Type.getDescriptor(builderB.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderTestClass = ClassNodeBuilder
            .createClass("de.tuberlin.uebb.jbop.optimizer.var.ChainedTestClass")
            .addField("a", Type.getDescriptor(builderA.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).//
            addMethod("get", "()D").//
            addGetClassField("a").// ;
            addGetField(builderA, "b").//
            addGetField(builderB, "c").//
            addGetField(builderC, "d").//
            addInsn(new InsnNode(ICONST_0)).//
            addInsn(new InsnNode(DALOAD)).//
            addInsn(new InsnNode(DRETURN));//

    // RUN
    final Object instance = builderTestClass.instance();

    inliner.setInputObject(instance);
    method = builderTestClass.getMethod("get");
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT
    assertEquals(2, optimized.size());
    assertEquals(1.0, NodeHelper.getNumberValue(optimized.get(0)).doubleValue(), .00001);
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case:/*from   w ww . ja  v a  2 s  .c o m*/
 * there is a single store of a known value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInliner() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_1)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_1, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that LocalVarInliner is working correctly.
 * There is more then one store to the var, therefore
 * the load of the var is not replaced.//from   ww w  .  j a  va 2 s. co m
 */
@Test
public void testLocalVarInlinerDoubleStore() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_1)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new InsnNode(ICONST_2)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(6, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(6, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_2, optimized.get(4).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that LocalVarInliner is working correctly.
 * There is a single (direct) store to the var, but
 * the var is altered via a iinc instruction. Therefore
 * the load of the variable is not replaced.
 *//*from  w  w w .java 2s.  c om*/
@Test
public void testLocalVarInlinerIInc() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_0)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new IincInsnNode(1, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(5, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(5, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_1, optimized.get(3).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case:/*from   www. j a va2s  .co  m*/
 * there is a single store of a known 'String' value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInlinerStringValue() {
    // INIT
    builder.addInsn(new LdcInsnNode("Hallo")).//
            addInsn(new VarInsnNode(ASTORE, 1)).//
            addInsn(new VarInsnNode(ALOAD, 1)).//
            addInsn(new InsnNode(ARETURN));//
    methodNode.desc = "()Ljava/lang/String;";

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(LDC, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case://from w ww.  j ava  2s .  com
 * there is a single store of a known 'null' value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInlinerNullValue() {
    // INIT
    builder.addInsn(new InsnNode(ACONST_NULL)).//
            addInsn(new VarInsnNode(ASTORE, 1)).//
            addInsn(new VarInsnNode(ALOAD, 1)).//
            addInsn(new InsnNode(ARETURN));//
    methodNode.desc = "()Ljava/lang/Object;";

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ACONST_NULL, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

@Test
public void testLocalVarInlinerWithIf2() {
    // INIT/*w w w.jav a2s.co  m*/
    final LabelNode label1 = new LabelNode();
    builder.add(ICONST_1).//
            add(ISTORE, 1).//
            add(ICONST_1).//
            add(IFEQ, label1).//
            add(ICONST_2).//
            add(ISTORE, 2).//
            add(ILOAD, 2).//
            add(ISTORE, 3).//
            addInsn(label1).//
            add(ILOAD, 1).//
            addReturn();

    // RUN
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_2, optimized.get(6).getOpcode());
    assertEquals(ICONST_1, optimized.get(9).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVarsTest.java

License:Open Source License

/**
 * Tests that RemoveUnusedLocalVars is working correctly.
 *///  w  w w . j  a  v  a 2s  .com
@Test
public void testRemoveUnusedLocalVars() {
    // INIT
    builder.add(ICONST_1).//
            add(ISTORE, 1).//
            add(ICONST_2).//
            add(ISTORE, 2).//
            add(ICONST_3).//
            add(ISTORE, 3).//
            add(ICONST_4).//
            add(ISTORE, 4).//
            add(ICONST_5).//
            add(ISTORE, 5).//
            add(DCONST_1).//
            add(DSTORE, 6).//
            add(ACONST_NULL).//
            add(ASTORE, 7).//
            add(FCONST_1).//
            add(FSTORE, 8).//
            add(LCONST_1).//
            add(LSTORE, 9).//
            add(ILOAD, 1).//
            addReturn();

    // RUN
    assertEquals(20, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertEquals(ICONST_1, optimized.get(0).getOpcode());
    assertEquals(ISTORE, optimized.get(1).getOpcode());
    assertEquals(1, ((VarInsnNode) optimized.get(1)).var);
    assertEquals(ILOAD, optimized.get(2).getOpcode());
    assertEquals(1, ((VarInsnNode) optimized.get(2)).var);
    assertEquals(IRETURN, optimized.get(3).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVarsTest.java

License:Open Source License

/**
 * Tests that RemoveUnusedLocalVars is working correctly.
 *///from  w  ww  .  ja  v  a2  s .co  m
@Test
public void testRemoveUnusedLocalVarsWithArray() {
    // INIT
    builder.addField("multiarr", "[[I").//
            selectMethod(methodNode.name, methodNode.desc).//
            loadFieldArrayValue("multiarr", 0).//
            store(Type.getType(Object.class), 1).//
            add(ICONST_1).//
            addReturn();
    // RUN
    assertEquals(7, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(2, optimized.size());
    assertEquals(ICONST_1, optimized.get(0).getOpcode());
    assertEquals(IRETURN, optimized.get(1).getOpcode());
}

From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java

License:Open Source License

private void findBlockBoundaries() {
    InsnList insns = methodNode.instructions;
    //We find the indices of any block-ending instruction and of any jump
    //target, sort, remove duplicates, then use pairs to define blocks. Note
    //these are end-exclusive indices, thus one after the block-enders, but
    //right on the jump targets (they're one-past-the-end of the preceding
    //block)./* w ww .jav a 2 s  .c  o m*/
    List<Integer> indices = new ArrayList<>();
    indices.add(0);
    for (int i = 0; i < insns.size(); ++i) {
        AbstractInsnNode insn = insns.get(i);
        int opcode = insn.getOpcode();
        //Terminator opcodes end blocks.
        if (insn instanceof JumpInsnNode || insn instanceof LookupSwitchInsnNode
                || insn instanceof TableSwitchInsnNode || opcode == Opcodes.ATHROW || opcode == Opcodes.IRETURN
                || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN
                || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN) {
            indices.add(i + 1);
        }
        //Jump targets of this instruction end blocks.
        if (insn instanceof JumpInsnNode)
            indices.add(insns.indexOf(((JumpInsnNode) insn).label));
        else if (insn instanceof LookupSwitchInsnNode) {
            indices.add(insns.indexOf(((LookupSwitchInsnNode) insn).dflt));
            for (Object label : ((LookupSwitchInsnNode) insn).labels)
                indices.add(insns.indexOf((LabelNode) label));
        } else if (insn instanceof TableSwitchInsnNode) {
            indices.add(insns.indexOf(((TableSwitchInsnNode) insn).dflt));
            for (Object label : ((TableSwitchInsnNode) insn).labels)
                indices.add(insns.indexOf((LabelNode) label));
        }

        //While we're scanning the instructions, make the UninitializedValue
        //values for 'new' opcodes.
        if (opcode == Opcodes.NEW) {
            Klass k = getKlassByInternalName(((TypeInsnNode) insn).desc);
            ReferenceType t = typeFactory.getReferenceType(k);
            newValues.put(insn, new UninitializedValue(t, "new" + (counter++)));
        }
    }

    //Remove duplicates and sort via TreeSet.
    indices = new ArrayList<>(new TreeSet<>(indices));
    for (int i = 1; i < indices.size(); ++i)
        blocks.add(new BBInfo(indices.get(i - 1), indices.get(i), i - 1));
}