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 " _goto_level = _out\n"
122 int RubyFFlatCodeGen::TO_STATE_ACTION( RedStateAp
*state
)
125 if ( state
->toStateAction
!= 0 )
126 act
= state
->toStateAction
->actListId
+1;
130 int RubyFFlatCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
133 if ( state
->fromStateAction
!= 0 )
134 act
= state
->fromStateAction
->actListId
+1;
138 int RubyFFlatCodeGen::EOF_ACTION( RedStateAp
*state
)
141 if ( state
->eofAction
!= 0 )
142 act
= state
->eofAction
->actListId
+1;
146 /* Write out the function for a transition. */
147 int RubyFFlatCodeGen::TRANS_ACTION( RedTransAp
*trans
)
150 if ( trans
->action
!= 0 )
151 action
= trans
->action
->actListId
+1;
155 /* Write out the function switch. This switch is keyed on the values
156 * of the func index. */
157 std::ostream
&RubyFFlatCodeGen::TO_STATE_ACTION_SWITCH()
159 /* Loop the actions. */
160 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
161 if ( redAct
->numToStateRefs
> 0 ) {
162 /* Write the entry label. */
163 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
165 /* Write each action in the list of action items. */
166 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
167 ACTION( out
, item
->value
, 0, false );
171 genLineDirective( out
);
175 /* Write out the function switch. This switch is keyed on the values
176 * of the func index. */
177 std::ostream
&RubyFFlatCodeGen::FROM_STATE_ACTION_SWITCH()
179 /* Loop the actions. */
180 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
181 if ( redAct
->numFromStateRefs
> 0 ) {
182 /* Write the entry label. */
183 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
185 /* Write each action in the list of action items. */
186 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
187 ACTION( out
, item
->value
, 0, false );
191 genLineDirective( out
);
195 std::ostream
&RubyFFlatCodeGen::EOF_ACTION_SWITCH()
197 /* Loop the actions. */
198 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
199 if ( redAct
->numEofRefs
> 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 ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
205 ACTION( out
, item
->value
, 0, true );
209 genLineDirective( out
);
213 /* Write out the function switch. This switch is keyed on the values
214 * of the func index. */
215 std::ostream
&RubyFFlatCodeGen::ACTION_SWITCH()
217 /* Loop the actions. */
218 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
219 if ( redAct
->numTransRefs
> 0 ) {
220 /* Write the entry label. */
221 out
<< "\twhen " << redAct
->actListId
+1 << " then\n";
223 /* Write each action in the list of action items. */
224 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
225 ACTION( out
, item
->value
, 0, false );
230 genLineDirective( out
);
234 void RubyFFlatCodeGen::writeData()
236 if ( redFsm
->anyConditions() ) {
237 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
242 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpan
), CSP() );
247 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCond
), C() );
252 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondIndexOffset
), CO() );
258 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
263 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSpan
), SP() );
268 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxFlatIndexOffset
), IO() );
273 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
278 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
283 if ( redFsm
->anyActions() ) {
284 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
290 if ( redFsm
->anyToStateActions() ) {
291 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
297 if ( redFsm
->anyFromStateActions() ) {
298 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
299 FROM_STATE_ACTIONS();
304 if ( redFsm
->anyEofActions() ) {
305 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), EA() );
311 if ( redFsm
->anyEofTrans() ) {
312 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
321 void RubyFFlatCodeGen::writeExec()
326 " _slen, _trans, _keys, _inds";
327 if ( redFsm
->anyRegCurStateRef() )
329 if ( redFsm
->anyConditions() )
330 out
<< ", _cond, _conds, _widec";
331 if ( redFsm
->anyToStateActions() || redFsm
->anyRegActions()
332 || redFsm
->anyFromStateActions() )
333 out
<< ", _acts, _nacts";
347 " if _goto_level <= 0\n";
351 " if " << P() << " == " << PE() << "\n"
352 " _goto_level = _test_eof\n"
357 if ( redFsm
->errState
!= 0 ) {
359 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
360 " _goto_level = _out\n"
365 /* The resume label. */
368 " if _goto_level <= _resume\n";
370 if ( redFsm
->anyFromStateActions() ) {
372 " case " << FSA() << "[" << CS() << "] \n";
373 FROM_STATE_ACTION_SWITCH() <<
377 if ( redFsm
->anyConditions() )
382 if ( redFsm
->anyEofTrans() ) {
385 " if _goto_level <= _eof_trans\n";
388 if ( redFsm
->anyRegCurStateRef() )
389 out
<< " _ps = " << CS() << "\n";
391 out
<< " " << CS() << " = " << TT() << "[_trans]\n";
393 if ( redFsm
->anyRegActions() ) {
396 " if " << TA() << "[_trans] != 0\n"
397 " case " << TA() << "[_trans]" << "\n";
403 /* The again label. */
406 " if _goto_level <= _again\n";
408 if ( redFsm
->anyToStateActions() ) {
410 " case " << TSA() << "[" << CS() << "] \n";
411 TO_STATE_ACTION_SWITCH() <<
416 if ( redFsm
->errState
!= 0 ) {
418 " if " << CS() << " == " << redFsm
->errState
->id
<< "\n"
419 " _goto_level = _out\n"
424 out
<< " " << P() << " += 1\n";
428 " if " << P() << " != " << PE() << "\n"
429 " _goto_level = _resume\n"
435 " _goto_level = _resume\n"
439 /* The test eof label. */
442 " if _goto_level <= _test_eof\n";
444 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
446 " if " << P() << " == " << EOFV() << "\n";
448 if ( redFsm
->anyEofTrans() ) {
450 " if " << ET() << "[" << CS() << "] > 0\n"
451 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
452 " _goto_level = _eof_trans\n"
457 if ( redFsm
->anyEofActions() ) {
459 " case " << EA() << "[" << CS() << "]\n";
460 EOF_ACTION_SWITCH() <<
471 " if _goto_level <= _out\n"
476 /* Wrapping the execute block. */
483 * indent-tabs-mode: 1
484 * c-file-style: "bsd"