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.jip.commons;
030:
031: import org.objectweb.asm.jip.Label;
032: import org.objectweb.asm.jip.MethodAdapter;
033: import org.objectweb.asm.jip.MethodVisitor;
034: import org.objectweb.asm.jip.Opcodes;
035: import org.objectweb.asm.jip.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.
041: *
042: * @author Chris Nokleberg
043: * @author Eric Bruneton
044: */
045: public class LocalVariablesSorter extends MethodAdapter {
046:
047: /**
048: * Mapping from old to new local variable indexes. A local variable at index
049: * i of size 1 is remapped to 'mapping[2*i]', while a local variable at
050: * index i of size 2 is remapped to 'mapping[2*i+1]'.
051: */
052: private int[] mapping = new int[40];
053:
054: protected final int firstLocal;
055:
056: private int nextLocal;
057:
058: public LocalVariablesSorter(final int access, final String desc,
059: final MethodVisitor mv) {
060: super (mv);
061: Type[] args = Type.getArgumentTypes(desc);
062: nextLocal = ((Opcodes.ACC_STATIC & access) != 0) ? 0 : 1;
063: for (int i = 0; i < args.length; i++) {
064: nextLocal += args[i].getSize();
065: }
066: firstLocal = nextLocal;
067: }
068:
069: public void visitVarInsn(final int opcode, final int var) {
070: int size;
071: switch (opcode) {
072: case Opcodes.LLOAD:
073: case Opcodes.LSTORE:
074: case Opcodes.DLOAD:
075: case Opcodes.DSTORE:
076: size = 2;
077: break;
078: default:
079: size = 1;
080: }
081: mv.visitVarInsn(opcode, remap(var, size));
082: }
083:
084: public void visitIincInsn(final int var, final int increment) {
085: mv.visitIincInsn(remap(var, 1), increment);
086: }
087:
088: public void visitMaxs(final int maxStack, final int maxLocals) {
089: mv.visitMaxs(maxStack, nextLocal);
090: }
091:
092: public void visitLocalVariable(final String name,
093: final String desc, final String signature,
094: final Label start, final Label end, final int index) {
095: mv.visitLocalVariable(name, desc, signature, start, end,
096: remap(index));
097: }
098:
099: // -------------
100:
101: protected int newLocal(final int size) {
102: int var = nextLocal;
103: nextLocal += size;
104: return var;
105: }
106:
107: private int remap(final int var, final int size) {
108: if (var < firstLocal) {
109: return var;
110: }
111: int key = 2 * var + size - 1;
112: int length = mapping.length;
113: if (key >= length) {
114: int[] newMapping = new int[Math.max(2 * length, key + 1)];
115: System.arraycopy(mapping, 0, newMapping, 0, length);
116: mapping = newMapping;
117: }
118: int value = mapping[key];
119: if (value == 0) {
120: value = nextLocal + 1;
121: mapping[key] = value;
122: nextLocal += size;
123: }
124: return value - 1;
125: }
126:
127: private int remap(final int var) {
128: if (var < firstLocal) {
129: return var;
130: }
131: int key = 2 * var;
132: int value = key < mapping.length ? mapping[key] : 0;
133: if (value == 0) {
134: value = key + 1 < mapping.length ? mapping[key + 1] : 0;
135: }
136: if (value == 0) {
137: throw new IllegalStateException("Unknown local variable "
138: + var);
139: }
140: return value - 1;
141: }
142: }
|