aboutsummaryrefslogtreecommitdiff
path: root/gas/config/tc-vax.c
blob: 275bff9dacd23477b3b514d227b8d744494cf24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
/* tc-vax.c - vax-specific -
   Copyright 1987, 1991, 1992, 1993, 1994, 1995, 1998, 2000, 2001
   Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.  */

#include "as.h"

#include "vax-inst.h"
#include "obstack.h"		/* For FRAG_APPEND_1_CHAR macro in "frags.h" */

/* These chars start a comment anywhere in a source file (except inside
   another comment */
const char comment_chars[] = "#";

/* These chars only start a comment at the beginning of a line.  */
/* Note that for the VAX the are the same as comment_chars above.  */
const char line_comment_chars[] = "#";

const char line_separator_chars[] = ";";

/* Chars that can be used to separate mant from exp in floating point nums */
const char EXP_CHARS[] = "eE";

/* Chars that mean this number is a floating point constant */
/* as in 0f123.456 */
/* or    0H1.234E-12 (see exp chars above) */
const char FLT_CHARS[] = "dDfFgGhH";

/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
   changed in read.c .  Ideally it shouldn't have to know about it at all,
   but nothing is ideal around here.  */

/* Hold details of an operand expression */
static expressionS exp_of_operand[VIT_MAX_OPERANDS];
static segT seg_of_operand[VIT_MAX_OPERANDS];

/* A vax instruction after decoding.  */
static struct vit v;

/* Hold details of big operands.  */
LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER];
FLONUM_TYPE float_operand[VIT_MAX_OPERANDS];
/* Above is made to point into big_operand_bits by md_begin().  */

int flag_hash_long_names;	/* -+ */
int flag_one;			/* -1 */
int flag_show_after_trunc;	/* -H */
int flag_no_hash_mixed_case;	/* -h NUM */

/*
 * For VAX, relative addresses of "just the right length" are easy.
 * The branch displacement is always the last operand, even in
 * synthetic instructions.
 * For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
 *
 *		    4       3       2       1       0	     bit number
 *	---/ /--+-------+-------+-------+-------+-------+
 *		|     what state ?	|  how long ?	|
 *	---/ /--+-------+-------+-------+-------+-------+
 *
 * The "how long" bits are 00=byte, 01=word, 10=long.
 * This is a Un*x convention.
 * Not all lengths are legit for a given value of (what state).
 * The "how long" refers merely to the displacement length.
 * The address usually has some constant bytes in it as well.
 *

 groups for VAX address relaxing.

 1.	"foo" pc-relative.
 length of byte, word, long

 2a.	J<cond> where <cond> is a simple flag test.
 length of byte, word, long.
 VAX opcodes are:	(Hex)
 bneq/bnequ	12
 beql/beqlu	13
 bgtr		14
 bleq		15
 bgeq		18
 blss		19
 bgtru		1a
 blequ		1b
 bvc		1c
 bvs		1d
 bgequ/bcc	1e
 blssu/bcs	1f
 Always, you complement 0th bit to reverse condition.
 Always, 1-byte opcode, then 1-byte displacement.

 2b.	J<cond> where cond tests a memory bit.
 length of byte, word, long.
 Vax opcodes are:	(Hex)
 bbs		e0
 bbc		e1
 bbss		e2
 bbcs		e3
 bbsc		e4
 bbcc		e5
 Always, you complement 0th bit to reverse condition.
 Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement

 2c.	J<cond> where cond tests low-order memory bit
 length of byte,word,long.
 Vax opcodes are:	(Hex)
 blbs		e8
 blbc		e9
 Always, you complement 0th bit to reverse condition.
 Always, 1-byte opcode, longword-address, 1-byte displacement.

 3.	Jbs/Jbr.
 length of byte,word,long.
 Vax opcodes are:	(Hex)
 bsbb		10
 brb		11
 These are like (2) but there is no condition to reverse.
 Always, 1 byte opcode, then displacement/absolute.

 4a.	JacbX
 length of word, long.
 Vax opcodes are:	(Hex)
 acbw		3d
 acbf		4f
 acbd		6f
 abcb		9d
 acbl		f1
 acbg	      4ffd
 acbh	      6ffd
 Always, we cannot reverse the sense of the branch; we have a word
 displacement.
 The double-byte op-codes don't hurt: we never want to modify the
 opcode, so we don't care how many bytes are between the opcode and
 the operand.

 4b.	JXobXXX
 length of long, long, byte.
 Vax opcodes are:	(Hex)
 aoblss		f2
 aobleq		f3
 sobgeq		f4
 sobgtr		f5
 Always, we cannot reverse the sense of the branch; we have a byte
 displacement.

 The only time we need to modify the opcode is for class 2 instructions.
 After relax() we may complement the lowest order bit of such instruction
 to reverse sense of branch.

 For class 2 instructions, we store context of "where is the opcode literal".
 We can change an opcode's lowest order bit without breaking anything else.

 We sometimes store context in the operand literal. This way we can figure out
 after relax() what the original addressing mode was.
 */

/* These displacements are relative to the start address of the
   displacement.  The first letter is Byte, Word.  2nd letter is
   Forward, Backward.  */
#define BF (1+ 127)
#define BB (1+-128)
#define WF (2+ 32767)
#define WB (2+-32768)
/* Dont need LF, LB because they always reach. [They are coded as 0.] */

#define C(a,b) ENCODE_RELAX(a,b)
/* This macro has no side-effects.  */
#define ENCODE_RELAX(what,length) (((what) << 2) + (length))

const relax_typeS md_relax_table[] =
{
  {1, 1, 0, 0},			/* error sentinel   0,0	*/
  {1, 1, 0, 0},			/* unused	    0,1	*/
  {1, 1, 0, 0},			/* unused	    0,2	*/
  {1, 1, 0, 0},			/* unused	    0,3	*/
  {BF + 1, BB + 1, 2, C (1, 1)},/* B^"foo"	    1,0 */
  {WF + 1, WB + 1, 3, C (1, 2)},/* W^"foo"	    1,1 */
  {0, 0, 5, 0},			/* L^"foo"	    1,2 */
  {1, 1, 0, 0},			/* unused	    1,3 */
  {BF, BB, 1, C (2, 1)},	/* b<cond> B^"foo"  2,0 */
  {WF + 2, WB + 2, 4, C (2, 2)},/* br.+? brw X	    2,1 */
  {0, 0, 7, 0},			/* br.+? jmp X	    2,2 */
  {1, 1, 0, 0},			/* unused	    2,3 */
  {BF, BB, 1, C (3, 1)},	/* brb B^foo	    3,0 */
  {WF, WB, 2, C (3, 2)},	/* brw W^foo	    3,1 */
  {0, 0, 5, 0},			/* Jmp L^foo	    3,2 */
  {1, 1, 0, 0},			/* unused	    3,3 */
  {1, 1, 0, 0},			/* unused	    4,0 */
  {WF, WB, 2, C (4, 2)},	/* acb_ ^Wfoo	    4,1 */
  {0, 0, 10, 0},		/* acb_,br,jmp L^foo4,2 */
  {1, 1, 0, 0},			/* unused	    4,3 */
  {BF, BB, 1, C (5, 1)},	/* Xob___,,foo      5,0 */
  {WF + 4, WB + 4, 6, C (5, 2)},/* Xob.+2,brb.+3,brw5,1 */
  {0, 0, 9, 0},			/* Xob.+2,brb.+6,jmp5,2 */
};

#undef C
#undef BF
#undef BB
#undef WF
#undef WB

void float_cons ();

const pseudo_typeS md_pseudo_table[] =
{
  {"dfloat", float_cons, 'd'},
  {"ffloat", float_cons, 'f'},
  {"gfloat", float_cons, 'g'},
  {"hfloat", float_cons, 'h'},
  {0},
};

#define STATE_PC_RELATIVE		(1)
#define STATE_CONDITIONAL_BRANCH	(2)
#define STATE_ALWAYS_BRANCH		(3)	/* includes BSB...  */
#define STATE_COMPLEX_BRANCH	        (4)
#define STATE_COMPLEX_HOP		(5)

#define STATE_BYTE			(0)
#define STATE_WORD			(1)
#define STATE_LONG			(2)
#define STATE_UNDF			(3)	/* Symbol undefined in pass1 */

#define min(a, b)	((a) < (b) ? (a) : (b))

int flonum_gen2vax PARAMS ((char format_letter, FLONUM_TYPE * f,
			    LITTLENUM_TYPE * words));
static const char *vip_begin PARAMS ((int, const char *, const char *,
				      const char *));
static void vip_op_defaults PARAMS ((const char *, const char *, const char *));
static void vip_op PARAMS ((char *, struct vop *));
static void vip PARAMS ((struct vit *, char *));

void
md_begin ()
{
  const char *errtxt;
  FLONUM_TYPE *fP;
  int i;

  if ((errtxt = vip_begin (1, "$", "*", "`")) != 0)
    {
      as_fatal (_("VIP_BEGIN error:%s"), errtxt);
    }

  for (i = 0, fP = float_operand;
       fP < float_operand + VIT_MAX_OPERANDS;
       i++, fP++)
    {
      fP->low = &big_operand_bits[i][0];
      fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
    }
}

void
md_number_to_chars (con, value, nbytes)
     char con[];
     valueT value;
     int nbytes;
{
  number_to_chars_littleendian (con, value, nbytes);
}

/* Fix up some data or instructions after we find out the value of a symbol
   that they reference.  */

void				/* Knows about order of bytes in address.  */
md_apply_fix (fixP, value)
     fixS *fixP;
     long value;
{
  number_to_chars_littleendian (fixP->fx_where + fixP->fx_frag->fr_literal,
				(valueT) value, fixP->fx_size);
}

long
md_chars_to_number (con, nbytes)
     unsigned char con[];	/* Low order byte 1st.  */
     int nbytes;		/* Number of bytes in the input.  */
{
  long retval;
  for (retval = 0, con += nbytes - 1; nbytes--; con--)
    {
      retval <<= BITS_PER_CHAR;
      retval |= *con;
    }
  return retval;
}

/* vax:md_assemble() emit frags for 1 instruction */

