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
25 #include "ftabcodegen.h"
29 /* Determine if we should use indicies or not. */
30 void FTabCodeGen::calcIndexSize()
32 int sizeWithInds
= 0, sizeWithoutInds
= 0;
34 /* Calculate cost of using with indicies. */
35 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
36 int totalIndex
= st
->outSingle
.length() + st
->outRange
.length() +
37 (st
->defTrans
== 0 ? 0 : 1);
38 sizeWithInds
+= arrayTypeSize(redFsm
->maxIndex
) * totalIndex
;
40 sizeWithInds
+= arrayTypeSize(redFsm
->maxState
) * redFsm
->transSet
.length();
41 if ( redFsm
->anyActions() )
42 sizeWithInds
+= arrayTypeSize(redFsm
->maxActListId
) * redFsm
->transSet
.length();
44 /* Calculate the cost of not using indicies. */
45 for ( RedStateList::Iter st
= redFsm
->stateList
; st
.lte(); st
++ ) {
46 int totalIndex
= st
->outSingle
.length() + st
->outRange
.length() +
47 (st
->defTrans
== 0 ? 0 : 1);
48 sizeWithoutInds
+= arrayTypeSize(redFsm
->maxState
) * totalIndex
;
49 if ( redFsm
->anyActions() )
50 sizeWithoutInds
+= arrayTypeSize(redFsm
->maxActListId
) * totalIndex
;
53 /* If using indicies reduces the size, use them. */
54 useIndicies
= sizeWithInds
< sizeWithoutInds
;
57 std::ostream
&FTabCodeGen::TO_STATE_ACTION( RedStateAp
*state
)
60 if ( state
->toStateAction
!= 0 )
61 act
= state
->toStateAction
->actListId
+1;
66 std::ostream
&FTabCodeGen::FROM_STATE_ACTION( RedStateAp
*state
)
69 if ( state
->fromStateAction
!= 0 )
70 act
= state
->fromStateAction
->actListId
+1;
75 std::ostream
&FTabCodeGen::EOF_ACTION( RedStateAp
*state
)
78 if ( state
->eofAction
!= 0 )
79 act
= state
->eofAction
->actListId
+1;
85 /* Write out the function for a transition. */
86 std::ostream
&FTabCodeGen::TRANS_ACTION( RedTransAp
*trans
)
89 if ( trans
->action
!= 0 )
90 action
= trans
->action
->actListId
+1;
95 /* Write out the function switch. This switch is keyed on the values
96 * of the func index. */
97 std::ostream
&FTabCodeGen::TO_STATE_ACTION_SWITCH()
99 /* Loop the actions. */
100 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
101 if ( redAct
->numToStateRefs
> 0 ) {
102 /* Write the entry label. */
103 out
<< "\tcase " << redAct
->actListId
+1 << ":\n";
105 /* Write each action in the list of action items. */
106 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
107 ACTION( out
, item
->value
, 0, false );
113 genLineDirective( out
);
117 /* Write out the function switch. This switch is keyed on the values
118 * of the func index. */
119 std::ostream
&FTabCodeGen::FROM_STATE_ACTION_SWITCH()
121 /* Loop the actions. */
122 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
123 if ( redAct
->numFromStateRefs
> 0 ) {
124 /* Write the entry label. */
125 out
<< "\tcase " << redAct
->actListId
+1 << ":\n";
127 /* Write each action in the list of action items. */
128 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
129 ACTION( out
, item
->value
, 0, false );
135 genLineDirective( out
);
139 std::ostream
&FTabCodeGen::EOF_ACTION_SWITCH()
141 /* Loop the actions. */
142 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
143 if ( redAct
->numEofRefs
> 0 ) {
144 /* Write the entry label. */
145 out
<< "\tcase " << redAct
->actListId
+1 << ":\n";
147 /* Write each action in the list of action items. */
148 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
149 ACTION( out
, item
->value
, 0, true );
155 genLineDirective( out
);
159 /* Write out the function switch. This switch is keyed on the values
160 * of the func index. */
161 std::ostream
&FTabCodeGen::ACTION_SWITCH()
163 /* Loop the actions. */
164 for ( ActionTableMap::Iter redAct
= redFsm
->actionMap
; redAct
.lte(); redAct
++ ) {
165 if ( redAct
->numTransRefs
> 0 ) {
166 /* Write the entry label. */
167 out
<< "\tcase " << redAct
->actListId
+1 << ":\n";
169 /* Write each action in the list of action items. */
170 for ( ActionTable::Iter item
= redAct
->key
; item
.lte(); item
++ )
171 ACTION( out
, item
->value
, 0, false );
177 genLineDirective( out
);
181 void FTabCodeGen::writeData()
183 if ( redFsm
->anyConditions() ) {
184 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondOffset
), CO() );
189 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondLen
), CL() );
194 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
199 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxCondSpaceId
), C() );
205 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxKeyOffset
), KO() );
210 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
215 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxSingleLen
), SL() );
220 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxRangeLen
), RL() );
225 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndexOffset
), IO() );
231 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
), I() );
236 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
241 if ( redFsm
->anyActions() ) {
242 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
249 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxState
), TT() );
254 if ( redFsm
->anyActions() ) {
255 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), TA() );
262 if ( redFsm
->anyToStateActions() ) {
263 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), TSA() );
269 if ( redFsm
->anyFromStateActions() ) {
270 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActionLoc
), FSA() );
271 FROM_STATE_ACTIONS();
276 if ( redFsm
->anyEofActions() ) {
277 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxActListId
), EA() );
283 if ( redFsm
->anyEofTrans() ) {
284 OPEN_ARRAY( ARRAY_TYPE(redFsm
->maxIndex
+1), ET() );
293 void FTabCodeGen::writeExec()
296 outLabelUsed
= false;
302 if ( redFsm
->anyRegCurStateRef() )
307 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
310 if ( redFsm
->anyConditions() )
311 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
318 " if ( " << P() << " == " << PE() << " )\n"
319 " goto _test_eof;\n";
322 if ( redFsm
->errState
!= 0 ) {
325 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
331 if ( redFsm
->anyFromStateActions() ) {
333 " switch ( " << FSA() << "[" << CS() << "] ) {\n";
334 FROM_STATE_ACTION_SWITCH();
340 if ( redFsm
->anyConditions() )
348 out
<< " _trans = " << I() << "[_trans];\n";
350 if ( redFsm
->anyEofTrans() )
351 out
<< "_eof_trans:\n";
353 if ( redFsm
->anyRegCurStateRef() )
354 out
<< " _ps = " << CS() << ";\n";
357 " " << CS() << " = " << TT() << "[_trans];\n"
360 if ( redFsm
->anyRegActions() ) {
362 " if ( " << TA() << "[_trans] == 0 )\n"
365 " switch ( " << TA() << "[_trans] ) {\n";
372 if ( redFsm
->anyRegActions() || redFsm
->anyActionGotos() ||
373 redFsm
->anyActionCalls() || redFsm
->anyActionRets() )
376 if ( redFsm
->anyToStateActions() ) {
378 " switch ( " << TSA() << "[" << CS() << "] ) {\n";
379 TO_STATE_ACTION_SWITCH();
385 if ( redFsm
->errState
!= 0 ) {
388 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
394 " if ( ++" << P() << " != " << PE() << " )\n"
399 " " << P() << " += 1;\n"
404 out
<< " _test_eof: {}\n";
406 if ( redFsm
->anyEofTrans() || redFsm
->anyEofActions() ) {
408 " if ( " << P() << " == " << EOFV() << " )\n"
411 if ( redFsm
->anyEofTrans() ) {
413 " if ( " << ET() << "[" << CS() << "] > 0 ) {\n"
414 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
415 " goto _eof_trans;\n"
419 if ( redFsm
->anyEofActions() ) {
421 " switch ( " << EA() << "[" << CS() << "] ) {\n";
433 out
<< " _out: {}\n";