Java Code Examples for proguard.classfile.instruction.InstructionConstants#OP_GOTO

The following examples show how to use proguard.classfile.instruction.InstructionConstants#OP_GOTO . 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: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    switch (branchInstruction.opcode)
    {
        case InstructionConstants.OP_GOTO:
        case InstructionConstants.OP_GOTO_W:
            // Don't replace unconditional branches.
            break;

        case InstructionConstants.OP_JSR:
        case InstructionConstants.OP_JSR_W:
            replaceJsrInstruction(clazz, offset, branchInstruction);
            break;

        default:
            replaceBranchInstruction(clazz, offset, branchInstruction);
            break;
    }
}
 
Example 2
Source File: EvaluationSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the given instruction by an infinite loop.
 */
private void replaceByInfiniteLoop(Clazz       clazz,
                                   int         offset,
                                   Instruction instruction)
{
    // Replace the instruction by an infinite loop.
    Instruction replacementInstruction =
        new BranchInstruction(InstructionConstants.OP_GOTO, 0);

    if (DEBUG) System.out.println("  Replacing unreachable instruction by infinite loop "+replacementInstruction.toString(offset));

    codeAttributeEditor.replaceInstruction(offset, replacementInstruction);

    // Visit the instruction, if required.
    if (extraInstructionVisitor != null)
    {
        // Note: we're not passing the right arguments for now, knowing that
        // they aren't used anyway.
        instruction.accept(clazz, null, null, offset, extraInstructionVisitor);
    }
}
 
Example 3
Source File: StackSizeComputer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    byte opcode = branchInstruction.opcode;

    // Evaluate the target instruction blocks.
    evaluateInstructionBlock(clazz,
                             method,
                             codeAttribute,
                             offset +
                             branchInstruction.branchOffset);

    // Evaluate the instructions after a subroutine branch.
    if (opcode == InstructionConstants.OP_JSR ||
        opcode == InstructionConstants.OP_JSR_W)
    {
        // We assume subroutine calls (jsr and jsr_w instructions) don't
        // change the stack, other than popping the return value.
        stackSize -= 1;

        evaluateInstructionBlock(clazz,
                                 method,
                                 codeAttribute,
                                 offset + branchInstruction.length(offset));
    }

    // Some branch instructions always end the current instruction block.
    exitInstructionBlock =
        opcode == InstructionConstants.OP_GOTO   ||
        opcode == InstructionConstants.OP_GOTO_W ||
        opcode == InstructionConstants.OP_JSR    ||
        opcode == InstructionConstants.OP_JSR_W;
}
 
Example 4
Source File: EvaluationShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the given instruction by an infinite loop.
 */
private void replaceByInfiniteLoop(Clazz clazz,
                                   int   offset)
{
    if (DEBUG) System.out.println("  Inserting infinite loop at ["+offset+"]");

    // Mark the instruction.
    markInstruction(offset);

    // Replace the instruction by an infinite loop.
    Instruction replacementInstruction =
        new BranchInstruction(InstructionConstants.OP_GOTO, 0);

    codeAttributeEditor.replaceInstruction(offset, replacementInstruction);
}
 
Example 5
Source File: ReachableCodeMarker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Mark the branch target.
    markBranchTarget(clazz,
                     method,
                     codeAttribute,
                     offset + branchInstruction.branchOffset);

    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_GOTO ||
        opcode == InstructionConstants.OP_GOTO_W)
    {
        next = false;
    }
}
 
Example 6
Source File: BranchTargetFinder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Mark the branch origin.
    markBranchOrigin(offset);

    // Check if this is the first instruction of a subroutine.
    checkSubroutine(offset);

    // Mark the branch target.
    markBranchTarget(offset, branchInstruction.branchOffset);

    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_JSR ||
        opcode == InstructionConstants.OP_JSR_W)
    {
        // Mark the subroutine invocation.
        instructionMarks[offset] |= SUBROUTINE_INVOCATION;

        // Mark the subroutine start.
        int targetOffset = offset + branchInstruction.branchOffset;
        subroutineStarts[targetOffset] = targetOffset;
    }
    else if (opcode == InstructionConstants.OP_GOTO ||
             opcode == InstructionConstants.OP_GOTO_W)
    {
        // Mark the next instruction.
        markAfterBranchOrigin(offset + branchInstruction.length(offset));
    }
}
 
