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

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

Introduction

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

Prototype

public void remove(final AbstractInsnNode insnNode) 

Source Link

Document

Removes the given instruction from this list.

Usage

From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInliner.java

License:Open Source License

private boolean handleNumberInstruction(final AbstractInsnNode currentNode, final AbstractInsnNode node1,
        final AbstractInsnNode node2, final InsnList list, final Iterator<AbstractInsnNode> iterator) {
    Number op1;//  www.ja  v a2 s . com
    AbstractInsnNode node3 = node1;
    AbstractInsnNode node4 = node2;
    final boolean eval;
    final Number op2;
    if (isCompare(node1)) {
        node3 = NodeHelper.getPrevious(node1);
        node4 = NodeHelper.getPrevious(node3);
    }
    AbstractInsnNode node5 = node3;

    try {
        if (NodeHelper.isCast(node3)) {
            node5 = node4;
            node4 = NodeHelper.getPrevious(node4);
        }
        op1 = NodeHelper.getNumberValue(node5);
    } catch (final NotANumberException nan) {
        return false;
    }
    AbstractInsnNode node6 = node4;
    if (node6 != null) {
        try {
            if (NodeHelper.isCast(node4)) {
                node6 = NodeHelper.getPrevious(node4);
            }
            op2 = NodeHelper.getNumberValue(node6);
        } catch (final NotANumberException nan) {
            return false;
        }
    } else {
        op2 = Double.valueOf(Double.NaN);
    }
    eval = evaluate(currentNode, node1, op1, op2);
    removeNodes(currentNode, node1, node3, node4, list, iterator, eval);
    if ((node5 != null) && (node5 != node3)) {
        list.remove(node5);
    }
    if ((node6 != null) && (node6 != node4)) {
        list.remove(node6);
    }
    return true;
}

From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInliner.java

License:Open Source License

private void removeNodes(final AbstractInsnNode currentNode, final AbstractInsnNode node1,
        final AbstractInsnNode node3, final AbstractInsnNode node4, final InsnList list,
        final Iterator<AbstractInsnNode> iterator, //
        final boolean eval) {
    list.remove(node1);
    if ((node3 != null) && (node3 != node1)) {
        list.remove(node3);/*from   ww  w . ja  v  a  2s.  c  o  m*/
    }
    if ((node4 != null) && (node4 != node1) && (node4 != node3)) {
        list.remove(node4);
    }
    AbstractInsnNode label = ((JumpInsnNode) currentNode).label;
    list.remove(currentNode);
    if (!eval) {
        final AbstractInsnNode previousOfLabel = NodeHelper.getPrevious(label);
        if (previousOfLabel.getOpcode() == Opcodes.GOTO) {
            while (iterator.hasNext()) {
                final AbstractInsnNode node = iterator.next();
                if (node == label) {
                    break;
                }
            }
            label = ((JumpInsnNode) previousOfLabel).label;
            list.remove(previousOfLabel);
        } else {
            return;
        }
    }

    while (iterator.hasNext()) {
        final AbstractInsnNode node = iterator.next();
        list.remove(node);
        if (node == label) {
            break;
        }
    }

}

From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopUnroller.java

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode method) {
    optimized = false;/*from  w ww . ja v  a 2  s  .co  m*/
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    final InsnList insn = new InsnList();
    final LinkedList<AbstractInsnNode> skipped = new LinkedList<>();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        final Loop loop = LoopMatcher.getLoop(currentNode);

        if ((loop == null) || !loop.isPlain()) {
            skipped.add(currentNode);
            continue;
        }
        final AbstractInsnNode last = skipped.getLast();
        skipped.remove(last);
        correctIteratorPosition(iterator, loop.getEndOfLoop());

        optimized = true;
        for (final AbstractInsnNode node : skipped) {
            insn.add(node);
        }
        skipped.clear();
        original.remove(last);
        final ForLoop forLoop = LoopMatcher.toForLoop(loop);
        insn.add(forLoop.getInsnList(method));
    }
    for (final AbstractInsnNode node : skipped) {
        insn.add(node);
    }
    return insn;

}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java

License:Open Source License

private InsnList clean(final InsnList original) {
    final ListIterator<AbstractInsnNode> iterator = original.iterator();

    while (iterator.hasNext()) {
        final AbstractInsnNode next = iterator.next();
        if ((next instanceof SplitMarkNode) || (next.getOpcode() == NOP)) {
            original.remove(next);
        }/*  ww w  .  j  a v a 2s  .co m*/
    }
    return original;
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java

License:Open Source License

private void add(final InsnList list, final List<AbstractInsnNode> insns, final InsnList original) {
    for (final AbstractInsnNode node : insns) {
        original.remove(node);
        list.add(node);//from   ww  w. ja v  a  2s.  c o  m
    }
}

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

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode methodNode) throws JBOPClassException {
    optimized = false;//from w  w w .  j a  va 2s  .  c  o m
    final ListIterator<AbstractInsnNode> iterator = original.iterator();
    final GetFieldChainInliner fieldChainInliner = new GetFieldChainInliner();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        if (!NodeHelper.isAload(currentNode)) {
            continue;
        }

        fieldChainInliner.setInputObject(instance);
        fieldChainInliner.setIterator(iterator);
        fieldChainInliner.optimize(original, methodNode);
        if (fieldChainInliner.isOptimized()) {
            original.remove(currentNode);
            optimized = true;
        }
    }
    return original;
}

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

