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
, InlineItem
*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
, InlineItem
*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 " _goto_level = _out\n"
133 std::ostream
&RubyFTabCodeGen::TO_STATE_ACTION_SWITCH()
135 /* Loop the actions. */
136 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
137 if ( redAct
->numToStateRefs
> 0 ) {
138 /* Write the entry label. */
139 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
141 /* Write each action in the list of action items. */
142 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
143 ACTION( out
, item
->value
, 0, false );
148 genLineDirective( out
);
152 /* Write out the function switch. This switch is keyed on the values
153 * of the func index. */
154 std::ostream
&RubyFTabCodeGen::FROM_STATE_ACTION_SWITCH()
156 /* Loop the actions. */
157 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
158 if ( redAct
->numFromStateRefs
> 0 ) {
159 /* Write the entry label. */
160 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
162 /* Write each action in the list of action items. */
163 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
164 ACTION( out
, item
->value
, 0, false );
169 genLineDirective( out
);
173 std::ostream
&RubyFTabCodeGen::EOF_ACTION_SWITCH()
175 /* Loop the actions. */
176 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
177 if ( redAct
->numEofRefs
> 0 ) {
178 /* Write the entry label. */
179 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
181 /* Write each action in the list of action items. */
182 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
183 ACTION( out
, item
->value
, 0, true );
188 genLineDirective( out
);
192 /* Write out the function switch. This switch is keyed on the values
193 * of the func index. */
194 std::ostream
&RubyFTabCodeGen::ACTION_SWITCH()
196 /* Loop the actions. */
197 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
198 if ( redAct
->numTransRefs
> 0 ) {
199 /* Write the entry label. */
200 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
202 /* Write each action in the list of action items. */
203 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
204 ACTION( out
, item
->value
, 0, false );
209 genLineDirective( out
);
214 int RubyFTabCodeGen::TO_STATE_ACTION( RedStateAp
*state
)
217 if ( state
->toStateAction
!= 0 )
218 act
= state
->toStateAction
->actListId
+1;
222 int RubyFTabCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
225 if ( state
->fromStateAction
!= 0 )
226 act
= state
->fromStateAction
->actListId
+1;
230 int RubyFTabCodeGen::EOF_ACTION( RedStateAp
*state
)
233 if ( state
->eofAction
!= 0 )
234 act
= state
->eofAction
->actListId
+1;
239 /* Write out the function for a transition. */
240 int RubyFTabCodeGen::TRANS_ACTION( RedTransAp
*trans
)
243 if ( trans
->action
!= 0 )
244 action
= trans
->action
->actListId
+1;
248 void RubyFTabCodeGen::writeData()
251 if ( redFsm
->anyConditions() ) {
252 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondOffset
), CO() );
257 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondLen
), CL() );
262 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
267 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpaceId
), C() );
273 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxKeyOffset
), KO() );
278 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
283 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSingleLen
), SL() );
288 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxRangeLen
), RL() );
293 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndexOffset
), IO() );
299 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
304 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
309 if ( redFsm
->anyActions() ) {
310 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
317 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
322 if ( redFsm
->anyActions() ) {
323 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
330 if ( redFsm
->anyToStateActions() ) {
331 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
337 if ( redFsm
->anyFromStateActions() ) {
338 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
339 FROM_STATE_ACTIONS();
344 if ( redFsm
->anyEofActions() ) {
345 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), EA() );
351 if ( redFsm
->anyEofTrans() ) {
352 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
361 void RubyFTabCodeGen::writeExec()
366 " _klen, _trans, _keys";
368 if ( redFsm
->anyRegCurStateRef() )
371 if ( redFsm
->anyConditions() )
386 " if _goto_level <= 0\n";
390 " if " << P() << " == " << PE() << "\n"
391 " _goto_level = _test_eof\n"
396 if ( redFsm
->errState
!= 0 ) {
398 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
399 " _goto_level = _out\n"
404 /* The resume label. */
407 " if _goto_level <= _resume\n";
409 if ( redFsm
->anyFromStateActions() ) {
411 " case " << FSA() << "[" << CS() << "] \n";
412 FROM_STATE_ACTION_SWITCH() <<
413 " end # from state action switch \n"
417 if ( redFsm
->anyConditions() )
423 out
<< " _trans = " << I() << "[_trans];\n";
425 if ( redFsm
->anyEofTrans() ) {
428 " if _goto_level <= _eof_trans\n";
431 if ( redFsm
->anyRegCurStateRef() )
432 out
<< " _ps = " << CS() << ";\n";
435 " " << CS() << " = " << TT() << "[_trans];\n"
438 if ( redFsm
->anyRegActions() ) {
440 " if " << TA() << "[_trans] != 0\n"
442 " case " << TA() << "[_trans] \n";
444 " end # action switch \n"
449 /* The again label. */
452 " if _goto_level <= _again\n";
454 if ( redFsm
->anyToStateActions() ) {
456 " case " << TSA() << "[" << CS() << "] \n";
457 TO_STATE_ACTION_SWITCH() <<
462 if ( redFsm
->errState
!= 0 ) {
464 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
465 " _goto_level = _out\n"
470 out
<< " " << P() << " += 1\n";
474 " if " << P() << " != " << PE() << "\n"
475 " _goto_level = _resume\n"
481 " _goto_level = _resume\n"
485 /* The test eof label. */
488 " if _goto_level <= _test_eof\n";
490 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
492 " if " << P() << " == " << EOFV() << "\n";
494 if ( redFsm
->anyEofTrans() ) {
496 " if " << ET() << "[" << CS() << "] > 0\n"
497 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
498 " _goto_level = _eof_trans\n"
503 if ( redFsm
->anyEofActions() ) {
506 " case ( " << EA() << "[" << CS() << "] )\n";
507 EOF_ACTION_SWITCH() <<
519 " if _goto_level <= _out\n"
524 /* Wrapping the execute block. */
529 void RubyFTabCodeGen::calcIndexSize()
531 int sizeWithInds
= 0, sizeWithoutInds
= 0;
533 /* Calculate cost of using with indicies. */
534 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
535 int totalIndex
= st
->outSingle
.length() + st
->outRange
.length() +
536 (st
->defTrans
== 0 ? 0 : 1);
537 sizeWithInds
+= arrayTypeSize(redFsm
->maxIndex
) * totalIndex
;
539 sizeWithInds
+= arrayTypeSize(redFsm
->maxState
) * redFsm
->transSet
.length();
540 if ( redFsm
->anyActions() )
541 sizeWithInds
+= arrayTypeSize(redFsm
->maxActListId
) * redFsm
->transSet
.length();
543 /* Calculate the cost of not using indicies. */
544 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
545 int totalIndex
= st
->outSingle
.length() + st
->outRange
.length() +
546 (st
->defTrans
== 0 ? 0 : 1);
547 sizeWithoutInds
+= arrayTypeSize(redFsm
->maxState
) * totalIndex
;
548 if ( redFsm
->anyActions() )
549 sizeWithoutInds
+= arrayTypeSize(redFsm
->maxActListId
) * totalIndex
;
552 /* If using indicies reduces the size, use them. */
553 useIndicies
= sizeWithInds
< sizeWithoutInds
;
559 * indent-tabs-mode: 1
560 * c-file-style: "bsd"