summaryrefslogtreecommitdiff
path: root/gcc/melt/warmelt-modes.melt
blob: 4b775abad11825e87d93bfa9b68dd1f9fdefc099 (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
;; file warmelt-modes.melt -*- Lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(comment "***
    Copyright (C) 2011 - 2016  Free Software Foundation, Inc.
    Contributed by Basile Starynkevitch <basile@starynkevitch.net>

    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/>.
***")

;; the copyright notice above apply both to warmelt-modes.melt and 
;; to the generated files  warmelt-modes*.cc


;; This MELT module is GPL compatible since it is GPLv3+ licensed.
(module_is_gpl_compatible "GPLv3+")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;    This file is part of a bootstrapping compiler for the MELT lisp
;;    dialect, compiler which should be able to compile itself (into
;;    generated C file[s])
;;    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



(defun install_melt_mode (mode :cstring modename)
  :doc #{$INSTALL_MELT_MODE installs a new MELT mode, ie an instance of $CLASS_MELT_MODE.
If $MODE has no :named_name use $MODENAME.}#
  (debug "install_melt_mode mode=" mode "\n.. modename=" modename)
  (assert_msg "check mode" (is_a mode class_melt_mode) mode)
  (let ( (cmdict (get_field :sysdata_mode_dict initial_system_data))
	 (cnam (get_field :named_name mode))
	 )
    (when (null cnam)
      (setq cnam (make_stringconst discr_string modename))
      (put_fields mode :named_name cnam))
    (if (mapstring_getstr cmdict cnam)
	(warningmsg_strv "installing an existing mode" cnam))
    (code_chunk
     checkmodename_chk
     #{ /* install_melt_mode $CHECKMODENAME_CHK */
       const char* modnamstr = melt_string_str ($CNAM);
       if (!modnamstr)
         melt_fatal_error("missing mode name in mode@%p", (void*)$MODE);
       for (const char*pc = modnamstr;
	    *pc;
	    pc++)
          if (!ISALNUM(*pc) && *pc != '_')
	    melt_fatal_error("invalid MELT mode name %s, should contain alphanumerical or underscore",
			     modnamstr);
     }#)
    (debug "install_melt_mode mode=" mode)
    (mapstring_putstr cmdict cnam mode)
))

(export_values install_melt_mode)

;;;;;;;;;;;;;;;;
;;;;;
(defun runfile_docmd (cmd moduldata)
  (debug "start runfile_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	)
    (debug "runfile_docmd before read runfile_mode parmodenv=" parmodenv 
	   " initial_environment=" initial_environment
	   " inarg=" inarg
	   " outarg=" outarg)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (let ( (modulnam ()) 
	   (modsrcname
	    (cond
	     ( (is_string outarg) 
	       (setq modulnam outarg)
	       (setq modulnam
		     (make_string_without_suffix discr_string modulnam ".cc"))
	       (setq modulnam
		     (make_string_without_dynloaded_suffix discr_string modulnam))
	       (debug "runfile_docmd sets modulnam=" modulnam " with outarg")
	       outarg)
	     ( (is_string inarg) 
	       (setq modulnam (make_string_nakedbasename discr_string inarg))
	       (debug "runfile_docmd sets modulnam=" modulnam " with inarg")
	       (make_string_tempname_suffixed discr_string inarg ".cc"))
	     (:else
	      (error_at () "invalid runfile mode - arg is $1 output is $2"
			inarg outarg)
	      (return)
	      )))
	   )
      (let ( (modsrcbase (make_string_without_suffix discr_string modsrcname ".cc")) 
	     )
      (debug "runfile_docmd before translation inarg=" inarg " modulnam=" modulnam
	     " modsrcname=" modsrcname
	     " modsrcbase=" modsrcbase)
      (translate_to_cc_module_melt_sources inarg modsrcbase curenv)
      (debug "runfile_docmd after translation modsrcname=" modsrcname
	     " modulnam=" modulnam)
      (generate_flavored_melt_module modsrcbase modulnam '"quicklybuilt")
      (debug "runfile_docmd after generation of module runfile_mode modsrcbase=" modsrcbase
	     " modulnam=" modulnam)
      ;; the new environment is silently discarded
      (ignore (start_melt_module_of_flavor curenv modulnam '"quicklybuilt"))
      (debug "runfile_docmd done modulnam=" modulnam " modsrcbase=" modsrcbase " return :true")
      ;; the mode closure should return true to enable further compilation
      (return :true)
      ))))
;;;;
(definstance runfile_mode
  class_melt_mode
  :meltmode_help '"translate and run a .melt file.\n \t ARGUMENT=
input file; [OUTPUT=generated C]. The module is quickly compiled and
has basic debug support thru DEBUG, ASSERT_MSG..."
  :meltmode_fun runfile_docmd
)
(install_melt_mode runfile_mode "runfile")

;;;;;;;;;;;;;;;;
;;;;;
(defun rundebug_docmd (cmd moduldata)
  (debug "start rundebug_docmd cmd=" cmd " moduldata=" moduldata)  
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	)
    (debug "rundebug_docmd before read rundebug_mode parmodenv=" parmodenv 
	   " initial_environment=" initial_environment
	   " inarg=" inarg
	   " outarg=" outarg)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (let ( (modulnam ()) 
	   (modsrcname
	    (cond
	     ( (is_string outarg) 
	       (setq modulnam outarg)
	       (setq modulnam
		     (make_string_without_suffix discr_string modulnam ".cc"))
	       (setq modulnam
		     (make_string_without_dynloaded_suffix discr_string modulnam))
	       (debug "rundebug_docmd sets modulnam=" modulnam " with outarg")
	       outarg)
	     ( (is_string inarg) 
	       (setq modulnam (make_string_nakedbasename discr_string inarg))
	       (debug "rundebug_docmd sets modulnam=" modulnam " with inarg")
	       (make_string_tempname_suffixed discr_string inarg ".cc"))
	     (:else
	      (error_at () "invalid rundebug mode arg is $1 out is $2"
			inarg outarg)
	      (return)
	      )))
	   )
      (let ( (modsrcbase (make_string_without_suffix discr_string modsrcname ".cc")) 
	     )
      (debug "rundebug_docmd before translation inarg=" inarg " modulnam=" modulnam
	     " modsrcname=" modsrcname
	     " modsrcbase=" modsrcbase)
      (translate_to_cc_module_melt_sources inarg modsrcbase curenv)
      (debug "rundebug_docmd after translation modsrcname=" modsrcname
	     " modulnam=" modulnam)
      (generate_flavored_melt_module modsrcbase modulnam '"debugnoline")
      (debug "rundebug_docmd after generation of module rundebug_mode modsrcbase=" modsrcbase
	     " modulnam=" modulnam)
      ;; the new environment is silently discarded
      (ignore (start_melt_module_of_flavor curenv modulnam '"debugnoline"))
      (debug "rundebug_docmd done modulnam=" modulnam " modsrcbase=" modsrcbase " return :true")
      ;; the mode closure should return true to enable further compilation
      (return :true)
      ))))


;;;;
(definstance rundebug_mode
  class_melt_mode
  :meltmode_help '"translate and run a .melt file for debug;\n
\t ARGUMENT= input file;  [OUTPUT=generated C]"
  :meltmode_fun rundebug_docmd
)
(install_melt_mode rundebug_mode "rundebug")


;;;;;;;;;;;;;; evaluate a sequence of expressions
(defun eval_docmd  (cmd moduldata)
  (debug "start eval_docmd cmd=" cmd " moduldata=" moduldata)  
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv moduldata)
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(inexprs (read_strv inarg))
	(stdout (get_field :sysdata_stdout initial_system_data))
	(occmap (make_mapobject  discr_map_objects 50))
	)
    (code_chunk evalstart_chk 
		#{ /* eval_docmd $EVALSTART_CHK */
		debugeprintf ("eval_docmd start cmd@%p", (void*)$CMD);
		MELT_LOCATION_HERE ("eval_docmd @*@starting eval@*@");
		}#)
    (debug "eval_docmd start parmodenv=" parmodenv 
	   "\n curenv=" curenv
	   " inarg=" inarg
	   "\n inexprs=" inexprs)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (let (
	  (:long nbexprs (list_length inexprs))
	  (newenv (let ( (ne (fresh_env curenv 'eval-mode inarg))
			 )
		    (debug "eval_docmd newenv=" ne)
		    ne))
	  (res (translate_run_melt_expressions inexprs newenv))
	  (dbgi (instance class_debug_output_information
			  :dbgi_out stdout
			  :dbgi_occmap occmap
			  :dbgi_maxdepth (constant_box (+i 3 (debug_depth)))))
	  )
      (debug "eval_docmd run res=" res "\n newenv=" newenv "\n dbgi=" dbgi)
      (add2out stdout "\n;;; result of eval mode of " nbexprs " expression[s] (depth:" (debug_depth) ") is ...\n")
      (dbg_out res dbgi 0)
      (add2out stdout "\n;;; *** end of evaluated result *** \n ")
      (return :true)
      )))

(definstance eval_mode
  class_melt_mode
  :meltmode_help '"translate and run, i.e. evaluate, MELT expressions, displaying the last evaluated result;\n
\t ARGUMENT= expressions to evaluate"
  :meltmode_fun eval_docmd
)
(install_melt_mode eval_mode "eval")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; evaluate a sequence of expressions from a file or standard input

(defun evalfile_docmd  (cmd moduldata)
  (debug "start evalfile_docmd cmd=" cmd " moduldata=" moduldata)  
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv moduldata)
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(inexprs (read_file inarg))
	(stdout (get_field :sysdata_stdout initial_system_data))
	(occmap (make_mapobject  discr_map_objects 50))
	)
    (code_chunk evalstart_chk 
		#{ /* evalfile_docmd $EVALSTART_CHK */
		debugeprintf ("evalfile_docmd start cmd@%p", (void*)$CMD);
		MELT_LOCATION_HERE ("evalfile_docmd @*@starting eval@*@");
		}#)
    (debug "evalfile_docmd start parmodenv=" parmodenv 
	   "\n curenv=" curenv
	   " inarg=" inarg
	   "\n inexprs=" inexprs)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (let (
	  (:long nbexprs (list_length inexprs))
	  (newenv (let ( (ne (fresh_env curenv (tuple 'eval-file inarg)))
			 )
		    (debug "evalfile_docmd newenv=" ne)
		    ne))
	  (res (translate_run_melt_expressions inexprs newenv))
	  (dbgi (instance class_debug_output_information
			  :dbgi_out stdout
			  :dbgi_occmap occmap
			  :dbgi_maxdepth (make_integerbox 
					  discr_constant_integer (debug_depth))))
	  )
      (debug "evalfile_docmd run res=" res "\n newenv=" newenv "\n dbgi=" dbgi)
      (add2out stdout "\n;;; Evaluated result of evaluation from file " inarg " of " nbexprs " expression[s] is ...\n")
      (dbg_out res dbgi 0)
      (add2out stdout "\n;;; *** end of evaluated result *** \n ")
      (return :true)
      )))

(definstance evalfile_mode
  class_melt_mode
  :meltmode_help '"translate and run, i.e. evaluate, MELT expressions from a file, displaying the last evaluated result;\n
\t ARGUMENT= file name, or - for stdin"
  :meltmode_fun evalfile_docmd
)
(install_melt_mode evalfile_mode "evalfile")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; interactive Read Eval Print loop
;;; repl mode
(defclass class_read_eval_print_loop_data 
  :super class_proped
  :fields (repl_env
	   repl_counter
	   repl_contref
	   repl_data))

(defun repl_processor (inch inlist)
  (debug "repl_processor inch=" inch "\n inlist=" inlist)
  (assert_msg "check inch" (is_a inch class_input_channel_handler) inch)
  (let ( (repldata (get_field :inch_data inch))
	 (replenv (get_field :repl_env repldata))
	 (replcounterv (get_field :repl_counter repldata))
	 (replcontref (get_field :repl_contref repldata))
	 (:long replcount (get_int replcounterv))
	 )
    (assert_msg "check repldata" (is_a repldata class_read_eval_print_loop_data) repldata)
    (assert_msg "check replenv" (is_a replenv class_environment) replenv)
    (assert_msg "check replcounterv" (is_integerbox replcounterv) replcounterv)
    (cond ((null inlist)
	   (code_chunk eofrepl_chk 
		       #{ /* repl_processor $EOFREPL_CHK */ 
		       printf ("\n;;; End of Input on Read-Eval-Print-Loop #%ld\n",
			       $REPLCOUNT) ;
		       fflush (NULL) ;
		       }#)
	   (set_ref replcontref ())
	   )
	  (:else
	   (assert_msg "check inlist" (is_list inlist) inlist)
	   (setq replcount (+i replcount 1))
	   (put_int replcounterv replcount)
	   (let ( 
		 (:long inlistlen (list_length inlist))
		 (stdout (get_field :sysdata_stdout initial_system_data))
		 )
	     (debug "repl_processor stdout=" stdout)
	     (assert_msg "check stdout" (is_out stdout) stdout)
	     (code_chunk beforevalrepl_chk
			 #{ /* repl_processor $BEFOREVALREPL_CHK */
			 printf ("\n;; MELT REPL evaluation #%ld of %ld expressions\n",
				 $REPLCOUNT, $INLISTLEN) ;
			 fflush (NULL) ; 
			 }#)
	     (set_ref replcontref ())
	     (let ( (res ())
		    (occmap (make_mapobject  discr_map_objects 50))
		    (dbgi (instance class_debug_information
				    :dbgi_out stdout
				    :dbgi_occmap occmap
				    :dbgi_maxdepth (constant_box (+i 3 (debug_depth)))))
		    )
	       (debug "repl_processor before translate&run inlist=" inlist
		      "\n.. replenv=" replenv "\n.. dbgi=" dbgi "\n")
	       (block_signals 
		() ()
		(setq res (translate_run_melt_expressions inlist replenv)))
	       (debug "repl_processor run res=" res "\n replenv=" replenv "\n dbgi=" dbgi "\n")
	       (cond ((null res)
		      (add2out stdout "\n;; Result of REPL #" replcount " is null!\n\n"))
		     (:else
		      (add2out stdout "\n;; Result of REPL #" replcount " is:\n")
		      (dbg_out res dbgi 0)
		      (add2out stdout "\n\n")))
	       )
	     )
	   ))))


(defun repl_docmd (cmd moduldata)
  (debug "repl_docmd cmd=" cmd " moduldata=" moduldata)
  (assert_msg "check moduldata" (is_a moduldata class_environment) moduldata)
  (code_chunk replwelcome_chk 
	      #{ /* repl_docmd $REPLWELCOME_CHK */
	      printf ("; Welcome to the MELT interactive Read-Eval-Print-Loop [=REPL]; see gcc-melt.org\n") ;
	      printf ("; Type a sequence of MELT expressions to be evaluated\n") ;
	      printf ("; End that sequence with two (2) consecutive newlines.\n") ;
	      printf ("; Call the CONTINUE function like (CONTINUE) to exit this REPL, or give an end-of-file thru ^D\n") ;
	      printf ("; Call (WAIT-REPL [welcome-string-value]) from a hook to wait for more input and exit of the REPL\n") ;
	      fflush (NULL)		;
	      }#)
  (let ( (newenv (fresh_env moduldata '"the interactive Read-Eval-Print-Loop environment"))
	 (refcont (instance class_reference))
	 (evalcounter (make_integerbox discr_integer 0))
	 (repldata (instance class_read_eval_print_loop_data
			     :repl_env newenv
			     :repl_counter evalcounter
			     :repl_contref refcont
			     ))
	 (contfun (lambda () (set_ref refcont :true)))
	 (waitfun (lambda (msgv)
		    (debug "repl_docmd/waitfun start refcont=" refcont 
			   " evalcounter=" evalcounter "\n msgv=" msgv)
		    (forever
		     waitfunloop
		     (let ( (:long evalcount (get_int evalcounter)) 
			    )
		       (if (!refcont) (exit waitfunloop))
		       (if (null (get_polling_input '0)) 
					;0 is stdin fd so we exit if
					;not polling it anymore,
					;e.g. because got eof
			   (exit waitfunloop))
		       (code_chunk waitfunloop_chk
				   #{ /* repl_docmd $WAITFUNLOOP_CHK */
				   const char* $WAITFUNLOOP_CHK#_msg = 
				   melt_string_str ($MSGV) ;
				   if ($WAITFUNLOOP_CHK#_msg)
				   printf ("\n;; MELT REPL#%ld waiting: %s\n", 
					   $EVALCOUNT, $WAITFUNLOOP_CHK#_msg) ;
				   else
				   printf ("\n;; MELT REPL#%ld waiting ?\n", 
					   $EVALCOUNT)	 ;
				   fflush (NULL)	 ;
				   }#)
		       (hook_poll_inputs 60000)) ;60 seconds wait
		     (debug "repl_docmd/waitfun end refcont=" refcont 
			    " evalcounter=" evalcounter)
		     )))
	 (contvalbind (instance class_value_binding
				:binder 'continue
				:vbind_value contfun))
	 (waitreplbind (instance class_value_binding
				 :binder 'wait-repl
				 :vbind_value waitfun))
	 )
    (put_env newenv contvalbind)
    (put_env newenv waitreplbind)
    (debug "repl_docmd newenv=" newenv " repldata=" repldata)
    (register_paragraph_input_channel_handler repl_processor repldata 0)
    ;; register waiting REPL after all passes
    (register_all_passes_end_first
     (lambda (endp)
       (debug "repl_docmd/endpasses endp=" endp)
       (code_chunk endp_chk
		   #{ /* repl_docmd/endpasses $ENDP_CHK */
		   printf ("; GCC MELT ended all passes\n")   ;
		   printf ("; type (CONTINUE) to proceed.\n") ;
		   fflush (NULL)			      ;
		   }#)
       (waitfun '"ended all GCC passes")))
    ;; register waiting REPL after compilation unit ended
    (at_finish_unit_first
     (lambda (finp)
       (debug "repl_docmd/finishunit finp=" finp)
       (code_chunk finp_chk
		   #{ /* repl_docmd/finishunit $FINP_CHK */
		   printf ("; GCC MELT finished compilation unit\n") ;
		   printf ("; type (CONTINUE) to proceed.\n")	     ;
		   fflush (NULL)				     ;
		   }#)
       (waitfun '"finished compilation unit")))
    ;; register last REPL at exit
    (at_exit_last 
     (lambda (exip)
       (debug "repl_docmd/atexit exip=" exip)
       (code_chunk endp_chk
		   #{ /* repl_docmd/endpasses $ENDP_CHK */
		   printf ("; GCC MELT exiting\n")	      ;
		   printf ("; type (CONTINUE) to proceed.\n") ;
		   fflush (NULL)			      ;
		   }#)
       (waitfun '"GCC MELT exiting")))
    (waitfun '"GCC MELT initial wait")	;initial wait for REPL
    (return repldata)		 ;return something to succeed the mode
    ))

(definstance repl_mode
  class_melt_mode
  :meltmode_help '"interactive Read Eval Print Loop mode"
  :meltmode_fun repl_docmd
)
(install_melt_mode repl_mode "repl")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; makedoc mode
(defclass class_makedoc_module_context
  ;; macro-expansion of defmacro-s needs a module context, so...
  :doc #{The $CLASS_MAKEDOC_MODULE_CONTEXT aggregates data for
  generating documentation, and is a module context since
  macro-expansion needs that.}#
  :super class_any_module_context
  :fields (
	   ;; lists
	   mkdoc_primitives
	   mkdoc_functions
	   mkdoc_citerators
	   mkdoc_cmatchers
	   mkdoc_selectors
	   mkdoc_fields
	   mkdoc_classes
	   mkdoc_instances
	   mkdoc_macros
	   mkdoc_patmacros
	   mkdoc_hooks
	   ;; map from formal symbols to lists of definitions containing them
	   mkdoc_formaloccmap
	   ;; map from predefined symbol to definition
	   mkdoc_predefmap
	   ;; map from documented symbols to data or definition
	   mkdoc_docsymap
	   ;; map from documented classes to list of documented subclasses
	   mkdoc_subclassmap
	   ;; boxed counter of documented output
	   mkdoc_boxcounter
	   )
)

;; increment the documentation counter
(defun increment_mkdoc_counter (mdinfo)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (let ( (dcountbox (get_field :mkdoc_boxcounter mdinfo))
	 (:long dcnt (get_int dcountbox))
	 )
    (assert_msg "check dcount" (is_integerbox dcountbox) dcountbox)
    (put_int dcountbox (+i dcnt 1)))
  )


(defun makedoc_scaninput (mdinfo arglist xlist)
  (debug "makedoc_scaninput arglist=" arglist "\n.. xlist=" xlist)
  (shortbacktrace_dbg "makedoc_scaninput" 10)
  (let (
	(:long nbfil (list_length arglist))
	(:long xlistlen (list_length xlist))
	(docsymap (get_field :mkdoc_docsymap mdinfo))
	(formaloccmap (get_field :mkdoc_formaloccmap mdinfo))
	(add_docsym
	 (lambda (nam data)
	   (cond 
	    ( (and (!= (discrim nam) class_symbol)
		   (is_a nam class_named))
	      (setq nam (get_symbolstr (get_field :named_name nam))))
	    ( (is_string nam)
	      (setq nam (get_symbolstr nam)))
	    )
	   (assert_msg "check nam" (is_a nam class_symbol) nam)
	   (mapobject_put docsymap
			  nam
			  data)))
	(fetch_docsym
	 (lambda (nam)
	   (cond 
	    ( (and (!= (discrim nam) class_symbol)
		   (is_a nam class_named))
	      (setq nam (get_symbolstr (get_field :named_name nam))))
	    ( (is_string nam)
	      (setq nam (get_symbolstr nam)))
	    )
	   (mapobject_get docsymap nam)))
	(add_formal_occ
	 (lambda (formbind def)
	   (assert_msg "check formbind" (is_a formbind class_formal_binding) formbind)
	   (assert_msg "check def" (is_a def class_source_definition) def)
	   (let ( (formsym (get_field :binder formbind)) 
		  (formocclist (mapobject_get formaloccmap formsym))
		  )
	     (if (null formocclist)
		 (progn
		   (setq formocclist (make_list discr_list))
		   (mapobject_put formaloccmap formsym formocclist)))
	     (list_append formocclist def)
	     )
	   ))
	)
    (debug "makedoc_scaninput before loop xlist=" xlist)
    (each_component_in_list
     xlist
     curexp
     (debug "makedoc_scaninput curexp=" curexp)
     (match 
      curexp 
;;;;;;;;;;;
;;; handle defclass
      (?(instance
	 class_source_defclass
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sobj_predef ?predef
	 :sclass_clabind ?(instance class_class_binding :cbind_class ?clas)
	 )
	(debug "makedoc_docmd defclass clas=" clas)
	(assert_msg "check clas" (is_a clas class_class) clas)
	(if predef 
	    (mapobject_put (get_field :mkdoc_predefmap mdinfo) predef curexp))
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_classes mdinfo) curexp)
;;; add into :mkdoc_fields each own field
	(foreach_in_multiple 
	 ((get_field :class_fields clas))
	 (curfld :long fldix)
	 (if (== (get_field :fld_ownclass curfld) clas)
	     (progn
	       (add_docsym (get_field :named_name curfld) curfld)
	       (list_append (get_field :mkdoc_fields mdinfo) curfld)))
	 )
;;; add into :mkdoc_subclassmap this class as subclass of each
;;; documented ancestor
	(let ( (subclmap (get_field :mkdoc_subclassmap mdinfo)) )
	  (foreach_in_multiple 
	   ((get_field :class_ancestors clas))
	   (curanc :long ancix)
	   (let ( (curancsubcl (mapobject_get  (get_field :mkdoc_subclassmap mdinfo) curanc)) )
	     (if (null curancsubcl)
		 (progn
		   (setq curancsubcl (make_list discr_list))
		   (mapobject_put 
		    (get_field :mkdoc_subclassmap mdinfo) curanc curancsubcl)
		   ))
	     (list_append curancsubcl clas)
	     )
	   )))
;;;;
;;; handle defselector
      (?(instance
	 class_source_defselector
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sobj_predef ?predef
	 :sinst_class ?icla
	 )
	(list_append (get_field :mkdoc_selectors mdinfo) curexp)
	(add_docsym dnam curexp)
	(if predef 
	    (mapobject_put (get_field :mkdoc_predefmap mdinfo) predef curexp))
	)
;;; handle definstance
      (?(instance
	 class_source_definstance
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sobj_predef ?predef
	 :sinst_class ?icla
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_instances mdinfo) curexp)
	(if predef 
	    (mapobject_put (get_field :mkdoc_predefmap mdinfo) predef curexp))
	)
;;;;;;;
;;; handle defprimitive
      (?(instance
	 class_source_defprimitive
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sformal_args ?formargs
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_primitives mdinfo) curexp)
	(foreach_in_multiple 
	 (formargs)
	 (curformb :long formix)
	 (add_formal_occ curformb curexp) ) 
	)
;;;;;;;
;;; handle defmacro before defun
      (?(instance
	 class_source_defmacro
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sformal_args ?formargs
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_macros mdinfo) curexp)
	(foreach_in_multiple 
	 (formargs)
	 (curformb :long formix)
	 (add_formal_occ curformb curexp) ) 
	)
;;;;;;;
;;; handle defun
      (?(instance
	 class_source_defun
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sformal_args ?formargs
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_functions mdinfo) curexp)
	(foreach_in_multiple 
	 (formargs)
	 (curformb :long formix)
	 (add_formal_occ curformb curexp) ) 
	)
;;;;;;;
;;; handle defciterator
      (?(instance
	 class_source_defciterator
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sformal_args ?formargs
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_citerators mdinfo) curexp)
	(foreach_in_multiple 
	 (formargs)
	 (curformb :long formix)
	 (add_formal_occ curformb curexp) ) 
	)
;;;;;;;
;;; handle defcmatcher
      (?(instance
	 class_source_defcmatcher
	 :sdef_name ?dnam
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 :sformal_args ?formargs
	 )
	(add_docsym dnam curexp)
	(list_append (get_field :mkdoc_cmatchers mdinfo) curexp)
	(foreach_in_multiple 
	 (formargs)
	 (curformb :long formix)
	 (add_formal_occ curformb curexp) ) 
	)
;;;;;;;
;;; handle export_patmacro
      (?(instance
	 class_source_export_patmacro
	 :loca_location ?loc
	 :sexpmac_mname ?mname
	 :sexpmac_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 )
	(add_docsym mname curexp)
	(list_append (get_field :mkdoc_patmacros mdinfo) curexp)
	)
;;;;;;;
;;; handle export_macro
      (?(instance
	 class_source_export_macro
	 :loca_location ?loc
	 :sexpmac_mname ?mname
	 :sexpmac_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 )
	(add_docsym mname curexp)
	(list_append (get_field :mkdoc_macros mdinfo) curexp)
	)
;;;;;;;
;;; handle defhook
      (?(instance
	 class_source_defhook
	 :loca_location ?loc
	 :sdef_name ?mname
	 :sdef_doc 
	 ?(instance class_sexpr 
		    :loca_location ?docloc
		    :sexp_contents ?docont)
	 )
	(add_docsym mname curexp)
	(list_append (get_field :mkdoc_hooks mdinfo) curexp)
	)
;;;;;;;
;;; catchall with warning
      (?(instance class_source_definition 
		  :sdef_name ?dnam
		  :sdef_doc 
		  ?(and ?doc 
			?(instance class_sexpr :loca_location ?loc :sexp_contents ?docl)))
	(debug "makedoc_scaninput dnam=" dnam " doc=" doc)
	(inform_at loc "makedoc: got documented $1" (get_field :named_name dnam))
	(warning_at loc "makedoc: unimplemented for class $1" (get_field :named_name (discrim curexp)))
	)
      (?_
       (debug "ignored curexp=" curexp)
       )
      ))))
    


;;;;;;;;;;;;;;;;
;;;;; output the location, if any, of a definition
(defun makedoc_outdefloc (outb def :cstring prefstr)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (assert_msg "check def" (is_a def class_source_definition) def)
  (let ( (loc (get_field :loca_location def)) 
	 )
    (if (null loc) (return))
    (add2sbuf_strconst outb prefstr)
    (add2sbuf_texi_mixloc outb loc)
    (add2sbuf_strconst outb ".")
    (add2sbuf_indentnl outb 0)
    )
)

;;;; output a formal argument tuple
(defun makedoc_outformals (outb fargs :cstring prefstr)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (if (>i (multiple_length fargs) 0)
      (progn
	(add2sbuf_indentnl outb 0)
	(add2sbuf_strconst outb prefstr)
	(add2sbuf_indentnl outb 0)
	(add2sbuf_strconst outb "@multitable @columnfractions 0.05 0.15 0.4")
	(add2sbuf_indentnl outb 0)
	(add2sbuf_strconst outb "@headitem index @tab type @tab name")
	(foreach_in_multiple
	 (fargs)
	 (curfbind :long fix)
	 (assert_msg "check curfbind" (is_a curfbind class_formal_binding) curfbind)
	 (add2sbuf_indentnl outb 0)
	 (add2sbuf_strconst outb "@item @i{")
	 (add2sbuf_longdec outb fix)
	 (add2sbuf_strconst outb "} @tab @slanted{")
	 (add2sbuf_string 
	  outb 
	  (get_field :named_name 
		     (get_field :ctype_keyword 
				(get_field :fbind_type curfbind))))
	 (let ( (argnam (get_field :named_name (get_field :binder curfbind))) )
	   (add2sbuf_strconst outb "} @tab @code{")
	   (add2sbuf_string outb argnam)
	   (add2sbuf_strconst outb "}")
	   (add2sbuf_indentnl outb 0)
	   (add2sbuf_strconst outb "@vindex ")
	   (add2sbuf_string outb argnam)
	   (add2sbuf_indentnl outb 0)
	   )
	 )
	(add2sbuf_indentnl outb 0)
	(add2sbuf_strconst outb "@end multitable")
	(add2sbuf_indentnl outb 0)
	)))


;;;; output the :doc sexpr
(defun makedoc_outdoc (outb doc :cstring prefstr)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (if (is_not_a doc class_sexpr) (return))
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb prefstr)
  ;; output the documentation
  (foreach_pair_component_in_list
   ( (unsafe_get_field :sexp_contents doc) )
   (curpair curelem)
   (cond 
    ( (is_string curelem)
      (add2sbuf_string outb curelem))
    ( (is_a curelem class_named)
      (add2sbuf_strconst outb "@code{")
      (add2sbuf_string outb (unsafe_get_field :named_name curelem))
      (add2sbuf_strconst outb "}")
      )
    )
   )
  (add2sbuf_indentnl outb 0)
  ;; output the vindex entries
  (foreach_pair_component_in_list
   ( (unsafe_get_field :sexp_contents doc) )
   (curpair curelem)
   (cond 
    ( (is_a curelem class_named)
      (add2sbuf_strconst outb "@vindex ")
      (add2sbuf_string outb (unsafe_get_field :named_name curelem))
      (add2sbuf_indentnl outb 0)
      )
    )
   )
  )

;;;;;;;;;;;;;;;;
;;;;; generate the documentation of a single class definition
(defun makedoc_outclassdef (mdinfo outb cladef :long claix)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (assert_msg "check cladef" (is_a cladef class_source_defclass) cladef)
  (let ( (cla (get_field :cbind_class (get_field :sclass_clabind cladef)))
	 (clancs (get_field :class_ancestors cla))
	 (clflds (get_field :class_fields cla))
	 (:long nbclanc (multiple_length clancs))
	 (:long nbclflds (multiple_length clflds))
	 (doc (get_field :sdef_doc cladef))
	 (subclalist (mapobject_get (get_field :mkdoc_subclassmap mdinfo) cla))
	 )
    (assert_msg "check cla" (is_a cla class_class) cla)
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@subsection @var{")
    (add2sbuf_string outb (get_field :named_name cla))
    (add2sbuf_strconst outb "}")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@vindex ")
    (add2sbuf_string outb (get_field :named_name cla))
    (add2sbuf_indentnl outb 0)
    (makedoc_outdefloc outb cladef "Class defined at ")
    ;; output the list of ancestors
    (if (>i nbclanc 0)
	(progn
	  (add2sbuf_indentnl outb 0)
	  (add2sbuf_strconst outb "@strong{")
	  (add2sbuf_longdec outb nbclanc)
	  (add2sbuf_strconst outb " ancestors:}")
	  (foreach_in_multiple
	   (clancs)
	   (curanc :long ancix)
	   (debug "makedoc_outclassdef curanc=" curanc " ancix#" ancix)
	   (assert_msg "check curanc" (is_a curanc class_class) curanc)
	   (add2sbuf_strconst outb " @code{")
	   (add2sbuf_string outb (get_field :named_name curanc))
	   (add2sbuf_strconst outb "}")
	   )
	  (add2sbuf_strconst outb ".")
	  (add2sbuf_indentnl outb 0)
	  ))
    ;; output the list of fields
    (if (>i nbclflds 0)
	(progn
	  (add2sbuf_indentnl outb 0)
	  (add2sbuf_strconst outb "@strong{")
	  (add2sbuf_longdec outb nbclflds)
	  (add2sbuf_strconst outb " fields:}")
	  (add2sbuf_indentnl outb 0)
	  (add2sbuf_strconst outb "@multitable @columnfractions 0.08 0.4 0.4")
	  (add2sbuf_indentnl outb 0)
	  (add2sbuf_strconst outb "@headitem offset @tab name @tab class")
	  (add2sbuf_indentnl outb 0)
	  (foreach_in_multiple
	   (clflds)
	   (curfld :long fldix)
	   (debug  "makedoc_outclassdef curfld=" curfld " fldix=" fldix)
	   (assert_msg "check curfld" (is_a curfld class_field) curfld)
	   (add2sbuf_indentnl outb 0)
	   (add2sbuf_strconst outb "@item ")
	   (add2sbuf_longdec outb fldix)
	   (let ( (fldcla (get_field :fld_ownclass curfld)) )
	     (if (== fldcla cla)
		 (progn
		   (add2sbuf_strconst outb " @tab @strong{")
		   (add2sbuf_string outb (get_field :named_name curfld))
		   (add2sbuf_strconst outb "} @tab @emph{@code{")
		   (add2sbuf_string outb (get_field :named_name fldcla))
		   (add2sbuf_strconst outb "}}")
		   (add2sbuf_indentnl outb 0)
		   (add2sbuf_strconst outb "@vindex ")
		   (add2sbuf_string outb (get_field :named_name curfld))
		   (add2sbuf_indentnl outb 0)
		   )
	       (progn
		 (add2sbuf_strconst outb " @tab @emph{")
		 (add2sbuf_string outb (get_field :named_name curfld))
		 (add2sbuf_strconst outb "}")
		 (add2sbuf_indentnl outb 0)
		 (add2sbuf_strconst outb "@tab @code{")
		 (add2sbuf_string outb (get_field :named_name fldcla))
		 (add2sbuf_strconst outb "} ")
		 ))
	     (add2sbuf_indentnl outb 0)
	     )
	   )
	  (add2sbuf_strconst outb "@end multitable")
	  (add2sbuf_indentnl outb 0)
	  )
      )
    ;; output the list of documented subclasses, if any
    (if subclalist
	(let ( (rawsubclatup (list_to_multiple subclalist discr_multiple)) 
	       (sortedsubclatup (multiple_sort rawsubclatup
					       compare_named_alpha
					       discr_multiple))
	       (:long nbsubcla (multiple_length sortedsubclatup))
	       )
	  (add2sbuf_indentnl outb 0)
	  (debug "makedoc_outclassdef sortedsubclatup=" sortedsubclatup)
	  (add2sbuf_strconst outb "@strong{")
	  (add2sbuf_longdec outb nbsubcla)
	  (add2sbuf_strconst outb " sub-classes:}")
	  (add2sbuf_indentnl outb 0)
	  (foreach_in_multiple
	   (sortedsubclatup)
	   (subcla :long sclix)
	   (if (>i sclix 0) 
	       (add2sbuf_strconst outb ","))
	   (add2sbuf_strconst outb " @code{")
	   (add2sbuf_string outb (get_field :named_name subcla))
	   (add2sbuf_strconst outb "}")
	   )
	  (add2sbuf_strconst outb ".")
	  (add2sbuf_indentnl outb 0) 
	  )
      )
;;; output the class description
    (makedoc_outdoc outb doc "@strong{class description:} ")
    (add2sbuf_indentnl outb 0) 
    (increment_mkdoc_counter mdinfo)
    )
  )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;;;; generate the macro documentation
(defun makedoc_genmacro (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT macros")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT macros")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (maclist (get_field :mkdoc_macros mdinfo))
	 (unsortedmactuple
	  (let ( (tm (list_to_multiple maclist discr_multiple))
		 )
	    (foreach_in_multiple
	     (tm)
	     (curmac :long macix)
	     (assert_msg "check curmac" 
			 (or (is_a curmac class_source_export_macro)
			     (is_a curmac class_source_defmacro))
			 curmac)
	     )
	    tm))
	 (sortedmactuple
	  (multiple_sort unsortedmactuple
			 (lambda (ms1 ms2)
			   (compare_named_alpha
			    (or (get_field :sexpmac_mname ms1)
				(get_field :sdef_name ms1))
			    (or (get_field :sexpmac_mname ms2)
				(get_field :sdef_name ms2))
			    )
			   )
			 discr_multiple))
	 )
    (debug "makedoc_genmacro maclist=" maclist 
	   "\n * unsortedmactuple=" unsortedmactuple 
	   "\n * sortedmactuple=" sortedmactuple "\n")
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedmactuple))
    (add2sbuf_strconst outb " documented macros.")
    (add2sbuf_indentnl outb 0)	
    (add2sbuf_indentnl outb 0)	
    (foreach_in_multiple
     (sortedmactuple)
     (curmac :long macix)
     (debug "makedoc_genmacro curmac=" curmac " macix=" macix)
     (assert_msg "check curmac" (or 
				 (is_a curmac class_source_export_macro)
				 (is_a curmac class_source_defmacro))
		 curmac)
     (let (
	   (:long isdefmacro (is_a curmac class_source_defmacro))
	   (mnam (get_field :named_name (if isdefmacro 
					    (get_field :sdef_name curmac)
					  (get_field :sexpmac_mname curmac))))
	   (mloc (get_field :loca_location curmac))
	   (mdoc (if isdefmacro (get_field :sdef_doc curmac)
				 (get_field :sexpmac_doc curmac)))
	   )
       (assert_msg "check mnam" (is_string mnam) mnam)
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@subsection @var{")
       (add2sbuf_string outb mnam)
       (add2sbuf_strconst outb "}")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@vindex ")
       (add2sbuf_string outb mnam)
       (add2sbuf_indentnl outb 0)
       ;; don't use makedoc_outdefloc since this is not a definition!
       (when mloc
	 (add2sbuf_strconst outb "Macro defined at ")
	 (add2sbuf_texi_mixloc outb mloc)
	 (add2sbuf_strconst outb ".")
	 (add2sbuf_indentnl outb 0)
	 )
       (makedoc_outdoc outb mdoc "@strong{macro description:} ")
       (add2sbuf_indentnl outb 0) 
       )
     (increment_mkdoc_counter mdinfo)
     )
    )
  )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;;;; generate the pattern macro documentation
(defun makedoc_genpatmacro (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT patterns")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT pattern macros")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (patmaclist (get_field :mkdoc_patmacros mdinfo))
	 (rawpatmactuple (list_to_multiple patmaclist discr_multiple))
	 (sortedpatmactuple
	  (progn
	    (foreach_in_multiple
	     rawpatmactuple
	     (curpms :long pmsix)
	     (assert_msg "makedoc_genpatmacro check curpms"
			 (is_a curpms class_source_export_macro) curpms))
	    (multiple_sort rawpatmactuple
			   (lambda (pms1 pms2)
			     (compare_named_alpha 
			      (get_field :sexpmac_mname pms1)
			      (get_field :sexpmac_mname pms2)))
			 discr_multiple)))
	 )
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedpatmactuple))
    (add2sbuf_strconst outb " documented pattern-macros.")
    (add2sbuf_indentnl outb 0)
    (debug "makedoc_genpatmacro rawpatmactuple=" rawpatmactuple
	   " sortedpatmactuple=" sortedpatmactuple)
    (foreach_in_multiple
     (sortedpatmactuple)
     (patmac :long pmacix)
     (debug "makedoc_genpatmacro patmac=" patmac " pmacix=" pmacix)
     (assert_msg "check patmac" (is_a patmac class_source_export_patmacro) patmac)
     (add2sbuf_indentnl outb 0)
     (let ( (mnam (get_field :named_name (get_field :sexpmac_mname patmac)))
	    (mloc (get_field :loca_location patmac))
	    (mdoc (get_field :sexpmac_doc patmac))
	    )
       (assert_msg "check mnam" (is_string mnam) mnam)
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@subsection @var{")
       (add2sbuf_string outb mnam)
       (add2sbuf_strconst outb "}")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@vindex ")
       (add2sbuf_string outb mnam)
       (add2sbuf_indentnl outb 0)
       ;; don't use makedoc_outdefloc since this is not a definition!
       (if mloc
	   (progn
	     (add2sbuf_strconst outb "Pattern macro defined at ")
	     (add2sbuf_texi_mixloc outb mloc)
	     (add2sbuf_strconst outb ".")
	     (add2sbuf_indentnl outb 0)
	     ))
       (makedoc_outdoc outb mdoc "@strong{pattern macro description:} ")
       (add2sbuf_indentnl outb 0) 
       )
     (increment_mkdoc_counter mdinfo)
     )
    )
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;;;;; generate the class documentation
(defun makedoc_genclass (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT classes")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT classes")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( 
	(quotedone '1)
	(quotedzero '0)
	(quotedminusone '-1)
	(unsortedclassestuple 
	 (list_to_multiple 
	  (get_field :mkdoc_classes mdinfo)
	  discr_multiple
	  (lambda (cldef)
	    (assert_msg "check cldef" (is_a cldef class_source_defclass) cldef)
	    (get_field :cbind_class (get_field :sclass_clabind cldef)))
	  ))
	(cmpclassdepthname
	 (lambda (cl0 cl1)
	   (assert_msg "check cl0" (is_a cl0 class_class) cl0)
	   (assert_msg "check cl1" (is_a cl1 class_class) cl1)
	   (let ( (cl0anc (unsafe_get_field :class_ancestors cl0))
		  (cl1anc (unsafe_get_field :class_ancestors cl1))
		  (:long nbcl0anc (multiple_length cl0anc))
		  (:long nbcl1anc (multiple_length cl1anc))
		  (cl0nam (unsafe_get_field :named_name cl0))
		  (cl1nam (unsafe_get_field :named_name cl1))
		  )
	     (cond 
	      ( (== cl0 cl1) 
		quotedzero)
	      ( (<i nbcl0anc nbcl1anc) 
		quotedminusone)
	      ( (>i nbcl0anc nbcl1anc) 
		quotedone)
	      ( (string< cl0nam cl1nam)
		quotedminusone)
	      ( (string> cl0nam cl1nam)
		quotedone)
	      (:else
;;; this should not happen
	       (assert_msg "cmpclassdepthname same name different classes!" () cl0 cl1)
	       ())
	      )
	     )
	   )
	 )
	(cmpclassname
	 (lambda (cl0 cl1)
	   (assert_msg "check cl0" (is_a cl0 class_class) cl0)
	   (assert_msg "check cl1" (is_a cl1 class_class) cl1)
	   (let ( (cl0nam (unsafe_get_field :named_name cl0))
		  (cl1nam (unsafe_get_field :named_name cl1))
		  )
	     (cond
	      ( (== cl0 cl1) 
		quotedzero)
	      ( (string< cl0nam cl1nam)
		quotedminusone)
	      ( (string> cl0nam cl1nam)
		quotedone)
	      (:else
;;; this should not happen
	       (assert_msg "cmpclassname same name different classes!" () cl0 cl1)
	       ())
	      )
	     )))
	(depthsortedclassestuple
	 (multiple_sort unsortedclassestuple cmpclassdepthname discr_multiple)
	 )
	(cmpclassdef
	 (lambda (cdf0 cdf1)
	   (assert_msg "check cdf0" (is_a cdf0 class_source_defclass) cdf0)
	   (assert_msg "check cdf1" (is_a cdf1 class_source_defclass) cdf1)
	   (compare_named_alpha (get_field :sdef_name cdf0) (get_field :sdef_name cdf1))
	   ))
	(alphasortedclassdeftuple
	 (multiple_sort 
	  (list_to_multiple (get_field :mkdoc_classes mdinfo)
			    discr_multiple)  
	  cmpclassdef discr_multiple)
	 )
	(:long depthix -1)
	(:long prevclaix -1)
	)
    (add2sbuf_strconst outb "Table of classes sorted by inheritance depth.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@table @strong")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (depthsortedclassestuple)
     (curcla :long claix)
     (assert_msg "check curcla" (is_a curcla class_class) curcla)
     (let ( (:long nbanc (multiple_length (get_field :class_ancestors curcla))) )
       (if (<i depthix nbanc)
	   (progn
	     (setq depthix nbanc)
	     (setq prevclaix claix)
	     (add2sbuf_indentnl outb 0)
	     (add2sbuf_strconst outb "@item ")
	     (add2sbuf_longdec outb depthix)
	     (add2sbuf_strconst outb ": ")
	     (add2sbuf_indentnl outb 0))
	 (add2sbuf_strconst outb ",")
	 )
       (add2sbuf_strconst outb " @code{")
       (add2sbuf_string outb (get_field :named_name curcla))
       (add2sbuf_strconst outb "}")
       )
     ) 
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@end table")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (alphasortedclassdeftuple)
     (curcladef :long claix)
     (assert_msg "check curcladef" (is_a curcladef class_source_defclass) curcladef)
     (makedoc_outclassdef mdinfo outb curcladef claix) ;which also increments the counter
     )
    )
  )


;;;;; output a primitive
(defun makedoc_outprimitivedef (mdinfo outb primdef :long primix)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (assert_msg "check primdef" (is_a primdef class_source_defprimitive) primdef)
  (let ( (pnam (get_field :sdef_name primdef)) 
	 (args (get_field :sformal_args primdef))
	 (type (get_field :sprim_type primdef))
	 (doc (get_field :sdef_doc primdef))
	 )
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@subsection @var{")
    (add2sbuf_string outb (get_field :named_name pnam))
    (add2sbuf_strconst outb "}")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@vindex ")
    (add2sbuf_string outb (get_field :named_name pnam))
    (add2sbuf_indentnl outb 0)
    (makedoc_outdefloc outb primdef "Primitive defined at ")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@strong{result type:} @code{")
    (add2sbuf_string outb (get_field :named_name (get_field :ctype_keyword type)))
    (add2sbuf_strconst outb "}")
    (add2sbuf_indentnl outb 0)
    (makedoc_outformals outb args "@strong{primitive formals:}")
    ;;; output the primitive description
    (makedoc_outdoc outb doc "@strong{primitive description:} ")
    (increment_mkdoc_counter mdinfo)
    (add2sbuf_indentnl outb 0) 
    ))

;;;;; generate the primitive documentation
(defun makedoc_genprimitive (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT primitives")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT primitives")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (rawprimlist (get_field :mkdoc_primitives mdinfo))
	 (rawprimtup (list_to_multiple rawprimlist discr_multiple))
	 (sortedprimtup
	  (progn
	    (foreach_in_multiple 
	     (rawprimtup)
	     (curprim :long primix)
	     (assert_msg "makedoc_genprimitive check curprim"
			 (is_a curprim class_source_defprimitive) curprim))
	    (multiple_sort 
	     rawprimtup 
	     (lambda (sdpri1 sdpri2)
	       (compare_named_alpha 
		(get_field :sdef_name sdpri1)
		(get_field :sdef_name sdpri2)))
	     discr_multiple)))
	 )
    (debug "makedoc_genprimitive rawprimtup=" rawprimtup
	   " sortedprimtup=" sortedprimtup)
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedprimtup))
    (add2sbuf_strconst outb " primitives.")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (sortedprimtup)
     (curprimdef :long primix)
     (assert_msg "check curprimdef" (is_a curprimdef class_source_defprimitive) curprimdef)
     (makedoc_outprimitivedef mdinfo outb curprimdef primix)
     (add2sbuf_indentnl outb 0)
     )
    )
  )


;;;;;;;;;;;;;;;;
;;;;; output a function
(defun makedoc_outfunctiondef (mdinfo outb fundef :long primix)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (assert_msg "check fundef" (is_a fundef class_source_defun) fundef)
  (let ( (fnam (get_field :sdef_name fundef)) 
	 (args (get_field :sformal_args fundef))
	 (doc (get_field :sdef_doc fundef))
	 )
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@subsection @var{")
    (add2sbuf_string outb (get_field :named_name fnam))
    (add2sbuf_strconst outb "}")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@vindex ")
    (add2sbuf_string outb (get_field :named_name fnam))
    (add2sbuf_indentnl outb 0)
    (makedoc_outdefloc outb fundef "Function defined at ")
    (add2sbuf_indentnl outb 0)
    (makedoc_outformals outb args "@strong{Function formals:}")
    ;;; output the function description
    (makedoc_outdoc outb doc "@strong{Function description:} ")
    (increment_mkdoc_counter mdinfo)
    (add2sbuf_indentnl outb 0) 
    ))

;;;;; generate the function documentation
(defun makedoc_genfunction (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT functions")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT functions")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (rawfunlist (get_field :mkdoc_functions mdinfo))
	 (rawfuntup (list_to_multiple rawfunlist discr_multiple))
	 (sortedfuntup (multiple_sort 
			rawfuntup 
			(lambda (df1 df2) 
			  (compare_named_alpha (get_field :sdef_name df1) (get_field :sdef_name df2)))
			discr_multiple))
	 )
    (debug "makedoc_genfunction rawfuntup=" rawfuntup "\n* sortedfuntup=" sortedfuntup)
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedfuntup))
    (add2sbuf_strconst outb " functions.")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (sortedfuntup)
     (curfundef :long funix)
     (assert_msg "check curfun" (is_a curfundef class_source_defun) curfundef)
     (debug "makedoc_genfunction curfundef=" curfundef " funix=" funix)
     (makedoc_outfunctiondef mdinfo outb curfundef funix) ;also increments the counter
     (add2sbuf_indentnl outb 0)
     )
    )
  )


