Fixed a problem reading hex numbers that have the high bit set when the
[external/ragel.git] / ragel / cdcodegen.cpp
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
3  *            2004 Erich Ocean <eric.ocean@ampede.com>
4  *            2005 Alan West <alan@alanz.com>
5  */
6
7 /*  This file is part of Ragel.
8  *
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.
13  * 
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.
18  * 
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 
22  */
23
24 #include "cdcodegen.h"
25 #include "ragel.h"
26 #include "redfsm.h"
27 #include "gendata.h"
28 #include <sstream>
29 #include <string>
30 #include <assert.h>
31
32 /* Code generators. */
33 #include "cdtable.h"
34 #include "cdftable.h"
35 #include "cdflat.h"
36 #include "cdfflat.h"
37 #include "cdgoto.h"
38 #include "cdfgoto.h"
39 #include "cdipgoto.h"
40 #include "cdsplit.h"
41
42 using std::ostream;
43 using std::ostringstream;
44 using std::string;
45 using std::cerr;
46 using std::endl;
47 using std::istream;
48 using std::ifstream;
49 using std::ostream;
50 using std::ios;
51 using std::cin;
52 using std::cout;
53 using std::cerr;
54 using std::endl;
55
56 /* Target language and output style. */
57 extern CodeStyleEnum codeStyle;
58
59 /* Io globals. */
60 extern istream *inStream;
61 extern ostream *outStream;
62 extern output_filter *outFilter;
63 extern const char *outputFileName;
64
65 /* Graphviz dot file generation. */
66 extern bool graphvizDone;
67
68 extern int numSplitPartitions;
69 extern bool noLineDirectives;
70
71 /* Invoked by the parser when the root element is opened. */
72 ostream *cdOpenOutput( const char *inputFile )
73 {
74         if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
75                 error() << "this code generator is for C and D only" << endl;
76                 exit(1);
77         }
78
79         /* If the output format is code and no output file name is given, then
80          * make a default. */
81         if ( outputFileName == 0 ) {
82                 const char *ext = findFileExtension( inputFile );
83                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
84                         outputFileName = fileNameFromStem( inputFile, ".h" );
85                 else {
86                         const char *defExtension = 0;
87                         switch ( hostLang->lang ) {
88                                 case HostLang::C: defExtension = ".c"; break;
89                                 case HostLang::D: defExtension = ".d"; break;
90                                 default: break;
91                         }
92                         outputFileName = fileNameFromStem( inputFile, defExtension );
93                 }
94         }
95
96         /* Make sure we are not writing to the same file as the input file. */
97         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
98                 error() << "output file \"" << outputFileName  << 
99                                 "\" is the same as the input file" << endl;
100         }
101
102         if ( outputFileName != 0 ) {
103                 /* Create the filter on the output and open it. */
104                 outFilter = new output_filter( outputFileName );
105                 outFilter->open( outputFileName, ios::out|ios::trunc );
106                 if ( !outFilter->is_open() ) {
107                         error() << "error opening " << outputFileName << " for writing" << endl;
108                         exit(1);
109                 }
110
111                 /* Open the output stream, attaching it to the filter. */
112                 outStream = new ostream( outFilter );
113         }
114         else {
115                 /* Writing out ot std out. */
116                 outStream = &cout;
117         }
118         return outStream;
119 }
120
121 /* Invoked by the parser when a ragel definition is opened. */
122 CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName, 
123                 ostream &out, bool wantComplete )
124 {
125         CodeGenData *codeGen = 0;
126         switch ( hostLang->lang ) {
127         case HostLang::C:
128                 switch ( codeStyle ) {
129                 case GenTables:
130                         codeGen = new CTabCodeGen(out);
131                         break;
132                 case GenFTables:
133                         codeGen = new CFTabCodeGen(out);
134                         break;
135                 case GenFlat:
136                         codeGen = new CFlatCodeGen(out);
137                         break;
138                 case GenFFlat:
139                         codeGen = new CFFlatCodeGen(out);
140                         break;
141                 case GenGoto:
142                         codeGen = new CGotoCodeGen(out);
143                         break;
144                 case GenFGoto:
145                         codeGen = new CFGotoCodeGen(out);
146                         break;
147                 case GenIpGoto:
148                         codeGen = new CIpGotoCodeGen(out);
149                         break;
150                 case GenSplit:
151                         codeGen = new CSplitCodeGen(out);
152                         break;
153                 }
154                 break;
155
156         case HostLang::D:
157                 switch ( codeStyle ) {
158                 case GenTables:
159                         codeGen = new DTabCodeGen(out);
160                         break;
161                 case GenFTables:
162                         codeGen = new DFTabCodeGen(out);
163                         break;
164                 case GenFlat:
165                         codeGen = new DFlatCodeGen(out);
166                         break;
167                 case GenFFlat:
168                         codeGen = new DFFlatCodeGen(out);
169                         break;
170                 case GenGoto:
171                         codeGen = new DGotoCodeGen(out);
172                         break;
173                 case GenFGoto:
174                         codeGen = new DFGotoCodeGen(out);
175                         break;
176                 case GenIpGoto:
177                         codeGen = new DIpGotoCodeGen(out);
178                         break;
179                 case GenSplit:
180                         codeGen = new DSplitCodeGen(out);
181                         break;
182                 }
183                 break;
184
185         default: break;
186         }
187
188         codeGen->sourceFileName = sourceFileName;
189         codeGen->fsmName = fsmName;
190         codeGen->wantComplete = wantComplete;
191
192         return codeGen;
193 }
194
195
196 void cdLineDirective( ostream &out, const char *fileName, int line )
197 {
198         if ( noLineDirectives )
199                 out << "/* ";
200
201         /* Write the preprocessor line info for to the input file. */
202         out << "#line " << line  << " \"";
203         for ( const char *pc = fileName; *pc != 0; pc++ ) {
204                 if ( *pc == '\\' )
205                         out << "\\\\";
206                 else
207                         out << *pc;
208         }
209         out << '"';
210
211         if ( noLineDirectives )
212                 out << " */";
213
214         out << '\n';
215 }
216
217 void FsmCodeGen::genLineDirective( ostream &out )
218 {
219         std::streambuf *sbuf = out.rdbuf();
220         output_filter *filter = static_cast<output_filter*>(sbuf);
221         cdLineDirective( out, filter->fileName, filter->line + 1 );
222 }
223
224
225 /* Init code gen with in parameters. */
226 FsmCodeGen::FsmCodeGen( ostream &out )
227 :
228         CodeGenData(out)
229 {
230 }
231
232 unsigned int FsmCodeGen::arrayTypeSize( unsigned long maxVal )
233 {
234         long long maxValLL = (long long) maxVal;
235         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
236         assert( arrayType != 0 );
237         return arrayType->size;
238 }
239
240 string FsmCodeGen::ARRAY_TYPE( unsigned long maxVal )
241 {
242         long long maxValLL = (long long) maxVal;
243         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
244         assert( arrayType != 0 );
245
246         string ret = arrayType->data1;
247         if ( arrayType->data2 != 0 ) {
248                 ret += " ";
249                 ret += arrayType->data2;
250         }
251         return ret;
252 }
253
254
255 /* Write out the fsm name. */
256 string FsmCodeGen::FSM_NAME()
257 {
258         return fsmName;
259 }
260
261 /* Emit the offset of the start state as a decimal integer. */
262 string FsmCodeGen::START_STATE_ID()
263 {
264         ostringstream ret;
265         ret << redFsm->startState->id;
266         return ret.str();
267 };
268
269 /* Write out the array of actions. */
270 std::ostream &FsmCodeGen::ACTIONS_ARRAY()
271 {
272         out << "\t0, ";
273         int totalActions = 1;
274         for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
275                 /* Write out the length, which will never be the last character. */
276                 out << act->key.length() << ", ";
277                 /* Put in a line break every 8 */
278                 if ( totalActions++ % 8 == 7 )
279                         out << "\n\t";
280
281                 for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) {
282                         out << item->value->actionId;
283                         if ( ! (act.last() && item.last()) )
284                                 out << ", ";
285
286                         /* Put in a line break every 8 */
287                         if ( totalActions++ % 8 == 7 )
288                                 out << "\n\t";
289                 }
290         }
291         out << "\n";
292         return out;
293 }
294
295
296 string FsmCodeGen::ACCESS()
297 {
298         ostringstream ret;
299         if ( accessExpr != 0 )
300                 INLINE_LIST( ret, accessExpr, 0, false, false );
301         return ret.str();
302 }
303
304
305 string FsmCodeGen::P()
306
307         ostringstream ret;
308         if ( pExpr == 0 )
309                 ret << "p";
310         else {
311                 ret << "(";
312                 INLINE_LIST( ret, pExpr, 0, false, false );
313                 ret << ")";
314         }
315         return ret.str();
316 }
317
318 string FsmCodeGen::PE()
319 {
320         ostringstream ret;
321         if ( peExpr == 0 )
322                 ret << "pe";
323         else {
324                 ret << "(";
325                 INLINE_LIST( ret, peExpr, 0, false, false );
326                 ret << ")";
327         }
328         return ret.str();
329 }
330
331 string FsmCodeGen::EOFV()
332 {
333         ostringstream ret;
334         if ( eofExpr == 0 )
335                 ret << "eof";
336         else {
337                 ret << "(";
338                 INLINE_LIST( ret, eofExpr, 0, false, false );
339                 ret << ")";
340         }
341         return ret.str();
342 }
343
344 string FsmCodeGen::CS()
345 {
346         ostringstream ret;
347         if ( csExpr == 0 )
348                 ret << ACCESS() << "cs";
349         else {
350                 /* Emit the user supplied method of retrieving the key. */
351                 ret << "(";
352                 INLINE_LIST( ret, csExpr, 0, false, false );
353                 ret << ")";
354         }
355         return ret.str();
356 }
357
358 string FsmCodeGen::TOP()
359 {
360         ostringstream ret;
361         if ( topExpr == 0 )
362                 ret << ACCESS() + "top";
363         else {
364                 ret << "(";
365                 INLINE_LIST( ret, topExpr, 0, false, false );
366                 ret << ")";
367         }
368         return ret.str();
369 }
370
371 string FsmCodeGen::STACK()
372 {
373         ostringstream ret;
374         if ( stackExpr == 0 )
375                 ret << ACCESS() + "stack";
376         else {
377                 ret << "(";
378                 INLINE_LIST( ret, stackExpr, 0, false, false );
379                 ret << ")";
380         }
381         return ret.str();
382 }
383
384 string FsmCodeGen::ACT()
385 {
386         ostringstream ret;
387         if ( actExpr == 0 )
388                 ret << ACCESS() + "act";
389         else {
390                 ret << "(";
391                 INLINE_LIST( ret, actExpr, 0, false, false );
392                 ret << ")";
393         }
394         return ret.str();
395 }
396
397 string FsmCodeGen::TOKSTART()
398 {
399         ostringstream ret;
400         if ( tokstartExpr == 0 )
401                 ret << ACCESS() + "ts";
402         else {
403                 ret << "(";
404                 INLINE_LIST( ret, tokstartExpr, 0, false, false );
405                 ret << ")";
406         }
407         return ret.str();
408 }
409
410 string FsmCodeGen::TOKEND()
411 {
412         ostringstream ret;
413         if ( tokendExpr == 0 )
414                 ret << ACCESS() + "te";
415         else {
416                 ret << "(";
417                 INLINE_LIST( ret, tokendExpr, 0, false, false );
418                 ret << ")";
419         }
420         return ret.str();
421 }
422
423 string FsmCodeGen::GET_WIDE_KEY()
424 {
425         if ( redFsm->anyConditions() ) 
426                 return "_widec";
427         else
428                 return GET_KEY();
429 }
430
431 string FsmCodeGen::GET_WIDE_KEY( RedStateAp *state )
432 {
433         if ( state->stateCondList.length() > 0 )
434                 return "_widec";
435         else
436                 return GET_KEY();
437 }
438
439 string FsmCodeGen::GET_KEY()
440 {
441         ostringstream ret;
442         if ( getKeyExpr != 0 ) { 
443                 /* Emit the user supplied method of retrieving the key. */
444                 ret << "(";
445                 INLINE_LIST( ret, getKeyExpr, 0, false, false );
446                 ret << ")";
447         }
448         else {
449                 /* Expression for retrieving the key, use simple dereference. */
450                 ret << "(*" << P() << ")";
451         }
452         return ret.str();
453 }
454
455 /* Write out level number of tabs. Makes the nested binary search nice
456  * looking. */
457 string FsmCodeGen::TABS( int level )
458 {
459         string result;
460         while ( level-- > 0 )
461                 result += "\t";
462         return result;
463 }
464
465 /* Write out a key from the fsm code gen. Depends on wether or not the key is
466  * signed. */
467 string FsmCodeGen::KEY( Key key )
468 {
469         ostringstream ret;
470         if ( keyOps->isSigned || !hostLang->explicitUnsigned )
471                 ret << key.getVal();
472         else
473                 ret << (unsigned long) key.getVal() << 'u';
474         return ret.str();
475 }
476
477 void FsmCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
478 {
479         /* The parser gives fexec two children. The double brackets are for D
480          * code. If the inline list is a single word it will get interpreted as a
481          * C-style cast by the D compiler. */
482         ret << "{" << P() << " = ((";
483         INLINE_LIST( ret, item->children, targState, inFinish, false );
484         ret << "))-1;}";
485 }
486
487 void FsmCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item, 
488                 int targState, int inFinish, bool csForced )
489 {
490         ret << 
491                 "       switch( " << ACT() << " ) {\n";
492
493         bool haveDefault = false;
494         for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
495                 /* Write the case label, the action and the case break. */
496                 if ( lma->lmId < 0 ) {
497                         ret << "        default:\n";
498                         haveDefault = true;
499                 }
500                 else
501                         ret << "        case " << lma->lmId << ":\n";
502
503                 /* Write the block and close it off. */
504                 ret << "        {";
505                 INLINE_LIST( ret, lma->children, targState, inFinish, csForced );
506                 ret << "}\n";
507
508                 ret << "        break;\n";
509         }
510
511         if ( hostLang->lang == HostLang::D && !haveDefault )
512                 ret << "        default: break;";
513
514         ret << 
515                 "       }\n"
516                 "\t";
517 }
518
519 void FsmCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
520 {
521         ret << ACT() << " = " << item->lmId << ";";
522 }
523
524 void FsmCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
525 {
526         /* The tokend action sets tokend. */
527         ret << TOKEND() << " = " << P();
528         if ( item->offset != 0 ) 
529                 out << "+" << item->offset;
530         out << ";";
531 }
532
533 void FsmCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
534 {
535         ret << TOKEND();
536 }
537
538 void FsmCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
539 {
540         ret << TOKSTART() << " = " << NULL_ITEM() << ";";
541 }
542
543 void FsmCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
544 {
545         ret << ACT() << " = 0;";
546 }
547
548 void FsmCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
549 {
550         ret << TOKSTART() << " = " << P() << ";";
551 }
552
553 void FsmCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item, 
554                 int targState, bool inFinish, bool csForced )
555 {
556         if ( item->children->length() > 0 ) {
557                 /* Write the block and close it off. */
558                 ret << "{";
559                 INLINE_LIST( ret, item->children, targState, inFinish, csForced );
560                 ret << "}";
561         }
562 }
563
564
565 /* Write out an inline tree structure. Walks the list and possibly calls out
566  * to virtual functions than handle language specific items in the tree. */
567 void FsmCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList, 
568                 int targState, bool inFinish, bool csForced )
569 {
570         for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
571                 switch ( item->type ) {
572                 case GenInlineItem::Text:
573                         ret << item->data;
574                         break;
575                 case GenInlineItem::Goto:
576                         GOTO( ret, item->targState->id, inFinish );
577                         break;
578                 case GenInlineItem::Call:
579                         CALL( ret, item->targState->id, targState, inFinish );
580                         break;
581                 case GenInlineItem::Next:
582                         NEXT( ret, item->targState->id, inFinish );
583                         break;
584                 case GenInlineItem::Ret:
585                         RET( ret, inFinish );
586                         break;
587                 case GenInlineItem::PChar:
588                         ret << P();
589                         break;
590                 case GenInlineItem::Char:
591                         ret << GET_KEY();
592                         break;
593                 case GenInlineItem::Hold:
594                         ret << P() << "--;";
595                         break;
596                 case GenInlineItem::Exec:
597                         EXEC( ret, item, targState, inFinish );
598                         break;
599                 case GenInlineItem::Curs:
600                         CURS( ret, inFinish );
601                         break;
602                 case GenInlineItem::Targs:
603                         TARGS( ret, inFinish, targState );
604                         break;
605                 case GenInlineItem::Entry:
606                         ret << item->targState->id;
607                         break;
608                 case GenInlineItem::GotoExpr:
609                         GOTO_EXPR( ret, item, inFinish );
610                         break;
611                 case GenInlineItem::CallExpr:
612                         CALL_EXPR( ret, item, targState, inFinish );
613                         break;
614                 case GenInlineItem::NextExpr:
615                         NEXT_EXPR( ret, item, inFinish );
616                         break;
617                 case GenInlineItem::LmSwitch:
618                         LM_SWITCH( ret, item, targState, inFinish, csForced );
619                         break;
620                 case GenInlineItem::LmSetActId:
621                         SET_ACT( ret, item );
622                         break;
623                 case GenInlineItem::LmSetTokEnd:
624                         SET_TOKEND( ret, item );
625                         break;
626                 case GenInlineItem::LmGetTokEnd:
627                         GET_TOKEND( ret, item );
628                         break;
629                 case GenInlineItem::LmInitTokStart:
630                         INIT_TOKSTART( ret, item );
631                         break;
632                 case GenInlineItem::LmInitAct:
633                         INIT_ACT( ret, item );
634                         break;
635                 case GenInlineItem::LmSetTokStart:
636                         SET_TOKSTART( ret, item );
637                         break;
638                 case GenInlineItem::SubAction:
639                         SUB_ACTION( ret, item, targState, inFinish, csForced );
640                         break;
641                 case GenInlineItem::Break:
642                         BREAK( ret, targState, csForced );
643                         break;
644                 }
645         }
646 }
647 /* Write out paths in line directives. Escapes any special characters. */
648 string FsmCodeGen::LDIR_PATH( char *path )
649 {
650         ostringstream ret;
651         for ( char *pc = path; *pc != 0; pc++ ) {
652                 if ( *pc == '\\' )
653                         ret << "\\\\";
654                 else
655                         ret << *pc;
656         }
657         return ret.str();
658 }
659
660 void FsmCodeGen::ACTION( ostream &ret, GenAction *action, int targState, 
661                 bool inFinish, bool csForced )
662 {
663         /* Write the preprocessor line info for going into the source file. */
664         cdLineDirective( ret, sourceFileName, action->loc.line );
665
666         /* Write the block and close it off. */
667         ret << "\t{";
668         INLINE_LIST( ret, action->inlineList, targState, inFinish, csForced );
669         ret << "}\n";
670 }
671
672 void FsmCodeGen::CONDITION( ostream &ret, GenAction *condition )
673 {
674         ret << "\n";
675         cdLineDirective( ret, sourceFileName, condition->loc.line );
676         INLINE_LIST( ret, condition->inlineList, 0, false, false );
677 }
678
679 string FsmCodeGen::ERROR_STATE()
680 {
681         ostringstream ret;
682         if ( redFsm->errState != 0 )
683                 ret << redFsm->errState->id;
684         else
685                 ret << "-1";
686         return ret.str();
687 }
688
689 string FsmCodeGen::FIRST_FINAL_STATE()
690 {
691         ostringstream ret;
692         if ( redFsm->firstFinState != 0 )
693                 ret << redFsm->firstFinState->id;
694         else
695                 ret << redFsm->nextStateId;
696         return ret.str();
697 }
698
699 void FsmCodeGen::writeInit()
700 {
701         out << "        {\n";
702
703         if ( !noCS )
704                 out << "\t" << CS() << " = " << START() << ";\n";
705         
706         /* If there are any calls, then the stack top needs initialization. */
707         if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
708                 out << "\t" << TOP() << " = 0;\n";
709
710         if ( hasLongestMatch ) {
711                 out << 
712                         "       " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
713                         "       " << TOKEND() << " = " << NULL_ITEM() << ";\n"
714                         "       " << ACT() << " = 0;\n";
715         }
716         out << "        }\n";
717 }
718
719 string FsmCodeGen::DATA_PREFIX()
720 {
721         if ( !noPrefix )
722                 return FSM_NAME() + "_";
723         return "";
724 }
725
726 /* Emit the alphabet data type. */
727 string FsmCodeGen::ALPH_TYPE()
728 {
729         string ret = keyOps->alphType->data1;
730         if ( keyOps->alphType->data2 != 0 ) {
731                 ret += " ";
732                 ret += + keyOps->alphType->data2;
733         }
734         return ret;
735 }
736
737 /* Emit the alphabet data type. */
738 string FsmCodeGen::WIDE_ALPH_TYPE()
739 {
740         string ret;
741         if ( redFsm->maxKey <= keyOps->maxKey )
742                 ret = ALPH_TYPE();
743         else {
744                 long long maxKeyVal = redFsm->maxKey.getLongLong();
745                 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
746                 assert( wideType != 0 );
747
748                 ret = wideType->data1;
749                 if ( wideType->data2 != 0 ) {
750                         ret += " ";
751                         ret += wideType->data2;
752                 }
753         }
754         return ret;
755 }
756
757 void FsmCodeGen::STATE_IDS()
758 {
759         if ( redFsm->startState != 0 )
760                 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
761
762         if ( !noFinal )
763                 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
764
765         if ( !noError )
766                 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
767
768         out << "\n";
769
770         if ( entryPointNames.length() > 0 ) {
771                 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
772                         STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) << 
773                                         " = " << entryPointIds[en.pos()] << ";\n";
774                 }
775                 out << "\n";
776         }
777 }
778
779 void FsmCodeGen::writeStart()
780 {
781         out << START_STATE_ID();
782 }
783
784 void FsmCodeGen::writeFirstFinal()
785 {
786         out << FIRST_FINAL_STATE();
787 }
788
789 void FsmCodeGen::writeError()
790 {
791         out << ERROR_STATE();
792 }
793
794 /*
795  * Language specific, but style independent code generators functions.
796  */
797
798 string CCodeGen::PTR_CONST()
799 {
800         return "const ";
801 }
802
803 std::ostream &CCodeGen::OPEN_ARRAY( string type, string name )
804 {
805         out << "static const " << type << " " << name << "[] = {\n";
806         return out;
807 }
808
809 std::ostream &CCodeGen::CLOSE_ARRAY()
810 {
811         return out << "};\n";
812 }
813
814 std::ostream &CCodeGen::STATIC_VAR( string type, string name )
815 {
816         out << "static const " << type << " " << name;
817         return out;
818 }
819
820 string CCodeGen::UINT( )
821 {
822         return "unsigned int";
823 }
824
825 string CCodeGen::ARR_OFF( string ptr, string offset )
826 {
827         return ptr + " + " + offset;
828 }
829
830 string CCodeGen::CAST( string type )
831 {
832         return "(" + type + ")";
833 }
834
835 string CCodeGen::NULL_ITEM()
836 {
837         return "0";
838 }
839
840 string CCodeGen::POINTER()
841 {
842         return " *";
843 }
844
845 std::ostream &CCodeGen::SWITCH_DEFAULT()
846 {
847         return out;
848 }
849
850 string CCodeGen::CTRL_FLOW()
851 {
852         return "";
853 }
854
855 void CCodeGen::writeExports()
856 {
857         if ( exportList.length() > 0 ) {
858                 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
859                         out << "#define " << DATA_PREFIX() << "ex_" << ex->name << " " << 
860                                         KEY(ex->key) << "\n";
861                 }
862                 out << "\n";
863         }
864 }
865
866 /*
867  * D Specific
868  */
869
870 string DCodeGen::NULL_ITEM()
871 {
872         return "null";
873 }
874
875 string DCodeGen::POINTER()
876 {
877         // multiple items seperated by commas can also be pointer types.
878         return "* ";
879 }
880
881 string DCodeGen::PTR_CONST()
882 {
883         return "";
884 }
885
886 std::ostream &DCodeGen::OPEN_ARRAY( string type, string name )
887 {
888         out << "static const " << type << "[] " << name << " = [\n";
889         return out;
890 }
891
892 std::ostream &DCodeGen::CLOSE_ARRAY()
893 {
894         return out << "];\n";
895 }
896
897 std::ostream &DCodeGen::STATIC_VAR( string type, string name )
898 {
899         out << "static const " << type << " " << name;
900         return out;
901 }
902
903 string DCodeGen::ARR_OFF( string ptr, string offset )
904 {
905         return "&" + ptr + "[" + offset + "]";
906 }
907
908 string DCodeGen::CAST( string type )
909 {
910         return "cast(" + type + ")";
911 }
912
913 string DCodeGen::UINT( )
914 {
915         return "uint";
916 }
917
918 std::ostream &DCodeGen::SWITCH_DEFAULT()
919 {
920         out << "                default: break;\n";
921         return out;
922 }
923
924 string DCodeGen::CTRL_FLOW()
925 {
926         return "if (true) ";
927 }
928
929 void DCodeGen::writeExports()
930 {
931         if ( exportList.length() > 0 ) {
932                 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
933                         out << "static const " << ALPH_TYPE() << " " << DATA_PREFIX() << 
934                                         "ex_" << ex->name << " = " << KEY(ex->key) << ";\n";
935                 }
936                 out << "\n";
937         }
938 }
939
940 /*
941  * End D-specific code.
942  */
943
944 void FsmCodeGen::finishRagelDef()
945 {
946         if ( codeStyle == GenGoto || codeStyle == GenFGoto || 
947                         codeStyle == GenIpGoto || codeStyle == GenSplit )
948         {
949                 /* For directly executable machines there is no required state
950                  * ordering. Choose a depth-first ordering to increase the
951                  * potential for fall-throughs. */
952                 redFsm->depthFirstOrdering();
953         }
954         else {
955                 /* The frontend will do this for us, but it may be a good idea to
956                  * force it if the intermediate file is edited. */
957                 redFsm->sortByStateId();
958         }
959
960         /* Choose default transitions and the single transition. */
961         redFsm->chooseDefaultSpan();
962                 
963         /* Maybe do flat expand, otherwise choose single. */
964         if ( codeStyle == GenFlat || codeStyle == GenFFlat )
965                 redFsm->makeFlat();
966         else
967                 redFsm->chooseSingle();
968
969         /* If any errors have occured in the input file then don't write anything. */
970         if ( gblErrorCount > 0 )
971                 return;
972         
973         if ( codeStyle == GenSplit )
974                 redFsm->partitionFsm( numSplitPartitions );
975
976         if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
977                 redFsm->setInTrans();
978
979         /* Anlayze Machine will find the final action reference counts, among
980          * other things. We will use these in reporting the usage
981          * of fsm directives in action code. */
982         analyzeMachine();
983
984         /* Determine if we should use indicies. */
985         calcIndexSize();
986 }
987
988 ostream &FsmCodeGen::source_warning( const InputLoc &loc )
989 {
990         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
991         return cerr;
992 }
993
994 ostream &FsmCodeGen::source_error( const InputLoc &loc )
995 {
996         gblErrorCount += 1;
997         assert( sourceFileName != 0 );
998         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
999         return cerr;
1000 }
1001