FSM fsm_simple() { FSM fsm; int even = fsm.addState("Even", true); int odd = fsm.addState("Odd"); fsm.addTransition(even, even, 1, "1"); fsm.addTransition(even, odd, 0, "0"); fsm.addTransition(odd, odd, 1, "1"); fsm.addTransition(odd, even, 0, "0"); return fsm; }
FSM fsm_moonman() { FSM fsm; int start = fsm.addState("No Input"); int bogus = fsm.addState("Not MOONMAN"); int m1 = fsm.addState("M"); int o1 = fsm.addState("O"); int o2 = fsm.addState("O"); int n1 = fsm.addState("N"); int m2 = fsm.addState("M"); int a1 = fsm.addState("A"); int n2 = fsm.addState("N", true); fsm.addTransition(start, m1, (int) 'M', "M"); // 1 fsm.addTransition(m1, o1, (int) 'O', "O"); // 2 fsm.addTransition(o1, o2, (int) 'O', "O"); // 3 fsm.addTransition(o2, n1, (int) 'N', "N"); // 4 fsm.addTransition(n1, m2, (int) 'M', "M"); // 5 fsm.addTransition(m2, a1, (int) 'A', "A"); // 6 fsm.addTransition(a1, n2, (int) 'N', "N"); // 7 fsm.addTransition(start, bogus, (int) 'X', "X"); // 8 fsm.addTransition(m1, bogus, (int) 'X', "X"); // 9 fsm.addTransition(o1, bogus, (int) 'X', "X"); // 10 fsm.addTransition(o2, bogus, (int) 'X', "X"); // 11 fsm.addTransition(n1, bogus, (int) 'X', "X"); // 12 fsm.addTransition(m2, bogus, (int) 'X', "X"); // 13 fsm.addTransition(a1, bogus, (int) 'X', "X"); // 14 fsm.addTransition(n2, bogus, (int) 'X', "X"); // 15 return fsm; }
FSM* IRParser::parseFSM(llvm::MDNode* node){ FSM* fsm = new FSM(); //Parse initial state MDString* initialState = cast<MDString>(node->getOperand(0)); //Parse all fsm state MDNode* stateArray = cast<MDNode>(node->getOperand(1)); for (unsigned i = 0, e = stateArray->getNumOperands(); i != e; ++i){ MDString* stateMD = cast<MDString>(stateArray->getOperand(i)); fsm->addState(stateMD->getString()); } // set the initial state after initializing the states fsm->setInitialState(initialState->getString()); //Parse transition MDNode* transitionsArray = cast<MDNode>(node->getOperand(2)); for (unsigned i = 0, e = transitionsArray->getNumOperands(); i != e; ++i){ MDNode* transitionArray = cast<MDNode>(transitionsArray->getOperand(i)); MDString* source = cast<MDString>(transitionArray->getOperand(0)); Value* targetValue = transitionArray->getOperand(1); //In case of "undefined" state, no target are given if (targetValue != NULL){ MDNode* targetsArray = cast<MDNode>(targetValue); for (unsigned j = 0, f = targetsArray->getNumOperands() ; j != f; ++j){ MDNode* targetArray = cast<MDNode>(targetsArray->getOperand(j)); Action* action = getAction(cast<MDNode>(targetArray->getOperand(0))); MDString* target = cast<MDString>(targetArray->getOperand(1)); fsm->addTransition(source->getString(), target->getString(), action); } } } return fsm; }
FSM fsm_brain_bag() { FSM fsm; int S = fsm.addState("Start State"); int Z = fsm.addState("Bogus State"); int b = fsm.addState("B"); int i = fsm.addState("I"); int r = fsm.addState("R"); int a = fsm.addState("A"); int n = fsm.addState("N", true); int g = fsm.addState("G", true); int a2 = fsm.addState("A", true); int s = fsm.addState("S", true); int i2 = fsm.addState("I"); int n2 = fsm.addState("N", true); fsm.addTransition(S, b, (int) 'B', "B"); fsm.addTransition(b, i, (int) 'I', "I"); fsm.addTransition(b, r, (int) 'R', "R"); fsm.addTransition(b, a, (int) 'A', "A"); fsm.addTransition(i, n, (int) 'N', "N"); fsm.addTransition(i, g, (int) 'G', "G"); fsm.addTransition(r, a2, (int) 'A', "A"); fsm.addTransition(a, g, (int) 'G', "G"); fsm.addTransition(n, s, (int) 'S', "S"); fsm.addTransition(a2, s, (int) 'S', "S"); fsm.addTransition(a2, i2, (int) 'I', "I"); fsm.addTransition(i2, n2, (int) 'N', "N"); fsm.addTransition(n2, s, (int) 'S', "S"); fsm.addTransition(S, Z, -1, "Bogus"); fsm.addTransition(b, Z, -1, "Bogus"); fsm.addTransition(i, Z, -1, "Bogus"); fsm.addTransition(r, Z, -1, "Bogus"); fsm.addTransition(a, Z, -1, "Bogus"); fsm.addTransition(n, Z, -1, "Bogus"); fsm.addTransition(g, Z, -1, "Bogus"); fsm.addTransition(a2, Z, -1, "Bogus"); fsm.addTransition(s, Z, -1, "Bogus"); fsm.addTransition(i2, Z, -1, "Bogus"); fsm.addTransition(n2, Z, -1, "Bogus"); return fsm; }
FSM* IRWriter::writeFSM(FSM* fsm){ list<llvm::Function*>::iterator it; FSM* newFSM = new FSM(); //Copy states of the source fsm std::map<std::string, FSM::State*>::iterator itState; std::map<std::string, FSM::State*>* states = fsm->getStates(); for (itState = states->begin(); itState != states->end(); itState++){ newFSM->addState(itState->first); } //Copy transitions of the source fsm map<string, Action*>::iterator itActionsMap; std::map<std::string, FSM::Transition*>::iterator itTransition; std::map<std::string, FSM::Transition*>* transitions = fsm->getTransitions(); for (itTransition = transitions->begin(); itTransition != transitions->end(); itTransition++){ FSM::Transition* transition = itTransition->second; FSM::State* sourceState = transition->getSourceState(); list<FSM::NextStateInfo*>::iterator itNextStateInfo; list<FSM::NextStateInfo*>* nextStateInfos = transition->getNextStateInfo(); for (itNextStateInfo = nextStateInfos->begin(); itNextStateInfo != nextStateInfos->end(); itNextStateInfo++){ FSM::State* targetState = (*itNextStateInfo)->getTargetState(); Action* targetAction = (*itNextStateInfo)->getAction(); newFSM->addTransition(sourceState->getName(), targetState->getName(), getAction(targetAction)); } } //Set initiale state of the FSM newFSM->setInitialState(fsm->getInitialState()->getName()); return newFSM; }