void
md_assemble (instruction_string)
     char *instruction_string;	/* A string: assemble 1 instruction.  */
{
  /* Non-zero if operand expression's segment is not known yet.  */
  int is_undefined;

  int length_code;
  char *p;
  /* An operand. Scans all operands.  */
  struct vop *operandP;
  char *save_input_line_pointer;
			/* What used to live after an expression.  */
  char c_save;
  /* 1: instruction_string bad for all passes.  */
  int goofed;
  /* Points to slot just after last operand.  */
  struct vop *end_operandP;
  /* Points to expression values for this operand.  */
  expressionS *expP;
  segT *segP;

  /* These refer to an instruction operand expression.  */
  /* Target segment of the address.	 */
  segT to_seg;
  valueT this_add_number;
  /* Positive (minuend) symbol.  */
  symbolS *this_add_symbol;
  /* As a number.  */
  long opcode_as_number;
  /* Least significant byte 1st.  */
  char *opcode_as_chars;
  /* As an array of characters.  */
  /* Least significant byte 1st */
  char *opcode_low_byteP;
  /* length (bytes) meant by vop_short.  */
  int length;
  /* 0, or 1 if '@' is in addressing mode.  */
  int at;
  /* From vop_nbytes: vax_operand_width (in bytes) */
  int nbytes;
  FLONUM_TYPE *floatP;
  LITTLENUM_TYPE literal_float[8];
  /* Big enough for any floating point literal.  */

  vip (&v, instruction_string);

  /*
   * Now we try to find as many as_warn()s as we can. If we do any as_warn()s
   * then goofed=1. Notice that we don't make any frags yet.
   * Should goofed be 1, then this instruction will wedge in any pass,
   * and we can safely flush it, without causing interpass symbol phase
   * errors. That is, without changing label values in different passes.
   */
  if ((goofed = (*v.vit_error)) != 0)
    {
      as_warn (_("Ignoring statement due to \"%s\""), v.vit_error);
    }
  /*
   * We need to use expression() and friends, which require us to diddle
   * input_line_pointer. So we save it and restore it later.
   */
  save_input_line_pointer = input_line_pointer;
  for (operandP = v.vit_operand,
       expP = exp_of_operand,
       segP = seg_of_operand,
       floatP = float_operand,
       end_operandP = v.vit_operand + v.vit_operands;

       operandP < end_operandP;

       operandP++, expP++, segP++, floatP++)
    {				/* for each operand */
      if (operandP->vop_error)
	{
	  as_warn (_("Ignoring statement because \"%s\""), operandP->vop_error);
	  goofed = 1;
	}
      else
	{
	  /* Statement has no syntax goofs: let's sniff the expression.  */
	  int can_be_short = 0;	/* 1 if a bignum can be reduced to a short literal.  */

	  input_line_pointer = operandP->vop_expr_begin;
	  c_save = operandP->vop_expr_end[1];
	  operandP->vop_expr_end[1] = '\0';
	  /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1.  */
	  *segP = expression (expP);
	  switch (expP->X_op)
	    {
	    case O_absent:
	      /* for BSD4.2 compatibility, missing expression is absolute 0 */
	      expP->X_op = O_constant;
	      expP->X_add_number = 0;
	      /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
		 X_add_symbol to any particular value.  But, we will program
		 defensively. Since this situation occurs rarely so it costs
		 us little to do, and stops Dean worrying about the origin of
		 random bits in expressionS's.  */
	      expP->X_add_symbol = NULL;
	      expP->X_op_symbol = NULL;
	      break;

	    case O_symbol:
	    case O_constant:
	      break;

	    default:
	      /*
	       * Major bug. We can't handle the case of a
	       * SEG_OP expression in a VIT_OPCODE_SYNTHETIC
	       * variable-length instruction.
	       * We don't have a frag type that is smart enough to
	       * relax a SEG_OP, and so we just force all
	       * SEG_OPs to behave like SEG_PASS1s.
	       * Clearly, if there is a demand we can invent a new or
	       * modified frag type and then coding up a frag for this
	       * case will be easy. SEG_OP was invented for the
	       * .words after a CASE opcode, and was never intended for
	       * instruction operands.
	       */
	      need_pass_2 = 1;
	      as_warn (_("Can't relocate expression"));
	      break;

	    case O_big:
	      /* Preserve the bits.  */
	      if (expP->X_add_number > 0)
		{
		  bignum_copy (generic_bignum, expP->X_add_number,
			       floatP->low, SIZE_OF_LARGE_NUMBER);
		}
	      else
		{
		  know (expP->X_add_number < 0);
		  flonum_copy (&generic_floating_point_number,
			       floatP);
		  if (strchr ("s i", operandP->vop_short))
		    {
		      /* Could possibly become S^# */
		      flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
		      switch (-expP->X_add_number)
			{
			case 'f':
			  can_be_short =
			    (literal_float[0] & 0xFC0F) == 0x4000
			    && literal_float[1] == 0;
			  break;

			case 'd':
			  can_be_short =
			    (literal_float[0] & 0xFC0F) == 0x4000
			    && literal_float[1] == 0
			    && literal_float[2] == 0
			    && literal_float[3] == 0;
			  break;

			case 'g':
			  can_be_short =
			    (literal_float[0] & 0xFF81) == 0x4000
			    && literal_float[1] == 0
			    && literal_float[2] == 0
			    && literal_float[3] == 0;
			  break;

			case 'h':
			  can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
					  && (literal_float[1] & 0xE000) == 0
					  && literal_float[2] == 0
					  && literal_float[3] == 0
					  && literal_float[4] == 0
					  && literal_float[5] == 0
					  && literal_float[6] == 0
					  && literal_float[7] == 0);
			  break;

			default:
			  BAD_CASE (-expP->X_add_number);
			  break;
			}	/* switch (float type) */
		    }		/* if (could want to become S^#...) */
		}		/* bignum or flonum ? */

	      if (operandP->vop_short == 's'
		  || operandP->vop_short == 'i'
		  || (operandP->vop_short == ' '
		      && operandP->vop_reg == 0xF
		      && (operandP->vop_mode & 0xE) == 0x8))
		{
		  /* Saw a '#'.  */
		  if (operandP->vop_short == ' ')
		    {
		      /* We must chose S^ or I^.  */
		      if (expP->X_add_number > 0)
			{
			  /* Bignum: Short literal impossible.  */
			  operandP->vop_short = 'i';
			  operandP->vop_mode = 8;
			  operandP->vop_reg = 0xF;	/* VAX PC.  */
			}
		      else
			{
			  /* Flonum: Try to do it.  */
			  if (can_be_short)
			    {
			      operandP->vop_short = 's';
			      operandP->vop_mode = 0;
			      operandP->vop_ndx = -1;
			      operandP->vop_reg = -1;
			      expP->X_op = O_constant;
			    }
			  else
			    {
			      operandP->vop_short = 'i';
			      operandP->vop_mode = 8;
			      operandP->vop_reg = 0xF;	/* VAX PC */
			    }
			}	/* bignum or flonum ? */
		    }		/*  if #, but no S^ or I^ seen.  */
		  /* No more ' ' case: either 's' or 'i'.  */
		  if (operandP->vop_short == 's')
		    {
		      /* Wants to be a short literal.  */
		      if (expP->X_add_number > 0)
			{
			  as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
			  operandP->vop_short = 'i';
			  operandP->vop_mode = 8;
			  operandP->vop_reg = 0xF;	/* VAX PC.  */
			}
		      else
			{
			  if (!can_be_short)
			    {
			      as_warn (_("Can't do flonum short literal: immediate mode used."));
			      operandP->vop_short = 'i';
			      operandP->vop_mode = 8;
			      operandP->vop_reg = 0xF;	/* VAX PC.  */
			    }
			  else
			    {	/* Encode short literal now.  */
			      int temp = 0;

			      switch (-expP->X_add_number)
				{
				case 'f':
				case 'd':
				  temp = literal_float[0] >> 4;
				  break;

				case 'g':
				  temp = literal_float[0] >> 1;
				  break;

				case 'h':
				  temp = ((literal_float[0] << 3) & 070)
				    | ((literal_float[1] >> 13) & 07);
				  break;

				default:
				  BAD_CASE (-expP->X_add_number);
				  break;
				}

			      floatP->low[0] = temp & 077;
			      floatP->low[1] = 0;
			    }	/* if can be short literal float */
			}	/* flonum or bignum ? */
		    }
		  else
		    {		/* I^# seen: set it up if float.  */
		      if (expP->X_add_number < 0)
			{
			  memcpy (floatP->low, literal_float, sizeof (literal_float));
			}
		    }		/* if S^# seen.  */
		}
	      else
		{
		  as_warn (_("A bignum/flonum may not be a displacement: 0x%lx used"),
			   (expP->X_add_number = 0x80000000L));
		  /* Chosen so luser gets the most offset bits to patch later.  */
		}
	      expP->X_add_number = floatP->low[0]
		| ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
	      /*
	       * For the O_big case we have:
	       * If vop_short == 's' then a short floating literal is in the
	       *	lowest 6 bits of floatP -> low [0], which is
	       *	big_operand_bits [---] [0].
	       * If vop_short == 'i' then the appropriate number of elements
	       *	of big_operand_bits [---] [...] are set up with the correct
	       *	bits.
	       * Also, just in case width is byte word or long, we copy the lowest
	       * 32 bits of the number to X_add_number.
	       */
	      break;
	    }
	  if (input_line_pointer != operandP->vop_expr_end + 1)
	    {
	      as_warn ("Junk at end of expression \"%s\"", input_line_pointer);
	      goofed = 1;
	    }
	  operandP->vop_expr_end[1] = c_save;
	}
    }				/* for(each operand) */

  input_line_pointer = save_input_line_pointer;

  if (need_pass_2 || goofed)
    {
      return;
    }

  /* Emit op-code.  */
  /* Remember where it is, in case we want to modify the op-code later.  */
  opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
  memcpy (opcode_low_byteP, v.vit_opcode, v.vit_opcode_nbytes);
  opcode_as_number = md_chars_to_number (opcode_as_chars = v.vit_opcode, 4);
  for (operandP = v.vit_operand,
       expP = exp_of_operand,
       segP = seg_of_operand,
       floatP = float_operand,
       end_operandP = v.vit_operand + v.vit_operands;

       operandP < end_operandP;

       operandP++,
       floatP++,
       segP++,
       expP++)
    {
      if (operandP->vop_ndx >= 0)
	{
	  /* indexed addressing byte */
	  /* Legality of indexed mode already checked: it is OK */
	  FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
	}			/* if(vop_ndx>=0) */

      /* Here to make main operand frag(s).  */
      this_add_number = expP->X_add_number;
      this_add_symbol = expP->X_add_symbol;
      to_seg = *segP;
      is_undefined = (to_seg == SEG_UNKNOWN);
      at = operandP->vop_mode & 1;
      length = (operandP->vop_short == 'b'
		? 1 : (operandP->vop_short == 'w'
		       ? 2 : (operandP->vop_short == 'l'
			      ? 4 : 0)));
      nbytes = operandP->vop_nbytes;
      if (operandP->vop_access == 'b')
	{
	  if (to_seg == now_seg || is_undefined)
	    {
	      /* If is_undefined, then it might BECOME now_seg.  */
	      if (nbytes)
		{
		  p = frag_more (nbytes);
		  fix_new (frag_now, p - frag_now->fr_literal, nbytes,
			   this_add_symbol, this_add_number, 1, NO_RELOC);
		}
	      else
		{		/* to_seg==now_seg || to_seg == SEG_UNKNOWN */
		  /* nbytes==0 */
		  length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
		  if (opcode_as_number & VIT_OPCODE_SPECIAL)
		    {
		      if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
			{
			  /* br or jsb */
			  frag_var (rs_machine_dependent, 5, 1,
			    ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
				    this_add_symbol, this_add_number,
				    opcode_low_byteP);
			}
		      else
			{
			  if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
			    {
			      length_code = STATE_WORD;
			      /* JF: There is no state_byte for this one! */
			      frag_var (rs_machine_dependent, 10, 2,
					ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
					this_add_symbol, this_add_number,
					opcode_low_byteP);
			    }
			  else
			    {
			      know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
			      frag_var (rs_machine_dependent, 9, 1,
			      ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
					this_add_symbol, this_add_number,
					opcode_low_byteP);
			    }
			}
		    }
		  else
		    {
		      know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
		      frag_var (rs_machine_dependent, 7, 1,
		       ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
				this_add_symbol, this_add_number,
				opcode_low_byteP);
		    }
		}
	    }
	  else
	    {
	      /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
	      /*
	       * --- SEG FLOAT MAY APPEAR HERE ----
	       */
	      if (to_seg == SEG_ABSOLUTE)
		{
		  if (nbytes)
		    {
		      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
		      p = frag_more (nbytes);
		      /* Conventional relocation.  */
		      fix_new (frag_now, p - frag_now->fr_literal,
			       nbytes, &abs_symbol, this_add_number,
			       1, NO_RELOC);
		    }
		  else
		    {
		      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
		      if (opcode_as_number & VIT_OPCODE_SPECIAL)
			{
			  if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
			    {
			      /* br or jsb */
			      *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
			      know (opcode_as_chars[1] == 0);
			      p = frag_more (5);
			      p[0] = VAX_ABSOLUTE_MODE;	/* @#...  */
			      md_number_to_chars (p + 1, this_add_number, 4);
			      /* Now (eg) JMP @#foo or JSB @#foo.  */
			    }
			  else
			    {
			      if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
				{
				  p = frag_more (10);
				  p[0] = 2;
				  p[1] = 0;
				  p[2] = VAX_BRB;
				  p[3] = 6;
				  p[4] = VAX_JMP;
				  p[5] = VAX_ABSOLUTE_MODE;	/* @#...  */
				  md_number_to_chars (p + 6, this_add_number, 4);
				  /*
				   * Now (eg)	ACBx	1f
				   *		BRB	2f
				   *	1:	JMP	@#foo
				   *	2:
				   */
				}
			      else
				{
				  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
				  p = frag_more (9);
				  p[0] = 2;
				  p[1] = VAX_BRB;
				  p[2] = 6;
				  p[3] = VAX_JMP;
                                  p[4] = VAX_ABSOLUTE_MODE;     /* @#...  */
				  md_number_to_chars (p + 5, this_add_number, 4);
				  /*
				   * Now (eg)	xOBxxx	1f
				   *		BRB	2f
				   *	1:	JMP	@#foo
				   *	2:
				   */
				}
			    }
			}
		      else
			{
			  /* b<cond> */
			  *opcode_low_byteP ^= 1;
			  /* To reverse the condition in a VAX branch,
			     complement the lowest order bit.  */
			  p = frag_more (7);
			  p[0] = 6;
			  p[1] = VAX_JMP;
			  p[2] = VAX_ABSOLUTE_MODE;	/* @#...  */
			  md_number_to_chars (p + 3, this_add_number, 4);
			  /*
			   * Now (eg)	BLEQ	1f
			   *		JMP	@#foo
			   *	1:
			   */
			}
		    }
		}
	      else
		{
		  /* to_seg != now_seg && to_seg != SEG_UNKNOWN && to_Seg != SEG_ABSOLUTE */
		  if (nbytes > 0)
		    {
		      /* Pc-relative. Conventional relocation.  */
		      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
		      p = frag_more (nbytes);
		      fix_new (frag_now, p - frag_now->fr_literal,
			       nbytes, &abs_symbol, this_add_number,
			       1, NO_RELOC);
		    }
		  else
		    {
		      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
		      if (opcode_as_number & VIT_OPCODE_SPECIAL)
			{
			  if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
			    {
			      /* br or jsb */
			      know (opcode_as_chars[1] == 0);
			      *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
			      p = frag_more (5);
			      p[0] = VAX_PC_RELATIVE_MODE;
			      fix_new (frag_now,
				       p + 1 - frag_now->fr_literal, 4,
				       this_add_symbol,
				       this_add_number, 1, NO_RELOC);
			      /* Now eg JMP foo or JSB foo.  */
			    }
			  else
			    {
			      if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
				{
				  p = frag_more (10);
				  p[0] = 0;
				  p[1] = 2;
				  p[2] = VAX_BRB;
				  p[3] = 6;
				  p[4] = VAX_JMP;
				  p[5] = VAX_PC_RELATIVE_MODE;
				  fix_new (frag_now,
					   p + 6 - frag_now->fr_literal, 4,
					   this_add_symbol,
					   this_add_number, 1, NO_RELOC);
				  /*
				   * Now (eg)	ACBx	1f
				   *		BRB	2f
				   *	1:	JMP	foo
				   *	2:
				   */
				}
			      else
				{
				  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
				  p = frag_more (10);
				  p[0] = 2;
				  p[1] = VAX_BRB;
				  p[2] = 6;
				  p[3] = VAX_JMP;
				  p[4] = VAX_PC_RELATIVE_MODE;
				  fix_new (frag_now,
					   p + 5 - frag_now->fr_literal,
					   4, this_add_symbol,
					   this_add_number, 1, NO_RELOC);
				  /*
				   * Now (eg)	xOBxxx	1f
				   *		BRB	2f
				   *	1:	JMP	foo
				   *	2:
				   */
				}
			    }
			}
		      else
			{
			  know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
			  *opcode_low_byteP ^= 1;	/* Reverse branch condition.  */
			  p = frag_more (7);
			  p[0] = 6;
			  p[1] = VAX_JMP;
			  p[2] = VAX_PC_RELATIVE_MODE;
			  fix_new (frag_now, p + 3 - frag_now->fr_literal,
				   4, this_add_symbol,
				   this_add_number, 1, NO_RELOC);
			}
		    }
		}
	    }
	}
      else
	{
	  know (operandP->vop_access != 'b');	/* So it is ordinary operand.  */
	  know (operandP->vop_access != ' ');	/* ' ' target-independent: elsewhere.  */
	  know (operandP->vop_access == 'a'
		|| operandP->vop_access == 'm'
		|| operandP->vop_access == 'r'
		|| operandP->vop_access == 'v'
		|| operandP->vop_access == 'w');
	  if (operandP->vop_short == 's')
	    {
	      if (to_seg == SEG_ABSOLUTE)
		{
		  if (this_add_number >= 64)
		    {
		      as_warn (_("Short literal overflow(%ld.), immediate mode assumed."),
			       (long) this_add_number);
		      operandP->vop_short = 'i';
		      operandP->vop_mode = 8;
		      operandP->vop_reg = 0xF;
		    }
		}
	      else
		{
		  as_warn (_("Forced short literal to immediate mode. now_seg=%s to_seg=%s"),
			   segment_name (now_seg), segment_name (to_seg));
		  operandP->vop_short = 'i';
		  operandP->vop_mode = 8;
		  operandP->vop_reg = 0xF;
		}
	    }
	  if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
		  || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
	    {
	      /* One byte operand.  */
	      know (operandP->vop_mode > 3);
	      FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
	      /* All 1-bytes except S^# happen here.  */
	    }
	  else
	    {
	      /* {@}{q^}foo{(Rn)} or S^#foo */
	      if (operandP->vop_reg == -1 && operandP->vop_short != 's')
		{
		  /* "{@}{q^}foo" */
		  if (to_seg == now_seg)
		    {
		      if (length == 0)
			{
			  know (operandP->vop_short == ' ');
			  p = frag_var (rs_machine_dependent, 10, 2,
			       ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE),
					this_add_symbol, this_add_number,
					opcode_low_byteP);
			  know (operandP->vop_mode == 10 + at);
			  *p = at << 4;
			  /* At is the only context we need to carry
			     to other side of relax() process.  Must
			     be in the correct bit position of VAX
			     operand spec. byte.  */
			}
		      else
			{
			  know (length);
			  know (operandP->vop_short != ' ');
			  p = frag_more (length + 1);
			  p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
			  fix_new (frag_now, p + 1 - frag_now->fr_literal,
				   length, this_add_symbol,
				   this_add_number, 1, NO_RELOC);
			}
		    }
		  else
		    {		/* to_seg != now_seg */
		      if (this_add_symbol == NULL)
			{
			  know (to_seg == SEG_ABSOLUTE);
			  /* Do @#foo: simpler relocation than foo-.(pc) anyway.  */
			  p = frag_more (5);
			  p[0] = VAX_ABSOLUTE_MODE;	/* @#...  */
			  md_number_to_chars (p + 1, this_add_number, 4);
			  if (length && length != 4)
			    {
			      as_warn (_("Length specification ignored. Address mode 9F used"));
			    }
			}
		      else
			{
			  /* {@}{q^}other_seg */
			  know ((length == 0 && operandP->vop_short == ' ')
			     || (length > 0 && operandP->vop_short != ' '));
			  if (is_undefined)
			    {
			      /*
			       * We have a SEG_UNKNOWN symbol. It might
			       * turn out to be in the same segment as
			       * the instruction, permitting relaxation.
			       */
			      p = frag_var (rs_machine_dependent, 5, 2,
			       ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
					    this_add_symbol, this_add_number,
					    0);
			      p[0] = at << 4;
			    }
			  else
			    {
			      if (length == 0)
				{
				  know (operandP->vop_short == ' ');
				  length = 4;	/* Longest possible.  */
				}
			      p = frag_more (length + 1);
			      p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
			      md_number_to_chars (p + 1, this_add_number, length);
			      fix_new (frag_now,
				       p + 1 - frag_now->fr_literal,
				       length, this_add_symbol,
				       this_add_number, 1, NO_RELOC);
			    }
			}
		    }
		}
	      else
		{
		  /* {@}{q^}foo(Rn) or S^# or I^# or # */
		  if (operandP->vop_mode < 0xA)
		    {
		      /* # or S^# or I^# */
		      if (operandP->vop_access == 'v'
			  || operandP->vop_access == 'a')
			{
			  if (operandP->vop_access == 'v')
			    as_warn (_("Invalid operand:  immediate value used as base address."));
			  else
			    as_warn (_("Invalid operand:  immediate value used as address."));
			  /* gcc 2.6.3 is known to generate these in at least
			     one case.  */
			}
		      if (length == 0
			  && to_seg == SEG_ABSOLUTE && (expP->X_op != O_big)
			  && operandP->vop_mode == 8	/* No '@'.  */
			  && this_add_number < 64)
			{
			  operandP->vop_short = 's';
			}
		      if (operandP->vop_short == 's')
			{
			  FRAG_APPEND_1_CHAR (this_add_number);
			}
		      else
			{
			  /* I^#...  */
			  know (nbytes);
			  p = frag_more (nbytes + 1);
			  know (operandP->vop_reg == 0xF);
			  p[0] = (operandP->vop_mode << 4) | 0xF;
			  if ((to_seg == SEG_ABSOLUTE) && (expP->X_op != O_big))
			    {
			      /*
			       * If nbytes > 4, then we are scrod. We
			       * don't know if the high order bytes
			       * are to be 0xFF or 0x00.  BSD4.2 & RMS
			       * say use 0x00. OK --- but this
			       * assembler needs ANOTHER rewrite to
			       * cope properly with this bug.  */
			      md_number_to_chars (p + 1, this_add_number, min (4, nbytes));
			      if (nbytes > 4)
				{
				  memset (p + 5, '\0', nbytes - 4);
				}
			    }
			  else
			    {
			      if (expP->X_op == O_big)
				{
				  /*
				   * Problem here is to get the bytes
				   * in the right order.  We stored
				   * our constant as LITTLENUMs, not
				   * bytes.  */
				  LITTLENUM_TYPE *lP;

				  lP = floatP->low;
				  if (nbytes & 1)
				    {
				      know (nbytes == 1);
				      p[1] = *lP;
				    }
				  else
				    {
				      for (p++; nbytes; nbytes -= 2, p += 2, lP++)
					{
					  md_number_to_chars (p, *lP, 2);
					}
				    }
				}
			      else
				{
				  fix_new (frag_now, p + 1 - frag_now->fr_literal,
					   nbytes, this_add_symbol,
					   this_add_number, 0, NO_RELOC);
				}
			    }
			}
		    }
		  else
		    {		/* {@}{q^}foo(Rn) */
		      know ((length == 0 && operandP->vop_short == ' ')
			    || (length > 0 && operandP->vop_short != ' '));
		      if (length == 0)
			{
			  if (to_seg == SEG_ABSOLUTE)
			    {
			      long test;

			      test = this_add_number;

			      if (test < 0)
				test = ~test;

			      length = test & 0xffff8000 ? 4
				: test & 0xffffff80 ? 2
				: 1;
			    }
			  else
			    {
			      length = 4;
			    }
			}
		      p = frag_more (1 + length);
		      know (operandP->vop_reg >= 0);
		      p[0] = operandP->vop_reg
			| ((at | "?\12\14?\16"[length]) << 4);
		      if (to_seg == SEG_ABSOLUTE)
			{
			  md_number_to_chars (p + 1, this_add_number, length);
			}
		      else
			{
			  fix_new (frag_now, p + 1 - frag_now->fr_literal,
				   length, this_add_symbol,
				   this_add_number, 0, NO_RELOC);
			}
		    }
		}
	    }			/* if(single-byte-operand) */
	}
    }				/* for(operandP) */
}				/* vax_assemble() */

