KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > peephole > InstructionSequencesReplacer


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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  */

21 package proguard.optimize.peephole;
22
23 import proguard.classfile.instruction.visitor.*;
24 import proguard.classfile.instruction.*;
25 import proguard.classfile.editor.CodeAttributeEditor;
26 import proguard.classfile.*;
27 import proguard.classfile.constant.Constant;
28 import proguard.classfile.attribute.CodeAttribute;
29
30 /**
31  * This InstructionVisitor replaces multiple instruction sequences at once.
32  *
33  * @see InstructionSequenceReplacer
34  * @author Eric Lafortune
35  */

36 public class InstructionSequencesReplacer
37 extends MultiInstructionVisitor
38 implements InstructionVisitor
39 {
40     private static final int PATTERN_INDEX = 0;
41     private static final int REPLACEMENT_INDEX = 1;
42
43
44     /**
45      * Creates a new InstructionSequencesReplacer.
46      * @param patternConstants any constants referenced by the pattern
47      * instruction.
48      * @param instructionSequences the instruction sequences to be replaced,
49      * with subsequently the sequence pair index,
50      * the patten/replacement index (0 or 1),
51      * and the instruction index in the sequence.
52      * @param branchTargetFinder a branch target finder that has been
53      * initialized to indicate branch targets
54      * in the visited code.
55      * @param codeAttributeEditor a code editor that can be used for
56      * accumulating changes to the code.
57      */

58     public InstructionSequencesReplacer(Constant[] patternConstants,
59                                         Instruction[][][] instructionSequences,
60                                         BranchTargetFinder branchTargetFinder,
61                                         CodeAttributeEditor codeAttributeEditor)
62     {
63         this(patternConstants,
64              instructionSequences,
65              branchTargetFinder,
66              codeAttributeEditor,
67              null);
68     }
69
70
71     /**
72      * Creates a new InstructionSequenceReplacer.
73      * @param patternConstants any constants referenced by the pattern
74      * instruction.
75      * @param instructionSequences the instruction sequences to be replaced,
76      * with subsequently the sequence pair index,
77      * the patten/replacement index (0 or 1),
78      * and the instruction index in the sequence.
79      * @param branchTargetFinder a branch target finder that has been
80      * initialized to indicate branch targets
81      * in the visited code.
82      * @param codeAttributeEditor a code editor that can be used for
83      * accumulating changes to the code.
84      * @param extraInstructionVisitor an optional extra visitor for all deleted
85      * load instructions.
86      */

87     public InstructionSequencesReplacer(Constant[] patternConstants,
88                                         Instruction[][][] instructionSequences,
89                                         BranchTargetFinder branchTargetFinder,
90                                         CodeAttributeEditor codeAttributeEditor,
91                                         InstructionVisitor extraInstructionVisitor)
92     {
93         super(createInstructionSequenceReplacers(patternConstants,
94                                                  instructionSequences,
95                                                  branchTargetFinder,
96                                                  codeAttributeEditor,
97                                                  extraInstructionVisitor));
98     }
99
100
101     /**
102      * Creates an array of InstructionSequenceReplacer instances.
103      * @param patternConstants any constants referenced by the pattern
104      * instruction.
105      * @param instructionSequences the instruction sequences to be replaced,
106      * with subsequently the sequence pair index,
107      * the from/to index (0 or 1), and the
108      * instruction index in the sequence.
109      * @param branchTargetFinder a branch target finder that has been
110      * initialized to indicate branch targets
111      * in the visited code.
112      * @param codeAttributeEditor a code editor that can be used for
113      * accumulating changes to the code.
114      * @param extraInstructionVisitor an optional extra visitor for all deleted
115      * load instructions.
116      */

117     private static InstructionVisitor[] createInstructionSequenceReplacers(Constant[] patternConstants,
118                                                                            Instruction[][][] instructionSequences,
119                                                                            BranchTargetFinder branchTargetFinder,
120                                                                            CodeAttributeEditor codeAttributeEditor,
121                                                                            InstructionVisitor extraInstructionVisitor)
122     {
123         InstructionVisitor[] instructionSequenceReplacers =
124             new InstructionSequenceReplacer[instructionSequences.length];
125
126         for (int index = 0; index < instructionSequenceReplacers.length; index++)
127         {
128             Instruction[][] instructionSequencePair = instructionSequences[index];
129             instructionSequenceReplacers[index] =
130                 new InstructionSequenceReplacer(patternConstants,
131                                                 instructionSequencePair[PATTERN_INDEX],
132                                                 instructionSequencePair[REPLACEMENT_INDEX],
133                                                 branchTargetFinder,
134                                                 codeAttributeEditor,
135                                                 extraInstructionVisitor);
136         }
137
138         return instructionSequenceReplacers;
139     }
140 }
141
Popular Tags