Parse errors in the intermediate XML file now cause the backend to exit
[external/ragel.git] / rlgen-cd / tabcodegen.cpp
1 /*
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>
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 "rlgen-cd.h"
25 #include "tabcodegen.h"
26 #include "redfsm.h"
27 #include "gendata.h"
28
29 /* Determine if we should use indicies or not. */
30 void TabCodeGen::calcIndexSize()
31 {
32         int sizeWithInds = 0, sizeWithoutInds = 0;
33
34         /* Calculate cost of using with indicies. */
35         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
36                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
37                                 (st->defTrans == 0 ? 0 : 1);
38                 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
39         }
40         sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
41         if ( redFsm->anyActions() )
42                 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
43
44         /* Calculate the cost of not using indicies. */
45         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
46                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
47                                 (st->defTrans == 0 ? 0 : 1);
48                 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
49                 if ( redFsm->anyActions() )
50                         sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
51         }
52
53         /* If using indicies reduces the size, use them. */
54         useIndicies = sizeWithInds < sizeWithoutInds;
55 }
56
57 std::ostream &TabCodeGen::TO_STATE_ACTION( RedStateAp *state )
58 {
59         int act = 0;
60         if ( state->toStateAction != 0 )
61                 act = state->toStateAction->location+1;
62         out << act;
63         return out;
64 }
65
66 std::ostream &TabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
67 {
68         int act = 0;
69         if ( state->fromStateAction != 0 )
70                 act = state->fromStateAction->location+1;
71         out << act;
72         return out;
73 }
74
75 std::ostream &TabCodeGen::EOF_ACTION( RedStateAp *state )
76 {
77         int act = 0;
78         if ( state->eofAction != 0 )
79                 act = state->eofAction->location+1;
80         out << act;
81         return out;
82 }
83
84
85 std::ostream &TabCodeGen::TRANS_ACTION( RedTransAp *trans )
86 {
87         /* If there are actions, emit them. Otherwise emit zero. */
88         int act = 0;
89         if ( trans->action != 0 )
90                 act = trans->action->location+1;
91         out << act;
92         return out;
93 }
94
95 std::ostream &TabCodeGen::TO_STATE_ACTION_SWITCH()
96 {
97         /* Walk the list of functions, printing the cases. */
98         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
99                 /* Write out referenced actions. */
100                 if ( act->numToStateRefs > 0 ) {
101                         /* Write the case label, the action and the case break. */
102                         out << "\tcase " << act->actionId << ":\n";
103                         ACTION( out, act, 0, false );
104                         out << "\tbreak;\n";
105                 }
106         }
107
108         genLineDirective( out );
109         return out;
110 }
111
112 std::ostream &TabCodeGen::FROM_STATE_ACTION_SWITCH()
113 {
114         /* Walk the list of functions, printing the cases. */
115         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
116                 /* Write out referenced actions. */
117                 if ( act->numFromStateRefs > 0 ) {
118                         /* Write the case label, the action and the case break. */
119                         out << "\tcase " << act->actionId << ":\n";
120                         ACTION( out, act, 0, false );
121                         out << "\tbreak;\n";
122                 }
123         }
124
125         genLineDirective( out );
126         return out;
127 }
128
129 std::ostream &TabCodeGen::EOF_ACTION_SWITCH()
130 {
131         /* Walk the list of functions, printing the cases. */
132         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
133                 /* Write out referenced actions. */
134                 if ( act->numEofRefs > 0 ) {
135                         /* Write the case label, the action and the case break. */
136                         out << "\tcase " << act->actionId << ":\n";
137                         ACTION( out, act, 0, true );
138                         out << "\tbreak;\n";
139                 }
140         }
141
142         genLineDirective( out );
143         return out;
144 }
145
146
147 std::ostream &TabCodeGen::ACTION_SWITCH()
148 {
149         /* Walk the list of functions, printing the cases. */
150         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
151                 /* Write out referenced actions. */
152                 if ( act->numTransRefs > 0 ) {
153                         /* Write the case label, the action and the case break. */
154                         out << "\tcase " << act->actionId << ":\n";
155                         ACTION( out, act, 0, false );
156                         out << "\tbreak;\n";
157                 }
158         }
159
160         genLineDirective( out );
161         return out;
162 }
163
164 std::ostream &TabCodeGen::COND_OFFSETS()
165 {
166         out << "\t";
167         int totalStateNum = 0, curKeyOffset = 0;
168         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
169                 /* Write the key offset. */
170                 out << curKeyOffset;
171                 if ( !st.last() ) {
172                         out << ", ";
173                         if ( ++totalStateNum % IALL == 0 )
174                                 out << "\n\t";
175                 }
176
177                 /* Move the key offset ahead. */
178                 curKeyOffset += st->stateCondList.length();
179         }
180         out << "\n";
181         return out;
182 }
183
184 std::ostream &TabCodeGen::KEY_OFFSETS()
185 {
186         out << "\t";
187         int totalStateNum = 0, curKeyOffset = 0;
188         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
189                 /* Write the key offset. */
190                 out << curKeyOffset;
191                 if ( !st.last() ) {
192                         out << ", ";
193                         if ( ++totalStateNum % IALL == 0 )
194                                 out << "\n\t";
195                 }
196
197                 /* Move the key offset ahead. */
198                 curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
199         }
200         out << "\n";
201         return out;
202 }
203
204
205 std::ostream &TabCodeGen::INDEX_OFFSETS()
206 {
207         out << "\t";
208         int totalStateNum = 0, curIndOffset = 0;
209         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
210                 /* Write the index offset. */
211                 out << curIndOffset;
212                 if ( !st.last() ) {
213                         out << ", ";
214                         if ( ++totalStateNum % IALL == 0 )
215                                 out << "\n\t";
216                 }
217
218                 /* Move the index offset ahead. */
219                 curIndOffset += st->outSingle.length() + st->outRange.length();
220                 if ( st->defTrans != 0 )
221                         curIndOffset += 1;
222         }
223         out << "\n";
224         return out;
225 }
226
227 std::ostream &TabCodeGen::COND_LENS()
228 {
229         out << "\t";
230         int totalStateNum = 0;
231         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
232                 /* Write singles length. */
233                 out << st->stateCondList.length();
234                 if ( !st.last() ) {
235                         out << ", ";
236                         if ( ++totalStateNum % IALL == 0 )
237                                 out << "\n\t";
238                 }
239         }
240         out << "\n";
241         return out;
242 }
243
244
245 std::ostream &TabCodeGen::SINGLE_LENS()
246 {
247         out << "\t";
248         int totalStateNum = 0;
249         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
250                 /* Write singles length. */
251                 out << st->outSingle.length();
252                 if ( !st.last() ) {
253                         out << ", ";
254                         if ( ++totalStateNum % IALL == 0 )
255                                 out << "\n\t";
256                 }
257         }
258         out << "\n";
259         return out;
260 }
261
262 std::ostream &TabCodeGen::RANGE_LENS()
263 {
264         out << "\t";
265         int totalStateNum = 0;
266         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
267                 /* Emit length of range index. */
268                 out << st->outRange.length();
269                 if ( !st.last() ) {
270                         out << ", ";
271                         if ( ++totalStateNum % IALL == 0 )
272                                 out << "\n\t";
273                 }
274         }
275         out << "\n";
276         return out;
277 }
278
279 std::ostream &TabCodeGen::TO_STATE_ACTIONS()
280 {
281         out << "\t";
282         int totalStateNum = 0;
283         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
284                 /* Write any eof action. */
285                 TO_STATE_ACTION(st);
286                 if ( !st.last() ) {
287                         out << ", ";
288                         if ( ++totalStateNum % IALL == 0 )
289                                 out << "\n\t";
290                 }
291         }
292         out << "\n";
293         return out;
294 }
295
296 std::ostream &TabCodeGen::FROM_STATE_ACTIONS()
297 {
298         out << "\t";
299         int totalStateNum = 0;
300         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
301                 /* Write any eof action. */
302                 FROM_STATE_ACTION(st);
303                 if ( !st.last() ) {
304                         out << ", ";
305                         if ( ++totalStateNum % IALL == 0 )
306                                 out << "\n\t";
307                 }
308         }
309         out << "\n";
310         return out;
311 }
312
313 std::ostream &TabCodeGen::EOF_ACTIONS()
314 {
315         out << "\t";
316         int totalStateNum = 0;
317         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
318                 /* Write any eof action. */
319                 EOF_ACTION(st);
320                 if ( !st.last() ) {
321                         out << ", ";
322                         if ( ++totalStateNum % IALL == 0 )
323                                 out << "\n\t";
324                 }
325         }
326         out << "\n";
327         return out;
328 }
329
330 std::ostream &TabCodeGen::COND_KEYS()
331 {
332         out << '\t';
333         int totalTrans = 0;
334         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
335                 /* Loop the state's transitions. */
336                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
337                         /* Lower key. */
338                         out << KEY( sc->lowKey ) << ", ";
339                         if ( ++totalTrans % IALL == 0 )
340                                 out << "\n\t";
341
342                         /* Upper key. */
343                         out << KEY( sc->highKey ) << ", ";
344                         if ( ++totalTrans % IALL == 0 )
345                                 out << "\n\t";
346                 }
347         }
348
349         /* Output one last number so we don't have to figure out when the last
350          * entry is and avoid writing a comma. */
351         out << 0 << "\n";
352         return out;
353 }
354
355 std::ostream &TabCodeGen::COND_SPACES()
356 {
357         out << '\t';
358         int totalTrans = 0;
359         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
360                 /* Loop the state's transitions. */
361                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
362                         /* Cond Space id. */
363                         out << sc->condSpace->condSpaceId << ", ";
364                         if ( ++totalTrans % IALL == 0 )
365                                 out << "\n\t";
366                 }
367         }
368
369         /* Output one last number so we don't have to figure out when the last
370          * entry is and avoid writing a comma. */
371         out << 0 << "\n";
372         return out;
373 }
374
375 std::ostream &TabCodeGen::KEYS()
376 {
377         out << '\t';
378         int totalTrans = 0;
379         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
380                 /* Loop the singles. */
381                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
382                         out << KEY( stel->lowKey ) << ", ";
383                         if ( ++totalTrans % IALL == 0 )
384                                 out << "\n\t";
385                 }
386
387                 /* Loop the state's transitions. */
388                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
389                         /* Lower key. */
390                         out << KEY( rtel->lowKey ) << ", ";
391                         if ( ++totalTrans % IALL == 0 )
392                                 out << "\n\t";
393
394                         /* Upper key. */
395                         out << KEY( rtel->highKey ) << ", ";
396                         if ( ++totalTrans % IALL == 0 )
397                                 out << "\n\t";
398                 }
399         }
400
401         /* Output one last number so we don't have to figure out when the last
402          * entry is and avoid writing a comma. */
403         out << 0 << "\n";
404         return out;
405 }
406
407 std::ostream &TabCodeGen::INDICIES()
408 {
409         int totalTrans = 0;
410         out << '\t';
411         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
412                 /* Walk the singles. */
413                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
414                         out << stel->value->id << ", ";
415                         if ( ++totalTrans % IALL == 0 )
416                                 out << "\n\t";
417                 }
418
419                 /* Walk the ranges. */
420                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
421                         out << rtel->value->id << ", ";
422                         if ( ++totalTrans % IALL == 0 )
423                                 out << "\n\t";
424                 }
425
426                 /* The state's default index goes next. */
427                 if ( st->defTrans != 0 ) {
428                         out << st->defTrans->id << ", ";
429                         if ( ++totalTrans % IALL == 0 )
430                                 out << "\n\t";
431                 }
432         }
433
434         /* Output one last number so we don't have to figure out when the last
435          * entry is and avoid writing a comma. */
436         out << 0 << "\n";
437         return out;
438 }
439
440 std::ostream &TabCodeGen::TRANS_TARGS()
441 {
442         int totalTrans = 0;
443         out << '\t';
444         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
445                 /* Walk the singles. */
446                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
447                         RedTransAp *trans = stel->value;
448                         out << trans->targ->id << ", ";
449                         if ( ++totalTrans % IALL == 0 )
450                                 out << "\n\t";
451                 }
452
453                 /* Walk the ranges. */
454                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
455                         RedTransAp *trans = rtel->value;
456                         out << trans->targ->id << ", ";
457                         if ( ++totalTrans % IALL == 0 )
458                                 out << "\n\t";
459                 }
460
461                 /* The state's default target state. */
462                 if ( st->defTrans != 0 ) {
463                         RedTransAp *trans = st->defTrans;
464                         out << trans->targ->id << ", ";
465                         if ( ++totalTrans % IALL == 0 )
466                                 out << "\n\t";
467                 }
468         }
469
470         /* Output one last number so we don't have to figure out when the last
471          * entry is and avoid writing a comma. */
472         out << 0 << "\n";
473         return out;
474 }
475
476
477 std::ostream &TabCodeGen::TRANS_ACTIONS()
478 {
479         int totalTrans = 0;
480         out << '\t';
481         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
482                 /* Walk the singles. */
483                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
484                         RedTransAp *trans = stel->value;
485                         TRANS_ACTION( trans ) << ", ";
486                         if ( ++totalTrans % IALL == 0 )
487                                 out << "\n\t";
488                 }
489
490                 /* Walk the ranges. */
491                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
492                         RedTransAp *trans = rtel->value;
493                         TRANS_ACTION( trans ) << ", ";
494                         if ( ++totalTrans % IALL == 0 )
495                                 out << "\n\t";
496                 }
497
498                 /* The state's default index goes next. */
499                 if ( st->defTrans != 0 ) {
500                         RedTransAp *trans = st->defTrans;
501                         TRANS_ACTION( trans ) << ", ";
502                         if ( ++totalTrans % IALL == 0 )
503                                 out << "\n\t";
504                 }
505         }
506
507         /* Output one last number so we don't have to figure out when the last
508          * entry is and avoid writing a comma. */
509         out << 0 << "\n";
510         return out;
511 }
512
513 std::ostream &TabCodeGen::TRANS_TARGS_WI()
514 {
515         /* Transitions must be written ordered by their id. */
516         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
517         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
518                 transPtrs[trans->id] = trans;
519
520         /* Keep a count of the num of items in the array written. */
521         out << '\t';
522         int totalStates = 0;
523         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
524                 /* Write out the target state. */
525                 RedTransAp *trans = transPtrs[t];
526                 out << trans->targ->id;
527                 if ( t < redFsm->transSet.length()-1 ) {
528                         out << ", ";
529                         if ( ++totalStates % IALL == 0 )
530                                 out << "\n\t";
531                 }
532         }
533         out << "\n";
534         delete[] transPtrs;
535         return out;
536 }
537
538
539 std::ostream &TabCodeGen::TRANS_ACTIONS_WI()
540 {
541         /* Transitions must be written ordered by their id. */
542         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
543         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
544                 transPtrs[trans->id] = trans;
545
546         /* Keep a count of the num of items in the array written. */
547         out << '\t';
548         int totalAct = 0;
549         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
550                 /* Write the function for the transition. */
551                 RedTransAp *trans = transPtrs[t];
552                 TRANS_ACTION( trans );
553                 if ( t < redFsm->transSet.length()-1 ) {
554                         out << ", ";
555                         if ( ++totalAct % IALL == 0 )
556                                 out << "\n\t";
557                 }
558         }
559         out << "\n";
560         delete[] transPtrs;
561         return out;
562 }
563
564 void TabCodeGen::LOCATE_TRANS()
565 {
566         out <<
567                 "       _keys = " << ARR_OFF( K(), KO() + "[" + CS() + "]" ) << ";\n"
568                 "       _trans = " << IO() << "[" << CS() << "];\n"
569                 "\n"
570                 "       _klen = " << SL() << "[" << CS() << "];\n"
571                 "       if ( _klen > 0 ) {\n"
572                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
573                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
574                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + _klen - 1;\n"
575                 "               while (1) {\n"
576                 "                       if ( _upper < _lower )\n"
577                 "                               break;\n"
578                 "\n"
579                 "                       _mid = _lower + ((_upper-_lower) >> 1);\n"
580                 "                       if ( " << GET_WIDE_KEY() << " < *_mid )\n"
581                 "                               _upper = _mid - 1;\n"
582                 "                       else if ( " << GET_WIDE_KEY() << " > *_mid )\n"
583                 "                               _lower = _mid + 1;\n"
584                 "                       else {\n"
585                 "                               _trans += (_mid - _keys);\n"
586                 "                               goto _match;\n"
587                 "                       }\n"
588                 "               }\n"
589                 "               _keys += _klen;\n"
590                 "               _trans += _klen;\n"
591                 "       }\n"
592                 "\n"
593                 "       _klen = " << RL() << "[" << CS() << "];\n"
594                 "       if ( _klen > 0 ) {\n"
595                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
596                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
597                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
598                 "               while (1) {\n"
599                 "                       if ( _upper < _lower )\n"
600                 "                               break;\n"
601                 "\n"
602                 "                       _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
603                 "                       if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
604                 "                               _upper = _mid - 2;\n"
605                 "                       else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
606                 "                               _lower = _mid + 2;\n"
607                 "                       else {\n"
608                 "                               _trans += ((_mid - _keys)>>1);\n"
609                 "                               goto _match;\n"
610                 "                       }\n"
611                 "               }\n"
612                 "               _trans += _klen;\n"
613                 "       }\n"
614                 "\n";
615 }
616
617 void TabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
618 {
619         ret << "{" << CS() << " = " << gotoDest << "; " << 
620                         CTRL_FLOW() << "goto _again;}";
621 }
622
623 void TabCodeGen::GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
624 {
625         ret << "{" << CS() << " = (";
626         INLINE_LIST( ret, ilItem->children, 0, inFinish );
627         ret << "); " << CTRL_FLOW() << "goto _again;}";
628 }
629
630 void TabCodeGen::CURS( ostream &ret, bool inFinish )
631 {
632         ret << "(_ps)";
633 }
634
635 void TabCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
636 {
637         ret << "(" << CS() << ")";
638 }
639
640 void TabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
641 {
642         ret << CS() << " = " << nextDest << ";";
643 }
644
645 void TabCodeGen::NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
646 {
647         ret << CS() << " = (";
648         INLINE_LIST( ret, ilItem->children, 0, inFinish );
649         ret << ");";
650 }
651
652 void TabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
653 {
654         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " << 
655                         callDest << "; " << CTRL_FLOW() << "goto _again;}";
656 }
657
658 void TabCodeGen::CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish )
659 {
660         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
661         INLINE_LIST( ret, ilItem->children, targState, inFinish );
662         ret << "); " << CTRL_FLOW() << "goto _again;}";
663 }
664
665 void TabCodeGen::RET( ostream &ret, bool inFinish )
666 {
667         ret << "{" << CS() << " = " << STACK() << "[--" << 
668                         TOP() << "]; " << CTRL_FLOW() <<  "goto _again;}";
669 }
670
671 void TabCodeGen::BREAK( ostream &ret, int targState )
672 {
673         outLabelUsed = true;
674         ret << CTRL_FLOW() << "goto _out;";
675 }
676
677 void TabCodeGen::writeData()
678 {
679         /* If there are any transtion functions then output the array. If there
680          * are none, don't bother emitting an empty array that won't be used. */
681         if ( redFsm->anyActions() ) {
682                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
683                 ACTIONS_ARRAY();
684                 CLOSE_ARRAY() <<
685                 "\n";
686         }
687
688         if ( redFsm->anyConditions() ) {
689                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
690                 COND_OFFSETS();
691                 CLOSE_ARRAY() <<
692                 "\n";
693
694                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
695                 COND_LENS();
696                 CLOSE_ARRAY() <<
697                 "\n";
698
699                 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
700                 COND_KEYS();
701                 CLOSE_ARRAY() <<
702                 "\n";
703
704                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
705                 COND_SPACES();
706                 CLOSE_ARRAY() <<
707                 "\n";
708         }
709
710         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
711         KEY_OFFSETS();
712         CLOSE_ARRAY() <<
713         "\n";
714
715         OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
716         KEYS();
717         CLOSE_ARRAY() <<
718         "\n";
719
720         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
721         SINGLE_LENS();
722         CLOSE_ARRAY() <<
723         "\n";
724
725         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
726         RANGE_LENS();
727         CLOSE_ARRAY() <<
728         "\n";
729
730         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
731         INDEX_OFFSETS();
732         CLOSE_ARRAY() <<
733         "\n";
734
735         if ( useIndicies ) {
736                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
737                 INDICIES();
738                 CLOSE_ARRAY() <<
739                 "\n";
740
741                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
742                 TRANS_TARGS_WI();
743                 CLOSE_ARRAY() <<
744                 "\n";
745
746                 if ( redFsm->anyActions() ) {
747                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
748                         TRANS_ACTIONS_WI();
749                         CLOSE_ARRAY() <<
750                         "\n";
751                 }
752         }
753         else {
754                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
755                 TRANS_TARGS();
756                 CLOSE_ARRAY() <<
757                 "\n";
758
759                 if ( redFsm->anyActions() ) {
760                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
761                         TRANS_ACTIONS();
762                         CLOSE_ARRAY() <<
763                         "\n";
764                 }
765         }
766
767         if ( redFsm->anyToStateActions() ) {
768                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
769                 TO_STATE_ACTIONS();
770                 CLOSE_ARRAY() <<
771                 "\n";
772         }
773
774         if ( redFsm->anyFromStateActions() ) {
775                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
776                 FROM_STATE_ACTIONS();
777                 CLOSE_ARRAY() <<
778                 "\n";
779         }
780
781         if ( redFsm->anyEofActions() ) {
782                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
783                 EOF_ACTIONS();
784                 CLOSE_ARRAY() <<
785                 "\n";
786         }
787
788         STATE_IDS();
789 }
790
791 void TabCodeGen::COND_TRANSLATE()
792 {
793         out << 
794                 "       _widec = " << GET_KEY() << ";\n"
795                 "       _klen = " << CL() << "[" << CS() << "];\n"
796                 "       _keys = " << ARR_OFF( CK(), "(" + CO() + "[" + CS() + "]*2)" ) << ";\n"
797                 "       if ( _klen > 0 ) {\n"
798                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
799                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
800                 "               " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
801                 "               while (1) {\n"
802                 "                       if ( _upper < _lower )\n"
803                 "                               break;\n"
804                 "\n"
805                 "                       _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
806                 "                       if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
807                 "                               _upper = _mid - 2;\n"
808                 "                       else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
809                 "                               _lower = _mid + 2;\n"
810                 "                       else {\n"
811                 "                               switch ( " << C() << "[" << CO() << "[" << CS() << "]"
812                                                         " + ((_mid - _keys)>>1)] ) {\n";
813
814         for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
815                 CondSpace *condSpace = csi;
816                 out << "        case " << condSpace->condSpaceId << ": {\n";
817                 out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
818                                 KEY(condSpace->baseKey) << " + (" << GET_KEY() << 
819                                 " - " << KEY(keyOps->minKey) << "));\n";
820
821                 for ( CondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
822                         out << TABS(2) << "if ( ";
823                         CONDITION( out, *csi );
824                         Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
825                         out << " ) _widec += " << condValOffset << ";\n";
826                 }
827
828                 out << 
829                         "               break;\n"
830                         "       }\n";
831         }
832
833         SWITCH_DEFAULT();
834
835         out << 
836                 "                               }\n"
837                 "                               break;\n"
838                 "                       }\n"
839                 "               }\n"
840                 "       }\n"
841                 "\n";
842 }
843
844 void TabCodeGen::writeExec()
845 {
846         outLabelUsed = false;
847
848         out <<
849                 "       {\n"
850                 "       int _klen";
851
852         if ( redFsm->anyRegCurStateRef() )
853                 out << ", _ps";
854
855         out << 
856                 ";\n"
857                 "       " << UINT() << " _trans;\n";
858
859         if ( redFsm->anyConditions() )
860                 out << "        " << WIDE_ALPH_TYPE() << " _widec;\n";
861
862         if ( redFsm->anyToStateActions() || redFsm->anyRegActions() 
863                         || redFsm->anyFromStateActions() )
864         {
865                 out << 
866                         "       " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << POINTER() << "_acts;\n"
867                         "       " << UINT() << " _nacts;\n";
868         }
869
870         out <<
871                 "       " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
872                 "\n";
873
874         if ( hasEnd ) {
875                 outLabelUsed = true;
876                 out << 
877                         "       if ( " << P() << " == " << PE() << " )\n"
878                         "               goto _out;\n";
879         }
880
881         out << "_resume:\n";
882
883         if ( redFsm->errState != 0 ) {
884                 outLabelUsed = true;
885                 out << 
886                         "       if ( " << CS() << " == " << redFsm->errState->id << " )\n"
887                         "               goto _out;\n";
888         }
889
890         if ( redFsm->anyFromStateActions() ) {
891                 out <<
892                         "       _acts = " << ARR_OFF( A(),  FSA() + "[" + CS() + "]" ) << ";\n"
893                         "       _nacts = " << CAST(UINT()) << " *_acts++;\n"
894                         "       while ( _nacts-- > 0 ) {\n"
895                         "               switch ( *_acts++ ) {\n";
896                         FROM_STATE_ACTION_SWITCH();
897                         SWITCH_DEFAULT() <<
898                         "               }\n"
899                         "       }\n"
900                         "\n";
901         }
902
903         if ( redFsm->anyConditions() )
904                 COND_TRANSLATE();
905
906         LOCATE_TRANS();
907
908         out << "_match:\n";
909
910         if ( redFsm->anyRegCurStateRef() )
911                 out << "        _ps = " << CS() << ";\n";
912
913         if ( useIndicies )
914                 out << "        _trans = " << I() << "[_trans];\n";
915
916         out <<
917                 "       " << CS() << " = " << TT() << "[_trans];\n"
918                 "\n";
919
920         if ( redFsm->anyRegActions() ) {
921                 out <<
922                         "       if ( " << TA() << "[_trans] == 0 )\n"
923                         "               goto _again;\n"
924                         "\n"
925                         "       _acts = " << ARR_OFF( A(), TA() + "[_trans]" ) << ";\n"
926                         "       _nacts = " << CAST(UINT()) << " *_acts++;\n"
927                         "       while ( _nacts-- > 0 )\n        {\n"
928                         "               switch ( *_acts++ )\n           {\n";
929                         ACTION_SWITCH();
930                         SWITCH_DEFAULT() <<
931                         "               }\n"
932                         "       }\n"
933                         "\n";
934         }
935
936         if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || 
937                         redFsm->anyActionCalls() || redFsm->anyActionRets() )
938                 out << "_again:\n";
939
940         if ( redFsm->anyToStateActions() ) {
941                 out <<
942                         "       _acts = " << ARR_OFF( A(), TSA() + "[" + CS() + "]" ) << ";\n"
943                         "       _nacts = " << CAST(UINT()) << " *_acts++;\n"
944                         "       while ( _nacts-- > 0 ) {\n"
945                         "               switch ( *_acts++ ) {\n";
946                         TO_STATE_ACTION_SWITCH();
947                         SWITCH_DEFAULT() <<
948                         "               }\n"
949                         "       }\n"
950                         "\n";
951         }
952
953         if ( hasEnd ) {
954                 out << 
955                         "       if ( ++" << P() << " != " << PE() << " )\n"
956                         "               goto _resume;\n";
957         }
958         else {
959                 out << 
960                         "       " << P() << " += 1;\n"
961                         "       goto _resume;\n";
962         }
963         
964         if ( outLabelUsed )
965                 out << "        _out: {}\n";
966
967         out << "        }\n";
968 }
969
970
971 void TabCodeGen::writeEOF()
972 {
973         if ( redFsm->anyEofActions() ) {
974                 out << 
975                         "       {\n"
976                         "       " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << POINTER() << "_acts = " << 
977                                         ARR_OFF( A(), EA() + "[" + CS() + "]" ) << ";\n"
978                         "       " << UINT() << " _nacts = " << CAST(UINT()) << " *_acts++;\n"
979                         "       while ( _nacts-- > 0 ) {\n"
980                         "               switch ( *_acts++ ) {\n";
981                         EOF_ACTION_SWITCH();
982                         SWITCH_DEFAULT() <<
983                         "               }\n"
984                         "       }\n"
985                         "       }\n"
986                         "\n";
987         }
988 }