📄 livenessanalyzer.java
字号:
/* * 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.*;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 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]; // A field acting as a global temporary variable. 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. boolean checkAgain; 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 after the instruction. isAliveAfter[offset] |= alive; // Update the liveness based on the instruction. codeAttribute.instructionAccept(clazz, method, offset, this); // Update the liveness based on the branch targets. InstructionOffsetValue branchTargets = partialEvaluator.branchTargets(offset); if (branchTargets != null) { alive |= combinedLiveness(branchTargets); // Mark the variables right after the switch instruction. isAliveAfter[offset] = alive; } // Update the liveness before the instruction. if ((~isAliveBefore[offset] & alive) != 0) { isAliveBefore[offset] |= alive; // Do we have to check again after this loop? if (!checkAgain) { checkAgain = offset < maxOffset(partialEvaluator.branchOrigins(offset)); } } } } } while (checkAgain); // Check the liveness at the start of the exception handlers. codeAttribute.exceptionsAccept(clazz, method, this); // Loop over all instructions, to mark variables that take up two entries.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -