Example usage for org.objectweb.asm.tree AbstractInsnNode getPrevious

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getPrevious

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getPrevious.

Prototype

public AbstractInsnNode getPrevious() 

Source Link

Document

Returns the previous instruction in the list to which this instruction belongs, if any.

Usage

From source file:com.friz.instruction.utility.InstructionUtil.java

License:Open Source License

/**
 * Finds the previous psuedo node following the specified node.
 * /* w ww . j  a v  a2s. c  o  m*/
 * @param node
 *            The node.
 * @return The previous psuedo node, or {@code null} if the start of the
 *         instruction list is reached.
 */
public static AbstractInsnNode previousPsuedoNode(AbstractInsnNode node) {
    while ((node = node.getPrevious()) != null && node.getOpcode() != -1)
        ;
    return node;
}

From source file:com.friz.instruction.utility.InstructionUtil.java

License:Open Source License

/**
 * Finds the previous psuedo node following the specified node.
 * /*from w  w w  .  j  a v  a2s . c  o  m*/
 * @param node
 *            The node.
 * @return The previous psuedo node, or {@code null} if the start of the
 *         instruction list is reached.
 */
public static AbstractInsnNode previousPsuedoNode(AbstractInsnNode node, int opcode) {
    while ((node = node.getPrevious()) != null && node.getOpcode() != opcode)
        ;
    return node;
}

From source file:com.github.fge.grappa.transform.process.LabellingGenerator.java

License:Apache License

@Override
public void process(@Nonnull ParserClassNode classNode, @Nonnull 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());

    InsnList instructions = method.instructions;

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

    LabelNode label = new LabelNode();
    CodeBlock block = CodeBlock.newCodeBlock().dup().ifnull(label).ldc(getLabelText(method))
            .invokeinterface(CodegenUtils.p(Rule.class), "label", CodegenUtils.sig(Rule.class, String.class))
            .label(label);//  w  ww  .  j  a  va2 s .c  o m

    instructions.insertBefore(retInsn, block.getInstructionList());
}

From source file:com.github.fge.grappa.transform.process.ReturnInstructionUnifier.java

License:Apache License

@Override
public void process(@Nonnull ParserClassNode classNode, @Nonnull RuleMethod method) throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");

    AbstractInsnNode current = method.instructions.getLast();

    // find last return
    while (current.getOpcode() != ARETURN)
        current = current.getPrevious();

    LabelNode lastReturnLabel = new LabelNode();
    method.instructions.insertBefore(current, lastReturnLabel);

    // iterate backwards up to first instructions
    while ((current = current.getPrevious()) != null) {

        // replace returns with gotos
        if (current.getOpcode() != ARETURN)
            continue;

        JumpInsnNode insn = new JumpInsnNode(GOTO, lastReturnLabel);
        method.instructions.set(current, insn);
        current = insn;/*from  www .jav a  2 s .  co m*/
    }
}

From source file:com.github.fge.grappa.transform.process.VarFramingGenerator.java

License:Apache License

@Override
public void process(@Nonnull ParserClassNode classNode, @Nonnull RuleMethod method) throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    InsnList instructions = method.instructions;

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

    CodeBlock block = CodeBlock.newCodeBlock();

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

    createVarFieldArray(block, method);/*from  w ww.ja v  a 2 s  . co m*/

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

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

    method.setBodyRewritten();
}

From source file:com.lodgon.parboiled.transform.FlagMarkingGenerator.java

License:Apache License

public void process(ParserClassNode classNode, RuleMethod method) throws Exception {
    checkArgNotNull(classNode, "classNode");
    checkArgNotNull(method, "method");
    checkState(!method.isSuperMethod()); // super methods have flag moved to the overriding method

    InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN) {
        ret = ret.getPrevious();
    }//from   w w w  .  j  a va  2 s.c  om

    // stack: <rule>
    instructions.insertBefore(ret, new InsnNode(DUP));
    // stack: <rule> :: <rule>
    LabelNode isNullLabel = new LabelNode();
    instructions.insertBefore(ret, new JumpInsnNode(IFNULL, isNullLabel));
    // stack: <rule>

    if (method.hasSuppressNodeAnnotation())
        generateMarkerCall(instructions, ret, "suppressNode");
    if (method.hasSuppressSubnodesAnnotation())
        generateMarkerCall(instructions, ret, "suppressSubnodes");
    if (method.hasSkipNodeAnnotation())
        generateMarkerCall(instructions, ret, "skipNode");
    if (method.hasMemoMismatchesAnnotation())
        generateMarkerCall(instructions, ret, "memoMismatches");

    // stack: <rule>
    instructions.insertBefore(ret, isNullLabel);
    // stack: <rule>
}

