2 * 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
6 /* This file is part of Ragel.
8 * Ragel is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Ragel is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Ragel; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "rubycodegen.h"
36 #include "rubytable.h"
37 #include "rubyftable.h"
39 #include "rubyfflat.h"
43 using std::ostringstream;
56 /* Target ruby impl */
58 /* Target language and output style. */
59 extern CodeStyle codeStyle;
62 extern istream *inStream;
63 extern ostream *outStream;
64 extern output_filter *outFilter;
65 extern const char *outputFileName;
67 /* Graphviz dot file generation. */
68 extern bool graphvizDone;
70 extern int numSplitPartitions;
73 * Callbacks invoked by the XML data parser.
77 void rubyLineDirective( ostream &out, const char *fileName, int line )
79 /* Write a comment containing line info. */
80 out << "# line " << line << " \"";
81 for ( const char *pc = fileName; *pc != 0; pc++ ) {
90 void RubyCodeGen::genLineDirective( ostream &out )
92 std::streambuf *sbuf = out.rdbuf();
93 output_filter *filter = static_cast<output_filter*>(sbuf);
94 rubyLineDirective( out, filter->fileName, filter->line + 1 );
97 string RubyCodeGen::DATA_PREFIX()
100 return FSM_NAME() + "_";
104 std::ostream &RubyCodeGen::STATIC_VAR( string type, string name )
108 " attr_accessor :" << name << "\n"
115 std::ostream &RubyCodeGen::OPEN_ARRAY( string type, string name )
119 " attr_accessor :" << name << "\n"
120 " private :" << name << ", :" << name << "=\n"
122 "self." << name << " = [\n";
126 std::ostream &RubyCodeGen::CLOSE_ARRAY()
133 string RubyCodeGen::ARR_OFF( string ptr, string offset )
135 return ptr + "[" + offset + "]";
138 string RubyCodeGen::NULL_ITEM()
144 string RubyCodeGen::P()
151 INLINE_LIST( ret, pExpr, 0, false );
157 string RubyCodeGen::PE()
164 INLINE_LIST( ret, peExpr, 0, false );
170 string RubyCodeGen::EOFV()
177 INLINE_LIST( ret, eofExpr, 0, false );
183 string RubyCodeGen::CS()
187 ret << ACCESS() << "cs";
190 INLINE_LIST( ret, csExpr, 0, false );
196 string RubyCodeGen::TOP()
200 ret << ACCESS() + "top";
203 INLINE_LIST( ret, topExpr, 0, false );
209 string RubyCodeGen::STACK()
212 if ( stackExpr == 0 )
213 ret << ACCESS() + "stack";
216 INLINE_LIST( ret, stackExpr, 0, false );
222 string RubyCodeGen::ACT()
226 ret << ACCESS() + "act";
229 INLINE_LIST( ret, actExpr, 0, false );
235 string RubyCodeGen::TOKSTART()
238 if ( tokstartExpr == 0 )
239 ret << ACCESS() + "ts";
242 INLINE_LIST( ret, tokstartExpr, 0, false );
248 string RubyCodeGen::TOKEND()
251 if ( tokendExpr == 0 )
252 ret << ACCESS() + "te";
255 INLINE_LIST( ret, tokendExpr, 0, false );
261 string RubyCodeGen::DATA()
265 ret << ACCESS() + "data";
268 INLINE_LIST( ret, dataExpr, 0, false );
274 /* Write out the fsm name. */
275 string RubyCodeGen::FSM_NAME()
281 void RubyCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish )
283 /* Write the preprocessor line info for going into the source file. */
284 rubyLineDirective( ret, sourceFileName, action->loc.line );
286 /* Write the block and close it off. */
288 INLINE_LIST( ret, action->inlineList, targState, inFinish );
290 rubyLineDirective( ret, sourceFileName, action->loc.line );
295 string RubyCodeGen::GET_WIDE_KEY()
297 if ( redFsm->anyConditions() )
303 string RubyCodeGen::GET_WIDE_KEY( RedStateAp *state )
305 if ( state->stateCondList.length() > 0 )
311 string RubyCodeGen::GET_KEY()
314 if ( getKeyExpr != 0 ) {
315 /* Emit the user supplied method of retrieving the key. */
317 INLINE_LIST( ret, getKeyExpr, 0, false );
321 /* Expression for retrieving the key, use simple dereference. */
322 ret << DATA() << "[" << P() << "]";
327 string RubyCodeGen::KEY( Key key )
330 if ( keyOps->isSigned || !hostLang->explicitUnsigned )
333 ret << (unsigned long) key.getVal();
338 /* Write out level number of tabs. Makes the nested binary search nice
340 string RubyCodeGen::TABS( int level )
343 while ( level-- > 0 )
348 string RubyCodeGen::INT( int i )
355 void RubyCodeGen::CONDITION( ostream &ret, GenAction *condition )
358 rubyLineDirective( ret, sourceFileName, condition->loc.line );
359 INLINE_LIST( ret, condition->inlineList, 0, false );
362 /* Emit the alphabet data type. */
363 string RubyCodeGen::ALPH_TYPE()
365 string ret = keyOps->alphType->data1;
366 if ( keyOps->alphType->data2 != 0 ) {
368 ret += + keyOps->alphType->data2;
373 /* Emit the alphabet data type. */
374 string RubyCodeGen::WIDE_ALPH_TYPE()
377 if ( redFsm->maxKey <= keyOps->maxKey )
380 long long maxKeyVal = redFsm->maxKey.getLongLong();
381 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
382 assert( wideType != 0 );
384 ret = wideType->data1;
385 if ( wideType->data2 != 0 ) {
387 ret += wideType->data2;
394 string RubyCodeGen::ARRAY_TYPE( unsigned long maxVal )
396 long long maxValLL = (long long) maxVal;
397 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
398 assert( arrayType != 0 );
400 string ret = arrayType->data1;
401 if ( arrayType->data2 != 0 ) {
403 ret += arrayType->data2;
408 /* Write out the array of actions. */
409 std::ostream &RubyCodeGen::ACTIONS_ARRAY()
412 int totalActions = 0;
413 ARRAY_ITEM( INT(0), ++totalActions, false );
414 for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
415 /* Write out the length, which will never be the last character. */
416 ARRAY_ITEM( INT(act->key.length()), ++totalActions, false );
418 for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) {
419 ARRAY_ITEM( INT(item->value->actionId), ++totalActions, (act.last() && item.last()) );
426 void RubyCodeGen::STATE_IDS()
428 if ( redFsm->startState != 0 )
429 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
432 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
435 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
439 if ( entryPointNames.length() > 0 ) {
440 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
441 STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) <<
442 " = " << entryPointIds[en.pos()] << ";\n";
448 std::ostream &RubyCodeGen::START_ARRAY_LINE()
454 std::ostream &RubyCodeGen::ARRAY_ITEM( string item, int count, bool last )
460 if ( count % IALL == 0 )
469 std::ostream &RubyCodeGen::END_ARRAY_LINE()
475 /* Emit the offset of the start state as a decimal integer. */
476 string RubyCodeGen::START_STATE_ID()
479 ret << redFsm->startState->id;
483 string RubyCodeGen::ERROR_STATE()
486 if ( redFsm->errState != 0 )
487 ret << redFsm->errState->id;
493 string RubyCodeGen::FIRST_FINAL_STATE()
496 if ( redFsm->firstFinState != 0 )
497 ret << redFsm->firstFinState->id;
499 ret << redFsm->nextStateId;
503 string RubyCodeGen::ACCESS()
506 if ( accessExpr != 0 )
507 INLINE_LIST( ret, accessExpr, 0, false );
511 /* Write out an inline tree structure. Walks the list and possibly calls out
512 * to virtual functions than handle language specific items in the tree. */
513 void RubyCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList,
514 int targState, bool inFinish )
516 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
517 switch ( item->type ) {
518 case GenInlineItem::Text:
521 case GenInlineItem::Goto:
522 GOTO( ret, item->targState->id, inFinish );
524 case GenInlineItem::Call:
525 CALL( ret, item->targState->id, targState, inFinish );
527 case GenInlineItem::Next:
528 NEXT( ret, item->targState->id, inFinish );
530 case GenInlineItem::Ret:
531 RET( ret, inFinish );
533 case GenInlineItem::PChar:
536 case GenInlineItem::Char:
539 case GenInlineItem::Hold:
540 ret << P() << " = " << P() << " - 1;";
542 case GenInlineItem::Exec:
543 EXEC( ret, item, targState, inFinish );
545 case GenInlineItem::Curs:
548 case GenInlineItem::Targs:
549 ret << "(" << CS() << ")";
551 case GenInlineItem::Entry:
552 ret << item->targState->id;
554 case GenInlineItem::GotoExpr:
555 GOTO_EXPR( ret, item, inFinish );
557 case GenInlineItem::CallExpr:
558 CALL_EXPR( ret, item, targState, inFinish );
560 case GenInlineItem::NextExpr:
561 NEXT_EXPR( ret, item, inFinish );
563 case GenInlineItem::LmSwitch:
564 LM_SWITCH( ret, item, targState, inFinish );
566 case GenInlineItem::LmSetActId:
567 SET_ACT( ret, item );
569 case GenInlineItem::LmSetTokEnd:
570 SET_TOKEND( ret, item );
572 case GenInlineItem::LmGetTokEnd:
573 GET_TOKEND( ret, item );
575 case GenInlineItem::LmInitTokStart:
576 INIT_TOKSTART( ret, item );
578 case GenInlineItem::LmInitAct:
579 INIT_ACT( ret, item );
581 case GenInlineItem::LmSetTokStart:
582 SET_TOKSTART( ret, item );
584 case GenInlineItem::SubAction:
585 SUB_ACTION( ret, item, targState, inFinish );
587 case GenInlineItem::Break:
588 BREAK( ret, targState );
595 void RubyCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
597 /* The parser gives fexec two children. The double brackets are for D
598 * code. If the inline list is a single word it will get interpreted as a
599 * C-style cast by the D compiler. */
600 ret << " begin " << P() << " = ((";
601 INLINE_LIST( ret, item->children, targState, inFinish );
602 ret << "))-1; end\n";
605 void RubyCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item,
606 int targState, int inFinish )
609 " case " << ACT() << "\n";
611 for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
612 /* Write the case label, the action and the case break. */
616 ret << " when " << lma->lmId << " then\n";
619 /* Write the block and close it off. */
621 INLINE_LIST( ret, lma->children, targState, inFinish );
628 void RubyCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
630 ret << ACT() << " = " << item->lmId << ";";
633 void RubyCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
635 ret << TOKSTART() << " = " << NULL_ITEM() << ";";
638 void RubyCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
640 ret << ACT() << " = 0\n";
643 void RubyCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
645 ret << TOKSTART() << " = " << P() << "\n";
648 void RubyCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
650 /* The tokend action sets tokend. */
651 ret << TOKEND() << " = " << P();
652 if ( item->offset != 0 )
653 out << "+" << item->offset;
657 void RubyCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
662 void RubyCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item,
663 int targState, bool inFinish )
665 if ( item->children->length() > 0 ) {
666 /* Write the block and close it off. */
668 INLINE_LIST( ret, item->children, targState, inFinish );
673 int RubyCodeGen::TRANS_ACTION( RedTransAp *trans )
675 /* If there are actions, emit them. Otherwise emit zero. */
677 if ( trans->action != 0 )
678 act = trans->action->location+1;
682 ostream &RubyCodeGen::source_warning( const InputLoc &loc )
684 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
688 ostream &RubyCodeGen::source_error( const InputLoc &loc )
691 assert( sourceFileName != 0 );
692 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
696 void RubyCodeGen::finishRagelDef()
699 if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
700 codeStyle == GenIpGoto || codeStyle == GenSplit )
702 /* For directly executable machines there is no required state
703 * ordering. Choose a depth-first ordering to increase the
704 * potential for fall-throughs. */
705 redFsm->depthFirstOrdering();
708 /* The frontend will do this for us, but it may be a good idea to
709 * force it if the intermediate file is edited. */
710 redFsm->sortByStateId();
713 /* Choose default transitions and the single transition. */
714 redFsm->chooseDefaultSpan();
716 /* Maybe do flat expand, otherwise choose single. */
717 if ( codeStyle == GenFlat || codeStyle == GenFFlat )
720 redFsm->chooseSingle();
722 /* If any errors have occured in the input file then don't write anything. */
723 if ( gblErrorCount > 0 )
726 if ( codeStyle == GenSplit )
727 redFsm->partitionFsm( numSplitPartitions );
729 if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
730 redFsm->setInTrans();
732 /* Anlayze Machine will find the final action reference counts, among
733 * other things. We will use these in reporting the usage
734 * of fsm directives in action code. */
737 /* Determine if we should use indicies. */
742 /* Determine if we should use indicies or not. */
743 void RubyCodeGen::calcIndexSize()
745 int sizeWithInds = 0, sizeWithoutInds = 0;
747 /* Calculate cost of using with indicies. */
748 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
749 int totalIndex = st->outSingle.length() + st->outRange.length() +
750 (st->defTrans == 0 ? 0 : 1);
751 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
753 sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
754 if ( redFsm->anyActions() )
755 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
757 /* Calculate the cost of not using indicies. */
758 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
759 int totalIndex = st->outSingle.length() + st->outRange.length() +
760 (st->defTrans == 0 ? 0 : 1);
761 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
762 if ( redFsm->anyActions() )
763 sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
766 /* If using indicies reduces the size, use them. */
767 useIndicies = sizeWithInds < sizeWithoutInds;
770 unsigned int RubyCodeGen::arrayTypeSize( unsigned long maxVal )
772 long long maxValLL = (long long) maxVal;
773 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
774 assert( arrayType != 0 );
775 return arrayType->size;
779 void RubyCodeGen::writeInit()
783 out << " " << P() << " ||= 0\n";
786 out << " " << PE() << " ||= " << DATA() << ".length\n";
789 out << " " << CS() << " = " << START() << "\n";
791 /* If there are any calls, then the stack top needs initialization. */
792 if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
793 out << " " << TOP() << " = 0\n";
795 if ( hasLongestMatch ) {
797 " " << TOKSTART() << " = " << NULL_ITEM() << "\n"
798 " " << TOKEND() << " = " << NULL_ITEM() << "\n"
799 " " << ACT() << " = 0\n";
805 void RubyCodeGen::writeExports()
807 if ( exportList.length() > 0 ) {
808 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
809 STATIC_VAR( ALPH_TYPE(), DATA_PREFIX() + "ex_" + ex->name )
810 << " = " << KEY(ex->key) << "\n";
816 void RubyCodeGen::writeStart()
818 out << START_STATE_ID();
821 void RubyCodeGen::writeFirstFinal()
823 out << FIRST_FINAL_STATE();
826 void RubyCodeGen::writeError()
828 out << ERROR_STATE();
835 * indent-tabs-mode: 1
836 * c-file-style: "bsd"