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:jaspex.speculation.newspec.FlowFrame.java

License:Open Source License

static void printCode(MethodNode mn, List<? extends Frame<?>> frames, Collection<FlowFrame> highlight,
        FlowFrame specialHighlight) {// www  .j  av  a  2s . c  o  m
    if (!Log.isTraceEnabled())
        return;

    Textifier textifier = new Textifier();
    TraceMethodVisitor tmv = new TraceMethodVisitor(textifier);
    mn.accept(tmv);

    highlight = highlight != null ? highlight : new ArrayList<FlowFrame>();

    List<String> instructions = listGenericCast(textifier.getText());
    InsnList il = mn.instructions;

    int offset = mn.tryCatchBlocks.size();
    for (int i = 0; i < instructions.size(); i++) {
        int pos = i - offset;
        Frame<?> f = pos >= 0 && pos < frames.size() ? frames.get(pos) : null;
        String insn = pos < il.size() && pos >= 0
                ? il.get(pos).toString().replace("org.objectweb.asm.tree.", "")
                : null;
        String highlightColor = specialHighlight != null && specialHighlight.equals(f) ? "45"
                : highlight.contains(f) ? "41" : "32";
        Log.trace(pos + instructions.get(i).replace("\n", "") + " "
                + (f != null ? color(f.toString(), highlightColor) : "") + " (" + insn + ")");
    }
}

From source file:jaspex.speculation.newspec.FlowFrame.java

License:Open Source License

/** Obtm AbstractInsnNode correspondente a uma Frame **/
public static AbstractInsnNode insnForFrame(InsnList il, List<? extends Frame<?>> frameList, Frame<?> f) {
    if (f != null)
        return il.get(frameList.indexOf(f));
    throw new AssertionError("Tried to lookup position of null frame");
}

From source file:kilim.analysis.BasicBlock.java

License:Open Source License

@SuppressWarnings("unchecked")
static ArrayList<BasicBlock> dupCopyContents(boolean deepCopy, BasicBlock targetBB, BasicBlock returnToBB,
        HashMap<BasicBlock, BasicBlock> bbCopyMap, HashMap<Label, LabelNode> labelCopyMap)
        throws KilimException {

    ArrayList<BasicBlock> newBBs = new ArrayList<BasicBlock>(targetBB.getSubBlocks().size());
    for (BasicBlock orig : targetBB.getSubBlocks()) {
        BasicBlock dup = bbCopyMap.get(orig);
        dup.flags = orig.flags;/*from w w  w .j  av  a  2 s .co m*/
        dup.caughtExceptionType = orig.caughtExceptionType;
        dup.startPos = orig.startPos;
        dup.endPos = orig.endPos;
        dup.flow = orig.flow;
        dup.numPredecessors = orig.numPredecessors;
        dup.startFrame = null;
        dup.usage = orig.usage.copy();
        dup.handlers = orig.handlers;
        if (orig.follower != null) {
            dup.follower = bbCopyMap.get(orig.follower);
            if (dup.follower == null) {
                assert dup.lastInstruction() == RET;
            }
        }
        dup.successors = new ArrayList<BasicBlock>(orig.successors.size());
        if (orig.lastInstruction() == RET) {
            dup.addSuccessor(returnToBB);
        } else {
            for (BasicBlock s : orig.successors) {
                BasicBlock b = bbCopyMap.get(s);
                dup.addSuccessor(b);
            }
        }

        if (deepCopy) {
            MethodFlow flow = targetBB.flow;
            InsnList instructions = flow.instructions;
            // copy instructions
            dup.startLabel = labelCopyMap.get(orig.startLabel).getLabel();
            dup.startPos = instructions.size();
            dup.endPos = dup.startPos + (orig.endPos - orig.startPos);
            // Note: last instruction (@endPos) isn't copied in the loop.
            // If it has labels, a new instruction is generated; either
            // way the last instruction is appended separately.
            int newPos = instructions.size();
            int end = orig.endPos;

            // create new labels and instructions
            for (int i = orig.startPos; i <= end; i++, newPos++) {
                Label l = flow.getLabelAt(i);
                if (l != null) {
                    l = labelCopyMap.get(l).getLabel();
                    assert l != null;
                    flow.setLabel(newPos, l);
                }
                if (i != end) {
                    // last insn gets special treatment
                    instructions.add(instructions.get(i));
                }
            }

            AbstractInsnNode lastInsn = (AbstractInsnNode) instructions.get(orig.endPos);
            LabelNode dupLabel;
            int opcode = lastInsn.getOpcode();
            if (lastInsn instanceof JumpInsnNode) {
                JumpInsnNode jin = (JumpInsnNode) lastInsn;
                if (lastInsn.getOpcode() != JSR) {
                    dupLabel = labelCopyMap.get(jin.label);
                    assert dupLabel != null;
                    lastInsn = new JumpInsnNode(lastInsn.getOpcode(), dupLabel);
                }
            } else if (opcode == TABLESWITCH) {
                TableSwitchInsnNode tsin = (TableSwitchInsnNode) lastInsn;
                LabelNode[] labels = new LabelNode[tsin.labels.size()];
                for (int i = 0; i < labels.length; i++) {
                    dupLabel = labelCopyMap.get(tsin.labels.get(i));
                    assert dupLabel != null;
                    labels[i] = dupLabel;
                }
                dupLabel = labelCopyMap.get(tsin.dflt);
                assert dupLabel != null;
                lastInsn = new TableSwitchInsnNode(tsin.min, tsin.max, dupLabel, labels);
            } else if (opcode == LOOKUPSWITCH) {
                LookupSwitchInsnNode lsin = (LookupSwitchInsnNode) lastInsn;
                LabelNode[] labels = new LabelNode[lsin.labels.size()];
                for (int i = 0; i < labels.length; i++) {
                    dupLabel = labelCopyMap.get(lsin.labels.get(i));
                    assert dupLabel != null;
                    labels[i] = dupLabel;
                }
                dupLabel = labelCopyMap.get(lsin.dflt);
                assert dupLabel != null;
                int[] keys = new int[lsin.keys.size()];
                for (int i = 0; i < keys.length; i++) {
                    keys[i] = (Integer) lsin.keys.get(i);
                }
                lastInsn = new LookupSwitchInsnNode(dupLabel, keys, labels);
            }
            instructions.add(lastInsn);
            // new handlers
            dup.handlers = new ArrayList<Handler>(orig.handlers.size());
            if (orig.handlers.size() > 0) {
                for (Handler oh : orig.handlers) {
                    Handler h = new Handler(dup.startPos + (oh.from - orig.startPos),
                            dup.endPos + (oh.to - orig.endPos), oh.type, oh.catchBB);
                    dup.handlers.add(h);
                }
            }
        }
        newBBs.add(dup);
    }
    return newBBs;
}

