summaryrefslogtreecommitdiff
path: root/tools/re2c/code.c
blob: bd54baacf3da90766c77c950f719689661ed8b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tools/re2c/substr.h"
#include "tools/re2c/globals.h"
#include "tools/re2c/dfa.h"
#include "tools/re2c/parse.h"

#ifdef _WIN32
/* tmpfile() replacment for Windows.
 *
 * On Windows tmpfile() creates the file in the root directory. This
 * may fail due to unsufficient privileges.
 */
static FILE *
win32_tmpfile (void)
{
    DWORD path_len;
    WCHAR path_name[MAX_PATH + 1];
    WCHAR file_name[MAX_PATH + 1];
    HANDLE handle;
    int fd;
    FILE *fp;

    path_len = GetTempPathW (MAX_PATH, path_name);
    if (path_len <= 0 || path_len >= MAX_PATH)
	return NULL;

    if (GetTempFileNameW (path_name, L"ps_", 0, file_name) == 0)
	return NULL;

    handle = CreateFileW (file_name,
			 GENERIC_READ | GENERIC_WRITE,
			 0,
			 NULL,
			 CREATE_ALWAYS,
			 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
			 NULL);
    if (handle == INVALID_HANDLE_VALUE) {
	DeleteFileW (file_name);
	return NULL;
    }

    fd = _open_osfhandle((intptr_t) handle, 0);
    if (fd < 0) {
	CloseHandle (handle);
	return NULL;
    }

    fp = fdopen(fd, "w+b");
    if (fp == NULL) {
	_close(fd);
	return NULL;
    }

    return fp;
}
#endif

static void useLabel(size_t value) {
    while (value >= vUsedLabelAlloc) {
	vUsedLabels = realloc(vUsedLabels, vUsedLabelAlloc * 2);
	if (!vUsedLabels) {
	    fputs("Out of memory.\n", stderr);
	    exit(EXIT_FAILURE);
	}
	memset(vUsedLabels + vUsedLabelAlloc, 0, vUsedLabelAlloc);
	vUsedLabelAlloc *= 2;
    }
    vUsedLabels[value] = 1;
}

/* there must be at least one span in list;  all spans must cover
 * same range
 */

void Go_compact(Go *g){
    /* arrange so that adjacent spans have different targets */
    unsigned int i = 0, j;
    for(j = 1; j < g->nSpans; ++j){
	if(g->span[j].to != g->span[i].to){
	    ++i; g->span[i].to = g->span[j].to;
	}
	g->span[i].ub = g->span[j].ub;
    }
    g->nSpans = i + 1;
}

void Go_unmap(Go *g, Go *base, State *x){
    Span *s = g->span, *b = base->span, *e = &b[base->nSpans];
    unsigned int lb = 0;
    s->ub = 0;
    s->to = NULL;
    for(; b != e; ++b){
	if(b->to == x){
	    if((s->ub - lb) > 1)
		s->ub = b->ub;
	} else {
	    if(b->to != s->to){
		if(s->ub){
		    lb = s->ub; ++s;
		}
		s->to = b->to;
	    }
	    s->ub = b->ub;
	}
    }
    s->ub = e[-1].ub; ++s;
    g->nSpans = s - g->span;
}

static void doGen(Go *g, State *s, unsigned char *bm, unsigned char m){
    Span *b = g->span, *e = &b[g->nSpans];
    unsigned int lb = 0;
    for(; b < e; ++b){
	if(b->to == s)
	    for(; lb < b->ub; ++lb) bm[lb] |= m;
	lb = b->ub;
    }
}
#if 0
static void prt(FILE *o, Go *g, State *s){
    Span *b = g->span, *e = &b[g->nSpans];
    unsigned int lb = 0;
    for(; b < e; ++b){
	if(b->to == s)
	    printSpan(o, lb, b->ub);
	lb = b->ub;
    }
}
#endif
static int matches(Go *g1, State *s1, Go *g2, State *s2){
    Span *b1 = g1->span, *e1 = &b1[g1->nSpans];
    unsigned int lb1 = 0;
    Span *b2 = g2->span, *e2 = &b2[g2->nSpans];
    unsigned int lb2 = 0;
    for(;;){
	for(; b1 < e1 && b1->to != s1; ++b1) lb1 = b1->ub;
	for(; b2 < e2 && b2->to != s2; ++b2) lb2 = b2->ub;
	if(b1 == e1) return b2 == e2;
	if(b2 == e2) return 0;
	if(lb1 != lb2 || b1->ub != b2->ub) return 0;
	++b1; ++b2;
    }
}

