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

TransSequence.java

00001 package edu.ksu.cis.bandera.birc;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.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 edu.ksu.cis.bandera.bir.*;
00036 
00037 import java.util.*;
00038 
00039 /**
00040  * A simple linked list representing a sequence of Transformations.
00041  * <p>
00042  * Note: there is always a dummy cell on the end of the list
00043  * (thus the expression 'new TransSequence()' represents an empty
00044  * sequence). 
00045  * <p>
00046  * This class was designed to handle collapsing sequences of
00047  * nontrivial transformations with guards, so it can record
00048  * the values of variables that appear in guards and update
00049  * them in the guard expression.  For example, collapsing:
00050  * <pre>
00051  * when true do { x := x + 1; } 
00052  * when x > 10 do { y := y * 2; }
00053  * when x > y do { z := 1; } 
00054  * </pre>
00055  * would have to produce:
00056  * <pre>
00057  * when x + 1 > 10 and x + 1 > y * 2 do { x := x + 1; y := y * 2; z := 1; } 
00058  * </pre>
00059  * Now that nontrivial transitions are no longer collapsed, this
00060  * functionality is unused.
00061  */
00062 
00063 public class TransSequence {
00064 
00065     Transformation trans;    // Transformation
00066     TransSequence next;      // Next in chain
00067 
00068     static Hashtable variableBindings = new Hashtable();
00069     static BindingSubstituter substituter = 
00070     new BindingSubstituter(variableBindings);
00071 
00072     /**
00073      * Actions of sequence.
00074      */
00075 
00076     public ActionVector actions() {
00077     ActionVector actions = new ActionVector();
00078     TransSequence seq = this;
00079     while (seq.next != null) {
00080         ActionVector transActions = seq.trans.getActions();
00081         for (int i = 0; i < transActions.size(); i++) 
00082         actions.insertElementAt(transActions.elementAt(i), i);
00083         seq = seq.next;
00084     }
00085     return actions; 
00086     }
00087     /**
00088      * Add transformation to end of sequence.
00089      */
00090 
00091     public TransSequence add(Transformation newTrans) {
00092     TransSequence seq = new TransSequence();
00093     seq.trans = newTrans;
00094     seq.next = this;
00095     return seq;
00096     }
00097     /**
00098      * Does sequence contain location?
00099      */
00100 
00101     public boolean containsLoc(Location loc) {
00102     if (next == null)
00103         return false;
00104     if (loc == trans.getFromLoc())
00105         return true;
00106     return next.containsLoc(loc);
00107     }
00108     public boolean empty() { return next == null; }
00109     /**
00110      * From location of sequence.
00111      */
00112 
00113     public Location fromLoc() {
00114     if (next.next == null)
00115         return trans.getFromLoc();
00116     return next.fromLoc();
00117     }
00118     /**
00119      * Guard of sequence (must be computed from variable bindings).
00120      */
00121 
00122     public Expr guard() {
00123     int count = this.size();
00124     Transformation [] trans = new Transformation[count];
00125     for (TransSequence seq = this; seq.next != null; seq = seq.next) 
00126         trans[--count] = seq.trans;
00127     variableBindings.clear();
00128     Expr guard = null;
00129     for (int i = 0; i < trans.length; i++) {
00130         if (trans[i].getGuard() != null) {
00131         trans[i].getGuard().apply(substituter);
00132         Expr expr = (Expr) substituter.getResult();
00133         guard = (guard == null) ? expr : new AndExpr(guard,expr);
00134         }
00135         updateBindings(trans[i]);
00136     }
00137     return guard;
00138     }
00139     /**
00140      * Number of transformations in sequence
00141      */
00142 
00143     public int size() {
00144     int result = 0;
00145     for (TransSequence seq = this; seq.next != null; seq = seq.next) 
00146         result++;
00147     return result;
00148     }
00149     /**
00150      * To location of sequence.
00151      */
00152 
00153     public Location toLoc() {
00154     return trans.getToLoc();
00155     }
00156     void updateBindings(Transformation trans) {
00157     ActionVector actions = trans.getActions();
00158     if (actions != null) 
00159         for (int j = 0; j < actions.size(); j++) 
00160         if (actions.elementAt(j) instanceof AssignAction) {
00161             AssignAction assign = 
00162             (AssignAction) actions.elementAt(j);
00163             assign.getRhs().apply(substituter);
00164             variableBindings.put(assign.getLhs(), 
00165                      substituter.getResult());
00166         }
00167     }
00168 }

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