2 * Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
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
29 /* Determine if we should use indicies or not. */
30 void CSharpFTabCodeGen::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 &CSharpFTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
60 if ( state->toStateAction != 0 )
61 act = state->toStateAction->actListId+1;
66 std::ostream &CSharpFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
69 if ( state->fromStateAction != 0 )
70 act = state->fromStateAction->actListId+1;
75 std::ostream &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::TO_STATE_ACTION_SWITCH()
99 /* Loop the actions. */
100 for ( GenActionTableMap::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 ( GenActionTable::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 &CSharpFTabCodeGen::FROM_STATE_ACTION_SWITCH()
121 /* Loop the actions. */
122 for ( GenActionTableMap::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 ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
129 ACTION( out, item->value, 0, false );
135 genLineDirective( out );
139 std::ostream &CSharpFTabCodeGen::EOF_ACTION_SWITCH()
141 /* Loop the actions. */
142 for ( GenActionTableMap::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 ( GenActionTable::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 &CSharpFTabCodeGen::ACTION_SWITCH()
163 /* Loop the actions. */
164 for ( GenActionTableMap::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 ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
171 ACTION( out, item->value, 0, false );
177 genLineDirective( out );
181 void CSharpFTabCodeGen::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->maxIndexOffset+1), ET() );
293 void CSharpFTabCodeGen::writeExec()
296 outLabelUsed = false;
301 " " << klenType << " _klen";
303 if ( redFsm->anyRegCurStateRef() )
308 " " << keysType << " _keys;\n"
309 " " << transType << " _trans;\n";
311 if ( redFsm->anyConditions() )
312 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
319 " if ( " << P() << " == " << PE() << " )\n"
320 " goto _test_eof;\n";
323 if ( redFsm->errState != 0 ) {
326 " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
332 if ( redFsm->anyFromStateActions() ) {
334 " switch ( " << FSA() << "[" << vCS() << "] ) {\n";
335 FROM_STATE_ACTION_SWITCH();
341 if ( redFsm->anyConditions() )
349 out << " _trans = " << CAST(transType) << I() << "[_trans];\n";
351 if ( redFsm->anyEofTrans() )
352 out << "_eof_trans:\n";
354 if ( redFsm->anyRegCurStateRef() )
355 out << " _ps = " << vCS() << ";\n";
358 " " << vCS() << " = " << TT() << "[_trans];\n"
361 if ( redFsm->anyRegActions() ) {
363 " if ( " << TA() << "[_trans] == 0 )\n"
366 " switch ( " << TA() << "[_trans] ) {\n";
373 if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
374 redFsm->anyActionCalls() || redFsm->anyActionRets() )
377 if ( redFsm->anyToStateActions() ) {
379 " switch ( " << TSA() << "[" << vCS() << "] ) {\n";
380 TO_STATE_ACTION_SWITCH();
386 if ( redFsm->errState != 0 ) {
389 " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
395 " if ( ++" << P() << " != " << PE() << " )\n"
400 " " << P() << " += 1;\n"
405 out << " _test_eof: {}\n";
407 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
409 " if ( " << P() << " == " << vEOF() << " )\n"
412 if ( redFsm->anyEofTrans() ) {
414 " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
415 " _trans = " << CAST(transType) << " (" << ET() <<
416 "[" << vCS() << "] - 1);\n"
417 " goto _eof_trans;\n"
421 if ( redFsm->anyEofActions() ) {
423 " switch ( " << EA() << "[" << vCS() << "] ) {\n";
435 out << " _out: {}\n";