aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/yasm/modules/preprocs/nasm/nasm-pp.c
blob: 27a8cc6c9358bdc8086b7f8c962b2301b9ffe80f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
/* -*- mode: c; c-file-style: "bsd" -*- */
/* preproc.c   macro preprocessor for the Netwide Assembler
 *
 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
 * Julian Hall. All rights reserved. The software is
 * redistributable under the licence given in the file "Licence"
 * distributed in the NASM archive.
 *
 * initial version 18/iii/97 by Simon Tatham
 */

/* Typical flow of text through preproc
 *
 * pp_getline gets tokenised lines, either
 *
 *   from a macro expansion
 *
 * or
 *   {
 *   read_line  gets raw text from stdmacpos, or predef, or current input file
 *   tokenise   converts to tokens
 *   }
 *
 * expand_mmac_params is used to expand %1 etc., unless a macro is being
 * defined or a false conditional is being processed
 * (%0, %1, %+1, %-1, %%foo
 *
 * do_directive checks for directives
 *
 * expand_smacro is used to expand single line macros
 *
 * expand_mmacro is used to expand multi-line macros
 *
 * detoken is used to convert the line back to text
 */
#include <util.h>
#include <libyasm-stdint.h>
#include <libyasm/coretype.h>
#include <libyasm/intnum.h>
#include <libyasm/expr.h>
#include <libyasm/file.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>

#include "nasm.h"
#include "nasmlib.h"
#include "nasm-pp.h"

typedef struct SMacro SMacro;
typedef struct MMacro MMacro;
typedef struct Context Context;
typedef struct Token Token;
typedef struct Blocks Blocks;
typedef struct Line Line;
typedef struct Include Include;
typedef struct Cond Cond;

/*
 * Store the definition of a single-line macro.
 */
struct SMacro
{
    SMacro *next;
    char *name;
    int level;
    int casesense;
    int nparam;
    int in_progress;
    Token *expansion;
};

/*
 * Store the definition of a multi-line macro. This is also used to
 * store the interiors of `%rep...%endrep' blocks, which are
 * effectively self-re-invoking multi-line macros which simply
 * don't have a name or bother to appear in the hash tables. %rep
 * blocks are signified by having a NULL `name' field.
 *
 * In a MMacro describing a `%rep' block, the `in_progress' field
 * isn't merely boolean, but gives the number of repeats left to
 * run.
 *
 * The `next' field is used for storing MMacros in hash tables; the
 * `next_active' field is for stacking them on istk entries.
 *
 * When a MMacro is being expanded, `params', `iline', `nparam',
 * `paramlen', `rotate' and `unique' are local to the invocation.
 */
struct MMacro
{
    MMacro *next;
    char *name;
    int casesense;
    long nparam_min, nparam_max;
    int plus;                   /* is the last parameter greedy? */
    int nolist;                 /* is this macro listing-inhibited? */
    int in_progress;
    Token *dlist;               /* All defaults as one list */
    Token **defaults;           /* Parameter default pointers */
    int ndefs;                  /* number of default parameters */
    Line *expansion;

    MMacro *next_active;
    MMacro *rep_nest;           /* used for nesting %rep */
    Token **params;             /* actual parameters */
    Token *iline;               /* invocation line */
    long nparam, rotate, *paramlen;
    unsigned long unique;
    int lineno;                 /* Current line number on expansion */
};

/*
 * The context stack is composed of a linked list of these.
 */
struct Context
{
    Context *next;
    SMacro *localmac;
    char *name;
    unsigned long number;
};

/*
 * This is the internal form which we break input lines up into.
 * Typically stored in linked lists.
 *
 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
 * necessarily used as-is, but is intended to denote the number of
 * the substituted parameter. So in the definition
 *
 *     %define a(x,y) ( (x) & ~(y) )
 * 
 * the token representing `x' will have its type changed to
 * TOK_SMAC_PARAM, but the one representing `y' will be
 * TOK_SMAC_PARAM+1.
 *
 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
 * which doesn't need quotes around it. Used in the pre-include
 * mechanism as an alternative to trying to find a sensible type of
 * quote to use on the filename we were passed.
 */
struct Token
{
    Token *next;
    char *text;
    SMacro *mac;                /* associated macro for TOK_SMAC_END */
    int type;
};
enum
{
    TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
    TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
    TOK_INTERNAL_STRING
};

/*
 * Multi-line macro definitions are stored as a linked list of
 * these, which is essentially a container to allow several linked
 * lists of Tokens.
 * 
 * Note that in this module, linked lists are treated as stacks
 * wherever possible. For this reason, Lines are _pushed_ on to the
 * `expansion' field in MMacro structures, so that the linked list,
 * if walked, would give the macro lines in reverse order; this
 * means that we can walk the list when expanding a macro, and thus
 * push the lines on to the `expansion' field in _istk_ in reverse
 * order (so that when popped back off they are in the right
 * order). It may seem cockeyed, and it relies on my design having
 * an even number of steps in, but it works...
 *
 * Some of these structures, rather than being actual lines, are
 * markers delimiting the end of the expansion of a given macro.
 * This is for use in the cycle-tracking and %rep-handling code.
 * Such structures have `finishes' non-NULL, and `first' NULL. All
 * others have `finishes' NULL, but `first' may still be NULL if
 * the line is blank.
 */
struct Line
{
    Line *next;
    MMacro *finishes;
    Token *first;
};

/*
 * To handle an arbitrary level of file inclusion, we maintain a
 * stack (ie linked list) of these things.
 */
struct Include
{
    Include *next;
    FILE *fp;
    Cond *conds;
    Line *expansion;
    char *fname;
    int lineno, lineinc;
    MMacro *mstk;               /* stack of active macros/reps */
};

/*
 * Conditional assembly: we maintain a separate stack of these for
 * each level of file inclusion. (The only reason we keep the
 * stacks separate is to ensure that a stray `%endif' in a file
 * included from within the true branch of a `%if' won't terminate
 * it and cause confusion: instead, rightly, it'll cause an error.)
 */
struct Cond
{
    Cond *next;
    int state;
};
enum
{
    /*
     * These states are for use just after %if or %elif: IF_TRUE
     * means the condition has evaluated to truth so we are
     * currently emitting, whereas IF_FALSE means we are not
     * currently emitting but will start doing so if a %else comes
     * up. In these states, all directives are admissible: %elif,
     * %else and %endif. (And of course %if.)
     */
    COND_IF_TRUE, COND_IF_FALSE,
    /*
     * These states come up after a %else: ELSE_TRUE means we're
     * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
     * any %elif or %else will cause an error.
     */
    COND_ELSE_TRUE, COND_ELSE_FALSE,
    /*
     * This state means that we're not emitting now, and also that
     * nothing until %endif will be emitted at all. It's for use in
     * two circumstances: (i) when we've had our moment of emission
     * and have now started seeing %elifs, and (ii) when the
     * condition construct in question is contained within a
     * non-emitting branch of a larger condition construct.
     */
    COND_NEVER
};
#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )

/* 
 * These defines are used as the possible return values for do_directive
 */
#define NO_DIRECTIVE_FOUND  0
#define DIRECTIVE_FOUND     1

/*
 * Condition codes. Note that we use c_ prefix not C_ because C_ is
 * used in nasm.h for the "real" condition codes. At _this_ level,
 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
 * ones, so we need a different enum...
 */
static const char *conditions[] = {
    "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
    "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
    "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
};
enum
{
    c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
    c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
    c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
};
static int inverse_ccs[] = {
    c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
    c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
    c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
};

/*
 * Directive names.
 */
static const char *directives[] = {
    "%arg",
    "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
    "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
    "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
    "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
    "%endrep", "%endscope", "%error", "%exitrep", "%iassign", "%idefine", "%if",
    "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
    "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
    "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
    "%ixdefine", "%line",
    "%local",
    "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
    "%scope", "%stacksize",
    "%strlen", "%substr", "%undef", "%xdefine"
};
enum
{
    PP_ARG,
    PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
    PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX, PP_ELIFNDEF,
    PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM, PP_ELIFNSTR,
    PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
    PP_ENDREP, PP_ENDSCOPE, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
    PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO, PP_IFNCTX,
    PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM,
    PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
    PP_IXDEFINE, PP_LINE,
    PP_LOCAL,
    PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
    PP_SCOPE, PP_STACKSIZE,
    PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
};

/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
static int is_condition(int arg)
{
    return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) ||
        ((arg >= PP_IF) && (arg <= PP_IFSTR));
}

/* For TASM compatibility we need to be able to recognise TASM compatible
 * conditional compilation directives. Using the NASM pre-processor does
 * not work, so we look for them specifically from the following list and
 * then jam in the equivalent NASM directive into the input stream.
 */

#ifndef MAX
#       define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
#endif

enum
{
    TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
    TM_IFNDEF, TM_INCLUDE, TM_LOCAL,
    TM_REPT, TM_IRP, TM_MACRO,
    TM_STRUC, TM_SEGMENT
};

static const char *tasm_directives[] = {
    "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
    "ifndef", "include", "local"
};

static int StackSize = 4;
static const char *StackPointer = "ebp";
static int ArgOffset = 8;
static int LocalOffset = 4;
static int Level = 0;


static Context *cstk;
static Include *istk;

static FILE *first_fp = NULL;

static efunc _error;            /* Pointer to client-provided error reporting function */
static evalfunc evaluate;

static int pass;                /* HACK: pass 0 = generate dependencies only */

static unsigned long unique;    /* unique identifier numbers */

static Line *builtindef = NULL;
static Line *stddef = NULL;
static Line *predef = NULL;
static int first_line = 1;

static ListGen *list;

/*
 * The number of hash values we use for the macro lookup tables.
 * FIXME: We should *really* be able to configure this at run time,
 * or even have the hash table automatically expanding when necessary.
 */
#define NHASH 4096

/*
 * The current set of multi-line macros we have defined.
 */
static MMacro *mmacros[NHASH];

/*
 * The current set of single-line macros we have defined.
 */
static SMacro *smacros[NHASH];

/*
 * The multi-line macro we are currently defining, or the %rep
 * block we are currently reading, if any.
 */
static MMacro *defining;

/*
 * The number of macro parameters to allocate space for at a time.
 */
#define PARAM_DELTA 16

/*
 * Macros to make NASM ignore some TASM directives before the first include
 * directive.
 */
static const char *tasm_compat_macros[] =
{
    "%idefine IDEAL",
    "%idefine JUMPS",
    "%idefine END",
    "%idefine P8086	CPU 8086",
    "%idefine P186	CPU 186",
    "%idefine P286	CPU 286",
    "%idefine P286N	CPU 286",
    "%idefine P286P	CPU 286 Priv",
    "%idefine P386	CPU 386",
    "%idefine P386N	CPU 386",
    "%idefine P386P	CPU 386 Priv",
    "%idefine P486	CPU 486",
    "%idefine P586	CPU 586",
    "%idefine .8086	CPU 8086",
    "%idefine .186	CPU 186",
    "%idefine .286	CPU 286",
    "%idefine .286C	CPU 286",
    "%idefine .286P	CPU 286",
    "%idefine .386	CPU 386",
    "%idefine .386C	CPU 386",
    "%idefine .386P	CPU 386",
    "%idefine .486	CPU 486",
    "%idefine .486C	CPU 486",
    "%idefine .486P	CPU 486",
    "%idefine .586	CPU 586",
    "%idefine .586C	CPU 586",
    "%idefine .586P	CPU 586",
    "",
    "%imacro TITLE 1",
    "%endm",
    "%imacro NAME 1",
    "%endm",
    "",
    "%imacro EXTRN 1-*.nolist",
    "%rep %0",
    "[extern %1]",
    "%rotate 1",
    "%endrep",
    "%endmacro",
    "",
    "%imacro PUBLIC 1-*.nolist",
    "%rep %0",
    "[global %1]",
    "%rotate 1",
    "%endrep",
    "%endmacro",
    "",
    "; this is not needed",
    "%idefine PTR",
    NULL
};

static int nested_mac_count, nested_rep_count;

/*
 * Tokens are allocated in blocks to improve speed
 */
#define TOKEN_BLOCKSIZE 4096
static Token *freeTokens = NULL;
struct Blocks {
        Blocks *next;
        void *chunk;
};

static Blocks blocks = { NULL, NULL };

/*
 * Forward declarations.
 */
static Token *expand_mmac_params(Token * tline);
static Token *expand_smacro(Token * tline);
static Token *expand_id(Token * tline);
static Context *get_ctx(char *name, int all_contexts);
static void make_tok_num(Token * tok, yasm_intnum *val);
static void error(int severity, const char *fmt, ...);
static void *new_Block(size_t size);
static void delete_Blocks(void);
static Token *new_Token(Token * next, int type, const char *text,
                        size_t txtlen);
static Token *delete_Token(Token * t);
static Token *tokenise(char *line);

/*
 * Macros for safe checking of token pointers, avoid *(NULL)
 */
#define tok_type_(x,t) ((x) && (x)->type == (t))
#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))

/* Handle TASM specific directives, which do not contain a % in
 * front of them. We do it here because I could not find any other
 * place to do it for the moment, and it is a hack (ideally it would
 * be nice to be able to use the NASM pre-processor to do it).
 */

typedef struct TMEndItem {
    int type;
    void *data;
    struct TMEndItem *next;
} TMEndItem;

static TMEndItem *EndmStack = NULL, *EndsStack = NULL;

char **TMParameters;

struct TStrucField {
    char *name;
    char *type;
    struct TStrucField *next;
};
struct TStruc {
    char *name;
    struct TStrucField *fields, *lastField;
    struct TStruc *next;
};
static struct TStruc *TStrucs = NULL;
static int inTstruc = 0;

struct TSegmentAssume {
    char *segreg;
    char *segment;
};
struct TSegmentAssume *TAssumes;

const char *tasm_get_segment_register(const char *segment)
{
    struct TSegmentAssume *assume;
    if (!TAssumes)
        return NULL;
    for (assume = TAssumes; assume->segreg; assume++) {
        if (!strcmp(assume->segment, segment))
            break;
    }
    return assume->segreg;
}

