1 30 package com.tc.asm.commons; 31 32 import com.tc.asm.Label; 33 import com.tc.asm.MethodAdapter; 34 import com.tc.asm.MethodVisitor; 35 import com.tc.asm.Opcodes; 36 import com.tc.asm.Type; 37 38 46 public class LocalVariablesSorter extends MethodAdapter { 47 48 53 private int[] mapping = new int[40]; 54 55 protected final int firstLocal; 56 57 private int nextLocal; 58 59 public LocalVariablesSorter( 60 final int access, 61 final String desc, 62 final MethodVisitor mv) 63 { 64 super(mv); 65 Type[] args = Type.getArgumentTypes(desc); 66 nextLocal = ((Opcodes.ACC_STATIC & access) != 0) ? 0 : 1; 67 for (int i = 0; i < args.length; i++) { 68 nextLocal += args[i].getSize(); 69 } 70 firstLocal = nextLocal; 71 } 72 73 public void visitVarInsn(final int opcode, final int var) { 74 int size; 75 switch (opcode) { 76 case Opcodes.LLOAD: 77 case Opcodes.LSTORE: 78 case Opcodes.DLOAD: 79 case Opcodes.DSTORE: 80 size = 2; 81 break; 82 default: 83 size = 1; 84 } 85 mv.visitVarInsn(opcode, remap(var, size)); 86 } 87 88 public void visitIincInsn(final int var, final int increment) { 89 mv.visitIincInsn(remap(var, 1), increment); 90 } 91 92 public void visitMaxs(final int maxStack, final int maxLocals) { 93 mv.visitMaxs(maxStack, nextLocal); 94 } 95 96 public void visitLocalVariable( 97 final String name, 98 final String desc, 99 final String signature, 100 final Label start, 101 final Label end, 102 final int index) 103 { 104 int size = "J".equals(desc) || "D".equals(desc) ? 2 : 1; 105 mv.visitLocalVariable(name, desc, signature, start, end, remap(index, size)); 106 } 107 108 110 protected int newLocal(final int size) { 111 int var = nextLocal; 112 nextLocal += size; 113 return var; 114 } 115 116 private int remap(final int var, final int size) { 117 if (var < firstLocal) { 118 return var; 119 } 120 int key = 2 * var + size - 1; 121 int length = mapping.length; 122 if (key >= length) { 123 int[] newMapping = new int[Math.max(2 * length, key + 1)]; 124 System.arraycopy(mapping, 0, newMapping, 0, length); 125 mapping = newMapping; 126 } 127 int value = mapping[key]; 128 if (value == 0) { 129 value = nextLocal + 1; 130 mapping[key] = value; 131 nextLocal += size; 132 } 133 return value - 1; 134 } 135 136 } 137 | Popular Tags |