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.info;
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.SimplifiedVisitor;
29
30/**
31 * This AttributeVisitor marks the local variables that are used in the code
32 * attributes that it visits.
33 *
34 * @author Eric Lafortune
35 */
36public class VariableUsageMarker
37extends      SimplifiedVisitor
38implements   AttributeVisitor,
39             InstructionVisitor
40{
41    private boolean[] variableUsed = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE];
42
43
44    /**
45     * Returns whether the given variable has been marked as being used.
46     */
47    public boolean isVariableUsed(int variableIndex)
48    {
49        return variableUsed[variableIndex];
50    }
51
52
53    // Implementations for AttributeVisitor.
54
55    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
56
57
58    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
59    {
60        int maxLocals = codeAttribute.u2maxLocals;
61
62        // Try to reuse the previous array.
63        if (variableUsed.length < maxLocals)
64        {
65            variableUsed = new boolean[maxLocals];
66        }
67        else
68        {
69            for (int index = 0; index < maxLocals; index++)
70            {
71                variableUsed[index] = false;
72            }
73        }
74
75        codeAttribute.instructionsAccept(clazz, method, this);
76    }
77
78
79    // Implementations for InstructionVisitor.
80
81    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
82
83
84    public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
85    {
86        // Mark the variable.
87        variableUsed[variableInstruction.variableIndex] = true;
88
89        // Account for Category 2 instructions, which take up two entries.
90        if (variableInstruction.isCategory2())
91        {
92            variableUsed[variableInstruction.variableIndex + 1] = true;
93        }
94    }
95}
96