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.info;
22:
23: import proguard.classfile.*;
24: import proguard.classfile.attribute.*;
25: import proguard.classfile.attribute.visitor.AttributeVisitor;
26: import proguard.classfile.instruction.*;
27: import proguard.classfile.instruction.visitor.InstructionVisitor;
28: import 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: */
36: public class VariableUsageMarker extends SimplifiedVisitor implements
37: AttributeVisitor, InstructionVisitor {
38: private boolean[] variableUsed = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE];
39:
40: /**
41: * Returns whether the given variable has been marked as being used.
42: */
43: public boolean isVariableUsed(int variableIndex) {
44: return variableUsed[variableIndex];
45: }
46:
47: // Implementations for AttributeVisitor.
48:
49: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
50: }
51:
52: public void visitCodeAttribute(Clazz clazz, Method method,
53: CodeAttribute codeAttribute) {
54: int maxLocals = codeAttribute.u2maxLocals;
55:
56: // Try to reuse the previous array.
57: if (variableUsed.length < maxLocals) {
58: variableUsed = new boolean[maxLocals];
59: } else {
60: for (int index = 0; index < maxLocals; index++) {
61: variableUsed[index] = false;
62: }
63: }
64:
65: codeAttribute.instructionsAccept(clazz, method, this );
66: }
67:
68: // Implementations for InstructionVisitor.
69:
70: public void visitAnyInstruction(Clazz clazz, Method method,
71: CodeAttribute codeAttribute, int offset,
72: Instruction instruction) {
73: }
74:
75: public void visitVariableInstruction(Clazz clazz, Method method,
76: CodeAttribute codeAttribute, int offset,
77: VariableInstruction variableInstruction) {
78: // Mark the variable.
79: variableUsed[variableInstruction.variableIndex] = true;
80:
81: // Account for Category 2 instructions, which take up two entries.
82: if (variableInstruction.isCategory2()) {
83: variableUsed[variableInstruction.variableIndex + 1] = true;
84: }
85: }
86: }
|