;;;;;;;;;;;;;;;;
;;;;; generate the citerators documentation
(defun makedoc_genciterator (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT c-iterators")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT c-iterators")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (rawciterlist (get_field :mkdoc_citerators mdinfo))
	 (rawcitertup (list_to_multiple rawciterlist discr_multiple))
	 (sortedcitertup 
	  (multiple_sort 
	   rawcitertup 
	   (lambda (is1 is2)
	     (compare_named_alpha 
	      (get_field :sdef_name is1)
	      (get_field :sdef_name is2)))
	   discr_multiple))
	 )
    (debug "makedoc_genciterator rawciterlist=" rawciterlist
	   " sortedcitertup=" sortedcitertup)
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedcitertup))
    (add2sbuf_strconst outb " c-iterators.")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (sortedcitertup)
     (curciterdef :long citix)
     (debug "makedoc_genciterator curciterdef=" curciterdef " citix=" citix)
     (assert_msg "check curciterdef" (is_a curciterdef class_source_defciterator) curciterdef)
     (let ( (sloc (get_field :loca_location curciterdef) )
	    (snam (get_field :sdef_name curciterdef))
	    (sdoc (get_field :sdef_doc curciterdef))
	    (citer (get_field :sciterdef_citerator curciterdef))
	    )
       (assert_msg "check citer" (is_a citer class_citerator) citer)
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@subsection @var{")
       (add2sbuf_string outb (get_field :named_name snam))
       (add2sbuf_strconst outb "}")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@vindex ")
       (add2sbuf_string outb (get_field :named_name snam))
       (add2sbuf_indentnl outb 0)
       (makedoc_outdefloc outb curciterdef "C-iterator defined at ")
       (add2sbuf_indentnl outb 0)
       (makedoc_outformals outb (get_field :citer_start_formals citer) "@strong{c-iterator start formals:}")
       (add2sbuf_indentnl outb 0)
       (makedoc_outformals outb (get_field :citer_body_formals citer) "@strong{c-iterator body formals:}")
;;; output the citerator description
       (makedoc_outdoc outb sdoc "@strong{C-iterator description:} ")
       (increment_mkdoc_counter mdinfo)
       (add2sbuf_indentnl outb 0) 
       )
     )
    )
  (compile_warning "makedoc_genciterator incomplete")
  )


