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 ca.mcgill.sable.soot.SootClass;
00036 import ca.mcgill.sable.soot.SootField;
00037
00038 import edu.ksu.cis.bandera.bir.*;
00039 import edu.ksu.cis.bandera.jext.*;
00040
00041 import java.util.*;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 public class JimpleStoreBuilder extends AbstractTypeSwitch
00054 implements BirConstants {
00055
00056 BirState state;
00057 Literal [] birStore;
00058 TransSystem system;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 Hashtable objectTable = new Hashtable();
00069
00070 public JimpleStoreBuilder(BirState state) {
00071 this.state = state;
00072 this.birStore = state.getStore();
00073 this.system = state.getSystem();
00074 }
00075 public void caseArray(Array type, Object o)
00076 {
00077
00078
00079 Object alreadyComputed = objectTable.get(o);
00080 if (alreadyComputed != null) {
00081
00082 setResult((ArrayLiteral)alreadyComputed);
00083 return;
00084 }
00085
00086 int arrayAddr = getVarOffset(o);
00087 int length = ((IntLit)getVarValue(o)).getValue();
00088 ArrayLiteral array = new ArrayLiteral(length,arrayAddr);
00089
00090 objectTable.put(o,array);
00091 for (int i = 0; i < length; i++) {
00092 int address = arrayAddr + 1 + i;
00093
00094 type.getBaseType().apply(this, new Integer(address));
00095 array.contents[i] = (JimpleLiteral) getResult();
00096 }
00097 setResult(array);
00098 }
00099 public void caseBool(Bool type, Object o)
00100 {
00101
00102 Literal ll = getVarValue(o);
00103 boolean value = false;
00104 if (ll instanceof BoolLit)
00105 value = ((BoolLit) ll).getValue();
00106 else if (ll instanceof IntLit)
00107 value = ((IntLit) ll).getValue() == 0;
00108
00109
00110 setResult(new BooleanLiteral(value));
00111 }
00112 public void caseLock(Lock type, Object o)
00113 {
00114 LockLit lock = (LockLit) getVarValue(o);
00115 setResult(new LockLiteral(lock));
00116 }
00117 public void caseRange(Range type, Object o)
00118 {
00119 int value = ((IntLit)getVarValue(o)).getValue();
00120 setResult(new IntegerLiteral(value));
00121 }
00122 public void caseRecord(Record type, Object o)
00123 {
00124
00125
00126 Object alreadyComputed = objectTable.get(o);
00127 if (alreadyComputed != null) {
00128
00129 setResult((ClassLiteral)alreadyComputed);
00130 return;
00131 }
00132
00133 int recAddr = getVarOffset(o);
00134 ClassLiteral classLit =
00135 new ClassLiteral((SootClass)system.getSource(type),recAddr);
00136
00137 objectTable.put(o, classLit);
00138 Vector fields = type.getFields();
00139 for (int i = 0; i < fields.size(); i++) {
00140 Field field = (Field) fields.elementAt(i);
00141 SootField sField = (SootField) system.getSource(field);
00142 int address = recAddr + field.getOffset();
00143
00144 field.getType().apply(this, new Integer(address));
00145 JimpleLiteral value = (JimpleLiteral)getResult();
00146
00147 if (sField != null)
00148 classLit.setFieldValue(sField,value);
00149 else
00150 classLit.setLockValue(value);
00151 }
00152 setResult(classLit);
00153 }
00154 public void caseRef(Ref type, Object o)
00155 {
00156 Literal value = getVarValue(o);
00157 if (value instanceof RefLit) {
00158
00159
00160 RefLit ref = (RefLit) getVarValue(o);
00161 int address = ref.getCollection().getOffset();
00162 if (ref.getCollection().getType().isKind(COLLECTION))
00163 address += ref.getIndex() * type.getTargetType().getExtent();
00164 Type targetType = ref.getTargetType();
00165 targetType.apply(this, new Integer(address));
00166 setResult(new ReferenceLiteral((JimpleLiteral)getResult()));
00167 } else
00168 setResult(new ReferenceLiteral(null));
00169 }
00170 public void defaultCase(Object obj)
00171 {
00172 throw new RuntimeException("Case not handled: " + obj);
00173 }
00174
00175
00176
00177
00178
00179
00180 int getVarOffset(Object context) {
00181 if (context instanceof Integer)
00182 return ((Integer)context).intValue();
00183 else
00184 return ((StateVar)context).getOffset();
00185 }
00186
00187
00188
00189
00190 Literal getVarValue(Object context) {
00191 return birStore[getVarOffset(context)];
00192 }
00193 }