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.peephole;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.CodeAttribute;
25import proguard.classfile.editor.CodeAttributeEditor;
26import proguard.classfile.instruction.*;
27import proguard.classfile.instruction.visitor.InstructionVisitor;
28import proguard.classfile.util.SimplifiedVisitor;
29
30/**
31 * This InstructionVisitor replaces unconditional branches to return instructions
32 * by these same return instructions.
33 *
34 * @author Eric Lafortune
35 */
36public class GotoReturnReplacer
37extends      SimplifiedVisitor
38implements   InstructionVisitor
39{
40    private final CodeAttributeEditor codeAttributeEditor;
41    private final InstructionVisitor  extraInstructionVisitor;
42
43
44    /**
45     * Creates a new GotoReturnReplacer.
46     * @param codeAttributeEditor     a code editor that can be used for
47     *                                accumulating changes to the code.
48     */
49    public GotoReturnReplacer(CodeAttributeEditor codeAttributeEditor)
50    {
51        this(codeAttributeEditor, null);
52    }
53
54
55    /**
56     * Creates a new GotoReturnReplacer.
57     * @param codeAttributeEditor     a code editor that can be used for
58     *                                accumulating changes to the code.
59     * @param extraInstructionVisitor an optional extra visitor for all replaced
60     *                                goto instructions.
61     */
62    public GotoReturnReplacer(CodeAttributeEditor codeAttributeEditor,
63                              InstructionVisitor  extraInstructionVisitor)
64    {
65        this.codeAttributeEditor     = codeAttributeEditor;
66        this.extraInstructionVisitor = extraInstructionVisitor;
67    }
68
69
70    // Implementations for InstructionVisitor.
71
72    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
73
74
75    public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
76    {
77        // Check if the instruction is an unconditional goto instruction.
78        byte opcode = branchInstruction.opcode;
79        if (opcode == InstructionConstants.OP_GOTO ||
80            opcode == InstructionConstants.OP_GOTO_W)
81        {
82            // Check if the goto instruction points to a return instruction.
83            int targetOffset = offset + branchInstruction.branchOffset;
84
85            if (!codeAttributeEditor.isModified(offset) &&
86                !codeAttributeEditor.isModified(targetOffset))
87            {
88                Instruction targetInstruction = InstructionFactory.create(codeAttribute.code,
89                                                                          targetOffset);
90                switch (targetInstruction.opcode)
91                {
92                    case InstructionConstants.OP_IRETURN:
93                    case InstructionConstants.OP_LRETURN:
94                    case InstructionConstants.OP_FRETURN:
95                    case InstructionConstants.OP_DRETURN:
96                    case InstructionConstants.OP_ARETURN:
97                    case InstructionConstants.OP_RETURN:
98                        // Replace the goto instruction by the return instruction.
99                        Instruction returnInstruction =
100                             new SimpleInstruction(targetInstruction.opcode);
101                        codeAttributeEditor.replaceInstruction(offset,
102                                                               returnInstruction);
103
104                        // Visit the instruction, if required.
105                        if (extraInstructionVisitor != null)
106                        {
107                            extraInstructionVisitor.visitBranchInstruction(clazz, method, codeAttribute, offset, branchInstruction);
108                        }
109
110                        break;
111                }
112            }
113        }
114    }
115}
116