/*
 *			md_estimate_size_before_relax()
 *
 * Called just before relax().
 * Any symbol that is now undefined will not become defined.
 * Return the correct fr_subtype in the frag.
 * Return the initial "guess for fr_var" to caller.
 * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
 * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
 * Although it may not be explicit in the frag, pretend fr_var starts with a
 * 0 value.
 */
int
md_estimate_size_before_relax (fragP, segment)
     fragS *fragP;
     segT segment;
{
  char *p;
  int old_fr_fix;

  old_fr_fix = fragP->fr_fix;
  switch (fragP->fr_subtype)
    {
    case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF):
      if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
	{			/* A relaxable case.  */
	  fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
	}
      else
	{
	  p = fragP->fr_literal + old_fr_fix;
	  p[0] |= VAX_PC_RELATIVE_MODE;	/* Preserve @ bit.  */
	  fragP->fr_fix += 1 + 4;
	  fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
		   fragP->fr_offset, 1, NO_RELOC);
	  frag_wane (fragP);
	}
      break;

    case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF):
      if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
	{
	  fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
	}
      else
	{
	  p = fragP->fr_literal + old_fr_fix;
	  *fragP->fr_opcode ^= 1;	/* Reverse sense of branch.  */
	  p[0] = 6;
	  p[1] = VAX_JMP;
	  p[2] = VAX_PC_RELATIVE_MODE;	/* ...(PC) */
	  fragP->fr_fix += 1 + 1 + 1 + 4;
	  fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol,
		   fragP->fr_offset, 1, NO_RELOC);
	  frag_wane (fragP);
	}
      break;

    case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_UNDF):
      if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
	{
	  fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
	}
      else
	{
	  p = fragP->fr_literal + old_fr_fix;
	  p[0] = 2;
	  p[1] = 0;
	  p[2] = VAX_BRB;
	  p[3] = 6;
	  p[4] = VAX_JMP;
	  p[5] = VAX_PC_RELATIVE_MODE;	/* ...(pc) */
	  fragP->fr_fix += 2 + 2 + 1 + 1 + 4;
	  fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol,
		   fragP->fr_offset, 1, NO_RELOC);
	  frag_wane (fragP);
	}
      break;

    case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_UNDF):
      if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
	{
	  fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
	}
      else
	{
	  p = fragP->fr_literal + old_fr_fix;
	  p[0] = 2;
	  p[1] = VAX_BRB;
	  p[2] = 6;
	  p[3] = VAX_JMP;
	  p[4] = VAX_PC_RELATIVE_MODE;	/* ...(pc) */
	  fragP->fr_fix += 1 + 2 + 1 + 1 + 4;
	  fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol,
		   fragP->fr_offset, 1, NO_RELOC);
	  frag_wane (fragP);
	}
      break;

    case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_UNDF):
      if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
	{
	  fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
	}
      else
	{
	  p = fragP->fr_literal + old_fr_fix;
	  *fragP->fr_opcode += VAX_WIDEN_LONG;
	  p[0] = VAX_PC_RELATIVE_MODE;	/* ...(PC) */
	  fragP->fr_fix += 1 + 4;
	  fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
		   fragP->fr_offset, 1, NO_RELOC);
	  frag_wane (fragP);
	}
      break;

    default:
      break;
    }
  return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
}				/* md_estimate_size_before_relax() */

