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
23 #include "ruby-fflatcodegen.h"
25 void RubyFFlatCodeGen::GOTO( ostream
&out
, int gotoDest
, bool inFinish
)
29 " " << CS() << " = " << gotoDest
<< "\n"
30 " _goto_level = _again\n"
35 void RubyFFlatCodeGen::GOTO_EXPR( ostream
&out
, InlineItem
*ilItem
, bool inFinish
)
39 " " << CS() << " = (";
40 INLINE_LIST( out
, ilItem
->children
, 0, inFinish
);
43 " _goto_level = _again\n"
48 void RubyFFlatCodeGen::CALL( ostream
&out
, int callDest
, int targState
, bool inFinish
)
50 if ( prePushExpr
!= 0 ) {
52 INLINE_LIST( out
, prePushExpr
, 0, false );
57 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
58 " " << TOP() << "+= 1\n"
59 " " << CS() << " = " << callDest
<< "\n"
60 " _goto_level = _again\n"
64 if ( prePushExpr
!= 0 )
68 void RubyFFlatCodeGen::CALL_EXPR(ostream
&out
, InlineItem
*ilItem
,
69 int targState
, bool inFinish
)
71 if ( prePushExpr
!= 0 ) {
73 INLINE_LIST( out
, prePushExpr
, 0, false );
78 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
79 " " << TOP() << " += 1\n"
80 " " << CS() << " = (";
81 INLINE_LIST( out
, ilItem
->children
, targState
, inFinish
);
85 " _goto_level = _again\n"
89 if ( prePushExpr
!= 0 )
93 void RubyFFlatCodeGen::RET( ostream
&out
, bool inFinish
)
97 " " << TOP() << " -= 1\n"
98 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
100 if ( postPopExpr
!= 0 ) {
102 INLINE_LIST( out
, postPopExpr
, 0, false );
107 " _goto_level = _again\n"
112 void RubyFFlatCodeGen::BREAK( ostream
&out
, int targState
)
116 " " << P() << " += 1\n"
117 " _goto_level = _out\n"
123 int RubyFFlatCodeGen::TO_STATE_ACTION( RedStateAp
*state
)
126 if ( state
->toStateAction
!= 0 )
127 act
= state
->toStateAction
->actListId
+1;
131 int RubyFFlatCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
134 if ( state
->fromStateAction
!= 0 )
135 act
= state
->fromStateAction
->actListId
+1;
139 int RubyFFlatCodeGen::EOF_ACTION( RedStateAp
*state
)
142 if ( state
->eofAction
!= 0 )
143 act
= state
->eofAction
->actListId
+1;
147 /* Write out the function for a transition. */
148 int RubyFFlatCodeGen::TRANS_ACTION( RedTransAp
*trans
)
151 if ( trans
->action
!= 0 )
152 action
= trans
->action
->actListId
+1;
156 /* Write out the function switch. This switch is keyed on the values
157 * of the func index. */
158 std::ostream
&RubyFFlatCodeGen::TO_STATE_ACTION_SWITCH()
160 /* Loop the actions. */
161 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
162 if ( redAct
->numToStateRefs
> 0 ) {
163 /* Write the entry label. */
164 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
166 /* Write each action in the list of action items. */
167 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
168 ACTION( out
, item
->value
, 0, false );
172 genLineDirective( out
);
176 /* Write out the function switch. This switch is keyed on the values
177 * of the func index. */
178 std::ostream
&RubyFFlatCodeGen::FROM_STATE_ACTION_SWITCH()
180 /* Loop the actions. */
181 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
182 if ( redAct
->numFromStateRefs
> 0 ) {
183 /* Write the entry label. */
184 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
186 /* Write each action in the list of action items. */
187 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
188 ACTION( out
, item
->value
, 0, false );
192 genLineDirective( out
);
196 std::ostream
&RubyFFlatCodeGen::EOF_ACTION_SWITCH()
198 /* Loop the actions. */
199 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
200 if ( redAct
->numEofRefs
> 0 ) {
201 /* Write the entry label. */
202 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
204 /* Write each action in the list of action items. */
205 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
206 ACTION( out
, item
->value
, 0, true );
210 genLineDirective( out
);
214 /* Write out the function switch. This switch is keyed on the values
215 * of the func index. */
216 std::ostream
&RubyFFlatCodeGen::ACTION_SWITCH()
218 /* Loop the actions. */
219 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
220 if ( redAct
->numTransRefs
> 0 ) {
221 /* Write the entry label. */
222 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
224 /* Write each action in the list of action items. */
225 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
226 ACTION( out
, item
->value
, 0, false );
231 genLineDirective( out
);
235 void RubyFFlatCodeGen::writeData()
237 if ( redFsm
->anyConditions() ) {
238 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
243 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpan
), CSP() );
248 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCond
), C() );
253 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondIndexOffset
), CO() );
259 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
264 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSpan
), SP() );
269 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxFlatIndexOffset
), IO() );
274 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
279 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
284 if ( redFsm
->anyActions() ) {
285 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
291 if ( redFsm
->anyToStateActions() ) {
292 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
298 if ( redFsm
->anyFromStateActions() ) {
299 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
300 FROM_STATE_ACTIONS();
305 if ( redFsm
->anyEofActions() ) {
306 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), EA() );
312 if ( redFsm
->anyEofTrans() ) {
313 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
322 void RubyFFlatCodeGen::writeExec()
327 " _slen, _trans, _keys, _inds";
328 if ( redFsm
->anyRegCurStateRef() )
330 if ( redFsm
->anyConditions() )
331 out
<< ", _cond, _conds, _widec";
332 if ( redFsm
->anyToStateActions() || redFsm
->anyRegActions()
333 || redFsm
->anyFromStateActions() )
334 out
<< ", _acts, _nacts";
348 " if _goto_level <= 0\n";
352 " if " << P() << " == " << PE() << "\n"
353 " _goto_level = _test_eof\n"
358 if ( redFsm
->errState
!= 0 ) {
360 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
361 " _goto_level = _out\n"
366 /* The resume label. */
369 " if _goto_level <= _resume\n";
371 if ( redFsm
->anyFromStateActions() ) {
373 " case " << FSA() << "[" << CS() << "] \n";
374 FROM_STATE_ACTION_SWITCH() <<
378 if ( redFsm
->anyConditions() )
383 if ( redFsm
->anyEofTrans() ) {
386 " if _goto_level <= _eof_trans\n";
389 if ( redFsm
->anyRegCurStateRef() )
390 out
<< " _ps = " << CS() << "\n";
392 out
<< " " << CS() << " = " << TT() << "[_trans]\n";
394 if ( redFsm
->anyRegActions() ) {
397 " if " << TA() << "[_trans] != 0\n"
398 " case " << TA() << "[_trans]" << "\n";
404 /* The again label. */
407 " if _goto_level <= _again\n";
409 if ( redFsm
->anyToStateActions() ) {
411 " case " << TSA() << "[" << CS() << "] \n";
412 TO_STATE_ACTION_SWITCH() <<
417 if ( redFsm
->errState
!= 0 ) {
419 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
420 " _goto_level = _out\n"
425 out
<< " " << P() << " += 1\n";
429 " if " << P() << " != " << PE() << "\n"
430 " _goto_level = _resume\n"
436 " _goto_level = _resume\n"
440 /* The test eof label. */
443 " if _goto_level <= _test_eof\n";
445 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
447 " if " << P() << " == " << EOFV() << "\n";
449 if ( redFsm
->anyEofTrans() ) {
451 " if " << ET() << "[" << CS() << "] > 0\n"
452 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
453 " _goto_level = _eof_trans\n"
458 if ( redFsm
->anyEofActions() ) {
460 " case " << EA() << "[" << CS() << "]\n";
461 EOF_ACTION_SWITCH() <<
472 " if _goto_level <= _out\n"
477 /* Wrapping the execute block. */
484 * indent-tabs-mode: 1
485 * c-file-style: "bsd"