livenessanalyzer.java

来自「proguard 一个java的混淆器」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification *             of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This library 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 library 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 Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.optimize.evaluation;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.visitor.*;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.classfile.util.SimplifiedVisitor;import proguard.evaluation.value.*;/** * This AttributeVisitor analyzes the liveness of the variables in the code * attributes that it visits, based on partial evaluation. * * @author Eric Lafortune */public class LivenessAnalyzerextends      SimplifiedVisitorimplements   AttributeVisitor,             InstructionVisitor,             ExceptionInfoVisitor{    //*    private static final boolean DEBUG = false;    /*/    private static       boolean DEBUG = true;    //*/    private static final int MAX_VARIABLES_SIZE = 64;    private final PartialEvaluator partialEvaluator;    private long[] isAliveBefore = new long[ClassConstants.TYPICAL_CODE_LENGTH];    private long[] isAliveAfter  = new long[ClassConstants.TYPICAL_CODE_LENGTH];    private long[] isCategory2   = new long[ClassConstants.TYPICAL_CODE_LENGTH];    // Fields acting as global temporary variables.    private boolean checkAgain;    private long    alive;    /**     * Creates a new LivenessAnalyzer.     */    public LivenessAnalyzer()    {        this(new PartialEvaluator());    }    /**     * Creates a new LivenessAnalyzer that will use the given partial evaluator.     * It will run this evaluator on every code attribute that it visits.     */    public LivenessAnalyzer(PartialEvaluator partialEvaluator)    {        this.partialEvaluator = partialEvaluator;    }    /**     * Returns whether the specified variable is alive before the instruction     * at the given offset.     */    public boolean isAliveBefore(int instructionOffset, int variableIndex)    {        return variableIndex >= MAX_VARIABLES_SIZE ||               (isAliveBefore[instructionOffset] & (1L << variableIndex)) != 0;    }    /**     * Sets whether the specified variable is alive before the instruction     * at the given offset.     */    public void setAliveBefore(int instructionOffset, int variableIndex, boolean alive)    {        if (variableIndex < MAX_VARIABLES_SIZE)        {            if (alive)            {                isAliveBefore[instructionOffset] |= 1L << variableIndex;            }            else            {                isAliveBefore[instructionOffset] &= ~(1L << variableIndex);            }        }    }    /**     * Returns whether the specified variable is alive after the instruction     * at the given offset.     */    public boolean isAliveAfter(int instructionOffset, int variableIndex)    {        return variableIndex >= MAX_VARIABLES_SIZE ||               (isAliveAfter[instructionOffset] & (1L << variableIndex)) != 0;    }    /**     * Sets whether the specified variable is alive after the instruction     * at the given offset.     */    public void setAliveAfter(int instructionOffset, int variableIndex, boolean alive)    {        if (variableIndex < MAX_VARIABLES_SIZE)        {            if (alive)            {                isAliveAfter[instructionOffset] |= 1L << variableIndex;            }            else            {                isAliveAfter[instructionOffset] &= ~(1L << variableIndex);            }        }    }    /**     * Returns whether the specified variable takes up two entries after the     * instruction at the given offset.     */    public boolean isCategory2(int instructionOffset, int variableIndex)    {        return variableIndex < MAX_VARIABLES_SIZE &&               (isCategory2[instructionOffset] & (1L << variableIndex)) != 0;    }    /**     * Sets whether the specified variable takes up two entries after the     * instruction at the given offset.     */    public void setCategory2(int instructionOffset, int variableIndex, boolean category2)    {        if (variableIndex < MAX_VARIABLES_SIZE)        {            if (category2)            {                isCategory2[instructionOffset] |= 1L << variableIndex;            }            else            {                isCategory2[instructionOffset] &= ~(1L << variableIndex);            }        }    }    // Implementations for AttributeVisitor.    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)    {//        DEBUG =//            clazz.getName().equals("abc/Def") &&//            method.getName(clazz).equals("abc");        if (DEBUG)        {            System.out.println();            System.out.println("Liveness analysis: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));        }        // Initialize the global arrays.        initializeArrays(codeAttribute);        // Evaluate the method.        partialEvaluator.visitCodeAttribute(clazz, method, codeAttribute);        int codeLength    = codeAttribute.u4codeLength;        int variablesSize = codeAttribute.u2maxLocals;        // We'll only really analyze the first 64 variables.        if (variablesSize > MAX_VARIABLES_SIZE)        {            variablesSize = MAX_VARIABLES_SIZE;        }        // Mark liveness blocks, as many times as necessary.        do        {            checkAgain = false;            alive      = 0L;            // Loop over all traced instructions, backward.            for (int offset = codeLength - 1; offset >= 0; offset--)            {                if (partialEvaluator.isTraced(offset))                {                    // Update the liveness based on the branch targets.                    InstructionOffsetValue branchTargets = partialEvaluator.branchTargets(offset);                    if (branchTargets != null)                    {                        // Update the liveness right after the branch instruction.                        alive = combinedLiveness(branchTargets);                    }                    // Merge the current liveness.                    alive |= isAliveAfter[offset];                    // Update the liveness after the instruction.                    isAliveAfter[offset] = alive;                    // Update the current liveness based on the instruction.                    codeAttribute.instructionAccept(clazz, method, offset, this);                    // Merge the current liveness.                    alive |= isAliveBefore[offset];                    // Update the liveness before the instruction.                    if ((~isAliveBefore[offset] & alive) != 0L)                    {                        isAliveBefore[offset] = alive;                        // Do we have to check again after this loop?                        checkAgain |= offset < maxOffset(partialEvaluator.branchOrigins(offset));                    }                }            }            // Account for the liveness at the start of the exception handlers.            codeAttribute.exceptionsAccept(clazz, method, this);        }        while (checkAgain);        // Loop over all instructions, to mark variables that take up two entries.        for (int offset = 0; offset < codeLength; offset++)        {            if (partialEvaluator.isTraced(offset))            {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?