std::wostream &CSharpTabCodeGen::COND_KEYS() { out << L'\t'; int totalTrans = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Loop the state's transitions. */ for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) { /* Lower key. */ out << ALPHA_KEY( sc->lowKey ) << L", "; if ( ++totalTrans % IALL == 0 ) out << L"\n\t"; /* Upper key. */ out << ALPHA_KEY( sc->highKey ) << L", "; if ( ++totalTrans % IALL == 0 ) out << L"\n\t"; } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ if ( keyOps->alphType->isChar ) out << L"(char) " << 0 << L"\n"; else out << 0 << L"\n"; return out; }
std::ostream &RbxGotoCodeGen::STATE_GOTOS() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( st == redFsm->errState ) STATE_GOTO_ERROR(); else { /* Writing code above state gotos. */ GOTO_HEADER( st ); if ( st->stateCondVect.length() > 0 ) { out << " _widec = " << GET_KEY() << ";\n"; emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 ); } /* Try singles. */ if ( st->outSingle.length() > 0 ) emitSingleSwitch( st ); /* Default case is to binary search for the ranges, if that fails then */ if ( st->outRange.length() > 0 ) emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 ); /* Write the default transition. */ TRANS_GOTO( st->defTrans, 1 ) << "\n"; } } return out; }
/* Determine if we should use indicies or not. */ void TabCodeGen::calcIndexSize() { int sizeWithInds = 0, sizeWithoutInds = 0; /* Calculate cost of using with indicies. */ for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { int totalIndex = st->outSingle.length() + st->outRange.length() + (st->defTrans == 0 ? 0 : 1); sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex; } sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length(); if ( redFsm->anyActions() ) sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length(); /* Calculate the cost of not using indicies. */ for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { int totalIndex = st->outSingle.length() + st->outRange.length() + (st->defTrans == 0 ? 0 : 1); sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex; if ( redFsm->anyActions() ) sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex; } /* If using indicies reduces the size, use them. */ useIndicies = sizeWithInds < sizeWithoutInds; }
std::wostream &SplitCodeGen::STATE_GOTOS( int partition ) { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( st->partition == partition ) { if ( st == redFsm->errState ) STATE_GOTO_ERROR(); else { /* We call into the base of the goto which calls back into us * using virtual functions. Set the current partition rather * than coding parameter passing throughout. */ currentPartition = partition; /* Writing code above state gotos. */ GOTO_HEADER( st, st->partition == partition ); if ( st->stateCondVect.length() > 0 ) { out << L" _widec = " << GET_KEY() << L";\n"; emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 ); } /* Try singles. */ if ( st->outSingle.length() > 0 ) emitSingleSwitch( st ); /* Default case is to binary search for the ranges, if that fails then */ if ( st->outRange.length() > 0 ) emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 ); /* Write the default transition. */ TRANS_GOTO( st->defTrans, 1 ) << L"\n"; } } } return out; }
std::ostream &RubyTabCodeGen::INDICIES() { int totalTrans = 0; START_ARRAY_LINE(); for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Walk the singles. */ for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { ARRAY_ITEM( KEY( stel->value->id ), ++totalTrans, false ); } /* Walk the ranges. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { ARRAY_ITEM( KEY( rtel->value->id ), ++totalTrans, false ); } /* The state's default index goes next. */ if ( st->defTrans != 0 ) { ARRAY_ITEM( KEY( st->defTrans->id ), ++totalTrans, false ); } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ ARRAY_ITEM( INT(0), ++totalTrans, true ); END_ARRAY_LINE(); return out; }
std::ostream &TabCodeGen::KEYS() { out << '\t'; int totalTrans = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Loop the singles. */ for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { out << KEY( stel->lowKey ) << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } /* Loop the state's transitions. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { /* Lower key. */ out << KEY( rtel->lowKey ) << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; /* Upper key. */ out << KEY( rtel->highKey ) << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ out << 0 << "\n"; return out; }
std::ostream &RubyTabCodeGen::KEYS() { START_ARRAY_LINE(); int totalTrans = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Loop the singles. */ for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { ARRAY_ITEM( KEY( stel->lowKey ), ++totalTrans, false ); } /* Loop the state's transitions. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { /* Lower key. */ ARRAY_ITEM( KEY( rtel->lowKey ), ++totalTrans, false ); /* Upper key. */ ARRAY_ITEM( KEY( rtel->highKey ), ++totalTrans, false ); } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ ARRAY_ITEM( INT(0), ++totalTrans, true ); END_ARRAY_LINE(); return out; }
/* Gather various info on the machine. */ void CodeGenData::analyzeMachine() { /* Find the true count of action references. */ findFinalActionRefs(); /* Check if there are any calls in action code. */ for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { /* Record the occurrence of various kinds of actions. */ if ( act->numToStateRefs > 0 ) redFsm->bAnyToStateActions = true; if ( act->numFromStateRefs > 0 ) redFsm->bAnyFromStateActions = true; if ( act->numEofRefs > 0 ) redFsm->bAnyEofActions = true; if ( act->numTransRefs > 0 ) redFsm->bAnyRegActions = true; /* Recurse through the action's parse tree looking for various things. */ analyzeAction( act, act->inlineList ); } /* Analyze reduced action lists. */ for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ ) analyzeActionList( redAct, act->value->inlineList ); } /* Find states that have transitions with actions that have next * statements. */ for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Check any actions out of outSinge. */ for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) { if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() ) st->bAnyRegCurStateRef = true; } /* Check any actions out of outRange. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() ) st->bAnyRegCurStateRef = true; } /* Check any action out of default. */ if ( st->defTrans != 0 && st->defTrans->action != 0 && st->defTrans->action->anyCurStateRef() ) st->bAnyRegCurStateRef = true; if ( st->stateCondList.length() > 0 ) redFsm->bAnyConditions = true; if ( st->eofTrans != 0 ) redFsm->bAnyEofTrans = true; } /* Assign ids to actions that are referenced. */ assignActionIds(); /* Set the maximums of various values used for deciding types. */ setValueLimits(); }
std::ostream &CSharpFlatCodeGen::INDICIES() { int totalTrans = 0; out << '\t'; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( st->transList != 0 ) { /* Walk the singles. */ unsigned long long span = keyOps->span( st->lowKey, st->highKey ); for ( unsigned long long pos = 0; pos < span; pos++ ) { out << st->transList[pos]->id << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } } /* The state's default index goes next. */ if ( st->defTrans != 0 ) out << st->defTrans->id << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ out << 0 << "\n"; return out; }
/* Find the final state with the lowest id. */ void RedFsmAp::findFirstFinState() { for ( RedStateList::Iter st = stateList; st.lte(); st++ ) { if ( st->isFinal && (firstFinState == 0 || st->id < firstFinState->id) ) firstFinState = st; } }
/* Assign state ids by appearance in the state list. */ void RedFsmAp::sequentialStateIds() { /* Table based machines depend on the state numbers starting at zero. */ nextStateId = 0; for ( RedStateList::Iter st = stateList; st.lte(); st++ ) st->id = nextStateId++; }
std::ostream &JavaTabCodeGen::TRANS_ACTIONS() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Walk the singles. */ for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { RedTransAp *trans = stel->value; ARRAY_ITEM( INT(TRANS_ACTION( trans )), false ); } /* Walk the ranges. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { RedTransAp *trans = rtel->value; ARRAY_ITEM( INT(TRANS_ACTION( trans )), false ); } /* The state's default index goes next. */ if ( st->defTrans != 0 ) { RedTransAp *trans = st->defTrans; ARRAY_ITEM( INT(TRANS_ACTION( trans )), false ); } } for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( st->eofTrans != 0 ) { RedTransAp *trans = st->eofTrans; ARRAY_ITEM( INT(TRANS_ACTION( trans )), false ); } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ ARRAY_ITEM( INT(0), true ); return out; }
std::ostream &TabCodeGen::INDICIES() { int totalTrans = 0; out << '\t'; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Walk the singles. */ for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { out << stel->value->id << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } /* Walk the ranges. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { out << rtel->value->id << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } /* The state's default index goes next. */ if ( st->defTrans != 0 ) { out << st->defTrans->id << ", "; if ( ++totalTrans % IALL == 0 ) out << "\n\t"; } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ out << 0 << "\n"; return out; }
std::ostream &JavaTabCodeGen::EOF_ACTIONS() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write any eof action. */ ARRAY_ITEM( INT(EOF_ACTION(st)), st.last() ); } return out; }
std::ostream &JavaTabCodeGen::RANGE_LENS() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Emit length of range index. */ ARRAY_ITEM( INT(st->outRange.length()), st.last() ); } return out; }
std::ostream &JavaTabCodeGen::SINGLE_LENS() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write singles length. */ ARRAY_ITEM( INT(st->outSingle.length()), st.last() ); } return out; }
/* Look through ranges and choose suitable single character transitions. */ void RedFsmAp::chooseSingle() { /* Loop the states. */ for ( RedStateList::Iter st = stateList; st.lte(); st++ ) { /* Rewrite the transition list taking out the suitable single * transtions. */ moveTransToSingle( st ); } }
void CodeGenData::findFinalActionRefs() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Rerence count out of single transitions. */ for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) { if ( rtel->value->action != 0 ) { rtel->value->action->numTransRefs += 1; for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ ) item->value->numTransRefs += 1; } } /* Reference count out of range transitions. */ for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { if ( rtel->value->action != 0 ) { rtel->value->action->numTransRefs += 1; for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ ) item->value->numTransRefs += 1; } } /* Reference count default transition. */ if ( st->defTrans != 0 && st->defTrans->action != 0 ) { st->defTrans->action->numTransRefs += 1; for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ ) item->value->numTransRefs += 1; } /* Reference count eof transitions. */ if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) { st->eofTrans->action->numTransRefs += 1; for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ ) item->value->numTransRefs += 1; } /* Reference count to state actions. */ if ( st->toStateAction != 0 ) { st->toStateAction->numToStateRefs += 1; for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ ) item->value->numToStateRefs += 1; } /* Reference count from state actions. */ if ( st->fromStateAction != 0 ) { st->fromStateAction->numFromStateRefs += 1; for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ ) item->value->numFromStateRefs += 1; } /* Reference count EOF actions. */ if ( st->eofAction != 0 ) { st->eofAction->numEofRefs += 1; for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ ) item->value->numEofRefs += 1; } } }
std::ostream &RubyTabCodeGen::EOF_ACTIONS() { START_ARRAY_LINE(); int totalStateNum = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write any eof action. */ ARRAY_ITEM( INT(EOF_ACTION(st)), ++totalStateNum, st.last() ); } END_ARRAY_LINE(); return out; }
std::ostream &RubyTabCodeGen::SINGLE_LENS() { START_ARRAY_LINE(); int totalStateNum = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write singles length. */ ARRAY_ITEM( INT(st->outSingle.length()), ++totalStateNum, st.last() ); } END_ARRAY_LINE(); return out; }
std::ostream &RubyTabCodeGen::RANGE_LENS() { START_ARRAY_LINE(); int totalStateNum = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Emit length of range index. */ ARRAY_ITEM( INT(st->outRange.length()), ++totalStateNum, st.last() ); } END_ARRAY_LINE(); return out; }
std::wostream &SplitCodeGen::EXIT_STATES( int partition ) { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( st->partition == partition && st->outNeeded ) { outLabelUsed = true; out << L" _out" << st->id << L": " << vCS() << L" = " << st->id << L"; goto _out; \n"; } } return out; }
std::ostream &JavaTabCodeGen::KEY_OFFSETS() { int curKeyOffset = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write the key offset. */ ARRAY_ITEM( INT(curKeyOffset), st.last() ); /* Move the key offset ahead. */ curKeyOffset += st->outSingle.length() + st->outRange.length()*2; } return out; }
void RedFsmAp::chooseDefaultNumRanges() { /* Loop the states. */ for ( RedStateList::Iter st = stateList; st.lte(); st++ ) { /* Pick a default transition. */ RedTransAp *defTrans = chooseDefaultNumRanges( st ); /* Rewrite the transition list taking out the transition we picked * as the default and store the default. */ moveToDefault( defTrans, st ); } }
void RedFsmAp::makeFlat() { for ( RedStateList::Iter st = stateList; st.lte(); st++ ) { if ( st->stateCondList.length() == 0 ) { st->condLowKey = 0; st->condHighKey = 0; } else { st->condLowKey = st->stateCondList.head->lowKey; st->condHighKey = st->stateCondList.tail->highKey; unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey ); st->condList = new GenCondSpace*[ span ]; memset( st->condList, 0, sizeof(GenCondSpace*)*span ); for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ ) { unsigned long long base, trSpan; base = keyOps->span( st->condLowKey, sci->lowKey )-1; trSpan = keyOps->span( sci->lowKey, sci->highKey ); for ( unsigned long long pos = 0; pos < trSpan; pos++ ) st->condList[base+pos] = sci->condSpace; } } if ( st->outRange.length() == 0 ) { st->lowKey = st->highKey = 0; st->transList = 0; } else { st->lowKey = st->outRange[0].lowKey; st->highKey = st->outRange[st->outRange.length()-1].highKey; unsigned long long span = keyOps->span( st->lowKey, st->highKey ); st->transList = new RedTransAp*[ span ]; memset( st->transList, 0, sizeof(RedTransAp*)*span ); for ( RedTransList::Iter trans = st->outRange; trans.lte(); trans++ ) { unsigned long long base, trSpan; base = keyOps->span( st->lowKey, trans->lowKey )-1; trSpan = keyOps->span( trans->lowKey, trans->highKey ); for ( unsigned long long pos = 0; pos < trSpan; pos++ ) st->transList[base+pos] = trans->value; } /* Fill in the gaps with the default transition. */ for ( unsigned long long pos = 0; pos < span; pos++ ) { if ( st->transList[pos] == 0 ) st->transList[pos] = st->defTrans; } } } }
void CodeGenData::closeMachine() { for ( GenActionList::Iter a = actionList; a.lte(); a++ ) resolveTargetStates( a->inlineList ); /* Note that even if we want a complete graph we do not give the error * state a default transition. All machines break out of the processing * loop when in the error state. */ for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ ) st->stateCondVect.append( sci ); } }
std::ostream &JavaTabCodeGen::INDEX_OFFSETS() { int curIndOffset = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write the index offset. */ ARRAY_ITEM( INT(curIndOffset), st.last() ); /* Move the index offset ahead. */ curIndOffset += st->outSingle.length() + st->outRange.length(); if ( st->defTrans != 0 ) curIndOffset += 1; } return out; }
std::ostream &RubyTabCodeGen::KEY_OFFSETS() { START_ARRAY_LINE(); int totalStateNum = 0, curKeyOffset = 0; for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Write the key offset. */ ARRAY_ITEM( INT(curKeyOffset), ++totalStateNum, st.last() ); /* Move the key offset ahead. */ curKeyOffset += st->outSingle.length() + st->outRange.length()*2; } END_ARRAY_LINE(); return out; }
std::wostream &FGotoCodeGen::FINISH_CASES() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* States that are final and have an out action need a case. */ if ( st->eofAction != 0 ) { /* Write the case label. */ out << L"\t\tcase " << st->id << L": "; /* Jump to the func. */ out << L"goto f" << st->eofAction->actListId << L";\n"; } } return out; }
std::ostream &JavaTabCodeGen::COND_SPACES() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { /* Loop the state's transitions. */ for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) { /* Cond Space id. */ ARRAY_ITEM( KEY( sc->condSpace->condSpaceId ), false ); } } /* Output one last number so we don't have to figure out when the last * entry is and avoid writing a comma. */ ARRAY_ITEM( INT(0), true ); return out; }