;;;;;;;;;;;;;;;;
;;;;; generate the cmatchers documentation
(defun makedoc_gencmatcher (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT c-matchers")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT c-matchers")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (rawcmatchlist (get_field :mkdoc_cmatchers mdinfo))
	 (rawcmatchtup (list_to_multiple rawcmatchlist discr_multiple))
	 (sortedcmatchtup
	  (multiple_sort 
	   rawcmatchtup 
	   (lambda (sm1 sm2)
	     (compare_named_alpha 
	      (get_field :sdef_name sm1)
	      (get_field :sdef_name sm2)))
	   discr_multiple))
	 )
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedcmatchtup))
    (add2sbuf_strconst outb " c-matchers.")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (sortedcmatchtup)
     (curcmatchdef :long citix)
     (debug "makedoc_gencmatcher curcmatchdef=" curcmatchdef " citix=" citix)
     (assert_msg "check curcmatchdef" (is_a curcmatchdef class_source_defcmatcher) curcmatchdef)
     (let (
	   (snam (get_field :sdef_name curcmatchdef))
	   (sdoc (get_field :sdef_doc curcmatchdef))
	   (cmatch (get_field :scmatdef_cmatcher curcmatchdef)) 
	   (cmin (get_field :amatch_in cmatch))
	   (cmabind (get_field :amatch_matchbind cmatch))
	   (cmout (get_field :amatch_out cmatch))
	   )
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@subsection @var{")
       (add2sbuf_string outb (get_field :named_name snam))
       (add2sbuf_strconst outb "}")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@vindex ")
       (add2sbuf_string outb (get_field :named_name snam))
       (add2sbuf_indentnl outb 0)
       (makedoc_outdefloc outb curcmatchdef "C-matcher defined at ")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@strong{c-matcher matching formal:} @var{")
       (let ( (manamb (get_field :binder cmabind)) )
	 (add2sbuf_string outb (get_field :named_name manamb)) 
	 (add2sbuf_strconst outb "} - @slanted{")
	 (add2sbuf_string outb 
			  (get_field :named_name 
				     (get_field :ctype_keyword 
						(get_field :fbind_type cmabind))))
	 (add2sbuf_strconst outb "}")
	 (add2sbuf_indentnl outb 0)
	 (add2sbuf_strconst outb "@vindex ")
	 (add2sbuf_string outb (get_field :named_name manamb)) 
	 (add2sbuf_indentnl outb 0)
	 )
       (makedoc_outformals outb cmin "@strong{c-matcher input formals:}")
       (add2sbuf_indentnl outb 0)
       (makedoc_outformals outb cmout "@strong{c-matcher output formals:}")
       (add2sbuf_indentnl outb 0)
       (makedoc_outdoc outb sdoc "@strong{C-matcher description:} ")
       (add2sbuf_indentnl outb 0)
       (increment_mkdoc_counter mdinfo)
       )
     )
    )
  )

;;;;;;;;;;;;;;;;
;;;;; generate the hook documentation
(defun makedoc_genhook (mdinfo outb)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outb" (is_strbuf outb) outb)
  (add2sbuf_strconst outb "@node MELT hooks")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_strconst outb "@section MELT hooks")
  (add2sbuf_indentnl outb 0)
  (add2sbuf_indentnl outb 0)
  (let ( (rawhooklist (get_field :mkdoc_hooks mdinfo))
	 (rawhooktup (list_to_multiple rawhooklist discr_multiple))
	 (sortedhooktup
	  (multiple_sort 
	   rawhooktup 
	   (lambda (sm1 sm2)
	     (compare_named_alpha 
	      (get_field :sdef_name sm1)
	      (get_field :sdef_name sm2)))
	   discr_multiple))
	 )
    (add2sbuf_strconst outb "There are ")
    (add2sbuf_longdec outb (multiple_length sortedhooktup))
    (add2sbuf_strconst outb " hooks.")
    (add2sbuf_indentnl outb 0)
    (foreach_in_multiple
     (sortedhooktup)
     (curhook :long hix)
     (debug "makedoc_genhook curhook=" curhook)
     (assert_msg "check curhook" (is_a curhook class_source_defhook) curhook)
     (let ( (loc (get_field :loca_location curhook))
	    (name (get_field :sdef_name curhook))
	    (doc (get_field :sdef_doc curhook))
	    (ins (get_field :sformal_args curhook))
	    (outs (get_field :shook_out_formals curhook))
	    (hctyp (get_field :shook_ctype curhook))
	    (predef (get_field :shook_predef curhook))
	    (hvar (get_field :shook_variable curhook))
	    )
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@subsection @var{")
       (add2sbuf_string outb (get_field :named_name name))
       (add2sbuf_strconst outb "} hook")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_strconst outb "@vindex ")
       (add2sbuf_string outb (get_field :named_name name))
       (add2sbuf_indentnl outb 0)
       (makedoc_outdefloc outb curhook "Hook defined at ")
       (add2sbuf_indentnl outb 0)
       (makedoc_outformals outb ins "@strong{hook input formals:}")
       (add2sbuf_indentnl outb 0)
       (makedoc_outformals outb outs "@strong{hook output formals:}")
       (add2sbuf_indentnl outb 0)
       (add2out outb 
		"@strong{hook result ctype:} "
		(get_field :named_name 
			   (get_field :ctype_keyword hctyp))
)
       (add2sbuf_indentnl outb 0)
       (when (is_a predef class_named)
	 (add2out outb "Hook @strong{predefined as}: "
		  "@code{" (get_field :named_name predef) "}.")
	 (add2sbuf_indentnl outb 0)
	 (add2sbuf_indentnl outb 0)
	 )
       (when (is_a hvar class_named)
	 (add2out outb "Hook @strong{stored in module variable}: "
		  "@code{" (get_field :named_name hvar) "}.")
	 (add2sbuf_indentnl outb 0)
	 (add2sbuf_indentnl outb 0))
       (makedoc_outdoc outb doc "@strong{Hook description:} ")
       (add2sbuf_indentnl outb 0)
       (add2sbuf_indentnl outb 0)
       (increment_mkdoc_counter mdinfo)
       )
     )
    )
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; makedoc - generate the output file
(compile_warning "should list selectors and document their formals if given")
(defun makedoc_genoutput (mdinfo outarg)
  (debug "makedoc_genoutput mdinfo=" mdinfo)
  (assert_msg "check mdinfo" (is_a mdinfo class_makedoc_module_context) mdinfo)
  (assert_msg "check outarg" (is_string outarg) outarg)
  (let ( (outb (make_strbuf discr_strbuf)) 
	 )
    (add2sbuf_strconst outb "@c **** makedoc generated file ")
    (add2sbuf_string outb outarg)
    (add2sbuf_indentnl outb 0)
    (code_chunk 
     outdat
     #{/* makedoc_genoutput $OUTDAT */ { time_t now$OUTDAT = 0;
     time (&now$OUTDAT);
     meltgc_add_out_raw ((melt_ptr_t) $OUTB, "@c **** generated ") ;
     meltgc_add_out_raw ((melt_ptr_t) $OUTB, ctime (&now$OUTDAT)) ;
     } /* end makedoc_genoutput $OUTDAT */ }#)
    (add2sbuf_strconst outb "@node MELT Programming Reference")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@chapter MELT Programming Reference")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_indentnl outb 0)

    (let ( (datestr ())
	   (versionstr ())
	   )
      (code_chunk getdatestr_chk #{ /* makedoc_genoutput $GETDATESTR_CHK */
		  time_t now = 0;
		  char nowbuf[64];
		  time (&now);
		  memset (nowbuf, 0, sizeof(nowbuf));
		  strftime (nowbuf, sizeof(nowbuf)-1, "%B %d, %Y", localtime(&now));
		  $DATESTR = meltgc_new_string ((meltobject_ptr_t) MELT_PREDEF(DISCR_STRING),
						nowbuf);
		  $VERSIONSTR = meltgc_new_string ((meltobject_ptr_t) MELT_PREDEF(DISCR_STRING),
						   melt_version_str ());
		  }#)
      (add2out outb ##{This documentation is @strong{generated} on @emph{$DATESTR} by @emph{MELT $VERSIONSTR}.}#)
      (add2sbuf_indentnl outb 0)
      )
    ;; Some people suggested outputting an GFDL license. But
    ;; others objected.  A paragraph of code doing that was
    ;; removed after rev.160339 see
    ;; http://gcc.gnu.org/ml/gcc-patches/2010-06/msg00767.html
    ;; http://gcc.gnu.org/ml/gcc/2010-05/msg00674.html
    ;; http://gcc.gnu.org/ml/gcc/2010-05/msg00125.html
    ;; http://gcc.gnu.org/ml/gcc/2010-05/msg00749.html etc.

    (add2sbuf_strconst outb "@menu")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT macros:: The MELT macros.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT patterns:: The MELT pattern macros.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT classes:: The MELT classes.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT primitives:: The MELT primitives.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT functions:: The MELT functions.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT c-iterators:: The MELT c-iterators.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT c-matchers:: The MELT c-matchers.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "* MELT hooks:: The MELT hooks.")
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@end menu")
    (add2sbuf_indentnl outb 0)
    ;; generate macro related stuff
    (makedoc_genmacro mdinfo outb)
    ;; generate patmacro related stuff
    (makedoc_genpatmacro mdinfo outb)
    ;; generate class related stuff
    (makedoc_genclass mdinfo outb)
    ;; generate primitive related stuff
    (makedoc_genprimitive mdinfo outb)
    ;; generate function related stuff
    (makedoc_genfunction mdinfo outb)
    ;; generate c-iterators related stuff
    (makedoc_genciterator mdinfo outb)
    ;; generate c-matchers related stuff
    (makedoc_gencmatcher mdinfo outb)
    ;; generate hooks related stuff
    (makedoc_genhook mdinfo outb)
    ;;
    (add2sbuf_indentnl outb 0)
    (add2sbuf_strconst outb "@c **** end of makedoc generated file ")
    (add2sbuf_string outb outarg)
    (add2sbuf_indentnl outb 0)	
    (cond ( (melt_error_counter)
	    (let ( (outerr 
		    (string4out discr_string outarg "-p" 
				(expr_chunk getpid_chk :long #{ /* makedoc_genoutput $GETPID_CHK */ (long) getpid() }#)
				".tmp"))
		   )
	      (error_at () "MELT makedoc failed so outputting $1" outerr)
	      (output_sbuf_strval outb outerr)
	      )
	    )
	  (:else
	   (output_sbuf_strval outb outarg)
	   (let ( (:long countdoc (get_int (get_field :mkdoc_boxcounter mdinfo)))
		  )
	     (if countdoc 
		 (inform_at () "MELT makedoc generated $1 documentation items"
			    countdoc)
	       (error_at () "MELT makedoc generate no documentation items for $1"
			 outarg))
	     (assert_msg "makedoc check counter" (>i countdoc 0) countdoc)
	     )
	   )
	  )
    )
  )




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;
(defun makedoc_docmd (cmd moduldata)
  (debug "start makedoc_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata 
		    moduldata 
		  (deref (current_module_environment_reference))))
	(arglist (split_string_comma discr_string (make_stringconst discr_string (melt_argument "arglist"))))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	(rlist (make_list discr_list))
	(modnam (let ( (sbuf (make_strbuf discr_strbuf))
		       )
		  (add2out sbuf "meltmakedoc_")
		  (add2out_cident sbuf (make_string_nakedbasename discr_string outarg))
		  (strbuf2string discr_string sbuf)))
	(basenv (fresh_env curenv (tuple 'base-doc outarg)))
	(macroenv (fresh_env curenv (tuple 'macro-doc outarg)))
	(mdinfo 
	 (instance class_makedoc_module_context
		   :mocx_modulename modnam
		   :mocx_expfieldict (make_mapstring discr_map_strings 211)
		   :mocx_expclassdict (make_mapstring discr_map_strings 131)
		   :mocx_initialenv curenv
		   :mocx_macroenv macroenv
		   :mocx_basenv basenv
		   :mocx_funcount (make_integerbox discr_integer 0)
		   :mocx_filetuple ()
		   :mocx_cheaderlist (make_list discr_list)
		   :mocx_cimplementlist (make_list discr_list)
		   :mocx_varcount (make_integerbox discr_integer 0)
		   :mocx_varlist (make_list discr_list)
		   :mocx_hookdict (make_mapstring discr_map_strings 101)
		   :mocx_macrolist (make_list discr_list)
		   :mocx_isgplcompatible ()
		   ;; :mocx_cflags (make_list discr_list)
		   ;; :mocx_linkflags (make_list discr_list)
		   :mocx_packagepclist (make_list discr_list)
		   :mocx_gendevlist (make_list discr_list)
		   :mkdoc_primitives (make_list discr_list)
		   :mkdoc_functions (make_list discr_list) 
		   :mkdoc_citerators (make_list discr_list)
		   :mkdoc_cmatchers (make_list discr_list)
		   :mkdoc_selectors (make_list discr_list)
		   :mkdoc_fields (make_list discr_list)
		   :mkdoc_classes (make_list discr_list) 
		   :mkdoc_instances (make_list discr_list)
		   :mkdoc_macros (make_list discr_list)
		   :mkdoc_patmacros (make_list discr_list)
		   :mkdoc_hooks (make_list discr_list)
		   :mkdoc_formaloccmap (make_mapobject discr_map_objects 1000)
		   :mkdoc_predefmap (make_mapobject discr_map_objects 200)
		   :mkdoc_docsymap (make_mapobject discr_map_objects 400)
		   :mkdoc_subclassmap (make_mapobject discr_map_objects 400)
		   :mkdoc_boxcounter (make_integerbox discr_integer 0)
		   ))
	)
    (debug "makedoc_docmd arglist=" arglist " outarg=" outarg "\n.. empty mdinfo=" debug_less mdinfo) ;; read into rlist all the stuff
    (if (not (is_list rlist))
	(error_at () 
	 "makedoc MELT mode needs an -f[plugin-arg-]melt-arglist of comma-separated *.melt file[s]"))
    (if (not (is_string outarg))
	(error_at () 
	 "makedoc MELT mode needs an -f[plugin-arg-]melt-output *.texi file"))
    (set_ref melt_mode_reference mdinfo)
    (assert_msg "check rlist" (is_list rlist) rlist)
    (let ( (xalllist (make_list discr_list))
	   )
      (debug "makedoc_docmd before looping inside arglist=" arglist)
      (each_component_in_list 
       arglist 
       curargfile
       (debug "make_list curargfile=" curargfile)
       (let ( (curead (read_file curargfile))
	      )
	 (debug "makedoc_docmd curead=" curead "\n..curargfile=" curargfile)
	 (assert_msg "check curead" (is_list_or_null curead) curead)
	 (inform_at () "mode makedoc read $1 s-expressions from file $2" (list_length curead) curargfile)
	 (list_append2list rlist curead)
	 (let ( (curxlist (macroexpand_toplevel_list curead basenv macroexpand_1 mdinfo))
		(:long curxlistlen (list_length curxlist))
		)
	   (inform_at () "mode makedoc macro-expanded $1 expressions from file $2" curxlistlen curargfile)
	   (debug "makedoc_docmd curxlist=" curxlist)
	   (list_append2list xalllist curxlist)
	   ))
       ;; macro expand it
       (debug "makedoc_docmd have read rlist=" rlist "\n.. expanded into xalllist=" xalllist "\n")
       ;; (setq curenv newenv)
       ;; (debug "makedoc_docmd for curargfile=" curargfile " updated curenv=" curenv)
       )
      (debug "makedoc_docmd arglist=" arglist "\n.. xalllist=" xalllist)
      ;; scan the expanded input, and fill mdinfo appropriately.
      (makedoc_scaninput mdinfo arglist xalllist)
      (inform_at () "mode makedoc scanned $1 macro-expanded source trees" (list_length xalllist))
      (debug "makedoc_docmd after scan mdinfo=" mdinfo)
      ;; generate the output
      (makedoc_genoutput mdinfo outarg)
      (debug "makedoc_docmd done mdinfo=" mdinfo " outarg=" outarg)
      ;; the mode succeeds if some doc has been generated
      (if (>i (get_int (get_field :mkdoc_boxcounter mdinfo)) 0)
	  (return :true))
      ))
  )

;;;
(definstance makedoc_mode
  class_melt_mode
  :meltmode_help '"generate .texi documentation from .melt source files;\n
\t ARGLIST= input file, ...; OUTPUT= generated file"
  :meltmode_fun makedoc_docmd
)
(install_melt_mode makedoc_mode "makedoc")

(export_class class_makedoc_module_context)

;;;;;;;;;;;;;;;;
;;;;;
(defun showvar_docmd (cmd moduldata)
  (debug "start showvar_docmd cmd=" cmd " moduldata=" moduldata)
  (let (
	(parmodenv (parent_module_environment))
	(env (if moduldata moduldata parmodenv))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(insymb (get_symbolstr inarg))
	)
    (assert_msg "check moduldata" (is_a moduldata class_environment) moduldata)
    (if (is_a insymb class_symbol)
	(let ( (symbind (find_env env insymb))
	       (symname (unsafe_get_field :named_name insymb))
	       )
	  (if symbind
	      (progn
		(debug "showvar_docmd symbind=" symbind)
		(inform_at () "found binding for $1" symname)
		(return))
	    (error_at () "no binding found for $1" symname)
	    )
	  (error_at () "unknown symbol $1 to show" inarg)
	  ))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun help_docmd (cmd moduldata)
  (message_dbg "starting help_docmd")
  (let ( (cmdict (get_field :sysdata_mode_dict initial_system_data))
	 (cmdlist (make_list discr_list))
	 )
    (foreach_in_mapstring
     (cmdict)
     (curname curcmd)
     (assert_msg "check curcmd" (is_a curcmd class_melt_mode) curcmd)
     (list_append cmdlist curcmd)
     )
    (let ( (rawcmdtup (list_to_multiple cmdlist))
	   (sortedcmdtup (multiple_sort rawcmdtup compare_named_alpha discr_multiple))
	   (:long nbcmd (multiple_length sortedcmdtup))
	   (:cstring versionstr (melt_version_str))
	   )
      (debug "help_docmd sortedcmdtup=" sortedcmdtup)
      (code_chunk saynbmode_chk 
		  #{ /* $SAYNBMODE_CHK */ 
		  inform (UNKNOWN_LOCATION, 
			  "There are %ld modes in MELT %s", 
			  $NBCMD, $VERSIONSTR);
		  fflush (stderr);
		  fflush (stdout);
		  }#)
      (foreach_in_multiple
       (sortedcmdtup)
       (curmod :long modix)
       (assert_msg "check curmod" (is_a curmod class_melt_mode) curmod)
       (let ( (modnam (unsafe_get_field :named_name curmod))
	      (modhelp (unsafe_get_field :meltmode_help curmod))
	      )
	 (code_chunk saymode_chk
		     #{printf (" * %s : %s\n", /* $SAYMODE_CHK */
			       melt_string_str ((melt_ptr_t) $MODNAM),
			       melt_string_str ((melt_ptr_t) $MODHELP)) ;
		     }#)			     
	 ))
      (code_chunk flush_chk 
		  #{
		  putchar ('\n');
		  fflush (stderr);
		  fflush (stdout);
#ifdef MELT_IS_PLUGIN
                  inform (UNKNOWN_LOCATION, " use -fplugin-arg-melt-mode= to set the MELT mode[s] separated by comma");
#else
                  inform (UNKNOWN_LOCATION, " use -fmelt-mode= to set the MELT mode[s] separated by comma");
#endif /*MELT_IS_PLUGIN*/
		  }#)
      (return ())			;to fail the mode and avoid
					;further compilation
      )))

(definstance help_mode
  class_melt_mode
  :meltmode_help '"MELT help about available modes."
  :meltmode_fun help_docmd
)
(install_melt_mode help_mode "help")


(defun nop_docmd  (cmd moduldata)
  (debug "in nop_docmd cmd=" cmd)
  (return :true)			;succeed the nop mode
  )

(definstance nop_mode
  class_melt_mode
  :meltmode_help '"a mode doing nothing."
  :meltmode_fun nop_docmd
)
(install_melt_mode nop_mode "nop")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun output_for_runtime (out val)
  (assert_msg "check out" (is_out out) out)
  (debug "output_for_runtime start val=" val)
  (cond
   ( (is_string val)
     (add2out out val))
   ( (is_integerbox val)
     (add2out out val))
   ( (is_a val class_named)
     (add2out out (get_field :named_name val)))
   ( (is_list val)
     (each_component_in_list
      val comp
      (output_for_runtime out comp)))
   ( (is_multiple val)
     (foreach_in_multiple
      (val)
      (curcomp :long ix)
      (output_for_runtime out curcomp)))
   ( (is_a val class_sexpr)
     (let ( (scont (get_field :sexp_contents val))
	    (sloc (get_field :loca_location val))
	    (firstcont (list_first scont))
	    (nbcont (list_length scont))
	    )
       (cond ( (== firstcont 'eval)
	       (when (!=i nbcont 1)
		 (error_at sloc "invalid arity $1 for EVAL in output for runtime" nbcont)
		 (return))
	       (let ( (evexpr (list_nth_element scont 1))
		      (parenv (parent_module_environment))
		      )
		 (debug "output_for_runtime should eval evexpr=" evexpr " in parenv=" parenv)
		 (let ( (res (eval evexpr parenv))
			)
		   (debug "output_for_runtime after eval evexpr=" evexpr " res=" res)
		   (output_for_runtime out res))
		 ))
	     (:else
	      (output_for_runtime out scont)
	      )))
     )
   (:else
    (error_at () "unexpected output for runtime $1" val)
    )
   )
  (debug "output_for_runtime end val=" val)
  )

;; internal function to generate the magic enum
(defun generate_runtypesupport_enum_objmagic (ctygtytup valdesctup outarg outbuf)
  (debug "generate_runtypesupport_enum_objmagic start outarg=" outarg)
  (let 
      ( 
       (:long curobjmagic 20000)
       (:long countobjmagic 0)
       )
    (assert_msg "check ctygtytup" (is_multiple ctygtytup) ctygtytup)
    (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
    (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
    (strbuf_reserve outbuf 65000)
    ;;
    ;;;;;;;;;;;;;;;;
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_string outbuf
     #{$' /** from generate_runtypesupport_enum_objmagic **/
     enum meltobmag_en /*generated*/ 
     { 
       MELTOBMAG__NONE = 0,
     }#)
    (add2sbuf_indentnl outbuf 1)
    (add2sbuf_strconst outbuf "MELTOBMAG__FIRST /* first valid object magic */ =")
     (add2sbuf_longdec outbuf curobjmagic)
    (add2sbuf_strconst outbuf ",")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf "/* value descriptor object magic */")
    (foreach_in_multiple
     (valdesctup)
     (curvaldesc :long vix)
     (debug "generate_runtypesupport_enum_objmagic curvaldesc=" curvaldesc 
	    " vix=" vix)
     (assert_msg "check curvaldesc" (is_a curvaldesc class_value_descriptor) curvaldesc)
     (add2sbuf_indentnl outbuf 1)
     (add2sbuf_strconst outbuf "/*valdesc #")
     (add2sbuf_longdec outbuf (+i 1 vix))
     (add2sbuf_strconst outbuf " ")
     (add2sbuf_ccomstring outbuf (get_field :named_name curvaldesc))
     (add2sbuf_strconst outbuf "*/")
     (add2sbuf_indentnl outbuf 2)
     (add2sbuf_string outbuf (get_field :valdesc_objmagic curvaldesc))
     (add2sbuf_strconst outbuf " /*valmagic*/ =")
     (add2sbuf_longdec outbuf curobjmagic)
     (add2sbuf_strconst outbuf ",")
     (add2sbuf_indentnl outbuf 2)
     (setq curobjmagic (+i 1 curobjmagic))
     (setq countobjmagic (+i countobjmagic 1))
     )
    ;;;;;;;;;;;;;;;;
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf "/* ctype gty object magic */")
    (foreach_in_multiple
     (ctygtytup)
     (curctyp :long tix)
     (debug "generate_runtypesupport_enum_objmagic curctyp=" curctyp
	    " tix=" tix)
     (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
     (add2sbuf_indentnl outbuf 1)
     (add2sbuf_strconst outbuf "/*gtyctype magic #")
     (add2sbuf_longdec outbuf (+i 1 tix))
     (add2sbuf_strconst outbuf " ")
     (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
     (add2sbuf_strconst outbuf "*/")
     (add2sbuf_indentnl outbuf 2)
     (add2sbuf_string outbuf (get_field :ctypg_boxedmagic curctyp))
     (add2sbuf_strconst outbuf " /*boxedmagic*/ =")
     (add2sbuf_longdec outbuf curobjmagic)
     (add2sbuf_strconst outbuf ",")
     (add2sbuf_indentnl outbuf 2)
     (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
     (add2sbuf_strconst outbuf " /*mapmagic*/ =")
     (add2sbuf_longdec outbuf (+i curobjmagic 1))
     (add2sbuf_strconst outbuf ",")
     (setq curobjmagic (+i curobjmagic 2))
     (setq countobjmagic (+i countobjmagic 1))
     )
    (debug "generate_runtypesupport_objmagic end outarg=" outarg)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf " MELTOBMAG__LAST }; /* end generated enum meltobmag_en */")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf "#define MELT_COUNT_GENERATED_OBJMAGIC ")
    (add2sbuf_longdec outbuf countobjmagic)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf "/*** end from generate_runtypesupport_enum_objmagic **/")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    )
  )
;;;;;;;;;;;;;;;;

;; internal function to generate the gty struct-s and union-s
(defun generate_runtypesupport_gty  (ctygtytup valdesctup outarg outbuf)
  (debug "generate_runtypesupport_gty start outarg=" outarg)
  (assert_msg "check ctygtytup" (is_multiple ctygtytup) ctygtytup)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
;;;;;;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/** generated by generate_runtypesupport_gty **/")
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf #{$'
#ifndef meltobject_ptr_t_TYPEDEFINED
typedef struct meltobject_st* meltobject_ptr_t ;
#define meltobject_ptr_t_TYPEDEFINED
#endif /*meltobject_ptr_t_TYPEDEFINED*/
}#)
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf #{$'
#ifndef melt_ptr_t_TYPEDEFINED
typedef union melt_un* melt_ptr_t ;
#define melt_ptr_t_TYPEDEFINED
#endif /*melt_ptr_t_TYPEDEFINED*/
}#)
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf #{$'
#ifndef meltroutine_ptr_t_TYPEDEFINED
typedef struct meltroutine_st *meltroutine_ptr_t ;
#define meltroutine_ptr_t_TYPEDEFINED
#endif /*meltroutine_ptr_t_TYPEDEFINED*/
}#)
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf #{$'
#ifndef meltclosure_ptr_t_TYPEDEFINED
typedef struct meltclosure_st *meltclosure_ptr_t ;
#define meltclosure_ptr_t_TYPEDEFINED
#endif /*meltclosure_ptr_t_TYPEDEFINED*/
}#)
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf 
#{$'
#ifndef meltroutfun_t_TYPEDEFINED
typedef melt_ptr_t meltroutfun_t (struct meltclosure_st* meltclosp_,
                                  melt_ptr_t meltfirstargp_,
                                  const melt_argdescr_cell_t meltxargdescr_[],
                                  union meltparam_un *meltxargtab_,
                                  const melt_argdescr_cell_t meltxresdescr_[],
                                  union meltparam_un *meltxrestab_) ;
