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, ostream &out )
72 CodeGenData *codeGen = new GraphvizDotGen(out);
74 codeGen->sourceFileName = sourceFileName;
75 codeGen->fsmName = fsmName;
77 /* For normal code generation we want a transition on every character so we never
78 * end up in an undefined state. For graphviz this just clutters the
79 * drawing so we turn it off. */
80 codeGen->wantComplete = false;
85 /* Invoked by the parser when a ragel definition is opened. */
86 CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
88 CodeGenData *codeGen = 0;
89 switch ( hostLang->lang ) {
91 switch ( codeStyle ) {
93 codeGen = new CTabCodeGen(out);
96 codeGen = new CFTabCodeGen(out);
99 codeGen = new CFlatCodeGen(out);
102 codeGen = new CFFlatCodeGen(out);
105 codeGen = new CGotoCodeGen(out);
108 codeGen = new CFGotoCodeGen(out);
111 codeGen = new CIpGotoCodeGen(out);
114 codeGen = new CSplitCodeGen(out);
120 switch ( codeStyle ) {
122 codeGen = new DTabCodeGen(out);
125 codeGen = new DFTabCodeGen(out);
128 codeGen = new DFlatCodeGen(out);
131 codeGen = new DFFlatCodeGen(out);
134 codeGen = new DGotoCodeGen(out);
137 codeGen = new DFGotoCodeGen(out);
140 codeGen = new DIpGotoCodeGen(out);
143 codeGen = new DSplitCodeGen(out);
151 codeGen->sourceFileName = sourceFileName;
152 codeGen->fsmName = fsmName;
157 /* Invoked by the parser when a ragel definition is opened. */
158 CodeGenData *javaMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
160 CodeGenData *codeGen = new JavaTabCodeGen(out);
162 codeGen->sourceFileName = sourceFileName;
163 codeGen->fsmName = fsmName;
168 /* Invoked by the parser when a ragel definition is opened. */
169 CodeGenData *rubyMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
171 CodeGenData *codeGen = 0;
172 switch ( codeStyle ) {
174 codeGen = new RubyTabCodeGen(out);
177 codeGen = new RubyFTabCodeGen(out);
180 codeGen = new RubyFlatCodeGen(out);
183 codeGen = new RubyFFlatCodeGen(out);
186 if ( rubyImpl == Rubinius ) {
187 codeGen = new RbxGotoCodeGen(out);
189 cerr << "Goto style is still _very_ experimental "
190 "and only supported using Rubinius.\n"
191 "You may want to enable the --rbx flag "
192 " to give it a try.\n";
197 cout << "Invalid code style\n";
201 codeGen->sourceFileName = sourceFileName;
202 codeGen->fsmName = fsmName;
207 /* Invoked by the parser when a ragel definition is opened. */
208 CodeGenData *csharpMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
210 CodeGenData *codeGen = 0;
212 switch ( codeStyle ) {
214 codeGen = new CSharpTabCodeGen(out);
217 codeGen = new CSharpFTabCodeGen(out);
220 codeGen = new CSharpFlatCodeGen(out);
223 codeGen = new CSharpFFlatCodeGen(out);
226 codeGen = new CSharpGotoCodeGen(out);
229 codeGen = new CSharpFGotoCodeGen(out);
232 codeGen = new CSharpIpGotoCodeGen(out);
235 codeGen = new CSharpSplitCodeGen(out);
239 codeGen->sourceFileName = sourceFileName;
240 codeGen->fsmName = fsmName;
246 CodeGenData *makeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
248 CodeGenData *cgd = 0;
250 cgd = dotMakeCodeGen( sourceFileName, fsmName, out );
251 else if ( hostLang == &hostLangC )
252 cgd = cdMakeCodeGen( sourceFileName, fsmName, out );
253 else if ( hostLang == &hostLangD )
254 cgd = cdMakeCodeGen( sourceFileName, fsmName, out );
255 else if ( hostLang == &hostLangJava )
256 cgd = javaMakeCodeGen( sourceFileName, fsmName, out );
257 else if ( hostLang == &hostLangRuby )
258 cgd = rubyMakeCodeGen( sourceFileName, fsmName, out );
259 else if ( hostLang == &hostLangCSharp )
260 cgd = csharpMakeCodeGen( sourceFileName, fsmName, out );
264 void lineDirective( ostream &out, const char *fileName, int line )
266 if ( !generateDot ) {
267 if ( hostLang == &hostLangC )
268 cdLineDirective( out, fileName, line );
269 else if ( hostLang == &hostLangD )
270 cdLineDirective( out, fileName, line );
271 else if ( hostLang == &hostLangJava )
272 javaLineDirective( out, fileName, line );
273 else if ( hostLang == &hostLangRuby )
274 rubyLineDirective( out, fileName, line );
275 else if ( hostLang == &hostLangCSharp )
276 csharpLineDirective( out, fileName, line );
280 void genLineDirective( ostream &out )
282 std::streambuf *sbuf = out.rdbuf();
283 output_filter *filter = static_cast<output_filter*>(sbuf);
284 lineDirective( out, filter->fileName, filter->line + 1 );
288 /* Total error count. */
289 /* int gblErrorCount = 0; */
291 CodeGenData::CodeGenData( ostream &out )
320 hasLongestMatch(false),
329 void CodeGenData::createMachine()
331 redFsm = new RedFsmAp();
334 void CodeGenData::initActionList( unsigned long length )
336 allActions = new GenAction[length];
337 for ( unsigned long a = 0; a < length; a++ )
338 actionList.append( allActions+a );
341 void CodeGenData::newAction( int anum, const char *name,
342 const InputLoc &loc, GenInlineList *inlineList )
344 allActions[anum].actionId = anum;
345 allActions[anum].name = name;
346 allActions[anum].loc = loc;
347 allActions[anum].inlineList = inlineList;
350 void CodeGenData::initActionTableList( unsigned long length )
352 allActionTables = new RedAction[length];
355 void CodeGenData::initStateList( unsigned long length )
357 allStates = new RedStateAp[length];
358 for ( unsigned long s = 0; s < length; s++ )
359 redFsm->stateList.append( allStates+s );
361 /* We get the start state as an offset, set the pointer now. */
362 if ( startState >= 0 )
363 redFsm->startState = allStates + startState;
365 redFsm->errState = allStates + errState;
366 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
367 redFsm->entryPoints.insert( allStates + *en );
369 /* The nextStateId is no longer used to assign state ids (they come in set
370 * from the frontend now), however generation code still depends on it.
371 * Should eventually remove this variable. */
372 redFsm->nextStateId = redFsm->stateList.length();
375 void CodeGenData::setStartState( unsigned long startState )
377 this->startState = startState;
380 void CodeGenData::setErrorState( unsigned long errState )
382 this->errState = errState;
385 void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
387 entryPointIds.append( entryState );
388 entryPointNames.append( name );
391 void CodeGenData::initTransList( int snum, unsigned long length )
393 /* Could preallocate the out range to save time growing it. For now do
397 void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
398 Key highKey, long targ, long action )
400 /* Get the current state and range. */
401 RedStateAp *curState = allStates + snum;
402 RedTransList &destRange = curState->outRange;
404 if ( curState == redFsm->errState )
407 /* Make the new transitions. */
408 RedStateAp *targState = targ >= 0 ? (allStates + targ) :
409 wantComplete ? redFsm->getErrorState() : 0;
410 RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
411 RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
412 RedTransEl transEl( lowKey, highKey, trans );
414 if ( wantComplete ) {
415 /* If the machine is to be complete then we need to fill any gaps with
416 * the error transitions. */
417 if ( destRange.length() == 0 ) {
418 /* Range is currently empty. */
419 if ( keyOps->minKey < lowKey ) {
420 /* The first range doesn't start at the low end. */
421 Key fillHighKey = lowKey;
422 fillHighKey.decrement();
424 /* Create the filler with the state's error transition. */
425 RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
426 destRange.append( newTel );
430 /* The range list is not empty, get the the last range. */
431 RedTransEl *last = &destRange[destRange.length()-1];
432 Key nextKey = last->highKey;
434 if ( nextKey < lowKey ) {
435 /* There is a gap to fill. Make the high key. */
436 Key fillHighKey = lowKey;
437 fillHighKey.decrement();
439 /* Create the filler with the state's error transtion. */
440 RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
441 destRange.append( newTel );
446 /* Filler taken care of. Append the range. */
447 destRange.append( RedTransEl( lowKey, highKey, trans ) );
450 void CodeGenData::finishTransList( int snum )
452 /* Get the current state and range. */
453 RedStateAp *curState = allStates + snum;
454 RedTransList &destRange = curState->outRange;
456 if ( curState == redFsm->errState )
459 /* If building a complete machine we may need filler on the end. */
460 if ( wantComplete ) {
461 /* Check if there are any ranges already. */
462 if ( destRange.length() == 0 ) {
463 /* Fill with the whole alphabet. */
464 /* Add the range on the lower and upper bound. */
465 RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
466 destRange.append( newTel );
469 /* Get the last and check for a gap on the end. */
470 RedTransEl *last = &destRange[destRange.length()-1];
471 if ( last->highKey < keyOps->maxKey ) {
472 /* Make the high key. */
473 Key fillLowKey = last->highKey;
474 fillLowKey.increment();
476 /* Create the new range with the error trans and append it. */
477 RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
478 destRange.append( newTel );
484 void CodeGenData::setId( int snum, int id )
486 RedStateAp *curState = allStates + snum;
490 void CodeGenData::setFinal( int snum )
492 RedStateAp *curState = allStates + snum;
493 curState->isFinal = true;
497 void CodeGenData::setStateActions( int snum, long toStateAction,
498 long fromStateAction, long eofAction )
500 RedStateAp *curState = allStates + snum;
501 if ( toStateAction >= 0 )
502 curState->toStateAction = allActionTables + toStateAction;
503 if ( fromStateAction >= 0 )
504 curState->fromStateAction = allActionTables + fromStateAction;
505 if ( eofAction >= 0 )
506 curState->eofAction = allActionTables + eofAction;
509 void CodeGenData::setEofTrans( int snum, long eofTarget, long actId )
511 RedStateAp *curState = allStates + snum;
512 RedStateAp *targState = allStates + eofTarget;
513 RedAction *eofAct = allActionTables + actId;
514 curState->eofTrans = redFsm->allocateTrans( targState, eofAct );
517 void CodeGenData::resolveTargetStates( GenInlineList *inlineList )
519 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
520 switch ( item->type ) {
521 case GenInlineItem::Goto: case GenInlineItem::Call:
522 case GenInlineItem::Next: case GenInlineItem::Entry:
523 item->targState = allStates + item->targId;
529 if ( item->children != 0 )
530 resolveTargetStates( item->children );
534 void CodeGenData::closeMachine()
536 for ( GenActionList::Iter a = actionList; a.lte(); a++ )
537 resolveTargetStates( a->inlineList );
539 /* Note that even if we want a complete graph we do not give the error
540 * state a default transition. All machines break out of the processing
541 * loop when in the error state. */
543 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
544 for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
545 st->stateCondVect.append( sci );
550 bool CodeGenData::setAlphType( const char *data )
552 HostType *alphType = findAlphTypeInternal( data );
556 thisKeyOps.setAlphType( alphType );
560 void CodeGenData::initCondSpaceList( ulong length )
562 allCondSpaces = new GenCondSpace[length];
563 for ( ulong c = 0; c < length; c++ )
564 condSpaceList.append( allCondSpaces + c );
567 void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
569 GenCondSpace *cond = allCondSpaces + cnum;
570 cond->condSpaceId = condSpaceId;
571 cond->baseKey = baseKey;
574 void CodeGenData::condSpaceItem( int cnum, long condActionId )
576 GenCondSpace *cond = allCondSpaces + cnum;
577 cond->condSet.append( allActions + condActionId );
580 void CodeGenData::initStateCondList( int snum, ulong length )
582 /* Could preallocate these, as we could with transitions. */
585 void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
587 RedStateAp *curState = allStates + snum;
589 /* Create the new state condition. */
590 GenStateCond *stateCond = new GenStateCond;
591 stateCond->lowKey = lowKey;
592 stateCond->highKey = highKey;
594 /* Assign it a cond space. */
595 GenCondSpace *condSpace = allCondSpaces + condNum;
596 stateCond->condSpace = condSpace;
598 curState->stateCondList.append( stateCond );
602 GenCondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
604 for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
605 Key csHighKey = cs->baseKey;
606 csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
608 if ( lowKey >= cs->baseKey && highKey <= csHighKey )
614 Condition *CodeGenData::findCondition( Key key )
616 for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
617 Key upperKey = cond->baseKey + (1 << cond->condSet.length());
618 if ( cond->baseKey <= key && key <= upperKey )
624 Key CodeGenData::findMaxKey()
626 Key maxKey = keyOps->maxKey;
627 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
628 assert( st->outSingle.length() == 0 );
629 assert( st->defTrans == 0 );
631 long rangeLen = st->outRange.length();
632 if ( rangeLen > 0 ) {
633 Key highKey = st->outRange[rangeLen-1].highKey;
634 if ( highKey > maxKey )
641 void CodeGenData::findFinalActionRefs()
643 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
644 /* Rerence count out of single transitions. */
645 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
646 if ( rtel->value->action != 0 ) {
647 rtel->value->action->numTransRefs += 1;
648 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
649 item->value->numTransRefs += 1;
653 /* Reference count out of range transitions. */
654 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
655 if ( rtel->value->action != 0 ) {
656 rtel->value->action->numTransRefs += 1;
657 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
658 item->value->numTransRefs += 1;
662 /* Reference count default transition. */
663 if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
664 st->defTrans->action->numTransRefs += 1;
665 for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
666 item->value->numTransRefs += 1;
669 /* Reference count eof transitions. */
670 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
671 st->eofTrans->action->numTransRefs += 1;
672 for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ )
673 item->value->numTransRefs += 1;
676 /* Reference count to state actions. */
677 if ( st->toStateAction != 0 ) {
678 st->toStateAction->numToStateRefs += 1;
679 for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
680 item->value->numToStateRefs += 1;
683 /* Reference count from state actions. */
684 if ( st->fromStateAction != 0 ) {
685 st->fromStateAction->numFromStateRefs += 1;
686 for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
687 item->value->numFromStateRefs += 1;
690 /* Reference count EOF actions. */
691 if ( st->eofAction != 0 ) {
692 st->eofAction->numEofRefs += 1;
693 for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
694 item->value->numEofRefs += 1;
699 void CodeGenData::analyzeAction( GenAction *act, GenInlineList *inlineList )
701 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
702 /* Only consider actions that are referenced. */
703 if ( act->numRefs() > 0 ) {
704 if ( item->type == GenInlineItem::Goto || item->type == GenInlineItem::GotoExpr )
705 redFsm->bAnyActionGotos = true;
706 else if ( item->type == GenInlineItem::Call || item->type == GenInlineItem::CallExpr )
707 redFsm->bAnyActionCalls = true;
708 else if ( item->type == GenInlineItem::Ret )
709 redFsm->bAnyActionRets = true;
712 /* Check for various things in regular actions. */
713 if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
714 /* Any returns in regular actions? */
715 if ( item->type == GenInlineItem::Ret )
716 redFsm->bAnyRegActionRets = true;
718 /* Any next statements in the regular actions? */
719 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
720 redFsm->bAnyRegNextStmt = true;
722 /* Any by value control in regular actions? */
723 if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
724 redFsm->bAnyRegActionByValControl = true;
726 /* Any references to the current state in regular actions? */
727 if ( item->type == GenInlineItem::Curs )
728 redFsm->bAnyRegCurStateRef = true;
730 if ( item->type == GenInlineItem::Break )
731 redFsm->bAnyRegBreak = true;
734 if ( item->children != 0 )
735 analyzeAction( act, item->children );
739 void CodeGenData::analyzeActionList( RedAction *redAct, GenInlineList *inlineList )
741 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
742 /* Any next statements in the action table? */
743 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
744 redAct->bAnyNextStmt = true;
746 /* Any references to the current state. */
747 if ( item->type == GenInlineItem::Curs )
748 redAct->bAnyCurStateRef = true;
750 if ( item->type == GenInlineItem::Break )
751 redAct->bAnyBreakStmt = true;
753 if ( item->children != 0 )
754 analyzeActionList( redAct, item->children );
758 /* Assign ids to referenced actions. */
759 void CodeGenData::assignActionIds()
761 int nextActionId = 0;
762 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
763 /* Only ever interested in referenced actions. */
764 if ( act->numRefs() > 0 )
765 act->actionId = nextActionId++;
769 void CodeGenData::setValueLimits()
771 redFsm->maxSingleLen = 0;
772 redFsm->maxRangeLen = 0;
773 redFsm->maxKeyOffset = 0;
774 redFsm->maxIndexOffset = 0;
775 redFsm->maxActListId = 0;
776 redFsm->maxActionLoc = 0;
777 redFsm->maxActArrItem = 0;
779 redFsm->maxCondSpan = 0;
780 redFsm->maxFlatIndexOffset = 0;
781 redFsm->maxCondOffset = 0;
782 redFsm->maxCondLen = 0;
783 redFsm->maxCondSpaceId = 0;
784 redFsm->maxCondIndexOffset = 0;
786 /* In both of these cases the 0 index is reserved for no value, so the max
787 * is one more than it would be if they started at 0. */
788 redFsm->maxIndex = redFsm->transSet.length();
789 redFsm->maxCond = condSpaceList.length();
791 /* The nextStateId - 1 is the last state id assigned. */
792 redFsm->maxState = redFsm->nextStateId - 1;
794 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
795 if ( csi->condSpaceId > redFsm->maxCondSpaceId )
796 redFsm->maxCondSpaceId = csi->condSpaceId;
799 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
800 /* Maximum cond length. */
801 if ( st->stateCondList.length() > redFsm->maxCondLen )
802 redFsm->maxCondLen = st->stateCondList.length();
804 /* Maximum single length. */
805 if ( st->outSingle.length() > redFsm->maxSingleLen )
806 redFsm->maxSingleLen = st->outSingle.length();
808 /* Maximum range length. */
809 if ( st->outRange.length() > redFsm->maxRangeLen )
810 redFsm->maxRangeLen = st->outRange.length();
812 /* The key offset index offset for the state after last is not used, skip it.. */
814 redFsm->maxCondOffset += st->stateCondList.length();
815 redFsm->maxKeyOffset += st->outSingle.length() + st->outRange.length()*2;
816 redFsm->maxIndexOffset += st->outSingle.length() + st->outRange.length() + 2;
820 if ( st->condList != 0 ) {
821 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
822 if ( span > redFsm->maxCondSpan )
823 redFsm->maxCondSpan = span;
827 if ( st->transList != 0 ) {
828 unsigned long long span = keyOps->span( st->lowKey, st->highKey );
829 if ( span > redFsm->maxSpan )
830 redFsm->maxSpan = span;
833 /* Max cond index offset. */
835 if ( st->condList != 0 )
836 redFsm->maxCondIndexOffset += keyOps->span( st->condLowKey, st->condHighKey );
839 /* Max flat index offset. */
841 if ( st->transList != 0 )
842 redFsm->maxFlatIndexOffset += keyOps->span( st->lowKey, st->highKey );
843 redFsm->maxFlatIndexOffset += 1;
847 for ( GenActionTableMap::Iter at = redFsm->actionMap; at.lte(); at++ ) {
848 /* Maximum id of action lists. */
849 if ( at->actListId+1 > redFsm->maxActListId )
850 redFsm->maxActListId = at->actListId+1;
852 /* Maximum location of items in action array. */
853 if ( at->location+1 > redFsm->maxActionLoc )
854 redFsm->maxActionLoc = at->location+1;
856 /* Maximum values going into the action array. */
857 if ( at->key.length() > redFsm->maxActArrItem )
858 redFsm->maxActArrItem = at->key.length();
859 for ( GenActionTable::Iter item = at->key; item.lte(); item++ ) {
860 if ( item->value->actionId > redFsm->maxActArrItem )
861 redFsm->maxActArrItem = item->value->actionId;
868 /* Gather various info on the machine. */
869 void CodeGenData::analyzeMachine()
871 /* Find the true count of action references. */
872 findFinalActionRefs();
874 /* Check if there are any calls in action code. */
875 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
876 /* Record the occurrence of various kinds of actions. */
877 if ( act->numToStateRefs > 0 )
878 redFsm->bAnyToStateActions = true;
879 if ( act->numFromStateRefs > 0 )
880 redFsm->bAnyFromStateActions = true;
881 if ( act->numEofRefs > 0 )
882 redFsm->bAnyEofActions = true;
883 if ( act->numTransRefs > 0 )
884 redFsm->bAnyRegActions = true;
886 /* Recurse through the action's parse tree looking for various things. */
887 analyzeAction( act, act->inlineList );
890 /* Analyze reduced action lists. */
891 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
892 for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ )
893 analyzeActionList( redAct, act->value->inlineList );
896 /* Find states that have transitions with actions that have next
898 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
899 /* Check any actions out of outSinge. */
900 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
901 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
902 st->bAnyRegCurStateRef = true;
905 /* Check any actions out of outRange. */
906 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
907 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
908 st->bAnyRegCurStateRef = true;
911 /* Check any action out of default. */
912 if ( st->defTrans != 0 && st->defTrans->action != 0 &&
913 st->defTrans->action->anyCurStateRef() )
914 st->bAnyRegCurStateRef = true;
916 if ( st->stateCondList.length() > 0 )
917 redFsm->bAnyConditions = true;
919 if ( st->eofTrans != 0 )
920 redFsm->bAnyEofTrans = true;
923 /* Assign ids to actions that are referenced. */
926 /* Set the maximums of various values used for deciding types. */
930 void CodeGenData::write_option_error( InputLoc &loc, char *arg )
932 source_warning(loc) << "unrecognized write option \"" << arg << "\"" << endl;
935 void CodeGenData::writeStatement( InputLoc &loc, int nargs, char **args )
937 /* FIXME: This should be moved to the virtual functions in the code
940 * Force a newline. */
942 genLineDirective( out );
944 if ( strcmp( args[0], "data" ) == 0 ) {
945 for ( int i = 1; i < nargs; i++ ) {
946 if ( strcmp( args[i], "noerror" ) == 0 )
948 else if ( strcmp( args[i], "noprefix" ) == 0 )
950 else if ( strcmp( args[i], "nofinal" ) == 0 )
953 write_option_error( loc, args[i] );
957 else if ( strcmp( args[0], "init" ) == 0 ) {
958 for ( int i = 1; i < nargs; i++ ) {
959 if ( strcmp( args[i], "nocs" ) == 0 )
962 write_option_error( loc, args[i] );
966 else if ( strcmp( args[0], "exec" ) == 0 ) {
967 for ( int i = 1; i < nargs; i++ ) {
968 if ( strcmp( args[i], "noend" ) == 0 )
971 write_option_error( loc, args[i] );
975 else if ( strcmp( args[0], "exports" ) == 0 ) {
976 for ( int i = 1; i < nargs; i++ )
977 write_option_error( loc, args[i] );
980 else if ( strcmp( args[0], "start" ) == 0 ) {
981 for ( int i = 1; i < nargs; i++ )
982 write_option_error( loc, args[i] );
985 else if ( strcmp( args[0], "first_final" ) == 0 ) {
986 for ( int i = 1; i < nargs; i++ )
987 write_option_error( loc, args[i] );
990 else if ( strcmp( args[0], "error" ) == 0 ) {
991 for ( int i = 1; i < nargs; i++ )
992 write_option_error( loc, args[i] );
996 /* EMIT An error here. */
997 source_error(loc) << "unrecognized write command \"" <<
998 args[0] << "\"" << endl;
1002 ostream &CodeGenData::source_warning( const InputLoc &loc )
1004 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
1008 ostream &CodeGenData::source_error( const InputLoc &loc )
1011 assert( sourceFileName != 0 );
1012 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";