1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/* 2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ProGuard -- shrinking, optimization, obfuscation, and preverification 3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * of Java bytecode. 4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 5b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu) 6b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 7b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is free software; you can redistribute it and/or modify it 8b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * under the terms of the GNU General Public License as published by the Free 9b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Software Foundation; either version 2 of the License, or (at your option) 10b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * any later version. 11b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 12b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is distributed in the hope that it will be useful, but WITHOUT 13b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * more details. 16b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 17b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * You should have received a copy of the GNU General Public License along 18b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with this program; if not, write to the Free Software Foundation, Inc., 19b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 21b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopackage proguard.optimize.peephole; 22b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 23b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.constant.Constant; 24b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.editor.CodeAttributeEditor; 25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.instruction.Instruction; 26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.instruction.visitor.*; 27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/** 29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This InstructionVisitor replaces multiple instruction sequences at once. 30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @see InstructionSequenceReplacer 32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @author Eric Lafortune 33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopublic class InstructionSequencesReplacer 35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoextends MultiInstructionVisitor 36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimplements InstructionVisitor 37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato{ 38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private static final int PATTERN_INDEX = 0; 39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private static final int REPLACEMENT_INDEX = 1; 40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Creates a new InstructionSequencesReplacer. 44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param patternConstants any constants referenced by the pattern 45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * instruction. 46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param instructionSequences the instruction sequences to be replaced, 47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with subsequently the sequence pair index, 48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * the patten/replacement index (0 or 1), 49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * and the instruction index in the sequence. 50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param branchTargetFinder a branch target finder that has been 51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * initialized to indicate branch targets 52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * in the visited code. 53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param codeAttributeEditor a code editor that can be used for 54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * accumulating changes to the code. 55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 56b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public InstructionSequencesReplacer(Constant[] patternConstants, 57b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato Instruction[][][] instructionSequences, 58b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato BranchTargetFinder branchTargetFinder, 59b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato CodeAttributeEditor codeAttributeEditor) 60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato this(patternConstants, 62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato instructionSequences, 63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato branchTargetFinder, 64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato codeAttributeEditor, 65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato null); 66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 67b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 68b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 69b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 70b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Creates a new InstructionSequenceReplacer. 71b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param patternConstants any constants referenced by the pattern 72b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * instruction. 73b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param instructionSequences the instruction sequences to be replaced, 74b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with subsequently the sequence pair index, 75b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * the patten/replacement index (0 or 1), 76b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * and the instruction index in the sequence. 77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param branchTargetFinder a branch target finder that has been 78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * initialized to indicate branch targets 79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * in the visited code. 80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param codeAttributeEditor a code editor that can be used for 81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * accumulating changes to the code. 82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param extraInstructionVisitor an optional extra visitor for all deleted 83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * load instructions. 84b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 85b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public InstructionSequencesReplacer(Constant[] patternConstants, 86b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato Instruction[][][] instructionSequences, 87b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato BranchTargetFinder branchTargetFinder, 88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato CodeAttributeEditor codeAttributeEditor, 89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato InstructionVisitor extraInstructionVisitor) 90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato super(createInstructionSequenceReplacers(patternConstants, 92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato instructionSequences, 93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato branchTargetFinder, 94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato codeAttributeEditor, 95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato extraInstructionVisitor)); 96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Creates an array of InstructionSequenceReplacer instances. 101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param patternConstants any constants referenced by the pattern 102b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * instruction. 103b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param instructionSequences the instruction sequences to be replaced, 104b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with subsequently the sequence pair index, 105b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * the from/to index (0 or 1), and the 106b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * instruction index in the sequence. 107b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param branchTargetFinder a branch target finder that has been 108b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * initialized to indicate branch targets 109b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * in the visited code. 110b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param codeAttributeEditor a code editor that can be used for 111b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * accumulating changes to the code. 112b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @param extraInstructionVisitor an optional extra visitor for all deleted 113b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * load instructions. 114b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 115b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[] patternConstants, 116b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato Instruction[][][] instructionSequences, 117b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato BranchTargetFinder branchTargetFinder, 118b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato CodeAttributeEditor codeAttributeEditor, 119b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato InstructionVisitor extraInstructionVisitor) 120b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 121b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato InstructionVisitor[] instructionSequenceReplacers = 122b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato new InstructionSequenceReplacer[instructionSequences.length]; 123b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 124b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato for (int index = 0; index < instructionSequenceReplacers.length; index++) 125b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 126b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato Instruction[][] instructionSequencePair = instructionSequences[index]; 127b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato instructionSequenceReplacers[index] = 128b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato new InstructionSequenceReplacer(patternConstants, 129b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato instructionSequencePair[PATTERN_INDEX], 130b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato instructionSequencePair[REPLACEMENT_INDEX], 131b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato branchTargetFinder, 132b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato codeAttributeEditor, 133b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato extraInstructionVisitor); 134b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 135b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 136b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato return instructionSequenceReplacers; 137b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 138b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato} 139