📄 evaluationsimplifier.java
字号:
/* $Id: EvaluationSimplifier.java,v 1.4.2.2 2006/01/16 22:57:56 eric Exp $ * * ProGuard -- shrinking, optimization, and obfuscation of Java class files. * * Copyright (c) 2002-2006 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.evaluation;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.annotation.*;import proguard.classfile.editor.*;import proguard.classfile.instruction.*;import proguard.classfile.util.*;import proguard.classfile.visitor.*;import proguard.optimize.*;import proguard.optimize.evaluation.value.*;/** * This MemberInfoVisitor performs partial evaluation on the program methods * that it visits. * * @author Eric Lafortune */public class EvaluationSimplifierimplements MemberInfoVisitor, AttrInfoVisitor, InstructionVisitor{ //* private static final boolean DEBUG_RESULTS = false; private static final boolean DEBUG_ANALYSIS = false; private static final boolean DEBUG = false; /*/ private static boolean DEBUG_RESULTS = true; private static boolean DEBUG_ANALYSIS = true; private static boolean DEBUG = true; //*/ private static final int INITIAL_CODE_LENGTH = 1024; private static final int INITIAL_VALUE_COUNT = 32; private InstructionVisitor extraPushInstructionVisitor; private InstructionVisitor extraBranchInstructionVisitor; private InstructionVisitor extraDeletedInstructionVisitor; private PartialEvaluator partialEvaluator = new PartialEvaluator(); private SideEffectInstructionChecker sideEffectInstructionChecker = new SideEffectInstructionChecker(true); private ClassFileCleaner classFileCleaner = new ClassFileCleaner(); private CodeAttrInfoEditor codeAttrInfoEditor = new CodeAttrInfoEditor(INITIAL_CODE_LENGTH); private Variables parameters = new TracedVariables(INITIAL_VALUE_COUNT); private boolean[] isNecessary = new boolean[INITIAL_CODE_LENGTH]; private boolean[] isSimplified = new boolean[INITIAL_CODE_LENGTH]; /** * Creates a new EvaluationSimplifier. */ public EvaluationSimplifier() { this(null, null, null); } /** * Creates a new EvaluationSimplifier. * @param extraPushInstructionVisitor an optional extra visitor for all * simplified push instructions. * @param extraBranchInstructionVisitor an optional extra visitor for all * simplified branch instructions. * @param extraDeletedInstructionVisitor an optional extra visitor for all * deleted instructions. */ public EvaluationSimplifier(InstructionVisitor extraPushInstructionVisitor, InstructionVisitor extraBranchInstructionVisitor, InstructionVisitor extraDeletedInstructionVisitor) { this.extraPushInstructionVisitor = extraPushInstructionVisitor; this.extraBranchInstructionVisitor = extraBranchInstructionVisitor; this.extraDeletedInstructionVisitor = extraDeletedInstructionVisitor; } // Implementations for MemberInfoVisitor. public void visitProgramFieldInfo(ProgramClassFile programClassFile, ProgramFieldInfo programFieldInfo) {} public void visitProgramMethodInfo(ProgramClassFile programClassFile, ProgramMethodInfo programMethodInfo) {// DEBUG = DEBUG_ANALYSIS = DEBUG_RESULTS =// programClassFile.getName().equals("abc/Def") &&// programMethodInfo.getName(programClassFile).equals("abc"); try { // Process the code. programMethodInfo.attributesAccept(programClassFile, this); } catch (RuntimeException ex) { // TODO: Remove this when the partial evaluator has stabilized. System.err.println("Unexpected error while optimizing after partial evaluation:"); System.err.println(" ClassFile = ["+programClassFile.getName()+"]"); System.err.println(" Method = ["+programMethodInfo.getName(programClassFile)+programMethodInfo.getDescriptor(programClassFile)+"]"); System.err.println("Not optimizing this method"); if (DEBUG) { throw ex; } } } public void visitLibraryFieldInfo(LibraryClassFile libraryClassFile, LibraryFieldInfo libraryFieldInfo) {} public void visitLibraryMethodInfo(LibraryClassFile libraryClassFile, LibraryMethodInfo libraryMethodInfo) {} // Implementations for AttrInfoVisitor. public void visitUnknownAttrInfo(ClassFile classFile, UnknownAttrInfo unknownAttrInfo) {} public void visitInnerClassesAttrInfo(ClassFile classFile, InnerClassesAttrInfo innerClassesAttrInfo) {} public void visitEnclosingMethodAttrInfo(ClassFile classFile, EnclosingMethodAttrInfo enclosingMethodAttrInfo) {} public void visitConstantValueAttrInfo(ClassFile classFile, FieldInfo fieldInfo, ConstantValueAttrInfo constantValueAttrInfo) {} public void visitExceptionsAttrInfo(ClassFile classFile, MethodInfo methodInfo, ExceptionsAttrInfo exceptionsAttrInfo) {} public void visitLineNumberTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LineNumberTableAttrInfo lineNumberTableAttrInfo) {} public void visitLocalVariableTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTableAttrInfo localVariableTableAttrInfo) {} public void visitLocalVariableTypeTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTypeTableAttrInfo localVariableTypeTableAttrInfo) {} public void visitSourceFileAttrInfo(ClassFile classFile, SourceFileAttrInfo sourceFileAttrInfo) {} public void visitSourceDirAttrInfo(ClassFile classFile, SourceDirAttrInfo sourceDirAttrInfo) {} public void visitDeprecatedAttrInfo(ClassFile classFile, DeprecatedAttrInfo deprecatedAttrInfo) {} public void visitSyntheticAttrInfo(ClassFile classFile, SyntheticAttrInfo syntheticAttrInfo) {} public void visitSignatureAttrInfo(ClassFile classFile, SignatureAttrInfo signatureAttrInfo) {} public void visitRuntimeVisibleAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleAnnotationsAttrInfo runtimeVisibleAnnotationsAttrInfo) {} public void visitRuntimeInvisibleAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleAnnotationsAttrInfo runtimeInvisibleAnnotationsAttrInfo) {} public void visitRuntimeVisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleParameterAnnotationsAttrInfo runtimeVisibleParameterAnnotationsAttrInfo) {} public void visitRuntimeInvisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleParameterAnnotationsAttrInfo runtimeInvisibleParameterAnnotationsAttrInfo) {} public void visitAnnotationDefaultAttrInfo(ClassFile classFile, AnnotationDefaultAttrInfo annotationDefaultAttrInfo) {} public void visitCodeAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo) { if (DEBUG_RESULTS) { System.out.println(); System.out.println("Class "+ClassUtil.externalClassName(classFile.getName())); System.out.println("Method "+ClassUtil.externalFullMethodDescription(classFile.getName(), 0, methodInfo.getName(classFile), methodInfo.getDescriptor(classFile))); System.out.println(" Params:"+parameters); } // Initialize the method parameters. initializeParameters(classFile, methodInfo, codeAttrInfo); // Initialize the necessary array. initializeNecessary(codeAttrInfo); // Evaluate the method. Value returnValue = partialEvaluator.evaluate(classFile, methodInfo, codeAttrInfo, parameters); // Clean up the visitor information in the exceptions right away. codeAttrInfo.exceptionsAccept(classFile, methodInfo, classFileCleaner); int codeLength = codeAttrInfo.u4codeLength; // Reset the code changes. codeAttrInfoEditor.reset(codeLength); // Replace any instructions that can be simplified. if (DEBUG_ANALYSIS) System.out.println("Instruction simplification:"); codeAttrInfo.instructionsAccept(classFile, methodInfo, this); // Mark all essential instructions that have been encountered as used. if (DEBUG_ANALYSIS) System.out.println("Usage initialization: "); // The invocation of the "super" or "this" <init> method inside a // constructor is always necessary, even if it is assumed to have no // side effects. boolean markSuperOrThis = methodInfo.getName(classFile).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT); int aload0Offset = 0; int offset = 0; do { if (partialEvaluator.isTraced(offset)) { Instruction instruction = InstructionFactory.create(codeAttrInfo.code, offset); // Remember the most recent aload0 instruction index. if (instruction.opcode == InstructionConstants.OP_ALOAD_0) { aload0Offset = offset; } // Mark that the instruction is necessary if it is the first // invocation of the "super" or "this" <init> method // inside a constructor. else if (markSuperOrThis && instruction.opcode == InstructionConstants.OP_INVOKESPECIAL && partialEvaluator.stackProducerOffsets(offset).contains(aload0Offset)) { markSuperOrThis = false; if (DEBUG_ANALYSIS) System.out.print(offset +","); isNecessary[offset] = true; } // Mark that the instruction is necessary if it has side effects. else if (sideEffectInstructionChecker.hasSideEffects(classFile, methodInfo, codeAttrInfo, offset, instruction)) { if (DEBUG_ANALYSIS) System.out.print(offset +","); isNecessary[offset] = true; } } offset++; } while (offset < codeLength); if (DEBUG_ANALYSIS) System.out.println(); // Mark all other instructions on which the essential instructions // depend. Instead of doing this recursively, we loop across all // instructions, starting at the last one, and restarting at any // higher, previously unmarked instruction that is being marked. if (DEBUG_ANALYSIS) System.out.println("Usage marking:"); int lowestNecessaryOffset = codeLength; offset = codeLength - 1; do { int nextOffset = offset - 1; // Update the lowest index of all marked instructions higher up. if (isNecessary[offset]) { lowestNecessaryOffset = offset; } // Check if this instruction is a branch origin from a branch that // straddles some marked code. nextOffset = markStraddlingBranches(offset, partialEvaluator.branchTargets(offset), true, lowestNecessaryOffset, nextOffset); // Mark the producers on which this instruction depends. if (isNecessaryConsumer(offset)) { nextOffset = markProducers(offset, nextOffset); } // Update the lowest index of all marked instructions higher up. if (isNecessary[offset]) { lowestNecessaryOffset = offset; } // Check if this instruction is a branch target from a branch that // straddles some marked code. nextOffset = markStraddlingBranches(offset, partialEvaluator.branchOrigins(offset), false, lowestNecessaryOffset, nextOffset);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -