2 * Copyright 2005-2006 Adrian Thurston <thurston@cs.queensu.ca>
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
24 /* Code Generators. */
26 #include "tabcodegen.h"
27 #include "ftabcodegen.h"
28 #include "flatcodegen.h"
29 #include "fflatcodegen.h"
30 #include "gotocodegen.h"
31 #include "fgotocodegen.h"
32 #include "ipgotocodegen.h"
33 #include "splitcodegen.h"
34 #include "javacodegen.h"
43 void CodeGenData::createMachine()
45 redFsm = new RedFsmAp();
48 void CodeGenData::initActionList( unsigned long length )
50 allActions = new Action[length];
51 for ( unsigned long a = 0; a < length; a++ )
52 actionList.append( allActions+a );
55 void CodeGenData::newAction( int anum, char *name, int line,
56 int col, InlineList *inlineList )
58 allActions[anum].actionId = anum;
59 allActions[anum].name = name;
60 allActions[anum].loc.line = line;
61 allActions[anum].loc.col = col;
62 allActions[anum].inlineList = inlineList;
65 void CodeGenData::initActionTableList( unsigned long length )
67 allActionTables = new RedAction[length];
70 void CodeGenData::initStateList( unsigned long length )
72 allStates = new RedStateAp[length];
73 for ( unsigned long s = 0; s < length; s++ )
74 redFsm->stateList.append( allStates+s );
77 void CodeGenData::setStartState( unsigned long startState )
79 this->startState = startState;
82 void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
84 entryPointIds.append( entryState );
85 entryPointNames.append( name );
88 void CodeGenData::initTransList( int snum, unsigned long length )
90 /* Could preallocate the out range to save time growing it. For now do
94 void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
95 Key highKey, long targ, long action )
97 /* Get the current state and range. */
98 RedStateAp *curState = allStates + snum;
99 RedTransList &destRange = curState->outRange;
101 /* Make the new transitions. */
102 RedStateAp *targState = targ >= 0 ? (allStates + targ) :
103 wantComplete ? redFsm->getErrorState() : 0;
104 RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
105 RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
106 RedTransEl transEl( lowKey, highKey, trans );
108 if ( wantComplete ) {
109 /* If the machine is to be complete then we need to fill any gaps with
110 * the error transitions. */
111 if ( destRange.length() == 0 ) {
112 /* Range is currently empty. */
113 if ( keyOps->minKey < lowKey ) {
114 /* The first range doesn't start at the low end. */
115 Key fillHighKey = lowKey;
116 fillHighKey.decrement();
118 /* Create the filler with the state's error transition. */
119 RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
120 destRange.append( newTel );
124 /* The range list is not empty, get the the last range. */
125 RedTransEl *last = &destRange[destRange.length()-1];
126 Key nextKey = last->highKey;
128 if ( nextKey < lowKey ) {
129 /* There is a gap to fill. Make the high key. */
130 Key fillHighKey = lowKey;
131 fillHighKey.decrement();
133 /* Create the filler with the state's error transtion. */
134 RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
135 destRange.append( newTel );
140 /* Filler taken care of. Append the range. */
141 destRange.append( RedTransEl( lowKey, highKey, trans ) );
144 void CodeGenData::finishTransList( int snum )
146 /* Get the current state and range. */
147 RedStateAp *curState = allStates + snum;
148 RedTransList &destRange = curState->outRange;
150 /* If building a complete machine we may need filler on the end. */
151 if ( wantComplete ) {
152 /* Check if there are any ranges already. */
153 if ( destRange.length() == 0 ) {
154 /* Fill with the whole alphabet. */
155 /* Add the range on the lower and upper bound. */
156 RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
157 destRange.append( newTel );
160 /* Get the last and check for a gap on the end. */
161 RedTransEl *last = &destRange[destRange.length()-1];
162 if ( last->highKey < keyOps->maxKey ) {
163 /* Make the high key. */
164 Key fillLowKey = last->highKey;
165 fillLowKey.increment();
167 /* Create the new range with the error trans and append it. */
168 RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
169 destRange.append( newTel );
175 void CodeGenData::setFinal( int snum )
177 RedStateAp *curState = allStates + snum;
178 curState->isFinal = true;
182 void CodeGenData::setStateActions( int snum, long toStateAction,
183 long fromStateAction, long eofAction )
185 RedStateAp *curState = allStates + snum;
186 if ( toStateAction >= 0 )
187 curState->toStateAction = allActionTables + toStateAction;
188 if ( fromStateAction >= 0 )
189 curState->fromStateAction = allActionTables + fromStateAction;
190 if ( eofAction >= 0 )
191 curState->eofAction = allActionTables + eofAction;
194 void CodeGenData::resolveTargetStates( InlineList *inlineList )
196 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
197 switch ( item->type ) {
198 case InlineItem::Goto: case InlineItem::Call:
199 case InlineItem::Next: case InlineItem::Entry:
200 item->targState = allStates + item->targId;
206 if ( item->children != 0 )
207 resolveTargetStates( item->children );
212 void CodeGenData::finishMachine()
214 if ( redFsm->forcedErrorState )
215 redFsm->getErrorState();
217 /* We get the start state as an offset, set the pointer now. */
218 redFsm->startState = allStates + startState;
219 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
220 redFsm->entryPoints.insert( allStates + *en );
222 for ( ActionList::Iter a = actionList; a.lte(); a++ )
223 resolveTargetStates( a->inlineList );
225 /* Note that even if we want a complete graph we do not give the error
226 * state a default transition. All machines break out of the processing
227 * loop when in the error state. */
229 if ( codeStyle == GenGoto || codeStyle == GenFGoto || codeStyle == GenIpGoto ) {
230 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
231 for ( StateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
232 st->stateCondVect.append( sci );
238 bool CodeGenData::setAlphType( char *data )
240 /* FIXME: This should validate the alphabet type selection. */
241 HostType *alphType = hostLang->hostTypes + atoi(data);
242 thisKeyOps.setAlphType( alphType );
246 void CodeGenData::initCondSpaceList( ulong length )
248 allCondSpaces = new CondSpace[length];
249 for ( ulong c = 0; c < length; c++ )
250 condSpaceList.append( allCondSpaces + c );
253 void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
255 CondSpace *cond = allCondSpaces + cnum;
256 cond->condSpaceId = condSpaceId;
257 cond->baseKey = baseKey;
260 void CodeGenData::condSpaceItem( int cnum, long condActionId )
262 CondSpace *cond = allCondSpaces + cnum;
263 cond->condSet.append( allActions + condActionId );
266 void CodeGenData::initStateCondList( int snum, ulong length )
268 /* Could preallocate these, as we could with transitions. */
271 void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
273 RedStateAp *curState = allStates + snum;
275 /* Create the new state condition. */
276 StateCond *stateCond = new StateCond;
277 stateCond->lowKey = lowKey;
278 stateCond->highKey = highKey;
280 /* Assign it a cond space. */
281 CondSpace *condSpace = allCondSpaces + condNum;
282 stateCond->condSpace = condSpace;
284 curState->stateCondList.append( stateCond );
288 /* Generate the codegen depending on the command line options given. */
289 void CodeGenData::makeCodeGen()
291 switch ( hostLangType ) {
293 switch ( codeStyle ) {
295 codeGen = new CTabCodeGen(out);
298 codeGen = new CFTabCodeGen(out);
301 codeGen = new CFlatCodeGen(out);
304 codeGen = new CFFlatCodeGen(out);
307 codeGen = new CGotoCodeGen(out);
310 codeGen = new CFGotoCodeGen(out);
313 codeGen = new CIpGotoCodeGen(out);
316 codeGen = new CSplitCodeGen(out);
322 switch ( codeStyle ) {
324 codeGen = new DTabCodeGen(out);
327 codeGen = new DFTabCodeGen(out);
330 codeGen = new DFlatCodeGen(out);
333 codeGen = new DFFlatCodeGen(out);
336 codeGen = new DGotoCodeGen(out);
339 codeGen = new DFGotoCodeGen(out);
342 codeGen = new DIpGotoCodeGen(out);
345 codeGen = new DSplitCodeGen(out);
351 switch ( codeStyle ) {
353 codeGen = new JavaTabCodeGen(out);
362 codeGen->fsmName = fsmName;
366 CondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
368 for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
369 Key csHighKey = cs->baseKey;
370 csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
372 if ( lowKey >= cs->baseKey && highKey <= csHighKey )
378 Condition *CodeGenData::findCondition( Key key )
380 for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
381 Key upperKey = cond->baseKey + (1 << cond->condSet.length());
382 if ( cond->baseKey <= key && key <= upperKey )
388 Key CodeGenData::findMaxKey()
390 Key maxKey = keyOps->maxKey;
391 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
392 assert( st->outSingle.length() == 0 );
393 assert( st->defTrans == 0 );
395 long rangeLen = st->outRange.length();
396 if ( rangeLen > 0 ) {
397 Key highKey = st->outRange[rangeLen-1].highKey;
398 if ( highKey > maxKey )
405 void CodeGenData::findFinalActionRefs()
407 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
408 /* Rerence count out of single transitions. */
409 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
410 if ( rtel->value->action != 0 ) {
411 rtel->value->action->numTransRefs += 1;
412 for ( ActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
413 item->value->numTransRefs += 1;
417 /* Reference count out of range transitions. */
418 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
419 if ( rtel->value->action != 0 ) {
420 rtel->value->action->numTransRefs += 1;
421 for ( ActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
422 item->value->numTransRefs += 1;
426 /* Reference count default transition. */
427 if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
428 st->defTrans->action->numTransRefs += 1;
429 for ( ActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
430 item->value->numTransRefs += 1;
433 /* Reference count to state actions. */
434 if ( st->toStateAction != 0 ) {
435 st->toStateAction->numToStateRefs += 1;
436 for ( ActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
437 item->value->numToStateRefs += 1;
440 /* Reference count from state actions. */
441 if ( st->fromStateAction != 0 ) {
442 st->fromStateAction->numFromStateRefs += 1;
443 for ( ActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
444 item->value->numFromStateRefs += 1;
447 /* Reference count EOF actions. */
448 if ( st->eofAction != 0 ) {
449 st->eofAction->numEofRefs += 1;
450 for ( ActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
451 item->value->numEofRefs += 1;
456 void CodeGenData::analyzeAction( Action *act, InlineList *inlineList )
458 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
459 /* Only consider actions that are referenced. */
460 if ( act->numRefs() > 0 ) {
461 if ( item->type == InlineItem::Goto || item->type == InlineItem::GotoExpr )
462 codeGen->bAnyActionGotos = true;
463 else if ( item->type == InlineItem::Call || item->type == InlineItem::CallExpr )
464 codeGen->bAnyActionCalls = true;
465 else if ( item->type == InlineItem::Ret )
466 codeGen->bAnyActionRets = true;
469 /* Check for various things in regular actions. */
470 if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
471 /* Any returns in regular actions? */
472 if ( item->type == InlineItem::Ret )
473 codeGen->bAnyRegActionRets = true;
475 /* Any next statements in the regular actions? */
476 if ( item->type == InlineItem::Next || item->type == InlineItem::NextExpr )
477 codeGen->bAnyRegNextStmt = true;
479 /* Any by value control in regular actions? */
480 if ( item->type == InlineItem::CallExpr || item->type == InlineItem::GotoExpr )
481 codeGen->bAnyRegActionByValControl = true;
483 /* Any references to the current state in regular actions? */
484 if ( item->type == InlineItem::Curs )
485 codeGen->bAnyRegCurStateRef = true;
487 if ( item->type == InlineItem::Break )
488 codeGen->bAnyRegBreak = true;
490 if ( item->type == InlineItem::LmSwitch && item->handlesError )
491 codeGen->bAnyLmSwitchError = true;
494 if ( item->children != 0 )
495 analyzeAction( act, item->children );
499 void CodeGenData::analyzeActionList( RedAction *redAct, InlineList *inlineList )
501 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
502 /* Any next statements in the action table? */
503 if ( item->type == InlineItem::Next || item->type == InlineItem::NextExpr )
504 redAct->bAnyNextStmt = true;
506 /* Any references to the current state. */
507 if ( item->type == InlineItem::Curs )
508 redAct->bAnyCurStateRef = true;
510 if ( item->type == InlineItem::Break )
511 redAct->bAnyBreakStmt = true;
513 if ( item->children != 0 )
514 analyzeActionList( redAct, item->children );
518 /* Gather various info on the machine. */
519 void CodeGenData::analyzeMachine()
521 /* Find the true count of action references. */
522 findFinalActionRefs();
524 /* Check if there are any calls in action code. */
525 for ( ActionList::Iter act = cgd->actionList; act.lte(); act++ ) {
526 /* Record the occurrence of various kinds of actions. */
527 if ( act->numToStateRefs > 0 )
528 codeGen->bAnyToStateActions = true;
529 if ( act->numFromStateRefs > 0 )
530 codeGen->bAnyFromStateActions = true;
531 if ( act->numEofRefs > 0 )
532 codeGen->bAnyEofActions = true;
533 if ( act->numTransRefs > 0 )
534 codeGen->bAnyRegActions = true;
536 /* Recurse through the action's parse tree looking for various things. */
537 analyzeAction( act, act->inlineList );
540 /* Analyze reduced action lists. */
541 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
542 for ( ActionTable::Iter act = redAct->key; act.lte(); act++ )
543 analyzeActionList( redAct, act->value->inlineList );
546 /* Find states that have transitions with actions that have next
548 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
549 /* Check any actions out of outSinge. */
550 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
551 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
552 st->bAnyRegCurStateRef = true;
555 /* Check any actions out of outRange. */
556 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
557 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
558 st->bAnyRegCurStateRef = true;
561 /* Check any action out of default. */
562 if ( st->defTrans != 0 && st->defTrans->action != 0 &&
563 st->defTrans->action->anyCurStateRef() )
564 st->bAnyRegCurStateRef = true;
566 if ( st->stateCondList.length() > 0 )
567 codeGen->bAnyConditions = true;
570 /* Assign ids to actions that are referenced. */
571 codeGen->assignActionIds();
573 /* Set the maximums of various values used for deciding types. */
574 codeGen->setValueLimits();
576 /* Determine if we should use indicies. */
577 codeGen->calcIndexSize();
580 /* Generate the code for an fsm. Assumes parseData is set up properly. Called
582 void CodeGenData::prepareMachine()
584 if ( hasBeenPrepared )
586 hasBeenPrepared = true;
588 /* Do this before distributing transitions out to singles and defaults
589 * makes life easier. */
590 Key maxKey = findMaxKey();
592 redFsm->assignActionLocs();
594 /* Order the states. */
595 redFsm->depthFirstOrdering();
597 if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
598 codeStyle == GenIpGoto || codeStyle == GenSplit )
600 /* For goto driven machines we can keep the original depth
601 * first ordering because it's ok if the state ids are not
602 * sequential. Split the the ids by final state status. */
603 redFsm->sortStateIdsByFinal();
606 /* For table driven machines the location of the state is used to
607 * identify it so the states must be sorted by their final ids.
608 * Though having a deterministic ordering is important,
609 * specifically preserving the depth first ordering is not because
610 * states are stored in tables. */
611 redFsm->sortStatesByFinal();
612 redFsm->sequentialStateIds();
615 /* Find the first final state. This is the final state with the lowest
617 redFsm->findFirstFinState();
619 /* Choose default transitions and the single transition. */
620 redFsm->chooseDefaultSpan();
622 /* Maybe do flat expand, otherwise choose single. */
623 if ( codeStyle == GenFlat || codeStyle == GenFFlat )
626 redFsm->chooseSingle();
628 /* If any errors have occured in the input file then don't write anything. */
629 if ( gblErrorCount > 0 )
632 if ( codeStyle == GenSplit )
633 redFsm->partitionFsm( numSplitPartitions );
635 if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
636 redFsm->setInTrans();
638 /* Make a code generator that will output the header/code. */
641 codeGen->redFsm = redFsm;
643 /* Anlayze Machine will find the final action reference counts, among
644 * other things. We will use these in reporting the usage
645 * of fsm directives in action code. */
647 codeGen->maxKey = maxKey;
650 void CodeGenData::generateGraphviz()
652 /* Do ordering and choose state ids. */
653 redFsm->depthFirstOrdering();
654 redFsm->sequentialStateIds();
656 /* For dot file generation we want to pick default transitions. */
657 redFsm->chooseDefaultSpan();
659 /* Make the generator. */
660 GraphvizDotGen dotGen( fsmName, this, redFsm, out );
662 /* Write out with it. */
663 dotGen.writeDotFile();
666 void CodeGenData::generateCode()
668 if ( writeOps & WO_NOEND )
671 if ( writeOps & WO_NOERROR )
674 if ( writeOps & WO_NOPREFIX )
677 if ( writeOps & WO_NOFF )
678 writeFirstFinal = false;
680 if ( writeData || writeInit || writeExec || writeEOF ) {
683 /* Force a newline. */
685 genLineDirective( out );
690 /* Must set labels immediately before writing because we may depend
691 * on the noend write option. */
692 codeGen->setLabelsNeeded();
696 codeGen->writeOutData();
699 codeGen->writeOutInit();
702 codeGen->writeOutExec();
705 codeGen->writeOutEOF();
708 void CodeGenData::generate()
711 if ( outputFormat == OutCode )
713 else if ( outputFormat == OutGraphvizDot && !graphvizDone ) {
720 void lineDirective( ostream &out, char *fileName, int line )
722 if ( hostLangType != JavaCode ) {
723 /* Write the preprocessor line info for to the input file. */
724 out << "#line " << line << " \"";
725 for ( char *pc = fileName; *pc != 0; pc++ ) {
735 void genLineDirective( ostream &out )
737 assert( outputFormat == OutCode );
738 std::streambuf *sbuf = out.rdbuf();
739 output_filter *filter = static_cast<output_filter*>(sbuf);
740 lineDirective( out, filter->fileName, filter->line + 1 );