Initialize Tizen 2.3
[external/ragel.git] / ragel / cdipgoto.cpp
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
3  *            2004 Erich Ocean <eric.ocean@ampede.com>
4  *            2005 Alan West <alan@alanz.com>
5  */
6
7 /*  This file is part of Ragel.
8  *
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.
13  * 
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.
18  * 
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 
22  */
23
24 #include "ragel.h"
25 #include "cdipgoto.h"
26 #include "redfsm.h"
27 #include "gendata.h"
28 #include "bstmap.h"
29
30 bool IpGotoCodeGen::useAgainLabel()
31 {
32         return redFsm->anyRegActionRets() || 
33                         redFsm->anyRegActionByValControl() || 
34                         redFsm->anyRegNextStmt();
35 }
36
37 void IpGotoCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
38 {
39         ret << "{" << CTRL_FLOW() << "goto st" << gotoDest << ";}";
40 }
41
42 void IpGotoCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
43 {
44         if ( prePushExpr != 0 ) {
45                 ret << "{";
46                 INLINE_LIST( ret, prePushExpr, 0, false, false );
47         }
48
49         ret << "{" << STACK() << "[" << TOP() << "++] = " << targState << 
50                         "; " << CTRL_FLOW() << "goto st" << callDest << ";}";
51
52         if ( prePushExpr != 0 )
53                 ret << "}";
54 }
55
56 void IpGotoCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
57 {
58         if ( prePushExpr != 0 ) {
59                 ret << "{";
60                 INLINE_LIST( ret, prePushExpr, 0, false, false );
61         }
62
63         ret << "{" << STACK() << "[" << TOP() << "++] = " << targState << "; " << vCS() << " = (";
64         INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
65         ret << "); " << CTRL_FLOW() << "goto _again;}";
66
67         if ( prePushExpr != 0 )
68                 ret << "}";
69 }
70
71 void IpGotoCodeGen::RET( ostream &ret, bool inFinish )
72 {
73         ret << "{" << vCS() << " = " << STACK() << "[--" << TOP() << "];";
74
75         if ( postPopExpr != 0 ) {
76                 ret << "{";
77                 INLINE_LIST( ret, postPopExpr, 0, false, false );
78                 ret << "}";
79         }
80
81         ret << CTRL_FLOW() << "goto _again;}";
82 }
83
84 void IpGotoCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
85 {
86         ret << "{" << vCS() << " = (";
87         INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
88         ret << "); " << CTRL_FLOW() << "goto _again;}";
89 }
90
91 void IpGotoCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
92 {
93         ret << vCS() << " = " << nextDest << ";";
94 }
95
96 void IpGotoCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
97 {
98         ret << vCS() << " = (";
99         INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
100         ret << ");";
101 }
102
103 void IpGotoCodeGen::CURS( ostream &ret, bool inFinish )
104 {
105         ret << "(_ps)";
106 }
107
108 void IpGotoCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
109 {
110         ret << targState;
111 }
112
113 void IpGotoCodeGen::BREAK( ostream &ret, int targState, bool csForced )
114 {
115         outLabelUsed = true;
116         ret << "{" << P() << "++; ";
117         if ( !csForced ) 
118                 ret << vCS() << " = " << targState << "; ";
119         ret << CTRL_FLOW() << "goto _out;}";
120 }
121
122 bool IpGotoCodeGen::IN_TRANS_ACTIONS( RedStateAp *state )
123 {
124         bool anyWritten = false;
125
126         /* Emit any transitions that have actions and that go to this state. */
127         for ( int it = 0; it < state->numInTrans; it++ ) {
128                 RedTransAp *trans = state->inTrans[it];
129                 if ( trans->action != 0 && trans->labelNeeded ) {
130                         /* Remember that we wrote an action so we know to write the
131                          * line directive for going back to the output. */
132                         anyWritten = true;
133
134                         /* Write the label for the transition so it can be jumped to. */
135                         out << "tr" << trans->id << ":\n";
136
137                         /* If the action contains a next, then we must preload the current
138                          * state since the action may or may not set it. */
139                         if ( trans->action->anyNextStmt() )
140                                 out << "        " << vCS() << " = " << trans->targ->id << ";\n";
141
142                         /* Write each action in the list. */
143                         for ( GenActionTable::Iter item = trans->action->key; item.lte(); item++ ) {
144                                 ACTION( out, item->value, trans->targ->id, false, 
145                                                 trans->action->anyNextStmt() );
146                         }
147
148                         /* If the action contains a next then we need to reload, otherwise
149                          * jump directly to the target state. */
150                         if ( trans->action->anyNextStmt() )
151                                 out << "\tgoto _again;\n";
152                         else
153                                 out << "\tgoto st" << trans->targ->id << ";\n";
154                 }
155         }
156
157         return anyWritten;
158 }
159
160 /* Called from GotoCodeGen::STATE_GOTOS just before writing the gotos for each
161  * state. */
162 void IpGotoCodeGen::GOTO_HEADER( RedStateAp *state )
163 {
164         bool anyWritten = IN_TRANS_ACTIONS( state );
165
166         if ( state->labelNeeded ) 
167                 out << "st" << state->id << ":\n";
168
169         if ( state->toStateAction != 0 ) {
170                 /* Remember that we wrote an action. Write every action in the list. */
171                 anyWritten = true;
172                 for ( GenActionTable::Iter item = state->toStateAction->key; item.lte(); item++ ) {
173                         ACTION( out, item->value, state->id, false, 
174                                         state->toStateAction->anyNextStmt() );
175                 }
176         }
177
178         /* Advance and test buffer pos. */
179         if ( state->labelNeeded ) {
180                 if ( !noEnd ) {
181                         out <<
182                                 "       if ( ++" << P() << " == " << PE() << " )\n"
183                                 "               goto _test_eof" << state->id << ";\n";
184                 }
185                 else {
186                         out << 
187                                 "       " << P() << " += 1;\n";
188                 }
189         }
190
191         /* Give the state a switch case. */
192         out << "case " << state->id << ":\n";
193
194         if ( state->fromStateAction != 0 ) {
195                 /* Remember that we wrote an action. Write every action in the list. */
196                 anyWritten = true;
197                 for ( GenActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ ) {
198                         ACTION( out, item->value, state->id, false,
199                                         state->fromStateAction->anyNextStmt() );
200                 }
201         }
202
203         if ( anyWritten )
204                 genLineDirective( out );
205
206         /* Record the prev state if necessary. */
207         if ( state->anyRegCurStateRef() )
208                 out << "        _ps = " << state->id << ";\n";
209 }
210
211 void IpGotoCodeGen::STATE_GOTO_ERROR()
212 {
213         /* In the error state we need to emit some stuff that usually goes into
214          * the header. */
215         RedStateAp *state = redFsm->errState;
216         bool anyWritten = IN_TRANS_ACTIONS( state );
217
218         /* No case label needed since we don't switch on the error state. */
219         if ( anyWritten )
220                 genLineDirective( out );
221
222         if ( state->labelNeeded ) 
223                 out << "st" << state->id << ":\n";
224
225         /* Break out here. */
226         outLabelUsed = true;
227         out << vCS() << " = " << state->id << ";\n";
228         out << "        goto _out;\n";
229 }
230
231
232 /* Emit the goto to take for a given transition. */
233 std::ostream &IpGotoCodeGen::TRANS_GOTO( RedTransAp *trans, int level )
234 {
235         if ( trans->action != 0 ) {
236                 /* Go to the transition which will go to the state. */
237                 out << TABS(level) << "goto tr" << trans->id << ";";
238         }
239         else {
240                 /* Go directly to the target state. */
241                 out << TABS(level) << "goto st" << trans->targ->id << ";";
242         }
243         return out;
244 }
245
246 std::ostream &IpGotoCodeGen::EXIT_STATES()
247 {
248         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
249                 if ( st->outNeeded ) {
250                         testEofUsed = true;
251                         out << "        _test_eof" << st->id << ": " << vCS() << " = " << 
252                                         st->id << "; goto _test_eof; \n";
253                 }
254         }
255         return out;
256 }
257
258 std::ostream &IpGotoCodeGen::AGAIN_CASES()
259 {
260         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
261                 out << 
262                         "               case " << st->id << ": goto st" << st->id << ";\n";
263         }
264         return out;
265 }
266
267 std::ostream &IpGotoCodeGen::FINISH_CASES()
268 {
269         bool anyWritten = false;
270
271         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
272                 if ( st->eofAction != 0 ) {
273                         if ( st->eofAction->eofRefs == 0 )
274                                 st->eofAction->eofRefs = new IntSet;
275                         st->eofAction->eofRefs->insert( st->id );
276                 }
277         }
278
279         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
280                 if ( st->eofTrans != 0 )
281                         out << "        case " << st->id << ": goto tr" << st->eofTrans->id << ";\n";
282         }
283
284         for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
285                 if ( act->eofRefs != 0 ) {
286                         for ( IntSet::Iter pst = *act->eofRefs; pst.lte(); pst++ )
287                                 out << "        case " << *pst << ": \n";
288
289                         /* Remember that we wrote a trans so we know to write the
290                          * line directive for going back to the output. */
291                         anyWritten = true;
292
293                         /* Write each action in the eof action list. */
294                         for ( GenActionTable::Iter item = act->key; item.lte(); item++ )
295                                 ACTION( out, item->value, STATE_ERR_STATE, true, false );
296                         out << "\tbreak;\n";
297                 }
298         }
299
300         if ( anyWritten )
301                 genLineDirective( out );
302         return out;
303 }
304
305 void IpGotoCodeGen::setLabelsNeeded( GenInlineList *inlineList )
306 {
307         for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
308                 switch ( item->type ) {
309                 case GenInlineItem::Goto: case GenInlineItem::Call: {
310                         /* Mark the target as needing a label. */
311                         item->targState->labelNeeded = true;
312                         break;
313                 }
314                 default: break;
315                 }
316
317                 if ( item->children != 0 )
318                         setLabelsNeeded( item->children );
319         }
320 }
321
322 /* Set up labelNeeded flag for each state. */
323 void IpGotoCodeGen::setLabelsNeeded()
324 {
325         /* If we use the _again label, then we the _again switch, which uses all
326          * labels. */
327         if ( useAgainLabel() ) {
328                 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
329                         st->labelNeeded = true;
330         }
331         else {
332                 /* Do not use all labels by default, init all labelNeeded vars to false. */
333                 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
334                         st->labelNeeded = false;
335
336                 /* Walk all transitions and set only those that have targs. */
337                 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
338                         /* If there is no action with a next statement, then the label will be
339                          * needed. */
340                         if ( trans->action == 0 || !trans->action->anyNextStmt() )
341                                 trans->targ->labelNeeded = true;
342
343                         /* Need labels for states that have goto or calls in action code
344                          * invoked on characters (ie, not from out action code). */
345                         if ( trans->action != 0 ) {
346                                 /* Loop the actions. */
347                                 for ( GenActionTable::Iter act = trans->action->key; act.lte(); act++ ) {
348                                         /* Get the action and walk it's tree. */
349                                         setLabelsNeeded( act->value->inlineList );
350                                 }
351                         }
352                 }
353         }
354
355         if ( !noEnd ) {
356                 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
357                         if ( st != redFsm->errState )
358                                 st->outNeeded = st->labelNeeded;
359                 }
360         }
361 }
362
363 void IpGotoCodeGen::writeData()
364 {
365         STATE_IDS();
366 }
367
368 void IpGotoCodeGen::writeExec()
369 {
370         /* Must set labels immediately before writing because we may depend on the
371          * noend write option. */
372         setLabelsNeeded();
373         testEofUsed = false;
374         outLabelUsed = false;
375
376         out << "        {\n";
377
378         if ( redFsm->anyRegCurStateRef() )
379                 out << "        int _ps = 0;\n";
380
381         if ( redFsm->anyConditions() )
382                 out << "        " << WIDE_ALPH_TYPE() << " _widec;\n";
383
384         if ( !noEnd ) {
385                 testEofUsed = true;
386                 out << 
387                         "       if ( " << P() << " == " << PE() << " )\n"
388                         "               goto _test_eof;\n";
389         }
390
391         if ( useAgainLabel() ) {
392                 out << 
393                         "       goto _resume;\n"
394                         "\n"
395                         "_again:\n"
396                         "       switch ( " << vCS() << " ) {\n";
397                         AGAIN_CASES() <<
398                         "       default: break;\n"
399                         "       }\n"
400                         "\n";
401
402                 if ( !noEnd ) {
403                         testEofUsed = true;
404                         out << 
405                                 "       if ( ++" << P() << " == " << PE() << " )\n"
406                                 "               goto _test_eof;\n";
407                 }
408                 else {
409                         out << 
410                                 "       " << P() << " += 1;\n";
411                 }
412
413                 out << "_resume:\n";
414         }
415
416         out << 
417                 "       switch ( " << vCS() << " )\n    {\n";
418                 STATE_GOTOS();
419                 SWITCH_DEFAULT() <<
420                 "       }\n";
421                 EXIT_STATES() << 
422                 "\n";
423
424         if ( testEofUsed ) 
425                 out << "        _test_eof: {}\n";
426
427         if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
428                 out <<
429                         "       if ( " << P() << " == " << vEOF() << " )\n"
430                         "       {\n"
431                         "       switch ( " << vCS() << " ) {\n";
432                         FINISH_CASES();
433                         SWITCH_DEFAULT() <<
434                         "       }\n"
435                         "       }\n"
436                         "\n";
437         }
438
439         if ( outLabelUsed ) 
440                 out << "        _out: {}\n";
441
442         out <<
443                 "       }\n";
444 }