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.info; 22 23import proguard.classfile.*; 24import proguard.classfile.attribute.CodeAttribute; 25import proguard.classfile.instruction.*; 26import proguard.classfile.instruction.visitor.InstructionVisitor; 27import proguard.classfile.util.SimplifiedVisitor; 28 29/** 30 * This InstructionVisitor marks all methods that branch backward in any of the 31 * instructions that it visits. 32 * 33 * @author Eric Lafortune 34 */ 35public class BackwardBranchMarker 36extends SimplifiedVisitor 37implements InstructionVisitor 38{ 39 // Implementations for InstructionVisitor. 40 41 public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {} 42 43 44 public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction) 45 { 46 markBackwardBranch(method, branchInstruction.branchOffset); 47 } 48 49 50 public void visitAnySwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SwitchInstruction switchInstruction) 51 { 52 markBackwardBranch(method, switchInstruction.defaultOffset); 53 54 for (int index = 0; index < switchInstruction.jumpOffsets.length; index++) 55 { 56 markBackwardBranch(method, switchInstruction.jumpOffsets[index]); 57 } 58 } 59 60 61 // Small utility methods. 62 63 /** 64 * Marks the given method if the given branch offset is negative. 65 */ 66 private void markBackwardBranch(Method method, int branchOffset) 67 { 68 if (branchOffset < 0) 69 { 70 setBranchesBackward(method); 71 } 72 } 73 74 75 private static void setBranchesBackward(Method method) 76 { 77 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 78 if (info != null) 79 { 80 info.setBranchesBackward(); 81 } 82 } 83 84 85 public static boolean branchesBackward(Method method) 86 { 87 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 88 return info == null || info.branchesBackward(); 89 } 90} 91