static char *
check_tasm_directive(char *line)
{
    int i, j, k, m;
    size_t len, len2;
    char *p, *oldline, oldchar, *q, oldchar2;
    TMEndItem *end;

    p = line;

    /* Skip whitespace */
    while (isspace(*p) && *p != 0 && *p != ';')
        p++;

    /* Ignore nasm directives */
    if (*p == '%')
        return line;

    /* Binary search for the directive name */
    len = 0;
    while (!isspace(p[len]) && p[len] != 0 && p[len] != ';')
        len++;
    if (!len)
        return line;

    oldchar = p[len];
    p[len] = 0;
    i = -1;
    j = elements(tasm_directives);
    while (j - i > 1)
    {
        k = (j + i) / 2;
        m = nasm_stricmp(p, tasm_directives[k]);
        if (m == 0)
        {
            /* We have found a directive, so jam a % in front of it
             * so that NASM will then recognise it as one if it's own.
             */
            p[len] = oldchar;
            len = strlen(p);
            oldline = line;
            if (k == TM_IFDIFI)
            {
                /* NASM does not recognise IFDIFI, so we convert it to
                 * %ifdef BOGUS. This is not used in NASM comaptible
                 * code, but does need to parse for the TASM macro
                 * package.
                 */
                line = nasm_malloc(13);
                strcpy(line, "%ifdef BOGUS");
            }
            else if (k == TM_INCLUDE)
            {
                /* add double quotes around file name */
                p += 7 + 1;
                while (isspace(*p) && *p)
                    p++;
                len = strlen(p);
                line = nasm_malloc(1 + 7 + 1 + 1 + len + 1 + 1);
                sprintf(line, "%%include \"%s\"", p);
            }
            else
            {
                line = nasm_malloc(len + 2);
                line[0] = '%';
                memcpy(line + 1, p, len + 1);
            }
            nasm_free(oldline);
            return line;
        }
        else if (m < 0)
        {
            j = k;
        }
        else
            i = k;
    }

    /* Not a simple directive */

    if (!nasm_stricmp(p, "endm")) {
        /* handle end of endm directive */
        char **parameter;
        end = EndmStack;
        /* undef parameters */
        if (!end) {
            error(ERR_FATAL, "ENDM: not in an endm context");
            return line;
        }
        EndmStack = EndmStack->next;
        nasm_free(line);
        switch (end->type) {
        case TM_MACRO:
            len = 0;
            for (parameter = end->data; *parameter; parameter++)
                len += 6 + 1 + strlen(*parameter) + 1;
            len += 5 + 1;
            line = nasm_malloc(len);
            p = line;
            for (parameter = end->data; *parameter; parameter++) {
                p += sprintf(p, "%%undef %s\n", *parameter);
                nasm_free(*parameter);
            }
            nasm_free(end->data);
            nasm_free(end);
            sprintf(p, "%%endm");
            return line;
        case TM_REPT:
            nasm_free(end);
            return nasm_strdup("%endrep");
        case TM_IRP: {
            char **data;
            const char *irp_format = 
                "%%undef %s\n"
                "%%rotate 1\n"
                "%%endrep\n"
                "%%endm\n"
                "irp %s\n"
                "%%undef irp";
            data = end->data;
            line = nasm_malloc(strlen(irp_format) - 4 + strlen(data[0])
                   + strlen(data[1]));
            sprintf(line, irp_format, data[0], data[1]);
            nasm_free(data[0]);
            nasm_free(data[1]);
            nasm_free(data);
            return line;
            }
        default:
            error(ERR_FATAL, "ENDM: bogus endm context type %d\n",end->type);
            return NULL;
        }
    } else if (!nasm_stricmp(p, "end")) {
        nasm_free(line);
        return nasm_strdup("");
    } else if (!nasm_stricmp(p, "rept")) {
        /* handle repeat directive */
        end = nasm_malloc(sizeof(*end));
        end->type = TM_REPT;
        end->next = EndmStack;
        EndmStack = end;
        memcpy(p, "%rep", 4);
        p[len] = oldchar;
        return line;
    } else if (!nasm_stricmp(p, "locals")) {
        tasm_locals = 1;
        nasm_free(line);
        return nasm_strdup("");
    }

    if (!oldchar)
        return line;

    /* handle two-words directives */
    q = p + len + 1;
    /* Skip whitespaces */
    while (isspace(*q) && *q)
        q++;

    len2 = 0;
    while (!isspace(q[len2]) && q[len2]!=',' && q[len2] != 0)
        len2++;
    oldchar2 = q[len2];
    q[len2] = '\0';

    if (!nasm_stricmp(p, "irp")) {
        /* handle indefinite repeat directive */
        const char *irp_format = 
            "%%imacro irp 0-*\n"
            "%%rep %%0\n"
            "%%define %s %%1\n";
        char **data;

        data = malloc(2*sizeof(char*));
        oldline = line;
        line = nasm_malloc(strlen(irp_format) - 2 + len2 + 1);
        sprintf(line,irp_format,q);
        data[0] = nasm_strdup(q);

        if (!oldchar2)
            error(ERR_FATAL, "%s: expected <values>", q + len2);
        p = strchr(q + len2 + 1, '<');
        if (!p)
            error(ERR_FATAL, "%s: expected <values>", q + len2);
        p++;
        q = strchr(p, '>');
        data[1] = nasm_strndup(p, q - p);

        end = nasm_malloc(sizeof(*end));
        end->type = TM_IRP;
        end->next = EndmStack;
        end->data = data;
        EndmStack = end;

        nasm_free(oldline);
        return line;
    } else if (!nasm_stricmp(q, "macro")) {
        char *name = p;
        /* handle MACRO */
        /* count parameters */
        j = 1;
        i = 0;
        TMParameters = nasm_malloc(j*sizeof(*TMParameters));
        len = 0;
        p = q + len2 + 1;
        /* Skip whitespaces */
        while (isspace(*p) && *p)
            p++;
        while (*p) {
            /* Get parameter name */
            for (q = p; !isspace(*q) && *q != ',' && *q; q++);
            len2 = q-p;
            if (len2 == 0)
                error(ERR_FATAL, "'%s': expected parameter name", p);
            TMParameters[i] = nasm_malloc(len2 + 1);
            memcpy(TMParameters[i], p, len2);
            TMParameters[i][len2] = '\0';
            len += len2;
            i++;
            if (i + 1 > j) {
                j *= 2;
                TMParameters = nasm_realloc(TMParameters,
                                               j*sizeof(*TMParameters));
            }
            if (i == 1000)
                error(ERR_FATAL, "too many parameters for macro %s", name);
            p = q;
            while (isspace(*p) && *p)
                p++;
            if (!*p)
                break;
            if (*p != ',')
                error(ERR_FATAL, "expected comma");
            p++;
            while (isspace(*p) && *p)
                p++;
        }
        TMParameters[i] = NULL;
        TMParameters = nasm_realloc(TMParameters,
                                        (i+1)*sizeof(*TMParameters));
        len += 1 + 6 + 1 + strlen(name) + 1 + 3; /* macro definition */
        len += i * (1 + 9 + 1 + 1 + 1 + 3 + 2); /* macro parameter definition */
        oldline = line;
        p = line = nasm_malloc(len + 1);
        p += sprintf(p, "%%imacro %s 0-*", name);
        nasm_free(oldline);
        for (j = 0; TMParameters[j]; j++) {
            p += sprintf(p, "\n%%idefine %s %%{%-u}", TMParameters[j], j + 1);
        }
        end = nasm_malloc(sizeof(*end));
        end->type = TM_MACRO;
        end->next = EndmStack;
        end->data = TMParameters;
        EndmStack = end;
        return line;
    } else if (!nasm_stricmp(q, "proc")) {
        /* handle PROC */
        oldline = line;
        line = nasm_malloc(2 + len + 1);
        sprintf(line, "..%s",p);
        nasm_free(oldline);
        return line;
    } else if (!nasm_stricmp(q, "struc")) {
        /* handle struc */
        struct TStruc *struc;
        if (inTstruc) {
            error(ERR_FATAL, "STRUC: already in a struc context");
            return line;
        }
        oldline = line;
        line = nasm_malloc(5 + 1 + len + 1);
        sprintf(line, "struc %s", p);
        struc = malloc(sizeof(*struc));
        struc->name = nasm_strdup(p);
        struc->fields = NULL;
        struc->lastField = NULL;
        struc->next = TStrucs;
        TStrucs = struc;
        inTstruc = 1;
        nasm_free(oldline);
        end = nasm_malloc(sizeof(*end));
        end->type = TM_STRUC;
        end->next = EndsStack;
        EndsStack = end;
        return line;
    } else if (!nasm_stricmp(q, "segment")) {
        /* handle SEGMENT */
        oldline = line;
        line = nasm_strdup(oldchar2?q+len2+1:"");
        if (tasm_segment) {
            error(ERR_FATAL, "SEGMENT: already in a segment context");
            return line;
        }
        tasm_segment = nasm_strdup(p);
        nasm_free(oldline);
        end = nasm_malloc(sizeof(*end));
        end->type = TM_SEGMENT;
        end->next = EndsStack;
        EndsStack = end;
        return line;
    } else if (!nasm_stricmp(p, "ends") || !nasm_stricmp(q, "ends")) {
        /* handle end of ends directive */
        end = EndsStack;
        /* undef parameters */
        if (!end) {
            error(ERR_FATAL, "ENDS: not in an ends context");
            return line;
        }
        EndsStack = EndsStack->next;
        nasm_free(line);
        switch (end->type) {
        case TM_STRUC:
            inTstruc = 0;
            return nasm_strdup("endstruc");
        case TM_SEGMENT:
            /* XXX: yes, we leak memory here, but that permits labels
             * to avoid strduping... */
            tasm_segment = NULL;
            return nasm_strdup("");
        default:
            error(ERR_FATAL, "ENDS: bogus ends context type %d",end->type);
            return NULL;
        }
    } else if (!nasm_stricmp(p, "endp") || !nasm_stricmp(q, "endp")) {
        nasm_free(line);
        return nasm_strdup("");
    } else if (!nasm_stricmp(p, "assume")) {
        struct TSegmentAssume *assume;
        /* handle ASSUME */
        if (!TAssumes) {
            TAssumes = nasm_malloc(sizeof(*TAssumes));
            TAssumes[0].segreg = NULL;
        }
        i = 0;
        q[len2] = oldchar2;
        /* Skip whitespaces */
        while (isspace(*q) && *q)
            q++;
        while (*q && *q != ';') {
            p = q;
            for (; *q && *q != ';' && *q != ':' && !isspace(*q); q++);
            if (!*q || *q == ';')
                break;
            /* segment register name */
            for (assume = TAssumes; assume->segreg; assume++)
                if (strlen(assume->segreg) == (size_t)(q-p) &&
                    !yasm__strncasecmp(assume->segreg, p, q-p))
                    break;
            if (!assume->segreg) {
                i = assume - TAssumes + 1;
                TAssumes = nasm_realloc(TAssumes, (i+1)*sizeof(*TAssumes));
                assume = TAssumes + i - 1;
                assume->segreg = nasm_strndup(p, q-p);
                assume[1].segreg = NULL;
            }
            for (; *q && *q != ';' && *q != ':' && isspace(*q); q++);
            if (*q != ':')
                error(ERR_FATAL, "expected `:' instead of `%c'", *q);
            for (q++; *q && isspace(*q); q++);

            /* segment name */
            p = q;
            for (; *q && *q != ';' && *q != ',' && !isspace(*q); q++);
            assume->segment = nasm_strndup(p, q-p);
            for (; *q && isspace(*q); q++);
            if (*q && *q != ';' && *q != ',')
                error(ERR_FATAL, "expected `,' instead of `%c'", *q);
        
            if (*q && *q != ';')
                q++;
            for (; *q && isspace(*q); q++);
        }
        TAssumes[i].segreg = NULL;
        TAssumes = nasm_realloc(TAssumes, (i+1)*sizeof(*TAssumes));
        nasm_free(line);
        return nasm_strdup("");
    } else if (inTstruc) {
        struct TStrucField *field;
        /* TODO: handle unnamed data */
        field = nasm_malloc(sizeof(*field));
        field->name = nasm_strdup(p);
        /* TODO: type struc ! */
        field->type = nasm_strdup(q);
        field->next = NULL;
        if (!TStrucs->fields)
                TStrucs->fields = field;
        else if (TStrucs->lastField)
                TStrucs->lastField->next = field;
        TStrucs->lastField = field;
        if (!oldchar2) {
            error(ERR_FATAL, "Expected struc field initializer after %s %s", p, q);
            return line;
        }
        oldline = line;
        line = nasm_malloc(1 + len + 1 + len2 + 1 + strlen(q+len2+1) + 1);
        sprintf(line, ".%s %s %s", p, q, q+len2+1);
        nasm_free(oldline);
        return line;
    }
    {
        struct TStruc *struc;
        for (struc = TStrucs; struc; struc = struc->next) {
            if (!yasm__strcasecmp(q, struc->name)) {
                char *r = q + len2 + 1, *s, *t, tasm_param[6];
                struct TStrucField *field = struc->fields;
                int size, n;
                if (!oldchar2) {
                    error(ERR_FATAL, "Expected struc field initializer after %s %s", p, q);
                    return line;
                }
                r = strchr(r, '<');
                if (!r) {
                    error(ERR_FATAL, "Expected < for struc field initializer in %s %s %s", p, q, r);
                    return line;
                }
                t = strchr(r + 1, '>');
                if (!t) {
                    error(ERR_FATAL, "Expected > for struc field initializer in %s %s %s", p, q, r);
                    return line;
                }
                *t = 0;
                oldline = line;
                size = len + len2 + 128;
                line = nasm_malloc(size);
                if (defining)
                    for (n=0;TMParameters[n];n++)
                        if (!strcmp(TMParameters[n],p)) {
                            sprintf(tasm_param,"%%{%d}",n+1);
                            p = tasm_param;
                            break;
                        }
                n = sprintf(line, "%s: istruc %s\n", p, q);
                /* use initialisers */
                while ((s = strchr(r + 1, ','))) {
                    if (!field) {
                        error(ERR_FATAL, "Too many initializers in structure %s %s", p, q);
                        return oldline;
                    }
                    *s = 0;
                    m = strlen(p) + 1 + strlen(field->name)*2 + 8 +
                        strlen(field->type) + 1 + strlen(r+1) + 2;
                    size += m;
                    line = nasm_realloc(line, size);
                    sprintf(line + n, "%s.%s: at .%s, %s %s\n",
                            p, field->name, field->name, field->type, r + 1);
                    n += m-1;
                    r = s;
                    field = field->next;
                }
                /* complete with last initializer and '?' */
                while(field) {
                    m = strlen(p) + 1 + strlen(field->name)*2 + 8 +
                        strlen(field->type) + 1 + (r ? strlen(r+1) : 1) + 2;
                    size += m;
                    line = nasm_realloc(line, size);
                    sprintf(line + n, "%s.%s: at .%s, %s %s\n", p, field->name,
                            field->name, field->type, r ? r + 1: "?");
                    n += m-1;
                    r = NULL;
                    field = field->next;
                }
                line = nasm_realloc(line, n + 5);
                sprintf(line + n, "iend");
                nasm_free(oldline);
                return line;
            }
        }
    }

    q[len2] = oldchar2;
    p[len] = oldchar;
    
    return line;
}

static Token * tasm_join_tokens(Token *tline)
{
    Token *t, *prev, *next;
    for (prev = NULL, t = tline; t; prev = t, t = next) {
        next = t->next;
        if (t->type == TOK_OTHER && !strcmp(t->text,"&")) {
            if (!prev)
                error(ERR_FATAL, "no token before &");
            else if (!next)
                error(ERR_FATAL, "no token after &");
            else if (prev->type != next->type)
                error(ERR_FATAL, "can't handle different types of token around &");
            else if (!prev->text || !next->text)
                error(ERR_FATAL, "can't handle empty token around &");
            else {
                int lenp = strlen(prev->text);
                int lenn = strlen(next->text);
                prev->text = nasm_realloc(prev->text, lenp + lenn + 1);
                strncpy(prev->text + lenp, next->text, lenn + 1);
                (void) delete_Token(t);
                prev->next = delete_Token(next);
                t = prev;
                next = t->next;
            }
        }
    }
    return tline;
}

/*
 * The pre-preprocessing stage... This function translates line
 * number indications as they emerge from GNU cpp (`# lineno "file"
 * flags') into NASM preprocessor line number indications (`%line
 * lineno file').
 */
static char *
prepreproc(char *line)
{
    int lineno;
    size_t fnlen;
    char *fname, *oldline;
    char *c, *d, *ret;
    Line *l, **lp;

    if (line[0] == '#' && line[1] == ' ')
    {
        oldline = line;
        fname = oldline + 2;
        lineno = atoi(fname);
        fname += strspn(fname, "0123456789 ");
        if (*fname == '"')
            fname++;
        fnlen = strcspn(fname, "\"");
        line = nasm_malloc(20 + fnlen);
        sprintf(line, "%%line %d %.*s", lineno, (int)fnlen, fname);
        nasm_free(oldline);
    }
    if (tasm_compatible_mode)
        line = check_tasm_directive(line);

    if (!(c = strchr(line, '\n')))
        return line;

    /* Turn multiline macros into several lines */
    *c = '\0';
    ret = nasm_strdup(line);

    lp = &istk->expansion;
    do {
        d = strchr(c+1, '\n');
        if (d)
            *d = '\0';
        l = malloc(sizeof(*l));
        l -> first = tokenise(c+1);
        l -> finishes = NULL;
        l -> next = *lp;
        *lp = l;
        c = d;
        lp = &l -> next;
    } while (c);
    nasm_free(line);
    return ret;
}

/*
 * The hash function for macro lookups. Note that due to some
 * macros having case-insensitive names, the hash function must be
 * invariant under case changes. We implement this by applying a
 * perfectly normal hash function to the uppercase of the string.
 */
static int
hash(char *s)
{
    unsigned int h = 0;
    unsigned int i = 0;
    /*
     * Powers of three, mod 31.
     */
    static const int multipliers[] = {
        1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
        30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
    };


    while (*s)
    {
        h += multipliers[i] * (unsigned char) (toupper(*s));
        s++;
        if (++i >= elements(multipliers))
            i = 0;
    }
    h %= NHASH;
    return h;
}

/*
 * Free a linked list of tokens.
 */
static void
free_tlist(Token * list_)
{
    while (list_)
    {
        list_ = delete_Token(list_);
    }
}

/*
 * Free a linked list of lines.
 */
static void
free_llist(Line * list_)
{
    Line *l;
    while (list_)
    {
        l = list_;
        list_ = list_->next;
        free_tlist(l->first);
        nasm_free(l);
    }
}

/*
 * Free an MMacro
 */
static void
free_mmacro(MMacro * m)
{
    nasm_free(m->name);
    free_tlist(m->dlist);
    nasm_free(m->defaults);
    free_llist(m->expansion);
    nasm_free(m);
}

/*
 * Pop the context stack.
 */
static void
ctx_pop(void)
{
    Context *c = cstk;
    SMacro *smac, *s;

    cstk = cstk->next;
    smac = c->localmac;
    while (smac)
    {
        s = smac;
        smac = smac->next;
        nasm_free(s->name);
        free_tlist(s->expansion);
        nasm_free(s);
    }
    nasm_free(c->name);
    nasm_free(c);
}

#define BUF_DELTA 512
/*
 * Read a line from the top file in istk, handling multiple CR/LFs
 * at the end of the line read, and handling spurious ^Zs. Will
 * return lines from the standard macro set if this has not already
 * been done.
 */
static char *
read_line(void)
{
    char *buffer, *p, *q;
    int bufsize, continued_count;

    bufsize = BUF_DELTA;
    buffer = nasm_malloc(BUF_DELTA);
    p = buffer;
    continued_count = 0;
    while (1)
    {
        q = fgets(p, bufsize - (int)(p - buffer), istk->fp);
        if (!q)
            break;
        p += strlen(p);
        if (p > buffer && p[-1] == '\n')
        {
           /* Convert backslash-CRLF line continuation sequences into
              nothing at all (for DOS and Windows) */
           if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
               p -= 3;
               *p = 0;
               continued_count++;
           }
           /* Also convert backslash-LF line continuation sequences into
              nothing at all (for Unix) */
           else if (((p - 1) > buffer) && (p[-2] == '\\')) {
               p -= 2;
               *p = 0;
               continued_count++;
           }
           else {
               break;
           }
        }
        if (p - buffer > bufsize - 10)
        {
            long offset = (long)(p - buffer);
            bufsize += BUF_DELTA;
            buffer = nasm_realloc(buffer, (size_t)bufsize);
            p = buffer + offset;        /* prevent stale-pointer problems */
        }
    }

    if (!q && p == buffer)
    {
        nasm_free(buffer);
        return NULL;
    }

    nasm_src_set_linnum(nasm_src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc));

    /*
     * Play safe: remove CRs as well as LFs, if any of either are
     * present at the end of the line.
     */
    while (--p >= buffer && (*p == '\n' || *p == '\r'))
        *p = '\0';

    /*
     * Handle spurious ^Z, which may be inserted into source files
     * by some file transfer utilities.
     */
    buffer[strcspn(buffer, "\032")] = '\0';

    list->line(LIST_READ, buffer);

    return buffer;
}

