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.drools.asm.commons;
030:
031: import org.drools.asm.Label;
032: import org.drools.asm.MethodAdapter;
033: import org.drools.asm.MethodVisitor;
034: import org.drools.asm.Opcodes;
035: import org.drools.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.
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: final Type[] args = Type.getArgumentTypes(desc);
062: this .nextLocal = ((Opcodes.ACC_STATIC & access) != 0) ? 0 : 1;
063: for (int i = 0; i < args.length; i++) {
064: this .nextLocal += args[i].getSize();
065: }
066: this .firstLocal = this .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: this .mv.visitVarInsn(opcode, remap(var, size));
082: }
083:
084: public void visitIincInsn(final int var, final int increment) {
085: this .mv.visitIincInsn(remap(var, 1), increment);
086: }
087:
088: public void visitMaxs(final int maxStack, final int maxLocals) {
089: this .mv.visitMaxs(maxStack, this .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: final int size = "J".equals(desc) || "D".equals(desc) ? 2 : 1;
096: this .mv.visitLocalVariable(name, desc, signature, start, end,
097: remap(index, size));
098: }
099:
100: // -------------
101:
102: protected int newLocal(final int size) {
103: final int var = this .nextLocal;
104: this .nextLocal += size;
105: return var;
106: }
107:
108: private int remap(final int var, final int size) {
109: if (var < this .firstLocal) {
110: return var;
111: }
112: final int key = 2 * var + size - 1;
113: final int length = this .mapping.length;
114: if (key >= length) {
115: final int[] newMapping = new int[Math.max(2 * length,
116: key + 1)];
117: System.arraycopy(this .mapping, 0, newMapping, 0, length);
118: this .mapping = newMapping;
119: }
120: int value = this .mapping[key];
121: if (value == 0) {
122: value = this .nextLocal + 1;
123: this .mapping[key] = value;
124: this .nextLocal += size;
125: }
126: return value - 1;
127: }
128:
129: }
|