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

CFGSkel.java

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 //CFGSkel.java
00036 import java.util.Vector;
00037 import edu.ksu.cis.bandera.util.*;
00038 
00039 /**
00040  * Control flow graph skeleton.  This class represents the flow graph
00041  * and allows a topological search to be performed.
00042  *
00043  * @author <a href="mailto:laubach@cis.ksu.edu">Shawn Laubach</a>
00044  *
00045  * @version 0.1
00046  */
00047 public class CFGSkel
00048 {
00049   /**
00050    * Internal node.  Keeps the marking, the index, and the
00051    * descendents.
00052    */
00053   class node
00054   {
00055     boolean marked;     
00056     Index index;
00057     Vector descendents;
00058 
00059     /**
00060      * Constructs a new node with false marking.
00061      *
00062      * @param i the index to put in the node.
00063      */
00064     node(Index i)
00065     {
00066       index = i;
00067       marked = false;
00068       descendents = new Vector();
00069     }
00070 
00071     /**
00072      * Checks the equality of a node and either another node or an
00073      * index.  This is because the main distinguishing part of a node
00074      * is the index.
00075      */
00076     public boolean equals(Object o)
00077     {
00078       if (o instanceof node)                    // If object is a node
00079         return index.equals(((node) o).index); // check the index
00080       else if (o instanceof Index) // If object is an index
00081         return index.equals(o);                // compare indexes
00082       else                             // else
00083         return false;                  // return false
00084     }
00085 
00086     public String toString()
00087     {
00088       if (marked)
00089         return index + ": * --> " + descendents;
00090       else
00091         return index + ": o --> " + descendents;
00092     }
00093   }
00094 
00095   /**
00096    * The list of nodes that comprise the skeleton.  The first one is
00097    * the head of the skeleton.
00098    */
00099   protected Vector skel;
00100 
00101   /**
00102    * Constructs a new CFG skeleton.  It just creates an empty node
00103    * list.
00104    */
00105   public CFGSkel()
00106   {
00107     skel = new Vector();
00108   }  
00109   /**
00110    * Adds a new index to the skeleton.
00111    *
00112    * @param i the index to add.
00113    */
00114   public void add(Index i)
00115   {
00116     skel.addElement(new node(i));
00117   }  
00118   /**
00119    * Adds a descendent to a node.
00120    *
00121    * @param p the parent node.
00122    * @param c the child node.
00123    */
00124   public void addDescendent(Index p, Index c)
00125   {
00126     int i;
00127     node n;
00128 
00129     i = find(p);
00130     if (i < 0)
00131       {
00132         System.out.println("No index found:  " + p);
00133         return;
00134       }
00135 
00136     n =(node) skel.elementAt(i);
00137 
00138     i = find(c);
00139 
00140     if (i < 0)
00141       {
00142         add(c);
00143         n.descendents.addElement(new Integer(find(c)));
00144       }
00145     else
00146       {
00147         if (n.descendents.indexOf(new Integer(i)) < 0)
00148           n.descendents.addElement(new Integer(i));
00149       }
00150   }  
00151   /**
00152    * Finds the position of a node.
00153    *
00154    * @param i the index to find.
00155    * 
00156    * @return The position of the node.
00157    */
00158   int find(Index i)
00159   {
00160     return skel.indexOf(new node(i));
00161   }  
00162   /**
00163    * Creates a new arc.
00164    *
00165    * @param p the parent (start) of the arc.
00166    * @param c the child of the arc.
00167    */
00168   public void makeArc(Index p, Index c)
00169   {
00170     addDescendent(p, c);
00171   }  
00172   /**
00173    * Makes an arc and sets the mark of the child to mark.
00174    *
00175    * @param p the parent.
00176    * @param c the child.
00177    * @param mark the child's mark.
00178    */
00179   public void makeArc(Index p, Index c, boolean mark)
00180   {
00181     addDescendent(p, c);
00182     mark(c, mark);
00183   }  
00184   /**
00185    * Marks the index.
00186    *
00187    * @param i the index to mark.
00188    */
00189   public void mark(Index i)
00190   {
00191     node n;
00192 
00193     if (i == null)
00194       {
00195         System.out.println("Index is null.");
00196         return;
00197       }
00198     if (find(i) < 0)
00199       System.out.println("Index " + i + " not found.");
00200     else
00201       {
00202         n =(node) skel.elementAt(find(i));
00203         n.marked = true;
00204       }
00205   }  
00206   /**
00207    * Sets the mark of an index to what is passed.
00208    *
00209    * @param i the index.
00210    * @param m the value to set the mark.
00211    */
00212   public void mark(Index i, boolean m)
00213   {
00214     node n;
00215 
00216     if (i == null)
00217       {
00218         System.out.println("Index is null.");
00219         return;
00220       }
00221     if (find(i) < 0)
00222       System.out.println("Index " + i + " not found.");
00223     else
00224       {
00225         n =(node) skel.elementAt(find(i));
00226         n.marked = m;
00227       }
00228   }  
00229   /**
00230    * Gets the next unmarked index using a topological sort.
00231    *
00232    * @return The next unmarked index.
00233    */
00234   public Index next()
00235   {
00236     int i,
00237       j;
00238     Vector q = new Vector();
00239     Vector visit = new Vector();
00240     node n;
00241 
00242     q.addElement(new Integer(0));
00243 
00244     for(i = 0; i < q.size(); i++)
00245       {
00246         n =(node) skel.elementAt(((Integer) q.elementAt(i)).intValue());
00247         if (n.marked == false)
00248           return n.index;
00249         else if (!visit.contains(q.elementAt(i)))
00250           {
00251             visit.addElement(q.elementAt(i));
00252             for(j = 0; j < n.descendents.size(); j++)
00253               q.addElement(n.descendents.elementAt(j));
00254           }
00255       }
00256 
00257     return null;
00258   }  
00259   /**
00260    * Sets the index to the starting point.
00261    *
00262    * @param i the index.
00263    */
00264   public void start(Index i)
00265   {
00266     int ind = find(i);
00267 
00268     if (ind == 0)
00269       unmark(i);
00270     else
00271       {
00272         skel.insertElementAt(new node(i), 0);
00273       }
00274   }  
00275   public String toString()
00276   {
00277     int i;
00278     String s = "";
00279 
00280     for(i = 0; i < skel.size(); i++)
00281       s += i + ":  " + skel.elementAt(i) + "\n";
00282 
00283     return s;
00284   }  
00285   /**
00286    * Unmarks an index.
00287    *
00288    * @param i the index.
00289    */
00290   public void unmark(Index i)
00291   {
00292     node n;
00293 
00294     if (i == null)
00295       {
00296         System.out.println("Index is null.");
00297         return;
00298       }
00299     if (find(i) < 0)
00300       System.out.println("Index " + i + " not found.");
00301     else
00302       {
00303         n =(node) skel.elementAt(find(i));
00304         n.marked = false;
00305       }
00306   }  
00307 }

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