00001 package edu.ksu.cis.bandera.smv;
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.Vector;
00037
00038 import edu.ksu.cis.bandera.bir.*;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 public class SmvTypeDecl extends AbstractTypeSwitch implements BirConstants {
00064
00065 SmvTrans smvTrans;
00066 TransSystem system;
00067
00068 StateVar source;
00069 SmvVar container;
00070 boolean constrained;
00071 SmvVar deadFlag;
00072
00073 String refIndexType;
00074 String instNumType;
00075 int maxSize;
00076
00077 public SmvTypeDecl(SmvTrans smvTrans, TransSystem sys) {
00078 this.smvTrans = smvTrans;
00079 this.system = sys;
00080
00081
00082
00083 StateVarVector targets = sys.refAnyType().getTargets();
00084 maxSize = 0;
00085 for (int i = 0; i < targets.size(); i++)
00086 if (targets.elementAt(i).getType().isKind(COLLECTION)) {
00087 int size = ((Collection)targets.elementAt(i).getType())
00088 .getSize().getValue();
00089 if (size > maxSize)
00090 maxSize = size;
00091 }
00092 refIndexType = "0 .. " + targets.size();
00093 instNumType = "0 .. " + (maxSize - 1);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 public void caseArray(Array type, Object o)
00107 {
00108 String name = varName(o);
00109 SmvVar array = smvTrans.declareVar(name,null,source,null,
00110 container,false,deadFlag);
00111 int size = type.getSize().getValue();
00112 String lengthType = "0 .. " + size;
00113 smvTrans.declareVar(smvTrans.lengthVar(name),
00114 lengthType,source,"0",array,constrained,deadFlag);
00115 for (int i = 0; i < size; i++) {
00116
00117 this.container = array;
00118 type.getBaseType().apply(this,smvTrans.elementVar(name, i));
00119 }
00120 }
00121
00122
00123
00124
00125 public void caseBool(Bool type, Object o)
00126 {
00127 String name = varName(o);
00128 smvTrans.declareVar(name,"boolean",source,
00129 initValue(type,o),container,constrained,deadFlag);
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 public void caseCollection(Collection type, Object o)
00144 {
00145 String name = varName(o);
00146 SmvVar col = smvTrans.declareVar(name,null,source,null,
00147 container,false,null);
00148 int size = type.getSize().getValue();
00149 for (int i = 0; i < size; i++) {
00150 SmvVar inuse = smvTrans.declareVar(smvTrans.inUseVar(name,i),
00151 "boolean",source, "0",
00152 col,constrained,null);
00153
00154 this.container = col;
00155
00156 this.deadFlag = inuse;
00157 type.getBaseType().apply(this,smvTrans.instanceVar(name,i));
00158 this.deadFlag = null;
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 public void caseEnumerated(Enumerated type, Object o)
00169 {
00170 String name = varName(o);
00171 Vector constants = type.getConstants();
00172 String smvType = type.getFirstElement() + " .. " +
00173 (type.getFirstElement() + type.getEnumeratedSize() - 1);
00174 smvTrans.declareVar(name,smvType,source,
00175 initValue(type,o),container,constrained,deadFlag);
00176 }
00177
00178
00179
00180
00181 public void caseField(Field type, Object o)
00182 {
00183 String name = varName(o);
00184 type.getType().apply(this, smvTrans.fieldVar(name,type));
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 public void caseLock(Lock type, Object o)
00200 {
00201 String name = varName(o);
00202 SmvVar lock = smvTrans.declareVar(name,null,source,
00203 null,container,false,deadFlag);
00204 smvTrans.declareVar(smvTrans.ownerVar(name),smvTrans.getThreadType(),
00205 source,"NoThread",lock,constrained,deadFlag);
00206 smvTrans.declareVar(smvTrans.countVar(name),SmvTrans.LOCK_COUNT_TYPE,
00207 source,"0",lock,constrained,deadFlag);
00208 ThreadVector threads = system.getThreads();
00209 for (int i = 0; i < threads.size(); i++)
00210 smvTrans.declareVar(smvTrans.waitVar(name,threads.elementAt(i)),
00211 "boolean",source,"0",
00212 lock,constrained,deadFlag);
00213 }
00214
00215
00216
00217
00218 public void caseRange(Range type, Object o)
00219 {
00220 String name = varName(o);
00221 String smvType = type.getFromVal().getValue() + " .. " +
00222 type.getToVal().getValue();
00223 smvTrans.declareVar(name,smvType,source,
00224 initValue(type,o),container,constrained,deadFlag);
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234 public void caseRecord(Record type, Object o)
00235 {
00236 String name = varName(o);
00237 Vector fields = type.getFields();
00238 SmvVar record = smvTrans.declareVar(name,null,source,
00239 null,container,false,deadFlag);
00240 for (int i = 0; i < fields.size(); i++) {
00241
00242 this.container = record;
00243 ((Field)fields.elementAt(i)).apply(this, o);
00244 }
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 public void caseRef(Ref type, Object o)
00269 {
00270 String name = varName(o);
00271 SmvVar ref = smvTrans.declareVar(name, null,source, null,
00272 container,constrained,deadFlag);
00273 this.container = ref;
00274 SmvVar refIndex =
00275 smvTrans.declareVar(smvTrans.refIndexVar(name),refIndexType,source,
00276 "0",container,false,deadFlag);
00277 SmvVar instNum =
00278 smvTrans.declareVar(smvTrans.instNumVar(name),instNumType,source,
00279 "0",container,false,deadFlag);
00280 ref.setPrintForm("(" + refIndex.getName() + " * MAX_COLLECT_SIZE + " +
00281 instNum.getName() + ")");
00282 }
00283 public void defaultCase(Object obj) {
00284 throw new RuntimeException("Construct not handled: " + obj);
00285 }
00286 public int getMaxCollectionSize() { return maxSize; }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 String initValue(Type type, Object context) {
00297 Expr initVal;
00298 if (context instanceof String)
00299 initVal = type.defaultVal();
00300 else
00301 initVal = ((StateVar)context).getInitVal();
00302 initVal.apply(smvTrans);
00303 return smvTrans.getResult().toString();
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 String varName(Object context) {
00316 if (context instanceof String)
00317 return (String) context;
00318 else {
00319 source = (StateVar)context;
00320
00321 container = null;
00322
00323 constrained = (source.getThread() == null);
00324 return source.getName();
00325 }
00326 }
00327 }