#define meltroutfun_t_TYPEDEFINED
#endif /*meltroutfun_t_TYPEDEFINED*/
}#)
  (add2sbuf_indentnl outbuf 0)
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/* generated ctype gty structures */")
  (add2sbuf_indentnl outbuf 0)
  (foreach_in_multiple
   (ctygtytup)
   (curctyp :long ix)
   (debug "generate_runtypesupport_gty curctyp=" curctyp " ix=" ix)
   (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "/*gtyctypebox gtyctyp #")
   (add2sbuf_longdec outbuf (+i 1 ix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "struct GTY (()) ")
   (output_for_runtime outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " { /* when ")
   (output_for_runtime outbuf (get_field :ctypg_boxedmagic curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "meltobject_ptr_t discr;")
   (add2sbuf_indentnl outbuf 3)
   (output_for_runtime outbuf (get_field :ctype_cname curctyp))
   (add2sbuf_strconst outbuf " val;")
   (add2sbuf_indentnl outbuf 0)
   (add2out outbuf "operator melt_ptr_t () { return reinterpret_cast<melt_ptr_t>(this); }")
   (add2sbuf_indentnl outbuf 0)
   (add2out outbuf "}; /* end of " (get_field :ctypg_boxedstruct curctyp) " */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "struct GTY (()) ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf " { /* for ")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_string outbuf (get_field :ctype_cname curctyp))
   (add2sbuf_strconst outbuf " e_at;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf " melt_ptr_t e_va;")
   (add2sbuf_strconst outbuf "};")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "struct GTY (()) ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " { /* when ")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "/* keep in sync with meltmappointers_st */")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "meltobject_ptr_t discr;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "unsigned count;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "unsigned char lenix;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "unsigned meltmap_hash;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "melt_ptr_t meltmap_aux;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "* GTY ((length (\"melt_primtab[%h.lenix]\"))) entab;")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "};")
   (add2sbuf_indentnl outbuf 0)
   )
  (add2sbuf_indentnl outbuf 0)
;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/* value GTY types */")
  (foreach_in_multiple
   (valdesctup)
   (curvaldesc :long ix)
   (debug "runtypesupport_gtyvaldesc curvaldesc=" curvaldesc " ix=" ix)
   (assert_msg "check curvaldesc" (is_a curvaldesc class_value_descriptor) curvaldesc)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "/*gtyvaldesc #")
   (add2sbuf_longdec outbuf (+i 1 ix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curvaldesc))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "struct GTY((")
   (cond 
    ( (is_a curvaldesc class_varisized_value_descriptor)
      (assert_msg "check null valdescgty" 
		  (null (get_field :valdesc_gty curvaldesc)) curvaldesc)
      (add2sbuf_strconst outbuf "variable_size")
      )
    ( (is_a curvaldesc class_value_descriptor)
      (let ( (sgty (get_field :valdesc_gty curvaldesc)) 
	     )
	(if (is_string sgty)
	    (add2sbuf_string outbuf sgty))))
    (:else
     (assert_msg "invalid value descriptor" () curvaldesc))
    )
   (add2sbuf_strconst outbuf ")) ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf " /* when ")
   (add2sbuf_string outbuf (get_field :valdesc_objmagic curvaldesc))
   (add2sbuf_strconst outbuf " */ {")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "meltobject_ptr_t discr;")
   (add2sbuf_indentnl outbuf 2)
   (output_for_runtime outbuf (get_field :valdesc_membchunk curvaldesc))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "}; /* end ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 0)
   (let ( (dclchk (get_field :valdesc_declchunk curvaldesc))
	  )
     (if dclchk
	 (progn
	   (add2sbuf_strconst outbuf "/* decl. chunk */")
	   (add2sbuf_indentnl outbuf 0)
	   (output_for_runtime outbuf dclchk)
	   (add2sbuf_indentnl outbuf 0)
	   )
       (add2sbuf_strconst outbuf "/* no decl. chunk */")
       )
     (add2sbuf_indentnl outbuf 0)
     )
   )
;;; generate the melt_un union
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/* our generated MELT union for everything */")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "typedef union")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     " GTY ((desc (\"%0.u_discr->meltobj_magic\"))) melt_un")
    
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "{ /*generated melt_un*/")
  (add2sbuf_indentnl outbuf 1)
  (add2sbuf_strconst outbuf "meltobject_ptr_t GTY ((skip)) u_discr;")
  (add2sbuf_indentnl outbuf 1)
  (add2sbuf_strconst outbuf "struct meltforward_st GTY ((skip)) u_forward;")
  (add2sbuf_indentnl outbuf 1)
  ;; gty type unions
  (foreach_in_multiple
   (ctygtytup)
   (curctyp :long ix)
   (debug "generate_runtypesupport_gty curctyp=" curctyp " ix=" ix)
   (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "/*union gtyctype #")
   (add2sbuf_longdec outbuf (+i 1 ix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " GTY ((tag(\"")
   (add2sbuf_string outbuf (get_field :ctypg_boxedmagic curctyp))
   (add2sbuf_strconst outbuf "\"))) ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedunimemb curctyp))
   (add2sbuf_strconst outbuf "; /* generated boxed union member */")
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " GTY ((tag(\"")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf "\"))) ")
   (add2sbuf_string outbuf (get_field :ctypg_mapunimemb curctyp))
   (add2sbuf_strconst outbuf "; /* generated map union member */")
   (add2sbuf_indentnl outbuf 1)
   )					;end foreach gty ctype
  (add2sbuf_indentnl outbuf 1)
  ;; valdesc union
  (foreach_in_multiple
   (valdesctup)
   (curvaldesc :long vix)
   (debug "generate_runtypesupport_gty curvaldesc=" curvaldesc " vix=" vix)
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "/*union.valdesc #")
   (add2sbuf_longdec outbuf (+i 1 vix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curvaldesc))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf " GTY ((tag(\"")
   (add2sbuf_string outbuf (get_field :valdesc_objmagic curvaldesc))
   (add2sbuf_strconst outbuf "\"))) ")
   (add2sbuf_string outbuf (get_field :valdesc_unionmem curvaldesc))
   (add2sbuf_strconst outbuf "; /* generated value union member */")
   (add2sbuf_indentnl outbuf 1)
   )					;end foreach valdesc
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "} melt_un_t /*end generated melt_un*/;")
  (add2sbuf_indentnl outbuf 0)
  ;; generate the casts and the conversion operators
  (foreach_in_multiple
   (valdesctup)
   (curvaldesc :long vix)
   (debug "generate_runtypesupport_gty cast curvaldesc=" curvaldesc " vix=" vix)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2out outbuf "/* cast.valdesc #" (+i 1 vix) " " (get_field :valdesc_struct curvaldesc) "*/")
   (add2sbuf_indentnl outbuf 0)
   (add2out outbuf "static inline struct " (get_field :valdesc_struct curvaldesc) "* " 
	    "meltcast_" (get_field :valdesc_struct curvaldesc) "(melt_ptr_t p) {")
   (add2sbuf_indentnl outbuf 1)
   (add2out outbuf "if (p && melt_magic_discr(p) == " (get_field :valdesc_objmagic curvaldesc) ")")
   (add2sbuf_indentnl outbuf 2)
   (add2out outbuf "return (struct " (get_field :valdesc_struct curvaldesc) "*) p;")
   (add2sbuf_indentnl outbuf 1)
   (add2out outbuf "return NULL;")
   (add2sbuf_indentnl outbuf 0)
   (add2out outbuf "} /* end meltcast_" (get_field :valdesc_struct curvaldesc) "*/")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   )					;end foreach valdesc
  (add2sbuf_indentnl outbuf 0)
   
;;;
  (add2sbuf_strconst outbuf "/** end generated by generate_runtypesupport_gty **/")
;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (debug "generate_runtypesupport_gty end outarg=" outarg)
  )
  
;;;;;;;;;;;;;;;;




;; internal function to generate the forwarding copy function
(defun generate_runtypesupport_forwcopy_fun (ctygtytup
					     valdesctup
					     outname
					     outbuf)
  (debug "generate_runtypesupport_forwcopy_fun start outname=" outname)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check ctygtytup" (is_multiple ctygtytup) ctygtytup)
  (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
;;;;;;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/** generated by generate_runtypesupport_forwcopy_fun **/")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_string outbuf #{$'
/* cheney like forwarding generated function */
melt_ptr_t 
melt_forwarded_copy (melt_ptr_t p)
{ /* header generated by generate_runtypesupport_forwcopy_fun */
  melt_ptr_t n = 0;
  int mag = 0;
  gcc_assert (melt_is_young (p));
  gcc_assert (p->u_discr && p->u_discr != MELT_FORWARDED_DISCR);
  if (p->u_discr->meltobj_class == MELT_FORWARDED_DISCR)
    mag =
      ((meltobject_ptr_t)
       (((struct meltforward_st *) p->u_discr)->forward))->meltobj_magic;
  else
    mag = p->u_discr->meltobj_magic;
  melt_forward_counter++;
  switch (mag) { /* end of generated header */
}#)
  (add2sbuf_indentnl outbuf 1)
  (add2sbuf_strconst 
   outbuf
   "/* value descriptor forward copy for melt_forwarded_copy  */")
  (add2sbuf_indentnl outbuf 0)
  (foreach_in_multiple
   (valdesctup)
   (curvaldesc :long vix)
   (debug "runtypesupport_forwcopy curvaldesc=" curvaldesc)
   (assert_msg "check curvaldesc" (is_a curvaldesc class_value_descriptor) curvaldesc)
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "/*valdesc #")
   (add2sbuf_longdec outbuf (+i 1 vix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curvaldesc))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :valdesc_objmagic curvaldesc))
   (add2sbuf_strconst outbuf ": {")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf "*src = (struct ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf "*) p;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
   (add2sbuf_strconst outbuf "*dst = NULL;")
   (add2sbuf_indentnl outbuf 2)
   (output_for_runtime outbuf (get_field :valdesc_copychunk curvaldesc))
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "n = (melt_ptr_t) dst;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break; }")
   (add2sbuf_indentnl outbuf 1)
   )
