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
|
/* gasp.c - Gnu assembler preprocessor main program.
Copyright (C) 1994 Free Software Foundation, Inc.
Written by Steve and Judy Chamberlain of Cygnus Support,
sac@cygnus.com
This file is part of GASP, the GNU Assembler Preprocessor.
GASP is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GASP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GASP; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/*
This program translates the input macros and stuff into a form
suitable for gas to consume.
gasp [-sdhau] [-c char] [-o <outfile>] <infile>*
-s copy source to output
-c <char> comments are started with <char> instead of !
-u allow unreasonable stuff
-p print line numbers
-d print debugging stats
-s semi colons start comments
-a use alternate syntax
Pseudo ops can start with or without a .
Labels have to be in first column.
Macro arg parameters subsituted by name, don't need the &.
String can start with ' too.
Strings can be surrounded by <..>
A %<exp> in a string evaluates the expression
Literal char in a string with !
*/
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include "host.h"
#include "libiberty.h"
char *program_version = "1.2";
#define MAX_INCLUDES 30 /* Maximum include depth */
#define MAX_REASONABLE 1000 /* Maximum number of expansions */
int unreasonable; /* -u on command line */
int stats; /* -d on command line */
int print_line_number; /* -p flag on command line */
int copysource; /* -c flag on command line */
int warnings; /* Number of WARNINGs generated so far. */
int errors; /* Number of ERRORs generated so far. */
int fatals; /* Number of fatal ERRORs generated so far (either 0 or 1). */
int alternate = 0; /* -a on command line */
char comment_char = '!';
int radix = 10; /* Default radix */
int had_end; /* Seen .END */
/* The output stream */
FILE *outfile;
/* Forward declarations. */
static int condass_lookup_name();
static int condass_on();
static int get();
static int get_and_process();
static int get_token();
static int getstring();
static int include_next_index();
static int macro_op();
static int linecount();
static int process_pseudo_op();
static void include_pop();
static void include_print_where_line();
/* string blocks
I had a couple of choices when deciding upon this data structure.
gas uses null terminated strings for all its internal work. This
often means that parts of the program that want to examine
substrings have to manipulate the data in the string to do the
right thing (a common operation is to single out a bit of text by
saving away the character after it, nulling it out, operating on
the substring and then replacing the character which was under the
null). This is a pain and I remember a load of problems that I had with
code in gas which almost got this right. Also, it's harder to grow and
allocate null terminated strings efficiently.
Obstacks provide all the functionality needed, but are too
complicated, hence the sb.
An sb is allocated by the caller, and is initialzed to point to an
sb_element. sb_elements are kept on a free lists, and used when
needed, replaced onto the free list when unused.
*/
#define max_power_two 30 /* don't allow strings more than
2^max_power_two long */
/* structure of an sb */
typedef struct sb
{
char *ptr; /* points to the current block. */
int len; /* how much is used. */
int pot; /* the maximum length is 1<<pot */
struct le *item;
}
sb;
/* Structure of the free list object of an sb */
typedef struct le
{
struct le *next;
int size;
char data[1];
}
sb_element;
/* The free list */
typedef struct
{
sb_element *size[max_power_two];
} sb_list_vector;
sb_list_vector free_list;
int string_count[max_power_two];
/* the attributes of each character are stored as a bit pattern
chartype, which gives us quick tests. */
#define FIRSTBIT 1
#define NEXTBIT 2
#define SEPBIT 4
#define WHITEBIT 8
#define COMMENTBIT 16
#define ISCOMMENTCHAR(x) (chartype[(unsigned)(x)] & COMMENTBIT)
#define ISFIRSTCHAR(x) (chartype[(unsigned)(x)] & FIRSTBIT)
#define ISNEXTCHAR(x) (chartype[(unsigned)(x)] & NEXTBIT)
#define ISSEP(x) (chartype[(unsigned)(x)] & SEPBIT)
#define ISWHITE(x) (chartype[(unsigned)(x)] & WHITEBIT)
static char chartype[256];
/* Conditional assembly uses the `ifstack'. Each aif pushes another
entry onto the stack, and sets the on flag if it should. The aelse
sets hadelse, and toggles on. An aend pops a level. We limit to
100 levels of nesting, not because we're facists pigs with read
only minds, but because more than 100 levels of nesting is probably
a bug in the user's macro structure. */
#define IFNESTING 100
struct
{
int on; /* is the level being output */
int hadelse; /* has an aelse been seen */
}
ifstack[IFNESTING];
int ifi;
/* The final and intermediate results of expression evaluation are kept in
exp_t's. Note that a symbol is not an sb, but a pointer into the input
line. It must be coped somewhere safe before the next line is read in. */
typedef struct
{
char *name;
int len;
}
symbol;
typedef struct
{
int value; /* constant part */
symbol add_symbol; /* name part */
symbol sub_symbol; /* name part */
}
exp_t;
/* Hashing is done in a pretty standard way. A hash_table has a
pointer to a vector of pointers to hash_entrys, and the size of the
vector. A hash_entry contains a union of all the info we like to
store in hash table. If there is a hash collision, hash_entries
with the same hash are kept in a chain. */
/* What the data in a hash_entry means */
typedef enum
{
hash_integer, /* name->integer mapping */
hash_string, /* name->string mapping */
hash_macro, /* name is a macro */
hash_formal /* name is a formal argument */
} hash_type;
typedef struct hs
{
sb key; /* symbol name */
hash_type type; /* symbol meaning */
union
{
sb s;
int i;
struct macro_struct *m;
struct formal_struct *f;
} value;
struct hs *next; /* next hash_entry with same hash key */
} hash_entry;
typedef struct
{
hash_entry **table;
int size;
} hash_table;
/* Structures used to store macros.
Each macro knows its name and included text. It gets built with a
list of formal arguments, and also keeps a hash table which points
into the list to speed up formal search. Each formal knows its
name and its default value. Each time the macro is expanded, the
formals get the actual values attatched to them. */
/* describe the formal arguments to a macro */
typedef struct formal_struct
{
struct formal_struct *next; /* next formal in list */
sb name; /* name of the formal */
sb def; /* the default value */
sb actual; /* the actual argument (changed on each expansion) */
int index; /* the index of the formal 0..formal_count-1 */
}
formal_entry;
/* describe the macro. */
typedef struct macro_struct
{
sb sub; /* substitution text. */
int formal_count; /* number of formal args. */
formal_entry *formals; /* pointer to list of formal_structs */
hash_table formal_hash; /* hash table of formals. */
}
macro_entry;
/* how we nest files and expand macros etc.
we keep a stack of of include_stack structs. each include file
pushes a new level onto the stack. we keep an sb with a pushback
too. unget chars are pushed onto the pushback sb, getchars first
checks the pushback sb before reading from the input stream.
small things are expanded by adding the text of the item onto the
pushback sb. larger items are grown by pushing a new level and
allocating the entire pushback buf for the item. each time
something like a macro is expanded, the stack index is changed. we
can then perform an exitm by popping all entries off the stack with
the same stack index. if we're being reasonable, we can detect
recusive expansion by checking the index is reasonably small.
*/
typedef enum
{
include_file, include_repeat, include_while, include_macro
} include_type;
struct include_stack
{
sb pushback; /* current pushback stream */
int pushback_index; /* next char to read from stream */
FILE *handle; /* open file */
sb name; /* name of file */
int linecount; /* number of lines read so far */
include_type type;
int index; /* index of this layer */
}
include_stack[MAX_INCLUDES];
struct include_stack *sp;
#define isp (sp - include_stack)
#define dsize 5
void include_print_where_line ();
#define FATAL(x) \
do { include_print_where_line (stderr); fprintf x ; fatals++; quit(); } while(0)
#define ERROR(x) \
do { include_print_where_line (stderr); fprintf x; errors++; } while(0)
#define WARNING(x) \
do { include_print_where_line (stderr); fprintf x; warnings++;} while(0)
/* exit the program and return the right ERROR code. */
void
quit ()
{
int exitcode;
if (fatals + errors)
exitcode = 1;
else
exitcode = 0;
if (stats)
{
int i;
for (i = 0; i < max_power_two; i++)
{
fprintf (stderr, "strings size %8d : %d\n", 1<<i, string_count[i]);
}
}
exit (exitcode);
}
/* this program is about manipulating strings.
they are managed in things called `sb's which is an abbreviation
for string buffers. an sb has to be created, things can be glued
on to it, and at the end of it's life it should be freed. the
contents should never be pointed at whilst it is still growing,
since it could be moved at any time
eg:
sb_new (&foo);
sb_grow... (&foo,...);
use foo->ptr[*];
sb_kill (&foo);
*/
/* initializes an sb. */
void
sb_build (ptr, size)
sb *ptr;
int size;
{
/* see if we can find one to allocate */
sb_element *e;
if (size > max_power_two)
{
FATAL ((stderr, "string longer than %d bytes requested.\n",
1 << max_power_two));
}
e = free_list.size[size];
if (!e)
{
/* nothing there, allocate one and stick into the free list */
e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
e->next = free_list.size[size];
e->size = 1 << size;
free_list.size[size] = e;
string_count[size]++;
}
/* remove from free list */
free_list.size[size] = e->next;
/* copy into callers world */
ptr->ptr = e->data;
ptr->pot = size;
ptr->len = 0;
ptr->item = e;
}
static void
sb_new (ptr)
sb *ptr;
{
sb_build (ptr, dsize);
}
/* deallocate the sb at ptr */
static
void
sb_kill (ptr)
sb *ptr;
{
/* return item to free list */
ptr->item->next = free_list.size[ptr->pot];
free_list.size[ptr->pot] = ptr->item;
}
/* add the sb at s to the end of the sb at ptr */
static void sb_check ();
static
void
sb_add_sb (ptr, s)
sb *ptr;
sb *s;
{
sb_check (ptr, s->len);
memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
ptr->len += s->len;
}
/* make sure that the sb at ptr has room for another len characters,
and grow it if it doesn't. */
static void
sb_check (ptr, len)
sb *ptr;
int len;
{
if (ptr->len + len >= 1 << ptr->pot)
{
sb tmp;
int pot = ptr->pot;
while (ptr->len + len >= 1 << pot)
pot++;
sb_build (&tmp, pot);
sb_add_sb (&tmp, ptr);
sb_kill (ptr);
*ptr = tmp;
}
}
/* make the sb at ptr point back to the beginning. */
static void
sb_reset (ptr)
sb *ptr;
{
ptr->len = 0;
}
/* add character c to the end of the sb at ptr. */
void
sb_add_char (ptr, c)
sb *ptr;
char c;
{
sb_check (ptr, 1);
ptr->ptr[ptr->len++] = c;
}
/* add null terminated string s to the end of sb at ptr. */
static void
sb_add_string (ptr, s)
sb *ptr;
char *s;
{
int len = strlen (s);
sb_check (ptr, len);
memcpy (ptr->ptr + ptr->len, s, len);
ptr->len += len;
}
/* add string at s of length len to sb at ptr */
static void
sb_add_buffer (ptr, s, len)
sb *ptr;
char *s;
int len;
{
sb_check (ptr, len);
memcpy (ptr->ptr + ptr->len, s, len);
ptr->len += len;
}
/* print the sb at ptr to the output file */
static
void
sb_print (ptr)
sb *ptr;
{
int i;
int nc = 0;
for (i = 0; i < ptr->len; i++)
{
if (nc)
{
fprintf (outfile, ",");
}
fprintf (outfile, "%d", ptr->ptr[i]);
nc = 1;
}
}
static
void
sb_print_at (idx, ptr)
int idx;
sb *ptr;
{
int i;
for (i = idx; i < ptr->len; i++)
putc (ptr->ptr[i], outfile);
}
/* put a null at the end of the sb at in and return the start of the
string, so that it can be used as an arg to printf %s. */
static
char *
sb_name (in)
sb *in;
{
/* stick a null on the end of the string */
sb_add_char (in, 0);
return in->ptr;
}
/* start at the index idx into the string in sb at ptr and skip
whitespace. return the index of the first non whitespace character */
static int
sb_skip_white (idx, ptr)
int idx;
sb *ptr;
{
while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
idx++;
return idx;
}
/* start at the index idx into the sb at ptr. skips whitespace,
a comma and any following whitespace. returnes the index of the
next character. */
static int
sb_skip_comma (idx, ptr)
int idx;
sb *ptr;
{
while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
idx++;
if (idx < ptr->len
&& ptr->ptr[idx] == ',')
idx++;
while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
idx++;
return idx;
}
/* hash table maintenance. */
/* build a new hash table with size buckets, and fill in the info at ptr. */
static void
hash_new_table (size, ptr)
int size;
hash_table *ptr;
{
int i;
ptr->size = size;
ptr->table = (hash_entry **) xmalloc (size * (sizeof (hash_entry *)));
/* Fill with null-pointer, not zero-bit-pattern. */
for (i = 0; i < size; i++)
ptr->table[i] = 0;
}
/* calculate and return the hash value of the sb at key. */
static int
hash (key)
sb *key;
{
int k = 0x1234;
int i;
char *p = key->ptr;
for (i = 0; i < key->len; i++)
{
k ^= (k << 2) ^ *p;
p++;
}
return k & 0xf0fff;
}
/* lookup key in hash_table tab, if present, then return it, otherwise
build a new one and fill it with hash_integer. */
static
hash_entry *
hash_create (tab, key)
hash_table *tab;
sb *key;
{
int k = hash (key) % tab->size;
hash_entry *p;
hash_entry **table = tab->table;
p = table[k];
while (1)
{
if (!p)
{
hash_entry *n = (hash_entry *) xmalloc (sizeof (hash_entry));
n->next = table[k];
sb_new (&n->key);
sb_add_sb (&n->key, key);
table[k] = n;
n->type = hash_integer;
return n;
}
if (strncmp (table[k]->key.ptr, key->ptr, key->len) == 0)
{
return p;
}
p = p->next;
}
}
/* add sb name with key into hash_table tab. if replacing old value
and again, then ERROR. */
static
void
hash_add_to_string_table (tab, key, name, again)
hash_table *tab;
sb *key;
sb *name;
int again;
{
hash_entry *ptr = hash_create (tab, key);
if (ptr->type == hash_integer)
{
sb_new (&ptr->value.s);
}
if (ptr->value.s.len)
{
if (!again)
ERROR ((stderr, "redefintion not allowed"));
}
ptr->type = hash_string;
sb_reset (&ptr->value.s);
sb_add_sb (&ptr->value.s, name);
}
/* add integer name to hash_table tab with sb key. */
static
void
hash_add_to_int_table (tab, key, name)
hash_table *tab;
sb *key;
int name;
{
hash_entry *ptr = hash_create (tab, key);
ptr->value.i = name;
}
/* lookup sb key in hash_table tab. if found return hash_entry result,
else 0. */
static
hash_entry *
hash_lookup (tab, key)
hash_table *tab;
sb *key;
{
int k = hash (key) % tab->size;
hash_entry **table = tab->table;
hash_entry *p = table[k];
while (p)
{
if (p->key.len == key->len
&& strncmp (p->key.ptr, key->ptr, key->len) == 0)
return p;
p = p->next;
}
return 0;
}
/* expressions
are handled in a really simple recursive decent way. each bit of
the machine takes an index into an sb and a pointer to an exp_t,
modifies the *exp_t and returns the index of the first character
past the part of the expression parsed.
expression precedence:
( )
unary + - ~
* /
+ -
&
| ~
*/
/* make sure that the exp_t at term is constant, if not the give the op ERROR. */
static
void
checkconst (op, term)
char op;
exp_t *term;
{
if (term->add_symbol.len
|| term->sub_symbol.len)
{
ERROR ((stderr, "the %c operator cannot take non-absolute arguments.\n", op));
}
}
/* turn the number in string at idx into a number of base,
fill in ptr and return the index of the first character not in the
number. */
static
int
sb_strtol (idx, string, base, ptr)
int idx;
sb *string;
int base;
int *ptr;
{
int value = 0;
idx = sb_skip_white (idx, string);
while (idx < string->len)
{
int ch = string->ptr[idx];
int dig = 0;
if (isdigit (ch))
dig = ch - '0';
else if (ch >= 'a' && ch <= 'f')
dig = ch - 'a' + 10;
else if (ch >= 'A' && ch <= 'F')
dig = ch - 'A' + 10;
else
break;
if (dig >= base)
break;
value = value * base + dig;
idx++;
}
*ptr = value;
return idx;
}
static int level_5 ();
int
level_0 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
lhs->add_symbol.len = 0;
lhs->add_symbol.name = 0;
lhs->sub_symbol.len = 0;
lhs->sub_symbol.name = 0;
idx = sb_skip_white (idx, string);
lhs->value = 0;
if (isdigit (string->ptr[idx]))
{
idx = sb_strtol (idx, string, 10, &lhs->value);
}
else if (ISFIRSTCHAR (string->ptr[idx]))
{
int len = 0;
lhs->add_symbol.name = string->ptr + idx;
while (idx < string->len && ISNEXTCHAR (string->ptr[idx]))
{
idx++;
len++;
}
lhs->add_symbol.len = len;
}
else if (string->ptr[idx] == '"')
{
sb acc;
sb_new (&acc);
ERROR ((stderr, "string where expression expected.\n"));
idx = getstring (idx, string, &acc);
sb_kill (&acc);
}
else
{
ERROR ((stderr, "can't find primary in expression.\n"));
idx++;
}
return sb_skip_white (idx, string);
}
static int
level_1 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
idx = sb_skip_white (idx, string);
switch (string->ptr[idx])
{
case '+':
idx = level_1 (idx + 1, string, lhs);
break;
case '~':
idx = level_1 (idx + 1, string, lhs);
checkconst ('~', lhs);
lhs->value = ~lhs->value;
break;
case '-':
{
symbol t;
idx = level_1 (idx + 1, string, lhs);
lhs->value = -lhs->value;
t = lhs->add_symbol;
lhs->add_symbol = lhs->sub_symbol;
lhs->sub_symbol = t;
break;
}
case '(':
idx++;
idx = level_5 (sb_skip_white (idx, string), string, lhs);
if (string->ptr[idx] != ')')
ERROR ((stderr, "misplaced closing parens.\n"));
else
idx++;
break;
default:
idx = level_0 (idx, string, lhs);
break;
}
return sb_skip_white (idx, string);
}
static int
level_2 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
exp_t rhs;
idx = level_1 (idx, string, lhs);
while (idx < string->len && (string->ptr[idx] == '*'
|| string->ptr[idx] == '/'))
{
char op = string->ptr[idx++];
idx = level_1 (idx, string, &rhs);
switch (op)
{
case '*':
checkconst ('*', lhs);
checkconst ('*', &rhs);
lhs->value *= rhs.value;
break;
case '/':
checkconst ('/', lhs);
checkconst ('/', &rhs);
if (rhs.value == 0)
ERROR ((stderr, "attempt to divide by zero.\n"));
else
lhs->value /= rhs.value;
break;
}
}
return sb_skip_white (idx, string);
}
static int
level_3 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
exp_t rhs;
idx = level_2 (idx, string, lhs);
while (idx < string->len
&& (string->ptr[idx] == '+'
|| string->ptr[idx] == '-'))
{
char op = string->ptr[idx++];
idx = level_2 (idx, string, &rhs);
switch (op)
{
case '+':
lhs->value += rhs.value;
if (lhs->add_symbol.name && rhs.add_symbol.name)
{
ERROR ((stderr, "can't add two relocatable expressions\n"));
}
/* change nn+symbol to symbol + nn */
if (rhs.add_symbol.name)
{
lhs->add_symbol = rhs.add_symbol;
}
break;
case '-':
lhs->value -= rhs.value;
lhs->sub_symbol = rhs.add_symbol;
break;
}
}
return sb_skip_white (idx, string);
}
static int
level_4 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
exp_t rhs;
idx = level_3 (idx, string, lhs);
while (idx < string->len &&
string->ptr[idx] == '&')
{
char op = string->ptr[idx++];
idx = level_3 (idx, string, &rhs);
switch (op)
{
case '&':
checkconst ('&', lhs);
checkconst ('&', &rhs);
lhs->value &= rhs.value;
break;
}
}
return sb_skip_white (idx, string);
}
static int
level_5 (idx, string, lhs)
int idx;
sb *string;
exp_t *lhs;
{
exp_t rhs;
idx = level_4 (idx, string, lhs);
while (idx < string->len
&& (string->ptr[idx] == '|' || string->ptr[idx] == '~'))
{
char op = string->ptr[idx++];
idx = level_4 (idx, string, &rhs);
switch (op)
{
case '|':
checkconst ('|', lhs);
checkconst ('|', &rhs);
lhs->value |= rhs.value;
break;
case '~':
checkconst ('~', lhs);
checkconst ('~', &rhs);
lhs->value ^= rhs.value;
break;
}
}
return sb_skip_white (idx, string);
}
/* parse the expression at offset idx into string, fill up res with
the result. return the index of the first char past the expression.
*/
static int
exp_parse (idx, string, res)
int idx;
sb *string;
exp_t *res;
{
return level_5 (sb_skip_white (idx, string), string, res);
}
/* turn the expression at exp into text and glue it onto the end of
string. */
static void
exp_string (exp, string)
exp_t *exp;
sb *string;
{
int np = 0;
int ad = 0;
sb_reset (string);
if (exp->add_symbol.len)
{
sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
np = 1;
ad = 1;
}
if (exp->value)
{
char buf[20];
if (np)
sb_add_char (string, '+');
sprintf (buf, "%d", exp->value);
sb_add_string (string, buf);
np = 1;
ad = 1;
}
if (exp->sub_symbol.len)
{
sb_add_char (string, '-');
sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
np = 0;
ad = 1;
}
if (!ad)
sb_add_char (string, '0');
}
/* parse the expression at offset idx into sb in, return the value in val.
if the expression is not constant, give ERROR emsg. returns the index
of the first character past the end of the expression. */
static int
exp_get_abs (emsg, idx, in, val)
char *emsg;
int idx;
sb *in;
int *val;
{
exp_t res;
idx = exp_parse (idx, in, &res);
if (res.add_symbol.len || res.sub_symbol.len)
ERROR ((stderr, emsg));
*val = res.value;
return idx;
}
sb label; /* current label parsed from line */
hash_table assign_hash_table; /* hash table for all assigned variables */
hash_table keyword_hash_table; /* hash table for keyword */
hash_table vars; /* hash table for eq variables */
#define in_comment ';'
#if 1
void
strip_comments (out)
sb *out;
{
char *s = out->ptr;
int i = 0;
for (i = 0; i < out->len; i++)
{
if (ISCOMMENTCHAR(s[i]))
{
out->len = i;
return;
}
}
}
#endif
/* push back character ch so that it can be read again. */
void
unget (ch)
int ch;
{
if (ch == '\n')
{
sp->linecount--;
}
if (sp->pushback_index)
sp->pushback_index--;
else
sb_add_char (&sp->pushback, ch);
}
/* push the sb ptr onto the include stack, with the given name, type and index. */
static
void
include_buf (name, ptr, type, index)
sb *name;
sb *ptr;
include_type type;
int index;
{
sp++;
if (sp - include_stack >= MAX_INCLUDES)
FATAL ((stderr, "unreasonable nesting.\n"));
sb_new (&sp->name);
sb_add_sb (&sp->name, name);
sp->handle = 0;
sp->linecount = 1;
sp->pushback_index = 0;
sp->type = type;
sp->index = index;
sb_new (&sp->pushback);
sb_add_sb (&sp->pushback, ptr);
}
/* used in ERROR messages, print info on where the include stack is onto file. */
static
void
include_print_where_line (file)
FILE *file;
{
struct include_stack *p = include_stack + 1;
while (p <= sp)
{
fprintf (file, "%s:%d ", sb_name (&p->name), p->linecount - ((p == sp) ? 1 : 0));
p++;
}
}
/* used in listings, print the line number onto file. */
static void
include_print_line (file)
FILE *file;
{
int n;
struct include_stack *p = include_stack + 1;
n = fprintf (file, "%4d", p->linecount);
p++;
while (p <= sp)
{
n += fprintf (file, ".%d", p->linecount);
p++;
}
while (n < 8 * 3)
{
fprintf (file, " ");
n++;
}
}
/* read a line from the top of the include stack into sb in. */
static int
get_line (in)
sb *in;
{
int online = 0;
int more = 1;
if (copysource)
{
putc (comment_char, outfile);
if (print_line_number)
include_print_line (outfile);
}
while (1)
{
int ch = get ();
while (ch == '\r')
ch = get ();
if (ch == EOF)
{
if (online)
{
WARNING ((stderr, "End of file not at start of line.\n"));
if (copysource)
putc ('\n', outfile);
ch = '\n';
}
else
more = 0;
break;
}
if (copysource)
{
putc (ch, outfile);
}
if (ch == '\n')
{
ch = get ();
online = 0;
if (ch == '+')
{
/* continued line */
if (copysource)
{
putc (comment_char, outfile);
putc ('+', outfile);
}
ch = get ();
}
else
{
if (ch != EOF)
unget (ch);
break;
}
}
else
{
sb_add_char (in, ch);
}
online++;
}
return more;
}
/* find a label from sb in and put it in out. */
static int
grab_label (in, out)
sb *in;
sb *out;
{
int i = 0;
sb_reset (out);
if (ISFIRSTCHAR (in->ptr[i]))
{
sb_add_char (out, in->ptr[i]);
i++;
while ((ISNEXTCHAR (in->ptr[i])
|| in->ptr[i] == '\\'
|| in->ptr[i] == '&')
&& i < in->len)
{
sb_add_char (out, in->ptr[i]);
i++;
}
}
return i;
}
/* find all strange base stuff and turn into decimal. also
find all the other numbers and convert them from the default radix */
static void
change_base (idx, in, out)
int idx;
sb *in;
sb *out;
{
char buffer[20];
while (idx < in->len)
{
if (idx < in->len - 1 && in->ptr[idx + 1] == '\'')
{
int base;
int value;
switch (in->ptr[idx])
{
case 'b':
case 'B':
base = 2;
break;
case 'q':
case 'Q':
base = 8;
break;
case 'h':
case 'H':
base = 16;
break;
case 'd':
case 'D':
base = 10;
break;
default:
ERROR ((stderr, "Illegal base character %c.\n", in->ptr[idx]));
base = 10;
break;
}
idx = sb_strtol (idx + 2, in, base, &value);
sprintf (buffer, "%d", value);
sb_add_string (out, buffer);
}
else if (ISFIRSTCHAR (in->ptr[idx]))
{
/* copy entire names through quickly */
sb_add_char (out, in->ptr[idx]);
idx++;
while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
{
sb_add_char (out, in->ptr[idx]);
idx++;
}
}
else if (isdigit (in->ptr[idx]))
{
int value;
/* all numbers must start with a digit, let's chew it and
spit out decimal */
idx = sb_strtol (idx, in, radix, &value);
sprintf (buffer, "%d", value);
sb_add_string (out, buffer);
/* skip all undigsested letters */
while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
{
sb_add_char (out, in->ptr[idx]);
idx++;
}
}
else
{
/* nothing special, just pass it through */
sb_add_char (out, in->ptr[idx]);
idx++;
}
}
}
/* .end */
static void
do_end ()
{
had_end = 1;
}
/* .assign */
static void
do_assign (again, idx, in)
int again;
int idx;
sb *in;
{
/* stick label in symbol table with following value */
exp_t e;
sb acc;
sb_new (&acc);
idx = exp_parse (idx, in, &e);
exp_string (&e, &acc);
hash_add_to_string_table (&assign_hash_table, &label, &acc, again);
sb_kill (&acc);
}
/* .radix [b|q|d|h] */
static
void
do_radix (ptr)
sb *ptr;
{
int idx = sb_skip_white (0, ptr);
switch (ptr->ptr[idx])
{
case 'B':
case 'b':
radix = 2;
break;
case 'q':
case 'Q':
radix = 8;
break;
case 'd':
case 'D':
radix = 10;
break;
case 'h':
case 'H':
radix = 16;
break;
default:
ERROR ((stderr, "radix is %c must be one of b, q, d or h", radix));
}
}
/* Parse off a .b, .w or .l */
static int
get_opsize (idx, in, size)
int idx;
sb *in;
int *size;
{
*size = 4;
if (in->ptr[idx] == '.')
{
idx++;
}
switch (in->ptr[idx])
{
case 'b':
case 'B':
*size = 1;
break;
case 'w':
case 'W':
*size = 2;
break;
case 'l':
case 'L':
*size = 4;
break;
case ' ':
case '\t':
break;
default:
ERROR ((stderr, "size must be one of b, w or l, is %c.\n", in->ptr[idx]));
break;
}
idx++;
return idx;
}
static
int eol(idx, line)
int idx;
sb *line;
{
idx = sb_skip_white (idx, line);
if (idx < line->len
&& ISCOMMENTCHAR(line->ptr[idx]))
return 1;
if (idx >= line->len)
return 1;
return 0;
}
/* .data [.b|.w|.l] <data>*
or d[bwl] <data>* */
static void
do_data (idx, in, size)
int idx;
sb *in;
int size;
{
int opsize = 4;
char *opname;
sb acc;
sb_new (&acc);
if (!size)
{
idx = get_opsize (idx, in, &opsize);
}
else {
opsize = size;
}
switch (opsize)
{
case 4:
opname = ".long";
break;
case 2:
opname = ".short";
break;
case 1:
opname = ".byte";
break;
}
fprintf (outfile, "%s\t", opname);
idx = sb_skip_white (idx, in);
if (alternate
&& idx < in->len
&& in->ptr[idx] == '"')
{
int i;
idx = getstring (idx, in, &acc);
for (i = 0; i < acc.len; i++)
{
if (i)
fprintf(outfile,",");
fprintf (outfile, "%d", acc.ptr[i]);
}
}
else
{
while (!eol (idx, in))
{
exp_t e;
idx = exp_parse (idx, in, &e);
exp_string (&e, &acc);
sb_add_char (&acc, 0);
fprintf (outfile, acc.ptr);
if (idx < in->len && in->ptr[idx] == ',')
{
fprintf (outfile, ",");
idx++;
}
}
}
sb_kill (&acc);
sb_print_at (idx, in);
fprintf (outfile, "\n");
}
/* .datab [.b|.w|.l] <repeat>,<fill> */
static void
do_datab (idx, in)
int idx;
sb *in;
{
int opsize;
int repeat;
int fill;
idx = get_opsize (idx, in, &opsize);
idx = exp_get_abs ("datab repeat must be constant.\n", idx, in, &repeat);
idx = sb_skip_comma (idx, in);
idx = exp_get_abs ("datab data must be absolute.\n", idx, in, &fill);
fprintf (outfile, ".fill\t%d,%d,%d\n", repeat, opsize, fill);
}
/* .align <size> */
void
do_align (idx, in)
int idx;
sb *in;
{
int al;
idx = exp_get_abs ("align needs absolute expression.\n", idx, in, &al);
if (al != 1
&& al != 2
&& al != 4)
WARNING ((stderr, "alignment must be one of 1, 2 or 4.\n"));
fprintf (outfile, ".align %d\n", al);
}
/* .res[.b|.w|.l] <size> */
void
do_res (idx, in, type)
int idx;
sb *in;
char type;
{
int size = 4;
int count = 0;
idx = get_opsize (idx, in, &size);
while (!eol(idx, in))
{
idx = sb_skip_white (idx, in);
if (in->ptr[idx] == ',')
idx++;
idx = exp_get_abs ("res needs absolute expression for fill count.\n", idx, in, &count);
if (type == 'c' || type == 'z')
count++;
fprintf (outfile, ".space %d\n", count * size);
}
}
/* .export */
void
do_export (in)
sb *in;
{
fprintf (outfile, ".global %s\n", sb_name (in));
}
/* .print [list] [nolist] */
void
do_print (idx, in)
int idx;
sb *in;
{
idx = sb_skip_white (idx, in);
while (idx < in->len)
{
if (strncmp (in->ptr + idx, "LIST", 4) == 0)
{
fprintf (outfile, ".list\n");
idx += 4;
}
else if (strncmp (in->ptr + idx, "NOLIST", 6) == 0)
{
fprintf (outfile, ".nolist\n");
idx += 6;
}
idx++;
}
}
/* .head */
void
do_heading (idx, in)
int idx;
sb *in;
{
sb head;
sb_new (&head);
idx = getstring (idx, in, &head);
fprintf (outfile, ".title \"%s\"\n", sb_name (&head));
sb_kill (&head);
}
/* .page */
void
do_page ()
{
fprintf (outfile, ".eject\n");
}
/* .form [lin=<value>] [col=<value>] */
void
do_form (idx, in)
int idx;
sb *in;
{
int lines = 60;
int columns = 132;
idx = sb_skip_white (idx, in);
while (idx < in->len)
{
if (strncmp (in->ptr + idx, "LIN=", 4) == 0)
{
idx += 4;
idx = exp_get_abs ("form LIN= needs absolute expresssion.\n", idx, in, &lines);
}
if (strncmp (in->ptr + idx, "COL=", 4) == 0)
{
idx += 4;
idx = exp_get_abs ("form COL= needs absolute expresssion.\n", idx, in, &columns);
}
idx++;
}
fprintf (outfile, ".psize %d,%d\n", lines, columns);
}
int
get_any_string (idx, in, out, expand)
int idx;
sb *in;
sb *out;
int expand;
{
sb_reset (out);
idx = sb_skip_white (idx, in);
if (idx < in->len)
{
if (in->ptr[idx] == '%'
&& alternate
&& expand)
{
int val;
char buf[20];
/* Turns the next expression into a string */
idx = exp_get_abs ("% operator needs absolute expression",
idx + 1,
in,
&val);
sprintf(buf, "%d", val);
sb_add_string (out, buf);
}
else if (in->ptr[idx] == '"'
|| in->ptr[idx] == '<'
|| (alternate && in->ptr[idx] == '\''))
{
if (alternate && !expand)
{
/* Keep the quotes */
/* sb_add_char (out, '\"');*/
idx = getstring (idx, in, out);
/* sb_add_char (out, '\"');*/
}
else {
idx = getstring (idx, in, out);
}
}
else
{
while (idx < in->len
&& (in->ptr[idx] == '"'
|| in->ptr[idx] == '\''
|| !ISSEP (in->ptr[idx])))
{
if (in->ptr[idx] == '"'
|| in->ptr[idx] == '\'')
{
char tchar = in->ptr[idx];
sb_add_char (out, in->ptr[idx++]);
while (idx < in->len
&& in->ptr[idx] != tchar)
sb_add_char (out, in->ptr[idx++]);
}
sb_add_char (out, in->ptr[idx++]);
}
}
}
return idx;
}
/* skip along sb in starting at idx, suck off whitespace a ( and more
whitespace. return the idx of the next char */
int
skip_openp (idx, in)
int idx;
sb *in;
{
idx = sb_skip_white (idx, in);
if (in->ptr[idx] != '(')
ERROR ((stderr, "misplaced ( .\n"));
idx = sb_skip_white (idx + 1, in);
return idx;
}
/* skip along sb in starting at idx, suck off whitespace a ) and more
whitespace. return the idx of the next char */
int
skip_closep (idx, in)
int idx;
sb *in;
{
idx = sb_skip_white (idx, in);
if (in->ptr[idx] != ')')
ERROR ((stderr, "misplaced ).\n"));
idx = sb_skip_white (idx + 1, in);
return idx;
}
/* .len */
int
dolen (idx, in, out)
int idx;
sb *in;
sb *out;
{
sb stringout;
char buffer[10];
sb_new (&stringout);
idx = skip_openp (idx, in);
idx = get_and_process (idx, in, &stringout);
idx = skip_closep (idx, in);
sprintf (buffer, "%d", stringout.len);
sb_add_string (out, buffer);
sb_kill (&stringout);
return idx;
}
/* .instr */
static
int
doinstr (idx, in, out)
int idx;
sb *in;
sb *out;
{
sb string;
sb search;
int i;
int start;
int res;
char buffer[10];
sb_new (&string);
sb_new (&search);
idx = skip_openp (idx, in);
idx = get_and_process (idx, in, &string);
idx = sb_skip_comma (idx, in);
idx = get_and_process (idx, in, &search);
idx = sb_skip_comma (idx, in);
if (isdigit (in->ptr[idx]))
{
idx = exp_get_abs (".instr needs absolute expresson.\n", idx, in, &start);
}
else
{
start = 0;
}
idx = skip_closep (idx, in);
res = -1;
for (i = start; i < string.len; i++)
{
if (strncmp (string.ptr + i, search.ptr, search.len) == 0)
{
res = i;
break;
}
}
sprintf (buffer, "%d", res);
sb_add_string (out, buffer);
sb_kill (&string);
sb_kill (&search);
return idx;
}
static int
dosubstr (idx, in, out)
int idx;
sb *in;
sb *out;
{
sb string;
int pos;
int len;
sb_new (&string);
idx = skip_openp (idx, in);
idx = get_and_process (idx, in, &string);
idx = sb_skip_comma (idx, in);
idx = exp_get_abs ("need absolute position.\n", idx, in, &pos);
idx = sb_skip_comma (idx, in);
idx = exp_get_abs ("need absolute length.\n", idx, in, &len);
idx = skip_closep (idx, in);
if (len < 0 || pos < 0 ||
pos > string.len
|| pos + len > string.len)
{
sb_add_string (out, " ");
}
else
{
sb_add_char (out, '"');
while (len > 0)
{
sb_add_char (out, string.ptr[pos++]);
len--;
}
sb_add_char (out, '"');
}
sb_kill(&string);
return idx;
}
/* scan line, change tokens in the hash table to their replacements */
void
process_assigns (idx, in, buf)
int idx;
sb *in;
sb *buf;
{
while (idx < in->len)
{
hash_entry *ptr;
if (in->ptr[idx] == '\\'
&& in->ptr[idx + 1] == '&')
{
idx = condass_lookup_name (in, idx + 2, buf, 1);
}
else if (in->ptr[idx] == '\\'
&& in->ptr[idx + 1] == '$')
{
idx = condass_lookup_name (in, idx + 2, buf, 0);
}
else if (idx + 3 < in->len
&& in->ptr[idx] == '.'
&& in->ptr[idx + 1] == 'L'
&& in->ptr[idx + 2] == 'E'
&& in->ptr[idx + 3] == 'N')
idx = dolen (idx + 4, in, buf);
else if (idx + 6 < in->len
&& in->ptr[idx] == '.'
&& in->ptr[idx + 1] == 'I'
&& in->ptr[idx + 2] == 'N'
&& in->ptr[idx + 3] == 'S'
&& in->ptr[idx + 4] == 'T'
&& in->ptr[idx + 5] == 'R')
idx = doinstr (idx + 6, in, buf);
else if (idx + 7 < in->len
&& in->ptr[idx] == '.'
&& in->ptr[idx + 1] == 'S'
&& in->ptr[idx + 2] == 'U'
&& in->ptr[idx + 3] == 'B'
&& in->ptr[idx + 4] == 'S'
&& in->ptr[idx + 5] == 'T'
&& in->ptr[idx + 6] == 'R')
idx = dosubstr (idx + 7, in, buf);
else if (ISFIRSTCHAR (in->ptr[idx]))
{
/* may be a simple name subsitution, see if we have a word */
sb acc;
int cur = idx + 1;
while (cur < in->len
&& (ISNEXTCHAR (in->ptr[cur])))
cur++;
sb_new (&acc);
sb_add_buffer (&acc, in->ptr + idx, cur - idx);
ptr = hash_lookup (&assign_hash_table, &acc);
if (ptr)
{
/* Found a definition for it */
sb_add_sb (buf, &ptr->value.s);
}
else
{
/* No definition, just copy the word */
sb_add_sb (buf, &acc);
}
sb_kill (&acc);
idx = cur;
}
else
{
sb_add_char (buf, in->ptr[idx++]);
}
}
}
static int
get_and_process (idx, in, out)
int idx;
sb *in;
sb *out;
{
sb t;
sb_new (&t);
idx = get_any_string (idx, in, &t, 1);
process_assigns (0, &t, out);
sb_kill (&t);
return idx;
}
static
void
process_file ()
{
sb line;
sb t1, t2;
sb acc;
sb label_in;
int more;
sb_new (&line);
sb_new (&t1);
sb_new (&t2);
sb_new(&acc);
sb_new (&label_in);
sb_reset (&line);
more = get_line (&line);
while (more)
{
/* Find any label and pseudo op that we're intested in */
int l;
if (line.len == 0)
{
if (condass_on ())
fprintf (outfile, "\n");
}
else
{
l = grab_label (&line, &label_in);
sb_reset (&label);
if (label_in.len)
{
/* Munge any label */
process_assigns (0, &label_in, &label);
}
if (line.ptr[l] == ':')
l++;
while (ISWHITE (line.ptr[l]) && l < line.len)
l++;
if (l < line.len)
{
if (process_pseudo_op (l, &line, &acc))
{
}
else if (condass_on ())
{
if (macro_op (l, &line))
{
}
else
{
{
if (label.len)
{
fprintf (outfile, "%s:\t", sb_name (&label));
}
else
fprintf (outfile, "\t");
sb_reset(&t1);
process_assigns (l, &line, &t1);
sb_reset (&t2);
change_base (0, &t1, &t2);
fprintf (outfile, "%s\n", sb_name (&t2));
}
}
}
}
else {
/* Only a label on this line */
if (label.len && condass_on())
{
fprintf (outfile, "%s:\n", sb_name (&label));
}
}
}
if (had_end)
break;
sb_reset (&line);
more = get_line (&line);
}
if (!had_end)
WARNING ((stderr, "END missing from end of file.\n"));
}
static void
free_old_entry (ptr)
hash_entry *ptr;
{
if (ptr)
{
if (ptr->type == hash_string)
sb_kill(&ptr->value.s);
}
}
/* name: .ASSIGNA <value> */
void
do_assigna (idx, in)
int idx;
sb *in;
{
sb tmp;
int val;
sb_new (&tmp);
process_assigns (idx, in, &tmp);
idx = exp_get_abs (".ASSIGNA needs constant expression argument.\n", 0, &tmp, &val);
if (!label.len)
{
ERROR ((stderr, ".ASSIGNA without label.\n"));
}
else
{
hash_entry *ptr = hash_create (&vars, &label);
free_old_entry (ptr);
ptr->type = hash_integer;
ptr->value.i = val;
}
sb_kill (&tmp);
}
/* name: .ASSIGNC <string> */
void
do_assignc (idx, in)
int idx;
sb *in;
{
sb acc;
sb_new (&acc);
idx = getstring (idx, in, &acc);
if (!label.len)
{
ERROR ((stderr, ".ASSIGNS without label.\n"));
}
else
{
hash_entry *ptr = hash_create (&vars, &label);
free_old_entry (ptr);
ptr->type = hash_string;
sb_new (&ptr->value.s);
sb_add_sb (&ptr->value.s, &acc);
}
sb_kill (&acc);
}
/* name: .REG (reg) */
static void
do_reg (idx, in)
int idx;
sb *in;
{
/* remove reg stuff from inside parens */
sb what;
idx = skip_openp (idx, in);
sb_new (&what);
while (idx < in->len && in->ptr[idx] != ')')
{
sb_add_char (&what, in->ptr[idx]);
idx++;
}
hash_add_to_string_table (&assign_hash_table, &label, &what, 1);
sb_kill (&what);
}
static int
condass_lookup_name (inbuf, idx, out, warn)
sb *inbuf;
int idx;
sb *out;
int warn;
{
hash_entry *ptr;
sb condass_acc;
sb_new (&condass_acc);
while (idx < inbuf->len
&& ISNEXTCHAR (inbuf->ptr[idx]))
{
sb_add_char (&condass_acc, inbuf->ptr[idx++]);
}
if (inbuf->ptr[idx] == '\'')
idx++;
ptr = hash_lookup (&vars, &condass_acc);
if (!ptr)
{
if (warn)
{
WARNING ((stderr, "Can't find preprocessor variable %s.\n", sb_name (&condass_acc)));
}
else
{
sb_add_string (out, "0");
}
}
else
{
if (ptr->type == hash_integer)
{
char buffer[30];
sprintf (buffer, "%d", ptr->value.i);
sb_add_string (out, buffer);
}
else
{
sb_add_sb (out, &ptr->value.s);
}
}
sb_kill (&condass_acc);
return idx;
}
#define EQ 1
#define NE 2
#define GE 3
#define LT 4
#define LE 5
#define GT 6
#define NEVER 7
int
whatcond (idx, in, val)
int idx;
sb *in;
int *val;
{
int cond;
char *p;
idx = sb_skip_white (idx, in);
p = in->ptr + idx;
if (p[0] == 'E' && p[1] == 'Q')
cond = EQ;
else if (p[0] == 'N' && p[1] == 'E')
cond = NE;
else if (p[0] == 'L' && p[1] == 'T')
cond = LT;
else if (p[0] == 'L' && p[1] == 'E')
cond = LE;
else if (p[0] == 'G' && p[1] == 'T')
cond = GT;
else if (p[0] == 'G' && p[1] == 'E')
cond = GE;
else
{
ERROR ((stderr, "Comparison operator must be one of EQ, NE, LT, LE, GT or GE.\n"));
cond = NEVER;
}
idx = sb_skip_white (idx + 2, in);
*val = cond;
return idx;
}
int
istrue (idx, in)
int idx;
sb *in;
{
int res;
sb acc_a;
sb cond;
sb acc_b;
sb_new (&acc_a);
sb_new (&cond);
sb_new (&acc_b);
idx = sb_skip_white (idx, in);
if (in->ptr[idx] == '"')
{
int cond;
int same;
/* This is a string comparision */
idx = getstring (idx, in, &acc_a);
idx = whatcond (idx, in, &cond);
idx = getstring (idx, in, &acc_b);
same = acc_a.len == acc_b.len && (strncmp (acc_a.ptr, acc_b.ptr, acc_a.len) == 0);
if (cond != EQ && cond != NE)
{
ERROR ((stderr, "Comparison operator for strings must be EQ or NE\n"));
res = 0;
}
else
res = cond == EQ && same;
}
else
/* This is a numeric expression */
{
int vala;
int valb;
int cond;
idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &vala);
idx = whatcond (idx, in, &cond);
idx = sb_skip_white (idx, in);
if (in->ptr[idx] == '"')
{
WARNING ((stderr, "String compared against expression.\n"));
res = 0;
}
else
{
idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &valb);
switch (cond)
{
case EQ:
res = vala == valb;
break;
case NE:
res = vala != valb;
break;
case LT:
res = vala < valb;
break;
case LE:
res = vala <= valb;
break;
case GT:
res = vala > valb;
break;
case GE:
res = vala >= valb;
break;
case NEVER:
res = 0;
break;
}
}
}
sb_kill (&acc_a);
sb_kill (&cond);
sb_kill (&acc_b);
return res;
}
/* .AIF */
static void
do_aif (idx, in)
int idx;
sb *in;
{
if (ifi >= IFNESTING)
{
FATAL ((stderr, "AIF nesting unreasonable.\n"));
}
ifi++;
ifstack[ifi].on = istrue (idx, in);
ifstack[ifi].hadelse = 0;
}
/* .AELSE */
static void
do_aelse ()
{
ifstack[ifi].on = !ifstack[ifi].on;
if (ifstack[ifi].hadelse)
{
ERROR ((stderr, "Multiple AELSEs in AIF.\n"));
}
ifstack[ifi].hadelse = 1;
}
/* .AENDI */
static void
do_aendi ()
{
if (ifi != 0)
{
ifi--;
}
else
{
ERROR ((stderr, "AENDI without AIF.\n"));
}
}
static int
condass_on ()
{
return ifstack[ifi].on;
}
/* Read input lines till we get to a TO string.
Increase nesting depth if we geta FROM string.
Put the results into sb at PTR. */
static void
buffer_and_nest (from, to, ptr)
char *from;
char *to;
sb *ptr;
{
int from_len = strlen (from);
int to_len = strlen (to);
int depth = 1;
int line_start = ptr->len;
int line = linecount ();
int more = get_line (ptr);
while (more)
{
/* Try and find the first pseudo op on the line */
int i = line_start;
if (!alternate)
{
/* With normal syntax we can suck what we want till we get to the dot.
With the alternate, labels have to start in the first column, since
we cant tell what's a label and whats a pseudoop */
/* Skip leading whitespace */
while (i < ptr->len
&& ISWHITE (ptr->ptr[i]))
i++;
/* Skip over a label */
while (i < ptr->len
&& ISNEXTCHAR (ptr->ptr[i]))
i++;
/* And a colon */
if (i < ptr->len
&& ptr->ptr[i] == ':')
i++;
}
/* Skip trailing whitespace */
while (i < ptr->len
&& ISWHITE (ptr->ptr[i]))
i++;
if (i < ptr->len && (ptr->ptr[i] == '.'
|| alternate))
{
if (ptr->ptr[i] == '.')
i++;
if (strncmp (ptr->ptr + i, from, from_len) == 0)
depth++;
if (strncmp (ptr->ptr + i, to, to_len) == 0)
{
depth--;
if (depth == 0)
{
/* Reset the string to not include the ending rune */
ptr->len = line_start;
break;
}
}
}
/* Add a CR to the end and keep running */
sb_add_char (ptr, '\n');
line_start = ptr->len;
more = get_line (ptr);
}
if (depth)
FATAL ((stderr, "End of file whilst inside %s, started on line %d.\n", from, line));
}
/* .ENDR */
void
do_aendr ()
{
ERROR ((stderr, "AENDR without a AREPEAT.\n"));
}
/* .AWHILE */
static
void
do_awhile (idx, in)
int idx;
sb *in;
{
sb exp;
sb sub;
int doit;
sb_new (&sub);
sb_new (&exp);
process_assigns (idx, in, &exp);
doit = istrue (0, &exp);
buffer_and_nest ("AWHILE", "AENDW", &sub);
/* Turn
.AWHILE exp
foo
.AENDW
into
foo
.AWHILE exp
foo
.ENDW
*/
if (doit)
{
int index = include_next_index ();
sb copy;
sb_new (©);
sb_add_sb (©, &sub);
sb_add_sb (©, in);
sb_add_string (©, "\n");
sb_add_sb (©, &sub);
sb_add_string (©, "\t.AENDW\n");
/* Push another WHILE */
include_buf (&exp, ©, include_while, index);
sb_kill (©);
}
sb_kill (&exp);
sb_kill (&sub);
}
/* .AENDW */
static void
do_aendw ()
{
ERROR ((stderr, "AENDW without a AENDW.\n"));
}
/* .EXITM
Pop things off the include stack until the type and index changes */
static void
do_exitm ()
{
include_type type = sp->type;
if (type == include_repeat
|| type == include_while
|| type == include_macro)
{
int index = sp->index;
include_pop ();
while (sp->index == index
&& sp->type == type)
{
include_pop ();
}
}
}
/* .AREPEAT */
static void
do_arepeat (idx, in)
int idx;
sb *in;
{
sb exp; /* buffer with expression in it */
sb copy; /* expanded repeat block */
sb sub; /* contents of AREPEAT */
int rc;
char buffer[30];
sb_new (&exp);
sb_new (©);
sb_new (&sub);
process_assigns (idx, in, &exp);
idx = exp_get_abs ("AREPEAT must have absolute operand.\n", 0, &exp, &rc);
buffer_and_nest ("AREPEAT", "AENDR", &sub);
if (rc > 0)
{
/* Push back the text following the repeat, and another repeat block
so
.AREPEAT 20
foo
.AENDR
gets turned into
foo
.AREPEAT 19
foo
.AENDR
*/
int index = include_next_index ();
sb_add_sb (©, &sub);
if (rc > 1)
{
sprintf (buffer, "\t.AREPEAT %d\n", rc - 1);
sb_add_string (©, buffer);
sb_add_sb (©, &sub);
sb_add_string (©, " .AENDR\n");
}
include_buf (&exp, ©, include_repeat, index);
}
sb_kill (&exp);
sb_kill (&sub);
sb_kill (©);
}
/* .ENDM */
static void
do_endm ()
{
ERROR ((stderr, ".ENDM without a matching .MACRO.\n"));
}
/* MARRO PROCESSING */
static int number;
hash_table macro_table;
/* Understand
.MACRO <name>
stuff
.ENDM
*/
static int
do_formals (macro, idx, in)
macro_entry *macro;
int idx;
sb *in;
{
formal_entry **p = ¯o->formals;
macro->formal_count = 0;
hash_new_table (5, ¯o->formal_hash);
while (idx < in->len)
{
formal_entry *formal;
formal = (formal_entry *) xmalloc (sizeof (formal_entry));
sb_new (&formal->name);
sb_new (&formal->def);
sb_new (&formal->actual);
idx = sb_skip_white (idx, in);
idx = get_token (idx, in, &formal->name);
if (formal->name.len == 0)
break;
idx = sb_skip_white (idx, in);
if (formal->name.len)
{
/* This is a formal */
if (idx < in->len && in->ptr[idx] == '=')
{
/* Got a default */
idx = get_any_string (idx + 1, in, &formal->def, 1);
}
}
{
/* Add to macro's hash table */
hash_entry *p = hash_create (¯o->formal_hash, &formal->name);
p->type = hash_formal;
p->value.f = formal;
}
formal->index = macro->formal_count;
idx = sb_skip_comma (idx, in);
macro->formal_count++;
*p = formal;
p = &formal->next;
}
return idx;
}
/* Parse off LOCAL n1, n2,... Invent a label name for it */
static
void
do_local (idx, line)
int idx;
sb *line;
{
static int ln;
sb acc;
sb sub;
char subs[10];
sb_new (&acc);
sb_new (&sub);
idx = sb_skip_white (idx, line);
while (!eol(idx, line))
{
sb_reset (&acc);
sb_reset (&sub);
ln++;
sprintf(subs, "LL%04x", ln);
idx = get_token(idx, line, &acc);
sb_add_string (&sub, subs);
hash_add_to_string_table (&assign_hash_table, &acc, &sub, 1);
idx = sb_skip_comma (idx, line);
}
sb_kill (&sub);
sb_kill (&acc);
}
static
void
do_macro (idx, in)
int idx;
sb *in;
{
macro_entry *macro;
sb name;
macro = (macro_entry *) xmalloc (sizeof (macro_entry));
sb_new (¯o->sub);
sb_new (&name);
macro->formal_count = 0;
macro->formals = 0;
idx = sb_skip_white (idx, in);
buffer_and_nest ("MACRO", "ENDM", ¯o->sub);
if (label.len)
{
sb_add_sb (&name, &label);
if (in->ptr[idx] == '(')
{
/* It's the label: MACRO (formals,...) sort */
idx = do_formals (macro, idx + 1, in);
if (in->ptr[idx] != ')')
ERROR ((stderr, "Missing ) after formals.\n"));
}
else {
/* It's the label: MACRO formals,... sort */
idx = do_formals (macro, idx, in);
}
}
else
{
idx = get_token (idx, in, &name);
idx = sb_skip_white (idx, in);
idx = do_formals (macro, idx, in);
}
/* and stick it in the macro hash table */
hash_create (¯o_table, &name)->value.m = macro;
}
static
int
get_token (idx, in, name)
int idx;
sb *in;
sb *name;
{
if (idx < in->len
&& ISFIRSTCHAR (in->ptr[idx]))
{
sb_add_char (name, in->ptr[idx++]);
while (idx < in->len
&& ISNEXTCHAR (in->ptr[idx]))
{
sb_add_char (name, in->ptr[idx++]);
}
}
/* Ignore trailing & */
if (alternate && idx < in->len && in->ptr[idx] == '&')
idx++;
return idx;
}
/* Scan a token, but stop if a ' is seen */
static int
get_apost_token (idx, in, name, kind)
int idx;
sb *in;
sb *name;
int kind;
{
idx = get_token (idx, in, name);
if (idx < in->len && in->ptr[idx] == kind)
idx++;
return idx;
}
static int
sub_actual (src, in, t, m, kind, out, copyifnotthere)
int src;
sb *in;
sb *t;
macro_entry *m;
int kind;
sb *out;
int copyifnotthere;
{
/* This is something to take care of */
hash_entry *ptr;
src = get_apost_token (src, in, t, kind);
/* See if it's in the macro's hash table */
ptr = hash_lookup (&m->formal_hash, t);
if (ptr)
{
if (ptr->value.f->actual.len)
{
sb_add_sb (out, &ptr->value.f->actual);
}
else
{
sb_add_sb (out, &ptr->value.f->def);
}
}
else if (copyifnotthere)
{
sb_add_sb (out, t);
}
else
{
sb_add_char (out, '\\');
sb_add_sb (out, t);
}
return src;
}
static
void
macro_expand (name, idx, in, m)
sb *name;
int idx;
sb *in;
macro_entry *m;
{
sb t;
sb out;
hash_entry *ptr;
formal_entry *f;
int is_positional = 0;
int is_keyword = 0;
sb_new (&t);
sb_new (&out);
/* Reset any old value the actuals may have */
for (f = m->formals; f; f = f->next)
sb_reset (&f->actual);
f = m->formals;
/* Peel off the actuals and store them away in the hash tables' actuals */
while (!eol(idx, in))
{
int scan;
idx = sb_skip_white (idx, in);
/* Look and see if it's a positional or keyword arg */
scan = idx;
while (scan < in->len
&& !ISSEP (in->ptr[scan])
&& (!alternate && in->ptr[scan] != '='))
scan++;
if (scan < in->len && (!alternate) && in->ptr[scan] == '=')
{
is_keyword = 1;
if (is_positional)
{
ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
return;
}
/* This is a keyword arg, fetch the formal name and
then the actual stuff */
sb_reset (&t);
idx = get_token (idx, in, &t);
if (in->ptr[idx] != '=')
ERROR ((stderr, "confused about formal params.\n"));
/* Lookup the formal in the macro's list */
ptr = hash_lookup (&m->formal_hash, &t);
if (!ptr)
{
ERROR ((stderr, "MACRO formal argument %s does not exist.\n", sb_name (&t)));
return;
}
else
{
/* Insert this value into the right place */
sb_reset (&ptr->value.f->actual);
idx = get_any_string (idx + 1, in, &ptr->value.f->actual, 0);
}
}
else
{
/* This is a positional arg */
is_positional = 1;
if (is_keyword)
{
ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
return;
}
if (!f)
{
ERROR ((stderr, "Too many positional arguments.\n"));
return;
}
sb_reset (&f->actual);
idx = get_any_string (idx, in, &f->actual, 1);
f = f->next;
}
idx = sb_skip_comma (idx, in);
}
/* Copy the stuff from the macro buffer into a safe place and substitute any args */
{
int src = 0;
int inquote = 0;
sb *in = &m->sub;
sb_reset (&out);
while (src < in->len)
{
if (in->ptr[src] == '&')
{
sb_reset (&t);
src = sub_actual (src + 1, in, &t, m, '&', &out, 0);
}
else if (in->ptr[src] == '\\')
{
src++;
if (in->ptr[src] == comment_char)
{
/* This is a comment, just drop the rest of the line */
while (src < in->len
&& in->ptr[src] != '\n')
src++;
}
else if (in->ptr[src] == '(')
{
/* Sub in till the next ')' literally */
src++;
while (src < in->len && in->ptr[src] != ')')
{
sb_add_char (&out, in->ptr[src++]);
}
if (in->ptr[src] == ')')
src++;
else
ERROR ((stderr, "Missplaced ).\n"));
}
else if (in->ptr[src] == '@')
{
/* Sub in the macro invocation number */
char buffer[6];
src++;
sprintf (buffer, "%05d", number);
sb_add_string (&out, buffer);
}
else if (in->ptr[src] == '&')
{
/* This is a preprocessor variable name, we don't do them
here */
sb_add_char (&out, '\\');
sb_add_char (&out, '&');
src++;
}
else
{
sb_reset (&t);
src = sub_actual (src, in, &t, m, '\'', &out, 0);
}
}
else if (ISFIRSTCHAR (in->ptr[src]) && alternate)
{
sb_reset (&t);
src = sub_actual (src, in, &t, m, '\'', &out, 1);
}
else if (ISCOMMENTCHAR (in->ptr[src])
&& src + 1 < in->len
&& ISCOMMENTCHAR (in->ptr[src+1])
&& !inquote)
{
/* Two comment chars in a row cause the rest of the line to be dropped */
while (src < in->len && in->ptr[src] != '\n')
src++;
}
else if (in->ptr[src] == '"')
{
inquote = !inquote;
sb_add_char (&out, in->ptr[src++]);
}
else
{
sb_add_char (&out, in->ptr[src++]);
}
}
include_buf (name, &out, include_macro, include_next_index ());
}
sb_kill (&t);
sb_kill (&out);
number++;
}
static int
macro_op (idx, in)
int idx;
sb *in;
{
int res = 0;
/* The macro name must be the first thing on the line */
if (idx < in->len)
{
sb name;
hash_entry *ptr;
sb_new (&name);
idx = get_token (idx, in, &name);
if (name.len)
{
/* Got a name, look it up */
ptr = hash_lookup (¯o_table, &name);
if (ptr)
{
/* It's in the table, copy out the stuff and convert any macro args */
macro_expand (&name, idx, in, ptr->value.m);
res = 1;
}
}
sb_kill (&name);
}
return res;
}
/* STRING HANDLING */
static int
getstring (idx, in, acc)
int idx;
sb *in;
sb *acc;
{
idx = sb_skip_white (idx, in);
while (idx < in->len
&& (in->ptr[idx] == '"'
|| in->ptr[idx] == '<'
|| (in->ptr[idx] == '\'' && alternate)))
{
if (in->ptr[idx] == '<')
{
if (alternate)
{
int nest = 0;
idx++;
while ((in->ptr[idx] != '>' || nest)
&& idx < in->len)
{
if (in->ptr[idx] == '!')
{
idx++ ;
sb_add_char (acc, in->ptr[idx++]);
}
else {
if (in->ptr[idx] == '>')
nest--;
if (in->ptr[idx] == '<')
nest++;
sb_add_char (acc, in->ptr[idx++]);
}
}
idx++;
}
else {
int code;
idx++;
idx = exp_get_abs ("Character code in string must be absolute expression.\n",
idx, in, &code);
sb_add_char (acc, code);
if (in->ptr[idx] != '>')
ERROR ((stderr, "Missing > for character code.\n"));
idx++;
}
}
else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
{
char tchar = in->ptr[idx];
idx++;
while (idx < in->len)
{
if (alternate && in->ptr[idx] == '!')
{
idx++ ;
sb_add_char (acc, in->ptr[idx++]);
}
else {
if (in->ptr[idx] == tchar)
{
idx++;
if (idx >= in->len || in->ptr[idx] != tchar)
break;
}
sb_add_char (acc, in->ptr[idx]);
idx++;
}
}
}
}
return idx;
}
/* .SDATA[C|Z] <string> */
static
void
do_sdata (idx, in, type)
int idx;
sb *in;
char type;
{
int nc = 0;
int pidx = -1;
sb acc;
sb_new (&acc);
fprintf (outfile, ".byte\t");
while (!eol (idx, in))
{
int i;
sb_reset (&acc);
idx = sb_skip_white (idx, in);
while (!eol (idx, in))
{
pidx = idx = get_any_string (idx, in, &acc, 1);
if (type == 'c')
{
if (acc.len > 255)
{
ERROR ((stderr, "string for SDATAC longer than 255 characters (%d).\n", acc.len));
}
fprintf (outfile, "%d", acc.len);
nc = 1;
}
for (i = 0; i < acc.len; i++)
{
if (nc)
{
fprintf (outfile, ",");
}
fprintf (outfile, "%d", acc.ptr[i]);
nc = 1;
}
if (type == 'z')
{
if (nc)
fprintf (outfile, ",");
fprintf (outfile, "0");
}
idx = sb_skip_comma (idx, in);
if (idx == pidx) break;
}
if (!alternate && in->ptr[idx] != ',' && idx != in->len)
{
fprintf (outfile, "\n");
ERROR ((stderr, "illegal character in SDATA line (0x%x).\n", in->ptr[idx]));
break;
}
idx++;
}
sb_kill (&acc);
fprintf (outfile, "\n");
}
/* .SDATAB <count> <string> */
static void
do_sdatab (idx, in)
int idx;
sb *in;
{
int repeat;
int i;
sb acc;
sb_new (&acc);
idx = exp_get_abs ("Must have absolute SDATAB repeat count.\n", idx, in, &repeat);
if (repeat <= 0)
{
ERROR ((stderr, "Must have positive SDATAB repeat count (%d).\n", repeat));
repeat = 1;
}
idx = sb_skip_comma (idx, in);
idx = getstring (idx, in, &acc);
for (i = 0; i < repeat; i++)
{
if (i)
fprintf (outfile, "\t");
fprintf (outfile, ".byte\t");
sb_print (&acc);
fprintf (outfile, "\n");
}
sb_kill (&acc);
}
int
new_file (name)
char *name;
{
FILE *newone = fopen (name, "r");
if (!newone)
return 0;
if (isp == MAX_INCLUDES)
FATAL ((stderr, "Unreasonable include depth (%d).\n", isp));
sp++;
sp->handle = newone;
sb_new (&sp->name);
sb_add_string (&sp->name, name);
sp->linecount = 1;
sp->pushback_index = 0;
sp->type = include_file;
sp->index = 0;
sb_new (&sp->pushback);
return 1;
}
static void
do_include (idx, in)
int idx;
sb *in;
{
sb t;
char *text;
sb_new (&t);
idx = getstring (idx, in, &t);
text = sb_name (&t);
if (!new_file (text))
{
FATAL ((stderr, "Can't open include file `%s'.\n", text));
}
sb_kill (&t);
}
static void
include_pop ()
{
if (sp != include_stack)
{
if (sp->handle)
fclose (sp->handle);
sp--;
}
}
/* Get the next character from the include stack. If there's anything
in the pushback buffer, take that first. If we're at eof, pop from
the stack and try again. Keep the linecount up to date. */
static int
get ()
{
int r;
if (sp->pushback.len != sp->pushback_index)
{
r = (char) (sp->pushback.ptr[sp->pushback_index++]);
/* When they've all gone, reset the pointer */
if (sp->pushback_index == sp->pushback.len)
{
sp->pushback.len = 0;
sp->pushback_index = 0;
}
}
else if (sp->handle)
{
r = getc (sp->handle);
}
else
r = EOF;
if (r == EOF && isp)
{
include_pop ();
r = get ();
while (r == EOF && isp)
{
include_pop ();
r = get ();
}
return r;
}
if (r == '\n')
{
sp->linecount++;
}
return r;
}
static int
linecount ()
{
return sp->linecount;
}
static int
include_next_index ()
{
static int index;
if (!unreasonable
&& index > MAX_REASONABLE)
FATAL ((stderr, "Unreasonable expansion (-u turns off check).\n"));
return ++index;
}
/* Initialize the chartype vector. */
static void
chartype_init ()
{
int x;
for (x = 0; x < 256; x++)
{
if (isalpha (x) || x == '_' || x == '$')
chartype[x] |= FIRSTBIT;
if (isdigit (x) || isalpha (x) || x == '_' || x == '$')
chartype[x] |= NEXTBIT;
if (x == ' ' || x == '\t' || x == ',' || x == '"' || x == ';'
|| x == '"' || x == '<' || x == '>' || x == ')' || x == '(')
chartype[x] |= SEPBIT;
if (x == ' ' || x == '\t')
chartype[x] |= WHITEBIT;
if (x == comment_char)
chartype[x] |= COMMENTBIT;
}
}
/* What to do with all the keywords */
#define PROCESS 0x1000 /* Run substitution over the line */
#define LAB 0x2000 /* Spit out the label */
#define K_EQU PROCESS|1
#define K_ASSIGN PROCESS|2
#define K_REG PROCESS|3
#define K_ORG PROCESS|4
#define K_RADIX PROCESS|5
#define K_DATA LAB|PROCESS|6
#define K_DATAB LAB|PROCESS|7
#define K_SDATA LAB|PROCESS|8
#define K_SDATAB LAB|PROCESS|9
#define K_SDATAC LAB|PROCESS|10
#define K_SDATAZ LAB|PROCESS|11
#define K_RES LAB|PROCESS|12
#define K_SRES LAB|PROCESS|13
#define K_SRESC LAB|PROCESS|14
#define K_SRESZ LAB|PROCESS|15
#define K_EXPORT LAB|PROCESS|16
#define K_GLOBAL LAB|PROCESS|17
#define K_PRINT LAB|PROCESS|19
#define K_FORM LAB|PROCESS|20
#define K_HEADING LAB|PROCESS|21
#define K_PAGE LAB|PROCESS|22
#define K_IMPORT LAB|PROCESS|23
#define K_PROGRAM LAB|PROCESS|24
#define K_END PROCESS|25
#define K_INCLUDE PROCESS|26
#define K_IGNORED PROCESS|27
#define K_ASSIGNA PROCESS|28
#define K_ASSIGNC 29
#define K_AIF PROCESS|30
#define K_AELSE PROCESS|31
#define K_AENDI PROCESS|32
#define K_AREPEAT PROCESS|33
#define K_AENDR PROCESS|34
#define K_AWHILE 35
#define K_AENDW PROCESS|36
#define K_EXITM 37
#define K_MACRO PROCESS|38
#define K_ENDM 39
#define K_ALIGN PROCESS|LAB|40
#define K_ALTERNATE 41
#define K_DB LAB|PROCESS|42
#define K_DW LAB|PROCESS|43
#define K_DL LAB|PROCESS|44
#define K_LOCAL 45
static struct
{
char *name;
int code;
int extra;
}
kinfo[] =
{
{ "EQU", K_EQU, 0 },
{ "ALTERNATE", K_ALTERNATE, 0 },
{ "ASSIGN", K_ASSIGN, 0 },
{ "REG", K_REG, 0 },
{ "ORG", K_ORG, 0 },
{ "RADIX", K_RADIX, 0 },
{ "DATA", K_DATA, 0 },
{ "DB", K_DB, 0 },
{ "DW", K_DW, 0 },
{ "DL", K_DL, 0 },
{ "DATAB", K_DATAB, 0 },
{ "SDATA", K_SDATA, 0 },
{ "SDATAB", K_SDATAB, 0 },
{ "SDATAZ", K_SDATAZ, 0 },
{ "SDATAC", K_SDATAC, 0 },
{ "RES", K_RES, 0 },
{ "SRES", K_SRES, 0 },
{ "SRESC", K_SRESC, 0 },
{ "SRESZ", K_SRESZ, 0 },
{ "EXPORT", K_EXPORT, 0 },
{ "GLOBAL", K_GLOBAL, 0 },
{ "PRINT", K_PRINT, 0 },
{ "FORM", K_FORM, 0 },
{ "HEADING", K_HEADING, 0 },
{ "PAGE", K_PAGE, 0 },
{ "PROGRAM", K_IGNORED, 0 },
{ "END", K_END, 0 },
{ "INCLUDE", K_INCLUDE, 0 },
{ "ASSIGNA", K_ASSIGNA, 0 },
{ "ASSIGNC", K_ASSIGNC, 0 },
{ "AIF", K_AIF, 0 },
{ "AELSE", K_AELSE, 0 },
{ "AENDI", K_AENDI, 0 },
{ "AREPEAT", K_AREPEAT, 0 },
{ "AENDR", K_AENDR, 0 },
{ "EXITM", K_EXITM, 0 },
{ "MACRO", K_MACRO, 0 },
{ "ENDM", K_ENDM, 0 },
{ "AWHILE", K_AWHILE, 0 },
{ "ALIGN", K_ALIGN, 0 },
{ "AENDW", K_AENDW, 0 },
{ "ALTERNATE", K_ALTERNATE, 0 },
{ "LOCAL", K_LOCAL, 0 },
{ NULL, 0, 0 }
};
/* Look for a pseudo op on the line. If one's there then call
its handler. */
static int
process_pseudo_op (idx, line, acc)
int idx;
sb *line;
sb *acc;
{
if (line->ptr[idx] == '.' || alternate)
{
/* Scan forward and find pseudo name */
char *in;
hash_entry *ptr;
char *s;
char *e;
if (line->ptr[idx] == '.')
idx++;
in = line->ptr + idx;
s = in;
e = s;
sb_reset (acc);
while (idx < line->len && *e && ISFIRSTCHAR (*e))
{
sb_add_char (acc, *e);
e++;
idx++;
}
ptr = hash_lookup (&keyword_hash_table, acc);
if (!ptr)
{
#if 0
/* This one causes lots of pain when trying to preprocess
ordinary code */
WARNING ((stderr, "Unrecognised pseudo op `%s'.\n", sb_name (acc)));
#endif
return 0;
}
if (ptr->value.i & LAB)
{ /* output the label */
if (label.len)
{
fprintf (outfile, "%s:\t", sb_name (&label));
}
else
fprintf (outfile, "\t");
}
if (ptr->value.i & PROCESS)
{
/* Polish the rest of the line before handling the pseudo op */
#if 0
strip_comments(line);
#endif
sb_reset (acc);
process_assigns (idx, line, acc);
sb_reset(line);
change_base (0, acc, line);
idx = 0;
}
if (!condass_on ())
{
switch (ptr->value.i)
{
case K_AELSE:
do_aelse ();
break;
case K_AENDI:
do_aendi ();
break;
}
return 1;
}
else
{
switch (ptr->value.i)
{
case K_ALTERNATE:
alternate = 1;
return 1;
case K_AELSE:
do_aelse ();
return 1;
case K_AENDI:
do_aendi ();
return 1;
case K_ORG:
ERROR ((stderr, "ORG command not allowed.\n"));
break;
case K_RADIX:
do_radix (line);
return 1;
case K_DB:
do_data (idx, line, 1);
return 1;
case K_DW:
do_data (idx, line, 2);
return 1;
case K_DL:
do_data (idx, line, 4);
return 1;
case K_DATA:
do_data (idx, line, 0);
return 1;
case K_DATAB:
do_datab (idx, line);
return 1;
case K_SDATA:
do_sdata (idx, line, 0);
return 1;
case K_SDATAB:
do_sdatab (idx, line);
return 1;
case K_SDATAC:
do_sdata (idx, line, 'c');
return 1;
case K_SDATAZ:
do_sdata (idx, line, 'z');
return 1;
case K_ASSIGN:
do_assign (1, 0, line);
return 1;
case K_AIF:
do_aif (idx, line);
return 1;
case K_AREPEAT:
do_arepeat (idx, line);
return 1;
case K_AENDW:
do_aendw ();
return 1;
case K_AWHILE:
do_awhile (idx, line);
return 1;
case K_AENDR:
do_aendr ();
return 1;
case K_EQU:
do_assign (0, idx, line);
return 1;
case K_ALIGN:
do_align (idx, line);
return 1;
case K_RES:
do_res (idx, line, 0);
return 1;
case K_SRES:
do_res (idx, line, 's');
return 1;
case K_INCLUDE:
do_include (idx, line);
return 1;
case K_LOCAL:
do_local (idx, line);
return 1;
case K_MACRO:
do_macro (idx, line);
return 1;
case K_ENDM:
do_endm ();
return 1;
case K_SRESC:
do_res (idx, line, 'c');
return 1;
case K_PRINT:
do_print (idx, line);
return 1;
case K_FORM:
do_form (idx, line);
return 1;
case K_HEADING:
do_heading (idx, line);
return 1;
case K_PAGE:
do_page ();
return 1;
case K_GLOBAL:
case K_EXPORT:
do_export (line);
return 1;
case K_IMPORT:
return 1;
case K_SRESZ:
do_res (idx, line, 'z');
return 1;
case K_IGNORED:
return 1;
case K_END:
do_end ();
return 1;
case K_ASSIGNA:
do_assigna (idx, line);
return 1;
case K_ASSIGNC:
do_assignc (idx, line);
return 1;
case K_EXITM:
do_exitm ();
return 1;
case K_REG:
do_reg (idx, line);
return 1;
}
}
}
return 0;
}
/* Build the keyword hash table - put each keyword in the table twice,
once upper and once lower case.*/
static void
process_init ()
{
int i;
for (i = 0; kinfo[i].name; i++)
{
sb label;
int j;
sb_new (&label);
sb_add_string (&label, kinfo[i].name);
hash_add_to_int_table (&keyword_hash_table, &label, kinfo[i].code);
sb_reset (&label);
for (j = 0; kinfo[i].name[j]; j++)
sb_add_char (&label, kinfo[i].name[j] - 'A' + 'a');
hash_add_to_int_table (&keyword_hash_table, &label, kinfo[i].code);
sb_kill (&label);
}
}
static void
do_define (string)
char *string;
{
sb label;
int res = 1;
hash_entry *ptr;
sb_new (&label);
while (*string)
{
if (*string == '=')
{
sb value;
sb_new (&value);
string++;
while (*string)
{
sb_add_char (&value, *string);
string++;
}
exp_get_abs ("Invalid expression on command line.\n", 0, &value, &res);
sb_kill (&value);
break;
}
sb_add_char (&label, *string);
string ++;
}
ptr = hash_create (&vars, &label);
free_old_entry (ptr);
ptr->type = hash_integer;
ptr->value.i = res;
sb_kill (&label);
}
char *program_name;
/* The list of long options. */
static struct option long_options[] =
{
{ "alternate", no_argument, 0, 'a' },
{ "commentchar", required_argument, 0, 'c' },
{ "copysource", no_argument, 0, 's' },
{ "debug", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ "output", required_argument, 0, 'o' },
{ "print", no_argument, 0, 'p' },
{ "unreasonable", no_argument, 0, 'u' },
{ "version", no_argument, 0, 'v' },
{ "define", required_argument, 0, 'd' },
{ NULL, no_argument, 0, 0 }
};
/* Show a usage message and exit. */
static void
show_usage (file, status)
FILE *file;
int status;
{
fprintf (file, "\
Usage: %s \n\
[-a] [--alternate] enter alternate macro mode\n\
[-c char] [--commentchar char] change the comment character from !\n\
[-d] [--debug] print some debugging info\n\
[-h] [--help] print this message\n\
[-o out] [--output out] set the output file\n\
[-p] [--print] print line numbers\n\
[-s] [--copysource] copy source through as comments \n\
[-u] [--unreasonable] allow unreasonable nesting\n\
[-v] [--version] print the program version\n\
[-Dname=value] create preprocessor variable called name, with value\n\
[in-file]\n", program_name);
exit (status);
}
/* Display a help message and exit. */
static void
show_help ()
{
printf ("%s: Gnu Assembler Macro Preprocessor\n",
program_name);
show_usage (stdout, 0);
}
int
main (argc, argv)
int argc;
char **argv;
{
int opt;
char *out_name = 0;
sp = include_stack;
ifstack[0].on = 1;
ifi = 0;
program_name = argv[0];
xmalloc_set_program_name (program_name);
hash_new_table (101, ¯o_table);
hash_new_table (101, &keyword_hash_table);
hash_new_table (101, &assign_hash_table);
hash_new_table (101, &vars);
sb_new (&label);
process_init ();
while ((opt = getopt_long (argc, argv, "sdhavc:upo:D:", long_options,
(int *) NULL))
!= EOF)
{
switch (opt)
{
case 'o':
out_name = optarg;
break;
case 'u':
unreasonable = 1;
break;
case 'p':
print_line_number = 1;
break;
case 'c':
comment_char = optarg[0];
break;
case 'a':
alternate = 1;
break;
case 's':
copysource = 1;
break;
case 'd':
stats = 1;
break;
case 'D':
do_define (optarg);
break;
case 'h':
show_help ();
/*NOTREACHED*/
case 'v':
printf ("GNU %s version %s\n", program_name, program_version);
exit (0);
/*NOTREACHED*/
case 0:
break;
default:
show_usage (stderr, 1);
/*NOTREACHED*/
}
}
if (out_name) {
outfile = fopen (out_name, "w");
if (!outfile)
{
fprintf (stderr, "%s: Can't open output file `%s'.\n",
program_name, out_name);
exit (1);
}
}
else {
outfile = stdout;
}
chartype_init ();
if (!outfile)
outfile = stdout;
/* Process all the input files */
while (optind < argc)
{
if (new_file (argv[optind]))
{
process_file ();
}
else
{
fprintf (stderr, "%s: Can't open input file `%s'.\n",
program_name, argv[optind]);
exit (1);
}
optind++;
}
quit ();
return 0;
}
|