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 ( 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 );
108 genLineDirective( 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 );
125 genLineDirective( 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 );
142 genLineDirective( 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 );
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 assert( st
->eofTrans
->pos
>= 0 );
339 trans
= st
->eofTrans
->pos
+1;
345 if ( ++totalStateNum
% IALL
== 0 )
354 std::ostream
&TabCodeGen::COND_KEYS()
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
++ ) {
362 out
<< KEY( sc
->lowKey
) << ", ";
363 if ( ++totalTrans
% IALL
== 0 )
367 out
<< KEY( sc
->highKey
) << ", ";
368 if ( ++totalTrans
% IALL
== 0 )
373 /* Output one last number so we don't have to figure out when the last
374 * entry is and avoid writing a comma. */
379 std::ostream
&TabCodeGen::COND_SPACES()
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
++ ) {
387 out
<< sc
->condSpace
->condSpaceId
<< ", ";
388 if ( ++totalTrans
% IALL
== 0 )
393 /* Output one last number so we don't have to figure out when the last
394 * entry is and avoid writing a comma. */
399 std::ostream
&TabCodeGen::KEYS()
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 )
411 /* Loop the state's transitions. */
412 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
414 out
<< KEY( rtel
->lowKey
) << ", ";
415 if ( ++totalTrans
% IALL
== 0 )
419 out
<< KEY( rtel
->highKey
) << ", ";
420 if ( ++totalTrans
% IALL
== 0 )
425 /* Output one last number so we don't have to figure out when the last
426 * entry is and avoid writing a comma. */
431 std::ostream
&TabCodeGen::INDICIES()
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 )
443 /* Walk the ranges. */
444 for ( RedTransList::Iter rtel
= st
->outRange
; rtel
.lte(); rtel
++ ) {
445 out
<< rtel
->value
->id
<< ", ";
446 if ( ++totalTrans
% IALL
== 0 )
450 /* The state's default index goes next. */
451 if ( st
->defTrans
!= 0 ) {
452 out
<< st
->defTrans
->id
<< ", ";
453 if ( ++totalTrans
% IALL
== 0 )
458 /* Output one last number so we don't have to figure out when the last
459 * entry is and avoid writing a comma. */
464 std::ostream
&TabCodeGen::TRANS_TARGS()
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 )
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 )
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 )
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 )
506 /* Output one last number so we don't have to figure out when the last
507 * entry is and avoid writing a comma. */
513 std::ostream
&TabCodeGen::TRANS_ACTIONS()
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 )
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 )
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 )
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 )
554 /* Output one last number so we don't have to figure out when the last
555 * entry is and avoid writing a comma. */
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. */
570 for ( int t
= 0; t
< redFsm
->transSet
.length(); t
++ ) {
571 /* Record the position, need this for eofTrans. */
572 RedTransAp
*trans
= transPtrs
[t
];
575 /* Write out the target state. */
576 out
<< trans
->targ
->id
;
577 if ( t
< redFsm
->transSet
.length()-1 ) {
579 if ( ++totalStates
% IALL
== 0 )
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. */
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 ) {
605 if ( ++totalAct
% IALL
== 0 )
614 void TabCodeGen::LOCATE_TRANS()
617 " _keys = " << ARR_OFF( K(), KO() + "[" + CS() + "]" ) << ";\n"
618 " _trans = " << IO() << "[" << CS() << "];\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"
626 " if ( _upper < _lower )\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"
635 " _trans += (_mid - _keys);\n"
640 " _trans += _klen;\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"
649 " if ( _upper < _lower )\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"
658 " _trans += ((_mid - _keys)>>1);\n"
662 " _trans += _klen;\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
)
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 );
702 void TabCodeGen::CALL( ostream
&ret
, int callDest
, int targState
, bool inFinish
)
704 if ( prePushExpr
!= 0 ) {
706 INLINE_LIST( ret
, prePushExpr
, 0, false, false );
709 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " <<
710 callDest
<< "; " << CTRL_FLOW() << "goto _again;}";
712 if ( prePushExpr
!= 0 )
716 void TabCodeGen::CALL_EXPR( ostream
&ret
, GenInlineItem
*ilItem
, int targState
, bool inFinish
)
718 if ( prePushExpr
!= 0 ) {
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 )
731 void TabCodeGen::RET( ostream
&ret
, bool inFinish
)
733 ret
<< "{" << CS() << " = " << STACK() << "[--" <<
736 if ( postPopExpr
!= 0 ) {
738 INLINE_LIST( ret
, postPopExpr
, 0, false, false );
742 ret
<< CTRL_FLOW() << "goto _again;}";
745 void TabCodeGen::BREAK( ostream
&ret
, int targState
, bool csForced
)
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() );
762 if ( redFsm
->anyConditions() ) {
763 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondOffset
), CO() );
768 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondLen
), CL() );
773 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
778 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpaceId
), C() );
784 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxKeyOffset
), KO() );
789 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
794 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSingleLen
), SL() );
799 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxRangeLen
), RL() );
804 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndexOffset
), IO() );
810 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
815 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
820 if ( redFsm
->anyActions() ) {
821 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TA() );
828 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
833 if ( redFsm
->anyActions() ) {
834 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TA() );
841 if ( redFsm
->anyToStateActions() ) {
842 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
848 if ( redFsm
->anyFromStateActions() ) {
849 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
850 FROM_STATE_ACTIONS();
855 if ( redFsm
->anyEofActions() ) {
856 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), EA() );
862 if ( redFsm
->anyEofTrans() ) {
863 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndexOffset
+1), ET() );
872 void TabCodeGen::COND_TRANSLATE()
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"
883 " if ( _upper < _lower )\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"
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";
925 void TabCodeGen::writeExec()
928 outLabelUsed
= false;
934 if ( redFsm
->anyRegCurStateRef() )
939 " " << UINT() << " _trans;\n";
941 if ( redFsm
->anyConditions() )
942 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
944 if ( redFsm
->anyToStateActions() || redFsm
->anyRegActions()
945 || redFsm
->anyFromStateActions() )
948 " " << PTR_CONST() << ARRAY_TYPE(redFsm
->maxActArrItem
) <<
949 POINTER() << "_acts;\n"
950 " " << UINT() << " _nacts;\n";
954 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
960 " if ( " << P() << " == " << PE() << " )\n"
961 " goto _test_eof;\n";
964 if ( redFsm
->errState
!= 0 ) {
967 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
973 if ( redFsm
->anyFromStateActions() ) {
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();
986 if ( redFsm
->anyConditions() )
994 out
<< " _trans = " << I() << "[_trans];\n";
996 if ( redFsm
->anyEofTrans() )
997 out
<< "_eof_trans:\n";
999 if ( redFsm
->anyRegCurStateRef() )
1000 out
<< " _ps = " << CS() << ";\n";
1003 " " << CS() << " = " << TT() << "[_trans];\n"
1006 if ( redFsm
->anyRegActions() ) {
1008 " if ( " << TA() << "[_trans] == 0 )\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";
1022 if ( redFsm
->anyRegActions() || redFsm
->anyActionGotos() ||
1023 redFsm
->anyActionCalls() || redFsm
->anyActionRets() )
1026 if ( redFsm
->anyToStateActions() ) {
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();
1039 if ( redFsm
->errState
!= 0 ) {
1040 outLabelUsed
= true;
1042 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
1048 " if ( ++" << P() << " != " << PE() << " )\n"
1053 " " << P() << " += 1;\n"
1058 out
<< " _test_eof: {}\n";
1060 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
1062 " if ( " << P() << " == " << EOFV() << " )\n"
1065 if ( redFsm
->anyEofTrans() ) {
1067 " if ( " << ET() << "[" << CS() << "] > 0 ) {\n"
1068 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
1069 " goto _eof_trans;\n"
1073 if ( redFsm
->anyEofActions() ) {
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();
1093 out
<< " _out: {}\n";