Type type = PtolemyUtilities.tokenType;
nameToType.put(name, port.getType());
// PtolemyUtilities.getSootTypeForTokenType(
// port.getType());
SootField field = new SootField(StringUtilities
.sanitizeName(name)
+ "Token", type);
entityInstanceClass.addField(field);
nameToField.put(name, field);
field = new SootField(StringUtilities.sanitizeName(name)
+ "IsPresent", type);
entityInstanceClass.addField(field);
nameToField.put(name + "_isPresent", field);
}
}
{
SootMethod preinitializeMethod = new SootMethod("preinitialize",
Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
entityInstanceClass.addMethod(preinitializeMethod);
JimpleBody body = Jimple.v().newBody(preinitializeMethod);
preinitializeMethod.setActiveBody(body);
body.insertIdentityStmts();
Stmt insertPoint = Jimple.v().newReturnVoidStmt();
body.getUnits().add(insertPoint);
ModelTransformer.initializeAttributesBefore(body, insertPoint,
entity, body.getThisLocal(), entity, body.getThisLocal(),
entityInstanceClass);
}
// Add a field to keep track of the current state.
SootField currentStateField = new SootField("_currentState", IntType
.v());
entityInstanceClass.addField(currentStateField);
SootField nextTransitionField = new SootField("_nextTransition",
IntType.v());
entityInstanceClass.addField(nextTransitionField);
// populate the initialize method.
{
System.out.println("create initialize()");
SootMethod initializeMethod = new SootMethod("initialize",
Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
entityInstanceClass.addMethod(initializeMethod);
JimpleBody body = Jimple.v().newBody(initializeMethod);
initializeMethod.setActiveBody(body);
body.insertIdentityStmts();
Chain units = body.getUnits();
Local thisLocal = body.getThisLocal();
// Set the initial state.
String initialStateName = ((StringAttribute) entity
.getAttribute("initialStateName")).getExpression();
int initialStateIndex = entity.entityList().indexOf(
entity.getEntity(initialStateName));
units.add(Jimple.v().newAssignStmt(
Jimple.v().newInstanceFieldRef(thisLocal,
currentStateField.makeRef()),
IntConstant.v(initialStateIndex)));
// return void
units.add(Jimple.v().newReturnVoidStmt());
LocalNameStandardizer.v().transform(body, "at.lns");
LocalSplitter.v().transform(body, "at.ls");
}
// populate the fire method.
{
System.out.println("create fire()");
SootMethod fireMethod = new SootMethod("fire",
Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
entityInstanceClass.addMethod(fireMethod);
JimpleBody body = Jimple.v().newBody(fireMethod);
fireMethod.setActiveBody(body);
body.insertIdentityStmts();
Chain units = body.getUnits();
Local thisLocal = body.getThisLocal();
Local hasTokenLocal = Jimple.v().newLocal("hasTokenLocal",
BooleanType.v());
body.getLocals().add(hasTokenLocal);
Local tokenLocal = Jimple.v().newLocal("tokenLocal",
PtolemyUtilities.tokenType);
body.getLocals().add(tokenLocal);
Iterator inputPorts = entity.inputPortList().iterator();
while (inputPorts.hasNext()) {
TypedIOPort port = (TypedIOPort) (inputPorts.next());
// FIXME: Handle multiports
if (port.getWidth() > 0) {
String name = port.getName(entity);
// Create an if statement.
//
Local portLocal = Jimple.v().newLocal("port",
PtolemyUtilities.componentPortType);
body.getLocals().add(portLocal);
SootField portField = entityInstanceClass
.getFieldByName(StringUtilities.sanitizeName(name));
units.add(Jimple.v().newAssignStmt(
portLocal,
Jimple.v().newInstanceFieldRef(thisLocal,
portField.makeRef())));
units.add(Jimple.v().newAssignStmt(
hasTokenLocal,
Jimple.v().newVirtualInvokeExpr(portLocal,
PtolemyUtilities.hasTokenMethod.makeRef(),
IntConstant.v(0))));
Local hasTokenToken = PtolemyUtilities.addTokenLocal(body,
"token", PtolemyUtilities.booleanTokenClass,
PtolemyUtilities.booleanTokenConstructor,
hasTokenLocal);
// store the isPresent
SootField tokenIsPresentField = (SootField) nameToField
.get(name + "_isPresent");
units.add(Jimple.v().newAssignStmt(
Jimple.v().newInstanceFieldRef(thisLocal,
tokenIsPresentField.makeRef()),
hasTokenToken));
Stmt target = Jimple.v().newNopStmt();
units.add(Jimple.v().newIfStmt(
Jimple.v().newEqExpr(hasTokenLocal,
IntConstant.v(0)), target));
units.add(Jimple.v().newAssignStmt(
tokenLocal,
Jimple.v().newVirtualInvokeExpr(portLocal,
PtolemyUtilities.getMethod.makeRef(),
IntConstant.v(0))));
SootField tokenField = (SootField) nameToField.get(name);
units.add(Jimple.v().newAssignStmt(
Jimple.v().newInstanceFieldRef(thisLocal,
tokenField.makeRef()), tokenLocal));
units.add(target);
}
}
Map stateToStartStmt = new HashMap();