License:Open Source License

private boolean handleBuiltIn(final String descriptor, final InsnList original, final AbstractInsnNode next,
        final Object object, final List<AbstractInsnNode> nodes,
        final ListIterator<AbstractInsnNode> localIterator) {
    if (FinalFieldInliner.isBuiltIn(descriptor)) {
        correctIterator(localIterator);/* w  ww  . ja v a  2 s .  c  om*/
        original.insertBefore(next, NodeHelper.getInsnNodeFor(object));
        for (final AbstractInsnNode node : nodes) {
            original.remove(node);
        }
        iterator.previous();
        return true;
    }
    return false;
}

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

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode methodNode) {
    optimized = false;/*  ww w  .  java  2 s . co  m*/
    final List<VarInsnNode> stores = new ArrayList<>();
    final List<VarInsnNode> users = new ArrayList<>();
    final List<IincInsnNode> iincs = new ArrayList<>();
    findNodes(original, stores, users, iincs);
    final List<AbstractInsnNode> toBeRemoved = new ArrayList<>();
    for (final VarInsnNode node : stores) {
        if (!usersContains(users, node)) {
            toBeRemoved.add(node);
            toBeRemoved.addAll(iincsContains(iincs, node));
        }
    }
    // final InsnList newList = new InsnList();
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        if (toBeRemoved.contains(currentNode)) {
            if (currentNode.getOpcode() == Opcodes.IINC) {
                original.remove(currentNode);
                optimized = true;
                continue;
            }
            final AbstractInsnNode firstOfStack = NodeHelper.getFirstOfStack(currentNode);
            if (firstOfStack != null) {
                optimized = true;
                AbstractInsnNode remove = firstOfStack;
                while (remove != currentNode) {
                    final AbstractInsnNode toRemove = remove;
                    remove = remove.getNext();
                    original.remove(toRemove);
                }
                original.remove(currentNode);
            }
        }
    }
    return original;
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.removeSystemExit.RemoveSystemExitMethodNode.java

License:Open Source License

@SuppressWarnings("unchecked")
// Call to pre-1.5 Code
@Override/*from  w w w.  jav  a2s .co  m*/
public void visitEnd() {
    MethodNode mn = (MethodNode) mv;
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    AbstractInsnNode prev = null;
    InsnList newInstrucionList = new InsnList();
    while (i.hasNext()) {
        boolean addInstruction = true;
        AbstractInsnNode i1 = (AbstractInsnNode) i.next();
        if (i1 instanceof MethodInsnNode) {
            MethodInsnNode methotInsnNode = (MethodInsnNode) i1;
            if (methotInsnNode.name.equals("exit") && methotInsnNode.owner.equals("java/lang/System")) {
                logger.info("Replacing System.exit ");
                newInstrucionList.remove(prev);
                // insns.remove(i1);
                InsnList il = new InsnList();
                Label mutationStartLabel = new Label();
                mutationStartLabel.info = new MutationMarker(true);

                il.add(new LabelNode(mutationStartLabel));
                il.add(new TypeInsnNode(Opcodes.NEW, "java/lang/RuntimeException"));
                il.add(new InsnNode(Opcodes.DUP));
                il.add(new LdcInsnNode("Replaced System.exit()"));
                il.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                        "(Ljava/lang/String;)V"));
                il.add(new InsnNode(Opcodes.ATHROW));

                Label mutationEndLabel = new Label();
                mutationEndLabel.info = new MutationMarker(false);
                il.add(new LabelNode(mutationEndLabel));
                newInstrucionList.add(il);
                addInstruction = false;
            }
        }
        if (addInstruction) {
            try {
                insns.remove(i1);
                newInstrucionList.add(i1);
            } catch (Exception e) {
                logger.error(e);
            }

        }
        prev = i1;
    }
    mn.instructions = newInstrucionList;
    mn.accept(next);
}

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

License:Open Source License

/**
 * Removes "xLOAD N xSTORE N"./*w  w  w .  j  ava 2  s .  c  om*/
 * @return true iff changes were made
 */
private boolean removeLoadStore() {
    InsnList insns = methodNode.instructions;
    for (int i = 0; i < insns.size() - 1; ++i) {
        AbstractInsnNode first = insns.get(i);
        int index = LOADS.indexOf(first.getOpcode());
        if (index == -1)
            continue;
        AbstractInsnNode second = insns.get(i + 1);
        if (second.getOpcode() != STORES.get(index))
            continue;
        if (((VarInsnNode) first).var != ((VarInsnNode) second).var)
            continue;
        insns.remove(first);
        insns.remove(second);
        return true;
    }
    return false;
}