/*
 * Tokenise a line of text. This is a very simple process since we
 * don't need to parse the value out of e.g. numeric tokens: we
 * simply split one string into many.
 */
static Token *
tokenise(char *line)
{
    char *p = line;
    int type;
    Token *list_ = NULL;
    Token *t, **tail = &list_;

    while (*line)
    {
        p = line;
        if (*p == '%')
        {
                p++;
                if ( isdigit(*p) ||
                        ((*p == '-' || *p == '+') && isdigit(p[1])) ||
                        ((*p == '+') && (isspace(p[1]) || !p[1])))
                                {
                        do
                        {
                        p++;
                        }
                        while (isdigit(*p));
                        type = TOK_PREPROC_ID;
                }
                else if (*p == '{')
                {
                        p++;
                        while (*p && *p != '}')
                        {
                        p[-1] = *p;
                        p++;
                        }
                        p[-1] = '\0';
                        if (*p)
                        p++;
                        type = TOK_PREPROC_ID;
                }
                else if (isidchar(*p) ||
                                ((*p == '!' || *p == '%' || *p == '$') &&
                                        isidchar(p[1])))
                {
                        do
                        {
                        p++;
                        }
                        while (isidchar(*p));
                        type = TOK_PREPROC_ID;
                }
                else
                {
                        type = TOK_OTHER;
                        if (*p == '%')
                                p++;
                }
        }
        else if (isidstart(*p) || (*p == '$' && isidstart(p[1])))
        {
            type = TOK_ID;
            p++;
            while (*p && isidchar(*p))
                p++;
        }
        else if (*p == '\'' || *p == '"')
        {
            /*
             * A string token.
             */
            char c = *p;
            p++;
            type = TOK_STRING;
            while (*p && *p != c)
                p++;

            if (*p)
            {
                p++;
            }
            else
            {
                error(ERR_WARNING, "unterminated string");
                type = -1;
            }
        }
        else if (isnumstart(*p))
        {
            /*
             * A number token.
             */
            type = TOK_NUMBER;
            p++;
            while (*p && isnumchar(*p))
                p++;
        }
        else if (isspace(*p))
        {
            type = TOK_WHITESPACE;
            p++;
            while (*p && isspace(*p))
                p++;
            /*
             * Whitespace just before end-of-line is discarded by
             * pretending it's a comment; whitespace just before a
             * comment gets lumped into the comment.
             */
            if (!*p || *p == ';')
            {
                type = TOK_COMMENT;
                while (*p)
                    p++;
            }
        }
        else if (*p == ';')
        {
            type = TOK_COMMENT;
            while (*p)
                p++;
        }
        else
        {
            /*
             * Anything else is an operator of some kind. We check
             * for all the double-character operators (>>, <<, //,
             * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
             * else is a single-character operator.
             */
            type = TOK_OTHER;
            if ((p[0] == '>' && p[1] == '>') ||
                    (p[0] == '<' && p[1] == '<') ||
                    (p[0] == '/' && p[1] == '/') ||
                    (p[0] == '<' && p[1] == '=') ||
                    (p[0] == '>' && p[1] == '=') ||
                    (p[0] == '=' && p[1] == '=') ||
                    (p[0] == '!' && p[1] == '=') ||
                    (p[0] == '<' && p[1] == '>') ||
                    (p[0] == '&' && p[1] == '&') ||
                    (p[0] == '|' && p[1] == '|') ||
                    (p[0] == '^' && p[1] == '^'))
            {
                p++;
            }
            p++;
        }

        /* Handle unterminated string */
        if (type == -1)
        {
            *tail = t = new_Token(NULL, TOK_STRING, line, (size_t)(p-line)+1);
            t->text[p-line] = *line;
            tail = &t->next;
        }
        else if (type != TOK_COMMENT)
        {
            *tail = t = new_Token(NULL, type, line, (size_t)(p - line));
            tail = &t->next;
        }
        line = p;
    }
    return list_;
}

/*
 * this function allocates a new managed block of memory and
 * returns a pointer to the block.  The managed blocks are 
 * deleted only all at once by the delete_Blocks function.
 */
static void *
new_Block(size_t size)
{
        Blocks *b = &blocks;
        
        /* first, get to the end of the linked list      */
        while (b->next)
                b = b->next;
        /* now allocate the requested chunk */
        b->chunk = nasm_malloc(size);
        
        /* now allocate a new block for the next request */
        b->next = nasm_malloc(sizeof(Blocks));
        /* and initialize the contents of the new block */
        b->next->next = NULL;
        b->next->chunk = NULL;
        return b->chunk;
}

/*
 * this function deletes all managed blocks of memory
 */
static void
delete_Blocks(void)
{
        Blocks *a,*b = &blocks;

        /* 
         * keep in mind that the first block, pointed to by blocks
         * is a static and not dynamically allocated, so we don't 
         * free it.
         */
        while (b)
        {
                if (b->chunk)
                        nasm_free(b->chunk);
                a = b;
                b = b->next;
                if (a != &blocks)
                        nasm_free(a);
        }
}       

/*
 *  this function creates a new Token and passes a pointer to it 
 *  back to the caller.  It sets the type and text elements, and
 *  also the mac and next elements to NULL.
 */
static Token *
new_Token(Token * next, int type, const char *text, size_t txtlen)
{
    Token *t;
    int i;

    if (freeTokens == NULL)
    {
        freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
        for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
            freeTokens[i].next = &freeTokens[i + 1];
        freeTokens[i].next = NULL;
    }
    t = freeTokens;
    freeTokens = t->next;
    t->next = next;
    t->mac = NULL;
    t->type = type;
    if (type == TOK_WHITESPACE || text == NULL)
    {
        t->text = NULL;
    }
    else
    {
        if (txtlen == 0)
            txtlen = strlen(text);
        t->text = nasm_malloc(1 + txtlen);
        strncpy(t->text, text, txtlen);
        t->text[txtlen] = '\0';
    }
    return t;
}

static Token *
delete_Token(Token * t)
{
    Token *next = t->next;
    nasm_free(t->text);
    t->next = freeTokens;
    freeTokens = t;
    return next;
}

/*
 * Convert a line of tokens back into text.
 * If expand_locals is not zero, identifiers of the form "%$*xxx"
 * will be transformed into ..@ctxnum.xxx
 */
static char *
detoken(Token * tlist, int expand_locals)
{
    Token *t;
    size_t len;
    char *line, *p;

    len = 0;
    for (t = tlist; t; t = t->next)
    {
        if (t->type == TOK_PREPROC_ID && t->text[1] == '!')
        {
            char *p2 = getenv(t->text + 2);
            nasm_free(t->text);
            if (p2)
                t->text = nasm_strdup(p2);
            else
                t->text = NULL;
        }
        /* Expand local macros here and not during preprocessing */
        if (expand_locals &&
                t->type == TOK_PREPROC_ID && t->text &&
                t->text[0] == '%' && t->text[1] == '$')
        {
            Context *ctx = get_ctx(t->text, FALSE);
            if (ctx)
            {
                char buffer[40];
                char *p2, *q = t->text + 2;

                q += strspn(q, "$");
                sprintf(buffer, "..@%lu.", ctx->number);
                p2 = nasm_strcat(buffer, q);
                nasm_free(t->text);
                t->text = p2;
            }
        }
        if (t->type == TOK_WHITESPACE)
        {
            len++;
        }
        else if (t->text)
        {
            len += strlen(t->text);
        }
    }
    p = line = nasm_malloc(len + 1);
    for (t = tlist; t; t = t->next)
    {
        if (t->type == TOK_WHITESPACE)
        {
            *p = ' ';
                p++;
                *p = '\0';
        }
        else if (t->text)
        {
            strcpy(p, t->text);
            p += strlen(p);
        }
    }
    *p = '\0';
    return line;
}

/*
 * A scanner, suitable for use by the expression evaluator, which
 * operates on a line of Tokens. Expects a pointer to a pointer to
 * the first token in the line to be passed in as its private_data
 * field.
 */
static int
ppscan(void *private_data, struct tokenval *tokval)
{
    Token **tlineptr = private_data;
    Token *tline;

    do
    {
        tline = *tlineptr;
        *tlineptr = tline ? tline->next : NULL;
    }
    while (tline && (tline->type == TOK_WHITESPACE ||
                    tline->type == TOK_COMMENT));

    if (!tline)
        return tokval->t_type = TOKEN_EOS;

    if (tline->text[0] == '$' && !tline->text[1])
        return tokval->t_type = TOKEN_HERE;
    if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
        return tokval->t_type = TOKEN_BASE;

    if (tline->type == TOK_ID)
    {
        tokval->t_charptr = tline->text;
        if (tline->text[0] == '$')
        {
            tokval->t_charptr++;
            return tokval->t_type = TOKEN_ID;
        }

        /*
         * This is the only special case we actually need to worry
         * about in this restricted context.
         */
        if (!nasm_stricmp(tline->text, "seg"))
            return tokval->t_type = TOKEN_SEG;

        return tokval->t_type = TOKEN_ID;
    }

    if (tline->type == TOK_NUMBER)
    {
        int rn_error;

        tokval->t_integer = nasm_readnum(tline->text, &rn_error);
        if (rn_error)
            return tokval->t_type = TOKEN_ERRNUM;
        tokval->t_charptr = NULL;
        return tokval->t_type = TOKEN_NUM;
    }

    if (tline->type == TOK_STRING)
    {
        int rn_warn;
        char q, *r;
        size_t l;

        r = tline->text;
        q = *r++;
        l = strlen(r);

        if (l == 0 || r[l - 1] != q)
            return tokval->t_type = TOKEN_ERRNUM;
        tokval->t_integer = nasm_readstrnum(r, l - 1, &rn_warn);
        if (rn_warn)
            error(ERR_WARNING | ERR_PASS1, "character constant too long");
        tokval->t_charptr = NULL;
        return tokval->t_type = TOKEN_NUM;
    }

    if (tline->type == TOK_OTHER)
    {
        if (!strcmp(tline->text, "<<"))
            return tokval->t_type = TOKEN_SHL;
        if (!strcmp(tline->text, ">>"))
            return tokval->t_type = TOKEN_SHR;
        if (!strcmp(tline->text, "//"))
            return tokval->t_type = TOKEN_SDIV;
        if (!strcmp(tline->text, "%%"))
            return tokval->t_type = TOKEN_SMOD;
        if (!strcmp(tline->text, "=="))
            return tokval->t_type = TOKEN_EQ;
        if (!strcmp(tline->text, "<>"))
            return tokval->t_type = TOKEN_NE;
        if (!strcmp(tline->text, "!="))
            return tokval->t_type = TOKEN_NE;
        if (!strcmp(tline->text, "<="))
            return tokval->t_type = TOKEN_LE;
        if (!strcmp(tline->text, ">="))
            return tokval->t_type = TOKEN_GE;
        if (!strcmp(tline->text, "&&"))
            return tokval->t_type = TOKEN_DBL_AND;
        if (!strcmp(tline->text, "^^"))
            return tokval->t_type = TOKEN_DBL_XOR;
        if (!strcmp(tline->text, "||"))
            return tokval->t_type = TOKEN_DBL_OR;
    }

    /*
     * We have no other options: just return the first character of
     * the token text.
     */
    return tokval->t_type = tline->text[0];
}

/*
 * Compare a string to the name of an existing macro; this is a
 * simple wrapper which calls either strcmp or nasm_stricmp
 * depending on the value of the `casesense' parameter.
 */
static int
mstrcmp(char *p, char *q, int casesense)
{
    return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
}

/*
 * Return the Context structure associated with a %$ token. Return
 * NULL, having _already_ reported an error condition, if the
 * context stack isn't deep enough for the supplied number of $
 * signs.
 * If all_contexts == TRUE, contexts that enclose current are
 * also scanned for such smacro, until it is found; if not -
 * only the context that directly results from the number of $'s
 * in variable's name.
 */
static Context *
get_ctx(char *name, int all_contexts)
{
    Context *ctx;
    SMacro *m;
    size_t i;

    if (!name || name[0] != '%' || name[1] != '$')
        return NULL;

    if (!cstk)
    {
        error(ERR_NONFATAL, "`%s': context stack is empty", name);
        return NULL;
    }

    for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--)
    {
        ctx = ctx->next;
/*        i--;  Lino - 02/25/02 */
    }
    if (!ctx)
    {
        error(ERR_NONFATAL, "`%s': context stack is only"
                " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
        return NULL;
    }
    if (!all_contexts)
        return ctx;

    do
    {
        /* Search for this smacro in found context */
        m = ctx->localmac;
        while (m)
        {
            if (!mstrcmp(m->name, name, m->casesense))
                return ctx;
            m = m->next;
        }
        ctx = ctx->next;
    }
    while (ctx);
    return NULL;
}

/*
 * Open an include file. This routine must always return a valid
 * file pointer if it returns - it's responsible for throwing an
 * ERR_FATAL and bombing out completely if not. It should also try
 * the include path one by one until it finds the file or reaches
 * the end of the path.
 */
static FILE *
inc_fopen(char *file, char **newname)
{
    FILE *fp;
    char *combine = NULL, *c;
    char *pb, *p1, *p2, *file2 = NULL;

    /* Try to expand all %ENVVAR% in filename.  Warn, and leave %string%
     * intact, if ENVVAR is not set in the environment.
     */
    pb = file;
    p1 = pb;
    for (;;) {
        char *env;
        while (*p1 != '\0' && *p1 != '%')
            p1++;
        if (*p1 == '\0')
            break;
        p2 = p1+1;
        while (*p2 != '\0' && *p2 != '%')
            p2++;
        if (*p2 == '\0')
            break;
        /* Okay, we have a %...%, with p1 pointing to the first %, and p2
         * pointing to the second %.
         */
        *p2 = '\0';
        env = getenv(p1+1);
        if (!env) {
            /* warn, restore %, and continue looking */
            error(ERR_WARNING, "environment variable `%s' does not exist",
                  p1+1);
            *p2 = '%';
            p1 = p2+1;
            continue;
        }
        /* need to expand */
        if (!file2) {
            file2 = nasm_malloc(strlen(file)+strlen(env)+1);
            file2[0] = '\0';
        } else
            file2 = nasm_realloc(file2, strlen(file2)+strlen(env)+1);
        *p1 = '\0';
        strcat(file2, pb);
        strcat(file2, env);
        pb = p2+1;
        p1 = pb;
    }
    /* add tail end; string is long enough that we don't need to realloc */
    if (file2)
        strcat(file2, pb);

    fp = yasm_fopen_include(file2 ? file2 : file, nasm_src_get_fname(), "r",
                            &combine);
    if (!fp && tasm_compatible_mode)
    {
        char *thefile = file2 ? file2 : file;
        /* try a few case combinations */
        do {
            for (c = thefile; *c; c++)
                *c = toupper(*c);
            fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
            if (fp) break;
            *thefile = tolower(*thefile);
            fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
            if (fp) break;
            for (c = thefile; *c; c++)
                *c = tolower(*c);
            fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
            if (fp) break;
            *thefile = toupper(*thefile);
            fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
            if (fp) break;
        } while (0);
    }
    if (!fp)
        error(ERR_FATAL, "unable to open include file `%s'",
              file2 ? file2 : file);
    nasm_preproc_add_dep(combine);

    if (file2)
        nasm_free(file2);

    *newname = combine;
    return fp;
}

/*
 * Determine if we should warn on defining a single-line macro of
 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
 * return TRUE if _any_ single-line macro of that name is defined.
 * Otherwise, will return TRUE if a single-line macro with either
 * `nparam' or no parameters is defined.
 *
 * If a macro with precisely the right number of parameters is
 * defined, or nparam is -1, the address of the definition structure
 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
 * is NULL, no action will be taken regarding its contents, and no
 * error will occur.
 *
 * Note that this is also called with nparam zero to resolve
 * `ifdef'.
 *
 * If you already know which context macro belongs to, you can pass
 * the context pointer as first parameter; if you won't but name begins
 * with %$ the context will be automatically computed. If all_contexts
 * is true, macro will be searched in outer contexts as well.
 */
static int
smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
        int nocase)
{
    SMacro *m;
    int highest_level = -1;

    if (ctx)
        m = ctx->localmac;
    else if (name[0] == '%' && name[1] == '$')
    {
        if (cstk)
            ctx = get_ctx(name, FALSE);
        if (!ctx)
            return FALSE;       /* got to return _something_ */
        m = ctx->localmac;
    }
    else
        m = smacros[hash(name)];

    while (m)
    {
        if (!mstrcmp(m->name, name, m->casesense && nocase) &&
                (nparam <= 0 || m->nparam == 0 || nparam == m->nparam) && (highest_level < 0 || m->level > highest_level))
        {
            highest_level = m->level;
            if (defn)
            {
                if (nparam == m->nparam || nparam == -1)
                    *defn = m;
                else
                    *defn = NULL;
            }
        }
        m = m->next;
    }

    return highest_level >= 0;
}

/*
 * Count and mark off the parameters in a multi-line macro call.
 * This is called both from within the multi-line macro expansion
 * code, and also to mark off the default parameters when provided
 * in a %macro definition line.
 */
