Editing pass.
[ragel.git] / rlgen-cd / ipgotocodegen.cpp
blob69f9da7d72471f83a9e1c81250ab23d188168287
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 );
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 );
63 ret << "{" << STACK() << "[" << TOP() << "++] = " << targState << "; " << CS() << " = (";
64 INLINE_LIST( ret, ilItem->children, 0, inFinish );
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 );
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 );
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 );
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 )
115 outLabelUsed = true;
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. */
130 anyWritten = true;
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";
148 else
149 out << "\tgoto st" << trans->targ->id << ";\n";
153 return anyWritten;
156 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
157 * state. */
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. */
167 anyWritten = true;
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 ) {
174 if ( hasEnd ) {
175 out <<
176 " if ( ++" << P() << " == " << PE() << " )\n"
177 " goto _test_eof" << state->id << ";\n";
179 else {
180 out <<
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. */
190 anyWritten = true;
191 for ( ActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ )
192 ACTION( out, item->value, state->id, false );
195 if ( anyWritten )
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
206 * the header. */
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. */
211 if ( anyWritten )
212 genLineDirective( out );
214 if ( state->labelNeeded )
215 out << "st" << state->id << ":\n";
217 /* Break out here. */
218 outLabelUsed = true;
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 << ";";
231 else {
232 /* Go directly to the target state. */
233 out << TABS(level) << "goto st" << trans->targ->id << ";";
235 return out;
238 std::ostream &IpGotoCodeGen::EXIT_STATES()
240 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
241 if ( st->outNeeded ) {
242 testEofUsed = true;
243 out << " _test_eof" << st->id << ": " << CS() << " = " <<
244 st->id << "; goto _test_eof; \n";
247 return out;
250 std::ostream &IpGotoCodeGen::AGAIN_CASES()
252 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
253 out <<
254 " case " << st->id << ": goto st" << st->id << ";\n";
256 return out;
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. */
283 anyWritten = true;
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 );
288 out << "\tbreak;\n";
292 if ( anyWritten )
293 genLineDirective( out );
294 return 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;
304 break;
306 default: break;
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
318 * labels. */
319 if ( useAgainLabel() ) {
320 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
321 st->labelNeeded = true;
323 else {
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
331 * needed. */
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 );
347 if ( hasEnd ) {
348 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
349 if ( st != redFsm->errState )
350 st->outNeeded = st->labelNeeded;
355 void IpGotoCodeGen::writeData()
357 STATE_IDS();
360 void IpGotoCodeGen::writeExec()
362 /* Must set labels immediately before writing because we may depend on the
363 * noend write option. */
364 setLabelsNeeded();
365 testEofUsed = false;
366 outLabelUsed = false;
368 out << " {\n";
370 if ( redFsm->anyRegCurStateRef() )
371 out << " int _ps = 0;\n";
373 if ( redFsm->anyConditions() )
374 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
376 if ( hasEnd ) {
377 testEofUsed = true;
378 out <<
379 " if ( " << P() << " == " << PE() << " )\n"
380 " goto _test_eof;\n";
383 if ( useAgainLabel() ) {
384 out <<
385 " goto _resume;\n"
386 "\n"
387 "_again:\n"
388 " switch ( " << CS() << " ) {\n";
389 AGAIN_CASES() <<
390 " default: break;\n"
391 " }\n"
392 "\n";
394 if ( hasEnd ) {
395 testEofUsed = true;
396 out <<
397 " if ( ++" << P() << " == " << PE() << " )\n"
398 " goto _test_eof;\n";
400 else {
401 out <<
402 " " << P() << " += 1;\n";
405 out << "_resume:\n";
408 out <<
409 " switch ( " << CS() << " )\n {\n";
410 STATE_GOTOS();
411 SWITCH_DEFAULT() <<
412 " }\n";
413 EXIT_STATES() <<
414 "\n";
416 if ( testEofUsed )
417 out << " _test_eof: {}\n";
419 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
420 out <<
421 " if ( " << P() << " == " << EOFV() << " )\n"
422 " {\n"
423 " switch ( " << CS() << " ) {\n";
424 FINISH_CASES();
425 SWITCH_DEFAULT() <<
426 " }\n"
427 " }\n"
428 "\n";
431 if ( outLabelUsed )
432 out << " _out: {}\n";
434 out <<
435 " }\n";