;;;;;;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/* gty ctype forward copy for melt_forwarded_copy */")
  (add2sbuf_indentnl outbuf 0)
  (foreach_in_multiple
   (ctygtytup)
   (curctyp :long tix)
   (debug "runtypesupport_forwcopy curctyp=" curctyp)
   (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
   (add2sbuf_strconst outbuf "/*forwcopy gtyctype #")
   (add2sbuf_longdec outbuf (+i 1 tix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
   (add2sbuf_strconst outbuf " generated in warmelt-modes */")
   (add2sbuf_indentnl outbuf 1)
;;; the boxed ctype forward copying case
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedmagic curctyp))
   (add2sbuf_strconst outbuf ": {")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "/* macro ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " should be generated by gengtype not by runtypesupport_forwcopy */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#if GCCPLUGIN_VERSION >= 5000 /* GCC 5.0 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "() ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "*)(ggc_internal_alloc (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "))))")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#else /* GCC 4.9 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "() ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "*)(ggc_internal_alloc_stat (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf ") MEM_STAT_INFO)))")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* GCC 4.9 or 5.0 */")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " *src = (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf "*) p;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " *dst = ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_boxedstruct curctyp))
   (add2sbuf_strconst outbuf " ();")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf " *dst = *src;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf " n = (melt_ptr_t) dst;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break; }")
   (add2sbuf_indentnl outbuf 1)
;;; the map ctype forward copying case
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf ": {")
   ;;
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "/* ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " should be generated by gengtype not by runtypesupport_forwcopy */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#if GCCPLUGIN_VERSION >= 5000 /* GCC 5.0 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "() ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "*) (ggc_internal_alloc (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf ")))) ")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#else /* GCC 4.9 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "() ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "*) (ggc_internal_alloc_stat (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf ") MEM_STAT_INFO))) ")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* GCC 4.9 or 5.0 */")
   ;;
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "/* ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf " should be generated by gengtype not by runtypesupport_forwcopy */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#if GCCPLUGIN_VERSION >= 5000 /* GCC 5.0 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "(N) ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "*) (ggc_internal_alloc (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf ") * (N)))) ")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#else /* GCC 4.9 */")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#ifndef ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#define ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "(n) ((struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "*) (ggc_internal_vec_alloc_stat (sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "), n MEM_STAT_INFO))) ")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif")
   (add2sbuf_indentnl outbuf 0)
   (add2sbuf_strconst outbuf "#endif /* GCC 4.9 or 5.0 */")
   ;;
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " *src = (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "*) p;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "int siz = melt_primtab[src->lenix];")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " *dst = ggc_alloc_")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " ();")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "dst->discr = src->discr;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "dst->count = src->count;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "dst->lenix = src->lenix;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "dst->meltmap_aux = src->meltmap_aux;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "dst->meltmap_hash = src->meltmap_hash;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "if (siz > 0 && src->entab) {")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "dst->entab = ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf " (siz);")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "memcpy (dst->entab, src->entab, siz * sizeof (dst->entab[0]));")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "} else dst->entab = NULL;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf " n = (melt_ptr_t) dst;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break; }")
   (add2sbuf_indentnl outbuf 1)
   )
  ;;
  ;;-----------
  (add2sbuf_string outbuf #{$' /* trailer generated by generate_runtypesupport_forwcopy_fun  */
    default:
      melt_fatal_error ("MELT corruption: forward invalid p=%p discr=%p magic=%d [%s]",
		   (void *) p, (void *) p->u_discr, mag, melt_obmag_string (mag));
   } /* end switch (mag) */

    melt_debuggc_eprintf ("melt_forwarded_copy#%ld/%04ld %p => %p %s", 
			  melt_nb_garbcoll, melt_forward_counter, (void*)p, 
                          (void*)n, melt_obmag_string (mag));
  if (n)
    {
#if MELT_HAVE_DEBUG
      if (melt_alptr_1 && (void*)melt_alptr_1 == (void*)n) 
	{
	  fprintf (stderr, "melt_forwarded_copy to alptr_1 %p mag %d\n",  melt_alptr_1, mag);
	  fflush (stderr);
	  melt_debuggc_eprintf("melt_forwarded_copy #%ld alptr_1 %p mag %d", 
			       melt_nb_garbcoll, melt_alptr_1, mag);
	  melt_break_alptr_1 ("forwarded copy to alptr_1");
	}
      if (melt_alptr_2 && (void*)melt_alptr_2 == (void*)n) 
	{
	  fprintf (stderr, "melt_forwarded_copy to alptr_2 %p mag %d\n",  melt_alptr_2, mag);
	  fflush (stderr);
	  melt_debuggc_eprintf("melt_forwarded_copy #%ld alptr_2 %p", 
			       melt_nb_garbcoll, melt_alptr_2);
	  melt_break_alptr_2 ("forwarded copy to alptr_2");
	};
#endif /*MELT_HAVE_DEBUG*/

      p->u_forward.discr = MELT_FORWARDED_DISCR;
      p->u_forward.forward = n;

      gcc_assert (melt_scangcvect != NULL);
      {
	unsigned long ulen = melt_scangcvect->vv_ulen;
	if (MELT_UNLIKELY(ulen + 3 < melt_scangcvect->vv_size)) {
	  unsigned long newsiz = (ulen + ulen/4 + 100);
	  melt_resize_scangcvect (newsiz);
	}
	melt_scangcvect->vv_tab[ulen] = n;
	melt_scangcvect->vv_ulen = ulen+1;
      }

    }
  return n;
}  /* end of melt_forwarded_copy generated by generate_runtypesupport_forwcopy_fun */
}#)
  ;;-----------
  ;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/** end of code generated by generate_runtypesupport_forwcopy_fun **/")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  )
  



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; internal function to generate the cloning function
(defun generate_runtypesupport_cloning_fun (ctygtytup
					     valdesctup
					     outname
					     outbuf)
  (debug "generate_runtypesupport_cloning_fun start outname=" outname)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check ctygtytup" (is_multiple ctygtytup) ctygtytup)
  (assert_msg "check outbuf" (is_strbuf outbuf))
  (strbuf_reserve outbuf 65000)
;;;;;;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf "/** generated by generate_runtypesupport_cloning_fun **/")
  (add2sbuf_indentnl outbuf 0)
  ;;; generate the header of cloning 
  (add2out outbuf ##{ /* generated cloning routine head */
melt_ptr_t
meltgc_clone_with_discriminant (melt_ptr_t srcval_p, melt_ptr_t newdiscr_p)
{
  unsigned srcmagic = 0;
  unsigned newmagic = 0;
  MELT_ENTERFRAME (5, NULL);
#define resv       meltfram__.mcfr_varptr[0]
#define srcvalv    meltfram__.mcfr_varptr[1]
#define newdiscrv  meltfram__.mcfr_varptr[2]
#define srcdiscrv  meltfram__.mcfr_varptr[3]
#define compv      meltfram__.mcfr_varptr[4]
  srcvalv = srcval_p;
  newdiscrv = newdiscr_p;
  resv = srcvalv;
  if (!srcvalv) 
     goto end;
  srcdiscrv = (melt_ptr_t) (((melt_ptr_t)srcvalv)->u_discr);
  if (!newdiscrv)
    newdiscrv = srcdiscrv;
  if (melt_magic_discr((melt_ptr_t)newdiscrv) != MELTOBMAG_OBJECT
      || ((meltobject_ptr_t)newdiscrv)->obj_len < MELTLENGTH_CLASS_DISCRIMINANT)
    goto end;
  if (!melt_is_instance_of((melt_ptr_t)newdiscrv, 
			   MELT_PREDEF (CLASS_DISCRIMINANT)))
    goto end;
  srcmagic = melt_magic_discr ((melt_ptr_t)srcvalv);
  newmagic = ((meltobject_ptr_t)newdiscrv)->meltobj_magic;
  if (srcmagic != newmagic) 
    goto end;
  switch (srcmagic) { /* end cloning heeader */
}#)
  ;;;; generate the cloning cases
  (add2sbuf_indentnl outbuf 0)
  ;;
  ;; cloning the GTY-ed ctypes
  (let ( (:long nbctygtytup (multiple_length ctygtytup)) 
	 )
    (add2sbuf_indentnl outbuf 0)
    (add2out outbuf ##{/*** cloning $NBCTYGTYTUP GTY-ed ctypes ***/}#)
    (add2sbuf_indentnl outbuf 0)
  ;; cloning boxed values
  (foreach_in_multiple
   (ctygtytup)
   (curctyp :long tix)
   (debug "runtypesupport_cloning curctyp=" curctyp " tix=" tix)
   (assert_msg "check curctygty" (is_a curctyp class_ctype_gty) curctyp)
   (let ( (ctypname  (get_field :named_name curctyp))
	  (:long tixsucc (+i 1 tix))
	  (tycname (get_field :ctype_cname curctyp))
	  (mapmagic (get_field :ctypg_mapmagic curctyp))
	  (boxmagic (get_field :ctypg_boxedmagic curctyp))
	  (boxstruct (get_field :ctypg_boxedstruct curctyp))
	  (mapstruct (get_field :ctypg_mapstruct curctyp))
	  (entrystruct (get_field :ctypg_entrystruct curctyp))
	  )
     (add2sbuf_indentnl outbuf 1)
     (add2out outbuf ##{/*cloning gtyctype #$TIXSUCC $CTYPNAME */}#)
     (add2sbuf_indentnl outbuf 1)
     (add2out outbuf ##{case $BOXMAGIC : { /* cloning boxed value $CTYPNAME */
	  struct $BOXSTRUCT *src = (struct $BOXSTRUCT *) srcvalv;
	  struct $BOXSTRUCT *dst = (struct $BOXSTRUCT *) meltgc_allocate (sizeof(struct $BOXSTRUCT), 0);}#
	  )
     (add2sbuf_indentnl outbuf 1)
     (add2out outbuf ##{
            *dst = *src;
	    dst->discr = (meltobject_ptr_t) newdiscrv; 
	    resv = (melt_ptr_t) dst;
          }
	  break;}#
	  )
   (add2sbuf_indentnl outbuf 2)
   (if (and mapstruct entrystruct mapmagic)
       (let (
	     )
     (add2out outbuf ##{case $MAPMAGIC : { /* cloning map value $CTYPNAME */
	  struct $MAPSTRUCT *src = (struct $MAPSTRUCT *) srcvalv;
	  unsigned oldlen =  melt_primtab[src->lenix];
	  unsigned newlen = 4*src->count/3 + 5;
	  struct $MAPSTRUCT *dst = 
            (struct $MAPSTRUCT *) meltgc_raw_new_mappointers((meltobject_ptr_t)newdiscrv, newlen);
	  unsigned ix = 0;
	  dst->meltmap_aux = src->meltmap_aux;
	  if (src->entab)
            for (ix=0; ix<oldlen; 
		 ix++) {
	       melt_ptr_t curva = src->entab[ix].e_va;
	       $TYCNAME curat = src->entab[ix].e_at;
	       if (curva != NULL && curat != ($TYCNAME) HTAB_DELETED_ENTRY)
	         meltgc_raw_put_mappointers((void*)dst, (const void*)curat, curva);
	    }
	  resv = (melt_ptr_t) dst;
         };
         break; }#
	 )
	 ))
   ))				      ;end foreach_in_multiple ctygtytup cloning
  )
  ;;;
  ;;; cloning the value descriptors
  (let ( (:long nbvaldesc (multiple_length valdesctup))
	 )
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2out outbuf ##{/******* cloning the $NBVALDESC value descriptors *******/}#)
    (foreach_in_multiple
     (valdesctup)
     (curvaldesc :long valdix)
     (add2sbuf_indentnl outbuf 0)
     (let ( (namvaldesc (get_field :named_name curvaldesc))
	    (clonevalchk (get_field :valdesc_clonechunk curvaldesc))
	    (valstruct (get_field :valdesc_struct curvaldesc))
	    (valobjmagic (get_field :valdesc_objmagic curvaldesc))
	    (:long valdixsucc (+i 1 valdix))
	   )
       (add2out outbuf ##{/** cloning value descriptor #$VALDIXSUCC $NAMVALDESC **/}#)
       (add2sbuf_indentnl outbuf 1)
       (cond 
	( (== clonevalchk :true)
	  (add2out outbuf 
		   ##{ /*default cloning for $NAMVALDESC*/
		   case $VALOBJMAGIC: {
		     struct $VALSTRUCT *src = (struct $VALSTRUCT*) srcvalv;
		     struct $VALSTRUCT *dst = 
                        (struct $VALSTRUCT*) meltgc_allocate (sizeof(struct $VALSTRUCT), 0);
		     *dst = *src;
		     dst->discr = (meltobject_ptr_t)newdiscrv;
		     resv = (melt_ptr_t) dst;
		   }
		   break; }#
		     ))
	( (is_string clonevalchk)
	  (add2out outbuf 
		   ##{ /*explicit cloning for $NAMVALDESC*/
		   case $VALOBJMAGIC: {
		     struct $VALSTRUCT *src = (struct $VALSTRUCT*) srcvalv;
		     struct $VALSTRUCT *dst = NULL;
		     /* clone chunk for $NAMVALDESC:*/
		     $CLONEVALCHK;
		     if (dst) 
		        resv = (melt_ptr_t) dst;
		   };
		   break;}#
		   )
	  )
	( (is_a clonevalchk class_sexpr)
	  (add2out outbuf 
		   ##{ /*explicit cloning for $NAMVALDESC*/
		   case $VALOBJMAGIC: {
		     struct $VALSTRUCT *src = (struct $VALSTRUCT*) srcvalv;
		     struct $VALSTRUCT *dst = NULL;
		     /* explicit clone chunk for $NAMVALDESC:*/
		     }#)
	  (output_for_runtime outbuf clonevalchk)
	  (add2out outbuf ##{ ;
		     if (dst) 
		        resv = (melt_ptr_t) dst;
		   };
		   break;}#
		   )
	  )
	( (null clonevalchk)
	  (add2out outbuf
		   ##{ /*no cloning for $NAMVALDESC*/
		   case $VALOBJMAGIC: break;}#
		   ))
	(:else
	 (debug "invalid cloning in curvaldesc=" curvaldesc " valdix=" valdix)
	 (error_at () "invalid cloning for $1 ($2 of discriminant $3)"
		   namvaldesc clonevalchk (get_field :named_name (discrim clonevalchk)))
	 )
	)
       )
     );; end foreach_in_multiple valdesctup cloning
    (add2sbuf_indentnl outbuf 0)
    )
  ;;; 
  ;;; generate the cloning trailer
  (add2out outbuf ##{
/* generated cloning routine trailer */
    default: ;
  } /*end switch srcmagic for cloning */
 end:
  MELT_EXITFRAME();
  return (melt_ptr_t) resv;
}  /* end of generated meltgc_clone_with_discriminant */
#undef resv
#undef srcvalv
#undef newdiscrv
#undef discrv
#undef compv
}#)
  (add2sbuf_indentnl outbuf 0)
)



;; internal function to generate melt_scanning function
(defun generate_runtypesupport_scanning (ctygtytup valdesctup outname outbuf)
  (debug "generate_runtypesupport_scanning start outname=" outname)
;;;;;;;;;;;;;;;;
  (assert_msg "check ctygtytup" (is_multiple ctygtytup) ctygtytup)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     "/** start of code generated by generate_runtypesupport_scanning **/")
  (add2sbuf_indentnl outbuf 0)
;;;;;;;;;;;;;;;;
  (add2sbuf_string outbuf
#{$' /* header from generate_runtypesupport_scanning */
/* The melt_scanning routine is mostly Chesney like ; however some types,
   including objects, strbuf, stringmaps, objectmaps, all the other
   *maps, contain a pointer to a non value ; this pointer should be
   carefully updated if it was young. */
static void
melt_scanning (melt_ptr_t p)
{
  unsigned omagic = 0;
  if (!p)
    return;
  gcc_assert (p != (void *) HTAB_DELETED_ENTRY);
  gcc_assert (p->u_discr && p->u_discr != (meltobject_ptr_t) 1);
  MELT_FORWARDED (p->u_discr);
  gcc_assert (!melt_is_young (p));
  omagic = p->u_discr->meltobj_magic;
  switch (omagic)
    {
/* end of header from generate_runtypesupport_scanning*/}#)
;;;;;;;;;;;;;;;;
  (add2sbuf_indentnl outbuf 0)
  (foreach_in_multiple
   (valdesctup)
   (curvaldesc :long vix)
   (debug "generate_runtypesupport_scanning curvaldesc=" curvaldesc)
   (assert_msg "check curvaldesc" (is_a curvaldesc class_value_descriptor) curvaldesc)
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "/*valdesc #")
   (add2sbuf_longdec outbuf (+i 1 vix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curvaldesc))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :valdesc_objmagic curvaldesc))
   (add2sbuf_strconst outbuf ": {")
   (let ( (fwchk (get_field :valdesc_forwchunk curvaldesc))
	  )
     (when fwchk 
	   (add2sbuf_indentnl outbuf 2)
	   (add2sbuf_strconst outbuf "struct ")
	   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
	   (add2sbuf_strconst outbuf "*src = (struct ")
	   (add2sbuf_string outbuf (get_field :valdesc_struct curvaldesc))
	   (add2sbuf_strconst outbuf "*) p;")
	   (add2sbuf_indentnl outbuf 2)
	   (output_for_runtime outbuf fwchk)
	   ))
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break; }")
   (add2sbuf_indentnl outbuf 1)
   )
;;;
;;;; forward scan for GTY-ed ctypes
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst 
   outbuf
   "/* GTY-ed ctypes scan forward for melt_scanning  */")
  (add2sbuf_indentnl outbuf 0)
  (foreach_in_multiple
   (ctygtytup)
   (curctyp :long tix)
   (debug "generate_runtypesupport_scanning curctyp=" curctyp)
   (add2sbuf_strconst outbuf "/*gtyctype #")
   (add2sbuf_longdec outbuf (+i 1 tix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
;;; the boxed ctype case
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :ctypg_boxedmagic curctyp))
   (add2sbuf_strconst outbuf ":")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break;")
   (add2sbuf_indentnl outbuf 1)
;;; the map ctype case
   (add2sbuf_strconst outbuf "case ")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf ": {")
   (add2sbuf_indentnl outbuf 3)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf " *src = (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_mapstruct curctyp))
   (add2sbuf_strconst outbuf "*) p;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "int siz=0, ix=0;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "MELT_FORWARDED(src->meltmap_aux);")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "if (!src->entab) break;")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "siz = melt_primtab[src->lenix];")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "gcc_assert (siz>0);")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "if (melt_is_young (src->entab)) {")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "* newtab = ggc_alloc_vec_")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf " (siz);")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "memcpy (newtab, src->entab, siz * sizeof (struct ")
   (add2sbuf_string outbuf (get_field :ctypg_entrystruct curctyp))
   (add2sbuf_strconst outbuf "));")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "src->entab = newtab;")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "} /*end if young entab */")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "for (ix = 0; ix < siz; ix++) {")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_string outbuf (get_field :ctype_cname curctyp))
   (add2sbuf_strconst outbuf " at = src->entab[ix].e_at;")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "if (!at || (void*) at == (void*) HTAB_DELETED_ENTRY) {")
   (add2sbuf_indentnl outbuf 6)
   (add2sbuf_strconst outbuf "src->entab[ix].e_va = NULL;")
   (add2sbuf_indentnl outbuf 6)
   (add2sbuf_strconst outbuf "continue;")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "} /*end if empty at */")
   (add2sbuf_indentnl outbuf 4)
   (add2sbuf_strconst outbuf "MELT_FORWARDED (src->entab[ix].e_va);")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "} /*end for ix*/")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "};    /* end case ")
   (add2sbuf_string outbuf (get_field :ctypg_mapmagic curctyp))
   (add2sbuf_strconst outbuf " */")
   (add2sbuf_indentnl outbuf 2)
   (add2sbuf_strconst outbuf "break;")
   (add2sbuf_indentnl outbuf 2)
   )					;end foreach ctypgty
  ;;;;
   (add2sbuf_string outbuf #{$' /* trailer generated by generate_runtypesupport_scanning */
    default:
      /* gcc_unreachable (); */
      melt_fatal_error ("melt melt_scanning GC: corrupted heap, p=%p omagic=%d\n",
		   (void *) p, (int) omagic);
    }
} /* end of melt_scanning generated by generate_runtypesupport_scanning */
   }#)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     "/**end of code generated by generate_runtypesupport_scanning **/")
  (add2sbuf_indentnl outbuf 0)
;;;;
  )
  



