2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Eric 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
24 #include "rlcodegen.h"
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(maxIndex) * totalIndex;
40 sizeWithInds += arrayTypeSize(maxState) * redFsm->transSet.length();
42 sizeWithInds += arrayTypeSize(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(maxState) * totalIndex;
50 sizeWithoutInds += arrayTypeSize(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::writeOutData()
183 if ( anyConditions() ) {
184 OPEN_ARRAY( ARRAY_TYPE(maxCondOffset), CO() );
189 OPEN_ARRAY( ARRAY_TYPE(maxCondLen), CL() );
194 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
199 OPEN_ARRAY( ARRAY_TYPE(maxCondSpaceId), C() );
205 OPEN_ARRAY( ARRAY_TYPE(maxKeyOffset), KO() );
210 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
215 OPEN_ARRAY( ARRAY_TYPE(maxSingleLen), SL() );
220 OPEN_ARRAY( ARRAY_TYPE(maxRangeLen), RL() );
225 OPEN_ARRAY( ARRAY_TYPE(maxIndexOffset), IO() );
231 OPEN_ARRAY( ARRAY_TYPE(maxIndex), I() );
236 OPEN_ARRAY( ARRAY_TYPE(maxState), TT() );
241 if ( anyActions() ) {
242 OPEN_ARRAY( ARRAY_TYPE(maxActListId), TA() );
249 OPEN_ARRAY( ARRAY_TYPE(maxState), TT() );
254 if ( anyActions() ) {
255 OPEN_ARRAY( ARRAY_TYPE(maxActListId), TA() );
262 if ( anyToStateActions() ) {
263 OPEN_ARRAY( ARRAY_TYPE(maxActionLoc), TSA() );
269 if ( anyFromStateActions() ) {
270 OPEN_ARRAY( ARRAY_TYPE(maxActionLoc), FSA() );
271 FROM_STATE_ACTIONS();
276 if ( anyEofActions() ) {
277 OPEN_ARRAY( ARRAY_TYPE(maxActListId), EA() );
284 "static const int " << START() << " = " << START_STATE_ID() << ";\n"
287 if ( cgd->writeFirstFinal ) {
289 "static const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
293 if ( cgd->writeErr ) {
295 "static const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
300 void FTabCodeGen::writeOutExec()
302 outLabelUsed = false;
308 if ( anyRegCurStateRef() )
313 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
316 if ( anyConditions() )
317 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
324 " if ( " << P() << " == " << PE() << " )\n"
330 if ( redFsm->errState != 0 ) {
333 " if ( " << CS() << " == " << redFsm->errState->id << " )\n"
337 if ( anyFromStateActions() ) {
339 " switch ( " << FSA() << "[" << CS() << "] ) {\n";
340 FROM_STATE_ACTION_SWITCH();
346 if ( anyConditions() )
353 if ( anyRegCurStateRef() )
354 out << " _ps = " << CS() << ";\n";
357 out << " _trans = " << I() << "[_trans];\n";
360 " " << CS() << " = " << TT() << "[_trans];\n"
363 if ( anyRegActions() ) {
365 " if ( " << TA() << "[_trans] == 0 )\n"
368 " switch ( " << TA() << "[_trans] ) {\n";
375 if ( anyRegActions() || anyActionGotos() || anyActionCalls() || anyActionRets() )
378 if ( anyToStateActions() ) {
380 " switch ( " << TSA() << "[" << CS() << "] ) {\n";
381 TO_STATE_ACTION_SWITCH();
389 " if ( ++" << P() << " != " << PE() << " )\n"
394 " " << P() << " += 1;\n"
400 out << " _out: {}\n";
406 void FTabCodeGen::writeOutEOF()
408 if ( anyEofActions() ) {
411 " switch ( " << EA() << "[" << CS() << "] ) {\n";