static void
count_mmac_params(Token * t, int *nparam, Token *** params)
{
    int paramsize, brace;

    *nparam = paramsize = 0;
    *params = NULL;
    while (t)
    {
        if (*nparam+1 >= paramsize)
        {
            paramsize += PARAM_DELTA;
            *params = nasm_realloc(*params, sizeof(**params) * paramsize);
        }
        skip_white_(t);
        brace = FALSE;
        if (tok_is_(t, "{"))
            brace = TRUE;
        (*params)[(*nparam)++] = t;
        while (tok_isnt_(t, brace ? "}" : ","))
            t = t->next;
        if (t)
        {                       /* got a comma/brace */
            t = t->next;
            if (brace)
            {
                /*
                 * Now we've found the closing brace, look further
                 * for the comma.
                 */
                skip_white_(t);
                if (tok_isnt_(t, ","))
                {
                    error(ERR_NONFATAL,
                            "braces do not enclose all of macro parameter");
                    while (tok_isnt_(t, ","))
                        t = t->next;
                }
                if (t)
                    t = t->next;        /* eat the comma */
            }
        }
    }
}

/*
 * Determine whether one of the various `if' conditions is true or
 * not.
 *
 * We must free the tline we get passed.
 */
static int
if_condition(Token * tline, int i)
{
    int j, casesense;
    Token *t, *tt, **tptr, *origline;
    struct tokenval tokval;
    yasm_expr *evalresult;
    yasm_intnum *intn;

    origline = tline;

    switch (i)
    {
        case PP_IFCTX:
        case PP_ELIFCTX:
        case PP_IFNCTX:
        case PP_ELIFNCTX:
            j = FALSE;          /* have we matched yet? */
            while (cstk && tline)
            {
                skip_white_(tline);
                if (!tline || tline->type != TOK_ID)
                {
                    error(ERR_NONFATAL,
                            "`%s' expects context identifiers",
                            directives[i]);
                    free_tlist(origline);
                    return -1;
                }
                if (!nasm_stricmp(tline->text, cstk->name))
                    j = TRUE;
                tline = tline->next;
            }
            if (i == PP_IFNCTX || i == PP_ELIFNCTX)
                j = !j;
            free_tlist(origline);
            return j;

        case PP_IFDEF:
        case PP_ELIFDEF:
        case PP_IFNDEF:
        case PP_ELIFNDEF:
            j = FALSE;          /* have we matched yet? */
            while (tline)
            {
                skip_white_(tline);
                if (!tline || (tline->type != TOK_ID &&
                                (tline->type != TOK_PREPROC_ID ||
                                        tline->text[1] != '$')))
                {
                    error(ERR_NONFATAL,
                          "`%s' expects macro identifiers",
                          directives[i]);
                    free_tlist(origline);
                    return -1;
                }
                if (smacro_defined(NULL, tline->text, 0, NULL, 1))
                    j = TRUE;
                tline = tline->next;
            }
            if (i == PP_IFNDEF || i == PP_ELIFNDEF)
                j = !j;
            free_tlist(origline);
            return j;

        case PP_IFIDN:
        case PP_ELIFIDN:
        case PP_IFNIDN:
        case PP_ELIFNIDN:
        case PP_IFIDNI:
        case PP_ELIFIDNI:
        case PP_IFNIDNI:
        case PP_ELIFNIDNI:
            tline = expand_smacro(tline);
            t = tt = tline;
            while (tok_isnt_(tt, ","))
                tt = tt->next;
            if (!tt)
            {
                error(ERR_NONFATAL,
                        "`%s' expects two comma-separated arguments",
                        directives[i]);
                free_tlist(tline);
                return -1;
            }
            tt = tt->next;
            casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
                    i == PP_IFNIDN || i == PP_ELIFNIDN);
            j = TRUE;           /* assume equality unless proved not */
            while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt)
            {
                if (tt->type == TOK_OTHER && !strcmp(tt->text, ","))
                {
                    error(ERR_NONFATAL, "`%s': more than one comma on line",
                            directives[i]);
                    free_tlist(tline);
                    return -1;
                }
                if (t->type == TOK_WHITESPACE)
                {
                    t = t->next;
                    continue;
                }
                if (tt->type == TOK_WHITESPACE)
                {
                    tt = tt->next;
                    continue;
                }
                if (tt->type != t->type)
                {
                    j = FALSE;  /* found mismatching tokens */
                    break;
                }
                /* Unify surrounding quotes for strings */
                if (t->type == TOK_STRING)
                {
                    tt->text[0] = t->text[0];
                    tt->text[strlen(tt->text) - 1] = t->text[0];
                }
                if (mstrcmp(tt->text, t->text, casesense) != 0)
                {
                    j = FALSE;  /* found mismatching tokens */
                    break;
                }

                t = t->next;
                tt = tt->next;
            }
            if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
                j = FALSE;      /* trailing gunk on one end or other */
            if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
                    i == PP_IFNIDNI || i == PP_ELIFNIDNI)
                j = !j;
            free_tlist(tline);
            return j;

        case PP_IFMACRO:
        case PP_ELIFMACRO:
        case PP_IFNMACRO:
        case PP_ELIFNMACRO:
        {
            int found = 0;
            MMacro searching, *mmac;

            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tok_type_(tline, TOK_ID))
            {
                error(ERR_NONFATAL,
                        "`%s' expects a macro name",
                      directives[i]);
                return -1;
            }
            searching.name = nasm_strdup(tline->text);
            searching.casesense = (i == PP_MACRO);
            searching.plus = FALSE;
            searching.nolist = FALSE;
            searching.in_progress = FALSE;
            searching.rep_nest = NULL;
            searching.nparam_min = 0;
            searching.nparam_max = INT_MAX;
            tline = expand_smacro(tline->next);
            skip_white_(tline);
            if (!tline)
            {
            } else if (!tok_type_(tline, TOK_NUMBER))
            {
                error(ERR_NONFATAL,
                      "`%s' expects a parameter count or nothing",
                      directives[i]);
            }
            else
            {
                intn = nasm_readnum(tline->text, &j);
                searching.nparam_min = searching.nparam_max =
                    yasm_intnum_get_int(intn);
                yasm_intnum_destroy(intn);
                if (j)
                    error(ERR_NONFATAL,
                          "unable to parse parameter count `%s'",
                          tline->text);
            }
            if (tline && tok_is_(tline->next, "-"))
            {
                tline = tline->next->next;
                if (tok_is_(tline, "*"))
                    searching.nparam_max = INT_MAX;
                else if (!tok_type_(tline, TOK_NUMBER))
                    error(ERR_NONFATAL,
                          "`%s' expects a parameter count after `-'",
                          directives[i]);
                else
                {
                    intn = nasm_readnum(tline->text, &j);
                    searching.nparam_max = yasm_intnum_get_int(intn);
                    yasm_intnum_destroy(intn);
                    if (j)
                        error(ERR_NONFATAL,
                                "unable to parse parameter count `%s'",
                                tline->text);
                    if (searching.nparam_min > searching.nparam_max)
                        error(ERR_NONFATAL,
                                "minimum parameter count exceeds maximum");
                }
            }
            if (tline && tok_is_(tline->next, "+"))
            {
                tline = tline->next;
                searching.plus = TRUE;
            }
            mmac = mmacros[hash(searching.name)];
            while (mmac)
            {
                if (!strcmp(mmac->name, searching.name) &&
                        (mmac->nparam_min <= searching.nparam_max
                                || searching.plus)
                        && (searching.nparam_min <= mmac->nparam_max
                                || mmac->plus))
                {
                    found = TRUE;
                    break;
                }
                mmac = mmac->next;
            }
            nasm_free(searching.name);
            free_tlist(origline);
            if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
                found = !found;
            return found;
        }

        case PP_IFID:
        case PP_ELIFID:
        case PP_IFNID:
        case PP_ELIFNID:
        case PP_IFNUM:
        case PP_ELIFNUM:
        case PP_IFNNUM:
        case PP_ELIFNNUM:
        case PP_IFSTR:
        case PP_ELIFSTR:
        case PP_IFNSTR:
        case PP_ELIFNSTR:
            tline = expand_smacro(tline);
            t = tline;
            while (tok_type_(t, TOK_WHITESPACE))
                t = t->next;
            j = FALSE;          /* placate optimiser */
            if (t)
                switch (i)
                {
                    case PP_IFID:
                    case PP_ELIFID:
                    case PP_IFNID:
                    case PP_ELIFNID:
                        j = (t->type == TOK_ID);
                        break;
                    case PP_IFNUM:
                    case PP_ELIFNUM:
                    case PP_IFNNUM:
                    case PP_ELIFNNUM:
                        j = (t->type == TOK_NUMBER);
                        break;
                    case PP_IFSTR:
                    case PP_ELIFSTR:
                    case PP_IFNSTR:
                    case PP_ELIFNSTR:
                        j = (t->type == TOK_STRING);
                        break;
                }
            if (i == PP_IFNID || i == PP_ELIFNID ||
                    i == PP_IFNNUM || i == PP_ELIFNNUM ||
                    i == PP_IFNSTR || i == PP_ELIFNSTR)
                j = !j;
            free_tlist(tline);
            return j;

        case PP_IF:
        case PP_ELIF:
            t = tline = expand_smacro(tline);
            tptr = &t;
            tokval.t_type = TOKEN_INVALID;
            evalresult = evaluate(ppscan, tptr, &tokval, pass | CRITICAL,
                                  error);
            free_tlist(tline);
            if (!evalresult)
                return -1;
            if (tokval.t_type)
                error(ERR_WARNING,
                        "trailing garbage after expression ignored");
            intn = yasm_expr_get_intnum(&evalresult, 0);
            if (!intn)
            {
                error(ERR_NONFATAL,
                        "non-constant value given to `%s'", directives[i]);
                yasm_expr_destroy(evalresult);
                return -1;
            }
            j = !yasm_intnum_is_zero(intn);
            yasm_expr_destroy(evalresult);
            return j;

        default:
            error(ERR_FATAL,
                    "preprocessor directive `%s' not yet implemented",
                    directives[i]);
            free_tlist(origline);
            return -1;          /* yeah, right */
    }
}

/*
 * Expand macros in a string. Used in %error and %include directives.
 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
 * The returned variable should ALWAYS be freed after usage.
 */
static void
expand_macros_in_string(char **p)
{
    Token *line = tokenise(*p);
    line = expand_smacro(line);
    *p = detoken(line, FALSE);
}

/**
 * find and process preprocessor directive in passed line
 * Find out if a line contains a preprocessor directive, and deal
 * with it if so.
 * 
 * If a directive _is_ found, it is the responsibility of this routine
 * (and not the caller) to free_tlist() the line.
 *
 * @param tline a pointer to the current tokeninzed line linked list
 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
 * 
 */
