2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
9 * Ragel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Ragel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Ragel; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "fsmcodegen.h"
34 using std::ostringstream;
39 void lineDirective( ostream &out, char *fileName, int line )
41 if ( noLineDirectives )
44 /* Write the preprocessor line info for to the input file. */
45 out << "#line " << line << " \"";
46 for ( char *pc = fileName; *pc != 0; pc++ ) {
54 if ( noLineDirectives )
60 void genLineDirective( ostream &out )
62 std::streambuf *sbuf = out.rdbuf();
63 output_filter *filter = static_cast<output_filter*>(sbuf);
64 lineDirective( out, filter->fileName, filter->line + 1 );
68 /* Init code gen with in parameters. */
69 FsmCodeGen::FsmCodeGen( ostream &out )
75 unsigned int FsmCodeGen::arrayTypeSize( unsigned long maxVal )
77 long long maxValLL = (long long) maxVal;
78 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
79 assert( arrayType != 0 );
80 return arrayType->size;
83 string FsmCodeGen::ARRAY_TYPE( unsigned long maxVal )
85 long long maxValLL = (long long) maxVal;
86 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
87 assert( arrayType != 0 );
89 string ret = arrayType->data1;
90 if ( arrayType->data2 != 0 ) {
92 ret += arrayType->data2;
98 /* Write out the fsm name. */
99 string FsmCodeGen::FSM_NAME()
104 /* Emit the offset of the start state as a decimal integer. */
105 string FsmCodeGen::START_STATE_ID()
108 ret << redFsm->startState->id;
112 /* Write out the array of actions. */
113 std::ostream &FsmCodeGen::ACTIONS_ARRAY()
116 int totalActions = 1;
117 for ( ActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
118 /* Write out the length, which will never be the last character. */
119 out << act->key.length() << ", ";
120 /* Put in a line break every 8 */
121 if ( totalActions++ % 8 == 7 )
124 for ( ActionTable::Iter item = act->key; item.lte(); item++ ) {
125 out << item->value->actionId;
126 if ( ! (act.last() && item.last()) )
129 /* Put in a line break every 8 */
130 if ( totalActions++ % 8 == 7 )
139 string FsmCodeGen::ACCESS()
142 if ( accessExpr != 0 )
143 INLINE_LIST( ret, accessExpr, 0, false, false );
148 string FsmCodeGen::P()
155 INLINE_LIST( ret, pExpr, 0, false, false );
161 string FsmCodeGen::PE()
168 INLINE_LIST( ret, peExpr, 0, false, false );
174 string FsmCodeGen::EOFV()
181 INLINE_LIST( ret, eofExpr, 0, false, false );
187 string FsmCodeGen::CS()
191 ret << ACCESS() << "cs";
193 /* Emit the user supplied method of retrieving the key. */
195 INLINE_LIST( ret, csExpr, 0, false, false );
201 string FsmCodeGen::TOP()
205 ret << ACCESS() + "top";
208 INLINE_LIST( ret, topExpr, 0, false, false );
214 string FsmCodeGen::STACK()
217 if ( stackExpr == 0 )
218 ret << ACCESS() + "stack";
221 INLINE_LIST( ret, stackExpr, 0, false, false );
227 string FsmCodeGen::ACT()
231 ret << ACCESS() + "act";
234 INLINE_LIST( ret, actExpr, 0, false, false );
240 string FsmCodeGen::TOKSTART()
243 if ( tokstartExpr == 0 )
244 ret << ACCESS() + "ts";
247 INLINE_LIST( ret, tokstartExpr, 0, false, false );
253 string FsmCodeGen::TOKEND()
256 if ( tokendExpr == 0 )
257 ret << ACCESS() + "te";
260 INLINE_LIST( ret, tokendExpr, 0, false, false );
266 string FsmCodeGen::GET_WIDE_KEY()
268 if ( redFsm->anyConditions() )
274 string FsmCodeGen::GET_WIDE_KEY( RedStateAp *state )
276 if ( state->stateCondList.length() > 0 )
282 string FsmCodeGen::GET_KEY()
285 if ( getKeyExpr != 0 ) {
286 /* Emit the user supplied method of retrieving the key. */
288 INLINE_LIST( ret, getKeyExpr, 0, false, false );
292 /* Expression for retrieving the key, use simple dereference. */
293 ret << "(*" << P() << ")";
298 /* Write out level number of tabs. Makes the nested binary search nice
300 string FsmCodeGen::TABS( int level )
303 while ( level-- > 0 )
308 /* Write out a key from the fsm code gen. Depends on wether or not the key is
310 string FsmCodeGen::KEY( Key key )
313 if ( keyOps->isSigned || !hostLang->explicitUnsigned )
316 ret << (unsigned long) key.getVal() << 'u';
320 void FsmCodeGen::EXEC( ostream &ret, InlineItem *item, int targState, int inFinish )
322 /* The parser gives fexec two children. The double brackets are for D
323 * code. If the inline list is a single word it will get interpreted as a
324 * C-style cast by the D compiler. */
325 ret << "{" << P() << " = ((";
326 INLINE_LIST( ret, item->children, targState, inFinish, false );
330 void FsmCodeGen::LM_SWITCH( ostream &ret, InlineItem *item,
331 int targState, int inFinish, bool csForced )
334 " switch( " << ACT() << " ) {\n";
336 for ( InlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
337 /* Write the case label, the action and the case break. */
338 ret << " case " << lma->lmId << ":\n";
340 /* Write the block and close it off. */
342 INLINE_LIST( ret, lma->children, targState, inFinish, csForced );
347 /* Default required for D code. */
354 void FsmCodeGen::SET_ACT( ostream &ret, InlineItem *item )
356 ret << ACT() << " = " << item->lmId << ";";
359 void FsmCodeGen::SET_TOKEND( ostream &ret, InlineItem *item )
361 /* The tokend action sets tokend. */
362 ret << TOKEND() << " = " << P();
363 if ( item->offset != 0 )
364 out << "+" << item->offset;
368 void FsmCodeGen::GET_TOKEND( ostream &ret, InlineItem *item )
373 void FsmCodeGen::INIT_TOKSTART( ostream &ret, InlineItem *item )
375 ret << TOKSTART() << " = " << NULL_ITEM() << ";";
378 void FsmCodeGen::INIT_ACT( ostream &ret, InlineItem *item )
380 ret << ACT() << " = 0;";
383 void FsmCodeGen::SET_TOKSTART( ostream &ret, InlineItem *item )
385 ret << TOKSTART() << " = " << P() << ";";
388 void FsmCodeGen::SUB_ACTION( ostream &ret, InlineItem *item,
389 int targState, bool inFinish, bool csForced )
391 if ( item->children->length() > 0 ) {
392 /* Write the block and close it off. */
394 INLINE_LIST( ret, item->children, targState, inFinish, csForced );
400 /* Write out an inline tree structure. Walks the list and possibly calls out
401 * to virtual functions than handle language specific items in the tree. */
402 void FsmCodeGen::INLINE_LIST( ostream &ret, InlineList *inlineList,
403 int targState, bool inFinish, bool csForced )
405 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
406 switch ( item->type ) {
407 case InlineItem::Text:
410 case InlineItem::Goto:
411 GOTO( ret, item->targState->id, inFinish );
413 case InlineItem::Call:
414 CALL( ret, item->targState->id, targState, inFinish );
416 case InlineItem::Next:
417 NEXT( ret, item->targState->id, inFinish );
419 case InlineItem::Ret:
420 RET( ret, inFinish );
422 case InlineItem::PChar:
425 case InlineItem::Char:
428 case InlineItem::Hold:
431 case InlineItem::Exec:
432 EXEC( ret, item, targState, inFinish );
434 case InlineItem::Curs:
435 CURS( ret, inFinish );
437 case InlineItem::Targs:
438 TARGS( ret, inFinish, targState );
440 case InlineItem::Entry:
441 ret << item->targState->id;
443 case InlineItem::GotoExpr:
444 GOTO_EXPR( ret, item, inFinish );
446 case InlineItem::CallExpr:
447 CALL_EXPR( ret, item, targState, inFinish );
449 case InlineItem::NextExpr:
450 NEXT_EXPR( ret, item, inFinish );
452 case InlineItem::LmSwitch:
453 LM_SWITCH( ret, item, targState, inFinish, csForced );
455 case InlineItem::LmSetActId:
456 SET_ACT( ret, item );
458 case InlineItem::LmSetTokEnd:
459 SET_TOKEND( ret, item );
461 case InlineItem::LmGetTokEnd:
462 GET_TOKEND( ret, item );
464 case InlineItem::LmInitTokStart:
465 INIT_TOKSTART( ret, item );
467 case InlineItem::LmInitAct:
468 INIT_ACT( ret, item );
470 case InlineItem::LmSetTokStart:
471 SET_TOKSTART( ret, item );
473 case InlineItem::SubAction:
474 SUB_ACTION( ret, item, targState, inFinish, csForced );
476 case InlineItem::Break:
477 BREAK( ret, targState, csForced );
482 /* Write out paths in line directives. Escapes any special characters. */
483 string FsmCodeGen::LDIR_PATH( char *path )
486 for ( char *pc = path; *pc != 0; pc++ ) {
495 void FsmCodeGen::ACTION( ostream &ret, Action *action, int targState,
496 bool inFinish, bool csForced )
498 /* Write the preprocessor line info for going into the source file. */
499 lineDirective( ret, sourceFileName, action->loc.line );
501 /* Write the block and close it off. */
503 INLINE_LIST( ret, action->inlineList, targState, inFinish, csForced );
507 void FsmCodeGen::CONDITION( ostream &ret, Action *condition )
510 lineDirective( ret, sourceFileName, condition->loc.line );
511 INLINE_LIST( ret, condition->inlineList, 0, false, false );
514 string FsmCodeGen::ERROR_STATE()
517 if ( redFsm->errState != 0 )
518 ret << redFsm->errState->id;
524 string FsmCodeGen::FIRST_FINAL_STATE()
527 if ( redFsm->firstFinState != 0 )
528 ret << redFsm->firstFinState->id;
530 ret << redFsm->nextStateId;
534 void FsmCodeGen::writeInit()
539 out << "\t" << CS() << " = " << START() << ";\n";
541 /* If there are any calls, then the stack top needs initialization. */
542 if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
543 out << "\t" << TOP() << " = 0;\n";
545 if ( hasLongestMatch ) {
547 " " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
548 " " << TOKEND() << " = " << NULL_ITEM() << ";\n"
549 " " << ACT() << " = 0;\n";
554 string FsmCodeGen::DATA_PREFIX()
557 return FSM_NAME() + "_";
561 /* Emit the alphabet data type. */
562 string FsmCodeGen::ALPH_TYPE()
564 string ret = keyOps->alphType->data1;
565 if ( keyOps->alphType->data2 != 0 ) {
567 ret += + keyOps->alphType->data2;
572 /* Emit the alphabet data type. */
573 string FsmCodeGen::WIDE_ALPH_TYPE()
576 if ( redFsm->maxKey <= keyOps->maxKey )
579 long long maxKeyVal = redFsm->maxKey.getLongLong();
580 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
581 assert( wideType != 0 );
583 ret = wideType->data1;
584 if ( wideType->data2 != 0 ) {
586 ret += wideType->data2;
592 void FsmCodeGen::STATE_IDS()
594 if ( redFsm->startState != 0 )
595 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
597 if ( writeFirstFinal )
598 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
601 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
605 if ( entryPointNames.length() > 0 ) {
606 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
607 STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) <<
608 " = " << entryPointIds[en.pos()] << ";\n";
616 * Language specific, but style independent code generators functions.
619 string CCodeGen::PTR_CONST()
624 std::ostream &CCodeGen::OPEN_ARRAY( string type, string name )
626 out << "static const " << type << " " << name << "[] = {\n";
630 std::ostream &CCodeGen::CLOSE_ARRAY()
632 return out << "};\n";
635 std::ostream &CCodeGen::STATIC_VAR( string type, string name )
637 out << "static const " << type << " " << name;
641 string CCodeGen::UINT( )
643 return "unsigned int";
646 string CCodeGen::ARR_OFF( string ptr, string offset )
648 return ptr + " + " + offset;
651 string CCodeGen::CAST( string type )
653 return "(" + type + ")";
656 string CCodeGen::NULL_ITEM()
661 string CCodeGen::POINTER()
666 std::ostream &CCodeGen::SWITCH_DEFAULT()
671 string CCodeGen::CTRL_FLOW()
676 void CCodeGen::writeExports()
678 if ( exportList.length() > 0 ) {
679 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
680 out << "#define " << DATA_PREFIX() << "ex_" << ex->name << " " <<
681 KEY(ex->key) << "\n";
691 string DCodeGen::NULL_ITEM()
696 string DCodeGen::POINTER()
698 // multiple items seperated by commas can also be pointer types.
702 string DCodeGen::PTR_CONST()
707 std::ostream &DCodeGen::OPEN_ARRAY( string type, string name )
709 out << "static const " << type << "[] " << name << " = [\n";
713 std::ostream &DCodeGen::CLOSE_ARRAY()
715 return out << "];\n";
718 std::ostream &DCodeGen::STATIC_VAR( string type, string name )
720 out << "static const " << type << " " << name;
724 string DCodeGen::ARR_OFF( string ptr, string offset )
726 return "&" + ptr + "[" + offset + "]";
729 string DCodeGen::CAST( string type )
731 return "cast(" + type + ")";
734 string DCodeGen::UINT( )
739 std::ostream &DCodeGen::SWITCH_DEFAULT()
741 out << " default: break;\n";
745 string DCodeGen::CTRL_FLOW()
750 void DCodeGen::writeExports()
752 if ( exportList.length() > 0 ) {
753 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
754 out << "static const " << ALPH_TYPE() << " " << DATA_PREFIX() <<
755 "ex_" << ex->name << " = " << KEY(ex->key) << ";\n";
762 * End D-specific code.
765 void FsmCodeGen::finishRagelDef()
767 if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
768 codeStyle == GenIpGoto || codeStyle == GenSplit )
770 /* For directly executable machines there is no required state
771 * ordering. Choose a depth-first ordering to increase the
772 * potential for fall-throughs. */
773 redFsm->depthFirstOrdering();
776 /* The frontend will do this for us, but it may be a good idea to
777 * force it if the intermediate file is edited. */
778 redFsm->sortByStateId();
781 /* Choose default transitions and the single transition. */
782 redFsm->chooseDefaultSpan();
784 /* Maybe do flat expand, otherwise choose single. */
785 if ( codeStyle == GenFlat || codeStyle == GenFFlat )
788 redFsm->chooseSingle();
790 /* If any errors have occured in the input file then don't write anything. */
791 if ( gblErrorCount > 0 )
794 if ( codeStyle == GenSplit )
795 redFsm->partitionFsm( numSplitPartitions );
797 if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
798 redFsm->setInTrans();
800 /* Anlayze Machine will find the final action reference counts, among
801 * other things. We will use these in reporting the usage
802 * of fsm directives in action code. */
805 /* Determine if we should use indicies. */
809 ostream &FsmCodeGen::source_warning( const InputLoc &loc )
811 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
815 ostream &FsmCodeGen::source_error( const InputLoc &loc )
818 assert( sourceFileName != 0 );
819 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";