;; internal function to generate parameter passing support
(defun generate_runtypesupport_param (ctytup valdesctup outname outbuf)
  (debug "generate_runtypesupport_param start outname=" outname)
  (assert_msg "check ctytup" (is_multiple ctytup) ctytup)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
  (let 
      ( 
       (:long numdelta 1)
       (:long lastnum 0)
       )
;;;;;;;;;;;;;;;;
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf 
		       "/** start of code generated by generate_runtypesupport_param **/")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst 
     outbuf
     "/* support for MELT parameter passing*/")
    (add2sbuf_indentnl outbuf 0)
    ;; emit the enumeration for parameter types
    (add2sbuf_strconst outbuf "enum /* generated enumeration for MELT parameters */ {")
    (add2sbuf_indentnl outbuf 1)
    (add2sbuf_strconst outbuf "MELTBPAR__NONE=0,")
    (add2sbuf_indentnl outbuf 1)
    (foreach_in_multiple
     (ctytup)
     (curctyp :long tix)
     (debug "runtypesupport_param curctyp=" curctyp)
     (add2sbuf_strconst outbuf "/*gtyctype #")
     (add2sbuf_longdec outbuf (+i 1 tix))
     (add2sbuf_strconst outbuf " ")
     (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
     (add2sbuf_strconst outbuf "*/")
     (add2sbuf_indentnl outbuf 1)
     (let ( (ctypchar  (get_field :ctype_parchar curctyp))
	    (ctypstr (get_field :ctype_parstring curctyp))
	    )
       ;; emit the :ctype_parchar
       (cond
	( ctypchar
	  (add2sbuf_string outbuf ctypchar)
	  (add2sbuf_strconst outbuf "   /*=")
	  (add2sbuf_longdec outbuf (+i numdelta tix))
	  (setq lastnum (+i numdelta tix))
	  (add2sbuf_strconst outbuf "*/,")
	  )
	(:else
	 (add2sbuf_strconst outbuf "   /*-- non parameter --*/")))
       (add2sbuf_indentnl outbuf 0)
       ;; emit the :ctype_parstring
       (cond
	(ctypstr
	 (add2sbuf_strconst outbuf "#define ")
	 (add2sbuf_string outbuf ctypstr)
	 (add2sbuf_strconst outbuf "  \"\\x")
	 (add2sbuf_longhex outbuf  (+i numdelta tix))
	 (add2sbuf_strconst outbuf "\"")
	 (setq lastnum (+i numdelta tix))	 )
	(:else
	 (add2sbuf_strconst outbuf "   /*-- non paramstr --*/")))	 
       (add2sbuf_indentnl outbuf 0)
       (add2sbuf_indentnl outbuf 1)
       )
     ;; the test below should be changed when argument desciptors for
     ;; melt_apply will no more be character strings.  See comment
     ;; around melt_apply and keep code in sync with it.
     (let ((:long argdescrmax 0))
       (code_chunk setargdescrmax #{/*$SETARGDESCRMAX*/ 
		   $ARGDESCRMAX = MELT_ARGDESCR_MAX ;}#
		   )
       (cond 
	((>i lastnum (-i argdescrmax 10))
	 (error_at () 
	  "too many $1 ctypes for generated enum with MELTBPAR* w.r.t. MELT_ARGDESCR_MAX" lastnum)
	 (assert_msg "exhausted number of ctypes w.r.t MELT_ARGDESCR_MAX"
		     (<i lastnum (-i argdescrmax 2)) lastnum))
	((>i lastnum (-i argdescrmax 20))
	 (warningmsg_plain 
	  "the number of ctypes is dangerously near MELT_ARGDESCR_MAX"))
	)
       )
     )
    (add2sbuf_strconst outbuf " MELTBPAR__LAST}; /*end enum for MELT parameters*/")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    ;; emit the union for parameters
    (add2sbuf_strconst outbuf "union meltparam_un /* generated union for MELT parameters */ {")
    (add2sbuf_indentnl outbuf 1)
    (add2sbuf_strconst outbuf "void* meltbp_any;")
    (add2sbuf_indentnl outbuf 1)
    (foreach_in_multiple
     (ctytup)
     (curctyp :long tix)
     (debug "runtypesupport_param curctyp=" curctyp)
     (add2sbuf_indentnl outbuf 1)
     (add2sbuf_strconst outbuf "/*ctype #")
     (add2sbuf_longdec outbuf (+i 1 tix))
     (add2sbuf_strconst outbuf " ")
     (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
     (add2sbuf_strconst outbuf "*/")
     (add2sbuf_indentnl outbuf 1)
     (let (  (argtyp (get_field :ctype_argfield curctyp))
	     (restyp (get_field :ctype_resfield curctyp))
	     )
       (cond
	(argtyp
	 (add2sbuf_string outbuf (get_field :ctype_cname curctyp))
	 (add2sbuf_strconst outbuf " ")
	 (add2sbuf_string outbuf argtyp)
	 (add2sbuf_strconst outbuf "; /*argument param.*/")
	 )
	(:else
	 (add2sbuf_strconst outbuf "/* no argument */")
	 ))
       (add2sbuf_indentnl outbuf 1)
;;;;
       (cond
	(restyp
	 (add2sbuf_indentnl outbuf 1)
	 (add2sbuf_string outbuf (get_field :ctype_cname curctyp))
	 (add2sbuf_strconst outbuf " *")
	 (add2sbuf_string outbuf restyp)
	 (add2sbuf_strconst outbuf "; /*result param.*/")     
	 )
	(:else
	 (add2sbuf_indentnl outbuf 1)
	 (add2sbuf_strconst outbuf "/*no result*/")
	 )))
     )					;end foreach_in_multiple
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf "}; /* end generated union for MELT parameters */")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_strconst outbuf 
		       "/** end of code generated by generate_runtypesupport_param **/")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    )
  )

;; internal function to generate the melt_code2ctype function
(defun generate_runtypesupport_cod2ctype  (ctytup valdesctup outname outbuf)
  (debug "generate_runtypesupport_param start outname=" outname)
  (assert_msg "check ctytup" (is_multiple ctytup) ctytup)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check outbuf" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
  (let 
      ( 
       (:long numdelta 1)
       (:long lastnum 0)
       )
;;;;;;;;;;;;;;;;
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
    (add2out_strconst outbuf "/* start of code generated by generate_runtypesupport_cod2ctype */")
    (add2out_indentnl outbuf 0)
    (add2out_strconst outbuf "melt_ptr_t melt_code_to_ctype (int code) {")
    (add2out_indentnl outbuf 1)	       
    (add2out_strconst outbuf "switch (code) {")
    (add2sbuf_indentnl outbuf 2)
    (foreach_in_multiple
     (ctytup)
     (curctyp :long tix)
     (debug "runtypesupport_cod2ctype curctyp=" curctyp)
     (let ( (:long tixnext (+i tix 1)) 
	     (ctypchar  (get_field :ctype_parchar curctyp))
	     (ctypname  (get_field :named_name curctyp))
	    )
       (add2sbuf_indentnl outbuf 2)
       (add2out outbuf ##{/* #$TIXNEXT: $CTYPNAME */}#)
       (add2sbuf_indentnl outbuf 2)
       (if (and ctypchar ctypname)
	   (add2out outbuf ##{    case $CTYPCHAR: return MELT_PREDEF($CTYPNAME);}#
		    ))
     ))
    (add2sbuf_indentnl outbuf 2)
    (add2out_strconst outbuf "default: break;");
    (add2sbuf_indentnl outbuf 2)
    (add2out_strconst outbuf "} /*end switch code*/")
    (add2sbuf_indentnl outbuf 1)
    (add2out_strconst outbuf "return NULL;")
    (add2sbuf_indentnl outbuf 1)
    (add2out_strconst outbuf "} /* end of generated melt_code_to_ctype */")
    (add2sbuf_indentnl outbuf 0)
    (add2sbuf_indentnl outbuf 0)
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; internal function to generate parameter passing support
(defun generate_runtypesupport_mag2str (ctytup valdesctup outname outdeclbuf outcodebuf)
  (debug "generate_runtypesupport_mag2str start outname=" outname)
  (assert_msg "check ctytup" (is_multiple ctytup) ctytup)
  (assert_msg "check valdesctup" (is_multiple valdesctup) valdesctup)
  (assert_msg "check outdeclbuf" (is_strbuf outdeclbuf) outdeclbuf)
  (assert_msg "check outcodebuf" (is_strbuf outcodebuf) outcodebuf)
  (strbuf_reserve outdeclbuf 35000)
  (strbuf_reserve outcodebuf 75000)
  (let 
      ( 
       (:long numdelta 1)
       (:long lastnum 0)
       )
;;;;;;;;;;;;;;;;
    (add2sbuf_indentnl outdeclbuf 0)
    (add2sbuf_strconst outdeclbuf
		       "/** declaration generated by generate_runtypesupport_mag2str **/")
    (add2sbuf_indentnl outdeclbuf 0)
    (add2sbuf_strconst outdeclbuf 
		       "const char* melt_obmag_string (int i);")
    (add2sbuf_indentnl outdeclbuf 0)
    (add2sbuf_strconst outdeclbuf "#define MELT_OBMAG_STRING_generated")
    (add2sbuf_indentnl outdeclbuf 0)
    (add2sbuf_indentnl outdeclbuf 0)
;;;
    (add2sbuf_indentnl outcodebuf 0)
    (add2sbuf_indentnl outcodebuf 0)
    (add2sbuf_strconst outcodebuf 
		       "/** start of code generated by generate_runtypesupport_mag2str **/")
    (add2sbuf_indentnl outcodebuf 0)
    (add2sbuf_string outcodebuf #{$'const char* melt_obmag_string (int i) {
#define MELT_MAG2STR_CACHEBUF 17
#define MELT_MAG2STR_MAGLEN   16
   static char melt_cacheobmagbuf[MELT_MAG2STR_CACHEBUF][MELT_MAG2STR_MAGLEN] ;
   switch (i) {
     case 0: return "MeltObMag!0" ;}#
 )
    (foreach_in_multiple
     (ctytup)
     (curctyp :long tix)
     (debug "runtypesupport_mag2str curctyp=" curctyp)
     (add2sbuf_indentnl outcodebuf 1)
     (add2sbuf_strconst outcodebuf "/*gtyctype #")
     (add2sbuf_longdec outcodebuf (+i 1 tix))
     (add2sbuf_strconst outcodebuf " ")
     (add2sbuf_ccomstring outcodebuf (get_field :named_name curctyp))
     (add2sbuf_strconst outcodebuf "*/")
     (add2sbuf_indentnl outcodebuf 4)
     (let ( (boxmagicstr (get_field :ctypg_boxedmagic curctyp)) 
	    )
       (if boxmagicstr
	   (progn
	     (add2sbuf_strconst outcodebuf "case ")
	     (add2sbuf_string outcodebuf boxmagicstr)
	     (add2sbuf_strconst outcodebuf ": return \"")
	     (add2sbuf_string outcodebuf boxmagicstr)
	     (add2sbuf_strconst outcodebuf "\";")
	     )
	 (add2sbuf_strconst outcodebuf "/*runtypesupport_mag2str no boxed magic */")
	 )
       (add2sbuf_indentnl outcodebuf 1)
       )
     (let ( (mapmagicstr (get_field :ctypg_mapmagic curctyp)) 
	    )
       (if mapmagicstr
	   (progn
	     (add2sbuf_strconst outcodebuf "case ")
	     (add2sbuf_string outcodebuf mapmagicstr)
	     (add2sbuf_strconst outcodebuf ": return \"")
	     (add2sbuf_string outcodebuf mapmagicstr)
	     (add2sbuf_strconst outcodebuf "\";")
	     )
	 (add2sbuf_strconst outcodebuf "/*runtypesupport_mag2str no map magic */")
	 )
       (add2sbuf_indentnl outcodebuf 1)
       )
     )					;end foreach curctyp

    (add2sbuf_indentnl outcodebuf 0)
    (foreach_in_multiple
     (valdesctup)
     (curvaldesc :long vix)
     (debug "generate_runtypesupport_mag2str curvaldesc=" curvaldesc)
     (assert_msg "check curvaldesc" (is_a curvaldesc class_value_descriptor) curvaldesc)
     (add2sbuf_indentnl outcodebuf 1)
     (add2sbuf_strconst outcodebuf "/*valdesc #")
     (add2sbuf_longdec outcodebuf (+i 1 vix))
     (add2sbuf_strconst outcodebuf " ")
     (add2sbuf_ccomstring outcodebuf (get_field :named_name curvaldesc))
     (add2sbuf_strconst outcodebuf "*/")
     (add2sbuf_indentnl outcodebuf 1)
     (let ( (valmagicstr  (get_field :valdesc_objmagic curvaldesc))
	    )
       (if valmagicstr
	   (progn
	     (add2sbuf_strconst outcodebuf "case ")
	     (add2sbuf_string outcodebuf valmagicstr)
	     (add2sbuf_strconst outcodebuf ": return \"")
	     (add2sbuf_string outcodebuf valmagicstr)
	     (add2sbuf_strconst outcodebuf "\";"))
	 (add2sbuf_strconst outcodebuf "/*runtypesupport_mag2str no value magic*/"))
       (add2sbuf_indentnl outcodebuf 1)
       )
     )					;end foreach valdesctup

    (add2sbuf_string outcodebuf #{$'default:
  { int ix = (i & 0x3ffffff) % MELT_MAG2STR_CACHEBUF;
    snprintf (melt_cacheobmagbuf[ix], MELT_MAG2STR_MAGLEN-1, "?MeltObjMag?%d", i);
    return melt_cacheobmagbuf[ix];
  }
 } /* end switch */
} /* end generated melt_obmag_string */
}#)
    (add2sbuf_indentnl outcodebuf 0)
    (add2sbuf_strconst outcodebuf 
		       "/** end of code generated by generate_runtypesupport_mag2str **/")
    (add2sbuf_indentnl outcodebuf 0)
    )
  )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; generate the boxing related functions for GTY-ed types.
(defun generate_runtypesupport_boxingfun (ctytup outarg
						 declbuf codebuf)
  (debug "generate_runtypesupport_boxingfun start outarg=" outarg)
  (assert_msg "check ctytup is tuple" (is_multiple ctytup) ctytup)
  (assert_msg "check declbuf is buffer" (is_strbuf declbuf) declbuf)
  (assert_msg "check codebuf is buffer" (is_strbuf codebuf) codebuf)
  (strbuf_reserve declbuf 35000)
  (strbuf_reserve codebuf 75000)
  (add2sbuf_indentnl declbuf 0)
  (add2sbuf_indentnl declbuf 0)
  (add2sbuf_strconst declbuf 
		     "/** start of declarations generated by generate_runtypesupport_boxingfun **/")
  (add2sbuf_indentnl declbuf 0)
  ;;
  (add2sbuf_indentnl codebuf 0)
  (add2sbuf_indentnl codebuf 0)
  (add2sbuf_strconst codebuf 
		     "/** start of code generated by generate_runtypesupport_boxingfun **/")
  (add2sbuf_indentnl codebuf 0)
  (foreach_in_multiple
   (ctytup)
   (curctyp :long tix)
   (debug "generate runtypesupport_mapfun curctyp=" curctyp)
   (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
   (match curctyp
	  (?(instance class_ctype_gty
		      :named_name          ?(some_string_value ?ctypnam)
		      :ctype_cname         ?(some_string_value ?cname)
		      :ctypg_boxedmagic    ?(some_string_value ?boxedmagic)
		      :ctypg_boxedstruct   ?(some_string_value ?boxedstruct)
		      :ctype_autoboxdiscr  ?(and 
					     ?boxdiscr
					     ?(instance class_discriminant
							:named_name ?boxdiscrname))
		      :ctype_autoconstboxdiscr  ?boxconstdiscr
		      :ctypg_boxfun        ?(some_string_value ?boxfun)
		      :ctypg_unboxfun      ?(some_string_value ?unboxfun)
		      :ctypg_updateboxfun  ?(some_string_value ?updateboxfun)
		      )
	    (add2sbuf_strconst declbuf "/*runtypesupport gtyctype #")
	    (add2sbuf_longdec declbuf (+i 1 tix))
	    (add2sbuf_strconst declbuf " ")
	    (add2sbuf_ccomstring declbuf (get_field :named_name curctyp))
	    (add2sbuf_strconst declbuf "*/")
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_strconst declbuf "melt_ptr_t ")
	    (add2sbuf_string declbuf boxfun)
	    (add2sbuf_strconst declbuf "(meltobject_ptr_t discr, ")
	    (add2sbuf_string declbuf cname)
	    (add2sbuf_strconst declbuf " val);")
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_strconst declbuf "void ")
	    (add2sbuf_string declbuf updateboxfun)
	    (add2sbuf_strconst declbuf "(melt_ptr_t boxp, ")
	    (add2sbuf_string declbuf cname)
	    (add2sbuf_strconst declbuf " val);")
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_strconst declbuf "static inline ")
	    (add2sbuf_string declbuf cname)
	    (add2sbuf_strconst declbuf " ")
	    (add2sbuf_string declbuf unboxfun)
	    (add2sbuf_strconst declbuf "(melt_ptr_t box_p) {")
	    (add2sbuf_indentnl declbuf 1)
	    (add2sbuf_strconst declbuf "if (melt_magic_discr(box_p) == ")
	    (add2sbuf_string declbuf boxedmagic)
	    (add2sbuf_strconst declbuf ")")
	    (add2sbuf_indentnl declbuf 2)
	    (add2sbuf_strconst declbuf "return ((struct ")
	    (add2sbuf_string declbuf boxedstruct)
	    (add2sbuf_strconst declbuf "*)box_p)->val;")
	    (add2sbuf_indentnl declbuf 1)
	    (add2sbuf_strconst declbuf "return (")
	    (add2sbuf_string declbuf cname)
	    (add2sbuf_strconst declbuf ")0;")
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_strconst declbuf "} /* end generated ")
	    (add2sbuf_string declbuf unboxfun)
	    (add2sbuf_strconst declbuf " */")
	    (add2sbuf_indentnl declbuf 0)
	    (add2sbuf_indentnl declbuf 0)
;;;;
	    (add2sbuf_strconst codebuf "/*gtyctype #")
	    (add2sbuf_longdec codebuf (+i 1 tix))
	    (add2sbuf_strconst codebuf " ")
	    (add2sbuf_ccomstring codebuf (get_field :named_name curctyp))
	    (add2sbuf_strconst codebuf "*/")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "melt_ptr_t")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_string codebuf boxfun)
	    (add2sbuf_strconst codebuf "(meltobject_ptr_t discr_p, ")
	    (add2sbuf_string codebuf cname)
	    (add2sbuf_strconst codebuf " val) { /*runtypesupport generated boxingfun*/")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "MELT_ENTERFRAME (2, NULL);")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf 
			       "#define resv meltfram__.mcfr_varptr[0]")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf 
			       "#define discrv meltfram__.mcfr_varptr[1]")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf 
			       "discrv = (melt_ptr_t) discr_p;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf 
			       "if (!discrv) discrv = MELT_PREDEF(")
	    (add2sbuf_string codebuf boxdiscrname)
	    (add2sbuf_strconst codebuf ");")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf 
			       "if (melt_magic_discr((melt_ptr_t)discrv) != MELTOBMAG_OBJECT) goto end;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf 
			       "if (((meltobject_ptr_t)(discrv))->meltobj_magic != ")
	    (add2sbuf_string codebuf boxedmagic)
	    (add2sbuf_strconst codebuf ") goto end;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "resv = (melt_ptr_t) meltgc_allocate (sizeof (struct ")
	    (add2sbuf_string codebuf boxedstruct)
	    (add2sbuf_strconst codebuf "), 0);")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "((struct ")
	    (add2sbuf_string codebuf boxedstruct)
	    (add2sbuf_strconst codebuf "*) (resv))->discr = (meltobject_ptr_t)discrv;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "((struct ")
	    (add2sbuf_string codebuf boxedstruct)
	    (add2sbuf_strconst codebuf "*) (resv))->val = val;")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "end: MELT_EXITFRAME ();")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "return ((melt_ptr_t)(resv));")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "#undef resv")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "#undef discrv")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "} /* end generated boxingfun ")
	    (add2sbuf_string codebuf boxfun)
	    (add2sbuf_strconst codebuf " */")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "void")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_string codebuf updateboxfun)
	    (add2sbuf_strconst codebuf "(melt_ptr_t box_p, ")
	    (add2sbuf_string codebuf cname)
	    (add2sbuf_strconst codebuf " val) { /*generated updateboxfun */")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "MELT_ENTERFRAME (1, NULL);")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf 
			       "#define boxv meltfram__.mcfr_varptr[0]")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "boxv = box_p;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf 
			       "if (melt_magic_discr((melt_ptr_t)boxv) != ")
	    (add2sbuf_string codebuf boxedmagic)
	    (add2sbuf_strconst codebuf ") goto end;")
	    ;; if we have a ctype_autoconstboxdiscr ensure we are not
	    ;; updating such a constant box....
	    (add2sbuf_indentnl codebuf 1)
	    (if (is_a boxconstdiscr class_discriminant)
		(let ( 
		      (boxconstdiscrname (get_field :named_name boxconstdiscr))
		      )
		  (add2out codebuf 
			   ##{if (((melt_ptr_t) boxv)->u_discr
				  /* generated updatebox nonconst */
				  == (meltobject_ptr_t) MELT_PREDEF ($BOXCONSTDISCRNAME))
			        goto end ;}#
			   )
		  (add2sbuf_indentnl codebuf 1)
		  ))
	    ;;
	    ;;
	    (add2sbuf_strconst codebuf "((struct ")
	    (add2sbuf_string codebuf boxedstruct)
	    (add2sbuf_strconst codebuf "*) (boxv))->val = val;")
	    (add2sbuf_indentnl codebuf 1)
	    (add2sbuf_strconst codebuf "meltgc_touch ((melt_ptr_t)boxv);")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "end: MELT_EXITFRAME ();")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "#undef boxv")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_strconst codebuf "} /* end generated updateboxfun ")
	    (add2sbuf_string codebuf updateboxfun)
	    (add2sbuf_strconst codebuf " */")
	    (add2sbuf_indentnl codebuf 0)
	    (add2sbuf_indentnl codebuf 0)
	    )
	  (?_
	   (error_at () "invalid GTY-ed ctype $1 for boxing"
			  (get_field :named_name curctyp))
	   (assert_msg "invalid curctype" () curctyp)
	   ))
   )					;end foreach
  (add2sbuf_indentnl declbuf 0)
  (add2sbuf_strconst declbuf 
		     "/** end of declarations generated by generate_runtypesupport_boxingfun **/")
  (add2sbuf_indentnl declbuf 0)
  (add2sbuf_indentnl declbuf 0)
  (add2sbuf_indentnl codebuf 0)
  (add2sbuf_strconst codebuf 
		     "/** end of code generated by generate_runtypesupport_boxingfun **/")
  (add2sbuf_indentnl codebuf 0)
  (add2sbuf_indentnl codebuf 0)
  (debug "generate_runtypesupport_boxingfun end outarg=" outarg)
  )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; generate the inlined map functions for map of GTY-ed types.

(defun generate_runtypesupport_mapfun (ctytup outarg outbuf)
  (debug "generate_runtypesupport_mapfun start outarg=" outarg)
  (assert_msg "check ctytup is tuple" (is_multiple ctytup) ctytup)
  (assert_msg "check outbuf is buffer" (is_strbuf outbuf) outbuf)
  (strbuf_reserve outbuf 65000)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     "/** start of code generated by generate_runtypesupport_mapfun **/")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