/*
 *			md_convert_frag();
 *
 * Called after relax() is finished.
 * In:	Address of frag.
 *	fr_type == rs_machine_dependent.
 *	fr_subtype is what the address relaxed to.
 *
 * Out:	Any fixSs and constants are set up.
 *	Caller will turn frag into a ".space 0".
 */
void
md_convert_frag (headers, seg, fragP)
     object_headers *headers;
     segT seg;
     fragS *fragP;
{
  char *addressP;		/* -> _var to change.  */
  char *opcodeP;		/* -> opcode char(s) to change.  */
  short int length_code;	/* 2=long 1=word 0=byte */
  short int extension = 0;	/* Size of relaxed address.  */
  /* Added to fr_fix: incl. ALL var chars.  */
  symbolS *symbolP;
  long where;
  long address_of_var;
  /* Where, in file space, is _var of *fragP? */
  long target_address = 0;
  /* Where, in file space, does addr point? */

  know (fragP->fr_type == rs_machine_dependent);
  length_code = fragP->fr_subtype & 3;	/* depends on ENCODE_RELAX() */
  know (length_code >= 0 && length_code < 3);
  where = fragP->fr_fix;
  addressP = fragP->fr_literal + where;
  opcodeP = fragP->fr_opcode;
  symbolP = fragP->fr_symbol;
  know (symbolP);
  target_address = S_GET_VALUE (symbolP) + fragP->fr_offset;
  address_of_var = fragP->fr_address + where;

  switch (fragP->fr_subtype)
    {

    case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
      know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
      addressP[0] |= 0xAF;	/* Byte displacement.  */
      addressP[1] = target_address - (address_of_var + 2);
      extension = 2;
      break;

    case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
      know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
      addressP[0] |= 0xCF;	/* Word displacement.  */
      md_number_to_chars (addressP + 1, target_address - (address_of_var + 3), 2);
      extension = 3;
      break;

    case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
      know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
      addressP[0] |= 0xEF;	/* Long word displacement.  */
      md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4);
      extension = 5;
      break;

    case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
      addressP[0] = target_address - (address_of_var + 1);
      extension = 1;
      break;

    case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
      opcodeP[0] ^= 1;		/* Reverse sense of test.  */
      addressP[0] = 3;
      addressP[1] = VAX_BRB + VAX_WIDEN_WORD;
      md_number_to_chars (addressP + 2, target_address - (address_of_var + 4), 2);
      extension = 4;
      break;

    case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
      opcodeP[0] ^= 1;		/* Reverse sense of test.  */
      addressP[0] = 6;
      addressP[1] = VAX_JMP;
      addressP[2] = VAX_PC_RELATIVE_MODE;
      md_number_to_chars (addressP + 3, target_address - (address_of_var + 7), 4);
      extension = 7;
      break;

    case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
      addressP[0] = target_address - (address_of_var + 1);
      extension = 1;
      break;

    case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
      opcodeP[0] += VAX_WIDEN_WORD;	/* brb -> brw, bsbb -> bsbw */
      md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
      extension = 2;
      break;

    case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
      opcodeP[0] += VAX_WIDEN_LONG;	/* brb -> jmp, bsbb -> jsb */
      addressP[0] = VAX_PC_RELATIVE_MODE;
      md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4);
      extension = 5;
      break;

    case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
      md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
      extension = 2;
      break;

    case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
      addressP[0] = 2;
      addressP[1] = 0;
      addressP[2] = VAX_BRB;
      addressP[3] = 6;
      addressP[4] = VAX_JMP;
      addressP[5] = VAX_PC_RELATIVE_MODE;
      md_number_to_chars (addressP + 6, target_address - (address_of_var + 10), 4);
      extension = 10;
      break;

    case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
      addressP[0] = target_address - (address_of_var + 1);
      extension = 1;
      break;

    case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
      addressP[0] = 2;
      addressP[1] = VAX_BRB;
      addressP[2] = 3;
      addressP[3] = VAX_BRW;
      md_number_to_chars (addressP + 4, target_address - (address_of_var + 6), 2);
      extension = 6;
      break;

    case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
      addressP[0] = 2;
      addressP[1] = VAX_BRB;
      addressP[2] = 6;
      addressP[3] = VAX_JMP;
      addressP[4] = VAX_PC_RELATIVE_MODE;
      md_number_to_chars (addressP + 5, target_address - (address_of_var + 9), 4);
      extension = 9;
      break;

    default:
      BAD_CASE (fragP->fr_subtype);
      break;
    }
  fragP->fr_fix += extension;
}				/* md_convert_frag() */

/* Translate internal format of relocation info into target format.

   On vax: first 4 bytes are normal unsigned long, next three bytes
   are symbolnum, least sig. byte first.  Last byte is broken up with
   the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
   bit 0 as pcrel.  */
#ifdef comment
void
md_ri_to_chars (the_bytes, ri)
     char *the_bytes;
     struct reloc_info_generic ri;
{
  /* this is easy */
  md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
  /* now the fun stuff */
  the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
  the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
  the_bytes[4] = ri.r_symbolnum & 0x0ff;
  the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06) |
		  ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
}

#endif /* comment */

void
tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
     char *where;
     fixS *fixP;
     relax_addressT segment_address_in_file;
{
  /*
   * In: length of relocation (or of address) in chars: 1, 2 or 4.
   * Out: GNU LD relocation length code: 0, 1, or 2.
   */

  static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
  long r_symbolnum;

  know (fixP->fx_addsy != NULL);

  md_number_to_chars (where,
       fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
		      4);

  r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
		 ? S_GET_TYPE (fixP->fx_addsy)
		 : fixP->fx_addsy->sy_number);

  where[6] = (r_symbolnum >> 16) & 0x0ff;
  where[5] = (r_symbolnum >> 8) & 0x0ff;
  where[4] = r_symbolnum & 0x0ff;
  where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
	      | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
	      | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
}

/*
 *       BUGS, GRIPES,  APOLOGIA, etc.
 *
 * The opcode table 'votstrs' needs to be sorted on opcode frequency.
 * That is, AFTER we hash it with hash_...(), we want most-used opcodes
 * to come out of the hash table faster.
 *
 * I am sorry to inflict yet another VAX assembler on the world, but
 * RMS says we must do everything from scratch, to prevent pin-heads
 * restricting this software.
 */

/*
 * This is a vaguely modular set of routines in C to parse VAX
 * assembly code using DEC mnemonics. It is NOT un*x specific.
 *
 * The idea here is that the assembler has taken care of all:
 *   labels
 *   macros
 *   listing
 *   pseudo-ops
 *   line continuation
 *   comments
 *   condensing any whitespace down to exactly one space
 * and all we have to do is parse 1 line into a vax instruction
 * partially formed. We will accept a line, and deliver:
 *   an error message (hopefully empty)
 *   a skeleton VAX instruction (tree structure)
 *   textual pointers to all the operand expressions
 *   a warning message that notes a silly operand (hopefully empty)
 */

/*
 *		E D I T   H I S T O R Y
 *
 * 17may86 Dean Elsner. Bug if line ends immediately after opcode.
 * 30apr86 Dean Elsner. New vip_op() uses arg block so change call.
 *  6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
 *  2jan86 Dean Elsner. Invent synthetic opcodes.
 *	Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
 *	which means this is not a real opcode, it is like a macro; it will
 *	be relax()ed into 1 or more instructions.
 *	Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
 *	like a regular branch instruction. Option added to vip_begin():
 *	exclude	synthetic opcodes. Invent synthetic_votstrs[].
 * 31dec85 Dean Elsner. Invent vit_opcode_nbytes.
 *	Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
 *	so caller's don't have to know the difference between a 1-byte & a
 *	2-byte op-code. Still need vax_opcodeT concept, so we know how
 *	big an object must be to hold an op.code.
 * 30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
 *	because vax opcodes may be 16 bits. Our crufty C compiler was
 *	happily initialising 8-bit vot_codes with 16-bit numbers!
 *	(Wouldn't the 'phone company like to compress data so easily!)
 * 29dec85 Dean Elsner. New static table vax_operand_width_size[].
 *	Invented so we know hw many bytes a "I^#42" needs in its immediate
 *	operand. Revised struct vop in "vax-inst.h": explicitly include
 *	byte length of each operand, and it's letter-code datum type.
 * 17nov85 Dean Elsner. Name Change.
 *	Due to ar(1) truncating names, we learned the hard way that
 *	"vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
 *	the archived object name. SO... we shortened the name of this
 *	source file, and changed the makefile.
 */

/* handle of the OPCODE hash table */
static struct hash_control *op_hash;

/*
 * In:	1 character, from "bdfghloqpw" being the data-type of an operand
 *	of a vax instruction.
 *
 * Out:	the length of an operand of that type, in bytes.
 *	Special branch operands types "-?!" have length 0.
 */

static const short int vax_operand_width_size[256] =
{
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,	/* ..b.d.fgh...l..o  */
  0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,	/* .q.....w........  */
  0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,	/* ..b.d.fgh...l..o  */
  0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,	/* .q.....w........  */
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
};

