Use portable types in the C/C++ code generator
[ragel-jkt.git] / ragel / gofflat.cpp
blob59e729c76ba5894c905e7ca9fe2408516e57e10c
1 /*
2 * Copyright 2004-2006 Adrian Thurston <thurston@complang.org>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
5 */
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 "ragel.h"
25 #include "gofflat.h"
26 #include "redfsm.h"
27 #include "gendata.h"
29 using std::endl;
31 std::ostream &GoFFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
33 int act = 0;
34 if ( state->toStateAction != 0 )
35 act = state->toStateAction->actListId+1;
36 out << act;
37 return out;
40 std::ostream &GoFFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
42 int act = 0;
43 if ( state->fromStateAction != 0 )
44 act = state->fromStateAction->actListId+1;
45 out << act;
46 return out;
49 std::ostream &GoFFlatCodeGen::EOF_ACTION( RedStateAp *state )
51 int act = 0;
52 if ( state->eofAction != 0 )
53 act = state->eofAction->actListId+1;
54 out << act;
55 return out;
58 /* Write out the function for a transition. */
59 std::ostream &GoFFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
61 int action = 0;
62 if ( trans->action != 0 )
63 action = trans->action->actListId+1;
64 out << action;
65 return out;
68 /* Write out the function switch. This switch is keyed on the values
69 * of the func index. */
70 std::ostream &GoFFlatCodeGen::TO_STATE_ACTION_SWITCH( int level )
72 /* Loop the actions. */
73 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
74 if ( redAct->numToStateRefs > 0 ) {
75 /* Write the entry label. */
76 out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
78 /* Write each action in the list of action items. */
79 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
80 ACTION( out, item->value, 0, false, false );
84 genLineDirective( out );
85 return out;
88 /* Write out the function switch. This switch is keyed on the values
89 * of the func index. */
90 std::ostream &GoFFlatCodeGen::FROM_STATE_ACTION_SWITCH( int level )
92 /* Loop the actions. */
93 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
94 if ( redAct->numFromStateRefs > 0 ) {
95 /* Write the entry label. */
96 out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
98 /* Write each action in the list of action items. */
99 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
100 ACTION( out, item->value, 0, false, false );
104 genLineDirective( out );
105 return out;
108 std::ostream &GoFFlatCodeGen::EOF_ACTION_SWITCH( int level )
110 /* Loop the actions. */
111 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
112 if ( redAct->numEofRefs > 0 ) {
113 /* Write the entry label. */
114 out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
116 /* Write each action in the list of action items. */
117 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
118 ACTION( out, item->value, 0, true, false );
122 genLineDirective( out );
123 return out;
126 /* Write out the function switch. This switch is keyed on the values
127 * of the func index. */
128 std::ostream &GoFFlatCodeGen::ACTION_SWITCH( int level )
130 /* Loop the actions. */
131 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
132 if ( redAct->numTransRefs > 0 ) {
133 /* Write the entry label. */
134 out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
136 /* Write each action in the list of action items. */
137 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
138 ACTION( out, item->value, 0, false, false );
142 genLineDirective( out );
143 return out;
146 void GoFFlatCodeGen::writeData()
148 if ( redFsm->anyConditions() ) {
149 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
150 COND_KEYS();
151 CLOSE_ARRAY() <<
152 endl;
154 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
155 COND_KEY_SPANS();
156 CLOSE_ARRAY() <<
157 endl;
159 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
160 CONDS();
161 CLOSE_ARRAY() <<
162 endl;
164 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
165 COND_INDEX_OFFSET();
166 CLOSE_ARRAY() <<
167 endl;
170 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
171 KEYS();
172 CLOSE_ARRAY() <<
173 endl;
175 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
176 KEY_SPANS();
177 CLOSE_ARRAY() <<
178 endl;
180 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
181 FLAT_INDEX_OFFSET();
182 CLOSE_ARRAY() <<
183 endl;
185 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
186 INDICIES();
187 CLOSE_ARRAY() <<
188 endl;
190 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
191 TRANS_TARGS();
192 CLOSE_ARRAY() <<
193 endl;
195 if ( redFsm->anyActions() ) {
196 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
197 TRANS_ACTIONS();
198 CLOSE_ARRAY() <<
199 endl;
202 if ( redFsm->anyToStateActions() ) {
203 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
204 TO_STATE_ACTIONS();
205 CLOSE_ARRAY() <<
206 endl;
209 if ( redFsm->anyFromStateActions() ) {
210 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
211 FROM_STATE_ACTIONS();
212 CLOSE_ARRAY() <<
213 endl;
216 if ( redFsm->anyEofActions() ) {
217 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
218 EOF_ACTIONS();
219 CLOSE_ARRAY() <<
220 endl;
223 if ( redFsm->anyEofTrans() ) {
224 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
225 EOF_TRANS();
226 CLOSE_ARRAY() <<
227 endl;
230 STATE_IDS();
233 void GoFFlatCodeGen::writeExec()
235 testEofUsed = false;
236 outLabelUsed = false;
238 out <<
239 " {" << endl <<
240 " var _slen " << INT() << endl;
242 if ( redFsm->anyRegCurStateRef() )
243 out << " var _ps " << INT() << endl;
245 out << " var _trans " << INT() << endl;
247 if ( redFsm->anyConditions() )
248 out << " var _cond " << INT() << endl;
250 out <<
251 " var _keys " << INT() << endl <<
252 " var _inds " << INT() << endl;
254 if ( redFsm->anyConditions() ) {
255 out <<
256 " var _conds " << INT() << endl <<
257 " var _widec " << WIDE_ALPH_TYPE() << endl;
260 if ( !noEnd ) {
261 testEofUsed = true;
262 out <<
263 " if " << P() << " == " << PE() << " {" << endl <<
264 " goto _test_eof" << endl <<
265 " }" << endl;
268 if ( redFsm->errState != 0 ) {
269 outLabelUsed = true;
270 out <<
271 " if " << vCS() << " == " << redFsm->errState->id << " {" << endl <<
272 " goto _out" << endl <<
273 " }" << endl;
276 out << "_resume:" << endl;
278 if ( redFsm->anyFromStateActions() ) {
279 out <<
280 " switch " << FSA() << "[" << vCS() << "] {" << endl;
281 FROM_STATE_ACTION_SWITCH(1);
282 out <<
283 " }" << endl <<
284 endl;
287 if ( redFsm->anyConditions() )
288 COND_TRANSLATE();
290 LOCATE_TRANS();
292 if ( redFsm->anyEofTrans() )
293 out << "_eof_trans:" << endl;
295 if ( redFsm->anyRegCurStateRef() )
296 out << " _ps = " << vCS() << endl;
298 out <<
299 " " << vCS() << " = " << CAST(INT(), TT() + "[_trans]") << endl <<
300 endl;
302 if ( redFsm->anyRegActions() ) {
303 out <<
304 " if " << TA() << "[_trans] == 0 {" << endl <<
305 " goto _again" << endl <<
306 " }" << endl <<
307 endl <<
308 " switch " << TA() << "[_trans] {" << endl;
309 ACTION_SWITCH(1);
310 out <<
311 " }" << endl <<
312 endl;
315 if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
316 redFsm->anyActionCalls() || redFsm->anyActionRets() )
317 out << "_again:" << endl;
319 if ( redFsm->anyToStateActions() ) {
320 out <<
321 " switch " << TSA() << "[" << vCS() << "] {" << endl;
322 TO_STATE_ACTION_SWITCH(1);
323 out <<
324 " }" << endl <<
325 endl;
328 if ( redFsm->errState != 0 ) {
329 outLabelUsed = true;
330 out <<
331 " if " << vCS() << " == " << redFsm->errState->id << " {" << endl <<
332 " goto _out" << endl <<
333 " }" << endl;
336 if ( !noEnd ) {
337 out <<
338 " if " << P() << "++; " << P() << " != " << PE() << " {"
339 " goto _resume" << endl <<
340 " }" << endl;
342 else {
343 out <<
344 " " << P() << "++" << endl <<
345 " goto _resume" << endl;
348 if ( testEofUsed )
349 out << " _test_eof: {}" << endl;
351 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
352 out <<
353 " if " << P() << " == " << vEOF() << " {" << endl;
355 if ( redFsm->anyEofTrans() ) {
356 out <<
357 " if " << ET() << "[" << vCS() << "] > 0 {" << endl <<
358 " _trans = " << CAST(INT(), ET() + "[" + vCS() + "] - 1") << endl <<
359 " goto _eof_trans" << endl <<
360 " }" << endl;
363 if ( redFsm->anyEofActions() ) {
364 out <<
365 " switch " << EA() << "[" << vCS() << "] {" << endl;
366 EOF_ACTION_SWITCH(2);
367 out <<
368 " }" << endl;
371 out <<
372 " }" << endl <<
373 endl;
376 if ( outLabelUsed )
377 out << " _out: {}" << endl;
379 out << " }" << endl;