1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/*
2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ProGuard -- shrinking, optimization, obfuscation, and preverification
3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *             of Java bytecode.
4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
52270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom * Copyright (c) 2002-2014 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.*;
24b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.attribute.*;
25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.attribute.visitor.AttributeVisitor;
26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.editor.CodeAttributeEditor;
27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.instruction.visitor.InstructionVisitor;
28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.util.SimplifiedVisitor;
29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/**
31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This AttributeVisitor sets up and applies the peephole optimizations of its
32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * instruction visitor. The instruction visitor should be using the same
33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * (optional) branch target finder and code attribute editor.
34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @author Eric Lafortune
36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */
37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopublic class PeepholeOptimizer
38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoextends      SimplifiedVisitor
39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimplements   AttributeVisitor
40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato{
41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final BranchTargetFinder  branchTargetFinder;
42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final CodeAttributeEditor codeAttributeEditor;
43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final InstructionVisitor  instructionVisitor;
44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new PeepholeOptimizer.
48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param codeAttributeEditor the code attribute editor that will be reset
49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            and then executed.
50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param instructionVisitor  the instruction visitor that performs
51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            peephole optimizations using the above code
52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            attribute editor.
53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public PeepholeOptimizer(CodeAttributeEditor codeAttributeEditor,
55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                             InstructionVisitor  instructionVisitor)
56b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
57b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this(null, codeAttributeEditor, instructionVisitor);
58b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
59b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new PeepholeOptimizer.
63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param branchTargetFinder  branch target finder that will be initialized
64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            to indicate branch targets in the visited code.
65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param codeAttributeEditor the code attribute editor that will be reset
66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            and then executed.
67b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param instructionVisitor  the instruction visitor that performs
68b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            peephole optimizations using the above code
69b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                            attribute editor.
70b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
71b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public PeepholeOptimizer(BranchTargetFinder  branchTargetFinder,
72b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                             CodeAttributeEditor codeAttributeEditor,
73b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                             InstructionVisitor  instructionVisitor)
74b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
75b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.branchTargetFinder  = branchTargetFinder;
76b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.codeAttributeEditor = codeAttributeEditor;
77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.instructionVisitor  = instructionVisitor;
78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Implementations for AttributeVisitor.
82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
84b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
85b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
86b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
87b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        if (branchTargetFinder != null)
89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // Set up the branch target finder.
91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute);
92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Set up the code attribute editor.
95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        codeAttributeEditor.reset(codeAttribute.u4codeLength);
96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Find the peephole optimizations.
98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        codeAttribute.instructionsAccept(clazz, method, instructionVisitor);
99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Apply the peephole optimizations.
101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
102b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
103b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato}
104