/*
 * This perversion encodes all the vax opcodes as a bunch of strings.
 * RMS says we should build our hash-table at run-time. Hmm.
 * Please would someone arrange these in decreasing frequency of opcode?
 * Because of the way hash_...() works, the most frequently used opcode
 * should be textually first and so on.
 *
 * Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
 * So change 'vax.opcodes', then re-generate this table.
 */

#include "opcode/vax.h"

/*
 * This is a table of optional op-codes. All of them represent
 * 'synthetic' instructions that seem popular.
 *
 * Here we make some pseudo op-codes. Every code has a bit set to say
 * it is synthetic. This lets you catch them if you want to
 * ban these opcodes. They are mnemonics for "elastic" instructions
 * that are supposed to assemble into the fewest bytes needed to do a
 * branch, or to do a conditional branch, or whatever.
 *
 * The opcode is in the usual place [low-order n*8 bits]. This means
 * that if you mask off the bucky bits, the usual rules apply about
 * how long the opcode is.
 *
 * All VAX branch displacements come at the end of the instruction.
 * For simple branches (1-byte opcode + 1-byte displacement) the last
 * operand is coded 'b?' where the "data type" '?' is a clue that we
 * may reverse the sense of the branch (complement lowest order bit)
 * and branch around a jump. This is by far the most common case.
 * That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
 * a 0-byte op-code followed by 2 or more bytes of operand address.
 *
 * If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
 * case.
 *
 * For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
 * option before (2) we can directly JSB/JMP because there is no condition.
 * These operands have 'b-' as their access/data type.
 *
 * That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
 * cases, we do the same idea. JACBxxx are all marked with a 'b!'
 * JAOBxxx & JSOBxxx are marked with a 'b:'.
 *
 */
#if (VIT_OPCODE_SYNTHETIC != 0x80000000)
You have just broken the encoding below, which assumes the sign bit
  means 'I am an imaginary instruction'.
#endif

#if (VIT_OPCODE_SPECIAL != 0x40000000)
  You have just broken the encoding below, which assumes the 0x40 M bit means
  'I am not to be "optimised" the way normal branches are'.
#endif

static const struct vot
  synthetic_votstrs[] =
{
  {"jbsb",	{"b-", 0xC0000010}},		/* BSD 4.2 */
/* jsb used already */
  {"jbr",	{"b-", 0xC0000011}},		/* BSD 4.2 */
  {"jr",	{"b-", 0xC0000011}},		/* consistent */
  {"jneq",	{"b?", 0x80000012}},
  {"jnequ",	{"b?", 0x80000012}},
  {"jeql",	{"b?", 0x80000013}},
  {"jeqlu",	{"b?", 0x80000013}},
  {"jgtr",	{"b?", 0x80000014}},
  {"jleq",	{"b?", 0x80000015}},
/* un-used opcodes here */
  {"jgeq",	{"b?", 0x80000018}},
  {"jlss",	{"b?", 0x80000019}},
  {"jgtru",	{"b?", 0x8000001a}},
  {"jlequ",	{"b?", 0x8000001b}},
  {"jvc",	{"b?", 0x8000001c}},
  {"jvs",	{"b?", 0x8000001d}},
  {"jgequ",	{"b?", 0x8000001e}},
  {"jcc",	{"b?", 0x8000001e}},
  {"jlssu",	{"b?", 0x8000001f}},
  {"jcs",	{"b?", 0x8000001f}},

  {"jacbw",	{"rwrwmwb!", 0xC000003d}},
  {"jacbf",	{"rfrfmfb!", 0xC000004f}},
  {"jacbd",	{"rdrdmdb!", 0xC000006f}},
  {"jacbb",	{"rbrbmbb!", 0xC000009d}},
  {"jacbl",	{"rlrlmlb!", 0xC00000f1}},
  {"jacbg",	{"rgrgmgb!", 0xC0004ffd}},
  {"jacbh",	{"rhrhmhb!", 0xC0006ffd}},

  {"jbs",	{"rlvbb?", 0x800000e0}},
  {"jbc",	{"rlvbb?", 0x800000e1}},
  {"jbss",	{"rlvbb?", 0x800000e2}},
  {"jbcs",	{"rlvbb?", 0x800000e3}},
  {"jbsc",	{"rlvbb?", 0x800000e4}},
  {"jbcc",	{"rlvbb?", 0x800000e5}},
  {"jlbs",	{"rlb?", 0x800000e8}},
  {"jlbc",	{"rlb?", 0x800000e9}},

  {"jaoblss",	{"rlmlb:", 0xC00000f2}},
  {"jaobleq",	{"rlmlb:", 0xC00000f3}},
  {"jsobgeq",	{"mlb:", 0xC00000f4}},
  {"jsobgtr",	{"mlb:", 0xC00000f5}},

/* CASEx has no branch addresses in our conception of it.  */
/* You should use ".word ..." statements after the "case ...".  */

  {"",	{"", 0}}			/* empty is end sentinel */

};				/* synthetic_votstrs */

/*
 *                  v i p _ b e g i n ( )
 *
 * Call me once before you decode any lines.
 * I decode votstrs into a hash table at op_hash (which I create).
 * I return an error text or null.
 * If you want, I will include the 'synthetic' jXXX instructions in the
 * instruction table.
 * You must nominate metacharacters for eg DEC's "#", "@", "^".
 */

static const char *
vip_begin (synthetic_too, immediate, indirect, displen)
     int synthetic_too;		/* 1 means include jXXX op-codes.  */
     const char *immediate, *indirect, *displen;
{
  const struct vot *vP;		/* scan votstrs */
  const char *retval = 0;	/* error text */

  op_hash = hash_new ();

  for (vP = votstrs; *vP->vot_name && !retval; vP++)
    retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);

  if (synthetic_too)
    for (vP = synthetic_votstrs; *vP->vot_name && !retval; vP++)
      retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);

#ifndef CONST_TABLE
  vip_op_defaults (immediate, indirect, displen);
#endif

  return retval;
}

/*
 *                  v i p ( )
 *
 * This converts a string into a vax instruction.
 * The string must be a bare single instruction in dec-vax (with BSD4 frobs)
 * format.
 * It provides some error messages: at most one fatal error message (which
 * stops the scan) and at most one warning message for each operand.
 * The vax instruction is returned in exploded form, since we have no
 * knowledge of how you parse (or evaluate) your expressions.
 * We do however strip off and decode addressing modes and operation
 * mnemonic.
 *
 * The exploded instruction is returned to a struct vit of your choice.
 * #include "vax-inst.h" to know what a struct vit is.
 *
 * This function's value is a string. If it is not "" then an internal
 * logic error was found: read this code to assign meaning to the string.
 * No argument string should generate such an error string:
 * it means a bug in our code, not in the user's text.
 *
 * You MUST have called vip_begin() once before using this function.
 */

static void
vip (vitP, instring)
     struct vit *vitP;		/* We build an exploded instruction here.  */
     char *instring;		/* Text of a vax instruction: we modify.  */
{
  /* How to bit-encode this opcode.  */
  struct vot_wot *vwP;
  /* 1/skip whitespace.2/scan vot_how */
  char *p;
  char *q;
  /* counts number of operands seen */
  unsigned char count;
  /* scan operands in struct vit */
  struct vop *operandp;
  /* error over all operands */
  const char *alloperr;
  /* Remember char, (we clobber it with '\0' temporarily).  */
  char c;
  /* Op-code of this instruction.  */
  vax_opcodeT oc;

  if (*instring == ' ')
    ++instring;			/* Skip leading whitespace.  */
  for (p = instring; *p && *p != ' '; p++);;	/* MUST end in end-of-string or exactly 1 space.  */
  /* Scanned up to end of operation-code.  */
  /* Operation-code is ended with whitespace.  */
  if (p - instring == 0)
    {
      vitP->vit_error = _("No operator");
      count = 0;
      memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
    }
  else
    {
      c = *p;
      *p = '\0';
      /*
       * Here with instring pointing to what better be an op-name, and p
       * pointing to character just past that.
       * We trust instring points to an op-name, with no whitespace.
       */
      vwP = (struct vot_wot *) hash_find (op_hash, instring);
      *p = c;			/* Restore char after op-code.  */
      if (vwP == 0)
	{
	  vitP->vit_error = _("Unknown operator");
	  count = 0;
	  memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
	}
      else
	{
	  /*
	   * We found a match! So let's pick up as many operands as the
	   * instruction wants, and even gripe if there are too many.
	   * We expect comma to seperate each operand.
	   * We let instring track the text, while p tracks a part of the
	   * struct vot.
	   */
	  const char *howp;
	  /*
	   * The lines below know about 2-byte opcodes starting FD,FE or FF.
	   * They also understand synthetic opcodes. Note:
	   * we return 32 bits of opcode, including bucky bits, BUT
	   * an opcode length is either 8 or 16 bits for vit_opcode_nbytes.
	   */
	  oc = vwP->vot_code;	/* The op-code.  */
	  vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
	  md_number_to_chars (vitP->vit_opcode, oc, 4);
	  count = 0;		/* no operands seen yet */
	  instring = p;		/* point just past operation code */
	  alloperr = "";
	  for (howp = vwP->vot_how, operandp = vitP->vit_operand;
	       !(alloperr && *alloperr) && *howp;
	       operandp++, howp += 2)
	    {
	      /*
	       * Here to parse one operand. Leave instring pointing just
	       * past any one ',' that marks the end of this operand.
	       */
	      if (!howp[1])
		as_fatal (_("odd number of bytes in operand description"));
	      else if (*instring)
		{
		  for (q = instring; (c = *q) && c != ','; q++)
		    ;
		  /*
		   * Q points to ',' or '\0' that ends argument. C is that
		   * character.
		   */
		  *q = 0;
		  operandp->vop_width = howp[1];
		  operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
		  operandp->vop_access = howp[0];
		  vip_op (instring, operandp);
		  *q = c;	/* Restore input text.  */
		  if (operandp->vop_error)
		    alloperr = _("Bad operand");
		  instring = q + (c ? 1 : 0);	/* next operand (if any) */
		  count++;	/*  won another argument, may have an operr */
		}
	      else
		alloperr = _("Not enough operands");
	    }
	  if (!*alloperr)
	    {
	      if (*instring == ' ')
		instring++;	/* Skip whitespace.  */
	      if (*instring)
		alloperr = _("Too many operands");
	    }
	  vitP->vit_error = alloperr;
	}
    }
  vitP->vit_operands = count;
}

#ifdef test

/*
 * Test program for above.
 */

struct vit myvit;		/* build an exploded vax instruction here */
char answer[100];		/* human types a line of vax assembler here */
char *mybug;			/* "" or an internal logic diagnostic */
int mycount;			/* number of operands */
struct vop *myvop;		/* scan operands from myvit */
int mysynth;			/* 1 means want synthetic opcodes.  */
char my_immediate[200];
char my_indirect[200];
char my_displen[200];

main ()
{
  char *p;

  printf ("0 means no synthetic instructions.   ");
  printf ("Value for vip_begin?  ");
  gets (answer);
  sscanf (answer, "%d", &mysynth);
  printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
  printf ("enter immediate symbols eg enter #   ");
  gets (my_immediate);
  printf ("enter indirect symbols  eg enter @   ");
  gets (my_indirect);
  printf ("enter displen symbols   eg enter ^   ");
  gets (my_displen);
  if (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))
    {
      error ("vip_begin=%s", p);
    }
  printf ("An empty input line will quit you from the vax instruction parser\n");
  for (;;)
    {
      printf ("vax instruction: ");
      fflush (stdout);
      gets (answer);
      if (!*answer)
	{
	  break;		/* out of for each input text loop */
	}
      vip (&myvit, answer);
      if (*myvit.vit_error)
	{
	  printf ("ERR:\"%s\"\n", myvit.vit_error);
	}
      printf ("opcode=");
      for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
	   mycount;
	   mycount--, p++
	)
	{
	  printf ("%02x ", *p & 0xFF);
	}
      printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
      for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
	{
	  printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
		  myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
		  myvop->vop_short, myvop->vop_access, myvop->vop_width,
		  myvop->vop_nbytes);
	  for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
	    {
	      putchar (*p);
	    }
	  printf ("\"\n");
	  if (myvop->vop_error)
	    {
	      printf ("  err:\"%s\"\n", myvop->vop_error);
	    }
	  if (myvop->vop_warn)
	    {
	      printf ("  wrn:\"%s\"\n", myvop->vop_warn);
	    }
	}
    }
  vip_end ();
  exit (EXIT_SUCCESS);
}

