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

BackEdgeAnalysis.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.prog.Index;
00040 import edu.ksu.cis.bandera.util.GenericSet;
00041 
00042 public class BackEdgeAnalysis
00043 {
00044   private List backEdgesList;
00045   private StmtList stmtList;
00046   private StmtGraph stmtGraph;
00047 
00048   public BackEdgeAnalysis(StmtList statementsList, StmtGraph sg)
00049     {
00050       stmtList = statementsList;
00051       stmtGraph =sg;
00052 
00053       backEdgeAnalysis();
00054     }
00055   private void backEdgeAnalysis()
00056     {
00057 
00058       DomAnalysis da = new DomAnalysis(stmtList, stmtGraph);
00059       Map stmtToDoms = da.stmtToDominatorsMap();
00060 
00061       backEdgesList = new ArrayList();
00062 
00063       Iterator keyStmtIt = stmtToDoms.keySet().iterator();
00064       while (keyStmtIt.hasNext())
00065     {
00066       Stmt keyStmt = (Stmt) keyStmtIt.next();
00067       Index stmtIndex = new Index(stmtList.indexOf(keyStmt));
00068 
00069       List succsOfKeyStmt = stmtGraph.getSuccsOf(keyStmt);
00070       GenericSet succSet = stmtListToIndexSet(succsOfKeyStmt);
00071 
00072       GenericSet dominators = (GenericSet) stmtToDoms.get(keyStmt);
00073       for (int i = 0; i < dominators.size(); i++)
00074         {   
00075           Index domIndex = (Index) dominators.setRef(i);
00076           if (succSet.contains(domIndex))
00077         {
00078           Edge backEdge = new Edge(stmtIndex, domIndex);
00079           List natLoop = naturalLoop(stmtIndex, domIndex);
00080           backEdge.setNaturalLoop(natLoop);
00081 
00082           //this if should be refined further
00083           //if (stmtIndex.index() > domIndex.index())
00084           //backEdge.setIndefiniteLoop();
00085 
00086           backEdgesList.add(backEdge);
00087         }
00088         }    
00089     }
00090     }
00091   public List backEdgeList()
00092     {
00093       return backEdgesList;
00094     }
00095   private List naturalLoop(Index fromIndex, Index toIndex)
00096     {
00097       List loop = new ArrayList();
00098 
00099       loop.add(fromIndex);
00100       loop.add(toIndex);
00101 
00102       if (fromIndex.equals(toIndex)) return loop;
00103 
00104       LinkedList stack = new LinkedList();
00105       stack.addFirst(fromIndex);
00106 
00107       while (!stack.isEmpty())
00108     {
00109       Index currentIndex = (Index) stack.removeFirst();
00110       Stmt currentStmt = (Stmt) stmtList.get(currentIndex.index());
00111       List predsList = stmtGraph.getPredsOf(currentStmt);
00112       Iterator predsIt = predsList.iterator();
00113       while (predsIt.hasNext())
00114         {
00115           Index predIndex = new Index(stmtList.indexOf(predsIt.next()));
00116           if (!loop.contains(predIndex))
00117         {
00118           loop.add(predIndex);
00119           stack.addLast(predIndex);
00120         }
00121         }
00122     }
00123 
00124       return loop;
00125     }
00126  private GenericSet stmtListToIndexSet(List succList)
00127     {
00128       GenericSet returnSet = new GenericSet();
00129 
00130       for (int i = 0; i<succList.size(); i++)
00131     {
00132       Stmt s = (Stmt) succList.get(i);
00133       Index sIndex = new Index(stmtList.indexOf(s));
00134       returnSet.addElemToSet(sIndex);
00135     }
00136       return returnSet;
00137     }
00138 }

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