typedef struct BitMap {
    Go			*go;
    State		*on;
    struct BitMap	*next;
    unsigned int	i;
    unsigned char	m;
} BitMap;

static BitMap *BitMap_find_go(Go*, State*);
static BitMap *BitMap_find(State*);
static void BitMap_gen(FILE *, unsigned int, unsigned int);
/* static void BitMap_stats(void);*/
static BitMap *BitMap_new(Go*, State*);

static BitMap *BitMap_first = NULL;

BitMap *
BitMap_new(Go *g, State *x)
{
    BitMap *b = malloc(sizeof(BitMap));
    b->go = g;
    b->on = x;
    b->next = BitMap_first;
    BitMap_first = b;
    return b;
}

BitMap *
BitMap_find_go(Go *g, State *x){
    BitMap *b;
    for(b = BitMap_first; b; b = b->next){
	if(matches(b->go, b->on, g, x))
	    return b;
    }
    return BitMap_new(g, x);
}

BitMap *
BitMap_find(State *x){
    BitMap *b;
    for(b = BitMap_first; b; b = b->next){
	if(b->on == x)
	    return b;
    }
    return NULL;
}

void BitMap_gen(FILE *o, unsigned int lb, unsigned int ub){
    BitMap *b = BitMap_first;
    if(b){
	unsigned int n = ub - lb;
	unsigned int i;
	unsigned char *bm = malloc(sizeof(unsigned char)*n);
	fputs("\tstatic unsigned char yybm[] = {", o);
	for(i = 0; b; i += n){
	    unsigned char m;
	    unsigned int j;
	    memset(bm, 0, n);
	    for(m = 0x80; b && m; b = b->next, m >>= 1){
		b->i = i; b->m = m;
		doGen(b->go, b->on, bm-lb, m);
	    }
	    for(j = 0; j < n; ++j){
		if(j%8 == 0) {fputs("\n\t", o); oline++;}
		fprintf(o, "%3u, ", (unsigned int) bm[j]);
	    }
	}
	fputs("\n\t};\n", o); oline+=2;
        free(bm);
    }
}

#if 0
void BitMap_stats(void){
    unsigned int n = 0;
    BitMap *b;
    for(b = BitMap_first; b; b = b->next){
	prt(stderr, b->go, b->on); fputs("\n", stderr);
	++n;
    }
    fprintf(stderr, "%u bitmaps\n", n);
    BitMap_first = NULL;
}
#endif

static void genGoTo(FILE *o, State *from, State *to, int *readCh,
		    const char *indent)
{
#if 0
    if (*readCh && from->label + 1 != to->label)
    {
	fputs("%syych = *YYCURSOR;\n", indent, o); oline++;
	*readCh = 0;
    }
#endif
    fprintf(o, "%sgoto yy%u;\n", indent, to->label); oline++;
    useLabel(to->label);
}

static void genIf(FILE *o, const char *cmp, unsigned int v, int *readCh)
{
#if 0
    if (*readCh)
    {
	fputs("\tif((yych = *YYCURSOR) ", o);
	*readCh = 0;
    } else {
#endif
	fputs("\tif(yych ", o);
#if 0
    }
#endif
    fprintf(o, "%s '", cmp);
    prtCh(o, v);
    fputs("')", o);
}

static void indent(FILE *o, unsigned int i){
    while(i-- > 0)
	fputc('\t', o);
}

static void need(FILE *o, unsigned int n, int *readCh)
{
    unsigned int fillIndex;
    int hasFillIndex = (0<=vFillIndexes);
    if (hasFillIndex) {
	fillIndex = vFillIndexes++;
	fprintf(o, "\tYYSETSTATE(%u);\n", fillIndex);
	++oline;
    }

    if(n == 1) {
	fputs("\tif(YYLIMIT == YYCURSOR) YYFILL(1);\n", o); oline++;
    } else {
	fprintf(o, "\tif((YYLIMIT - YYCURSOR) < %u) YYFILL(%u);\n", n, n);
	oline++;
    }

    if (hasFillIndex) {
	fprintf(o, "yyFillLabel%u:\n", fillIndex);
	++oline;
    }

    fputs("\tyych = *YYCURSOR;\n", o); oline++;
    *readCh = 0;
}

