summaryrefslogtreecommitdiff
path: root/gcc/genautomata.c
blob: e6295e607578770b565ba682813c66d4878ce2c7 (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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
/* Pipeline hazard description translator.
   Copyright (C) 2000-2021 Free Software Foundation, Inc.

   Written by Vladimir Makarov <vmakarov@redhat.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* References:

   1. The finite state automaton based pipeline hazard recognizer and
      instruction scheduler in GCC.  V. Makarov.  Proceedings of GCC
      summit, 2003.

   2. Detecting pipeline structural hazards quickly. T. Proebsting,
      C. Fraser. Proceedings of ACM SIGPLAN-SIGACT Symposium on
      Principles of Programming Languages, pages 280--286, 1994.

      This article is a good start point to understand usage of finite
      state automata for pipeline hazard recognizers.  But I'd
      recommend the 1st and 3rd article for more deep understanding.

   3. Efficient Instruction Scheduling Using Finite State Automata:
      V. Bala and N. Rubin, Proceedings of MICRO-28.  This is the best
      article about usage of finite state automata for pipeline hazard
      recognizers.

   The current implementation is described in the 1st article and it
   is different from the 3rd article in the following:

   1. New operator `|' (alternative) is permitted in functional unit
      reservation which can be treated deterministically and
      non-deterministically.

   2. Possibility of usage of nondeterministic automata too.

   3. Possibility to query functional unit reservations for given
      automaton state.

   4. Several constructions to describe impossible reservations
      (`exclusion_set', `presence_set', `final_presence_set',
      `absence_set', and `final_absence_set').

   5. No reverse automata are generated.  Trace instruction scheduling
      requires this.  It can be easily added in the future if we
      really need this.

   6. Union of automaton states are not generated yet.  It is planned
      to be implemented.  Such feature is needed to make more accurate
      interlock insn scheduling to get state describing functional
      unit reservation in a joint CFG point.  */

/* This file code processes constructions of machine description file
   which describes automaton used for recognition of processor pipeline
   hazards by insn scheduler and can be used for other tasks (such as
   VLIW insn packing.

   The translator functions `gen_cpu_unit', `gen_query_cpu_unit',
   `gen_bypass', `gen_excl_set', `gen_presence_set',
   `gen_final_presence_set', `gen_absence_set',
   `gen_final_absence_set', `gen_automaton', `gen_automata_option',
   `gen_reserv', `gen_insn_reserv' are called from file
   `genattrtab.c'.  They transform RTL constructions describing
   automata in .md file into internal representation convenient for
   further processing.

   The translator major function `expand_automata' processes the
   description internal representation into finite state automaton.
   It can be divided on:

     o checking correctness of the automaton pipeline description
       (major function is `check_all_description').

     o generating automaton (automata) from the description (major
       function is `make_automaton').

     o optional transformation of nondeterministic finite state
       automata into deterministic ones if the alternative operator
       `|' is treated nondeterministically in the description (major
       function is NDFA_to_DFA).

     o optional minimization of the finite state automata by merging
       equivalent automaton states (major function is `minimize_DFA').

     o forming tables (some as comb vectors) and attributes
       representing the automata (functions output_..._table).

   Function `write_automata' outputs the created finite state
   automaton as different tables and functions which works with the
   automata to inquire automaton state and to change its state.  These
   function are used by gcc instruction scheduler and may be some
   other gcc code.  */

#include "bconfig.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "obstack.h"
#include "errors.h"
#include "gensupport.h"

#include <math.h>
#include "fnmatch.h"

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

/* Positions in machine description file.  Now they are not used.  But
   they could be used in the future for better diagnostic messages.  */
typedef int pos_t;

/* The following is element of vector of current (and planned in the
   future) functional unit reservations.  */
typedef unsigned HOST_WIDE_INT set_el_t;

/* Reservations of function units are represented by value of the following
   type.  */
typedef set_el_t *reserv_sets_t;
typedef const set_el_t *const_reserv_sets_t;

/* The following structure describes a ticker.  */
struct ticker
{
  /* The following member value is time of the ticker creation with
     taking into account time when the ticker is off.  Active time of
     the ticker is current time minus the value.  */
  int modified_creation_time;
  /* The following member value is time (incremented by one) when the
     ticker was off.  Zero value means that now the ticker is on.  */
  int incremented_off_time;
};

/* The ticker is represented by the following type.  */
typedef struct ticker ticker_t;

/* The following type describes elements of output vectors.  */
typedef HOST_WIDE_INT vect_el_t;

/* Forward declaration of structures of internal representation of
   pipeline description based on NDFA.  */

struct unit_decl;
struct bypass_decl;
struct result_decl;
struct automaton_decl;
struct unit_pattern_rel_decl;
struct reserv_decl;
struct insn_reserv_decl;
struct decl;
struct unit_regexp;
struct result_regexp;
struct reserv_regexp;
struct nothing_regexp;
struct sequence_regexp;
struct repeat_regexp;
struct allof_regexp;
struct oneof_regexp;
struct regexp;
struct description;
struct unit_set_el;
struct pattern_set_el;
struct pattern_reserv;
struct state;
struct alt_state;
struct arc;
struct ainsn;
struct automaton;
struct state_ainsn_table;

/* The following typedefs are for brevity.  */
typedef struct unit_decl *unit_decl_t;
typedef const struct unit_decl *const_unit_decl_t;
typedef struct decl *decl_t;
typedef const struct decl *const_decl_t;
typedef struct regexp *regexp_t;
typedef struct unit_set_el *unit_set_el_t;
typedef struct pattern_set_el *pattern_set_el_t;
typedef struct pattern_reserv *pattern_reserv_t;
typedef struct alt_state *alt_state_t;
typedef struct state *state_t;
typedef const struct state *const_state_t;
typedef struct arc *arc_t;
typedef struct ainsn *ainsn_t;
typedef struct automaton *automaton_t;
typedef struct automata_list_el *automata_list_el_t;
typedef const struct automata_list_el *const_automata_list_el_t;
typedef struct state_ainsn_table *state_ainsn_table_t;

/* Undefined position.  */
static pos_t no_pos = 0;

/* All IR is stored in the following obstack.  */
static struct obstack irp;


/* Declare vector types for various data structures: */


typedef vec<vect_el_t> vla_hwint_t;

/* Forward declarations of functions used before their definitions, only.  */
static regexp_t gen_regexp_sequence    (const char *);
static void reserv_sets_or             (reserv_sets_t, reserv_sets_t,
				        reserv_sets_t);
static reserv_sets_t get_excl_set      (reserv_sets_t);
static int check_presence_pattern_sets (reserv_sets_t,
					reserv_sets_t, int);
static int check_absence_pattern_sets  (reserv_sets_t, reserv_sets_t,
					int);
static arc_t first_out_arc             (const_state_t);
static arc_t next_out_arc              (arc_t);



/* Options with the following names can be set up in automata_option
   construction.  Because the strings occur more one time we use the
   macros.  */

#define NO_MINIMIZATION_OPTION "-no-minimization"
#define TIME_OPTION "-time"
#define STATS_OPTION "-stats"
#define V_OPTION "-v"
#define W_OPTION "-w"
#define NDFA_OPTION "-ndfa"
#define COLLAPSE_OPTION "-collapse-ndfa"
#define NO_COMB_OPTION "-no-comb-vect"
#define PROGRESS_OPTION "-progress"

/* The following flags are set up by function `initiate_automaton_gen'.  */

/* Make automata with nondeterministic reservation by insns (`-ndfa').  */
static int ndfa_flag;

/* When making an NDFA, produce additional transitions that collapse
   NDFA state into a deterministic one suitable for querying CPU units.
   Provide advance-state transitions only for deterministic states.  */
static int collapse_flag;

/* Do not make minimization of DFA (`-no-minimization').  */
static int no_minimization_flag;

/* Do not try to generate a comb vector (`-no-comb-vect').  */
static int no_comb_flag;

/* Value of this variable is number of automata being generated.  The
   actual number of automata may be less this value if there is not
   sufficient number of units.  This value is defined by argument of
   option `-split' or by constructions automaton if the value is zero
   (it is default value of the argument).  */
static int split_argument;

/* Flag of output time statistics (`-time').  */
static int time_flag;

/* Flag of automata statistics (`-stats').  */
static int stats_flag;

/* Flag of creation of description file which contains description of
   result automaton and statistics information (`-v').  */
static int v_flag;

/* Flag of output of a progress bar showing how many states were
   generated so far for automaton being processed (`-progress').  */
static int progress_flag;

/* Flag of generating warning instead of error for non-critical errors
   (`-w').  */
static int w_flag;


/* Output file for pipeline hazard recognizer (PHR) being generated.
   The value is NULL if the file is not defined.  */
static FILE *output_file;

/* Description file of PHR.  The value is NULL if the file is not
   created.  */
static FILE *output_description_file;

/* PHR description file name.  */
static char *output_description_file_name;

/* Value of the following variable is node representing description
   being processed.  This is start point of IR.  */
static struct description *description;



/* This page contains description of IR structure (nodes).  */

enum decl_mode
{
  dm_unit,
  dm_bypass,
  dm_automaton,
  dm_excl,
  dm_presence,
  dm_absence,
  dm_reserv,
  dm_insn_reserv
};

/* This describes define_cpu_unit and define_query_cpu_unit (see file
   rtl.def).  */
struct unit_decl
{
  const char *name;
  /* NULL if the automaton name is absent.  */
  const char *automaton_name;
  /* If the following value is not zero, the cpu unit reservation is
     described in define_query_cpu_unit.  */
  char query_p;

  /* The following fields are defined by checker.  */

  /* The following field value is nonzero if the unit is used in an
     regexp.  */
  char unit_is_used;

  /* The following field value is order number (0, 1, ...) of given
     unit.  */
  int unit_num;
  /* The following field value is corresponding declaration of
     automaton which was given in description.  If the field value is
     NULL then automaton in the unit declaration was absent.  */
  struct automaton_decl *automaton_decl;
  /* The following field value is maximal cycle number (1, ...) on
     which given unit occurs in insns.  Zero value means that given
     unit is not used in insns.  */
  int max_occ_cycle_num;
  /* The following field value is minimal cycle number (0, ...) on
     which given unit occurs in insns.  -1 value means that given
     unit is not used in insns.  */
  int min_occ_cycle_num;
  /* The following list contains units which conflict with given
     unit.  */
  unit_set_el_t excl_list;
  /* The following list contains patterns which are required to
     reservation of given unit.  */
  pattern_set_el_t presence_list;
  pattern_set_el_t final_presence_list;
  /* The following list contains patterns which should be not present
     in reservation for given unit.  */
  pattern_set_el_t absence_list;
  pattern_set_el_t final_absence_list;
  /* The following is used only when `query_p' has nonzero value.
     This is query number for the unit.  */
  int query_num;
  /* The following is the last cycle on which the unit was checked for
     correct distributions of units to automata in a regexp.  */
  int last_distribution_check_cycle;

  /* The following fields are defined by automaton generator.  */

  /* The following field value is number of the automaton to which
     given unit belongs.  */
  int corresponding_automaton_num;
  /* If the following value is not zero, the cpu unit is present in a
     `exclusion_set' or in right part of a `presence_set',
     `final_presence_set', `absence_set', and
     `final_absence_set'define_query_cpu_unit.  */
  char in_set_p;
};

/* This describes define_bypass (see file rtl.def).  */
struct bypass_decl
{
  int latency;
  const char *out_pattern;
  const char *in_pattern;
  const char *bypass_guard_name;

  /* The following fields are defined by checker.  */

  /* output and input insns of given bypass.  */
  struct insn_reserv_decl *out_insn_reserv;
  struct insn_reserv_decl *in_insn_reserv;
  /* The next bypass for given output insn.  */
  struct bypass_decl *next;
};

/* This describes define_automaton (see file rtl.def).  */
struct automaton_decl
{
  const char *name;

  /* The following fields are defined by automaton generator.  */

  /* The following field value is nonzero if the automaton is used in
     an regexp definition.  */
  char automaton_is_used;

  /* The following fields are defined by checker.  */

  /* The following field value is the corresponding automaton.  This
     field is not NULL only if the automaton is present in unit
     declarations and the automatic partition on automata is not
     used.  */
  automaton_t corresponding_automaton;
};

/* This describes exclusion relations: exclusion_set (see file
   rtl.def).  */
struct excl_rel_decl
{
  int all_names_num;
  int first_list_length;
  char *names [1];
};

/* This describes unit relations: [final_]presence_set or
   [final_]absence_set (see file rtl.def).  */
struct unit_pattern_rel_decl
{
  int final_p;
  int names_num;
  int patterns_num;
  char **names;
  char ***patterns;
};

/* This describes define_reservation (see file rtl.def).  */
struct reserv_decl
{
  const char *name;
  regexp_t regexp;

  /* The following fields are defined by checker.  */

  /* The following field value is nonzero if the unit is used in an
     regexp.  */
  char reserv_is_used;
  /* The following field is used to check up cycle in expression
     definition.  */
  int loop_pass_num;
};

/* This describes define_insn_reservation (see file rtl.def).  */
struct insn_reserv_decl
{
  rtx condexp;
  int default_latency;
  regexp_t regexp;
  const char *name;

  /* The following fields are defined by checker.  */

  /* The following field value is order number (0, 1, ...) of given
     insn.  */
  int insn_num;
  /* The following field value is list of bypasses in which given insn
     is output insn.  Bypasses with the same input insn stay one after
     another in the list in the same order as their occurrences in the
     description but the bypass without a guard stays always the last
     in a row of bypasses with the same input insn.  */
  struct bypass_decl *bypass_list;

  /* The following fields are defined by automaton generator.  */

  /* The following field is the insn regexp transformed that
     the regexp has not optional regexp, repetition regexp, and an
     reservation name (i.e. reservation identifiers are changed by the
     corresponding regexp) and all alternations are the top level
     of the regexp.  The value can be NULL only if it is special
     insn `cycle advancing'.  */
  regexp_t transformed_regexp;
  /* The following field value is list of arcs marked given
     insn.  The field is used in transformation NDFA -> DFA.  */
  arc_t arcs_marked_by_insn;
  /* The two following fields are used during minimization of a finite state
     automaton.  */
  /* The field value is number of equivalence class of state into
     which arc marked by given insn enters from a state (fixed during
     an automaton minimization).  */
  int equiv_class_num;
  /* The following member value is the list to automata which can be
     changed by the insn issue.  */
  automata_list_el_t important_automata_list;
  /* The following member is used to process insn once for output.  */
  int processed_p;
};

/* This contains a declaration mentioned above.  */
struct decl
{
  /* What node in the union? */
  enum decl_mode mode;
  pos_t pos;
  union
  {
    struct unit_decl unit;
    struct bypass_decl bypass;
    struct automaton_decl automaton;
    struct excl_rel_decl excl;
    struct unit_pattern_rel_decl presence;
    struct unit_pattern_rel_decl absence;
    struct reserv_decl reserv;
    struct insn_reserv_decl insn_reserv;
  } decl;
};

/* The following structures represent parsed reservation strings.  */
enum regexp_mode
{
  rm_unit,
  rm_reserv,
  rm_nothing,
  rm_sequence,
  rm_repeat,
  rm_allof,
  rm_oneof
};

/* Cpu unit in reservation.  */
struct unit_regexp
{
  const char *name;
  unit_decl_t unit_decl;
};

/* Define_reservation in a reservation.  */
struct reserv_regexp
{
  const char *name;
  struct reserv_decl *reserv_decl;
};

/* Absence of reservation (represented by string `nothing').  */
struct nothing_regexp
{
  /* This used to be empty but ISO C doesn't allow that.  */
  char unused;
};

/* Representation of reservations separated by ',' (see file
   rtl.def).  */
struct sequence_regexp
{
  int regexps_num;
  regexp_t regexps [1];
};

/* Representation of construction `repeat' (see file rtl.def).  */
struct repeat_regexp
{
  int repeat_num;
  regexp_t regexp;
};

/* Representation of reservations separated by '+' (see file
   rtl.def).  */
struct allof_regexp
{
  int regexps_num;
  regexp_t regexps [1];
};

/* Representation of reservations separated by '|' (see file
   rtl.def).  */
struct oneof_regexp
{
  int regexps_num;
  regexp_t regexps [1];
};

/* Representation of a reservation string.  */
struct regexp
{
  /* What node in the union? */
  enum regexp_mode mode;
  pos_t pos;
  union
  {
    struct unit_regexp unit;
    struct reserv_regexp reserv;
    struct nothing_regexp nothing;
    struct sequence_regexp sequence;
    struct repeat_regexp repeat;
    struct allof_regexp allof;
    struct oneof_regexp oneof;
  } regexp;
};

/* Represents description of pipeline hazard description based on
   NDFA.  */
struct description
{
  int decls_num, normal_decls_num;

  /* The following fields are defined by checker.  */

  /* The following fields values are correspondingly number of all
     units, query units, and insns in the description.  */
  int units_num;
  int query_units_num;
  int insns_num;
  /* The following field value is max length (in cycles) of
     reservations of insns.  The field value is defined only for
     correct programs.  */
  int max_insn_reserv_cycles;

  /* The following fields are defined by automaton generator.  */

  /* The following field value is the first automaton.  */
  automaton_t first_automaton;

  /* The following field is created by pipeline hazard parser and
     contains all declarations.  We allocate additional entries for
     two special insns which are added by the automaton generator.  */
  decl_t decls [1];
};


/* The following nodes are created in automaton checker.  */

/* The following nodes represent exclusion set for cpu units.  Each
   element is accessed through only one excl_list.  */
struct unit_set_el
{
  unit_decl_t unit_decl;
  unit_set_el_t next_unit_set_el;
};

/* The following nodes represent presence or absence pattern for cpu
   units.  Each element is accessed through only one presence_list or
   absence_list.  */
struct pattern_set_el
{
  /* The number of units in unit_decls.  */
  int units_num;
  /* The units forming the pattern.  */
  struct unit_decl **unit_decls;
  pattern_set_el_t next_pattern_set_el;
};


/* The following nodes are created in automaton generator.  */


/* The following nodes represent presence or absence pattern for cpu
   units.  Each element is accessed through only one element of
   unit_presence_set_table or unit_absence_set_table.  */
struct pattern_reserv
{
  reserv_sets_t reserv;
  pattern_reserv_t next_pattern_reserv;
};

/* The following node type describes state automaton.  The state may
   be deterministic or non-deterministic.  Non-deterministic state has
   several component states which represent alternative cpu units
   reservations.  The state also is used for describing a
   deterministic reservation of automaton insn.  */
struct state
{
  /* The following member value is nonzero if there is a transition by
     cycle advancing.  */
  int new_cycle_p;
  /* The following field is list of processor unit reservations on
     each cycle.  */
  reserv_sets_t reservs;
  /* The following field is unique number of given state between other
     states.  */
  int unique_num;
  /* The following field value is automaton to which given state
     belongs.  */
  automaton_t automaton;
  /* The following field value is the first arc output from given
     state.  */
  arc_t first_out_arc;
  unsigned int num_out_arcs;
  /* The following field is used to form NDFA.  */
  char it_was_placed_in_stack_for_NDFA_forming;
  /* The following field is used to form DFA.  */
  char it_was_placed_in_stack_for_DFA_forming;
  /* The following field is used to transform NDFA to DFA and DFA
     minimization.  The field value is not NULL if the state is a
     compound state.  In this case the value of field `unit_sets_list'
     is NULL.  All states in the list are in the hash table.  The list
     is formed through field `next_sorted_alt_state'.  We should
     support only one level of nesting state.  */
  alt_state_t component_states;
  /* The following field is used for passing graph of states.  */
  int pass_num;
  /* The list of states belonging to one equivalence class is formed
     with the aid of the following field.  */
  state_t next_equiv_class_state;
  /* The two following fields are used during minimization of a finite
     state automaton.  */
  int equiv_class_num_1, equiv_class_num_2;
  /* The following field is used during minimization of a finite state
     automaton.  The field value is state corresponding to equivalence
     class to which given state belongs.  */
  state_t equiv_class_state;
  unsigned int *presence_signature;
  /* The following field value is the order number of given state.
     The states in final DFA is enumerated with the aid of the
     following field.  */
  int order_state_num;
  /* This member is used for passing states for searching minimal
     delay time.  */
  int state_pass_num;
  /* The following member is used to evaluate min issue delay of insn
     for a state.  */
  int min_insn_issue_delay;
};

/* Automaton arc.  */
struct arc
{
  /* The following field refers for the state into which given arc
     enters.  */
  state_t to_state;
  /* The following field describes that the insn issue (with cycle
     advancing for special insn `cycle advancing' and without cycle
     advancing for others) makes transition from given state to
     another given state.  */
  ainsn_t insn;
  /* The following field value is the next arc output from the same
     state.  */
  arc_t next_out_arc;
  /* List of arcs marked given insn is formed with the following
     field.  The field is used in transformation NDFA -> DFA.  */
  arc_t next_arc_marked_by_insn;
};

/* The following node type describes a deterministic alternative in
   non-deterministic state which characterizes cpu unit reservations
   of automaton insn or which is part of NDFA.  */
struct alt_state
{
  /* The following field is a deterministic state which characterizes
     unit reservations of the instruction.  */
  state_t state;
  /* The following field refers to the next state which characterizes
     unit reservations of the instruction.  */
  alt_state_t next_alt_state;
  /* The following field refers to the next state in sorted list.  */
  alt_state_t next_sorted_alt_state;
};

/* The following node type describes insn of automaton.  They are
   labels of FA arcs.  */
struct ainsn
{
  /* The following field value is the corresponding insn declaration
     of description.  */
  struct insn_reserv_decl *insn_reserv_decl;
  /* The following field value is the next insn declaration for an
     automaton.  */
  ainsn_t next_ainsn;
  /* The following field is states which characterize automaton unit
     reservations of the instruction.  The value can be NULL only if it
     is special insn `cycle advancing'.  */
  alt_state_t alt_states;
  /* The following field is sorted list of states which characterize
     automaton unit reservations of the instruction.  The value can be
     NULL only if it is special insn `cycle advancing'.  */
  alt_state_t sorted_alt_states;
  /* The following field refers the next automaton insn with
     the same reservations.  */
  ainsn_t next_same_reservs_insn;
  /* The following field is flag of the first automaton insn with the
     same reservations in the declaration list.  Only arcs marked such
     insn is present in the automaton.  This significantly decreases
     memory requirements especially when several automata are
     formed.  */
  char first_insn_with_same_reservs;
  /* The following member has nonzero value if there is arc from state of
     the automaton marked by the ainsn.  */
  char arc_exists_p;
  /* Cyclic list of insns of an equivalence class is formed with the
     aid of the following field.  */
  ainsn_t next_equiv_class_insn;
  /* The following field value is nonzero if the insn declaration is
     the first insn declaration with given equivalence number.  */
  char first_ainsn_with_given_equivalence_num;
  /* The following field is number of class of equivalence of insns.
     It is necessary because many insns may be equivalent with the
     point of view of pipeline hazards.  */
  int insn_equiv_class_num;
  /* The following member value is TRUE if there is an arc in the
     automaton marked by the insn into another state.  In other
     words, the insn can change the state of the automaton.  */
  int important_p;
};

/* The following describes an automaton for PHR.  */
struct automaton
{
  /* The following field value is the list of insn declarations for
     given automaton.  */
  ainsn_t ainsn_list;
  /* Pointers to the ainsns corresponding to the special reservations.  */
  ainsn_t advance_ainsn, collapse_ainsn;

  /* The following field value is the corresponding automaton
     declaration.  This field is not NULL only if the automatic
     partition on automata is not used.  */
  struct automaton_decl *corresponding_automaton_decl;
  /* The following field value is the next automaton.  */
  automaton_t next_automaton;
  /* The following field is start state of FA.  There are not unit
     reservations in the state.  */
  state_t start_state;
  /* The following field value is number of equivalence classes of
     insns (see field `insn_equiv_class_num' in
     `insn_reserv_decl').  */
  int insn_equiv_classes_num;
  /* The following field value is number of states of final DFA.  */
  int achieved_states_num;
  /* The following field value is the order number (0, 1, ...) of
     given automaton.  */
  int automaton_order_num;
  /* The following fields contain statistics information about
     building automaton.  */
  int NDFA_states_num, DFA_states_num;
  /* The following field value is defined only if minimization of DFA
     is used.  */
  int minimal_DFA_states_num;
  int NDFA_arcs_num, DFA_arcs_num;
  /* The following field value is defined only if minimization of DFA
     is used.  */
  int minimal_DFA_arcs_num;
  /* The following member refers for two table state x ainsn -> int.
     ??? Above sentence is incomprehensible.  */
  state_ainsn_table_t trans_table;
  /* The following member value is maximal value of min issue delay
     for insns of the automaton.  */
  int max_min_delay;
  /* Usually min issue delay is small and we can place several (2, 4,
     8) elements in one vector element.  So the compression factor can
     be 1 (no compression), 2, 4, 8.  */
  int min_issue_delay_table_compression_factor;
  /* Total number of locked states in this automaton.  */
  int locked_states;
};

/* The following is the element of the list of automata.  */
struct automata_list_el
{
  /* The automaton itself.  */
  automaton_t automaton;
  /* The next automata set element.  */
  automata_list_el_t next_automata_list_el;
};

/* The following structure describes a table state X ainsn -> int(>= 0).  */
struct state_ainsn_table
{
  /* Automaton to which given table belongs.  */
  automaton_t automaton;
  /* The following tree vectors for comb vector implementation of the
     table.  */
  vla_hwint_t comb_vect;
  vla_hwint_t check_vect;
  vla_hwint_t base_vect;
  /* This is simple implementation of the table.  */
  vla_hwint_t full_vect;
  /* Minimal and maximal values of the previous vectors.  */
  int min_comb_vect_el_value, max_comb_vect_el_value;
  int min_base_vect_el_value, max_base_vect_el_value;
};

/* Macros to access members of unions.  Use only them for access to
   union members of declarations and regexps.  */

#if CHECKING_P && (GCC_VERSION >= 2007)

#define DECL_UNIT(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_unit)					\
       decl_mode_check_failed (_decl->mode, "dm_unit",			\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.unit; }))

#define DECL_BYPASS(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_bypass)					\
       decl_mode_check_failed (_decl->mode, "dm_bypass",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.bypass; }))

#define DECL_AUTOMATON(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_automaton)					\
       decl_mode_check_failed (_decl->mode, "dm_automaton",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.automaton; }))

#define DECL_EXCL(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_excl)					\
       decl_mode_check_failed (_decl->mode, "dm_excl",			\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.excl; }))

#define DECL_PRESENCE(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_presence)					\
       decl_mode_check_failed (_decl->mode, "dm_presence",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.presence; }))

#define DECL_ABSENCE(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_absence)					\
       decl_mode_check_failed (_decl->mode, "dm_absence",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.absence; }))

#define DECL_RESERV(d) __extension__					\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_reserv)					\
       decl_mode_check_failed (_decl->mode, "dm_reserv",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.reserv; }))

#define DECL_INSN_RESERV(d) __extension__				\
(({ __typeof (d) const _decl = (d);					\
     if (_decl->mode != dm_insn_reserv)					\
       decl_mode_check_failed (_decl->mode, "dm_insn_reserv",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_decl)->decl.insn_reserv; }))

static const char *decl_name (enum decl_mode);
static void decl_mode_check_failed (enum decl_mode, const char *,
				    const char *, int, const char *)
     ATTRIBUTE_NORETURN;

/* Return string representation of declaration mode MODE.  */
static const char *
decl_name (enum decl_mode mode)
{
  static char str [100];

  if (mode == dm_unit)
    return "dm_unit";
  else if (mode == dm_bypass)
    return "dm_bypass";
  else if (mode == dm_automaton)
    return "dm_automaton";
  else if (mode == dm_excl)
    return "dm_excl";
  else if (mode == dm_presence)
    return "dm_presence";
  else if (mode == dm_absence)
    return "dm_absence";
  else if (mode == dm_reserv)
    return "dm_reserv";
  else if (mode == dm_insn_reserv)
    return "dm_insn_reserv";
  else
    sprintf (str, "unknown (%d)", (int) mode);
  return str;
}

/* The function prints message about unexpected declaration and finish
   the program.  */
static void
decl_mode_check_failed (enum decl_mode mode, const char *expected_mode_str,
			const char *file, int line, const char *func)
{
  fprintf
    (stderr,
     "\n%s: %d: error in %s: DECL check: expected decl %s, have %s\n",
     file, line, func, expected_mode_str, decl_name (mode));
  exit (1);
}


#define REGEXP_UNIT(r) __extension__					\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_unit)					\
       regexp_mode_check_failed (_regexp->mode, "rm_unit",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.unit; }))

#define REGEXP_RESERV(r) __extension__					\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_reserv)					\
       regexp_mode_check_failed (_regexp->mode, "rm_reserv",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.reserv; }))

#define REGEXP_SEQUENCE(r) __extension__				\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_sequence)					\
       regexp_mode_check_failed (_regexp->mode, "rm_sequence",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.sequence; }))

#define REGEXP_REPEAT(r) __extension__					\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_repeat)					\
       regexp_mode_check_failed (_regexp->mode, "rm_repeat",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.repeat; }))

#define REGEXP_ALLOF(r) __extension__					\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_allof)					\
       regexp_mode_check_failed (_regexp->mode, "rm_allof",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.allof; }))

#define REGEXP_ONEOF(r) __extension__					\
(({ struct regexp *const _regexp = (r);					\
     if (_regexp->mode != rm_oneof)					\
       regexp_mode_check_failed (_regexp->mode, "rm_oneof",		\
			       __FILE__, __LINE__, __FUNCTION__);	\
     &(_regexp)->regexp.oneof; }))

static const char *regexp_name (enum regexp_mode);
static void regexp_mode_check_failed (enum regexp_mode, const char *,
				      const char *, int,
				      const char *) ATTRIBUTE_NORETURN;


/* Return string representation of regexp mode MODE.  */
static const char *
regexp_name (enum regexp_mode mode)
{
  switch (mode)
    {
    case rm_unit:
      return "rm_unit";
    case rm_reserv:
      return "rm_reserv";
    case rm_nothing:
      return "rm_nothing";
    case rm_sequence:
      return "rm_sequence";
    case rm_repeat:
      return "rm_repeat";
    case rm_allof:
      return "rm_allof";
    case rm_oneof:
      return "rm_oneof";
    default:
      gcc_unreachable ();
    }
}

/* The function prints message about unexpected regexp and finish the
   program.  */
static void
regexp_mode_check_failed (enum regexp_mode mode,
			  const char *expected_mode_str,
			  const char *file, int line, const char *func)
{
  fprintf
    (stderr,
     "\n%s: %d: error in %s: REGEXP check: expected decl %s, have %s\n",
     file, line, func, expected_mode_str, regexp_name (mode));
  exit (1);
}

#else /* #if CHECKING_P && (GCC_VERSION >= 2007) */

#define DECL_UNIT(d) (&(d)->decl.unit)
#define DECL_BYPASS(d) (&(d)->decl.bypass)
#define DECL_AUTOMATON(d) (&(d)->decl.automaton)
#define DECL_EXCL(d) (&(d)->decl.excl)
#define DECL_PRESENCE(d) (&(d)->decl.presence)
#define DECL_ABSENCE(d) (&(d)->decl.absence)
#define DECL_RESERV(d) (&(d)->decl.reserv)
#define DECL_INSN_RESERV(d) (&(d)->decl.insn_reserv)

#define REGEXP_UNIT(r) (&(r)->regexp.unit)
#define REGEXP_RESERV(r) (&(r)->regexp.reserv)
#define REGEXP_SEQUENCE(r) (&(r)->regexp.sequence)
#define REGEXP_REPEAT(r) (&(r)->regexp.repeat)
#define REGEXP_ALLOF(r) (&(r)->regexp.allof)
#define REGEXP_ONEOF(r) (&(r)->regexp.oneof)

#endif /* #if CHECKING_P && (GCC_VERSION >= 2007) */

#define XCREATENODE(T) ((T *) create_node (sizeof (T)))
#define XCREATENODEVEC(T, N) ((T *) create_node (sizeof (T) * (N)))
#define XCREATENODEVAR(T, S) ((T *) create_node ((S)))

#define XCOPYNODE(T, P) ((T *) copy_node ((P), sizeof (T)))
#define XCOPYNODEVEC(T, P, N) ((T *) copy_node ((P), sizeof (T) * (N)))
#define XCOPYNODEVAR(T, P, S) ((T *) copy_node ((P), (S)))

/* Create IR structure (node).  */
static void *
create_node (size_t size)
{
  void *result;

  obstack_blank (&irp, size);
  result = obstack_base (&irp);
  obstack_finish (&irp);
  /* Default values of members are NULL and zero.  */
  memset (result, 0, size);
  return result;
}

/* Copy IR structure (node).  */
static void *
copy_node (const void *from, size_t size)
{
  void *const result = create_node (size);
  memcpy (result, from, size);
  return result;
}

/* The function checks that NAME does not contain quotes (`"').  */
static const char *
check_name (const char * name, pos_t pos ATTRIBUTE_UNUSED)
{
  const char *str;

  for (str = name; *str != '\0'; str++)
    if (*str == '\"')
      error ("Name `%s' contains quotes", name);
  return name;
}

/* Pointers to all declarations during IR generation are stored in the
   following.  */
static vec<decl_t> decls;

/* Given a pointer to a (char *) and a separator, return an alloc'ed
   string containing the next separated element, taking parentheses
   into account if PAR_FLAG has nonzero value.  Advance the pointer to
   after the string scanned, or the end-of-string.  Return NULL if at
   end of string.  */
static char *
next_sep_el (const char **pstr, int sep, int par_flag)
{
  char *out_str;
  const char *p;
  int pars_num;
  int n_spaces;

  /* Remove leading whitespaces.  */
  while (ISSPACE ((int) **pstr))
    (*pstr)++;

  if (**pstr == '\0')
    return NULL;

  n_spaces = 0;
  for (pars_num = 0, p = *pstr; *p != '\0'; p++)
    {
      if (par_flag && *p == '(')
	pars_num++;
      else if (par_flag && *p == ')')
	pars_num--;
      else if (pars_num == 0 && *p == sep)
	break;
      if (pars_num == 0 && ISSPACE ((int) *p))
	n_spaces++;
      else
	{
	  for (; n_spaces != 0; n_spaces--)
	    obstack_1grow (&irp, p [-n_spaces]);
	  obstack_1grow (&irp, *p);
	}
    }
  obstack_1grow (&irp, '\0');
  out_str = (char *) obstack_base (&irp);
  obstack_finish (&irp);

  *pstr = p;
  if (**pstr == sep)
    (*pstr)++;

  return out_str;
}

/* Given a string and a separator, return the number of separated
   elements in it, taking parentheses into account if PAR_FLAG has
   nonzero value.  Return 0 for the null string, -1 if parentheses is
   not balanced.  */
static int
n_sep_els (const char *s, int sep, int par_flag)
{
  int n;
  int pars_num;

  if (*s == '\0')
    return 0;

  for (pars_num = 0, n = 1; *s; s++)
    if (par_flag && *s == '(')
      pars_num++;
    else if (par_flag && *s == ')')
      pars_num--;
    else if (pars_num == 0 && *s == sep)
      n++;

  return (pars_num != 0 ? -1 : n);
}

/* Given a string and a separator, return vector of strings which are
   elements in the string and number of elements through els_num.
   Take parentheses into account if PAREN_P has nonzero value.  The
   function also inserts the end marker NULL at the end of vector.
   Return 0 for the null string, -1 if parentheses are not balanced.  */
static char **
get_str_vect (const char *str, int *els_num, int sep, int paren_p)
{
  int i;
  char **vect;
  const char **pstr;
  char *trail;

  *els_num = n_sep_els (str, sep, paren_p);
  if (*els_num <= 0)
    return NULL;
  obstack_blank (&irp, sizeof (char *) * (*els_num + 1));
  vect = (char **) obstack_base (&irp);
  obstack_finish (&irp);
  pstr = &str;
  for (i = 0; i < *els_num; i++)
    vect [i] = next_sep_el (pstr, sep, paren_p);
  trail = next_sep_el (pstr, sep, paren_p);
  gcc_assert (!trail);
  vect [i] = NULL;
  return vect;
}

/* Process a DEFINE_CPU_UNIT.

   This gives information about a unit contained in CPU.  We fill a
   struct unit_decl with information used later by `expand_automata'.  */
static void
gen_cpu_unit (md_rtx_info *info)
{
  decl_t decl;
  char **str_cpu_units;
  int vect_length;
  int i;

  rtx def = info->def;
  str_cpu_units = get_str_vect (XSTR (def, 0), &vect_length, ',', FALSE);
  if (str_cpu_units == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 0), GET_RTX_NAME (GET_CODE (def)));
  for (i = 0; i < vect_length; i++)
    {
      decl = XCREATENODE (struct decl);
      decl->mode = dm_unit;
      decl->pos = 0;
      DECL_UNIT (decl)->name = check_name (str_cpu_units [i], decl->pos);
      DECL_UNIT (decl)->automaton_name = XSTR (def, 1);
      DECL_UNIT (decl)->query_p = 0;
      DECL_UNIT (decl)->min_occ_cycle_num = -1;
      DECL_UNIT (decl)->in_set_p = 0;
      decls.safe_push (decl);
    }
}

/* Process a DEFINE_QUERY_CPU_UNIT.

   This gives information about a unit contained in CPU.  We fill a
   struct unit_decl with information used later by `expand_automata'.  */
static void
gen_query_cpu_unit (md_rtx_info *info)
{
  decl_t decl;
  char **str_cpu_units;
  int vect_length;
  int i;

  rtx def = info->def;
  str_cpu_units = get_str_vect (XSTR (def, 0), &vect_length, ',',
				FALSE);
  if (str_cpu_units == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 0), GET_RTX_NAME (GET_CODE (def)));
  for (i = 0; i < vect_length; i++)
    {
      decl = XCREATENODE (struct decl);
      decl->mode = dm_unit;
      decl->pos = 0;
      DECL_UNIT (decl)->name = check_name (str_cpu_units [i], decl->pos);
      DECL_UNIT (decl)->automaton_name = XSTR (def, 1);
      DECL_UNIT (decl)->query_p = 1;
      decls.safe_push (decl);
    }
}

/* Process a DEFINE_BYPASS.

   This gives information about a unit contained in the CPU.  We fill
   in a struct bypass_decl with information used later by
   `expand_automata'.  */
static void
gen_bypass (md_rtx_info *info)
{
  decl_t decl;
  char **out_patterns;
  int out_length;
  char **in_patterns;
  int in_length;
  int i, j;

  rtx def = info->def;
  out_patterns = get_str_vect (XSTR (def, 1), &out_length, ',', FALSE);
  if (out_patterns == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 1), GET_RTX_NAME (GET_CODE (def)));
  in_patterns = get_str_vect (XSTR (def, 2), &in_length, ',', FALSE);
  if (in_patterns == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 2), GET_RTX_NAME (GET_CODE (def)));
  for (i = 0; i < out_length; i++)
    for (j = 0; j < in_length; j++)
      {
	decl = XCREATENODE (struct decl);
	decl->mode = dm_bypass;
	decl->pos = 0;
	DECL_BYPASS (decl)->latency = XINT (def, 0);
	DECL_BYPASS (decl)->out_pattern = out_patterns[i];
	DECL_BYPASS (decl)->in_pattern = in_patterns[j];
	DECL_BYPASS (decl)->bypass_guard_name = XSTR (def, 3);
	decls.safe_push (decl);
      }
}

/* Process an EXCLUSION_SET.

   This gives information about a cpu unit conflicts.  We fill a
   struct excl_rel_decl (excl) with information used later by
   `expand_automata'.  */
static void
gen_excl_set (md_rtx_info *info)
{
  decl_t decl;
  char **first_str_cpu_units;
  char **second_str_cpu_units;
  int first_vect_length;
  int length;
  int i;

  rtx def = info->def;
  first_str_cpu_units
    = get_str_vect (XSTR (def, 0), &first_vect_length, ',', FALSE);
  if (first_str_cpu_units == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 0), GET_RTX_NAME (GET_CODE (def)));
  second_str_cpu_units = get_str_vect (XSTR (def, 1), &length, ',',
				       FALSE);
  if (second_str_cpu_units == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 1), GET_RTX_NAME (GET_CODE (def)));
  length += first_vect_length;
  decl = XCREATENODEVAR (struct decl, (sizeof (struct decl)
				       + (length - 1) * sizeof (char *)));
  decl->mode = dm_excl;
  decl->pos = 0;
  DECL_EXCL (decl)->all_names_num = length;
  DECL_EXCL (decl)->first_list_length = first_vect_length;
  for (i = 0; i < length; i++)
    if (i < first_vect_length)
      DECL_EXCL (decl)->names [i] = first_str_cpu_units [i];
    else
      DECL_EXCL (decl)->names [i]
	= second_str_cpu_units [i - first_vect_length];
  decls.safe_push (decl);
}

/* Process a PRESENCE_SET, a FINAL_PRESENCE_SET, an ABSENCE_SET,
   FINAL_ABSENCE_SET (it is depended on PRESENCE_P and FINAL_P).

   This gives information about a cpu unit reservation requirements.
   We fill a struct unit_pattern_rel_decl with information used later
   by `expand_automata'.  */
static void
gen_presence_absence_set (md_rtx_info *info, int presence_p, int final_p)
{
  decl_t decl;
  char **str_cpu_units;
  char **str_pattern_lists;
  char ***str_patterns;
  int cpu_units_length;
  int length;
  int patterns_length;
  int i;

  rtx def = info->def;
  str_cpu_units = get_str_vect (XSTR (def, 0), &cpu_units_length, ',',
				FALSE);
  if (str_cpu_units == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 0), GET_RTX_NAME (GET_CODE (def)));
  str_pattern_lists = get_str_vect (XSTR (def, 1),
				    &patterns_length, ',', FALSE);
  if (str_pattern_lists == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 1), GET_RTX_NAME (GET_CODE (def)));
  str_patterns = XOBNEWVEC (&irp, char **, patterns_length);
  for (i = 0; i < patterns_length; i++)
    {
      str_patterns [i] = get_str_vect (str_pattern_lists [i],
				       &length, ' ', FALSE);
      gcc_assert (str_patterns [i]);
    }
  decl = XCREATENODE (struct decl);
  decl->pos = 0;
  if (presence_p)
    {
      decl->mode = dm_presence;
      DECL_PRESENCE (decl)->names_num = cpu_units_length;
      DECL_PRESENCE (decl)->names = str_cpu_units;
      DECL_PRESENCE (decl)->patterns = str_patterns;
      DECL_PRESENCE (decl)->patterns_num = patterns_length;
      DECL_PRESENCE (decl)->final_p = final_p;
    }
  else
    {
      decl->mode = dm_absence;
      DECL_ABSENCE (decl)->names_num = cpu_units_length;
      DECL_ABSENCE (decl)->names = str_cpu_units;
      DECL_ABSENCE (decl)->patterns = str_patterns;
      DECL_ABSENCE (decl)->patterns_num = patterns_length;
      DECL_ABSENCE (decl)->final_p = final_p;
    }
  decls.safe_push (decl);
}

/* Process a PRESENCE_SET.

   This gives information about a cpu unit reservation requirements.
   We fill a struct unit_pattern_rel_decl (presence) with information
   used later by `expand_automata'.  */
static void
gen_presence_set (md_rtx_info *info)
{
  gen_presence_absence_set (info, TRUE, FALSE);
}

/* Process a FINAL_PRESENCE_SET.

   This gives information about a cpu unit reservation requirements.
   We fill a struct unit_pattern_rel_decl (presence) with information
   used later by `expand_automata'.  */
static void
gen_final_presence_set (md_rtx_info *info)
{
  gen_presence_absence_set (info, TRUE, TRUE);
}

/* Process an ABSENCE_SET.

   This gives information about a cpu unit reservation requirements.
   We fill a struct unit_pattern_rel_decl (absence) with information
   used later by `expand_automata'.  */
static void
gen_absence_set (md_rtx_info *info)
{
  gen_presence_absence_set (info, FALSE, FALSE);
}

/* Process a FINAL_ABSENCE_SET.

   This gives information about a cpu unit reservation requirements.
   We fill a struct unit_pattern_rel_decl (absence) with information
   used later by `expand_automata'.  */
static void
gen_final_absence_set (md_rtx_info *info)
{
  gen_presence_absence_set (info, FALSE, TRUE);
}

/* Process a DEFINE_AUTOMATON.

   This gives information about a finite state automaton used for
   recognizing pipeline hazards.  We fill a struct automaton_decl
   with information used later by `expand_automata'.  */
static void
gen_automaton (md_rtx_info *info)
{
  decl_t decl;
  char **str_automata;
  int vect_length;
  int i;

  rtx def = info->def;
  str_automata = get_str_vect (XSTR (def, 0), &vect_length, ',', FALSE);
  if (str_automata == NULL)
    fatal_at (info->loc, "invalid string `%s' in %s",
	      XSTR (def, 0), GET_RTX_NAME (GET_CODE (def)));
  for (i = 0; i < vect_length; i++)
    {
      decl = XCREATENODE (struct decl);
      decl->mode = dm_automaton;
      decl->pos = 0;
      DECL_AUTOMATON (decl)->name = check_name (str_automata [i], decl->pos);
      decls.safe_push (decl);
    }
}

/* Process an AUTOMATA_OPTION.

   This gives information how to generate finite state automaton used
   for recognizing pipeline hazards.  */
static void
gen_automata_option (md_rtx_info *info)
{
  const char *option = XSTR (info->def, 0);
  if (strcmp (option, NO_MINIMIZATION_OPTION + 1) == 0)
    no_minimization_flag = 1;
  else if (strcmp (option, TIME_OPTION + 1) == 0)
    time_flag = 1;
  else if (strcmp (option, STATS_OPTION + 1) == 0)
    stats_flag = 1;
  else if (strcmp (option, V_OPTION + 1) == 0)
    v_flag = 1;
  else if (strcmp (option, W_OPTION + 1) == 0)
    w_flag = 1;
  else if (strcmp (option, NDFA_OPTION + 1) == 0)
    ndfa_flag = 1;
  else if (strcmp (option, COLLAPSE_OPTION + 1) == 0)
    collapse_flag = 1;
  else if (strcmp (option, NO_COMB_OPTION + 1) == 0)
    no_comb_flag = 1;
  else if (strcmp (option, PROGRESS_OPTION + 1) == 0)
    progress_flag = 1;
  else
    fatal_at (info->loc, "invalid option `%s' in %s",
	      option, GET_RTX_NAME (GET_CODE (info->def)));
}

/* Name in reservation to denote absence reservation.  */
#define NOTHING_NAME "nothing"

/* The following string contains original reservation string being
   parsed.  */
static const char *reserv_str;

/* Parse an element in STR.  */
static regexp_t
gen_regexp_el (const char *str)
{
  regexp_t regexp;
  char *dstr;
  int len;

  if (*str == '(')
    {
      len = strlen (str);
      if (str [len - 1] != ')')
	fatal ("garbage after ) in reservation `%s'", reserv_str);
      dstr = XALLOCAVAR (char, len - 1);
      memcpy (dstr, str + 1, len - 2);
      dstr [len-2] = '\0';
      regexp = gen_regexp_sequence (dstr);
    }
  else if (strcmp (str, NOTHING_NAME) == 0)
    {
      regexp = XCREATENODE (struct regexp);
      regexp->mode = rm_nothing;
    }
  else
    {
      regexp = XCREATENODE (struct regexp);
      regexp->mode = rm_unit;
      REGEXP_UNIT (regexp)->name = str;
    }
  return regexp;
}

/* Parse construction `repeat' in STR.  */
static regexp_t
gen_regexp_repeat (const char *str)
{
  regexp_t regexp;
  regexp_t repeat;
  char **repeat_vect;
  int els_num;
  int i;

  repeat_vect = get_str_vect (str, &els_num, '*', TRUE);
  if (repeat_vect == NULL)
    fatal ("invalid `%s' in reservation `%s'", str, reserv_str);
  if (els_num > 1)
    {
      regexp = gen_regexp_el (repeat_vect [0]);
      for (i = 1; i < els_num; i++)
	{
	  repeat = XCREATENODE (struct regexp);
	  repeat->mode = rm_repeat;
	  REGEXP_REPEAT (repeat)->regexp = regexp;
	  REGEXP_REPEAT (repeat)->repeat_num = atoi (repeat_vect [i]);
          if (REGEXP_REPEAT (repeat)->repeat_num <= 1)
            fatal ("repetition `%s' <= 1 in reservation `%s'",
                   str, reserv_str);
          regexp = repeat;
	}
      return regexp;
    }
  else
    return gen_regexp_el (repeat_vect[0]);
}

/* Parse reservation STR which possibly contains separator '+'.  */
static regexp_t
gen_regexp_allof (const char *str)
{
  regexp_t allof;
  char **allof_vect;
  int els_num;
  int i;

  allof_vect = get_str_vect (str, &els_num, '+', TRUE);
  if (allof_vect == NULL)
    fatal ("invalid `%s' in reservation `%s'", str, reserv_str);
  if (els_num > 1)
    {
      allof = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
			      + sizeof (regexp_t) * (els_num - 1));
      allof->mode = rm_allof;
      REGEXP_ALLOF (allof)->regexps_num = els_num;
      for (i = 0; i < els_num; i++)
	REGEXP_ALLOF (allof)->regexps [i] = gen_regexp_repeat (allof_vect [i]);
      return allof;
    }
  else
    return gen_regexp_repeat (allof_vect[0]);
}

/* Parse reservation STR which possibly contains separator '|'.  */
static regexp_t
gen_regexp_oneof (const char *str)
{
  regexp_t oneof;
  char **oneof_vect;
  int els_num;
  int i;

  oneof_vect = get_str_vect (str, &els_num, '|', TRUE);
  if (oneof_vect == NULL)
    fatal ("invalid `%s' in reservation `%s'", str, reserv_str);
  if (els_num > 1)
    {
      oneof = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
			      + sizeof (regexp_t) * (els_num - 1));
      oneof->mode = rm_oneof;
      REGEXP_ONEOF (oneof)->regexps_num = els_num;
      for (i = 0; i < els_num; i++)
	REGEXP_ONEOF (oneof)->regexps [i] = gen_regexp_allof (oneof_vect [i]);
      return oneof;
    }
  else
    return gen_regexp_allof (oneof_vect[0]);
}

/* Parse reservation STR which possibly contains separator ','.  */
static regexp_t
gen_regexp_sequence (const char *str)
{
  regexp_t sequence;
  char **sequence_vect;
  int els_num;
  int i;

  sequence_vect = get_str_vect (str, &els_num, ',', TRUE);
  if (els_num == -1)
    fatal ("unbalanced parentheses in reservation `%s'", str);
  if (sequence_vect == NULL)
    fatal ("invalid reservation `%s'", str);
  if (els_num > 1)
    {
      sequence = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				 + sizeof (regexp_t) * (els_num - 1));
      sequence->mode = rm_sequence;
      REGEXP_SEQUENCE (sequence)->regexps_num = els_num;
      for (i = 0; i < els_num; i++)
	REGEXP_SEQUENCE (sequence)->regexps [i]
          = gen_regexp_oneof (sequence_vect [i]);
      return sequence;
    }
  else
    return gen_regexp_oneof (sequence_vect[0]);
}

/* Parse construction reservation STR.  */
static regexp_t
gen_regexp (const char *str)
{
  reserv_str = str;
  return gen_regexp_sequence (str);
}

/* Process a DEFINE_RESERVATION.

   This gives information about a reservation of cpu units.  We fill
   in a struct reserv_decl with information used later by
   `expand_automata'.  */
static void
gen_reserv (md_rtx_info *info)
{
  decl_t decl;

  rtx def = info->def;
  decl = XCREATENODE (struct decl);
  decl->mode = dm_reserv;
  decl->pos = 0;
  DECL_RESERV (decl)->name = check_name (XSTR (def, 0), decl->pos);
  DECL_RESERV (decl)->regexp = gen_regexp (XSTR (def, 1));
  decls.safe_push (decl);
}

/* Process a DEFINE_INSN_RESERVATION.

   This gives information about the reservation of cpu units by an
   insn.  We fill a struct insn_reserv_decl with information used
   later by `expand_automata'.  */
static void
gen_insn_reserv (md_rtx_info *info)
{
  decl_t decl;

  rtx def = info->def;
  decl = XCREATENODE (struct decl);
  decl->mode = dm_insn_reserv;
  decl->pos = 0;
  DECL_INSN_RESERV (decl)->name
    = check_name (XSTR (def, 0), decl->pos);
  DECL_INSN_RESERV (decl)->default_latency = XINT (def, 1);
  DECL_INSN_RESERV (decl)->condexp = XEXP (def, 2);
  DECL_INSN_RESERV (decl)->regexp = gen_regexp (XSTR (def, 3));
  decls.safe_push (decl);
}



/* The function evaluates hash value (0..UINT_MAX) of string.  */
static unsigned
string_hash (const char *string)
{
  unsigned result, i;

  for (result = i = 0;*string++ != '\0'; i++)
    result += ((unsigned char) *string << (i % CHAR_BIT));
  return result;
}



/* This page contains abstract data `table of automaton declarations'.
   Elements of the table is nodes representing automaton declarations.
   Key of the table elements is name of given automaton.  Remember
   that automaton names have own space.  */

/* The function evaluates hash value of an automaton declaration.  The
   function is used by abstract data `hashtab'.  The function returns
   hash value (0..UINT_MAX) of given automaton declaration.  */
static hashval_t
automaton_decl_hash (const void *automaton_decl)
{
  const_decl_t const decl = (const_decl_t) automaton_decl;

  gcc_assert (decl->mode != dm_automaton
	      || DECL_AUTOMATON (decl)->name);
  return string_hash (DECL_AUTOMATON (decl)->name);
}

/* The function tests automaton declarations on equality of their
   keys.  The function is used by abstract data `hashtab'.  The
   function returns 1 if the declarations have the same key, 0
   otherwise.  */
static int
automaton_decl_eq_p (const void* automaton_decl_1,
		     const void* automaton_decl_2)
{
  const_decl_t const decl1 = (const_decl_t) automaton_decl_1;
  const_decl_t const decl2 = (const_decl_t) automaton_decl_2;

  gcc_assert (decl1->mode == dm_automaton
	      && DECL_AUTOMATON (decl1)->name
	      && decl2->mode == dm_automaton
	      && DECL_AUTOMATON (decl2)->name);
  return strcmp (DECL_AUTOMATON (decl1)->name,
		 DECL_AUTOMATON (decl2)->name) == 0;
}

/* The automaton declaration table itself is represented by the
   following variable.  */
static htab_t automaton_decl_table;

/* The function inserts automaton declaration into the table.  The
   function does nothing if an automaton declaration with the same key
   exists already in the table.  The function returns automaton
   declaration node in the table with the same key as given automaton
   declaration node.  */
static decl_t
insert_automaton_decl (decl_t automaton_decl)
{
  void **entry_ptr;

  entry_ptr = htab_find_slot (automaton_decl_table, automaton_decl, INSERT);
  if (*entry_ptr == NULL)
    *entry_ptr = (void *) automaton_decl;
  return (decl_t) *entry_ptr;
}

/* The following variable value is node representing automaton
   declaration.  The node used for searching automaton declaration
   with given name.  */
static struct decl work_automaton_decl;

/* The function searches for automaton declaration in the table with
   the same key as node representing name of the automaton
   declaration.  The function returns node found in the table, NULL if
   such node does not exist in the table.  */
static decl_t
find_automaton_decl (const char *name)
{
  void *entry;

  work_automaton_decl.mode = dm_automaton;
  DECL_AUTOMATON (&work_automaton_decl)->name = name;
  entry = htab_find (automaton_decl_table, &work_automaton_decl);
  return (decl_t) entry;
}

/* The function creates empty automaton declaration table and node
   representing automaton declaration and used for searching automaton
   declaration with given name.  The function must be called only once
   before any work with the automaton declaration table.  */
static void
initiate_automaton_decl_table (void)
{
  work_automaton_decl.mode = dm_automaton;
  automaton_decl_table = htab_create (10, automaton_decl_hash,
				      automaton_decl_eq_p, (htab_del) 0);
}

/* The function deletes the automaton declaration table.  Only call of
   function `initiate_automaton_decl_table' is possible immediately
   after this function call.  */
static void
finish_automaton_decl_table (void)
{
  htab_delete (automaton_decl_table);
}



/* This page contains abstract data `table of insn declarations'.
   Elements of the table is nodes representing insn declarations.  Key
   of the table elements is name of given insn (in corresponding
   define_insn_reservation).  Remember that insn names have own
   space.  */

/* The function evaluates hash value of an insn declaration.  The
   function is used by abstract data `hashtab'.  The function returns
   hash value (0..UINT_MAX) of given insn declaration.  */
static hashval_t
insn_decl_hash (const void *insn_decl)
{
  const_decl_t const decl = (const_decl_t) insn_decl;

  gcc_assert (decl->mode == dm_insn_reserv
	      && DECL_INSN_RESERV (decl)->name);
  return string_hash (DECL_INSN_RESERV (decl)->name);
}

/* The function tests insn declarations on equality of their keys.
   The function is used by abstract data `hashtab'.  The function
   returns 1 if declarations have the same key, 0 otherwise.  */
static int
insn_decl_eq_p (const void *insn_decl_1, const void *insn_decl_2)
{
  const_decl_t const decl1 = (const_decl_t) insn_decl_1;
  const_decl_t const decl2 = (const_decl_t) insn_decl_2;

  gcc_assert (decl1->mode == dm_insn_reserv
	      && DECL_INSN_RESERV (decl1)->name
	      && decl2->mode == dm_insn_reserv
	      && DECL_INSN_RESERV (decl2)->name);
  return strcmp (DECL_INSN_RESERV (decl1)->name,
                 DECL_INSN_RESERV (decl2)->name) == 0;
}

/* The insn declaration table itself is represented by the following
   variable.  The table does not contain insn reservation
   declarations.  */
static htab_t insn_decl_table;

/* The function inserts insn declaration into the table.  The function
   does nothing if an insn declaration with the same key exists
   already in the table.  The function returns insn declaration node
   in the table with the same key as given insn declaration node.  */
static decl_t
insert_insn_decl (decl_t insn_decl)
{
  void **entry_ptr;

  entry_ptr = htab_find_slot (insn_decl_table, insn_decl, INSERT);
  if (*entry_ptr == NULL)
    *entry_ptr = (void *) insn_decl;
  return (decl_t) *entry_ptr;
}

/* The following variable value is node representing insn reservation
   declaration.  The node used for searching insn reservation
   declaration with given name.  */
static struct decl work_insn_decl;

/* The function searches for insn reservation declaration in the table
   with the same key as node representing name of the insn reservation
   declaration.  The function returns node found in the table, NULL if
   such node does not exist in the table.  */
static decl_t
find_insn_decl (const char *name)
{
  void *entry;

  work_insn_decl.mode = dm_insn_reserv;
  DECL_INSN_RESERV (&work_insn_decl)->name = name;
  entry = htab_find (insn_decl_table, &work_insn_decl);
  return (decl_t) entry;
}

/* The function creates empty insn declaration table and node
   representing insn declaration and used for searching insn
   declaration with given name.  The function must be called only once
   before any work with the insn declaration table.  */
static void
initiate_insn_decl_table (void)
{
  work_insn_decl.mode = dm_insn_reserv;
  insn_decl_table = htab_create (10, insn_decl_hash, insn_decl_eq_p,
				 (htab_del) 0);
}

/* The function deletes the insn declaration table.  Only call of
   function `initiate_insn_decl_table' is possible immediately after
   this function call.  */
static void
finish_insn_decl_table (void)
{
  htab_delete (insn_decl_table);
}



/* This page contains abstract data `table of declarations'.  Elements
   of the table is nodes representing declarations (of units and
   reservations).  Key of the table elements is names of given
   declarations.  */

/* The function evaluates hash value of a declaration.  The function
   is used by abstract data `hashtab'.  The function returns hash
   value (0..UINT_MAX) of given declaration.  */
static hashval_t
decl_hash (const void *decl)
{
  const_decl_t const d = (const_decl_t) decl;

  gcc_assert ((d->mode == dm_unit && DECL_UNIT (d)->name)
	      || (d->mode == dm_reserv && DECL_RESERV (d)->name));
  return string_hash (d->mode == dm_unit
		      ? DECL_UNIT (d)->name : DECL_RESERV (d)->name);
}

/* The function tests declarations on equality of their keys.  The
   function is used by abstract data 'hashtab'.  The function
   returns 1 if the declarations have the same key, 0 otherwise.  */
static int
decl_eq_p (const void *decl_1, const void *decl_2)
{
  const_decl_t const d1 = (const_decl_t) decl_1;
  const_decl_t const d2 = (const_decl_t) decl_2;

  gcc_assert ((d1->mode == dm_unit && DECL_UNIT (d1)->name)
	      || (d1->mode == dm_reserv && DECL_RESERV (d1)->name));
  gcc_assert ((d2->mode == dm_unit && DECL_UNIT (d2)->name)
	      || (d2->mode == dm_reserv && DECL_RESERV (d2)->name));
  return strcmp ((d1->mode == dm_unit
                  ? DECL_UNIT (d1)->name : DECL_RESERV (d1)->name),
                 (d2->mode == dm_unit
                  ? DECL_UNIT (d2)->name : DECL_RESERV (d2)->name)) == 0;
}

/* The declaration table itself is represented by the following
   variable.  */
static htab_t decl_table;

/* The function inserts declaration into the table.  The function does
   nothing if a declaration with the same key exists already in the
   table.  The function returns declaration node in the table with the
   same key as given declaration node.  */

static decl_t
insert_decl (decl_t decl)
{
  void **entry_ptr;

  entry_ptr = htab_find_slot (decl_table, decl, INSERT);
  if (*entry_ptr == NULL)
    *entry_ptr = (void *) decl;
  return (decl_t) *entry_ptr;
}

/* The following variable value is node representing declaration.  The
   node used for searching declaration with given name.  */
static struct decl work_decl;

/* The function searches for declaration in the table with the same
   key as node representing name of the declaration.  The function
   returns node found in the table, NULL if such node does not exist
   in the table.  */
static decl_t
find_decl (const char *name)
{
  void *entry;

  work_decl.mode = dm_unit;
  DECL_UNIT (&work_decl)->name = name;
  entry = htab_find (decl_table, &work_decl);
  return (decl_t) entry;
}

/* The function creates empty declaration table and node representing
   declaration and used for searching declaration with given name.
   The function must be called only once before any work with the
   declaration table.  */
static void
initiate_decl_table (void)
{
  work_decl.mode = dm_unit;
  decl_table = htab_create (10, decl_hash, decl_eq_p, (htab_del) 0);
}

/* The function deletes the declaration table.  Only call of function
   `initiate_declaration_table' is possible immediately after this
   function call.  */
static void
finish_decl_table (void)
{
  htab_delete (decl_table);
}



/* This page contains checker of pipeline hazard description.  */

/* Checking NAMES in an exclusion clause vector and returning formed
   unit_set_el_list.  */
static unit_set_el_t
process_excls (char **names, int num, pos_t excl_pos ATTRIBUTE_UNUSED)
{
  unit_set_el_t el_list;
  unit_set_el_t last_el;
  unit_set_el_t new_el;
  decl_t decl_in_table;
  int i;

  el_list = NULL;
  last_el = NULL;
  for (i = 0; i < num; i++)
    {
      decl_in_table = find_decl (names [i]);
      if (decl_in_table == NULL)
	error ("unit `%s' in exclusion is not declared", names [i]);
      else if (decl_in_table->mode != dm_unit)
	error ("`%s' in exclusion is not unit", names [i]);
      else
	{
	  new_el = XCREATENODE (struct unit_set_el);
	  new_el->unit_decl = DECL_UNIT (decl_in_table);
	  new_el->next_unit_set_el = NULL;
	  if (last_el == NULL)
	    el_list = last_el = new_el;
	  else
	    {
	      last_el->next_unit_set_el = new_el;
	      last_el = last_el->next_unit_set_el;
	    }
	}
    }
  return el_list;
}

/* The function adds each element from SOURCE_LIST to the exclusion
   list of the each element from DEST_LIST.  Checking situation "unit
   excludes itself".  */
static void
add_excls (unit_set_el_t dest_list, unit_set_el_t source_list,
	   pos_t excl_pos ATTRIBUTE_UNUSED)
{
  unit_set_el_t dst;
  unit_set_el_t src;
  unit_set_el_t curr_el;
  unit_set_el_t prev_el;
  unit_set_el_t copy;

  for (dst = dest_list; dst != NULL; dst = dst->next_unit_set_el)
    for (src = source_list; src != NULL; src = src->next_unit_set_el)
      {
	if (dst->unit_decl == src->unit_decl)
	  {
	    error ("unit `%s' excludes itself", src->unit_decl->name);
	    continue;
	  }
	if (dst->unit_decl->automaton_name != NULL
	    && src->unit_decl->automaton_name != NULL
	    && strcmp (dst->unit_decl->automaton_name,
		       src->unit_decl->automaton_name) != 0)
	  {
	    error ("units `%s' and `%s' in exclusion set belong to different automata",
		   src->unit_decl->name, dst->unit_decl->name);
	    continue;
	  }
	for (curr_el = dst->unit_decl->excl_list, prev_el = NULL;
	     curr_el != NULL;
	     prev_el = curr_el, curr_el = curr_el->next_unit_set_el)
	  if (curr_el->unit_decl == src->unit_decl)
	    break;
	if (curr_el == NULL)
	  {
	    /* Element not found - insert.  */
	    copy = XCOPYNODE (struct unit_set_el, src);
	    copy->next_unit_set_el = NULL;
	    if (prev_el == NULL)
	      dst->unit_decl->excl_list = copy;
	    else
	      prev_el->next_unit_set_el = copy;
	}
    }
}

/* Checking NAMES in presence/absence clause and returning the
   formed unit_set_el_list.  The function is called only after
   processing all exclusion sets.  */
static unit_set_el_t
process_presence_absence_names (char **names, int num,
				pos_t req_pos ATTRIBUTE_UNUSED,
				int presence_p, int final_p)
{
  unit_set_el_t el_list;
  unit_set_el_t last_el;
  unit_set_el_t new_el;
  decl_t decl_in_table;
  int i;

  el_list = NULL;
  last_el = NULL;
  for (i = 0; i < num; i++)
    {
      decl_in_table = find_decl (names [i]);
      if (decl_in_table == NULL)
	error ((presence_p
		? (final_p
		   ? "unit `%s' in final presence set is not declared"
		   : "unit `%s' in presence set is not declared")
		: (final_p
		   ? "unit `%s' in final absence set is not declared"
		   : "unit `%s' in absence set is not declared")), names [i]);
      else if (decl_in_table->mode != dm_unit)
	error ((presence_p
		? (final_p
		   ? "`%s' in final presence set is not unit"
		   : "`%s' in presence set is not unit")
		: (final_p
		   ? "`%s' in final absence set is not unit"
		   : "`%s' in absence set is not unit")), names [i]);
      else
	{
	  new_el = XCREATENODE (struct unit_set_el);
	  new_el->unit_decl = DECL_UNIT (decl_in_table);
	  new_el->next_unit_set_el = NULL;
	  if (last_el == NULL)
	    el_list = last_el = new_el;
	  else
	    {
	      last_el->next_unit_set_el = new_el;
	      last_el = last_el->next_unit_set_el;
	    }
	}
    }
  return el_list;
}

/* Checking NAMES in patterns of a presence/absence clause and
   returning the formed pattern_set_el_list.  The function is called
   only after processing all exclusion sets.  */
static pattern_set_el_t
process_presence_absence_patterns (char ***patterns, int num,
				   pos_t req_pos ATTRIBUTE_UNUSED,
				   int presence_p, int final_p)
{
  pattern_set_el_t el_list;
  pattern_set_el_t last_el;
  pattern_set_el_t new_el;
  decl_t decl_in_table;
  int i, j;

  el_list = NULL;
  last_el = NULL;
  for (i = 0; i < num; i++)
    {
      for (j = 0; patterns [i] [j] != NULL; j++)
	;
      new_el = XCREATENODEVAR (struct pattern_set_el,
			       sizeof (struct pattern_set_el)
			       + sizeof (struct unit_decl *) * j);
      new_el->unit_decls
	= (struct unit_decl **) ((char *) new_el
				 + sizeof (struct pattern_set_el));
      new_el->next_pattern_set_el = NULL;
      if (last_el == NULL)
	el_list = last_el = new_el;
      else
	{
	  last_el->next_pattern_set_el = new_el;
	  last_el = last_el->next_pattern_set_el;
	}
      new_el->units_num = 0;
      for (j = 0; patterns [i] [j] != NULL; j++)
	{
	  decl_in_table = find_decl (patterns [i] [j]);
	  if (decl_in_table == NULL)
	    error ((presence_p
		    ? (final_p
		       ? "unit `%s' in final presence set is not declared"
		       : "unit `%s' in presence set is not declared")
		    : (final_p
		       ? "unit `%s' in final absence set is not declared"
		       : "unit `%s' in absence set is not declared")),
		   patterns [i] [j]);
	  else if (decl_in_table->mode != dm_unit)
	    error ((presence_p
		    ? (final_p
		       ? "`%s' in final presence set is not unit"
		       : "`%s' in presence set is not unit")
		    : (final_p
		       ? "`%s' in final absence set is not unit"
		       : "`%s' in absence set is not unit")),
		   patterns [i] [j]);
	  else
	    {
	      new_el->unit_decls [new_el->units_num]
		= DECL_UNIT (decl_in_table);
	      new_el->units_num++;
	    }
	}
    }
  return el_list;
}

/* The function adds each element from PATTERN_LIST to presence (if
   PRESENCE_P) or absence list of the each element from DEST_LIST.
   Checking situations "unit requires own absence", and "unit excludes
   and requires presence of ...", "unit requires absence and presence
   of ...", "units in (final) presence set belong to different
   automata", and "units in (final) absence set belong to different
   automata".  Remember that we process absence sets only after all
   presence sets.  */
static void
add_presence_absence (unit_set_el_t dest_list,
		      pattern_set_el_t pattern_list,
		      pos_t req_pos ATTRIBUTE_UNUSED,
		      int presence_p, int final_p)
{
  unit_set_el_t dst;
  pattern_set_el_t pat;
  struct unit_decl *unit;
  unit_set_el_t curr_excl_el;
  pattern_set_el_t curr_pat_el;
  pattern_set_el_t prev_el;
  pattern_set_el_t copy;
  int i;
  int no_error_flag;

  for (dst = dest_list; dst != NULL; dst = dst->next_unit_set_el)
    for (pat = pattern_list; pat != NULL; pat = pat->next_pattern_set_el)
      {
	for (i = 0; i < pat->units_num; i++)
	  {
	    unit = pat->unit_decls [i];
	    if (dst->unit_decl == unit && pat->units_num == 1 && !presence_p)
	      {
		error ("unit `%s' requires own absence", unit->name);
		continue;
	      }
	    if (dst->unit_decl->automaton_name != NULL
		&& unit->automaton_name != NULL
		&& strcmp (dst->unit_decl->automaton_name,
			   unit->automaton_name) != 0)
	      {
		error ((presence_p
			? (final_p
			   ? "units `%s' and `%s' in final presence set belong to different automata"
			   : "units `%s' and `%s' in presence set belong to different automata")
			: (final_p
			   ? "units `%s' and `%s' in final absence set belong to different automata"
			   : "units `%s' and `%s' in absence set belong to different automata")),
		       unit->name, dst->unit_decl->name);
		continue;
	      }
	    no_error_flag = 1;
	    if (presence_p)
	      for (curr_excl_el = dst->unit_decl->excl_list;
		   curr_excl_el != NULL;
		   curr_excl_el = curr_excl_el->next_unit_set_el)
		{
		  if (unit == curr_excl_el->unit_decl && pat->units_num == 1)
		    {
		      if (!w_flag)
			{
			  error ("unit `%s' excludes and requires presence of `%s'",
				 dst->unit_decl->name, unit->name);
			  no_error_flag = 0;
			}
		      else
			warning ("unit `%s' excludes and requires presence of `%s'",
			   dst->unit_decl->name, unit->name);
		    }
		}
	    else if (pat->units_num == 1)
	      for (curr_pat_el = dst->unit_decl->presence_list;
		   curr_pat_el != NULL;
		   curr_pat_el = curr_pat_el->next_pattern_set_el)
		if (curr_pat_el->units_num == 1
		    && unit == curr_pat_el->unit_decls [0])
		  {
		    if (!w_flag)
		      {
			error ("unit `%s' requires absence and presence of `%s'",
			       dst->unit_decl->name, unit->name);
			no_error_flag = 0;
		      }
		    else
		      warning ("unit `%s' requires absence and presence of `%s'",
			       dst->unit_decl->name, unit->name);
		  }
	    if (no_error_flag)
	      {
		for (prev_el = (presence_p
				? (final_p
				   ? dst->unit_decl->final_presence_list
				   : dst->unit_decl->presence_list)
				: (final_p
				   ? dst->unit_decl->final_absence_list
				   : dst->unit_decl->absence_list));
		     prev_el != NULL && prev_el->next_pattern_set_el != NULL;
		     prev_el = prev_el->next_pattern_set_el)
		  ;
		copy = XCOPYNODE (struct pattern_set_el, pat);
		copy->next_pattern_set_el = NULL;
		if (prev_el == NULL)
		  {
		    if (presence_p)
		      {
			if (final_p)
			  dst->unit_decl->final_presence_list = copy;
			else
			  dst->unit_decl->presence_list = copy;
		      }
		    else if (final_p)
		      dst->unit_decl->final_absence_list = copy;
		    else
		      dst->unit_decl->absence_list = copy;
		  }
		else
		  prev_el->next_pattern_set_el = copy;
	      }
	  }
      }
}


/* The function inserts BYPASS in the list of bypasses of the
   corresponding output insn.  The order of bypasses in the list is
   described in a comment for member `bypass_list' (see above).  If
   there is already the same bypass in the list the function reports
   this and does nothing.  */
static void
insert_bypass (struct bypass_decl *bypass)
{
  struct bypass_decl *curr, *last;
  struct insn_reserv_decl *out_insn_reserv = bypass->out_insn_reserv;
  struct insn_reserv_decl *in_insn_reserv = bypass->in_insn_reserv;

  for (curr = out_insn_reserv->bypass_list, last = NULL;
       curr != NULL;
       last = curr, curr = curr->next)
    if (curr->in_insn_reserv == in_insn_reserv)
      {
	if ((bypass->bypass_guard_name != NULL
	     && curr->bypass_guard_name != NULL
	     && ! strcmp (bypass->bypass_guard_name, curr->bypass_guard_name))
	    || bypass->bypass_guard_name == curr->bypass_guard_name)
	  {
	    if (bypass->bypass_guard_name == NULL)
	      {
		if (!w_flag)
		  error ("the same bypass `%s - %s' is already defined",
			 bypass->out_pattern, bypass->in_pattern);
		else
		  warning ("the same bypass `%s - %s' is already defined",
			   bypass->out_pattern, bypass->in_pattern);
	      }
	    else if (!w_flag)
	      error ("the same bypass `%s - %s' (guard %s) is already defined",
		     bypass->out_pattern, bypass->in_pattern,
		     bypass->bypass_guard_name);
	    else
	      warning
		("the same bypass `%s - %s' (guard %s) is already defined",
		 bypass->out_pattern, bypass->in_pattern,
		 bypass->bypass_guard_name);
	    return;
	  }
	if (curr->bypass_guard_name == NULL)
	  break;
	if (curr->next == NULL || curr->next->in_insn_reserv != in_insn_reserv)
	  {
	    last = curr;
	    break;
	  }

      }
  if (last == NULL)
    {
      bypass->next = out_insn_reserv->bypass_list;
      out_insn_reserv->bypass_list = bypass;
    }
  else
    {
      bypass->next = last->next;
      last->next = bypass;
    }
}

/* BYPASS is a define_bypass decl that includes glob pattern PATTERN.
   Call FN (BYPASS, INSN, DATA) for each matching instruction INSN.  */

static void
for_each_matching_insn (decl_t bypass, const char *pattern,
			void (*fn) (decl_t, decl_t, void *), void *data)
{
  decl_t insn_reserv;
  bool matched_p;
  int i;

  matched_p = false;
  if (strpbrk (pattern, "*?["))
    for (i = 0; i < description->decls_num; i++)
      {
	insn_reserv = description->decls[i];
	if (insn_reserv->mode == dm_insn_reserv
	    && fnmatch (pattern, DECL_INSN_RESERV (insn_reserv)->name, 0) == 0)
	  {
	    fn (bypass, insn_reserv, data);
	    matched_p = true;
	  }
      }
  else
    {
      insn_reserv = find_insn_decl (pattern);
      if (insn_reserv)
	{
	  fn (bypass, insn_reserv, data);
	  matched_p = true;
	}
    }
  if (!matched_p)
    error ("there is no insn reservation that matches `%s'", pattern);
}

/* A subroutine of process_bypass that is called for each pair
   of matching instructions.  OUT_INSN_RESERV is the output
   instruction and DATA is the input instruction.  */

static void
process_bypass_2 (decl_t model, decl_t out_insn_reserv, void *data)
{
  struct bypass_decl *bypass;
  decl_t in_insn_reserv;

  in_insn_reserv = (decl_t) data;
  if (strcmp (DECL_INSN_RESERV (in_insn_reserv)->name,
	      DECL_BYPASS (model)->in_pattern) == 0
      && strcmp (DECL_INSN_RESERV (out_insn_reserv)->name,
		 DECL_BYPASS (model)->out_pattern) == 0)
    bypass = DECL_BYPASS (model);
  else
    {
      bypass = XCNEW (struct bypass_decl);
      bypass->latency = DECL_BYPASS (model)->latency;
      bypass->out_pattern = DECL_INSN_RESERV (out_insn_reserv)->name;
      bypass->in_pattern = DECL_INSN_RESERV (in_insn_reserv)->name;
      bypass->bypass_guard_name = DECL_BYPASS (model)->bypass_guard_name;
    }
  bypass->out_insn_reserv = DECL_INSN_RESERV (out_insn_reserv);
  bypass->in_insn_reserv = DECL_INSN_RESERV (in_insn_reserv);
  insert_bypass (bypass);
}

/* A subroutine of process_bypass that is called for each input
   instruction IN_INSN_RESERV.  */

static void
process_bypass_1 (decl_t bypass, decl_t in_insn_reserv,
		  void *data ATTRIBUTE_UNUSED)
{
  for_each_matching_insn (bypass, DECL_BYPASS (bypass)->out_pattern,
			  process_bypass_2, in_insn_reserv);
}

/* Process define_bypass decl BYPASS, inserting a bypass for each specific
   pair of insn reservations.  */

static void
process_bypass (decl_t bypass)
{
  for_each_matching_insn (bypass, DECL_BYPASS (bypass)->in_pattern,
			  process_bypass_1, NULL);
}

/* The function processes pipeline description declarations, checks
   their correctness, and forms exclusion/presence/absence sets.  */
static void
process_decls (void)
{
  decl_t decl;
  decl_t automaton_decl;
  decl_t decl_in_table;
  int automaton_presence;
  int i;

  /* Checking repeated automata declarations.  */
  automaton_presence = 0;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_automaton)
	{
	  automaton_presence = 1;
	  decl_in_table = insert_automaton_decl (decl);
	  if (decl_in_table != decl)
	    {
	      if (!w_flag)
		error ("repeated declaration of automaton `%s'",
		       DECL_AUTOMATON (decl)->name);
	      else
		warning ("repeated declaration of automaton `%s'",
			 DECL_AUTOMATON (decl)->name);
	    }
	}
    }
  /* Checking undeclared automata, repeated declarations (except for
     automata) and correctness of their attributes (insn latency times
     etc.).  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	{
	  if (DECL_INSN_RESERV (decl)->default_latency < 0)
	    error ("define_insn_reservation `%s' has negative latency time",
		   DECL_INSN_RESERV (decl)->name);
	  DECL_INSN_RESERV (decl)->insn_num = description->insns_num;
	  description->insns_num++;
	  decl_in_table = insert_insn_decl (decl);
	  if (decl_in_table != decl)
	    error ("`%s' is already used as insn reservation name",
		   DECL_INSN_RESERV (decl)->name);
	}
      else if (decl->mode == dm_bypass)
	{
	  if (DECL_BYPASS (decl)->latency < 0)
	    error ("define_bypass `%s - %s' has negative latency time",
		   DECL_BYPASS (decl)->out_pattern,
		   DECL_BYPASS (decl)->in_pattern);
	}
      else if (decl->mode == dm_unit || decl->mode == dm_reserv)
	{
	  if (decl->mode == dm_unit)
	    {
	      DECL_UNIT (decl)->automaton_decl = NULL;
	      if (DECL_UNIT (decl)->automaton_name != NULL)
		{
		  automaton_decl
                    = find_automaton_decl (DECL_UNIT (decl)->automaton_name);
		  if (automaton_decl == NULL)
		    error ("automaton `%s' is not declared",
			   DECL_UNIT (decl)->automaton_name);
		  else
		    {
		      DECL_AUTOMATON (automaton_decl)->automaton_is_used = 1;
		      DECL_UNIT (decl)->automaton_decl
			= DECL_AUTOMATON (automaton_decl);
		    }
		}
	      else if (automaton_presence)
		error ("define_unit `%s' without automaton when one defined",
		       DECL_UNIT (decl)->name);
	      DECL_UNIT (decl)->unit_num = description->units_num;
	      description->units_num++;
	      if (strcmp (DECL_UNIT (decl)->name, NOTHING_NAME) == 0)
		{
		  error ("`%s' is declared as cpu unit", NOTHING_NAME);
		  continue;
		}
	      decl_in_table = find_decl (DECL_UNIT (decl)->name);
	    }
	  else
	    {
	      if (strcmp (DECL_RESERV (decl)->name, NOTHING_NAME) == 0)
		{
		  error ("`%s' is declared as cpu reservation", NOTHING_NAME);
		  continue;
		}
	      decl_in_table = find_decl (DECL_RESERV (decl)->name);
	    }
	  if (decl_in_table == NULL)
	    decl_in_table = insert_decl (decl);
	  else
	    {
	      if (decl->mode == dm_unit)
		error ("repeated declaration of unit `%s'",
		       DECL_UNIT (decl)->name);
	      else
		error ("repeated declaration of reservation `%s'",
		       DECL_RESERV (decl)->name);
	    }
	}
    }
  /* Check bypasses and form list of bypasses for each (output)
     insn.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_bypass)
	process_bypass (decl);
    }

  /* Check exclusion set declarations and form exclusion sets.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_excl)
	{
	  unit_set_el_t unit_set_el_list;
	  unit_set_el_t unit_set_el_list_2;

	  unit_set_el_list
            = process_excls (DECL_EXCL (decl)->names,
			     DECL_EXCL (decl)->first_list_length, decl->pos);
	  unit_set_el_list_2
	    = process_excls (&DECL_EXCL (decl)->names
			     [DECL_EXCL (decl)->first_list_length],
                             DECL_EXCL (decl)->all_names_num
                             - DECL_EXCL (decl)->first_list_length,
                             decl->pos);
	  add_excls (unit_set_el_list, unit_set_el_list_2, decl->pos);
	  add_excls (unit_set_el_list_2, unit_set_el_list, decl->pos);
	}
    }

  /* Check presence set declarations and form presence sets.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_presence)
	{
	  unit_set_el_t unit_set_el_list;
	  pattern_set_el_t pattern_set_el_list;

	  unit_set_el_list
            = process_presence_absence_names
	      (DECL_PRESENCE (decl)->names, DECL_PRESENCE (decl)->names_num,
	       decl->pos, TRUE, DECL_PRESENCE (decl)->final_p);
	  pattern_set_el_list
	    = process_presence_absence_patterns
	      (DECL_PRESENCE (decl)->patterns,
	       DECL_PRESENCE (decl)->patterns_num,
	       decl->pos, TRUE, DECL_PRESENCE (decl)->final_p);
	  add_presence_absence (unit_set_el_list, pattern_set_el_list,
				decl->pos, TRUE,
				DECL_PRESENCE (decl)->final_p);
	}
    }

  /* Check absence set declarations and form absence sets.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_absence)
	{
	  unit_set_el_t unit_set_el_list;
	  pattern_set_el_t pattern_set_el_list;

	  unit_set_el_list
            = process_presence_absence_names
	      (DECL_ABSENCE (decl)->names, DECL_ABSENCE (decl)->names_num,
	       decl->pos, FALSE, DECL_ABSENCE (decl)->final_p);
	  pattern_set_el_list
	    = process_presence_absence_patterns
	      (DECL_ABSENCE (decl)->patterns,
	       DECL_ABSENCE (decl)->patterns_num,
	       decl->pos, FALSE, DECL_ABSENCE (decl)->final_p);
	  add_presence_absence (unit_set_el_list, pattern_set_el_list,
				decl->pos, FALSE,
				DECL_ABSENCE (decl)->final_p);
	}
    }
}

/* The following function checks that declared automaton is used.  If
   the automaton is not used, the function fixes error/warning.  The
   following function must be called only after `process_decls'.  */
static void
check_automaton_usage (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_automaton
	  && !DECL_AUTOMATON (decl)->automaton_is_used)
	{
	  if (!w_flag)
	    error ("automaton `%s' is not used", DECL_AUTOMATON (decl)->name);
	  else
	    warning ("automaton `%s' is not used",
		     DECL_AUTOMATON (decl)->name);
	}
    }
}

/* The following recursive function processes all regexp in order to
   fix usage of units or reservations and to fix errors of undeclared
   name.  The function may change unit_regexp onto reserv_regexp.
   Remember that reserv_regexp does not exist before the function
   call.  */
static regexp_t
process_regexp (regexp_t regexp)
{
  decl_t decl_in_table;
  regexp_t new_regexp;
  int i;

  switch (regexp->mode)
    {
    case rm_unit:
      decl_in_table = find_decl (REGEXP_UNIT (regexp)->name);
      if (decl_in_table == NULL)
        error ("undeclared unit or reservation `%s'",
	       REGEXP_UNIT (regexp)->name);
      else
	switch (decl_in_table->mode)
	  {
	  case dm_unit:
	    DECL_UNIT (decl_in_table)->unit_is_used = 1;
	    REGEXP_UNIT (regexp)->unit_decl = DECL_UNIT (decl_in_table);
	    break;

	  case dm_reserv:
	    DECL_RESERV (decl_in_table)->reserv_is_used = 1;
	    new_regexp = XCREATENODE (struct regexp);
	    new_regexp->mode = rm_reserv;
	    new_regexp->pos = regexp->pos;
	    REGEXP_RESERV (new_regexp)->name = REGEXP_UNIT (regexp)->name;
	    REGEXP_RESERV (new_regexp)->reserv_decl
	      = DECL_RESERV (decl_in_table);
	    regexp = new_regexp;
	    break;

	  default:
	    gcc_unreachable ();
	}
      break;
    case rm_sequence:
      for (i = 0; i <REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	REGEXP_SEQUENCE (regexp)->regexps [i]
	  = process_regexp (REGEXP_SEQUENCE (regexp)->regexps [i]);
      break;
    case rm_allof:
      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	REGEXP_ALLOF (regexp)->regexps [i]
	  = process_regexp (REGEXP_ALLOF (regexp)->regexps [i]);
      break;
    case rm_oneof:
      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	REGEXP_ONEOF (regexp)->regexps [i]
	  = process_regexp (REGEXP_ONEOF (regexp)->regexps [i]);
      break;
    case rm_repeat:
      REGEXP_REPEAT (regexp)->regexp
	= process_regexp (REGEXP_REPEAT (regexp)->regexp);
      break;
    case rm_nothing:
      break;
    default:
      gcc_unreachable ();
    }
  return regexp;
}

/* The following function processes regexp of define_reservation and
   define_insn_reservation with the aid of function
   `process_regexp'.  */
static void
process_regexp_decls (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_reserv)
	DECL_RESERV (decl)->regexp
	  = process_regexp (DECL_RESERV (decl)->regexp);
      else if (decl->mode == dm_insn_reserv)
	DECL_INSN_RESERV (decl)->regexp
	  = process_regexp (DECL_INSN_RESERV (decl)->regexp);
    }
}

/* The following function checks that declared unit is used.  If the
   unit is not used, the function fixes errors/warnings.  The
   following function must be called only after `process_decls',
   `process_regexp_decls'.  */
static void
check_usage (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit && !DECL_UNIT (decl)->unit_is_used)
	{
	  if (!w_flag)
	    error ("unit `%s' is not used", DECL_UNIT (decl)->name);
	  else
	    warning ("unit `%s' is not used", DECL_UNIT (decl)->name);
	}
      else if (decl->mode == dm_reserv && !DECL_RESERV (decl)->reserv_is_used)
	{
	  if (!w_flag)
	    error ("reservation `%s' is not used", DECL_RESERV (decl)->name);
	  else
	    warning ("reservation `%s' is not used", DECL_RESERV (decl)->name);
	}
    }
}

/* The following variable value is number of reservation being
   processed on loop recognition.  */
static int curr_loop_pass_num;

/* The following recursive function returns nonzero value if REGEXP
   contains given decl or reservations in given regexp refers for
   given decl.  */
static int
loop_in_regexp (regexp_t regexp, decl_t start_decl)
{
  int i;

  if (regexp == NULL)
    return 0;
  switch (regexp->mode)
    {
      case rm_unit:
	return 0;

    case rm_reserv:
      if (start_decl->mode == dm_reserv
          && REGEXP_RESERV (regexp)->reserv_decl == DECL_RESERV (start_decl))
        return 1;
      else if (REGEXP_RESERV (regexp)->reserv_decl->loop_pass_num
	       == curr_loop_pass_num)
        /* declaration has been processed.  */
        return 0;
      else
        {
	  REGEXP_RESERV (regexp)->reserv_decl->loop_pass_num
            = curr_loop_pass_num;
          return loop_in_regexp (REGEXP_RESERV (regexp)->reserv_decl->regexp,
                                 start_decl);
        }

    case rm_sequence:
      for (i = 0; i <REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	if (loop_in_regexp (REGEXP_SEQUENCE (regexp)->regexps [i], start_decl))
	  return 1;
      return 0;

    case rm_allof:
      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	if (loop_in_regexp (REGEXP_ALLOF (regexp)->regexps [i], start_decl))
	  return 1;
      return 0;

    case rm_oneof:
      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	if (loop_in_regexp (REGEXP_ONEOF (regexp)->regexps [i], start_decl))
	  return 1;
      return 0;

    case rm_repeat:
      return loop_in_regexp (REGEXP_REPEAT (regexp)->regexp, start_decl);

    case rm_nothing:
      return 0;

    default:
      gcc_unreachable ();
    }
}

/* The following function fixes errors "cycle in definition ...".  The
   function uses function `loop_in_regexp' for that.  */
static void
check_loops_in_regexps (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_reserv)
	DECL_RESERV (decl)->loop_pass_num = 0;
    }
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      curr_loop_pass_num = i;

      if (decl->mode == dm_reserv)
	  {
	    DECL_RESERV (decl)->loop_pass_num = curr_loop_pass_num;
	    if (loop_in_regexp (DECL_RESERV (decl)->regexp, decl))
	      {
		gcc_assert (DECL_RESERV (decl)->regexp);
		error ("cycle in definition of reservation `%s'",
		       DECL_RESERV (decl)->name);
	      }
	  }
    }
}

/* The function recursively processes IR of reservation and defines
   max and min cycle for reservation of unit.  */
static void
process_regexp_cycles (regexp_t regexp, int max_start_cycle,
		       int min_start_cycle, int *max_finish_cycle,
		       int *min_finish_cycle)
{
  int i;

  switch (regexp->mode)
    {
    case rm_unit:
      if (REGEXP_UNIT (regexp)->unit_decl->max_occ_cycle_num < max_start_cycle)
	REGEXP_UNIT (regexp)->unit_decl->max_occ_cycle_num = max_start_cycle;
      if (REGEXP_UNIT (regexp)->unit_decl->min_occ_cycle_num > min_start_cycle
	  || REGEXP_UNIT (regexp)->unit_decl->min_occ_cycle_num == -1)
	REGEXP_UNIT (regexp)->unit_decl->min_occ_cycle_num = min_start_cycle;
      *max_finish_cycle = max_start_cycle;
      *min_finish_cycle = min_start_cycle;
      break;

    case rm_reserv:
      process_regexp_cycles (REGEXP_RESERV (regexp)->reserv_decl->regexp,
			     max_start_cycle, min_start_cycle,
			     max_finish_cycle, min_finish_cycle);
      break;

    case rm_repeat:
      for (i = 0; i < REGEXP_REPEAT (regexp)->repeat_num; i++)
	{
	  process_regexp_cycles (REGEXP_REPEAT (regexp)->regexp,
				 max_start_cycle, min_start_cycle,
				 max_finish_cycle, min_finish_cycle);
	  max_start_cycle = *max_finish_cycle + 1;
	  min_start_cycle = *min_finish_cycle + 1;
	}
      break;

    case rm_sequence:
      for (i = 0; i <REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	{
	  process_regexp_cycles (REGEXP_SEQUENCE (regexp)->regexps [i],
				 max_start_cycle, min_start_cycle,
				 max_finish_cycle, min_finish_cycle);
	  max_start_cycle = *max_finish_cycle + 1;
	  min_start_cycle = *min_finish_cycle + 1;
	}
      break;

    case rm_allof:
      {
	int max_cycle = 0;
	int min_cycle = 0;

	for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	  {
	    process_regexp_cycles (REGEXP_ALLOF (regexp)->regexps [i],
				   max_start_cycle, min_start_cycle,
				   max_finish_cycle, min_finish_cycle);
	    if (max_cycle < *max_finish_cycle)
	      max_cycle = *max_finish_cycle;
	    if (i == 0 || min_cycle > *min_finish_cycle)
	      min_cycle = *min_finish_cycle;
	  }
	*max_finish_cycle = max_cycle;
	*min_finish_cycle = min_cycle;
      }
      break;

    case rm_oneof:
      {
	int max_cycle = 0;
	int min_cycle = 0;

	for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	  {
	    process_regexp_cycles (REGEXP_ONEOF (regexp)->regexps [i],
				   max_start_cycle, min_start_cycle,
				   max_finish_cycle, min_finish_cycle);
	    if (max_cycle < *max_finish_cycle)
	      max_cycle = *max_finish_cycle;
	    if (i == 0 || min_cycle > *min_finish_cycle)
	      min_cycle = *min_finish_cycle;
	  }
	*max_finish_cycle = max_cycle;
	*min_finish_cycle = min_cycle;
      }
      break;

    case rm_nothing:
      *max_finish_cycle = max_start_cycle;
      *min_finish_cycle = min_start_cycle;
      break;

    default:
      gcc_unreachable ();
    }
}

/* The following function is called only for correct program.  The
   function defines max reservation of insns in cycles.  */
static void
evaluate_max_reserv_cycles (void)
{
  int max_insn_cycles_num;
  int min_insn_cycles_num;
  decl_t decl;
  int i;

  description->max_insn_reserv_cycles = 0;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
      {
        process_regexp_cycles (DECL_INSN_RESERV (decl)->regexp, 0, 0,
			       &max_insn_cycles_num, &min_insn_cycles_num);
        if (description->max_insn_reserv_cycles < max_insn_cycles_num)
	  description->max_insn_reserv_cycles = max_insn_cycles_num;
      }
    }
  description->max_insn_reserv_cycles++;
}

/* The following function calls functions for checking all
   description.  */
static void
check_all_description (void)
{
  process_decls ();
  check_automaton_usage ();
  process_regexp_decls ();
  check_usage ();
  check_loops_in_regexps ();
  if (!have_error)
    evaluate_max_reserv_cycles ();
}



/* The page contains abstract data `ticker'.  This data is used to
   report time of different phases of building automata.  It is
   possibly to write a description for which automata will be built
   during several minutes even on fast machine.  */

/* The following function creates ticker and makes it active.  */
static ticker_t
create_ticker (void)
{
  ticker_t ticker;

  ticker.modified_creation_time = get_run_time ();
  ticker.incremented_off_time = 0;
  return ticker;
}

/* The following function switches off given ticker.  */
static void
ticker_off (ticker_t *ticker)
{
  if (ticker->incremented_off_time == 0)
    ticker->incremented_off_time = get_run_time () + 1;
}

/* The following function switches on given ticker.  */
static void
ticker_on (ticker_t *ticker)
{
  if (ticker->incremented_off_time != 0)
    {
      ticker->modified_creation_time
        += get_run_time () - ticker->incremented_off_time + 1;
      ticker->incremented_off_time = 0;
    }
}

/* The following function returns current time in milliseconds since
   the moment when given ticker was created.  */
static int
active_time (ticker_t ticker)
{
  if (ticker.incremented_off_time != 0)
    return ticker.incremented_off_time - 1 - ticker.modified_creation_time;
  else
    return get_run_time () - ticker.modified_creation_time;
}

/* The following function returns string representation of active time
   of given ticker.  The result is string representation of seconds
   with accuracy of 1/100 second.  Only result of the last call of the
   function exists.  Therefore the following code is not correct

      printf ("parser time: %s\ngeneration time: %s\n",
              active_time_string (parser_ticker),
              active_time_string (generation_ticker));

   Correct code has to be the following

      printf ("parser time: %s\n", active_time_string (parser_ticker));
      printf ("generation time: %s\n",
              active_time_string (generation_ticker));

*/
static void
print_active_time (FILE *f, ticker_t ticker)
{
  int msecs;

  msecs = active_time (ticker);
  fprintf (f, "%d.%06d", msecs / 1000000, msecs % 1000000);
}



/* The following variable value is number of automaton which are
   really being created.  This value is defined on the base of
   argument of option `-split'.  If the variable has zero value the
   number of automata is defined by the constructions `%automaton'.
   This case occurs when option `-split' is absent or has zero
   argument.  If constructions `define_automaton' is absent only one
   automaton is created.  */
static int automata_num;

/* The following variable values are times of
       o transformation of regular expressions
       o building NDFA (DFA if !ndfa_flag)
       o NDFA -> DFA   (simply the same automaton if !ndfa_flag)
       o DFA minimization
       o building insn equivalence classes
       o all previous ones
       o code output */
static ticker_t transform_time;
static ticker_t NDFA_time;
static ticker_t NDFA_to_DFA_time;
static ticker_t minimize_time;
static ticker_t equiv_time;
static ticker_t automaton_generation_time;
static ticker_t output_time;

/* The following variable values are times of
       all checking
       all generation
       all pipeline hazard translator work */
static ticker_t check_time;
static ticker_t generation_time;
static ticker_t all_time;



/* Pseudo insn decl which denotes advancing cycle.  */
static decl_t advance_cycle_insn_decl;
/* Pseudo insn decl which denotes collapsing the NDFA state.  */
static decl_t collapse_ndfa_insn_decl;

/* Create and record a decl for the special advance-cycle transition.  */
static void
add_advance_cycle_insn_decl (void)
{
  advance_cycle_insn_decl = XCREATENODE (struct decl);
  advance_cycle_insn_decl->mode = dm_insn_reserv;
  advance_cycle_insn_decl->pos = no_pos;
  DECL_INSN_RESERV (advance_cycle_insn_decl)->regexp = NULL;
  DECL_INSN_RESERV (advance_cycle_insn_decl)->name = "$advance_cycle";
  DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num
    = description->insns_num;
  description->decls [description->decls_num] = advance_cycle_insn_decl;
  description->decls_num++;
  description->insns_num++;
}

/* Create and record a decl for the special collapse-NDFA transition.  */
static void
add_collapse_ndfa_insn_decl (void)
{
  collapse_ndfa_insn_decl = XCREATENODE (struct decl);
  collapse_ndfa_insn_decl->mode = dm_insn_reserv;
  collapse_ndfa_insn_decl->pos = no_pos;
  DECL_INSN_RESERV (collapse_ndfa_insn_decl)->regexp = NULL;
  DECL_INSN_RESERV (collapse_ndfa_insn_decl)->name = "$collapse_ndfa";
  DECL_INSN_RESERV (collapse_ndfa_insn_decl)->insn_num
    = description->insns_num;
  description->decls [description->decls_num] = collapse_ndfa_insn_decl;
  description->decls_num++;
  description->insns_num++;
}

/* True if DECL is either of the two special decls we created.  */
static bool
special_decl_p (struct insn_reserv_decl *decl)
{
  return (decl == DECL_INSN_RESERV (advance_cycle_insn_decl)
	  || (collapse_flag
	      && decl == DECL_INSN_RESERV (collapse_ndfa_insn_decl)));
}


/* Abstract data `alternative states' which represents
   nondeterministic nature of the description (see comments for
   structures alt_state and state).  */

/* List of free states.  */
static alt_state_t first_free_alt_state;

#ifndef NDEBUG
/* The following variables is maximal number of allocated nodes
   alt_state.  */
static int allocated_alt_states_num = 0;
#endif

/* The following function returns free node alt_state.  It may be new
   allocated node or node freed earlier.  */
static alt_state_t
get_free_alt_state (void)
{
  alt_state_t result;

  if (first_free_alt_state != NULL)
    {
      result = first_free_alt_state;
      first_free_alt_state = first_free_alt_state->next_alt_state;
    }
  else
    {
#ifndef NDEBUG
      allocated_alt_states_num++;
#endif
      result = XCREATENODE (struct alt_state);
    }
  result->state = NULL;
  result->next_alt_state = NULL;
  result->next_sorted_alt_state = NULL;
  return result;
}

/* The function frees node ALT_STATE.  */
static void
free_alt_state (alt_state_t alt_state)
{
  if (alt_state == NULL)
    return;
  alt_state->next_alt_state = first_free_alt_state;
  first_free_alt_state = alt_state;
}

/* The function frees list started with node ALT_STATE_LIST.  */
static void
free_alt_states (alt_state_t alt_states_list)
{
  alt_state_t curr_alt_state;
  alt_state_t next_alt_state;

  for (curr_alt_state = alt_states_list;
       curr_alt_state != NULL;
       curr_alt_state = next_alt_state)
    {
      next_alt_state = curr_alt_state->next_alt_state;
      free_alt_state (curr_alt_state);
    }
}

/* The function compares unique numbers of alt states.  */
static int
alt_state_cmp (const void *alt_state_ptr_1, const void *alt_state_ptr_2)
{
  if ((*(const alt_state_t *) alt_state_ptr_1)->state->unique_num
      == (*(const alt_state_t *) alt_state_ptr_2)->state->unique_num)
    return 0;
  else if ((*(const alt_state_t *) alt_state_ptr_1)->state->unique_num
	   < (*(const alt_state_t *) alt_state_ptr_2)->state->unique_num)
    return -1;
  else
    return 1;
}

/* The function sorts ALT_STATES_LIST and removes duplicated alt
   states from the list.  The comparison key is alt state unique
   number.  */

static alt_state_t
uniq_sort_alt_states (alt_state_t alt_states_list)
{
  alt_state_t curr_alt_state;
  size_t i;
  size_t prev_unique_state_ind;
  alt_state_t result;

  if (alt_states_list == 0)
    return 0;
  if (alt_states_list->next_alt_state == 0)
    return alt_states_list;

  auto_vec<alt_state_t, 150> alt_states;
  for (curr_alt_state = alt_states_list;
       curr_alt_state != NULL;
       curr_alt_state = curr_alt_state->next_alt_state)
    alt_states.safe_push (curr_alt_state);

  alt_states.qsort (alt_state_cmp);

  prev_unique_state_ind = 0;
  for (i = 1; i < alt_states.length (); i++)
    if (alt_states[prev_unique_state_ind]->state != alt_states[i]->state)
      {
	prev_unique_state_ind++;
	alt_states[prev_unique_state_ind] = alt_states[i];
      }
  alt_states.truncate (prev_unique_state_ind + 1);

  for (i = 1; i < alt_states.length (); i++)
    alt_states[i-1]->next_sorted_alt_state
      = alt_states[i];
  alt_states.last ()->next_sorted_alt_state = 0;

  result = alt_states[0];

  return result;
}

/* The function checks equality of alt state lists.  Remember that the
   lists must be already sorted by the previous function.  */
static int
alt_states_eq (alt_state_t alt_states_1, alt_state_t alt_states_2)
{
  while (alt_states_1 != NULL && alt_states_2 != NULL
         && alt_state_cmp (&alt_states_1, &alt_states_2) == 0)
    {
      alt_states_1 = alt_states_1->next_sorted_alt_state;
      alt_states_2 = alt_states_2->next_sorted_alt_state;
    }
  return alt_states_1 == alt_states_2;
}

/* Initialization of the abstract data.  */
static void
initiate_alt_states (void)
{
  first_free_alt_state = NULL;
}

/* Finishing work with the abstract data.  */
static void
finish_alt_states (void)
{
}



/* The page contains macros for work with bits strings.  We could use
   standard gcc bitmap or sbitmap but it would result in difficulties
   of building canadian cross.  */

/* Set bit number bitno in the bit string.  The macro is not side
   effect proof.  */
#define bitmap_set_bit(bitstring, bitno)					  \
  ((bitstring)[(bitno) / (sizeof (*(bitstring)) * CHAR_BIT)] |=		  \
	(HOST_WIDE_INT)1 << (bitno) % (sizeof (*(bitstring)) * CHAR_BIT))

#define CLEAR_BIT(bitstring, bitno)					  \
  ((bitstring)[(bitno) / (sizeof (*(bitstring)) * CHAR_BIT)] &=		  \
	~((HOST_WIDE_INT)1 << (bitno) % (sizeof (*(bitstring)) * CHAR_BIT)))

/* Test if bit number bitno in the bitstring is set.  The macro is not
   side effect proof.  */
#define bitmap_bit_p(bitstring, bitno)					  \
  ((bitstring)[(bitno) / (sizeof (*(bitstring)) * CHAR_BIT)] >>		  \
	(bitno) % (sizeof (*(bitstring)) * CHAR_BIT) & 1)



/* This page contains abstract data `state'.  */

/* Maximal length of reservations in cycles (>= 1).  */
static int max_cycles_num;

/* Number of set elements (see type set_el_t) needed for
   representation of one cycle reservation.  It is depended on units
   number.  */
static int els_in_cycle_reserv;

/* Number of set elements (see type set_el_t) needed for
   representation of maximal length reservation.  Deterministic
   reservation is stored as set (bit string) of length equal to the
   variable value * number of bits in set_el_t.  */
static int els_in_reservs;

/* Array of pointers to unit declarations.  */
static unit_decl_t *units_array;

/* Temporary reservation of maximal length.  */
static reserv_sets_t temp_reserv;

/* The state table itself is represented by the following variable.  */
static htab_t state_table;

/* Linked list of free 'state' structures to be recycled.  The
   next_equiv_class_state pointer is borrowed for a free list.  */
static state_t first_free_state;

static int curr_unique_state_num;

#ifndef NDEBUG
/* The following variables is maximal number of allocated nodes
   `state'.  */
static int allocated_states_num = 0;
#endif

/* Allocate new reservation set.  */
static reserv_sets_t
alloc_empty_reserv_sets (void)
{
  reserv_sets_t result;

  obstack_blank (&irp, els_in_reservs * sizeof (set_el_t));
  result = (reserv_sets_t) obstack_base (&irp);
  obstack_finish (&irp);
  memset (result, 0, els_in_reservs * sizeof (set_el_t));
  return result;
}

/* Hash value of reservation set.  */
static unsigned
reserv_sets_hash_value (reserv_sets_t reservs)
{
  set_el_t hash_value;
  unsigned result;
  int reservs_num, i;
  set_el_t *reserv_ptr;

  hash_value = 0;
  reservs_num = els_in_reservs;
  reserv_ptr = reservs;
  i = 0;
  while (reservs_num != 0)
    {
      reservs_num--;
      hash_value += ((*reserv_ptr >> i)
		     | (*reserv_ptr << (((sizeof (set_el_t) * CHAR_BIT) - 1) & -i)));
      i++;
      if (i == sizeof (set_el_t) * CHAR_BIT)
	i = 0;
      reserv_ptr++;
    }
  if (sizeof (set_el_t) <= sizeof (unsigned))
    return hash_value;
  result = 0;
  for (i = sizeof (set_el_t); i > 0; i -= sizeof (unsigned) - 1)
    {
      result += (unsigned) hash_value;
      hash_value >>= (sizeof (unsigned) - 1) * CHAR_BIT;
    }
  return result;
}

/* Comparison of given reservation sets.  */
static int
reserv_sets_cmp (const_reserv_sets_t reservs_1, const_reserv_sets_t reservs_2)
{
  int reservs_num;
  const set_el_t *reserv_ptr_1;
  const set_el_t *reserv_ptr_2;

  gcc_assert (reservs_1 && reservs_2);
  reservs_num = els_in_reservs;
  reserv_ptr_1 = reservs_1;
  reserv_ptr_2 = reservs_2;
  while (reservs_num != 0 && *reserv_ptr_1 == *reserv_ptr_2)
    {
      reservs_num--;
      reserv_ptr_1++;
      reserv_ptr_2++;
    }
  if (reservs_num == 0)
    return 0;
  else if (*reserv_ptr_1 < *reserv_ptr_2)
    return -1;
  else
    return 1;
}

/* The function checks equality of the reservation sets.  */
static int
reserv_sets_eq (const_reserv_sets_t reservs_1, const_reserv_sets_t reservs_2)
{
  return reserv_sets_cmp (reservs_1, reservs_2) == 0;
}

/* Set up in the reservation set that unit with UNIT_NUM is used on
   CYCLE_NUM.  */
static void
set_unit_reserv (reserv_sets_t reservs, int cycle_num, int unit_num)
{
  gcc_assert (cycle_num < max_cycles_num);
  bitmap_set_bit (reservs, cycle_num * els_in_cycle_reserv
           * sizeof (set_el_t) * CHAR_BIT + unit_num);
}

/* Set up in the reservation set RESERVS that unit with UNIT_NUM is
   used on CYCLE_NUM.  */
static int
test_unit_reserv (reserv_sets_t reservs, int cycle_num, int unit_num)
{
  gcc_assert (cycle_num < max_cycles_num);
  return bitmap_bit_p (reservs, cycle_num * els_in_cycle_reserv
		   * sizeof (set_el_t) * CHAR_BIT + unit_num);
}

/* The function checks that the reservation sets are intersected,
   i.e. there is a unit reservation on a cycle in both reservation
   sets.  */
static int
reserv_sets_are_intersected (reserv_sets_t operand_1,
			     reserv_sets_t operand_2)
{
  set_el_t *el_ptr_1;
  set_el_t *el_ptr_2;
  set_el_t *cycle_ptr_1;
  set_el_t *cycle_ptr_2;

  gcc_assert (operand_1 && operand_2);
  for (el_ptr_1 = operand_1, el_ptr_2 = operand_2;
       el_ptr_1 < operand_1 + els_in_reservs;
       el_ptr_1++, el_ptr_2++)
    if (*el_ptr_1 & *el_ptr_2)
      return 1;
  reserv_sets_or (temp_reserv, operand_1, operand_2);
  for (cycle_ptr_1 = operand_1, cycle_ptr_2 = operand_2;
       cycle_ptr_1 < operand_1 + els_in_reservs;
       cycle_ptr_1 += els_in_cycle_reserv, cycle_ptr_2 += els_in_cycle_reserv)
    {
      for (el_ptr_1 = cycle_ptr_1, el_ptr_2 = get_excl_set (cycle_ptr_2);
	   el_ptr_1 < cycle_ptr_1 + els_in_cycle_reserv;
	   el_ptr_1++, el_ptr_2++)
	if (*el_ptr_1 & *el_ptr_2)
	  return 1;
      if (!check_presence_pattern_sets (cycle_ptr_1, cycle_ptr_2, FALSE))
	return 1;
      if (!check_presence_pattern_sets (temp_reserv + (cycle_ptr_2
						       - operand_2),
					cycle_ptr_2, TRUE))
	return 1;
      if (!check_absence_pattern_sets (cycle_ptr_1, cycle_ptr_2, FALSE))
	return 1;
      if (!check_absence_pattern_sets (temp_reserv + (cycle_ptr_2 - operand_2),
				       cycle_ptr_2, TRUE))
	return 1;
    }
  return 0;
}

/* The function sets up RESULT bits by bits of OPERAND shifted on one
   cpu cycle.  The remaining bits of OPERAND (representing the last
   cycle unit reservations) are not changed.  */
static void
reserv_sets_shift (reserv_sets_t result, reserv_sets_t operand)
{
  int i;

  gcc_assert (result && operand && result != operand);
  for (i = els_in_cycle_reserv; i < els_in_reservs; i++)
    result [i - els_in_cycle_reserv] = operand [i];
}

/* OR of the reservation sets.  */
static void
reserv_sets_or (reserv_sets_t result, reserv_sets_t operand_1,
		reserv_sets_t operand_2)
{
  set_el_t *el_ptr_1;
  set_el_t *el_ptr_2;
  set_el_t *result_set_el_ptr;

  gcc_assert (result && operand_1 && operand_2);
  for (el_ptr_1 = operand_1, el_ptr_2 = operand_2, result_set_el_ptr = result;
       el_ptr_1 < operand_1 + els_in_reservs;
       el_ptr_1++, el_ptr_2++, result_set_el_ptr++)
    *result_set_el_ptr = *el_ptr_1 | *el_ptr_2;
}

/* AND of the reservation sets.  */
static void
reserv_sets_and (reserv_sets_t result, reserv_sets_t operand_1,
		reserv_sets_t operand_2)
{
  set_el_t *el_ptr_1;
  set_el_t *el_ptr_2;
  set_el_t *result_set_el_ptr;

  gcc_assert (result && operand_1 && operand_2);
  for (el_ptr_1 = operand_1, el_ptr_2 = operand_2, result_set_el_ptr = result;
       el_ptr_1 < operand_1 + els_in_reservs;
       el_ptr_1++, el_ptr_2++, result_set_el_ptr++)
    *result_set_el_ptr = *el_ptr_1 & *el_ptr_2;
}

/* The function outputs string representation of units reservation on
   cycle START_CYCLE in the reservation set.  The function uses repeat
   construction if REPETITION_NUM > 1.  */
static void
output_cycle_reservs (FILE *f, reserv_sets_t reservs, int start_cycle,
		      int repetition_num)
{
  int unit_num;
  int reserved_units_num;

  reserved_units_num = 0;
  for (unit_num = 0; unit_num < description->units_num; unit_num++)
    if (bitmap_bit_p (reservs, start_cycle * els_in_cycle_reserv
                  * sizeof (set_el_t) * CHAR_BIT + unit_num))
      reserved_units_num++;
  gcc_assert (repetition_num > 0);
  if (repetition_num != 1 && reserved_units_num > 1)
    fprintf (f, "(");
  reserved_units_num = 0;
  for (unit_num = 0;
       unit_num < description->units_num;
       unit_num++)
    if (bitmap_bit_p (reservs, start_cycle * els_in_cycle_reserv
		  * sizeof (set_el_t) * CHAR_BIT + unit_num))
      {
        if (reserved_units_num != 0)
          fprintf (f, "+");
        reserved_units_num++;
        fprintf (f, "%s", units_array [unit_num]->name);
      }
  if (reserved_units_num == 0)
    fprintf (f, NOTHING_NAME);
  gcc_assert (repetition_num > 0);
  if (repetition_num != 1 && reserved_units_num > 1)
    fprintf (f, ")");
  if (repetition_num != 1)
    fprintf (f, "*%d", repetition_num);
}

/* The function outputs string representation of units reservation in
   the reservation set.  */
static void
output_reserv_sets (FILE *f, reserv_sets_t reservs)
{
  int start_cycle = 0;
  int cycle;
  int repetition_num;

  repetition_num = 0;
  for (cycle = 0; cycle < max_cycles_num; cycle++)
    if (repetition_num == 0)
      {
        repetition_num++;
        start_cycle = cycle;
      }
    else if (memcmp
             ((char *) reservs + start_cycle * els_in_cycle_reserv
	      * sizeof (set_el_t),
              (char *) reservs + cycle * els_in_cycle_reserv
	      * sizeof (set_el_t),
	      els_in_cycle_reserv * sizeof (set_el_t)) == 0)
      repetition_num++;
    else
      {
        if (start_cycle != 0)
          fprintf (f, ", ");
        output_cycle_reservs (f, reservs, start_cycle, repetition_num);
        repetition_num = 1;
        start_cycle = cycle;
      }
  if (start_cycle < max_cycles_num)
    {
      if (start_cycle != 0)
        fprintf (f, ", ");
      output_cycle_reservs (f, reservs, start_cycle, repetition_num);
    }
}

/* The following function returns free node state for AUTOMATON.  It
   may be new allocated node or node freed earlier.  The function also
   allocates reservation set if WITH_RESERVS has nonzero value.  */
static state_t
get_free_state (int with_reservs, automaton_t automaton)
{
  state_t result;

  gcc_assert (max_cycles_num > 0 && automaton);
  if (first_free_state)
    {
      result = first_free_state;
      first_free_state = result->next_equiv_class_state;

      result->next_equiv_class_state = NULL;
      result->automaton = automaton;
      result->first_out_arc = NULL;
      result->it_was_placed_in_stack_for_NDFA_forming = 0;
      result->it_was_placed_in_stack_for_DFA_forming = 0;
      result->component_states = NULL;
    }
  else
    {
#ifndef NDEBUG
      allocated_states_num++;
#endif
      result = XCREATENODE (struct state);
      result->automaton = automaton;
      result->first_out_arc = NULL;
      result->unique_num = curr_unique_state_num;
      curr_unique_state_num++;
    }
  if (with_reservs)
    {
      if (result->reservs == NULL)
        result->reservs = alloc_empty_reserv_sets ();
      else
        memset (result->reservs, 0, els_in_reservs * sizeof (set_el_t));
    }
  return result;
}

/* The function frees node STATE.  */
static void
free_state (state_t state)
{
  free_alt_states (state->component_states);
  state->next_equiv_class_state = first_free_state;
  first_free_state = state;
}

/* Hash value of STATE.  If STATE represents deterministic state it is
   simply hash value of the corresponding reservation set.  Otherwise
   it is formed from hash values of the component deterministic
   states.  One more key is order number of state automaton.  */
static hashval_t
state_hash (const void *state)
{
  unsigned int hash_value;
  alt_state_t alt_state;

  if (((const_state_t) state)->component_states == NULL)
    hash_value = reserv_sets_hash_value (((const_state_t) state)->reservs);
  else
    {
      hash_value = 0;
      for (alt_state = ((const_state_t) state)->component_states;
           alt_state != NULL;
           alt_state = alt_state->next_sorted_alt_state)
        hash_value = (((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
                       | (hash_value << CHAR_BIT))
                      + alt_state->state->unique_num);
    }
  hash_value = (((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
                 | (hash_value << CHAR_BIT))
                + ((const_state_t) state)->automaton->automaton_order_num);
  return hash_value;
}

/* Return nonzero value if the states are the same.  */
static int
state_eq_p (const void *state_1, const void *state_2)
{
  alt_state_t alt_state_1;
  alt_state_t alt_state_2;

  if (((const_state_t) state_1)->automaton != ((const_state_t) state_2)->automaton)
    return 0;
  else if (((const_state_t) state_1)->component_states == NULL
           && ((const_state_t) state_2)->component_states == NULL)
    return reserv_sets_eq (((const_state_t) state_1)->reservs,
			   ((const_state_t) state_2)->reservs);
  else if (((const_state_t) state_1)->component_states != NULL
           && ((const_state_t) state_2)->component_states != NULL)
    {
      for (alt_state_1 = ((const_state_t) state_1)->component_states,
           alt_state_2 = ((const_state_t) state_2)->component_states;
           alt_state_1 != NULL && alt_state_2 != NULL;
           alt_state_1 = alt_state_1->next_sorted_alt_state,
	   alt_state_2 = alt_state_2->next_sorted_alt_state)
        /* All state in the list must be already in the hash table.
           Also the lists must be sorted.  */
        if (alt_state_1->state != alt_state_2->state)
          return 0;
      return alt_state_1 == alt_state_2;
    }
  else
    return 0;
}

/* Insert STATE into the state table.  */
static state_t
insert_state (state_t state)
{
  void **entry_ptr;

  entry_ptr = htab_find_slot (state_table, (void *) state, INSERT);
  if (*entry_ptr == NULL)
    *entry_ptr = (void *) state;
  return (state_t) *entry_ptr;
}

/* Add reservation of unit with UNIT_NUM on cycle CYCLE_NUM to
   deterministic STATE.  */
static void
set_state_reserv (state_t state, int cycle_num, int unit_num)
{
  set_unit_reserv (state->reservs, cycle_num, unit_num);
}

/* Return nonzero value if the deterministic states contains a
   reservation of the same cpu unit on the same cpu cycle.  */
static int
intersected_state_reservs_p (state_t state1, state_t state2)
{
  gcc_assert (state1->automaton == state2->automaton);
  return reserv_sets_are_intersected (state1->reservs, state2->reservs);
}

/* Return deterministic state (inserted into the table) which
   representing the automaton state which is union of reservations of
   the deterministic states masked by RESERVS.  */
static state_t
states_union (state_t state1, state_t state2, reserv_sets_t reservs)
{
  state_t result;
  state_t state_in_table;

  gcc_assert (state1->automaton == state2->automaton);
  result = get_free_state (1, state1->automaton);
  reserv_sets_or (result->reservs, state1->reservs, state2->reservs);
  reserv_sets_and (result->reservs, result->reservs, reservs);
  state_in_table = insert_state (result);
  if (result != state_in_table)
    {
      free_state (result);
      result = state_in_table;
    }
  return result;
}

/* Return deterministic state (inserted into the table) which
   represent the automaton state is obtained from deterministic STATE
   by advancing cpu cycle and masking by RESERVS.  */
static state_t
state_shift (state_t state, reserv_sets_t reservs)
{
  state_t result;
  state_t state_in_table;

  result = get_free_state (1, state->automaton);
  reserv_sets_shift (result->reservs, state->reservs);
  reserv_sets_and (result->reservs, result->reservs, reservs);
  state_in_table = insert_state (result);
  if (result != state_in_table)
    {
      free_state (result);
      result = state_in_table;
    }
  return result;
}

/* Initialization of the abstract data.  */
static void
initiate_states (void)
{
  decl_t decl;
  int i;

  if (description->units_num)
    units_array = XNEWVEC (unit_decl_t, description->units_num);
  else
    units_array = 0;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	units_array [DECL_UNIT (decl)->unit_num] = DECL_UNIT (decl);
    }
  max_cycles_num = description->max_insn_reserv_cycles;
  els_in_cycle_reserv
    = ((description->units_num + sizeof (set_el_t) * CHAR_BIT - 1)
       / (sizeof (set_el_t) * CHAR_BIT));
  els_in_reservs = els_in_cycle_reserv * max_cycles_num;
  curr_unique_state_num = 0;
  initiate_alt_states ();
  state_table = htab_create (1500, state_hash, state_eq_p, (htab_del) 0);
  temp_reserv = alloc_empty_reserv_sets ();
}

/* Finishing work with the abstract data.  */
static void
finish_states (void)
{
  free (units_array);
  units_array = 0;
  htab_delete (state_table);
  first_free_state = NULL;
  finish_alt_states ();
}



/* Abstract data `arcs'.  */

/* List of free arcs.  */
static arc_t first_free_arc;

#ifndef NDEBUG
/* The following variables is maximal number of allocated nodes
   `arc'.  */
static int allocated_arcs_num = 0;
#endif

/* The function frees node ARC.  */
static void
free_arc (arc_t arc)
{
  arc->next_out_arc = first_free_arc;
  first_free_arc = arc;
}

/* The function removes and frees ARC staring from FROM_STATE.  */
static void
remove_arc (state_t from_state, arc_t arc)
{
  arc_t prev_arc;
  arc_t curr_arc;

  gcc_assert (arc);
  for (prev_arc = NULL, curr_arc = from_state->first_out_arc;
       curr_arc != NULL;
       prev_arc = curr_arc, curr_arc = curr_arc->next_out_arc)
    if (curr_arc == arc)
      break;
  gcc_assert (curr_arc);
  if (prev_arc == NULL)
    from_state->first_out_arc = arc->next_out_arc;
  else
    prev_arc->next_out_arc = arc->next_out_arc;
  from_state->num_out_arcs--;
  free_arc (arc);
}

/* The functions returns arc with given characteristics (or NULL if
   the arc does not exist).  */
static arc_t
find_arc (state_t from_state, state_t to_state, ainsn_t insn)
{
  arc_t arc;

  for (arc = first_out_arc (from_state); arc != NULL; arc = next_out_arc (arc))
    if (arc->insn == insn
	&& (arc->to_state == to_state
	    || (collapse_flag
		/* Any arc is good enough for a collapse-ndfa transition.  */
		&& (insn->insn_reserv_decl
		    == DECL_INSN_RESERV (collapse_ndfa_insn_decl)))))
      return arc;
  return NULL;
}

/* The function adds arc from FROM_STATE to TO_STATE marked by AINSN,
   unless such an arc already exists.  */
static void
add_arc (state_t from_state, state_t to_state, ainsn_t ainsn)
{
  arc_t new_arc;

  new_arc = find_arc (from_state, to_state, ainsn);
  if (new_arc != NULL)
    return;
  if (first_free_arc == NULL)
    {
#ifndef NDEBUG
      allocated_arcs_num++;
#endif
      new_arc = XCREATENODE (struct arc);
      new_arc->to_state = NULL;
      new_arc->insn = NULL;
      new_arc->next_out_arc = NULL;
    }
  else
    {
      new_arc = first_free_arc;
      first_free_arc =  first_free_arc->next_out_arc;
    }
  new_arc->to_state = to_state;
  new_arc->insn = ainsn;
  ainsn->arc_exists_p = 1;
  new_arc->next_out_arc = from_state->first_out_arc;
  from_state->first_out_arc = new_arc;
  from_state->num_out_arcs++;
  new_arc->next_arc_marked_by_insn = NULL;
}

/* The function returns the first arc starting from STATE.  */
static arc_t
first_out_arc (const_state_t state)
{
  return state->first_out_arc;
}

/* The function returns next out arc after ARC.  */
static arc_t
next_out_arc (arc_t arc)
{
  return arc->next_out_arc;
}

/* Initialization of the abstract data.  */
static void
initiate_arcs (void)
{
  first_free_arc = NULL;
}

/* Finishing work with the abstract data.  */
static void
finish_arcs (void)
{
}



/* Abstract data `automata lists'.  */

/* List of free states.  */
static automata_list_el_t first_free_automata_list_el;

/* The list being formed.  */
static automata_list_el_t current_automata_list;

/* Hash table of automata lists.  */
static htab_t automata_list_table;

/* The following function returns free automata list el.  It may be
   new allocated node or node freed earlier.  */
static automata_list_el_t
get_free_automata_list_el (void)
{
  automata_list_el_t result;

  if (first_free_automata_list_el != NULL)
    {
      result = first_free_automata_list_el;
      first_free_automata_list_el
	= first_free_automata_list_el->next_automata_list_el;
    }
  else
    result = XCREATENODE (struct automata_list_el);
  result->automaton = NULL;
  result->next_automata_list_el = NULL;
  return result;
}

/* The function frees node AUTOMATA_LIST_EL.  */
static void
free_automata_list_el (automata_list_el_t automata_list_el)
{
  if (automata_list_el == NULL)
    return;
  automata_list_el->next_automata_list_el = first_free_automata_list_el;
  first_free_automata_list_el = automata_list_el;
}

/* The function frees list AUTOMATA_LIST.  */
static void
free_automata_list (automata_list_el_t automata_list)
{
  automata_list_el_t curr_automata_list_el;
  automata_list_el_t next_automata_list_el;

  for (curr_automata_list_el = automata_list;
       curr_automata_list_el != NULL;
       curr_automata_list_el = next_automata_list_el)
    {
      next_automata_list_el = curr_automata_list_el->next_automata_list_el;
      free_automata_list_el (curr_automata_list_el);
    }
}

/* Hash value of AUTOMATA_LIST.  */
static hashval_t
automata_list_hash (const void *automata_list)
{
  unsigned int hash_value;
  const_automata_list_el_t curr_automata_list_el;

  hash_value = 0;
  for (curr_automata_list_el = (const_automata_list_el_t) automata_list;
       curr_automata_list_el != NULL;
       curr_automata_list_el = curr_automata_list_el->next_automata_list_el)
    hash_value = (((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
		   | (hash_value << CHAR_BIT))
		  + curr_automata_list_el->automaton->automaton_order_num);
  return hash_value;
}

/* Return nonzero value if the automata_lists are the same.  */
static int
automata_list_eq_p (const void *automata_list_1, const void *automata_list_2)
{
  const_automata_list_el_t automata_list_el_1;
  const_automata_list_el_t automata_list_el_2;

  for (automata_list_el_1 = (const_automata_list_el_t) automata_list_1,
	 automata_list_el_2 = (const_automata_list_el_t) automata_list_2;
       automata_list_el_1 != NULL && automata_list_el_2 != NULL;
       automata_list_el_1 = automata_list_el_1->next_automata_list_el,
	 automata_list_el_2 = automata_list_el_2->next_automata_list_el)
    if (automata_list_el_1->automaton != automata_list_el_2->automaton)
      return 0;
  return automata_list_el_1 == automata_list_el_2;
}

/* Initialization of the abstract data.  */
static void
initiate_automata_lists (void)
{
  first_free_automata_list_el = NULL;
  automata_list_table = htab_create (1500, automata_list_hash,
				     automata_list_eq_p, (htab_del) 0);
}

/* The following function starts new automata list and makes it the
   current one.  */
static void
automata_list_start (void)
{
  current_automata_list = NULL;
}

/* The following function adds AUTOMATON to the current list.  */
static void
automata_list_add (automaton_t automaton)
{
  automata_list_el_t el;

  el = get_free_automata_list_el ();
  el->automaton = automaton;
  el->next_automata_list_el = current_automata_list;
  current_automata_list = el;
}

/* The following function finishes forming the current list, inserts
   it into the table and returns it.  */
static automata_list_el_t
automata_list_finish (void)
{
  void **entry_ptr;

  if (current_automata_list == NULL)
    return NULL;
  entry_ptr = htab_find_slot (automata_list_table,
			      (void *) current_automata_list, INSERT);
  if (*entry_ptr == NULL)
    *entry_ptr = (void *) current_automata_list;
  else
    free_automata_list (current_automata_list);
  current_automata_list = NULL;
  return (automata_list_el_t) *entry_ptr;
}

/* Finishing work with the abstract data.  */
static void
finish_automata_lists (void)
{
  htab_delete (automata_list_table);
}



/* The page contains abstract data for work with exclusion sets (see
   exclusion_set in file rtl.def).  */

/* The following variable refers to an exclusion set returned by
   get_excl_set.  This is bit string of length equal to cpu units
   number.  If exclusion set for given unit contains 1 for a unit,
   then simultaneous reservation of the units is prohibited.  */
static reserv_sets_t excl_set;

/* The array contains exclusion sets for each unit.  */
static reserv_sets_t *unit_excl_set_table;

/* The following function forms the array containing exclusion sets
   for each unit.  */
static void
initiate_excl_sets (void)
{
  decl_t decl;
  reserv_sets_t unit_excl_set;
  unit_set_el_t el;
  int i;

  obstack_blank (&irp, els_in_cycle_reserv * sizeof (set_el_t));
  excl_set = (reserv_sets_t) obstack_base (&irp);
  obstack_finish (&irp);
  obstack_blank (&irp, description->units_num * sizeof (reserv_sets_t));
  unit_excl_set_table = (reserv_sets_t *) obstack_base (&irp);
  obstack_finish (&irp);
  /* Evaluate unit exclusion sets.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	{
	  obstack_blank (&irp, els_in_cycle_reserv * sizeof (set_el_t));
	  unit_excl_set = (reserv_sets_t) obstack_base (&irp);
	  obstack_finish (&irp);
	  memset (unit_excl_set, 0, els_in_cycle_reserv * sizeof (set_el_t));
	  for (el = DECL_UNIT (decl)->excl_list;
	       el != NULL;
	       el = el->next_unit_set_el)
	    {
	      bitmap_set_bit (unit_excl_set, el->unit_decl->unit_num);
	      el->unit_decl->in_set_p = TRUE;
	    }
          unit_excl_set_table [DECL_UNIT (decl)->unit_num] = unit_excl_set;
        }
    }
}

/* The function sets up and return EXCL_SET which is union of
   exclusion sets for each unit in IN_SET.  */
static reserv_sets_t
get_excl_set (reserv_sets_t in_set)
{
  int el;
  unsigned int i;
  int start_unit_num;
  int unit_num;

  memset (excl_set, 0, els_in_cycle_reserv * sizeof (set_el_t));
  for (el = 0; el < els_in_cycle_reserv; el++)
    if (in_set[el])
      for (i = 0; i < CHAR_BIT * sizeof (set_el_t); i++)
	if ((in_set[el] >> i) & 1)
	  {
	    start_unit_num = el * CHAR_BIT * sizeof (set_el_t) + i;
	    if (start_unit_num >= description->units_num)
	      return excl_set;
	    for (unit_num = 0; unit_num < els_in_cycle_reserv; unit_num++)
	      {
		excl_set [unit_num]
		  |= unit_excl_set_table [start_unit_num] [unit_num];
	      }
	  }
  return excl_set;
}



/* The page contains abstract data for work with presence/absence
   pattern sets (see presence_set/absence_set in file rtl.def).  */

/* The following arrays contain correspondingly presence, final
   presence, absence, and final absence patterns for each unit.  */
static pattern_reserv_t *unit_presence_set_table;
static pattern_reserv_t *unit_final_presence_set_table;
static pattern_reserv_t *unit_absence_set_table;
static pattern_reserv_t *unit_final_absence_set_table;

/* The following function forms list of reservation sets for given
   PATTERN_LIST.  */
static pattern_reserv_t
form_reserv_sets_list (pattern_set_el_t pattern_list)
{
  pattern_set_el_t el;
  pattern_reserv_t first, curr, prev;
  int i;

  prev = first = NULL;
  for (el = pattern_list; el != NULL; el = el->next_pattern_set_el)
    {
      curr = XCREATENODE (struct pattern_reserv);
      curr->reserv = alloc_empty_reserv_sets ();
      curr->next_pattern_reserv = NULL;
      for (i = 0; i < el->units_num; i++)
	{
	  bitmap_set_bit (curr->reserv, el->unit_decls [i]->unit_num);
	  el->unit_decls [i]->in_set_p = TRUE;
	}
      if (prev != NULL)
	prev->next_pattern_reserv = curr;
      else
	first = curr;
      prev = curr;
    }
  return first;
}

 /* The following function forms the array containing presence and
   absence pattern sets for each unit.  */
static void
initiate_presence_absence_pattern_sets (void)
{
  decl_t decl;
  int i;

  obstack_blank (&irp, description->units_num * sizeof (pattern_reserv_t));
  unit_presence_set_table = (pattern_reserv_t *) obstack_base (&irp);
  obstack_finish (&irp);
  obstack_blank (&irp, description->units_num * sizeof (pattern_reserv_t));
  unit_final_presence_set_table = (pattern_reserv_t *) obstack_base (&irp);
  obstack_finish (&irp);
  obstack_blank (&irp, description->units_num * sizeof (pattern_reserv_t));
  unit_absence_set_table = (pattern_reserv_t *) obstack_base (&irp);
  obstack_finish (&irp);
  obstack_blank (&irp, description->units_num * sizeof (pattern_reserv_t));
  unit_final_absence_set_table = (pattern_reserv_t *) obstack_base (&irp);
  obstack_finish (&irp);
  /* Evaluate unit presence/absence sets.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	{
          unit_presence_set_table [DECL_UNIT (decl)->unit_num]
	    = form_reserv_sets_list (DECL_UNIT (decl)->presence_list);
          unit_final_presence_set_table [DECL_UNIT (decl)->unit_num]
	    = form_reserv_sets_list (DECL_UNIT (decl)->final_presence_list);
          unit_absence_set_table [DECL_UNIT (decl)->unit_num]
	    = form_reserv_sets_list (DECL_UNIT (decl)->absence_list);
          unit_final_absence_set_table [DECL_UNIT (decl)->unit_num]
	    = form_reserv_sets_list (DECL_UNIT (decl)->final_absence_list);
        }
    }
}

/* The function checks that CHECKED_SET satisfies all presence pattern
   sets for units in ORIGINAL_SET.  The function returns TRUE if it
   is ok.  */
static int
check_presence_pattern_sets (reserv_sets_t checked_set,
			     reserv_sets_t original_set,
			     int final_p)
{
  int el;
  unsigned int i;
  int start_unit_num;
  int unit_num;
  int presence_p;
  pattern_reserv_t pat_reserv;

  for (el = 0; el < els_in_cycle_reserv; el++)
    if (original_set[el])
      for (i = 0; i < CHAR_BIT * sizeof (set_el_t); i++)
	if ((original_set[el] >> i) & 1)
	  {
	    start_unit_num = el * CHAR_BIT * sizeof (set_el_t) + i;
	    if (start_unit_num >= description->units_num)
	      break;
	    if ((final_p
		 && unit_final_presence_set_table [start_unit_num] == NULL)
		|| (!final_p
		    && unit_presence_set_table [start_unit_num] == NULL))
	      continue;
	    presence_p = FALSE;
	    for (pat_reserv = (final_p
			       ? unit_final_presence_set_table [start_unit_num]
			       : unit_presence_set_table [start_unit_num]);
		 pat_reserv != NULL;
		 pat_reserv = pat_reserv->next_pattern_reserv)
	      {
		for (unit_num = 0; unit_num < els_in_cycle_reserv; unit_num++)
		  if ((checked_set [unit_num] & pat_reserv->reserv [unit_num])
		      != pat_reserv->reserv [unit_num])
		    break;
		presence_p = presence_p || unit_num >= els_in_cycle_reserv;
	      }
	    if (!presence_p)
	      return FALSE;
	  }
  return TRUE;
}

/* The function checks that CHECKED_SET satisfies all absence pattern
   sets for units in ORIGINAL_SET.  The function returns TRUE if it
   is ok.  */
static int
check_absence_pattern_sets (reserv_sets_t checked_set,
			    reserv_sets_t original_set,
			    int final_p)
{
  int el;
  unsigned int i;
  int start_unit_num;
  int unit_num;
  pattern_reserv_t pat_reserv;

  for (el = 0; el < els_in_cycle_reserv; el++)
    if (original_set[el])
      for (i = 0; i < CHAR_BIT * sizeof (set_el_t); i++)
	if ((original_set[el] >> i) & 1)
	  {
	    start_unit_num = el * CHAR_BIT * sizeof (set_el_t) + i;
	    if (start_unit_num >= description->units_num)
	      break;
	    for (pat_reserv = (final_p
			       ? unit_final_absence_set_table [start_unit_num]
			       : unit_absence_set_table [start_unit_num]);
		 pat_reserv != NULL;
		 pat_reserv = pat_reserv->next_pattern_reserv)
	      {
		for (unit_num = 0; unit_num < els_in_cycle_reserv; unit_num++)
		  if ((checked_set [unit_num] & pat_reserv->reserv [unit_num])
		      != pat_reserv->reserv [unit_num]
		      && pat_reserv->reserv [unit_num])
		    break;
		if (unit_num >= els_in_cycle_reserv)
		  return FALSE;
	      }
	  }
  return TRUE;
}



/* This page contains code for transformation of original reservations
   described in .md file.  The main goal of transformations is
   simplifying reservation and lifting up all `|' on the top of IR
   reservation representation.  */


/* The following function makes copy of IR representation of
   reservation.  The function also substitutes all reservations
   defined by define_reservation by corresponding value during making
   the copy.  */
static regexp_t
copy_insn_regexp (regexp_t regexp)
{
  regexp_t  result;
  int i;

  switch (regexp->mode)
    {
    case rm_reserv:
      result = copy_insn_regexp (REGEXP_RESERV (regexp)->reserv_decl->regexp);
      break;

    case rm_unit:
      result = XCOPYNODE (struct regexp, regexp);
      break;

    case rm_repeat:
      result = XCOPYNODE (struct regexp, regexp);
      REGEXP_REPEAT (result)->regexp
        = copy_insn_regexp (REGEXP_REPEAT (regexp)->regexp);
      break;

    case rm_sequence:
      result = XCOPYNODEVAR (struct regexp, regexp,
			     sizeof (struct regexp) + sizeof (regexp_t)
			     * (REGEXP_SEQUENCE (regexp)->regexps_num - 1));
      for (i = 0; i <REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	REGEXP_SEQUENCE (result)->regexps [i]
	  = copy_insn_regexp (REGEXP_SEQUENCE (regexp)->regexps [i]);
      break;

    case rm_allof:
      result = XCOPYNODEVAR (struct regexp, regexp,
			     sizeof (struct regexp) + sizeof (regexp_t)
			     * (REGEXP_ALLOF (regexp)->regexps_num - 1));
      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	REGEXP_ALLOF (result)->regexps [i]
	  = copy_insn_regexp (REGEXP_ALLOF (regexp)->regexps [i]);
      break;

    case rm_oneof:
      result = XCOPYNODEVAR (struct regexp, regexp,
			     sizeof (struct regexp) + sizeof (regexp_t)
			     * (REGEXP_ONEOF (regexp)->regexps_num - 1));
      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	REGEXP_ONEOF (result)->regexps [i]
	  = copy_insn_regexp (REGEXP_ONEOF (regexp)->regexps [i]);
      break;

    case rm_nothing:
      result = XCOPYNODE (struct regexp, regexp);
      break;

    default:
      gcc_unreachable ();
    }
  return result;
}

/* The following variable is set up 1 if a transformation has been
   applied.  */
static int regexp_transformed_p;

/* The function makes transformation
   A*N -> A, A, ...  */
static regexp_t
transform_1 (regexp_t regexp)
{
  int i;
  int repeat_num;
  regexp_t operand;
  pos_t pos;

  if (regexp->mode == rm_repeat)
    {
      repeat_num = REGEXP_REPEAT (regexp)->repeat_num;
      gcc_assert (repeat_num > 1);
      operand = REGEXP_REPEAT (regexp)->regexp;
      pos = regexp->mode;
      regexp = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
			       + sizeof (regexp_t) * (repeat_num - 1));
      regexp->mode = rm_sequence;
      regexp->pos = pos;
      REGEXP_SEQUENCE (regexp)->regexps_num = repeat_num;
      for (i = 0; i < repeat_num; i++)
	REGEXP_SEQUENCE (regexp)->regexps [i] = copy_insn_regexp (operand);
      regexp_transformed_p = 1;
    }
  return regexp;
}

/* The function makes transformations
   ...,(A,B,...),C,... -> ...,A,B,...,C,...
   ...+(A+B+...)+C+... -> ...+A+B+...+C+...
   ...|(A|B|...)|C|... -> ...|A|B|...|C|...  */
static regexp_t
transform_2 (regexp_t regexp)
{
  if (regexp->mode == rm_sequence)
    {
      regexp_t sequence = NULL;
      regexp_t result;
      int sequence_index = 0;
      int i, j;

      for (i = 0; i < REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	if (REGEXP_SEQUENCE (regexp)->regexps [i]->mode == rm_sequence)
	  {
	    sequence_index = i;
	    sequence = REGEXP_SEQUENCE (regexp)->regexps [i];
	    break;
	  }
      if (i < REGEXP_SEQUENCE (regexp)->regexps_num)
	{
	  gcc_assert (REGEXP_SEQUENCE (sequence)->regexps_num > 1
		      && REGEXP_SEQUENCE (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t)
				   * (REGEXP_SEQUENCE (regexp)->regexps_num
				      + REGEXP_SEQUENCE (sequence)->regexps_num
				      - 2));
	  result->mode = rm_sequence;
	  result->pos = regexp->pos;
	  REGEXP_SEQUENCE (result)->regexps_num
            = (REGEXP_SEQUENCE (regexp)->regexps_num
               + REGEXP_SEQUENCE (sequence)->regexps_num - 1);
	  for (i = 0; i < REGEXP_SEQUENCE (regexp)->regexps_num; i++)
            if (i < sequence_index)
              REGEXP_SEQUENCE (result)->regexps [i]
                = copy_insn_regexp (REGEXP_SEQUENCE (regexp)->regexps [i]);
            else if (i > sequence_index)
              REGEXP_SEQUENCE (result)->regexps
                [i + REGEXP_SEQUENCE (sequence)->regexps_num - 1]
                = copy_insn_regexp (REGEXP_SEQUENCE (regexp)->regexps [i]);
            else
              for (j = 0; j < REGEXP_SEQUENCE (sequence)->regexps_num; j++)
                REGEXP_SEQUENCE (result)->regexps [i + j]
                  = copy_insn_regexp (REGEXP_SEQUENCE (sequence)->regexps [j]);
	  regexp_transformed_p = 1;
	  regexp = result;
	}
    }
  else if (regexp->mode == rm_allof)
    {
      regexp_t allof = NULL;
      regexp_t result;
      int allof_index = 0;
      int i, j;

      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	if (REGEXP_ALLOF (regexp)->regexps [i]->mode == rm_allof)
	  {
	    allof_index = i;
	    allof = REGEXP_ALLOF (regexp)->regexps [i];
	    break;
	  }
      if (i < REGEXP_ALLOF (regexp)->regexps_num)
	{
	  gcc_assert (REGEXP_ALLOF (allof)->regexps_num > 1
		      && REGEXP_ALLOF (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t)
				   * (REGEXP_ALLOF (regexp)->regexps_num
				      + REGEXP_ALLOF (allof)->regexps_num - 2));
	  result->mode = rm_allof;
	  result->pos = regexp->pos;
	  REGEXP_ALLOF (result)->regexps_num
            = (REGEXP_ALLOF (regexp)->regexps_num
               + REGEXP_ALLOF (allof)->regexps_num - 1);
	  for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
            if (i < allof_index)
              REGEXP_ALLOF (result)->regexps [i]
                = copy_insn_regexp (REGEXP_ALLOF (regexp)->regexps [i]);
            else if (i > allof_index)
              REGEXP_ALLOF (result)->regexps
                [i + REGEXP_ALLOF (allof)->regexps_num - 1]
                = copy_insn_regexp (REGEXP_ALLOF (regexp)->regexps [i]);
            else
              for (j = 0; j < REGEXP_ALLOF (allof)->regexps_num; j++)
                REGEXP_ALLOF (result)->regexps [i + j]
                  = copy_insn_regexp (REGEXP_ALLOF (allof)->regexps [j]);
	  regexp_transformed_p = 1;
	  regexp = result;
	}
    }
  else if (regexp->mode == rm_oneof)
    {
      regexp_t oneof = NULL;
      regexp_t result;
      int oneof_index = 0;
      int i, j;

      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	if (REGEXP_ONEOF (regexp)->regexps [i]->mode == rm_oneof)
	  {
	    oneof_index = i;
	    oneof = REGEXP_ONEOF (regexp)->regexps [i];
	    break;
	  }
      if (i < REGEXP_ONEOF (regexp)->regexps_num)
	{
	  gcc_assert (REGEXP_ONEOF (oneof)->regexps_num > 1
		      && REGEXP_ONEOF (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t)
				   * (REGEXP_ONEOF (regexp)->regexps_num
				      + REGEXP_ONEOF (oneof)->regexps_num - 2));
	  result->mode = rm_oneof;
	  result->pos = regexp->pos;
	  REGEXP_ONEOF (result)->regexps_num
	    = (REGEXP_ONEOF (regexp)->regexps_num
               + REGEXP_ONEOF (oneof)->regexps_num - 1);
	  for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
            if (i < oneof_index)
              REGEXP_ONEOF (result)->regexps [i]
                = copy_insn_regexp (REGEXP_ONEOF (regexp)->regexps [i]);
            else if (i > oneof_index)
              REGEXP_ONEOF (result)->regexps
                [i + REGEXP_ONEOF (oneof)->regexps_num - 1]
                = copy_insn_regexp (REGEXP_ONEOF (regexp)->regexps [i]);
            else
              for (j = 0; j < REGEXP_ONEOF (oneof)->regexps_num; j++)
                REGEXP_ONEOF (result)->regexps [i + j]
                  = copy_insn_regexp (REGEXP_ONEOF (oneof)->regexps [j]);
	  regexp_transformed_p = 1;
	  regexp = result;
	}
    }
  return regexp;
}

/* The function makes transformations
   ...,A|B|...,C,... -> (...,A,C,...)|(...,B,C,...)|...
   ...+(A|B|...)+C+... -> (...+A+C+...)|(...+B+C+...)|...
   ...+(A,B,...)+C+... -> (...+A+C+...),B,...
   ...+(A,B,...)+(C,D,...) -> (A+C),(B+D),...  */
static regexp_t
transform_3 (regexp_t regexp)
{
  if (regexp->mode == rm_sequence)
    {
      regexp_t oneof = NULL;
      int oneof_index = 0;
      regexp_t result;
      regexp_t sequence;
      int i, j;

      for (i = 0; i <REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	if (REGEXP_SEQUENCE (regexp)->regexps [i]->mode == rm_oneof)
	  {
	    oneof_index = i;
	    oneof = REGEXP_SEQUENCE (regexp)->regexps [i];
	    break;
	  }
      if (i < REGEXP_SEQUENCE (regexp)->regexps_num)
	{
	  gcc_assert (REGEXP_ONEOF (oneof)->regexps_num > 1
		      && REGEXP_SEQUENCE (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t)
				   * (REGEXP_ONEOF (oneof)->regexps_num - 1));
	  result->mode = rm_oneof;
	  result->pos = regexp->pos;
	  REGEXP_ONEOF (result)->regexps_num
	    = REGEXP_ONEOF (oneof)->regexps_num;
	  for (i = 0; i < REGEXP_ONEOF (result)->regexps_num; i++)
	    {
	      sequence
                = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				  + sizeof (regexp_t)
				  * (REGEXP_SEQUENCE (regexp)->regexps_num - 1));
	      sequence->mode = rm_sequence;
	      sequence->pos = regexp->pos;
	      REGEXP_SEQUENCE (sequence)->regexps_num
                = REGEXP_SEQUENCE (regexp)->regexps_num;
              REGEXP_ONEOF (result)->regexps [i] = sequence;
	      for (j = 0; j < REGEXP_SEQUENCE (sequence)->regexps_num; j++)
		if (j != oneof_index)
		  REGEXP_SEQUENCE (sequence)->regexps [j]
		    = copy_insn_regexp (REGEXP_SEQUENCE (regexp)->regexps [j]);
		else
		  REGEXP_SEQUENCE (sequence)->regexps [j]
		    = copy_insn_regexp (REGEXP_ONEOF (oneof)->regexps [i]);
	    }
	  regexp_transformed_p = 1;
	  regexp = result;
	}
    }
  else if (regexp->mode == rm_allof)
    {
      regexp_t oneof = NULL;
      regexp_t seq;
      int oneof_index = 0;
      int max_seq_length, allof_length;
      regexp_t result;
      regexp_t allof = NULL;
      regexp_t allof_op = NULL;
      int i, j;

      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	if (REGEXP_ALLOF (regexp)->regexps [i]->mode == rm_oneof)
	  {
	    oneof_index = i;
	    oneof = REGEXP_ALLOF (regexp)->regexps [i];
	    break;
	  }
      if (i < REGEXP_ALLOF (regexp)->regexps_num)
	{
	  gcc_assert (REGEXP_ONEOF (oneof)->regexps_num > 1
		      && REGEXP_ALLOF (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t)
				   * (REGEXP_ONEOF (oneof)->regexps_num - 1));
	  result->mode = rm_oneof;
	  result->pos = regexp->pos;
	  REGEXP_ONEOF (result)->regexps_num
	    = REGEXP_ONEOF (oneof)->regexps_num;
	  for (i = 0; i < REGEXP_ONEOF (result)->regexps_num; i++)
	    {
	      allof
		= XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				  + sizeof (regexp_t)
				  * (REGEXP_ALLOF (regexp)->regexps_num - 1));
	      allof->mode = rm_allof;
	      allof->pos = regexp->pos;
	      REGEXP_ALLOF (allof)->regexps_num
                = REGEXP_ALLOF (regexp)->regexps_num;
              REGEXP_ONEOF (result)->regexps [i] = allof;
	      for (j = 0; j < REGEXP_ALLOF (allof)->regexps_num; j++)
		if (j != oneof_index)
		  REGEXP_ALLOF (allof)->regexps [j]
		    = copy_insn_regexp (REGEXP_ALLOF (regexp)->regexps [j]);
		else
		  REGEXP_ALLOF (allof)->regexps [j]
		    = copy_insn_regexp (REGEXP_ONEOF (oneof)->regexps [i]);
	    }
	  regexp_transformed_p = 1;
	  regexp = result;
	}
      max_seq_length = 0;
      if (regexp->mode == rm_allof)
	for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	  {
	    switch (REGEXP_ALLOF (regexp)->regexps [i]->mode)
	      {
	      case rm_sequence:
		seq = REGEXP_ALLOF (regexp)->regexps [i];
		if (max_seq_length < REGEXP_SEQUENCE (seq)->regexps_num)
		  max_seq_length = REGEXP_SEQUENCE (seq)->regexps_num;
		break;

	      case rm_unit:
	      case rm_nothing:
		break;

	      default:
		max_seq_length = 0;
		goto break_for;
	      }
	  }
    break_for:
      if (max_seq_length != 0)
	{
	  gcc_assert (max_seq_length != 1
		      && REGEXP_ALLOF (regexp)->regexps_num > 1);
	  result = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
				   + sizeof (regexp_t) * (max_seq_length - 1));
	  result->mode = rm_sequence;
	  result->pos = regexp->pos;
	  REGEXP_SEQUENCE (result)->regexps_num = max_seq_length;
	  for (i = 0; i < max_seq_length; i++)
	    {
	      allof_length = 0;
	      for (j = 0; j < REGEXP_ALLOF (regexp)->regexps_num; j++)
		switch (REGEXP_ALLOF (regexp)->regexps [j]->mode)
		  {
		  case rm_sequence:
		    if (i < (REGEXP_SEQUENCE (REGEXP_ALLOF (regexp)
					      ->regexps [j])->regexps_num))
		      {
			allof_op
			  = (REGEXP_SEQUENCE (REGEXP_ALLOF (regexp)
					      ->regexps [j])
			     ->regexps [i]);
			allof_length++;
		      }
		    break;
		  case rm_unit:
		  case rm_nothing:
		    if (i == 0)
		      {
			allof_op = REGEXP_ALLOF (regexp)->regexps [j];
			allof_length++;
		      }
		    break;
		  default:
		    break;
		  }

	      if (allof_length == 1)
		REGEXP_SEQUENCE (result)->regexps [i] = allof_op;
	      else
		{
		  allof = XCREATENODEVAR (struct regexp, sizeof (struct regexp)
					  + sizeof (regexp_t)
					  * (allof_length - 1));
		  allof->mode = rm_allof;
		  allof->pos = regexp->pos;
		  REGEXP_ALLOF (allof)->regexps_num = allof_length;
		  REGEXP_SEQUENCE (result)->regexps [i] = allof;
		  allof_length = 0;
		  for (j = 0; j < REGEXP_ALLOF (regexp)->regexps_num; j++)
		    if (REGEXP_ALLOF (regexp)->regexps [j]->mode == rm_sequence
			&& (i <
			    (REGEXP_SEQUENCE (REGEXP_ALLOF (regexp)
					      ->regexps [j])->regexps_num)))
		      {
			allof_op = (REGEXP_SEQUENCE (REGEXP_ALLOF (regexp)
						     ->regexps [j])
				    ->regexps [i]);
			REGEXP_ALLOF (allof)->regexps [allof_length]
			  = allof_op;
			allof_length++;
		      }
		    else if (i == 0
			     && (REGEXP_ALLOF (regexp)->regexps [j]->mode
				 == rm_unit
				 || (REGEXP_ALLOF (regexp)->regexps [j]->mode
				     == rm_nothing)))
		      {
			allof_op = REGEXP_ALLOF (regexp)->regexps [j];
			REGEXP_ALLOF (allof)->regexps [allof_length]
			  = allof_op;
			allof_length++;
		      }
		}
	    }
	  regexp_transformed_p = 1;
	  regexp = result;
	}
    }
  return regexp;
}

/* The function traverses IR of reservation and applies transformations
   implemented by FUNC.  */
static regexp_t
regexp_transform_func (regexp_t regexp, regexp_t (*func) (regexp_t regexp))
{
  int i;

  switch (regexp->mode)
    {
    case rm_sequence:
      for (i = 0; i < REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	REGEXP_SEQUENCE (regexp)->regexps [i]
	  = regexp_transform_func (REGEXP_SEQUENCE (regexp)->regexps [i],
				   func);
      break;

    case rm_allof:
      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	REGEXP_ALLOF (regexp)->regexps [i]
	  = regexp_transform_func (REGEXP_ALLOF (regexp)->regexps [i], func);
      break;

    case rm_oneof:
      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	REGEXP_ONEOF (regexp)->regexps [i]
	  = regexp_transform_func (REGEXP_ONEOF (regexp)->regexps [i], func);
      break;

    case rm_repeat:
      REGEXP_REPEAT (regexp)->regexp
	= regexp_transform_func (REGEXP_REPEAT (regexp)->regexp, func);
      break;

    case rm_nothing:
    case rm_unit:
      break;

    default:
      gcc_unreachable ();
    }
  return (*func) (regexp);
}

/* The function applies all transformations for IR representation of
   reservation REGEXP.  */
static regexp_t
transform_regexp (regexp_t regexp)
{
  regexp = regexp_transform_func (regexp, transform_1);
  do
    {
      regexp_transformed_p = 0;
      regexp = regexp_transform_func (regexp, transform_2);
      regexp = regexp_transform_func (regexp, transform_3);
    }
  while (regexp_transformed_p);
  return regexp;
}

/* The function applies all transformations for reservations of all
   insn declarations.  */
static void
transform_insn_regexps (void)
{
  decl_t decl;
  int i;

  transform_time = create_ticker ();
  add_advance_cycle_insn_decl ();
  if (collapse_flag)
    add_collapse_ndfa_insn_decl ();
  if (progress_flag)
    fprintf (stderr, "Reservation transformation...");
  for (i = 0; i < description->normal_decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	DECL_INSN_RESERV (decl)->transformed_regexp
	  = transform_regexp (copy_insn_regexp
			      (DECL_INSN_RESERV (decl)->regexp));
    }
  if (progress_flag)
    fprintf (stderr, "done\n");
  ticker_off (&transform_time);
}



/* The following variable value is TRUE if the first annotated message
   about units to automata distribution has been output.  */
static int annotation_message_reported_p;

/* The vector contains all decls which are automata.  */
static vec<decl_t> automaton_decls;

/* The following structure describes usage of a unit in a reservation.  */
struct unit_usage
{
  unit_decl_t unit_decl;
  /* The following forms a list of units used on the same cycle in the
     same alternative.  The list is ordered by the correspdoning unit
     declarations and there is no unit declaration duplication in the
     list.  */
  struct unit_usage *next;
};
typedef struct unit_usage *unit_usage_t;


/* Obstack for unit_usage structures.  */
static struct obstack unit_usages;

/* VLA for representation of array of pointers to unit usage
   structures.  There is an element for each combination of
   (alternative number, cycle).  Unit usages on given cycle in
   alternative with given number are referred through element with
   index equals to the cycle * number of all alternatives in the
   regexp + the alternative number.  */
static vec<unit_usage_t> cycle_alt_unit_usages;

/* The following function creates the structure unit_usage for UNIT on
   CYCLE in REGEXP alternative with ALT_NUM.  The structure is made
   accessed through cycle_alt_unit_usages.  */
static void
store_alt_unit_usage (regexp_t regexp, regexp_t unit, int cycle,
		      int alt_num)
{
  size_t length;
  unit_decl_t unit_decl;
  unit_usage_t unit_usage_ptr, curr, prev;
  int index;

  gcc_assert (regexp && regexp->mode == rm_oneof
	      && alt_num < REGEXP_ONEOF (regexp)->regexps_num);
  unit_decl = REGEXP_UNIT (unit)->unit_decl;

  length = (cycle + 1) * REGEXP_ONEOF (regexp)->regexps_num;
  while (cycle_alt_unit_usages.length () < length)
    cycle_alt_unit_usages.safe_push (NULL);

  index = cycle * REGEXP_ONEOF (regexp)->regexps_num + alt_num;
  prev = NULL;
  for (curr = cycle_alt_unit_usages[index];
       curr != NULL;
       prev = curr, curr = curr->next)
    if (curr->unit_decl >= unit_decl)
      break;
  if (curr != NULL && curr->unit_decl == unit_decl)
    return;
  obstack_blank (&unit_usages, sizeof (struct unit_usage));
  unit_usage_ptr = (struct unit_usage *) obstack_base (&unit_usages);
  obstack_finish (&unit_usages);
  unit_usage_ptr->unit_decl = unit_decl;
  unit_decl->last_distribution_check_cycle = -1; /* undefined */
  unit_usage_ptr->next = curr;
  if (prev == NULL)
    cycle_alt_unit_usages[index] = unit_usage_ptr;
  else
    prev->next = unit_usage_ptr;
}

/* Return true if unit UNIT_DECL is present on the LIST.  */
static bool
unit_present_on_list_p (unit_usage_t list, unit_decl_t unit_decl)
{
  while (list != NULL)
    {
      if (list->unit_decl == unit_decl)
	return true;
      list = list->next;
    }
  return false;
}

/* The function returns true if reservations of alternatives ALT1 and
   ALT2 are equal after excluding reservations of units of
   EXCLUDED_AUTOMATON_DECL.  */
static bool
equal_alternatives_p (int alt1, int alt2, int n_alts,
		      struct automaton_decl *excluded_automaton_decl)
{
  int i;
  unit_usage_t list1, list2;

  for (i = 0;
       i < (int) cycle_alt_unit_usages.length ();
       i += n_alts)
    {
      for (list1 = cycle_alt_unit_usages[i + alt1],
	     list2 = cycle_alt_unit_usages[i + alt2];;
	   list1 = list1->next, list2 = list2->next)
	{
	  while (list1 != NULL
		 && list1->unit_decl->automaton_decl == excluded_automaton_decl)
	    list1 = list1->next;
	  while (list2 != NULL
		 && list2->unit_decl->automaton_decl == excluded_automaton_decl)
	    list2 = list2->next;
	  if (list1 == NULL || list2 == NULL)
	    {
	      if (list1 != list2)
		return false;
	      else
		break;
	    }
	  if (list1->unit_decl != list2->unit_decl)
	    return false;
	}
    }
  return true;
}


/* The function processes given REGEXP to find units with the wrong
   distribution.  */
static void
check_regexp_units_distribution (const char *insn_reserv_name,
				 regexp_t regexp)
{
  int i, j, k, cycle, start, n_alts, alt, alt2;
  bool annotation_reservation_message_reported_p;
  regexp_t seq, allof, unit;
  struct unit_usage *unit_usage_ptr;

  if (regexp == NULL || regexp->mode != rm_oneof)
    return;
  /* Store all unit usages in the regexp:  */
  obstack_init (&unit_usages);
  cycle_alt_unit_usages.create (10);

  for (i = REGEXP_ONEOF (regexp)->regexps_num - 1; i >= 0; i--)
    {
      seq = REGEXP_ONEOF (regexp)->regexps [i];
      switch (seq->mode)
	{
	case rm_sequence:
	  for (j = 0; j < REGEXP_SEQUENCE (seq)->regexps_num; j++)
	    {
	      allof = REGEXP_SEQUENCE (seq)->regexps [j];
	      switch (allof->mode)
		{
		case rm_allof:
		  for (k = 0; k < REGEXP_ALLOF (allof)->regexps_num; k++)
		    {
		      unit = REGEXP_ALLOF (allof)->regexps [k];
		      if (unit->mode == rm_unit)
			store_alt_unit_usage (regexp, unit, j, i);
		      else
			gcc_assert (unit->mode == rm_nothing);
		    }
		  break;

		case rm_unit:
		  store_alt_unit_usage (regexp, allof, j, i);
		  break;

		case rm_nothing:
		  break;

		default:
		  gcc_unreachable ();
		}
	    }
	  break;

	case rm_allof:
	  for (k = 0; k < REGEXP_ALLOF (seq)->regexps_num; k++)
	    {
	      unit = REGEXP_ALLOF (seq)->regexps [k];
	      switch (unit->mode)
		{
		case rm_unit:
		  store_alt_unit_usage (regexp, unit, 0, i);
		  break;

		case rm_nothing:
		  break;

		default:
		  gcc_unreachable ();
		}
	    }
	  break;

	case rm_unit:
	  store_alt_unit_usage (regexp, seq, 0, i);
	  break;

	case rm_nothing:
	  break;

	default:
	  gcc_unreachable ();
	}
    }
  /* Check distribution:  */
  for (i = 0; i < (int) cycle_alt_unit_usages.length (); i++)
    for (unit_usage_ptr = cycle_alt_unit_usages[i];
	 unit_usage_ptr != NULL;
	 unit_usage_ptr = unit_usage_ptr->next)
      unit_usage_ptr->unit_decl->last_distribution_check_cycle = -1;
  n_alts = REGEXP_ONEOF (regexp)->regexps_num;
  auto_vec<int> marked (n_alts);
  for (i = 0; i < n_alts; i++)
    marked.safe_push (0);
  annotation_reservation_message_reported_p = false;
  for (i = 0; i < (int) cycle_alt_unit_usages.length (); i++)
    {
      cycle = i / n_alts;
      start = cycle * n_alts;
      for (unit_usage_ptr = cycle_alt_unit_usages[i];
	   unit_usage_ptr != NULL;
	   unit_usage_ptr = unit_usage_ptr->next)
	{
	  if (unit_usage_ptr->unit_decl->last_distribution_check_cycle == cycle)
	    continue;
	  unit_usage_ptr->unit_decl->last_distribution_check_cycle = cycle;
	  for (alt = 0; alt < n_alts; alt++)
	    if (! unit_present_on_list_p (cycle_alt_unit_usages[start + alt],
					  unit_usage_ptr->unit_decl))
	      break;
	  if (alt >= n_alts)
	    continue;
	  memset (marked.address (), 0, n_alts * sizeof (int));
	  for (alt = 0; alt < n_alts; alt++)
	    {
	      if (! unit_present_on_list_p (cycle_alt_unit_usages[start + alt],
					    unit_usage_ptr->unit_decl))
		continue;
	      for (j = 0;
		   j < (int) cycle_alt_unit_usages.length ();
		   j++)
		{
		  alt2 = j % n_alts;
		  if (! unit_present_on_list_p
		        (cycle_alt_unit_usages[start + alt2],
			 unit_usage_ptr->unit_decl)
		      && equal_alternatives_p (alt, alt2, n_alts,
					       unit_usage_ptr
					       ->unit_decl->automaton_decl))
		    {
		      marked[alt] = 1;
		      marked[alt2] = 1;
		    }
		}
	    }
	  for (alt = 0; alt < n_alts && marked[alt]; alt++)
	    ;
	  if (alt < n_alts && 0)
	    {
	      if (! annotation_message_reported_p)
		{
		  fprintf (stderr, "\n");
		  error ("The following units do not satisfy units-automata distribution rule");
		  error ("(Unit presence on one alt and its absence on other alt\n");
		  error (" result in different other automata reservations)");
		  annotation_message_reported_p = TRUE;
		}
	      if (! annotation_reservation_message_reported_p)
		{
		  error ("Reserv %s:", insn_reserv_name);
		  annotation_reservation_message_reported_p = true;
		}
	      error ("  Unit %s, cycle %d, alt %d, another alt %d",
		     unit_usage_ptr->unit_decl->name, cycle, i % n_alts, alt);
	    }
	}
    }
  cycle_alt_unit_usages.release ();
  obstack_free (&unit_usages, NULL);
}

/* The function finds units which violates units to automata
   distribution rule.  If the units exist, report about them.  */
static void
check_unit_distributions_to_automata (void)
{
  decl_t decl;
  int i;

  if (progress_flag)
    fprintf (stderr, "Check unit distributions to automata...");
  automaton_decls.create (0);
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_automaton)
	automaton_decls.safe_push (decl);
    }
  if (automaton_decls.length () > 1)
    {
      annotation_message_reported_p = FALSE;
      for (i = 0; i < description->decls_num; i++)
	{
	  decl = description->decls [i];
	  if (decl->mode == dm_insn_reserv)
	    check_regexp_units_distribution
	      (DECL_INSN_RESERV (decl)->name,
	       DECL_INSN_RESERV (decl)->transformed_regexp);
	}
    }
  automaton_decls.release ();
  if (progress_flag)
    fprintf (stderr, "done\n");
}



/* The page contains code for building alt_states (see comments for
   IR) describing all possible insns reservations of an automaton.  */

/* Current state being formed for which the current alt_state
   refers.  */
static state_t state_being_formed;

/* Current alt_state being formed.  */
static alt_state_t alt_state_being_formed;

/* This recursive function processes `,' and units in reservation
   REGEXP for forming alt_states of AUTOMATON.  It is believed that
   CURR_CYCLE is start cycle of all reservation REGEXP.  */
static int
process_seq_for_forming_states (regexp_t regexp, automaton_t automaton,
				int curr_cycle)
{
  int i;

  if (regexp == NULL)
    return curr_cycle;

  switch (regexp->mode)
    {
    case rm_unit:
      if (REGEXP_UNIT (regexp)->unit_decl->corresponding_automaton_num
          == automaton->automaton_order_num)
        set_state_reserv (state_being_formed, curr_cycle,
                          REGEXP_UNIT (regexp)->unit_decl->unit_num);
      return curr_cycle;

    case rm_sequence:
      for (i = 0; i < REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	curr_cycle
	  = process_seq_for_forming_states
	    (REGEXP_SEQUENCE (regexp)->regexps [i], automaton, curr_cycle) + 1;
      return curr_cycle;

    case rm_allof:
      {
	int finish_cycle = 0;
	int cycle;

	for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	  {
	    cycle = process_seq_for_forming_states (REGEXP_ALLOF (regexp)
						    ->regexps [i],
						    automaton, curr_cycle);
	    if (finish_cycle < cycle)
	      finish_cycle = cycle;
	  }
	return finish_cycle;
      }

    case rm_nothing:
      return curr_cycle;

    default:
      gcc_unreachable ();
    }
}

/* This recursive function finishes forming ALT_STATE of AUTOMATON and
   inserts alt_state into the table.  */
static void
finish_forming_alt_state (alt_state_t alt_state,
			  automaton_t automaton ATTRIBUTE_UNUSED)
{
  state_t state_in_table;
  state_t corresponding_state;

  corresponding_state = alt_state->state;
  state_in_table = insert_state (corresponding_state);
  if (state_in_table != corresponding_state)
    {
      free_state (corresponding_state);
      alt_state->state = state_in_table;
    }
}

/* The following variable value is current automaton insn for whose
   reservation the alt states are created.  */
static ainsn_t curr_ainsn;

/* This recursive function processes `|' in reservation REGEXP for
   forming alt_states of AUTOMATON.  List of the alt states should
   have the same order as in the description.  */
static void
process_alts_for_forming_states (regexp_t regexp, automaton_t automaton,
				 int inside_oneof_p)
{
  int i;

  if (regexp->mode != rm_oneof)
    {
      alt_state_being_formed = get_free_alt_state ();
      state_being_formed = get_free_state (1, automaton);
      alt_state_being_formed->state = state_being_formed;
      /* We inserts in reverse order but we process alternatives also
         in reverse order.  So we have the same order of alternative
         as in the description.  */
      alt_state_being_formed->next_alt_state = curr_ainsn->alt_states;
      curr_ainsn->alt_states = alt_state_being_formed;
      (void) process_seq_for_forming_states (regexp, automaton, 0);
      finish_forming_alt_state (alt_state_being_formed, automaton);
    }
  else
    {
      gcc_assert (!inside_oneof_p);
      /* We processes it in reverse order to get list with the same
	 order as in the description.  See also the previous
	 commentary.  */
      for (i = REGEXP_ONEOF (regexp)->regexps_num - 1; i >= 0; i--)
	process_alts_for_forming_states (REGEXP_ONEOF (regexp)->regexps [i],
					 automaton, 1);
    }
}

/* Create nodes alt_state for all AUTOMATON insns.  */
static void
create_alt_states (automaton_t automaton)
{
  struct insn_reserv_decl *reserv_decl;

  for (curr_ainsn = automaton->ainsn_list;
       curr_ainsn != NULL;
       curr_ainsn = curr_ainsn->next_ainsn)
    {
      reserv_decl = curr_ainsn->insn_reserv_decl;
      if (!special_decl_p (reserv_decl))
        {
          curr_ainsn->alt_states = NULL;
          process_alts_for_forming_states (reserv_decl->transformed_regexp,
					   automaton, 0);
          curr_ainsn->sorted_alt_states
	    = uniq_sort_alt_states (curr_ainsn->alt_states);
        }
    }
}



/* The page contains major code for building DFA(s) for fast pipeline
   hazards recognition.  */

/* The function forms list of ainsns of AUTOMATON with the same
   reservation.  */

static void
form_ainsn_with_same_reservs (automaton_t automaton)
{
  ainsn_t curr_ainsn;
  size_t i;
  auto_vec<ainsn_t, 150> last_insns;

  for (curr_ainsn = automaton->ainsn_list;
       curr_ainsn != NULL;
       curr_ainsn = curr_ainsn->next_ainsn)
    if (special_decl_p (curr_ainsn->insn_reserv_decl))
      {
        curr_ainsn->next_same_reservs_insn = NULL;
        curr_ainsn->first_insn_with_same_reservs = 1;
      }
    else
      {
        for (i = 0; i < last_insns.length (); i++)
          if (alt_states_eq
              (curr_ainsn->sorted_alt_states,
               last_insns[i]->sorted_alt_states))
            break;
        curr_ainsn->next_same_reservs_insn = NULL;
        if (i < last_insns.length ())
          {
            curr_ainsn->first_insn_with_same_reservs = 0;
	    last_insns[i]->next_same_reservs_insn = curr_ainsn;
            last_insns[i] = curr_ainsn;
          }
        else
          {
            last_insns.safe_push (curr_ainsn);
            curr_ainsn->first_insn_with_same_reservs = 1;
          }
      }
}

/* Forming unit reservations which can affect creating the automaton
   states achieved from a given state.  It permits to build smaller
   automata in many cases.  We would have the same automata after
   the minimization without such optimization, but the automaton
   right after the building could be huge.  So in other words, usage
   of reservs_matter means some minimization during building the
   automaton.  */
static reserv_sets_t
form_reservs_matter (automaton_t automaton)
{
  int cycle, unit;
  reserv_sets_t reservs_matter = alloc_empty_reserv_sets ();

  for (cycle = 0; cycle < max_cycles_num; cycle++)
    for (unit = 0; unit < description->units_num; unit++)
      if (units_array [unit]->automaton_decl
	  == automaton->corresponding_automaton_decl
	  && (cycle >= units_array [unit]->min_occ_cycle_num
	      /* We cannot remove queried unit from reservations.  */
	      || units_array [unit]->query_p
	      /* We cannot remove units which are used
		 `exclusion_set', `presence_set',
		 `final_presence_set', `absence_set', and
		 `final_absence_set'.  */
	      || units_array [unit]->in_set_p))
	set_unit_reserv (reservs_matter, cycle, unit);
  return reservs_matter;
}

/* The following function creates all states of nondeterministic AUTOMATON.  */
static void
make_automaton (automaton_t automaton)
{
  ainsn_t ainsn;
  struct insn_reserv_decl *insn_reserv_decl;
  alt_state_t alt_state;
  state_t state;
  state_t start_state;
  state_t state2;
  auto_vec<state_t, 150> state_stack;
  int states_n;
  reserv_sets_t reservs_matter = form_reservs_matter (automaton);

  /* Create the start state (empty state).  */
  start_state = insert_state (get_free_state (1, automaton));
  automaton->start_state = start_state;
  start_state->it_was_placed_in_stack_for_NDFA_forming = 1;
  state_stack.safe_push (start_state);
  states_n = 1;
  while (state_stack.length () != 0)
    {
      state = state_stack.pop ();
      for (ainsn = automaton->ainsn_list;
	   ainsn != NULL;
	   ainsn = ainsn->next_ainsn)
        if (ainsn->first_insn_with_same_reservs)
          {
            insn_reserv_decl = ainsn->insn_reserv_decl;
            if (!special_decl_p (insn_reserv_decl))
              {
		/* We process alt_states in the same order as they are
                   present in the description.  */
                for (alt_state = ainsn->alt_states;
                     alt_state != NULL;
                     alt_state = alt_state->next_alt_state)
                  {
                    state2 = alt_state->state;
                    if (!intersected_state_reservs_p (state, state2))
                      {
                        state2 = states_union (state, state2, reservs_matter);
                        if (!state2->it_was_placed_in_stack_for_NDFA_forming)
                          {
                            state2->it_was_placed_in_stack_for_NDFA_forming
			      = 1;
                            state_stack.safe_push (state2);
			    states_n++;
			    if (progress_flag && states_n % 100 == 0)
			      fprintf (stderr, ".");
                          }
			add_arc (state, state2, ainsn);
			if (!ndfa_flag)
			  break;
                      }
                  }
              }
          }
      /* Add transition to advance cycle.  */
      state2 = state_shift (state, reservs_matter);
      if (!state2->it_was_placed_in_stack_for_NDFA_forming)
        {
          state2->it_was_placed_in_stack_for_NDFA_forming = 1;
          state_stack.safe_push (state2);
	  states_n++;
	  if (progress_flag && states_n % 100 == 0)
	    fprintf (stderr, ".");
        }
      add_arc (state, state2, automaton->advance_ainsn);
    }
}

/* Form lists of all arcs of STATE marked by the same ainsn.  */
static void
form_arcs_marked_by_insn (state_t state)
{
  decl_t decl;
  arc_t arc;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	DECL_INSN_RESERV (decl)->arcs_marked_by_insn = NULL;
    }
  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    {
      gcc_assert (arc->insn);
      arc->next_arc_marked_by_insn
	= arc->insn->insn_reserv_decl->arcs_marked_by_insn;
      arc->insn->insn_reserv_decl->arcs_marked_by_insn = arc;
    }
}

/* The function creates composed state (see comments for IR) from
   ORIGINAL_STATE and list of arcs ARCS_MARKED_BY_INSN marked by the
   same insn.  If the composed state is not in STATE_STACK yet, it is
   pushed into STATE_STACK.  */

static int
create_composed_state (state_t original_state, arc_t arcs_marked_by_insn,
		       vec<state_t> *state_stack)
{
  state_t state;
  alt_state_t alt_state, curr_alt_state;
  alt_state_t new_alt_state;
  arc_t curr_arc;
  arc_t next_arc;
  state_t state_in_table;
  state_t temp_state;
  alt_state_t canonical_alt_states_list;
  int alts_number;
  int new_state_p = 0;

  if (arcs_marked_by_insn == NULL)
    return new_state_p;
  if (arcs_marked_by_insn->next_arc_marked_by_insn == NULL)
    state = arcs_marked_by_insn->to_state;
  else
    {
      gcc_assert (ndfa_flag);
      /* Create composed state.  */
      state = get_free_state (0, arcs_marked_by_insn->to_state->automaton);
      curr_alt_state = NULL;
      for (curr_arc = arcs_marked_by_insn;
           curr_arc != NULL;
           curr_arc = curr_arc->next_arc_marked_by_insn)
	if (curr_arc->to_state->component_states == NULL)
	  {
	    new_alt_state = get_free_alt_state ();
	    new_alt_state->next_alt_state = curr_alt_state;
	    new_alt_state->state = curr_arc->to_state;
	    curr_alt_state = new_alt_state;
	  }
	else
	  for (alt_state = curr_arc->to_state->component_states;
	       alt_state != NULL;
	       alt_state = alt_state->next_sorted_alt_state)
	    {
	      new_alt_state = get_free_alt_state ();
	      new_alt_state->next_alt_state = curr_alt_state;
	      new_alt_state->state = alt_state->state;
	      gcc_assert (!alt_state->state->component_states);
	      curr_alt_state = new_alt_state;
	    }
      /* There are not identical sets in the alt state list.  */
      canonical_alt_states_list = uniq_sort_alt_states (curr_alt_state);
      if (canonical_alt_states_list->next_sorted_alt_state == NULL)
        {
          temp_state = state;
          state = canonical_alt_states_list->state;
          free_state (temp_state);
        }
      else
        {
          state->component_states = canonical_alt_states_list;
          state_in_table = insert_state (state);
          if (state_in_table != state)
            {
              gcc_assert
		(state_in_table->it_was_placed_in_stack_for_DFA_forming);
              free_state (state);
              state = state_in_table;
            }
          else
            {
              gcc_assert (!state->it_was_placed_in_stack_for_DFA_forming);
	      new_state_p = 1;
              for (curr_alt_state = state->component_states;
                   curr_alt_state != NULL;
                   curr_alt_state = curr_alt_state->next_sorted_alt_state)
                for (curr_arc = first_out_arc (curr_alt_state->state);
                     curr_arc != NULL;
                     curr_arc = next_out_arc (curr_arc))
		  if (!collapse_flag
		      /* When producing collapse-NDFA transitions, we
			 only add advance-cycle transitions to the
			 collapsed states.  */
		      || (curr_arc->insn->insn_reserv_decl
			  != DECL_INSN_RESERV (advance_cycle_insn_decl)))
		    add_arc (state, curr_arc->to_state, curr_arc->insn);
            }
          arcs_marked_by_insn->to_state = state;
          for (alts_number = 0,
	       curr_arc = arcs_marked_by_insn->next_arc_marked_by_insn;
               curr_arc != NULL;
               curr_arc = next_arc)
            {
              next_arc = curr_arc->next_arc_marked_by_insn;
              remove_arc (original_state, curr_arc);
	      alts_number++;
            }
        }
    }
  if (!state->it_was_placed_in_stack_for_DFA_forming)
    {
      state->it_was_placed_in_stack_for_DFA_forming = 1;
      state_stack->safe_push (state);
    }
  return new_state_p;
}

/* The function transforms nondeterministic AUTOMATON into
   deterministic.  */

static void
NDFA_to_DFA (automaton_t automaton)
{
  state_t start_state;
  state_t state;
  decl_t decl;
  auto_vec<state_t> state_stack;
  int i;
  int states_n;

  /* Create the start state (empty state).  */
  start_state = automaton->start_state;
  start_state->it_was_placed_in_stack_for_DFA_forming = 1;
  state_stack.safe_push (start_state);
  states_n = 1;
  while (state_stack.length () != 0)
    {
      state = state_stack.pop ();
      form_arcs_marked_by_insn (state);
      for (i = 0; i < description->decls_num; i++)
	{
	  decl = description->decls [i];
	  if (decl->mode == dm_insn_reserv
	      && decl != collapse_ndfa_insn_decl
	      && create_composed_state
	         (state, DECL_INSN_RESERV (decl)->arcs_marked_by_insn,
		  &state_stack))
	    {
	      states_n++;
	      if (progress_flag && states_n % 100 == 0)
		fprintf (stderr, ".");
	    }
	}
      /* Add a transition to collapse the NDFA.  */
      if (collapse_flag)
	{
	  if (state->component_states != NULL)
	    {
	      state_t state2 = state->component_states->state;
	      if (!state2->it_was_placed_in_stack_for_DFA_forming)
		{
		  state2->it_was_placed_in_stack_for_DFA_forming = 1;
		  state_stack.safe_push (state2);
		}
	      add_arc (state, state2, automaton->collapse_ainsn);
	    }
	  else
	    add_arc (state, state, automaton->collapse_ainsn);
	}
    }
}

/* The following variable value is current number (1, 2, ...) of passing
   graph of states.  */
static int curr_state_graph_pass_num;

/* This recursive function passes all states achieved from START_STATE
   and applies APPLIED_FUNC to them.  */
static void
pass_state_graph (state_t start_state, void (*applied_func) (state_t state))
{
  arc_t arc;

  if (start_state->pass_num == curr_state_graph_pass_num)
    return;
  start_state->pass_num = curr_state_graph_pass_num;
  (*applied_func) (start_state);
  for (arc = first_out_arc (start_state);
       arc != NULL;
       arc = next_out_arc (arc))
    pass_state_graph (arc->to_state, applied_func);
}

/* This recursive function passes all states of AUTOMATON and applies
   APPLIED_FUNC to them.  */
static void
pass_states (automaton_t automaton, void (*applied_func) (state_t state))
{
  curr_state_graph_pass_num++;
  pass_state_graph (automaton->start_state, applied_func);
}

/* The function initializes code for passing of all states.  */
static void
initiate_pass_states (void)
{
  curr_state_graph_pass_num = 0;
}

/* The following vla is used for storing pointers to all achieved
   states.  */
static vec<state_t> all_achieved_states;

/* This function is called by function pass_states to add an achieved
   STATE.  */
static void
add_achieved_state (state_t state)
{
  all_achieved_states.safe_push (state);
}

/* The function sets up equivalence numbers of insns which mark all
   out arcs of STATE by equiv_class_num_1 (if ODD_ITERATION_FLAG has
   nonzero value) or by equiv_class_num_2 of the destination state.  */
static void
set_out_arc_insns_equiv_num (state_t state, int odd_iteration_flag)
{
  arc_t arc;

  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    {
      gcc_assert (!arc->insn->insn_reserv_decl->equiv_class_num);
      arc->insn->insn_reserv_decl->equiv_class_num
	= (odd_iteration_flag
           ? arc->to_state->equiv_class_num_1
	   : arc->to_state->equiv_class_num_2);
      gcc_assert (arc->insn->insn_reserv_decl->equiv_class_num);
    }
}

/* The function clears equivalence numbers and alt_states in all insns
   which mark all out arcs of STATE.  */
static void
clear_arc_insns_equiv_num (state_t state)
{
  arc_t arc;

  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    arc->insn->insn_reserv_decl->equiv_class_num = 0;
}


/* The following function returns TRUE if STATE reserves the unit with
   UNIT_NUM on the first cycle.  */
static int
first_cycle_unit_presence (state_t state, int unit_num)
{
  alt_state_t alt_state;

  if (state->component_states == NULL)
    return test_unit_reserv (state->reservs, 0, unit_num);
  else
    {
      for (alt_state = state->component_states;
	   alt_state != NULL;
	   alt_state = alt_state->next_sorted_alt_state)
	if (test_unit_reserv (alt_state->state->reservs, 0, unit_num))
	  return true;
    }
  return false;
}

/* This fills in the presence_signature[] member of STATE.  */
static void
cache_presence (state_t state)
{
  int i, num = 0;
  unsigned int sz;
  sz = (description->query_units_num + sizeof (int) * CHAR_BIT - 1)
        / (sizeof (int) * CHAR_BIT);

  state->presence_signature = XCREATENODEVEC (unsigned int, sz);
  for (i = 0; i < description->units_num; i++)
    if (units_array [i]->query_p)
      {
	int presence1_p = first_cycle_unit_presence (state, i);
	state->presence_signature[num / (sizeof (int) * CHAR_BIT)]
	  |= (!!presence1_p) << (num % (sizeof (int) * CHAR_BIT));
	num++;
      }
}

/* The function returns nonzero value if STATE is not equivalent to
   ANOTHER_STATE from the same current partition on equivalence
   classes.  Another state has ANOTHER_STATE_OUT_ARCS_NUM number of
   output arcs.  Iteration of making equivalence partition is defined
   by ODD_ITERATION_FLAG.  */
static int
state_is_differed (state_t state, state_t another_state,
		   int odd_iteration_flag)
{
  arc_t arc;
  unsigned int sz, si;

  gcc_assert (state->num_out_arcs == another_state->num_out_arcs);

  sz = (description->query_units_num + sizeof (int) * CHAR_BIT - 1)
	/ (sizeof (int) * CHAR_BIT);

  for (si = 0; si < sz; si++)
    gcc_assert (state->presence_signature[si]
		== another_state->presence_signature[si]);

  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    {
      if ((odd_iteration_flag
           ? arc->to_state->equiv_class_num_1
	   : arc->to_state->equiv_class_num_2)
          != arc->insn->insn_reserv_decl->equiv_class_num)
        return 1;
    }

  return 0;
}

/* Compares two states pointed to by STATE_PTR_1 and STATE_PTR_2
   and return -1, 0 or 1.  This function can be used as predicate for
   qsort().  It requires the member presence_signature[] of both
   states be filled.  */
static int
compare_states_for_equiv (const void *state_ptr_1,
			  const void *state_ptr_2)
{
  const_state_t const s1 = *(const_state_t const*)state_ptr_1;
  const_state_t const s2 = *(const_state_t const*)state_ptr_2;
  unsigned int sz, si;
  if (s1->num_out_arcs < s2->num_out_arcs)
    return -1;
  else if (s1->num_out_arcs > s2->num_out_arcs)
    return 1;

  sz = (description->query_units_num + sizeof (int) * CHAR_BIT - 1)
	/ (sizeof (int) * CHAR_BIT);

  for (si = 0; si < sz; si++)
    if (s1->presence_signature[si] < s2->presence_signature[si])
      return -1;
    else if (s1->presence_signature[si] > s2->presence_signature[si])
      return 1;
  return 0;
}

/* The function makes initial partition of STATES on equivalent
   classes and saves it into CLASSES.  This function requires the input
   to be sorted via compare_states_for_equiv().  */
static int
init_equiv_class (vec<state_t> states, vec<state_t> *classes)
{
  size_t i;
  state_t prev = 0;
  int class_num = 1;

  classes->create (150);
  for (i = 0; i < states.length (); i++)
    {
      state_t state = states[i];
      if (prev)
        {
	  if (compare_states_for_equiv (&prev, &state) != 0)
	    {
	      classes->safe_push (prev);
	      class_num++;
	      prev = NULL;
	    }
        }
      state->equiv_class_num_1 = class_num;
      state->next_equiv_class_state = prev;
      prev = state;
    }
  if (prev)
    classes->safe_push (prev);
  return class_num;
}

/* The function copies pointers to equivalent states from vla FROM
   into vla TO.  */
static void
copy_equiv_class (vec<state_t> *to, vec<state_t> from)
{
  to->release ();
  *to = from.copy ();
}

/* The function processes equivalence class given by its first state,
   FIRST_STATE, on odd iteration if ODD_ITERATION_FLAG.  If there
   are not equivalent states, the function partitions the class
   removing nonequivalent states and placing them in
   *NEXT_ITERATION_CLASSES, increments *NEW_EQUIV_CLASS_NUM_PTR ans
   assigns it to the state equivalence number.  If the class has been
   partitioned, the function returns nonzero value.  */
static int
partition_equiv_class (state_t first_state, int odd_iteration_flag,
		       vec<state_t> *next_iteration_classes,
		       int *new_equiv_class_num_ptr)
{
  state_t new_equiv_class;
  int partition_p;
  state_t curr_state;
  state_t prev_state;
  state_t next_state;

  partition_p = 0;

  while (first_state != NULL)
    {
      new_equiv_class = NULL;
      if (first_state->next_equiv_class_state != NULL)
	{
	  /* There are more one states in the class equivalence.  */
	  set_out_arc_insns_equiv_num (first_state, odd_iteration_flag);
	  for (prev_state = first_state,
		 curr_state = first_state->next_equiv_class_state;
	       curr_state != NULL;
	       curr_state = next_state)
	    {
	      next_state = curr_state->next_equiv_class_state;
	      if (state_is_differed (curr_state, first_state,
				     odd_iteration_flag))
		{
		  /* Remove curr state from the class equivalence.  */
		  prev_state->next_equiv_class_state = next_state;
		  /* Add curr state to the new class equivalence.  */
		  curr_state->next_equiv_class_state = new_equiv_class;
		  if (new_equiv_class == NULL)
		    (*new_equiv_class_num_ptr)++;
		  if (odd_iteration_flag)
		    curr_state->equiv_class_num_2 = *new_equiv_class_num_ptr;
		  else
		    curr_state->equiv_class_num_1 = *new_equiv_class_num_ptr;
		  new_equiv_class = curr_state;
		  partition_p = 1;
		}
	      else
		prev_state = curr_state;
	    }
	  clear_arc_insns_equiv_num (first_state);
	}
      if (new_equiv_class != NULL)
	next_iteration_classes->safe_push (new_equiv_class);
      first_state = new_equiv_class;
    }
  return partition_p;
}

/* The function finds equivalent states of AUTOMATON.  */
static void
evaluate_equiv_classes (automaton_t automaton, vec<state_t> *equiv_classes)
{
  int new_equiv_class_num;
  int odd_iteration_flag;
  int finish_flag;
  vec<state_t> next_iteration_classes;
  size_t i;

  all_achieved_states.create (1500);
  pass_states (automaton, add_achieved_state);
  pass_states (automaton, cache_presence);
  all_achieved_states.qsort (compare_states_for_equiv);

  odd_iteration_flag = 0;
  new_equiv_class_num = init_equiv_class (all_achieved_states,
  					  &next_iteration_classes);

  do
    {
      odd_iteration_flag = !odd_iteration_flag;
      finish_flag = 1;
      copy_equiv_class (equiv_classes, next_iteration_classes);

      /* Transfer equiv numbers for the next iteration.  */
      for (i = 0; i < all_achieved_states.length (); i++)
	if (odd_iteration_flag)
	  all_achieved_states[i]->equiv_class_num_2
	    = all_achieved_states[i]->equiv_class_num_1;
	else
	  all_achieved_states[i]->equiv_class_num_1
	    = all_achieved_states[i]->equiv_class_num_2;

      for (i = 0; i < equiv_classes->length (); i++)
	if (partition_equiv_class ((*equiv_classes)[i],
				   odd_iteration_flag,
				   &next_iteration_classes,
				   &new_equiv_class_num))
	  finish_flag = 0;
    }
  while (!finish_flag);
  next_iteration_classes.release ();
  all_achieved_states.release ();
}

/* The function merges equivalent states of AUTOMATON.  */
static void
merge_states (automaton_t automaton, vec<state_t> equiv_classes)
{
  state_t curr_state;
  state_t new_state;
  state_t first_class_state;
  alt_state_t alt_states;
  alt_state_t alt_state, new_alt_state;
  arc_t curr_arc;
  arc_t next_arc;
  size_t i;

  /* Create states corresponding to equivalence classes containing two
     or more states.  */
  for (i = 0; i < equiv_classes.length (); i++)
    {
      curr_state = equiv_classes[i];
      if (curr_state->next_equiv_class_state != NULL)
	{
	  /* There are more one states in the class equivalence.  */
	  /* Create new compound state.  */
	  new_state = get_free_state (0, automaton);
	  alt_states = NULL;
	  first_class_state = curr_state;
	  for (curr_state = first_class_state;
	       curr_state != NULL;
	       curr_state = curr_state->next_equiv_class_state)
	    {
	      curr_state->equiv_class_state = new_state;
	      if (curr_state->component_states == NULL)
		{
		  new_alt_state = get_free_alt_state ();
		  new_alt_state->state = curr_state;
		  new_alt_state->next_alt_state = alt_states;
		  alt_states = new_alt_state;
		}
	      else
		for (alt_state = curr_state->component_states;
		     alt_state != NULL;
		     alt_state = alt_state->next_sorted_alt_state)
		  {
		    new_alt_state = get_free_alt_state ();
		    new_alt_state->state = alt_state->state;
		    new_alt_state->next_alt_state = alt_states;
		    alt_states = new_alt_state;
		  }
	    }
	  /* It is important that alt states were sorted before and
	     after merging to have the same querying results.  */
	  new_state->component_states = uniq_sort_alt_states (alt_states);
	}
      else
	curr_state->equiv_class_state = curr_state;
    }

  for (i = 0; i < equiv_classes.length (); i++)
    {
      curr_state = equiv_classes[i];
      if (curr_state->next_equiv_class_state != NULL)
	{
	  first_class_state = curr_state;
	  /* Create new arcs output from the state corresponding to
	     equiv class.  */
	  for (curr_arc = first_out_arc (first_class_state);
	       curr_arc != NULL;
	       curr_arc = next_out_arc (curr_arc))
	    add_arc (first_class_state->equiv_class_state,
		     curr_arc->to_state->equiv_class_state,
		     curr_arc->insn);
	  /* Delete output arcs from states of given class equivalence.  */
	  for (curr_state = first_class_state;
	       curr_state != NULL;
	       curr_state = curr_state->next_equiv_class_state)
	    {
	      if (automaton->start_state == curr_state)
		automaton->start_state = curr_state->equiv_class_state;
	      /* Delete the state and its output arcs.  */
	      for (curr_arc = first_out_arc (curr_state);
		   curr_arc != NULL;
		   curr_arc = next_arc)
		{
		  next_arc = next_out_arc (curr_arc);
		  free_arc (curr_arc);
		}
	    }
	}
      else
	{
	  /* Change `to_state' of arcs output from the state of given
	     equivalence class.  */
	  for (curr_arc = first_out_arc (curr_state);
	       curr_arc != NULL;
	       curr_arc = next_out_arc (curr_arc))
	    curr_arc->to_state = curr_arc->to_state->equiv_class_state;
	}
    }
}

/* The function sets up new_cycle_p for states if there is arc to the
   state marked by advance_cycle_insn_decl.  */
static void
set_new_cycle_flags (state_t state)
{
  arc_t arc;

  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    if (arc->insn->insn_reserv_decl
	== DECL_INSN_RESERV (advance_cycle_insn_decl))
      arc->to_state->new_cycle_p = 1;
}

/* The top level function for minimization of deterministic
   AUTOMATON.  */
static void
minimize_DFA (automaton_t automaton)
{
  auto_vec<state_t> equiv_classes;

  evaluate_equiv_classes (automaton, &equiv_classes);
  merge_states (automaton, equiv_classes);
  pass_states (automaton, set_new_cycle_flags);
}

/* Values of two variables are counted number of states and arcs in an
   automaton.  */
static int curr_counted_states_num;
static int curr_counted_arcs_num;

/* The function is called by function `pass_states' to count states
   and arcs of an automaton.  */
static void
incr_states_and_arcs_nums (state_t state)
{
  arc_t arc;

  curr_counted_states_num++;
  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    curr_counted_arcs_num++;
}

/* The function counts states and arcs of AUTOMATON.  */
static void
count_states_and_arcs (automaton_t automaton, int *states_num,
		       int *arcs_num)
{
  curr_counted_states_num = 0;
  curr_counted_arcs_num = 0;
  pass_states (automaton, incr_states_and_arcs_nums);
  *states_num = curr_counted_states_num;
  *arcs_num = curr_counted_arcs_num;
}

/* The function builds one DFA AUTOMATON for fast pipeline hazards
   recognition after checking and simplifying IR of the
   description.  */
static void
build_automaton (automaton_t automaton)
{
  int states_num;
  int arcs_num;

  ticker_on (&NDFA_time);
  if (progress_flag)
    {
      if (automaton->corresponding_automaton_decl == NULL)
	fprintf (stderr, "Create anonymous automaton");
      else
	fprintf (stderr, "Create automaton `%s'",
		 automaton->corresponding_automaton_decl->name);
      fprintf (stderr, " (1 dot is 100 new states):");
    }
  make_automaton (automaton);
  if (progress_flag)
    fprintf (stderr, " done\n");
  ticker_off (&NDFA_time);
  count_states_and_arcs (automaton, &states_num, &arcs_num);
  automaton->NDFA_states_num = states_num;
  automaton->NDFA_arcs_num = arcs_num;
  ticker_on (&NDFA_to_DFA_time);
  if (progress_flag)
    {
      if (automaton->corresponding_automaton_decl == NULL)
	fprintf (stderr, "Make anonymous DFA");
      else
	fprintf (stderr, "Make DFA `%s'",
		 automaton->corresponding_automaton_decl->name);
      fprintf (stderr, " (1 dot is 100 new states):");
    }
  NDFA_to_DFA (automaton);
  if (progress_flag)
    fprintf (stderr, " done\n");
  ticker_off (&NDFA_to_DFA_time);
  count_states_and_arcs (automaton, &states_num, &arcs_num);
  automaton->DFA_states_num = states_num;
  automaton->DFA_arcs_num = arcs_num;
  if (!no_minimization_flag)
    {
      ticker_on (&minimize_time);
      if (progress_flag)
	{
	  if (automaton->corresponding_automaton_decl == NULL)
	    fprintf (stderr, "Minimize anonymous DFA...");
	  else
	    fprintf (stderr, "Minimize DFA `%s'...",
		     automaton->corresponding_automaton_decl->name);
	}
      minimize_DFA (automaton);
      if (progress_flag)
	fprintf (stderr, "done\n");
      ticker_off (&minimize_time);
      count_states_and_arcs (automaton, &states_num, &arcs_num);
      automaton->minimal_DFA_states_num = states_num;
      automaton->minimal_DFA_arcs_num = arcs_num;
    }
}



/* The page contains code for enumeration  of all states of an automaton.  */

/* Variable used for enumeration of all states of an automaton.  Its
   value is current number of automaton states.  */
static int curr_state_order_num;

/* The function is called by function `pass_states' for enumerating
   states.  */
static void
set_order_state_num (state_t state)
{
  state->order_state_num = curr_state_order_num;
  curr_state_order_num++;
}

/* The function enumerates all states of AUTOMATON.  */
static void
enumerate_states (automaton_t automaton)
{
  curr_state_order_num = 0;
  pass_states (automaton, set_order_state_num);
  automaton->achieved_states_num = curr_state_order_num;
}



/* The page contains code for finding equivalent automaton insns
   (ainsns).  */

/* The function inserts AINSN into cyclic list
   CYCLIC_EQUIV_CLASS_INSN_LIST of ainsns.  */
static ainsn_t
insert_ainsn_into_equiv_class (ainsn_t ainsn,
			       ainsn_t cyclic_equiv_class_insn_list)
{
  if (cyclic_equiv_class_insn_list == NULL)
    ainsn->next_equiv_class_insn = ainsn;
  else
    {
      ainsn->next_equiv_class_insn
        = cyclic_equiv_class_insn_list->next_equiv_class_insn;
      cyclic_equiv_class_insn_list->next_equiv_class_insn = ainsn;
    }
  return ainsn;
}

/* The function deletes equiv_class_insn into cyclic list of
   equivalent ainsns.  */
static void
delete_ainsn_from_equiv_class (ainsn_t equiv_class_insn)
{
  ainsn_t curr_equiv_class_insn;
  ainsn_t prev_equiv_class_insn;

  prev_equiv_class_insn = equiv_class_insn;
  for (curr_equiv_class_insn = equiv_class_insn->next_equiv_class_insn;
       curr_equiv_class_insn != equiv_class_insn;
       curr_equiv_class_insn = curr_equiv_class_insn->next_equiv_class_insn)
    prev_equiv_class_insn = curr_equiv_class_insn;
  if (prev_equiv_class_insn != equiv_class_insn)
    prev_equiv_class_insn->next_equiv_class_insn
      = equiv_class_insn->next_equiv_class_insn;
}

/* The function processes AINSN of a state in order to find equivalent
   ainsns.  INSN_ARCS_ARRAY is table: code of insn -> out arc of the
   state.  */
static void
process_insn_equiv_class (ainsn_t ainsn, arc_t *insn_arcs_array)
{
  ainsn_t next_insn;
  ainsn_t curr_insn;
  ainsn_t cyclic_insn_list;
  arc_t arc;

  gcc_assert (insn_arcs_array [ainsn->insn_reserv_decl->insn_num]);
  curr_insn = ainsn;
  /* New class of ainsns which are not equivalent to given ainsn.  */
  cyclic_insn_list = NULL;
  do
    {
      next_insn = curr_insn->next_equiv_class_insn;
      arc = insn_arcs_array [curr_insn->insn_reserv_decl->insn_num];
      if (arc == NULL
          || (insn_arcs_array [ainsn->insn_reserv_decl->insn_num]->to_state
              != arc->to_state))
        {
          delete_ainsn_from_equiv_class (curr_insn);
          cyclic_insn_list = insert_ainsn_into_equiv_class (curr_insn,
							    cyclic_insn_list);
        }
      curr_insn = next_insn;
    }
  while (curr_insn != ainsn);
}

/* The function processes STATE in order to find equivalent ainsns.  */
static void
process_state_for_insn_equiv_partition (state_t state)
{
  arc_t arc;
  arc_t *insn_arcs_array = XCNEWVEC (arc_t, description->insns_num);

  /* Process insns of the arcs.  */
  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    insn_arcs_array [arc->insn->insn_reserv_decl->insn_num] = arc;
  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    process_insn_equiv_class (arc->insn, insn_arcs_array);

  free (insn_arcs_array);
}

/* The function searches for equivalent ainsns of AUTOMATON.  */
static void
set_insn_equiv_classes (automaton_t automaton)
{
  ainsn_t ainsn;
  ainsn_t first_insn;
  ainsn_t curr_insn;
  ainsn_t cyclic_insn_list;
  ainsn_t insn_with_same_reservs;
  int equiv_classes_num;

  /* All insns are included in one equivalence class.  */
  cyclic_insn_list = NULL;
  for (ainsn = automaton->ainsn_list; ainsn != NULL; ainsn = ainsn->next_ainsn)
    if (ainsn->first_insn_with_same_reservs)
      cyclic_insn_list = insert_ainsn_into_equiv_class (ainsn,
							cyclic_insn_list);
  /* Process insns in order to make equivalence partition.  */
  pass_states (automaton, process_state_for_insn_equiv_partition);
  /* Enumerate equiv classes.  */
  for (ainsn = automaton->ainsn_list; ainsn != NULL; ainsn = ainsn->next_ainsn)
    /* Set undefined value.  */
    ainsn->insn_equiv_class_num = -1;
  equiv_classes_num = 0;
  for (ainsn = automaton->ainsn_list; ainsn != NULL; ainsn = ainsn->next_ainsn)
    if (ainsn->insn_equiv_class_num < 0)
      {
        first_insn = ainsn;
        gcc_assert (first_insn->first_insn_with_same_reservs);
        first_insn->first_ainsn_with_given_equivalence_num = 1;
        curr_insn = first_insn;
        do
          {
            for (insn_with_same_reservs = curr_insn;
                 insn_with_same_reservs != NULL;
                 insn_with_same_reservs
		   = insn_with_same_reservs->next_same_reservs_insn)
              insn_with_same_reservs->insn_equiv_class_num = equiv_classes_num;
            curr_insn = curr_insn->next_equiv_class_insn;
          }
        while (curr_insn != first_insn);
        equiv_classes_num++;
      }
  automaton->insn_equiv_classes_num = equiv_classes_num;
}



/* This page contains code for creating DFA(s) and calls functions
   building them.  */


/* The following value is used to prevent floating point overflow for
   estimating an automaton bound.  The value should be less DBL_MAX on
   the host machine.  We use here approximate minimum of maximal
   double floating point value required by ANSI C standard.  It
   will work for non ANSI sun compiler too.  */

#define MAX_FLOATING_POINT_VALUE_FOR_AUTOMATON_BOUND  1.0E37

/* The function estimate size of the single DFA used by PHR (pipeline
   hazards recognizer).  */
static double
estimate_one_automaton_bound (void)
{
  decl_t decl;
  double one_automaton_estimation_bound;
  double root_value;
  int i;

  one_automaton_estimation_bound = 1.0;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	{
	  root_value = exp (log (DECL_UNIT (decl)->max_occ_cycle_num
				 - DECL_UNIT (decl)->min_occ_cycle_num + 1.0)
                            / automata_num);
	  if (MAX_FLOATING_POINT_VALUE_FOR_AUTOMATON_BOUND / root_value
	      > one_automaton_estimation_bound)
	    one_automaton_estimation_bound *= root_value;
	}
    }
  return one_automaton_estimation_bound;
}

/* The function compares unit declarations according to their maximal
   cycle in reservations.  */
static int
compare_max_occ_cycle_nums (const void *unit_decl_1,
			    const void *unit_decl_2)
{
  if ((DECL_UNIT (*(const_decl_t const*) unit_decl_1)->max_occ_cycle_num)
      < (DECL_UNIT (*(const_decl_t const*) unit_decl_2)->max_occ_cycle_num))
    return 1;
  else if ((DECL_UNIT (*(const_decl_t const*) unit_decl_1)->max_occ_cycle_num)
	   == (DECL_UNIT (*(const_decl_t const*) unit_decl_2)->max_occ_cycle_num))
    return 0;
  else
    return -1;
}

/* The function makes heuristic assigning automata to units.  Actually
   efficacy of the algorithm has been checked yet??? */

static void
units_to_automata_heuristic_distr (void)
{
  double estimation_bound;
  int automaton_num;
  int rest_units_num;
  double bound_value;
  unit_decl_t *unit_decls;
  int i, j;

  if (description->units_num == 0)
    return;
  estimation_bound = estimate_one_automaton_bound ();
  unit_decls = XNEWVEC (unit_decl_t, description->units_num);

  for (i = 0, j = 0; i < description->decls_num; i++)
    if (description->decls[i]->mode == dm_unit)
      unit_decls[j++] = DECL_UNIT (description->decls[i]);
  gcc_assert (j == description->units_num);

  qsort (unit_decls, description->units_num,
         sizeof (unit_decl_t), compare_max_occ_cycle_nums);

  automaton_num = 0;
  bound_value = unit_decls[0]->max_occ_cycle_num;
  unit_decls[0]->corresponding_automaton_num = automaton_num;

  for (i = 1; i < description->units_num; i++)
    {
      rest_units_num = description->units_num - i + 1;
      gcc_assert (automata_num - automaton_num - 1 <= rest_units_num);
      if (automaton_num < automata_num - 1
          && ((automata_num - automaton_num - 1 == rest_units_num)
              || (bound_value
                  > (estimation_bound
		     / unit_decls[i]->max_occ_cycle_num))))
        {
          bound_value = unit_decls[i]->max_occ_cycle_num;
          automaton_num++;
        }
      else
        bound_value *= unit_decls[i]->max_occ_cycle_num;
      unit_decls[i]->corresponding_automaton_num = automaton_num;
    }
  gcc_assert (automaton_num == automata_num - 1);
  free (unit_decls);
}

/* The functions creates automaton insns for each automata.  Automaton
   insn is simply insn for given automaton which makes reservation
   only of units of the automaton.  */
static void
create_ainsns (automaton_t automaton)
{
  decl_t decl;
  ainsn_t first_ainsn;
  ainsn_t curr_ainsn;
  ainsn_t prev_ainsn;
  int i;

  first_ainsn = NULL;
  prev_ainsn = NULL;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	{
	  curr_ainsn = XCREATENODE (struct ainsn);
	  curr_ainsn->insn_reserv_decl = DECL_INSN_RESERV (decl);
	  curr_ainsn->important_p = FALSE;
	  curr_ainsn->next_ainsn = NULL;
	  if (prev_ainsn == NULL)
	    first_ainsn = curr_ainsn;
	  else
	    prev_ainsn->next_ainsn = curr_ainsn;
	  if (decl == advance_cycle_insn_decl)
	    automaton->advance_ainsn = curr_ainsn;
	  else if (decl == collapse_ndfa_insn_decl)
	    automaton->collapse_ainsn = curr_ainsn;
	  prev_ainsn = curr_ainsn;
	}
    }
  automaton->ainsn_list = first_ainsn;
}

/* The function assigns automata to units according to constructions
   `define_automaton' in the description.  */
static void
units_to_automata_distr (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	{
	  if (DECL_UNIT (decl)->automaton_decl == NULL
	      || (DECL_UNIT (decl)->automaton_decl->corresponding_automaton
		  == NULL))
	    /* Distribute to the first automaton.  */
	    DECL_UNIT (decl)->corresponding_automaton_num = 0;
	  else
	    DECL_UNIT (decl)->corresponding_automaton_num
	      = (DECL_UNIT (decl)->automaton_decl
                 ->corresponding_automaton->automaton_order_num);
	}
    }
}

/* The function creates DFA(s) for fast pipeline hazards recognition
   after checking and simplifying IR of the description.  */
static void
create_automata (void)
{
  automaton_t curr_automaton;
  automaton_t prev_automaton;
  decl_t decl;
  int curr_automaton_num;
  int i;

  if (automata_num != 0)
    {
      units_to_automata_heuristic_distr ();
      for (prev_automaton = NULL, curr_automaton_num = 0;
           curr_automaton_num < automata_num;
           curr_automaton_num++, prev_automaton = curr_automaton)
        {
	  curr_automaton = XCREATENODE (struct automaton);
	  create_ainsns (curr_automaton);
	  curr_automaton->corresponding_automaton_decl = NULL;
	  curr_automaton->next_automaton = NULL;
          curr_automaton->automaton_order_num = curr_automaton_num;
          if (prev_automaton == NULL)
            description->first_automaton = curr_automaton;
          else
            prev_automaton->next_automaton = curr_automaton;
        }
    }
  else
    {
      curr_automaton_num = 0;
      prev_automaton = NULL;
      for (i = 0; i < description->decls_num; i++)
	{
	  decl = description->decls [i];
	  if (decl->mode == dm_automaton
	      && DECL_AUTOMATON (decl)->automaton_is_used)
	    {
	      curr_automaton = XCREATENODE (struct automaton);
	      create_ainsns (curr_automaton);
	      curr_automaton->corresponding_automaton_decl
		= DECL_AUTOMATON (decl);
	      curr_automaton->next_automaton = NULL;
	      DECL_AUTOMATON (decl)->corresponding_automaton = curr_automaton;
	      curr_automaton->automaton_order_num = curr_automaton_num;
	      if (prev_automaton == NULL)
		description->first_automaton = curr_automaton;
	      else
		prev_automaton->next_automaton = curr_automaton;
	      curr_automaton_num++;
	      prev_automaton = curr_automaton;
	    }
	}
      if (curr_automaton_num == 0)
	{
	  curr_automaton = XCREATENODE (struct automaton);
	  create_ainsns (curr_automaton);
	  curr_automaton->corresponding_automaton_decl = NULL;
	  curr_automaton->next_automaton = NULL;
	  description->first_automaton = curr_automaton;
	}
      units_to_automata_distr ();
    }
  NDFA_time = create_ticker ();
  ticker_off (&NDFA_time);
  NDFA_to_DFA_time = create_ticker ();
  ticker_off (&NDFA_to_DFA_time);
  minimize_time = create_ticker ();
  ticker_off (&minimize_time);
  equiv_time = create_ticker ();
  ticker_off (&equiv_time);
  for (curr_automaton = description->first_automaton;
       curr_automaton != NULL;
       curr_automaton = curr_automaton->next_automaton)
    {
      if (progress_flag)
	{
	  if (curr_automaton->corresponding_automaton_decl == NULL)
	    fprintf (stderr, "Prepare anonymous automaton creation ... ");
	  else
	    fprintf (stderr, "Prepare automaton `%s' creation...",
		     curr_automaton->corresponding_automaton_decl->name);
	}
      create_alt_states (curr_automaton);
      form_ainsn_with_same_reservs (curr_automaton);
      if (progress_flag)
	fprintf (stderr, "done\n");
      build_automaton (curr_automaton);
      enumerate_states (curr_automaton);
      ticker_on (&equiv_time);
      set_insn_equiv_classes (curr_automaton);
      ticker_off (&equiv_time);
    }
}



/* This page contains code for forming string representation of
   regexp.  The representation is formed on IR obstack.  So you should
   not work with IR obstack between regexp_representation and
   finish_regexp_representation calls.  */

/* This recursive function forms string representation of regexp
   (without tailing '\0').  */
static void
form_regexp (regexp_t regexp)
{
  int i;

  switch (regexp->mode)
    {
    case rm_unit: case rm_reserv:
      {
	const char *name = (regexp->mode == rm_unit
			    ? REGEXP_UNIT (regexp)->name
			    : REGEXP_RESERV (regexp)->name);

	obstack_grow (&irp, name, strlen (name));
	break;
      }

    case rm_sequence:
      for (i = 0; i < REGEXP_SEQUENCE (regexp)->regexps_num; i++)
	{
	  if (i != 0)
	    obstack_1grow (&irp, ',');
	  form_regexp (REGEXP_SEQUENCE (regexp)->regexps [i]);
	}
      break;

    case rm_allof:
      obstack_1grow (&irp, '(');
      for (i = 0; i < REGEXP_ALLOF (regexp)->regexps_num; i++)
	{
	  if (i != 0)
            obstack_1grow (&irp, '+');
	  if (REGEXP_ALLOF (regexp)->regexps[i]->mode == rm_sequence
              || REGEXP_ALLOF (regexp)->regexps[i]->mode == rm_oneof)
            obstack_1grow (&irp, '(');
	  form_regexp (REGEXP_ALLOF (regexp)->regexps [i]);
	  if (REGEXP_ALLOF (regexp)->regexps[i]->mode == rm_sequence
              || REGEXP_ALLOF (regexp)->regexps[i]->mode == rm_oneof)
            obstack_1grow (&irp, ')');
        }
      obstack_1grow (&irp, ')');
      break;

    case rm_oneof:
      for (i = 0; i < REGEXP_ONEOF (regexp)->regexps_num; i++)
	{
	  if (i != 0)
	    obstack_1grow (&irp, '|');
	  if (REGEXP_ONEOF (regexp)->regexps[i]->mode == rm_sequence)
	    obstack_1grow (&irp, '(');
	  form_regexp (REGEXP_ONEOF (regexp)->regexps [i]);
	  if (REGEXP_ONEOF (regexp)->regexps[i]->mode == rm_sequence)
          obstack_1grow (&irp, ')');
	}
      break;

    case rm_repeat:
      {
	char digits [30];

	if (REGEXP_REPEAT (regexp)->regexp->mode == rm_sequence
	    || REGEXP_REPEAT (regexp)->regexp->mode == rm_allof
	    || REGEXP_REPEAT (regexp)->regexp->mode == rm_oneof)
	  obstack_1grow (&irp, '(');
	form_regexp (REGEXP_REPEAT (regexp)->regexp);
	if (REGEXP_REPEAT (regexp)->regexp->mode == rm_sequence
	    || REGEXP_REPEAT (regexp)->regexp->mode == rm_allof
	    || REGEXP_REPEAT (regexp)->regexp->mode == rm_oneof)
	  obstack_1grow (&irp, ')');
	sprintf (digits, "*%d", REGEXP_REPEAT (regexp)->repeat_num);
	obstack_grow (&irp, digits, strlen (digits));
	break;
      }

    case rm_nothing:
      obstack_grow (&irp, NOTHING_NAME, strlen (NOTHING_NAME));
      break;

    default:
      gcc_unreachable ();
    }
}

/* The function returns string representation of REGEXP on IR
   obstack.  */
static const char *
regexp_representation (regexp_t regexp)
{
  form_regexp (regexp);
  obstack_1grow (&irp, '\0');
  return (char *) obstack_base (&irp);
}

/* The function frees memory allocated for last formed string
   representation of regexp.  */
static void
finish_regexp_representation (void)
{
  int length = obstack_object_size (&irp);

  obstack_blank_fast (&irp, -length);
}



/* This page contains code for output PHR (pipeline hazards recognizer).  */

/* The function outputs minimal C type which is sufficient for
   representation numbers in range min_range_value and
   max_range_value.  Because host machine and build machine may be
   different, we use here minimal values required by ANSI C standard
   instead of UCHAR_MAX, SHRT_MAX, SHRT_MIN, etc.  This is a good
   approximation.  */

static void
output_range_type (FILE *f, long int min_range_value,
		   long int max_range_value)
{
  if (min_range_value >= 0 && max_range_value <= 255)
    fprintf (f, "unsigned char");
  else if (min_range_value >= -127 && max_range_value <= 127)
    fprintf (f, "signed char");
  else if (min_range_value >= 0 && max_range_value <= 65535)
    fprintf (f, "unsigned short");
  else if (min_range_value >= -32767 && max_range_value <= 32767)
    fprintf (f, "short");
  else
    fprintf (f, "int");
}

/* The function outputs all initialization values of VECT.  */
static void
output_vect (vla_hwint_t vect)
{
  int els_on_line;
  size_t vect_length = vect.length ();
  size_t i;

  els_on_line = 1;
  if (vect_length == 0)
    fputs ("0 /* This is dummy el because the vect is empty */", output_file);
  else
    for (i = 0; i < vect_length; i++)
      {
	fprintf (output_file, "%5ld", (long) vect[i]);
	if (els_on_line == 10)
	  {
	    els_on_line = 0;
	    fputs (",\n", output_file);
	  }
	else if (i < vect_length-1)
	  fputs (", ", output_file);
	els_on_line++;
      }
}

/* The following is name of the structure which represents DFA(s) for
   PHR.  */
#define CHIP_NAME "DFA_chip"

/* The following is name of member which represents state of a DFA for
   PHR.  */
static void
output_chip_member_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "automaton_state_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_automaton_state",
             automaton->corresponding_automaton_decl->name);
}

/* The following is name of temporary variable which stores state of a
   DFA for PHR.  */
static void
output_temp_chip_member_name (FILE *f, automaton_t automaton)
{
  fprintf (f, "_");
  output_chip_member_name (f, automaton);
}

/* This is name of macro value which is code of pseudo_insns
   representing advancing cpu cycle and collapsing the NDFA.
   Its value is used as internal code unknown insn.  */
#define ADVANCE_CYCLE_VALUE_NAME "DFA__ADVANCE_CYCLE"
#define COLLAPSE_NDFA_VALUE_NAME "NDFA__COLLAPSE"

/* Output name of translate vector for given automaton.  */
static void
output_translate_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "translate_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_translate", automaton->corresponding_automaton_decl->name);
}

/* Output name for simple transition table representation.  */
static void
output_trans_full_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "transitions_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_transitions",
	     automaton->corresponding_automaton_decl->name);
}

/* Output name of comb vector of the transition table for given
   automaton.  */
static void
output_trans_comb_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "transitions_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_transitions",
             automaton->corresponding_automaton_decl->name);
}

/* Output name of check vector of the transition table for given
   automaton.  */
static void
output_trans_check_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "check_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_check", automaton->corresponding_automaton_decl->name);
}

/* Output name of base vector of the transition table for given
   automaton.  */
static void
output_trans_base_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "base_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_base", automaton->corresponding_automaton_decl->name);
}

/* Output name of simple min issue delay table representation.  */
static void
output_min_issue_delay_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "min_issue_delay_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_min_issue_delay",
             automaton->corresponding_automaton_decl->name);
}

/* Output name of deadlock vector for given automaton.  */
static void
output_dead_lock_vect_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "dead_lock_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_dead_lock", automaton->corresponding_automaton_decl->name);
}

/* Output name of reserved units table for AUTOMATON into file F.  */
static void
output_reserved_units_table_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "reserved_units_%d", automaton->automaton_order_num);
  else
    fprintf (f, "%s_reserved_units",
	     automaton->corresponding_automaton_decl->name);
}

/* Name of the PHR interface macro.  */
#define CPU_UNITS_QUERY_MACRO_NAME "CPU_UNITS_QUERY"

/* Names of an internal functions: */
#define INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME "internal_min_issue_delay"

/* This is external type of DFA(s) state.  */
#define STATE_TYPE_NAME "state_t"

#define INTERNAL_TRANSITION_FUNC_NAME "internal_state_transition"

#define INTERNAL_RESET_FUNC_NAME "internal_reset"

#define INTERNAL_DEAD_LOCK_FUNC_NAME "internal_state_dead_lock_p"

#define INTERNAL_INSN_LATENCY_FUNC_NAME "internal_insn_latency"

/* Name of cache of insn dfa codes.  */
#define DFA_INSN_CODES_VARIABLE_NAME "dfa_insn_codes"

/* Name of length of cache of insn dfa codes.  */
#define DFA_INSN_CODES_LENGTH_VARIABLE_NAME "dfa_insn_codes_length"

/* Names of the PHR interface functions: */
#define SIZE_FUNC_NAME "state_size"

#define TRANSITION_FUNC_NAME "state_transition"

#define MIN_ISSUE_DELAY_FUNC_NAME "min_issue_delay"

#define MIN_INSN_CONFLICT_DELAY_FUNC_NAME "min_insn_conflict_delay"

#define DEAD_LOCK_FUNC_NAME "state_dead_lock_p"

#define RESET_FUNC_NAME "state_reset"

#define INSN_LATENCY_FUNC_NAME "insn_latency"

#define PRINT_RESERVATION_FUNC_NAME "print_reservation"

#define GET_CPU_UNIT_CODE_FUNC_NAME "get_cpu_unit_code"

#define CPU_UNIT_RESERVATION_P_FUNC_NAME "cpu_unit_reservation_p"

#define INSN_HAS_DFA_RESERVATION_P_FUNC_NAME "insn_has_dfa_reservation_p"

#define DFA_CLEAN_INSN_CACHE_FUNC_NAME  "dfa_clean_insn_cache"

#define DFA_CLEAR_SINGLE_INSN_CACHE_FUNC_NAME "dfa_clear_single_insn_cache"

#define DFA_START_FUNC_NAME  "dfa_start"

#define DFA_FINISH_FUNC_NAME "dfa_finish"

/* Names of parameters of the PHR interface functions.  */
#define STATE_NAME "state"

#define INSN_PARAMETER_NAME "insn"

#define INSN2_PARAMETER_NAME "insn2"

#define CHIP_PARAMETER_NAME "chip"

#define FILE_PARAMETER_NAME "f"

#define CPU_UNIT_NAME_PARAMETER_NAME "cpu_unit_name"

#define CPU_CODE_PARAMETER_NAME "cpu_unit_code"

/* Names of the variables whose values are internal insn code of rtx
   insn.  */
#define INTERNAL_INSN_CODE_NAME "insn_code"

#define INTERNAL_INSN2_CODE_NAME "insn2_code"

/* Names of temporary variables in some functions.  */
#define TEMPORARY_VARIABLE_NAME "temp"

#define I_VARIABLE_NAME "i"

/* Name of result variable in some functions.  */
#define RESULT_VARIABLE_NAME "res"

/* Name of function (attribute) to translate insn into internal insn
   code.  */
#define INTERNAL_DFA_INSN_CODE_FUNC_NAME "internal_dfa_insn_code"

/* Name of function (attribute) to translate insn into internal insn
   code with caching.  */
#define DFA_INSN_CODE_FUNC_NAME "dfa_insn_code"

/* Output C type which is used for representation of codes of states
   of AUTOMATON.  */
static void
output_state_member_type (FILE *f, automaton_t automaton)
{
  output_range_type (f, 0, automaton->achieved_states_num);
}

/* Output definition of the structure representing current DFA(s)
   state(s).  */
static void
output_chip_definitions (void)
{
  automaton_t automaton;

  fprintf (output_file, "struct %s\n{\n", CHIP_NAME);
  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      fprintf (output_file, "  ");
      output_state_member_type (output_file, automaton);
      fprintf (output_file, " ");
      output_chip_member_name (output_file, automaton);
      fprintf (output_file, ";\n");
    }
  fprintf (output_file, "};\n\n");
#if 0
  fprintf (output_file, "static struct %s %s;\n\n", CHIP_NAME, CHIP_NAME);
#endif
}


/* The function outputs translate vector of internal insn code into
   insn equivalence class number.  The equivalence class number is
   used to access to table and vectors representing DFA(s).  */
static void
output_translate_vect (automaton_t automaton)
{
  ainsn_t ainsn;
  int insn_value;
  vla_hwint_t translate_vect;

  translate_vect.create (description->insns_num);

  for (insn_value = 0; insn_value < description->insns_num; insn_value++)
    /* Undefined value */
    translate_vect.quick_push (automaton->insn_equiv_classes_num);

  for (ainsn = automaton->ainsn_list; ainsn != NULL; ainsn = ainsn->next_ainsn)
    translate_vect[ainsn->insn_reserv_decl->insn_num] =
	  ainsn->insn_equiv_class_num;

  fprintf (output_file,
           "/* Vector translating external insn codes to internal ones.*/\n");
  fprintf (output_file, "static const ");
  output_range_type (output_file, 0, automaton->insn_equiv_classes_num);
  fprintf (output_file, " ");
  output_translate_vect_name (output_file, automaton);
  fprintf (output_file, "[] ATTRIBUTE_UNUSED = {\n");
  output_vect (translate_vect);
  fprintf (output_file, "};\n\n");
  translate_vect.release ();
}

/* The value in a table state x ainsn -> something which represents
   undefined value.  */
static int undefined_vect_el_value;

/* The following function returns nonzero value if the best
   representation of the table is comb vector.  */
static int
comb_vect_p (state_ainsn_table_t tab)
{
  if (no_comb_flag)
    return false;
  return  (2 * tab->full_vect.length () > 5 * tab->comb_vect.length ());
}

/* The following function creates new table for AUTOMATON.  */
static state_ainsn_table_t
create_state_ainsn_table (automaton_t automaton)
{
  state_ainsn_table_t tab;
  int full_vect_length;
  int i;

  tab = XCREATENODE (struct state_ainsn_table);
  tab->automaton = automaton;

  tab->comb_vect.create (10000);
  tab->check_vect.create (10000);

  tab->base_vect.create (0);
  tab->base_vect.safe_grow (automaton->achieved_states_num, true);

  full_vect_length = (automaton->insn_equiv_classes_num
                      * automaton->achieved_states_num);
  tab->full_vect.create (full_vect_length);
  for (i = 0; i < full_vect_length; i++)
    tab->full_vect.quick_push (undefined_vect_el_value);

  tab->min_base_vect_el_value = 0;
  tab->max_base_vect_el_value = 0;
  tab->min_comb_vect_el_value = 0;
  tab->max_comb_vect_el_value = 0;
  return tab;
}

/* The following function outputs the best C representation of the
   table TAB of given TABLE_NAME.  */
static void
output_state_ainsn_table (state_ainsn_table_t tab, const char *table_name,
			  void (*output_full_vect_name_func) (FILE *, automaton_t),
			  void (*output_comb_vect_name_func) (FILE *, automaton_t),
			  void (*output_check_vect_name_func) (FILE *, automaton_t),
			  void (*output_base_vect_name_func) (FILE *, automaton_t))
{
  if (!comb_vect_p (tab))
    {
      fprintf (output_file, "/* Vector for %s.  */\n", table_name);
      fprintf (output_file, "static const ");
      output_range_type (output_file, tab->min_comb_vect_el_value,
                         tab->max_comb_vect_el_value);
      fprintf (output_file, " ");
      (*output_full_vect_name_func) (output_file, tab->automaton);
      fprintf (output_file, "[] ATTRIBUTE_UNUSED = {\n");
      output_vect (tab->full_vect);
      fprintf (output_file, "};\n\n");
    }
  else
    {
      fprintf (output_file, "/* Comb vector for %s.  */\n", table_name);
      fprintf (output_file, "static const ");
      output_range_type (output_file, tab->min_comb_vect_el_value,
                         tab->max_comb_vect_el_value);
      fprintf (output_file, " ");
      (*output_comb_vect_name_func) (output_file, tab->automaton);
      fprintf (output_file, "[] ATTRIBUTE_UNUSED = {\n");
      output_vect (tab->comb_vect);
      fprintf (output_file, "};\n\n");
      fprintf (output_file, "/* Check vector for %s.  */\n", table_name);
      fprintf (output_file, "static const ");
      output_range_type (output_file, 0, tab->automaton->achieved_states_num);
      fprintf (output_file, " ");
      (*output_check_vect_name_func) (output_file, tab->automaton);
      fprintf (output_file, "[] = {\n");
      output_vect (tab->check_vect);
      fprintf (output_file, "};\n\n");
      fprintf (output_file, "/* Base vector for %s.  */\n", table_name);
      fprintf (output_file, "static const ");
      output_range_type (output_file, tab->min_base_vect_el_value,
                         tab->max_base_vect_el_value);
      fprintf (output_file, " ");
      (*output_base_vect_name_func) (output_file, tab->automaton);
      fprintf (output_file, "[] = {\n");
      output_vect (tab->base_vect);
      fprintf (output_file, "};\n\n");
    }
}

/* The following function adds vector VECT to table TAB as its line
   with number VECT_NUM.  */
static void
add_vect (state_ainsn_table_t tab, int vect_num, vla_hwint_t vect)
{
  int vect_length;
  size_t real_vect_length;
  int comb_vect_index;
  int comb_vect_els_num;
  int vect_index;
  int first_unempty_vect_index;
  int additional_els_num;
  int no_state_value;
  vect_el_t vect_el;
  int i;
  unsigned long vect_mask, comb_vect_mask;

  vect_length = vect.length ();
  gcc_assert (vect_length);
  gcc_assert (vect.last () != undefined_vect_el_value);
  real_vect_length = tab->automaton->insn_equiv_classes_num;
  /* Form full vector in the table: */
  {
    size_t full_base = tab->automaton->insn_equiv_classes_num * vect_num;
    if (tab->full_vect.length () < full_base + vect_length)
      tab->full_vect.safe_grow (full_base + vect_length, true);
    for (i = 0; i < vect_length; i++)
      tab->full_vect[full_base + i] = vect[i];
  }

  /* The comb_vect min/max values are also used for the full vector, so
     compute them now.  */
  for (vect_index = 0; vect_index < vect_length; vect_index++)
    if (vect[vect_index] != undefined_vect_el_value)
      {
	vect_el_t x = vect[vect_index];
        gcc_assert (x >= 0);
        if (tab->max_comb_vect_el_value < x)
          tab->max_comb_vect_el_value = x;
        if (tab->min_comb_vect_el_value > x)
          tab->min_comb_vect_el_value = x;
      }
  if (no_comb_flag)
    return;

  /* Form comb vector in the table: */
  gcc_assert (tab->comb_vect.length () == tab->check_vect.length ());

  comb_vect_els_num = tab->comb_vect.length ();
  for (first_unempty_vect_index = 0;
       first_unempty_vect_index < vect_length;
       first_unempty_vect_index++)
    if (vect[first_unempty_vect_index]
	!= undefined_vect_el_value)
      break;

  /* Search for the place in comb vect for the inserted vect.  */

  /* Slow case.  */
  if (vect_length - first_unempty_vect_index >= SIZEOF_LONG * CHAR_BIT)
    {
      for (comb_vect_index = 0;
           comb_vect_index < comb_vect_els_num;
           comb_vect_index++)
        {
          for (vect_index = first_unempty_vect_index;
               vect_index < vect_length
               && vect_index + comb_vect_index < comb_vect_els_num;
               vect_index++)
            if (vect[vect_index]
		!= undefined_vect_el_value
                && (tab->comb_vect[vect_index + comb_vect_index]
		    != undefined_vect_el_value))
              break;
          if (vect_index >= vect_length
              || vect_index + comb_vect_index >= comb_vect_els_num)
            break;
        }
      goto found;
    }

  /* Fast case.  */
  vect_mask = 0;
  for (vect_index = first_unempty_vect_index;
       vect_index < vect_length;
       vect_index++)
    {
      vect_mask = vect_mask << 1;
      if (vect[vect_index] != undefined_vect_el_value)
	vect_mask |= 1;
    }

  /* Search for the place in comb vect for the inserted vect.  */
  comb_vect_index = 0;
  if (comb_vect_els_num == 0)
    goto found;

  comb_vect_mask = 0;
  for (vect_index = first_unempty_vect_index;
       vect_index < vect_length && vect_index < comb_vect_els_num;
       vect_index++)
    {
      comb_vect_mask <<= 1;
      if (vect_index + comb_vect_index < comb_vect_els_num
	  && tab->comb_vect[vect_index + comb_vect_index]
	     != undefined_vect_el_value)
	comb_vect_mask |= 1;
    }
  if ((vect_mask & comb_vect_mask) == 0)
    goto found;

  for (comb_vect_index = 1, i = vect_length; i < comb_vect_els_num;
       comb_vect_index++, i++)
    {
      comb_vect_mask = (comb_vect_mask << 1) | 1;
      comb_vect_mask ^= (tab->comb_vect[i]
			 == undefined_vect_el_value);
      if ((vect_mask & comb_vect_mask) == 0)
	goto found;
    }
  for ( ; comb_vect_index < comb_vect_els_num; comb_vect_index++)
    {
      comb_vect_mask <<= 1;
      if ((vect_mask & comb_vect_mask) == 0)
	goto found;
    }

 found:
  /* Slot was found.  */
  additional_els_num = comb_vect_index + real_vect_length - comb_vect_els_num;
  if (additional_els_num < 0)
    additional_els_num = 0;
  /* Expand comb and check vectors.  */
  vect_el = undefined_vect_el_value;
  no_state_value = tab->automaton->achieved_states_num;
  while (additional_els_num > 0)
    {
      tab->comb_vect.safe_push (vect_el);
      tab->check_vect.safe_push (no_state_value);
      additional_els_num--;
    }
  gcc_assert (tab->comb_vect.length ()
	      >= comb_vect_index + real_vect_length);
  /* Fill comb and check vectors.  */
  for (vect_index = 0; vect_index < vect_length; vect_index++)
    if (vect[vect_index] != undefined_vect_el_value)
      {
	vect_el_t x = vect[vect_index];
        gcc_assert (tab->comb_vect[comb_vect_index + vect_index]
		    == undefined_vect_el_value);
        gcc_assert (x >= 0);
	tab->comb_vect[comb_vect_index + vect_index] = x;
	tab->check_vect[comb_vect_index + vect_index] = vect_num;
      }
  if (tab->max_comb_vect_el_value < undefined_vect_el_value)
    tab->max_comb_vect_el_value = undefined_vect_el_value;
  if (tab->min_comb_vect_el_value > undefined_vect_el_value)
    tab->min_comb_vect_el_value = undefined_vect_el_value;
  if (tab->max_base_vect_el_value < comb_vect_index)
    tab->max_base_vect_el_value = comb_vect_index;
  if (tab->min_base_vect_el_value > comb_vect_index)
    tab->min_base_vect_el_value = comb_vect_index;

  tab->base_vect[vect_num] = comb_vect_index;
}

/* Return number of out arcs of STATE.  */
static int
out_state_arcs_num (const_state_t state)
{
  int result;
  arc_t arc;

  result = 0;
  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    {
      gcc_assert (arc->insn);
      if (arc->insn->first_ainsn_with_given_equivalence_num)
        result++;
    }
  return result;
}

/* Compare number of possible transitions from the states.  */
static int
compare_transition_els_num (const void *state_ptr_1,
			    const void *state_ptr_2)
{
  const int transition_els_num_1
    = out_state_arcs_num (*(const_state_t const*) state_ptr_1);
  const int transition_els_num_2
    = out_state_arcs_num (*(const_state_t const*) state_ptr_2);

  if (transition_els_num_1 < transition_els_num_2)
    return 1;
  else if (transition_els_num_1 == transition_els_num_2)
    return 0;
  else
    return -1;
}

/* The function adds element EL_VALUE to vector VECT for a table state
   x AINSN.  */
static void
add_vect_el (vla_hwint_t &vect, ainsn_t ainsn, int el_value)
{
  int equiv_class_num;
  int vect_index;

  gcc_assert (ainsn);
  equiv_class_num = ainsn->insn_equiv_class_num;
  for (vect_index = vect.length ();
       vect_index <= equiv_class_num;
       vect_index++)
    vect.safe_push (undefined_vect_el_value);
  vect[equiv_class_num] = el_value;
}

/* This is for forming vector of states of an automaton.  */
static vec<state_t> output_states_vect;

/* The function is called by function pass_states.  The function adds
   STATE to `output_states_vect'.  */
static void
add_states_vect_el (state_t state)
{
  output_states_vect.safe_push (state);
}

/* Form and output vectors (comb, check, base or full vector)
   representing transition table of AUTOMATON.  */
static void
output_trans_table (automaton_t automaton)
{
  size_t i;
  arc_t arc;
  vla_hwint_t transition_vect = vla_hwint_t ();

  undefined_vect_el_value = automaton->achieved_states_num;
  automaton->trans_table = create_state_ainsn_table (automaton);
  /* Create vect of pointers to states ordered by num of transitions
     from the state (state with the maximum num is the first).  */
  output_states_vect.create (0);
  pass_states (automaton, add_states_vect_el);
  output_states_vect.qsort (compare_transition_els_num);

  for (i = 0; i < output_states_vect.length (); i++)
    {
      transition_vect.truncate (0);
      for (arc = first_out_arc (output_states_vect[i]);
	   arc != NULL;
	   arc = next_out_arc (arc))
        {
          gcc_assert (arc->insn);
          if (arc->insn->first_ainsn_with_given_equivalence_num)
            add_vect_el (transition_vect, arc->insn,
                         arc->to_state->order_state_num);
        }
      add_vect (automaton->trans_table,
		output_states_vect[i]->order_state_num,
		transition_vect);
    }
  output_state_ainsn_table
    (automaton->trans_table, "state transitions",
     output_trans_full_vect_name, output_trans_comb_vect_name,
     output_trans_check_vect_name, output_trans_base_vect_name);

  output_states_vect.release ();
  transition_vect.release ();
}

/* Form and output vectors representing minimal issue delay table of
   AUTOMATON.  The table is state x ainsn -> minimal issue delay of
   the ainsn.  */
static void
output_min_issue_delay_table (automaton_t automaton)
{
  vla_hwint_t min_issue_delay_vect;
  vla_hwint_t compressed_min_issue_delay_vect;
  ainsn_t ainsn;
  size_t i;
  size_t min_issue_delay_len, compressed_min_issue_delay_len;
  size_t cfactor;
  int changed;

  /* Create vect of pointers to states ordered by num of transitions
     from the state (state with the maximum num is the first).  */
  output_states_vect.create (0);
  pass_states (automaton, add_states_vect_el);

  min_issue_delay_len = (output_states_vect.length ()
			 * automaton->insn_equiv_classes_num);
  min_issue_delay_vect.create (min_issue_delay_len);
  for (i = 0; i < min_issue_delay_len; i++)
    min_issue_delay_vect.quick_push (-1);

  automaton->max_min_delay = 0;

  do
    {
      size_t state_no;

      changed = 0;

      for (state_no = 0; state_no < output_states_vect.length ();
           state_no++)
	{
	  state_t s = output_states_vect[state_no];
	  arc_t arc;

	  for (arc = first_out_arc (s); arc; arc = next_out_arc (arc))
	    {
	      int k;

	      size_t asn = s->order_state_num
	                   * automaton->insn_equiv_classes_num
	                   + arc->insn->insn_equiv_class_num;

	      if (min_issue_delay_vect[asn])
		{
		  min_issue_delay_vect[asn] = (vect_el_t) 0;
		  changed = 1;
		}

	      for (k = 0; k < automaton->insn_equiv_classes_num; k++)
		{
		  size_t n0, n1;
		  vect_el_t delay0, delay1;

		  n0 = s->order_state_num
		       * automaton->insn_equiv_classes_num
		       + k;
		  n1 = arc->to_state->order_state_num
		       * automaton->insn_equiv_classes_num
		       + k;
		  delay0 = min_issue_delay_vect[n0];
		  delay1 = min_issue_delay_vect[n1];
		  if (delay1 != -1)
		    {
		      if (arc->insn->insn_reserv_decl
		          == DECL_INSN_RESERV (advance_cycle_insn_decl))
			delay1++;
		      if (delay1 < delay0 || delay0 == -1)
			{
			  min_issue_delay_vect[n0] = delay1;
			  changed = 1;
			}
		    }
		}
	    }
	}
    }
  while (changed);

  automaton->max_min_delay = 0;

  for (ainsn = automaton->ainsn_list; ainsn; ainsn = ainsn->next_ainsn)
    if (ainsn->first_ainsn_with_given_equivalence_num)
      {
	for (i = 0; i < output_states_vect.length (); i++)
	  {
	    state_t s = output_states_vect[i];
	    size_t np = s->order_state_num
	                * automaton->insn_equiv_classes_num
	                + ainsn->insn_equiv_class_num;
	    vect_el_t x = min_issue_delay_vect[np];

	    if (automaton->max_min_delay < x)
	      automaton->max_min_delay = x;
	    if (x == -1)
	      min_issue_delay_vect[np] = (vect_el_t) 0;
	  }
      }

  fprintf (output_file, "/* Vector of min issue delay of insns.  */\n");
  fprintf (output_file, "static const ");
  output_range_type (output_file, 0, automaton->max_min_delay);
  fprintf (output_file, " ");
  output_min_issue_delay_vect_name (output_file, automaton);
  fprintf (output_file, "[] ATTRIBUTE_UNUSED = {\n");
  /* Compress the vector.  */
  if (automaton->max_min_delay < 2)
    cfactor = 8;
  else if (automaton->max_min_delay < 4)
    cfactor = 4;
  else if (automaton->max_min_delay < 16)
    cfactor = 2;
  else
    cfactor = 1;
  automaton->min_issue_delay_table_compression_factor = cfactor;

  compressed_min_issue_delay_len = (min_issue_delay_len+cfactor-1) / cfactor;
  compressed_min_issue_delay_vect.create (compressed_min_issue_delay_len);

  for (i = 0; i < compressed_min_issue_delay_len; i++)
    compressed_min_issue_delay_vect.quick_push (0);

  for (i = 0; i < min_issue_delay_len; i++)
    {
      size_t ci = i / cfactor;
      vect_el_t x = min_issue_delay_vect[i];
      vect_el_t cx = compressed_min_issue_delay_vect[ci];

      cx |= x << (8 - (i % cfactor + 1) * (8 / cfactor));
      compressed_min_issue_delay_vect[ci] = cx;
    }
  output_vect (compressed_min_issue_delay_vect);
  fprintf (output_file, "};\n\n");
  output_states_vect.release ();
  min_issue_delay_vect.release ();
  compressed_min_issue_delay_vect.release ();
}

/* Form and output vector representing the locked states of
   AUTOMATON.  */
static void
output_dead_lock_vect (automaton_t automaton)
{
  size_t i;
  arc_t arc;
  vla_hwint_t dead_lock_vect = vla_hwint_t ();

  /* Create vect of pointers to states ordered by num of
     transitions from the state (state with the maximum num is the
     first).  */
  automaton->locked_states = 0;
  output_states_vect.create (0);
  pass_states (automaton, add_states_vect_el);

  dead_lock_vect.safe_grow (output_states_vect.length (), true);
  for (i = 0; i < output_states_vect.length (); i++)
    {
      state_t s = output_states_vect[i];
      arc = first_out_arc (s);
      gcc_assert (arc);
      if (next_out_arc (arc) == NULL
	  && (arc->insn->insn_reserv_decl
	      == DECL_INSN_RESERV (advance_cycle_insn_decl)))
	{
	  dead_lock_vect[s->order_state_num] = 1;
	  automaton->locked_states++;
	}
      else
	dead_lock_vect[s->order_state_num] = (vect_el_t) 0;
    }
  if (automaton->locked_states == 0)
    return;

  fprintf (output_file, "/* Vector for locked state flags.  */\n");
  fprintf (output_file, "static const ");
  output_range_type (output_file, 0, 1);
  fprintf (output_file, " ");
  output_dead_lock_vect_name (output_file, automaton);
  fprintf (output_file, "[] = {\n");
  output_vect (dead_lock_vect);
  fprintf (output_file, "};\n\n");
  output_states_vect.release ();
  dead_lock_vect.release ();
}

/* Form and output vector representing reserved units of the states of
   AUTOMATON.  */
static void
output_reserved_units_table (automaton_t automaton)
{
  vla_hwint_t reserved_units_table = vla_hwint_t ();
  int state_byte_size;
  int reserved_units_size;
  size_t n;
  int i;

  if (description->query_units_num == 0)
    return;

  /* Create vect of pointers to states.  */
  output_states_vect.create (0);
  pass_states (automaton, add_states_vect_el);
  /* Create vector.  */
  state_byte_size = (description->query_units_num + 7) / 8;
  reserved_units_size = (output_states_vect.length ()
			 * state_byte_size);

  reserved_units_table.create (reserved_units_size);

  for (i = 0; i < reserved_units_size; i++)
    reserved_units_table.quick_push (0);
  for (n = 0; n < output_states_vect.length (); n++)
    {
      state_t s = output_states_vect[n];
      for (i = 0; i < description->units_num; i++)
	if (units_array [i]->query_p
	    && first_cycle_unit_presence (s, i))
	  {
	    int ri = (s->order_state_num * state_byte_size
		      + units_array [i]->query_num / 8);
	    vect_el_t x = reserved_units_table[ri];

	    x += 1 << (units_array [i]->query_num % 8);
	    reserved_units_table[ri] = x;
	  }
    }
  fprintf (output_file, "\n#if %s\n", CPU_UNITS_QUERY_MACRO_NAME);
  fprintf (output_file, "/* Vector for reserved units of states.  */\n");
  fprintf (output_file, "static const ");
  output_range_type (output_file, 0, 255);
  fprintf (output_file, " ");
  output_reserved_units_table_name (output_file, automaton);
  fprintf (output_file, "[] = {\n");
  output_vect (reserved_units_table);
  fprintf (output_file, "};\n#endif /* #if %s */\n\n",
	   CPU_UNITS_QUERY_MACRO_NAME);

  output_states_vect.release ();
  reserved_units_table.release ();
}

/* The function outputs all tables representing DFA(s) used for fast
   pipeline hazards recognition.  */
static void
output_tables (void)
{
  automaton_t automaton;

  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      output_translate_vect (automaton);
      output_trans_table (automaton);
      output_min_issue_delay_table (automaton);
      output_dead_lock_vect (automaton);
      output_reserved_units_table (automaton);
    }
  fprintf (output_file, "\n#define %s %d\n\n", ADVANCE_CYCLE_VALUE_NAME,
           DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num);
  if (collapse_flag)
    fprintf (output_file, "\n#define %s %d\n\n", COLLAPSE_NDFA_VALUE_NAME,
	     DECL_INSN_RESERV (collapse_ndfa_insn_decl)->insn_num);
}

/* The function outputs definition and value of PHR interface variable
   `max_insn_queue_index'.  Its value is not less than maximal queue
   length needed for the insn scheduler.  */
static void
output_max_insn_queue_index_def (void)
{
  int i, max, latency;
  decl_t decl;

  max = description->max_insn_reserv_cycles;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv && decl != advance_cycle_insn_decl)
	{
	  latency = DECL_INSN_RESERV (decl)->default_latency;
	  if (latency > max)
	    max = latency;
	}
      else if (decl->mode == dm_bypass)
	{
	  latency = DECL_BYPASS (decl)->latency;
	  if (latency > max)
	    max = latency;
	}
    }
  for (i = 0; (1 << i) <= max; i++)
    ;
  gcc_assert (i >= 0);
  fprintf (output_file, "\nconst int max_insn_queue_index = %d;\n\n",
	   (1 << i) - 1);
}

/* The function outputs switch cases for insn reservations using
   function *output_automata_list_code.  */
static void
output_insn_code_cases (void (*output_automata_list_code)
			(automata_list_el_t))
{
  decl_t decl, decl2;
  int i, j;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	DECL_INSN_RESERV (decl)->processed_p = FALSE;
    }
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv
	  && !DECL_INSN_RESERV (decl)->processed_p)
	{
	  for (j = i; j < description->decls_num; j++)
	    {
	      decl2 = description->decls [j];
	      if (decl2->mode == dm_insn_reserv
		  && (DECL_INSN_RESERV (decl2)->important_automata_list
		      == DECL_INSN_RESERV (decl)->important_automata_list))
		{
		  DECL_INSN_RESERV (decl2)->processed_p = TRUE;
		  fprintf (output_file, "    case %d: /* %s */\n",
			   DECL_INSN_RESERV (decl2)->insn_num,
			   DECL_INSN_RESERV (decl2)->name);
		}
	    }
	  (*output_automata_list_code)
	    (DECL_INSN_RESERV (decl)->important_automata_list);
	}
    }
}


/* The function outputs a code for evaluation of a minimal delay of
   issue of insns which have reservations in given AUTOMATA_LIST.  */
static void
output_automata_list_min_issue_delay_code (automata_list_el_t automata_list)
{
  automata_list_el_t el;
  automaton_t automaton;

  for (el = automata_list; el != NULL; el = el->next_automata_list_el)
    {
      automaton = el->automaton;
      fprintf (output_file, "\n      %s = ", TEMPORARY_VARIABLE_NAME);
      output_min_issue_delay_vect_name (output_file, automaton);
      fprintf (output_file,
	       (automaton->min_issue_delay_table_compression_factor != 1
		? " [(" : " ["));
      output_translate_vect_name (output_file, automaton);
      fprintf (output_file, " [%s] + ", INTERNAL_INSN_CODE_NAME);
      fprintf (output_file, "%s->", CHIP_PARAMETER_NAME);
      output_chip_member_name (output_file, automaton);
      fprintf (output_file, " * %d", automaton->insn_equiv_classes_num);
      if (automaton->min_issue_delay_table_compression_factor == 1)
	fprintf (output_file, "];\n");
      else
	{
	  fprintf (output_file, ") / %d];\n",
		   automaton->min_issue_delay_table_compression_factor);
	  fprintf (output_file, "      %s = (%s >> (8 - ((",
		   TEMPORARY_VARIABLE_NAME, TEMPORARY_VARIABLE_NAME);
	  output_translate_vect_name (output_file, automaton);
	  fprintf (output_file, " [%s] + ", INTERNAL_INSN_CODE_NAME);
	  fprintf (output_file, "%s->", CHIP_PARAMETER_NAME);
	  output_chip_member_name (output_file, automaton);
	  fprintf (output_file, " * %d)", automaton->insn_equiv_classes_num);
	  fprintf
	    (output_file, " %% %d + 1) * %d)) & %d;\n",
	     automaton->min_issue_delay_table_compression_factor,
	     8 / automaton->min_issue_delay_table_compression_factor,
	     (1 << (8 / automaton->min_issue_delay_table_compression_factor))
	     - 1);
	}
      if (el == automata_list)
	fprintf (output_file, "      %s = %s;\n",
		 RESULT_VARIABLE_NAME, TEMPORARY_VARIABLE_NAME);
      else
	{
	  fprintf (output_file, "      if (%s > %s)\n",
		   TEMPORARY_VARIABLE_NAME, RESULT_VARIABLE_NAME);
	  fprintf (output_file, "        %s = %s;\n",
		   RESULT_VARIABLE_NAME, TEMPORARY_VARIABLE_NAME);
	}
    }
  fprintf (output_file, "      break;\n\n");
}

/* Output function `internal_min_issue_delay'.  */
static void
output_internal_min_issue_delay_func (void)
{
  fprintf (output_file,
	   "static int\n%s (int %s, struct %s *%s ATTRIBUTE_UNUSED)\n",
	   INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME, INTERNAL_INSN_CODE_NAME,
	   CHIP_NAME, CHIP_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s ATTRIBUTE_UNUSED;\n  int %s = -1;\n",
	   TEMPORARY_VARIABLE_NAME, RESULT_VARIABLE_NAME);
  fprintf (output_file, "\n  switch (%s)\n    {\n", INTERNAL_INSN_CODE_NAME);
  output_insn_code_cases (output_automata_list_min_issue_delay_code);
  fprintf (output_file,
	   "\n    default:\n      %s = -1;\n      break;\n    }\n",
	   RESULT_VARIABLE_NAME);
  fprintf (output_file, "  return %s;\n", RESULT_VARIABLE_NAME);
  fprintf (output_file, "}\n\n");
}

/* The function outputs a code changing state after issue of insns
   which have reservations in given AUTOMATA_LIST.  */
static void
output_automata_list_transition_code (automata_list_el_t automata_list)
{
  automata_list_el_t el, next_el;

  fprintf (output_file, "      {\n");
  if (automata_list != NULL && automata_list->next_automata_list_el != NULL)
    for (el = automata_list;; el = next_el)
      {
        next_el = el->next_automata_list_el;
        if (next_el == NULL)
          break;
        fprintf (output_file, "        ");
        output_state_member_type (output_file, el->automaton);
	fprintf (output_file, " ");
        output_temp_chip_member_name (output_file, el->automaton);
        fprintf (output_file, ";\n");
      }
  for (el = automata_list; el != NULL; el = el->next_automata_list_el)
    if (comb_vect_p (el->automaton->trans_table))
      {
	fprintf (output_file, "\n        %s = ", TEMPORARY_VARIABLE_NAME);
	output_trans_base_vect_name (output_file, el->automaton);
	fprintf (output_file, " [%s->", CHIP_PARAMETER_NAME);
	output_chip_member_name (output_file, el->automaton);
	fprintf (output_file, "] + ");
	output_translate_vect_name (output_file, el->automaton);
	fprintf (output_file, " [%s];\n", INTERNAL_INSN_CODE_NAME);
	fprintf (output_file, "        if (");
	output_trans_check_vect_name (output_file, el->automaton);
	fprintf (output_file, " [%s] != %s->",
		 TEMPORARY_VARIABLE_NAME, CHIP_PARAMETER_NAME);
	output_chip_member_name (output_file, el->automaton);
	fprintf (output_file, ")\n");
	fprintf (output_file, "          return %s (%s, %s);\n",
		 INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME, INTERNAL_INSN_CODE_NAME,
		 CHIP_PARAMETER_NAME);
	fprintf (output_file, "        else\n");
	fprintf (output_file, "          ");
	if (el->next_automata_list_el != NULL)
	  output_temp_chip_member_name (output_file, el->automaton);
	else
	  {
	    fprintf (output_file, "%s->", CHIP_PARAMETER_NAME);
	    output_chip_member_name (output_file, el->automaton);
	  }
	fprintf (output_file, " = ");
	output_trans_comb_vect_name (output_file, el->automaton);
	fprintf (output_file, " [%s];\n", TEMPORARY_VARIABLE_NAME);
      }
    else
      {
	fprintf (output_file, "\n        %s = ", TEMPORARY_VARIABLE_NAME);
	output_trans_full_vect_name (output_file, el->automaton);
	fprintf (output_file, " [");
	output_translate_vect_name (output_file, el->automaton);
	fprintf (output_file, " [%s] + ", INTERNAL_INSN_CODE_NAME);
	fprintf (output_file, "%s->", CHIP_PARAMETER_NAME);
	output_chip_member_name (output_file, el->automaton);
	fprintf (output_file, " * %d];\n",
		 el->automaton->insn_equiv_classes_num);
	fprintf (output_file, "        if (%s >= %d)\n",
		 TEMPORARY_VARIABLE_NAME, el->automaton->achieved_states_num);
	fprintf (output_file, "          return %s (%s, %s);\n",
		 INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME, INTERNAL_INSN_CODE_NAME,
		 CHIP_PARAMETER_NAME);
	fprintf (output_file, "        else\n          ");
	if (el->next_automata_list_el != NULL)
	  output_temp_chip_member_name (output_file, el->automaton);
	else
	  {
	    fprintf (output_file, "%s->", CHIP_PARAMETER_NAME);
	    output_chip_member_name (output_file, el->automaton);
	  }
	fprintf (output_file, " = %s;\n", TEMPORARY_VARIABLE_NAME);
      }
  if (automata_list != NULL && automata_list->next_automata_list_el != NULL)
    for (el = automata_list;; el = next_el)
      {
        next_el = el->next_automata_list_el;
        if (next_el == NULL)
          break;
        fprintf (output_file, "        %s->", CHIP_PARAMETER_NAME);
        output_chip_member_name (output_file, el->automaton);
        fprintf (output_file, " = ");
        output_temp_chip_member_name (output_file, el->automaton);
        fprintf (output_file, ";\n");
      }
  fprintf (output_file, "        return -1;\n");
  fprintf (output_file, "      }\n");
}

/* Output function `internal_state_transition'.  */
static void
output_internal_trans_func (void)
{
  fprintf (output_file,
	   "static int\n%s (int %s, struct %s *%s ATTRIBUTE_UNUSED)\n",
	   INTERNAL_TRANSITION_FUNC_NAME, INTERNAL_INSN_CODE_NAME,
	   CHIP_NAME, CHIP_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s ATTRIBUTE_UNUSED;\n", TEMPORARY_VARIABLE_NAME);
  fprintf (output_file, "\n  switch (%s)\n    {\n", INTERNAL_INSN_CODE_NAME);
  output_insn_code_cases (output_automata_list_transition_code);
  fprintf (output_file, "\n    default:\n      return -1;\n    }\n");
  fprintf (output_file, "}\n\n");
}

/* Output code

  gcc_checking_assert (insn != 0);
  insn_code = dfa_insn_code (insn);
  if (insn_code >= DFA__ADVANCE_CYCLE)
    return code;

  where insn denotes INSN_NAME, insn_code denotes INSN_CODE_NAME, and
  code denotes CODE.  */
static void
output_internal_insn_code_evaluation (const char *insn_name,
				      const char *insn_code_name,
				      int code)
{
  fprintf (output_file, "  gcc_checking_assert (%s != 0);\n"
           "  %s = %s (%s);\n"
           "  if (%s >= %s)\n    return %d;\n",
           insn_name,
           insn_code_name, DFA_INSN_CODE_FUNC_NAME, insn_name,
           insn_code_name, ADVANCE_CYCLE_VALUE_NAME, code);
}


/* This function outputs `dfa_insn_code' and its helper function
   `dfa_insn_code_enlarge'.  */
static void
output_dfa_insn_code_func (void)
{
  /* Emacs c-mode gets really confused if there's a { or } in column 0
     inside a string, so don't do that.  */
  fprintf (output_file, "\
static void\n\
dfa_insn_code_enlarge (int uid)\n\
{\n\
  int i = %s;\n\
  %s = 2 * uid;\n\
  %s = XRESIZEVEC (int, %s,\n\
                 %s);\n\
  for (; i < %s; i++)\n\
    %s[i] = -1;\n}\n\n",
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME,
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME,
	   DFA_INSN_CODES_VARIABLE_NAME, DFA_INSN_CODES_VARIABLE_NAME,
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME,
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME,
	   DFA_INSN_CODES_VARIABLE_NAME);
  fprintf (output_file, "\
static inline int\n%s (rtx_insn *%s)\n\
{\n\
  int uid = INSN_UID (%s);\n\
  int %s;\n\n",
	   DFA_INSN_CODE_FUNC_NAME, INSN_PARAMETER_NAME,
	   INSN_PARAMETER_NAME, INTERNAL_INSN_CODE_NAME);

  fprintf (output_file,
	   "  if (uid >= %s)\n    dfa_insn_code_enlarge (uid);\n\n",
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME);
  fprintf (output_file, "  %s = %s[uid];\n",
	   INTERNAL_INSN_CODE_NAME, DFA_INSN_CODES_VARIABLE_NAME);
  fprintf (output_file, "\
  if (%s < 0)\n\
    {\n\
      %s = %s (%s);\n\
      %s[uid] = %s;\n\
    }\n",
	   INTERNAL_INSN_CODE_NAME,
	   INTERNAL_INSN_CODE_NAME,
	   INTERNAL_DFA_INSN_CODE_FUNC_NAME, INSN_PARAMETER_NAME,
	   DFA_INSN_CODES_VARIABLE_NAME, INTERNAL_INSN_CODE_NAME);
  fprintf (output_file, "  return %s;\n}\n\n", INTERNAL_INSN_CODE_NAME);
}

/* The function outputs PHR interface function `state_transition'.  */
static void
output_trans_func (void)
{
  fprintf (output_file, "int\n%s (%s %s, rtx %s)\n",
	   TRANSITION_FUNC_NAME, STATE_TYPE_NAME, STATE_NAME,
	   INSN_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s;\n", INTERNAL_INSN_CODE_NAME);
  fprintf (output_file, "\n  if (%s == 0)\n", INSN_PARAMETER_NAME);
  fprintf (output_file, "    %s = %s;\n",
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  if (collapse_flag)
    {
      fprintf (output_file, "  else if (%s == const0_rtx)\n",
	       INSN_PARAMETER_NAME);
      fprintf (output_file, "    %s = %s;\n",
	       INTERNAL_INSN_CODE_NAME, COLLAPSE_NDFA_VALUE_NAME);
    }
  fprintf (output_file, "  else\n    {\n");
  fprintf (output_file, "      %s = %s (as_a <rtx_insn *> (%s));\n",
	   INTERNAL_INSN_CODE_NAME, DFA_INSN_CODE_FUNC_NAME,
	   INSN_PARAMETER_NAME);
  fprintf (output_file, "      if (%s > %s)\n        return -1;\n    }\n",
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  fprintf (output_file, "  return %s (%s, (struct %s *) %s);\n}\n\n",
	   INTERNAL_TRANSITION_FUNC_NAME, INTERNAL_INSN_CODE_NAME, CHIP_NAME, STATE_NAME);
}

/* Output function `min_issue_delay'.  */
static void
output_min_issue_delay_func (void)
{
  fprintf (output_file, "int\n%s (%s %s, rtx_insn *%s)\n",
	   MIN_ISSUE_DELAY_FUNC_NAME, STATE_TYPE_NAME, STATE_NAME,
	   INSN_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s;\n", INTERNAL_INSN_CODE_NAME);
  fprintf (output_file, "\n  if (%s != 0)\n    {\n", INSN_PARAMETER_NAME);
  fprintf (output_file, "      %s = %s (%s);\n", INTERNAL_INSN_CODE_NAME,
	   DFA_INSN_CODE_FUNC_NAME, INSN_PARAMETER_NAME);
  fprintf (output_file, "      if (%s > %s)\n        return 0;\n",
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  fprintf (output_file, "    }\n  else\n    %s = %s;\n",
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  fprintf (output_file, "\n  return %s (%s, (struct %s *) %s);\n",
	   INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME, INTERNAL_INSN_CODE_NAME,
	   CHIP_NAME, STATE_NAME);
  fprintf (output_file, "}\n\n");
}

/* Output function `internal_dead_lock'.  */
static void
output_internal_dead_lock_func (void)
{
  automaton_t automaton;

  fprintf (output_file, "static int\n%s (struct %s *ARG_UNUSED (%s))\n",
	   INTERNAL_DEAD_LOCK_FUNC_NAME, CHIP_NAME, CHIP_PARAMETER_NAME);
  fprintf (output_file, "{\n");
  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    if (automaton->locked_states)
      {
	fprintf (output_file, "  if (");
	output_dead_lock_vect_name (output_file, automaton);
	fprintf (output_file, " [%s->", CHIP_PARAMETER_NAME);
	output_chip_member_name (output_file, automaton);
	fprintf (output_file, "])\n    return 1/* TRUE */;\n");
      }
  fprintf (output_file, "  return 0/* FALSE */;\n}\n\n");
}

/* The function outputs PHR interface function `state_dead_lock_p'.  */
static void
output_dead_lock_func (void)
{
  fprintf (output_file, "int\n%s (%s %s)\n",
	   DEAD_LOCK_FUNC_NAME, STATE_TYPE_NAME, STATE_NAME);
  fprintf (output_file, "{\n  return %s ((struct %s *) %s);\n}\n\n",
	   INTERNAL_DEAD_LOCK_FUNC_NAME, CHIP_NAME, STATE_NAME);
}

/* Output function `internal_reset'.  */
static void
output_internal_reset_func (void)
{
  fprintf (output_file, "static inline void\n%s (struct %s *%s)\n",
	   INTERNAL_RESET_FUNC_NAME, CHIP_NAME, CHIP_PARAMETER_NAME);
  fprintf (output_file, "{\n  memset (%s, 0, sizeof (struct %s));\n}\n\n",
	   CHIP_PARAMETER_NAME, CHIP_NAME);
}

/* The function outputs PHR interface function `state_size'.  */
static void
output_size_func (void)
{
  fprintf (output_file, "int\n%s (void)\n", SIZE_FUNC_NAME);
  fprintf (output_file, "{\n  return sizeof (struct %s);\n}\n\n", CHIP_NAME);
}

/* The function outputs PHR interface function `state_reset'.  */
static void
output_reset_func (void)
{
  fprintf (output_file, "void\n%s (%s %s)\n",
	   RESET_FUNC_NAME, STATE_TYPE_NAME, STATE_NAME);
  fprintf (output_file, "{\n  %s ((struct %s *) %s);\n}\n\n", INTERNAL_RESET_FUNC_NAME,
	   CHIP_NAME, STATE_NAME);
}

/* Output function `min_insn_conflict_delay'.  */
static void
output_min_insn_conflict_delay_func (void)
{
  fprintf (output_file,
	   "int\n%s (%s %s, rtx_insn *%s, rtx_insn *%s)\n",
	   MIN_INSN_CONFLICT_DELAY_FUNC_NAME, STATE_TYPE_NAME,
	   STATE_NAME, INSN_PARAMETER_NAME, INSN2_PARAMETER_NAME);
  fprintf (output_file, "{\n  struct %s %s;\n  int %s, %s, transition;\n",
	   CHIP_NAME, CHIP_NAME, INTERNAL_INSN_CODE_NAME,
	   INTERNAL_INSN2_CODE_NAME);
  output_internal_insn_code_evaluation (INSN_PARAMETER_NAME,
					INTERNAL_INSN_CODE_NAME, 0);
  output_internal_insn_code_evaluation (INSN2_PARAMETER_NAME,
					INTERNAL_INSN2_CODE_NAME, 0);
  fprintf (output_file, "  memcpy (&%s, %s, sizeof (%s));\n",
	   CHIP_NAME, STATE_NAME, CHIP_NAME);
  fprintf (output_file, "  %s (&%s);\n", INTERNAL_RESET_FUNC_NAME, CHIP_NAME);
  fprintf (output_file, "  transition = %s (%s, &%s);\n",
	   INTERNAL_TRANSITION_FUNC_NAME, INTERNAL_INSN_CODE_NAME, CHIP_NAME);
  fprintf (output_file, "  gcc_assert (transition <= 0);\n");
  fprintf (output_file, "  return %s (%s, &%s);\n",
	   INTERNAL_MIN_ISSUE_DELAY_FUNC_NAME, INTERNAL_INSN2_CODE_NAME,
	   CHIP_NAME);
  fprintf (output_file, "}\n\n");
}

/* Output the array holding default latency values.  These are used in
   insn_latency and maximal_insn_latency function implementations.  */
static void
output_default_latencies (void)
{
  int i, j, col;
  decl_t decl;
  const char *tabletype = "unsigned char";

  /* Find the smallest integer type that can hold all the default
     latency values.  */
  for (i = 0; i < description->decls_num; i++)
    if (description->decls[i]->mode == dm_insn_reserv)
      {
	decl = description->decls[i];
	if (DECL_INSN_RESERV (decl)->default_latency > UCHAR_MAX
	    && tabletype[0] != 'i')  /* Don't shrink it.  */
	  tabletype = "unsigned short";
	if (DECL_INSN_RESERV (decl)->default_latency > USHRT_MAX)
	  tabletype = "int";
      }

  fprintf (output_file, "  static const %s default_latencies[] =\n    {",
	   tabletype);

  for (i = 0, j = 0, col = 7; i < description->normal_decls_num; i++)
    if (description->decls[i]->mode == dm_insn_reserv)
      {
	if ((col = (col+1) % 8) == 0)
	  fputs ("\n     ", output_file);
	decl = description->decls[i];
	gcc_assert (j++ == DECL_INSN_RESERV (decl)->insn_num);
	fprintf (output_file, "% 4d,",
		 DECL_INSN_RESERV (decl)->default_latency);
      }
  gcc_assert (j == description->insns_num - (collapse_flag ? 2 : 1));
  fputs ("\n    };\n", output_file);
}

/* Output function `internal_insn_latency'.  */
static void
output_internal_insn_latency_func (void)
{
  int i;
  decl_t decl;
  struct bypass_decl *bypass;

  fprintf (output_file, "static int\n"
	   "%s (int %s ATTRIBUTE_UNUSED, int %s ATTRIBUTE_UNUSED,\n"
	   "\trtx_insn *%s ATTRIBUTE_UNUSED, rtx_insn *%s ATTRIBUTE_UNUSED)\n",
	   INTERNAL_INSN_LATENCY_FUNC_NAME,
	   INTERNAL_INSN_CODE_NAME, INTERNAL_INSN2_CODE_NAME,
	   INSN_PARAMETER_NAME, INSN2_PARAMETER_NAME);
  fprintf (output_file, "{\n");

  if (DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num == 0)
    {
      fputs ("  return 0;\n}\n\n", output_file);
      return;
    }

  fprintf (output_file, "  switch (%s)\n    {\n", INTERNAL_INSN_CODE_NAME);
  for (i = 0; i < description->decls_num; i++)
    if (description->decls[i]->mode == dm_insn_reserv
	&& DECL_INSN_RESERV (description->decls[i])->bypass_list)
      {
	decl = description->decls [i];
	fprintf (output_file,
		 "    case %d:\n      switch (%s)\n        {\n",
		 DECL_INSN_RESERV (decl)->insn_num,
		 INTERNAL_INSN2_CODE_NAME);
	for (bypass = DECL_INSN_RESERV (decl)->bypass_list;
	     bypass != NULL;
	     bypass = bypass->next)
	  {
	    gcc_assert (bypass->in_insn_reserv->insn_num
			!= (DECL_INSN_RESERV
			    (advance_cycle_insn_decl)->insn_num));
	    fprintf (output_file, "        case %d:\n",
		     bypass->in_insn_reserv->insn_num);
	    for (;;)
	      {
		if (bypass->bypass_guard_name == NULL)
		  {
		    gcc_assert (bypass->next == NULL
				|| (bypass->in_insn_reserv
				    != bypass->next->in_insn_reserv));
		    fprintf (output_file, "          return %d;\n",
			     bypass->latency);
		  }
		else
		  {
		    fprintf (output_file,
			     "          if (%s (%s, %s))\n",
			     bypass->bypass_guard_name, INSN_PARAMETER_NAME,
			     INSN2_PARAMETER_NAME);
		    fprintf (output_file, "            return %d;\n",
			     bypass->latency);
		  }
		if (bypass->next == NULL
		    || bypass->in_insn_reserv != bypass->next->in_insn_reserv)
		  break;
		bypass = bypass->next;
	      }
	    if (bypass->bypass_guard_name != NULL)
	      fprintf (output_file, "          break;\n");
	  }
	fputs ("        }\n      break;\n", output_file);
      }

  fprintf (output_file, "    }\n  return default_latencies[%s];\n}\n\n",
	   INTERNAL_INSN_CODE_NAME);
}

/* Output function `internal_maximum_insn_latency'.  */
static void
output_internal_maximal_insn_latency_func (void)
{
  decl_t decl;
  struct bypass_decl *bypass;
  int i;
  int max;

  fprintf (output_file, "static int\n%s (int %s ATTRIBUTE_UNUSED)\n",
	   "internal_maximal_insn_latency", INTERNAL_INSN_CODE_NAME);
  fprintf (output_file, "{\n");

  if (DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num == 0)
    {
      fputs ("  return 0;\n}\n\n", output_file);
      return;
    }

  fprintf (output_file, "  switch (%s)\n    {\n", INTERNAL_INSN_CODE_NAME);
  for (i = 0; i < description->decls_num; i++)
    if (description->decls[i]->mode == dm_insn_reserv
	&& DECL_INSN_RESERV (description->decls[i])->bypass_list)
      {
	decl = description->decls [i];
        max = DECL_INSN_RESERV (decl)->default_latency;
	fprintf (output_file,
		 "    case %d: {",
		 DECL_INSN_RESERV (decl)->insn_num);
	for (bypass = DECL_INSN_RESERV (decl)->bypass_list;
	     bypass != NULL;
	     bypass = bypass->next)
	  {
	    if (bypass->latency > max)
              max = bypass->latency;
	  }
	fprintf (output_file, " return %d; }\n      break;\n", max);
      }

  fprintf (output_file, "    }\n  return default_latencies[%s];\n}\n\n",
	   INTERNAL_INSN_CODE_NAME);
}

/* The function outputs PHR interface function `insn_latency'.  */
static void
output_insn_latency_func (void)
{
  fprintf (output_file, "int\n%s (rtx_insn *%s, rtx_insn *%s)\n",
	   INSN_LATENCY_FUNC_NAME, INSN_PARAMETER_NAME, INSN2_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s, %s;\n",
	   INTERNAL_INSN_CODE_NAME, INTERNAL_INSN2_CODE_NAME);
  output_internal_insn_code_evaluation (INSN_PARAMETER_NAME,
					INTERNAL_INSN_CODE_NAME, 0);
  output_internal_insn_code_evaluation (INSN2_PARAMETER_NAME,
					INTERNAL_INSN2_CODE_NAME, 0);
  fprintf (output_file, "  return %s (%s, %s, %s, %s);\n}\n\n",
	   INTERNAL_INSN_LATENCY_FUNC_NAME,
	   INTERNAL_INSN_CODE_NAME, INTERNAL_INSN2_CODE_NAME,
	   INSN_PARAMETER_NAME, INSN2_PARAMETER_NAME);
}

/* The function outputs PHR interface function `maximal_insn_latency'.  */
static void
output_maximal_insn_latency_func (void)
{
  fprintf (output_file, "int\n%s (rtx_insn *%s)\n",
	   "maximal_insn_latency", INSN_PARAMETER_NAME);
  fprintf (output_file, "{\n  int %s;\n",
	   INTERNAL_INSN_CODE_NAME);
  output_internal_insn_code_evaluation (INSN_PARAMETER_NAME,
					INTERNAL_INSN_CODE_NAME, 0);
  fprintf (output_file, "  return %s (%s);\n}\n\n",
	   "internal_maximal_insn_latency", INTERNAL_INSN_CODE_NAME);
}

/* The function outputs PHR interface function `print_reservation'.  */
static void
output_print_reservation_func (void)
{
  decl_t decl;
  int i, j;

  fprintf (output_file,
	   "void\n%s (FILE *%s, rtx_insn *%s ATTRIBUTE_UNUSED)\n{\n",
           PRINT_RESERVATION_FUNC_NAME, FILE_PARAMETER_NAME,
           INSN_PARAMETER_NAME);

  if (DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num == 0)
    {
      fprintf (output_file, "  fputs (\"%s\", %s);\n}\n\n",
	       NOTHING_NAME, FILE_PARAMETER_NAME);
      return;
    }


  fputs ("  static const char *const reservation_names[] =\n    {",
	 output_file);

  for (i = 0, j = 0; i < description->normal_decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	{
	  gcc_assert (j == DECL_INSN_RESERV (decl)->insn_num);
	  j++;

	  fprintf (output_file, "\n      \"%s\",",
		   regexp_representation (DECL_INSN_RESERV (decl)->regexp));
	  finish_regexp_representation ();
	}
    }
  gcc_assert (j == description->insns_num - (collapse_flag ? 2 : 1));

  fprintf (output_file, "\n      \"%s\"\n    };\n  int %s;\n\n",
	   NOTHING_NAME, INTERNAL_INSN_CODE_NAME);

  fprintf (output_file, "  if (%s == 0)\n    %s = %s;\n",
	   INSN_PARAMETER_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  fprintf (output_file, "  else\n\
    {\n\
      %s = %s (%s);\n\
      if (%s > %s)\n\
        %s = %s;\n\
    }\n",
	   INTERNAL_INSN_CODE_NAME, DFA_INSN_CODE_FUNC_NAME,
	       INSN_PARAMETER_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);

  fprintf (output_file, "  fputs (reservation_names[%s], %s);\n}\n\n",
	   INTERNAL_INSN_CODE_NAME, FILE_PARAMETER_NAME);
}

/* The following function is used to sort unit declaration by their
   names.  */
static int
units_cmp (const void *unit1, const void *unit2)
{
  const_unit_decl_t const u1 = *(const_unit_decl_t const*) unit1;
  const_unit_decl_t const u2 = *(const_unit_decl_t const*) unit2;

  return strcmp (u1->name, u2->name);
}

/* The following macro value is name of struct containing unit name
   and unit code.  */
#define NAME_CODE_STRUCT_NAME  "name_code"

/* The following macro value is name of table of struct name_code.  */
#define NAME_CODE_TABLE_NAME   "name_code_table"

/* The following macro values are member names for struct name_code.  */
#define NAME_MEMBER_NAME       "name"
#define CODE_MEMBER_NAME       "code"

/* The following macro values are local variable names for function
   `get_cpu_unit_code'.  */
#define CMP_VARIABLE_NAME      "cmp"
#define LOW_VARIABLE_NAME      "l"
#define MIDDLE_VARIABLE_NAME   "m"
#define HIGH_VARIABLE_NAME     "h"

/* The following function outputs function to obtain internal cpu unit
   code by the cpu unit name.  */
static void
output_get_cpu_unit_code_func (void)
{
  int i;
  unit_decl_t *units;

  fprintf (output_file, "int\n%s (const char *%s)\n",
	   GET_CPU_UNIT_CODE_FUNC_NAME, CPU_UNIT_NAME_PARAMETER_NAME);
  fprintf (output_file, "{\n  struct %s {const char *%s; int %s;};\n",
	   NAME_CODE_STRUCT_NAME, NAME_MEMBER_NAME, CODE_MEMBER_NAME);
  fprintf (output_file, "  int %s, %s, %s, %s;\n", CMP_VARIABLE_NAME,
	   LOW_VARIABLE_NAME, MIDDLE_VARIABLE_NAME, HIGH_VARIABLE_NAME);
  fprintf (output_file, "  static struct %s %s [] =\n    {\n",
	   NAME_CODE_STRUCT_NAME, NAME_CODE_TABLE_NAME);
  units = XNEWVEC (unit_decl_t, description->units_num);
  memcpy (units, units_array, sizeof (unit_decl_t) * description->units_num);
  qsort (units, description->units_num, sizeof (unit_decl_t), units_cmp);
  for (i = 0; i < description->units_num; i++)
    if (units [i]->query_p)
      fprintf (output_file, "      {\"%s\", %d},\n",
	       units[i]->name, units[i]->query_num);
  fprintf (output_file, "    };\n\n");
  fprintf (output_file, "  /* The following is binary search: */\n");
  fprintf (output_file, "  %s = 0;\n", LOW_VARIABLE_NAME);
  fprintf (output_file, "  %s = sizeof (%s) / sizeof (struct %s) - 1;\n",
	   HIGH_VARIABLE_NAME, NAME_CODE_TABLE_NAME, NAME_CODE_STRUCT_NAME);
  fprintf (output_file, "  while (%s <= %s)\n    {\n",
	   LOW_VARIABLE_NAME, HIGH_VARIABLE_NAME);
  fprintf (output_file, "      %s = (%s + %s) / 2;\n",
	   MIDDLE_VARIABLE_NAME, LOW_VARIABLE_NAME, HIGH_VARIABLE_NAME);
  fprintf (output_file, "      %s = strcmp (%s, %s [%s].%s);\n",
	   CMP_VARIABLE_NAME, CPU_UNIT_NAME_PARAMETER_NAME,
	   NAME_CODE_TABLE_NAME, MIDDLE_VARIABLE_NAME, NAME_MEMBER_NAME);
  fprintf (output_file, "      if (%s < 0)\n", CMP_VARIABLE_NAME);
  fprintf (output_file, "        %s = %s - 1;\n",
	   HIGH_VARIABLE_NAME, MIDDLE_VARIABLE_NAME);
  fprintf (output_file, "      else if (%s > 0)\n", CMP_VARIABLE_NAME);
  fprintf (output_file, "        %s = %s + 1;\n",
	   LOW_VARIABLE_NAME, MIDDLE_VARIABLE_NAME);
  fprintf (output_file, "      else\n");
  fprintf (output_file, "        return %s [%s].%s;\n    }\n",
	   NAME_CODE_TABLE_NAME, MIDDLE_VARIABLE_NAME, CODE_MEMBER_NAME);
  fprintf (output_file, "  return -1;\n}\n\n");
  free (units);
}

/* The following function outputs function to check reservation of cpu
   unit (its internal code will be passed as the function argument) in
   given cpu state.  */
static void
output_cpu_unit_reservation_p (void)
{
  automaton_t automaton;

  fprintf (output_file, "int\n%s (%s %s, int %s)\n",
	   CPU_UNIT_RESERVATION_P_FUNC_NAME,
	   STATE_TYPE_NAME, STATE_NAME,
	   CPU_CODE_PARAMETER_NAME);
  fprintf (output_file, "{\n  gcc_assert (%s >= 0 && %s < %d);\n",
	   CPU_CODE_PARAMETER_NAME, CPU_CODE_PARAMETER_NAME,
	   description->query_units_num);
  if (description->query_units_num > 0)
    for (automaton = description->first_automaton;
	 automaton != NULL;
	 automaton = automaton->next_automaton)
      {
	fprintf (output_file, "  if ((");
	output_reserved_units_table_name (output_file, automaton);
	fprintf (output_file, " [((struct %s *) %s)->", CHIP_NAME, STATE_NAME);
	output_chip_member_name (output_file, automaton);
	fprintf (output_file, " * %d + %s / 8] >> (%s %% 8)) & 1)\n",
		 (description->query_units_num + 7) / 8,
		 CPU_CODE_PARAMETER_NAME, CPU_CODE_PARAMETER_NAME);
	fprintf (output_file, "    return 1;\n");
      }
  fprintf (output_file, "  return 0;\n}\n\n");
}

/* The following function outputs a function to check if insn
   has a dfa reservation.  */
static void
output_insn_has_dfa_reservation_p (void)
{
  fprintf (output_file,
	   "bool\n%s (rtx_insn *%s ATTRIBUTE_UNUSED)\n{\n",
           INSN_HAS_DFA_RESERVATION_P_FUNC_NAME,
           INSN_PARAMETER_NAME);

  if (DECL_INSN_RESERV (advance_cycle_insn_decl)->insn_num == 0)
    {
      fprintf (output_file, "  return false;\n}\n\n");
      return;
    }

  fprintf (output_file, "  int %s;\n\n", INTERNAL_INSN_CODE_NAME);

  fprintf (output_file, "  if (%s == 0)\n    %s = %s;\n",
	   INSN_PARAMETER_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
  fprintf (output_file, "  else\n\
    {\n\
      %s = %s (%s);\n\
      if (%s > %s)\n\
        %s = %s;\n\
    }\n\n",
	   INTERNAL_INSN_CODE_NAME, DFA_INSN_CODE_FUNC_NAME,
	       INSN_PARAMETER_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME,
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);

  fprintf (output_file, "  return %s != %s;\n}\n\n",
	   INTERNAL_INSN_CODE_NAME, ADVANCE_CYCLE_VALUE_NAME);
}

/* The function outputs PHR interface functions `dfa_clean_insn_cache'
   and 'dfa_clear_single_insn_cache'.  */
static void
output_dfa_clean_insn_cache_func (void)
{
  fprintf (output_file,
	   "void\n%s (void)\n{\n  int %s;\n\n",
	   DFA_CLEAN_INSN_CACHE_FUNC_NAME, I_VARIABLE_NAME);
  fprintf (output_file,
	   "  for (%s = 0; %s < %s; %s++)\n    %s [%s] = -1;\n}\n\n",
	   I_VARIABLE_NAME, I_VARIABLE_NAME,
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME, I_VARIABLE_NAME,
	   DFA_INSN_CODES_VARIABLE_NAME, I_VARIABLE_NAME);

  fprintf (output_file,
           "void\n%s (rtx_insn *%s)\n{\n  int %s;\n\n",
           DFA_CLEAR_SINGLE_INSN_CACHE_FUNC_NAME, INSN_PARAMETER_NAME,
	   I_VARIABLE_NAME);
  fprintf (output_file,
           "  %s = INSN_UID (%s);\n  if (%s < %s)\n    %s [%s] = -1;\n}\n\n",
           I_VARIABLE_NAME, INSN_PARAMETER_NAME, I_VARIABLE_NAME,
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME, DFA_INSN_CODES_VARIABLE_NAME,
	   I_VARIABLE_NAME);
}

/* The function outputs PHR interface function `dfa_start'.  */
static void
output_dfa_start_func (void)
{
  fprintf (output_file,
	   "void\n%s (void)\n{\n  %s = get_max_uid ();\n",
	   DFA_START_FUNC_NAME, DFA_INSN_CODES_LENGTH_VARIABLE_NAME);
  fprintf (output_file, "  %s = XNEWVEC (int, %s);\n",
	   DFA_INSN_CODES_VARIABLE_NAME, DFA_INSN_CODES_LENGTH_VARIABLE_NAME);
  fprintf (output_file, "  %s ();\n}\n\n", DFA_CLEAN_INSN_CACHE_FUNC_NAME);
}

/* The function outputs PHR interface function `dfa_finish'.  */
static void
output_dfa_finish_func (void)
{
  fprintf (output_file, "void\n%s (void)\n{\n  free (%s);\n}\n\n",
	   DFA_FINISH_FUNC_NAME, DFA_INSN_CODES_VARIABLE_NAME);
}



/* The page contains code for output description file (readable
   representation of original description and generated DFA(s).  */

/* The function outputs string representation of IR reservation.  */
static void
output_regexp (regexp_t regexp)
{
  fprintf (output_description_file, "%s", regexp_representation (regexp));
  finish_regexp_representation ();
}

/* Output names of units in LIST separated by comma.  */
static void
output_unit_set_el_list (unit_set_el_t list)
{
  unit_set_el_t el;

  for (el = list; el != NULL; el = el->next_unit_set_el)
    {
      if (el != list)
	fprintf (output_description_file, ", ");
      fprintf (output_description_file, "%s", el->unit_decl->name);
    }
}

/* Output patterns in LIST separated by comma.  */
static void
output_pattern_set_el_list (pattern_set_el_t list)
{
  pattern_set_el_t el;
  int i;

  for (el = list; el != NULL; el = el->next_pattern_set_el)
    {
      if (el != list)
	fprintf (output_description_file, ", ");
      for (i = 0; i < el->units_num; i++)
	fprintf (output_description_file, (i == 0 ? "%s" : " %s"),
		 el->unit_decls [i]->name);
    }
}

/* The function outputs string representation of IR define_reservation
   and define_insn_reservation.  */
static void
output_description (void)
{
  decl_t decl;
  int i;

  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit)
	{
	  if (DECL_UNIT (decl)->excl_list != NULL)
	    {
	      fprintf (output_description_file, "unit %s exclusion_set: ",
		       DECL_UNIT (decl)->name);
	      output_unit_set_el_list (DECL_UNIT (decl)->excl_list);
	      fprintf (output_description_file, "\n");
	    }
	  if (DECL_UNIT (decl)->presence_list != NULL)
	    {
	      fprintf (output_description_file, "unit %s presence_set: ",
		       DECL_UNIT (decl)->name);
	      output_pattern_set_el_list (DECL_UNIT (decl)->presence_list);
	      fprintf (output_description_file, "\n");
	    }
	  if (DECL_UNIT (decl)->final_presence_list != NULL)
	    {
	      fprintf (output_description_file, "unit %s final_presence_set: ",
		       DECL_UNIT (decl)->name);
	      output_pattern_set_el_list
		(DECL_UNIT (decl)->final_presence_list);
	      fprintf (output_description_file, "\n");
	    }
	  if (DECL_UNIT (decl)->absence_list != NULL)
	    {
	      fprintf (output_description_file, "unit %s absence_set: ",
		       DECL_UNIT (decl)->name);
	      output_pattern_set_el_list (DECL_UNIT (decl)->absence_list);
	      fprintf (output_description_file, "\n");
	    }
	  if (DECL_UNIT (decl)->final_absence_list != NULL)
	    {
	      fprintf (output_description_file, "unit %s final_absence_set: ",
		       DECL_UNIT (decl)->name);
	      output_pattern_set_el_list
		(DECL_UNIT (decl)->final_absence_list);
	      fprintf (output_description_file, "\n");
	    }
	}
    }
  fprintf (output_description_file, "\n");
  for (i = 0; i < description->normal_decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_reserv)
	{
          fprintf (output_description_file, "reservation %s: ",
		   DECL_RESERV (decl)->name);
          output_regexp (DECL_RESERV (decl)->regexp);
          fprintf (output_description_file, "\n");
        }
      else if (decl->mode == dm_insn_reserv)
        {
          fprintf (output_description_file, "insn reservation %s ",
		   DECL_INSN_RESERV (decl)->name);
          print_rtl (output_description_file,
		     DECL_INSN_RESERV (decl)->condexp);
          fprintf (output_description_file, ": ");
          output_regexp (DECL_INSN_RESERV (decl)->regexp);
          fprintf (output_description_file, "\n");
        }
      else if (decl->mode == dm_bypass)
	fprintf (output_description_file, "bypass %d %s %s\n",
		 DECL_BYPASS (decl)->latency,
		 DECL_BYPASS (decl)->out_pattern,
		 DECL_BYPASS (decl)->in_pattern);
    }
  fprintf (output_description_file, "\n\f\n");
}

/* The function outputs name of AUTOMATON.  */
static void
output_automaton_name (FILE *f, automaton_t automaton)
{
  if (automaton->corresponding_automaton_decl == NULL)
    fprintf (f, "#%d", automaton->automaton_order_num);
  else
    fprintf (f, "`%s'", automaton->corresponding_automaton_decl->name);
}

/* Maximal length of line for pretty printing into description
   file.  */
#define MAX_LINE_LENGTH 70

/* The function outputs units name belonging to AUTOMATON.  */
static void
output_automaton_units (automaton_t automaton)
{
  decl_t decl;
  const char *name;
  int curr_line_length;
  int there_is_an_automaton_unit;
  int i;

  fprintf (output_description_file, "\n  Corresponding units:\n");
  fprintf (output_description_file, "    ");
  curr_line_length = 4;
  there_is_an_automaton_unit = 0;
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_unit
          && (DECL_UNIT (decl)->corresponding_automaton_num
	      == automaton->automaton_order_num))
	{
	  there_is_an_automaton_unit = 1;
	  name = DECL_UNIT (decl)->name;
	  if (curr_line_length + strlen (name) + 1 > MAX_LINE_LENGTH )
	    {
	      curr_line_length = strlen (name) + 4;
	      fprintf (output_description_file, "\n    ");
	    }
	  else
	    {
	      curr_line_length += strlen (name) + 1;
	      fprintf (output_description_file, " ");
	    }
	  fprintf (output_description_file, "%s", name);
	}
    }
  if (!there_is_an_automaton_unit)
    fprintf (output_description_file, "<None>");
  fprintf (output_description_file, "\n\n");
}

/* The following variable is used for forming array of all possible cpu unit
   reservations described by the current DFA state.  */
static vec<reserv_sets_t> state_reservs;

/* The function forms `state_reservs' for STATE.  */
static void
add_state_reservs (state_t state)
{
  alt_state_t curr_alt_state;

  if (state->component_states != NULL)
    for (curr_alt_state = state->component_states;
         curr_alt_state != NULL;
         curr_alt_state = curr_alt_state->next_sorted_alt_state)
      add_state_reservs (curr_alt_state->state);
  else
    state_reservs.safe_push (state->reservs);
}

/* The function outputs readable representation of all out arcs of
   STATE.  */
static void
output_state_arcs (state_t state)
{
  arc_t arc;
  ainsn_t ainsn;
  const char *insn_name;
  int curr_line_length;

  for (arc = first_out_arc (state); arc != NULL; arc = next_out_arc (arc))
    {
      ainsn = arc->insn;
      gcc_assert (ainsn->first_insn_with_same_reservs);
      fprintf (output_description_file, "    ");
      curr_line_length = 7;
      fprintf (output_description_file, "%2d: ", ainsn->insn_equiv_class_num);
      do
        {
          insn_name = ainsn->insn_reserv_decl->name;
          if (curr_line_length + strlen (insn_name) > MAX_LINE_LENGTH)
            {
              if (ainsn != arc->insn)
                {
                  fprintf (output_description_file, ",\n      ");
                  curr_line_length = strlen (insn_name) + 6;
                }
              else
                curr_line_length += strlen (insn_name);
            }
          else
            {
              curr_line_length += strlen (insn_name);
              if (ainsn != arc->insn)
                {
                  curr_line_length += 2;
                  fprintf (output_description_file, ", ");
                }
            }
          fprintf (output_description_file, "%s", insn_name);
          ainsn = ainsn->next_same_reservs_insn;
        }
      while (ainsn != NULL);
      fprintf (output_description_file, "    %d \n",
	       arc->to_state->order_state_num);
    }
  fprintf (output_description_file, "\n");
}

/* The following function is used for sorting possible cpu unit
   reservation of a DFA state.  */
static int
state_reservs_cmp (const void *reservs_ptr_1, const void *reservs_ptr_2)
{
  return reserv_sets_cmp (*(const_reserv_sets_t const*) reservs_ptr_1,
                          *(const_reserv_sets_t const*) reservs_ptr_2);
}

/* The following function is used for sorting possible cpu unit
   reservation of a DFA state.  */
static void
remove_state_duplicate_reservs (void)
{
  size_t i, j;

  for (i = 1, j = 0; i < state_reservs.length (); i++)
    if (reserv_sets_cmp (state_reservs[j], state_reservs[i]))
      {
	j++;
	state_reservs[j] = state_reservs[i];
      }
  state_reservs.truncate (j + 1);
}

/* The following function output readable representation of DFA(s)
   state used for fast recognition of pipeline hazards.  State is
   described by possible (current and scheduled) cpu unit
   reservations.  */
static void
output_state (state_t state)
{
  size_t i;

  state_reservs.create (0);

  fprintf (output_description_file, "  State #%d", state->order_state_num);
  fprintf (output_description_file,
	   state->new_cycle_p ? " (new cycle)\n" : "\n");
  add_state_reservs (state);
  state_reservs.qsort (state_reservs_cmp);
  remove_state_duplicate_reservs ();
  for (i = 0; i < state_reservs.length (); i++)
    {
      fprintf (output_description_file, "    ");
      output_reserv_sets (output_description_file, state_reservs[i]);
      fprintf (output_description_file, "\n");
    }
  fprintf (output_description_file, "\n");
  output_state_arcs (state);
  state_reservs.release ();
}

/* The following function output readable representation of
   DFAs used for fast recognition of pipeline hazards.  */
static void
output_automaton_descriptions (void)
{
  automaton_t automaton;

  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      fprintf (output_description_file, "\nAutomaton ");
      output_automaton_name (output_description_file, automaton);
      fprintf (output_description_file, "\n");
      output_automaton_units (automaton);
      pass_states (automaton, output_state);
    }
}



/* The page contains top level function for generation DFA(s) used for
   PHR.  */

/* The function outputs statistics about work of different phases of
   DFA generator.  */
static void
output_statistics (FILE *f)
{
  automaton_t automaton;
  int states_num;
#ifndef NDEBUG
  int transition_comb_vect_els = 0;
  int transition_full_vect_els = 0;
  int min_issue_delay_vect_els = 0;
  int locked_states = 0;
#endif

  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      fprintf (f, "\nAutomaton ");
      output_automaton_name (f, automaton);
      fprintf (f, "\n    %5d NDFA states,          %5d NDFA arcs\n",
	       automaton->NDFA_states_num, automaton->NDFA_arcs_num);
      fprintf (f, "    %5d DFA states,           %5d DFA arcs\n",
	       automaton->DFA_states_num, automaton->DFA_arcs_num);
      states_num = automaton->DFA_states_num;
      if (!no_minimization_flag)
	{
	  fprintf (f, "    %5d minimal DFA states,   %5d minimal DFA arcs\n",
		   automaton->minimal_DFA_states_num,
		   automaton->minimal_DFA_arcs_num);
	  states_num = automaton->minimal_DFA_states_num;
	}
      fprintf (f, "    %5d all insns      %5d insn equivalence classes\n",
	       description->insns_num, automaton->insn_equiv_classes_num);
      fprintf (f, "    %d locked states\n", automaton->locked_states);
#ifndef NDEBUG
      fprintf
	(f, "%5ld transition comb vector els, %5ld trans table els: %s\n",
	 (long) automaton->trans_table->comb_vect.length (),
	 (long) automaton->trans_table->full_vect.length (),
	 (comb_vect_p (automaton->trans_table)
	  ? "use comb vect" : "use simple vect"));
      fprintf
        (f, "%5ld min delay table els, compression factor %d\n",
         (long) states_num * automaton->insn_equiv_classes_num,
	 automaton->min_issue_delay_table_compression_factor);
      transition_comb_vect_els
	+= automaton->trans_table->comb_vect.length ();
      transition_full_vect_els
        += automaton->trans_table->full_vect.length ();
      min_issue_delay_vect_els
	+= states_num * automaton->insn_equiv_classes_num;
      locked_states
	+= automaton->locked_states;
#endif
    }
#ifndef NDEBUG
  fprintf (f, "\n%5d all allocated states,     %5d all allocated arcs\n",
	   allocated_states_num, allocated_arcs_num);
  fprintf (f, "%5d all allocated alternative states\n",
	   allocated_alt_states_num);
  fprintf (f, "%5d all transition comb vector els, %5d all trans table els\n",
	   transition_comb_vect_els, transition_full_vect_els);
  fprintf (f, "%5d all min delay table els\n", min_issue_delay_vect_els);
  fprintf (f, "%5d all locked states\n", locked_states);
#endif
}

/* The function output times of work of different phases of DFA
   generator.  */
static void
output_time_statistics (FILE *f)
{
  fprintf (f, "\n  transformation: ");
  print_active_time (f, transform_time);
  fprintf (f, (!ndfa_flag ? ", building DFA: " : ", building NDFA: "));
  print_active_time (f, NDFA_time);
  if (ndfa_flag)
    {
      fprintf (f, ", NDFA -> DFA: ");
      print_active_time (f, NDFA_to_DFA_time);
    }
  fprintf (f, "\n  DFA minimization: ");
  print_active_time (f, minimize_time);
  fprintf (f, ", making insn equivalence: ");
  print_active_time (f, equiv_time);
  fprintf (f, "\n all automaton generation: ");
  print_active_time (f, automaton_generation_time);
  fprintf (f, ", output: ");
  print_active_time (f, output_time);
  fprintf (f, "\n");
}

/* The function generates DFA (deterministic finite state automaton)
   for fast recognition of pipeline hazards.  No errors during
   checking must be fixed before this function call.  */
static void
generate (void)
{
  automata_num = split_argument;
  if (description->units_num < automata_num)
    automata_num = description->units_num;
  initiate_states ();
  initiate_arcs ();
  initiate_automata_lists ();
  initiate_pass_states ();
  initiate_excl_sets ();
  initiate_presence_absence_pattern_sets ();
  automaton_generation_time = create_ticker ();
  create_automata ();
  ticker_off (&automaton_generation_time);
}



/* This page mainly contains top level functions of pipeline hazards
   description translator.  */

/* The following macro value is suffix of name of description file of
   pipeline hazards description translator.  */
#define STANDARD_OUTPUT_DESCRIPTION_FILE_SUFFIX ".dfa"

/* The function returns suffix of given file name.  The returned
   string cannot be changed.  */
static const char *
file_name_suffix (const char *file_name)
{
  const char *last_period;

  for (last_period = NULL; *file_name != '\0'; file_name++)
    if (*file_name == '.')
      last_period = file_name;
  return (last_period == NULL ? file_name : last_period);
}

/* The function returns base name of given file name, i.e. pointer to
   first char after last `/' (or `\' for WIN32) in given file name,
   given file name itself if the directory name is absent.  The
   returned string cannot be changed.  */
static const char *
base_file_name (const char *file_name)
{
  int directory_name_length;

  directory_name_length = strlen (file_name);
#ifdef WIN32
  while (directory_name_length >= 0 && file_name[directory_name_length] != '/'
         && file_name[directory_name_length] != '\\')
#else
  while (directory_name_length >= 0 && file_name[directory_name_length] != '/')
#endif
    directory_name_length--;
  return file_name + directory_name_length + 1;
}

/* A function passed as argument to init_rtx_reader_args_cb.  It parses the
   options available for genautomata.  Returns true if the option was
   recognized.  */
static bool
parse_automata_opt (const char *str)
{
  if (strcmp (str, NO_MINIMIZATION_OPTION) == 0)
    no_minimization_flag = 1;
  else if (strcmp (str, TIME_OPTION) == 0)
    time_flag = 1;
  else if (strcmp (str, STATS_OPTION) == 0)
    stats_flag = 1;
  else if (strcmp (str, V_OPTION) == 0)
    v_flag = 1;
  else if (strcmp (str, W_OPTION) == 0)
    w_flag = 1;
  else if (strcmp (str, NDFA_OPTION) == 0)
    ndfa_flag = 1;
  else if (strcmp (str, COLLAPSE_OPTION) == 0)
    collapse_flag = 1;
  else if (strcmp (str, PROGRESS_OPTION) == 0)
    progress_flag = 1;
  else if (strcmp (str, "-split") == 0)
    {
      fatal ("option `-split' has not been implemented yet\n");
      /* split_argument = atoi (argument_vect [i + 1]); */
    }
  else
    return false;

  return true;
}

/* The following is top level function to initialize the work of
   pipeline hazards description translator.  */
static void
initiate_automaton_gen (const char **argv)
{
  const char *base_name;

  /* Initialize IR storage.  */
  obstack_init (&irp);
  initiate_automaton_decl_table ();
  initiate_insn_decl_table ();
  initiate_decl_table ();
  output_file = stdout;
  output_description_file = NULL;
  base_name = base_file_name (argv[1]);
  obstack_grow (&irp, base_name,
		strlen (base_name) - strlen (file_name_suffix (base_name)));
  obstack_grow (&irp, STANDARD_OUTPUT_DESCRIPTION_FILE_SUFFIX,
		strlen (STANDARD_OUTPUT_DESCRIPTION_FILE_SUFFIX) + 1);
  obstack_1grow (&irp, '\0');
  output_description_file_name = (char *) obstack_base (&irp);
  obstack_finish (&irp);
}

/* The following function checks existence at least one arc marked by
   each insn.  */
static void
check_automata_insn_issues (void)
{
  automaton_t automaton;
  ainsn_t ainsn, reserv_ainsn;

  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      for (ainsn = automaton->ainsn_list;
	   ainsn != NULL;
	   ainsn = ainsn->next_ainsn)
	if (ainsn->first_insn_with_same_reservs && !ainsn->arc_exists_p
	    && ainsn != automaton->collapse_ainsn)
	  {
	    for (reserv_ainsn = ainsn;
		 reserv_ainsn != NULL;
		 reserv_ainsn = reserv_ainsn->next_same_reservs_insn)
	      if (automaton->corresponding_automaton_decl != NULL)
		{
		  if (!w_flag)
		    error ("Automaton `%s': Insn `%s' will never be issued",
			   automaton->corresponding_automaton_decl->name,
			   reserv_ainsn->insn_reserv_decl->name);
		  else
		    warning ("Automaton `%s': Insn `%s' will never be issued",
			     automaton->corresponding_automaton_decl->name,
			     reserv_ainsn->insn_reserv_decl->name);
		}
	      else
		{
		  if (!w_flag)
		    error ("Insn `%s' will never be issued",
			   reserv_ainsn->insn_reserv_decl->name);
		  else
		    warning ("Insn `%s' will never be issued",
			     reserv_ainsn->insn_reserv_decl->name);
		}
	  }
    }
}

/* The following vla is used for storing pointers to all achieved
   states.  */
static vec<state_t> automaton_states;

/* This function is called by function pass_states to add an achieved
   STATE.  */
static void
add_automaton_state (state_t state)
{
  automaton_states.safe_push (state);
}

/* The following function forms list of important automata (whose
   states may be changed after the insn issue) for each insn.  */
static void
form_important_insn_automata_lists (void)
{
  automaton_t automaton;
  decl_t decl;
  ainsn_t ainsn;
  arc_t arc;
  int i;
  size_t n;

  automaton_states.create (0);
  /* Mark important ainsns.  */
  for (automaton = description->first_automaton;
       automaton != NULL;
       automaton = automaton->next_automaton)
    {
      automaton_states.truncate (0);
      pass_states (automaton, add_automaton_state);
      for (n = 0; n < automaton_states.length (); n++)
	{
	  state_t s = automaton_states[n];
	  for (arc = first_out_arc (s);
	       arc != NULL;
	       arc = next_out_arc (arc))
	    if (arc->to_state != s)
	      {
		gcc_assert (arc->insn->first_insn_with_same_reservs);
		for (ainsn = arc->insn;
		     ainsn != NULL;
		     ainsn = ainsn->next_same_reservs_insn)
		  ainsn->important_p = TRUE;
	      }
	}
    }
  automaton_states.release ();

  /* Create automata sets for the insns.  */
  for (i = 0; i < description->decls_num; i++)
    {
      decl = description->decls [i];
      if (decl->mode == dm_insn_reserv)
	{
	  automata_list_start ();
	  for (automaton = description->first_automaton;
	       automaton != NULL;
	       automaton = automaton->next_automaton)
	    for (ainsn = automaton->ainsn_list;
		 ainsn != NULL;
		 ainsn = ainsn->next_ainsn)
	      if (ainsn->important_p
		  && ainsn->insn_reserv_decl == DECL_INSN_RESERV (decl))
		{
		  automata_list_add (automaton);
		  break;
		}
	  DECL_INSN_RESERV (decl)->important_automata_list
	    = automata_list_finish ();
	}
    }
}


/* The following is top level function to generate automat(a,on) for
   fast recognition of pipeline hazards.  */
static void
expand_automata (void)
{
  int i;

  description = XCREATENODEVAR (struct description,
				sizeof (struct description)
				/* Two entries for special insns.  */
				+ sizeof (decl_t) * (decls.length () + 1));
  description->decls_num = decls.length ();
  description->normal_decls_num = description->decls_num;
  description->query_units_num = 0;
  for (i = 0; i < description->decls_num; i++)
    {
      description->decls [i] = decls[i];
      if (description->decls [i]->mode == dm_unit
	  && DECL_UNIT (description->decls [i])->query_p)
        DECL_UNIT (description->decls [i])->query_num
	  = description->query_units_num++;
    }
  all_time = create_ticker ();
  check_time = create_ticker ();
  if (progress_flag)
    fprintf (stderr, "Check description...");
  check_all_description ();
  if (progress_flag)
    fprintf (stderr, "done\n");
  ticker_off (&check_time);
  generation_time = create_ticker ();
  if (!have_error)
    {
      transform_insn_regexps ();
      check_unit_distributions_to_automata ();
    }
  if (!have_error)
    {
      generate ();
      check_automata_insn_issues ();
    }
  if (!have_error)
    {
      form_important_insn_automata_lists ();
    }
  ticker_off (&generation_time);
}

/* The following is top level function to output PHR and to finish
   work with pipeline description translator.  */
static void
write_automata (void)
{
  output_time = create_ticker ();
  if (progress_flag)
    fprintf (stderr, "Forming and outputting automata tables...");
  output_tables ();
  if (progress_flag)
    {
      fprintf (stderr, "done\n");
      fprintf (stderr, "Output functions to work with automata...");
    }
  output_chip_definitions ();
  output_max_insn_queue_index_def ();
  output_internal_min_issue_delay_func ();
  output_internal_trans_func ();
  /* Cache of insn dfa codes: */
  fprintf (output_file, "\nstatic int *%s;\n", DFA_INSN_CODES_VARIABLE_NAME);
  fprintf (output_file, "\nstatic int %s;\n\n",
	   DFA_INSN_CODES_LENGTH_VARIABLE_NAME);
  output_dfa_insn_code_func ();
  output_trans_func ();
  output_min_issue_delay_func ();
  output_internal_dead_lock_func ();
  output_dead_lock_func ();
  output_size_func ();
  output_internal_reset_func ();
  output_reset_func ();
  output_min_insn_conflict_delay_func ();
  output_default_latencies ();
  output_internal_insn_latency_func ();
  output_insn_latency_func ();
  output_internal_maximal_insn_latency_func ();
  output_maximal_insn_latency_func ();
  output_print_reservation_func ();
  /* Output function get_cpu_unit_code.  */
  fprintf (output_file, "\n#if %s\n\n", CPU_UNITS_QUERY_MACRO_NAME);
  output_get_cpu_unit_code_func ();
  output_cpu_unit_reservation_p ();
  output_insn_has_dfa_reservation_p ();
  fprintf (output_file, "\n#endif /* #if %s */\n\n",
	   CPU_UNITS_QUERY_MACRO_NAME);
  output_dfa_clean_insn_cache_func ();
  output_dfa_start_func ();
  output_dfa_finish_func ();
  if (progress_flag)
    fprintf (stderr, "done\n");
  if (v_flag)
    {
      output_description_file = fopen (output_description_file_name, "w");
      if (output_description_file == NULL)
	{
	  perror (output_description_file_name);
	  exit (FATAL_EXIT_CODE);
	}
      if (progress_flag)
	fprintf (stderr, "Output automata description...");
      output_description ();
      output_automaton_descriptions ();
      if (progress_flag)
	fprintf (stderr, "done\n");
      output_statistics (output_description_file);
    }
  if (stats_flag)
    output_statistics (stderr);
  ticker_off (&output_time);
  if (time_flag)
    output_time_statistics (stderr);
  finish_states ();
  finish_arcs ();
  finish_automata_lists ();
  if (time_flag)
    {
      fprintf (stderr, "Summary:\n");
      fprintf (stderr, "  check time ");
      print_active_time (stderr, check_time);
      fprintf (stderr, ", generation time ");
      print_active_time (stderr, generation_time);
      fprintf (stderr, ", all time ");
      print_active_time (stderr, all_time);
      fprintf (stderr, "\n");
    }
  /* Finish all work.  */
  if (output_description_file != NULL)
    {
      fflush (output_description_file);
      if (ferror (stdout) != 0)
	fatal ("Error in writing DFA description file %s: %s",
               output_description_file_name, xstrerror (errno));
      fclose (output_description_file);
    }
  finish_automaton_decl_table ();
  finish_insn_decl_table ();
  finish_decl_table ();
  obstack_free (&irp, NULL);
  if (have_error && output_description_file != NULL)
    remove (output_description_file_name);
}

int
main (int argc, const char **argv)
{
  progname = "genautomata";

  if (!init_rtx_reader_args_cb (argc, argv, parse_automata_opt))
    return (FATAL_EXIT_CODE);

  initiate_automaton_gen (argv);
  md_rtx_info info;
  while (read_md_rtx (&info))
    switch (GET_CODE (info.def))
      {
      case DEFINE_CPU_UNIT:
	gen_cpu_unit (&info);
	break;

      case DEFINE_QUERY_CPU_UNIT:
	gen_query_cpu_unit (&info);
	break;

      case DEFINE_BYPASS:
	gen_bypass (&info);
	break;

      case EXCLUSION_SET:
	gen_excl_set (&info);
	break;

      case PRESENCE_SET:
	gen_presence_set (&info);
	break;

      case FINAL_PRESENCE_SET:
	gen_final_presence_set (&info);
	break;

      case ABSENCE_SET:
	gen_absence_set (&info);
	break;

      case FINAL_ABSENCE_SET:
	gen_final_absence_set (&info);
	break;

      case DEFINE_AUTOMATON:
	gen_automaton (&info);
	break;

      case AUTOMATA_OPTION:
	gen_automata_option (&info);
	break;

      case DEFINE_RESERVATION:
	gen_reserv (&info);
	break;

      case DEFINE_INSN_RESERVATION:
	gen_insn_reserv (&info);
	break;

      default:
	break;
      }

  if (have_error)
    return FATAL_EXIT_CODE;

  if (decls.length () > 0)
    {
      expand_automata ();
      if (!have_error)
	{
	  puts ("/* Generated automatically by the program `genautomata'\n"
		"   from the machine description file `md'.  */\n\n"
		"#define IN_TARGET_CODE 1\n"
		"#include \"config.h\"\n"
		"#include \"system.h\"\n"
		"#include \"coretypes.h\"\n"
		"#include \"tm.h\"\n"
		"#include \"alias.h\"\n"
		"#include \"tree.h\"\n"
		"#include \"varasm.h\"\n"
		"#include \"stor-layout.h\"\n"
		"#include \"calls.h\"\n"
		"#include \"rtl.h\"\n"
		"#include \"memmodel.h\"\n"
		"#include \"tm_p.h\"\n"
		"#include \"insn-config.h\"\n"
		"#include \"recog.h\"\n"
		"#include \"regs.h\"\n"
		"#include \"output.h\"\n"
		"#include \"insn-attr.h\"\n"
                "#include \"diagnostic-core.h\"\n"
		"#include \"flags.h\"\n"
		"#include \"function.h\"\n"
		"#include \"emit-rtl.h\"\n");
	         /* FIXME: emit-rtl.h can go away once crtl is in rtl.h.  */

	  write_automata ();
	}
    }
  else
    {
      puts ("/* Generated automatically by the program `genautomata'\n"
	    "   from the machine description file `md'.  */\n\n"
	    "/* There is no automaton, but ISO C forbids empty\n"
	    "   translation units, so include a header file with some\n"
	    "   declarations, and its pre-requisite header file.  */\n"
	    "#include \"config.h\"\n"
	    "#include \"system.h\"\n");
    }

  fflush (stdout);
  return (ferror (stdout) != 0 || have_error
	  ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
}