;;;
  (foreach_in_multiple
   (ctytup)
   (curctyp :long tix)
   (debug "generate runtypesupport_mapfun curctyp=" curctyp)
   (assert_msg "check curctyp" (is_a curctyp class_ctype_gty) curctyp)
   (add2sbuf_strconst outbuf "/*gtyctype #")
   (add2sbuf_longdec outbuf (+i 1 tix))
   (add2sbuf_strconst outbuf " ")
   (add2sbuf_ccomstring outbuf (get_field :named_name curctyp))
   (add2sbuf_strconst outbuf "*/")
   (add2sbuf_indentnl outbuf 1)
   (match curctyp
	  (?(instance class_ctype_gty
		      :named_name          ?(some_string_value ?ctypnam)
		      :ctype_cname         ?(some_string_value ?cname)
		      :ctypg_mapstruct     ?(some_string_value ?mapstruct)
		      :ctypg_mapmagic      ?(some_string_value ?mapmagic)
		      :ctypg_mapdiscr      ?(and ?(instance class_discriminant 
							    :named_name ?mapdiscrname) ?mapdiscr)
		      :ctypg_mapunimemb    ?(some_string_value ?mapunimemb)
		      :ctypg_newmapfun     ?(some_string_value ?newmapfun)
		      :ctypg_mapgetfun     ?(some_string_value ?mapgetfun)
		      :ctypg_mapputfun     ?(some_string_value ?mapputfun)
		      :ctypg_mapremovefun  ?(some_string_value ?mapremovefun)
	   	      :ctypg_mapcountfun   ?(some_string_value ?mapcountfun)
	   	      :ctypg_mapsizefun	   ?(some_string_value ?mapsizefun)	 
	   	      :ctypg_mapnattfun	   ?(some_string_value ?mapnattfun)	 
	   	      :ctypg_mapnvalfun	   ?(some_string_value ?mapnvalfun)
		      :ctypg_mapauxdatafun ?(some_string_value ?mapauxdatafun)
		      :ctypg_mapauxputfun  ?(some_string_value ?mapauxputfun)
		      :ctypg_maphashfun    ?(some_string_value ?maphashfun)
		      )
	    (add2sbuf_indentnl outbuf 0)
	    (add2sbuf_strconst outbuf "/***map support for GTY ctype ")
	    (add2sbuf_string outbuf ctypnam)
	    (add2sbuf_strconst outbuf " **/")
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the new map function
	    (cond
	     ((is_string newmapfun)
	      (add2sbuf_strconst outbuf "static inline melt_ptr_t /*New map for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf newmapfun)
	      (add2sbuf_strconst outbuf " (meltobject_ptr_t discr, unsigned len) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_string outbuf 
			       #{$' /*generated map creation */
			       if (melt_magic_discr ((melt_ptr_t) discr) != MELTOBMAG_OBJECT)
			       return NULL ;
			       if (discr->meltobj_magic != }#)
			       (add2sbuf_string outbuf mapmagic)
			       (add2sbuf_string outbuf #{$') /* not map magic */
			       return NULL ;
			       return (melt_ptr_t) meltgc_raw_new_mappointers (discr, len) ;
			       } /*end generated new map for }#)
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf " */")
	      (add2sbuf_indentnl outbuf 0)
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/* no new map function */")
	      )
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map getter function
	    (cond
	     ((is_string mapgetfun)
	      (add2sbuf_strconst outbuf "static inline melt_ptr_t /* Map getter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapgetfun)
	      (add2sbuf_strconst outbuf " (melt_ptr_t map_p, ")
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf " attr) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_p || !attr ")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_p) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return NULL;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "return melt_raw_get_mappointers ((void*)map_p, (void*)attr);")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map getter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      (add2sbuf_indentnl outbuf 0)
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map getter function*/")
	      )
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map putter function
	    (cond
	     ((is_string mapputfun)
	      (add2sbuf_strconst outbuf "static inline void /* Map putter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapputfun)
	      (add2sbuf_strconst outbuf " (melt_ptr_t map_p, ")
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf " attr, melt_ptr_t valu_p) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_p || !attr || !valu_p")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_p) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "meltgc_raw_put_mappointers ((void*)map_p, (void*)attr, valu_p);")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map putter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map putter*/")
	      ))
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map remover function
	    (cond
	     ((is_string mapremovefun)
	      (add2sbuf_strconst outbuf "static inline void /* Map remover for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapremovefun)
	      (add2sbuf_strconst outbuf " (melt_ptr_t map_p, ")
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf " attr) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_p || !attr")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_p) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "meltgc_raw_remove_mappointers ((void*)map_p, (void*)attr);")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map remover for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      (void)
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map remover*/")
	      )
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map counter function
	    (cond
	     ((is_string mapcountfun)
	      (add2sbuf_strconst outbuf "static inline unsigned /* Map counter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapcountfun)
	      (add2sbuf_strconst outbuf " (struct ")
	      (add2sbuf_string outbuf mapstruct)
	      (add2sbuf_strconst outbuf "* map_s) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_s")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_s) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "return map_s->count;")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map counter for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      (void)
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map counter function*/")
	      )
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map size function
	    (cond
	     ((is_string mapsizefun)
	      (add2sbuf_strconst outbuf "static inline unsigned /* Map size for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapsizefun)
	      (add2sbuf_strconst outbuf " (struct ")
	      (add2sbuf_string outbuf mapstruct)
	      (add2sbuf_strconst outbuf "* map_s) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_s")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_s) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "return melt_primtab[map_s->lenix];")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map size for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      )
	     (:else 
	      (add2sbuf_strconst outbuf "/* no map size function */")
	      ))
	    (add2sbuf_indentnl outbuf 0)
;;;
;;;
;;; generate the map nth attr function
	    (cond 
	     ((is_string mapnattfun)
	      (add2sbuf_strconst outbuf "static inline ")
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf "/* Map nth attr for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapnattfun)
	      (add2sbuf_strconst outbuf " (struct ")
	      (add2sbuf_string outbuf mapstruct)
	      (add2sbuf_strconst outbuf "* map_s, int ix) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf " at = 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_s")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_s) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "at = map_s->entab[ix].e_at;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if ((void*) at == (void*) HTAB_DELETED_ENTRY) return 0;")
	      (add2sbuf_strconst outbuf "return at;")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map nth attr for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map nth attr function*/"))
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map nth value function
	    (cond 
	     ((is_string mapnvalfun)
	      (add2sbuf_strconst outbuf "static inline melt_ptr_t ")
	      (add2sbuf_strconst outbuf "/* Map nth value for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/ ")
	      (add2sbuf_string outbuf mapnvalfun)
	      (add2sbuf_strconst outbuf " (struct ")
	      (add2sbuf_string outbuf mapstruct)
	      (add2sbuf_strconst outbuf "* map_s, int ix) {")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_string outbuf cname)
	      (add2sbuf_strconst outbuf " at = 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if (!map_s")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf " || melt_magic_discr ((melt_ptr_t) map_s) != ")
	      (add2sbuf_string outbuf mapmagic)
	      (add2sbuf_strconst outbuf ")")
	      (add2sbuf_indentnl outbuf 4)
	      (add2sbuf_strconst outbuf "return 0;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "at = map_s->entab[ix].e_at;")
	      (add2sbuf_indentnl outbuf 2)
	      (add2sbuf_strconst outbuf "if ((void*) at == (void*) HTAB_DELETED_ENTRY) return 0;")
	      (add2sbuf_strconst outbuf "return map_s->entab[ix].e_va;")
	      (add2sbuf_indentnl outbuf 0)
	      (add2sbuf_strconst outbuf "} /*end generated map nth value for ")
	      (add2sbuf_string outbuf ctypnam)
	      (add2sbuf_strconst outbuf "*/")
	      )
	     (:else
	      (add2sbuf_strconst outbuf "/*no map nth value function*/"))
	     )
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map auxiliary data access function
	    (cond 
	     ((is_string mapauxdatafun)
	      (add2out 
	       outbuf
	       ##{ /* map auxiliary data access for $CTYPNAM */
	       static inline melt_ptr_t $MAPAUXDATAFUN (melt_ptr_t map_p)
	       {
	         if (melt_magic_discr (map_p) == $MAPMAGIC)
		   return ((struct $MAPSTRUCT*)map_p)->meltmap_aux;
		 return NULL;
	       }
	       }#)
	      )
	     (:else
	      (add2out outbuf ##{/*no map auxiliary data access function for $CTYPNAM*/}#)
	     ))
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map auxiliary data putter function
	    (cond 
	     ((is_string mapauxputfun)
	      (add2out 
	       outbuf
	       ##{ /* map auxiliary data put for $CTYPNAM */
	       static inline melt_ptr_t $MAPAUXPUTFUN (melt_ptr_t map_p, melt_ptr_t val_p)
	       {
	         if (melt_magic_discr (map_p) == $MAPMAGIC) {
		   ((struct $MAPSTRUCT*)map_p)->meltmap_aux = val_p;
		   meltgc_touch_dest (map_p, val_p);
                 }
		 return NULL;
	       }
	       }#)
	      )
	     (:else
	      (add2out outbuf ##{/*no map auxiliary data put function for $CTYPNAM*/}#)
	     ))
	    (add2sbuf_indentnl outbuf 0)
;;;
;;; generate the map hash function
	    (cond 
	     ((is_string maphashfun)
	      (add2out 
	       outbuf
	       ##{ /* map hash access for $CTYPNAM */
	       static inline unsigned $MAPHASHFUN (melt_ptr_t map_p)
	       {
	         if (melt_magic_discr (map_p) == $MAPMAGIC)
		   return ((struct $MAPSTRUCT*)map_p)->meltmap_hash;
		 return 0;
	       }
	       }#)
	      )
	     (:else
	      (add2out outbuf ##{/*no map hash function for $CTYPNAM*/}#)
	     ))
	    (add2sbuf_indentnl outbuf 0)
	      

;;;;;;;;;;
;;; trailer of map support
	    (add2sbuf_strconst outbuf "/***end of map support for GTY ctype ")
	    (add2sbuf_string outbuf ctypnam)
	    (add2sbuf_strconst outbuf " **/")
	    (add2sbuf_indentnl outbuf 1)
	    )
	  (?_
	   (add2sbuf_strconst outbuf "/*incomplete gtypctype*/")
	   )
	  ) ;; end match
   (add2sbuf_indentnl outbuf 1)
   )					;end foreach ctype
;;;
  (add2sbuf_strconst outbuf 
		     "/** end of code generated by generate_runtypesupport_mapfun **/")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (debug "generate_runtypesupport_mapfun end outarg=" outarg)
  )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; generate the predefined class lengths and field offsets 

(defun generate_runtypesupport_predef_fields (outbuf)
  (debug "generate_runtypesupport_predef_fields start outbuf=" outbuf)
  (assert_msg "check outbuf" (is_out outbuf) outbuf)
  (strbuf_reserve outbuf 27500)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     "/** start of code generated by generate_runtypesupport_predef_fields **/")
  (add2sbuf_indentnl outbuf 0)
  (let ( (curpredef ())
	 (:long nbpredef 0)
	 )
    (code_chunk getnbpredefchk #{/*$GETNBPREDEFCHK*/ $NBPREDEF= MELTGLOB__LASTWIRED ;}#
		)
    (foreach_long_upto
     (1 nbpredef)
     (:long prix)
     (code_chunk getpredefchk #{/*$GETPREDEFCHK*/ $CURPREDEF= melt_fetch_predefined($PRIX) ; }#
		 )
     (debug "generate_runtypesupport_predef_fields prix=" prix " curpredef=" curpredef)
     (if (is_a curpredef class_class)
	 (let ( (clasnam (get_field :named_name curpredef))
		(clasfields (get_field :class_fields curpredef))
		(:long nbfields (multiple_length clasfields))
		)
	   (add2sbuf_indentnl outbuf 0)
	   (add2out outbuf "#define MELT_HAS_PREDEF_" clasnam)
	   (add2sbuf_indentnl outbuf 0)
	   (add2out outbuf ##{/* predefined class $CLASNAM index $PRIX */ enum { }#)
	   (foreach_in_multiple
	    (clasfields)
	    (curfield :long fldix)
	    (assert_msg "check curfield" (is_a curfield class_field) curfield)
	    (if (== (get_field :fld_ownclass curfield) curpredef)
		(let ( (fldnam (get_field :named_name curfield))
		       (:long fldix (get_int curfield))
		       )
		  (add2sbuf_indentnl outbuf 1)
		  (add2out outbuf ##{ MELTFIELD_$FLDNAM = $FLDIX /*in $CLASNAM*/,}#)
		  ))
	    )
	   (add2sbuf_indentnl outbuf 1)
	   (add2out outbuf ##{  MELTLENGTH_$CLASNAM = $NBFIELDS } ;}#
		    )
	   ))))
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_strconst outbuf 
		     "/** end of code generated by generate_runtypesupport_predef_fields **/")
  (add2sbuf_indentnl outbuf 0)
  (add2sbuf_indentnl outbuf 0)
  (debug "generate_runtypesupport_predef_fields end outbuf=" outbuf)
  )
  



(defun generate_runtypesupport_predefined_hooks (outdeclbuf outcodebuf)
  (add2sbuf_indentnl outdeclbuf 0)
  (add2sbuf_indentnl outcodebuf 0)
  (let ( (curpredef ())
	 (:long nbpredef 0)
	 (parenv (parent_module_environment))
	 )
    (shortbacktrace_dbg "generate_runtypesupport_predefined_hooks" 10)
    (code_chunk getnbpredefchk #{/*$GETNBPREDEFCHK*/ $NBPREDEF= MELTGLOB__LASTWIRED ;}#
		)
    (add2out outdeclbuf ##{/* declarations generated by generate_runtypesupport_predefined_hooks for $NBPREDEF predefined */}#)
    (add2sbuf_indentnl outdeclbuf 0)
    (add2out outcodebuf ##{/*code generated by generate_runtypesupport_predefined_hooks for $NBPREDEF predefined */}#)
    (add2sbuf_indentnl outcodebuf 0)
    (foreach_long_upto
     (1 nbpredef)
     (:long prix)
     (code_chunk getpredefchk #{/*$GETPREDEFCHK*/ $CURPREDEF= melt_fetch_predefined($PRIX) ; }#
		 )
     (if (is_hook curpredef)
	 (let ( (hkname (hook_name curpredef))
		(hksymb (get_symbolstr hkname))
		(hkbind (find_env parenv hksymb))
		(hkdata (hook_data curpredef))
		)
	   (add2sbuf_indentnl outcodebuf 0)
	   (add2sbuf_indentnl outdeclbuf 0)
	   (debug "generate_runtypesupport_predefined_hooks prix#" prix " curpredef=" curpredef 
		  "\n hkname=" hkname "\n.. hksymb=" hksymb "\n.. hkbind=" hkbind "\n.. hkdata=" hkdata)
	   (add2out outdeclbuf ##{/*predefined hook declaration $HKNAME #$PRIX*/}#)
	   (add2sbuf_indentnl outdeclbuf 0)
	   (add2out outcodebuf ##{/*predefined hook definition $HKNAME #$PRIX*/}#)
	   (add2sbuf_indentnl outcodebuf 0)
	   (if (is_a hkdata class_hook_descriptor)
	       (let ( (dname (get_field :named_name hkdata))
		      (dins (get_field :hookdesc_in_formals hkdata))
		      (douts (get_field :hookdesc_out_formals hkdata))
		      (dctype (get_field :hookdesc_ctype hkdata))
		      (ctynam (get_field :ctype_cname dctype))
		      (:long nbparam 0)
		      )
		 (add2out outdeclbuf "MELT_EXTERN " ctynam " melthookproc_" hkname "(")
		 (add2out outcodebuf  ctynam " melthookproc_" hkname "(")
		 (foreach_in_multiple 
		  (dins)
		  (curinsb :long inix)
		  (debug "generate_runtypesupport_predefined_hooks curinsb=" curinsb " inix#" inix)
		  (when (>i nbparam 0) 
		    (add2out outdeclbuf ", ")
		    (add2out outcodebuf ", "))
		  (setq nbparam (+i nbparam 1))
		  (assert_msg "check curinsb" (is_a curinsb class_formal_binding) curinsb)
		  (add2out outdeclbuf (get_field :ctype_cname (get_field :fbind_type curinsb)))
		  (add2out outdeclbuf " meltin_" (get_field :named_name (get_field :binder curinsb)) "_p" inix)
		  (add2out outcodebuf (get_field :ctype_cname (get_field :fbind_type curinsb)))
		  (add2out outcodebuf " meltin_" (get_field :named_name (get_field :binder curinsb)) "_p" inix)
		  )
		 (foreach_in_multiple 
		  (douts)
		  (curoutb :long outix)
		  (debug "generate_runtypesupport_predefined_hooks curoutb=" curoutb " outix#" outix)
		  (when (>i nbparam 0) 
		    (add2out outdeclbuf ", ")
		    (add2out outcodebuf ", "))
		  (setq nbparam (+i nbparam 1))
		  (add2out outdeclbuf 
			   (get_field :ctype_cname (get_field :fbind_type curoutb))
			   "* meltout_" (get_field :named_name (get_field :binder curoutb)) "_o" outix)
		  (add2out outcodebuf (get_field :ctype_cname (get_field :fbind_type curoutb)))
		  (add2out outcodebuf "* meltout_" (get_field :named_name (get_field :binder curoutb)) "_o" outix)
		  )
		 (add2out outdeclbuf ");")
		 (add2out outcodebuf ")")
		 (add2sbuf_indentnl outdeclbuf 0)
		 (add2sbuf_indentnl outcodebuf 0)
		 (add2out 
		  outcodebuf 
		  "{ /* code emitted by generate_runtypesupport_predefined_hooks for " hkname "*/")
		 (add2sbuf_indentnl outcodebuf 0)
		 (add2out outcodebuf "#ifndef MELT_SKIP_HOOK_PREDEFINED_CODE")
		 (add2sbuf_indentnl outcodebuf 1)
		 (add2out outcodebuf 
			  "  melthook_ptr_t melthkp = (melthook_ptr_t) MELT_PREDEF (" hkname ");")
		 (add2sbuf_indentnl outcodebuf 1)
		 (add2out outcodebuf "typedef " ctynam " melthooksig_" hkname "_t (")
		 (add2out outcodebuf "melt_ptr_t")
		 (foreach_in_multiple 
		  (dins)
		  (curinsb :long inix)
		  (add2out outcodebuf ", " (get_field :ctype_cname (get_field :fbind_type curinsb)))
		  )
		 (foreach_in_multiple 
		  (douts)
		  (curoutb :long outix)
		  (add2out outcodebuf ", "  (get_field :ctype_cname (get_field :fbind_type curoutb)) "* ")
		  )
		 (add2out outcodebuf ");")
                 (add2sbuf_indentnl outcodebuf 1)
                 (add2out outcodebuf "/*checkhook exist*/ melt_assertmsg (\" check predefhook " hkname " exists\", ")
                 (add2sbuf_indentnl outcodebuf 15)
                 (add2out outcodebuf "             melthkp != NULL);")
                 (add2sbuf_indentnl outcodebuf 1)
                 (add2out outcodebuf "/*checkhook ishook*/ melt_assertmsg (\" check predefhook " hkname " is hook\", ")
                 (add2sbuf_indentnl outcodebuf 15)
                 (add2out outcodebuf "             melt_magic_discr ((melt_ptr_t) melthkp) == MELTOBMAG_HOOK);")
                 (add2sbuf_indentnl outcodebuf 1)
                 (add2out
                  outcodebuf 
##{ /* check hook routine $HKNAME */
                  melthooksig_$HKNAME#_t *melthookptr_$HKNAME = 
		     reinterpret_cast <melthooksig_$HKNAME#_t*>
		        (melthkp->hookad) ;
                  melt_assertmsg("check predefhook routine $HKNAME", 
                                 melthookptr_$HKNAME);
}#)
                 (add2sbuf_indentnl outcodebuf 1)
                 (add2out
                  outcodebuf ##{ /* check predefhook $HKNAME name */
		  melt_assertmsg("check predefhook name $HKNAME",
				 !strncmp (melthkp->hookname,
					   "$HKNAME", MELT_HOOKNAME_LEN-1));}#
		  )
                 (add2sbuf_indentnl outcodebuf 1)
                  (add2out
                  outcodebuf ##{return (*melthookptr_$HKNAME) ((melt_ptr_t)melthkp}#
                                                            )
		  (foreach_in_multiple 
		   (dins)
		   (curinsb :long inix)
		   (add2out outcodebuf ",")
		   (add2sbuf_indentnl outcodebuf 20)
		   (add2out outcodebuf " meltin_" (get_field :named_name (get_field :binder curinsb)) "_p" inix)
		   )
		  (foreach_in_multiple
		   (douts)
		   (curoutb :long outix)
		   (add2out outcodebuf ",") 
		   (add2sbuf_indentnl outcodebuf 20)
		   (add2out outcodebuf " meltout_" (get_field :named_name (get_field :binder curoutb)) "_o" outix)
		   )
		  (add2out outcodebuf ");")
		  (add2sbuf_indentnl outcodebuf 0)
		  (add2out outcodebuf "#endif /* MELT_SKIP_HOOK_PREDEFINED_CODE */")
		  (add2sbuf_indentnl outcodebuf 0)
		  (add2out outcodebuf "} /* end  melthookproc_" hkname " */")
		  (add2sbuf_indentnl outcodebuf 0)
		  (add2sbuf_indentnl outcodebuf 0)
		  )
	     ;; else, not a hook data
	     (progn
	       (assert_msg "check hkdata is empty " (is_not_a hkdata class_hook_descriptor) hkdata)
	       (add2out outcodebuf "#warning hook " hkname " without data")
	       (add2out_indentnl outcodebuf 1)
	       )
	     ))
	   )
	   (add2sbuf_indentnl outcodebuf 0)
       )
     ;;
     (add2out outdeclbuf
	      ##{/* end of declarations generated by generate_runtypesupport_predefined_hooks for $NBPREDEF predefined */}#)
     (add2sbuf_indentnl outdeclbuf 0)
     (add2out outcodebuf 
	      ##{/* end of code generated by generate_runtypesupport_predefined_hooks for $NBPREDEF predefined */}#)
     (add2sbuf_indentnl outcodebuf 0)
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun runtypesupport_docmd (cmd moduldata)
  (debug "start runtypesupport_docmd cmd=" cmd " moduldata=" moduldata)
  (let (
	(outarg (or (make_stringconst discr_string (melt_argument "output"))
		    '"meltrunsup"
		    ))
	(outdeclname (let ( (nambuf (make_strbuf discr_strbuf))
			    )
		       (add2sbuf_string nambuf outarg)
		       (add2sbuf_strconst nambuf ".h")
		       (strbuf2string discr_string nambuf)
		       ))
	(outcodename  (let ( (nambuf (make_strbuf discr_strbuf))
			     )
			(add2sbuf_string nambuf outarg)
			(add2sbuf_strconst nambuf "-inc.cc")
			(strbuf2string discr_string nambuf)
			))
	(dictypgty (retrieve_dictionnary_ctype_gty))
	(rawctypgtylist (list))
	(dictyp (retrieve_dictionnary_ctype))
	(rawctyplist (list))
	(outdeclbuf (make_strbuf discr_strbuf))
	(outcodebuf (make_strbuf discr_strbuf))
	)
    (debug "runtypesupport_docmd dictypgty=" dictypgty
	   " outarg=" outarg)
    (generate_gplv3plus_copyright_notice_c_comment outdeclbuf outdeclname)
    (generate_gplv3plus_copyright_notice_c_comment outcodebuf outcodename)
    (foreach_in_mapstring
     (dictypgty)
     (curstr curcty)
     (list_append rawctypgtylist curcty)
     )
    (debug "runtypesupport_docmd rawctypgtylist=" rawctypgtylist)
    (foreach_in_mapstring
     (dictyp)
     (curstr curcty)
     (list_append rawctyplist curcty)
     )
    (debug "runtypesupport_docmd rawctyplist=" rawctyplist)
    (let ( (sortedctygtytuple 
	    (multiple_sort (list_to_multiple rawctypgtylist discr_multiple)
			   compare_named_alpha discr_multiple)) 
	   (sortedctytuple 
	    (multiple_sort (list_to_multiple rawctyplist discr_multiple)
			   compare_named_alpha discr_multiple))
	   (rawvaldesctuple  
	    (list_to_multiple (retrieve_value_descriptor_list)))
	   (sortedvaldesctuple
	    (multiple_sort rawvaldesctuple
			   compare_named_alpha discr_multiple))
	   )
      (debug "runtypesupport_docmd sortedctygtytuple=" sortedctygtytuple 
	     " sortedvaldesctuple=" sortedvaldesctuple) 
      ;;
      ;; generate the enummagic ie the MELTOBMAG_* enumeration
      (generate_runtypesupport_enum_objmagic sortedctygtytuple 
					     sortedvaldesctuple
					     outarg outdeclbuf)
      ;; generate the structures
      (generate_runtypesupport_gty sortedctygtytuple sortedvaldesctuple
				   outarg outdeclbuf) 
      ;; generate the parameters support for every ctype, even the non
      ;; GTY-ed ones!
      (generate_runtypesupport_param sortedctytuple sortedvaldesctuple
				     outarg outdeclbuf) 
      ;; generate the melt_code_to_ctype support function
      (generate_runtypesupport_cod2ctype sortedctytuple sortedvaldesctuple
					 outarg outcodebuf) 
      (add2sbuf_indentnl outdeclbuf 0)
      (add2sbuf_strconst outdeclbuf "melt_ptr_t melt_code_to_ctype (int);")
      (add2sbuf_indentnl outdeclbuf 0)
      ;; generate the magic to string function
      (generate_runtypesupport_mag2str sortedctytuple sortedvaldesctuple
				       outarg outdeclbuf outcodebuf) 
      ;; generate the melt_forwarded_copy funtion
      (generate_runtypesupport_forwcopy_fun sortedctygtytuple 
					    sortedvaldesctuple
					    outarg outcodebuf)
      ;; generate the melt_scanning function
      (generate_runtypesupport_scanning sortedctygtytuple
					sortedvaldesctuple
					outarg outcodebuf)
      ;; generate the melt box related functions
      (generate_runtypesupport_boxingfun sortedctygtytuple outarg
					 outdeclbuf outcodebuf) 
      ;; generate the meltgc_clone_with_discriminant funtion
      (generate_runtypesupport_cloning_fun sortedctygtytuple 
					   sortedvaldesctuple
					   outarg outcodebuf)
      ;; generate the inlined melt map functions
      (generate_runtypesupport_mapfun sortedctygtytuple outarg outdeclbuf)
      ;; generate the predefined fields
      (generate_runtypesupport_predef_fields outdeclbuf)
      ;; generate the predefined hooks
      (generate_runtypesupport_predefined_hooks outdeclbuf outcodebuf)
      ;;;;;;;;;;;;;;;;;;
      ;; add a terminating comment and write the files
      ;;
      ;;; generated declaration
      (code_chunk termcommentdeclchk 
		  #{ /* runtypesupport_docmd $TERMCOMMENTDECLCHK */ {
		  time_t nowdecl = 0;
		  char decldatebuf[48];
		  memset (decldatebuf, 0, sizeof(decldatebuf));
		  time(&nowdecl);
		  strftime (decldatebuf, sizeof(decldatebuf)-1,
			    "%Y %b %d", localtime(&nowdecl));
		  meltgc_out_printf ((melt_ptr_t) $OUTDECLBUF,
				     "\n/*** End of declaration file %s generated on %s\n"
				     " * by GCC MELT %s . ***/\n",
				     melt_string_str((melt_ptr_t) $OUTDECLNAME),
				     decldatebuf, melt_gccversionstr);
		  } /* end  $TERMCOMMENTDECLCHK runtypesupport_docmd */}#)
      (output_sbuf_strval outdeclbuf outdeclname)
      (inform_at () "generated runtype support declaration file $1"
		 outdeclname)
      ;;; generated code
      (code_chunk 
       termcommentcodechk 
       #{ /* runtypesupport_docmd $TERMCOMMENTCODECHK */ {
       time_t nowcode = 0;
       char codedatebuf[48];
       memset (codedatebuf, 0, sizeof(codedatebuf));
       time(&nowcode);
       strftime (codedatebuf, sizeof(codedatebuf)-1,
			      "%Y %b %d", localtime(&nowcode));
       meltgc_out_printf ((melt_ptr_t) $OUTCODEBUF,
			  "\n/*** End of code file %s generated on %s\n"
			  " * by GCC MELT %s . ***/\n",
			  melt_string_str((melt_ptr_t) $OUTCODENAME),
			  codedatebuf, melt_gccversionstr);
       } /* end  $TERMCOMMENTCODECHK runtypesupport_docmd*/}#)
      (output_sbuf_strval outcodebuf outcodename)
      (inform_at () "generated runtype support implementation file $1"
		 outcodename)
      ;;
      (debug "runtypesupport_docmd done outarg=" outarg)
      (return :true)			;mode success
      )))

;;;;;;;;;;;;;;;;
(definstance runtypesupport_mode
  class_melt_mode
  :meltmode_help '"generate runtime support for our GTY types (ctypes, values);\n
\t OUTPUT=generated file prefix"
  :meltmode_fun runtypesupport_docmd
)
(install_melt_mode runtypesupport_mode "runtypesupport")


;;;;;;;;;;;;;;;;

;; utility function to translate something to various flavors of modules
(defun translate_to_flavored_module (inarg outarg carg flavors curenv)
  (debug "translate_to_flavored_module inarg=" inarg 
	 " outarg=" outarg " carg=" carg " flavors=" flavors " curenv=" curenv)
  (let ( 
	(basnam
	 (cond
	  ( (is_string outarg)
	    (make_string_without_suffix discr_string outarg ".cc"))
	  ( (is_string inarg) 
	    (make_string_nakedbasename discr_string inarg))
	  (:else 
	   (error_at () "invalid translate to flavored module arguments")
	   (return)))
	 )
	(outnam (or outarg basnam))
	(rawsrcnam (or (make_string_without_suffix discr_string carg ".cc")
		       (make_string_nakedbasename discr_string outnam)))
	(outbase (make_string_without_suffix discr_string outnam  ".cc"))
	)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (debug "translate_to_flavored_module basnam=" basnam 
	   " rawsrcnam=" rawsrcnam " outbase=" outbase)
    (translate_to_cc_module_melt_sources inarg outbase curenv)
    (cond 
     ( (is_string flavors)
       (debug "before generate_flavored_melt_module rawsrcnam=" rawsrcnam " outnam=" outnam
	      "flavor=" flavors)
       (generate_flavored_melt_module rawsrcnam outnam flavors))
     ( (is_list flavors)
       (foreach_pair_component_in_list
	(flavors)
	(curpair curflavor)
	(debug "before generate_flavored_melt_module rawsrcnam=" rawsrcnam " outnam=" outnam
	       "flavor=" curflavor)
	(generate_flavored_melt_module rawsrcnam outnam curflavor)))
     ( (is_multiple flavors)
       (foreach_in_multiple
	(flavors)
	(curflavor :long flix)
	(debug "before generate_flavored_melt_module rawsrcnam=" rawsrcnam " outnam=" outnam
	       "flavor=" curflavor " flix=" flix)
	(generate_flavored_melt_module rawsrcnam outnam curflavor)))
     (:else
      (debug "translate_to_flavored_module bad flavors=" flavors)
      (assert_msg "invalid flavors" () flavors)
      )
     )))

(defun translatetomodule_docmd (cmd moduldata)
  (message_dbg "starting translatetomodule_docmd")
  (debug "start translatetomodule_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	(coutarg (make_stringconst discr_string (melt_argument "coutput")))
	)
    (debug "translatetomodule_docmd inarg=" inarg " outarg=" outarg
	   " coutarg=" coutarg " curenv=" curenv)
    (translate_to_flavored_module inarg outarg coutarg
				  (tuple '"quicklybuilt" '"optimized" '"debugnoline")
				  curenv) 
    (return :true)			;succeed the mode
    ))

(definstance translatetomodule_mode
  class_melt_mode
  :meltmode_help '"translate a .melt file to MELT_DYNLOADED_SUFFIX modules in quicklybuilt, optimized, debugnoline flavors;\n
\t ARGUMENT= input file; OUTPUT= generated module with MELT_DYNLOADED_SUFFIX; OUTPUTC= generated *.cc file."
  :meltmode_fun translatetomodule_docmd
)
(install_melt_mode translatetomodule_mode "translatetomodule")

;;;;;;;;;;;;;;;;

(defun translatedebug_docmd (cmd moduldata)
  (debug "start translatedebug_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	(coutarg (make_stringconst discr_string (melt_argument "coutput")))
	)
    (debug "translatedebug_docmd inarg=" inarg " outarg=" outarg
	   " coutarg=" coutarg " curenv=" curenv)
    (translate_to_flavored_module inarg outarg coutarg '"debugnoline" curenv))
  (return :true)			;succeed the mode
  )

(definstance translatedebug_mode
  class_melt_mode
  :meltmode_help '"translate a .melt file to .debugnoline module for low-level
debug, including gdb;\n
\t ARGUMENT= input file; OUTPUT= generated module with MELT_DYNLOADED_SUFFIX; COUTPUT= generated *.cc file\n
\t generates also *.cc and no MELT line number;\n
\t Useful for running gdb on the module. MELT debugging (DEBUG,
ASSERT_MSG) is enabled"
  :meltmode_fun translatedebug_docmd
)
(install_melt_mode translatedebug_mode "translatedebug")

;;;;;;;;;;;;;;;;
(defun translatequickly_docmd (cmd moduldata)
  (debug "start translatequickly_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	(coutarg (make_stringconst discr_string (melt_argument "coutput")))
	)
    (debug "translatequickly_docmd inarg=" inarg " outarg=" outarg
	   " coutarg=" coutarg " curenv=" curenv)
    (translate_to_flavored_module inarg outarg coutarg '"quicklybuilt" curenv)
    (return :true) ;;; succeed the mode
    ))



(definstance translatequickly_mode
  class_melt_mode
  :meltmode_help '"translate quickly a .melt file to .quicklybuilt module with MELT debugging enabled (DEBUG, ASSERT_MSG...);\n
\t ARGUMENT= input file; OUTPUT= generated module with MELT_DYNLOADED_SUFFIX; COUTPUT= generated *.cc file\n."
  :meltmode_fun translatequickly_docmd
)
(install_melt_mode translatequickly_mode "translatequickly")


;;;;;;;;;;;;;;;;
(defun translateoptimized_docmd (cmd moduldata)
  (debug "start translateoptimized_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	(coutarg (make_stringconst discr_string (melt_argument "coutput")))
	)
    (debug "translateoptimized_docmd inarg=" inarg " outarg=" outarg
	   " coutarg=" coutarg " curenv=" curenv)
    (translate_to_flavored_module inarg outarg coutarg '"optimized" curenv)
    (return :true) ;;succeed the mode
    ))



(definstance translateoptimized_mode
  class_melt_mode
  :meltmode_help '"translate  a .melt file to an .optimized module without MELT debugging\n
\t ARGUMENT= input file; OUTPUT= generated module with MELT_DYNLOADED_SUFFIX; COUTPUT= generated *.cc file\n."
  :meltmode_fun translateoptimized_docmd
)
(install_melt_mode translateoptimized_mode "translateoptimized")


;;;;;;;;;;;;;;;;
;;;;;
(defun translatefile_docmd (cmd moduldata)
  (message_dbg "starting translatefile_docmd")
  (debug "start translatefile_docmd cmd=" cmd " moduldata=" moduldata)
  (let ( 
	(parmodenv (parent_module_environment))
	(curenv (if moduldata moduldata initial_environment))
	(inarg (make_stringconst discr_string (melt_argument "arg")))
	(outarg (make_stringconst discr_string (melt_argument "output")))
	)
    (debug "translatefile_docmd inarg=" inarg " outarg=" outarg 
	   " parmodenv=" parmodenv 
	   " initial_environment=" initial_environment)
    (assert_msg "check curenv" (is_a curenv class_environment) curenv)
    (let ( (basnam (cond
		    ( (is_string outarg) 
		      (make_string_without_suffix discr_string outarg ".cc")
		      )
		    ( (is_string inarg)
		      (if (not (string_suffixed inarg ".melt"))
			  (warningmsg_strv "MELT translated input file without .melt suffix" 
					   inarg))
		      (make_string_nakedbasename discr_string inarg))
		    (:else
		     (error_at () "invalid translatefile mode, arg $1 output $2"
			       inarg outarg)
		     (return)
		     )))
	   )
    (debug "translatefile_mode basnam" basnam)
    (if (or (string_dynloaded_suffixed basnam)
	    (string_suffixed basnam ".melt")
	    (string_suffixed basnam ".o")
	    (string_suffixed basnam ".c")
	    (string_suffixed basnam ".cc"))
	(progn 	  
	  (error_at () "tranlatefile mode needs a base name $1 without suffix" 
		       basnam)
	  (return)			;fail the mode
	  ))
      (translate_to_cc_module_melt_sources inarg basnam curenv)
      (return :true)			;succeed the mode
      )))


(definstance translatefile_mode
  class_melt_mode
  :meltmode_help '"translate a .melt file to .cc;\n
\t ARGUMENT= input file; OUTPUT= generated C++ file"
  :meltmode_fun translatefile_docmd
)
(install_melt_mode translatefile_mode "translatefile")



;;;;;;;;;;;;;;;;
(defun translateinit_docmd (cmd moduldata)
  (debug "start translateinit_mode cmd=" cmd " moduldata=" moduldata
	 " initial_environment=" initial_environment)
  (let ( (rlist (make_list discr_list))
	 (:cstring progarg (melt_argument "arg"))
	 (:cstring progarglist (melt_argument "arglist"))
	 (inarg
	  (cond ( progarg 
		  (let ( (progargstr (make_stringconst discr_string progarg))
			 )
		    (if (not (string_suffixed progargstr ".melt"))
			(warningmsg_strv "MELT translated initial file without .melt suffix" 
					 progargstr))
		    progargstr))
		( progarglist
		  (split_string_comma discr_string (make_stringconst discr_string progarglist))
		  )
		(:else
		 (error_at () "invalid arg $1 or arglist $2 to translateinit mode" progarg progarglist)
		 (return))))
	 (outarg (make_stringconst discr_string (melt_argument "output")))
	 (basnam (cond
		  ( (is_string outarg) outarg)
		  ( (is_string inarg) (make_string_nakedbasename discr_string inarg))
		  (:else
		   (error_at () "invalid translateinit mode inarg $1 outarg $2" inarg outarg)
		   (return)
		   )))
	 )
    (debug "translateinit_mode basnam=" basnam)
    (if (or (string_dynloaded_suffixed basnam)
	    (string_suffixed basnam ".melt")
	    (string_suffixed basnam ".c")
	    (string_suffixed basnam ".cc"))
	(progn 
	  (error_at () "tranlateinit mode needs a base name $1 without suffix" 
			 basnam)
	  (return)
	  ))
    (cond 
     ((is_string inarg) 
      (list_append2list rlist (read_file inarg)))
     ((is_list inarg)
      (list_every inarg 
		  (lambda (curarg)
		    (inform_at () "translateinit reading from file $1" curarg)
		    (let ( (curead (read_file curarg))
			   )
		      (assert_msg "check rlist" (is_list rlist) rlist)
		      (assert_msg "check curead" (is_list_or_null curead) curead)
		      (debug "translateinit_mode curead=" curead)
		      (list_append2list rlist curead)))))
     )
    (debug "after read translateinit_mode rlist=" rlist)
    ;; special magic for the initial first file
    (compile_first_bootstrap_list_sexpr rlist basnam)
    ;; we trigger explicitly a full GC to stress the runtime. The
    ;; translateinit mode is not useful to the casual user.
    (full_garbcoll 10000)
    (return :true)			;mode succeed
    ))

;;;
(definstance translateinit_mode
  class_melt_mode
  :meltmode_help '"translate the very first (warmelt-first.melt) file for bootstrapping;\n
\t useful only at MELT installation! ARGUMENT= input file; OUTPUT= generated file."
  :meltmode_fun translateinit_docmd
)
(install_melt_mode translateinit_mode "translateinit")



;;;;;;;;;;;;;;;;

(defun translateinit_flavored (flavor)
  (let ( (rlist (make_list discr_list))
	 (:cstring progarg (melt_argument "arg"))
	 (:cstring progarglist (melt_argument "arglist"))
	 (inarg
	  (cond ( progarg 
		  (let ( (progargstr (make_stringconst discr_string progarg))
			 )
		    (if (not (string_suffixed progargstr ".melt"))
			(warningmsg_strv "MELT translated initial file without .melt suffix" 
					 progargstr))
		    progargstr))
		( progarglist
		  (split_string_comma discr_string (make_stringconst discr_string progarglist))
		  )
		(:else
		 (error_at ()
			   "invalid arg $1 or arglist $2 to translateinitmodule mode"
			   progarg progarglist)
		 (return))))
	 (outarg (make_stringconst discr_string (melt_argument "output")))
	 (basnam (cond
		  ( (is_string outarg) outarg)
		  ( (is_string inarg) (make_string_nakedbasename discr_string inarg))
		  (:else
		   (error_at () "invalid translateinitmodule mode arg $1 output $2" inarg outarg)
		   (return)
		   )))
	 )
    (debug "translateinitmodule_flavored basnam=" basnam " flavor=" flavor)
    (if (or (string_dynloaded_suffixed basnam)
	    (string_suffixed basnam ".melt")
	    (string_suffixed basnam ".c")
	    (string_suffixed basnam ".cc"))
	(progn 
	  (error_at () "tranlateinitmodule_flavored mode needs a base name $1 without suffix" 
			 basnam)
	  (return)
	  ))
    (cond 
     ((is_string inarg) 
      (list_append2list rlist (read_file inarg)))
     ((is_list inarg)
      (list_every inarg 
		  (lambda (curarg)
		    (inform_at ()
			       "tranlateinitmodule_flavored reading from file $1"
			       curarg)
		    (let ( (curead (read_file curarg))
			   )
		      (assert_msg "check rlist" (is_list rlist) rlist)
		      (assert_msg "check curead" (is_list_or_null curead) curead)
		      (debug "translateinitmodule_flavored curead=" curead)
		      (list_append2list rlist curead)))))
     )
    (debug "after read translateinitmodule_flavored rlist=" rlist)
    ;; we pass a null initial environment, so that
    ;; MELT_HAS_INITIAL_ENVIRONMENT is emitted as 0
    (compile_list_sexpr rlist () basnam)
    ;; we trigger explicitly a full GC to stress the runtime. The
    ;; translateinitmodule mode is not useful to the casual user.
    (full_garbcoll 10000)
    (let ( 
	  (outbase (or outarg basnam))
	  )
      (cond
       ((is_multiple flavor)
	(foreach_in_multiple 
	 (flavor)
	 (curflavor :long flavix)
	 (debug "translateinitmodule_flavored before module generation basnam=" basnam 
		" outbase=" outbase " curflavor=" curflavor)
	 (generate_flavored_melt_module basnam outbase curflavor)
	 (debug "translateinitmodule_flavored after module generation basnam=" basnam 
		" outbase=" outbase " curflavor=" curflavor)
	 )
	)
       ((is_string flavor)
	(debug "translateinitmodule_flavored before module generation basnam=" basnam 
	       " outbase=" outbase " flavor=" flavor)
	(generate_flavored_melt_module basnam outbase flavor)
	(debug "translateinitmodule_flavored after module generation basnam=" basnam 
	       " outbase=" outbase " flavor=" flavor)
	)
       (:else
	(debug "translateinitmodule_flavored bad flavor=" flavor)
	(error_at () "bad flavor $1 for translating initial module" flavor)
	(assert_msg "translateinitmodule_flavored bad flavor" () flavor))
       )
      )
    )
  )

;; translate the initial module in all 3 flavors: 
(defun translateinitmodule_docmd (cmd moduldata)
  (debug "start translateinitmodule_mode cmd=" cmd " moduldata=" moduldata
	 " initial_environment=" initial_environment)
  (translateinit_flavored (tuple '"quicklybuilt" '"debugnoline" '"optimized"))
  )


;; three modes for each flavor of init module
;;;
(definstance translateinitmodule_mode
  class_melt_mode
  :meltmode_help '"translate the very first *.melt file into quicklybuilt, optimized, debugnoline falvors of modules;\n
\t useful only at MELT installation! ARGUMENT= input file; OUTPUT= generated module base."
  :meltmode_fun translateinitmodule_docmd
)
(install_melt_mode translateinitmodule_mode "translateinitmodule")


;; translate the initial module quicklybuilt: 
(defun translateinitmodulequicklybuilt_docmd (cmd moduldata)
  (debug "start translateinitmodulequicklybuild_mode cmd=" cmd " moduldata=" moduldata
	 " initial_environment=" initial_environment)
  (translateinit_flavored '"quicklybuilt")
  )

;;;
(definstance translateinitmodulequicklybuilt_mode
  class_melt_mode
  :meltmode_help '"translate the very first *.melt file into quicklybuilt module;\n
\t useful only at MELT installation! ARGUMENT= input file; OUTPUT= generated module base."
  :meltmode_fun translateinitmodulequicklybuilt_docmd
)
(install_melt_mode translateinitmodulequicklybuilt_mode "translateinitmodulequicklybuilt")


;; translate the initial module debugnoline: 
(defun translateinitmoduledebugnoline_docmd (cmd moduldata)
  (debug "start translateinitmodulequicklybuild_mode cmd=" cmd " moduldata=" moduldata
	 " initial_environment=" initial_environment)
  (translateinit_flavored '"debugnoline")
  )

;;;
(definstance translateinitmoduledebugnoline_mode
  class_melt_mode
  :meltmode_help '"translate the very first *.melt file into debugnoline module;\n
\t useful only at MELT installation! ARGUMENT= input file; OUTPUT= generated module base."
  :meltmode_fun translateinitmoduledebugnoline_docmd
)
(install_melt_mode translateinitmoduledebugnoline_mode "translateinitmoduledebugnoline")


;; translate the initial module optimized: 
(defun translateinitmoduleoptimized_docmd (cmd moduldata)
  (debug "start translateinitmodulequicklybuild_mode cmd=" cmd " moduldata=" moduldata
	 " initial_environment=" initial_environment)
  (translateinit_flavored '"optimized")
  )

;;;
(definstance translateinitmoduleoptimized_mode
  class_melt_mode
  :meltmode_help '"translate the very first *.melt file into optimized module;\n
\t useful only at MELT installation! ARGUMENT= input file; OUTPUT= generated module base."
  :meltmode_fun translateinitmoduleoptimized_docmd
)
(install_melt_mode translateinitmoduleoptimized_mode "translateinitmoduleoptimized")





;;;;
;; eof warmelt-modes.melt