void
Action_emit(Action *a, FILE *o, int *readCh)
{
    int first = 1;
    unsigned int i;
    unsigned int back;

    switch (a->type) {
	case MATCHACT:
	    if(a->state->link){
		fputs("\t++YYCURSOR;\n", o);
		need(o, a->state->depth, readCh);
#if 0
	    } else if (!Action_readAhead(a)) {
		/* do not read next char if match */
		fputs("\t++YYCURSOR;\n", o);
		*readCh = 1;
#endif
	    } else {
		fputs("\tyych = *++YYCURSOR;\n", o);
		*readCh = 0;
	    }
	    oline++;
	    break;
	case ENTERACT:
	    if(a->state->link){
		fputs("\t++YYCURSOR;\n", o);
		fprintf(o, "yy%u:\n", a->d.label); oline+=2;
		need(o, a->state->depth, readCh);
	    } else {
		/* we shouldn't need 'rule-following' protection here */
		fputs("\tyych = *++YYCURSOR;\n", o);
		fprintf(o, "yy%u:\n", a->d.label); oline+=2;
		*readCh = 0;
	    }
	    break;
	case SAVEMATCHACT:
	    if (bUsedYYAccept) {
		fprintf(o, "\tyyaccept = %u;\n", a->d.selector);
		oline++;
	    }
	    if(a->state->link){
		fputs("\tYYMARKER = ++YYCURSOR;\n", o); oline++;
		need(o, a->state->depth, readCh);
	    } else {
		fputs("\tyych = *(YYMARKER = ++YYCURSOR);\n", o); oline++;
		*readCh = 0;
	    }
	    break;
	case MOVEACT:
	    break;
	case ACCEPTACT:
	    for(i = 0; i < a->d.Accept.nRules; ++i)
		if(a->d.Accept.saves[i] != ~0u){
		    if(first){
			first = 0;
			bUsedYYAccept = 1;
			fputs("\tYYCURSOR = YYMARKER;\n", o);
			fputs("\tswitch(yyaccept){\n", o); oline+=2;
		    }
		    fprintf(o, "\tcase %u:", a->d.Accept.saves[i]);
		    genGoTo(o, a->state, a->d.Accept.rules[i], readCh, "\t");
		}
	    if(!first) {
		fputs("\t}\n", o); oline++;
	    }
	    break;
	case RULEACT:
	    back = RegExp_fixedLength(a->d.rule->d.RuleOp.ctx);
	    if(back != ~0u && back > 0u)
		fprintf(o, "\tYYCURSOR -= %u;", back);
	    fprintf(o, "\n"); oline++;
	    line_source(o, a->d.rule->d.RuleOp.code->line);
	    SubStr_out(&a->d.rule->d.RuleOp.code->text, o);
	    fprintf(o, "\n"); oline++;
	    if (!iFlag)
		fprintf(o, "#line %u \"%s\"\n", oline++, outputFileName);
	    break;
    }
}

Action *
Action_new_Accept(State *x, unsigned int n, unsigned int *s, State **r)
{
    Action *a = malloc(sizeof(Action));
    a->type = ACCEPTACT;
    a->state = x;
    a->d.Accept.nRules = n;
    a->d.Accept.saves = s;
    a->d.Accept.rules = r;
    x->action = a;
    return a;
}