static int
do_directive(Token * tline)
{
    int i, j, k, m, nparam, nolist;
    int offset;
    char *p, *mname, *newname;
    Include *inc;
    Context *ctx;
    Cond *cond;
    SMacro *smac, **smhead;
    MMacro *mmac;
    Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
    Line *l;
    struct tokenval tokval;
    yasm_expr *evalresult;
    MMacro *tmp_defining;       /* Used when manipulating rep_nest */
    yasm_intnum *intn;

    origline = tline;

    skip_white_(tline);
    if (!tok_type_(tline, TOK_PREPROC_ID) ||
            (tline->text[1] == '%' || tline->text[1] == '$'
                    || tline->text[1] == '!'))
        return NO_DIRECTIVE_FOUND;

    i = -1;
    j = elements(directives);
    while (j - i > 1)
    {
        k = (j + i) / 2;
        m = nasm_stricmp(tline->text, directives[k]);
        if (m == 0) {
                if (tasm_compatible_mode) {
                i = k;
                j = -2;
                } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) {
                    i = k;
                j = -2;
                }
            break;
        }
        else if (m < 0) {
            j = k;
        }
        else
            i = k;
    }

    /*
     * If we're in a non-emitting branch of a condition construct,
     * or walking to the end of an already terminated %rep block,
     * we should ignore all directives except for condition
     * directives.
     */
    if (((istk->conds && !emitting(istk->conds->state)) ||
         (istk->mstk && !istk->mstk->in_progress)) &&
        !is_condition(i))
    {
        return NO_DIRECTIVE_FOUND;
    }

    /*
     * If we're defining a macro or reading a %rep block, we should
     * ignore all directives except for %macro/%imacro (which
     * generate an error), %endm/%endmacro, and (only if we're in a
     * %rep block) %endrep. If we're in a %rep block, another %rep
     * causes an error, so should be let through.
     */
    if (defining && i != PP_MACRO && i != PP_IMACRO &&
            i != PP_ENDMACRO && i != PP_ENDM &&
            (defining->name || (i != PP_ENDREP && i != PP_REP)))
    {
        return NO_DIRECTIVE_FOUND;
    }

    if (defining) {
        if (i == PP_MACRO || i == PP_IMACRO) {
            nested_mac_count++;
            return NO_DIRECTIVE_FOUND;
        } else if (nested_mac_count > 0) {
            if (i == PP_ENDMACRO) {
                nested_mac_count--;
                return NO_DIRECTIVE_FOUND;
            }
        }
        if (!defining->name) {
            if (i == PP_REP) {
                nested_rep_count++;
                return NO_DIRECTIVE_FOUND;
            } else if (nested_rep_count > 0) {
                if (i == PP_ENDREP) {
                    nested_rep_count--;
                    return NO_DIRECTIVE_FOUND;
                }
            }
        }
    }

    if (j != -2)
    {
        error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
                tline->text);
        return NO_DIRECTIVE_FOUND;              /* didn't get it */
    }

    switch (i)
    {
        case PP_STACKSIZE:
            /* Directive to tell NASM what the default stack size is. The
             * default is for a 16-bit stack, and this can be overriden with
             * %stacksize large.
             * the following form:
             *
             *      ARG arg1:WORD, arg2:DWORD, arg4:QWORD
             */
            tline = tline->next;
            if (tline && tline->type == TOK_WHITESPACE)
                tline = tline->next;
            if (!tline || tline->type != TOK_ID)
            {
                error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            if (nasm_stricmp(tline->text, "flat") == 0)
            {
                /* All subsequent ARG directives are for a 32-bit stack */
                StackSize = 4;
                StackPointer = "ebp";
                ArgOffset = 8;
                LocalOffset = 4;
            }
            else if (nasm_stricmp(tline->text, "large") == 0)
            {
                /* All subsequent ARG directives are for a 16-bit stack,
                 * far function call.
                 */
                StackSize = 2;
                StackPointer = "bp";
                ArgOffset = 4;
                LocalOffset = 2;
            }
            else if (nasm_stricmp(tline->text, "small") == 0)
            {
                /* All subsequent ARG directives are for a 16-bit stack,
                   * far function call. We don't support near functions.
                 */
                StackSize = 2;
                StackPointer = "bp";
                ArgOffset = 6;
                LocalOffset = 2;
            }
            else
            {
                error(ERR_NONFATAL, "`%%stacksize' invalid size type");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_ARG:
            /* TASM like ARG directive to define arguments to functions, in
             * the following form:
             *
             *      ARG arg1:WORD, arg2:DWORD, arg4:QWORD
             */
            offset = ArgOffset;
            do
            {
                char *arg, directive[256];
                int size = StackSize;

                /* Find the argument name */
                tline = tline->next;
                if (tline && tline->type == TOK_WHITESPACE)
                    tline = tline->next;
                if (!tline || tline->type != TOK_ID)
                {
                    error(ERR_NONFATAL, "`%%arg' missing argument parameter");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                arg = tline->text;

                /* Find the argument size type */
                tline = tline->next;
                if (!tline || tline->type != TOK_OTHER
                        || tline->text[0] != ':')
                {
                    error(ERR_NONFATAL,
                            "Syntax error processing `%%arg' directive");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                tline = tline->next;
                if (!tline || tline->type != TOK_ID)
                {
                    error(ERR_NONFATAL,
                            "`%%arg' missing size type parameter");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }

                /* Allow macro expansion of type parameter */
                tt = tokenise(tline->text);
                tt = expand_smacro(tt);
                if (nasm_stricmp(tt->text, "byte") == 0)
                {
                    size = MAX(StackSize, 1);
                }
                else if (nasm_stricmp(tt->text, "word") == 0)
                {
                    size = MAX(StackSize, 2);
                }
                else if (nasm_stricmp(tt->text, "dword") == 0)
                {
                    size = MAX(StackSize, 4);
                }
                else if (nasm_stricmp(tt->text, "qword") == 0)
                {
                    size = MAX(StackSize, 8);
                }
                else if (nasm_stricmp(tt->text, "tword") == 0)
                {
                    size = MAX(StackSize, 10);
                }
                else
                {
                    error(ERR_NONFATAL,
                            "Invalid size type for `%%arg' missing directive");
                    free_tlist(tt);
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                free_tlist(tt);

                /* Now define the macro for the argument */
                sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer,
                        offset);
                do_directive(tokenise(directive));
                offset += size;

                /* Move to the next argument in the list */
                tline = tline->next;
                if (tline && tline->type == TOK_WHITESPACE)
                    tline = tline->next;
            }
            while (tline && tline->type == TOK_OTHER
                    && tline->text[0] == ',');
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_LOCAL:
            /* TASM like LOCAL directive to define local variables for a
             * function, in the following form:
             *
             *      LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
             *
             * The '= LocalSize' at the end is ignored by NASM, but is
             * required by TASM to define the local parameter size (and used
             * by the TASM macro package).
             */
            offset = LocalOffset;
            do
            {
                char *local, directive[256];
                int size = StackSize;

                /* Find the argument name */
                tline = tline->next;
                if (tline && tline->type == TOK_WHITESPACE)
                    tline = tline->next;
                if (!tline || tline->type != TOK_ID)
                {
                    error(ERR_NONFATAL,
                            "`%%local' missing argument parameter");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                local = tline->text;

                /* Find the argument size type */
                tline = tline->next;
                if (!tline || tline->type != TOK_OTHER
                        || tline->text[0] != ':')
                {
                    error(ERR_NONFATAL,
                            "Syntax error processing `%%local' directive");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                tline = tline->next;
                if (!tline || tline->type != TOK_ID)
                {
                    error(ERR_NONFATAL,
                            "`%%local' missing size type parameter");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }

                /* Allow macro expansion of type parameter */
                tt = tokenise(tline->text);
                tt = expand_smacro(tt);
                if (nasm_stricmp(tt->text, "byte") == 0)
                {
                    size = MAX(StackSize, 1);
                }
                else if (nasm_stricmp(tt->text, "word") == 0)
                {
                    size = MAX(StackSize, 2);
                }
                else if (nasm_stricmp(tt->text, "dword") == 0)
                {
                    size = MAX(StackSize, 4);
                }
                else if (nasm_stricmp(tt->text, "qword") == 0)
                {
                    size = MAX(StackSize, 8);
                }
                else if (nasm_stricmp(tt->text, "tword") == 0)
                {
                    size = MAX(StackSize, 10);
                }
                else
                {
                    error(ERR_NONFATAL,
                            "Invalid size type for `%%local' missing directive");
                    free_tlist(tt);
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                free_tlist(tt);

                /* Now define the macro for the argument */
                sprintf(directive, "%%define %s (%s-%d)", local, StackPointer,
                        offset);
                do_directive(tokenise(directive));
                offset += size;

                /* Now define the assign to setup the enter_c macro correctly */
                sprintf(directive, "%%assign %%$localsize %%$localsize+%d",
                        size);
                do_directive(tokenise(directive));

                /* Move to the next argument in the list */
                tline = tline->next;
                if (tline && tline->type == TOK_WHITESPACE)
                    tline = tline->next;
            }
            while (tline && tline->type == TOK_OTHER
                    && tline->text[0] == ',');
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_CLEAR:
            if (tline->next)
                error(ERR_WARNING,
                        "trailing garbage after `%%clear' ignored");
            for (j = 0; j < NHASH; j++)
            {
                while (mmacros[j])
                {
                    MMacro *m2 = mmacros[j];
                    mmacros[j] = m2->next;
                    free_mmacro(m2);
                }
                while (smacros[j])
                {
                    SMacro *s = smacros[j];
                    smacros[j] = smacros[j]->next;
                    nasm_free(s->name);
                    free_tlist(s->expansion);
                    nasm_free(s);
                }
            }
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_INCLUDE:
            tline = tline->next;
            skip_white_(tline);
            if (!tline || (tline->type != TOK_STRING &&
                            tline->type != TOK_INTERNAL_STRING))
            {
                error(ERR_NONFATAL, "`%%include' expects a file name");
                free_tlist(origline);
                return DIRECTIVE_FOUND; /* but we did _something_ */
            }
            if (tline->next)
                error(ERR_WARNING,
                        "trailing garbage after `%%include' ignored");
            if (tline->type != TOK_INTERNAL_STRING)
            {
                p = tline->text + 1;    /* point past the quote to the name */
                p[strlen(p) - 1] = '\0';        /* remove the trailing quote */
            }
            else
                p = tline->text;        /* internal_string is easier */
            expand_macros_in_string(&p);
            inc = nasm_malloc(sizeof(Include));
            inc->next = istk;
            inc->conds = NULL;
            inc->fp = inc_fopen(p, &newname);
            inc->fname = nasm_src_set_fname(newname);
            inc->lineno = nasm_src_set_linnum(0);
            inc->lineinc = 1;
            inc->expansion = NULL;
            inc->mstk = NULL;
            istk = inc;
            list->uplevel(LIST_INCLUDE);
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_PUSH:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tok_type_(tline, TOK_ID))
            {
                error(ERR_NONFATAL, "`%%push' expects a context identifier");
                free_tlist(origline);
                return DIRECTIVE_FOUND; /* but we did _something_ */
            }
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%push' ignored");
            ctx = nasm_malloc(sizeof(Context));
            ctx->next = cstk;
            ctx->localmac = NULL;
            ctx->name = nasm_strdup(tline->text);
            ctx->number = unique++;
            cstk = ctx;
            free_tlist(origline);
            break;

        case PP_REPL:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tok_type_(tline, TOK_ID))
            {
                error(ERR_NONFATAL, "`%%repl' expects a context identifier");
                free_tlist(origline);
                return DIRECTIVE_FOUND; /* but we did _something_ */
            }
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
            if (!cstk)
                error(ERR_NONFATAL, "`%%repl': context stack is empty");
            else
            {
                nasm_free(cstk->name);
                cstk->name = nasm_strdup(tline->text);
            }
            free_tlist(origline);
            break;

        case PP_POP:
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
            if (!cstk)
                error(ERR_NONFATAL,
                        "`%%pop': context stack is already empty");
            else
                ctx_pop();
            free_tlist(origline);
            break;

        case PP_SCOPE:
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%scope' ignored");
            Level++;
            free_tlist(origline);
            break;

        case PP_ENDSCOPE:
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%endscope' ignored");
            if (!Level)
                error(ERR_NONFATAL,
                        "`%%endscope': already popped all levels");
            else
            {
                for (k = 0; k < NHASH; k++)
                {
                    SMacro **smlast = &smacros[k];
                    smac = smacros[k];
                    while (smac)
                    {
                        if (smac->level < Level)
                        {
                            smlast = &smac->next;
                            smac = smac->next;
                        }
                        else
                        {
                            *smlast = smac->next;
                            nasm_free(smac->name);
                            free_tlist(smac->expansion);
                            nasm_free(smac);
                            smac = *smlast;
                        }
                    }
                }
                for (ctx = cstk; ctx; ctx = ctx->next)
                {
                    SMacro **smlast = &ctx->localmac;
                    smac = ctx->localmac;
                    while (smac)
                    {
                        if (smac->level < Level)
                        {
                            smlast = &smac->next;
                            smac = smac->next;
                        }
                        else
                        {
                            *smlast = smac->next;
                            nasm_free(smac->name);
                            free_tlist(smac->expansion);
                            nasm_free(smac);
                            smac = *smlast;
                        }
                    }
                }
                Level--;
            }
            free_tlist(origline);
            break;

        case PP_ERROR:
            tline->next = expand_smacro(tline->next);
            tline = tline->next;
            skip_white_(tline);
            if (tok_type_(tline, TOK_STRING))
            {
                p = tline->text + 1;    /* point past the quote to the name */
                p[strlen(p) - 1] = '\0';        /* remove the trailing quote */
                expand_macros_in_string(&p);
                error(ERR_NONFATAL, "%s", p);
                nasm_free(p);
            }
            else
            {
                p = detoken(tline, FALSE);
                error(ERR_WARNING, "%s", p);
                nasm_free(p);
            }
            free_tlist(origline);
            break;

        case PP_IF:
        case PP_IFCTX:
        case PP_IFDEF:
        case PP_IFID:
        case PP_IFIDN:
        case PP_IFIDNI:
        case PP_IFMACRO:
        case PP_IFNCTX:
        case PP_IFNDEF:
        case PP_IFNID:
        case PP_IFNIDN:
        case PP_IFNIDNI:
        case PP_IFNMACRO:
        case PP_IFNNUM:
        case PP_IFNSTR:
        case PP_IFNUM:
        case PP_IFSTR:
            if (istk->conds && !emitting(istk->conds->state))
                j = COND_NEVER;
            else
            {
                j = if_condition(tline->next, i);
                tline->next = NULL;     /* it got freed */
                j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
            }
            free_tlist(origline);
            cond = nasm_malloc(sizeof(Cond));
            cond->next = istk->conds;
            cond->state = j;
            istk->conds = cond;
            return DIRECTIVE_FOUND;

        case PP_ELIF:
        case PP_ELIFCTX:
        case PP_ELIFDEF:
        case PP_ELIFID:
        case PP_ELIFIDN:
        case PP_ELIFIDNI:
        case PP_ELIFMACRO:
        case PP_ELIFNCTX:
        case PP_ELIFNDEF:
        case PP_ELIFNID:
        case PP_ELIFNIDN:
        case PP_ELIFNIDNI:
        case PP_ELIFNMACRO:
        case PP_ELIFNNUM:
        case PP_ELIFNSTR:
        case PP_ELIFNUM:
        case PP_ELIFSTR:
            if (!istk->conds)
                error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]);
            if (emitting(istk->conds->state)
                    || istk->conds->state == COND_NEVER)
                istk->conds->state = COND_NEVER;
            else
            {
                /*
                 * IMPORTANT: In the case of %if, we will already have
                 * called expand_mmac_params(); however, if we're
                 * processing an %elif we must have been in a
                 * non-emitting mode, which would have inhibited
                 * the normal invocation of expand_mmac_params().  Therefore,
                 * we have to do it explicitly here.
                 */
                j = if_condition(expand_mmac_params(tline->next), i);
                tline->next = NULL; /* it got freed */
                istk->conds->state =
                        j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
            }
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_ELSE:
            if (tline->next)
                error(ERR_WARNING, "trailing garbage after `%%else' ignored");
            if (!istk->conds)
                error(ERR_FATAL, "`%%else': no matching `%%if'");
            if (emitting(istk->conds->state)
                    || istk->conds->state == COND_NEVER)
                istk->conds->state = COND_ELSE_FALSE;
            else
                istk->conds->state = COND_ELSE_TRUE;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_ENDIF:
            if (tline->next)
                error(ERR_WARNING,
                        "trailing garbage after `%%endif' ignored");
            if (!istk->conds)
                error(ERR_FATAL, "`%%endif': no matching `%%if'");
            cond = istk->conds;
            istk->conds = cond->next;
            nasm_free(cond);
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_MACRO:
        case PP_IMACRO:
            if (defining)
                error(ERR_FATAL,
                        "`%%%smacro': already defining a macro",
                        (i == PP_IMACRO ? "i" : ""));
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tok_type_(tline, TOK_ID))
            {
                error(ERR_NONFATAL,
                        "`%%%smacro' expects a macro name",
                        (i == PP_IMACRO ? "i" : ""));
                return DIRECTIVE_FOUND;
            }
            defining = nasm_malloc(sizeof(MMacro));
            defining->name = nasm_strdup(tline->text);
            defining->casesense = (i == PP_MACRO);
            defining->plus = FALSE;
            defining->nolist = FALSE;
            defining->in_progress = FALSE;
            defining->rep_nest = NULL;
            tline = expand_smacro(tline->next);
            skip_white_(tline);
            if (!tok_type_(tline, TOK_NUMBER))
            {
                error(ERR_NONFATAL,
                        "`%%%smacro' expects a parameter count",
                        (i == PP_IMACRO ? "i" : ""));
                defining->nparam_min = defining->nparam_max = 0;
            }
            else
            {
                intn = nasm_readnum(tline->text, &j);
                defining->nparam_min = defining->nparam_max =
                    yasm_intnum_get_int(intn);
                yasm_intnum_destroy(intn);
                if (j)
                    error(ERR_NONFATAL,
                            "unable to parse parameter count `%s'",
                            tline->text);
            }
            if (tline && tok_is_(tline->next, "-"))
            {
                tline = tline->next->next;
                if (tok_is_(tline, "*"))
                    defining->nparam_max = INT_MAX;
                else if (!tok_type_(tline, TOK_NUMBER))
                    error(ERR_NONFATAL,
                            "`%%%smacro' expects a parameter count after `-'",
                            (i == PP_IMACRO ? "i" : ""));
                else
                {
                    intn = nasm_readnum(tline->text, &j);
                    defining->nparam_max = yasm_intnum_get_int(intn);
                    yasm_intnum_destroy(intn);
                    if (j)
                        error(ERR_NONFATAL,
                                "unable to parse parameter count `%s'",
                                tline->text);
                    if (defining->nparam_min > defining->nparam_max)
                        error(ERR_NONFATAL,
                                "minimum parameter count exceeds maximum");
                }
            }
            if (tline && tok_is_(tline->next, "+"))
            {
                tline = tline->next;
                defining->plus = TRUE;
            }
            if (tline && tok_type_(tline->next, TOK_ID) &&
                    !nasm_stricmp(tline->next->text, ".nolist"))
            {
                tline = tline->next;
                defining->nolist = TRUE;
            }
            mmac = mmacros[hash(defining->name)];
            while (mmac)
            {
                if (!strcmp(mmac->name, defining->name) &&
                        (mmac->nparam_min <= defining->nparam_max
                                || defining->plus)
                        && (defining->nparam_min <= mmac->nparam_max
                                || mmac->plus))
                {
                    error(ERR_WARNING,
                            "redefining multi-line macro `%s'",
                            defining->name);
                    break;
                }
                mmac = mmac->next;
            }
            /*
             * Handle default parameters.
             */
            if (tline && tline->next)
            {
                defining->dlist = tline->next;
                tline->next = NULL;
                count_mmac_params(defining->dlist, &defining->ndefs,
                        &defining->defaults);
            }
            else
            {
                defining->dlist = NULL;
                defining->defaults = NULL;
            }
            defining->expansion = NULL;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_ENDM:
        case PP_ENDMACRO:
            if (!defining)
            {
                error(ERR_NONFATAL, "`%s': not defining a macro",
                        tline->text);
                return DIRECTIVE_FOUND;
            }
            k = hash(defining->name);
            defining->next = mmacros[k];
            mmacros[k] = defining;
            defining = NULL;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_ROTATE:
            if (tline->next && tline->next->type == TOK_WHITESPACE)
                tline = tline->next;
            if (tline->next == NULL)
            {
                free_tlist(origline);
                error(ERR_NONFATAL, "`%%rotate' missing rotate count");
                return DIRECTIVE_FOUND;
            }
            t = expand_smacro(tline->next);
            tline->next = NULL;
            free_tlist(origline);
            tline = t;
            tptr = &t;
            tokval.t_type = TOKEN_INVALID;
            evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
            free_tlist(tline);
            if (!evalresult)
                return DIRECTIVE_FOUND;
            if (tokval.t_type)
                error(ERR_WARNING,
                        "trailing garbage after expression ignored");
            intn = yasm_expr_get_intnum(&evalresult, 0);
            if (!intn)
            {
                error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
                yasm_expr_destroy(evalresult);
                return DIRECTIVE_FOUND;
            }
            mmac = istk->mstk;
            while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
                mmac = mmac->next_active;
            if (!mmac)
            {
                error(ERR_NONFATAL,
                        "`%%rotate' invoked outside a macro call");
            } 
            else if (mmac->nparam == 0)
            {
                error(ERR_NONFATAL,
                        "`%%rotate' invoked within macro without parameters");
            }
            else
            {
                mmac->rotate = mmac->rotate + yasm_intnum_get_int(intn);
                
                if (mmac->rotate < 0)
                    mmac->rotate = 
                        mmac->nparam - (-mmac->rotate) % mmac->nparam;
                mmac->rotate %= mmac->nparam;
            }
            yasm_expr_destroy(evalresult);
            return DIRECTIVE_FOUND;

        case PP_REP:
            nolist = FALSE;
            do {
                tline = tline->next;
            } while (tok_type_(tline, TOK_WHITESPACE));

            if (tok_type_(tline, TOK_ID) &&
                nasm_stricmp(tline->text, ".nolist") == 0)
            {
                nolist = TRUE;
                do {
                    tline = tline->next;
                } while (tok_type_(tline, TOK_WHITESPACE));
            }

            if (tline)
            {
                t = expand_smacro(tline);
                tptr = &t;
                tokval.t_type = TOKEN_INVALID;
                evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
                if (!evalresult)
                {
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                if (tokval.t_type)
                    error(ERR_WARNING,
                          "trailing garbage after expression ignored");
                intn = yasm_expr_get_intnum(&evalresult, 0);
                if (!intn)
                {
                    error(ERR_NONFATAL, "non-constant value given to `%%rep'");
                    yasm_expr_destroy(evalresult);
                    return DIRECTIVE_FOUND;
                }
                i = (int)yasm_intnum_get_int(intn) + 1;
                yasm_expr_destroy(evalresult);
            }
            else
            {
                error(ERR_NONFATAL, "`%%rep' expects a repeat count");
                i = 0;
            }
            free_tlist(origline);

            tmp_defining = defining;
            defining = nasm_malloc(sizeof(MMacro));
            defining->name = NULL;      /* flags this macro as a %rep block */
            defining->casesense = 0;
            defining->plus = FALSE;
            defining->nolist = nolist;
            defining->in_progress = i;
            defining->nparam_min = defining->nparam_max = 0;
            defining->defaults = NULL;
            defining->dlist = NULL;
            defining->expansion = NULL;
            defining->next_active = istk->mstk;
            defining->rep_nest = tmp_defining;
            return DIRECTIVE_FOUND;

        case PP_ENDREP:
            if (!defining || defining->name)
            {
                error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
                return DIRECTIVE_FOUND;
            }

            /*
             * Now we have a "macro" defined - although it has no name
             * and we won't be entering it in the hash tables - we must
             * push a macro-end marker for it on to istk->expansion.
             * After that, it will take care of propagating itself (a
             * macro-end marker line for a macro which is really a %rep
             * block will cause the macro to be re-expanded, complete
             * with another macro-end marker to ensure the process
             * continues) until the whole expansion is forcibly removed
             * from istk->expansion by a %exitrep.
             */
            l = nasm_malloc(sizeof(Line));
            l->next = istk->expansion;
            l->finishes = defining;
            l->first = NULL;
            istk->expansion = l;

            istk->mstk = defining;

            list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
            tmp_defining = defining;
            defining = defining->rep_nest;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_EXITREP:
            /*
             * We must search along istk->expansion until we hit a
             * macro-end marker for a macro with no name. Then we set
             * its `in_progress' flag to 0.
             */
            for (l = istk->expansion; l; l = l->next)
                if (l->finishes && !l->finishes->name)
                    break;

            if (l)
                l->finishes->in_progress = 0;
            else
                error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_XDEFINE:
        case PP_IXDEFINE:
        case PP_DEFINE:
        case PP_IDEFINE:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tline || (tline->type != TOK_ID &&
                            (tline->type != TOK_PREPROC_ID ||
                                    tline->text[1] != '$')))
            {
                error(ERR_NONFATAL,
                        "`%%%s%sdefine' expects a macro identifier",
                        ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
                        ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }

            ctx = get_ctx(tline->text, FALSE);
            if (!ctx)
                smhead = &smacros[hash(tline->text)];
            else
                smhead = &ctx->localmac;
            mname = tline->text;
            last = tline;
            param_start = tline = tline->next;
            nparam = 0;

            /* Expand the macro definition now for %xdefine and %ixdefine */
            if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
                tline = expand_smacro(tline);

            if (tok_is_(tline, "("))
            {
                /*
                 * This macro has parameters.
                 */

                tline = tline->next;
                while (1)
                {
                    skip_white_(tline);
                    if (!tline)
                    {
                        error(ERR_NONFATAL, "parameter identifier expected");
                        free_tlist(origline);
                        return DIRECTIVE_FOUND;
                    }
                    if (tline->type != TOK_ID)
                    {
                        error(ERR_NONFATAL,
                                "`%s': parameter identifier expected",
                                tline->text);
                        free_tlist(origline);
                        return DIRECTIVE_FOUND;
                    }
                    tline->type = TOK_SMAC_PARAM + nparam++;
                    tline = tline->next;
                    skip_white_(tline);
                    if (tok_is_(tline, ","))
                    {
                        tline = tline->next;
                        continue;
                    }
                    if (!tok_is_(tline, ")"))
                    {
                        error(ERR_NONFATAL,
                                "`)' expected to terminate macro template");
                        free_tlist(origline);
                        return DIRECTIVE_FOUND;
                    }
                    break;
                }
                last = tline;
                tline = tline->next;
            }
            if (tok_type_(tline, TOK_WHITESPACE))
                last = tline, tline = tline->next;
            macro_start = NULL;
            last->next = NULL;
            t = tline;
            while (t)
            {
                if (t->type == TOK_ID)
                {
                    for (tt = param_start; tt; tt = tt->next)
                        if (tt->type >= TOK_SMAC_PARAM &&
                                !strcmp(tt->text, t->text))
                            t->type = tt->type;
                }
                tt = t->next;
                t->next = macro_start;
                macro_start = t;
                t = tt;
            }
            /*
             * Good. We now have a macro name, a parameter count, and a
             * token list (in reverse order) for an expansion. We ought
             * to be OK just to create an SMacro, store it, and let
             * free_tlist have the rest of the line (which we have
             * carefully re-terminated after chopping off the expansion
             * from the end).
             */
            if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE))
            {
                if (!smac)
                {
                    error(ERR_WARNING,
                            "single-line macro `%s' defined both with and"
                            " without parameters", mname);
                    free_tlist(origline);
                    free_tlist(macro_start);
                    return DIRECTIVE_FOUND;
                }
                else if (smac->level == Level)
                {
                    /*
                     * We're redefining in the same level, so we have to 
                     * take over an existing SMacro structure. This means 
                     * freeing what was already in it.
                     */
                    nasm_free(smac->name);
                    free_tlist(smac->expansion);
                }
                else
                {
                    smac = nasm_malloc(sizeof(SMacro));
                    smac->next = *smhead;
                    *smhead = smac;
                }
            }
            else
            {
                smac = nasm_malloc(sizeof(SMacro));
                smac->next = *smhead;
                *smhead = smac;
            }
            smac->name = nasm_strdup(mname);
            smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
            smac->nparam = nparam;
            smac->level = Level;
            smac->expansion = macro_start;
            smac->in_progress = FALSE;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_UNDEF:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tline || (tline->type != TOK_ID &&
                            (tline->type != TOK_PREPROC_ID ||
                                    tline->text[1] != '$')))
            {
                error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            if (tline->next)
            {
                error(ERR_WARNING,
                        "trailing garbage after macro name ignored");
            }

            /* Find the context that symbol belongs to */
            ctx = get_ctx(tline->text, FALSE);
            if (!ctx)
                smhead = &smacros[hash(tline->text)];
            else
                smhead = &ctx->localmac;

            mname = tline->text;

            /*
             * We now have a macro name... go hunt for it.
             */
            while (smacro_defined(ctx, mname, -1, &smac, 1))
            {
                /* Defined, so we need to find its predecessor and nuke it */
                SMacro **s;
                for (s = smhead; *s && *s != smac; s = &(*s)->next);
                if (*s)
                {
                    *s = smac->next;
                    nasm_free(smac->name);
                    free_tlist(smac->expansion);
                    nasm_free(smac);
                }
            }
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_STRLEN:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tline || (tline->type != TOK_ID &&
                            (tline->type != TOK_PREPROC_ID ||
                                    tline->text[1] != '$')))
            {
                error(ERR_NONFATAL,
                        "`%%strlen' expects a macro identifier as first parameter");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            ctx = get_ctx(tline->text, FALSE);
            if (!ctx)
                smhead = &smacros[hash(tline->text)];
            else
                smhead = &ctx->localmac;
            mname = tline->text;
            last = tline;
            tline = expand_smacro(tline->next);
            last->next = NULL;

            t = tline;
            while (tok_type_(t, TOK_WHITESPACE))
                t = t->next;
            /* t should now point to the string */
            if (t->type != TOK_STRING)
            {
                error(ERR_NONFATAL,
                        "`%%strlen` requires string as second parameter");
                free_tlist(tline);
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }

            macro_start = nasm_malloc(sizeof(*macro_start));
            macro_start->next = NULL;
            make_tok_num(macro_start,
                yasm_intnum_create_uint((unsigned long)(strlen(t->text) - 2)));
            macro_start->mac = NULL;

            /*
             * We now have a macro name, an implicit parameter count of
             * zero, and a numeric token to use as an expansion. Create
             * and store an SMacro.
             */
            if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN))
            {
                if (!smac)
                    error(ERR_WARNING,
                            "single-line macro `%s' defined both with and"
                            " without parameters", mname);
                else
                {
                    /*
                     * We're redefining, so we have to take over an
                     * existing SMacro structure. This means freeing
                     * what was already in it.
                     */
                    nasm_free(smac->name);
                    free_tlist(smac->expansion);
                }
            }
            else
            {
                smac = nasm_malloc(sizeof(SMacro));
                smac->next = *smhead;
                *smhead = smac;
            }
            smac->name = nasm_strdup(mname);
            smac->casesense = (i == PP_STRLEN);
            smac->nparam = 0;
            smac->level = 0;
            smac->expansion = macro_start;
            smac->in_progress = FALSE;
            free_tlist(tline);
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_SUBSTR:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tline || (tline->type != TOK_ID &&
                            (tline->type != TOK_PREPROC_ID ||
                                    tline->text[1] != '$')))
            {
                error(ERR_NONFATAL,
                        "`%%substr' expects a macro identifier as first parameter");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            ctx = get_ctx(tline->text, FALSE);
            if (!ctx)
                smhead = &smacros[hash(tline->text)];
            else
                smhead = &ctx->localmac;
            mname = tline->text;
            last = tline;
            tline = expand_smacro(tline->next);
            last->next = NULL;

            t = tline->next;
            while (tok_type_(t, TOK_WHITESPACE))
                t = t->next;

            /* t should now point to the string */
            if (t->type != TOK_STRING)
            {
                error(ERR_NONFATAL,
                        "`%%substr` requires string as second parameter");
                free_tlist(tline);
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }

            tt = t->next;
            tptr = &tt;
            tokval.t_type = TOKEN_INVALID;
            evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
            if (!evalresult)
            {
                free_tlist(tline);
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            intn = yasm_expr_get_intnum(&evalresult, 0);
            if (!intn)
            {
                error(ERR_NONFATAL, "non-constant value given to `%%substr`");
                free_tlist(tline);
                free_tlist(origline);
                yasm_expr_destroy(evalresult);
                return DIRECTIVE_FOUND;
            }

            macro_start = nasm_malloc(sizeof(*macro_start));
            macro_start->next = NULL;
            macro_start->text = nasm_strdup("'''");
            if (yasm_intnum_sign(intn) == 1
                    && yasm_intnum_get_uint(intn) < strlen(t->text) - 1)
            {
                macro_start->text[1] = t->text[yasm_intnum_get_uint(intn)];
            }
            else
            {
                macro_start->text[2] = '\0';
            }
            yasm_expr_destroy(evalresult);
            macro_start->type = TOK_STRING;
            macro_start->mac = NULL;

            /*
             * We now have a macro name, an implicit parameter count of
             * zero, and a numeric token to use as an expansion. Create
             * and store an SMacro.
             */
            if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR))
            {
                if (!smac)
                    error(ERR_WARNING,
                            "single-line macro `%s' defined both with and"
                            " without parameters", mname);
                else
                {
                    /*
                     * We're redefining, so we have to take over an
                     * existing SMacro structure. This means freeing
                     * what was already in it.
                     */
                    nasm_free(smac->name);
                    free_tlist(smac->expansion);
                }
            }
            else
            {
                smac = nasm_malloc(sizeof(SMacro));
                smac->next = *smhead;
                *smhead = smac;
            }
            smac->name = nasm_strdup(mname);
            smac->casesense = (i == PP_SUBSTR);
            smac->nparam = 0;
            smac->level = 0;
            smac->expansion = macro_start;
            smac->in_progress = FALSE;
            free_tlist(tline);
            free_tlist(origline);
            return DIRECTIVE_FOUND;


        case PP_ASSIGN:
        case PP_IASSIGN:
            tline = tline->next;
            skip_white_(tline);
            tline = expand_id(tline);
            if (!tline || (tline->type != TOK_ID &&
                            (tline->type != TOK_PREPROC_ID ||
                                    tline->text[1] != '$')))
            {
                error(ERR_NONFATAL,
                        "`%%%sassign' expects a macro identifier",
                        (i == PP_IASSIGN ? "i" : ""));
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            ctx = get_ctx(tline->text, FALSE);
            if (!ctx)
                smhead = &smacros[hash(tline->text)];
            else
                smhead = &ctx->localmac;
            mname = tline->text;
            last = tline;
            tline = expand_smacro(tline->next);
            last->next = NULL;

            t = tline;
            tptr = &t;
            tokval.t_type = TOKEN_INVALID;
            evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
            free_tlist(tline);
            if (!evalresult)
            {
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }

            if (tokval.t_type)
                error(ERR_WARNING,
                        "trailing garbage after expression ignored");

            intn = yasm_expr_get_intnum(&evalresult, 0);
            if (!intn)
            {
                error(ERR_NONFATAL,
                        "non-constant value given to `%%%sassign'",
                        (i == PP_IASSIGN ? "i" : ""));
                free_tlist(origline);
                yasm_expr_destroy(evalresult);
                return DIRECTIVE_FOUND;
            }

            macro_start = nasm_malloc(sizeof(*macro_start));
            macro_start->next = NULL;
            make_tok_num(macro_start, yasm_intnum_copy(intn));
            yasm_expr_destroy(evalresult);
            macro_start->mac = NULL;

            /*
             * We now have a macro name, an implicit parameter count of
             * zero, and a numeric token to use as an expansion. Create
             * and store an SMacro.
             */
            if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN))
            {
                if (!smac)
                    error(ERR_WARNING,
                            "single-line macro `%s' defined both with and"
                            " without parameters", mname);
                else
                {
                    /*
                     * We're redefining, so we have to take over an
                     * existing SMacro structure. This means freeing
                     * what was already in it.
                     */
                    nasm_free(smac->name);
                    free_tlist(smac->expansion);
                }
            }
            else
            {
                smac = nasm_malloc(sizeof(SMacro));
                smac->next = *smhead;
                *smhead = smac;
            }
            smac->name = nasm_strdup(mname);
            smac->casesense = (i == PP_ASSIGN);
            smac->nparam = 0;
            smac->level = 0;
            smac->expansion = macro_start;
            smac->in_progress = FALSE;
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        case PP_LINE:
            /*
             * Syntax is `%line nnn[+mmm] [filename]'
             */
            tline = tline->next;
            skip_white_(tline);
            if (!tok_type_(tline, TOK_NUMBER))
            {
                error(ERR_NONFATAL, "`%%line' expects line number");
                free_tlist(origline);
                return DIRECTIVE_FOUND;
            }
            intn = nasm_readnum(tline->text, &j);
            k = yasm_intnum_get_int(intn);
            yasm_intnum_destroy(intn);
            m = 1;
            tline = tline->next;
            if (tok_is_(tline, "+"))
            {
                tline = tline->next;
                if (!tok_type_(tline, TOK_NUMBER))
                {
                    error(ERR_NONFATAL, "`%%line' expects line increment");
                    free_tlist(origline);
                    return DIRECTIVE_FOUND;
                }
                intn = nasm_readnum(tline->text, &j);
                m = yasm_intnum_get_int(intn);
                yasm_intnum_destroy(intn);
                tline = tline->next;
            }
            skip_white_(tline);
            nasm_src_set_linnum(k);
            istk->lineinc = m;
            if (tline)
            {
                nasm_free(nasm_src_set_fname(detoken(tline, FALSE)));
            }
            free_tlist(origline);
            return DIRECTIVE_FOUND;

        default:
            error(ERR_FATAL,
                    "preprocessor directive `%s' not yet implemented",
                    directives[i]);
            break;
    }
    return DIRECTIVE_FOUND;
}

