A fix to the documentation makefile from John D. Mitchell.
[ragel.git] / rlgen-cd / tabcodegen.cpp
blob82d9d4329e32b67951e2a16dbafad9a15a056194
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 */
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
24 #include "rlgen-cd.h"
25 #include "tabcodegen.h"
26 #include "redfsm.h"
27 #include "gendata.h"
29 /* Determine if we should use indicies or not. */
30 void TabCodeGen::calcIndexSize()
32 int sizeWithInds = 0, sizeWithoutInds = 0;
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;
40 sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
41 if ( redFsm->anyActions() )
42 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
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;
53 /* If using indicies reduces the size, use them. */
54 useIndicies = sizeWithInds < sizeWithoutInds;
57 std::ostream &TabCodeGen::TO_STATE_ACTION( RedStateAp *state )
59 int act = 0;
60 if ( state->toStateAction != 0 )
61 act = state->toStateAction->location+1;
62 out << act;
63 return out;
66 std::ostream &TabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
68 int act = 0;
69 if ( state->fromStateAction != 0 )
70 act = state->fromStateAction->location+1;
71 out << act;
72 return out;
75 std::ostream &TabCodeGen::EOF_ACTION( RedStateAp *state )
77 int act = 0;
78 if ( state->eofAction != 0 )
79 act = state->eofAction->location+1;
80 out << act;
81 return out;
85 std::ostream &TabCodeGen::TRANS_ACTION( RedTransAp *trans )
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;
95 std::ostream &TabCodeGen::TO_STATE_ACTION_SWITCH()
97 /* Walk the list of functions, printing the cases. */
98 for ( GenActionList::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, false );
104 out << "\tbreak;\n";
108 genLineDirective( out );
109 return out;
112 std::ostream &TabCodeGen::FROM_STATE_ACTION_SWITCH()
114 /* Walk the list of functions, printing the cases. */
115 for ( GenActionList::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, false );
121 out << "\tbreak;\n";
125 genLineDirective( out );
126 return out;
129 std::ostream &TabCodeGen::EOF_ACTION_SWITCH()
131 /* Walk the list of functions, printing the cases. */
132 for ( GenActionList::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, false );
138 out << "\tbreak;\n";
142 genLineDirective( out );
143 return out;
147 std::ostream &TabCodeGen::ACTION_SWITCH()
149 /* Walk the list of functions, printing the cases. */
150 for ( GenActionList::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, false );
156 out << "\tbreak;\n";
160 genLineDirective( out );
161 return out;
164 std::ostream &TabCodeGen::COND_OFFSETS()
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";
177 /* Move the key offset ahead. */
178 curKeyOffset += st->stateCondList.length();
180 out << "\n";
181 return out;
184 std::ostream &TabCodeGen::KEY_OFFSETS()
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";
197 /* Move the key offset ahead. */
198 curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
200 out << "\n";
201 return out;
205 std::ostream &TabCodeGen::INDEX_OFFSETS()
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";
218 /* Move the index offset ahead. */
219 curIndOffset += st->outSingle.length() + st->outRange.length();
220 if ( st->defTrans != 0 )
221 curIndOffset += 1;
223 out << "\n";
224 return out;
227 std::ostream &TabCodeGen::COND_LENS()
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";
240 out << "\n";
241 return out;
245 std::ostream &TabCodeGen::SINGLE_LENS()
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";
258 out << "\n";
259 return out;
262 std::ostream &TabCodeGen::RANGE_LENS()
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";
275 out << "\n";
276 return out;
279 std::ostream &TabCodeGen::TO_STATE_ACTIONS()
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";
292 out << "\n";
293 return out;
296 std::ostream &TabCodeGen::FROM_STATE_ACTIONS()
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";
309 out << "\n";
310 return out;
313 std::ostream &TabCodeGen::EOF_ACTIONS()
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";
326 out << "\n";
327 return out;
330 std::ostream &TabCodeGen::EOF_TRANS()
332 out << "\t";
333 int totalStateNum = 0;
334 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
335 /* Write any eof action. */
336 long trans = 0;
337 if ( st->eofTrans != 0 ) {
338 assert( st->eofTrans->pos >= 0 );
339 trans = st->eofTrans->pos+1;
341 out << trans;
343 if ( !st.last() ) {
344 out << ", ";
345 if ( ++totalStateNum % IALL == 0 )
346 out << "\n\t";
349 out << "\n";
350 return out;
354 std::ostream &TabCodeGen::COND_KEYS()
356 out << '\t';
357 int totalTrans = 0;
358 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
359 /* Loop the state's transitions. */
360 for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
361 /* Lower key. */
362 out << KEY( sc->lowKey ) << ", ";
363 if ( ++totalTrans % IALL == 0 )
364 out << "\n\t";
366 /* Upper key. */
367 out << KEY( sc->highKey ) << ", ";
368 if ( ++totalTrans % IALL == 0 )
369 out << "\n\t";
373 /* Output one last number so we don't have to figure out when the last
374 * entry is and avoid writing a comma. */
375 out << 0 << "\n";
376 return out;
379 std::ostream &TabCodeGen::COND_SPACES()
381 out << '\t';
382 int totalTrans = 0;
383 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
384 /* Loop the state's transitions. */
385 for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
386 /* Cond Space id. */
387 out << sc->condSpace->condSpaceId << ", ";
388 if ( ++totalTrans % IALL == 0 )
389 out << "\n\t";
393 /* Output one last number so we don't have to figure out when the last
394 * entry is and avoid writing a comma. */
395 out << 0 << "\n";
396 return out;
399 std::ostream &TabCodeGen::KEYS()
401 out << '\t';
402 int totalTrans = 0;
403 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
404 /* Loop the singles. */
405 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
406 out << KEY( stel->lowKey ) << ", ";
407 if ( ++totalTrans % IALL == 0 )
408 out << "\n\t";
411 /* Loop the state's transitions. */
412 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
413 /* Lower key. */
414 out << KEY( rtel->lowKey ) << ", ";
415 if ( ++totalTrans % IALL == 0 )
416 out << "\n\t";
418 /* Upper key. */
419 out << KEY( rtel->highKey ) << ", ";
420 if ( ++totalTrans % IALL == 0 )
421 out << "\n\t";
425 /* Output one last number so we don't have to figure out when the last
426 * entry is and avoid writing a comma. */
427 out << 0 << "\n";
428 return out;
431 std::ostream &TabCodeGen::INDICIES()
433 int totalTrans = 0;
434 out << '\t';
435 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
436 /* Walk the singles. */
437 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
438 out << stel->value->id << ", ";
439 if ( ++totalTrans % IALL == 0 )
440 out << "\n\t";
443 /* Walk the ranges. */
444 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
445 out << rtel->value->id << ", ";
446 if ( ++totalTrans % IALL == 0 )
447 out << "\n\t";
450 /* The state's default index goes next. */
451 if ( st->defTrans != 0 ) {
452 out << st->defTrans->id << ", ";
453 if ( ++totalTrans % IALL == 0 )
454 out << "\n\t";
458 /* Output one last number so we don't have to figure out when the last
459 * entry is and avoid writing a comma. */
460 out << 0 << "\n";
461 return out;
464 std::ostream &TabCodeGen::TRANS_TARGS()
466 int totalTrans = 0;
467 out << '\t';
468 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
469 /* Walk the singles. */
470 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
471 RedTransAp *trans = stel->value;
472 out << trans->targ->id << ", ";
473 if ( ++totalTrans % IALL == 0 )
474 out << "\n\t";
477 /* Walk the ranges. */
478 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
479 RedTransAp *trans = rtel->value;
480 out << trans->targ->id << ", ";
481 if ( ++totalTrans % IALL == 0 )
482 out << "\n\t";
485 /* The state's default target state. */
486 if ( st->defTrans != 0 ) {
487 RedTransAp *trans = st->defTrans;
488 out << trans->targ->id << ", ";
489 if ( ++totalTrans % IALL == 0 )
490 out << "\n\t";
494 /* Add any eof transitions that have not yet been written out above. */
495 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
496 if ( st->eofTrans != 0 ) {
497 RedTransAp *trans = st->eofTrans;
498 trans->pos = totalTrans;
499 out << trans->targ->id << ", ";
500 if ( ++totalTrans % IALL == 0 )
501 out << "\n\t";
506 /* Output one last number so we don't have to figure out when the last
507 * entry is and avoid writing a comma. */
508 out << 0 << "\n";
509 return out;
513 std::ostream &TabCodeGen::TRANS_ACTIONS()
515 int totalTrans = 0;
516 out << '\t';
517 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
518 /* Walk the singles. */
519 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
520 RedTransAp *trans = stel->value;
521 TRANS_ACTION( trans ) << ", ";
522 if ( ++totalTrans % IALL == 0 )
523 out << "\n\t";
526 /* Walk the ranges. */
527 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
528 RedTransAp *trans = rtel->value;
529 TRANS_ACTION( trans ) << ", ";
530 if ( ++totalTrans % IALL == 0 )
531 out << "\n\t";
534 /* The state's default index goes next. */
535 if ( st->defTrans != 0 ) {
536 RedTransAp *trans = st->defTrans;
537 TRANS_ACTION( trans ) << ", ";
538 if ( ++totalTrans % IALL == 0 )
539 out << "\n\t";
543 /* Add any eof transitions that have not yet been written out above. */
544 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
545 if ( st->eofTrans != 0 ) {
546 RedTransAp *trans = st->eofTrans;
547 TRANS_ACTION( trans ) << ", ";
548 if ( ++totalTrans % IALL == 0 )
549 out << "\n\t";
554 /* Output one last number so we don't have to figure out when the last
555 * entry is and avoid writing a comma. */
556 out << 0 << "\n";
557 return out;
560 std::ostream &TabCodeGen::TRANS_TARGS_WI()
562 /* Transitions must be written ordered by their id. */
563 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
564 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
565 transPtrs[trans->id] = trans;
567 /* Keep a count of the num of items in the array written. */
568 out << '\t';
569 int totalStates = 0;
570 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
571 /* Record the position, need this for eofTrans. */
572 RedTransAp *trans = transPtrs[t];
573 trans->pos = t;
575 /* Write out the target state. */
576 out << trans->targ->id;
577 if ( t < redFsm->transSet.length()-1 ) {
578 out << ", ";
579 if ( ++totalStates % IALL == 0 )
580 out << "\n\t";
583 out << "\n";
584 delete[] transPtrs;
585 return out;
589 std::ostream &TabCodeGen::TRANS_ACTIONS_WI()
591 /* Transitions must be written ordered by their id. */
592 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
593 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
594 transPtrs[trans->id] = trans;
596 /* Keep a count of the num of items in the array written. */
597 out << '\t';
598 int totalAct = 0;
599 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
600 /* Write the function for the transition. */
601 RedTransAp *trans = transPtrs[t];
602 TRANS_ACTION( trans );
603 if ( t < redFsm->transSet.length()-1 ) {
604 out << ", ";
605 if ( ++totalAct % IALL == 0 )
606 out << "\n\t";
609 out << "\n";
610 delete[] transPtrs;
611 return out;
614 void TabCodeGen::LOCATE_TRANS()
616 out <<
617 " _keys = " << ARR_OFF( K(), KO() + "[" + CS() + "]" ) << ";\n"
618 " _trans = " << IO() << "[" << CS() << "];\n"
619 "\n"
620 " _klen = " << SL() << "[" << CS() << "];\n"
621 " if ( _klen > 0 ) {\n"
622 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
623 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
624 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + _klen - 1;\n"
625 " while (1) {\n"
626 " if ( _upper < _lower )\n"
627 " break;\n"
628 "\n"
629 " _mid = _lower + ((_upper-_lower) >> 1);\n"
630 " if ( " << GET_WIDE_KEY() << " < *_mid )\n"
631 " _upper = _mid - 1;\n"
632 " else if ( " << GET_WIDE_KEY() << " > *_mid )\n"
633 " _lower = _mid + 1;\n"
634 " else {\n"
635 " _trans += (_mid - _keys);\n"
636 " goto _match;\n"
637 " }\n"
638 " }\n"
639 " _keys += _klen;\n"
640 " _trans += _klen;\n"
641 " }\n"
642 "\n"
643 " _klen = " << RL() << "[" << CS() << "];\n"
644 " if ( _klen > 0 ) {\n"
645 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
646 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
647 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
648 " while (1) {\n"
649 " if ( _upper < _lower )\n"
650 " break;\n"
651 "\n"
652 " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
653 " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
654 " _upper = _mid - 2;\n"
655 " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
656 " _lower = _mid + 2;\n"
657 " else {\n"
658 " _trans += ((_mid - _keys)>>1);\n"
659 " goto _match;\n"
660 " }\n"
661 " }\n"
662 " _trans += _klen;\n"
663 " }\n"
664 "\n";
667 void TabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
669 ret << "{" << CS() << " = " << gotoDest << "; " <<
670 CTRL_FLOW() << "goto _again;}";
673 void TabCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
675 ret << "{" << CS() << " = (";
676 INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
677 ret << "); " << CTRL_FLOW() << "goto _again;}";
680 void TabCodeGen::CURS( ostream &ret, bool inFinish )
682 ret << "(_ps)";
685 void TabCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
687 ret << "(" << CS() << ")";
690 void TabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
692 ret << CS() << " = " << nextDest << ";";
695 void TabCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
697 ret << CS() << " = (";
698 INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
699 ret << ");";
702 void TabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
704 if ( prePushExpr != 0 ) {
705 ret << "{";
706 INLINE_LIST( ret, prePushExpr, 0, false, false );
709 ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " <<
710 callDest << "; " << CTRL_FLOW() << "goto _again;}";
712 if ( prePushExpr != 0 )
713 ret << "}";
716 void TabCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
718 if ( prePushExpr != 0 ) {
719 ret << "{";
720 INLINE_LIST( ret, prePushExpr, 0, false, false );
723 ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
724 INLINE_LIST( ret, ilItem->children, targState, inFinish, false );
725 ret << "); " << CTRL_FLOW() << "goto _again;}";
727 if ( prePushExpr != 0 )
728 ret << "}";
731 void TabCodeGen::RET( ostream &ret, bool inFinish )
733 ret << "{" << CS() << " = " << STACK() << "[--" <<
734 TOP() << "]; ";
736 if ( postPopExpr != 0 ) {
737 ret << "{";
738 INLINE_LIST( ret, postPopExpr, 0, false, false );
739 ret << "}";
742 ret << CTRL_FLOW() << "goto _again;}";
745 void TabCodeGen::BREAK( ostream &ret, int targState, bool csForced )
747 outLabelUsed = true;
748 ret << "{" << P() << "++; " << CTRL_FLOW() << "goto _out; }";
751 void TabCodeGen::writeData()
753 /* If there are any transtion functions then output the array. If there
754 * are none, don't bother emitting an empty array that won't be used. */
755 if ( redFsm->anyActions() ) {
756 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
757 ACTIONS_ARRAY();
758 CLOSE_ARRAY() <<
759 "\n";
762 if ( redFsm->anyConditions() ) {
763 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
764 COND_OFFSETS();
765 CLOSE_ARRAY() <<
766 "\n";
768 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
769 COND_LENS();
770 CLOSE_ARRAY() <<
771 "\n";
773 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
774 COND_KEYS();
775 CLOSE_ARRAY() <<
776 "\n";
778 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
779 COND_SPACES();
780 CLOSE_ARRAY() <<
781 "\n";
784 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
785 KEY_OFFSETS();
786 CLOSE_ARRAY() <<
787 "\n";
789 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
790 KEYS();
791 CLOSE_ARRAY() <<
792 "\n";
794 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
795 SINGLE_LENS();
796 CLOSE_ARRAY() <<
797 "\n";
799 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
800 RANGE_LENS();
801 CLOSE_ARRAY() <<
802 "\n";
804 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
805 INDEX_OFFSETS();
806 CLOSE_ARRAY() <<
807 "\n";
809 if ( useIndicies ) {
810 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
811 INDICIES();
812 CLOSE_ARRAY() <<
813 "\n";
815 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
816 TRANS_TARGS_WI();
817 CLOSE_ARRAY() <<
818 "\n";
820 if ( redFsm->anyActions() ) {
821 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
822 TRANS_ACTIONS_WI();
823 CLOSE_ARRAY() <<
824 "\n";
827 else {
828 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
829 TRANS_TARGS();
830 CLOSE_ARRAY() <<
831 "\n";
833 if ( redFsm->anyActions() ) {
834 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
835 TRANS_ACTIONS();
836 CLOSE_ARRAY() <<
837 "\n";
841 if ( redFsm->anyToStateActions() ) {
842 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
843 TO_STATE_ACTIONS();
844 CLOSE_ARRAY() <<
845 "\n";
848 if ( redFsm->anyFromStateActions() ) {
849 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
850 FROM_STATE_ACTIONS();
851 CLOSE_ARRAY() <<
852 "\n";
855 if ( redFsm->anyEofActions() ) {
856 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
857 EOF_ACTIONS();
858 CLOSE_ARRAY() <<
859 "\n";
862 if ( redFsm->anyEofTrans() ) {
863 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
864 EOF_TRANS();
865 CLOSE_ARRAY() <<
866 "\n";
869 STATE_IDS();
872 void TabCodeGen::COND_TRANSLATE()
874 out <<
875 " _widec = " << GET_KEY() << ";\n"
876 " _klen = " << CL() << "[" << CS() << "];\n"
877 " _keys = " << ARR_OFF( CK(), "(" + CO() + "[" + CS() + "]*2)" ) << ";\n"
878 " if ( _klen > 0 ) {\n"
879 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
880 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
881 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
882 " while (1) {\n"
883 " if ( _upper < _lower )\n"
884 " break;\n"
885 "\n"
886 " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
887 " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
888 " _upper = _mid - 2;\n"
889 " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
890 " _lower = _mid + 2;\n"
891 " else {\n"
892 " switch ( " << C() << "[" << CO() << "[" << CS() << "]"
893 " + ((_mid - _keys)>>1)] ) {\n";
895 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
896 GenCondSpace *condSpace = csi;
897 out << " case " << condSpace->condSpaceId << ": {\n";
898 out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
899 KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
900 " - " << KEY(keyOps->minKey) << "));\n";
902 for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
903 out << TABS(2) << "if ( ";
904 CONDITION( out, *csi );
905 Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
906 out << " ) _widec += " << condValOffset << ";\n";
909 out <<
910 " break;\n"
911 " }\n";
914 SWITCH_DEFAULT();
916 out <<
917 " }\n"
918 " break;\n"
919 " }\n"
920 " }\n"
921 " }\n"
922 "\n";
925 void TabCodeGen::writeExec()
927 testEofUsed = false;
928 outLabelUsed = false;
930 out <<
931 " {\n"
932 " int _klen";
934 if ( redFsm->anyRegCurStateRef() )
935 out << ", _ps";
937 out <<
938 ";\n"
939 " " << UINT() << " _trans;\n";
941 if ( redFsm->anyConditions() )
942 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
944 if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
945 || redFsm->anyFromStateActions() )
947 out <<
948 " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) <<
949 POINTER() << "_acts;\n"
950 " " << UINT() << " _nacts;\n";
953 out <<
954 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
955 "\n";
957 if ( hasEnd ) {
958 testEofUsed = true;
959 out <<
960 " if ( " << P() << " == " << PE() << " )\n"
961 " goto _test_eof;\n";
964 if ( redFsm->errState != 0 ) {
965 outLabelUsed = true;
966 out <<
967 " if ( " << CS() << " == " << redFsm->errState->id << " )\n"
968 " goto _out;\n";
971 out << "_resume:\n";
973 if ( redFsm->anyFromStateActions() ) {
974 out <<
975 " _acts = " << ARR_OFF( A(), FSA() + "[" + CS() + "]" ) << ";\n"
976 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
977 " while ( _nacts-- > 0 ) {\n"
978 " switch ( *_acts++ ) {\n";
979 FROM_STATE_ACTION_SWITCH();
980 SWITCH_DEFAULT() <<
981 " }\n"
982 " }\n"
983 "\n";
986 if ( redFsm->anyConditions() )
987 COND_TRANSLATE();
989 LOCATE_TRANS();
991 out << "_match:\n";
993 if ( useIndicies )
994 out << " _trans = " << I() << "[_trans];\n";
996 if ( redFsm->anyEofTrans() )
997 out << "_eof_trans:\n";
999 if ( redFsm->anyRegCurStateRef() )
1000 out << " _ps = " << CS() << ";\n";
1002 out <<
1003 " " << CS() << " = " << TT() << "[_trans];\n"
1004 "\n";
1006 if ( redFsm->anyRegActions() ) {
1007 out <<
1008 " if ( " << TA() << "[_trans] == 0 )\n"
1009 " goto _again;\n"
1010 "\n"
1011 " _acts = " << ARR_OFF( A(), TA() + "[_trans]" ) << ";\n"
1012 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
1013 " while ( _nacts-- > 0 )\n {\n"
1014 " switch ( *_acts++ )\n {\n";
1015 ACTION_SWITCH();
1016 SWITCH_DEFAULT() <<
1017 " }\n"
1018 " }\n"
1019 "\n";
1022 if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
1023 redFsm->anyActionCalls() || redFsm->anyActionRets() )
1024 out << "_again:\n";
1026 if ( redFsm->anyToStateActions() ) {
1027 out <<
1028 " _acts = " << ARR_OFF( A(), TSA() + "[" + CS() + "]" ) << ";\n"
1029 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
1030 " while ( _nacts-- > 0 ) {\n"
1031 " switch ( *_acts++ ) {\n";
1032 TO_STATE_ACTION_SWITCH();
1033 SWITCH_DEFAULT() <<
1034 " }\n"
1035 " }\n"
1036 "\n";
1039 if ( redFsm->errState != 0 ) {
1040 outLabelUsed = true;
1041 out <<
1042 " if ( " << CS() << " == " << redFsm->errState->id << " )\n"
1043 " goto _out;\n";
1046 if ( hasEnd ) {
1047 out <<
1048 " if ( ++" << P() << " != " << PE() << " )\n"
1049 " goto _resume;\n";
1051 else {
1052 out <<
1053 " " << P() << " += 1;\n"
1054 " goto _resume;\n";
1057 if ( testEofUsed )
1058 out << " _test_eof: {}\n";
1060 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
1061 out <<
1062 " if ( " << P() << " == " << EOFV() << " )\n"
1063 " {\n";
1065 if ( redFsm->anyEofTrans() ) {
1066 out <<
1067 " if ( " << ET() << "[" << CS() << "] > 0 ) {\n"
1068 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
1069 " goto _eof_trans;\n"
1070 " }\n";
1073 if ( redFsm->anyEofActions() ) {
1074 out <<
1075 " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) <<
1076 POINTER() << "__acts = " <<
1077 ARR_OFF( A(), EA() + "[" + CS() + "]" ) << ";\n"
1078 " " << UINT() << " __nacts = " << CAST(UINT()) << " *__acts++;\n"
1079 " while ( __nacts-- > 0 ) {\n"
1080 " switch ( *__acts++ ) {\n";
1081 EOF_ACTION_SWITCH();
1082 SWITCH_DEFAULT() <<
1083 " }\n"
1084 " }\n";
1087 out <<
1088 " }\n"
1089 "\n";
1092 if ( outLabelUsed )
1093 out << " _out: {}\n";
1095 out << " }\n";