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
28 /* Total error count. */
29 /* int gblErrorCount = 0; */
31 CodeGenData::CodeGenData( ostream &out )
60 hasLongestMatch(false),
63 writeFirstFinal(true),
69 void CodeGenData::createMachine()
71 redFsm = new RedFsmAp();
74 void CodeGenData::initActionList( unsigned long length )
76 allActions = new GenAction[length];
77 for ( unsigned long a = 0; a < length; a++ )
78 actionList.append( allActions+a );
81 void CodeGenData::newAction( int anum, char *name, int line,
82 int col, GenInlineList *inlineList )
84 allActions[anum].actionId = anum;
85 allActions[anum].name = name;
86 allActions[anum].loc.line = line;
87 allActions[anum].loc.col = col;
88 allActions[anum].inlineList = inlineList;
91 void CodeGenData::initActionTableList( unsigned long length )
93 allActionTables = new RedAction[length];
96 void CodeGenData::initStateList( unsigned long length )
98 allStates = new RedStateAp[length];
99 for ( unsigned long s = 0; s < length; s++ )
100 redFsm->stateList.append( allStates+s );
102 /* We get the start state as an offset, set the pointer now. */
103 if ( startState >= 0 )
104 redFsm->startState = allStates + startState;
106 redFsm->errState = allStates + errState;
107 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
108 redFsm->entryPoints.insert( allStates + *en );
110 /* The nextStateId is no longer used to assign state ids (they come in set
111 * from the frontend now), however generation code still depends on it.
112 * Should eventually remove this variable. */
113 redFsm->nextStateId = redFsm->stateList.length();
116 void CodeGenData::setStartState( unsigned long startState )
118 this->startState = startState;
121 void CodeGenData::setErrorState( unsigned long errState )
123 this->errState = errState;
126 void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
128 entryPointIds.append( entryState );
129 entryPointNames.append( name );
132 void CodeGenData::initTransList( int snum, unsigned long length )
134 /* Could preallocate the out range to save time growing it. For now do
138 void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
139 Key highKey, long targ, long action )
141 /* Get the current state and range. */
142 RedStateAp *curState = allStates + snum;
143 RedTransList &destRange = curState->outRange;
145 if ( curState == redFsm->errState )
148 /* Make the new transitions. */
149 RedStateAp *targState = targ >= 0 ? (allStates + targ) :
150 wantComplete ? redFsm->getErrorState() : 0;
151 RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
152 RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
153 RedTransEl transEl( lowKey, highKey, trans );
155 if ( wantComplete ) {
156 /* If the machine is to be complete then we need to fill any gaps with
157 * the error transitions. */
158 if ( destRange.length() == 0 ) {
159 /* Range is currently empty. */
160 if ( keyOps->minKey < lowKey ) {
161 /* The first range doesn't start at the low end. */
162 Key fillHighKey = lowKey;
163 fillHighKey.decrement();
165 /* Create the filler with the state's error transition. */
166 RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
167 destRange.append( newTel );
171 /* The range list is not empty, get the the last range. */
172 RedTransEl *last = &destRange[destRange.length()-1];
173 Key nextKey = last->highKey;
175 if ( nextKey < lowKey ) {
176 /* There is a gap to fill. Make the high key. */
177 Key fillHighKey = lowKey;
178 fillHighKey.decrement();
180 /* Create the filler with the state's error transtion. */
181 RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
182 destRange.append( newTel );
187 /* Filler taken care of. Append the range. */
188 destRange.append( RedTransEl( lowKey, highKey, trans ) );
191 void CodeGenData::finishTransList( int snum )
193 /* Get the current state and range. */
194 RedStateAp *curState = allStates + snum;
195 RedTransList &destRange = curState->outRange;
197 if ( curState == redFsm->errState )
200 /* If building a complete machine we may need filler on the end. */
201 if ( wantComplete ) {
202 /* Check if there are any ranges already. */
203 if ( destRange.length() == 0 ) {
204 /* Fill with the whole alphabet. */
205 /* Add the range on the lower and upper bound. */
206 RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
207 destRange.append( newTel );
210 /* Get the last and check for a gap on the end. */
211 RedTransEl *last = &destRange[destRange.length()-1];
212 if ( last->highKey < keyOps->maxKey ) {
213 /* Make the high key. */
214 Key fillLowKey = last->highKey;
215 fillLowKey.increment();
217 /* Create the new range with the error trans and append it. */
218 RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
219 destRange.append( newTel );
225 void CodeGenData::setId( int snum, int id )
227 RedStateAp *curState = allStates + snum;
231 void CodeGenData::setFinal( int snum )
233 RedStateAp *curState = allStates + snum;
234 curState->isFinal = true;
238 void CodeGenData::setStateActions( int snum, long toStateAction,
239 long fromStateAction, long eofAction )
241 RedStateAp *curState = allStates + snum;
242 if ( toStateAction >= 0 )
243 curState->toStateAction = allActionTables + toStateAction;
244 if ( fromStateAction >= 0 )
245 curState->fromStateAction = allActionTables + fromStateAction;
246 if ( eofAction >= 0 )
247 curState->eofAction = allActionTables + eofAction;
250 void CodeGenData::setEofTrans( int snum, long eofTarget, long actId )
252 RedStateAp *curState = allStates + snum;
253 RedStateAp *targState = allStates + eofTarget;
254 RedAction *eofAct = allActionTables + actId;
255 curState->eofTrans = redFsm->allocateTrans( targState, eofAct );
258 void CodeGenData::resolveTargetStates( GenInlineList *inlineList )
260 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
261 switch ( item->type ) {
262 case GenInlineItem::Goto: case GenInlineItem::Call:
263 case GenInlineItem::Next: case GenInlineItem::Entry:
264 item->targState = allStates + item->targId;
270 if ( item->children != 0 )
271 resolveTargetStates( item->children );
275 void CodeGenData::closeMachine()
277 for ( GenActionList::Iter a = actionList; a.lte(); a++ )
278 resolveTargetStates( a->inlineList );
280 /* Note that even if we want a complete graph we do not give the error
281 * state a default transition. All machines break out of the processing
282 * loop when in the error state. */
284 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
285 for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
286 st->stateCondVect.append( sci );
291 bool CodeGenData::setAlphType( const char *data )
293 HostType *alphType = findAlphTypeInternal( data );
297 thisKeyOps.setAlphType( alphType );
301 void CodeGenData::initCondSpaceList( ulong length )
303 allCondSpaces = new GenCondSpace[length];
304 for ( ulong c = 0; c < length; c++ )
305 condSpaceList.append( allCondSpaces + c );
308 void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
310 GenCondSpace *cond = allCondSpaces + cnum;
311 cond->condSpaceId = condSpaceId;
312 cond->baseKey = baseKey;
315 void CodeGenData::condSpaceItem( int cnum, long condActionId )
317 GenCondSpace *cond = allCondSpaces + cnum;
318 cond->condSet.append( allActions + condActionId );
321 void CodeGenData::initStateCondList( int snum, ulong length )
323 /* Could preallocate these, as we could with transitions. */
326 void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
328 RedStateAp *curState = allStates + snum;
330 /* Create the new state condition. */
331 GenStateCond *stateCond = new GenStateCond;
332 stateCond->lowKey = lowKey;
333 stateCond->highKey = highKey;
335 /* Assign it a cond space. */
336 GenCondSpace *condSpace = allCondSpaces + condNum;
337 stateCond->condSpace = condSpace;
339 curState->stateCondList.append( stateCond );
343 GenCondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
345 for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
346 Key csHighKey = cs->baseKey;
347 csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
349 if ( lowKey >= cs->baseKey && highKey <= csHighKey )
355 Condition *CodeGenData::findCondition( Key key )
357 for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
358 Key upperKey = cond->baseKey + (1 << cond->condSet.length());
359 if ( cond->baseKey <= key && key <= upperKey )
365 Key CodeGenData::findMaxKey()
367 Key maxKey = keyOps->maxKey;
368 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
369 assert( st->outSingle.length() == 0 );
370 assert( st->defTrans == 0 );
372 long rangeLen = st->outRange.length();
373 if ( rangeLen > 0 ) {
374 Key highKey = st->outRange[rangeLen-1].highKey;
375 if ( highKey > maxKey )
382 void CodeGenData::findFinalActionRefs()
384 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
385 /* Rerence count out of single transitions. */
386 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
387 if ( rtel->value->action != 0 ) {
388 rtel->value->action->numTransRefs += 1;
389 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
390 item->value->numTransRefs += 1;
394 /* Reference count out of range transitions. */
395 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
396 if ( rtel->value->action != 0 ) {
397 rtel->value->action->numTransRefs += 1;
398 for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
399 item->value->numTransRefs += 1;
403 /* Reference count default transition. */
404 if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
405 st->defTrans->action->numTransRefs += 1;
406 for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
407 item->value->numTransRefs += 1;
410 /* Reference count eof transitions. */
411 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
412 st->eofTrans->action->numTransRefs += 1;
413 for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ )
414 item->value->numTransRefs += 1;
417 /* Reference count to state actions. */
418 if ( st->toStateAction != 0 ) {
419 st->toStateAction->numToStateRefs += 1;
420 for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
421 item->value->numToStateRefs += 1;
424 /* Reference count from state actions. */
425 if ( st->fromStateAction != 0 ) {
426 st->fromStateAction->numFromStateRefs += 1;
427 for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
428 item->value->numFromStateRefs += 1;
431 /* Reference count EOF actions. */
432 if ( st->eofAction != 0 ) {
433 st->eofAction->numEofRefs += 1;
434 for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
435 item->value->numEofRefs += 1;
440 void CodeGenData::analyzeAction( GenAction *act, GenInlineList *inlineList )
442 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
443 /* Only consider actions that are referenced. */
444 if ( act->numRefs() > 0 ) {
445 if ( item->type == GenInlineItem::Goto || item->type == GenInlineItem::GotoExpr )
446 redFsm->bAnyActionGotos = true;
447 else if ( item->type == GenInlineItem::Call || item->type == GenInlineItem::CallExpr )
448 redFsm->bAnyActionCalls = true;
449 else if ( item->type == GenInlineItem::Ret )
450 redFsm->bAnyActionRets = true;
453 /* Check for various things in regular actions. */
454 if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
455 /* Any returns in regular actions? */
456 if ( item->type == GenInlineItem::Ret )
457 redFsm->bAnyRegActionRets = true;
459 /* Any next statements in the regular actions? */
460 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
461 redFsm->bAnyRegNextStmt = true;
463 /* Any by value control in regular actions? */
464 if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
465 redFsm->bAnyRegActionByValControl = true;
467 /* Any references to the current state in regular actions? */
468 if ( item->type == GenInlineItem::Curs )
469 redFsm->bAnyRegCurStateRef = true;
471 if ( item->type == GenInlineItem::Break )
472 redFsm->bAnyRegBreak = true;
475 if ( item->children != 0 )
476 analyzeAction( act, item->children );
480 void CodeGenData::analyzeActionList( RedAction *redAct, GenInlineList *inlineList )
482 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
483 /* Any next statements in the action table? */
484 if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
485 redAct->bAnyNextStmt = true;
487 /* Any references to the current state. */
488 if ( item->type == GenInlineItem::Curs )
489 redAct->bAnyCurStateRef = true;
491 if ( item->type == GenInlineItem::Break )
492 redAct->bAnyBreakStmt = true;
494 if ( item->children != 0 )
495 analyzeActionList( redAct, item->children );
499 /* Assign ids to referenced actions. */
500 void CodeGenData::assignActionIds()
502 int nextActionId = 0;
503 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
504 /* Only ever interested in referenced actions. */
505 if ( act->numRefs() > 0 )
506 act->actionId = nextActionId++;
510 void CodeGenData::setValueLimits()
512 redFsm->maxSingleLen = 0;
513 redFsm->maxRangeLen = 0;
514 redFsm->maxKeyOffset = 0;
515 redFsm->maxIndexOffset = 0;
516 redFsm->maxActListId = 0;
517 redFsm->maxActionLoc = 0;
518 redFsm->maxActArrItem = 0;
520 redFsm->maxCondSpan = 0;
521 redFsm->maxFlatIndexOffset = 0;
522 redFsm->maxCondOffset = 0;
523 redFsm->maxCondLen = 0;
524 redFsm->maxCondSpaceId = 0;
525 redFsm->maxCondIndexOffset = 0;
527 /* In both of these cases the 0 index is reserved for no value, so the max
528 * is one more than it would be if they started at 0. */
529 redFsm->maxIndex = redFsm->transSet.length();
530 redFsm->maxCond = condSpaceList.length();
532 /* The nextStateId - 1 is the last state id assigned. */
533 redFsm->maxState = redFsm->nextStateId - 1;
535 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
536 if ( csi->condSpaceId > redFsm->maxCondSpaceId )
537 redFsm->maxCondSpaceId = csi->condSpaceId;
540 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
541 /* Maximum cond length. */
542 if ( st->stateCondList.length() > redFsm->maxCondLen )
543 redFsm->maxCondLen = st->stateCondList.length();
545 /* Maximum single length. */
546 if ( st->outSingle.length() > redFsm->maxSingleLen )
547 redFsm->maxSingleLen = st->outSingle.length();
549 /* Maximum range length. */
550 if ( st->outRange.length() > redFsm->maxRangeLen )
551 redFsm->maxRangeLen = st->outRange.length();
553 /* The key offset index offset for the state after last is not used, skip it.. */
555 redFsm->maxCondOffset += st->stateCondList.length();
556 redFsm->maxKeyOffset += st->outSingle.length() + st->outRange.length()*2;
557 redFsm->maxIndexOffset += st->outSingle.length() + st->outRange.length() + 2;
561 if ( st->condList != 0 ) {
562 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
563 if ( span > redFsm->maxCondSpan )
564 redFsm->maxCondSpan = span;
568 if ( st->transList != 0 ) {
569 unsigned long long span = keyOps->span( st->lowKey, st->highKey );
570 if ( span > redFsm->maxSpan )
571 redFsm->maxSpan = span;
574 /* Max cond index offset. */
576 if ( st->condList != 0 )
577 redFsm->maxCondIndexOffset += keyOps->span( st->condLowKey, st->condHighKey );
580 /* Max flat index offset. */
582 if ( st->transList != 0 )
583 redFsm->maxFlatIndexOffset += keyOps->span( st->lowKey, st->highKey );
584 redFsm->maxFlatIndexOffset += 1;
588 for ( GenActionTableMap::Iter at = redFsm->actionMap; at.lte(); at++ ) {
589 /* Maximum id of action lists. */
590 if ( at->actListId+1 > redFsm->maxActListId )
591 redFsm->maxActListId = at->actListId+1;
593 /* Maximum location of items in action array. */
594 if ( at->location+1 > redFsm->maxActionLoc )
595 redFsm->maxActionLoc = at->location+1;
597 /* Maximum values going into the action array. */
598 if ( at->key.length() > redFsm->maxActArrItem )
599 redFsm->maxActArrItem = at->key.length();
600 for ( GenActionTable::Iter item = at->key; item.lte(); item++ ) {
601 if ( item->value->actionId > redFsm->maxActArrItem )
602 redFsm->maxActArrItem = item->value->actionId;
609 /* Gather various info on the machine. */
610 void CodeGenData::analyzeMachine()
612 /* Find the true count of action references. */
613 findFinalActionRefs();
615 /* Check if there are any calls in action code. */
616 for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
617 /* Record the occurrence of various kinds of actions. */
618 if ( act->numToStateRefs > 0 )
619 redFsm->bAnyToStateActions = true;
620 if ( act->numFromStateRefs > 0 )
621 redFsm->bAnyFromStateActions = true;
622 if ( act->numEofRefs > 0 )
623 redFsm->bAnyEofActions = true;
624 if ( act->numTransRefs > 0 )
625 redFsm->bAnyRegActions = true;
627 /* Recurse through the action's parse tree looking for various things. */
628 analyzeAction( act, act->inlineList );
631 /* Analyze reduced action lists. */
632 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
633 for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ )
634 analyzeActionList( redAct, act->value->inlineList );
637 /* Find states that have transitions with actions that have next
639 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
640 /* Check any actions out of outSinge. */
641 for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
642 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
643 st->bAnyRegCurStateRef = true;
646 /* Check any actions out of outRange. */
647 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
648 if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
649 st->bAnyRegCurStateRef = true;
652 /* Check any action out of default. */
653 if ( st->defTrans != 0 && st->defTrans->action != 0 &&
654 st->defTrans->action->anyCurStateRef() )
655 st->bAnyRegCurStateRef = true;
657 if ( st->stateCondList.length() > 0 )
658 redFsm->bAnyConditions = true;
660 if ( st->eofTrans != 0 )
661 redFsm->bAnyEofTrans = true;
664 /* Assign ids to actions that are referenced. */
667 /* Set the maximums of various values used for deciding types. */
671 void CodeGenData::writeStatement( GenInputLoc &loc, int nargs, char **args )
673 /* FIXME: This should be moved to the virtual functions in the code
676 * Force a newline. */
678 genLineDirective( out );
680 if ( strcmp( args[0], "data" ) == 0 ) {
681 for ( int i = 1; i < nargs; i++ ) {
682 if ( strcmp( args[i], "noerror" ) == 0 )
684 else if ( strcmp( args[i], "noprefix" ) == 0 )
686 else if ( strcmp( args[i], "nofinal" ) == 0 )
687 writeFirstFinal = false;
689 source_warning(loc) << "unrecognized write option \"" <<
690 args[i] << "\"" << endl;
695 else if ( strcmp( args[0], "init" ) == 0 ) {
696 for ( int i = 1; i < nargs; i++ ) {
697 if ( strcmp( args[i], "nocs" ) == 0 )
700 source_warning(loc) << "unrecognized write option \"" <<
701 args[i] << "\"" << endl;
706 else if ( strcmp( args[0], "exec" ) == 0 ) {
707 for ( int i = 1; i < nargs; i++ ) {
708 if ( strcmp( args[i], "noend" ) == 0 )
711 source_warning(loc) << "unrecognized write option \"" <<
712 args[i] << "\"" << endl;
717 else if ( strcmp( args[0], "exports" ) == 0 ) {
718 for ( int i = 1; i < nargs; i++ ) {
719 source_warning(loc) << "unrecognized write option \"" <<
720 args[i] << "\"" << endl;
725 /* EMIT An error here. */
726 source_error(loc) << "unrecognized write command \"" <<
727 args[0] << "\"" << endl;
731 ostream &CodeGenData::source_warning( const GenInputLoc &loc )
733 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
737 ostream &CodeGenData::source_error( const GenInputLoc &loc )
740 assert( sourceFileName != 0 );
741 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";