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() );
286 void FTabCodeGen::writeExec()
289 outLabelUsed
= false;
295 if ( redFsm
->anyRegCurStateRef() )
300 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
303 if ( redFsm
->anyConditions() )
304 out
<< " " << WIDE_ALPH_TYPE() << " _widec;\n";
311 " if ( " << P() << " == " << PE() << " )\n"
312 " goto _test_eof;\n";
315 if ( redFsm
->errState
!= 0 ) {
318 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
324 if ( redFsm
->anyFromStateActions() ) {
326 " switch ( " << FSA() << "[" << CS() << "] ) {\n";
327 FROM_STATE_ACTION_SWITCH();
333 if ( redFsm
->anyConditions() )
340 if ( redFsm
->anyRegCurStateRef() )
341 out
<< " _ps = " << CS() << ";\n";
344 out
<< " _trans = " << I() << "[_trans];\n";
347 " " << CS() << " = " << TT() << "[_trans];\n"
350 if ( redFsm
->anyRegActions() ) {
352 " if ( " << TA() << "[_trans] == 0 )\n"
355 " switch ( " << TA() << "[_trans] ) {\n";
362 if ( redFsm
->anyRegActions() || redFsm
->anyActionGotos() ||
363 redFsm
->anyActionCalls() || redFsm
->anyActionRets() )
366 if ( redFsm
->anyToStateActions() ) {
368 " switch ( " << TSA() << "[" << CS() << "] ) {\n";
369 TO_STATE_ACTION_SWITCH();
375 if ( redFsm
->errState
!= 0 ) {
378 " if ( " << CS() << " == " << redFsm
->errState
->id
<< " )\n"
384 " if ( ++" << P() << " != " << PE() << " )\n"
389 " " << P() << " += 1;\n"
394 out
<< " _test_eof: {}\n";
396 if ( redFsm
->anyEofActions() ) {
398 " if ( " << P() << " == " << EOFV() << " )\n"
400 " switch ( " << EA() << "[" << CS() << "] ) {\n";
409 out
<< " _out: {}\n";
415 void FTabCodeGen::writeEOF()