1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu) 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 2 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21package proguard.optimize.peephole; 22 23import proguard.classfile.*; 24import proguard.classfile.attribute.*; 25import proguard.classfile.attribute.visitor.AttributeVisitor; 26import proguard.classfile.editor.CodeAttributeEditor; 27import proguard.classfile.instruction.visitor.InstructionVisitor; 28import proguard.classfile.util.SimplifiedVisitor; 29 30/** 31 * This AttributeVisitor sets up and applies the peephole optimizations of its 32 * instruction visitor. The instruction visitor should be using the same 33 * (optional) branch target finder and code attribute editor. 34 * 35 * @author Eric Lafortune 36 */ 37public class PeepholeOptimizer 38extends SimplifiedVisitor 39implements AttributeVisitor 40{ 41 private final BranchTargetFinder branchTargetFinder; 42 private final CodeAttributeEditor codeAttributeEditor; 43 private final InstructionVisitor instructionVisitor; 44 45 46 /** 47 * Creates a new PeepholeOptimizer. 48 * @param codeAttributeEditor the code attribute editor that will be reset 49 * and then executed. 50 * @param instructionVisitor the instruction visitor that performs 51 * peephole optimizations using the above code 52 * attribute editor. 53 */ 54 public PeepholeOptimizer(CodeAttributeEditor codeAttributeEditor, 55 InstructionVisitor instructionVisitor) 56 { 57 this(null, codeAttributeEditor, instructionVisitor); 58 } 59 60 61 /** 62 * Creates a new PeepholeOptimizer. 63 * @param branchTargetFinder branch target finder that will be initialized 64 * to indicate branch targets in the visited code. 65 * @param codeAttributeEditor the code attribute editor that will be reset 66 * and then executed. 67 * @param instructionVisitor the instruction visitor that performs 68 * peephole optimizations using the above code 69 * attribute editor. 70 */ 71 public PeepholeOptimizer(BranchTargetFinder branchTargetFinder, 72 CodeAttributeEditor codeAttributeEditor, 73 InstructionVisitor instructionVisitor) 74 { 75 this.branchTargetFinder = branchTargetFinder; 76 this.codeAttributeEditor = codeAttributeEditor; 77 this.instructionVisitor = instructionVisitor; 78 } 79 80 81 // Implementations for AttributeVisitor. 82 83 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 84 85 86 public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) 87 { 88 if (branchTargetFinder != null) 89 { 90 // Set up the branch target finder. 91 branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute); 92 } 93 94 // Set up the code attribute editor. 95 codeAttributeEditor.reset(codeAttribute.u4codeLength); 96 97 // Find the peephole optimizations. 98 codeAttribute.instructionsAccept(clazz, method, instructionVisitor); 99 100 // Apply the peephole optimizations. 101 codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute); 102 } 103} 104