📄 branchtargetfinder.java
字号:
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.optimize.peephole;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.visitor.*;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.classfile.util.SimplifiedVisitor;/** * This AttributeVisitor finds all instruction offsets, branch targets, and * exception targets in the CodeAttribute objects that it visits. * * @author Eric Lafortune */public class BranchTargetFinderextends SimplifiedVisitorimplements AttributeVisitor, InstructionVisitor, ExceptionInfoVisitor, ConstantVisitor{ public static final int NONE = -2; public static final int AT_METHOD_ENTRY = -1; private static final short INSTRUCTION = 1 << 0; private static final short BRANCH_ORIGIN = 1 << 1; private static final short BRANCH_TARGET = 1 << 2; private static final short AFTER_BRANCH = 1 << 3; private static final short EXCEPTION_START = 1 << 4; private static final short EXCEPTION_END = 1 << 5; private static final short EXCEPTION_HANDLER = 1 << 6; private static final short SUBROUTINE_INVOCATION = 1 << 7; private static final short SUBROUTINE_START = 1 << 8; private static final short SUBROUTINE = 1 << 9; private static final short SUBROUTINE_RETURNING = 1 << 10; private static final short UNREACHABLE = 1 << 11; private static final int MAXIMUM_CREATION_OFFSETS = 32; private short[] instructionMarks = new short[ClassConstants.TYPICAL_CODE_LENGTH + 1]; private int[] subroutineStarts = new int[ClassConstants.TYPICAL_CODE_LENGTH]; private int[] subroutineEnds = new int[ClassConstants.TYPICAL_CODE_LENGTH]; private int[] creationOffsets = new int[ClassConstants.TYPICAL_CODE_LENGTH]; private int[] initializationOffsets = new int[ClassConstants.TYPICAL_CODE_LENGTH]; private int superInitializationOffset; private int minimumSubroutineEnd; private int[] recentCreationOffsets = new int[MAXIMUM_CREATION_OFFSETS]; private int recentCreationOffsetIndex; private boolean isInitializer; /** * Returns whether there is an instruction at the given offset in the * CodeAttribute that was visited most recently. */ public boolean isInstruction(int offset) { return (instructionMarks[offset] & INSTRUCTION) != 0; } /** * Returns whether the instruction at the given offset is the target of * any kind in the CodeAttribute that was visited most recently. */ public boolean isTarget(int offset) { return offset == 0 || (instructionMarks[offset] & (BRANCH_TARGET | EXCEPTION_START | EXCEPTION_END | EXCEPTION_HANDLER)) != 0; } /** * Returns whether the instruction at the given offset is the origin of a * branch instruction in the CodeAttribute that was visited most recently. */ public boolean isBranchOrigin(int offset) { return (instructionMarks[offset] & BRANCH_ORIGIN) != 0; } /** * Returns whether the instruction at the given offset is the target of a * branch instruction in the CodeAttribute that was visited most recently. */ public boolean isBranchTarget(int offset) { return (instructionMarks[offset] & BRANCH_TARGET) != 0; } /** * Returns whether the instruction at the given offset comes right after a * definite branch instruction in the CodeAttribute that was visited most * recently. */ public boolean isAfterBranch(int offset) { return (instructionMarks[offset] & AFTER_BRANCH) != 0; } /** * Returns whether the instruction at the given offset is the start of an * exception try block in the CodeAttribute that was visited most recently. */ public boolean isExceptionStart(int offset) { return (instructionMarks[offset] & EXCEPTION_START) != 0; } /** * Returns whether the instruction at the given offset is the end of an * exception try block in the CodeAttribute that was visited most recently. */ public boolean isExceptionEnd(int offset) { return (instructionMarks[offset] & EXCEPTION_END) != 0; } /** * Returns whether the instruction at the given offset is the start of an * exception catch block in the CodeAttribute that was visited most recently. */ public boolean isExceptionHandler(int offset) { return (instructionMarks[offset] & EXCEPTION_HANDLER) != 0; } /** * Returns whether the instruction at the given offset is a subroutine * invocation in the CodeAttribute that was visited most recently. */ public boolean isSubroutineInvocation(int offset) { return (instructionMarks[offset] & SUBROUTINE_INVOCATION) != 0; } /** * Returns whether the instruction at the given offset is the start of a * subroutine in the CodeAttribute that was visited most recently. */ public boolean isSubroutineStart(int offset) { return (instructionMarks[offset] & SUBROUTINE_START) != 0; } /** * Returns whether the instruction at the given offset is part of a * subroutine in the CodeAttribute that was visited most recently. */ public boolean isSubroutine(int offset) { return (instructionMarks[offset] & SUBROUTINE) != 0; } /** * Returns whether the subroutine at the given offset is ever returning * by means of a regular 'ret' instruction. */ public boolean isSubroutineReturning(int offset) { return (instructionMarks[offset] & SUBROUTINE_RETURNING) != 0; } /** * Returns the start offset of the subroutine at the given offset, in the * CodeAttribute that was visited most recently. */ public int subroutineStart(int offset) { return subroutineStarts[offset]; } /** * Returns the offset after the subroutine at the given offset, in the * CodeAttribute that was visited most recently. */ public int subroutineEnd(int offset) { return subroutineEnds[offset]; } /** * Returns whether the instruction at the given offset is unreachable in * the CodeAttribute that was visited most recently. */ public boolean isUnreachable(int offset) { return (instructionMarks[offset] & UNREACHABLE) != 0; } /** * Returns whether the instruction at the given offset is a 'new' * instruction, in the CodeAttribute that was visited most recently. */ public boolean isNew(int offset) { return initializationOffsets[offset] != NONE; } /** * Returns the instruction offset at which the object instance that is * created at the given 'new' instruction offset is initialized, or * <code>NONE</code> if it is not being created. */ public int initializationOffset(int offset) { return initializationOffsets[offset]; } /** * Returns whether the method is an instance initializer, in the * CodeAttribute that was visited most recently. */ public boolean isInitializer() { return superInitializationOffset != NONE; } /** * Returns the instruction offset at which this initializer is calling * the "super" or "this" initializer method, or <code>NONE</code> if it is * not an initializer. */ public int superInitializationOffset() { return superInitializationOffset; } /** * Returns whether the instruction at the given offset is the special * invocation of an instance initializer, in the CodeAttribute that was * visited most recently. */ public boolean isInitializer(int offset) { return creationOffsets[offset] != NONE; } /** * Returns the offset of the 'new' instruction that corresponds to the * invocation of the instance initializer at the given offset, or * <code>AT_METHOD_ENTRY</code> if the invocation is calling the "super" or * "this" initializer method, , or <code>NONE</code> if it is not a 'new' * instruction. */ public int creationOffset(int offset) { return creationOffsets[offset]; } // Implementations for AttributeVisitor. public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) { // Make sure there are sufficiently large arrays. int codeLength = codeAttribute.u4codeLength; if (subroutineStarts.length < codeLength) { // Create new arrays. instructionMarks = new short[codeLength + 1]; subroutineStarts = new int[codeLength]; subroutineEnds = new int[codeLength]; creationOffsets = new int[codeLength]; initializationOffsets = new int[codeLength]; // Reset the arrays. for (int index = 0; index < codeLength; index++) { creationOffsets[index] = NONE; initializationOffsets[index] = NONE; } } else { // Reset the arrays. for (int index = 0; index < codeLength; index++) { instructionMarks[index] = 0; subroutineStarts[index] = 0; subroutineEnds[index] = 0; creationOffsets[index] = NONE; initializationOffsets[index] = NONE; } instructionMarks[codeLength] = 0; } superInitializationOffset = NONE; // We're not starting in a subroutine. minimumSubroutineEnd = Integer.MAX_VALUE; recentCreationOffsetIndex = 0; // Initialize the stack of 'new' instruction offsets if this method is // an instance initializer. if (method.getName(clazz).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)) { recentCreationOffsets[recentCreationOffsetIndex++] = AT_METHOD_ENTRY; } // The end of the code is a branch target sentinel. instructionMarks[codeLength] = BRANCH_TARGET; // Mark branch targets by going over all instructions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -