001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2007 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.commons;
030:
031: import org.objectweb.asm.Label;
032: import org.objectweb.asm.MethodAdapter;
033: import org.objectweb.asm.MethodVisitor;
034: import org.objectweb.asm.Opcodes;
035: import org.objectweb.asm.Type;
036:
037: /**
038: * A {@link MethodAdapter} that renumbers local variables in their order of
039: * appearance. This adapter allows one to easily add new local variables to a
040: * method. It may be used by inheriting from this class, but the preferred way
041: * of using it is via delegation: the next visitor in the chain can indeed add
042: * new locals when needed by calling {@link #newLocal} on this adapter (this
043: * requires a reference back to this {@link LocalVariablesSorter}).
044: *
045: * @author Chris Nokleberg
046: * @author Eugene Kuleshov
047: * @author Eric Bruneton
048: */
049: public class LocalVariablesSorter extends MethodAdapter {
050:
051: private static final Type OBJECT_TYPE = Type
052: .getObjectType("java/lang/Object");
053:
054: /**
055: * Mapping from old to new local variable indexes. A local variable at index
056: * i of size 1 is remapped to 'mapping[2*i]', while a local variable at
057: * index i of size 2 is remapped to 'mapping[2*i+1]'.
058: */
059: private int[] mapping = new int[40];
060:
061: /**
062: * Array used to store stack map local variable types after remapping.
063: */
064: private Object[] newLocals = new Object[20];
065:
066: /**
067: * Index of the first local variable, after formal parameters.
068: */
069: protected final int firstLocal;
070:
071: /**
072: * Index of the next local variable to be created by {@link #newLocal}.
073: */
074: protected int nextLocal;
075:
076: /**
077: * Indicates if at least one local variable has moved due to remapping.
078: */
079: private boolean changed;
080:
081: /**
082: * Creates a new {@link LocalVariablesSorter}.
083: *
084: * @param access access flags of the adapted method.
085: * @param desc the method's descriptor (see {@link Type Type}).
086: * @param mv the method visitor to which this adapter delegates calls.
087: */
088: public LocalVariablesSorter(final int access, final String desc,
089: final MethodVisitor mv) {
090: super (mv);
091: Type[] args = Type.getArgumentTypes(desc);
092: nextLocal = (Opcodes.ACC_STATIC & access) == 0 ? 1 : 0;
093: for (int i = 0; i < args.length; i++) {
094: nextLocal += args[i].getSize();
095: }
096: firstLocal = nextLocal;
097: }
098:
099: public void visitVarInsn(final int opcode, final int var) {
100: Type type;
101: switch (opcode) {
102: case Opcodes.LLOAD:
103: case Opcodes.LSTORE:
104: type = Type.LONG_TYPE;
105: break;
106:
107: case Opcodes.DLOAD:
108: case Opcodes.DSTORE:
109: type = Type.DOUBLE_TYPE;
110: break;
111:
112: case Opcodes.FLOAD:
113: case Opcodes.FSTORE:
114: type = Type.FLOAT_TYPE;
115: break;
116:
117: case Opcodes.ILOAD:
118: case Opcodes.ISTORE:
119: type = Type.INT_TYPE;
120: break;
121:
122: case Opcodes.ALOAD:
123: case Opcodes.ASTORE:
124: type = OBJECT_TYPE;
125: break;
126:
127: // case RET:
128: default:
129: type = Type.VOID_TYPE;
130: }
131: mv.visitVarInsn(opcode, remap(var, type));
132: }
133:
134: public void visitIincInsn(final int var, final int increment) {
135: mv.visitIincInsn(remap(var, Type.INT_TYPE), increment);
136: }
137:
138: public void visitMaxs(final int maxStack, final int maxLocals) {
139: mv.visitMaxs(maxStack, nextLocal);
140: }
141:
142: public void visitLocalVariable(final String name,
143: final String desc, final String signature,
144: final Label start, final Label end, final int index) {
145: int newIndex = remap(index, Type.getType(desc));
146: mv.visitLocalVariable(name, desc, signature, start, end,
147: newIndex);
148: }
149:
150: public void visitFrame(final int type, final int nLocal,
151: final Object[] local, final int nStack, final Object[] stack) {
152: if (type != Opcodes.F_NEW) { // uncompressed frame
153: throw new IllegalStateException(
154: "ClassReader.accept() should be called with EXPAND_FRAMES flag");
155: }
156:
157: if (!changed) { // optimization for the case where mapping = identity
158: mv.visitFrame(type, nLocal, local, nStack, stack);
159: return;
160: }
161:
162: // creates a copy of newLocals
163: Object[] oldLocals = new Object[newLocals.length];
164: System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length);
165:
166: // copies types from 'local' to 'newLocals'
167: // 'newLocals' already contains the variables added with 'newLocal'
168:
169: int index = 0; // old local variable index
170: int number = 0; // old local variable number
171: for (; number < nLocal; ++number) {
172: Object t = local[number];
173: int size = t == Opcodes.LONG || t == Opcodes.DOUBLE ? 2 : 1;
174: if (t != Opcodes.TOP) {
175: setFrameLocal(remap(index, size), t);
176: }
177: index += size;
178: }
179:
180: // removes TOP after long and double types as well as trailing TOPs
181:
182: index = 0;
183: number = 0;
184: for (int i = 0; index < newLocals.length; ++i) {
185: Object t = newLocals[index++];
186: if (t != null && t != Opcodes.TOP) {
187: newLocals[i] = t;
188: number = i + 1;
189: if (t == Opcodes.LONG || t == Opcodes.DOUBLE) {
190: index += 1;
191: }
192: } else {
193: newLocals[i] = Opcodes.TOP;
194: }
195: }
196:
197: // visits remapped frame
198: mv.visitFrame(type, number, newLocals, nStack, stack);
199:
200: // restores original value of 'newLocals'
201: newLocals = oldLocals;
202: }
203:
204: // -------------
205:
206: /**
207: * Creates a new local variable of the given type.
208: *
209: * @param type the type of the local variable to be created.
210: * @return the identifier of the newly created local variable.
211: */
212: public int newLocal(final Type type) {
213: Object t;
214: switch (type.getSort()) {
215: case Type.BOOLEAN:
216: case Type.CHAR:
217: case Type.BYTE:
218: case Type.SHORT:
219: case Type.INT:
220: t = Opcodes.INTEGER;
221: break;
222: case Type.FLOAT:
223: t = Opcodes.FLOAT;
224: break;
225: case Type.LONG:
226: t = Opcodes.LONG;
227: break;
228: case Type.DOUBLE:
229: t = Opcodes.DOUBLE;
230: break;
231: case Type.ARRAY:
232: t = type.getDescriptor();
233: break;
234: // case Type.OBJECT:
235: default:
236: t = type.getInternalName();
237: break;
238: }
239: int local = nextLocal;
240: nextLocal += type.getSize();
241: setLocalType(local, type);
242: setFrameLocal(local, t);
243: return local;
244: }
245:
246: /**
247: * Sets the current type of the given local variable. The default
248: * implementation of this method does nothing.
249: *
250: * @param local a local variable identifier, as returned by {@link #newLocal
251: * newLocal()}.
252: * @param type the type of the value being stored in the local variable
253: */
254: protected void setLocalType(final int local, final Type type) {
255: }
256:
257: private void setFrameLocal(final int local, final Object type) {
258: int l = newLocals.length;
259: if (local >= l) {
260: Object[] a = new Object[Math.max(2 * l, local + 1)];
261: System.arraycopy(newLocals, 0, a, 0, l);
262: newLocals = a;
263: }
264: newLocals[local] = type;
265: }
266:
267: private int remap(final int var, final Type type) {
268: if (var < firstLocal) {
269: return var;
270: }
271: int key = 2 * var + type.getSize() - 1;
272: int size = mapping.length;
273: if (key >= size) {
274: int[] newMapping = new int[Math.max(2 * size, key + 1)];
275: System.arraycopy(mapping, 0, newMapping, 0, size);
276: mapping = newMapping;
277: }
278: int value = mapping[key];
279: if (value == 0) {
280: value = newLocalMapping(type);
281: setLocalType(value, type);
282: mapping[key] = value + 1;
283: } else {
284: value--;
285: }
286: if (value != var) {
287: changed = true;
288: }
289: return value;
290: }
291:
292: protected int newLocalMapping(final Type type) {
293: int local = nextLocal;
294: nextLocal += type.getSize();
295: return local;
296: }
297:
298: private int remap(final int var, final int size) {
299: if (var < firstLocal || !changed) {
300: return var;
301: }
302: int key = 2 * var + size - 1;
303: int value = key < mapping.length ? mapping[key] : 0;
304: if (value == 0) {
305: throw new IllegalStateException("Unknown local variable "
306: + var);
307: }
308: return value - 1;
309: }
310: }
|