#endif /* #ifdef test */

/* end of vax_ins_parse.c */

/* vax_reg_parse.c - convert a VAX register name to a number */

/* Copyright (C) 1987 Free Software Foundation, Inc. A part of GNU.  */

/*
 *          v a x _ r e g _ p a r s e ( )
 *
 * Take 3 char.s, the last of which may be `\0` (non-existent)
 * and return the VAX register number that they represent.
 *
 * Return -1 if they don't form a register name. Good names return
 * a number from 0:15 inclusive.
 *
 * Case is not important in a name.
 *
 * Register names understood are:
 *
 *	R0
 *	R1
 *	R2
 *	R3
 *	R4
 *	R5
 *	R6
 * 	R7
 *	R8
 *	R9
 *	R10
 *	R11
 *	R12	AP
 *	R13	FP
 *	R14	SP
 *	R15	PC
 *
 */

#include <ctype.h>
#define AP (12)
#define FP (13)
#define SP (14)
#define PC (15)

int				/* return -1 or 0:15 */
vax_reg_parse (c1, c2, c3)	/* 3 chars of register name */
     char c1, c2, c3;		/* c3 == 0 if 2-character reg name */
{
  int retval;		/* return -1:15 */

  retval = -1;

  if (isupper (c1))
    c1 = tolower (c1);
  if (isupper (c2))
    c2 = tolower (c2);
  if (isdigit (c2) && c1 == 'r')
    {
      retval = c2 - '0';
      if (isdigit (c3))
	{
	  retval = retval * 10 + c3 - '0';
	  retval = (retval > 15) ? -1 : retval;
	  /* clamp the register value to 1 hex digit */
	}
      else if (c3)
	retval = -1;		/* c3 must be '\0' or a digit */
    }
  else if (c3)			/* There are no three letter regs */
    retval = -1;
  else if (c2 == 'p')
    {
      switch (c1)
	{
	case 's':
	  retval = SP;
	  break;
	case 'f':
	  retval = FP;
	  break;
	case 'a':
	  retval = AP;
	  break;
	default:
	  retval = -1;
	}
    }
  else if (c1 == 'p' && c2 == 'c')
    retval = PC;
  else
    retval = -1;
  return (retval);
}

/*
 *               v i p _ o p ( )
 *
 * Parse a vax operand in DEC assembler notation.
 * For speed, expect a string of whitespace to be reduced to a single ' '.
 * This is the case for GNU AS, and is easy for other DEC-compatible
 * assemblers.
 *
 * Knowledge about DEC VAX assembler operand notation lives here.
 * This doesn't even know what a register name is, except it believes
 * all register names are 2 or 3 characters, and lets vax_reg_parse() say
 * what number each name represents.
 * It does, however, know that PC, SP etc are special registers so it can
 * detect addressing modes that are silly for those registers.
 *
 * Where possible, it delivers 1 fatal or 1 warning message if the operand
 * is suspect. Exactly what we test for is still evolving.
 */

/*
 *		   	B u g s
 *
 *	Arg block.
 *
 * There were a number of 'mismatched argument type' bugs to vip_op.
 * The most general solution is to typedef each (of many) arguments.
 * We used instead a typedef'd argument block. This is less modular
 * than using seperate return pointers for each result, but runs faster
 * on most engines, and seems to keep programmers happy. It will have
 * to be done properly if we ever want to use vip_op as a general-purpose
 * module (it was designed to be).
 *
 *	G^
 *
 * Doesn't support DEC "G^" format operands. These always take 5 bytes
 * to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
 * optimising to (say) a "B^" if you are lucky in the way you link.
 * When someone builds a linker smart enough to convert "G^" to "B^", "W^"
 * whenever possible, then we should implement it.
 * If there is some other use for "G^", feel free to code it in!
 *
 *
 *	speed
 *
 * If I nested if()s more, I could avoid testing (*err) which would save
 * time, space and page faults. I didn't nest all those if()s for clarity
 * and because I think the mode testing can be re-arranged 1st to test the
 * commoner constructs 1st. Does anybody have statistics on this?
 *
 *
 *
 *	error messages
 *
 * In future, we should be able to 'compose' error messages in a scratch area
 * and give the user MUCH more informative error messages. Although this takes
 * a little more code at run-time, it will make this module much more self-
 * documenting. As an example of what sucks now: most error messages have
 * hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
 * the Un*x characters "$`*", that most users will expect from this AS.
 */

/*
 * The input is a string, ending with '\0'.
 *
 * We also require a 'hint' of what kind of operand is expected: so
 * we can remind caller not to write into literals for instance.
 *
 * The output is a skeletal instruction.
 *
 * The algorithm has two parts.
 * 1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
 * 2. express the @^#-()+[] as some parameters suited to further analysis.
 *
 * 2nd step is where we detect the googles of possible invalid combinations
 * a human (or compiler) might write. Note that if we do a half-way
 * decent assembler, we don't know how long to make (eg) displacement
 * fields when we first meet them (because they may not have defined values).
 * So we must wait until we know how many bits are needed for each address,
 * then we can know both length and opcodes of instructions.
 * For reason(s) above, we will pass to our caller a 'broken' instruction
 * of these major components, from which our caller can generate instructions:
 *  -  displacement length      I^ S^ L^ B^ W^ unspecified
 *  -  mode                     (many)
 *  -  register                 R0-R15 or absent
 *  -  index register           R0-R15 or absent
 *  -  expression text          what we don't parse
 *  -  error text(s)            why we couldn't understand the operand
 */

/*
 * To decode output of this, test errtxt. If errtxt[0] == '\0', then
 * we had no errors that prevented parsing. Also, if we ever report
 * an internal bug, errtxt[0] is set non-zero. So one test tells you
 * if the other outputs are to be taken seriously.
 */

/*
 * Because this module is useful for both VMS and UN*X style assemblers
 * and because of the variety of UN*X assemblers we must recognise
 * the different conventions for assembler operand notation. For example
 * VMS says "#42" for immediate mode, while most UN*X say "$42".
 * We permit arbitrary sets of (single) characters to represent the
 * 3 concepts that DEC writes '#', '@', '^'.
 */

/* character tests */
#define VIP_IMMEDIATE 01	/* Character is like DEC # */
#define VIP_INDIRECT  02	/* Char is like DEC @ */
#define VIP_DISPLEN   04	/* Char is like DEC ^ */

#define IMMEDIATEP(c)	(vip_metacharacters [(c)&0xff]&VIP_IMMEDIATE)
#define INDIRECTP(c)	(vip_metacharacters [(c)&0xff]&VIP_INDIRECT)
#define DISPLENP(c)	(vip_metacharacters [(c)&0xff]&VIP_DISPLEN)

/* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
 * are ever called.
 */

#if defined(CONST_TABLE)
#define _ 0,
#define I VIP_IMMEDIATE,
#define S VIP_INDIRECT,
#define D VIP_DISPLEN,
static const char
vip_metacharacters[256] =
{
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/* ^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/* ^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  _ _ _ _ I _ _ _ _ _ S _ _ _ _ _	/* sp !  "  #  $  %  & '  (  )  *  +  ,  -  .  / */
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?*/
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O*/
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _*/
  D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o*/
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  ^?*/

  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
};
#undef _
#undef I
#undef S
#undef D
#else
static char vip_metacharacters[256];

static void
vip_op_1 (bit, syms)
     int bit;
     const char *syms;
{
  unsigned char t;

  while ((t = *syms++) != 0)
    vip_metacharacters[t] |= bit;
}

/* Can be called any time.  More arguments may appear in future.  */
static void
vip_op_defaults (immediate, indirect, displen)
     const char *immediate;
     const char *indirect;
     const char *displen;
{
  vip_op_1 (VIP_IMMEDIATE, immediate);
  vip_op_1 (VIP_INDIRECT, indirect);
  vip_op_1 (VIP_DISPLEN, displen);
}

#endif


/*
 * Dec defines the semantics of address modes (and values)
 * by a two-letter code, explained here.
 *
 *   letter 1:   access type
 *
 *     a         address calculation - no data access, registers forbidden
 *     b         branch displacement
 *     m         read - let go of bus - write back    "modify"
 *     r         read
 *     v         bit field address: like 'a' but registers are OK
 *     w         write
 *     space	 no operator (eg ".long foo") [our convention]
 *
 *   letter 2:   data type (i.e. width, alignment)
 *
 *     b         byte
 *     d         double precision floating point (D format)
 *     f         single precision floating point (F format)
 *     g         G format floating
 *     h         H format floating
 *     l         longword
 *     o         octaword
 *     q         quadword
 *     w         word
 *     ?	 simple synthetic branch operand
 *     -	 unconditional synthetic JSB/JSR operand
 *     !	 complex synthetic branch operand
 *
 * The '-?!' letter 2's are not for external consumption. They are used
 * for various assemblers. Generally, all unknown widths are assumed 0.
 * We don't limit your choice of width character.
 *
 * DEC operands are hard work to parse. For example, '@' as the first
 * character means indirect (deferred) mode but elswhere it is a shift
 * operator.
 * The long-winded explanation of how this is supposed to work is
 * cancelled. Read a DEC vax manual.
 * We try hard not to parse anything that MIGHT be part of the expression
 * buried in that syntax. For example if we see @...(Rn) we don't check
 * for '-' before the '(' because mode @-(Rn) does not exist.
 *
 * After parsing we have:
 *
 * at                     1 if leading '@' (or Un*x '*')
 * len                    takes one value from " bilsw". eg B^ -> 'b'.
 * hash                   1 if leading '#' (or Un*x '$')
 * expr_begin, expr_end   the expression we did not parse
 *                        even though we don't interpret it, we make use
 *                        of its presence or absence.
 * sign                   -1: -(Rn)    0: absent    +1: (Rn)+
 * paren                  1 if () are around register
 * reg                    major register number 0:15    -1 means absent
 * ndx                    index register number 0:15    -1 means absent
 *
 * Again, I dare not explain it: just trace ALL the code!
 */

static void
vip_op (optext, vopP)
     /* user's input string e.g.: "@B^foo@bar(AP)[FP]:" */
     char *optext;
     /* Input fields: vop_access, vop_width.
	Output fields: _ndx, _reg, _mode, _short, _warn,
	_error _expr_begin, _expr_end, _nbytes.
	vop_nbytes : number of bytes in a datum.  */
     struct vop *vopP;
{
  /* track operand text forward */
  char *p;
  /* track operand text backward */
  char *q;
  /* 1 if leading '@' ('*') seen */
  int at;
  /* one of " bilsw" */
  char len;
  /* 1 if leading '#' ('$') seen */
  int hash;
  /* -1, 0 or +1 */
  int sign = 0;
  /* 1 if () surround register */
  int paren = 0;
  /* register number, -1:absent */
  int reg = 0;
  /* index register number -1:absent */
  int ndx = 0;
  /* report illegal operand, ""==OK */
  /* " " is a FAKE error: means we won */
  /* ANY err that begins with ' ' is a fake.  */
  /* " " is converted to "" before return */
  const char *err;
  /* warn about weird modes pf address */
  const char *wrn;
  /* preserve q in case we backup */
  char *oldq = NULL;
  /* build up 4-bit operand mode here */
  /* note: index mode is in ndx, this is */
  /* the major mode of operand address */
  int mode = 0;
  /*
   * Notice how we move wrong-arg-type bugs INSIDE this module: if we
   * get the types wrong below, we lose at compile time rather than at
   * lint or run time.
   */
  char access_mode;		/* vop_access.  */
  char width;			/* vop_width.  */