/*
 * Ensure that a macro parameter contains a condition code and
 * nothing else. Return the condition code index if so, or -1
 * otherwise.
 */
static int
find_cc(Token * t)
{
    Token *tt;
    int i, j, k, m;

    skip_white_(t);
    if (t->type != TOK_ID)
        return -1;
    tt = t->next;
    skip_white_(tt);
    if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
        return -1;

    i = -1;
    j = elements(conditions);
    while (j - i > 1)
    {
        k = (j + i) / 2;
        m = nasm_stricmp(t->text, conditions[k]);
        if (m == 0)
        {
            i = k;
            j = -2;
            break;
        }
        else if (m < 0)
        {
            j = k;
        }
        else
            i = k;
    }
    if (j != -2)
        return -1;
    return i;
}

/*
 * Expand MMacro-local things: parameter references (%0, %n, %+n,
 * %-n) and MMacro-local identifiers (%%foo).
 */
static Token *
expand_mmac_params(Token * tline)
{
    Token *t, *tt, **tail, *thead;

    tail = &thead;
    thead = NULL;

    while (tline)
    {
        if (tline->type == TOK_PREPROC_ID &&
                (((tline->text[1] == '+' || tline->text[1] == '-')
                                && tline->text[2]) || tline->text[1] == '%'
                        || (tline->text[1] >= '0' && tline->text[1] <= '9')))
        {
            char *text = NULL;
            int type = 0, cc;   /* type = 0 to placate optimisers */
            char tmpbuf[30];
            char *second_text = NULL;
            int n, i;
            MMacro *mac;

            t = tline;
            tline = tline->next;

            second_text = strchr(t->text, ':');

            mac = istk->mstk;
            while (mac && !mac->name)   /* avoid mistaking %reps for macros */
                mac = mac->next_active;
            if (!mac)
                error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
            else
            {
                if (second_text)
                {
                    int end = atoi(second_text+1)-1;
                    int is_fst = 1;
                    int k;
                    n = atoi(t->text + 1)-1;
                    if (end < 0)
                        end += mac->nparam;

                    for (k = n; k <= end; k++)
                    {
                        if (k >= mac->nparam)
                            tt = NULL;
                        else
                        {
                            if (mac->nparam > 1)
                                k = (k + mac->rotate) % mac->nparam;
                            tt = mac->params[k];
                        }
                        if (tt)
                        {
                            if (!is_fst && mac->paramlen[k])
                            {
                                *tail = new_Token(NULL, TOK_OTHER, ",", 0);
                                tail = &(*tail)->next;
                            }
                            if (mac->paramlen[k])
                                is_fst = 0;
                            for (i = 0; i < mac->paramlen[k]; i++)
                            {
                                *tail =
                                        new_Token(NULL, tt->type, tt->text,
                                        0);
                                tail = &(*tail)->next;
                                tt = tt->next;
                            }
                        }
                        text = NULL;    /* we've done it here */
                    }
                }
                else
                {
                    switch (t->text[1])
                    {
                            /*
                             * We have to make a substitution of one of the
                             * forms %1, %-1, %+1, %%foo, %0.
                             */
                        case '0':
                            type = TOK_NUMBER;
                            sprintf(tmpbuf, "%ld", mac->nparam);
                            text = nasm_strdup(tmpbuf);
                            break;
                        case '%':
                            type = TOK_ID;
                            sprintf(tmpbuf, "..@%lu.", mac->unique);
                            text = nasm_strcat(tmpbuf, t->text + 2);
                            break;
                        case '-':
                            n = atoi(t->text + 2) - 1;
                            if (n >= mac->nparam)
                                tt = NULL;
                            else
                            {
                                if (mac->nparam > 1)
                                    n = (n + mac->rotate) % mac->nparam;
                                tt = mac->params[n];
                            }
                            cc = find_cc(tt);
                            if (cc == -1)
                            {
                                error(ERR_NONFATAL,
                                        "macro parameter %d is not a condition code",
                                        n + 1);
                                text = NULL;
                            }
                            else
                            {
                                type = TOK_ID;
                                if (inverse_ccs[cc] == -1)
                                {
                                    error(ERR_NONFATAL,
                                            "condition code `%s' is not invertible",
                                            conditions[cc]);
                                    text = NULL;
                                }
                                else
                                    text =
                                            nasm_strdup(conditions[inverse_ccs
                                                     [cc]]);
                            }
                            break;
                        case '+':
                            n = atoi(t->text + 2) - 1;
                            if (n >= mac->nparam)
                                tt = NULL;
                            else
                            {
                                if (mac->nparam > 1)
                                    n = (n + mac->rotate) % mac->nparam;
                                tt = mac->params[n];
                            }
                            cc = find_cc(tt);
                            if (cc == -1)
                            {
                                error(ERR_NONFATAL,
                                        "macro parameter %d is not a condition code",
                                        n + 1);
                                text = NULL;
                            }
                            else
                            {
                                type = TOK_ID;
                                text = nasm_strdup(conditions[cc]);
                            }
                            break;
                        default:
                            n = atoi(t->text + 1) - 1;
                            if (n >= mac->nparam)
                                tt = NULL;
                            else
                            {
                                if (mac->nparam > 1)
                                    n = (n + mac->rotate) % mac->nparam;
                                tt = mac->params[n];
                            }
                            if (tt)
                            {
                                for (i = 0; i < mac->paramlen[n]; i++)
                                {
                                    *tail =
                                            new_Token(NULL, tt->type, tt->text,
                                            0);
                                    tail = &(*tail)->next;
                                    tt = tt->next;
                                }
                            }
                            text = NULL;    /* we've done it here */
                            break;
                    }
                }
            }

            if (!text)
            {
                delete_Token(t);
            }
            else
            {
                *tail = t;
                tail = &t->next;
                t->type = type;
                nasm_free(t->text);
                t->text = text;
                t->mac = NULL;
            }
            continue;
        }
        else
        {
            t = *tail = tline;
            tline = tline->next;
            t->mac = NULL;
            tail = &t->next;
        }
    }
    *tail = NULL;
    t = thead;
    for (; t && (tt = t->next) != NULL; t = t->next)
        switch (t->type)
        {
            case TOK_WHITESPACE:
                if (tt->type == TOK_WHITESPACE)
                {
                    t->next = delete_Token(tt);
                }
                break;
            case TOK_ID:
                if (tt->type == TOK_ID || tt->type == TOK_NUMBER)
                {
                    char *tmp = nasm_strcat(t->text, tt->text);
                    nasm_free(t->text);
                    t->text = tmp;
                    t->next = delete_Token(tt);
                }
                break;
            case TOK_NUMBER:
                if (tt->type == TOK_NUMBER)
                {
                    char *tmp = nasm_strcat(t->text, tt->text);
                    nasm_free(t->text);
                    t->text = tmp;
                    t->next = delete_Token(tt);
                }
                break;
        }

    return thead;
}

