Java Code Examples for org.objectweb.asm.tree.InsnList#getLast()

The following examples show how to use org.objectweb.asm.tree.InsnList#getLast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterUtil.java    From diff-check with GNU Lesser General Public License v2.1 7 votes vote down vote up
public static List<LineNumberNodeWrapper> collectLineNumberNodeList(InsnList instructions) {
    List<LineNumberNodeWrapper> list = new ArrayList<>();
    AbstractInsnNode node = instructions.getFirst();
    while (node != instructions.getLast()) {
        if (node instanceof LineNumberNode) {
            if (CollectionUtils.isNotEmpty(list)) {
                list.get(list.size() - 1).setNext(node);
            }
            list.add(new LineNumberNodeWrapper(LineNumberNode.class.cast(node)));
        }
        node = node.getNext();
    }
    if (CollectionUtils.isNotEmpty(list)) {
        list.get(list.size() - 1).setNext(instructions.getLast());
    }
    return list;
}
 
Example 2
Source File: ClinitCutter.java    From zelixkiller with GNU General Public License v3.0 6 votes vote down vote up
public static AbstractInsnNode findEndLabel(InsnList insns) {
	AbstractInsnNode ain = insns.getFirst();
	while (ain != null) {
		if (ain.getOpcode() == GOTO && ain.getPrevious() != null
				&& (blockContainsSetter(ain.getPrevious()) || ain.getPrevious().getOpcode() == ASTORE)) {
			return ((JumpInsnNode) ain).label;
		}
		ain = ain.getNext();
	}
	ain = insns.getLast();
	while (ain != null) {
		if (ain.getOpcode() == IF_ICMPGE && ain.getPrevious() != null && (ain.getPrevious().getOpcode() == ILOAD)) {
			return ((JumpInsnNode) ain).label;
		}
		ain = ain.getPrevious();
	}
	throw new RuntimeException();
}
 
Example 3
Source File: InsnUtils.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
public static AbstractInsnNode findLast(InsnList il, int op) {
	AbstractInsnNode ain = il.getLast();
	while (ain != null) {
		if (ain.getOpcode() == op) {
			return ain;
		}
		ain = ain.getPrevious();
	}
	return null;
}
 
Example 4
Source File: LabellingGenerator.java    From grappa with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@Nonnull final ParserClassNode classNode,
    @Nonnull final RuleMethod method)
    throws Exception
{
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    // super methods have flag moved to the overriding method
    Preconditions.checkState(!method.isSuperMethod());

    final InsnList instructions = method.instructions;

    AbstractInsnNode retInsn = instructions.getLast();
    while (retInsn.getOpcode() != ARETURN)
        retInsn = retInsn.getPrevious();

    final LabelNode label = new LabelNode();
    final CodeBlock block = CodeBlock.newCodeBlock()
        .dup()
        .ifnull(label)
        .ldc(getLabelText(method))
        .invokeinterface(CodegenUtils.p(Rule.class), "label",
            CodegenUtils.sig(Rule.class, String.class))
        .label(label);

    instructions.insertBefore(retInsn, block.getInstructionList());
}
 
Example 5
Source File: VarFramingGenerator.java    From grappa with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@Nonnull final ParserClassNode classNode,
    @Nonnull final RuleMethod method)
    throws Exception
{
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    final InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN)
        ret = ret.getPrevious();

    final CodeBlock block = CodeBlock.newCodeBlock();

    block.newobj(CodegenUtils.p(VarFramingMatcher.class))
        .dup_x1()
        .swap();

    createVarFieldArray(block, method);

    block.invokespecial(CodegenUtils.p(VarFramingMatcher.class), "<init>",
        CodegenUtils.sig(void.class, Rule.class, Var[].class));

    instructions.insertBefore(ret, block.getInstructionList());

    method.setBodyRewritten();
}
 
Example 6
Source File: BlockSplitter.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
private static void doSplit(MethodNode methodNode, AtomicInteger counter, int callStackSize) {
    InsnList insns = methodNode.instructions;

    if (insns.size() > 10 && callStackSize < LIMIT_SIZE) {
        LabelNode p1 = new LabelNode();
        LabelNode p2 = new LabelNode();

        AbstractInsnNode p2Start = insns.get((insns.size() - 1) / 2);
        AbstractInsnNode p2End = insns.getLast();

        AbstractInsnNode p1Start = insns.getFirst();

        // We can't have trap ranges mutilated by block splitting
        if (methodNode.tryCatchBlocks.stream().anyMatch(tcbn ->
                insns.indexOf(tcbn.end) >= insns.indexOf(p2Start)
                        && insns.indexOf(tcbn.start) <= insns.indexOf(p2Start)))
            return;

        ArrayList<AbstractInsnNode> insnNodes = new ArrayList<>();
        AbstractInsnNode currentInsn = p1Start;

        InsnList p1Block = new InsnList();

        while (currentInsn != p2Start) {
            insnNodes.add(currentInsn);

            currentInsn = currentInsn.getNext();
        }

        insnNodes.forEach(insn -> {
            insns.remove(insn);
            p1Block.add(insn);
        });

        p1Block.insert(p1);
        p1Block.add(new JumpInsnNode(GOTO, p2));

        insns.insert(p2End, p1Block);
        insns.insertBefore(p2Start, new JumpInsnNode(GOTO, p1));
        insns.insertBefore(p2Start, p2);

        counter.incrementAndGet();

        // We might have messed up variable ranges when rearranging the block order.
        if (methodNode.localVariables != null)
            new ArrayList<>(methodNode.localVariables).stream().filter(lvn ->
                    insns.indexOf(lvn.end) < insns.indexOf(lvn.start)
            ).forEach(methodNode.localVariables::remove);

        doSplit(methodNode, counter, callStackSize + 1);
    }
}