00001 package edu.ksu.cis.bandera.prog; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * Bandera, a Java(TM) analysis and transformation toolkit * 00005 * Copyright (C) 1998, 1999 Shawn Laubach (laubach@acm.org) * 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 //BasicBlock.java 00036 import ca.mcgill.sable.soot.jimple.*; 00037 import java.util.*; 00038 00039 /** 00040 * Holds the basic block and some control flow information. The 00041 * structures are the name and a list of statements. It also keeps 00042 * two other list of statements to keep track of predecessor and 00043 * successors. 00044 * 00045 * @author <a href="mailto:laubach@cis.ksu.edu">Shawn Laubach</a> 00046 * 00047 * @version 0.1 00048 */ 00049 public class BasicBlock { 00050 /** 00051 * Name of the block. 00052 */ 00053 protected Index name; 00054 00055 /** 00056 * List of predecessors. 00057 */ 00058 protected List preds; 00059 00060 /** 00061 * List of statements in the block. 00062 */ 00063 protected ca.mcgill.sable.util.List stmts; 00064 00065 /** 00066 * List of successors in the block. 00067 */ 00068 protected List succs; 00069 protected List goOn; 00070 /** 00071 * Constructs a new basic block with index n. 00072 * 00073 * @param n the name of the basic block. 00074 */ 00075 public BasicBlock(Index n) { 00076 name = n; 00077 preds = new ArrayList(); 00078 stmts = new ca.mcgill.sable.util.ArrayList(); 00079 succs = new ArrayList(); 00080 } 00081 /** 00082 * As a predecessor to the block. 00083 * 00084 * @param pred the name of the predecessor. 00085 */ 00086 public void addPreds(Index pred) 00087 { 00088 if (!preds.contains(pred)) 00089 preds.add(pred); 00090 } 00091 /** 00092 * Adds a statement to the block. 00093 * 00094 * @param stmt the statement to add. 00095 */ 00096 public void addStmt(Stmt stmt) 00097 { 00098 stmts.add(stmt); 00099 } 00100 /** 00101 * Adds a successor to the block. 00102 * 00103 * @param succ the successor name. 00104 */ 00105 public void addSuccs(Index succ) 00106 { 00107 succs.add(succ); 00108 } 00109 /** 00110 * Changes the name of an index to another name in the predecessor. 00111 * 00112 * @param from the index to change. 00113 * @param to the index to change to. 00114 */ 00115 public void changePreds(Index from, List to) 00116 { 00117 preds.remove(from); 00118 preds.addAll(to); 00119 } 00120 /** 00121 * Changes the name of an index to another name in the successors. 00122 * 00123 * @param from the index to change. 00124 * @param to the index to change to. 00125 */ 00126 public void changeSuccs(Index from, Index to) { 00127 while (succs.indexOf(from) != -1) 00128 succs.set(succs.indexOf(from), to); 00129 } 00130 /** 00131 * Gets the list of statements. 00132 * 00133 * @return The list of statements. 00134 */ 00135 public ca.mcgill.sable.util.List get() { 00136 return stmts; 00137 } 00138 /** 00139 * Get the ith statement from the list. 00140 * 00141 * @param i the position of the statement to get. 00142 * 00143 * @return The statement at the ith position or null if the position 00144 * is out of the range of statements. 00145 */ 00146 public Stmt get(int i) 00147 { 00148 if (stmts.size() > i) 00149 return (Stmt)stmts.get(i); 00150 else 00151 return null; 00152 } 00153 /** 00154 * Insert the method's description here. 00155 * Creation date: (4/13/00 2:25:06 PM) 00156 * @return ca.mcgill.sable.util.List 00157 */ 00158 public List getGoOn() { 00159 return goOn; 00160 } 00161 /** 00162 * Gets the index of a basic block. 00163 * 00164 * @return The index of the block. 00165 */ 00166 public Index getIndex() 00167 { 00168 return name; 00169 } 00170 /** 00171 * Gets the list of predecessors. 00172 * 00173 * @return The list of predecessors. 00174 */ 00175 public List getPreds() 00176 { 00177 return preds; 00178 } 00179 /** 00180 * Gets the ith predecessor. 00181 * 00182 * @param i the position of the predecessor. 00183 * 00184 * @return The index at position i unless i is out of bounds and 00185 * then null. 00186 */ 00187 public Index getPreds(int i) 00188 { 00189 if (succs.size() > i) 00190 return (Index)preds.get(i); 00191 else 00192 return null; 00193 } 00194 /** 00195 * Gets the list of successors. 00196 * 00197 * @return The list of successors. 00198 */ 00199 public List getSuccs() 00200 { 00201 return succs; 00202 } 00203 /** 00204 * Gets the ith successor. 00205 * 00206 * @param i the position of the successor. 00207 * 00208 * @return The index at position i unless i is out of bounds and 00209 * then null. 00210 */ 00211 public Index getSuccs(int i) 00212 { 00213 if (succs.size() > i) 00214 return (Index)succs.get(i); 00215 else 00216 return null; 00217 } 00218 /** 00219 * Insert the method's description here. 00220 * Creation date: (4/13/00 2:25:06 PM) 00221 * @param newGoOn ca.mcgill.sable.util.List 00222 */ 00223 public void setGoOn(List newGoOn) { 00224 goOn = newGoOn; 00225 } 00226 /** 00227 * Returns the number of statements. 00228 * 00229 * @return The number of statements. 00230 */ 00231 public int size() 00232 { 00233 return stmts.size(); 00234 } 00235 public String toString() 00236 { 00237 int i; 00238 String s = preds + "->\n" + name + ":\n"; 00239 00240 for (i = 0; i < stmts.size(); i++) 00241 s += stmts.get(i) + "\n"; 00242 00243 s += "-> " + succs + "\n"; 00244 00245 return s; 00246 } 00247 }