2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3 * Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
6 /* This file is part of Ragel.
8 * Ragel is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Ragel is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Ragel; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ruby-flatcodegen.h"
24 #include "rlgen-ruby.h"
31 std::ostream
&RubyFlatCodeGen::TO_STATE_ACTION_SWITCH()
33 /* Walk the list of functions, printing the cases. */
34 for ( ActionList::Iter act
= actionList
; act
.lte(); act
++ ) {
35 /* Write out referenced actions. */
36 if ( act
->numToStateRefs
> 0 ) {
37 /* Write the case label, the action and the case break */
38 out
<< "\twhen " << act
->actionId
<< " then\n";
39 ACTION( out
, act
, 0, false );
43 genLineDirective( out
);
47 std::ostream
&RubyFlatCodeGen::FROM_STATE_ACTION_SWITCH()
49 /* Walk the list of functions, printing the cases. */
50 for ( ActionList::Iter act
= actionList
; act
.lte(); act
++ ) {
51 /* Write out referenced actions. */
52 if ( act
->numFromStateRefs
> 0 ) {
53 /* Write the case label, the action and the case break */
54 out
<< "\twhen " << act
->actionId
<< " then\n";
55 ACTION( out
, act
, 0, false );
59 genLineDirective( out
);
63 std::ostream
&RubyFlatCodeGen::EOF_ACTION_SWITCH()
65 /* Walk the list of functions, printing the cases. */
66 for ( ActionList::Iter act
= actionList
; act
.lte(); act
++ ) {
67 /* Write out referenced actions. */
68 if ( act
->numEofRefs
> 0 ) {
69 /* Write the case label, the action and the case break */
70 out
<< "\twhen " << act
->actionId
<< " then\n";
71 ACTION( out
, act
, 0, true );
75 genLineDirective( out
);
79 std::ostream
&RubyFlatCodeGen::ACTION_SWITCH()
81 /* Walk the list of functions, printing the cases. */
82 for ( ActionList::Iter act
= actionList
; act
.lte(); act
++ ) {
83 /* Write out referenced actions. */
84 if ( act
->numTransRefs
> 0 ) {
85 /* Write the case label, the action and the case break */
86 out
<< "\twhen " << act
->actionId
<< " then\n";
87 ACTION( out
, act
, 0, false );
91 genLineDirective( out
);
96 std::ostream
&RubyFlatCodeGen::KEYS()
100 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
101 /* Emit just low key and high key. */
102 ARRAY_ITEM( KEY( st
->lowKey
), ++totalTrans
, false );
103 ARRAY_ITEM( KEY( st
->highKey
), ++totalTrans
, false );
104 if ( ++totalTrans
% IALL
== 0 )
109 /* Output one last number so we don't have to figure out when the last
110 * entry is and avoid writing a comma. */
111 ARRAY_ITEM( INT( 0 ), ++totalTrans
, true );
116 std::ostream
&RubyFlatCodeGen::INDICIES()
120 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
121 if ( st
->transList
!= 0 ) {
122 /* Walk the singles. */
123 unsigned long long span
= keyOps
->span( st
->lowKey
, st
->highKey
);
124 for ( unsigned long long pos
= 0; pos
< span
; pos
++ ) {
125 ARRAY_ITEM( KEY( st
->transList
[pos
]->id
), ++totalTrans
, false );
129 /* The state's default index goes next. */
130 if ( st
->defTrans
!= 0 )
131 ARRAY_ITEM( KEY( st
->defTrans
->id
), ++totalTrans
, false );
134 /* Output one last number so we don't have to figure out when the last
135 * entry is and avoid writing a comma. */
136 ARRAY_ITEM( INT( 0 ), ++totalTrans
, true );
141 std::ostream
&RubyFlatCodeGen::FLAT_INDEX_OFFSET()
144 int totalStateNum
= 0, curIndOffset
= 0;
145 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
146 /* Write the index offset. */
147 ARRAY_ITEM( INT( curIndOffset
), ++totalStateNum
, st
.last() );
148 /* Move the index offset ahead. */
149 if ( st
->transList
!= 0 )
150 curIndOffset
+= keyOps
->span( st
->lowKey
, st
->highKey
);
152 if ( st
->defTrans
!= 0 )
160 std::ostream
&RubyFlatCodeGen::KEY_SPANS()
163 int totalStateNum
= 0;
164 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
165 /* Write singles length. */
166 unsigned long long span
= 0;
167 if ( st
->transList
!= 0 )
168 span
= keyOps
->span( st
->lowKey
, st
->highKey
);
169 ARRAY_ITEM( INT( span
), ++totalStateNum
, st
.last() );
175 std::ostream
&RubyFlatCodeGen::TO_STATE_ACTIONS()
178 int totalStateNum
= 0;
179 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
180 /* Write any eof action. */
181 ARRAY_ITEM( INT( TO_STATE_ACTION(st
) ), ++totalStateNum
, st
.last() );
187 std::ostream
&RubyFlatCodeGen::FROM_STATE_ACTIONS()
190 int totalStateNum
= 0;
191 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
192 /* Write any eof action. */
193 ARRAY_ITEM( INT( FROM_STATE_ACTION(st
) ), ++totalStateNum
, st
.last() );
199 std::ostream
&RubyFlatCodeGen::EOF_ACTIONS()
202 int totalStateNum
= 0;
203 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
204 /* Write any eof action. */
205 ARRAY_ITEM( INT( EOF_ACTION(st
) ), ++totalStateNum
, st
.last() );
211 std::ostream
&RubyFlatCodeGen::EOF_TRANS()
214 int totalStateNum
= 0;
215 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
216 /* Write any eof action. */
218 if ( st
->eofTrans
!= 0 )
219 trans
= st
->eofTrans
->id
+1;
221 /* Write any eof action. */
222 ARRAY_ITEM( INT(trans
), ++totalStateNum
, st
.last() );
228 std::ostream
&RubyFlatCodeGen::TRANS_TARGS()
230 /* Transitions must be written ordered by their id. */
231 RedTransAp
**transPtrs
= new RedTransAp
*[redFsm
->transSet
.length()];
232 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
233 transPtrs
[trans
->id
] = trans
;
235 /* Keep a count of the num of items in the array written. */
239 for ( int t
= 0; t
< redFsm
->transSet
.length(); t
++ ) {
240 /* Write out the target state. */
241 RedTransAp
*trans
= transPtrs
[t
];
242 ARRAY_ITEM( INT( trans
->targ
->id
), ++totalStates
, t
>= redFsm
->transSet
.length()-1 );
250 std::ostream
&RubyFlatCodeGen::TRANS_ACTIONS()
252 /* Transitions must be written ordered by their id. */
253 RedTransAp
**transPtrs
= new RedTransAp
*[redFsm
->transSet
.length()];
254 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
255 transPtrs
[trans
->id
] = trans
;
257 /* Keep a count of the num of items in the array written. */
260 for ( int t
= 0; t
< redFsm
->transSet
.length(); t
++ ) {
261 /* Write the function for the transition. */
262 RedTransAp
*trans
= transPtrs
[t
];
263 ARRAY_ITEM( INT( TRANS_ACTION( trans
) ), ++totalAct
, t
>= redFsm
->transSet
.length()-1 );
271 void RubyFlatCodeGen::LOCATE_TRANS()
274 " _keys = " << CS() << " << 1\n"
275 " _inds = " << IO() << "[" << CS() << "]\n"
276 " _slen = " << SP() << "[" << CS() << "]\n"
277 " _trans = if ( _slen > 0 && \n"
278 " " << K() << "[_keys] <= " << GET_WIDE_KEY() << " && \n"
279 " " << GET_WIDE_KEY() << " <= " << K() << "[_keys + 1] \n"
281 " " << I() << "[ _inds + " << GET_WIDE_KEY() << " - " << K() << "[_keys] ] \n"
283 " " << I() << "[ _inds + _slen ]\n"
289 std::ostream
&RubyFlatCodeGen::COND_INDEX_OFFSET()
292 int totalStateNum
= 0, curIndOffset
= 0;
293 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
294 /* Write the index offset. */
295 ARRAY_ITEM( INT( curIndOffset
), ++totalStateNum
, st
.last() );
296 /* Move the index offset ahead. */
297 if ( st
->condList
!= 0 )
298 curIndOffset
+= keyOps
->span( st
->condLowKey
, st
->condHighKey
);
304 void RubyFlatCodeGen::COND_TRANSLATE()
307 " _widec = " << GET_KEY() << "\n"
308 " _keys = " << CS() << " << 1\n"
309 " _conds = " << CO() << "[" << CS() << "]\n"
310 " _slen = " << CSP() << "[" << CS() << "]\n"
311 " _cond = if ( _slen > 0 && \n"
312 " " << CK() << "[_keys] <= " << GET_WIDE_KEY() << " &&\n"
313 " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys + 1]\n"
315 " " << C() << "[ _conds + " << GET_WIDE_KEY() << " - " << CK() << "[_keys]" << " ]\n"
321 for ( CondSpaceList::Iter csi
= condSpaceList
; csi
.lte(); csi
++ ) {
322 CondSpace
*condSpace
= csi
;
323 out
<< " when " << condSpace
->condSpaceId
+ 1 << " then\n";
324 out
<< TABS(2) << "_widec = " << "(" <<
325 KEY(condSpace
->baseKey
) << " + (" << GET_KEY() <<
326 " - " << KEY(keyOps
->minKey
) << "))\n";
328 for ( CondSet::Iter csi
= condSpace
->condSet
; csi
.lte(); csi
++ ) {
329 out
<< TABS(2) << "if ( ";
330 CONDITION( out
, *csi
);
331 Size condValOffset
= ((1 << csi
.pos()) * keyOps
->alphSize());
334 TABS(3) << " _widec += " << condValOffset
<< "\n"
340 " end # _cond switch \n";
343 std::ostream
&RubyFlatCodeGen::CONDS()
347 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
348 if ( st
->condList
!= 0 ) {
349 /* Walk the singles. */
350 unsigned long long span
= keyOps
->span( st
->condLowKey
, st
->condHighKey
);
351 for ( unsigned long long pos
= 0; pos
< span
; pos
++ ) {
352 if ( st
->condList
[pos
] != 0 )
353 ARRAY_ITEM( INT( st
->condList
[pos
]->condSpaceId
+ 1 ), ++totalTrans
, false );
355 ARRAY_ITEM( INT( 0 ), ++totalTrans
, false );
360 /* Output one last number so we don't have to figure out when the last
361 * entry is and avoid writing a comma. */
362 ARRAY_ITEM( INT( 0 ), ++totalTrans
, true );
367 std::ostream
&RubyFlatCodeGen::COND_KEYS()
371 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
372 /* Emit just cond low key and cond high key. */
373 ARRAY_ITEM( KEY( st
->condLowKey
), ++totalTrans
, false );
374 ARRAY_ITEM( KEY( st
->condHighKey
), ++totalTrans
, false );
377 /* Output one last number so we don't have to figure out when the last
378 * entry is and avoid writing a comma. */
379 ARRAY_ITEM( INT( 0 ), ++totalTrans
, true );
384 std::ostream
&RubyFlatCodeGen::COND_KEY_SPANS()
387 int totalStateNum
= 0;
388 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
389 /* Write singles length. */
390 unsigned long long span
= 0;
391 if ( st
->condList
!= 0 )
392 span
= keyOps
->span( st
->condLowKey
, st
->condHighKey
);
393 ARRAY_ITEM( INT( span
), ++totalStateNum
, false );
400 void RubyFlatCodeGen::GOTO( ostream
&out
, int gotoDest
, bool inFinish
)
404 " " << CS() << " = " << gotoDest
<< "\n"
405 " _trigger_goto = true\n"
406 " _goto_level = _again\n"
411 void RubyFlatCodeGen::CALL( ostream
&out
, int callDest
, int targState
, bool inFinish
)
413 if ( prePushExpr
!= 0 ) {
415 INLINE_LIST( out
, prePushExpr
, 0, false );
420 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
421 " " << TOP() << "+= 1\n"
422 " " << CS() << " = " << callDest
<< "\n"
423 " _trigger_goto = true\n"
424 " _goto_level = _again\n"
428 if ( prePushExpr
!= 0 )
432 void RubyFlatCodeGen::CALL_EXPR(ostream
&out
, InlineItem
*ilItem
, int targState
, bool inFinish
)
434 if ( prePushExpr
!= 0 ) {
436 INLINE_LIST( out
, prePushExpr
, 0, false );
441 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
442 " " << TOP() << " += 1\n"
443 " " << CS() << " = (";
444 INLINE_LIST( out
, ilItem
->children
, targState
, inFinish
);
448 " _trigger_goto = true\n"
449 " _goto_level = _again\n"
453 if ( prePushExpr
!= 0 )
457 void RubyFlatCodeGen::RET( ostream
&out
, bool inFinish
)
461 " " << TOP() << " -= 1\n"
462 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
464 if ( postPopExpr
!= 0 ) {
466 INLINE_LIST( out
, postPopExpr
, 0, false );
471 " _trigger_goto = true\n"
472 " _goto_level = _again\n"
477 void RubyFlatCodeGen::NEXT( ostream
&ret
, int nextDest
, bool inFinish
)
479 ret
<< CS() << " = " << nextDest
<< ";";
482 void RubyFlatCodeGen::GOTO_EXPR( ostream
&out
, InlineItem
*ilItem
, bool inFinish
)
486 " " << CS() << " = (";
487 INLINE_LIST( out
, ilItem
->children
, 0, inFinish
);
490 " _trigger_goto = true\n"
491 " _goto_level = _again\n"
496 void RubyFlatCodeGen::NEXT_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
498 ret
<< CS() << " = (";
499 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
504 void RubyFlatCodeGen::CURS( ostream
&ret
, bool inFinish
)
509 void RubyFlatCodeGen::TARGS( ostream
&ret
, bool inFinish
, int targState
)
511 ret
<< "(" << CS() << ")";
514 void RubyFlatCodeGen::BREAK( ostream
&out
, int targState
)
518 " _trigger_goto = true\n"
519 " _goto_level = _out\n"
524 int RubyFlatCodeGen::TO_STATE_ACTION( RedStateAp
*state
)
527 if ( state
->toStateAction
!= 0 )
528 act
= state
->toStateAction
->location
+1;
532 int RubyFlatCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
535 if ( state
->fromStateAction
!= 0 )
536 act
= state
->fromStateAction
->location
+1;
540 int RubyFlatCodeGen::EOF_ACTION( RedStateAp
*state
)
543 if ( state
->eofAction
!= 0 )
544 act
= state
->eofAction
->location
+1;
548 int RubyFlatCodeGen::TRANS_ACTION( RedTransAp
*trans
)
550 /* If there are actions, emit them. Otherwise emit zero. */
552 if ( trans
->action
!= 0 )
553 act
= trans
->action
->location
+1;
557 void RubyFlatCodeGen::writeData()
559 /* If there are any transtion functions then output the array. If there
560 * are none, don't bother emitting an empty array that won't be used. */
561 if ( redFsm
->anyActions() ) {
562 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActArrItem
), A() );
568 if ( redFsm
->anyConditions() ) {
569 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
574 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpan
), CSP() );
579 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCond
), C() );
584 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondIndexOffset
), CO() );
590 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
595 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSpan
), SP() );
600 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxFlatIndexOffset
), IO() );
605 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
610 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
615 if ( redFsm
->anyActions() ) {
616 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TA() );
622 if ( redFsm
->anyToStateActions() ) {
623 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
629 if ( redFsm
->anyFromStateActions() ) {
630 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
631 FROM_STATE_ACTIONS();
636 if ( redFsm
->anyEofActions() ) {
637 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), EA() );
643 if ( redFsm
->anyEofTrans() ) {
644 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
653 void RubyFlatCodeGen::writeExec()
656 "begin # ragel flat\n"
658 " _slen, _trans, _keys, _inds";
659 if ( redFsm
->anyRegCurStateRef() )
661 if ( redFsm
->anyConditions() )
662 out
<< ", _cond, _conds, _widec";
663 if ( redFsm
->anyToStateActions() || redFsm
->anyRegActions()
664 || redFsm
->anyFromStateActions() )
665 out
<< ", _acts, _nacts";
679 " _trigger_goto = false\n"
680 " if _goto_level <= 0\n";
684 " if " << P() << " == " << PE() << "\n"
685 " _goto_level = _test_eof\n"
690 if ( redFsm
->errState
!= 0 ) {
692 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
693 " _goto_level = _out\n"
698 /* The resume label. */
701 " if _goto_level <= _resume\n";
703 if ( redFsm
->anyFromStateActions() ) {
705 " _acts = " << FSA() << "[" << CS() << "]\n"
706 " _nacts = " << A() << "[_acts]\n"
708 " while _nacts > 0\n"
711 " case " << A() << "[_acts - 1]\n";
712 FROM_STATE_ACTION_SWITCH();
714 " end # from state action switch\n"
716 " if _trigger_goto\n"
721 if ( redFsm
->anyConditions() )
726 if ( redFsm
->anyEofTrans() ) {
729 " if _goto_level <= _eof_trans\n";
732 if ( redFsm
->anyRegCurStateRef() )
733 out
<< " _ps = " << CS() << "\n";
735 out
<< " " << CS() << " = " << TT() << "[_trans]\n";
737 if ( redFsm
->anyRegActions() ) {
739 " if " << TA() << "[_trans] != 0\n"
740 " _acts = " << TA() << "[_trans]\n"
741 " _nacts = " << A() << "[_acts]\n"
743 " while _nacts > 0\n"
746 " case " << A() << "[_acts - 1]\n";
749 " end # action switch\n"
752 " if _trigger_goto\n"
757 /* The again label. */
760 " if _goto_level <= _again\n";
762 if ( redFsm
->anyToStateActions() ) {
764 " _acts = " << TSA() << "[" << CS() << "]\n"
765 " _nacts = " << A() << "[_acts]\n"
767 " while _nacts > 0\n"
770 " case " << A() << "[_acts - 1]\n";
771 TO_STATE_ACTION_SWITCH() <<
772 " end # to state action switch\n"
774 " if _trigger_goto\n"
779 if ( redFsm
->errState
!= 0 ) {
781 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
782 " _goto_level = _out\n"
787 out
<< " " << P() << " += 1\n";
791 " if " << P() << " != " << PE() << "\n"
792 " _goto_level = _resume\n"
798 " _goto_level = _resume\n"
802 /* The test_eof label. */
805 " if _goto_level <= _test_eof\n";
807 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
809 " if " << P() << " == " << EOFV() << "\n";
811 if ( redFsm
->anyEofTrans() ) {
813 " if " << ET() << "[" << CS() << "] > 0\n"
814 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
815 " _goto_level = _eof_trans\n"
820 if ( redFsm
->anyEofActions() ) {
823 " __acts = " << EA() << "[" << CS() << "]\n"
824 " __nacts = " << A() << "[__acts]\n" <<
826 " while ( __nacts > 0 ) \n"
829 " case ( "<< A() << "[__acts-1] ) \n";
830 EOF_ACTION_SWITCH() <<
833 " if _trigger_goto\n"
845 " if _goto_level <= _out\n"
849 /* The loop for faking goto. */
853 /* Wrapping the execute block. */
862 * indent-tabs-mode: 1
863 * c-file-style: "bsd"