Example 7
Source File: GotoReturnReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Check if the instruction is an unconditional goto instruction.
    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_GOTO ||
        opcode == InstructionConstants.OP_GOTO_W)
    {
        // Check if the goto instruction points to a return instruction.
        int targetOffset = offset + branchInstruction.branchOffset;

        if (!codeAttributeEditor.isModified(offset) &&
            !codeAttributeEditor.isModified(targetOffset))
        {
            Instruction targetInstruction = InstructionFactory.create(codeAttribute.code,
                                                                      targetOffset);
            switch (targetInstruction.opcode)
            {
                case InstructionConstants.OP_IRETURN:
                case InstructionConstants.OP_LRETURN:
                case InstructionConstants.OP_FRETURN:
                case InstructionConstants.OP_DRETURN:
                case InstructionConstants.OP_ARETURN:
                case InstructionConstants.OP_RETURN:
                    // Replace the goto instruction by the return instruction.
                    Instruction returnInstruction =
                         new SimpleInstruction(targetInstruction.opcode);
                    codeAttributeEditor.replaceInstruction(offset,
                                                           returnInstruction);

                    // Visit the instruction, if required.
                    if (extraInstructionVisitor != null)
                    {
                        extraInstructionVisitor.visitBranchInstruction(clazz, method, codeAttribute, offset, branchInstruction);
                    }

                    break;
            }
        }
    }
}
 
Example 8
Source File: GotoGotoReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Check if the instruction is an unconditional goto instruction.
    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_GOTO ||
        opcode == InstructionConstants.OP_GOTO_W)
    {
        // Check if the goto instruction points to another simple goto
        // instruction.
        int branchOffset = branchInstruction.branchOffset;
        int targetOffset = offset + branchOffset;

        if (branchOffset != branchInstruction.length(offset) &&
            !codeAttributeEditor.isModified(offset) &&
            !codeAttributeEditor.isModified(targetOffset))
        {
            Instruction targetInstruction =
                InstructionFactory.create(codeAttribute.code, targetOffset);

            if (targetInstruction.opcode == InstructionConstants.OP_GOTO)
            {
                // Simplify the goto instruction.
                int targetBranchOffset = ((BranchInstruction)targetInstruction).branchOffset;

                Instruction newBranchInstruction =
                     new BranchInstruction(opcode,
                                           (branchOffset + targetBranchOffset));
                codeAttributeEditor.replaceInstruction(offset,
                                                       newBranchInstruction);

                // Visit the instruction, if required.
                if (extraInstructionVisitor != null)
                {
                    extraInstructionVisitor.visitBranchInstruction(clazz, method, codeAttribute, offset, branchInstruction);
                }
            }
        }
    }
}
 
Example 9
Source File: GotoCommonCodeReplacer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
{
    // Check if the instruction is an unconditional goto instruction that
    // isn't the target of a branch itself.
    byte opcode = branchInstruction.opcode;
    if ((opcode == InstructionConstants.OP_GOTO ||
         opcode == InstructionConstants.OP_GOTO_W) &&
        !branchTargetFinder.isBranchTarget(offset))
    {
        int branchOffset = branchInstruction.branchOffset;
        int targetOffset = offset + branchOffset;

        // Get the number of common bytes.
        int commonCount = commonByteCodeCount(codeAttribute, offset, targetOffset);

        if (commonCount > 0 &&
            !exceptionBoundary(codeAttribute, offset, targetOffset))
        {
            if (DEBUG)
            {
                System.out.println("GotoCommonCodeReplacer: "+clazz.getName()+"."+method.getName(clazz)+" (["+(offset-commonCount)+"] - "+branchInstruction.toString(offset)+" -> "+targetOffset+")");
            }

            // Delete the common instructions.
            for (int delta = 0; delta <= commonCount; delta++)
            {
                int deleteOffset = offset - delta;
                if (branchTargetFinder.isInstruction(deleteOffset))
                {
                    codeAttributeEditor.replaceInstruction(     deleteOffset, (Instruction)null);
                    codeAttributeEditor.insertBeforeInstruction(deleteOffset, (Instruction)null);
                    codeAttributeEditor.insertAfterInstruction( deleteOffset, (Instruction)null);

                    codeAttributeEditor.deleteInstruction(deleteOffset);
                }
            }

            // Redirect the goto instruction, if it is still necessary.
            int newBranchOffset = branchOffset - commonCount;
            if (newBranchOffset != branchInstruction.length(offset))
            {
                Instruction newGotoInstruction =
                     new BranchInstruction(opcode, newBranchOffset);
                codeAttributeEditor.replaceInstruction(offset,
                                                       newGotoInstruction);
            }

            // Visit the instruction, if required.
            if (extraInstructionVisitor != null)
            {
                extraInstructionVisitor.visitBranchInstruction(clazz, method, codeAttribute, offset, branchInstruction);
            }
        }
    }
}