00001 package edu.ksu.cis.bandera.birc;
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 java.io.*;
00036 import java.util.*;
00037
00038 import edu.ksu.cis.bandera.bir.*;
00039
00040 public class ChooseExpander {
00041
00042 TransSystem system;
00043 int mark;
00044 TransVector newTransformations = new TransVector();
00045
00046 public ChooseExpander(TransSystem system) {
00047 this.system = system;
00048 this.mark = Location.getNewMark();
00049 }
00050 void expand(Location currentLoc) {
00051 currentLoc.setMark(mark);
00052 TransVector outTrans = currentLoc.getOutTrans();
00053 for (int i = 0; i < outTrans.size(); i++) {
00054 Transformation trans = outTrans.elementAt(i);
00055 expandTrans(trans);
00056 if ((trans.getToLoc().getMark() != mark))
00057 expand(trans.getToLoc());
00058 }
00059 }
00060 void expandTrans(Transformation trans) {
00061 ActionVector actions = trans.getActions();
00062 if (actions.size() > 0) {
00063 if (actions.size() > 1)
00064 throw new RuntimeException("ChooseExpander doesn't handle multiple assignments per transformation" + trans);
00065 if (actions.elementAt(0).isAssignAction()) {
00066 AssignAction assign =
00067 (AssignAction) actions.elementAt(0);
00068 if (assign.getRhs() instanceof ChooseExpr) {
00069 trans.markDeleted();
00070 Vector choices =
00071 ((ChooseExpr)assign.getRhs()).getChoices();
00072 for (int i = 0; i < choices.size(); i++) {
00073 Expr choice = (Expr) choices.elementAt(i);
00074 ActionVector newActions = new ActionVector();
00075 AssignAction choiceAssign =
00076 new AssignAction(assign.getLhs(), choice);
00077 newActions.addElement(choiceAssign);
00078 trans.getFromLoc().addTrans(trans.getToLoc(),
00079 trans.getGuard(),
00080 newActions);
00081 }
00082 }
00083 }
00084 }
00085 }
00086 public void run() {
00087 ThreadVector threadVector = system.getThreads();
00088 for (int i = 0; i < threadVector.size(); i++) {
00089 BirThread thread = threadVector.elementAt(i);
00090 expand(thread.getStartLoc());
00091 }
00092 Transformation.purge();
00093 }
00094 }