An fnext followed by an fbreak in -G2 did not work. The fbreak was not aware
[ragel.git] / rlgen-cd / ipgotocodegen.cpp
blobf12824abb8f901869ac247e660957906606caead
1 /*
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>
5 */
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
24 #include "rlgen-cd.h"
25 #include "ipgotocodegen.h"
26 #include "redfsm.h"
27 #include "gendata.h"
28 #include "bstmap.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 ) {
45 ret << "{";
46 INLINE_LIST( ret, prePushExpr, 0, false, false );
49 ret << "{" << STACK() << "[" << TOP() << "++] = " << targState <<
50 "; " << CTRL_FLOW() << "goto st" << callDest << ";}";
52 if ( prePushExpr != 0 )
53 ret << "}";
56 void IpGotoCodeGen::CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish )
58 if ( prePushExpr != 0 ) {
59 ret << "{";
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 )
68 ret << "}";
71 void IpGotoCodeGen::RET( ostream &ret, bool inFinish )
73 ret << "{" << CS() << " = " << STACK() << "[--" << TOP() << "];";
75 if ( postPopExpr != 0 ) {
76 ret << "{";
77 INLINE_LIST( ret, postPopExpr, 0, false, false );
78 ret << "}";
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 );
100 ret << ");";
103 void IpGotoCodeGen::CURS( ostream &ret, bool inFinish )
105 ret << "(_ps)";
108 void IpGotoCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
110 ret << targState;
113 void IpGotoCodeGen::BREAK( ostream &ret, int targState, bool csForced )
115 outLabelUsed = true;
116 ret << "{" << P() << "++; ";
117 if ( !csForced )
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. */
132 anyWritten = true;
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";
152 else
153 out << "\tgoto st" << trans->targ->id << ";\n";
157 return anyWritten;
160 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
161 * state. */
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. */
171 anyWritten = true;
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 ) {
180 if ( hasEnd ) {
181 out <<
182 " if ( ++" << P() << " == " << PE() << " )\n"
183 " goto _test_eof" << state->id << ";\n";
185 else {
186 out <<
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. */
196 anyWritten = true;
197 for ( ActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ ) {
198 ACTION( out, item->value, state->id, false,
199 state->fromStateAction->anyNextStmt() );
203 if ( anyWritten )
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
214 * the header. */
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. */
219 if ( anyWritten )
220 genLineDirective( out );
222 if ( state->labelNeeded )
223 out << "st" << state->id << ":\n";
225 /* Break out here. */
226 outLabelUsed = true;
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 << ";";
239 else {
240 /* Go directly to the target state. */
241 out << TABS(level) << "goto st" << trans->targ->id << ";";
243 return out;
246 std::ostream &IpGotoCodeGen::EXIT_STATES()
248 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
249 if ( st->outNeeded ) {
250 testEofUsed = true;
251 out << " _test_eof" << st->id << ": " << CS() << " = " <<
252 st->id << "; goto _test_eof; \n";
255 return out;
258 std::ostream &IpGotoCodeGen::AGAIN_CASES()
260 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
261 out <<
262 " case " << st->id << ": goto st" << st->id << ";\n";
264 return out;
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. */
291 anyWritten = true;
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 );
296 out << "\tbreak;\n";
300 if ( anyWritten )
301 genLineDirective( out );
302 return 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;
312 break;
314 default: break;
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
326 * labels. */
327 if ( useAgainLabel() ) {
328 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
329 st->labelNeeded = true;
331 else {
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
339 * needed. */
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 );
355 if ( hasEnd ) {
356 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
357 if ( st != redFsm->errState )
358 st->outNeeded = st->labelNeeded;
363 void IpGotoCodeGen::writeData()
365 STATE_IDS();
368 void IpGotoCodeGen::writeExec()
370 /* Must set labels immediately before writing because we may depend on the
371 * noend write option. */
372 setLabelsNeeded();
373 testEofUsed = false;
374 outLabelUsed = false;
376 out << " {\n";
378 if ( redFsm->anyRegCurStateRef() )
379 out << " int _ps = 0;\n";
381 if ( redFsm->anyConditions() )
382 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
384 if ( hasEnd ) {
385 testEofUsed = true;
386 out <<
387 " if ( " << P() << " == " << PE() << " )\n"
388 " goto _test_eof;\n";
391 if ( useAgainLabel() ) {
392 out <<
393 " goto _resume;\n"
394 "\n"
395 "_again:\n"
396 " switch ( " << CS() << " ) {\n";
397 AGAIN_CASES() <<
398 " default: break;\n"
399 " }\n"
400 "\n";
402 if ( hasEnd ) {
403 testEofUsed = true;
404 out <<
405 " if ( ++" << P() << " == " << PE() << " )\n"
406 " goto _test_eof;\n";
408 else {
409 out <<
410 " " << P() << " += 1;\n";
413 out << "_resume:\n";
416 out <<
417 " switch ( " << CS() << " )\n {\n";
418 STATE_GOTOS();
419 SWITCH_DEFAULT() <<
420 " }\n";
421 EXIT_STATES() <<
422 "\n";
424 if ( testEofUsed )
425 out << " _test_eof: {}\n";
427 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
428 out <<
429 " if ( " << P() << " == " << EOFV() << " )\n"
430 " {\n"
431 " switch ( " << CS() << " ) {\n";
432 FINISH_CASES();
433 SWITCH_DEFAULT() <<
434 " }\n"
435 " }\n"
436 "\n";
439 if ( outLabelUsed )
440 out << " _out: {}\n";
442 out <<
443 " }\n";