static void doLinear(FILE *o, unsigned int i, Span *s, unsigned int n,
		     State *from, State *next, int *readCh){
    for(;;){
	State *bg = s[0].to;
	while(n >= 3 && s[2].to == bg && (s[1].ub - s[0].ub) == 1){
	    if(s[1].to == next && n == 3){
		indent(o, i);
		genIf(o, "!=", s[0].ub, readCh);
		genGoTo(o, from, bg, readCh, "\t");
		indent(o, i);
		genGoTo(o, from, next, readCh, "\t");
		return;
	    } else {
		indent(o, i);
		genIf(o, "==", s[0].ub, readCh);
		genGoTo(o, from, s[1].to, readCh, "\t");
	    }
	    n -= 2; s += 2;
	}
	if(n == 1){
	    indent(o, i);
	    genGoTo(o, from, s[0].to, readCh, "\t");
	    return;
	} else if(n == 2 && bg == next){
	    indent(o, i);
	    genIf(o, ">=", s[0].ub, readCh);
	    genGoTo(o, from, s[1].to, readCh, "\t");
	    indent(o, i);
	    genGoTo(o, from, next, readCh, "\t");
	    return;
	} else {
	    indent(o, i);
	    genIf(o, "<=", s[0].ub - 1, readCh);
	    genGoTo(o, from, bg, readCh, "\t");
	    n -= 1; s += 1;
	}
    }
    indent(o, i);
    genGoTo(o, from, next, readCh, "\t");
}

void
Go_genLinear(Go *g, FILE *o, State *from, State *next, int *readCh){
    doLinear(o, 0, g->span, g->nSpans, from, next, readCh);
}

static void genCases(FILE *o, unsigned int lb, Span *s){
    if(lb < s->ub){
	for(;;){
	    fputs("\tcase '", o); prtCh(o, lb); fputs("':", o);
	    if(++lb == s->ub)
		break;
	    fputs("\n", o); oline++;
	}
    }
}

void
Go_genSwitch(Go *g, FILE *o, State *from, State *next, int *readCh){
    if(g->nSpans <= 2){
	Go_genLinear(g, o, from, next, readCh);
    } else {
	State *def = g->span[g->nSpans-1].to;
	Span **sP = malloc(sizeof(Span*)*(g->nSpans-1)), **r, **s, **t;
	unsigned int i;

	t = &sP[0];
	for(i = 0; i < g->nSpans; ++i)
	    if(g->span[i].to != def)
		*(t++) = &g->span[i];

	    if (dFlag)
		fputs("\tYYDEBUG(-1, yych);\n", o);

#if 0
	if (*readCh) {
	    fputs("\tswitch((yych = *YYCURSOR)) {\n", o);
	    *readCh = 0;
	} else
#endif
	    fputs("\tswitch(yych){\n", o);
	oline++;
	while(t != &sP[0]){
	    State *to;
	    r = s = &sP[0];
	    if(*s == &g->span[0])
		genCases(o, 0, *s);
	    else
		genCases(o, (*s)[-1].ub, *s);
	    to = (*s)->to;
	    while(++s < t){
		if((*s)->to == to)
		    genCases(o, (*s)[-1].ub, *s);
		else
		    *(r++) = *s;
	    }
	    genGoTo(o, from, to, readCh, "\t");
	    t = r;
	}
	fputs("\tdefault:", o);
	genGoTo(o, from, def, readCh, "\t");
	fputs("\t}\n", o); oline++;

	free(sP);
    }
}

static void doBinary(FILE *o, unsigned int i, Span *s, unsigned int n,
		     State *from, State *next, int *readCh){
    if(n <= 4){
	doLinear(o, i, s, n, from, next, readCh);
    } else {
	unsigned int h = n/2;
	indent(o, i);
	genIf(o, "<=", s[h-1].ub - 1, readCh);
	fputs("{\n", o); oline++;
	doBinary(o, i+1, &s[0], h, from, next, readCh);
	indent(o, i); fputs("\t} else {\n", o); oline++;
	doBinary(o, i+1, &s[h], n - h, from, next, readCh);
	indent(o, i); fputs("\t}\n", o); oline++;
    }
}

void
Go_genBinary(Go *g, FILE *o, State *from, State *next, int *readCh){
    doBinary(o, 0, g->span, g->nSpans, from, next, readCh);
}

