2 * Copyright 2005-2007 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 #include "dotcodegen.h"
50 #include "javacodegen.h"
52 #include "rubytable.h"
53 #include "rubyftable.h"
55 #include "rubyfflat.h"
61 sprintf( buf, "%i", i );
69 /* Invoked by the parser when a ragel definition is opened. */
70 CodeGenData *dotMakeCodeGen( const char *sourceFileName, const char *fsmName,
71 ostream &out, bool wantComplete )
73 CodeGenData *codeGen = new GraphvizDotGen(out);
75 codeGen->sourceFileName = sourceFileName;
76 codeGen->fsmName = fsmName;
77 codeGen->wantComplete = wantComplete;
82 /* Invoked by the parser when a ragel definition is opened. */
83 CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName,
84 ostream &out, bool wantComplete )
86 CodeGenData *codeGen = 0;
87 switch ( hostLang->lang ) {
89 switch ( codeStyle ) {
91 codeGen = new CTabCodeGen(out);
94 codeGen = new CFTabCodeGen(out);
97 codeGen = new CFlatCodeGen(out);
100 codeGen = new CFFlatCodeGen(out);
103 codeGen = new CGotoCodeGen(out);
106 codeGen = new CFGotoCodeGen(out);
109 codeGen = new CIpGotoCodeGen(out);
112 codeGen = new CSplitCodeGen(out);
118 switch ( codeStyle ) {
120 codeGen = new DTabCodeGen(out);
123 codeGen = new DFTabCodeGen(out);
126 codeGen = new DFlatCodeGen(out);
129 codeGen = new DFFlatCodeGen(out);
132 codeGen = new DGotoCodeGen(out);
135 codeGen = new DFGotoCodeGen(out);
138 codeGen = new DIpGotoCodeGen(out);
141 codeGen = new DSplitCodeGen(out);
149 codeGen->sourceFileName = sourceFileName;
150 codeGen->fsmName = fsmName;
151 codeGen->wantComplete = wantComplete;
156 /* Invoked by the parser when a ragel definition is opened. */
157 CodeGenData *javaMakeCodeGen( const char *sourceFileName, const char *fsmName,
158 ostream &out, bool wantComplete )
160 CodeGenData *codeGen = new JavaTabCodeGen(out);
162 codeGen->sourceFileName = sourceFileName;
163 codeGen->fsmName = fsmName;
164 codeGen->wantComplete = wantComplete;
169 /* Invoked by the parser when a ragel definition is opened. */
170 CodeGenData *rubyMakeCodeGen( const char *sourceFileName, const char *fsmName,
171 ostream &out, bool wantComplete )
173 CodeGenData *codeGen = 0;
174 switch ( codeStyle ) {
176 codeGen = new RubyTabCodeGen(out);
179 codeGen = new RubyFTabCodeGen(out);
182 codeGen = new RubyFlatCodeGen(out);
185 codeGen = new RubyFFlatCodeGen(out);
188 if ( rubyImpl == Rubinius ) {
189 codeGen = new RbxGotoCodeGen(out);
191 cerr << "Goto style is still _very_ experimental "
192 "and only supported using Rubinius.\n"
193 "You may want to enable the --rbx flag "
194 " to give it a try.\n";
199 cout << "Invalid code style\n";
203 codeGen->sourceFileName = sourceFileName;
204 codeGen->fsmName = fsmName;
205 codeGen->wantComplete = wantComplete;
210 /* Invoked by the parser when a ragel definition is opened. */
211 CodeGenData *csharpMakeCodeGen( const char *sourceFileName, const char *fsmName,
212 ostream &out, bool wantComplete )
214 CodeGenData *codeGen = 0;
216 switch ( codeStyle ) {
218 codeGen = new CSharpTabCodeGen(out);
221 codeGen = new CSharpFTabCodeGen(out);
224 codeGen = new CSharpFlatCodeGen(out);
227 codeGen = new CSharpFFlatCodeGen(out);
230 codeGen = new CSharpGotoCodeGen(out);
233 codeGen = new CSharpFGotoCodeGen(out);
236 codeGen = new CSharpIpGotoCodeGen(out);
239 codeGen = new CSharpSplitCodeGen(out);
243 codeGen->sourceFileName = sourceFileName;
244 codeGen->fsmName = fsmName;
245 codeGen->wantComplete = wantComplete;
251 CodeGenData *makeCodeGen( const char *sourceFileName, const char *fsmName,
252 ostream &out, bool wantComplete )
254 CodeGenData *cgd = 0;
256 cgd = dotMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
257 else if ( hostLang == &hostLangC )
258 cgd = cdMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
259 else if ( hostLang == &hostLangD )
260 cgd = cdMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
261 else if ( hostLang == &hostLangJava )
262 cgd = javaMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
263 else if ( hostLang == &hostLangRuby )
264 cgd = rubyMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
265 else if ( hostLang == &hostLangCSharp )
266 cgd = csharpMakeCodeGen( sourceFileName, fsmName, out, wantComplete );
270 void lineDirective( ostream &out, const char *fileName, int line )
272 if ( !generateDot ) {
273 if ( hostLang == &hostLangC )
274 cdLineDirective( out, fileName, line );
275 else if ( hostLang == &hostLangD )
276 cdLineDirective( out, fileName, line );
277 else if ( hostLang == &hostLangJava )
278 javaLineDirective( out, fileName, line );
279 else if ( hostLang == &hostLangRuby )
280 rubyLineDirective( out, fileName, line );
281 else if ( hostLang == &hostLangCSharp )
282 csharpLineDirective( out, fileName, line );
286 void genLineDirective( ostream &out )
288 std::streambuf *sbuf = out.rdbuf();
289 output_filter *filter = static_cast<output_filter*>(sbuf);
290 lineDirective( out, filter->fileName, filter->line + 1 );
294 /* Total error count. */
295 /* int gblErrorCount = 0; */
297 CodeGenData::CodeGenData( ostream &out )
326 hasLongestMatch(false),
335 void CodeGenData::createMachine()
337 redFsm = new RedFsmAp();
340 void CodeGenData::initActionList( unsigned long length )
342 allActions = new GenAction[length];
343 for ( unsigned long a = 0; a < length; a++ )
344 actionList.append( allActions+a );
347 void CodeGenData::newAction( int anum, const char *name, int line,
348 int col, GenInlineList *inlineList )
350 allActions[anum].actionId = anum;
351 allActions[anum].name = name;
352 allActions[anum].loc.line = line;
353 allActions[anum].loc.col = col;
354 allActions[anum].inlineList = inlineList;
357 void CodeGenData::initActionTableList( unsigned long length )
359 allActionTables = new RedAction[length];
362 void CodeGenData::initStateList( unsigned long length )
364 allStates = new RedStateAp[length];
365 for ( unsigned long s = 0; s < length; s++ )
366 redFsm->stateList.append( allStates+s );
368 /* We get the start state as an offset, set the pointer now. */
369 if ( startState >= 0 )
370 redFsm->startState = allStates + startState;
372 redFsm->errState = allStates + errState;
373 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
374 redFsm->entryPoints.insert( allStates + *en );
376 /* The nextStateId is no longer used to assign state ids (they come in set
377 * from the frontend now), however generation code still depends on it.
378 * Should eventually remove this variable. */
379 redFsm->nextStateId = redFsm->stateList.length();
382 void CodeGenData::setStartState( unsigned long startState )
384 this->startState = startState;
387 void CodeGenData::setErrorState( unsigned long errState )
389 this->errState = errState;
392 void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
394 entryPointIds.append( entryState );
395 entryPointNames.append( name );
398 void CodeGenData::initTransList( int snum, unsigned long length )
400 /* Could preallocate the out range to save time growing it. For now do
404 void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
405 Key highKey, long targ, long action )
407 /* Get the current state and range. */
408 RedStateAp *curState = allStates + snum;
409 RedTransList &destRange = curState->outRange;
411 if ( curState == redFsm->errState )
414 /* Make the new transitions. */
415 RedStateAp *targState = targ >= 0 ? (allStates + targ) :
416 wantComplete ? redFsm->getErrorState() : 0;
417 RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
418 RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
419 RedTransEl transEl( lowKey, highKey, trans );
421 if ( wantComplete ) {
422 /* If the machine is to be complete then we need to fill any gaps with
423 * the error transitions. */
424 if ( destRange.length() == 0 ) {
425 /* Range is currently empty. */
426 if ( keyOps->minKey < lowKey ) {
427 /* The first range doesn't start at the low end. */
428 Key fillHighKey = lowKey;
429 fillHighKey.decrement();
431 /* Create the filler with the state's error transition. */
432 RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
433 destRange.append( newTel );
437 /* The range list is not empty, get the the last range. */
438 RedTransEl *last = &destRange[destRange.length()-1];
439 Key nextKey = last->highKey;
441 if ( nextKey < lowKey ) {
442 /* There is a gap to fill. Make the high key. */
443 Key fillHighKey = lowKey;
444 fillHighKey.decrement();
446 /* Create the filler with the state's error transtion. */
447 RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
448 destRange.append( newTel );
453 /* Filler taken care of. Append the range. */
454 destRange.append( RedTransEl( lowKey, highKey, trans ) );
457 void CodeGenData::finishTransList( int snum )
459 /* Get the current state and range. */
460 RedStateAp *curState = allStates + snum;
461 RedTransList &destRange = curState->outRange;
463 if ( curState == redFsm->errState )
466 /* If building a complete machine we may need filler on the end. */
467 if ( wantComplete ) {
468 /* Check if there are any ranges already. */
469 if ( destRange.length() == 0 ) {
470 /* Fill with the whole alphabet. */
471 /* Add the range on the lower and upper bound. */
472 RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
473 destRange.append( newTel );
476 /* Get the last and check for a gap on the end. */
477 RedTransEl *last = &destRange[destRange.length()-1];
478 if ( last->highKey < keyOps->maxKey ) {
479 /* Make the high key. */
480 Key fillLowKey = last->highKey;
481 fillLowKey.increment();
483 /* Create the new range with the error trans and append it. */
484 RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
485 destRange.append( newTel );
491 void CodeGenData::setId( int snum, int id )
493 RedStateAp *curState = allStates + snum;
497 void CodeGenData::setFinal( int snum )
499 RedStateAp *curState = allStates + snum;
500 curState->isFinal = true;
504 void CodeGenData::setStateActions( int snum, long toStateAction,
505 long fromStateAction, long eofAction )
507 RedStateAp *curState = allStates + snum;
508 if ( toStateAction >= 0 )
509 curState->toStateAction = allActionTables + toStateAction;
510 if ( fromStateAction >= 0 )
511 curState->fromStateAction = allActionTables + fromStateAction;
512 if ( eofAction >= 0 )
513 curState->eofAction = allActionTables + eofAction;
516 void CodeGenData::setEofTrans( int snum, long eofTarget, long actId )
518 RedStateAp *curState = allStates + snum;
519 RedStateAp *targState = allStates + eofTarget;
520 RedAction *eofAct = allActionTables + actId;
521 curState->eofTrans = redFsm->allocateTrans( targState, eofAct );
524 void CodeGenData::resolveTargetStates( GenInlineList *inlineList )
526 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
527 switch ( item->type ) {
528 case GenInlineItem::Goto: case GenInlineItem::Call:
529 case GenInlineItem::Next: case GenInlineItem::Entry:
530 item->targState = allStates + item->targId;
536 if ( item->children != 0 )
537 resolveTargetStates( item->children );
541 void CodeGenData::closeMachine()
543 for ( GenActionList::Iter a = actionList; a.lte(); a++ )
544 resolveTargetStates( a->inlineList );
546 /* Note that even if we want a complete graph we do not give the error
547 * state a default transition. All machines break out of the processing
548 * loop when in the error state. */
550 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
551 for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
552 st->stateCondVect.append( sci );
557 bool CodeGenData::setAlphType( const char *data )
559 HostType *alphType = findAlphTypeInternal( data );
563 thisKeyOps.setAlphType( alphType );
567 void CodeGenData::initCondSpaceList( ulong length )
569 allCondSpaces = new GenCondSpace[length];
570 for ( ulong c = 0; c < length; c++ )
571 condSpaceList.append( allCondSpaces + c );
574 void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
576 GenCondSpace *cond = allCondSpaces + cnum;
577 cond->condSpaceId = condSpaceId;
578 cond->baseKey = baseKey;
581 void CodeGenData::condSpaceItem( int cnum, long condActionId )
583 GenCondSpace *cond = allCondSpaces + cnum;
584 cond->condSet.append( allActions + condActionId );
587 void CodeGenData::initStateCondList( int snum, ulong length )
589 /* Could preallocate these, as we could with transitions. */
592 void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
594 RedStateAp *curState = allStates + snum;
596 /* Create the new state condition. */
597 GenStateCond *stateCond = new GenStateCond;
598 stateCond->lowKey = lowKey;
599 stateCond->highKey = highKey;
601 /* Assign it a cond space. */
602 GenCondSpace *condSpace = allCondSpaces + condNum;
603 stateCond->condSpace = condSpace;
605 curState->stateCondList.append( stateCond );
609 GenCondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
611 for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
612 Key csHighKey = cs->baseKey;
613 csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
615 if ( lowKey >= cs->baseKey && highKey <= csHighKey )
621 Condition *CodeGenData::findCondition( Key key )
623 for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
624 Key upperKey = cond->baseKey + (1 << cond->condSet.length());
625 if ( cond->baseKey <= key && key <= upperKey )
631 Key CodeGenData::findMaxKey()
633 Key maxKey = keyOps->maxKey;
634 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
635 assert( st->outSingle.length() == 0 );
636 assert( st->defTrans == 0 );
638 long rangeLen = st->outRange.length();
639 if ( rangeLen > 0 ) {
640 Key highKey = st->outRange[rangeLen-1].highKey;
641 if ( highKey > maxKey )
648 void CodeGenData::findFinalActionRefs()
650 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
651 /* Rerence count out of single transitions. */
652 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
653 if ( rtel->value->action != 0 ) {
654 rtel->value->action->numTransRefs += 1;
655 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
656 item->value->numTransRefs += 1;
660 /* Reference count out of range transitions. */
661 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
662 if ( rtel->value->action != 0 ) {
663 rtel->value->action->numTransRefs += 1;
664 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
665 item->value->numTransRefs += 1;
669 /* Reference count default transition. */
670 if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
671 st->defTrans->action->numTransRefs += 1;
672 for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
673 item->value->numTransRefs += 1;
676 /* Reference count eof transitions. */
677 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
678 st->eofTrans->action->numTransRefs += 1;
679 for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ )
680 item->value->numTransRefs += 1;
683 /* Reference count to state actions. */
684 if ( st->toStateAction != 0 ) {
685 st->toStateAction->numToStateRefs += 1;
686 for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
687 item->value->numToStateRefs += 1;
690 /* Reference count from state actions. */
691 if ( st->fromStateAction != 0 ) {
692 st->fromStateAction->numFromStateRefs += 1;
693 for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
694 item->value->numFromStateRefs += 1;
697 /* Reference count EOF actions. */
698 if ( st->eofAction != 0 ) {
699 st->eofAction->numEofRefs += 1;
700 for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
701 item->value->numEofRefs += 1;
706 void CodeGenData::analyzeAction( GenAction *act, GenInlineList *inlineList )
708 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
709 /* Only consider actions that are referenced. */
710 if ( act->numRefs() > 0 ) {
711 if ( item->type == GenInlineItem::Goto || item->type == GenInlineItem::GotoExpr )
712 redFsm->bAnyActionGotos = true;
713 else if ( item->type == GenInlineItem::Call || item->type == GenInlineItem::CallExpr )
714 redFsm->bAnyActionCalls = true;
715 else if ( item->type == GenInlineItem::Ret )
716 redFsm->bAnyActionRets = true;
719 /* Check for various things in regular actions. */
720 if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
721 /* Any returns in regular actions? */
722 if ( item->type == GenInlineItem::Ret )
723 redFsm->bAnyRegActionRets = true;
725 /* Any next statements in the regular actions? */
726 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
727 redFsm->bAnyRegNextStmt = true;
729 /* Any by value control in regular actions? */
730 if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
731 redFsm->bAnyRegActionByValControl = true;
733 /* Any references to the current state in regular actions? */
734 if ( item->type == GenInlineItem::Curs )
735 redFsm->bAnyRegCurStateRef = true;
737 if ( item->type == GenInlineItem::Break )
738 redFsm->bAnyRegBreak = true;
741 if ( item->children != 0 )
742 analyzeAction( act, item->children );
746 void CodeGenData::analyzeActionList( RedAction *redAct, GenInlineList *inlineList )
748 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
749 /* Any next statements in the action table? */
750 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
751 redAct->bAnyNextStmt = true;
753 /* Any references to the current state. */
754 if ( item->type == GenInlineItem::Curs )
755 redAct->bAnyCurStateRef = true;
757 if ( item->type == GenInlineItem::Break )
758 redAct->bAnyBreakStmt = true;
760 if ( item->children != 0 )
761 analyzeActionList( redAct, item->children );
765 /* Assign ids to referenced actions. */
766 void CodeGenData::assignActionIds()
768 int nextActionId = 0;
769 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
770 /* Only ever interested in referenced actions. */
771 if ( act->numRefs() > 0 )
772 act->actionId = nextActionId++;
776 void CodeGenData::setValueLimits()
778 redFsm->maxSingleLen = 0;
779 redFsm->maxRangeLen = 0;
780 redFsm->maxKeyOffset = 0;
781 redFsm->maxIndexOffset = 0;
782 redFsm->maxActListId = 0;
783 redFsm->maxActionLoc = 0;
784 redFsm->maxActArrItem = 0;
786 redFsm->maxCondSpan = 0;
787 redFsm->maxFlatIndexOffset = 0;
788 redFsm->maxCondOffset = 0;
789 redFsm->maxCondLen = 0;
790 redFsm->maxCondSpaceId = 0;
791 redFsm->maxCondIndexOffset = 0;
793 /* In both of these cases the 0 index is reserved for no value, so the max
794 * is one more than it would be if they started at 0. */
795 redFsm->maxIndex = redFsm->transSet.length();
796 redFsm->maxCond = condSpaceList.length();
798 /* The nextStateId - 1 is the last state id assigned. */
799 redFsm->maxState = redFsm->nextStateId - 1;
801 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
802 if ( csi->condSpaceId > redFsm->maxCondSpaceId )
803 redFsm->maxCondSpaceId = csi->condSpaceId;
806 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
807 /* Maximum cond length. */
808 if ( st->stateCondList.length() > redFsm->maxCondLen )
809 redFsm->maxCondLen = st->stateCondList.length();
811 /* Maximum single length. */
812 if ( st->outSingle.length() > redFsm->maxSingleLen )
813 redFsm->maxSingleLen = st->outSingle.length();
815 /* Maximum range length. */
816 if ( st->outRange.length() > redFsm->maxRangeLen )
817 redFsm->maxRangeLen = st->outRange.length();
819 /* The key offset index offset for the state after last is not used, skip it.. */
821 redFsm->maxCondOffset += st->stateCondList.length();
822 redFsm->maxKeyOffset += st->outSingle.length() + st->outRange.length()*2;
823 redFsm->maxIndexOffset += st->outSingle.length() + st->outRange.length() + 2;
827 if ( st->condList != 0 ) {
828 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
829 if ( span > redFsm->maxCondSpan )
830 redFsm->maxCondSpan = span;
834 if ( st->transList != 0 ) {
835 unsigned long long span = keyOps->span( st->lowKey, st->highKey );
836 if ( span > redFsm->maxSpan )
837 redFsm->maxSpan = span;
840 /* Max cond index offset. */
842 if ( st->condList != 0 )
843 redFsm->maxCondIndexOffset += keyOps->span( st->condLowKey, st->condHighKey );
846 /* Max flat index offset. */
848 if ( st->transList != 0 )
849 redFsm->maxFlatIndexOffset += keyOps->span( st->lowKey, st->highKey );
850 redFsm->maxFlatIndexOffset += 1;
854 for ( GenActionTableMap::Iter at = redFsm->actionMap; at.lte(); at++ ) {
855 /* Maximum id of action lists. */
856 if ( at->actListId+1 > redFsm->maxActListId )
857 redFsm->maxActListId = at->actListId+1;
859 /* Maximum location of items in action array. */
860 if ( at->location+1 > redFsm->maxActionLoc )
861 redFsm->maxActionLoc = at->location+1;
863 /* Maximum values going into the action array. */
864 if ( at->key.length() > redFsm->maxActArrItem )
865 redFsm->maxActArrItem = at->key.length();
866 for ( GenActionTable::Iter item = at->key; item.lte(); item++ ) {
867 if ( item->value->actionId > redFsm->maxActArrItem )
868 redFsm->maxActArrItem = item->value->actionId;
875 /* Gather various info on the machine. */
876 void CodeGenData::analyzeMachine()
878 /* Find the true count of action references. */
879 findFinalActionRefs();
881 /* Check if there are any calls in action code. */
882 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
883 /* Record the occurrence of various kinds of actions. */
884 if ( act->numToStateRefs > 0 )
885 redFsm->bAnyToStateActions = true;
886 if ( act->numFromStateRefs > 0 )
887 redFsm->bAnyFromStateActions = true;
888 if ( act->numEofRefs > 0 )
889 redFsm->bAnyEofActions = true;
890 if ( act->numTransRefs > 0 )
891 redFsm->bAnyRegActions = true;
893 /* Recurse through the action's parse tree looking for various things. */
894 analyzeAction( act, act->inlineList );
897 /* Analyze reduced action lists. */
898 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
899 for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ )
900 analyzeActionList( redAct, act->value->inlineList );
903 /* Find states that have transitions with actions that have next
905 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
906 /* Check any actions out of outSinge. */
907 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
908 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
909 st->bAnyRegCurStateRef = true;
912 /* Check any actions out of outRange. */
913 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
914 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
915 st->bAnyRegCurStateRef = true;
918 /* Check any action out of default. */
919 if ( st->defTrans != 0 && st->defTrans->action != 0 &&
920 st->defTrans->action->anyCurStateRef() )
921 st->bAnyRegCurStateRef = true;
923 if ( st->stateCondList.length() > 0 )
924 redFsm->bAnyConditions = true;
926 if ( st->eofTrans != 0 )
927 redFsm->bAnyEofTrans = true;
930 /* Assign ids to actions that are referenced. */
933 /* Set the maximums of various values used for deciding types. */
937 void CodeGenData::write_option_error( InputLoc &loc, char *arg )
939 source_warning(loc) << "unrecognized write option \"" << arg << "\"" << endl;
942 void CodeGenData::writeStatement( InputLoc &loc, int nargs, char **args )
944 /* FIXME: This should be moved to the virtual functions in the code
947 * Force a newline. */
949 genLineDirective( out );
951 if ( strcmp( args[0], "data" ) == 0 ) {
952 for ( int i = 1; i < nargs; i++ ) {
953 if ( strcmp( args[i], "noerror" ) == 0 )
955 else if ( strcmp( args[i], "noprefix" ) == 0 )
957 else if ( strcmp( args[i], "nofinal" ) == 0 )
960 write_option_error( loc, args[i] );
964 else if ( strcmp( args[0], "init" ) == 0 ) {
965 for ( int i = 1; i < nargs; i++ ) {
966 if ( strcmp( args[i], "nocs" ) == 0 )
969 write_option_error( loc, args[i] );
973 else if ( strcmp( args[0], "exec" ) == 0 ) {
974 for ( int i = 1; i < nargs; i++ ) {
975 if ( strcmp( args[i], "noend" ) == 0 )
978 write_option_error( loc, args[i] );
982 else if ( strcmp( args[0], "exports" ) == 0 ) {
983 for ( int i = 1; i < nargs; i++ )
984 write_option_error( loc, args[i] );
987 else if ( strcmp( args[0], "start" ) == 0 ) {
988 for ( int i = 1; i < nargs; i++ )
989 write_option_error( loc, args[i] );
992 else if ( strcmp( args[0], "first_final" ) == 0 ) {
993 for ( int i = 1; i < nargs; i++ )
994 write_option_error( loc, args[i] );
997 else if ( strcmp( args[0], "error" ) == 0 ) {
998 for ( int i = 1; i < nargs; i++ )
999 write_option_error( loc, args[i] );
1003 /* EMIT An error here. */
1004 source_error(loc) << "unrecognized write command \"" <<
1005 args[0] << "\"" << endl;
1009 ostream &CodeGenData::source_warning( const InputLoc &loc )
1011 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
1015 ostream &CodeGenData::source_error( const InputLoc &loc )
1018 assert( sourceFileName != 0 );
1019 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";