  access_mode = vopP->vop_access;
  width = vopP->vop_width;
  /* None of our code bugs (yet), no user text errors, no warnings
     even.  */
  err = wrn = 0;

  p = optext;

  if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
    p++;			/* skip over whitespace */

  if ((at = INDIRECTP (*p)) != 0)
    {				/* 1 if *p=='@'(or '*' for Un*x) */
      p++;			/* at is determined */
      if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
	p++;			/* skip over whitespace */
    }

  /*
   * This code is subtle. It tries to detect all legal (letter)'^'
   * but it doesn't waste time explicitly testing for premature '\0' because
   * this case is rejected as a mismatch against either (letter) or '^'.
   */
  {
    char c;

    c = *p;
    if (isupper (c))
      c = tolower (c);
    if (DISPLENP (p[1]) && strchr ("bilws", len = c))
      p += 2;			/* skip (letter) '^' */
    else			/* no (letter) '^' seen */
      len = ' ';		/* len is determined */
  }

  if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
    p++;			/* skip over whitespace */

  if ((hash = IMMEDIATEP (*p)) != 0)	/* 1 if *p=='#' ('$' for Un*x) */
    p++;			/* hash is determined */

  /*
   * p points to what may be the beginning of an expression.
   * We have peeled off the front all that is peelable.
   * We know at, len, hash.
   *
   * Lets point q at the end of the text and parse that (backwards).
   */

  for (q = p; *q; q++)
    ;
  q--;				/* now q points at last char of text */

  if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
    q--;
  /* reverse over whitespace, but don't */
  /* run back over *p */

  /*
   * As a matter of policy here, we look for [Rn], although both Rn and S^#
   * forbid [Rn]. This is because it is easy, and because only a sick
   * cyborg would have [...] trailing an expression in a VAX-like assembler.
   * A meticulous parser would first check for Rn followed by '(' or '['
   * and not parse a trailing ']' if it found another. We just ban expressions
   * ending in ']'.
   */
  if (*q == ']')
    {
      while (q >= p && *q != '[')
	q--;
      /* either q<p or we got matching '[' */
      if (q < p)
	err = _("no '[' to match ']'");
      else
	{
	  /*
	   * Confusers like "[]" will eventually lose with a bad register
	   * name error. So again we don't need to check for early '\0'.
	   */
	  if (q[3] == ']')
	    ndx = vax_reg_parse (q[1], q[2], 0);
	  else if (q[4] == ']')
	    ndx = vax_reg_parse (q[1], q[2], q[3]);
	  else
	    ndx = -1;
	  /*
	   * Since we saw a ']' we will demand a register name in the [].
	   * If luser hasn't given us one: be rude.
	   */
	  if (ndx < 0)
	    err = _("bad register in []");
	  else if (ndx == PC)
	    err = _("[PC] index banned");
	  else
	    q--;		/* point q just before "[...]" */
	}
    }
  else
    ndx = -1;			/* no ']', so no iNDeX register */

  /*
   * If err = "..." then we lost: run away.
   * Otherwise ndx == -1 if there was no "[...]".
   * Otherwise, ndx is index register number, and q points before "[...]".
   */

  if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
    q--;
  /* reverse over whitespace, but don't */
  /* run back over *p */
  if (!err || !*err)
    {
      sign = 0;			/* no ()+ or -() seen yet */

      if (q > p + 3 && *q == '+' && q[-1] == ')')
	{
	  sign = 1;		/* we saw a ")+" */
	  q--;			/* q points to ')' */
	}

      if (*q == ')' && q > p + 2)
	{
	  paren = 1;		/* assume we have "(...)" */
	  while (q >= p && *q != '(')
	    q--;
	  /* either q<p or we got matching '(' */
	  if (q < p)
	    err = _("no '(' to match ')'");
	  else
	    {
	      /*
	       * Confusers like "()" will eventually lose with a bad register
	       * name error. So again we don't need to check for early '\0'.
	       */
	      if (q[3] == ')')
		reg = vax_reg_parse (q[1], q[2], 0);
	      else if (q[4] == ')')
		reg = vax_reg_parse (q[1], q[2], q[3]);
	      else
		reg = -1;
	      /*
	       * Since we saw a ')' we will demand a register name in the ')'.
	       * This is nasty: why can't our hypothetical assembler permit
	       * parenthesised expressions? BECAUSE I AM LAZY! That is why.
	       * Abuse luser if we didn't spy a register name.
	       */
	      if (reg < 0)
		{
		  /* JF allow parenthasized expressions.  I hope this works */
		  paren = 0;
		  while (*q != ')')
		    q++;
		  /* err = "unknown register in ()"; */
		}
	      else
		q--;		/* point just before '(' of "(...)" */
	      /*
	       * If err == "..." then we lost. Run away.
	       * Otherwise if reg >= 0 then we saw (Rn).
	       */
	    }
	  /*
	   * If err == "..." then we lost.
	   * Otherwise paren==1 and reg = register in "()".
	   */
	}
      else
	paren = 0;
      /*
       * If err == "..." then we lost.
       * Otherwise, q points just before "(Rn)", if any.
       * If there was a "(...)" then paren==1, and reg is the register.
       */

      /*
       * We should only seek '-' of "-(...)" if:
       *   we saw "(...)"                    paren == 1
       *   we have no errors so far          ! *err
       *   we did not see '+' of "(...)+"    sign < 1
       * We don't check len. We want a specific error message later if
       * user tries "x^...-(Rn)". This is a feature not a bug.
       */
      if (!err || !*err)
	{
	  if (paren && sign < 1)/* !sign is adequate test */
	    {
	      if (*q == '-')
		{
		  sign = -1;
		  q--;
		}
	    }
	  /*
	   * We have back-tracked over most
	   * of the crud at the end of an operand.
	   * Unless err, we know: sign, paren. If paren, we know reg.
	   * The last case is of an expression "Rn".
	   * This is worth hunting for if !err, !paren.
	   * We wouldn't be here if err.
	   * We remember to save q, in case we didn't want "Rn" anyway.
	   */
	  if (!paren)
	    {
	      if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
		q--;
	      /* reverse over whitespace, but don't */
	      /* run back over *p */
	      if (q > p && q < p + 3)	/* room for Rn or Rnn exactly? */
		reg = vax_reg_parse (p[0], p[1], q < p + 2 ? 0 : p[2]);
	      else
		reg = -1;	/* always comes here if no register at all */
	      /*
	       * Here with a definitive reg value.
	       */
	      if (reg >= 0)
		{
		  oldq = q;
		  q = p - 1;
		}
	    }
	}
    }
  /*
   * have reg. -1:absent; else 0:15
   */

  /*
   * We have:  err, at, len, hash, ndx, sign, paren, reg.
   * Also, any remaining expression is from *p through *q inclusive.
   * Should there be no expression, q==p-1. So expression length = q-p+1.
   * This completes the first part: parsing the operand text.
   */

  /*
   * We now want to boil the data down, checking consistency on the way.
   * We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
   * We will deliver a 4-bit reg, and a 4-bit mode.
   */

  /*
   * Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
   *
   * in:  at	?
   *      len	?
   *      hash	?
   *      p:q	?
   *      sign  ?
   *      paren	?
   *      reg   ?
   *      ndx   ?
   *
   * out: mode  0
   *      reg   -1
   *      len	' '
   *      p:q	whatever was input
   *      ndx	-1
   *      err	" "		 or error message, and other outputs trashed
   */
  /* branch operands have restricted forms */
  if ((!err || !*err) && access_mode == 'b')
    {
      if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
	err = _("invalid branch operand");
      else
	err = " ";
    }

  /* Since nobody seems to use it: comment this 'feature'(?) out for now.  */
#ifdef NEVER
  /*
   * Case of stand-alone operand. e.g. ".long foo"
   *
   * in:  at	?
   *      len	?
   *      hash	?
   *      p:q	?
   *      sign  ?
   *      paren	?
   *      reg   ?
   *      ndx   ?
   *
   * out: mode  0
   *      reg   -1
   *      len	' '
   *      p:q	whatever was input
   *      ndx	-1
   *      err	" "		 or error message, and other outputs trashed
   */
  if ((!err || !*err) && access_mode == ' ')
    {
      if (at)
	err = _("address prohibits @");
      else if (hash)
	err = _("address prohibits #");
      else if (sign)
	{
	  if (sign < 0)
	    err = _("address prohibits -()");
	  else
	    err = _("address prohibits ()+");
	}
      else if (paren)
	err = _("address prohibits ()");
      else if (ndx >= 0)
	err = _("address prohibits []");
      else if (reg >= 0)
	err = _("address prohibits register");
      else if (len != ' ')
	err = _("address prohibits displacement length specifier");
      else
	{
	  err = " ";	/* succeed */
	  mode = 0;
	}
    }
#endif /*#Ifdef NEVER*/

  /*
   * Case of S^#.
   *
   * in:  at       0
   *      len      's'               definition
   *      hash     1              demand
   *      p:q                        demand not empty
   *      sign     0                 by paren==0
   *      paren    0             by "()" scan logic because "S^" seen
   *      reg      -1                or nn by mistake
   *      ndx      -1
   *
   * out: mode     0
   *      reg      -1
   *      len      's'
   *      exp
   *      ndx      -1
   */
  if ((!err || !*err) && len == 's')
    {
      if (!hash || paren || at || ndx >= 0)
	err = _("invalid operand of S^#");
      else
	{
	  if (reg >= 0)
	    {
	      /*
	       * SHIT! we saw S^#Rnn ! put the Rnn back in
	       * expression. KLUDGE! Use oldq so we don't
	       * need to know exact length of reg name.
	       */
	      q = oldq;
	      reg = 0;
	    }
	  /*
	   * We have all the expression we will ever get.
	   */
	  if (p > q)
	    err = _("S^# needs expression");
	  else if (access_mode == 'r')
	    {
	      err = " ";	/* WIN! */
	      mode = 0;
	    }
	  else
	    err = _("S^# may only read-access");
	}
    }

  /*
   * Case of -(Rn), which is weird case.
   *
   * in:  at       0
   *      len      '
   *      hash     0
   *      p:q      q<p
   *      sign     -1                by definition
   *      paren    1              by definition
   *      reg      present           by definition
   *      ndx      optional
   *
   * out: mode     7
   *      reg      present
   *      len      ' '
   *      exp      ""                enforce empty expression
   *      ndx      optional          warn if same as reg
   */
  if ((!err || !*err) && sign < 0)
    {
      if (len != ' ' || hash || at || p <= q)
	err = _("invalid operand of -()");
      else
	{
	  err = " ";		/* win */
	  mode = 7;
	  if (reg == PC)
	    wrn = _("-(PC) unpredictable");
	  else if (reg == ndx)
	    wrn = _("[]index same as -()register: unpredictable");
	}
    }

  /*
   * We convert "(Rn)" to "@Rn" for our convenience.
   * (I hope this is convenient: has someone got a better way to parse this?)
   * A side-effect of this is that "@Rn" is a valid operand.
   */
  if (paren && !sign && !hash && !at && len == ' ' && p > q)
    {
      at = 1;
      paren = 0;
    }

  /*
   * Case of (Rn)+, which is slightly different.
   *
   * in:  at
   *      len      ' '
   *      hash     0
   *      p:q      q<p
   *      sign     +1                by definition
   *      paren    1              by definition
   *      reg      present           by definition
   *      ndx      optional
   *
   * out: mode     8+@
   *      reg      present
   *      len      ' '
   *      exp      ""                enforce empty expression
   *      ndx      optional          warn if same as reg
   */
  if ((!err || !*err) && sign > 0)
    {
      if (len != ' ' || hash || p <= q)
	err = _("invalid operand of ()+");
      else
	{
	  err = " ";		/* win */
	  mode = 8 + (at ? 1 : 0);
	  if (reg == PC)
	    wrn = _("(PC)+ unpredictable");
	  else if (reg == ndx)
	    wrn = _("[]index same as ()+register: unpredictable");
	}
    }

