00001 package edu.ksu.cis.bandera.bir;
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
00037 import java.io.*;
00038 import java.util.*;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
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
00256
00257
00258
00259 public static void print(TransSystem system) {
00260 PrintWriter writerOut = new PrintWriter(System.out, true);
00261 print(system, writerOut);
00262 }
00263
00264
00265
00266
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 }