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, 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, false );
63 ret
<< "{" << STACK() << "[" << TOP() << "++] = " << targState
<< "; " << CS() << " = (";
64 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
, false );
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, 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
, false );
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
, false );
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
, bool csForced
)
116 ret
<< "{" << P() << "++; ";
118 ret
<< CS() << " = " << targState
<< "; ";
119 ret
<< CTRL_FLOW() << "goto _out;}";
122 bool IpGotoCodeGen::IN_TRANS_ACTIONS( RedStateAp
*state
)
124 bool anyWritten
= false;
126 /* Emit any transitions that have actions and that go to this state. */
127 for ( int it
= 0; it
< state
->numInTrans
; it
++ ) {
128 RedTransAp
*trans
= state
->inTrans
[it
];
129 if ( trans
->action
!= 0 && trans
->labelNeeded
) {
130 /* Remember that we wrote an action so we know to write the
131 * line directive for going back to the output. */
134 /* Write the label for the transition so it can be jumped to. */
135 out
<< "tr" << trans
->id
<< ":\n";
137 /* If the action contains a next, then we must preload the current
138 * state since the action may or may not set it. */
139 if ( trans
->action
->anyNextStmt() )
140 out
<< " " << CS() << " = " << trans
->targ
->id
<< ";\n";
142 /* Write each action in the list. */
143 for ( ActionTable::Iter item
= trans
->action
->key
; item
.lte(); item
++ ) {
144 ACTION( out
, item
->value
, trans
->targ
->id
, false,
145 trans
->action
->anyNextStmt() );
148 /* If the action contains a next then we need to reload, otherwise
149 * jump directly to the target state. */
150 if ( trans
->action
->anyNextStmt() )
151 out
<< "\tgoto _again;\n";
153 out
<< "\tgoto st" << trans
->targ
->id
<< ";\n";
160 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
162 void IpGotoCodeGen::GOTO_HEADER( RedStateAp
*state
)
164 bool anyWritten
= IN_TRANS_ACTIONS( state
);
166 if ( state
->labelNeeded
)
167 out
<< "st" << state
->id
<< ":\n";
169 if ( state
->toStateAction
!= 0 ) {
170 /* Remember that we wrote an action. Write every action in the list. */
172 for ( ActionTable::Iter item
= state
->toStateAction
->key
; item
.lte(); item
++ ) {
173 ACTION( out
, item
->value
, state
->id
, false,
174 state
->toStateAction
->anyNextStmt() );
178 /* Advance and test buffer pos. */
179 if ( state
->labelNeeded
) {
182 " if ( ++" << P() << " == " << PE() << " )\n"
183 " goto _test_eof" << state
->id
<< ";\n";
187 " " << P() << " += 1;\n";
191 /* Give the state a switch case. */
192 out
<< "case " << state
->id
<< ":\n";
194 if ( state
->fromStateAction
!= 0 ) {
195 /* Remember that we wrote an action. Write every action in the list. */
197 for ( ActionTable::Iter item
= state
->fromStateAction
->key
; item
.lte(); item
++ ) {
198 ACTION( out
, item
->value
, state
->id
, false,
199 state
->fromStateAction
->anyNextStmt() );
204 genLineDirective( out
);
206 /* Record the prev state if necessary. */
207 if ( state
->anyRegCurStateRef() )
208 out
<< " _ps = " << state
->id
<< ";\n";
211 void IpGotoCodeGen::STATE_GOTO_ERROR()
213 /* In the error state we need to emit some stuff that usually goes into
215 RedStateAp
*state
= redFsm
->errState
;
216 bool anyWritten
= IN_TRANS_ACTIONS( state
);
218 /* No case label needed since we don't switch on the error state. */
220 genLineDirective( out
);
222 if ( state
->labelNeeded
)
223 out
<< "st" << state
->id
<< ":\n";
225 /* Break out here. */
227 out
<< CS() << " = " << state
->id
<< ";\n";
228 out
<< " goto _out;\n";
232 /* Emit the goto to take for a given transition. */
233 std::ostream
&IpGotoCodeGen::TRANS_GOTO( RedTransAp
*trans
, int level
)
235 if ( trans
->action
!= 0 ) {
236 /* Go to the transition which will go to the state. */
237 out
<< TABS(level
) << "goto tr" << trans
->id
<< ";";
240 /* Go directly to the target state. */
241 out
<< TABS(level
) << "goto st" << trans
->targ
->id
<< ";";
246 std::ostream
&IpGotoCodeGen::EXIT_STATES()
248 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
249 if ( st
->outNeeded
) {
251 out
<< " _test_eof" << st
->id
<< ": " << CS() << " = " <<
252 st
->id
<< "; goto _test_eof; \n";
258 std::ostream
&IpGotoCodeGen::AGAIN_CASES()
260 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
262 " case " << st
->id
<< ": goto st" << st
->id
<< ";\n";
267 std::ostream
&IpGotoCodeGen::FINISH_CASES()
269 bool anyWritten
= false;
271 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
272 if ( st
->eofAction
!= 0 ) {
273 if ( st
->eofAction
->eofRefs
== 0 )
274 st
->eofAction
->eofRefs
= new IntSet
;
275 st
->eofAction
->eofRefs
->insert( st
->id
);
279 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
280 if ( st
->eofTrans
!= 0 )
281 out
<< " case " << st
->id
<< ": goto tr" << st
->eofTrans
->id
<< ";\n";
284 for ( ActionTableMap::Iter act
= redFsm
->actionMap
; act
.lte(); act
++ ) {
285 if ( act
->eofRefs
!= 0 ) {
286 for ( IntSet::Iter pst
= *act
->eofRefs
; pst
.lte(); pst
++ )
287 out
<< " case " << *pst
<< ": \n";
289 /* Remember that we wrote a trans so we know to write the
290 * line directive for going back to the output. */
293 /* Write each action in the eof action list. */
294 for ( ActionTable::Iter item
= act
->key
; item
.lte(); item
++ )
295 ACTION( out
, item
->value
, STATE_ERR_STATE
, true, false );
301 genLineDirective( out
);
305 void IpGotoCodeGen::setLabelsNeeded( InlineList
*inlineList
)
307 for ( InlineList::Iter item
= *inlineList
; item
.lte(); item
++ ) {
308 switch ( item
->type
) {
309 case InlineItem::Goto
: case InlineItem::Call
: {
310 /* Mark the target as needing a label. */
311 item
->targState
->labelNeeded
= true;
317 if ( item
->children
!= 0 )
318 setLabelsNeeded( item
->children
);
322 /* Set up labelNeeded flag for each state. */
323 void IpGotoCodeGen::setLabelsNeeded()
325 /* If we use the _again label, then we the _again switch, which uses all
327 if ( useAgainLabel() ) {
328 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
329 st
->labelNeeded
= true;
332 /* Do not use all labels by default, init all labelNeeded vars to false. */
333 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
334 st
->labelNeeded
= false;
336 /* Walk all transitions and set only those that have targs. */
337 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
338 /* If there is no action with a next statement, then the label will be
340 if ( trans
->action
== 0 || !trans
->action
->anyNextStmt() )
341 trans
->targ
->labelNeeded
= true;
343 /* Need labels for states that have goto or calls in action code
344 * invoked on characters (ie, not from out action code). */
345 if ( trans
->action
!= 0 ) {
346 /* Loop the actions. */
347 for ( ActionTable::Iter act
= trans
->action
->key
; act
.lte(); act
++ ) {
348 /* Get the action and walk it's tree. */
349 setLabelsNeeded( act
->value
->inlineList
);
356 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
357 if ( st
!= redFsm
->errState
)
358 st
->outNeeded
= st
->labelNeeded
;
363 void IpGotoCodeGen::writeData()
368 void IpGotoCodeGen::writeExec()
370 /* Must set labels immediately before writing because we may depend on the
371 * noend write option. */
374 outLabelUsed
= false;
378 if ( redFsm
->anyRegCurStateRef() )
379 out
<< " int _ps = 0;\n";
381 if ( redFsm
->anyConditions() )
382 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
387 " if ( " << P() << " == " << PE() << " )\n"
388 " goto _test_eof;\n";
391 if ( useAgainLabel() ) {
396 " switch ( " << CS() << " ) {\n";
405 " if ( ++" << P() << " == " << PE() << " )\n"
406 " goto _test_eof;\n";
410 " " << P() << " += 1;\n";
417 " switch ( " << CS() << " )\n {\n";
425 out
<< " _test_eof: {}\n";
427 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
429 " if ( " << P() << " == " << EOFV() << " )\n"
431 " switch ( " << CS() << " ) {\n";
440 out
<< " _out: {}\n";