From source file:com.lodgon.parboiled.transform.LabellingGenerator.java

License:Apache License

public void process(ParserClassNode classNode, RuleMethod method) throws Exception {
    checkArgNotNull(classNode, "classNode");
    checkArgNotNull(method, "method");
    checkState(!method.isSuperMethod()); // super methods have flag moved to the overriding method

    InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN) {
        ret = ret.getPrevious();
    }/*  w  w  w .  j a v  a2 s.  c  o  m*/

    LabelNode isNullLabel = new LabelNode();
    // stack: <rule>
    instructions.insertBefore(ret, new InsnNode(DUP));
    // stack: <rule> :: <rule>
    instructions.insertBefore(ret, new JumpInsnNode(IFNULL, isNullLabel));
    // stack: <rule>
    instructions.insertBefore(ret, new LdcInsnNode(getLabelText(method)));
    // stack: <rule> :: <labelText>
    instructions.insertBefore(ret, new MethodInsnNode(INVOKEINTERFACE, Types.RULE.getInternalName(), "label",
            "(Ljava/lang/String;)" + Types.RULE_DESC));
    // stack: <rule>
    instructions.insertBefore(ret, isNullLabel);
    // stack: <rule>
}

From source file:com.lodgon.parboiled.transform.ReturnInstructionUnifier.java

License:Apache License

public void process(ParserClassNode classNode, RuleMethod method) throws Exception {
    checkArgNotNull(classNode, "classNode");
    checkArgNotNull(method, "method");
    if (method.getNumberOfReturns() == 1)
        return;/* w w w  . j a v  a 2 s.c om*/
    checkState(method.getNumberOfReturns() > 1);

    AbstractInsnNode current = method.instructions.getLast();

    // find last return
    while (current.getOpcode() != ARETURN) {
        current = current.getPrevious();
    }

    LabelNode lastReturnLabel = new LabelNode();
    method.instructions.insertBefore(current, lastReturnLabel);

    // iterate backwards up to first instructions
    while ((current = current.getPrevious()) != null) {

        // replace returns with gotos
        if (current.getOpcode() == ARETURN) {
            JumpInsnNode gotoInstruction = new JumpInsnNode(GOTO, lastReturnLabel);
            method.instructions.set(current, gotoInstruction);
            current = gotoInstruction;
        }
    }
}

From source file:com.lodgon.parboiled.transform.VarFramingGenerator.java

License:Apache License

public void process(ParserClassNode classNode, RuleMethod method) throws Exception {
    checkArgNotNull(classNode, "classNode");
    checkArgNotNull(method, "method");
    InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN) {
        ret = ret.getPrevious();
    }//from ww w. j ava  2  s.  c  om

    // stack: <Matcher>
    instructions.insertBefore(ret, new TypeInsnNode(NEW, VAR_FRAMING_MATCHER.getInternalName()));
    // stack: <Matcher> :: <VarFramingMatcher>
    instructions.insertBefore(ret, new InsnNode(DUP_X1));
    // stack: <VarFramingMatcher> :: <Matcher> :: <VarFramingMatcher>
    instructions.insertBefore(ret, new InsnNode(SWAP));
    // stack: <VarFramingMatcher> :: <VarFramingMatcher> :: <Matcher>
    createVarFieldArray(method, instructions, ret);
    // stack: <VarFramingMatcher> :: <VarFramingMatcher> :: <Matcher> :: <VarFieldArray>
    instructions.insertBefore(ret, new MethodInsnNode(INVOKESPECIAL, VAR_FRAMING_MATCHER.getInternalName(),
            "<init>", '(' + RULE_DESC + '[' + VAR_DESC + ")V"));
    // stack: <VarFramingMatcher>

    method.setBodyRewritten();
}

From source file:cuchaz.enigma.CompiledSourceTypeLoader.java

License:Open Source License

private void removeRedundantClassCalls(ClassNode node) {
    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP/*www  . j  ava 2  s  .  c o m*/
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }
}