2 * Copyright 2006 Adrian Thurston <thurston@cs.queensu.ca>
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
24 #include "splitcodegen.h"
32 /* Emit the goto to take for a given transition. */
33 std::ostream
&SplitCodeGen::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 SplitCodeGen::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 ( ActionTable::Iter item
= state
->toStateAction
->key
; item
.lte(); item
++ ) {
72 ACTION( out
, item
->value
, state
->id
, false,
73 state
->toStateAction
->anyNextStmt() );
77 /* Advance and test buffer pos. */
78 if ( state
->labelNeeded
) {
81 " if ( ++" << P() << " == " << PE() << " )\n"
82 " goto _out" << state
->id
<< ";\n";
86 " " << P() << " += 1;\n";
90 /* Give the state a switch case. */
91 out
<< "case " << state
->id
<< ":\n";
93 if ( state
->fromStateAction
!= 0 ) {
94 /* Remember that we wrote an action. Write every action in the list. */
96 for ( ActionTable::Iter item
= state
->fromStateAction
->key
; item
.lte(); item
++ ) {
97 ACTION( out
, item
->value
, state
->id
, false,
98 state
->fromStateAction
->anyNextStmt() );
103 genLineDirective( out
);
105 /* Record the prev state if necessary. */
106 if ( state
->anyRegCurStateRef() )
107 out
<< " _ps = " << state
->id
<< ";\n";
110 std::ostream
&SplitCodeGen::STATE_GOTOS( int partition
)
112 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
113 if ( st
->partition
== partition
) {
114 if ( st
== redFsm
->errState
)
117 /* We call into the base of the goto which calls back into us
118 * using virtual functions. Set the current partition rather
119 * than coding parameter passing throughout. */
120 currentPartition
= partition
;
122 /* Writing code above state gotos. */
123 GOTO_HEADER( st
, st
->partition
== partition
);
125 if ( st
->stateCondVect
.length() > 0 ) {
126 out
<< " _widec = " << GET_KEY() << ";\n";
127 emitCondBSearch( st
, 1, 0, st
->stateCondVect
.length() - 1 );
131 if ( st
->outSingle
.length() > 0 )
132 emitSingleSwitch( st
);
134 /* Default case is to binary search for the ranges, if that fails then */
135 if ( st
->outRange
.length() > 0 )
136 emitRangeBSearch( st
, 1, 0, st
->outRange
.length() - 1 );
138 /* Write the default transition. */
139 TRANS_GOTO( st
->defTrans
, 1 ) << "\n";
147 std::ostream
&SplitCodeGen::PART_TRANS( int partition
)
149 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
150 if ( trans
->partitionBoundary
) {
152 "ptr" << trans
->id
<< ":\n";
154 if ( trans
->action
!= 0 ) {
155 /* If the action contains a next, then we must preload the current
156 * state since the action may or may not set it. */
157 if ( trans
->action
->anyNextStmt() )
158 out
<< " " << CS() << " = " << trans
->targ
->id
<< ";\n";
160 /* Write each action in the list. */
161 for ( ActionTable::Iter item
= trans
->action
->key
; item
.lte(); item
++ ) {
162 ACTION( out
, item
->value
, trans
->targ
->id
, false,
163 trans
->action
->anyNextStmt() );
168 " goto pst" << trans
->targ
->id
<< ";\n";
169 trans
->targ
->partitionBoundary
= true;
173 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
174 if ( st
->partitionBoundary
) {
176 " pst" << st
->id
<< ":\n"
177 " " << CS() << " = " << st
->id
<< ";\n";
179 if ( st
->toStateAction
!= 0 ) {
180 /* Remember that we wrote an action. Write every action in the list. */
181 for ( ActionTable::Iter item
= st
->toStateAction
->key
; item
.lte(); item
++ ) {
182 ACTION( out
, item
->value
, st
->id
, false,
183 st
->toStateAction
->anyNextStmt() );
185 genLineDirective( out
);
188 ptOutLabelUsed
= true;
189 out
<< " goto _pt_out; \n";
195 std::ostream
&SplitCodeGen::EXIT_STATES( int partition
)
197 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
198 if ( st
->partition
== partition
&& st
->outNeeded
) {
200 out
<< " _out" << st
->id
<< ": " << CS() << " = " <<
201 st
->id
<< "; goto _out; \n";
208 std::ostream
&SplitCodeGen::PARTITION( int partition
)
210 outLabelUsed
= false;
211 ptOutLabelUsed
= false;
213 /* Initialize the partition boundaries, which get set during the writing
214 * of states. After the state writing we will */
215 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
216 trans
->partitionBoundary
= false;
217 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
218 st
->partitionBoundary
= false;
220 out
<< " " << ALPH_TYPE() << " *p = *_pp, *pe = *_ppe;\n";
222 if ( redFsm
->anyRegCurStateRef() )
223 out
<< " int _ps = 0;\n";
225 if ( redFsm
->anyConditions() )
226 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
228 if ( useAgainLabel() ) {
233 " switch ( " << CS() << " ) {\n";
243 " if ( ++" << P() << " == " << PE() << " )\n"
249 " " << P() << " += 1;\n";
257 " switch ( " << CS() << " )\n {\n";
258 STATE_GOTOS( partition
);
261 PART_TRANS( partition
);
262 EXIT_STATES( partition
);
264 if ( outLabelUsed
) {
273 if ( ptOutLabelUsed
) {
285 std::ostream
&SplitCodeGen::PART_MAP()
287 int *partMap
= new int[redFsm
->stateList
.length()];
288 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
289 partMap
[st
->id
] = st
->partition
;
293 for ( int i
= 0; i
< redFsm
->stateList
.length(); i
++ ) {
295 if ( i
!= redFsm
->stateList
.length() - 1 ) {
297 if ( ++totalItem
% IALL
== 0 )
306 void SplitCodeGen::writeData()
309 "static const int " << START() << " = " << START_STATE_ID() << ";\n"
312 if ( writeFirstFinal
) {
314 "static const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
320 "static const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
325 OPEN_ARRAY( ARRAY_TYPE(numSplitPartitions
), PM() );
330 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
331 out
<< "int partition" << p
<< "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
332 " **_ppe, struct " << FSM_NAME() << " *fsm );\n";
337 std::ostream
&SplitCodeGen::ALL_PARTITIONS()
339 /* compute the format string. */
340 int width
= 0, high
= redFsm
->nParts
- 1;
345 assert( width
<= 8 );
346 char suffFormat
[] = "_%6.6d.c";
347 suffFormat
[2] = suffFormat
[4] = ( '0' + width
);
349 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
351 sprintf( suffix
, suffFormat
, p
);
352 char *fn
= fileNameFromStem( sourceFileName
, suffix
);
353 char *include
= fileNameFromStem( sourceFileName
, ".h" );
355 /* Create the filter on the output and open it. */
356 output_filter
*partFilter
= new output_filter( fn
);
357 partFilter
->open( fn
, ios::out
|ios::trunc
);
358 if ( !partFilter
->is_open() ) {
359 error() << "error opening " << fn
<< " for writing" << endl
;
363 /* Attach the new file to the output stream. */
364 std::streambuf
*prev_rdbuf
= out
.rdbuf( partFilter
);
367 "#include \"" << include
<< "\"\n"
368 "int partition" << p
<< "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
369 " **_ppe, struct " << FSM_NAME() << " *fsm )\n"
375 /* Fix the output stream. */
376 out
.rdbuf( prev_rdbuf
);
382 void SplitCodeGen::writeExec()
384 /* Must set labels immediately before writing because we may depend on the
385 * noend write option. */
393 " if ( " << P() << " == " << PE() << " )\n"
397 out
<< " goto _resume;\n";
399 /* In this reentry, to-state actions have already been executed on the
400 * partition-switch exit from the last partition. */
401 out
<< "_reenter:\n";
405 " if ( ++" << P() << " == " << PE() << " )\n"
410 " " << P() << " += 1;\n";
416 " switch ( " << PM() << "[" << CS() << "] ) {\n";
417 for ( int p
= 0; p
< redFsm
->nParts
; p
++ ) {
419 " case " << p
<< ":\n"
420 " _stat = partition" << p
<< "( &p, &pe, fsm );\n"
429 out
<< " _out: {}\n";
437 void SplitCodeGen::setLabelsNeeded( RedStateAp
*fromState
, InlineList
*inlineList
)
439 for ( InlineList::Iter item
= *inlineList
; item
.lte(); item
++ ) {
440 switch ( item
->type
) {
441 case InlineItem::Goto
: case InlineItem::Call
: {
442 /* In split code gen we only need labels for transitions across
444 if ( fromState
->partition
== item
->targState
->partition
){
445 /* Mark the target as needing a label. */
446 item
->targState
->labelNeeded
= true;
453 if ( item
->children
!= 0 )
454 setLabelsNeeded( fromState
, item
->children
);
458 void SplitCodeGen::setLabelsNeeded( RedStateAp
*fromState
, RedTransAp
*trans
)
460 /* In the split code gen we don't need labels for transitions across
462 if ( fromState
->partition
== trans
->targ
->partition
) {
463 /* If there is no action with a next statement, then the label will be
465 trans
->labelNeeded
= true;
466 if ( trans
->action
== 0 || !trans
->action
->anyNextStmt() )
467 trans
->targ
->labelNeeded
= true;
470 /* Need labels for states that have goto or calls in action code
471 * invoked on characters (ie, not from out action code). */
472 if ( trans
->action
!= 0 ) {
473 /* Loop the actions. */
474 for ( ActionTable::Iter act
= trans
->action
->key
; act
.lte(); act
++ ) {
475 /* Get the action and walk it's tree. */
476 setLabelsNeeded( fromState
, act
->value
->inlineList
);
481 /* Set up labelNeeded flag for each state. */
482 void SplitCodeGen::setLabelsNeeded()
484 /* If we use the _again label, then we the _again switch, which uses all
486 if ( useAgainLabel() ) {
487 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
488 st
->labelNeeded
= true;
491 /* Do not use all labels by default, init all labelNeeded vars to false. */
492 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
493 st
->labelNeeded
= false;
494 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ )
495 trans
->labelNeeded
= false;
497 /* Walk all transitions and set only those that have targs. */
498 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
499 for ( RedTransList::Iter tel
= st
->outRange
; tel
.lte(); tel
++ )
500 setLabelsNeeded( st
, tel
->value
);
502 for ( RedTransList::Iter tel
= st
->outSingle
; tel
.lte(); tel
++ )
503 setLabelsNeeded( st
, tel
->value
);
505 if ( st
->defTrans
!= 0 )
506 setLabelsNeeded( st
, st
->defTrans
);
511 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ )
512 st
->outNeeded
= st
->labelNeeded
;
515 if ( redFsm
->errState
!= 0 )
516 redFsm
->errState
->outNeeded
= true;
518 for ( TransApSet::Iter trans
= redFsm
->transSet
; trans
.lte(); trans
++ ) {
519 /* Any state with a transition in that has a break will need an
521 if ( trans
->action
!= 0 && trans
->action
->anyBreakStmt() )
522 trans
->targ
->outNeeded
= true;