C# patch Daniel Tang.
[external/ragel.git] / rlgen-csharp / tabcodegen.cpp
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
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 "rlgen-csharp.h"
25 #include "tabcodegen.h"
26 #include "redfsm.h"
27 #include "gendata.h"
28
29 /* Determine if we should use indicies or not. */
30 void CSharpTabCodeGen::calcIndexSize()
31 {
32         int sizeWithInds = 0, sizeWithoutInds = 0;
33
34         /* Calculate cost of using with indicies. */
35         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
36                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
37                                 (st->defTrans == 0 ? 0 : 1);
38                 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
39         }
40         sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
41         if ( redFsm->anyActions() )
42                 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
43
44         /* Calculate the cost of not using indicies. */
45         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
46                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
47                                 (st->defTrans == 0 ? 0 : 1);
48                 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
49                 if ( redFsm->anyActions() )
50                         sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
51         }
52
53         /* If using indicies reduces the size, use them. */
54         useIndicies = sizeWithInds < sizeWithoutInds;
55 }
56
57 std::ostream &CSharpTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
58 {
59         int act = 0;
60         if ( state->toStateAction != 0 )
61                 act = state->toStateAction->location+1;
62         out << act;
63         return out;
64 }
65
66 std::ostream &CSharpTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
67 {
68         int act = 0;
69         if ( state->fromStateAction != 0 )
70                 act = state->fromStateAction->location+1;
71         out << act;
72         return out;
73 }
74
75 std::ostream &CSharpTabCodeGen::EOF_ACTION( RedStateAp *state )
76 {
77         int act = 0;
78         if ( state->eofAction != 0 )
79                 act = state->eofAction->location+1;
80         out << act;
81         return out;
82 }
83
84
85 std::ostream &CSharpTabCodeGen::TRANS_ACTION( RedTransAp *trans )
86 {
87         /* If there are actions, emit them. Otherwise emit zero. */
88         int act = 0;
89         if ( trans->action != 0 )
90                 act = trans->action->location+1;
91         out << act;
92         return out;
93 }
94
95 std::ostream &CSharpTabCodeGen::TO_STATE_ACTION_SWITCH()
96 {
97         /* Walk the list of functions, printing the cases. */
98         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
99                 /* Write out referenced actions. */
100                 if ( act->numToStateRefs > 0 ) {
101                         /* Write the case label, the action and the case break. */
102                         out << "\tcase " << act->actionId << ":\n";
103                         ACTION( out, act, 0, false );
104                         out << "\tbreak;\n";
105                 }
106         }
107
108         genLineDirective( out );
109         return out;
110 }
111
112 std::ostream &CSharpTabCodeGen::FROM_STATE_ACTION_SWITCH()
113 {
114         /* Walk the list of functions, printing the cases. */
115         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
116                 /* Write out referenced actions. */
117                 if ( act->numFromStateRefs > 0 ) {
118                         /* Write the case label, the action and the case break. */
119                         out << "\tcase " << act->actionId << ":\n";
120                         ACTION( out, act, 0, false );
121                         out << "\tbreak;\n";
122                 }
123         }
124
125         genLineDirective( out );
126         return out;
127 }
128
129 std::ostream &CSharpTabCodeGen::EOF_ACTION_SWITCH()
130 {
131         /* Walk the list of functions, printing the cases. */
132         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
133                 /* Write out referenced actions. */
134                 if ( act->numEofRefs > 0 ) {
135                         /* Write the case label, the action and the case break. */
136                         out << "\tcase " << act->actionId << ":\n";
137                         ACTION( out, act, 0, true );
138                         out << "\tbreak;\n";
139                 }
140         }
141
142         genLineDirective( out );
143         return out;
144 }
145
146
147 std::ostream &CSharpTabCodeGen::ACTION_SWITCH()
148 {
149         /* Walk the list of functions, printing the cases. */
150         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
151                 /* Write out referenced actions. */
152                 if ( act->numTransRefs > 0 ) {
153                         /* Write the case label, the action and the case break. */
154                         out << "\tcase " << act->actionId << ":\n";
155                         ACTION( out, act, 0, false );
156                         out << "\tbreak;\n";
157                 }
158         }
159
160         genLineDirective( out );
161         return out;
162 }
163
164 std::ostream &CSharpTabCodeGen::COND_OFFSETS()
165 {
166         out << "\t";
167         int totalStateNum = 0, curKeyOffset = 0;
168         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
169                 /* Write the key offset. */
170                 out << curKeyOffset;
171                 if ( !st.last() ) {
172                         out << ", ";
173                         if ( ++totalStateNum % IALL == 0 )
174                                 out << "\n\t";
175                 }
176
177                 /* Move the key offset ahead. */
178                 curKeyOffset += st->stateCondList.length();
179         }
180         out << "\n";
181         return out;
182 }
183
184 std::ostream &CSharpTabCodeGen::KEY_OFFSETS()
185 {
186         out << "\t";
187         int totalStateNum = 0, curKeyOffset = 0;
188         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
189                 /* Write the key offset. */
190                 out << curKeyOffset;
191                 if ( !st.last() ) {
192                         out << ", ";
193                         if ( ++totalStateNum % IALL == 0 )
194                                 out << "\n\t";
195                 }
196
197                 /* Move the key offset ahead. */
198                 curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
199         }
200         out << "\n";
201         return out;
202 }
203
204
205 std::ostream &CSharpTabCodeGen::INDEX_OFFSETS()
206 {
207         out << "\t";
208         int totalStateNum = 0, curIndOffset = 0;
209         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
210                 /* Write the index offset. */
211                 out << curIndOffset;
212                 if ( !st.last() ) {
213                         out << ", ";
214                         if ( ++totalStateNum % IALL == 0 )
215                                 out << "\n\t";
216                 }
217
218                 /* Move the index offset ahead. */
219                 curIndOffset += st->outSingle.length() + st->outRange.length();
220                 if ( st->defTrans != 0 )
221                         curIndOffset += 1;
222         }
223         out << "\n";
224         return out;
225 }
226
227 std::ostream &CSharpTabCodeGen::COND_LENS()
228 {
229         out << "\t";
230         int totalStateNum = 0;
231         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
232                 /* Write singles length. */
233                 out << st->stateCondList.length();
234                 if ( !st.last() ) {
235                         out << ", ";
236                         if ( ++totalStateNum % IALL == 0 )
237                                 out << "\n\t";
238                 }
239         }
240         out << "\n";
241         return out;
242 }
243
244
245 std::ostream &CSharpTabCodeGen::SINGLE_LENS()
246 {
247         out << "\t";
248         int totalStateNum = 0;
249         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
250                 /* Write singles length. */
251                 out << st->outSingle.length();
252                 if ( !st.last() ) {
253                         out << ", ";
254                         if ( ++totalStateNum % IALL == 0 )
255                                 out << "\n\t";
256                 }
257         }
258         out << "\n";
259         return out;
260 }
261
262 std::ostream &CSharpTabCodeGen::RANGE_LENS()
263 {
264         out << "\t";
265         int totalStateNum = 0;
266         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
267                 /* Emit length of range index. */
268                 out << st->outRange.length();
269                 if ( !st.last() ) {
270                         out << ", ";
271                         if ( ++totalStateNum % IALL == 0 )
272                                 out << "\n\t";
273                 }
274         }
275         out << "\n";
276         return out;
277 }
278
279 std::ostream &CSharpTabCodeGen::TO_STATE_ACTIONS()
280 {
281         out << "\t";
282         int totalStateNum = 0;
283         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
284                 /* Write any eof action. */
285                 TO_STATE_ACTION(st);
286                 if ( !st.last() ) {
287                         out << ", ";
288                         if ( ++totalStateNum % IALL == 0 )
289                                 out << "\n\t";
290                 }
291         }
292         out << "\n";
293         return out;
294 }
295
296 std::ostream &CSharpTabCodeGen::FROM_STATE_ACTIONS()
297 {
298         out << "\t";
299         int totalStateNum = 0;
300         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
301                 /* Write any eof action. */
302                 FROM_STATE_ACTION(st);
303                 if ( !st.last() ) {
304                         out << ", ";
305                         if ( ++totalStateNum % IALL == 0 )
306                                 out << "\n\t";
307                 }
308         }
309         out << "\n";
310         return out;
311 }
312
313 std::ostream &CSharpTabCodeGen::EOF_ACTIONS()
314 {
315         out << "\t";
316         int totalStateNum = 0;
317         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
318                 /* Write any eof action. */
319                 EOF_ACTION(st);
320                 if ( !st.last() ) {
321                         out << ", ";
322                         if ( ++totalStateNum % IALL == 0 )
323                                 out << "\n\t";
324                 }
325         }
326         out << "\n";
327         return out;
328 }
329
330 std::ostream &CSharpTabCodeGen::EOF_TRANS()
331 {
332         out << "\t";
333         int totalStateNum = 0;
334         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
335                 /* Write any eof action. */
336                 long trans = 0;
337                 if ( st->eofTrans != 0 )
338                         trans = st->eofTrans->id+1;
339                 out << trans;
340
341                 if ( !st.last() ) {
342                         out << ", ";
343                         if ( ++totalStateNum % IALL == 0 )
344                                 out << "\n\t";
345                 }
346         }
347         out << "\n";
348         return out;
349 }
350
351
352 std::ostream &CSharpTabCodeGen::COND_KEYS()
353 {
354         out << '\t';
355         int totalTrans = 0;
356         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
357                 /* Loop the state's transitions. */
358                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
359                         /* Lower key. */
360                         out << ALPHA_KEY( sc->lowKey ) << ", ";
361                         if ( ++totalTrans % IALL == 0 )
362                                 out << "\n\t";
363
364                         /* Upper key. */
365                         out << ALPHA_KEY( sc->highKey ) << ", ";
366                         if ( ++totalTrans % IALL == 0 )
367                                 out << "\n\t";
368                 }
369         }
370
371         /* Output one last number so we don't have to figure out when the last
372          * entry is and avoid writing a comma. */
373         out << 0 << "\n";
374         return out;
375 }
376
377 std::ostream &CSharpTabCodeGen::COND_SPACES()
378 {
379         out << '\t';
380         int totalTrans = 0;
381         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
382                 /* Loop the state's transitions. */
383                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
384                         /* Cond Space id. */
385                         out << sc->condSpace->condSpaceId << ", ";
386                         if ( ++totalTrans % IALL == 0 )
387                                 out << "\n\t";
388                 }
389         }
390
391         /* Output one last number so we don't have to figure out when the last
392          * entry is and avoid writing a comma. */
393         out << 0 << "\n";
394         return out;
395 }
396
397 std::ostream &CSharpTabCodeGen::KEYS()
398 {
399         out << '\t';
400         int totalTrans = 0;
401         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
402                 /* Loop the singles. */
403                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
404                         out << ALPHA_KEY( stel->lowKey ) << ", ";
405                         if ( ++totalTrans % IALL == 0 )
406                                 out << "\n\t";
407                 }
408
409                 /* Loop the state's transitions. */
410                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
411                         /* Lower key. */
412                         out << ALPHA_KEY( rtel->lowKey ) << ", ";
413                         if ( ++totalTrans % IALL == 0 )
414                                 out << "\n\t";
415
416                         /* Upper key. */
417                         out << ALPHA_KEY( rtel->highKey ) << ", ";
418                         if ( ++totalTrans % IALL == 0 )
419                                 out << "\n\t";
420                 }
421         }
422
423         /* Output one last number so we don't have to figure out when the last
424          * entry is and avoid writing a comma. */
425         out << "(char) " << 0 << "\n";
426         return out;
427 }
428
429 std::ostream &CSharpTabCodeGen::INDICIES()
430 {
431         int totalTrans = 0;
432         out << '\t';
433         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
434                 /* Walk the singles. */
435                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
436                         out << stel->value->id << ", ";
437                         if ( ++totalTrans % IALL == 0 )
438                                 out << "\n\t";
439                 }
440
441                 /* Walk the ranges. */
442                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
443                         out << rtel->value->id << ", ";
444                         if ( ++totalTrans % IALL == 0 )
445                                 out << "\n\t";
446                 }
447
448                 /* The state's default index goes next. */
449                 if ( st->defTrans != 0 ) {
450                         out << st->defTrans->id << ", ";
451                         if ( ++totalTrans % IALL == 0 )
452                                 out << "\n\t";
453                 }
454         }
455
456         /* Output one last number so we don't have to figure out when the last
457          * entry is and avoid writing a comma. */
458         out << 0 << "\n";
459         return out;
460 }
461
462 std::ostream &CSharpTabCodeGen::TRANS_TARGS()
463 {
464         int totalTrans = 0;
465         out << '\t';
466         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
467                 /* Walk the singles. */
468                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
469                         RedTransAp *trans = stel->value;
470                         out << trans->targ->id << ", ";
471                         if ( ++totalTrans % IALL == 0 )
472                                 out << "\n\t";
473                 }
474
475                 /* Walk the ranges. */
476                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
477                         RedTransAp *trans = rtel->value;
478                         out << trans->targ->id << ", ";
479                         if ( ++totalTrans % IALL == 0 )
480                                 out << "\n\t";
481                 }
482
483                 /* The state's default target state. */
484                 if ( st->defTrans != 0 ) {
485                         RedTransAp *trans = st->defTrans;
486                         out << trans->targ->id << ", ";
487                         if ( ++totalTrans % IALL == 0 )
488                                 out << "\n\t";
489                 }
490         }
491
492         /* Output one last number so we don't have to figure out when the last
493          * entry is and avoid writing a comma. */
494         out << 0 << "\n";
495         return out;
496 }
497
498
499 std::ostream &CSharpTabCodeGen::TRANS_ACTIONS()
500 {
501         int totalTrans = 0;
502         out << '\t';
503         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
504                 /* Walk the singles. */
505                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
506                         RedTransAp *trans = stel->value;
507                         TRANS_ACTION( trans ) << ", ";
508                         if ( ++totalTrans % IALL == 0 )
509                                 out << "\n\t";
510                 }
511
512                 /* Walk the ranges. */
513                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
514                         RedTransAp *trans = rtel->value;
515                         TRANS_ACTION( trans ) << ", ";
516                         if ( ++totalTrans % IALL == 0 )
517                                 out << "\n\t";
518                 }
519
520                 /* The state's default index goes next. */
521                 if ( st->defTrans != 0 ) {
522                         RedTransAp *trans = st->defTrans;
523                         TRANS_ACTION( trans ) << ", ";
524                         if ( ++totalTrans % IALL == 0 )
525                                 out << "\n\t";
526                 }
527         }
528
529         /* Output one last number so we don't have to figure out when the last
530          * entry is and avoid writing a comma. */
531         out << 0 << "\n";
532         return out;
533 }
534
535 std::ostream &CSharpTabCodeGen::TRANS_TARGS_WI()
536 {
537         /* Transitions must be written ordered by their id. */
538         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
539         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
540                 transPtrs[trans->id] = trans;
541
542         /* Keep a count of the num of items in the array written. */
543         out << '\t';
544         int totalStates = 0;
545         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
546                 /* Write out the target state. */
547                 RedTransAp *trans = transPtrs[t];
548                 out << trans->targ->id;
549                 if ( t < redFsm->transSet.length()-1 ) {
550                         out << ", ";
551                         if ( ++totalStates % IALL == 0 )
552                                 out << "\n\t";
553                 }
554         }
555         out << "\n";
556         delete[] transPtrs;
557         return out;
558 }
559
560
561 std::ostream &CSharpTabCodeGen::TRANS_ACTIONS_WI()
562 {
563         /* Transitions must be written ordered by their id. */
564         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
565         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
566                 transPtrs[trans->id] = trans;
567
568         /* Keep a count of the num of items in the array written. */
569         out << '\t';
570         int totalAct = 0;
571         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
572                 /* Write the function for the transition. */
573                 RedTransAp *trans = transPtrs[t];
574                 TRANS_ACTION( trans );
575                 if ( t < redFsm->transSet.length()-1 ) {
576                         out << ", ";
577                         if ( ++totalAct % IALL == 0 )
578                                 out << "\n\t";
579                 }
580         }
581         out << "\n";
582         delete[] transPtrs;
583         return out;
584 }
585
586 void CSharpTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
587 {
588         ret << "{" << CS() << " = " << gotoDest << "; " << 
589                         CTRL_FLOW() << "goto _again;}";
590 }
591
592 void CSharpTabCodeGen::GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
593 {
594         ret << "{" << CS() << " = (";
595         INLINE_LIST( ret, ilItem->children, 0, inFinish );
596         ret << "); " << CTRL_FLOW() << "goto _again;}";
597 }
598
599 void CSharpTabCodeGen::CURS( ostream &ret, bool inFinish )
600 {
601         ret << "(_ps)";
602 }
603
604 void CSharpTabCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
605 {
606         ret << "(" << CS() << ")";
607 }
608
609 void CSharpTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
610 {
611         ret << CS() << " = " << nextDest << ";";
612 }
613
614 void CSharpTabCodeGen::NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
615 {
616         ret << CS() << " = (";
617         INLINE_LIST( ret, ilItem->children, 0, inFinish );
618         ret << ");";
619 }
620
621 void CSharpTabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
622 {
623         if ( prePushExpr != 0 ) {
624                 ret << "{";
625                 INLINE_LIST( ret, prePushExpr, 0, false );
626         }
627
628         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " << 
629                         callDest << "; " << CTRL_FLOW() << "goto _again;}";
630
631         if ( prePushExpr != 0 )
632                 ret << "}";
633 }
634
635 void CSharpTabCodeGen::CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish )
636 {
637         if ( prePushExpr != 0 ) {
638                 ret << "{";
639                 INLINE_LIST( ret, prePushExpr, 0, false );
640         }
641
642         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
643         INLINE_LIST( ret, ilItem->children, targState, inFinish );
644         ret << "); " << CTRL_FLOW() << "goto _again;}";
645
646         if ( prePushExpr != 0 )
647                 ret << "}";
648 }
649
650 void CSharpTabCodeGen::RET( ostream &ret, bool inFinish )
651 {
652         ret << "{" << CS() << " = " << STACK() << "[--" << 
653                         TOP() << "]; ";
654
655         if ( postPopExpr != 0 ) {
656                 ret << "{";
657                 INLINE_LIST( ret, postPopExpr, 0, false );
658                 ret << "}";
659         }
660
661         ret << CTRL_FLOW() <<  "goto _again;}";
662 }
663
664 void CSharpTabCodeGen::BREAK( ostream &ret, int targState )
665 {
666         outLabelUsed = true;
667         ret << "{" << P() << "++; " << CTRL_FLOW() << "goto _out; }";
668 }
669
670 void CSharpTabCodeGen::writeData()
671 {
672         /* If there are any transtion functions then output the array. If there
673          * are none, don't bother emitting an empty array that won't be used. */
674         if ( redFsm->anyActions() ) {
675                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
676                 ACTIONS_ARRAY();
677                 CLOSE_ARRAY() <<
678                 "\n";
679         }
680
681         if ( redFsm->anyConditions() ) {
682                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
683                 COND_OFFSETS();
684                 CLOSE_ARRAY() <<
685                 "\n";
686
687                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
688                 COND_LENS();
689                 CLOSE_ARRAY() <<
690                 "\n";
691
692                 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
693                 COND_KEYS();
694                 CLOSE_ARRAY() <<
695                 "\n";
696
697                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
698                 COND_SPACES();
699                 CLOSE_ARRAY() <<
700                 "\n";
701         }
702
703         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
704         KEY_OFFSETS();
705         CLOSE_ARRAY() <<
706         "\n";
707
708         OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
709         KEYS();
710         CLOSE_ARRAY() <<
711         "\n";
712
713         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
714         SINGLE_LENS();
715         CLOSE_ARRAY() <<
716         "\n";
717
718         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
719         RANGE_LENS();
720         CLOSE_ARRAY() <<
721         "\n";
722
723         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
724         INDEX_OFFSETS();
725         CLOSE_ARRAY() <<
726         "\n";
727
728         if ( useIndicies ) {
729                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
730                 INDICIES();
731                 CLOSE_ARRAY() <<
732                 "\n";
733
734                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
735                 TRANS_TARGS_WI();
736                 CLOSE_ARRAY() <<
737                 "\n";
738
739                 if ( redFsm->anyActions() ) {
740                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
741                         TRANS_ACTIONS_WI();
742                         CLOSE_ARRAY() <<
743                         "\n";
744                 }
745         }
746         else {
747                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
748                 TRANS_TARGS();
749                 CLOSE_ARRAY() <<
750                 "\n";
751
752                 if ( redFsm->anyActions() ) {
753                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
754                         TRANS_ACTIONS();
755                         CLOSE_ARRAY() <<
756                         "\n";
757                 }
758         }
759
760         if ( redFsm->anyToStateActions() ) {
761                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
762                 TO_STATE_ACTIONS();
763                 CLOSE_ARRAY() <<
764                 "\n";
765         }
766
767         if ( redFsm->anyFromStateActions() ) {
768                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
769                 FROM_STATE_ACTIONS();
770                 CLOSE_ARRAY() <<
771                 "\n";
772         }
773
774         if ( redFsm->anyEofActions() ) {
775                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
776                 EOF_ACTIONS();
777                 CLOSE_ARRAY() <<
778                 "\n";
779         }
780
781         if ( redFsm->anyEofTrans() ) {
782                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex+1), ET() );
783                 EOF_TRANS();
784                 CLOSE_ARRAY() <<
785                 "\n";
786         }
787
788         STATE_IDS();
789 }
790
791 void CSharpTabCodeGen::LOCATE_TRANS()
792 {
793         out <<
794                 "       _keys = " << KO() + "[" + CS() + "]" << ";\n"
795                 "       _trans = " << CAST(transType) << IO() << "[" << CS() << "];\n"
796                 "\n"
797                 "       _klen = " << SL() << "[" << CS() << "];\n"
798                 "       if ( _klen > 0 ) {\n"
799                 "               " << signedKeysType << " _lower = _keys;\n"
800                 "               " << signedKeysType << " _mid;\n"
801                 "               " << signedKeysType << " _upper = " << CAST(signedKeysType) << 
802                         " (_keys + _klen - 1);\n"
803                 "               while (true) {\n"
804                 "                       if ( _upper < _lower )\n"
805                 "                               break;\n"
806                 "\n"
807                 "                       _mid = " << CAST(signedKeysType) << 
808                         " (_lower + ((_upper-_lower) >> 1));\n"
809                 "                       if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
810                 "                               _upper = " << CAST(signedKeysType) << " (_mid - 1);\n"
811                 "                       else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid] )\n"
812                 "                               _lower = " << CAST(signedKeysType) << " (_mid + 1);\n"
813                 "                       else {\n"
814                 "                               _trans += " << CAST(transType) << " (_mid - _keys);\n"
815                 "                               goto _match;\n"
816                 "                       }\n"
817                 "               }\n"
818                 "               _keys += " << CAST(keysType) << " _klen;\n"
819                 "               _trans += " << CAST(transType) << " _klen;\n"
820                 "       }\n"
821                 "\n"
822                 "       _klen = " << RL() << "[" << CS() << "];\n"
823                 "       if ( _klen > 0 ) {\n"
824                 "               " << signedKeysType << " _lower = _keys;\n"
825                 "               " << signedKeysType << " _mid;\n"
826                 "               " << signedKeysType << " _upper = " << CAST(signedKeysType) <<
827                         " (_keys + (_klen<<1) - 2);\n"
828                 "               while (true) {\n"
829                 "                       if ( _upper < _lower )\n"
830                 "                               break;\n"
831                 "\n"
832                 "                       _mid = " << CAST(signedKeysType) << 
833                         " (_lower + (((_upper-_lower) >> 1) & ~1));\n"
834                 "                       if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
835                 "                               _upper = " << CAST(signedKeysType) << " (_mid - 2);\n"
836                 "                       else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid+1] )\n"
837                 "                               _lower = " << CAST(signedKeysType) << " (_mid + 2);\n"
838                 "                       else {\n"
839                 "                               _trans += " << CAST(transType) << "((_mid - _keys)>>1);\n"
840                 "                               goto _match;\n"
841                 "                       }\n"
842                 "               }\n"
843                 "               _trans += " << CAST(transType) << " _klen;\n"
844                 "       }\n"
845                 "\n";
846 }
847
848 void CSharpTabCodeGen::COND_TRANSLATE()
849 {
850         out << 
851                 "       _widec = " << GET_KEY() << ";\n"
852                 "       _klen = " << CL() << "[" << CS() << "];\n"
853                 "       _keys = " << CAST(keysType) << " ("<< CO() << "[" << CS() << "]*2);\n"
854                 "       if ( _klen > 0 ) {\n"
855                 "               " << signedKeysType << " _lower = _keys;\n"
856                 "               " << signedKeysType << " _mid;\n"
857                 "               " << signedKeysType << " _upper = " << CAST(signedKeysType) << 
858                         " (_keys + (_klen<<1) - 2);\n"
859                 "               while (true) {\n"
860                 "                       if ( _upper < _lower )\n"
861                 "                               break;\n"
862                 "\n"
863                 "                       _mid = " << CAST(signedKeysType) << 
864                         " (_lower + (((_upper-_lower) >> 1) & ~1));\n"
865                 "                       if ( " << GET_WIDE_KEY() << " < " << CK() << "[_mid] )\n"
866                 "                               _upper = " << CAST(signedKeysType) << " (_mid - 2);\n"
867                 "                       else if ( " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1] )\n"
868                 "                               _lower = " << CAST(signedKeysType) << " (_mid + 2);\n"
869                 "                       else {\n"
870                 "                               switch ( " << C() << "[" << CO() << "[" << CS() << "]"
871                                                         " + ((_mid - _keys)>>1)] ) {\n";
872
873         for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
874                 CondSpace *condSpace = csi;
875                 out << "        case " << condSpace->condSpaceId << ": {\n";
876                 out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
877                                 KEY(condSpace->baseKey) << " + (" << GET_KEY() << 
878                                 " - " << KEY(keyOps->minKey) << "));\n";
879
880                 for ( CondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
881                         out << TABS(2) << "if ( ";
882                         CONDITION( out, *csi );
883                         Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
884                         out << " ) _widec += " << condValOffset << ";\n";
885                 }
886
887                 out << 
888                         "               break;\n"
889                         "       }\n";
890         }
891
892         SWITCH_DEFAULT();
893
894         out << 
895                 "                               }\n"
896                 "                               break;\n"
897                 "                       }\n"
898                 "               }\n"
899                 "       }\n"
900                 "\n";
901 }
902
903 void CSharpTabCodeGen::writeExec()
904 {
905         testEofUsed = false;
906         outLabelUsed = false;
907         initVarTypes();
908
909         out <<
910                 "       {\n"
911                 "       " << klenType << " _klen";
912
913         if ( redFsm->anyRegCurStateRef() )
914                 out << ", _ps";
915
916         out << 
917                 ";\n"
918                 "       " << transType << " _trans;\n";
919
920         if ( redFsm->anyConditions() )
921                 out << "        " << WIDE_ALPH_TYPE() << " _widec;\n";
922
923         if ( redFsm->anyToStateActions() || redFsm->anyRegActions() 
924                         || redFsm->anyFromStateActions() )
925         {
926                 out << 
927                         "       " << actsType << " _acts;\n"
928                         "       " << nactsType << " _nacts;\n";
929         }
930
931         out <<
932                 "       " << keysType << " _keys;\n"
933                 "\n";
934 //              "       " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
935
936         if ( hasEnd ) {
937                 testEofUsed = true;
938                 out << 
939                         "       if ( " << P() << " == " << PE() << " )\n"
940                         "               goto _test_eof;\n";
941         }
942
943         if ( redFsm->errState != 0 ) {
944                 outLabelUsed = true;
945                 out << 
946                         "       if ( " << CS() << " == " << redFsm->errState->id << " )\n"
947                         "               goto _out;\n";
948         }
949
950         out << "_resume:\n";
951
952         if ( redFsm->anyFromStateActions() ) {
953                 out <<
954                         "       _acts = " << FSA() << "[" + CS() + "]" << ";\n"
955                         "       _nacts = " << A() << "[_acts++];\n"
956                         "       while ( _nacts-- > 0 ) {\n"
957                         "               switch ( " << A() << "[_acts++] ) {\n";
958                         FROM_STATE_ACTION_SWITCH();
959                         SWITCH_DEFAULT() <<
960                         "               }\n"
961                         "       }\n"
962                         "\n";
963         }
964
965         if ( redFsm->anyConditions() )
966                 COND_TRANSLATE();
967
968         LOCATE_TRANS();
969
970         out << "_match:\n";
971
972         if ( useIndicies )
973                 out << "        _trans = " << CAST(transType) << I() << "[_trans];\n";
974         
975         if ( redFsm->anyEofTrans() )
976                 out << "_eof_trans:\n";
977
978         if ( redFsm->anyRegCurStateRef() )
979                 out << "        _ps = " << CS() << ";\n";
980
981         out <<
982                 "       " << CS() << " = " << TT() << "[_trans];\n"
983                 "\n";
984
985         if ( redFsm->anyRegActions() ) {
986                 out <<
987                         "       if ( " << TA() << "[_trans] == 0 )\n"
988                         "               goto _again;\n"
989                         "\n"
990                         "       _acts = " << TA() << "[_trans]" << ";\n"
991                         "       _nacts = " << A() << "[_acts++];\n"
992                         "       while ( _nacts-- > 0 )\n        {\n"
993                         "               switch ( " << A() << "[_acts++] )\n             {\n";
994                         ACTION_SWITCH();
995                         SWITCH_DEFAULT() <<
996                         "               }\n"
997                         "       }\n"
998                         "\n";
999         }
1000
1001         if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || 
1002                         redFsm->anyActionCalls() || redFsm->anyActionRets() )
1003                 out << "_again:\n";
1004
1005         if ( redFsm->anyToStateActions() ) {
1006                 out <<
1007                         "       _acts = " << TSA() << "[" << CS() << "]" << ";\n"
1008                         "       _nacts = " << A() << "[_acts++];\n"
1009                         "       while ( _nacts-- > 0 ) {\n"
1010                         "               switch ( " << A() << "[_acts++] ) {\n";
1011                         TO_STATE_ACTION_SWITCH();
1012                         SWITCH_DEFAULT() <<
1013                         "               }\n"
1014                         "       }\n"
1015                         "\n";
1016         }
1017
1018         if ( redFsm->errState != 0 ) {
1019                 outLabelUsed = true;
1020                 out << 
1021                         "       if ( " << CS() << " == " << redFsm->errState->id << " )\n"
1022                         "               goto _out;\n";
1023         }
1024
1025         if ( hasEnd ) {
1026                 out << 
1027                         "       if ( ++" << P() << " != " << PE() << " )\n"
1028                         "               goto _resume;\n";
1029         }
1030         else {
1031                 out << 
1032                         "       " << P() << " += 1;\n"
1033                         "       goto _resume;\n";
1034         }
1035         
1036         if ( testEofUsed )
1037                 out << "        _test_eof: {}\n";
1038         
1039         if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
1040                 out << 
1041                         "       if ( " << P() << " == " << EOFV() << " )\n"
1042                         "       {\n";
1043
1044                 if ( redFsm->anyEofTrans() ) {
1045                         out <<
1046                                 "       if ( " << ET() << "[" << CS() << "] > 0 ) {\n"
1047                                 "               _trans = " << CAST(transType) << " (" << ET() <<
1048                                         "[" << CS() << "] - 1);\n"
1049                                 "               goto _eof_trans;\n"
1050                                 "       }\n";
1051                 }
1052
1053                 if ( redFsm->anyEofActions() ) {
1054                         out <<
1055                                 "       " << actsType << " __acts = " << 
1056                                                 EA() << "[" << CS() << "]" << ";\n"
1057                                 "       " << nactsType << " __nacts = " << 
1058                                 A() << "[__acts++];\n"
1059                                 "       while ( __nacts-- > 0 ) {\n"
1060                                 "               switch ( " << A() << "[__acts++] ) {\n";
1061                                 EOF_ACTION_SWITCH();
1062                                 SWITCH_DEFAULT() <<
1063                                 "               }\n"
1064                                 "       }\n";
1065                 }
1066                 
1067                 out << 
1068                         "       }\n"
1069                         "\n";
1070         }
1071
1072         if ( outLabelUsed )
1073                 out << "        _out: {}\n";
1074
1075         out << "        }\n";
1076 }
1077
1078 void CSharpTabCodeGen::initVarTypes()
1079 {
1080         int klenMax = MAX(MAX(redFsm->maxCondLen, redFsm->maxRangeLen),
1081                                 redFsm->maxSingleLen);
1082         int keysMax = MAX(MAX(redFsm->maxKeyOffset, klenMax),
1083                                 redFsm->maxCondOffset);
1084         int transMax = MAX(MAX(redFsm->maxIndex+1, redFsm->maxIndexOffset), keysMax);
1085         transMax = MAX(transMax, klenMax);
1086         transType = ARRAY_TYPE(transMax);
1087         klenType = ARRAY_TYPE(klenMax);
1088         keysType = ARRAY_TYPE(keysMax);
1089         signedKeysType = ARRAY_TYPE(keysMax, true);
1090         actsType = ARRAY_TYPE(redFsm->maxActionLoc);
1091         nactsType = ARRAY_TYPE(redFsm->maxActArrItem);
1092 }