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

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

Introduction

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

Prototype

int size

To view the source code for org.objectweb.asm.tree InsnList size.

Click Source Link

Document

The number of instructions in this list.

Usage

From source file:de.loskutov.bco.asm.CommentedASMifierClassVisitor.java

License:Open Source License

@Override
public void visitLabel(Label label) {
    addIndex(-1);//from ww  w .  j a v a  2 s.  c  o  m
    super.visitLabel(label);

    InsnList instructions = currMethod.meth.instructions;
    LabelNode currLabel = null;
    for (int i = 0; i < instructions.size(); i++) {
        AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode instanceof LabelNode) {
            LabelNode labelNode = (LabelNode) insnNode;
            if (labelNode.getLabel() == label) {
                currLabel = labelNode;
            }
        }
    }
    setCurrentLabel(currLabel);
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }/*from  ww w  .java 2s  .  co m*/

    HashMap<LabelNode, LabelNode> labels = new HashMap<>();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }
    }

    InsnList importantNodeList = new InsnList();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }

        importantNodeList.add(insn.clone(labels));
    }
    return importantNodeList;
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

public static List<Integer> insnListFind(InsnList haystack, InsnList needle) {
    LinkedList<Integer> list = new LinkedList<>();

    for (int start = 0; start <= haystack.size() - needle.size(); start++) {
        if (insnListMatches(haystack, needle, start)) {
            list.add(start);// w  w w  .  j a va2  s.co  m
        }
    }

    return list;
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindEnd(InsnList haystack, InsnList needle) {
    LinkedList<AbstractInsnNode> callNodes = new LinkedList<>();

    for (int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint + needle.size() - 1));
    }/* w  ww. j a  v a2s  .c o m*/
    return callNodes;
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

public static List<InsnListSection> insnListFindL(InsnList haystack, InsnList needle) {
    HashSet<LabelNode> controlFlowLabels = new HashSet<>();

    for (AbstractInsnNode insn = haystack.getFirst(); insn != null; insn = insn.getNext()) {
        switch (insn.getType()) {
        case 8:/*from www  .j  a v a 2s  .  co m*/
        case 15:
            break;
        case 7:
            JumpInsnNode jinsn = (JumpInsnNode) insn;
            controlFlowLabels.add(jinsn.label);
            break;
        case 11:
            TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
            for (LabelNode label : tsinsn.labels) {
                controlFlowLabels.add(label);
            }
            break;
        case 12:
            LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
            for (LabelNode label : lsinsn.labels) {
                controlFlowLabels.add(label);
            }
            break;
        }
    }

    LinkedList<InsnListSection> list = new LinkedList<>();

    nextsection: for (int start = 0; start <= haystack.size() - needle.size(); start++) {
        InsnListSection section = insnListMatchesL(haystack, needle, start, controlFlowLabels);
        if (section != null) {
            for (InsnListSection asection : list) {
                if (asection.last == section.last) {
                    continue nextsection;
                }
            }

            list.add(section);
        }
    }

    return list;
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

public static boolean insnListMatches(InsnList haystack, InsnList needle, int start) {
    if (haystack.size() - start < needle.size()) {
        return false;
    }/*from  ww w . j av  a  2s.  co m*/

    for (int i = 0; i < needle.size(); i++) {
        if (!insnEqual(haystack.get(i + start), needle.get(i))) {
            return false;
        }
    }
    return true;
}

From source file:de.sanandrew.core.manpack.transformer.InstructionComparator.java

License:Creative Commons License

private static InsnListSection insnListMatchesL(InsnList haystack, InsnList needle, int start,
        HashSet<LabelNode> controlFlowLabels) {
    int h = start, n = 0;

    for (; h < haystack.size() && n < needle.size(); h++) {
        AbstractInsnNode insn = haystack.get(h);

        if (insn.getType() == 15) {
            continue;
        }//www  .  ja va 2 s . co  m
        if (insn.getType() == 8 && !controlFlowLabels.contains(insn)) {
            continue;
        }
        if (!insnEqual(haystack.get(h), needle.get(n))) {
            return null;
        }
        n++;
    }
    if (n != needle.size()) {
        return null;
    }

    return new InsnListSection(haystack, start, h - 1);
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * if nothing is todo (empty amethod)./* w  w w.j a  va 2  s .  c  o m*/
 */
@Test
public void testArithmeticExpressionInterpreterEmptyMethod() {
    // INIT

    // RUN
    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT
    assertFalse(interpreter.isOptimized());
    assertEquals(0, optimized.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * if nothing is todo (no arithmetic Expression occur in instructions).
 *//* w  w  w .  ja  v  a2  s.co m*/
@Test
public void testArithmeticExpressionInterpreterNoArithmeticInstructions() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 2)).//
            addInsn(new InsnNode(Opcodes.ICONST_3)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 3)).//
            addInsn(new InsnNode(Opcodes.RETURN));
    // RUN
    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT
    assertFalse(interpreter.isOptimized());
    assertEquals(7, optimized.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * if arithmetic Expression occur in instructions.
 *//*w w  w  .  ja  v  a  2s.  c o  m*/
@Test
public void testArithmeticExpressionInterpreter() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.RETURN));

    // RUN STEP1
    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT STEP 1
    assertTrue(interpreter.isOptimized());
    assertEquals(2, optimized.size());
    assertEquals(Opcodes.ICONST_5, optimized.getFirst().getOpcode());

    // RUN STEP 2
    final InsnList optimized2 = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT STEP 2
    assertFalse(interpreter.isOptimized());
    assertEquals(2, optimized2.size());
    assertEquals(Opcodes.ICONST_5, optimized2.getFirst().getOpcode());
}