void
Go_genBase(Go *g, FILE *o, State *from, State *next, int *readCh){
    if(g->nSpans == 0)
	return;
    if(!sFlag){
	Go_genSwitch(g, o, from, next, readCh);
	return;
    }
    if(g->nSpans > 8){
	Span *bot = &g->span[0], *top = &g->span[g->nSpans-1];
	unsigned int util;
	if(bot[0].to == top[0].to){
	    util = (top[-1].ub - bot[0].ub)/(g->nSpans - 2);
	} else {
	    if(bot[0].ub > (top[0].ub - top[-1].ub)){
		util = (top[0].ub - bot[0].ub)/(g->nSpans - 1);
	    } else {
		util = top[-1].ub/(g->nSpans - 1);
	    }
	}
	if(util <= 2){
	    Go_genSwitch(g, o, from, next, readCh);
	    return;
	}
    }
    if(g->nSpans > 5){
	Go_genBinary(g, o, from, next, readCh);
    } else {
	Go_genLinear(g, o, from, next, readCh);
    }
}

void
Go_genGoto(Go *g, FILE *o, State *from, State *next, int *readCh){
    unsigned int i;
    if(bFlag){
	for(i = 0; i < g->nSpans; ++i){
	    State *to = g->span[i].to;
	    if(to && to->isBase){
		BitMap *b = BitMap_find(to);
		if(b && matches(b->go, b->on, g, to)){
		    Go go;
		    go.span = malloc(sizeof(Span)*g->nSpans);
		    Go_unmap(&go, g, to);
		    fprintf(o, "\tif(yybm[%u+", b->i);
#if 0
		    if (*readCh)
			fputs("(yych = *YYCURSOR)", o);
		    else
#endif
			fputs("yych", o);
		    fprintf(o, "] & %u) {\n", (unsigned int) b->m); oline++;
		    genGoTo(o, from, to, readCh, "\t\t");
		    fputs("\t}\n", o); oline++;
		    Go_genBase(&go, o, from, next, readCh);
		    free(go.span);
		    return;
		}
	    }
	}
    }
    Go_genBase(g, o, from, next, readCh);
}

void State_emit(State *s, FILE *o, int *readCh){
    if (vUsedLabels[s->label])
	fprintf(o, "yy%u:", s->label);
    if (dFlag)
	fprintf(o, "\n\tYYDEBUG(%u, *YYCURSOR);\n", s->label);
    Action_emit(s->action, o, readCh);
}

static unsigned int merge(Span *x0, State *fg, State *bg){
    Span *x = x0, *f = fg->go.span, *b = bg->go.span;
    unsigned int nf = fg->go.nSpans, nb = bg->go.nSpans;
    State *prev = NULL, *to;
    /* NB: we assume both spans are for same range */
    for(;;){
	if(f->ub == b->ub){
	    to = f->to == b->to? bg : f->to;
	    if(to == prev){
		--x;
	    } else {
		x->to = prev = to;
	    }
	    x->ub = f->ub;
	    ++x; ++f; --nf; ++b; --nb;
	    if(nf == 0 && nb == 0)
		return x - x0;
	}
	while(f->ub < b->ub){
	    to = f->to == b->to? bg : f->to;
	    if(to == prev){
		--x;
	    } else {
		x->to = prev = to;
	    }
	    x->ub = f->ub;
	    ++x; ++f; --nf;
	}
	while(b->ub < f->ub){
	    to = b->to == f->to? bg : f->to;
	    if(to == prev){
		--x;
	    } else {
		x->to = prev = to;
	    }
	    x->ub = b->ub;
	    ++x; ++b; --nb;
	}
    }
}

const unsigned int cInfinity = ~0;

typedef struct SCC {
    State	**top, **stk;
} SCC;

static void SCC_init(SCC*, unsigned int);
static SCC *SCC_new(unsigned int);
static void SCC_destroy(SCC*);
static void SCC_delete(SCC*);
static void SCC_traverse(SCC*, State*);

static void
SCC_init(SCC *s, unsigned int size)
{
    s->top = s->stk = malloc(sizeof(State*)*size);
}

static SCC *
SCC_new(unsigned int size){
    SCC *s = malloc(sizeof(SCC));
    s->top = s->stk = malloc(sizeof(State*)*size);
    return s;
}

static void
SCC_destroy(SCC *s){
    free(s->stk);
}

static void
SCC_delete(SCC *s){
    free(s->stk);
    free(s);
}

static void SCC_traverse(SCC *s, State *x){
    unsigned int k, i;

    *s->top = x;
    k = ++s->top - s->stk;
    x->depth = k;
    for(i = 0; i < x->go.nSpans; ++i){
	State *y = x->go.span[i].to;
	if(y){
	    if(y->depth == 0)
		SCC_traverse(s, y);
	    if(y->depth < x->depth)
		x->depth = y->depth;
	}
    }
    if(x->depth == k)
	do {
	    (*--s->top)->depth = cInfinity;
	    (*s->top)->link = x;
	} while(*s->top != x);
}

