001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 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 final static Type OBJECT_TYPE = Type
052: .getType("Ljava/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 ? 0 : 1;
093: for (int i = 0; i < args.length; i++) {
094: nextLocal += args[i].getSize();
095: }
096: firstLocal = nextLocal;
097: }
098:
099: @Override
100: public void visitVarInsn(final int opcode, final int var) {
101: Type type;
102: switch (opcode) {
103: case Opcodes.LLOAD:
104: case Opcodes.LSTORE:
105: type = Type.LONG_TYPE;
106: break;
107:
108: case Opcodes.DLOAD:
109: case Opcodes.DSTORE:
110: type = Type.DOUBLE_TYPE;
111: break;
112:
113: case Opcodes.FLOAD:
114: case Opcodes.FSTORE:
115: type = Type.FLOAT_TYPE;
116: break;
117:
118: case Opcodes.ILOAD:
119: case Opcodes.ISTORE:
120: type = Type.INT_TYPE;
121: break;
122:
123: case Opcodes.ALOAD:
124: case Opcodes.ASTORE:
125: type = OBJECT_TYPE;
126: break;
127:
128: // case RET:
129: default:
130: type = Type.VOID_TYPE;
131: }
132: mv.visitVarInsn(opcode, remap(var, type));
133: }
134:
135: @Override
136: public void visitIincInsn(final int var, final int increment) {
137: mv.visitIincInsn(remap(var, Type.INT_TYPE), increment);
138: }
139:
140: @Override
141: public void visitMaxs(final int maxStack, final int maxLocals) {
142: mv.visitMaxs(maxStack, nextLocal);
143: }
144:
145: @Override
146: public void visitLocalVariable(final String name,
147: final String desc, final String signature,
148: final Label start, final Label end, final int index) {
149: int size = "J".equals(desc) || "D".equals(desc) ? 2 : 1;
150: int newIndex = remap(index, size);
151: mv.visitLocalVariable(name, desc, signature, start, end,
152: newIndex);
153: }
154:
155: @Override
156: public void visitFrame(final int type, final int nLocal,
157: final Object[] local, final int nStack, final Object[] stack) {
158: if (type != Opcodes.F_NEW) { // uncompressed frame
159: throw new IllegalStateException(
160: "ClassReader.accept() should be called with EXPAND_FRAMES flag");
161: }
162:
163: if (!changed) { // optimization for the case where mapping = identity
164: mv.visitFrame(type, nLocal, local, nStack, stack);
165: return;
166: }
167:
168: // creates a copy of newLocals
169: Object[] oldLocals = new Object[newLocals.length];
170: System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length);
171:
172: // copies types from 'local' to 'newLocals'
173: // 'newLocals' already contains the variables added with 'newLocal'
174:
175: int index = 0; // old local variable index
176: int number = 0; // old local variable number
177: for (; number < nLocal; ++number) {
178: Object t = local[number];
179: int size = t == Opcodes.LONG || t == Opcodes.DOUBLE ? 2 : 1;
180: if (t != Opcodes.TOP) {
181: setFrameLocal(remap(index, size), t);
182: }
183: index += size;
184: }
185:
186: // removes TOP after long and double types as well as trailing TOPs
187:
188: index = 0;
189: number = 0;
190: for (int i = 0; index < newLocals.length; ++i) {
191: Object t = newLocals[index++];
192: if (t != null && t != Opcodes.TOP) {
193: newLocals[i] = t;
194: number = i + 1;
195: if (t == Opcodes.LONG || t == Opcodes.DOUBLE) {
196: index += 1;
197: }
198: } else {
199: newLocals[i] = Opcodes.TOP;
200: }
201: }
202:
203: // visits remapped frame
204: mv.visitFrame(type, number, newLocals, nStack, stack);
205:
206: // restores original value of 'newLocals'
207: newLocals = oldLocals;
208: }
209:
210: // -------------
211:
212: /**
213: * Creates a new local variable of the given type.
214: *
215: * @param type the type of the local variable to be created.
216: * @return the identifier of the newly created local variable.
217: */
218: public int newLocal(final Type type) {
219: Object t;
220: switch (type.getSort()) {
221: case Type.BOOLEAN:
222: case Type.CHAR:
223: case Type.BYTE:
224: case Type.SHORT:
225: case Type.INT:
226: t = Opcodes.INTEGER;
227: break;
228: case Type.FLOAT:
229: t = Opcodes.FLOAT;
230: break;
231: case Type.LONG:
232: t = Opcodes.LONG;
233: break;
234: case Type.DOUBLE:
235: t = Opcodes.DOUBLE;
236: break;
237: case Type.ARRAY:
238: t = type.getDescriptor();
239: break;
240: // case Type.OBJECT:
241: default:
242: t = type.getInternalName();
243: break;
244: }
245: int local = nextLocal;
246: setLocalType(local, type);
247: setFrameLocal(local, t);
248: nextLocal += type.getSize();
249: return local;
250: }
251:
252: /**
253: * Sets the current type of the given local variable. The default
254: * implementation of this method does nothing.
255: *
256: * @param local a local variable identifier, as returned by {@link #newLocal
257: * newLocal()}.
258: * @param type the type of the value being stored in the local variable
259: */
260: protected void setLocalType(final int local, final Type type) {
261: }
262:
263: private void setFrameLocal(final int local, final Object type) {
264: int l = newLocals.length;
265: if (local >= l) {
266: Object[] a = new Object[Math.max(2 * l, local + 1)];
267: System.arraycopy(newLocals, 0, a, 0, l);
268: newLocals = a;
269: }
270: newLocals[local] = type;
271: }
272:
273: private int remap(final int var, final Type type) {
274: if (var < firstLocal) {
275: return var;
276: }
277: int key = 2 * var + type.getSize() - 1;
278: int size = mapping.length;
279: if (key >= size) {
280: int[] newMapping = new int[Math.max(2 * size, key + 1)];
281: System.arraycopy(mapping, 0, newMapping, 0, size);
282: mapping = newMapping;
283: }
284: int value = mapping[key];
285: if (value == 0) {
286: value = nextLocal + 1;
287: mapping[key] = value;
288: setLocalType(nextLocal, type);
289: nextLocal += type.getSize();
290: }
291: if (value - 1 != var) {
292: changed = true;
293: }
294: return value - 1;
295: }
296:
297: private int remap(final int var, final int size) {
298: if (var < firstLocal || !changed) {
299: return var;
300: }
301: int key = 2 * var + size - 1;
302: int value = key < mapping.length ? mapping[key] : 0;
303: if (value == 0) {
304: throw new IllegalStateException("Unknown local variable "
305: + var);
306: }
307: return value - 1;
308: }
309: }
|