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 "fgotocodegen.h"
30 std::ostream &FGotoCodeGen::EXEC_ACTIONS()
32 /* Loop the actions. */
33 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
34 if ( redAct->numTransRefs > 0 ) {
35 /* We are at the start of a glob, write the case. */
36 out << "f" << redAct->actListId << ":\n";
38 /* Write each action in the list of action items. */
39 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
40 ACTION( out, item->value, 0, false );
42 out << "\tgoto _again;\n";
48 /* Write out the function switch. This switch is keyed on the values
49 * of the func index. */
50 std::ostream &FGotoCodeGen::TO_STATE_ACTION_SWITCH()
52 /* Loop the actions. */
53 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
54 if ( redAct->numToStateRefs > 0 ) {
55 /* Write the entry label. */
56 out << "\tcase " << redAct->actListId+1 << ":\n";
58 /* Write each action in the list of action items. */
59 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
60 ACTION( out, item->value, 0, false );
66 genLineDirective( out );
70 /* Write out the function switch. This switch is keyed on the values
71 * of the func index. */
72 std::ostream &FGotoCodeGen::FROM_STATE_ACTION_SWITCH()
74 /* Loop the actions. */
75 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
76 if ( redAct->numFromStateRefs > 0 ) {
77 /* Write the entry label. */
78 out << "\tcase " << redAct->actListId+1 << ":\n";
80 /* Write each action in the list of action items. */
81 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
82 ACTION( out, item->value, 0, false );
88 genLineDirective( out );
92 std::ostream &FGotoCodeGen::EOF_ACTION_SWITCH()
94 /* Loop the actions. */
95 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
96 if ( redAct->numEofRefs > 0 ) {
97 /* Write the entry label. */
98 out << "\tcase " << redAct->actListId+1 << ":\n";
100 /* Write each action in the list of action items. */
101 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
102 ACTION( out, item->value, 0, true );
108 genLineDirective( out );
113 std::ostream &FGotoCodeGen::FINISH_CASES()
115 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
116 /* States that are final and have an out action need a case. */
117 if ( st->eofAction != 0 ) {
118 /* Write the case label. */
119 out << "\t\tcase " << st->id << ": ";
121 /* Jump to the func. */
122 out << "goto f" << st->eofAction->actListId << ";\n";
129 unsigned int FGotoCodeGen::TO_STATE_ACTION( RedStateAp *state )
132 if ( state->toStateAction != 0 )
133 act = state->toStateAction->actListId+1;
137 unsigned int FGotoCodeGen::FROM_STATE_ACTION( RedStateAp *state )
140 if ( state->fromStateAction != 0 )
141 act = state->fromStateAction->actListId+1;
145 unsigned int FGotoCodeGen::EOF_ACTION( RedStateAp *state )
148 if ( state->eofAction != 0 )
149 act = state->eofAction->actListId+1;
153 void FGotoCodeGen::writeOutData()
156 "static const int " << START() << " = " << START_STATE_ID() << ";\n"
159 if ( cgd->writeFirstFinal ) {
161 "static const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
165 if ( cgd->writeErr ) {
167 "static const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
171 if ( anyToStateActions() ) {
172 OPEN_ARRAY( ARRAY_TYPE(maxActionLoc), TSA() );
178 if ( anyFromStateActions() ) {
179 OPEN_ARRAY( ARRAY_TYPE(maxActionLoc), FSA() );
180 FROM_STATE_ACTIONS();
185 if ( anyEofActions() ) {
186 OPEN_ARRAY( ARRAY_TYPE(maxActionLoc), EA() );
193 void FGotoCodeGen::writeOutExec()
195 outLabelUsed = false;
199 if ( anyRegCurStateRef() )
200 out << " int _ps = 0;\n";
202 if ( anyConditions() )
203 out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
208 " if ( " << P() << " == " << PE() << " )\n"
214 if ( anyFromStateActions() ) {
216 " switch ( " << FSA() << "[" << CS() << "] ) {\n";
217 FROM_STATE_ACTION_SWITCH();
224 " switch ( " << CS() << " ) {\n";
232 if ( anyRegActions() )
233 EXEC_ACTIONS() << "\n";
237 if ( anyToStateActions() ) {
239 " switch ( " << TSA() << "[" << CS() << "] ) {\n";
240 TO_STATE_ACTION_SWITCH();
248 " if ( ++" << P() << " != " << PE() << " )\n"
253 " " << P() << " += 1;\n"
259 out << " _out: {}\n";
264 void FGotoCodeGen::writeOutEOF()
266 if ( anyEofActions() ) {
269 " switch ( " << EA() << "[" << CS() << "] ) {\n";