static unsigned int maxDist(State *s){
    unsigned int mm = 0, i;
    for(i = 0; i < s->go.nSpans; ++i){
	State *t = s->go.span[i].to;
	if(t){
	    unsigned int m = 1;
	    if(!t->link) {
		if (t->depth == -1)
		    t->depth = maxDist(t);
		m += t->depth;
	    }
	    if(m > mm)
		mm = m;
	}
    }
    return mm;
}

static void calcDepth(State *head){
    State *t, *s;
    for(s = head; s; s = s->next){
	if(s->link == s){
	    unsigned int i;
	    for(i = 0; i < s->go.nSpans; ++i){
		t = s->go.span[i].to;
		if(t && t->link == s)
		    goto inSCC;
	    }
	    s->link = NULL;
	} else {
	inSCC:
	    s->depth = maxDist(s);
	}
    }
}
 
void DFA_findSCCs(DFA *d){
    SCC scc;
    State *s;

    SCC_init(&scc, d->nStates);
    for(s = d->head; s; s = s->next){
	s->depth = 0;
	s->link = NULL;
    }

    for(s = d->head; s; s = s->next)
	if(!s->depth)
	    SCC_traverse(&scc, s);

    calcDepth(d->head);

    SCC_destroy(&scc);
}

void DFA_split(DFA *d, State *s){
    State *move = State_new();
    Action_new_Move(move);
    DFA_addState(d, &s->next, move);
    move->link = s->link;
    move->rule = s->rule;
    move->go = s->go;
    s->rule = NULL;
    s->go.nSpans = 1;
    s->go.span = malloc(sizeof(Span));
    s->go.span[0].ub = d->ubChar;
    s->go.span[0].to = move;
}

