Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

KSUOptimizing.java

00001 package edu.ksu.cis.bandera.prog;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   Hongjun Zheng (zheng@cis.ksu.edu)      *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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     // Assign each local to a group, and set that group's color count to 0.
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     // Assign colors to the parameter locals.
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     // Call the graph colorer.
00096     FastColorer.assignColorsToLocals(body, localToGroup, localToColor, groupToColorCount);
00097 
00098     // Map each local to a new local.
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     // Go through all valueBoxes of this method and perform changes
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     // Assign each local to a group, and set that group's color count to 0.
00149     {
00150         Iterator localIt = body.getLocals().iterator();
00151         while (localIt.hasNext()) {
00152             Local l = (Local) localIt.next();
00153 
00154             //******* added by Hongjunn 1/26/00
00155             //if (l.toString().indexOf("JJJCTEMP$")>=0)
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     // Assign colors to the parameter locals.
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                 //******** added by Hongjun 1/26/00
00176                 //if (l.toString().indexOf("JJJCTEMP$")>=0)
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     // Call the graph colorer.
00189     FastColorer.assignColorsToLocals(body, localToGroup, localToColor, groupToColorCount);
00190 
00191     // Map each local to a new local.
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         //****** this should only clear the JJJCTEMP$ local
00199         //body.getLocals().clear();
00200 
00201         //System.out.println("locals in body before clear: " + body.getLocals());
00202         clearJJJCTemp(body.getLocals());
00203 
00204         //System.out.println("locals in body after clear: " + body.getLocals());
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                 //****** if statement is added by Hongjun 1/26/00
00220                 if (!body.getLocals().contains(newLocal))
00221                     body.getLocals().add(newLocal);
00222             }
00223 
00224             // System.out.println("original local : " + original + "   -->  new local : " + newLocal);
00225             //System.out.println("toString: " + original.toString() + "   toBriefString() : " + original.toBriefString());
00226 
00227             //added by Hongjun 1/26/00
00228             if ((original.toString().indexOf("JJJCTEMP$") >= 0)
00229                     || (original.toString().indexOf("SLABS$") >= 0)) {
00230                 if (!original.toString().endsWith("JJJCTEMP$0")) {
00231                     //System.out.println("original: " + original + "   -->  new local : " + newLocal);
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     //      System.out.println("locals in body after adds: " + body.getLocals());
00246 
00247     // Go through all valueBoxes of this method and perform changes
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                     //******** added by Hongjun 1/26/00
00259                     //Local newLoc = (Local) localToNewLocal.get(l);
00260                     //if (newLoc !=null)
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 }

Generated at Thu Feb 7 06:49:06 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001