2 * 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
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
27 #include "rlgen-ruby.h"
28 #include "ruby-ftabcodegen.h"
31 using std::ostringstream;
36 void RubyFTabCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
40 " " << CS() << " = " << gotoDest << "\n"
41 " _goto_level = _again\n"
46 void RubyFTabCodeGen::GOTO_EXPR( ostream &out, GenInlineItem *ilItem, bool inFinish )
50 " " << CS() << " = (";
51 INLINE_LIST( out, ilItem->children, 0, inFinish );
54 " _goto_level = _again\n"
59 void RubyFTabCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
61 if ( prePushExpr != 0 ) {
63 INLINE_LIST( out, prePushExpr, 0, false );
68 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
69 " " << TOP() << "+= 1\n"
70 " " << CS() << " = " << callDest << "\n"
71 " _goto_level = _again\n"
75 if ( prePushExpr != 0 )
79 void RubyFTabCodeGen::CALL_EXPR(ostream &out, GenInlineItem *ilItem,
80 int targState, bool inFinish )
82 if ( prePushExpr != 0 ) {
84 INLINE_LIST( out, prePushExpr, 0, false );
89 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
90 " " << TOP() << " += 1\n"
91 " " << CS() << " = (";
92 INLINE_LIST( out, ilItem->children, targState, inFinish );
96 " _goto_level = _again\n"
100 if ( prePushExpr != 0 )
104 void RubyFTabCodeGen::RET( ostream &out, bool inFinish )
108 " " << TOP() << " -= 1\n"
109 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
111 if ( postPopExpr != 0 ) {
113 INLINE_LIST( out, postPopExpr, 0, false );
118 " _goto_level = _again\n"
123 void RubyFTabCodeGen::BREAK( ostream &out, int targState )
127 " " << P() << " += 1\n"
128 " _goto_level = _out\n"
134 std::ostream &RubyFTabCodeGen::TO_STATE_ACTION_SWITCH()
136 /* Loop the actions. */
137 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
138 if ( redAct->numToStateRefs > 0 ) {
139 /* Write the entry label. */
140 out << "\twhen " << redAct->actListId+1 << " then\n";
142 /* Write each action in the list of action items. */
143 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
144 ACTION( out, item->value, 0, false );
149 genLineDirective( out );
153 /* Write out the function switch. This switch is keyed on the values
154 * of the func index. */
155 std::ostream &RubyFTabCodeGen::FROM_STATE_ACTION_SWITCH()
157 /* Loop the actions. */
158 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
159 if ( redAct->numFromStateRefs > 0 ) {
160 /* Write the entry label. */
161 out << "\twhen " << redAct->actListId+1 << " then\n";
163 /* Write each action in the list of action items. */
164 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
165 ACTION( out, item->value, 0, false );
170 genLineDirective( out );
174 std::ostream &RubyFTabCodeGen::EOF_ACTION_SWITCH()
176 /* Loop the actions. */
177 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
178 if ( redAct->numEofRefs > 0 ) {
179 /* Write the entry label. */
180 out << "\twhen " << redAct->actListId+1 << " then\n";
182 /* Write each action in the list of action items. */
183 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
184 ACTION( out, item->value, 0, true );
189 genLineDirective( out );
193 /* Write out the function switch. This switch is keyed on the values
194 * of the func index. */
195 std::ostream &RubyFTabCodeGen::ACTION_SWITCH()
197 /* Loop the actions. */
198 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
199 if ( redAct->numTransRefs > 0 ) {
200 /* Write the entry label. */
201 out << "\twhen " << redAct->actListId+1 << " then\n";
203 /* Write each action in the list of action items. */
204 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
205 ACTION( out, item->value, 0, false );
210 genLineDirective( out );
215 int RubyFTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
218 if ( state->toStateAction != 0 )
219 act = state->toStateAction->actListId+1;
223 int RubyFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
226 if ( state->fromStateAction != 0 )
227 act = state->fromStateAction->actListId+1;
231 int RubyFTabCodeGen::EOF_ACTION( RedStateAp *state )
234 if ( state->eofAction != 0 )
235 act = state->eofAction->actListId+1;
240 /* Write out the function for a transition. */
241 int RubyFTabCodeGen::TRANS_ACTION( RedTransAp *trans )
244 if ( trans->action != 0 )
245 action = trans->action->actListId+1;
249 void RubyFTabCodeGen::writeData()
252 if ( redFsm->anyConditions() ) {
253 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
258 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
263 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
268 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
274 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
279 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
284 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
289 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
294 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
300 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
305 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
310 if ( redFsm->anyActions() ) {
311 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
318 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
323 if ( redFsm->anyActions() ) {
324 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
331 if ( redFsm->anyToStateActions() ) {
332 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
338 if ( redFsm->anyFromStateActions() ) {
339 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
340 FROM_STATE_ACTIONS();
345 if ( redFsm->anyEofActions() ) {
346 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
352 if ( redFsm->anyEofTrans() ) {
353 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
362 void RubyFTabCodeGen::writeExec()
367 " _klen, _trans, _keys";
369 if ( redFsm->anyRegCurStateRef() )
372 if ( redFsm->anyConditions() )
387 " if _goto_level <= 0\n";
391 " if " << P() << " == " << PE() << "\n"
392 " _goto_level = _test_eof\n"
397 if ( redFsm->errState != 0 ) {
399 " if " << CS() << " == " << redFsm->errState->id << "\n"
400 " _goto_level = _out\n"
405 /* The resume label. */
408 " if _goto_level <= _resume\n";
410 if ( redFsm->anyFromStateActions() ) {
412 " case " << FSA() << "[" << CS() << "] \n";
413 FROM_STATE_ACTION_SWITCH() <<
414 " end # from state action switch \n"
418 if ( redFsm->anyConditions() )
424 out << " _trans = " << I() << "[_trans];\n";
426 if ( redFsm->anyEofTrans() ) {
429 " if _goto_level <= _eof_trans\n";
432 if ( redFsm->anyRegCurStateRef() )
433 out << " _ps = " << CS() << ";\n";
436 " " << CS() << " = " << TT() << "[_trans];\n"
439 if ( redFsm->anyRegActions() ) {
441 " if " << TA() << "[_trans] != 0\n"
443 " case " << TA() << "[_trans] \n";
445 " end # action switch \n"
450 /* The again label. */
453 " if _goto_level <= _again\n";
455 if ( redFsm->anyToStateActions() ) {
457 " case " << TSA() << "[" << CS() << "] \n";
458 TO_STATE_ACTION_SWITCH() <<
463 if ( redFsm->errState != 0 ) {
465 " if " << CS() << " == " << redFsm->errState->id << "\n"
466 " _goto_level = _out\n"
471 out << " " << P() << " += 1\n";
475 " if " << P() << " != " << PE() << "\n"
476 " _goto_level = _resume\n"
482 " _goto_level = _resume\n"
486 /* The test eof label. */
489 " if _goto_level <= _test_eof\n";
491 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
493 " if " << P() << " == " << EOFV() << "\n";
495 if ( redFsm->anyEofTrans() ) {
497 " if " << ET() << "[" << CS() << "] > 0\n"
498 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
499 " _goto_level = _eof_trans\n"
504 if ( redFsm->anyEofActions() ) {
507 " case ( " << EA() << "[" << CS() << "] )\n";
508 EOF_ACTION_SWITCH() <<
520 " if _goto_level <= _out\n"
525 /* Wrapping the execute block. */
530 void RubyFTabCodeGen::calcIndexSize()
532 int sizeWithInds = 0, sizeWithoutInds = 0;
534 /* Calculate cost of using with indicies. */
535 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
536 int totalIndex = st->outSingle.length() + st->outRange.length() +
537 (st->defTrans == 0 ? 0 : 1);
538 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
540 sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
541 if ( redFsm->anyActions() )
542 sizeWithInds += arrayTypeSize(redFsm->maxActListId) * redFsm->transSet.length();
544 /* Calculate the cost of not using indicies. */
545 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
546 int totalIndex = st->outSingle.length() + st->outRange.length() +
547 (st->defTrans == 0 ? 0 : 1);
548 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
549 if ( redFsm->anyActions() )
550 sizeWithoutInds += arrayTypeSize(redFsm->maxActListId) * totalIndex;
553 /* If using indicies reduces the size, use them. */
554 useIndicies = sizeWithInds < sizeWithoutInds;
560 * indent-tabs-mode: 1
561 * c-file-style: "bsd"