2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
9 * Ragel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Ragel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Ragel; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "ipgotocodegen.h"
30 bool IpGotoCodeGen::useAgainLabel()
32 return redFsm
->anyRegActionRets() ||
33 redFsm
->anyRegActionByValControl() ||
34 redFsm
->anyRegNextStmt();
37 void IpGotoCodeGen::GOTO( ostream
&ret
, int gotoDest
, bool inFinish
)
39 ret
<< "{" << CTRL_FLOW() << "goto st" << gotoDest
<< ";}";
42 void IpGotoCodeGen::CALL( ostream
&ret
, int callDest
, int targState
, bool inFinish
)
44 if ( prePushExpr
!= 0 ) {
46 INLINE_LIST( ret
, prePushExpr
, 0, false );
49 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << targState
<<
50 "; " << CTRL_FLOW() << "goto st" << callDest
<< ";}";
52 if ( prePushExpr
!= 0 )
56 void IpGotoCodeGen::CALL_EXPR( ostream
&ret
, InlineItem
*ilItem
, int targState
, bool inFinish
)
58 if ( prePushExpr
!= 0 ) {
60 INLINE_LIST( ret
, prePushExpr
, 0, false );
63 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << targState
<< "; " << CS() << " = (";
64 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
65 ret
<< "); " << CTRL_FLOW() << "goto _again;}";
67 if ( prePushExpr
!= 0 )
71 void IpGotoCodeGen::RET( ostream
&ret
, bool inFinish
)
73 ret
<< "{" << CS() << " = " << STACK() << "[--" << TOP() << "];";
75 if ( postPopExpr
!= 0 ) {
77 INLINE_LIST( ret
, postPopExpr
, 0, false );
81 ret
<< CTRL_FLOW() << "goto _again;}";
84 void IpGotoCodeGen::GOTO_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
86 ret
<< "{" << CS() << " = (";
87 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
88 ret
<< "); " << CTRL_FLOW() << "goto _again;}";
91 void IpGotoCodeGen::NEXT( ostream
&ret
, int nextDest
, bool inFinish
)
93 ret
<< CS() << " = " << nextDest
<< ";";
96 void IpGotoCodeGen::NEXT_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
98 ret
<< CS() << " = (";
99 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
103 void IpGotoCodeGen::CURS( ostream
&ret
, bool inFinish
)
108 void IpGotoCodeGen::TARGS( ostream
&ret
, bool inFinish
, int targState
)
113 void IpGotoCodeGen::BREAK( ostream
&ret
, int targState
)
116 ret
<< "{" << P() << "++; " << CS() << " = " << targState
<<
117 "; " << CTRL_FLOW() << "goto _out;}";
120 bool IpGotoCodeGen::IN_TRANS_ACTIONS( RedStateAp
*state
)
122 bool anyWritten
= false;
124 /* Emit any transitions that have actions and that go to this state. */
125 for ( int it
= 0; it
< state
->numInTrans
; it
++ ) {
126 RedTransAp
*trans
= state
->inTrans
[it
];
127 if ( trans
->action
!= 0 && trans
->labelNeeded
) {
128 /* Remember that we wrote an action so we know to write the
129 * line directive for going back to the output. */
132 /* Write the label for the transition so it can be jumped to. */
133 out
<< "tr" << trans
->id
<< ":\n";
135 /* If the action contains a next, then we must preload the current
136 * state since the action may or may not set it. */
137 if ( trans
->action
->anyNextStmt() )
138 out
<< " " << CS() << " = " << trans
->targ
->id
<< ";\n";
140 /* Write each action in the list. */
141 for ( ActionTable::Iter item
= trans
->action
->key
; item
.lte(); item
++ )
142 ACTION( out
, item
->value
, trans
->targ
->id
, false );
144 /* If the action contains a next then we need to reload, otherwise
145 * jump directly to the target state. */
146 if ( trans
->action
->anyNextStmt() )
147 out
<< "\tgoto _again;\n";
149 out
<< "\tgoto st" << trans
->targ
->id
<< ";\n";
156 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
158 void IpGotoCodeGen::GOTO_HEADER( RedStateAp
*state
)
160 bool anyWritten
= IN_TRANS_ACTIONS( state
);
162 if ( state
->labelNeeded
)
163 out
<< "st" << state
->id
<< ":\n";
165 if ( state
->toStateAction
!= 0 ) {
166 /* Remember that we wrote an action. Write every action in the list. */
168 for ( ActionTable::Iter item
= state
->toStateAction
->key
; item
.lte(); item
++ )
169 ACTION( out
, item
->value
, state
->id
, false );
172 /* Advance and test buffer pos. */
173 if ( state
->labelNeeded
) {
176 " if ( ++" << P() << " == " << PE() << " )\n"
177 " goto _test_eof" << state
->id
<< ";\n";
181 " " << P() << " += 1;\n";
185 /* Give the state a switch case. */
186 out
<< "case " << state
->id
<< ":\n";
188 if ( state
->fromStateAction
!= 0 ) {
189 /* Remember that we wrote an action. Write every action in the list. */
191 for ( ActionTable::Iter item
= state
->fromStateAction
->key
; item
.lte(); item
++ )
192 ACTION( out
, item
->value
, state
->id
, false );
196 genLineDirective( out
);
198 /* Record the prev state if necessary. */
199 if ( state
->anyRegCurStateRef() )
200 out
<< " _ps = " << state
->id
<< ";\n";
203 void IpGotoCodeGen::STATE_GOTO_ERROR()
205 /* In the error state we need to emit some stuff that usually goes into
207 RedStateAp
*state
= redFsm
->errState
;
208 bool anyWritten
= IN_TRANS_ACTIONS( state
);
210 /* No case label needed since we don't switch on the error state. */
212 genLineDirective( out
);
214 if ( state
->labelNeeded
)
215 out
<< "st" << state
->id
<< ":\n";
217 /* Break out here. */
219 out
<< CS() << " = " << state
->id
<< ";\n";
220 out
<< " goto _out;\n";
224 /* Emit the goto to take for a given transition. */
225 std::ostream
&IpGotoCodeGen::TRANS_GOTO( RedTransAp
*trans
, int level
)
227 if ( trans
->action
!= 0 ) {
228 /* Go to the transition which will go to the state. */
229 out
<< TABS(level
) << "goto tr" << trans
->id
<< ";";
232 /* Go directly to the target state. */
233 out
<< TABS(level
) << "goto st" << trans
->targ
->id
<< ";";
238 std::ostream
&IpGotoCodeGen::EXIT_STATES()
240 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
241 if ( st
->outNeeded
) {
243 out
<< " _test_eof" << st
->id
<< ": " << CS() << " = " <<
244 st
->id
<< "; goto _test_eof; \n";
250 std::ostream
&IpGotoCodeGen::AGAIN_CASES()
252 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
254 " case " << st
->id
<< ": goto st" << st
->id
<< ";\n";
259 std::ostream
&IpGotoCodeGen::FINISH_CASES()
261 bool anyWritten
= false;
263 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
264 if ( st
->eofAction
!= 0 ) {
265 if ( st
->eofAction
->eofRefs
== 0 )
266 st
->eofAction
->eofRefs
= new IntSet
;
267 st
->eofAction
->eofRefs
->insert( st
->id
);
271 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
272 if ( st
->eofTrans
!= 0 )
273 out
<< " case " << st
->id
<< ": goto tr" << st
->eofTrans
->id
<< ";\n";
276 for ( ActionTableMap::Iter act
= redFsm
->actionMap
; act
.lte(); act
++ ) {
277 if ( act
->eofRefs
!= 0 ) {
278 for ( IntSet::Iter pst
= *act
->eofRefs
; pst
.lte(); pst
++ )
279 out
<< " case " << *pst
<< ": \n";
281 /* Remember that we wrote a trans so we know to write the
282 * line directive for going back to the output. */
285 /* Write each action in the eof action list. */
286 for ( ActionTable::Iter item
= act
->key
; item
.lte(); item
++ )
287 ACTION( out
, item
->value
, STATE_ERR_STATE
, true );
293 genLineDirective( out
);
297 void IpGotoCodeGen::setLabelsNeeded( InlineList
*inlineList
)
299 for ( InlineList::Iter item
= *inlineList
; item
.lte(); item
++ ) {
300 switch ( item
->type
) {
301 case InlineItem::Goto
: case InlineItem::Call
: {
302 /* Mark the target as needing a label. */
303 item
->targState
->labelNeeded
= true;
309 if ( item
->children
!= 0 )
310 setLabelsNeeded( item
->children
);
314 /* Set up labelNeeded flag for each state. */
315 void IpGotoCodeGen::setLabelsNeeded()
317 /* If we use the _again label, then we the _again switch, which uses all
319 if ( useAgainLabel() ) {
320 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
321 st
->labelNeeded
= true;
324 /* Do not use all labels by default, init all labelNeeded vars to false. */
325 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
326 st
->labelNeeded
= false;
328 /* Walk all transitions and set only those that have targs. */
329 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
330 /* If there is no action with a next statement, then the label will be
332 if ( trans
->action
== 0 || !trans
->action
->anyNextStmt() )
333 trans
->targ
->labelNeeded
= true;
335 /* Need labels for states that have goto or calls in action code
336 * invoked on characters (ie, not from out action code). */
337 if ( trans
->action
!= 0 ) {
338 /* Loop the actions. */
339 for ( ActionTable::Iter act
= trans
->action
->key
; act
.lte(); act
++ ) {
340 /* Get the action and walk it's tree. */
341 setLabelsNeeded( act
->value
->inlineList
);
348 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
349 if ( st
!= redFsm
->errState
)
350 st
->outNeeded
= st
->labelNeeded
;
355 void IpGotoCodeGen::writeData()
360 void IpGotoCodeGen::writeExec()
362 /* Must set labels immediately before writing because we may depend on the
363 * noend write option. */
366 outLabelUsed
= false;
370 if ( redFsm
->anyRegCurStateRef() )
371 out
<< " int _ps = 0;\n";
373 if ( redFsm
->anyConditions() )
374 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
379 " if ( " << P() << " == " << PE() << " )\n"
380 " goto _test_eof;\n";
383 if ( useAgainLabel() ) {
388 " switch ( " << CS() << " ) {\n";
397 " if ( ++" << P() << " == " << PE() << " )\n"
398 " goto _test_eof;\n";
402 " " << P() << " += 1;\n";
409 " switch ( " << CS() << " )\n {\n";
417 out
<< " _test_eof: {}\n";
419 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
421 " if ( " << P() << " == " << EOFV() << " )\n"
423 " switch ( " << CS() << " ) {\n";
432 out
<< " _out: {}\n";