01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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.*;
24: import proguard.classfile.attribute.*;
25: import proguard.classfile.attribute.visitor.AttributeVisitor;
26: import proguard.classfile.editor.CodeAttributeEditor;
27: import proguard.classfile.instruction.visitor.InstructionVisitor;
28: import 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: */
37: public class PeepholeOptimizer extends SimplifiedVisitor implements
38: AttributeVisitor {
39: private final BranchTargetFinder branchTargetFinder;
40: private final CodeAttributeEditor codeAttributeEditor;
41: private final InstructionVisitor instructionVisitor;
42:
43: /**
44: * Creates a new PeepholeOptimizer.
45: * @param codeAttributeEditor the code attribute editor that will be reset
46: * and then executed.
47: * @param instructionVisitor the instruction visitor that performs
48: * peephole optimizations using the above code
49: * attribute editor.
50: */
51: public PeepholeOptimizer(CodeAttributeEditor codeAttributeEditor,
52: InstructionVisitor instructionVisitor) {
53: this (null, codeAttributeEditor, instructionVisitor);
54: }
55:
56: /**
57: * Creates a new PeepholeOptimizer.
58: * @param branchTargetFinder branch target finder that will be initialized
59: * to indicate branch targets in the visited code.
60: * @param codeAttributeEditor the code attribute editor that will be reset
61: * and then executed.
62: * @param instructionVisitor the instruction visitor that performs
63: * peephole optimizations using the above code
64: * attribute editor.
65: */
66: public PeepholeOptimizer(BranchTargetFinder branchTargetFinder,
67: CodeAttributeEditor codeAttributeEditor,
68: InstructionVisitor instructionVisitor) {
69: this .branchTargetFinder = branchTargetFinder;
70: this .codeAttributeEditor = codeAttributeEditor;
71: this .instructionVisitor = instructionVisitor;
72: }
73:
74: // Implementations for AttributeVisitor.
75:
76: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
77: }
78:
79: public void visitCodeAttribute(Clazz clazz, Method method,
80: CodeAttribute codeAttribute) {
81: if (branchTargetFinder != null) {
82: // Set up the branch target finder.
83: branchTargetFinder.visitCodeAttribute(clazz, method,
84: codeAttribute);
85: }
86:
87: // Set up the code attribute editor.
88: codeAttributeEditor.reset(codeAttribute.u4codeLength);
89:
90: // Find the peephole optimizations.
91: codeAttribute.instructionsAccept(clazz, method,
92: instructionVisitor);
93:
94: // Apply the peephole optimizations.
95: codeAttributeEditor.visitCodeAttribute(clazz, method,
96: codeAttribute);
97: }
98: }
|