00001 package edu.ksu.cis.bandera.prog;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.util.*;
00036 import ca.mcgill.sable.soot.*;
00037 import ca.mcgill.sable.soot.jimple.*;
00038 import edu.ksu.cis.bandera.annotation.*;
00039 import edu.ksu.cis.bandera.jjjc.*;
00040 import java.util.Hashtable;
00041 import edu.ksu.cis.bandera.jext.*;
00042
00043 public class KSUOptimizing
00044 {
00045 private static Hashtable methodLocalPackingTable;
00046 public static void clearJJJCTemp(List localList) {
00047 List tempList = new ArrayList();
00048 Iterator locIt = localList.iterator();
00049 while (locIt.hasNext()) {
00050 Local loc = (Local) locIt.next();
00051 if ((loc.toString().indexOf("JJJCTEMP$") >= 0)
00052 || (loc.toString().indexOf("SLABS$") >= 0)){
00053 if (!loc.toString().endsWith("JJJCTEMP$0"))
00054 tempList.add(loc);
00055 }
00056 }
00057 localList.removeAll(tempList);
00058 }
00059 public static void packLocalsForBody(SootMethod sootMethod) {
00060 StmtBody body = (StmtBody) sootMethod.getBody(Jimple.v());
00061 Map localToGroup = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00062 Map groupToColorCount = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00063 Map localToColor = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00064 Map localToNewLocal;
00065
00066
00067 {
00068 Iterator localIt = body.getLocals().iterator();
00069 while (localIt.hasNext()) {
00070 Local l = (Local) localIt.next();
00071 Object g = l.getType();
00072 localToGroup.put(l, g);
00073 if (!groupToColorCount.containsKey(g)) {
00074 groupToColorCount.put(g, new Integer(0));
00075 }
00076 }
00077 }
00078
00079
00080 {
00081 Iterator codeIt = body.getStmtList().iterator();
00082 while (codeIt.hasNext()) {
00083 Stmt s = (Stmt) codeIt.next();
00084 if (s instanceof IdentityStmt && ((IdentityStmt) s).getLeftOp() instanceof Local) {
00085 Local l = (Local) ((IdentityStmt) s).getLeftOp();
00086 Object group = localToGroup.get(l);
00087 int count = ((Integer) groupToColorCount.get(group)).intValue();
00088 localToColor.put(l, new Integer(count));
00089 count++;
00090 groupToColorCount.put(group, new Integer(count));
00091 }
00092 }
00093 }
00094
00095
00096 FastColorer.assignColorsToLocals(body, localToGroup, localToColor, groupToColorCount);
00097
00098
00099 {
00100 List originalLocals = new ArrayList();
00101 localToNewLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00102 Map groupIntToLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00103 originalLocals.addAll(body.getLocals());
00104 body.getLocals().clear();
00105 Iterator localIt = originalLocals.iterator();
00106 while (localIt.hasNext()) {
00107 Local original = (Local) localIt.next();
00108 Object group = localToGroup.get(original);
00109 int color = ((Integer) localToColor.get(original)).intValue();
00110 GroupIntPair pair = new GroupIntPair(group, color);
00111 Local newLocal;
00112 if (groupIntToLocal.containsKey(pair))
00113 newLocal = (Local) groupIntToLocal.get(pair);
00114 else {
00115 newLocal = new Local(original.getName(), (Type) group);
00116 groupIntToLocal.put(pair, newLocal);
00117 body.getLocals().add(newLocal);
00118 }
00119 methodLocalPackingTable.put(new LocalExpr(sootMethod, original), new LocalExpr(sootMethod, newLocal));
00120 localToNewLocal.put(original, newLocal);
00121 }
00122 }
00123
00124
00125
00126 {
00127 Iterator codeIt = body.getStmtList().iterator();
00128 while (codeIt.hasNext()) {
00129 Stmt s = (Stmt) codeIt.next();
00130 Iterator boxIt = s.getUseAndDefBoxes().iterator();
00131 while (boxIt.hasNext()) {
00132 ValueBox box = (ValueBox) boxIt.next();
00133 if (box.getValue() instanceof Local) {
00134 Local l = (Local) box.getValue();
00135 box.setValue((Local) localToNewLocal.get(l));
00136 }
00137 }
00138 }
00139 }
00140 }
00141 private static void packLocalsForBody2(SootMethod sootMethod) {
00142 StmtBody body = (StmtBody) sootMethod.getBody(Jimple.v());
00143 Map localToGroup = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00144 Map groupToColorCount = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00145 Map localToColor = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00146 Hashtable localToNewLocal;
00147
00148
00149 {
00150 Iterator localIt = body.getLocals().iterator();
00151 while (localIt.hasNext()) {
00152 Local l = (Local) localIt.next();
00153
00154
00155
00156 {
00157 Object g = l.getType();
00158 localToGroup.put(l, g);
00159 if (!groupToColorCount.containsKey(g)) {
00160 groupToColorCount.put(g, new Integer(0));
00161 }
00162 }
00163 }
00164 }
00165
00166
00167 {
00168 Iterator codeIt = body.getStmtList().iterator();
00169 while (codeIt.hasNext()) {
00170 Stmt s = (Stmt) codeIt.next();
00171 if (s instanceof IdentityStmt && ((IdentityStmt) s).getLeftOp() instanceof Local) {
00172 Local l = (Local) ((IdentityStmt) s).getLeftOp();
00173
00174
00175
00176
00177 {
00178 Object group = localToGroup.get(l);
00179 int count = ((Integer) groupToColorCount.get(group)).intValue();
00180 localToColor.put(l, new Integer(count));
00181 count++;
00182 groupToColorCount.put(group, new Integer(count));
00183 }
00184 }
00185 }
00186 }
00187
00188
00189 FastColorer.assignColorsToLocals(body, localToGroup, localToColor, groupToColorCount);
00190
00191
00192 {
00193 List originalLocals = new ArrayList();
00194 localToNewLocal = new Hashtable(body.getLocalCount() * 2 + 1, 0.7f);
00195 Map groupIntToLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
00196 originalLocals.addAll(body.getLocals());
00197
00198
00199
00200
00201
00202 clearJJJCTemp(body.getLocals());
00203
00204
00205
00206 Iterator localIt = originalLocals.iterator();
00207 while (localIt.hasNext()) {
00208 Local original = (Local) localIt.next();
00209 Object group = localToGroup.get(original);
00210 int color = ((Integer) localToColor.get(original)).intValue();
00211 GroupIntPair pair = new GroupIntPair(group, color);
00212 Local newLocal;
00213 if (groupIntToLocal.containsKey(pair))
00214 newLocal = (Local) groupIntToLocal.get(pair);
00215 else {
00216 newLocal = new Local(original.getName(), (Type) group);
00217 groupIntToLocal.put(pair, newLocal);
00218
00219
00220 if (!body.getLocals().contains(newLocal))
00221 body.getLocals().add(newLocal);
00222 }
00223
00224
00225
00226
00227
00228 if ((original.toString().indexOf("JJJCTEMP$") >= 0)
00229 || (original.toString().indexOf("SLABS$") >= 0)) {
00230 if (!original.toString().endsWith("JJJCTEMP$0")) {
00231
00232 localToNewLocal.put(original, newLocal);
00233 methodLocalPackingTable.put(new LocalExpr(sootMethod, original), new LocalExpr(sootMethod, newLocal));
00234 } else {
00235 localToNewLocal.put(original, original);
00236 methodLocalPackingTable.put(new LocalExpr(sootMethod, original), new LocalExpr(sootMethod, original));
00237 }
00238 } else {
00239 localToNewLocal.put(original, original);
00240 methodLocalPackingTable.put(new LocalExpr(sootMethod, original), new LocalExpr(sootMethod, original));
00241 }
00242 }
00243 }
00244
00245
00246
00247
00248 {
00249 Iterator codeIt = body.getStmtList().iterator();
00250 while (codeIt.hasNext()) {
00251 Stmt s = (Stmt) codeIt.next();
00252 Iterator boxIt = s.getUseAndDefBoxes().iterator();
00253 while (boxIt.hasNext()) {
00254 ValueBox box = (ValueBox) boxIt.next();
00255 if (box.getValue() instanceof Local) {
00256 Local l = (Local) box.getValue();
00257
00258
00259
00260
00261 box.setValue((Local) localToNewLocal.get(l));
00262 }
00263 }
00264 }
00265 }
00266 }
00267 public static void packLocalsForClasses(SootClass classes[]) {
00268 methodLocalPackingTable = new Hashtable();
00269 for (int j = 0; j < classes.length; j++) {
00270 SootClass sootClass = classes[j];
00271 List methodsList = sootClass.getMethods();
00272 Object methods[] = methodsList.toArray();
00273 SootMethod sootMethod;
00274 for (int i = 0; i < methods.length; i++) {
00275 sootMethod = (SootMethod) methods[i];
00276 packLocalsForBody2(sootMethod);
00277 }
00278 }
00279 AnnotationManager am = CompilationManager.getAnnotationManager();
00280 am.setLocalPackingTable(methodLocalPackingTable);
00281 }
00282 }