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

BirPrinter.java

00001 package edu.ksu.cis.bandera.bir;
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 ca.mcgill.sable.util.*;
00036 
00037 import java.io.*;
00038 import java.util.*;
00039 
00040 /**
00041  * Print visitor for a transition system.
00042  * <p>
00043  * To invoke:
00044  * <pre>
00045  * // Parameters 
00046  * TransSystem system = ...;   // the transition system
00047  * PrintWriter out = ...;      // PrintWriter to send to
00048  * BirPrinter.print(system,out);  
00049  * </pre>
00050  * or to send to System.out, just:
00051  * <pre>
00052  * BirPrinter.print(system);  
00053  * </pre>
00054  */
00055 
00056 public class BirPrinter extends AbstractExprSwitch implements BirConstants
00057 {
00058     TransSystem system;
00059     PrintWriter out;
00060     boolean inPredicate = false;
00061 
00062     BirPrinter(TransSystem system, PrintWriter out) {
00063     this.system = system;
00064     this.out = out;
00065     }
00066     public void caseAddExpr(AddExpr expr)
00067     {
00068        translateBinaryOp(expr.getOp1(), expr.getOp2(), "+");
00069     }
00070     public void caseAndExpr(AndExpr expr)
00071     {
00072        translateBinaryOp(expr.getOp1(), expr.getOp2(), "&&");
00073     }
00074     public void caseArrayExpr(ArrayExpr expr) 
00075     {
00076     expr.getArray().apply(this);
00077     out.print("[");
00078     expr.getIndex().apply(this);
00079     out.print("]");
00080     }
00081     public void caseAssertAction(AssertAction assertAction) {
00082     out.print("assert(");
00083     assertAction.getCondition().apply(this);
00084     out.print("); ");
00085     }
00086     public void caseAssignAction(AssignAction assign) 
00087     {
00088     assign.getLhs().apply(this);
00089     out.print(" := ");
00090     assign.getRhs().apply(this);
00091     out.print("; ");
00092     }
00093     public void caseBoolLit(BoolLit expr)
00094     {
00095     out.print(expr.getValue());
00096     }
00097     public void caseChooseExpr(ChooseExpr expr) {
00098     out.print("choose(");
00099     Vector choices = expr.getChoices();
00100     ((Expr)choices.elementAt(0)).apply(this);
00101     for (int i = 1; i < choices.size(); i++) {
00102         out.print(",");
00103         ((Expr)choices.elementAt(i)).apply(this);
00104     }
00105     out.print(")");
00106     }
00107     public void caseConstant(Constant expr)
00108     {
00109     out.print(expr.getName());
00110     }
00111     public void caseDerefExpr(DerefExpr expr)
00112     {
00113     expr.getTarget().apply(this);
00114     }
00115     public void caseDivExpr(DivExpr expr)
00116     {
00117        translateBinaryOp(expr.getOp1(), expr.getOp2(), "/");
00118     }
00119     public void caseEqExpr(EqExpr expr)
00120     {
00121         translateBinaryOp(expr.getOp1(), expr.getOp2(), "==");
00122     }
00123     public void caseInstanceOfExpr(InstanceOfExpr expr)
00124     {
00125     out.print("(");
00126     expr.getRefExpr().apply(this);
00127     out.print(" instanceof " + expr.getRefType().getName() + ")");
00128     }
00129     public void caseIntLit(IntLit expr)
00130     {
00131     out.print(expr.getValue());
00132     }
00133     public void caseLeExpr(LeExpr expr)
00134     {
00135         translateBinaryOp(expr.getOp1(), expr.getOp2(), "<=");
00136     }
00137     public void caseLengthExpr(LengthExpr expr)
00138     {
00139     expr.getArray().apply(this);
00140     out.print(".length");
00141     }
00142     public void caseLockAction(LockAction lockAction) 
00143     {
00144     String opName = 
00145         LockAction.operationName(lockAction.getOperation());
00146     out.print(opName + "(");
00147     lockAction.getLockExpr().apply(this);
00148     out.print("); ");
00149     }
00150     public void caseLockTest(LockTest lockTest) {
00151     String opName = 
00152         LockTest.operationName(lockTest.getOperation());
00153     out.print(opName + "(");
00154     lockTest.getLockExpr().apply(this);
00155     out.print(")");
00156     }
00157     public void caseLtExpr(LtExpr expr)
00158     {
00159         translateBinaryOp(expr.getOp1(), expr.getOp2(), "<");
00160     }
00161     public void caseMulExpr(MulExpr expr)
00162     {
00163        translateBinaryOp(expr.getOp1(), expr.getOp2(), "*");
00164     }
00165     public void caseNeExpr(NeExpr expr)
00166     {
00167         translateBinaryOp(expr.getOp1(), expr.getOp2(), "!=");
00168     }
00169     public void caseNewArrayExpr(NewArrayExpr expr)
00170     {
00171     out.print("new " + expr.getCollection().getName() + "[");
00172     expr.getLength().apply(this);
00173     out.print("]");
00174     }
00175     public void caseNewExpr(NewExpr expr)
00176     {
00177     out.print("new " + expr.getCollection().getName());
00178     }
00179     public void caseNotExpr(NotExpr expr)
00180     {
00181     translateUnaryOp(expr.getOp(),"!");
00182     }
00183     public void caseNullExpr(NullExpr expr)
00184     {
00185     out.print("null");
00186     }
00187     public void caseOrExpr(OrExpr expr)
00188     {
00189        translateBinaryOp(expr.getOp1(), expr.getOp2(), "||");
00190     }
00191     public void casePrintAction(PrintAction printAction) 
00192     {
00193     out.print("println(");
00194     Vector printItems = printAction.getPrintItems();
00195     for (int i = 0; i < printItems.size(); i++) {
00196         Object item = printItems.elementAt(i);
00197         if (i > 0)
00198         out.print(",");
00199         if (item instanceof String)
00200         out.print("\"" + item + "\"");
00201         else 
00202         ((Expr)item).apply(this);
00203     }
00204     out.print("); ");
00205     }
00206     public void caseRecordExpr(RecordExpr expr)
00207     {
00208     expr.getRecord().apply(this);
00209     out.print("." + expr.getField().getName());
00210     }
00211     public void caseRefExpr(RefExpr expr)
00212     {
00213     out.print("ref " + expr.getTarget().getName());
00214     }
00215     public void caseRemExpr(RemExpr expr)
00216     {
00217        translateBinaryOp(expr.getOp1(), expr.getOp2(), "%");
00218     }
00219     public void caseStateVar(StateVar expr)
00220     {
00221     if (inPredicate && expr.getThread() != null)
00222         out.print(expr.getThread().getName() + ":" + expr.getName());
00223     else
00224         out.print(expr.getName());
00225     }
00226     public void caseSubExpr(SubExpr expr)
00227     {
00228        translateBinaryOp(expr.getOp1(), expr.getOp2(), "-");
00229     }
00230     public void caseThreadAction(ThreadAction threadAction) 
00231     {
00232     String opName = 
00233         ThreadAction.operationName(threadAction.getOperation());
00234     out.print(opName + "(");
00235     String threadName = threadAction.getThreadArg().getName();
00236     out.print(threadName + "); ");
00237     }
00238     public void caseThreadLocTest(ThreadLocTest threadLocTest) {
00239     String threadName = threadLocTest.getThread().getName();
00240     String locLabel = threadLocTest.getLocation().getLabel();
00241     out.print(threadName + "@" + locLabel);
00242     }
00243     public void caseThreadTest(ThreadTest threadTest) {
00244     String opName = 
00245         ThreadTest.operationName(threadTest.getOperation());
00246     out.print(opName + "(");
00247     String threadName = threadTest.getThreadArg().getName();
00248     out.print(threadName + ")");
00249     }
00250     public void defaultCase(Object obj)
00251     {
00252     throw new RuntimeException("Trans type not handled: " + obj);
00253     }
00254     /**
00255      * Print the transition system to System.out
00256      * @param system the transition system
00257      */
00258 
00259     public static void print(TransSystem system) {
00260     PrintWriter writerOut = new PrintWriter(System.out, true);
00261     print(system, writerOut);
00262     }
00263     /**
00264      * Print the transition system.
00265      * @param system the transition system
00266      * @param out a PrintWriter to send the output to
00267      */
00268 
00269     public static void print(TransSystem system, PrintWriter out) {
00270     (new BirPrinter(system,out)).run();
00271     }
00272     void printDefs() {
00273     Vector definitions = system.getDefinitions();
00274     for (int i = 0; i < definitions.size(); i++) {
00275         Definition def = (Definition) definitions.elementAt(i);
00276         out.println("  " + def.getName() + " = " + def.getDef() + ";");
00277     }
00278     }
00279     public void printLocation(Location loc)
00280     {
00281     out.print("loc " + loc.getLabel()  + ":");
00282     if (loc.getLiveVars() != null) {
00283         StateVarVector liveVars = loc.getLiveVars();
00284         if (liveVars.size() == 0)
00285         out.print(" live { }");
00286         else {
00287         out.print(" live { " +  liveVars.elementAt(0).getName());
00288         for (int i = 1; i < liveVars.size(); i++) 
00289             out.print(", " + liveVars.elementAt(i).getName());
00290         out.print(" } ");
00291         }
00292     }
00293     out.println();
00294     TransVector outTrans = loc.getOutTrans();
00295     for (int i = 0; i < outTrans.size(); i++) 
00296         translateTrans(outTrans.elementAt(i));
00297     }
00298     void printPredicates() {
00299     Vector preds = system.getPredicates();
00300     if (preds.size() > 0)
00301         out.println("predicates");
00302     inPredicate = true;
00303     for (int i = 0; i < preds.size(); i++) {
00304         Expr pred = (Expr) preds.elementAt(i);
00305         String name = system.predicateName(pred);
00306         out.print("  " + name + " = ");
00307         pred.apply(this);
00308         out.println(";");
00309     }
00310     inPredicate = false;
00311     }
00312     void printThreads() {
00313     ThreadVector threadVector = system.getThreads();
00314     for (int i = 0; i < threadVector.size(); i++) {
00315         BirThread thread = threadVector.elementAt(i);
00316         if (thread.isMain())
00317         out.print("main ");
00318         out.println("thread " + thread.getName());
00319         printVars(thread);
00320         LocVector threadLocVector = thread.getLocations();
00321         for (int j = 0; j < threadLocVector.size(); j++) 
00322         printLocation(threadLocVector.elementAt(j));
00323         out.println("end " + thread.getName() + ";");
00324     }
00325     }
00326     static void printTrans(Transformation trans) {
00327     PrintWriter writerOut = new PrintWriter(System.out, true);
00328     TransSystem system = trans.getFromLoc().getThread().getSystem();
00329     (new BirPrinter(system,writerOut)).translateTrans(trans);
00330     }
00331     void printVars(BirThread thread) {
00332     StateVarVector stateVarVector = system.getStateVars();
00333     for (int i = 0; i < stateVarVector.size(); i++) {
00334         StateVar var = stateVarVector.elementAt(i);
00335         if (var.getThread() == thread) {
00336         out.print("  " + var.getName() + " : " 
00337               + var.getType().typeSpec());
00338         if (var.getInitVal() != null) {
00339             out.print(" := ");
00340             ((Expr)var.getInitVal()).apply(this);
00341         }
00342         out.println(";");
00343         }
00344     }
00345     }
00346     void run() {
00347     out.println("process " + system.getName() + "()");
00348     printDefs();
00349     printVars(null);
00350     printThreads();
00351     printPredicates();
00352     out.println("end " + system.getName() + ";");
00353     }
00354     void translateBinaryOp(Expr e1, Expr e2, String op) {
00355     out.print("(");
00356     e1.apply(this);
00357     out.print(" " + op + " ");
00358     e2.apply(this);
00359     out.print(")");
00360     }
00361     public void translateTrans(Transformation trans)
00362     {
00363         out.print("  when ");
00364     if (trans.getGuard() != null) 
00365         trans.getGuard().apply(this);
00366     else
00367         out.print("true");
00368     out.print(" do");
00369         if (! trans.isVisible())
00370         out.print(" invisible");
00371         out.print(" { ");
00372     ActionVector actions = trans.getActions();
00373     for (int i = 0; i < actions.size(); i++) {
00374         actions.elementAt(i).apply(this);
00375         if ((i+1) < actions.size())
00376         out.print("\n    ");
00377     }
00378     out.println("} goto " + trans.getToLoc().getLabel() + ";");
00379     }
00380     void translateUnaryOp(Expr e, String op) {
00381     out.print("(" + op + " ");
00382     e.apply(this);
00383     out.print(")");
00384     }
00385 }

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