2 * Copyright 2006 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 /* Emit the goto to take for a given transition. */
33 std::ostream
&CSharpSplitCodeGen::TRANS_GOTO( RedTransAp
*trans
, int level
)
35 if ( trans
->targ
->partition
== currentPartition
) {
36 if ( trans
->action
!= 0 ) {
37 /* Go to the transition which will go to the state. */
38 out
<< TABS(level
) << "goto tr" << trans
->id
<< ";";
41 /* Go directly to the target state. */
42 out
<< TABS(level
) << "goto st" << trans
->targ
->id
<< ";";
46 if ( trans
->action
!= 0 ) {
47 /* Go to the transition which will go to the state. */
48 out
<< TABS(level
) << "goto ptr" << trans
->id
<< ";";
49 trans
->partitionBoundary
= true;
52 /* Go directly to the target state. */
53 out
<< TABS(level
) << "goto pst" << trans
->targ
->id
<< ";";
54 trans
->targ
->partitionBoundary
= true;
60 /* Called from before writing the gotos for each state. */
61 void CSharpSplitCodeGen::GOTO_HEADER( RedStateAp
*state
, bool stateInPartition
)
63 bool anyWritten
= IN_TRANS_ACTIONS( state
);
65 if ( state
->labelNeeded
)
66 out
<< "st" << state
->id
<< ":\n";
68 if ( state
->toStateAction
!= 0 ) {
69 /* Remember that we wrote an action. Write every action in the list. */
71 for ( GenActionTable::Iter item
= state
->toStateAction
->key
; item
.lte(); item
++ )
72 ACTION( out
, item
->value
, state
->id
, false );
75 /* Advance and test buffer pos. */
76 if ( state
->labelNeeded
) {
79 " if ( ++" << P() << " == " << PE() << " )\n"
80 " goto _out" << state
->id
<< ";\n";
84 " " << P() << " += 1;\n";
88 /* Give the state a switch case. */
89 out
<< "case " << state
->id
<< ":\n";
91 if ( state
->fromStateAction
!= 0 ) {
92 /* Remember that we wrote an action. Write every action in the list. */
94 for ( GenActionTable::Iter item
= state
->fromStateAction
->key
; item
.lte(); item
++ )
95 ACTION( out
, item
->value
, state
->id
, false );
99 genLineDirective( out
);
101 /* Record the prev state if necessary. */
102 if ( state
->anyRegCurStateRef() )
103 out
<< " _ps = " << state
->id
<< ";\n";
106 std::ostream
&CSharpSplitCodeGen::STATE_GOTOS( int partition
)
108 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
109 if ( st
->partition
== partition
) {
110 if ( st
== redFsm
->errState
)
113 /* We call into the base of the goto which calls back into us
114 * using virtual functions. Set the current partition rather
115 * than coding parameter passing throughout. */
116 currentPartition
= partition
;
118 /* Writing code above state gotos. */
119 GOTO_HEADER( st
, st
->partition
== partition
);
121 if ( st
->stateCondVect
.length() > 0 ) {
122 out
<< " _widec = " << GET_KEY() << ";\n";
123 emitCondBSearch( st
, 1, 0, st
->stateCondVect
.length() - 1 );
127 if ( st
->outSingle
.length() > 0 )
128 emitSingleSwitch( st
);
130 /* Default case is to binary search for the ranges, if that fails then */
131 if ( st
->outRange
.length() > 0 )
132 emitRangeBSearch( st
, 1, 0, st
->outRange
.length() - 1 );
134 /* Write the default transition. */
135 TRANS_GOTO( st
->defTrans
, 1 ) << "\n";
143 std::ostream
&CSharpSplitCodeGen::PART_TRANS( int partition
)
145 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
146 if ( trans
->partitionBoundary
) {
148 "ptr" << trans
->id
<< ":\n";
150 if ( trans
->action
!= 0 ) {
151 /* If the action contains a next, then we must preload the current
152 * state since the action may or may not set it. */
153 if ( trans
->action
->anyNextStmt() )
154 out
<< " " << vCS() << " = " << trans
->targ
->id
<< ";\n";
156 /* Write each action in the list. */
157 for ( GenActionTable::Iter item
= trans
->action
->key
; item
.lte(); item
++ )
158 ACTION( out
, item
->value
, trans
->targ
->id
, false );
162 " goto pst" << trans
->targ
->id
<< ";\n";
163 trans
->targ
->partitionBoundary
= true;
167 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
168 if ( st
->partitionBoundary
) {
170 " pst" << st
->id
<< ":\n"
171 " " << vCS() << " = " << st
->id
<< ";\n";
173 if ( st
->toStateAction
!= 0 ) {
174 /* Remember that we wrote an action. Write every action in the list. */
175 for ( GenActionTable::Iter item
= st
->toStateAction
->key
; item
.lte(); item
++ )
176 ACTION( out
, item
->value
, st
->id
, false );
177 genLineDirective( out
);
180 ptOutLabelUsed
= true;
181 out
<< " goto _pt_out; \n";
187 std::ostream
&CSharpSplitCodeGen::EXIT_STATES( int partition
)
189 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
190 if ( st
->partition
== partition
&& st
->outNeeded
) {
192 out
<< " _out" << st
->id
<< ": " << vCS() << " = " <<
193 st
->id
<< "; goto _out; \n";
200 std::ostream
&CSharpSplitCodeGen::PARTITION( int partition
)
202 outLabelUsed
= false;
203 ptOutLabelUsed
= false;
205 /* Initialize the partition boundaries, which get set during the writing
206 * of states. After the state writing we will */
207 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
208 trans
->partitionBoundary
= false;
209 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
210 st
->partitionBoundary
= false;
212 out
<< " " << ALPH_TYPE() << " *p = *_pp, *pe = *_ppe;\n";
214 if ( redFsm
->anyRegCurStateRef() )
215 out
<< " int _ps = 0;\n";
217 if ( redFsm
->anyConditions() )
218 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
220 if ( useAgainLabel() ) {
225 " switch ( " << vCS() << " ) {\n";
235 " if ( ++" << P() << " == " << PE() << " )\n"
241 " " << P() << " += 1;\n";
249 " switch ( " << vCS() << " )\n {\n";
250 STATE_GOTOS( partition
);
253 PART_TRANS( partition
);
254 EXIT_STATES( partition
);
256 if ( outLabelUsed
) {
265 if ( ptOutLabelUsed
) {
277 std::ostream
&CSharpSplitCodeGen::PART_MAP()
279 int *partMap
= new int[redFsm
->stateList
.length()];
280 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
281 partMap
[st
->id
] = st
->partition
;
285 for ( int i
= 0; i
< redFsm
->stateList
.length(); i
++ ) {
287 if ( i
!= redFsm
->stateList
.length() - 1 ) {
289 if ( ++totalItem
% IALL
== 0 )
298 void CSharpSplitCodeGen::writeData()
301 "const int " << START() << " = " << START_STATE_ID() << ";\n"
306 "const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
312 "const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
317 OPEN_ARRAY( ARRAY_TYPE(numSplitPartitions
), PM() );
322 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
323 out
<< "int partition" << p
<< "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
324 " **_ppe, struct " << FSM_NAME() << " *fsm );\n";
329 std::ostream
&CSharpSplitCodeGen::ALL_PARTITIONS()
331 /* compute the format string. */
332 int width
= 0, high
= redFsm
->nParts
- 1;
337 assert( width
<= 8 );
338 char suffFormat
[] = "_%6.6d.c";
339 suffFormat
[2] = suffFormat
[4] = ( '0' + width
);
341 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
343 sprintf( suffix
, suffFormat
, p
);
344 const char *fn
= fileNameFromStem( sourceFileName
, suffix
);
345 const char *include
= fileNameFromStem( sourceFileName
, ".h" );
347 /* Create the filter on the output and open it. */
348 output_filter
*partFilter
= new output_filter( fn
);
349 partFilter
->open( fn
, ios::out
|ios::trunc
);
350 if ( !partFilter
->is_open() ) {
351 error() << "error opening " << fn
<< " for writing" << endl
;
355 /* Attach the new file to the output stream. */
356 std::streambuf
*prev_rdbuf
= out
.rdbuf( partFilter
);
359 "#include \"" << include
<< "\"\n"
360 "int partition" << p
<< "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
361 " **_ppe, struct " << FSM_NAME() << " *fsm )\n"
367 /* Fix the output stream. */
368 out
.rdbuf( prev_rdbuf
);
374 void CSharpSplitCodeGen::writeExec()
376 /* Must set labels immediately before writing because we may depend on the
377 * noend write option. */
385 " if ( " << P() << " == " << PE() << " )\n"
389 out
<< " goto _resume;\n";
391 /* In this reentry, to-state actions have already been executed on the
392 * partition-switch exit from the last partition. */
393 out
<< "_reenter:\n";
397 " if ( ++" << P() << " == " << PE() << " )\n"
402 " " << P() << " += 1;\n";
408 " switch ( " << PM() << "[" << vCS() << "] ) {\n";
409 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
411 " case " << p
<< ":\n"
412 " _stat = partition" << p
<< "( &p, &pe, fsm );\n"
421 out
<< " _out: {}\n";
429 void CSharpSplitCodeGen::setLabelsNeeded( RedStateAp
*fromState
, GenInlineList
*inlineList
)
431 for ( GenInlineList::Iter item
= *inlineList
; item
.lte(); item
++ ) {
432 switch ( item
->type
) {
433 case GenInlineItem::Goto
: case GenInlineItem::Call
: {
434 /* In split code gen we only need labels for transitions across
436 if ( fromState
->partition
== item
->targState
->partition
){
437 /* Mark the target as needing a label. */
438 item
->targState
->labelNeeded
= true;
445 if ( item
->children
!= 0 )
446 setLabelsNeeded( fromState
, item
->children
);
450 void CSharpSplitCodeGen::setLabelsNeeded( RedStateAp
*fromState
, RedTransAp
*trans
)
452 /* In the split code gen we don't need labels for transitions across
454 if ( fromState
->partition
== trans
->targ
->partition
) {
455 /* If there is no action with a next statement, then the label will be
457 trans
->labelNeeded
= true;
458 if ( trans
->action
== 0 || !trans
->action
->anyNextStmt() )
459 trans
->targ
->labelNeeded
= true;
462 /* Need labels for states that have goto or calls in action code
463 * invoked on characters (ie, not from out action code). */
464 if ( trans
->action
!= 0 ) {
465 /* Loop the actions. */
466 for ( GenActionTable::Iter act
= trans
->action
->key
; act
.lte(); act
++ ) {
467 /* Get the action and walk it's tree. */
468 setLabelsNeeded( fromState
, act
->value
->inlineList
);
473 /* Set up labelNeeded flag for each state. */
474 void CSharpSplitCodeGen::setLabelsNeeded()
476 /* If we use the _again label, then we the _again switch, which uses all
478 if ( useAgainLabel() ) {
479 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
480 st
->labelNeeded
= true;
483 /* Do not use all labels by default, init all labelNeeded vars to false. */
484 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
485 st
->labelNeeded
= false;
486 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
487 trans
->labelNeeded
= false;
489 /* Walk all transitions and set only those that have targs. */
490 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
491 for ( RedTransList::Iter tel
= st
->outRange
; tel
.lte(); tel
++ )
492 setLabelsNeeded( st
, tel
->value
);
494 for ( RedTransList::Iter tel
= st
->outSingle
; tel
.lte(); tel
++ )
495 setLabelsNeeded( st
, tel
->value
);
497 if ( st
->defTrans
!= 0 )
498 setLabelsNeeded( st
, st
->defTrans
);
503 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
504 st
->outNeeded
= st
->labelNeeded
;
507 if ( redFsm
->errState
!= 0 )
508 redFsm
->errState
->outNeeded
= true;
510 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
511 /* Any state with a transition in that has a break will need an
513 if ( trans
->action
!= 0 && trans
->action
->anyBreakStmt() )
514 trans
->targ
->outNeeded
= true;