1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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.classfile.editor;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.visitor.AttributeVisitor;
26import proguard.classfile.instruction.*;
27import proguard.classfile.instruction.visitor.InstructionVisitor;
28import proguard.classfile.util.*;
29
30/**
31 * This AttributeVisitor computes and updates the maximum local variable frame
32 * size of the code attributes that it visits. It also cleans up the local
33 * variable tables.
34 *
35 * @author Eric Lafortune
36 */
37public class VariableSizeUpdater
38extends      SimplifiedVisitor
39implements   AttributeVisitor,
40             InstructionVisitor
41{
42    //*
43    private static final boolean DEBUG = false;
44    /*/
45    private static       boolean DEBUG = true;
46    //*/
47
48
49    private VariableCleaner variableCleaner = new VariableCleaner();
50
51
52    // Implementations for AttributeVisitor.
53
54    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
55
56
57    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
58    {
59//        DEBUG =
60//            clazz.getName().equals("abc/Def") &&
61//            method.getName(clazz).equals("abc");
62
63        // The minimum variable size is determined by the arguments.
64        codeAttribute.u2maxLocals =
65            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
66                                                  method.getAccessFlags());
67
68        if (DEBUG)
69        {
70            System.out.println("VariableSizeUpdater: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
71            System.out.println("  Max locals: "+codeAttribute.u2maxLocals+" <- parameters");
72        }
73
74        // Go over all instructions.
75        codeAttribute.instructionsAccept(clazz, method, this);
76
77        // Remove the unused variables of the attributes.
78        variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
79    }
80
81
82    // Implementations for InstructionVisitor.
83
84    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
85
86
87    public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
88    {
89        int variableSize = variableInstruction.variableIndex + 1;
90        if (variableInstruction.isCategory2())
91        {
92            variableSize++;
93        }
94
95        if (codeAttribute.u2maxLocals < variableSize)
96        {
97            codeAttribute.u2maxLocals = variableSize;
98
99            if (DEBUG)
100            {
101                System.out.println("  Max locals: "+codeAttribute.u2maxLocals+" <- "+variableInstruction.toString(offset));
102            }
103        }
104    }
105}
106