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 /* Write the preprocessor line info for to the input file. */
42 out << "#line " << line << " \"";
43 for ( char *pc = fileName; *pc != 0; pc++ ) {
52 void genLineDirective( ostream &out )
54 std::streambuf *sbuf = out.rdbuf();
55 output_filter *filter = static_cast<output_filter*>(sbuf);
56 lineDirective( out, filter->fileName, filter->line + 1 );
60 /* Init code gen with in parameters. */
61 FsmCodeGen::FsmCodeGen( ostream &out )
67 unsigned int FsmCodeGen::arrayTypeSize( unsigned long maxVal )
69 long long maxValLL = (long long) maxVal;
70 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
71 assert( arrayType != 0 );
72 return arrayType->size;
75 string FsmCodeGen::ARRAY_TYPE( unsigned long maxVal )
77 long long maxValLL = (long long) maxVal;
78 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
79 assert( arrayType != 0 );
81 string ret = arrayType->data1;
82 if ( arrayType->data2 != 0 ) {
84 ret += arrayType->data2;
90 /* Write out the fsm name. */
91 string FsmCodeGen::FSM_NAME()
96 /* Emit the offset of the start state as a decimal integer. */
97 string FsmCodeGen::START_STATE_ID()
100 ret << redFsm->startState->id;
104 /* Write out the array of actions. */
105 std::ostream &FsmCodeGen::ACTIONS_ARRAY()
108 int totalActions = 1;
109 for ( ActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
110 /* Write out the length, which will never be the last character. */
111 out << act->key.length() << ", ";
112 /* Put in a line break every 8 */
113 if ( totalActions++ % 8 == 7 )
116 for ( ActionTable::Iter item = act->key; item.lte(); item++ ) {
117 out << item->value->actionId;
118 if ( ! (act.last() && item.last()) )
121 /* Put in a line break every 8 */
122 if ( totalActions++ % 8 == 7 )
131 string FsmCodeGen::CS()
134 if ( curStateExpr != 0 ) {
135 /* Emit the user supplied method of retrieving the key. */
137 INLINE_LIST( ret, curStateExpr, 0, false );
141 /* Expression for retrieving the key, use simple dereference. */
142 ret << ACCESS() << "cs";
147 string FsmCodeGen::ACCESS()
150 if ( accessExpr != 0 )
151 INLINE_LIST( ret, accessExpr, 0, false );
155 string FsmCodeGen::GET_WIDE_KEY()
157 if ( redFsm->anyConditions() )
163 string FsmCodeGen::GET_WIDE_KEY( RedStateAp *state )
165 if ( state->stateCondList.length() > 0 )
171 string FsmCodeGen::GET_KEY()
174 if ( getKeyExpr != 0 ) {
175 /* Emit the user supplied method of retrieving the key. */
177 INLINE_LIST( ret, getKeyExpr, 0, false );
181 /* Expression for retrieving the key, use simple dereference. */
182 ret << "(*" << P() << ")";
187 /* Write out level number of tabs. Makes the nested binary search nice
189 string FsmCodeGen::TABS( int level )
192 while ( level-- > 0 )
197 /* Write out a key from the fsm code gen. Depends on wether or not the key is
199 string FsmCodeGen::KEY( Key key )
202 if ( keyOps->isSigned || !hostLang->explicitUnsigned )
205 ret << (unsigned long) key.getVal() << 'u';
209 void FsmCodeGen::EXEC( ostream &ret, InlineItem *item, int targState, int inFinish )
211 /* The parser gives fexec two children. The double brackets are for D
212 * code. If the inline list is a single word it will get interpreted as a
213 * C-style cast by the D compiler. */
214 ret << "{" << P() << " = ((";
215 INLINE_LIST( ret, item->children, targState, inFinish );
219 void FsmCodeGen::EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish )
221 /* Tokend version of exec. */
223 /* The parser gives fexec two children. The double brackets are for D
224 * code. If the inline list is a single word it will get interpreted as a
225 * C-style cast by the D compiler. */
226 ret << "{" << TOKEND() << " = ((";
227 INLINE_LIST( ret, item->children, targState, inFinish );
232 void FsmCodeGen::LM_SWITCH( ostream &ret, InlineItem *item,
233 int targState, int inFinish )
236 " switch( " << ACT() << " ) {\n";
238 /* If the switch handles error then we also forced the error state. It
240 if ( item->handlesError ) {
241 ret << " case 0: " << TOKEND() << " = " << TOKSTART() << "; ";
242 GOTO( ret, redFsm->errState->id, inFinish );
246 for ( InlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
247 /* Write the case label, the action and the case break. */
248 ret << " case " << lma->lmId << ":\n";
250 /* Write the block and close it off. */
252 INLINE_LIST( ret, lma->children, targState, inFinish );
257 /* Default required for D code. */
264 void FsmCodeGen::SET_ACT( ostream &ret, InlineItem *item )
266 ret << ACT() << " = " << item->lmId << ";";
269 void FsmCodeGen::SET_TOKEND( ostream &ret, InlineItem *item )
271 /* The tokend action sets tokend. */
272 ret << TOKEND() << " = " << P();
273 if ( item->offset != 0 )
274 out << "+" << item->offset;
278 void FsmCodeGen::GET_TOKEND( ostream &ret, InlineItem *item )
283 void FsmCodeGen::INIT_TOKSTART( ostream &ret, InlineItem *item )
285 ret << TOKSTART() << " = " << NULL_ITEM() << ";";
288 void FsmCodeGen::INIT_ACT( ostream &ret, InlineItem *item )
290 ret << ACT() << " = 0;";
293 void FsmCodeGen::SET_TOKSTART( ostream &ret, InlineItem *item )
295 ret << TOKSTART() << " = " << P() << ";";
298 void FsmCodeGen::SUB_ACTION( ostream &ret, InlineItem *item,
299 int targState, bool inFinish )
301 if ( item->children->length() > 0 ) {
302 /* Write the block and close it off. */
304 INLINE_LIST( ret, item->children, targState, inFinish );
310 /* Write out an inline tree structure. Walks the list and possibly calls out
311 * to virtual functions than handle language specific items in the tree. */
312 void FsmCodeGen::INLINE_LIST( ostream &ret, InlineList *inlineList,
313 int targState, bool inFinish )
315 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
316 switch ( item->type ) {
317 case InlineItem::Text:
320 case InlineItem::Goto:
321 GOTO( ret, item->targState->id, inFinish );
323 case InlineItem::Call:
324 CALL( ret, item->targState->id, targState, inFinish );
326 case InlineItem::Next:
327 NEXT( ret, item->targState->id, inFinish );
329 case InlineItem::Ret:
330 RET( ret, inFinish );
332 case InlineItem::PChar:
335 case InlineItem::Char:
338 case InlineItem::Hold:
341 case InlineItem::Exec:
342 EXEC( ret, item, targState, inFinish );
344 case InlineItem::HoldTE:
345 ret << TOKEND() << "--;";
347 case InlineItem::ExecTE:
348 EXECTE( ret, item, targState, inFinish );
350 case InlineItem::Curs:
351 CURS( ret, inFinish );
353 case InlineItem::Targs:
354 TARGS( ret, inFinish, targState );
356 case InlineItem::Entry:
357 ret << item->targState->id;
359 case InlineItem::GotoExpr:
360 GOTO_EXPR( ret, item, inFinish );
362 case InlineItem::CallExpr:
363 CALL_EXPR( ret, item, targState, inFinish );
365 case InlineItem::NextExpr:
366 NEXT_EXPR( ret, item, inFinish );
368 case InlineItem::LmSwitch:
369 LM_SWITCH( ret, item, targState, inFinish );
371 case InlineItem::LmSetActId:
372 SET_ACT( ret, item );
374 case InlineItem::LmSetTokEnd:
375 SET_TOKEND( ret, item );
377 case InlineItem::LmGetTokEnd:
378 GET_TOKEND( ret, item );
380 case InlineItem::LmInitTokStart:
381 INIT_TOKSTART( ret, item );
383 case InlineItem::LmInitAct:
384 INIT_ACT( ret, item );
386 case InlineItem::LmSetTokStart:
387 SET_TOKSTART( ret, item );
389 case InlineItem::SubAction:
390 SUB_ACTION( ret, item, targState, inFinish );
392 case InlineItem::Break:
393 BREAK( ret, targState );
398 /* Write out paths in line directives. Escapes any special characters. */
399 string FsmCodeGen::LDIR_PATH( char *path )
402 for ( char *pc = path; *pc != 0; pc++ ) {
411 void FsmCodeGen::ACTION( ostream &ret, Action *action, int targState, bool inFinish )
413 /* Write the preprocessor line info for going into the source file. */
414 lineDirective( ret, sourceFileName, action->loc.line );
416 /* Write the block and close it off. */
418 INLINE_LIST( ret, action->inlineList, targState, inFinish );
422 void FsmCodeGen::CONDITION( ostream &ret, Action *condition )
425 lineDirective( ret, sourceFileName, condition->loc.line );
426 INLINE_LIST( ret, condition->inlineList, 0, false );
429 string FsmCodeGen::ERROR_STATE()
432 if ( redFsm->errState != 0 )
433 ret << redFsm->errState->id;
439 string FsmCodeGen::FIRST_FINAL_STATE()
442 if ( redFsm->firstFinState != 0 )
443 ret << redFsm->firstFinState->id;
445 ret << redFsm->nextStateId;
449 void FsmCodeGen::writeOutInit()
452 out << "\t" << CS() << " = " << START() << ";\n";
454 /* If there are any calls, then the stack top needs initialization. */
455 if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
456 out << "\t" << TOP() << " = 0;\n";
458 if ( hasLongestMatch ) {
460 " " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
461 " " << TOKEND() << " = " << NULL_ITEM() << ";\n"
462 " " << ACT() << " = 0;\n";
467 string FsmCodeGen::DATA_PREFIX()
470 return FSM_NAME() + "_";
474 /* Emit the alphabet data type. */
475 string FsmCodeGen::ALPH_TYPE()
477 string ret = keyOps->alphType->data1;
478 if ( keyOps->alphType->data2 != 0 ) {
480 ret += + keyOps->alphType->data2;
485 /* Emit the alphabet data type. */
486 string FsmCodeGen::WIDE_ALPH_TYPE()
489 if ( redFsm->maxKey <= keyOps->maxKey )
492 long long maxKeyVal = redFsm->maxKey.getLongLong();
493 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
494 assert( wideType != 0 );
496 ret = wideType->data1;
497 if ( wideType->data2 != 0 ) {
499 ret += wideType->data2;
507 * Language specific, but style independent code generators functions.
510 string CCodeGen::PTR_CONST()
515 std::ostream &CCodeGen::OPEN_ARRAY( string type, string name )
517 out << "static const " << type << " " << name << "[] = {\n";
521 std::ostream &CCodeGen::CLOSE_ARRAY()
523 return out << "};\n";
526 std::ostream &CCodeGen::STATIC_VAR( string type, string name )
528 out << "static const " << type << " " << name;
532 string CCodeGen::UINT( )
534 return "unsigned int";
537 string CCodeGen::ARR_OFF( string ptr, string offset )
539 return ptr + " + " + offset;
542 string CCodeGen::CAST( string type )
544 return "(" + type + ")";
547 string CCodeGen::NULL_ITEM()
552 string CCodeGen::POINTER()
557 std::ostream &CCodeGen::SWITCH_DEFAULT()
562 string CCodeGen::CTRL_FLOW()
571 string DCodeGen::NULL_ITEM()
576 string DCodeGen::POINTER()
578 // multiple items seperated by commas can also be pointer types.
582 string DCodeGen::PTR_CONST()
587 std::ostream &DCodeGen::OPEN_ARRAY( string type, string name )
589 out << "static const " << type << "[] " << name << " = [\n";
593 std::ostream &DCodeGen::CLOSE_ARRAY()
595 return out << "];\n";
598 std::ostream &DCodeGen::STATIC_VAR( string type, string name )
600 out << "static const " << type << " " << name;
604 string DCodeGen::ARR_OFF( string ptr, string offset )
606 return "&" + ptr + "[" + offset + "]";
609 string DCodeGen::CAST( string type )
611 return "cast(" + type + ")";
614 string DCodeGen::UINT( )
619 std::ostream &DCodeGen::SWITCH_DEFAULT()
621 out << " default: break;\n";
625 string DCodeGen::CTRL_FLOW()
630 void FsmCodeGen::finishRagelDef()
632 if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
633 codeStyle == GenIpGoto || codeStyle == GenSplit )
635 /* For directly executable machines there is no required state
636 * ordering. Choose a depth-first ordering to increase the
637 * potential for fall-throughs. */
638 redFsm->depthFirstOrdering();
641 /* The frontend will do this for us, but it may be a good idea to
642 * force it if the intermediate file is edited. */
643 redFsm->sortByStateId();
646 /* Choose default transitions and the single transition. */
647 redFsm->chooseDefaultSpan();
649 /* Maybe do flat expand, otherwise choose single. */
650 if ( codeStyle == GenFlat || codeStyle == GenFFlat )
653 redFsm->chooseSingle();
655 /* If any errors have occured in the input file then don't write anything. */
656 if ( gblErrorCount > 0 )
659 if ( codeStyle == GenSplit )
660 redFsm->partitionFsm( numSplitPartitions );
662 if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
663 redFsm->setInTrans();
665 /* Anlayze Machine will find the final action reference counts, among
666 * other things. We will use these in reporting the usage
667 * of fsm directives in action code. */
670 /* Determine if we should use indicies. */
674 ostream &FsmCodeGen::source_warning( const InputLoc &loc )
676 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
680 ostream &FsmCodeGen::source_error( const InputLoc &loc )
683 assert( sourceFileName != 0 );
684 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";