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
24 #include "rlgen-csharp.h"
25 #include "ipgotocodegen.h"
30 bool CSharpIpGotoCodeGen::useAgainLabel()
32 return redFsm
->anyRegActionRets() ||
33 redFsm
->anyRegActionByValControl() ||
34 redFsm
->anyRegNextStmt();
37 void CSharpIpGotoCodeGen::GOTO( ostream
&ret
, int gotoDest
, bool inFinish
)
39 ret
<< "{" << CTRL_FLOW() << "goto st" << gotoDest
<< ";}";
42 void CSharpIpGotoCodeGen::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 CSharpIpGotoCodeGen::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 CSharpIpGotoCodeGen::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 CSharpIpGotoCodeGen::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 CSharpIpGotoCodeGen::NEXT( ostream
&ret
, int nextDest
, bool inFinish
)
93 ret
<< CS() << " = " << nextDest
<< ";";
96 void CSharpIpGotoCodeGen::NEXT_EXPR( ostream
&ret
, InlineItem
*ilItem
, bool inFinish
)
98 ret
<< CS() << " = (";
99 INLINE_LIST( ret
, ilItem
->children
, 0, inFinish
);
103 void CSharpIpGotoCodeGen::CURS( ostream
&ret
, bool inFinish
)
108 void CSharpIpGotoCodeGen::TARGS( ostream
&ret
, bool inFinish
, int targState
)
113 void CSharpIpGotoCodeGen::BREAK( ostream
&ret
, int targState
)
115 /* FIXME: If this code generator is made active then BREAK generation
116 * needs to check csForced. */
118 ret
<< "{" << P() << "++; " << CS() << " = " << targState
<<
119 "; " << CTRL_FLOW() << "goto _out;}";
122 bool CSharpIpGotoCodeGen::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 );
146 /* If the action contains a next then we need to reload, otherwise
147 * jump directly to the target state. */
148 if ( trans
->action
->anyNextStmt() )
149 out
<< "\tgoto _again;\n";
151 out
<< "\tgoto st" << trans
->targ
->id
<< ";\n";
158 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
160 void CSharpIpGotoCodeGen::GOTO_HEADER( RedStateAp
*state
)
162 bool anyWritten
= IN_TRANS_ACTIONS( state
);
164 if ( state
->labelNeeded
)
165 out
<< "st" << state
->id
<< ":\n";
167 if ( state
->toStateAction
!= 0 ) {
168 /* Remember that we wrote an action. Write every action in the list. */
170 for ( ActionTable::Iter item
= state
->toStateAction
->key
; item
.lte(); item
++ )
171 ACTION( out
, item
->value
, state
->id
, false );
174 /* Advance and test buffer pos. */
175 if ( state
->labelNeeded
) {
178 " if ( ++" << P() << " == " << PE() << " )\n"
179 " goto _test_eof" << state
->id
<< ";\n";
183 " " << P() << " += 1;\n";
187 /* Give the state a switch case. */
188 out
<< "case " << state
->id
<< ":\n";
190 if ( state
->fromStateAction
!= 0 ) {
191 /* Remember that we wrote an action. Write every action in the list. */
193 for ( ActionTable::Iter item
= state
->fromStateAction
->key
; item
.lte(); item
++ )
194 ACTION( out
, item
->value
, state
->id
, false );
198 genLineDirective( out
);
200 /* Record the prev state if necessary. */
201 if ( state
->anyRegCurStateRef() )
202 out
<< " _ps = " << state
->id
<< ";\n";
205 void CSharpIpGotoCodeGen::STATE_GOTO_ERROR()
207 /* In the error state we need to emit some stuff that usually goes into
209 RedStateAp
*state
= redFsm
->errState
;
210 bool anyWritten
= IN_TRANS_ACTIONS( state
);
212 /* No case label needed since we don't switch on the error state. */
214 genLineDirective( out
);
216 if ( state
->labelNeeded
)
217 out
<< "st" << state
->id
<< ":\n";
219 /* Break out here. */
221 out
<< CS() << " = " << state
->id
<< ";\n";
222 out
<< " goto _out;\n";
226 /* Emit the goto to take for a given transition. */
227 std::ostream
&CSharpIpGotoCodeGen::TRANS_GOTO( RedTransAp
*trans
, int level
)
229 if ( trans
->action
!= 0 ) {
230 /* Go to the transition which will go to the state. */
231 out
<< TABS(level
) << "goto tr" << trans
->id
<< ";";
234 /* Go directly to the target state. */
235 out
<< TABS(level
) << "goto st" << trans
->targ
->id
<< ";";
240 std::ostream
&CSharpIpGotoCodeGen::EXIT_STATES()
242 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
243 if ( st
->outNeeded
) {
245 out
<< " _test_eof" << st
->id
<< ": " << CS() << " = " <<
246 st
->id
<< "; goto _test_eof; \n";
252 std::ostream
&CSharpIpGotoCodeGen::AGAIN_CASES()
254 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
256 " case " << st
->id
<< ": goto st" << st
->id
<< ";\n";
261 std::ostream
&CSharpIpGotoCodeGen::FINISH_CASES()
263 bool anyWritten
= false;
265 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
266 if ( st
->eofAction
!= 0 ) {
267 if ( st
->eofAction
->eofRefs
== 0 )
268 st
->eofAction
->eofRefs
= new IntSet
;
269 st
->eofAction
->eofRefs
->insert( st
->id
);
273 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
274 if ( st
->eofTrans
!= 0 )
275 out
<< " case " << st
->id
<< ": goto tr" << st
->eofTrans
->id
<< ";\n";
278 for ( ActionTableMap::Iter act
= redFsm
->actionMap
; act
.lte(); act
++ ) {
279 if ( act
->eofRefs
!= 0 ) {
280 for ( IntSet::Iter pst
= *act
->eofRefs
; pst
.lte(); pst
++ )
281 out
<< " case " << *pst
<< ": \n";
283 /* Remember that we wrote a trans so we know to write the
284 * line directive for going back to the output. */
287 /* Write each action in the eof action list. */
288 for ( ActionTable::Iter item
= act
->key
; item
.lte(); item
++ )
289 ACTION( out
, item
->value
, STATE_ERR_STATE
, true );
295 genLineDirective( out
);
299 void CSharpIpGotoCodeGen::setLabelsNeeded( InlineList
*inlineList
)
301 for ( InlineList::Iter item
= *inlineList
; item
.lte(); item
++ ) {
302 switch ( item
->type
) {
303 case InlineItem::Goto
: case InlineItem::Call
: {
304 /* Mark the target as needing a label. */
305 item
->targState
->labelNeeded
= true;
311 if ( item
->children
!= 0 )
312 setLabelsNeeded( item
->children
);
316 /* Set up labelNeeded flag for each state. */
317 void CSharpIpGotoCodeGen::setLabelsNeeded()
319 /* If we use the _again label, then we the _again switch, which uses all
321 if ( useAgainLabel() ) {
322 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
323 st
->labelNeeded
= true;
326 /* Do not use all labels by default, init all labelNeeded vars to false. */
327 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
328 st
->labelNeeded
= false;
330 /* Walk all transitions and set only those that have targs. */
331 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
332 /* If there is no action with a next statement, then the label will be
334 if ( trans
->action
== 0 || !trans
->action
->anyNextStmt() )
335 trans
->targ
->labelNeeded
= true;
337 /* Need labels for states that have goto or calls in action code
338 * invoked on characters (ie, not from out action code). */
339 if ( trans
->action
!= 0 ) {
340 /* Loop the actions. */
341 for ( ActionTable::Iter act
= trans
->action
->key
; act
.lte(); act
++ ) {
342 /* Get the action and walk it's tree. */
343 setLabelsNeeded( act
->value
->inlineList
);
350 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
351 if ( st
!= redFsm
->errState
)
352 st
->outNeeded
= st
->labelNeeded
;
357 void CSharpIpGotoCodeGen::writeData()
362 void CSharpIpGotoCodeGen::writeExec()
364 /* Must set labels immediately before writing because we may depend on the
365 * noend write option. */
368 outLabelUsed
= false;
372 if ( redFsm
->anyRegCurStateRef() )
373 out
<< " int _ps = 0;\n";
375 if ( redFsm
->anyConditions() )
376 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
381 " if ( " << P() << " == " << PE() << " )\n"
382 " goto _test_eof;\n";
385 if ( useAgainLabel() ) {
390 " switch ( " << CS() << " ) {\n";
399 " if ( ++" << P() << " == " << PE() << " )\n"
400 " goto _test_eof;\n";
404 " " << P() << " += 1;\n";
411 " switch ( " << CS() << " )\n {\n";
419 out
<< " _test_eof: {}\n";
421 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
423 " if ( " << P() << " == " << EOFV() << " )\n"
425 " switch ( " << CS() << " ) {\n";
434 out
<< " _out: {}\n";