2 * Copyright 2001-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
27 using std::ostringstream;
31 string Action::nameOrLoc()
37 ret << loc.line << ":" << loc.col;
45 forcedErrorState(false),
52 bAnyToStateActions(false),
53 bAnyFromStateActions(false),
54 bAnyRegActions(false),
55 bAnyEofActions(false),
56 bAnyActionGotos(false),
57 bAnyActionCalls(false),
58 bAnyActionRets(false),
59 bAnyRegActionRets(false),
60 bAnyRegActionByValControl(false),
61 bAnyRegNextStmt(false),
62 bAnyRegCurStateRef(false),
64 bAnyLmSwitchError(false),
69 /* Does the machine have any actions. */
70 bool RedFsmAp::anyActions()
72 return actionMap.length() > 0;
75 void RedFsmAp::depthFirstOrdering( RedStateAp *state )
77 /* Nothing to do if the state is already on the list. */
78 if ( state->onStateList )
81 /* Doing depth first, put state on the list. */
82 state->onStateList = true;
83 stateList.append( state );
85 /* At this point transitions should only be in ranges. */
86 assert( state->outSingle.length() == 0 );
87 assert( state->defTrans == 0 );
89 /* Recurse on everything ranges. */
90 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ ) {
91 if ( rtel->value->targ != 0 )
92 depthFirstOrdering( rtel->value->targ );
96 /* Ordering states by transition connections. */
97 void RedFsmAp::depthFirstOrdering()
99 /* Init on state list flags. */
100 for ( RedStateList::Iter st = stateList; st.lte(); st++ )
101 st->onStateList = false;
103 /* Clear out the state list, we will rebuild it. */
104 int stateListLen = stateList.length();
107 /* Add back to the state list from the start state and all other entry
109 depthFirstOrdering( startState );
110 for ( RedStateSet::Iter en = entryPoints; en.lte(); en++ )
111 depthFirstOrdering( *en );
112 if ( forcedErrorState )
113 depthFirstOrdering( errState );
115 /* Make sure we put everything back on. */
116 assert( stateListLen == stateList.length() );
119 /* Assign state ids by appearance in the state list. */
120 void RedFsmAp::sequentialStateIds()
122 /* Table based machines depend on the state numbers starting at zero. */
124 for ( RedStateList::Iter st = stateList; st.lte(); st++ )
125 st->id = nextStateId++;
128 /* Stable sort the states by final state status. */
129 void RedFsmAp::sortStatesByFinal()
131 /* Move forward through the list and throw final states onto the end. */
132 RedStateAp *state = 0;
133 RedStateAp *next = stateList.head;
134 RedStateAp *last = stateList.tail;
135 while ( state != last ) {
136 /* Move forward and load up the next. */
140 /* Throw to the end? */
141 if ( state->isFinal ) {
142 stateList.detach( state );
143 stateList.append( state );
148 /* Assign state ids by final state state status. */
149 void RedFsmAp::sortStateIdsByFinal()
151 /* Table based machines depend on this starting at zero. */
154 /* First pass to assign non final ids. */
155 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
157 st->id = nextStateId++;
160 /* Second pass to assign final ids. */
161 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
163 st->id = nextStateId++;
167 void RedFsmAp::sortByStateId()
169 /* FIXME: Implement. */
172 /* Find the final state with the lowest id. */
173 void RedFsmAp::findFirstFinState()
175 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
176 if ( st->isFinal && (firstFinState == 0 || st->id < firstFinState->id) )
181 void RedFsmAp::assignActionLocs()
183 int nextLocation = 0;
184 for ( ActionTableMap::Iter act = actionMap; act.lte(); act++ ) {
185 /* Store the loc, skip over the array and a null terminator. */
186 act->location = nextLocation;
187 nextLocation += act->key.length() + 1;
191 /* Check if we can extend the current range by displacing any ranges
192 * ahead to the singles. */
193 bool RedFsmAp::canExtend( const RedTransList &list, int pos )
195 /* Get the transition that we want to extend. */
196 RedTransAp *extendTrans = list[pos].value;
198 /* Look ahead in the transition list. */
199 for ( int next = pos + 1; next < list.length(); pos++, next++ ) {
200 /* If they are not continuous then cannot extend. */
201 Key nextKey = list[next].lowKey;
203 if ( list[pos].highKey != nextKey )
206 /* Check for the extenstion property. */
207 if ( extendTrans == list[next].value )
210 /* If the span of the next element is more than one, then don't keep
211 * checking, it won't be moved to single. */
212 unsigned long long nextSpan = keyOps->span( list[next].lowKey, list[next].highKey );
219 /* Move ranges to the singles list. */
220 void RedFsmAp::moveTransToSingle( RedStateAp *state )
222 RedTransList &range = state->outRange;
223 RedTransList &single = state->outSingle;
224 for ( int rpos = 0; rpos < range.length(); ) {
225 /* Check if this is a range we can extend. */
226 if ( canExtend( range, rpos ) ) {
227 /* Transfer singles over. */
228 while ( range[rpos].value != range[rpos+1].value ) {
229 /* Transfer the range to single. */
230 single.append( range[rpos+1] );
231 range.remove( rpos+1 );
235 range[rpos].highKey = range[rpos+1].highKey;
236 range.remove( rpos+1 );
238 /* Maybe move it to the singles. */
239 else if ( keyOps->span( range[rpos].lowKey, range[rpos].highKey ) == 1 ) {
240 single.append( range[rpos] );
241 range.remove( rpos );
244 /* Keeping it in the ranges. */
250 /* Look through ranges and choose suitable single character transitions. */
251 void RedFsmAp::chooseSingle()
253 /* Loop the states. */
254 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
255 /* Rewrite the transition list taking out the suitable single
257 moveTransToSingle( st );
261 void RedFsmAp::makeFlat()
263 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
264 if ( st->stateCondList.length() == 0 ) {
269 st->condLowKey = st->stateCondList.head->lowKey;
270 st->condHighKey = st->stateCondList.tail->highKey;
272 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
273 st->condList = new CondSpace*[ span ];
274 memset( st->condList, 0, sizeof(CondSpace*)*span );
276 for ( StateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ ) {
277 unsigned long long base, trSpan;
278 base = keyOps->span( st->condLowKey, sci->lowKey )-1;
279 trSpan = keyOps->span( sci->lowKey, sci->highKey );
280 for ( unsigned long long pos = 0; pos < trSpan; pos++ )
281 st->condList[base+pos] = sci->condSpace;
285 if ( st->outRange.length() == 0 ) {
286 st->lowKey = st->highKey = 0;
290 st->lowKey = st->outRange[0].lowKey;
291 st->highKey = st->outRange[st->outRange.length()-1].highKey;
292 unsigned long long span = keyOps->span( st->lowKey, st->highKey );
293 st->transList = new RedTransAp*[ span ];
294 memset( st->transList, 0, sizeof(RedTransAp*)*span );
296 for ( RedTransList::Iter trans = st->outRange; trans.lte(); trans++ ) {
297 unsigned long long base, trSpan;
298 base = keyOps->span( st->lowKey, trans->lowKey )-1;
299 trSpan = keyOps->span( trans->lowKey, trans->highKey );
300 for ( unsigned long long pos = 0; pos < trSpan; pos++ )
301 st->transList[base+pos] = trans->value;
304 /* Fill in the gaps with the default transition. */
305 for ( unsigned long long pos = 0; pos < span; pos++ ) {
306 if ( st->transList[pos] == 0 )
307 st->transList[pos] = st->defTrans;
314 /* A default transition has been picked, move it from the outRange to the
315 * default pointer. */
316 void RedFsmAp::moveToDefault( RedTransAp *defTrans, RedStateAp *state )
318 /* Rewrite the outRange, omitting any ranges that use
319 * the picked default. */
320 RedTransList outRange;
321 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ ) {
322 /* If it does not take the default, copy it over. */
323 if ( rtel->value != defTrans )
324 outRange.append( *rtel );
327 /* Save off the range we just created into the state's range. */
328 state->outRange.transfer( outRange );
330 /* Store the default. */
331 state->defTrans = defTrans;
334 bool RedFsmAp::alphabetCovered( RedTransList &outRange )
336 /* Cannot cover without any out ranges. */
337 if ( outRange.length() == 0 )
340 /* If the first range doesn't start at the the lower bound then the
341 * alphabet is not covered. */
342 RedTransList::Iter rtel = outRange;
343 if ( keyOps->minKey < rtel->lowKey )
346 /* Check that every range is next to the previous one. */
348 for ( ; rtel.lte(); rtel++ ) {
349 Key highKey = rtel[-1].highKey;
351 if ( highKey != rtel->lowKey )
355 /* The last must extend to the upper bound. */
356 RedTransEl *last = &outRange[outRange.length()-1];
357 if ( last->highKey < keyOps->maxKey )
363 RedTransAp *RedFsmAp::chooseDefaultSpan( RedStateAp *state )
365 /* Make a set of transitions from the outRange. */
366 RedTransSet stateTransSet;
367 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ )
368 stateTransSet.insert( rtel->value );
370 /* For each transition in the find how many alphabet characters the
371 * transition spans. */
372 unsigned long long *span = new unsigned long long[stateTransSet.length()];
373 memset( span, 0, sizeof(unsigned long long) * stateTransSet.length() );
374 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ ) {
375 /* Lookup the transition in the set. */
376 RedTransAp **inSet = stateTransSet.find( rtel->value );
377 int pos = inSet - stateTransSet.data;
378 span[pos] += keyOps->span( rtel->lowKey, rtel->highKey );
381 /* Find the max span, choose it for making the default. */
382 RedTransAp *maxTrans = 0;
383 unsigned long long maxSpan = 0;
384 for ( RedTransSet::Iter rtel = stateTransSet; rtel.lte(); rtel++ ) {
385 if ( span[rtel.pos()] > maxSpan ) {
386 maxSpan = span[rtel.pos()];
395 /* Pick default transitions from ranges for the states. */
396 void RedFsmAp::chooseDefaultSpan()
398 /* Loop the states. */
399 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
400 /* Only pick a default transition if the alphabet is covered. This
401 * avoids any transitions in the out range that go to error and avoids
402 * the need for an ERR state. */
403 if ( alphabetCovered( st->outRange ) ) {
404 /* Pick a default transition by largest span. */
405 RedTransAp *defTrans = chooseDefaultSpan( st );
407 /* Rewrite the transition list taking out the transition we picked
408 * as the default and store the default. */
409 moveToDefault( defTrans, st );
414 RedTransAp *RedFsmAp::chooseDefaultGoto( RedStateAp *state )
416 /* Make a set of transitions from the outRange. */
417 RedTransSet stateTransSet;
418 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ ) {
419 if ( rtel->value->targ == state->next )
425 void RedFsmAp::chooseDefaultGoto()
427 /* Loop the states. */
428 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
429 /* Pick a default transition. */
430 RedTransAp *defTrans = chooseDefaultGoto( st );
432 defTrans = chooseDefaultSpan( st );
434 /* Rewrite the transition list taking out the transition we picked
435 * as the default and store the default. */
436 moveToDefault( defTrans, st );
440 RedTransAp *RedFsmAp::chooseDefaultNumRanges( RedStateAp *state )
442 /* Make a set of transitions from the outRange. */
443 RedTransSet stateTransSet;
444 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ )
445 stateTransSet.insert( rtel->value );
447 /* For each transition in the find how many ranges use the transition. */
448 int *numRanges = new int[stateTransSet.length()];
449 memset( numRanges, 0, sizeof(int) * stateTransSet.length() );
450 for ( RedTransList::Iter rtel = state->outRange; rtel.lte(); rtel++ ) {
451 /* Lookup the transition in the set. */
452 RedTransAp **inSet = stateTransSet.find( rtel->value );
453 numRanges[inSet - stateTransSet.data] += 1;
456 /* Find the max number of ranges. */
457 RedTransAp *maxTrans = 0;
458 int maxNumRanges = 0;
459 for ( RedTransSet::Iter rtel = stateTransSet; rtel.lte(); rtel++ ) {
460 if ( numRanges[rtel.pos()] > maxNumRanges ) {
461 maxNumRanges = numRanges[rtel.pos()];
470 void RedFsmAp::chooseDefaultNumRanges()
472 /* Loop the states. */
473 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
474 /* Pick a default transition. */
475 RedTransAp *defTrans = chooseDefaultNumRanges( st );
477 /* Rewrite the transition list taking out the transition we picked
478 * as the default and store the default. */
479 moveToDefault( defTrans, st );
483 RedTransAp *RedFsmAp::getErrorTrans( )
485 /* If the error trans has not been made aready, make it. */
486 if ( errTrans == 0 ) {
487 /* This insert should always succeed since no transition created by
488 * the user can point to the error state. */
489 errTrans = new RedTransAp( getErrorState(), 0, nextTransId++ );
490 RedTransAp *inRes = transSet.insert( errTrans );
491 assert( inRes != 0 );
496 RedStateAp *RedFsmAp::getErrorState()
498 /* Something went wrong. An error state is needed but one was not supplied
499 * by the frontend. */
500 assert( errState != 0 );
505 RedTransAp *RedFsmAp::allocateTrans( RedStateAp *targ, RedAction *action )
507 /* Create a reduced trans and look for it in the transiton set. */
508 RedTransAp redTrans( targ, action, 0 );
509 RedTransAp *inDict = transSet.find( &redTrans );
511 inDict = new RedTransAp( targ, action, nextTransId++ );
512 transSet.insert( inDict );
517 void RedFsmAp::partitionFsm( int nparts )
519 /* At this point the states are ordered by a depth-first traversal. We
520 * will allocate to partitions based on this ordering. */
521 this->nParts = nparts;
522 int partSize = stateList.length() / nparts;
523 int remainder = stateList.length() % nparts;
524 int numInPart = partSize;
526 if ( remainder-- > 0 )
528 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
529 st->partition = partition;
532 if ( numInPart == 0 ) {
534 numInPart = partSize;
535 if ( remainder-- > 0 )
541 void RedFsmAp::setInTrans()
543 /* First pass counts the number of transitions. */
544 for ( TransApSet::Iter trans = transSet; trans.lte(); trans++ )
545 trans->targ->numInTrans += 1;
547 /* Pass over states to allocate the needed memory. Reset the counts so we
548 * can use them as the current size. */
549 for ( RedStateList::Iter st = stateList; st.lte(); st++ ) {
550 st->inTrans = new RedTransAp*[st->numInTrans];
554 /* Second pass over transitions copies pointers into the in trans list. */
555 for ( TransApSet::Iter trans = transSet; trans.lte(); trans++ )
556 trans->targ->inTrans[trans->targ->numInTrans++] = trans;