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

AbstractDom.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 
00039 import edu.ksu.cis.bandera.util.GenericSet;
00040 import edu.ksu.cis.bandera.prog.Index;
00041 
00042 public abstract class AbstractDom
00043 {
00044   protected StmtList stmtList;
00045   protected StmtGraph stmtGraph;
00046   protected final static Index specialExitNode = new Index(-2);
00047   protected final static Index STARTNODE = new Index(-1);
00048 
00049   public AbstractDom(StmtList statementsList, StmtGraph sg)
00050     {
00051       stmtList = statementsList;
00052       stmtGraph = sg;
00053     }
00054   protected Map computeImmedDom(Map stmtToSet) 
00055     {
00056       Map stmtToImmediate =  new HashMap(stmtList.size() *2 +1, 0.7f);
00057 
00058       Iterator keyStmtIt = stmtToSet.keySet().iterator();
00059       while (keyStmtIt.hasNext())
00060     {
00061       Stmt keyStmt = (Stmt) keyStmtIt.next();
00062       GenericSet doms = (GenericSet) stmtToSet.get(keyStmt);
00063 
00064       GenericSet workSet = new GenericSet();
00065       workSet = workSet.union(doms);
00066       workSet.remove(new Index(stmtList.indexOf(keyStmt)));
00067       
00068       GenericSet immediate = new GenericSet();
00069       immediate = immediate.union(workSet);
00070 
00071       for (int i=0; i<workSet.size(); i++)
00072         {
00073           Index domIndex = (Index) workSet.setRef(i);
00074           GenericSet domX = (GenericSet) stmtToSet.get((Stmt) stmtList.get(domIndex.index()));
00075           GenericSet domX2 = new GenericSet();
00076           domX2 = domX2.union(domX);
00077           domX2.remove(domIndex);
00078           
00079           GenericSet interDoms = domX2.intersect(immediate);
00080 
00081           if (interDoms.size() != 0) immediate = immediate.difference(interDoms);
00082         }
00083 
00084       if (immediate.size() == 1) 
00085         stmtToImmediate.put(keyStmt, (Index) immediate.setRef(0));
00086       else if (immediate.size() == 0) 
00087         stmtToImmediate.put(keyStmt, STARTNODE);
00088       else
00089         {
00090           System.out.println("immediate dominator and postdominator should be unique or none!");
00091           System.exit(0);
00092         }
00093     }
00094       return stmtToImmediate;
00095     }
00096   protected GenericSet getStartNodes()
00097     {
00098       List heads = stmtGraph.getHeads();
00099       Iterator headIt = heads.iterator();
00100       
00101       GenericSet nodeSet = new GenericSet();
00102 
00103       while (headIt.hasNext())
00104     {
00105       Stmt head = (Stmt) headIt.next();
00106       Index headIndex = new Index(stmtList.indexOf(head));
00107       nodeSet.addElemToSet(headIndex);
00108     }
00109       if (nodeSet.size() == 0) nodeSet.addElemToSet(new Index(0));
00110       return nodeSet;
00111     }
00112   protected GenericSet indexSetWithoutExceptionHandling()
00113     {
00114       LinkedList workList = new LinkedList();
00115       GenericSet startSet = this.getStartNodes();
00116       GenericSet indexSet = new GenericSet();
00117 
00118       for (int i=0; i<startSet.size(); i++)
00119     {
00120       Index nodeIndex = (Index) startSet.setRef(i);
00121       workList.addFirst(nodeIndex);
00122     }
00123 
00124       while (workList.size() != 0)
00125     {
00126       Index nodeIndex = (Index) workList.removeFirst();
00127       indexSet.addElemToSet(nodeIndex);
00128       Stmt nodeStmt = (Stmt) stmtList.get(nodeIndex.index());
00129       List nodeStmtSuccs = stmtGraph.getSuccsOf(nodeStmt);
00130       List newSuccs = removeExceptionCaught(nodeStmtSuccs);
00131       Iterator succIt = newSuccs.iterator();
00132       while (succIt.hasNext())
00133         {
00134           Stmt succStmt = (Stmt) succIt.next();
00135           Index succIndex = new Index(stmtList.indexOf(succStmt));
00136           if (workList.contains(succIndex) || indexSet.contains(succIndex))
00137         {
00138         }
00139           else workList.addLast(succIndex);
00140         }
00141     }
00142       return indexSet;
00143     }
00144   private  boolean isExceptionHandler(Stmt succStmt)
00145     {
00146       boolean isHandler = false;
00147 
00148       Integer succStmtIndex = new Integer(stmtList.indexOf(succStmt));
00149       JimpleBody jimpleBody = (JimpleBody) stmtList.getBody();
00150       Iterator trapIt = jimpleBody.getTraps().iterator();
00151       while(trapIt.hasNext())
00152     {
00153       Trap trap = (Trap) trapIt.next();
00154       Stmt trapHandler = (Stmt) trap.getHandlerUnit();
00155       Integer handlerIndex = new Integer(stmtList.indexOf(trapHandler));
00156       if (succStmtIndex.equals(handlerIndex))
00157         {
00158           isHandler = true;
00159           break;
00160         }
00161     }
00162 
00163       return isHandler;
00164     }
00165   protected List removeExceptionCaught(List actualSuccs)
00166     {
00167       List newSuccs = new ArrayList();
00168       Iterator asuccsIt = actualSuccs.iterator();
00169       while (asuccsIt.hasNext())
00170     {
00171       Stmt succStmt = (Stmt) asuccsIt.next();
00172       if (!isExceptionHandler(succStmt))
00173         newSuccs.add(succStmt);
00174     }
00175 
00176       return newSuccs;
00177     }
00178 }

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