Edits to chapter four.
[ragel.git] / rlgen-ruby / ruby-ftabcodegen.cpp
blobf841e4952f656329b17b2e7e635e787dae18acc9
1 /*
2 * 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
4 */
6 /* This file is part of Ragel.
8 * Ragel is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Ragel is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Ragel; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <iomanip>
24 #include <sstream>
25 #include "redfsm.h"
26 #include "gendata.h"
27 #include "rlgen-ruby.h"
28 #include "ruby-ftabcodegen.h"
30 using std::ostream;
31 using std::ostringstream;
32 using std::string;
33 using std::cerr;
34 using std::endl;
36 void RubyFTabCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
38 out <<
39 " begin\n"
40 " " << CS() << " = " << gotoDest << "\n"
41 " _goto_level = _again\n"
42 " next\n"
43 " end\n";
46 void RubyFTabCodeGen::GOTO_EXPR( ostream &out, InlineItem *ilItem, bool inFinish )
48 out <<
49 " begin\n"
50 " " << CS() << " = (";
51 INLINE_LIST( out, ilItem->children, 0, inFinish );
52 out << ")\n";
53 out <<
54 " _goto_level = _again\n"
55 " next\n"
56 " end\n";
59 void RubyFTabCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
61 if ( prePushExpr != 0 ) {
62 out << "begin\n";
63 INLINE_LIST( out, prePushExpr, 0, false );
66 out <<
67 " begin\n"
68 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
69 " " << TOP() << "+= 1\n"
70 " " << CS() << " = " << callDest << "\n"
71 " _goto_level = _again\n"
72 " next\n"
73 " end\n";
75 if ( prePushExpr != 0 )
76 out << "end\n";
79 void RubyFTabCodeGen::CALL_EXPR(ostream &out, InlineItem *ilItem,
80 int targState, bool inFinish )
82 if ( prePushExpr != 0 ) {
83 out << "begin\n";
84 INLINE_LIST( out, prePushExpr, 0, false );
87 out <<
88 " begin\n"
89 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
90 " " << TOP() << " += 1\n"
91 " " << CS() << " = (";
92 INLINE_LIST( out, ilItem->children, targState, inFinish );
93 out << ")\n";
95 out <<
96 " _goto_level = _again\n"
97 " next\n"
98 " end\n";
100 if ( prePushExpr != 0 )
101 out << "end\n";
104 void RubyFTabCodeGen::RET( ostream &out, bool inFinish )
106 out <<
107 " begin\n"
108 " " << TOP() << " -= 1\n"
109 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
111 if ( postPopExpr != 0 ) {
112 out << "begin\n";
113 INLINE_LIST( out, postPopExpr, 0, false );
114 out << "end\n";
117 out <<
118 " _goto_level = _again\n"
119 " next\n"
120 " end\n";
123 void RubyFTabCodeGen::BREAK( ostream &out, int targState )
125 out <<
126 " begin\n"
127 " _goto_level = _out\n"
128 " next\n"
129 " end\n";
133 std::ostream &RubyFTabCodeGen::TO_STATE_ACTION_SWITCH()
135 /* Loop the actions. */
136 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
137 if ( redAct->numToStateRefs > 0 ) {
138 /* Write the entry label. */
139 out << "\twhen " << redAct->actListId+1 << " then\n";
141 /* Write each action in the list of action items. */
142 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
143 ACTION( out, item->value, 0, false );
148 genLineDirective( out );
149 return out;
152 /* Write out the function switch. This switch is keyed on the values
153 * of the func index. */
154 std::ostream &RubyFTabCodeGen::FROM_STATE_ACTION_SWITCH()
156 /* Loop the actions. */
157 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
158 if ( redAct->numFromStateRefs > 0 ) {
159 /* Write the entry label. */
160 out << "\twhen " << redAct->actListId+1 << " then\n";
162 /* Write each action in the list of action items. */
163 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
164 ACTION( out, item->value, 0, false );
169 genLineDirective( out );
170 return out;
173 std::ostream &RubyFTabCodeGen::EOF_ACTION_SWITCH()
175 /* Loop the actions. */
176 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
177 if ( redAct->numEofRefs > 0 ) {
178 /* Write the entry label. */
179 out << "\twhen " << redAct->actListId+1 << " then\n";
181 /* Write each action in the list of action items. */
182 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
183 ACTION( out, item->value, 0, true );
188 genLineDirective( out );
189 return out;
192 /* Write out the function switch. This switch is keyed on the values
193 * of the func index. */
194 std::ostream &RubyFTabCodeGen::ACTION_SWITCH()
196 /* Loop the actions. */
197 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
198 if ( redAct->numTransRefs > 0 ) {
199 /* Write the entry label. */
200 out << "\twhen " << redAct->actListId+1 << " then\n";
202 /* Write each action in the list of action items. */
203 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
204 ACTION( out, item->value, 0, false );
209 genLineDirective( out );
210 return out;
214 int RubyFTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
216 int act = 0;
217 if ( state->toStateAction != 0 )
218 act = state->toStateAction->actListId+1;
219 return act;
222 int RubyFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
224 int act = 0;
225 if ( state->fromStateAction != 0 )
226 act = state->fromStateAction->actListId+1;
227 return act;
230 int RubyFTabCodeGen::EOF_ACTION( RedStateAp *state )
232 int act = 0;
233 if ( state->eofAction != 0 )
234 act = state->eofAction->actListId+1;
235 return act;
239 /* Write out the function for a transition. */
240 int RubyFTabCodeGen::TRANS_ACTION( RedTransAp *trans )
242 int action = 0;
243 if ( trans->action != 0 )
244 action = trans->action->actListId+1;
245 return action;
248 void RubyFTabCodeGen::writeData()
251 if ( redFsm->anyConditions() ) {
252 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
253 COND_OFFSETS();
254 CLOSE_ARRAY() <<
255 "\n";
257 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
258 COND_LENS();
259 CLOSE_ARRAY() <<
260 "\n";
262 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
263 COND_KEYS();
264 CLOSE_ARRAY() <<
265 "\n";
267 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
268 COND_SPACES();
269 CLOSE_ARRAY() <<
270 "\n";
273 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
274 KEY_OFFSETS();
275 CLOSE_ARRAY() <<
276 "\n";
278 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
279 KEYS();
280 CLOSE_ARRAY() <<
281 "\n";
283 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
284 SINGLE_LENS();
285 CLOSE_ARRAY() <<
286 "\n";
288 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
289 RANGE_LENS();
290 CLOSE_ARRAY() <<
291 "\n";
293 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
294 INDEX_OFFSETS();
295 CLOSE_ARRAY() <<
296 "\n";
298 if ( useIndicies ) {
299 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
300 INDICIES();
301 CLOSE_ARRAY() <<
302 "\n";
304 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
305 TRANS_TARGS_WI();
306 CLOSE_ARRAY() <<
307 "\n";
309 if ( redFsm->anyActions() ) {
310 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
311 TRANS_ACTIONS_WI();
312 CLOSE_ARRAY() <<
313 "\n";
316 else {
317 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
318 TRANS_TARGS();
319 CLOSE_ARRAY() <<
320 "\n";
322 if ( redFsm->anyActions() ) {
323 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
324 TRANS_ACTIONS();
325 CLOSE_ARRAY() <<
326 "\n";
330 if ( redFsm->anyToStateActions() ) {
331 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
332 TO_STATE_ACTIONS();
333 CLOSE_ARRAY() <<
334 "\n";
337 if ( redFsm->anyFromStateActions() ) {
338 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
339 FROM_STATE_ACTIONS();
340 CLOSE_ARRAY() <<
341 "\n";
344 if ( redFsm->anyEofActions() ) {
345 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
346 EOF_ACTIONS();
347 CLOSE_ARRAY() <<
348 "\n";
351 if ( redFsm->anyEofTrans() ) {
352 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex+1), ET() );
353 EOF_TRANS();
354 CLOSE_ARRAY() <<
355 "\n";
358 STATE_IDS();
361 void RubyFTabCodeGen::writeExec()
363 out <<
364 "begin\n"
365 " testEof = false\n"
366 " _klen, _trans, _keys";
368 if ( redFsm->anyRegCurStateRef() )
369 out << ", _ps";
371 if ( redFsm->anyConditions() )
372 out << ", _widec";
374 out << " = nil\n";
376 out <<
377 " _goto_level = 0\n"
378 " _resume = 10\n"
379 " _eof_trans = 15\n"
380 " _again = 20\n"
381 " _test_eof = 30\n"
382 " _out = 40\n";
384 out <<
385 " while true\n"
386 " if _goto_level <= 0\n";
388 if ( hasEnd ) {
389 out <<
390 " if " << P() << " == " << PE() << "\n"
391 " _goto_level = _test_eof\n"
392 " next\n"
393 " end\n";
396 if ( redFsm->errState != 0 ) {
397 out <<
398 " if " << CS() << " == " << redFsm->errState->id << "\n"
399 " _goto_level = _out\n"
400 " next\n"
401 " end\n";
404 /* The resume label. */
405 out <<
406 " end\n"
407 " if _goto_level <= _resume\n";
409 if ( redFsm->anyFromStateActions() ) {
410 out <<
411 " case " << FSA() << "[" << CS() << "] \n";
412 FROM_STATE_ACTION_SWITCH() <<
413 " end # from state action switch \n"
414 "\n";
417 if ( redFsm->anyConditions() )
418 COND_TRANSLATE();
420 LOCATE_TRANS();
422 if ( useIndicies )
423 out << " _trans = " << I() << "[_trans];\n";
425 if ( redFsm->anyEofTrans() ) {
426 out <<
427 " end\n"
428 " if _goto_level <= _eof_trans\n";
431 if ( redFsm->anyRegCurStateRef() )
432 out << " _ps = " << CS() << ";\n";
434 out <<
435 " " << CS() << " = " << TT() << "[_trans];\n"
436 "\n";
438 if ( redFsm->anyRegActions() ) {
439 out <<
440 " if " << TA() << "[_trans] != 0\n"
441 "\n"
442 " case " << TA() << "[_trans] \n";
443 ACTION_SWITCH() <<
444 " end # action switch \n"
445 " end\n"
446 "\n";
449 /* The again label. */
450 out <<
451 " end\n"
452 " if _goto_level <= _again\n";
454 if ( redFsm->anyToStateActions() ) {
455 out <<
456 " case " << TSA() << "[" << CS() << "] \n";
457 TO_STATE_ACTION_SWITCH() <<
458 " end\n"
459 "\n";
462 if ( redFsm->errState != 0 ) {
463 out <<
464 " if " << CS() << " == " << redFsm->errState->id << "\n"
465 " _goto_level = _out\n"
466 " next\n"
467 " end\n";
470 out << " " << P() << " += 1\n";
472 if ( hasEnd ) {
473 out <<
474 " if " << P() << " != " << PE() << "\n"
475 " _goto_level = _resume\n"
476 " next\n"
477 " end\n";
479 else {
480 out <<
481 " _goto_level = _resume\n"
482 " next\n";
485 /* The test eof label. */
486 out <<
487 " end\n"
488 " if _goto_level <= _test_eof\n";
490 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
491 out <<
492 " if " << P() << " == " << EOFV() << "\n";
494 if ( redFsm->anyEofTrans() ) {
495 out <<
496 " if " << ET() << "[" << CS() << "] > 0\n"
497 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
498 " _goto_level = _eof_trans\n"
499 " next;\n"
500 " end\n";
503 if ( redFsm->anyEofActions() ) {
504 out <<
505 " begin\n"
506 " case ( " << EA() << "[" << CS() << "] )\n";
507 EOF_ACTION_SWITCH() <<
508 " end\n"
509 " end\n";
512 out <<
513 " end\n"
514 "\n";
517 out <<
518 " end\n"
519 " if _goto_level <= _out\n"
520 " break\n"
521 " end\n"
522 "end\n";
524 /* Wrapping the execute block. */
525 out << " end\n";
529 void RubyFTabCodeGen::calcIndexSize()
531 int sizeWithInds = 0, sizeWithoutInds = 0;
533 /* Calculate cost of using with indicies. */
534 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
535 int totalIndex = st->outSingle.length() + st->outRange.length() +
536 (st->defTrans == 0 ? 0 : 1);
537 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
539 sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
540 if ( redFsm->anyActions() )
541 sizeWithInds += arrayTypeSize(redFsm->maxActListId) * redFsm->transSet.length();
543 /* Calculate the cost of not using indicies. */
544 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
545 int totalIndex = st->outSingle.length() + st->outRange.length() +
546 (st->defTrans == 0 ? 0 : 1);
547 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
548 if ( redFsm->anyActions() )
549 sizeWithoutInds += arrayTypeSize(redFsm->maxActListId) * totalIndex;
552 /* If using indicies reduces the size, use them. */
553 useIndicies = sizeWithInds < sizeWithoutInds;
557 * Local Variables:
558 * mode: c++
559 * indent-tabs-mode: 1
560 * c-file-style: "bsd"
561 * End: