2 * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
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 ( GenActionList::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 ( GenActionList::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 ( GenActionList::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 ( GenActionList::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 assert( st->eofTrans->pos >= 0 );
220 trans = st->eofTrans->pos+1;
223 /* Write any eof action. */
224 ARRAY_ITEM( INT(trans), ++totalStateNum, st.last() );
230 std::ostream &RubyFlatCodeGen::TRANS_TARGS()
232 /* Transitions must be written ordered by their id. */
233 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
234 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
235 transPtrs[trans->id] = trans;
237 /* Keep a count of the num of items in the array written. */
241 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
242 /* Save the position. Needed for eofTargs. */
243 RedTransAp *trans = transPtrs[t];
246 /* Write out the target state. */
247 ARRAY_ITEM( INT( trans->targ->id ), ++totalStates, t >= redFsm->transSet.length()-1 );
255 std::ostream &RubyFlatCodeGen::TRANS_ACTIONS()
257 /* Transitions must be written ordered by their id. */
258 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
259 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
260 transPtrs[trans->id] = trans;
262 /* Keep a count of the num of items in the array written. */
265 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
266 /* Write the function for the transition. */
267 RedTransAp *trans = transPtrs[t];
268 ARRAY_ITEM( INT( TRANS_ACTION( trans ) ), ++totalAct, t >= redFsm->transSet.length()-1 );
276 void RubyFlatCodeGen::LOCATE_TRANS()
279 " _keys = " << CS() << " << 1\n"
280 " _inds = " << IO() << "[" << CS() << "]\n"
281 " _slen = " << SP() << "[" << CS() << "]\n"
282 " _trans = if ( _slen > 0 && \n"
283 " " << K() << "[_keys] <= " << GET_WIDE_KEY() << " && \n"
284 " " << GET_WIDE_KEY() << " <= " << K() << "[_keys + 1] \n"
286 " " << I() << "[ _inds + " << GET_WIDE_KEY() << " - " << K() << "[_keys] ] \n"
288 " " << I() << "[ _inds + _slen ]\n"
294 std::ostream &RubyFlatCodeGen::COND_INDEX_OFFSET()
297 int totalStateNum = 0, curIndOffset = 0;
298 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
299 /* Write the index offset. */
300 ARRAY_ITEM( INT( curIndOffset ), ++totalStateNum, st.last() );
301 /* Move the index offset ahead. */
302 if ( st->condList != 0 )
303 curIndOffset += keyOps->span( st->condLowKey, st->condHighKey );
309 void RubyFlatCodeGen::COND_TRANSLATE()
312 " _widec = " << GET_KEY() << "\n"
313 " _keys = " << CS() << " << 1\n"
314 " _conds = " << CO() << "[" << CS() << "]\n"
315 " _slen = " << CSP() << "[" << CS() << "]\n"
316 " _cond = if ( _slen > 0 && \n"
317 " " << CK() << "[_keys] <= " << GET_WIDE_KEY() << " &&\n"
318 " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys + 1]\n"
320 " " << C() << "[ _conds + " << GET_WIDE_KEY() << " - " << CK() << "[_keys]" << " ]\n"
326 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
327 GenCondSpace *condSpace = csi;
328 out << " when " << condSpace->condSpaceId + 1 << " then\n";
329 out << TABS(2) << "_widec = " << "(" <<
330 KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
331 " - " << KEY(keyOps->minKey) << "))\n";
333 for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
334 out << TABS(2) << "if ( ";
335 CONDITION( out, *csi );
336 Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
339 TABS(3) << " _widec += " << condValOffset << "\n"
345 " end # _cond switch \n";
348 std::ostream &RubyFlatCodeGen::CONDS()
352 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
353 if ( st->condList != 0 ) {
354 /* Walk the singles. */
355 unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
356 for ( unsigned long long pos = 0; pos < span; pos++ ) {
357 if ( st->condList[pos] != 0 )
358 ARRAY_ITEM( INT( st->condList[pos]->condSpaceId + 1 ), ++totalTrans, false );
360 ARRAY_ITEM( INT( 0 ), ++totalTrans, false );
365 /* Output one last number so we don't have to figure out when the last
366 * entry is and avoid writing a comma. */
367 ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
372 std::ostream &RubyFlatCodeGen::COND_KEYS()
376 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
377 /* Emit just cond low key and cond high key. */
378 ARRAY_ITEM( KEY( st->condLowKey ), ++totalTrans, false );
379 ARRAY_ITEM( KEY( st->condHighKey ), ++totalTrans, false );
382 /* Output one last number so we don't have to figure out when the last
383 * entry is and avoid writing a comma. */
384 ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
389 std::ostream &RubyFlatCodeGen::COND_KEY_SPANS()
392 int totalStateNum = 0;
393 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
394 /* Write singles length. */
395 unsigned long long span = 0;
396 if ( st->condList != 0 )
397 span = keyOps->span( st->condLowKey, st->condHighKey );
398 ARRAY_ITEM( INT( span ), ++totalStateNum, false );
405 void RubyFlatCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
409 " " << CS() << " = " << gotoDest << "\n"
410 " _trigger_goto = true\n"
411 " _goto_level = _again\n"
416 void RubyFlatCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
418 if ( prePushExpr != 0 ) {
420 INLINE_LIST( out, prePushExpr, 0, false );
425 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
426 " " << TOP() << "+= 1\n"
427 " " << CS() << " = " << callDest << "\n"
428 " _trigger_goto = true\n"
429 " _goto_level = _again\n"
433 if ( prePushExpr != 0 )
437 void RubyFlatCodeGen::CALL_EXPR(ostream &out, GenInlineItem *ilItem, int targState, bool inFinish )
439 if ( prePushExpr != 0 ) {
441 INLINE_LIST( out, prePushExpr, 0, false );
446 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
447 " " << TOP() << " += 1\n"
448 " " << CS() << " = (";
449 INLINE_LIST( out, ilItem->children, targState, inFinish );
453 " _trigger_goto = true\n"
454 " _goto_level = _again\n"
458 if ( prePushExpr != 0 )
462 void RubyFlatCodeGen::RET( ostream &out, bool inFinish )
466 " " << TOP() << " -= 1\n"
467 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
469 if ( postPopExpr != 0 ) {
471 INLINE_LIST( out, postPopExpr, 0, false );
476 " _trigger_goto = true\n"
477 " _goto_level = _again\n"
482 void RubyFlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
484 ret << CS() << " = " << nextDest << ";";
487 void RubyFlatCodeGen::GOTO_EXPR( ostream &out, GenInlineItem *ilItem, bool inFinish )
491 " " << CS() << " = (";
492 INLINE_LIST( out, ilItem->children, 0, inFinish );
495 " _trigger_goto = true\n"
496 " _goto_level = _again\n"
501 void RubyFlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
503 ret << CS() << " = (";
504 INLINE_LIST( ret, ilItem->children, 0, inFinish );
509 void RubyFlatCodeGen::CURS( ostream &ret, bool inFinish )
514 void RubyFlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
516 ret << "(" << CS() << ")";
519 void RubyFlatCodeGen::BREAK( ostream &out, int targState )
523 " " << P() << " += 1\n"
524 " _trigger_goto = true\n"
525 " _goto_level = _out\n"
530 int RubyFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
533 if ( state->toStateAction != 0 )
534 act = state->toStateAction->location+1;
538 int RubyFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
541 if ( state->fromStateAction != 0 )
542 act = state->fromStateAction->location+1;
546 int RubyFlatCodeGen::EOF_ACTION( RedStateAp *state )
549 if ( state->eofAction != 0 )
550 act = state->eofAction->location+1;
554 int RubyFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
556 /* If there are actions, emit them. Otherwise emit zero. */
558 if ( trans->action != 0 )
559 act = trans->action->location+1;
563 void RubyFlatCodeGen::writeData()
565 /* If there are any transtion functions then output the array. If there
566 * are none, don't bother emitting an empty array that won't be used. */
567 if ( redFsm->anyActions() ) {
568 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
574 if ( redFsm->anyConditions() ) {
575 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
580 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
585 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
590 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
596 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
601 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
606 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
611 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
616 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
621 if ( redFsm->anyActions() ) {
622 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
628 if ( redFsm->anyToStateActions() ) {
629 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
635 if ( redFsm->anyFromStateActions() ) {
636 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
637 FROM_STATE_ACTIONS();
642 if ( redFsm->anyEofActions() ) {
643 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
649 if ( redFsm->anyEofTrans() ) {
650 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
659 void RubyFlatCodeGen::writeExec()
662 "begin # ragel flat\n"
664 " _slen, _trans, _keys, _inds";
665 if ( redFsm->anyRegCurStateRef() )
667 if ( redFsm->anyConditions() )
668 out << ", _cond, _conds, _widec";
669 if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
670 || redFsm->anyFromStateActions() )
671 out << ", _acts, _nacts";
685 " _trigger_goto = false\n"
686 " if _goto_level <= 0\n";
690 " if " << P() << " == " << PE() << "\n"
691 " _goto_level = _test_eof\n"
696 if ( redFsm->errState != 0 ) {
698 " if " << CS() << " == " << redFsm->errState->id << "\n"
699 " _goto_level = _out\n"
704 /* The resume label. */
707 " if _goto_level <= _resume\n";
709 if ( redFsm->anyFromStateActions() ) {
711 " _acts = " << FSA() << "[" << CS() << "]\n"
712 " _nacts = " << A() << "[_acts]\n"
714 " while _nacts > 0\n"
717 " case " << A() << "[_acts - 1]\n";
718 FROM_STATE_ACTION_SWITCH();
720 " end # from state action switch\n"
722 " if _trigger_goto\n"
727 if ( redFsm->anyConditions() )
732 if ( redFsm->anyEofTrans() ) {
735 " if _goto_level <= _eof_trans\n";
738 if ( redFsm->anyRegCurStateRef() )
739 out << " _ps = " << CS() << "\n";
741 out << " " << CS() << " = " << TT() << "[_trans]\n";
743 if ( redFsm->anyRegActions() ) {
745 " if " << TA() << "[_trans] != 0\n"
746 " _acts = " << TA() << "[_trans]\n"
747 " _nacts = " << A() << "[_acts]\n"
749 " while _nacts > 0\n"
752 " case " << A() << "[_acts - 1]\n";
755 " end # action switch\n"
758 " if _trigger_goto\n"
763 /* The again label. */
766 " if _goto_level <= _again\n";
768 if ( redFsm->anyToStateActions() ) {
770 " _acts = " << TSA() << "[" << CS() << "]\n"
771 " _nacts = " << A() << "[_acts]\n"
773 " while _nacts > 0\n"
776 " case " << A() << "[_acts - 1]\n";
777 TO_STATE_ACTION_SWITCH() <<
778 " end # to state action switch\n"
780 " if _trigger_goto\n"
785 if ( redFsm->errState != 0 ) {
787 " if " << CS() << " == " << redFsm->errState->id << "\n"
788 " _goto_level = _out\n"
793 out << " " << P() << " += 1\n";
797 " if " << P() << " != " << PE() << "\n"
798 " _goto_level = _resume\n"
804 " _goto_level = _resume\n"
808 /* The test_eof label. */
811 " if _goto_level <= _test_eof\n";
813 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
815 " if " << P() << " == " << EOFV() << "\n";
817 if ( redFsm->anyEofTrans() ) {
819 " if " << ET() << "[" << CS() << "] > 0\n"
820 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
821 " _goto_level = _eof_trans\n"
826 if ( redFsm->anyEofActions() ) {
829 " __acts = " << EA() << "[" << CS() << "]\n"
830 " __nacts = " << A() << "[__acts]\n" <<
832 " while ( __nacts > 0 ) \n"
835 " case ( "<< A() << "[__acts-1] ) \n";
836 EOF_ACTION_SWITCH() <<
839 " if _trigger_goto\n"
851 " if _goto_level <= _out\n"
855 /* The loop for faking goto. */
859 /* Wrapping the execute block. */
868 * indent-tabs-mode: 1
869 * c-file-style: "bsd"