1 19 20 25 26 27 28 29 30 31 package soot.toolkits.scalar; 32 import soot.options.*; 33 34 import soot.*; 35 import soot.util.*; 36 import java.util.*; 37 import soot.jimple.*; 38 39 40 41 42 62 public class LocalPacker extends BodyTransformer 63 { 64 public LocalPacker( Singletons.Global g ) {} 65 public static LocalPacker v() { return G.v().soot_toolkits_scalar_LocalPacker(); } 66 67 protected void internalTransform(Body body, String phaseName, Map options) 68 { 69 boolean isUnsplit = PhaseOptions.getBoolean(options, "unsplit-original-locals"); 70 71 if(Options.v().verbose()) 72 G.v().out.println("[" + body.getMethod().getName() + "] Packing locals..."); 73 74 Map localToGroup = new DeterministicHashMap(body.getLocalCount() * 2 + 1, 0.7f); 75 79 Map groupToColorCount = new HashMap(body.getLocalCount() * 2 + 1, 0.7f); 80 Map localToColor = new HashMap(body.getLocalCount() * 2 + 1, 0.7f); 81 Map localToNewLocal; 82 83 { 85 Iterator localIt = body.getLocals().iterator(); 86 87 while(localIt.hasNext()) 88 { 89 Local l = (Local) localIt.next(); 90 Object g = l.getType(); 91 92 localToGroup.put(l, g); 93 94 if(!groupToColorCount.containsKey(g)) 95 { 96 groupToColorCount.put(g, new Integer (0)); 97 } 98 } 99 } 100 101 { 103 Iterator codeIt = body.getUnits().iterator(); 104 105 while(codeIt.hasNext()) 106 { 107 Unit s = (Unit) codeIt.next(); 108 109 if(s instanceof IdentityUnit && 110 ((IdentityUnit) s).getLeftOp() instanceof Local) 111 { 112 Local l = (Local) ((IdentityUnit) s).getLeftOp(); 113 114 Object group = localToGroup.get(l); 115 int count = ((Integer ) groupToColorCount.get(group)).intValue(); 116 117 localToColor.put(l, new Integer (count)); 118 119 count++; 120 121 groupToColorCount.put(group, new Integer (count)); 122 } 123 } 124 } 125 126 if(isUnsplit) 128 FastColorer.unsplitAssignColorsToLocals(body, localToGroup, 129 localToColor, groupToColorCount); 130 else 131 FastColorer.assignColorsToLocals(body, localToGroup, 132 localToColor, groupToColorCount); 133 134 135 { 137 List originalLocals = new ArrayList(); 138 localToNewLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f); 139 Map groupIntToLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f); 140 141 originalLocals.addAll(body.getLocals()); 142 body.getLocals().clear(); 143 144 Iterator localIt = originalLocals.iterator(); 145 146 while(localIt.hasNext()) 147 { 148 Local original = (Local) localIt.next(); 149 150 Object group = localToGroup.get(original); 151 int color = ((Integer ) localToColor.get(original)).intValue(); 152 GroupIntPair pair = new GroupIntPair(group, color); 153 154 Local newLocal; 155 156 if(groupIntToLocal.containsKey(pair)) 157 newLocal = (Local) groupIntToLocal.get(pair); 158 else { 159 newLocal = (Local) original.clone(); 160 newLocal.setType((Type) group); 161 162 168 int signIndex = newLocal.getName().indexOf("#"); 169 170 if(signIndex != -1) 171 newLocal.setName(newLocal.getName().substring(0, signIndex)); 172 173 groupIntToLocal.put(pair, newLocal); 174 body.getLocals().add(newLocal); 175 } 176 177 localToNewLocal.put(original, newLocal); 178 } 179 } 180 181 182 { 184 Iterator codeIt = body.getUnits().iterator(); 185 186 while(codeIt.hasNext()) 187 { 188 Unit s = (Unit) codeIt.next(); 189 190 Iterator boxIt; 191 boxIt = s.getUseBoxes().iterator(); 192 while(boxIt.hasNext()) 193 { 194 ValueBox box = (ValueBox) boxIt.next(); 195 196 if(box.getValue() instanceof Local) 197 { 198 Local l = (Local) box.getValue(); 199 box.setValue((Local) localToNewLocal.get(l)); 200 } 201 } 202 boxIt = s.getDefBoxes().iterator(); 203 while(boxIt.hasNext()) 204 { 205 ValueBox box = (ValueBox) boxIt.next(); 206 207 if(box.getValue() instanceof Local) 208 { 209 Local l = (Local) box.getValue(); 210 box.setValue((Local) localToNewLocal.get(l)); 211 } 212 } 213 } 214 } 215 } 216 } 217 218 | Popular Tags |