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
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 }