  /*
   * Case of #, without S^.
   *
   * in:  at
   *      len      ' ' or 'i'
   *      hash     1              by definition
   *      p:q
   *      sign     0
   *      paren    0
   *      reg      absent
   *      ndx      optional
   *
   * out: mode     8+@
   *      reg      PC
   *      len      ' ' or 'i'
   *      exp
   *      ndx      optional
   */
  if ((!err || !*err) && hash)
    {
      if (len != 'i' && len != ' ')
	err = _("# conflicts length");
      else if (paren)
	err = _("# bars register");
      else
	{
	  if (reg >= 0)
	    {
	      /*
	       * SHIT! we saw #Rnn! Put the Rnn back into the expression.
	       * By using oldq, we don't need to know how long Rnn was.
	       * KLUDGE!
	       */
	      q = oldq;
	      reg = -1;		/* no register any more */
	    }
	  err = " ";		/* win */

	  /* JF a bugfix, I think! */
	  if (at && access_mode == 'a')
	    vopP->vop_nbytes = 4;

	  mode = (at ? 9 : 8);
	  reg = PC;
	  if ((access_mode == 'm' || access_mode == 'w') && !at)
	    wrn = _("writing or modifying # is unpredictable");
	}
    }
  /*
   * If !*err, then        sign == 0
   *                       hash == 0
   */

  /*
   * Case of Rn. We seperate this one because it has a few special
   * errors the remaining modes lack.
   *
   * in:  at       optional
   *      len      ' '
   *      hash     0             by program logic
   *      p:q      empty
   *      sign     0                 by program logic
   *      paren    0             by definition
   *      reg      present           by definition
   *      ndx      optional
   *
   * out: mode     5+@
   *      reg      present
   *      len      ' '               enforce no length
   *      exp      ""                enforce empty expression
   *      ndx      optional          warn if same as reg
   */
  if ((!err || !*err) && !paren && reg >= 0)
    {
      if (len != ' ')
	err = _("length not needed");
      else if (at)
	{
	  err = " ";		/* win */
	  mode = 6;		/* @Rn */
	}
      else if (ndx >= 0)
	err = _("can't []index a register, because it has no address");
      else if (access_mode == 'a')
	err = _("a register has no address");
      else
	{
	  /*
	   * Idea here is to detect from length of datum
	   * and from register number if we will touch PC.
	   * Warn if we do.
	   * vop_nbytes is number of bytes in operand.
	   * Compute highest byte affected, compare to PC0.
	   */
	  if ((vopP->vop_nbytes + reg * 4) > 60)
	    wrn = _("PC part of operand unpredictable");
	  err = " ";		/* win */
	  mode = 5;		/* Rn */
	}
    }
  /*
   * If !*err,        sign  == 0
   *                  hash  == 0
   *                  paren == 1  OR reg==-1
   */

  /*
   * Rest of cases fit into one bunch.
   *
   * in:  at       optional
   *      len      ' ' or 'b' or 'w' or 'l'
   *      hash     0             by program logic
   *      p:q      expected          (empty is not an error)
   *      sign     0                 by program logic
   *      paren    optional
   *      reg      optional
   *      ndx      optional
   *
   * out: mode     10 + @ + len
   *      reg      optional
   *      len      ' ' or 'b' or 'w' or 'l'
   *      exp                        maybe empty
   *      ndx      optional          warn if same as reg
   */
  if (!err || !*err)
    {
      err = " ";		/* win (always) */
      mode = 10 + (at ? 1 : 0);
      switch (len)
	{
	case 'l':
	  mode += 2;
	case 'w':
	  mode += 2;
	case ' ':		/* assumed B^ until our caller changes it */
	case 'b':
	  break;
	}
    }

  /*
   * here with completely specified     mode
   *					len
   *					reg
   *					expression   p,q
   *					ndx
   */

  if (*err == ' ')
    err = 0;			/* " " is no longer an error */

  vopP->vop_mode = mode;
  vopP->vop_reg = reg;
  vopP->vop_short = len;
  vopP->vop_expr_begin = p;
  vopP->vop_expr_end = q;
  vopP->vop_ndx = ndx;
  vopP->vop_error = err;
  vopP->vop_warn = wrn;
}

/*

  Summary of vip_op outputs.

  mode	reg	len	ndx
  (Rn) => @Rn
  {@}Rn			5+@	n	' '	optional
  branch operand		0	-1	' '	-1
  S^#foo			0	-1	's'	-1
  -(Rn)			7	n	' '	optional
  {@}(Rn)+		8+@	n	' '	optional
  {@}#foo, no S^		8+@	PC	" i"	optional
  {@}{q^}{(Rn)}		10+@+q	option	" bwl"	optional

  */

#ifdef TEST			/* #Define to use this testbed.  */

/*
 * Follows a test program for this function.
 * We declare arrays non-local in case some of our tiny-minded machines
 * default to small stacks. Also, helps with some debuggers.
 */

#include <stdio.h>

char answer[100];		/* human types into here */
char *p;			/*  */
char *myerr;
char *mywrn;
char *mybug;
char myaccess;
char mywidth;
char mymode;
char myreg;
char mylen;
char *myleft;
char *myright;
char myndx;
int my_operand_length;
char my_immediate[200];
char my_indirect[200];
char my_displen[200];

main ()
{
  printf ("enter immediate symbols eg enter #   ");
  gets (my_immediate);
  printf ("enter indirect symbols  eg enter @   ");
  gets (my_indirect);
  printf ("enter displen symbols   eg enter ^   ");
  gets (my_displen);
  vip_op_defaults (my_immediate, my_indirect, my_displen);
  for (;;)
    {
      printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
      fflush (stdout);
      gets (answer);
      if (!answer[0])
	exit (EXIT_SUCCESS);
      myaccess = answer[0];
      mywidth = answer[1];
      switch (mywidth)
	{
	case 'b':
	  my_operand_length = 1;
	  break;
	case 'd':
	  my_operand_length = 8;
	  break;
	case 'f':
	  my_operand_length = 4;
	  break;
	case 'g':
	  my_operand_length = 16;
	  break;
	case 'h':
	  my_operand_length = 32;
	  break;
	case 'l':
	  my_operand_length = 4;
	  break;
	case 'o':
	  my_operand_length = 16;
	  break;
	case 'q':
	  my_operand_length = 8;
	  break;
	case 'w':
	  my_operand_length = 2;
	  break;
	case '!':
	case '?':
	case '-':
	  my_operand_length = 0;
	  break;

	default:
	  my_operand_length = 2;
	  printf ("I dn't understand access width %c\n", mywidth);
	  break;
	}
      printf ("VAX assembler instruction operand: ");
      fflush (stdout);
      gets (answer);
      mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
		      &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
		      &myerr, &mywrn);
      if (*myerr)
	{
	  printf ("error: \"%s\"\n", myerr);
	  if (*mybug)
	    printf (" bug: \"%s\"\n", mybug);
	}
      else
	{
	  if (*mywrn)
	    printf ("warning: \"%s\"\n", mywrn);
	  mumble ("mode", mymode);
	  mumble ("register", myreg);
	  mumble ("index", myndx);
	  printf ("width:'%c'  ", mylen);
	  printf ("expression: \"");
	  while (myleft <= myright)
	    putchar (*myleft++);
	  printf ("\"\n");
	}
    }
}

mumble (text, value)
     char *text;
     int value;
{
  printf ("%s:", text);
  if (value >= 0)
    printf ("%xx", value);
  else
    printf ("ABSENT");
  printf ("  ");
}

#endif /* ifdef TEST */

/* end: vip_op.c */

const int md_short_jump_size = 3;
const int md_long_jump_size = 6;
const int md_reloc_size = 8;	/* Size of relocation record */

void
md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
     char *ptr;
     addressT from_addr, to_addr;
     fragS *frag;
     symbolS *to_symbol;
{
  valueT offset;

  /* This former calculation was off by two:
      offset = to_addr - (from_addr + 1);
     We need to account for the one byte instruction and also its
     two byte operand.  */
  offset = to_addr - (from_addr + 1 + 2);
  *ptr++ = VAX_BRW;		/* branch with word (16 bit) offset */
  md_number_to_chars (ptr, offset, 2);
}

void
md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
     char *ptr;
     addressT from_addr, to_addr;
     fragS *frag;
     symbolS *to_symbol;
{
  valueT offset;

  offset = to_addr - S_GET_VALUE (to_symbol);
  *ptr++ = VAX_JMP;		/* arbitrary jump */
  *ptr++ = VAX_ABSOLUTE_MODE;
  md_number_to_chars (ptr, offset, 4);
  fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (long) 0, 0, NO_RELOC);
}

#ifdef OBJ_VMS
CONST char *md_shortopts = "d:STt:V+1h:Hv::";
#else
CONST char *md_shortopts = "d:STt:V";
#endif
struct option md_longopts[] = {
  {NULL, no_argument, NULL, 0}
};
size_t md_longopts_size = sizeof (md_longopts);

int
md_parse_option (c, arg)
     int c;
     char *arg;
{
  switch (c)
    {
    case 'S':
      as_warn (_("SYMBOL TABLE not implemented"));
      break;

    case 'T':
      as_warn (_("TOKEN TRACE not implemented"));
      break;

    case 'd':
      as_warn (_("Displacement length %s ignored!"), arg);
      break;

    case 't':
      as_warn (_("I don't need or use temp. file \"%s\"."), arg);
      break;

    case 'V':
      as_warn (_("I don't use an interpass file! -V ignored"));
      break;

#ifdef OBJ_VMS
    case '+':			/* For g++.  Hash any name > 31 chars long.  */
      flag_hash_long_names = 1;
      break;

    case '1':			/* For backward compatibility */
      flag_one = 1;
      break;

    case 'H':			/* Show new symbol after hash truncation */
      flag_show_after_trunc = 1;
      break;

    case 'h':			/* No hashing of mixed-case names */
      {
	extern char vms_name_mapping;
	vms_name_mapping = atoi (arg);
	flag_no_hash_mixed_case = 1;
      }
      break;

    case 'v':
      {
	extern char *compiler_version_string;
	if (!arg || !*arg || access (arg, 0) == 0)
	  return 0;		/* have caller show the assembler version */
	compiler_version_string = arg;
      }
      break;
#endif

    default:
      return 0;
    }

  return 1;
}

void
md_show_usage (stream)
     FILE *stream;
{
  fprintf (stream, _("\
VAX options:\n\
-d LENGTH		ignored\n\
-J			ignored\n\
-S			ignored\n\
-t FILE			ignored\n\
-T			ignored\n\
-V			ignored\n"));
#ifdef OBJ_VMS
  fprintf (stream, _("\
VMS options:\n\
-+			hash encode names longer than 31 characters\n\
-1			`const' handling compatible with gcc 1.x\n\
-H			show new symbol after hash truncation\n\
-h NUM			don't hash mixed-case names, and adjust case:\n\
			0 = upper, 2 = lower, 3 = preserve case\n\
-v\"VERSION\"		code being assembled was produced by compiler \"VERSION\"\n"));
#endif
}

/* We have no need to default values of symbols.  */

symbolS *
md_undefined_symbol (name)
     char *name;
{
  return 0;
}

/* Round up a section size to the appropriate boundary.  */
valueT
md_section_align (segment, size)
     segT segment;
     valueT size;
{
  return size;			/* Byte alignment is fine */
}

/* Exactly what point is a PC-relative offset relative TO?
   On the vax, they're relative to the address of the offset, plus
   its size. (??? Is this right?  FIXME-SOON) */
long
md_pcrel_from (fixP)
     fixS *fixP;
{
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
}

/* end of tc-vax.c */