2 * Copyright 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
23 #include "rlcodegen.h"
24 #include "splitcodegen.h"
32 /* Emit the goto to take for a given transition. */
33 std::ostream &SplitCodeGen::TRANS_GOTO( RedTransAp *trans, int level )
35 if ( trans->targ->partition == currentPartition ) {
36 if ( trans->action != 0 ) {
37 /* Go to the transition which will go to the state. */
38 out << TABS(level) << "goto tr" << trans->id << ";";
41 /* Go directly to the target state. */
42 out << TABS(level) << "goto st" << trans->targ->id << ";";
46 if ( trans->action != 0 ) {
47 /* Go to the transition which will go to the state. */
48 out << TABS(level) << "goto ptr" << trans->id << ";";
49 trans->partitionBoundary = true;
52 /* Go directly to the target state. */
53 out << TABS(level) << "goto pst" << trans->targ->id << ";";
54 trans->targ->partitionBoundary = true;
60 /* Called from before writing the gotos for each state. */
61 void SplitCodeGen::GOTO_HEADER( RedStateAp *state, bool stateInPartition )
63 bool anyWritten = IN_TRANS_ACTIONS( state );
65 if ( state->labelNeeded )
66 out << "st" << state->id << ":\n";
68 if ( state->toStateAction != 0 ) {
69 /* Remember that we wrote an action. Write every action in the list. */
71 for ( ActionTable::Iter item = state->toStateAction->key; item.lte(); item++ )
72 ACTION( out, item->value, state->id, false );
75 /* Advance and test buffer pos. */
76 if ( state->labelNeeded ) {
79 " if ( ++" << P() << " == " << PE() << " )\n"
80 " goto _out" << state->id << ";\n";
84 " " << P() << " += 1;\n";
88 /* Give the state a switch case. */
89 out << "case " << state->id << ":\n";
91 if ( state->fromStateAction != 0 ) {
92 /* Remember that we wrote an action. Write every action in the list. */
94 for ( ActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ )
95 ACTION( out, item->value, state->id, false );
99 genLineDirective( out );
101 /* Record the prev state if necessary. */
102 if ( state->anyRegCurStateRef() )
103 out << " _ps = " << state->id << ";\n";
106 std::ostream &SplitCodeGen::STATE_GOTOS( int partition )
108 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
109 if ( st->partition == partition ) {
110 if ( st == redFsm->errState )
113 /* We call into the base of the goto which calls back into us
114 * using virtual functions. Set the current partition rather
115 * than coding parameter passing throughout. */
116 currentPartition = partition;
118 /* Writing code above state gotos. */
119 GOTO_HEADER( st, st->partition == partition );
121 if ( st->stateCondVect.length() > 0 ) {
122 out << " _widec = " << GET_KEY() << ";\n";
123 emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 );
127 if ( st->outSingle.length() > 0 )
128 emitSingleSwitch( st );
130 /* Default case is to binary search for the ranges, if that fails then */
131 if ( st->outRange.length() > 0 )
132 emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 );
134 /* Write the default transition. */
135 TRANS_GOTO( st->defTrans, 1 ) << "\n";
143 std::ostream &SplitCodeGen::PART_TRANS( int partition )
145 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
146 if ( trans->partitionBoundary ) {
148 "ptr" << trans->id << ":\n";
150 if ( trans->action != 0 ) {
151 /* If the action contains a next, then we must preload the current
152 * state since the action may or may not set it. */
153 if ( trans->action->anyNextStmt() )
154 out << " " << CS() << " = " << trans->targ->id << ";\n";
156 /* Write each action in the list. */
157 for ( ActionTable::Iter item = trans->action->key; item.lte(); item++ )
158 ACTION( out, item->value, trans->targ->id, false );
162 " goto pst" << trans->targ->id << ";\n";
163 trans->targ->partitionBoundary = true;
167 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
168 if ( st->partitionBoundary ) {
170 " pst" << st->id << ":\n"
171 " " << CS() << " = " << st->id << ";\n";
173 if ( st->toStateAction != 0 ) {
174 /* Remember that we wrote an action. Write every action in the list. */
175 for ( ActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
176 ACTION( out, item->value, st->id, false );
177 genLineDirective( out );
180 ptOutLabelUsed = true;
181 out << " goto _pt_out; \n";
187 std::ostream &SplitCodeGen::EXIT_STATES( int partition )
189 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
190 if ( st->partition == partition && st->outNeeded ) {
192 out << " _out" << st->id << ": " << CS() << " = " <<
193 st->id << "; goto _out; \n";
200 std::ostream &SplitCodeGen::PARTITION( int partition )
202 outLabelUsed = false;
203 ptOutLabelUsed = false;
205 /* Initialize the partition boundaries, which get set during the writing
206 * of states. After the state writing we will */
207 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
208 trans->partitionBoundary = false;
209 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
210 st->partitionBoundary = false;
212 out << " " << ALPH_TYPE() << " *p = *_pp, *pe = *_ppe;\n";
214 if ( anyRegCurStateRef() )
215 out << " int _ps = 0;\n";
217 if ( anyConditions() )
218 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
220 if ( useAgainLabel() ) {
225 " switch ( " << CS() << " ) {\n";
235 " if ( ++" << P() << " == " << PE() << " )\n"
241 " " << P() << " += 1;\n";
249 " switch ( " << CS() << " )\n {\n";
250 STATE_GOTOS( partition );
253 PART_TRANS( partition );
254 EXIT_STATES( partition );
256 if ( outLabelUsed ) {
265 if ( ptOutLabelUsed ) {
277 std::ostream &SplitCodeGen::PART_MAP()
279 int *partMap = new int[redFsm->stateList.length()];
280 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
281 partMap[st->id] = st->partition;
285 for ( int i = 0; i < redFsm->stateList.length(); i++ ) {
287 if ( i != redFsm->stateList.length() - 1 ) {
289 if ( ++totalItem % IALL == 0 )
298 void SplitCodeGen::writeOutData()
301 "static const int " << START() << " = " << START_STATE_ID() << ";\n"
304 if ( cgd->writeFirstFinal ) {
306 "static const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
310 if ( cgd->writeErr ) {
312 "static const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
317 OPEN_ARRAY( ARRAY_TYPE(numSplitPartitions), PM() );
322 for ( int p = 0; p < redFsm->nParts; p++ ) {
323 out << "int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
324 " **_ppe, struct " << FSM_NAME() << " *fsm );\n";
329 std::ostream &SplitCodeGen::ALL_PARTITIONS()
331 /* compute the format string. */
332 int width = 0, high = redFsm->nParts - 1;
337 assert( width <= 8 );
338 char suffFormat[] = "_%6.6d.c";
339 suffFormat[2] = suffFormat[4] = ( '0' + width );
341 for ( int p = 0; p < redFsm->nParts; p++ ) {
343 sprintf( suffix, suffFormat, p );
344 char *fn = fileNameFromStem( cgd->fileName, suffix );
345 char *include = fileNameFromStem( cgd->fileName, ".h" );
347 /* Create the filter on the output and open it. */
348 output_filter *partFilter = new output_filter;
349 partFilter->open( fn, ios::out|ios::trunc );
350 if ( !outFilter->is_open() ) {
351 error() << "error opening " << fn << " for writing" << endl;
355 /* Attach the new file to the output stream. */
356 std::streambuf *prev_rdbuf = out.rdbuf( partFilter );
359 "#include \"" << include << "\"\n"
360 "int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
361 " **_ppe, struct " << FSM_NAME() << " *fsm )\n"
367 /* Fix the output stream. */
368 out.rdbuf( prev_rdbuf );
374 void SplitCodeGen::writeOutExec()
382 " if ( " << P() << " == " << PE() << " )\n"
386 out << " goto _resume;\n";
388 /* In this reentry, to-state actions have already been executed on the
389 * partition-switch exit from the last partition. */
390 out << "_reenter:\n";
394 " if ( ++" << P() << " == " << PE() << " )\n"
399 " " << P() << " += 1;\n";
405 " switch ( " << PM() << "[" << CS() << "] ) {\n";
406 for ( int p = 0; p < redFsm->nParts; p++ ) {
408 " case " << p << ":\n"
409 " _stat = partition" << p << "( &p, &pe, fsm );\n"
418 out << " _out: {}\n";
426 void SplitCodeGen::setLabelsNeeded( RedStateAp *fromState, InlineList *inlineList )
428 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
429 switch ( item->type ) {
430 case InlineItem::Goto: case InlineItem::Call: {
431 /* In split code gen we only need labels for transitions across
433 if ( fromState->partition == item->targState->partition ){
434 /* Mark the target as needing a label. */
435 item->targState->labelNeeded = true;
442 if ( item->children != 0 )
443 setLabelsNeeded( fromState, item->children );
447 void SplitCodeGen::setLabelsNeeded( RedStateAp *fromState, RedTransAp *trans )
449 /* In the split code gen we don't need labels for transitions across
451 if ( fromState->partition == trans->targ->partition ) {
452 /* If there is no action with a next statement, then the label will be
454 trans->labelNeeded = true;
455 if ( trans->action == 0 || !trans->action->anyNextStmt() )
456 trans->targ->labelNeeded = true;
459 /* Need labels for states that have goto or calls in action code
460 * invoked on characters (ie, not from out action code). */
461 if ( trans->action != 0 ) {
462 /* Loop the actions. */
463 for ( ActionTable::Iter act = trans->action->key; act.lte(); act++ ) {
464 /* Get the action and walk it's tree. */
465 setLabelsNeeded( fromState, act->value->inlineList );
470 /* Set up labelNeeded flag for each state. */
471 void SplitCodeGen::setLabelsNeeded()
473 /* If we use the _again label, then we the _again switch, which uses all
475 if ( useAgainLabel() ) {
476 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
477 st->labelNeeded = true;
480 /* Do not use all labels by default, init all labelNeeded vars to false. */
481 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
482 st->labelNeeded = false;
483 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
484 trans->labelNeeded = false;
486 if ( redFsm->errState != 0 && anyLmSwitchError() )
487 redFsm->errState->labelNeeded = true;
489 /* Walk all transitions and set only those that have targs. */
490 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
491 for ( RedTransList::Iter tel = st->outRange; tel.lte(); tel++ )
492 setLabelsNeeded( st, tel->value );
494 for ( RedTransList::Iter tel = st->outSingle; tel.lte(); tel++ )
495 setLabelsNeeded( st, tel->value );
497 if ( st->defTrans != 0 )
498 setLabelsNeeded( st, st->defTrans );
503 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
504 st->outNeeded = st->labelNeeded;
507 if ( redFsm->errState != 0 )
508 redFsm->errState->outNeeded = true;
510 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
511 /* Any state with a transition in that has a break will need an
513 if ( trans->action != 0 && trans->action->anyBreakStmt() )
514 trans->targ->outNeeded = true;