/*
 * Expand all single-line macro calls made in the given line.
 * Return the expanded version of the line. The original is deemed
 * to be destroyed in the process. (In reality we'll just move
 * Tokens from input to output a lot of the time, rather than
 * actually bothering to destroy and replicate.)
 */
static Token *
expand_smacro(Token * tline)
{
    Token *t, *tt, *mstart, **tail, *thead;
    SMacro *head = NULL, *m;
    Token **params;
    int *paramsize;
    int nparam, sparam, brackets, rescan;
    Token *org_tline = tline;
    Context *ctx;
    char *mname;

    /*
     * Trick: we should avoid changing the start token pointer since it can
     * be contained in "next" field of other token. Because of this
     * we allocate a copy of first token and work with it; at the end of
     * routine we copy it back
     */
    if (org_tline)
    {
        tline =
                new_Token(org_tline->next, org_tline->type, org_tline->text,
                0);
        tline->mac = org_tline->mac;
        nasm_free(org_tline->text);
        org_tline->text = NULL;
    }

  again:
    tail = &thead;
    thead = NULL;

    while (tline)
    {                           /* main token loop */
        if ((mname = tline->text))
        {
            /* if this token is a local macro, look in local context */
            if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
                ctx = get_ctx(mname, TRUE);
            else
                ctx = NULL;
            if (!ctx)
                head = smacros[hash(mname)];
            else
                head = ctx->localmac;
            /*
             * We've hit an identifier. As in is_mmacro below, we first
             * check whether the identifier is a single-line macro at
             * all, then think about checking for parameters if
             * necessary.
             */
            for (m = head; m; m = m->next)
                if (!mstrcmp(m->name, mname, m->casesense))
                    break;
            if (m)
            {
                mstart = tline;
                params = NULL;
                paramsize = NULL;
                if (m->nparam == 0)
                {
                    /*
                     * Simple case: the macro is parameterless. Discard the
                     * one token that the macro call took, and push the
                     * expansion back on the to-do stack.
                     */
                    if (!m->expansion)
                    {
                        if (!strcmp("__FILE__", m->name))
                        {
                            long num = 0;
                            nasm_src_get(&num, &(tline->text));
                            nasm_quote(&(tline->text));
                            tline->type = TOK_STRING;
                            continue;
                        }
                        if (!strcmp("__LINE__", m->name))
                        {
                            nasm_free(tline->text);
                            make_tok_num(tline, yasm_intnum_create_int(nasm_src_get_linnum()));
                            continue;
                        }
                        tline = delete_Token(tline);
                        continue;
                    }
                }
                else
                {
                    /*
                     * Complicated case: at least one macro with this name
                     * exists and takes parameters. We must find the
                     * parameters in the call, count them, find the SMacro
                     * that corresponds to that form of the macro call, and
                     * substitute for the parameters when we expand. What a
                     * pain.
                     */
                    /*tline = tline->next;
                    skip_white_(tline);*/
                    do {
                        t = tline->next;
                        while (tok_type_(t, TOK_SMAC_END))
                        {
                            t->mac->in_progress = FALSE;
                            t->text = NULL;
                            t = tline->next = delete_Token(t);
                        }
                        tline = t;
                    } while (tok_type_(tline, TOK_WHITESPACE));
                    if (!tok_is_(tline, "("))
                    {
                        /*
                         * This macro wasn't called with parameters: ignore
                         * the call. (Behaviour borrowed from gnu cpp.)
                         */
                        tline = mstart;
                        m = NULL;
                    }
                    else
                    {
                        int paren = 0;
                        int white = 0;
                        brackets = 0;
                        nparam = 0;
                        sparam = PARAM_DELTA;
                        params = nasm_malloc(sparam * sizeof(Token *));
                        params[0] = tline->next;
                        paramsize = nasm_malloc(sparam * sizeof(int));
                        paramsize[0] = 0;
                        while (TRUE)
                        {       /* parameter loop */
                            /*
                             * For some unusual expansions
                             * which concatenates function call
                             */
                            t = tline->next;
                            while (tok_type_(t, TOK_SMAC_END))
                            {
                                t->mac->in_progress = FALSE;
                                t->text = NULL;
                                t = tline->next = delete_Token(t);
                            }
                            tline = t;

                            if (!tline)
                            {
                                error(ERR_NONFATAL,
                                        "macro call expects terminating `)'");
                                break;
                            }
                            if (tline->type == TOK_WHITESPACE
                                    && brackets <= 0)
                            {
                                if (paramsize[nparam])
                                    white++;
                                else
                                    params[nparam] = tline->next;
                                continue;       /* parameter loop */
                            }
                            if (tline->type == TOK_OTHER
                                    && tline->text[1] == 0)
                            {
                                char ch = tline->text[0];
                                if (ch == ',' && !paren && brackets <= 0)
                                {
                                    if (++nparam >= sparam)
                                    {
                                        sparam += PARAM_DELTA;
                                        params = nasm_realloc(params,
                                                sparam * sizeof(Token *));
                                        paramsize = nasm_realloc(paramsize,
                                                sparam * sizeof(int));
                                    }
                                    params[nparam] = tline->next;
                                    paramsize[nparam] = 0;
                                    white = 0;
                                    continue;   /* parameter loop */
                                }
                                if (ch == '{' &&
                                        (brackets > 0 || (brackets == 0 &&
                                                        !paramsize[nparam])))
                                {
                                    if (!(brackets++))
                                    {
                                        params[nparam] = tline->next;
                                        continue;       /* parameter loop */
                                    }
                                }
                                if (ch == '}' && brackets > 0)
                                    if (--brackets == 0)
                                    {
                                        brackets = -1;
                                        continue;       /* parameter loop */
                                    }
                                if (ch == '(' && !brackets)
                                    paren++;
                                if (ch == ')' && brackets <= 0)
                                    if (--paren < 0)
                                        break;
                            }
                            if (brackets < 0)
                            {
                                brackets = 0;
                                error(ERR_NONFATAL, "braces do not "
                                        "enclose all of macro parameter");
                            }
                            paramsize[nparam] += white + 1;
                            white = 0;
                        }       /* parameter loop */
                        nparam++;
                        while (m && (m->nparam != nparam ||
                                        mstrcmp(m->name, mname,
                                                m->casesense)))
                            m = m->next;
                        if (!m)
                            error(ERR_WARNING | ERR_WARN_MNP,
                                    "macro `%s' exists, "
                                    "but not taking %d parameters",
                                    mstart->text, nparam);
                    }
                }
                if (m && m->in_progress)
                    m = NULL;
                if (!m)         /* in progess or didn't find '(' or wrong nparam */
                {
                    /* 
                     * Design question: should we handle !tline, which
                     * indicates missing ')' here, or expand those
                     * macros anyway, which requires the (t) test a few
                     * lines down?  
                     */
                    nasm_free(params);
                    nasm_free(paramsize);
                    tline = mstart;
                }
                else
                {
                    /*
                     * Expand the macro: we are placed on the last token of the
                     * call, so that we can easily split the call from the
                     * following tokens. We also start by pushing an SMAC_END
                     * token for the cycle removal.
                     */
                    t = tline;
                    if (t)
                    {
                        tline = t->next;
                        t->next = NULL;
                    }
                    tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
                    tt->mac = m;
                    m->in_progress = TRUE;
                    tline = tt;
                    for (t = m->expansion; t; t = t->next)
                    {
                        if (t->type >= TOK_SMAC_PARAM)
                        {
                            Token *pcopy = tline, **ptail = &pcopy;
                            Token *ttt, *pt;
                            int i;

                            ttt = params[t->type - TOK_SMAC_PARAM];
                            for (i = paramsize[t->type - TOK_SMAC_PARAM];
                                    --i >= 0;)
                            {
                                pt = *ptail =
                                        new_Token(tline, ttt->type, ttt->text,
                                        0);
                                ptail = &pt->next;
                                ttt = ttt->next;
                            }
                            tline = pcopy;
                        }
                        else
                        {
                            tt = new_Token(tline, t->type, t->text, 0);
                            tline = tt;
                        }
                    }

                    /*
                     * Having done that, get rid of the macro call, and clean
                     * up the parameters.
                     */
                    nasm_free(params);
                    nasm_free(paramsize);
                    free_tlist(mstart);
                    continue;   /* main token loop */
                }
            }
        }

        if (tline->type == TOK_SMAC_END)
        {
            tline->mac->in_progress = FALSE;
            tline = delete_Token(tline);
        }
        else
        {
            t = *tail = tline;
            tline = tline->next;
            t->mac = NULL;
            t->next = NULL;
            tail = &t->next;
        }
    }

    /*
     * Now scan the entire line and look for successive TOK_IDs that resulted
     * after expansion (they can't be produced by tokenise()). The successive
     * TOK_IDs should be concatenated.
     * Also we look for %+ tokens and concatenate the tokens before and after
     * them (without white spaces in between).
     */
    t = thead;
    rescan = 0;
    while (t)
    {
        while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
            t = t->next;
        if (!t || !t->next)
            break;
        if (t->next->type == TOK_ID ||
                t->next->type == TOK_PREPROC_ID ||
                t->next->type == TOK_NUMBER)
        {
            char *p = nasm_strcat(t->text, t->next->text);
            nasm_free(t->text);
            t->next = delete_Token(t->next);
            t->text = p;
            rescan = 1;
        }
        else if (t->next->type == TOK_WHITESPACE && t->next->next &&
                t->next->next->type == TOK_PREPROC_ID &&
                strcmp(t->next->next->text, "%+") == 0)
        {
            /* free the next whitespace, the %+ token and next whitespace */
            int i;
            for (i = 1; i <= 3; i++)
            {
                if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
                    break;
                t->next = delete_Token(t->next);
            }                   /* endfor */
        }
        else
            t = t->next;
    }
    /* If we concatenaded something, re-scan the line for macros */
    if (rescan)
    {
        tline = thead;
        goto again;
    }

    if (org_tline)
    {
        if (thead)
        {
            *org_tline = *thead;
            /* since we just gave text to org_line, don't free it */
            thead->text = NULL;
            delete_Token(thead);
        }
        else
        {
            /* the expression expanded to empty line;
               we can't return NULL for some reasons
               we just set the line to a single WHITESPACE token. */
            memset(org_tline, 0, sizeof(*org_tline));
            org_tline->text = NULL;
            org_tline->type = TOK_WHITESPACE;
        }
        thead = org_tline;
    }

    return thead;
}

/*
 * Similar to expand_smacro but used exclusively with macro identifiers
 * right before they are fetched in. The reason is that there can be
 * identifiers consisting of several subparts. We consider that if there
 * are more than one element forming the name, user wants a expansion,
 * otherwise it will be left as-is. Example:
 *
 *      %define %$abc cde
 *
 * the identifier %$abc will be left as-is so that the handler for %define
 * will suck it and define the corresponding value. Other case:
 *
 *      %define _%$abc cde
 *
 * In this case user wants name to be expanded *before* %define starts
 * working, so we'll expand %$abc into something (if it has a value;
 * otherwise it will be left as-is) then concatenate all successive
 * PP_IDs into one.
 */
static Token *
expand_id(Token * tline)
{
    Token *cur, *oldnext = NULL;

    if (!tline || !tline->next)
        return tline;

    cur = tline;
    while (cur->next &&
            (cur->next->type == TOK_ID ||
        cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER))
        cur = cur->next;

    /* If identifier consists of just one token, don't expand */
    if (cur == tline)
        return tline;

    if (cur)
    {
        oldnext = cur->next;    /* Detach the tail past identifier */
        cur->next = NULL;       /* so that expand_smacro stops here */
    }

    tline = expand_smacro(tline);

    if (cur)
    {
        /* expand_smacro possibly changhed tline; re-scan for EOL */
        cur = tline;
        while (cur && cur->next)
            cur = cur->next;
        if (cur)
            cur->next = oldnext;
    }

    return tline;
}

/*
 * Determine whether the given line constitutes a multi-line macro
 * call, and return the MMacro structure called if so. Doesn't have
 * to check for an initial label - that's taken care of in
 * expand_mmacro - but must check numbers of parameters. Guaranteed
 * to be called with tline->type == TOK_ID, so the putative macro
 * name is easy to find.
 */
static MMacro *
is_mmacro(Token * tline, Token *** params_array)
{
    MMacro *head, *m;
    Token **params;
    int nparam;

    head = mmacros[hash(tline->text)];

    /*
     * Efficiency: first we see if any macro exists with the given
     * name. If not, we can return NULL immediately. _Then_ we
     * count the parameters, and then we look further along the
     * list if necessary to find the proper MMacro.
     */
    for (m = head; m; m = m->next)
        if (!mstrcmp(m->name, tline->text, m->casesense))
            break;
    if (!m)
        return NULL;

    /*
     * OK, we have a potential macro. Count and demarcate the
     * parameters.
     */
    count_mmac_params(tline->next, &nparam, &params);

    /*
     * So we know how many parameters we've got. Find the MMacro
     * structure that handles this number.
     */
    while (m)
    {
        if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max))
        {
            /*
             * This one is right. Just check if cycle removal
             * prohibits us using it before we actually celebrate...
             */
            if (m->in_progress)
            {
#if 0
                error(ERR_NONFATAL,
                        "self-reference in multi-line macro `%s'", m->name);
#endif
                nasm_free(params);
                return NULL;
            }
            /*
             * It's right, and we can use it. Add its default
             * parameters to the end of our list if necessary.
             */
            if (m->defaults && nparam < m->nparam_min + m->ndefs)
            {
                params =
                        nasm_realloc(params,
                        ((m->nparam_min + m->ndefs + 1) * sizeof(*params)));
                while (nparam < m->nparam_min + m->ndefs)
                {
                    params[nparam] = m->defaults[nparam - m->nparam_min];
                    nparam++;
                }
            }
            /*
             * If we've gone over the maximum parameter count (and
             * we're in Plus mode), ignore parameters beyond
             * nparam_max.
             */
            if (m->plus && nparam > m->nparam_max)
                nparam = m->nparam_max;
            /*
             * Then terminate the parameter list, and leave.
             */
            if (!params)
            {                   /* need this special case */
                params = nasm_malloc(sizeof(*params));
                nparam = 0;
            }
            params[nparam] = NULL;
            *params_array = params;
            return m;
        }
        /*
         * This one wasn't right: look for the next one with the
         * same name.
         */
        for (m = m->next; m; m = m->next)
            if (!mstrcmp(m->name, tline->text, m->casesense))
                break;
    }

    /*
     * After all that, we didn't find one with the right number of
     * parameters. Issue a warning, and fail to expand the macro.
     */
    error(ERR_WARNING | ERR_WARN_MNP,
            "macro `%s' exists, but not taking %d parameters",
            tline->text, nparam);
    nasm_free(params);
    return NULL;
}