From source file:me.themallard.bitmmo.api.analysis.util.pattern.Pattern.java

License:Open Source License

public int getOffset(InsnList list) {
    for (int i = 0; i < list.size() - elements.size(); i++) {
        boolean x = true;
        for (int j = 0; j < elements.size(); j++) {
            if (!elements.get(j).matches(list.get(i + j))) {
                x = false;//from  ww w .ja  va  2s .  c o m
                break;
            }
        }

        if (x)
            return i;
    }

    return -1;
}

From source file:net.epoxide.surge.asm.ASMUtils.java

License:Creative Commons License

/**
 * Removes a specific set of instructions (the needle) from a much larger set of
 * instructions (the hay stack). Be cautious when using this method, as it is almost never
 * a good idea to remove instructions.//from   ww  w. ja v a  2s .  com
 *
 * @param haystack: A large list of instructions which is being searched through.
 * @param needle: A specific list of instructions which are to be removed from the larger
 *        instruction list.
 */
public static void removeNeedleFromHaystack(InsnList haystack, InsnList needle) {

    final int firstInd = haystack.indexOf(findFirstNodeFromNeedle(haystack, needle));
    final int lastInd = haystack.indexOf(findLastNodeFromNeedle(haystack, needle));
    final List<AbstractInsnNode> realNeedle = new ArrayList<AbstractInsnNode>();

    for (int i = firstInd; i <= lastInd; i++)
        realNeedle.add(haystack.get(i));

    for (final AbstractInsnNode node : realNeedle)
        haystack.remove(node);
}

From source file:net.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindStart(InsnList haystack, InsnList needle) {

    final LinkedList<AbstractInsnNode> callNodes = new LinkedList<AbstractInsnNode>();

    for (final int callPoint : insnListFind(haystack, needle))
        callNodes.add(haystack.get(callPoint));

    return callNodes;
}

From source file:net.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindEnd(InsnList haystack, InsnList needle) {

    final LinkedList<AbstractInsnNode> callNodes = new LinkedList<AbstractInsnNode>();

    for (final int callPoint : insnListFind(haystack, needle))
        callNodes.add(haystack.get(callPoint + needle.size() - 1));

    return callNodes;
}

From source file:net.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static boolean insnListMatches(InsnList haystack, InsnList needle, int start) {

    if (haystack.size() - start < needle.size())
        return false;

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

    return true;//from  w  w  w  . j  a  v a  2  s  . c o  m
}

From source file:net.epoxide.surge.asm.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++) {

        final AbstractInsnNode insn = haystack.get(h);

        if (insn.getType() == 15)
            continue;

        if (insn.getType() == 8 && !controlFlowLabels.contains(insn))
            continue;

        if (!insnEqual(haystack.get(h), needle.get(n)))
            return null;

        n++;/*from   w  ww . j  ava 2  s  . co m*/
    }

    if (n != needle.size())
        return null;

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

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

public static List<AbstractInsnNode> insnListFindStart(InsnList haystack, InsnList needle) {
    LinkedList<AbstractInsnNode> callNodes = new LinkedList<AbstractInsnNode>();
    for (int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint));
    }//from  w w  w .j a v a  2  s  .c om
    return callNodes;
}