123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831 |
- /*
- * Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
- * 2006-2007 Adrian Thurston <thurston@complang.org>
- */
- /* This file is part of Ragel.
- *
- * Ragel is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Ragel is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ragel; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <stdio.h>
- #include <string>
- #include "rbxgoto.h"
- #include "ragel.h"
- #include "redfsm.h"
- #include "bstmap.h"
- #include "gendata.h"
- using std::ostream;
- using std::string;
- inline string label(string a, int i)
- {
- return a + itoa(i);
- }
- ostream &RbxGotoCodeGen::rbxLabel(ostream &out, string label)
- {
- out << "Rubinius.asm { @labels[:_" << FSM_NAME() << "_" << label << "].set! }\n";
- return out;
- }
- ostream &RbxGotoCodeGen::rbxGoto(ostream &out, string label)
- {
- out << "Rubinius.asm { goto @labels[:_" << FSM_NAME() << "_" << label << "] }\n";
- return out;
- }
- /* Emit the goto to take for a given transition. */
- std::ostream &RbxGotoCodeGen::TRANS_GOTO( RedTransAp *trans, int level )
- {
- out << TABS(level);
- return rbxGoto(out, label("tr",trans->id));
- }
- std::ostream &RbxGotoCodeGen::TO_STATE_ACTION_SWITCH()
- {
- /* Walk the list of functions, printing the cases. */
- for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
- /* Write out referenced actions. */
- if ( act->numToStateRefs > 0 ) {
- /* Write the case label, the action and the case break. */
- out << "\twhen " << act->actionId << " then\n";
- ACTION( out, act, 0, false );
- }
- }
- genLineDirective( out );
- return out;
- }
- std::ostream &RbxGotoCodeGen::FROM_STATE_ACTION_SWITCH()
- {
- /* Walk the list of functions, printing the cases. */
- for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
- /* Write out referenced actions. */
- if ( act->numFromStateRefs > 0 ) {
- /* Write the case label, the action and the case break. */
- out << "\twhen " << act->actionId << " then\n";
- ACTION( out, act, 0, false );
- }
- }
- genLineDirective( out );
- return out;
- }
- std::ostream &RbxGotoCodeGen::EOF_ACTION_SWITCH()
- {
- /* Walk the list of functions, printing the cases. */
- for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
- /* Write out referenced actions. */
- if ( act->numEofRefs > 0 ) {
- /* Write the case label, the action and the case break. */
- out << "\twhen " << act->actionId << " then\n";
- ACTION( out, act, 0, true );
- }
- }
- genLineDirective( out );
- return out;
- }
- std::ostream &RbxGotoCodeGen::ACTION_SWITCH()
- {
- /* Walk the list of functions, printing the cases. */
- for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
- /* Write out referenced actions. */
- if ( act->numTransRefs > 0 ) {
- /* Write the case label, the action and the case break. */
- out << "\twhen " << act->actionId << " then\n";
- ACTION( out, act, 0, false );
- }
- }
- genLineDirective( out );
- return out;
- }
- void RbxGotoCodeGen::GOTO_HEADER( RedStateAp *state )
- {
- /* Label the state. */
- out << "when " << state->id << " then\n";
- }
- void RbxGotoCodeGen::emitSingleSwitch( RedStateAp *state )
- {
- /* Load up the singles. */
- int numSingles = state->outSingle.length();
- RedTransEl *data = state->outSingle.data;
- if ( numSingles == 1 ) {
- /* If there is a single single key then write it out as an if. */
- out << "\tif " << GET_WIDE_KEY(state) << " == " <<
- KEY(data[0].lowKey) << " \n\t\t";
- /* Virtual function for writing the target of the transition. */
- TRANS_GOTO(data[0].value, 0) << "\n";
- out << "end\n";
- }
- else if ( numSingles > 1 ) {
- /* Write out single keys in a switch if there is more than one. */
- out << "\tcase " << GET_WIDE_KEY(state) << "\n";
- /* Write out the single indicies. */
- for ( int j = 0; j < numSingles; j++ ) {
- out << "\t\twhen " << KEY(data[j].lowKey) << " then\n";
- TRANS_GOTO(data[j].value, 0) << "\n";
- }
-
- /* Close off the transition switch. */
- out << "\tend\n";
- }
- }
- void RbxGotoCodeGen::emitRangeBSearch( RedStateAp *state, int level, int low, int high )
- {
- /* Get the mid position, staying on the lower end of the range. */
- int mid = (low + high) >> 1;
- RedTransEl *data = state->outRange.data;
- /* Determine if we need to look higher or lower. */
- bool anyLower = mid > low;
- bool anyHigher = mid < high;
- /* Determine if the keys at mid are the limits of the alphabet. */
- bool limitLow = data[mid].lowKey == keyOps->minKey;
- bool limitHigh = data[mid].highKey == keyOps->maxKey;
- if ( anyLower && anyHigher ) {
- /* Can go lower and higher than mid. */
- out << TABS(level) << "if " << GET_WIDE_KEY(state) << " < " <<
- KEY(data[mid].lowKey) << " \n";
- emitRangeBSearch( state, level+1, low, mid-1 );
- out << TABS(level) << "elsif " << GET_WIDE_KEY(state) << " > " <<
- KEY(data[mid].highKey) << " \n";
- emitRangeBSearch( state, level+1, mid+1, high );
- out << TABS(level) << "else\n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- out << TABS(level) << "end\n";
- }
- else if ( anyLower && !anyHigher ) {
- /* Can go lower than mid but not higher. */
- out << TABS(level) << "if " << GET_WIDE_KEY(state) << " < " <<
- KEY(data[mid].lowKey) << " then\n";
- emitRangeBSearch( state, level+1, low, mid-1 );
- /* if the higher is the highest in the alphabet then there is no
- * sense testing it. */
- if ( limitHigh ) {
- out << TABS(level) << "else\n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- }
- else {
- out << TABS(level) << "elsif" << GET_WIDE_KEY(state) << " <= " <<
- KEY(data[mid].highKey) << " )\n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- }
- out << TABS(level) << "end\n";
- }
- else if ( !anyLower && anyHigher ) {
- /* Can go higher than mid but not lower. */
- out << TABS(level) << "if " << GET_WIDE_KEY(state) << " > " <<
- KEY(data[mid].highKey) << " \n";
- emitRangeBSearch( state, level+1, mid+1, high );
- /* If the lower end is the lowest in the alphabet then there is no
- * sense testing it. */
- if ( limitLow ) {
- out << TABS(level) << "else\n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- }
- else {
- out << TABS(level) << "elsif " << GET_WIDE_KEY(state) << " >= " <<
- KEY(data[mid].lowKey) << " then\n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- }
- out << TABS(level) << "end\n";
- }
- else {
- /* Cannot go higher or lower than mid. It's mid or bust. What
- * tests to do depends on limits of alphabet. */
- if ( !limitLow && !limitHigh ) {
- out << TABS(level) << "if " << KEY(data[mid].lowKey) << " <= " <<
- GET_WIDE_KEY(state) << " && " << GET_WIDE_KEY(state) << " <= " <<
- KEY(data[mid].highKey) << " \n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- out << TABS(level) << "end\n";
- }
- else if ( limitLow && !limitHigh ) {
- out << TABS(level) << "if " << GET_WIDE_KEY(state) << " <= " <<
- KEY(data[mid].highKey) << " \n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- out << TABS(level) << "end\n";
- }
- else if ( !limitLow && limitHigh ) {
- out << TABS(level) << "if " << KEY(data[mid].lowKey) << " <= " <<
- GET_WIDE_KEY(state) << " \n";
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- out << TABS(level) << "end\n";
- }
- else {
- /* Both high and low are at the limit. No tests to do. */
- TRANS_GOTO(data[mid].value, level+1) << "\n";
- }
- }
- }
- void RbxGotoCodeGen::STATE_GOTO_ERROR()
- {
- /* Label the state and bail immediately. */
- outLabelUsed = true;
- RedStateAp *state = redFsm->errState;
- out << "when " << state->id << " then\n";
- rbxGoto(out << " ", "_out") << "\n";
- }
- void RbxGotoCodeGen::COND_TRANSLATE( GenStateCond *stateCond, int level )
- {
- GenCondSpace *condSpace = stateCond->condSpace;
- out << TABS(level) << "_widec = " <<
- KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
- " - " << KEY(keyOps->minKey) << ");\n";
- for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
- out << TABS(level) << "if ";
- CONDITION( out, *csi );
- Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
- out << "\n _widec += " << condValOffset << ";\n end";
- }
- }
- void RbxGotoCodeGen::emitCondBSearch( RedStateAp *state, int level, int low, int high )
- {
- /* Get the mid position, staying on the lower end of the range. */
- int mid = (low + high) >> 1;
- GenStateCond **data = state->stateCondVect.data;
- /* Determine if we need to look higher or lower. */
- bool anyLower = mid > low;
- bool anyHigher = mid < high;
- /* Determine if the keys at mid are the limits of the alphabet. */
- bool limitLow = data[mid]->lowKey == keyOps->minKey;
- bool limitHigh = data[mid]->highKey == keyOps->maxKey;
- if ( anyLower && anyHigher ) {
- /* Can go lower and higher than mid. */
- out << TABS(level) << "if " << GET_KEY() << " < " <<
- KEY(data[mid]->lowKey) << " \n";
- emitCondBSearch( state, level+1, low, mid-1 );
- out << TABS(level) << "elsif " << GET_KEY() << " > " <<
- KEY(data[mid]->highKey) << " \n";
- emitCondBSearch( state, level+1, mid+1, high );
- out << TABS(level) << "else\n";
- COND_TRANSLATE(data[mid], level+1);
- out << TABS(level) << "end\n";
- }
- else if ( anyLower && !anyHigher ) {
- /* Can go lower than mid but not higher. */
- out << TABS(level) << "if " << GET_KEY() << " < " <<
- KEY(data[mid]->lowKey) << " \n";
- emitCondBSearch( state, level+1, low, mid-1 );
- /* if the higher is the highest in the alphabet then there is no
- * sense testing it. */
- if ( limitHigh ) {
- out << TABS(level) << "else\n";
- COND_TRANSLATE(data[mid], level+1);
- }
- else {
- out << TABS(level) << "elsif " << GET_KEY() << " <= " <<
- KEY(data[mid]->highKey) << " then\n";
- COND_TRANSLATE(data[mid], level+1);
- }
- out << TABS(level) << "end\n";
- }
- else if ( !anyLower && anyHigher ) {
- /* Can go higher than mid but not lower. */
- out << TABS(level) << "if " << GET_KEY() << " > " <<
- KEY(data[mid]->highKey) << " \n";
- emitCondBSearch( state, level+1, mid+1, high );
- /* If the lower end is the lowest in the alphabet then there is no
- * sense testing it. */
- if ( limitLow ) {
- out << TABS(level) << "else\n";
- COND_TRANSLATE(data[mid], level+1);
- }
- else {
- out << TABS(level) << "elsif " << GET_KEY() << " >= " <<
- KEY(data[mid]->lowKey) << " then\n";
- COND_TRANSLATE(data[mid], level+1);
- }
- out << TABS(level) << "end\n";
- }
- else {
- /* Cannot go higher or lower than mid. It's mid or bust. What
- * tests to do depends on limits of alphabet. */
- if ( !limitLow && !limitHigh ) {
- out << TABS(level) << "if " << KEY(data[mid]->lowKey) << " <= " <<
- GET_KEY() << " && " << GET_KEY() << " <= " <<
- KEY(data[mid]->highKey) << " then\n";
- COND_TRANSLATE(data[mid], level+1);
- out << TABS(level) << "end\n";
- }
- else if ( limitLow && !limitHigh ) {
- out << TABS(level) << "if " << GET_KEY() << " <= " <<
- KEY(data[mid]->highKey) << " then\n";
- COND_TRANSLATE(data[mid], level+1);
- out << TABS(level) << "end\n";
- }
- else if ( !limitLow && limitHigh ) {
- out << TABS(level) << "if " << KEY(data[mid]->lowKey) << " <= " <<
- GET_KEY() << " then\n";
- COND_TRANSLATE(data[mid], level+1);
- out << TABS(level) << "end\n";
- }
- else {
- /* Both high and low are at the limit. No tests to do. */
- COND_TRANSLATE(data[mid], level);
- }
- }
- }
- std::ostream &RbxGotoCodeGen::STATE_GOTOS()
- {
- for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
- if ( st == redFsm->errState )
- STATE_GOTO_ERROR();
- else {
- /* Writing code above state gotos. */
- GOTO_HEADER( st );
- if ( st->stateCondVect.length() > 0 ) {
- out << " _widec = " << GET_KEY() << ";\n";
- emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 );
- }
- /* Try singles. */
- if ( st->outSingle.length() > 0 )
- emitSingleSwitch( st );
- /* Default case is to binary search for the ranges, if that fails then */
- if ( st->outRange.length() > 0 )
- emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 );
- /* Write the default transition. */
- TRANS_GOTO( st->defTrans, 1 ) << "\n";
- }
- }
- return out;
- }
- std::ostream &RbxGotoCodeGen::TRANSITIONS()
- {
- /* Emit any transitions that have functions and that go to
- * this state. */
- for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
- /* Write the label for the transition so it can be jumped to. */
- rbxLabel(out << " ", label("tr", trans->id)) << "\n";
- /* Destination state. */
- if ( trans->action != 0 && trans->action->anyCurStateRef() )
- out << "_ps = " << vCS() << "'n";
- out << vCS() << " = " << trans->targ->id << "\n";
- if ( trans->action != 0 ) {
- /* Write out the transition func. */
- rbxGoto(out, label("f", trans->action->actListId)) << "\n";
- }
- else {
- /* No code to execute, just loop around. */
- rbxGoto(out, "_again") << "\n";
- }
- }
- return out;
- }
- std::ostream &RbxGotoCodeGen::EXEC_FUNCS()
- {
- /* Make labels that set acts and jump to execFuncs. Loop func indicies. */
- for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
- if ( redAct->numTransRefs > 0 ) {
- rbxLabel(out, label("f", redAct->actListId)) << "\n" <<
- "_acts = " << itoa( redAct->location+1 ) << "\n";
- rbxGoto(out, "execFuncs") << "\n";
- }
- }
- rbxLabel(out, "execFuncs") <<
- "\n"
- " _nacts = " << A() << "[_acts]\n"
- " _acts += 1\n"
- " while ( _nacts > 0 ) \n"
- " _nacts -= 1\n"
- " _acts += 1\n"
- " case ( "<< A() << "[_acts-1] ) \n";
- ACTION_SWITCH();
- out <<
- " end\n"
- " end \n";
- rbxGoto(out, "_again");
- return out;
- }
- int RbxGotoCodeGen::TO_STATE_ACTION( RedStateAp *state )
- {
- int act = 0;
- if ( state->toStateAction != 0 )
- act = state->toStateAction->location+1;
- return act;
- }
- int RbxGotoCodeGen::FROM_STATE_ACTION( RedStateAp *state )
- {
- int act = 0;
- if ( state->fromStateAction != 0 )
- act = state->fromStateAction->location+1;
- return act;
- }
- int RbxGotoCodeGen::EOF_ACTION( RedStateAp *state )
- {
- int act = 0;
- if ( state->eofAction != 0 )
- act = state->eofAction->location+1;
- return act;
- }
- std::ostream &RbxGotoCodeGen::TO_STATE_ACTIONS()
- {
- /* Take one off for the psuedo start state. */
- int numStates = redFsm->stateList.length();
- unsigned int *vals = new unsigned int[numStates];
- memset( vals, 0, sizeof(unsigned int)*numStates );
- for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
- vals[st->id] = TO_STATE_ACTION(st);
- out << "\t";
- for ( int st = 0; st < redFsm->nextStateId; st++ ) {
- /* Write any eof action. */
- out << vals[st];
- if ( st < numStates-1 ) {
- out << ", ";
- if ( (st+1) % IALL == 0 )
- out << "\n\t";
- }
- }
- out << "\n";
- delete[] vals;
- return out;
- }
- std::ostream &RbxGotoCodeGen::FROM_STATE_ACTIONS()
- {
- /* Take one off for the psuedo start state. */
- int numStates = redFsm->stateList.length();
- unsigned int *vals = new unsigned int[numStates];
- memset( vals, 0, sizeof(unsigned int)*numStates );
- for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
- vals[st->id] = FROM_STATE_ACTION(st);
- out << "\t";
- for ( int st = 0; st < redFsm->nextStateId; st++ ) {
- /* Write any eof action. */
- out << vals[st];
- if ( st < numStates-1 ) {
- out << ", ";
- if ( (st+1) % IALL == 0 )
- out << "\n\t";
- }
- }
- out << "\n";
- delete[] vals;
- return out;
- }
- std::ostream &RbxGotoCodeGen::EOF_ACTIONS()
- {
- /* Take one off for the psuedo start state. */
- int numStates = redFsm->stateList.length();
- unsigned int *vals = new unsigned int[numStates];
- memset( vals, 0, sizeof(unsigned int)*numStates );
- for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
- vals[st->id] = EOF_ACTION(st);
- out << "\t";
- for ( int st = 0; st < redFsm->nextStateId; st++ ) {
- /* Write any eof action. */
- out << vals[st];
- if ( st < numStates-1 ) {
- out << ", ";
- if ( (st+1) % IALL == 0 )
- out << "\n\t";
- }
- }
- out << "\n";
- delete[] vals;
- return out;
- }
- std::ostream &RbxGotoCodeGen::FINISH_CASES()
- {
- for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
- /* States that are final and have an out action need a case. */
- if ( st->eofAction != 0 ) {
- /* Write the case label. */
- out << "\t\twhen " << st->id << " then\n";
- /* Write the goto func. */
- rbxGoto(out, label("f", st->eofAction->actListId)) << "\n";
- }
- }
-
- return out;
- }
- void RbxGotoCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
- {
- ret << "begin\n" << vCS() << " = " << gotoDest << " ";
- rbxGoto(ret, "_again") <<
- "\nend\n";
- }
- void RbxGotoCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
- {
- ret << "begin\n" << vCS() << " = (";
- INLINE_LIST( ret, ilItem->children, 0, inFinish );
- ret << ")";
- rbxGoto(ret, "_again") <<
- "\nend\n";
- }
- void RbxGotoCodeGen::CURS( ostream &ret, bool inFinish )
- {
- ret << "(_ps)";
- }
- void RbxGotoCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
- {
- ret << "(" << vCS() << ")";
- }
- void RbxGotoCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
- {
- ret << vCS() << " = " << nextDest << ";";
- }
- void RbxGotoCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
- {
- ret << vCS() << " = (";
- INLINE_LIST( ret, ilItem->children, 0, inFinish );
- ret << ");";
- }
- void RbxGotoCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
- {
- if ( prePushExpr != 0 ) {
- ret << "{";
- INLINE_LIST( ret, prePushExpr, 0, false );
- }
- ret << "begin\n"
- << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = " <<
- callDest << "; ";
- rbxGoto(ret, "_again") <<
- "\nend\n";
- if ( prePushExpr != 0 )
- ret << "}";
- }
- void RbxGotoCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
- {
- if ( prePushExpr != 0 ) {
- ret << "{";
- INLINE_LIST( ret, prePushExpr, 0, false );
- }
- ret << "begin\n" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = (";
- INLINE_LIST( ret, ilItem->children, targState, inFinish );
- ret << "); ";
- rbxGoto(ret, "_again") <<
- "\nend\n";
- if ( prePushExpr != 0 )
- ret << "}";
- }
- void RbxGotoCodeGen::RET( ostream &ret, bool inFinish )
- {
- ret << "begin\n" << vCS() << " = " << STACK() << "[--" << TOP() << "]; " ;
- if ( postPopExpr != 0 ) {
- ret << "{";
- INLINE_LIST( ret, postPopExpr, 0, false );
- ret << "}";
- }
- rbxGoto(ret, "_again") <<
- "\nend\n";
- }
- void RbxGotoCodeGen::BREAK( ostream &ret, int targState )
- {
- outLabelUsed = true;
- out <<
- " begin\n"
- " " << P() << " += 1\n"
- " "; rbxGoto(ret, "_out") << "\n"
- " end\n";
- }
- void RbxGotoCodeGen::writeData()
- {
- if ( redFsm->anyActions() ) {
- OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
- ACTIONS_ARRAY();
- CLOSE_ARRAY() <<
- "\n";
- }
- if ( redFsm->anyToStateActions() ) {
- OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
- TO_STATE_ACTIONS();
- CLOSE_ARRAY() <<
- "\n";
- }
- if ( redFsm->anyFromStateActions() ) {
- OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
- FROM_STATE_ACTIONS();
- CLOSE_ARRAY() <<
- "\n";
- }
- if ( redFsm->anyEofActions() ) {
- OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
- EOF_ACTIONS();
- CLOSE_ARRAY() <<
- "\n";
- }
- STATE_IDS();
- }
- void RbxGotoCodeGen::writeExec()
- {
- outLabelUsed = false;
- out << " begin\n";
- out << " Rubinius.asm { @labels = Hash.new { |h,k| h[k] = new_label } }\n";
- if ( redFsm->anyRegCurStateRef() )
- out << " _ps = 0;\n";
- if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
- || redFsm->anyFromStateActions() )
- {
- out << " _acts, _nacts = nil\n";
- }
- if ( redFsm->anyConditions() )
- out << " _widec = nil\n";
- out << "\n";
- if ( !noEnd ) {
- outLabelUsed = true;
- out <<
- " if ( " << P() << " == " << PE() << " )\n";
- rbxGoto(out << " ", "_out") << "\n" <<
- " end\n";
- }
- if ( redFsm->errState != 0 ) {
- outLabelUsed = true;
- out <<
- " if ( " << vCS() << " == " << redFsm->errState->id << " )\n";
- rbxGoto(out << " ", "_out") << "\n" <<
- " end\n";
- }
- rbxLabel(out, "_resume") << "\n";
- if ( redFsm->anyFromStateActions() ) {
- out <<
- " _acts = " << ARR_OFF( A(), FSA() + "[" + vCS() + "]" ) << ";\n"
- " _nacts = " << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- FROM_STATE_ACTION_SWITCH();
- out <<
- " }\n"
- " }\n"
- "\n";
- }
- out <<
- " case ( " << vCS() << " )\n";
- STATE_GOTOS();
- out <<
- " end # case\n"
- "\n";
- TRANSITIONS() <<
- "\n";
- if ( redFsm->anyRegActions() )
- EXEC_FUNCS() << "\n";
- rbxLabel(out, "_again") << "\n";
- if ( redFsm->anyToStateActions() ) {
- out <<
- " _acts = " << ARR_OFF( A(), TSA() + "[" + vCS() + "]" ) << ";\n"
- " _nacts = " << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- TO_STATE_ACTION_SWITCH();
- out <<
- " }\n"
- " }\n"
- "\n";
- }
- if ( redFsm->errState != 0 ) {
- outLabelUsed = true;
- out <<
- " if ( " << vCS() << " == " << redFsm->errState->id << " )\n";
- rbxGoto(out << " ", "_out") << "\n" <<
- " end" << "\n";
- }
- if ( !noEnd ) {
- out << " " << P() << " += 1\n"
- " if ( " << P() << " != " << PE() << " )\n";
- rbxGoto(out << " ", "_resume") << "\n" <<
- " end" << "\n";
- }
- else {
- out <<
- " " << P() << " += 1;\n";
- rbxGoto(out << " ", "_resume") << "\n";
- }
- if ( outLabelUsed )
- rbxLabel(out, "_out") << "\n";
- out << " end\n";
- }
- void RbxGotoCodeGen::writeEOF()
- {
- if ( redFsm->anyEofActions() ) {
- out <<
- " {\n"
- " _acts = " <<
- ARR_OFF( A(), EA() + "[" + vCS() + "]" ) << ";\n"
- " " << " _nacts = " << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- EOF_ACTION_SWITCH();
- out <<
- " }\n"
- " }\n"
- " }\n"
- "\n";
- }
- }
- /*
- * Local Variables:
- * mode: c++
- * indent-tabs-mode: 1
- * c-file-style: "bsd"
- * End:
- */
|