void DFA_emit(DFA *d, FILE *o){
    static unsigned int label = 0;
    State *s;
    unsigned int i, bitmap_brace = 0;
    unsigned int nRules = 0;
    unsigned int nSaves = 0;
    unsigned int *saves;
    unsigned int nOrgOline;
    State **rules;
    State *accept = NULL;
    Span *span;
    FILE *tmpo;
    int hasFillLabels;
    int maxFillIndexes, orgVFillIndexes;
    unsigned int start_label;

    hasFillLabels = (0<=vFillIndexes);
    if (hasFillLabels && label!=0) {
	fputs("re2c : error : multiple /*!re2c blocks aren't supported when -f is specified\n", stderr);
	exit(1);
    }

    DFA_findSCCs(d);
    d->head->link = d->head;

    maxFill = 1;
    for(s = d->head; s; s = s->next) {
	s->depth = maxDist(s);
	if (maxFill < s->depth)
	    maxFill = s->depth;
	if(s->rule && s->rule->d.RuleOp.accept >= nRules)
		nRules = s->rule->d.RuleOp.accept + 1;
    }

    saves = malloc(sizeof(unsigned int)*nRules);
    memset(saves, ~0, (nRules)*sizeof(unsigned int));

    /* mark backtracking points */
    for(s = d->head; s; s = s->next){
	RegExp *ignore = NULL;/*RuleOp*/
	if(s->rule){
	    for(i = 0; i < s->go.nSpans; ++i)
		if(s->go.span[i].to && !s->go.span[i].to->rule){
		    free(s->action);
		    if(saves[s->rule->d.RuleOp.accept] == ~0u)
			saves[s->rule->d.RuleOp.accept] = nSaves++;
		    Action_new_Save(s, saves[s->rule->d.RuleOp.accept]);
		    continue;
		}
	    ignore = s->rule;
	}
    }

    /* insert actions */
    rules = malloc(sizeof(State*)*nRules);
    memset(rules, 0, (nRules)*sizeof(State*));
    for(s = d->head; s; s = s->next){
	State *ow;
	if(!s->rule){
	    ow = accept;
	} else {
	    if(!rules[s->rule->d.RuleOp.accept]){
		State *n = State_new();
		Action_new_Rule(n, s->rule);
		rules[s->rule->d.RuleOp.accept] = n;
		DFA_addState(d, &s->next, n);
	    }
	    ow = rules[s->rule->d.RuleOp.accept];
	}
	for(i = 0; i < s->go.nSpans; ++i)
	    if(!s->go.span[i].to){
		if(!ow){
		    ow = accept = State_new();
		    Action_new_Accept(accept, nRules, saves, rules);
		    DFA_addState(d, &s->next, accept);
		}
		s->go.span[i].to = ow;
	    }
    }

    /* split ``base'' states into two parts */
    for(s = d->head; s; s = s->next){
	s->isBase = 0;
	if(s->link){
	    for(i = 0; i < s->go.nSpans; ++i){
		if(s->go.span[i].to == s){
		    s->isBase = 1;
		    DFA_split(d, s);
		    if(bFlag)
			BitMap_find_go(&s->next->go, s);
		    s = s->next;
		    break;
		}
	    }
	}
    }

    /* find ``base'' state, if possible */
    span = malloc(sizeof(Span)*(d->ubChar - d->lbChar));
    for(s = d->head; s; s = s->next){
	if(!s->link){
	    for(i = 0; i < s->go.nSpans; ++i){
		State *to = s->go.span[i].to;
		if(to && to->isBase){
		    unsigned int nSpans;
		    to = to->go.span[0].to;
		    nSpans = merge(span, s, to);
		    if(nSpans < s->go.nSpans){
			free(s->go.span);
			s->go.nSpans = nSpans;
			s->go.span = malloc(sizeof(Span)*nSpans);
			memcpy(s->go.span, span, nSpans*sizeof(Span));
		    }
		    break;
		}
	    }
	}
    }
    free(span);

    free(d->head->action);

    if(bFlag) {
	fputs("{\n", o);
	oline++;
	bitmap_brace = 1;
	BitMap_gen(o, d->lbChar, d->ubChar);
    }

    bUsedYYAccept = 0;

    start_label = label;

    Action_new_Enter(d->head, label++);

    for(s = d->head; s; s = s->next)
	s->label = label++;

    nOrgOline = oline;
    maxFillIndexes = vFillIndexes;
    orgVFillIndexes = vFillIndexes;
#ifdef _WIN32
    tmpo = win32_tmpfile();
#else
    tmpo = tmpfile();
#endif
    for(s = d->head; s; s = s->next){
	int readCh = 0;
	State_emit(s, tmpo, &readCh);
	Go_genGoto(&s->go, tmpo, s, s->next, &readCh);
    }
    fclose(tmpo);
    maxFillIndexes = vFillIndexes;
    vFillIndexes = orgVFillIndexes;
    oline = nOrgOline;

    fputs("\n", o);
    oline++;
    if (!iFlag)
	fprintf(o, "#line %u \"%s\"\n", oline++, outputFileName);

    if (!hasFillLabels) {
	fputs("{\n\tYYCTYPE yych;\n", o);
	oline += 2;
	if (bUsedYYAccept) {
	    fputs("\tunsigned int yyaccept;\n", o);
	    oline++;
	}
    } else {
	fputs("{\n\n", o);
	oline += 2;
    }

    if (!hasFillLabels) {
	fprintf(o, "\tgoto yy%u;\n", start_label);
	oline++;
	useLabel(label);
    } else {
	int i;
	fputs("\tswitch(YYGETSTATE()) {\n", o);
	fputs("\t\tcase -1: goto yy0;\n", o);

	for (i=0; i<maxFillIndexes; ++i)
	    fprintf(o, "\t\tcase %u: goto yyFillLabel%u;\n", i, i);

	fputs("\t\tdefault: /* abort() */;\n", o);
	fputs("\t}\n", o);
	fputs("yyNext:\n", o);

	oline += maxFillIndexes;
	oline += 5;
    }

    for(s = d->head; s; s = s->next){
	int readCh = 0;
	State_emit(s, o, &readCh);
	Go_genGoto(&s->go, o, s, s->next, &readCh);
    }
    fputs("}\n", o); oline++;
    if (bitmap_brace) {
	fputs("}\n", o);
	oline++;
    }

    BitMap_first = NULL;

    free(saves);
    free(rules);
}