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, int line,
342 int col, GenInlineList *inlineList )
344 allActions[anum].actionId = anum;
345 allActions[anum].name = name;
346 allActions[anum].loc.line = line;
347 allActions[anum].loc.col = col;
348 allActions[anum].inlineList = inlineList;
351 void CodeGenData::initActionTableList( unsigned long length )
353 allActionTables = new RedAction[length];
356 void CodeGenData::initStateList( unsigned long length )
358 allStates = new RedStateAp[length];
359 for ( unsigned long s = 0; s < length; s++ )
360 redFsm->stateList.append( allStates+s );
362 /* We get the start state as an offset, set the pointer now. */
363 if ( startState >= 0 )
364 redFsm->startState = allStates + startState;
366 redFsm->errState = allStates + errState;
367 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
368 redFsm->entryPoints.insert( allStates + *en );
370 /* The nextStateId is no longer used to assign state ids (they come in set
371 * from the frontend now), however generation code still depends on it.
372 * Should eventually remove this variable. */
373 redFsm->nextStateId = redFsm->stateList.length();
376 void CodeGenData::setStartState( unsigned long startState )
378 this->startState = startState;
381 void CodeGenData::setErrorState( unsigned long errState )
383 this->errState = errState;
386 void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
388 entryPointIds.append( entryState );
389 entryPointNames.append( name );
392 void CodeGenData::initTransList( int snum, unsigned long length )
394 /* Could preallocate the out range to save time growing it. For now do
398 void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
399 Key highKey, long targ, long action )
401 /* Get the current state and range. */
402 RedStateAp *curState = allStates + snum;
403 RedTransList &destRange = curState->outRange;
405 if ( curState == redFsm->errState )
408 /* Make the new transitions. */
409 RedStateAp *targState = targ >= 0 ? (allStates + targ) :
410 wantComplete ? redFsm->getErrorState() : 0;
411 RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
412 RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
413 RedTransEl transEl( lowKey, highKey, trans );
415 if ( wantComplete ) {
416 /* If the machine is to be complete then we need to fill any gaps with
417 * the error transitions. */
418 if ( destRange.length() == 0 ) {
419 /* Range is currently empty. */
420 if ( keyOps->minKey < lowKey ) {
421 /* The first range doesn't start at the low end. */
422 Key fillHighKey = lowKey;
423 fillHighKey.decrement();
425 /* Create the filler with the state's error transition. */
426 RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
427 destRange.append( newTel );
431 /* The range list is not empty, get the the last range. */
432 RedTransEl *last = &destRange[destRange.length()-1];
433 Key nextKey = last->highKey;
435 if ( nextKey < lowKey ) {
436 /* There is a gap to fill. Make the high key. */
437 Key fillHighKey = lowKey;
438 fillHighKey.decrement();
440 /* Create the filler with the state's error transtion. */
441 RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
442 destRange.append( newTel );
447 /* Filler taken care of. Append the range. */
448 destRange.append( RedTransEl( lowKey, highKey, trans ) );
451 void CodeGenData::finishTransList( int snum )
453 /* Get the current state and range. */
454 RedStateAp *curState = allStates + snum;
455 RedTransList &destRange = curState->outRange;
457 if ( curState == redFsm->errState )
460 /* If building a complete machine we may need filler on the end. */
461 if ( wantComplete ) {
462 /* Check if there are any ranges already. */
463 if ( destRange.length() == 0 ) {
464 /* Fill with the whole alphabet. */
465 /* Add the range on the lower and upper bound. */
466 RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
467 destRange.append( newTel );
470 /* Get the last and check for a gap on the end. */
471 RedTransEl *last = &destRange[destRange.length()-1];
472 if ( last->highKey < keyOps->maxKey ) {
473 /* Make the high key. */
474 Key fillLowKey = last->highKey;
475 fillLowKey.increment();
477 /* Create the new range with the error trans and append it. */
478 RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
479 destRange.append( newTel );
485 void CodeGenData::setId( int snum, int id )
487 RedStateAp *curState = allStates + snum;
491 void CodeGenData::setFinal( int snum )
493 RedStateAp *curState = allStates + snum;
494 curState->isFinal = true;
498 void CodeGenData::setStateActions( int snum, long toStateAction,
499 long fromStateAction, long eofAction )
501 RedStateAp *curState = allStates + snum;
502 if ( toStateAction >= 0 )
503 curState->toStateAction = allActionTables + toStateAction;
504 if ( fromStateAction >= 0 )
505 curState->fromStateAction = allActionTables + fromStateAction;
506 if ( eofAction >= 0 )
507 curState->eofAction = allActionTables + eofAction;
510 void CodeGenData::setEofTrans( int snum, long eofTarget, long actId )
512 RedStateAp *curState = allStates + snum;
513 RedStateAp *targState = allStates + eofTarget;
514 RedAction *eofAct = allActionTables + actId;
515 curState->eofTrans = redFsm->allocateTrans( targState, eofAct );
518 void CodeGenData::resolveTargetStates( GenInlineList *inlineList )
520 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
521 switch ( item->type ) {
522 case GenInlineItem::Goto: case GenInlineItem::Call:
523 case GenInlineItem::Next: case GenInlineItem::Entry:
524 item->targState = allStates + item->targId;
530 if ( item->children != 0 )
531 resolveTargetStates( item->children );
535 void CodeGenData::closeMachine()
537 for ( GenActionList::Iter a = actionList; a.lte(); a++ )
538 resolveTargetStates( a->inlineList );
540 /* Note that even if we want a complete graph we do not give the error
541 * state a default transition. All machines break out of the processing
542 * loop when in the error state. */
544 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
545 for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
546 st->stateCondVect.append( sci );
551 bool CodeGenData::setAlphType( const char *data )
553 HostType *alphType = findAlphTypeInternal( data );
557 thisKeyOps.setAlphType( alphType );
561 void CodeGenData::initCondSpaceList( ulong length )
563 allCondSpaces = new GenCondSpace[length];
564 for ( ulong c = 0; c < length; c++ )
565 condSpaceList.append( allCondSpaces + c );
568 void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
570 GenCondSpace *cond = allCondSpaces + cnum;
571 cond->condSpaceId = condSpaceId;
572 cond->baseKey = baseKey;
575 void CodeGenData::condSpaceItem( int cnum, long condActionId )
577 GenCondSpace *cond = allCondSpaces + cnum;
578 cond->condSet.append( allActions + condActionId );
581 void CodeGenData::initStateCondList( int snum, ulong length )
583 /* Could preallocate these, as we could with transitions. */
586 void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
588 RedStateAp *curState = allStates + snum;
590 /* Create the new state condition. */
591 GenStateCond *stateCond = new GenStateCond;
592 stateCond->lowKey = lowKey;
593 stateCond->highKey = highKey;
595 /* Assign it a cond space. */
596 GenCondSpace *condSpace = allCondSpaces + condNum;
597 stateCond->condSpace = condSpace;
599 curState->stateCondList.append( stateCond );
603 GenCondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
605 for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
606 Key csHighKey = cs->baseKey;
607 csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
609 if ( lowKey >= cs->baseKey && highKey <= csHighKey )
615 Condition *CodeGenData::findCondition( Key key )
617 for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
618 Key upperKey = cond->baseKey + (1 << cond->condSet.length());
619 if ( cond->baseKey <= key && key <= upperKey )
625 Key CodeGenData::findMaxKey()
627 Key maxKey = keyOps->maxKey;
628 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
629 assert( st->outSingle.length() == 0 );
630 assert( st->defTrans == 0 );
632 long rangeLen = st->outRange.length();
633 if ( rangeLen > 0 ) {
634 Key highKey = st->outRange[rangeLen-1].highKey;
635 if ( highKey > maxKey )
642 void CodeGenData::findFinalActionRefs()
644 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
645 /* Rerence count out of single transitions. */
646 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
647 if ( rtel->value->action != 0 ) {
648 rtel->value->action->numTransRefs += 1;
649 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
650 item->value->numTransRefs += 1;
654 /* Reference count out of range transitions. */
655 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
656 if ( rtel->value->action != 0 ) {
657 rtel->value->action->numTransRefs += 1;
658 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
659 item->value->numTransRefs += 1;
663 /* Reference count default transition. */
664 if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
665 st->defTrans->action->numTransRefs += 1;
666 for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
667 item->value->numTransRefs += 1;
670 /* Reference count eof transitions. */
671 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
672 st->eofTrans->action->numTransRefs += 1;
673 for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ )
674 item->value->numTransRefs += 1;
677 /* Reference count to state actions. */
678 if ( st->toStateAction != 0 ) {
679 st->toStateAction->numToStateRefs += 1;
680 for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
681 item->value->numToStateRefs += 1;
684 /* Reference count from state actions. */
685 if ( st->fromStateAction != 0 ) {
686 st->fromStateAction->numFromStateRefs += 1;
687 for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
688 item->value->numFromStateRefs += 1;
691 /* Reference count EOF actions. */
692 if ( st->eofAction != 0 ) {
693 st->eofAction->numEofRefs += 1;
694 for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
695 item->value->numEofRefs += 1;
700 void CodeGenData::analyzeAction( GenAction *act, GenInlineList *inlineList )
702 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
703 /* Only consider actions that are referenced. */
704 if ( act->numRefs() > 0 ) {
705 if ( item->type == GenInlineItem::Goto || item->type == GenInlineItem::GotoExpr )
706 redFsm->bAnyActionGotos = true;
707 else if ( item->type == GenInlineItem::Call || item->type == GenInlineItem::CallExpr )
708 redFsm->bAnyActionCalls = true;
709 else if ( item->type == GenInlineItem::Ret )
710 redFsm->bAnyActionRets = true;
713 /* Check for various things in regular actions. */
714 if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
715 /* Any returns in regular actions? */
716 if ( item->type == GenInlineItem::Ret )
717 redFsm->bAnyRegActionRets = true;
719 /* Any next statements in the regular actions? */
720 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
721 redFsm->bAnyRegNextStmt = true;
723 /* Any by value control in regular actions? */
724 if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
725 redFsm->bAnyRegActionByValControl = true;
727 /* Any references to the current state in regular actions? */
728 if ( item->type == GenInlineItem::Curs )
729 redFsm->bAnyRegCurStateRef = true;
731 if ( item->type == GenInlineItem::Break )
732 redFsm->bAnyRegBreak = true;
735 if ( item->children != 0 )
736 analyzeAction( act, item->children );
740 void CodeGenData::analyzeActionList( RedAction *redAct, GenInlineList *inlineList )
742 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
743 /* Any next statements in the action table? */
744 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
745 redAct->bAnyNextStmt = true;
747 /* Any references to the current state. */
748 if ( item->type == GenInlineItem::Curs )
749 redAct->bAnyCurStateRef = true;
751 if ( item->type == GenInlineItem::Break )
752 redAct->bAnyBreakStmt = true;
754 if ( item->children != 0 )
755 analyzeActionList( redAct, item->children );
759 /* Assign ids to referenced actions. */
760 void CodeGenData::assignActionIds()
762 int nextActionId = 0;
763 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
764 /* Only ever interested in referenced actions. */
765 if ( act->numRefs() > 0 )
766 act->actionId = nextActionId++;
770 void CodeGenData::setValueLimits()
772 redFsm->maxSingleLen = 0;
773 redFsm->maxRangeLen = 0;
774 redFsm->maxKeyOffset = 0;
775 redFsm->maxIndexOffset = 0;
776 redFsm->maxActListId = 0;
777 redFsm->maxActionLoc = 0;
778 redFsm->maxActArrItem = 0;
780 redFsm->maxCondSpan = 0;
781 redFsm->maxFlatIndexOffset = 0;
782 redFsm->maxCondOffset = 0;
783 redFsm->maxCondLen = 0;
784 redFsm->maxCondSpaceId = 0;
785 redFsm->maxCondIndexOffset = 0;
787 /* In both of these cases the 0 index is reserved for no value, so the max
788 * is one more than it would be if they started at 0. */
789 redFsm->maxIndex = redFsm->transSet.length();
790 redFsm->maxCond = condSpaceList.length();
792 /* The nextStateId - 1 is the last state id assigned. */
793 redFsm->maxState = redFsm->nextStateId - 1;
795 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
796 if ( csi->condSpaceId > redFsm->maxCondSpaceId )
797 redFsm->maxCondSpaceId = csi->condSpaceId;
800 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
801 /* Maximum cond length. */
802 if ( st->stateCondList.length() > redFsm->maxCondLen )
803 redFsm->maxCondLen = st->stateCondList.length();
805 /* Maximum single length. */
806 if ( st->outSingle.length() > redFsm->maxSingleLen )
807 redFsm->maxSingleLen = st->outSingle.length();
809 /* Maximum range length. */
810 if ( st->outRange.length() > redFsm->maxRangeLen )
811 redFsm->maxRangeLen = st->outRange.length();
813 /* The key offset index offset for the state after last is not used, skip it.. */
815 redFsm->maxCondOffset += st->stateCondList.length();
816 redFsm->maxKeyOffset += st->outSingle.length() + st->outRange.length()*2;
817 redFsm->maxIndexOffset += st->outSingle.length() + st->outRange.length() + 2;
821 if ( st->condList != 0 ) {
822 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
823 if ( span > redFsm->maxCondSpan )
824 redFsm->maxCondSpan = span;
828 if ( st->transList != 0 ) {
829 unsigned long long span = keyOps->span( st->lowKey, st->highKey );
830 if ( span > redFsm->maxSpan )
831 redFsm->maxSpan = span;
834 /* Max cond index offset. */
836 if ( st->condList != 0 )
837 redFsm->maxCondIndexOffset += keyOps->span( st->condLowKey, st->condHighKey );
840 /* Max flat index offset. */
842 if ( st->transList != 0 )
843 redFsm->maxFlatIndexOffset += keyOps->span( st->lowKey, st->highKey );
844 redFsm->maxFlatIndexOffset += 1;
848 for ( GenActionTableMap::Iter at = redFsm->actionMap; at.lte(); at++ ) {
849 /* Maximum id of action lists. */
850 if ( at->actListId+1 > redFsm->maxActListId )
851 redFsm->maxActListId = at->actListId+1;
853 /* Maximum location of items in action array. */
854 if ( at->location+1 > redFsm->maxActionLoc )
855 redFsm->maxActionLoc = at->location+1;
857 /* Maximum values going into the action array. */
858 if ( at->key.length() > redFsm->maxActArrItem )
859 redFsm->maxActArrItem = at->key.length();
860 for ( GenActionTable::Iter item = at->key; item.lte(); item++ ) {
861 if ( item->value->actionId > redFsm->maxActArrItem )
862 redFsm->maxActArrItem = item->value->actionId;
869 /* Gather various info on the machine. */
870 void CodeGenData::analyzeMachine()
872 /* Find the true count of action references. */
873 findFinalActionRefs();
875 /* Check if there are any calls in action code. */
876 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
877 /* Record the occurrence of various kinds of actions. */
878 if ( act->numToStateRefs > 0 )
879 redFsm->bAnyToStateActions = true;
880 if ( act->numFromStateRefs > 0 )
881 redFsm->bAnyFromStateActions = true;
882 if ( act->numEofRefs > 0 )
883 redFsm->bAnyEofActions = true;
884 if ( act->numTransRefs > 0 )
885 redFsm->bAnyRegActions = true;
887 /* Recurse through the action's parse tree looking for various things. */
888 analyzeAction( act, act->inlineList );
891 /* Analyze reduced action lists. */
892 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
893 for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ )
894 analyzeActionList( redAct, act->value->inlineList );
897 /* Find states that have transitions with actions that have next
899 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
900 /* Check any actions out of outSinge. */
901 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
902 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
903 st->bAnyRegCurStateRef = true;
906 /* Check any actions out of outRange. */
907 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
908 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
909 st->bAnyRegCurStateRef = true;
912 /* Check any action out of default. */
913 if ( st->defTrans != 0 && st->defTrans->action != 0 &&
914 st->defTrans->action->anyCurStateRef() )
915 st->bAnyRegCurStateRef = true;
917 if ( st->stateCondList.length() > 0 )
918 redFsm->bAnyConditions = true;
920 if ( st->eofTrans != 0 )
921 redFsm->bAnyEofTrans = true;
924 /* Assign ids to actions that are referenced. */
927 /* Set the maximums of various values used for deciding types. */
931 void CodeGenData::write_option_error( InputLoc &loc, char *arg )
933 source_warning(loc) << "unrecognized write option \"" << arg << "\"" << endl;
936 void CodeGenData::writeStatement( InputLoc &loc, int nargs, char **args )
938 /* FIXME: This should be moved to the virtual functions in the code
941 * Force a newline. */
943 genLineDirective( out );
945 if ( strcmp( args[0], "data" ) == 0 ) {
946 for ( int i = 1; i < nargs; i++ ) {
947 if ( strcmp( args[i], "noerror" ) == 0 )
949 else if ( strcmp( args[i], "noprefix" ) == 0 )
951 else if ( strcmp( args[i], "nofinal" ) == 0 )
954 write_option_error( loc, args[i] );
958 else if ( strcmp( args[0], "init" ) == 0 ) {
959 for ( int i = 1; i < nargs; i++ ) {
960 if ( strcmp( args[i], "nocs" ) == 0 )
963 write_option_error( loc, args[i] );
967 else if ( strcmp( args[0], "exec" ) == 0 ) {
968 for ( int i = 1; i < nargs; i++ ) {
969 if ( strcmp( args[i], "noend" ) == 0 )
972 write_option_error( loc, args[i] );
976 else if ( strcmp( args[0], "exports" ) == 0 ) {
977 for ( int i = 1; i < nargs; i++ )
978 write_option_error( loc, args[i] );
981 else if ( strcmp( args[0], "start" ) == 0 ) {
982 for ( int i = 1; i < nargs; i++ )
983 write_option_error( loc, args[i] );
986 else if ( strcmp( args[0], "first_final" ) == 0 ) {
987 for ( int i = 1; i < nargs; i++ )
988 write_option_error( loc, args[i] );
991 else if ( strcmp( args[0], "error" ) == 0 ) {
992 for ( int i = 1; i < nargs; i++ )
993 write_option_error( loc, args[i] );
997 /* EMIT An error here. */
998 source_error(loc) << "unrecognized write command \"" <<
999 args[0] << "\"" << endl;
1003 ostream &CodeGenData::source_warning( const InputLoc &loc )
1005 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
1009 ostream &CodeGenData::source_error( const InputLoc &loc )
1012 assert( sourceFileName != 0 );
1013 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";