2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
9 * Ragel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Ragel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Ragel; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "tabcodegen.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
)
60 if ( state
->toStateAction
!= 0 )
61 act
= state
->toStateAction
->location
+1;
66 std::ostream
&TabCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
69 if ( state
->fromStateAction
!= 0 )
70 act
= state
->fromStateAction
->location
+1;
75 std::ostream
&TabCodeGen::EOF_ACTION( RedStateAp
*state
)
78 if ( state
->eofAction
!= 0 )
79 act
= state
->eofAction
->location
+1;
85 std::ostream
&TabCodeGen::TRANS_ACTION( RedTransAp
*trans
)
87 /* If there are actions, emit them. Otherwise emit zero. */
89 if ( trans
->action
!= 0 )
90 act
= trans
->action
->location
+1;
95 std::ostream
&TabCodeGen::TO_STATE_ACTION_SWITCH()
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 );
108 genLineDirective( out
);
112 std::ostream
&TabCodeGen::FROM_STATE_ACTION_SWITCH()
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 );
125 genLineDirective( out
);
129 std::ostream
&TabCodeGen::EOF_ACTION_SWITCH()
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 );
142 genLineDirective( out
);
147 std::ostream
&TabCodeGen::ACTION_SWITCH()
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 );
160 genLineDirective( out
);
164 std::ostream
&TabCodeGen::COND_OFFSETS()
167 int totalStateNum
= 0, curKeyOffset
= 0;
168 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
169 /* Write the key offset. */
173 if ( ++totalStateNum
% IALL
== 0 )
177 /* Move the key offset ahead. */
178 curKeyOffset
+= st
->stateCondList
.length();
184 std::ostream
&TabCodeGen::KEY_OFFSETS()
187 int totalStateNum
= 0, curKeyOffset
= 0;
188 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
189 /* Write the key offset. */
193 if ( ++totalStateNum
% IALL
== 0 )
197 /* Move the key offset ahead. */
198 curKeyOffset
+= st
->outSingle
.length() + st
->outRange
.length()*2;
205 std::ostream
&TabCodeGen::INDEX_OFFSETS()
208 int totalStateNum
= 0, curIndOffset
= 0;
209 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
210 /* Write the index offset. */
214 if ( ++totalStateNum
% IALL
== 0 )
218 /* Move the index offset ahead. */
219 curIndOffset
+= st
->outSingle
.length() + st
->outRange
.length();
220 if ( st
->defTrans
!= 0 )
227 std::ostream
&TabCodeGen::COND_LENS()
230 int totalStateNum
= 0;
231 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
232 /* Write singles length. */
233 out
<< st
->stateCondList
.length();
236 if ( ++totalStateNum
% IALL
== 0 )
245 std::ostream
&TabCodeGen::SINGLE_LENS()
248 int totalStateNum
= 0;
249 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
250 /* Write singles length. */
251 out
<< st
->outSingle
.length();
254 if ( ++totalStateNum
% IALL
== 0 )
262 std::ostream
&TabCodeGen::RANGE_LENS()
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();
271 if ( ++totalStateNum
% IALL
== 0 )
279 std::ostream
&TabCodeGen::TO_STATE_ACTIONS()
282 int totalStateNum
= 0;
283 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
284 /* Write any eof action. */
288 if ( ++totalStateNum
% IALL
== 0 )
296 std::ostream
&TabCodeGen::FROM_STATE_ACTIONS()
299 int totalStateNum
= 0;
300 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
301 /* Write any eof action. */
302 FROM_STATE_ACTION(st
);
305 if ( ++totalStateNum
% IALL
== 0 )
313 std::ostream
&TabCodeGen::EOF_ACTIONS()
316 int totalStateNum
= 0;
317 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
318 /* Write any eof action. */
322 if ( ++totalStateNum
% IALL
== 0 )
330 std::ostream
&TabCodeGen::EOF_TRANS()
333 int totalStateNum
= 0;
334 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
335 /* Write any eof action. */
337 if ( st
->eofTrans
!= 0 )
338 trans
= st
->eofTrans
->id
+1;
343 if ( ++totalStateNum
% IALL
== 0 )
352 std::ostream
&TabCodeGen::COND_KEYS()
356 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
357 /* Loop the state's transitions. */
358 for ( StateCondList::Iter sc
= st
->stateCondList
; sc
.lte(); sc
++ ) {
360 out
<< KEY( sc
->lowKey
) << ", ";
361 if ( ++totalTrans
% IALL
== 0 )
365 out
<< KEY( sc
->highKey
) << ", ";
366 if ( ++totalTrans
% IALL
== 0 )
371 /* Output one last number so we don't have to figure out when the last
372 * entry is and avoid writing a comma. */
377 std::ostream
&TabCodeGen::COND_SPACES()
381 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
382 /* Loop the state's transitions. */
383 for ( StateCondList::Iter sc
= st
->stateCondList
; sc
.lte(); sc
++ ) {
385 out
<< sc
->condSpace
->condSpaceId
<< ", ";
386 if ( ++totalTrans
% IALL
== 0 )
391 /* Output one last number so we don't have to figure out when the last
392 * entry is and avoid writing a comma. */
397 std::ostream
&TabCodeGen::KEYS()
401 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
402 /* Loop the singles. */
403 for ( RedTransList::Iter stel
= st
->outSingle
; stel
.lte(); stel
++ ) {
404 out
<< KEY( stel
->lowKey
) << ", ";
405 if ( ++totalTrans
% IALL
== 0 )
409 /* Loop the state's transitions. */
410 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
412 out
<< KEY( rtel
->lowKey
) << ", ";
413 if ( ++totalTrans
% IALL
== 0 )
417 out
<< KEY( rtel
->highKey
) << ", ";
418 if ( ++totalTrans
% IALL
== 0 )
423 /* Output one last number so we don't have to figure out when the last
424 * entry is and avoid writing a comma. */
429 std::ostream
&TabCodeGen::INDICIES()
433 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
434 /* Walk the singles. */
435 for ( RedTransList::Iter stel
= st
->outSingle
; stel
.lte(); stel
++ ) {
436 out
<< stel
->value
->id
<< ", ";
437 if ( ++totalTrans
% IALL
== 0 )
441 /* Walk the ranges. */
442 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
443 out
<< rtel
->value
->id
<< ", ";
444 if ( ++totalTrans
% IALL
== 0 )
448 /* The state's default index goes next. */
449 if ( st
->defTrans
!= 0 ) {
450 out
<< st
->defTrans
->id
<< ", ";
451 if ( ++totalTrans
% IALL
== 0 )
456 /* Output one last number so we don't have to figure out when the last
457 * entry is and avoid writing a comma. */
462 std::ostream
&TabCodeGen::TRANS_TARGS()
466 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
467 /* Walk the singles. */
468 for ( RedTransList::Iter stel
= st
->outSingle
; stel
.lte(); stel
++ ) {
469 RedTransAp
*trans
= stel
->value
;
470 out
<< trans
->targ
->id
<< ", ";
471 if ( ++totalTrans
% IALL
== 0 )
475 /* Walk the ranges. */
476 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
477 RedTransAp
*trans
= rtel
->value
;
478 out
<< trans
->targ
->id
<< ", ";
479 if ( ++totalTrans
% IALL
== 0 )
483 /* The state's default target state. */
484 if ( st
->defTrans
!= 0 ) {
485 RedTransAp
*trans
= st
->defTrans
;
486 out
<< trans
->targ
->id
<< ", ";
487 if ( ++totalTrans
% IALL
== 0 )
492 /* Output one last number so we don't have to figure out when the last
493 * entry is and avoid writing a comma. */
499 std::ostream
&TabCodeGen::TRANS_ACTIONS()
503 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
504 /* Walk the singles. */
505 for ( RedTransList::Iter stel
= st
->outSingle
; stel
.lte(); stel
++ ) {
506 RedTransAp
*trans
= stel
->value
;
507 TRANS_ACTION( trans
) << ", ";
508 if ( ++totalTrans
% IALL
== 0 )
512 /* Walk the ranges. */
513 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
514 RedTransAp
*trans
= rtel
->value
;
515 TRANS_ACTION( trans
) << ", ";
516 if ( ++totalTrans
% IALL
== 0 )
520 /* The state's default index goes next. */
521 if ( st
->defTrans
!= 0 ) {
522 RedTransAp
*trans
= st
->defTrans
;
523 TRANS_ACTION( trans
) << ", ";
524 if ( ++totalTrans
% IALL
== 0 )
529 /* Output one last number so we don't have to figure out when the last
530 * entry is and avoid writing a comma. */
535 std::ostream
&TabCodeGen::TRANS_TARGS_WI()
537 /* Transitions must be written ordered by their id. */
538 RedTransAp
**transPtrs
= new RedTransAp
*[redFsm
->transSet
.length()];
539 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
540 transPtrs
[trans
->id
] = trans
;
542 /* Keep a count of the num of items in the array written. */
545 for ( int t
= 0; t
< redFsm
->transSet
.length(); t
++ ) {
546 /* Write out the target state. */
547 RedTransAp
*trans
= transPtrs
[t
];
548 out
<< trans
->targ
->id
;
549 if ( t
< redFsm
->transSet
.length()-1 ) {
551 if ( ++totalStates
% IALL
== 0 )
561 std::ostream
&TabCodeGen::TRANS_ACTIONS_WI()
563 /* Transitions must be written ordered by their id. */
564 RedTransAp
**transPtrs
= new RedTransAp
*[redFsm
->transSet
.length()];
565 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
566 transPtrs
[trans
->id
] = trans
;
568 /* Keep a count of the num of items in the array written. */
571 for ( int t
= 0; t
< redFsm
->transSet
.length(); t
++ ) {
572 /* Write the function for the transition. */
573 RedTransAp
*trans
= transPtrs
[t
];
574 TRANS_ACTION( trans
);
575 if ( t
< redFsm
->transSet
.length()-1 ) {
577 if ( ++totalAct
% IALL
== 0 )
586 void TabCodeGen::LOCATE_TRANS()
589 " _keys = " << ARR_OFF( K(), KO() + "[" + CS() + "]" ) << ";\n"
590 " _trans = " << IO() << "[" << CS() << "];\n"
592 " _klen = " << SL() << "[" << CS() << "];\n"
593 " if ( _klen > 0 ) {\n"
594 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
595 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
596 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + _klen - 1;\n"
598 " if ( _upper < _lower )\n"
601 " _mid = _lower + ((_upper-_lower) >> 1);\n"
602 " if ( " << GET_WIDE_KEY() << " < *_mid )\n"
603 " _upper = _mid - 1;\n"
604 " else if ( " << GET_WIDE_KEY() << " > *_mid )\n"
605 " _lower = _mid + 1;\n"
607 " _trans += (_mid - _keys);\n"
612 " _trans += _klen;\n"
615 " _klen = " << RL() << "[" << CS() << "];\n"
616 " if ( _klen > 0 ) {\n"
617 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
618 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
619 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
621 " if ( _upper < _lower )\n"
624 " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
625 " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
626 " _upper = _mid - 2;\n"
627 " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
628 " _lower = _mid + 2;\n"
630 " _trans += ((_mid - _keys)>>1);\n"
634 " _trans += _klen;\n"
639 void TabCodeGen::GOTO( ostream
&ret
, int gotoDest
, bool inFinish
)
641 ret
<< "{" << CS() << " = " << gotoDest
<< "; " <<
642 CTRL_FLOW() << "goto _again;}";
645 void TabCodeGen::GOTO_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
647 ret
<< "{" << CS() << " = (";
648 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
649 ret
<< "); " << CTRL_FLOW() << "goto _again;}";
652 void TabCodeGen::CURS( ostream
&ret
, bool inFinish
)
657 void TabCodeGen::TARGS( ostream
&ret
, bool inFinish
, int targState
)
659 ret
<< "(" << CS() << ")";
662 void TabCodeGen::NEXT( ostream
&ret
, int nextDest
, bool inFinish
)
664 ret
<< CS() << " = " << nextDest
<< ";";
667 void TabCodeGen::NEXT_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
669 ret
<< CS() << " = (";
670 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
674 void TabCodeGen::CALL( ostream
&ret
, int callDest
, int targState
, bool inFinish
)
676 if ( prePushExpr
!= 0 ) {
678 INLINE_LIST( ret
, prePushExpr
, 0, false );
681 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " <<
682 callDest
<< "; " << CTRL_FLOW() << "goto _again;}";
684 if ( prePushExpr
!= 0 )
688 void TabCodeGen::CALL_EXPR( ostream
&ret
, InlineItem
*ilItem
, int targState
, bool inFinish
)
690 if ( prePushExpr
!= 0 ) {
692 INLINE_LIST( ret
, prePushExpr
, 0, false );
695 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
696 INLINE_LIST( ret
, ilItem
->children
, targState
, inFinish
);
697 ret
<< "); " << CTRL_FLOW() << "goto _again;}";
699 if ( prePushExpr
!= 0 )
703 void TabCodeGen::RET( ostream
&ret
, bool inFinish
)
705 ret
<< "{" << CS() << " = " << STACK() << "[--" <<
708 if ( postPopExpr
!= 0 ) {
710 INLINE_LIST( ret
, postPopExpr
, 0, false );
714 ret
<< CTRL_FLOW() << "goto _again;}";
717 void TabCodeGen::BREAK( ostream
&ret
, int targState
)
720 ret
<< CTRL_FLOW() << "goto _out;";
723 void TabCodeGen::writeData()
725 /* If there are any transtion functions then output the array. If there
726 * are none, don't bother emitting an empty array that won't be used. */
727 if ( redFsm
->anyActions() ) {
728 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActArrItem
), A() );
734 if ( redFsm
->anyConditions() ) {
735 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondOffset
), CO() );
740 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondLen
), CL() );
745 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
750 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpaceId
), C() );
756 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxKeyOffset
), KO() );
761 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
766 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSingleLen
), SL() );
771 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxRangeLen
), RL() );
776 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndexOffset
), IO() );
782 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
787 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
792 if ( redFsm
->anyActions() ) {
793 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TA() );
800 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
805 if ( redFsm
->anyActions() ) {
806 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TA() );
813 if ( redFsm
->anyToStateActions() ) {
814 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
820 if ( redFsm
->anyFromStateActions() ) {
821 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
822 FROM_STATE_ACTIONS();
827 if ( redFsm
->anyEofActions() ) {
828 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), EA() );
834 if ( redFsm
->anyEofTrans() ) {
835 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
844 void TabCodeGen::COND_TRANSLATE()
847 " _widec = " << GET_KEY() << ";\n"
848 " _klen = " << CL() << "[" << CS() << "];\n"
849 " _keys = " << ARR_OFF( CK(), "(" + CO() + "[" + CS() + "]*2)" ) << ";\n"
850 " if ( _klen > 0 ) {\n"
851 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
852 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
853 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
855 " if ( _upper < _lower )\n"
858 " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
859 " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
860 " _upper = _mid - 2;\n"
861 " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
862 " _lower = _mid + 2;\n"
864 " switch ( " << C() << "[" << CO() << "[" << CS() << "]"
865 " + ((_mid - _keys)>>1)] ) {\n";
867 for ( CondSpaceList::Iter csi
= condSpaceList
; csi
.lte(); csi
++ ) {
868 CondSpace
*condSpace
= csi
;
869 out
<< " case " << condSpace
->condSpaceId
<< ": {\n";
870 out
<< TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
871 KEY(condSpace
->baseKey
) << " + (" << GET_KEY() <<
872 " - " << KEY(keyOps
->minKey
) << "));\n";
874 for ( CondSet::Iter csi
= condSpace
->condSet
; csi
.lte(); csi
++ ) {
875 out
<< TABS(2) << "if ( ";
876 CONDITION( out
, *csi
);
877 Size condValOffset
= ((1 << csi
.pos()) * keyOps
->alphSize());
878 out
<< " ) _widec += " << condValOffset
<< ";\n";
897 void TabCodeGen::writeExec()
900 outLabelUsed
= false;
906 if ( redFsm
->anyRegCurStateRef() )
911 " " << UINT() << " _trans;\n";
913 if ( redFsm
->anyConditions() )
914 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
916 if ( redFsm
->anyToStateActions() || redFsm
->anyRegActions()
917 || redFsm
->anyFromStateActions() )
920 " " << PTR_CONST() << ARRAY_TYPE(redFsm
->maxActArrItem
) <<
921 POINTER() << "_acts;\n"
922 " " << UINT() << " _nacts;\n";
926 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
932 " if ( " << P() << " == " << PE() << " )\n"
933 " goto _test_eof;\n";
936 if ( redFsm
->errState
!= 0 ) {
939 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
945 if ( redFsm
->anyFromStateActions() ) {
947 " _acts = " << ARR_OFF( A(), FSA() + "[" + CS() + "]" ) << ";\n"
948 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
949 " while ( _nacts-- > 0 ) {\n"
950 " switch ( *_acts++ ) {\n";
951 FROM_STATE_ACTION_SWITCH();
958 if ( redFsm
->anyConditions() )
966 out
<< " _trans = " << I() << "[_trans];\n";
968 if ( redFsm
->anyEofTrans() )
969 out
<< "_eof_trans:\n";
971 if ( redFsm
->anyRegCurStateRef() )
972 out
<< " _ps = " << CS() << ";\n";
975 " " << CS() << " = " << TT() << "[_trans];\n"
978 if ( redFsm
->anyRegActions() ) {
980 " if ( " << TA() << "[_trans] == 0 )\n"
983 " _acts = " << ARR_OFF( A(), TA() + "[_trans]" ) << ";\n"
984 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
985 " while ( _nacts-- > 0 )\n {\n"
986 " switch ( *_acts++ )\n {\n";
994 if ( redFsm
->anyRegActions() || redFsm
->anyActionGotos() ||
995 redFsm
->anyActionCalls() || redFsm
->anyActionRets() )
998 if ( redFsm
->anyToStateActions() ) {
1000 " _acts = " << ARR_OFF( A(), TSA() + "[" + CS() + "]" ) << ";\n"
1001 " _nacts = " << CAST(UINT()) << " *_acts++;\n"
1002 " while ( _nacts-- > 0 ) {\n"
1003 " switch ( *_acts++ ) {\n";
1004 TO_STATE_ACTION_SWITCH();
1011 if ( redFsm
->errState
!= 0 ) {
1012 outLabelUsed
= true;
1014 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
1020 " if ( ++" << P() << " != " << PE() << " )\n"
1025 " " << P() << " += 1;\n"
1030 out
<< " _test_eof: {}\n";
1032 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
1034 " if ( " << P() << " == " << EOFV() << " )\n"
1037 if ( redFsm
->anyEofTrans() ) {
1039 " if ( " << ET() << "[" << CS() << "] > 0 ) {\n"
1040 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
1041 " goto _eof_trans;\n"
1045 if ( redFsm
->anyEofActions() ) {
1047 " " << PTR_CONST() << ARRAY_TYPE(redFsm
->maxActArrItem
) <<
1048 POINTER() << "__acts = " <<
1049 ARR_OFF( A(), EA() + "[" + CS() + "]" ) << ";\n"
1050 " " << UINT() << " __nacts = " << CAST(UINT()) << " *__acts++;\n"
1051 " while ( __nacts-- > 0 ) {\n"
1052 " switch ( *__acts++ ) {\n";
1053 EOF_ACTION_SWITCH();
1065 out
<< " _out: {}\n";