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* 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; }