/*
 * Expand the multi-line macro call made by the given line, if
 * there is one to be expanded. If there is, push the expansion on
 * istk->expansion and return 1. Otherwise return 0.
 */
static int
expand_mmacro(Token * tline)
{
    Token *startline = tline;
    Token *label = NULL;
    int dont_prepend = 0;
    Token **params, *t, *tt;
    MMacro *m;
    Line *l, *ll;
    int i, nparam;
    long *paramlen;

    t = tline;
    skip_white_(t);
/*    if (!tok_type_(t, TOK_ID))  Lino 02/25/02 */
    if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
        return 0;
    m = is_mmacro(t, &params);
    if (!m)
    {
        Token *last;
        /*
         * We have an id which isn't a macro call. We'll assume
         * it might be a label; we'll also check to see if a
         * colon follows it. Then, if there's another id after
         * that lot, we'll check it again for macro-hood.
         */
        label = last = t;
        t = t->next;
        if (tok_type_(t, TOK_WHITESPACE))
            last = t, t = t->next;
        if (tok_is_(t, ":"))
        {
            dont_prepend = 1;
            last = t, t = t->next;
            if (tok_type_(t, TOK_WHITESPACE))
                last = t, t = t->next;
        }
        if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
            return 0;
        last->next = NULL;
        tline = t;
    }

    /*
     * Fix up the parameters: this involves stripping leading and
     * trailing whitespace, then stripping braces if they are
     * present.
     */
    for (nparam = 0; params[nparam]; nparam++)
        ;
    paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;

    for (i = 0; params[i]; i++)
    {
        int brace = FALSE;
        int comma = (!m->plus || i < nparam - 1);

        t = params[i];
        skip_white_(t);
        if (tok_is_(t, "{"))
            t = t->next, brace = TRUE, comma = FALSE;
        params[i] = t;
        paramlen[i] = 0;
        while (t)
        {
            if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
                break;          /* ... because we have hit a comma */
            if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
                break;          /* ... or a space then a comma */
            if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
                break;          /* ... or a brace */
            t = t->next;
            paramlen[i]++;
        }
    }

    /*
     * OK, we have a MMacro structure together with a set of
     * parameters. We must now go through the expansion and push
     * copies of each Line on to istk->expansion. Substitution of
     * parameter tokens and macro-local tokens doesn't get done
     * until the single-line macro substitution process; this is
     * because delaying them allows us to change the semantics
     * later through %rotate.
     *
     * First, push an end marker on to istk->expansion, mark this
     * macro as in progress, and set up its invocation-specific
     * variables.
     */
    ll = nasm_malloc(sizeof(Line));
    ll->next = istk->expansion;
    ll->finishes = m;
    ll->first = NULL;
    istk->expansion = ll;

    m->in_progress = TRUE;
    m->params = params;
    m->iline = tline;
    m->nparam = nparam;
    m->rotate = 0;
    m->paramlen = paramlen;
    m->unique = unique++;
    m->lineno = 0;

    m->next_active = istk->mstk;
    istk->mstk = m;

    for (l = m->expansion; l; l = l->next)
    {
        Token **tail;

        ll = nasm_malloc(sizeof(Line));
        ll->finishes = NULL;
        ll->next = istk->expansion;
        istk->expansion = ll;
        tail = &ll->first;

        for (t = l->first; t; t = t->next)
        {
            Token *x = t;
            if (t->type == TOK_PREPROC_ID &&
                    t->text[1] == '0' && t->text[2] == '0')
            {
                dont_prepend = -1;
                x = label;
                if (!x)
                    continue;
            }
            tt = *tail = new_Token(NULL, x->type, x->text, 0);
            tail = &tt->next;
        }
        *tail = NULL;
    }

    /*
     * If we had a label, push it on as the first line of
     * the macro expansion.
     */
    if (label)
    {
        if (dont_prepend < 0)
            free_tlist(startline);
        else
        {
            ll = nasm_malloc(sizeof(Line));
            ll->finishes = NULL;
            ll->next = istk->expansion;
            istk->expansion = ll;
            ll->first = startline;
            if (!dont_prepend)
            {
                while (label->next)
                    label = label->next;
                label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
            }
        }
    }

    list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);

    return 1;
}

/*
 * Since preprocessor always operates only on the line that didn't
 * arrive yet, we should always use ERR_OFFBY1. Also since user
 * won't want to see same error twice (preprocessing is done once
 * per pass) we will want to show errors only during pass one.
 */
static void
error(int severity, const char *fmt, ...)
{
    va_list arg;
    char buff[1024];

    /* If we're in a dead branch of IF or something like it, ignore the error */
    if (istk && istk->conds && !emitting(istk->conds->state))
        return;

    va_start(arg, fmt);
#ifdef HAVE_VSNPRINTF
    vsnprintf(buff, sizeof(buff), fmt, arg);
#else
    vsprintf(buff, fmt, arg);
#endif
    va_end(arg);

    if (istk && istk->mstk && istk->mstk->name)
        _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
                istk->mstk->lineno, buff);
    else 
        _error(severity | ERR_PASS1, "%s", buff);
}

static void
pp_reset(FILE *f, const char *file, int apass, efunc errfunc, evalfunc eval,
        ListGen * listgen)
{
    int h;

    first_fp = f;
    _error = errfunc;
    cstk = NULL;
    istk = nasm_malloc(sizeof(Include));
    istk->next = NULL;
    istk->conds = NULL;
    istk->expansion = NULL;
    istk->mstk = NULL;
    istk->fp = f;
    istk->fname = NULL;
    nasm_free(nasm_src_set_fname(nasm_strdup(file)));
    nasm_src_set_linnum(0);
    istk->lineinc = 1;
    defining = NULL;
    nested_mac_count = 0;
    nested_rep_count = 0;
    for (h = 0; h < NHASH; h++)
    {
        mmacros[h] = NULL;
        smacros[h] = NULL;
    }
    unique = 0;
    if (tasm_compatible_mode) {
        pp_extra_stdmac(tasm_compat_macros);
    }
    list = listgen;
    evaluate = eval;
    pass = apass;
    first_line = 1;
}

/*
 * Nasty hack: here we push the contents of `predef' on
 * to the top-level expansion stack, since this is the
 * most convenient way to implement the pre-include and
 * pre-define features.
 */
static void
poke_predef(Line *predef_lines)
{
    Line *pd, *l;
    Token *head, **tail, *t;

    for (pd = predef_lines; pd; pd = pd->next)
    {
        head = NULL;
        tail = &head;
        for (t = pd->first; t; t = t->next)
        {
            *tail = new_Token(NULL, t->type, t->text, 0);
            tail = &(*tail)->next;
        }
        l = nasm_malloc(sizeof(Line));
        l->next = istk->expansion;
        l->first = head;
        l->finishes = FALSE;
        istk->expansion = l;
    }
}

static char *
pp_getline(void)
{
    char *line;
    Token *tline;

    while (1)
    {
        /*
         * Fetch a tokenised line, either from the macro-expansion
         * buffer or from the input file.
         */
        tline = NULL;

        if (first_line)
        {
            /* Reverse order */
            poke_predef(predef);
            poke_predef(stddef);
            poke_predef(builtindef);
            first_line = 0;
        }

        if (!istk)
            return NULL;
        while (istk->expansion && istk->expansion->finishes)
        {
            Line *l = istk->expansion;
            if (!l->finishes->name && l->finishes->in_progress > 1)
            {
                Line *ll;

                /*
                 * This is a macro-end marker for a macro with no
                 * name, which means it's not really a macro at all
                 * but a %rep block, and the `in_progress' field is
                 * more than 1, meaning that we still need to
                 * repeat. (1 means the natural last repetition; 0
                 * means termination by %exitrep.) We have
                 * therefore expanded up to the %endrep, and must
                 * push the whole block on to the expansion buffer
                 * again. We don't bother to remove the macro-end
                 * marker: we'd only have to generate another one
                 * if we did.
                 */
                l->finishes->in_progress--;
                for (l = l->finishes->expansion; l; l = l->next)
                {
                    Token *t, *tt, **tail;

                    ll = nasm_malloc(sizeof(Line));
                    ll->next = istk->expansion;
                    ll->finishes = NULL;
                    ll->first = NULL;
                    tail = &ll->first;

                    for (t = l->first; t; t = t->next)
                    {
                        if (t->text || t->type == TOK_WHITESPACE)
                        {
                            tt = *tail = new_Token(NULL, t->type, t->text, 0);
                            tail = &tt->next;
                        }
                    }

                    istk->expansion = ll;
                }
            }
            else
            {
                /*
                 * Check whether a `%rep' was started and not ended
                 * within this macro expansion. This can happen and
                 * should be detected. It's a fatal error because
                 * I'm too confused to work out how to recover
                 * sensibly from it.
                 */
                if (defining)
                {
                    if (defining->name)
                        error(ERR_PANIC, "defining with name in expansion");
                    else if (istk->mstk->name)
                        error(ERR_FATAL, "`%%rep' without `%%endrep' within"
                                " expansion of macro `%s'", istk->mstk->name);
                }

                /*
                 * FIXME:  investigate the relationship at this point between
                 * istk->mstk and l->finishes
                 */
                {
                    MMacro *m = istk->mstk;
                    istk->mstk = m->next_active;
                    if (m->name)
                    {
                        /*
                         * This was a real macro call, not a %rep, and
                         * therefore the parameter information needs to
                         * be freed.
                         */
                        nasm_free(m->params);
                        free_tlist(m->iline);
                        nasm_free(m->paramlen);
                        l->finishes->in_progress = FALSE;
                    }
                    else
                        free_mmacro(m);
                }
                istk->expansion = l->next;
                nasm_free(l);
                list->downlevel(LIST_MACRO);
            }
        }
        while (1)
        {                       /* until we get a line we can use */

            if (istk->expansion)
            {                   /* from a macro expansion */
                char *p;
                Line *l = istk->expansion;
                if (istk->mstk)
                    istk->mstk->lineno++;
                tline = l->first;
                istk->expansion = l->next;
                nasm_free(l);
                p = detoken(tline, FALSE);
                list->line(LIST_MACRO, p);
                nasm_free(p);
                break;
            }
            line = read_line();
            if (line)
            {                   /* from the current input file */
                line = prepreproc(line);
                tline = tokenise(line);
                nasm_free(line);
                break;
            }
            /*
             * The current file has ended; work down the istk
             */
            {
                Include *i = istk;
                if (i->fp != first_fp)
                    fclose(i->fp);
                if (i->conds)
                    error(ERR_FATAL, "expected `%%endif' before end of file");
                /* only set line and file name if there's a next node */
                if (i->next) 
                {
                    nasm_src_set_linnum(i->lineno);
                    nasm_free(nasm_src_set_fname(nasm_strdup(i->fname)));
                }
                istk = i->next;
                list->downlevel(LIST_INCLUDE);
                nasm_free(i);
                if (!istk)
                    return NULL;
                if (istk->expansion && istk->expansion->finishes)
                    break;
            }
        }

        /*
         * We must expand MMacro parameters and MMacro-local labels
         * _before_ we plunge into directive processing, to cope
         * with things like `%define something %1' such as STRUC
         * uses. Unless we're _defining_ a MMacro, in which case
         * those tokens should be left alone to go into the
         * definition; and unless we're in a non-emitting
         * condition, in which case we don't want to meddle with
         * anything.
         */
        if (!defining && !(istk->conds && !emitting(istk->conds->state)))
            tline = expand_mmac_params(tline);

        /*
         * Check the line to see if it's a preprocessor directive.
         */
        if (do_directive(tline) == DIRECTIVE_FOUND)
        {
            continue;
        }
        else if (defining)
        {
            /*
             * We're defining a multi-line macro. We emit nothing
             * at all, and just
             * shove the tokenised line on to the macro definition.
             */
            Line *l = nasm_malloc(sizeof(Line));
            l->next = defining->expansion;
            l->first = tline;
            l->finishes = FALSE;
            defining->expansion = l;
            continue;
        }
        else if (istk->conds && !emitting(istk->conds->state))
        {
            /*
             * We're in a non-emitting branch of a condition block.
             * Emit nothing at all, not even a blank line: when we
             * emerge from the condition we'll give a line-number
             * directive so we keep our place correctly.
             */
            free_tlist(tline);
            continue;
        }
        else if (istk->mstk && !istk->mstk->in_progress)
        {
            /*
             * We're in a %rep block which has been terminated, so
             * we're walking through to the %endrep without
             * emitting anything. Emit nothing at all, not even a
             * blank line: when we emerge from the %rep block we'll
             * give a line-number directive so we keep our place
             * correctly.
             */
            free_tlist(tline);
            continue;
        }
        else
        {
            tline = expand_smacro(tline);
            if (!expand_mmacro(tline))
            {
                /*
                 * De-tokenise the line again, and emit it.
                 */
                if (tasm_compatible_mode)
                    tline = tasm_join_tokens(tline);

                line = detoken(tline, TRUE);
                free_tlist(tline);
                break;
            }
            else
            {
                continue;       /* expand_mmacro calls free_tlist */
            }
        }
    }

    return line;
}

static void
pp_cleanup(int pass_)
{
    int h;

    if (pass_ == 1)
    {
        if (defining)
        {
            error(ERR_NONFATAL, "end of file while still defining macro `%s'",
                    defining->name);
            free_mmacro(defining);
        }
        return;
    }
    while (cstk)
        ctx_pop();
    for (h = 0; h < NHASH; h++)
    {
        while (mmacros[h])
        {
            MMacro *m = mmacros[h];
            mmacros[h] = mmacros[h]->next;
            free_mmacro(m);
        }
        while (smacros[h])
        {
            SMacro *s = smacros[h];
            smacros[h] = smacros[h]->next;
            nasm_free(s->name);
            free_tlist(s->expansion);
            nasm_free(s);
        }
    }
    while (istk)
    {
        Include *i = istk;
        istk = istk->next;
        if (i->fp != first_fp)
            fclose(i->fp);
        nasm_free(i->fname);
        nasm_free(i);
    }
    while (cstk)
        ctx_pop();
    if (pass_ == 0)
        {
                free_llist(builtindef);
                free_llist(stddef);
                free_llist(predef);
                builtindef = NULL;
                stddef = NULL;
                predef = NULL;
                freeTokens = NULL;
                delete_Blocks();
                blocks.next = NULL;
                blocks.chunk = NULL;
        }
}

void
pp_pre_include(const char *fname)
{
    Token *inc, *space, *name;
    Line *l;

    name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
    space = new_Token(name, TOK_WHITESPACE, NULL, 0);
    inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);

    l = nasm_malloc(sizeof(Line));
    l->next = predef;
    l->first = inc;
    l->finishes = FALSE;
    predef = l;
}

void
pp_pre_define(char *definition)
{
    Token *def, *space;
    Line *l;
    char *equals;

    equals = strchr(definition, '=');
    space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
    def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
    if (equals)
        *equals = ' ';
    space->next = tokenise(definition);
    if (equals)
        *equals = '=';

    l = nasm_malloc(sizeof(Line));
    l->next = predef;
    l->first = def;
    l->finishes = FALSE;
    predef = l;
}

void
pp_pre_undefine(char *definition)
{
    Token *def, *space;
    Line *l;

    space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
    def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
    space->next = tokenise(definition);

    l = nasm_malloc(sizeof(Line));
    l->next = predef;
    l->first = def;
    l->finishes = FALSE;
    predef = l;
}

void
pp_builtin_define(char *definition)
{
    Token *def, *space;
    Line *l;
    char *equals;

    equals = strchr(definition, '=');
    space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
    def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
    if (equals)
        *equals = ' ';
    space->next = tokenise(definition);
    if (equals)
        *equals = '=';

    l = nasm_malloc(sizeof(Line));
    l->next = builtindef;
    l->first = def;
    l->finishes = FALSE;
    builtindef = l;
}

void
pp_extra_stdmac(const char **macros)
{
    const char **lp;

    for (lp=macros; *lp; lp++)
    {
        char *macro;
        Token *t;
        Line *l;

        macro = nasm_strdup(*lp);
        t = tokenise(macro);
        nasm_free(macro);

        l = nasm_malloc(sizeof(Line));
        l->next = stddef;
        l->first = t;
        l->finishes = FALSE;
        stddef = l;
    }
}

static void
make_tok_num(Token * tok, yasm_intnum *val)
{
    tok->text = yasm_intnum_get_str(val);
    tok->type = TOK_NUMBER;
    yasm_intnum_destroy(val);
}

Preproc nasmpp = {
    pp_reset,
    pp_getline,
    pp_cleanup
};