Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/* tc-bfin.c -- Assembler for the ADI Blackfin.
   Copyright (C) 2005-2020 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include "as.h"
#include "bfin-defs.h"
#include "obstack.h"
#include "safe-ctype.h"
#ifdef OBJ_ELF
#include "dwarf2dbg.h"
#endif
#include "elf/common.h"
#include "elf/bfin.h"

extern int yyparse (void);
struct yy_buffer_state;
typedef struct yy_buffer_state *YY_BUFFER_STATE;
extern YY_BUFFER_STATE yy_scan_string (const char *yy_str);
extern void yy_delete_buffer (YY_BUFFER_STATE b);
static parse_state parse (char *line);

/* Global variables. */
struct bfin_insn *insn;
int last_insn_size;

extern struct obstack mempool;
FILE *errorf;

/* Flags to set in the elf header */
#define DEFAULT_FLAGS 0

#ifdef OBJ_FDPIC_ELF
# define DEFAULT_FDPIC EF_BFIN_FDPIC
#else
# define DEFAULT_FDPIC 0
#endif

static flagword bfin_flags = DEFAULT_FLAGS | DEFAULT_FDPIC;
static const char *bfin_pic_flag = DEFAULT_FDPIC ? "-mfdpic" : (const char *)0;

/* Blackfin specific function to handle FD-PIC pointer initializations.  */

static void
bfin_pic_ptr (int nbytes)
{
  expressionS exp;
  char *p;

  if (nbytes != 4)
    abort ();

#ifdef md_flush_pending_output
  md_flush_pending_output ();
#endif

  if (is_it_end_of_statement ())
    {
      demand_empty_rest_of_line ();
      return;
    }

#ifdef md_cons_align
  md_cons_align (nbytes);
#endif

  do
    {
      bfd_reloc_code_real_type reloc_type = BFD_RELOC_BFIN_FUNCDESC;

      if (strncasecmp (input_line_pointer, "funcdesc(", 9) == 0)
	{
	  input_line_pointer += 9;
	  expression (&exp);
	  if (*input_line_pointer == ')')
	    input_line_pointer++;
	  else
	    as_bad (_("missing ')'"));
	}
      else
	error ("missing funcdesc in picptr");

      p = frag_more (4);
      memset (p, 0, 4);
      fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
		   reloc_type);
    }
  while (*input_line_pointer++ == ',');

  input_line_pointer--;			/* Put terminator back into stream. */
  demand_empty_rest_of_line ();
}

static void
bfin_s_bss (int ignore ATTRIBUTE_UNUSED)
{
  int temp;

  temp = get_absolute_expression ();
  subseg_set (bss_section, (subsegT) temp);
  demand_empty_rest_of_line ();
}

const pseudo_typeS md_pseudo_table[] = {
  {"align", s_align_bytes, 0},
  {"byte2", cons, 2},
  {"byte4", cons, 4},
  {"picptr", bfin_pic_ptr, 4},
  {"code", obj_elf_section, 0},
  {"db", cons, 1},
  {"dd", cons, 4},
  {"dw", cons, 2},
  {"p", s_ignore, 0},
  {"pdata", s_ignore, 0},
  {"var", s_ignore, 0},
  {"bss", bfin_s_bss, 0},
  {0, 0, 0}
};

/* Characters that are used to denote comments and line separators. */
const char comment_chars[] = "#";
const char line_comment_chars[] = "#";
const char line_separator_chars[] = ";";

/* Characters that can be used to separate the mantissa from the
   exponent in floating point numbers. */
const char EXP_CHARS[] = "eE";

/* Characters that mean this number is a floating point constant.
   As in 0f12.456 or  0d1.2345e12.  */
const char FLT_CHARS[] = "fFdDxX";

typedef enum bfin_cpu_type
{
  BFIN_CPU_UNKNOWN,
  BFIN_CPU_BF504,
  BFIN_CPU_BF506,
  BFIN_CPU_BF512,
  BFIN_CPU_BF514,
  BFIN_CPU_BF516,
  BFIN_CPU_BF518,
  BFIN_CPU_BF522,
  BFIN_CPU_BF523,
  BFIN_CPU_BF524,
  BFIN_CPU_BF525,
  BFIN_CPU_BF526,
  BFIN_CPU_BF527,
  BFIN_CPU_BF531,
  BFIN_CPU_BF532,
  BFIN_CPU_BF533,
  BFIN_CPU_BF534,
  BFIN_CPU_BF536,
  BFIN_CPU_BF537,
  BFIN_CPU_BF538,
  BFIN_CPU_BF539,
  BFIN_CPU_BF542,
  BFIN_CPU_BF542M,
  BFIN_CPU_BF544,
  BFIN_CPU_BF544M,
  BFIN_CPU_BF547,
  BFIN_CPU_BF547M,
  BFIN_CPU_BF548,
  BFIN_CPU_BF548M,
  BFIN_CPU_BF549,
  BFIN_CPU_BF549M,
  BFIN_CPU_BF561,
  BFIN_CPU_BF592,
} bfin_cpu_t;

bfin_cpu_t bfin_cpu_type = BFIN_CPU_UNKNOWN;
/* -msi-revision support. There are three special values:
   -1      -msi-revision=none.
   0xffff  -msi-revision=any.  */
int bfin_si_revision;

unsigned int bfin_anomaly_checks = 0;

struct bfin_cpu
{
  const char *name;
  bfin_cpu_t type;
  int si_revision;
  unsigned int anomaly_checks;
};

struct bfin_cpu bfin_cpus[] =
{
  {"bf504", BFIN_CPU_BF504, 0x0000, AC_05000074},

  {"bf506", BFIN_CPU_BF506, 0x0000, AC_05000074},

  {"bf512", BFIN_CPU_BF512, 0x0002, AC_05000074},
  {"bf512", BFIN_CPU_BF512, 0x0001, AC_05000074},
  {"bf512", BFIN_CPU_BF512, 0x0000, AC_05000074},

  {"bf514", BFIN_CPU_BF514, 0x0002, AC_05000074},
  {"bf514", BFIN_CPU_BF514, 0x0001, AC_05000074},
  {"bf514", BFIN_CPU_BF514, 0x0000, AC_05000074},

  {"bf516", BFIN_CPU_BF516, 0x0002, AC_05000074},
  {"bf516", BFIN_CPU_BF516, 0x0001, AC_05000074},
  {"bf516", BFIN_CPU_BF516, 0x0000, AC_05000074},

  {"bf518", BFIN_CPU_BF518, 0x0002, AC_05000074},
  {"bf518", BFIN_CPU_BF518, 0x0001, AC_05000074},
  {"bf518", BFIN_CPU_BF518, 0x0000, AC_05000074},

  {"bf522", BFIN_CPU_BF522, 0x0002, AC_05000074},
  {"bf522", BFIN_CPU_BF522, 0x0001, AC_05000074},
  {"bf522", BFIN_CPU_BF522, 0x0000, AC_05000074},

  {"bf523", BFIN_CPU_BF523, 0x0002, AC_05000074},
  {"bf523", BFIN_CPU_BF523, 0x0001, AC_05000074},
  {"bf523", BFIN_CPU_BF523, 0x0000, AC_05000074},

  {"bf524", BFIN_CPU_BF524, 0x0002, AC_05000074},
  {"bf524", BFIN_CPU_BF524, 0x0001, AC_05000074},
  {"bf524", BFIN_CPU_BF524, 0x0000, AC_05000074},

  {"bf525", BFIN_CPU_BF525, 0x0002, AC_05000074},
  {"bf525", BFIN_CPU_BF525, 0x0001, AC_05000074},
  {"bf525", BFIN_CPU_BF525, 0x0000, AC_05000074},

  {"bf526", BFIN_CPU_BF526, 0x0002, AC_05000074},
  {"bf526", BFIN_CPU_BF526, 0x0001, AC_05000074},
  {"bf526", BFIN_CPU_BF526, 0x0000, AC_05000074},

  {"bf527", BFIN_CPU_BF527, 0x0002, AC_05000074},
  {"bf527", BFIN_CPU_BF527, 0x0001, AC_05000074},
  {"bf527", BFIN_CPU_BF527, 0x0000, AC_05000074},

  {"bf531", BFIN_CPU_BF531, 0x0006, AC_05000074},
  {"bf531", BFIN_CPU_BF531, 0x0005, AC_05000074},
  {"bf531", BFIN_CPU_BF531, 0x0004, AC_05000074},
  {"bf531", BFIN_CPU_BF531, 0x0003, AC_05000074},

  {"bf532", BFIN_CPU_BF532, 0x0006, AC_05000074},
  {"bf532", BFIN_CPU_BF532, 0x0005, AC_05000074},
  {"bf532", BFIN_CPU_BF532, 0x0004, AC_05000074},
  {"bf532", BFIN_CPU_BF532, 0x0003, AC_05000074},

  {"bf533", BFIN_CPU_BF533, 0x0006, AC_05000074},
  {"bf533", BFIN_CPU_BF533, 0x0005, AC_05000074},
  {"bf533", BFIN_CPU_BF533, 0x0004, AC_05000074},
  {"bf533", BFIN_CPU_BF533, 0x0003, AC_05000074},

  {"bf534", BFIN_CPU_BF534, 0x0003, AC_05000074},
  {"bf534", BFIN_CPU_BF534, 0x0002, AC_05000074},
  {"bf534", BFIN_CPU_BF534, 0x0001, AC_05000074},

  {"bf536", BFIN_CPU_BF536, 0x0003, AC_05000074},
  {"bf536", BFIN_CPU_BF536, 0x0002, AC_05000074},
  {"bf536", BFIN_CPU_BF536, 0x0001, AC_05000074},

  {"bf537", BFIN_CPU_BF537, 0x0003, AC_05000074},
  {"bf537", BFIN_CPU_BF537, 0x0002, AC_05000074},
  {"bf537", BFIN_CPU_BF537, 0x0001, AC_05000074},

  {"bf538", BFIN_CPU_BF538, 0x0005, AC_05000074},
  {"bf538", BFIN_CPU_BF538, 0x0004, AC_05000074},
  {"bf538", BFIN_CPU_BF538, 0x0003, AC_05000074},
  {"bf538", BFIN_CPU_BF538, 0x0002, AC_05000074},

  {"bf539", BFIN_CPU_BF539, 0x0005, AC_05000074},
  {"bf539", BFIN_CPU_BF539, 0x0004, AC_05000074},
  {"bf539", BFIN_CPU_BF539, 0x0003, AC_05000074},
  {"bf539", BFIN_CPU_BF539, 0x0002, AC_05000074},

  {"bf542m", BFIN_CPU_BF542M, 0x0003, AC_05000074},

  {"bf542", BFIN_CPU_BF542, 0x0004, AC_05000074},
  {"bf542", BFIN_CPU_BF542, 0x0002, AC_05000074},
  {"bf542", BFIN_CPU_BF542, 0x0001, AC_05000074},
  {"bf542", BFIN_CPU_BF542, 0x0000, AC_05000074},

  {"bf544m", BFIN_CPU_BF544M, 0x0003, AC_05000074},

  {"bf544", BFIN_CPU_BF544, 0x0004, AC_05000074},
  {"bf544", BFIN_CPU_BF544, 0x0002, AC_05000074},
  {"bf544", BFIN_CPU_BF544, 0x0001, AC_05000074},
  {"bf544", BFIN_CPU_BF544, 0x0000, AC_05000074},

  {"bf547m", BFIN_CPU_BF547M, 0x0003, AC_05000074},

  {"bf547", BFIN_CPU_BF547, 0x0004, AC_05000074},
  {"bf547", BFIN_CPU_BF547, 0x0002, AC_05000074},
  {"bf547", BFIN_CPU_BF547, 0x0001, AC_05000074},
  {"bf547", BFIN_CPU_BF547, 0x0000, AC_05000074},

  {"bf548m", BFIN_CPU_BF548M, 0x0003, AC_05000074},

  {"bf548", BFIN_CPU_BF548, 0x0004, AC_05000074},
  {"bf548", BFIN_CPU_BF548, 0x0002, AC_05000074},
  {"bf548", BFIN_CPU_BF548, 0x0001, AC_05000074},
  {"bf548", BFIN_CPU_BF548, 0x0000, AC_05000074},

  {"bf549m", BFIN_CPU_BF549M, 0x0003, AC_05000074},

  {"bf549", BFIN_CPU_BF549, 0x0004, AC_05000074},
  {"bf549", BFIN_CPU_BF549, 0x0002, AC_05000074},
  {"bf549", BFIN_CPU_BF549, 0x0001, AC_05000074},
  {"bf549", BFIN_CPU_BF549, 0x0000, AC_05000074},

  {"bf561", BFIN_CPU_BF561, 0x0005, AC_05000074},
  {"bf561", BFIN_CPU_BF561, 0x0003, AC_05000074},
  {"bf561", BFIN_CPU_BF561, 0x0002, AC_05000074},

  {"bf592", BFIN_CPU_BF592, 0x0001, AC_05000074},
  {"bf592", BFIN_CPU_BF592, 0x0000, AC_05000074},
};

/* Define bfin-specific command-line options (there are none). */
const char *md_shortopts = "";

#define OPTION_FDPIC		(OPTION_MD_BASE)
#define OPTION_NOPIC		(OPTION_MD_BASE + 1)
#define OPTION_MCPU		(OPTION_MD_BASE + 2)

struct option md_longopts[] =
{
  { "mcpu",		required_argument,	NULL, OPTION_MCPU	},
  { "mfdpic",		no_argument,		NULL, OPTION_FDPIC      },
  { "mnopic",		no_argument,		NULL, OPTION_NOPIC      },
  { "mno-fdpic",	no_argument,		NULL, OPTION_NOPIC      },
  { NULL,		no_argument,		NULL, 0                 },
};

size_t md_longopts_size = sizeof (md_longopts);


int
md_parse_option (int c ATTRIBUTE_UNUSED, const char *arg ATTRIBUTE_UNUSED)
{
  switch (c)
    {
    default:
      return 0;

    case OPTION_MCPU:
      {
	const char *q;
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE (bfin_cpus); i++)
	  {
	    const char *p = bfin_cpus[i].name;
	    if (strncmp (arg, p, strlen (p)) == 0)
	      break;
	  }

	if (i == ARRAY_SIZE (bfin_cpus))
	  as_fatal ("-mcpu=%s is not valid", arg);

	bfin_cpu_type = bfin_cpus[i].type;

	q = arg + strlen (bfin_cpus[i].name);

	if (*q == '\0')
	  {
	    bfin_si_revision = bfin_cpus[i].si_revision;
	    bfin_anomaly_checks |= bfin_cpus[i].anomaly_checks;
	  }
	else if (strcmp (q, "-none") == 0)
	  bfin_si_revision = -1;
      	else if (strcmp (q, "-any") == 0)
	  {
	    bfin_si_revision = 0xffff;
	    while (i < ARRAY_SIZE (bfin_cpus)
		   && bfin_cpus[i].type == bfin_cpu_type)
	      {
		bfin_anomaly_checks |= bfin_cpus[i].anomaly_checks;
		i++;
	      }
	  }
	else
	  {
	    unsigned int si_major, si_minor;
	    int rev_len, n;

	    rev_len = strlen (q);

	    if (sscanf (q, "-%u.%u%n", &si_major, &si_minor, &n) != 2
		|| n != rev_len
		|| si_major > 0xff || si_minor > 0xff)
	      {
	      invalid_silicon_revision:
		as_fatal ("-mcpu=%s has invalid silicon revision", arg);
	      }

	    bfin_si_revision = (si_major << 8) | si_minor;

	    while (i < ARRAY_SIZE (bfin_cpus)
		   && bfin_cpus[i].type == bfin_cpu_type
		   && bfin_cpus[i].si_revision != bfin_si_revision)
	      i++;

	    if (i == ARRAY_SIZE (bfin_cpus)
	       	|| bfin_cpus[i].type != bfin_cpu_type)
	      goto invalid_silicon_revision;

	    bfin_anomaly_checks |= bfin_cpus[i].anomaly_checks;
	  }

	break;
      }

    case OPTION_FDPIC:
      bfin_flags |= EF_BFIN_FDPIC;
      bfin_pic_flag = "-mfdpic";
      break;

    case OPTION_NOPIC:
      bfin_flags &= ~(EF_BFIN_FDPIC);
      bfin_pic_flag = 0;
      break;
    }

  return 1;
}

void
md_show_usage (FILE * stream)
{
  fprintf (stream, _(" Blackfin specific assembler options:\n"));
  fprintf (stream, _("  -mcpu=<cpu[-sirevision]> specify the name of the target CPU\n"));
  fprintf (stream, _("  -mfdpic                  assemble for the FDPIC ABI\n"));
  fprintf (stream, _("  -mno-fdpic/-mnopic       disable -mfdpic\n"));
}

/* Perform machine-specific initializations.  */
void
md_begin (void)
{
  /* Set the ELF flags if desired. */
  if (bfin_flags)
    bfd_set_private_flags (stdoutput, bfin_flags);

  /* Set the default machine type. */
  if (!bfd_set_arch_mach (stdoutput, bfd_arch_bfin, 0))
    as_warn (_("Could not set architecture and machine."));

  /* Ensure that lines can begin with '(', for multiple
     register stack pops. */
  lex_type ['('] = LEX_BEGIN_NAME;

#ifdef OBJ_ELF
  record_alignment (text_section, 2);
  record_alignment (data_section, 2);
  record_alignment (bss_section, 2);
#endif

  errorf = stderr;
  obstack_init (&mempool);

#ifdef DEBUG
  extern int debug_codeselection;
  debug_codeselection = 1;
#endif

  last_insn_size = 0;
}

/* Perform the main parsing, and assembly of the input here.  Also,
   call the required routines for alignment and fixups here.
   This is called for every line that contains real assembly code.  */

void
md_assemble (char *line)
{
  char *toP = 0;
  int size, insn_size;
  struct bfin_insn *tmp_insn;
  size_t len;
  static size_t buffer_len = 0;
  static char *current_inputline;
  parse_state state;

  len = strlen (line);
  if (len + 2 > buffer_len)
    {
      buffer_len = len + 40;
      current_inputline = XRESIZEVEC (char, current_inputline, buffer_len);
    }
  memcpy (current_inputline, line, len);
  current_inputline[len] = ';';
  current_inputline[len + 1] = '\0';

  state = parse (current_inputline);
  if (state == NO_INSN_GENERATED)
    return;

  for (insn_size = 0, tmp_insn = insn; tmp_insn; tmp_insn = tmp_insn->next)
    if (!tmp_insn->reloc || !tmp_insn->exp->symbol)
      insn_size += 2;

  if (insn_size)
    toP = frag_more (insn_size);

  last_insn_size = insn_size;

#ifdef DEBUG
  printf ("INS:");
#endif
  while (insn)
    {
      if (insn->reloc && insn->exp->symbol)
	{
	  char *prev_toP = toP - 2;
	  switch (insn->reloc)
	    {
	    case BFD_RELOC_BFIN_24_PCREL_JUMP_L:
	    case BFD_RELOC_24_PCREL:
	    case BFD_RELOC_BFIN_16_LOW:
	    case BFD_RELOC_BFIN_16_HIGH:
	      size = 4;
	      break;
	    default:
	      size = 2;
	    }

	  /* Following if condition checks for the arithmetic relocations.
	     If the case then it doesn't required to generate the code.
	     It has been assumed that, their ID will be contiguous.  */
	  if ((BFD_ARELOC_BFIN_PUSH <= insn->reloc
               && BFD_ARELOC_BFIN_COMP >= insn->reloc)
              || insn->reloc == BFD_RELOC_BFIN_16_IMM)
	    {
	      size = 2;
	    }
	  if (insn->reloc == BFD_ARELOC_BFIN_CONST
              || insn->reloc == BFD_ARELOC_BFIN_PUSH)
	    size = 4;

	  fix_new (frag_now,
                   (prev_toP - frag_now->fr_literal),
		   size, insn->exp->symbol, insn->exp->value,
                   insn->pcrel, insn->reloc);
	}
      else
	{
	  md_number_to_chars (toP, insn->value, 2);
	  toP += 2;
	}

#ifdef DEBUG
      printf (" reloc :");
      printf (" %02x%02x", ((unsigned char *) &insn->value)[0],
              ((unsigned char *) &insn->value)[1]);
      printf ("\n");
#endif
      insn = insn->next;
    }
#ifdef OBJ_ELF
  dwarf2_emit_insn (insn_size);
#endif

  while (*line++ != '\0')
    if (*line == '\n')
      bump_line_counters ();
}

/* Parse one line of instructions, and generate opcode for it.
   To parse the line, YACC and LEX are used, because the instruction set
   syntax doesn't confirm to the AT&T assembly syntax.
   To call a YACC & LEX generated parser, we must provide the input via
   a FILE stream, otherwise stdin is used by default.  Below the input
   to the function will be put into a temporary file, then the generated
   parser uses the temporary file for parsing.  */

static parse_state
parse (char *line)
{
  parse_state state;
  YY_BUFFER_STATE buffstate;

  buffstate = yy_scan_string (line);

  /* our lex requires setting the start state to keyword
     every line as the first word may be a keyword.
     Fixes a bug where we could not have keywords as labels.  */
  set_start_state ();

  /* Call yyparse here.  */
  state = yyparse ();
  if (state == SEMANTIC_ERROR)
    {
      as_bad (_("Parse failed."));
      insn = 0;
    }

  yy_delete_buffer (buffstate);
  return state;
}

/* We need to handle various expressions properly.
   Such as, [SP--] = 34, concerned by md_assemble().  */

void
md_operand (expressionS * expressionP)
{
  if (*input_line_pointer == '[')
    {
      as_tsktsk ("We found a '['!");
      input_line_pointer++;
      expression (expressionP);
    }
}

/* Handle undefined symbols. */
symbolS *
md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
{
  return (symbolS *) 0;
}

int
md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED,
                               segT segment ATTRIBUTE_UNUSED)
{
  return 0;
}

/* Convert from target byte order to host byte order.  */

static int
md_chars_to_number (char *val, int n)
{
  int retval;

  for (retval = 0; n--;)
    {
      retval <<= 8;
      retval |= val[n];
    }
  return retval;
}

void
md_apply_fix (fixS *fixP, valueT *valueP, segT seg ATTRIBUTE_UNUSED)
{
  char *where = fixP->fx_frag->fr_literal + fixP->fx_where;

  long value = *valueP;
  long newval;

  switch (fixP->fx_r_type)
    {
    case BFD_RELOC_BFIN_GOT:
    case BFD_RELOC_BFIN_GOT17M4:
    case BFD_RELOC_BFIN_FUNCDESC_GOT17M4:
      fixP->fx_no_overflow = 1;
      newval = md_chars_to_number (where, 2);
      newval |= 0x0 & 0x7f;
      md_number_to_chars (where, newval, 2);
      break;

    case BFD_RELOC_BFIN_10_PCREL:
      if (!value)
	break;
      if (value < -1024 || value > 1022)
	as_bad_where (fixP->fx_file, fixP->fx_line,
                      _("pcrel too far BFD_RELOC_BFIN_10"));

      /* 11 bit offset even numbered, so we remove right bit.  */
      value = value >> 1;
      newval = md_chars_to_number (where, 2);
      newval |= value & 0x03ff;
      md_number_to_chars (where, newval, 2);
      break;

    case BFD_RELOC_BFIN_12_PCREL_JUMP:
    case BFD_RELOC_BFIN_12_PCREL_JUMP_S:
    case BFD_RELOC_12_PCREL:
      if (!value)
	break;

      if (value < -4096 || value > 4094)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far BFD_RELOC_BFIN_12"));
      /* 13 bit offset even numbered, so we remove right bit.  */
      value = value >> 1;
      newval = md_chars_to_number (where, 2);
      newval |= value & 0xfff;
      md_number_to_chars (where, newval, 2);
      break;

    case BFD_RELOC_BFIN_16_LOW:
    case BFD_RELOC_BFIN_16_HIGH:
      fixP->fx_done = FALSE;
      break;

    case BFD_RELOC_BFIN_24_PCREL_JUMP_L:
    case BFD_RELOC_BFIN_24_PCREL_CALL_X:
    case BFD_RELOC_24_PCREL:
      if (!value)
	break;

      if (value < -16777216 || value > 16777214)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far BFD_RELOC_BFIN_24"));

      /* 25 bit offset even numbered, so we remove right bit.  */
      value = value >> 1;
      value++;

      md_number_to_chars (where - 2, value >> 16, 1);
      md_number_to_chars (where, value, 1);
      md_number_to_chars (where + 1, value >> 8, 1);
      break;

    case BFD_RELOC_BFIN_5_PCREL:	/* LSETUP (a, b) : "a" */
      if (!value)
	break;
      if (value < 4 || value > 30)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far BFD_RELOC_BFIN_5"));
      value = value >> 1;
      newval = md_chars_to_number (where, 1);
      newval = (newval & 0xf0) | (value & 0xf);
      md_number_to_chars (where, newval, 1);
      break;

    case BFD_RELOC_BFIN_11_PCREL:	/* LSETUP (a, b) : "b" */
      if (!value)
	break;
      value += 2;
      if (value < 4 || value > 2046)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far BFD_RELOC_BFIN_11_PCREL"));
      /* 11 bit unsigned even, so we remove right bit.  */
      value = value >> 1;
      newval = md_chars_to_number (where, 2);
      newval |= value & 0x03ff;
      md_number_to_chars (where, newval, 2);
      break;

    case BFD_RELOC_8:
      if (value < -0x80 || value >= 0x7f)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("rel too far BFD_RELOC_8"));
      md_number_to_chars (where, value, 1);
      break;

    case BFD_RELOC_BFIN_16_IMM:
    case BFD_RELOC_16:
      if (value < -0x8000 || value >= 0x7fff)
	as_bad_where (fixP->fx_file, fixP->fx_line, _("rel too far BFD_RELOC_16"));
      md_number_to_chars (where, value, 2);
      break;

    case BFD_RELOC_32:
      md_number_to_chars (where, value, 4);
      break;

    case BFD_RELOC_BFIN_PLTPC:
      md_number_to_chars (where, value, 2);
      break;

    case BFD_RELOC_BFIN_FUNCDESC:
    case BFD_RELOC_VTABLE_INHERIT:
    case BFD_RELOC_VTABLE_ENTRY:
      fixP->fx_done = FALSE;
      break;

    default:
      if ((BFD_ARELOC_BFIN_PUSH > fixP->fx_r_type) || (BFD_ARELOC_BFIN_COMP < fixP->fx_r_type))
	{
	  fprintf (stderr, "Relocation %d not handled in gas." " Contact support.\n", fixP->fx_r_type);
	  return;
	}
    }

  if (!fixP->fx_addsy)
    fixP->fx_done = TRUE;

}

/* Round up a section size to the appropriate boundary.  */
valueT
md_section_align (segT segment, valueT size)
{
  int boundary = bfd_section_alignment (segment);
  return ((size + (1 << boundary) - 1) & -(1 << boundary));
}


const char *
md_atof (int type, char * litP, int * sizeP)
{
  return ieee_md_atof (type, litP, sizeP, FALSE);
}


/* If while processing a fixup, a reloc really needs to be created
   then it is done here.  */

arelent *
tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
{
  arelent *reloc;

  reloc		      = XNEW (arelent);
  reloc->sym_ptr_ptr  = XNEW (asymbol *);
  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  reloc->address      = fixp->fx_frag->fr_address + fixp->fx_where;

  reloc->addend = fixp->fx_offset;
  reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);

  if (reloc->howto == (reloc_howto_type *) NULL)
    {
      as_bad_where (fixp->fx_file, fixp->fx_line,
		    /* xgettext:c-format.  */
		    _("reloc %d not supported by object file format"),
		    (int) fixp->fx_r_type);

      xfree (reloc);

      return NULL;
    }

  return reloc;
}

/*  The location from which a PC relative jump should be calculated,
    given a PC relative reloc.  */

long
md_pcrel_from_section (fixS *fixP, segT sec)
{
  if (fixP->fx_addsy != (symbolS *) NULL
      && (!S_IS_DEFINED (fixP->fx_addsy)
      || S_GET_SEGMENT (fixP->fx_addsy) != sec))
    {
      /* The symbol is undefined (or is defined but not in this section).
         Let the linker figure it out.  */
      return 0;
    }
  return fixP->fx_frag->fr_address + fixP->fx_where;
}

/* Return true if the fix can be handled by GAS, false if it must
   be passed through to the linker.  */

bfd_boolean
bfin_fix_adjustable (fixS *fixP)
{
  switch (fixP->fx_r_type)
    {
  /* Adjust_reloc_syms doesn't know about the GOT.  */
    case BFD_RELOC_BFIN_GOT:
    case BFD_RELOC_BFIN_PLTPC:
  /* We need the symbol name for the VTABLE entries.  */
    case BFD_RELOC_VTABLE_INHERIT:
    case BFD_RELOC_VTABLE_ENTRY:
      return 0;

    default:
      return 1;
    }
}

/* Special extra functions that help bfin-parse.y perform its job.  */

struct obstack mempool;

INSTR_T
conscode (INSTR_T head, INSTR_T tail)
{
  if (!head)
    return tail;
  head->next = tail;
  return head;
}

INSTR_T
conctcode (INSTR_T head, INSTR_T tail)
{
  INSTR_T temp = (head);
  if (!head)
    return tail;
  while (temp->next)
    temp = temp->next;
  temp->next = tail;

  return head;
}

INSTR_T
note_reloc (INSTR_T code, Expr_Node * symbol, int reloc, int pcrel)
{
  /* Assert that the symbol is not an operator.  */
  gas_assert (symbol->type == Expr_Node_Reloc);

  return note_reloc1 (code, symbol->value.s_value, reloc, pcrel);

}

INSTR_T
note_reloc1 (INSTR_T code, const char *symbol, int reloc, int pcrel)
{
  code->reloc = reloc;
  code->exp = mkexpr (0, symbol_find_or_make (symbol));
  code->pcrel = pcrel;
  return code;
}

INSTR_T
note_reloc2 (INSTR_T code, const char *symbol, int reloc, int value, int pcrel)
{
  code->reloc = reloc;
  code->exp = mkexpr (value, symbol_find_or_make (symbol));
  code->pcrel = pcrel;
  return code;
}

INSTR_T
gencode (unsigned long x)
{
  INSTR_T cell = XOBNEW (&mempool, struct bfin_insn);
  memset (cell, 0, sizeof (struct bfin_insn));
  cell->value = (x);
  return cell;
}

int reloc;
int ninsns;
int count_insns;

static void *
allocate (size_t n)
{
  return obstack_alloc (&mempool, n);
}

Expr_Node *
Expr_Node_Create (Expr_Node_Type type,
	          Expr_Node_Value value,
                  Expr_Node *Left_Child,
                  Expr_Node *Right_Child)
{


  Expr_Node *node = (Expr_Node *) allocate (sizeof (Expr_Node));
  node->type = type;
  node->value = value;
  node->Left_Child = Left_Child;
  node->Right_Child = Right_Child;
  return node;
}

static const char *con = ".__constant";
static const char *op = ".__operator";
static INSTR_T Expr_Node_Gen_Reloc_R (Expr_Node * head);
INSTR_T Expr_Node_Gen_Reloc (Expr_Node *head, int parent_reloc);

INSTR_T
Expr_Node_Gen_Reloc (Expr_Node * head, int parent_reloc)
{
  /* Top level relocation expression generator VDSP style.
   If the relocation is just by itself, generate one item
   else generate this convoluted expression.  */

  INSTR_T note = NULL_CODE;
  INSTR_T note1 = NULL_CODE;
  int pcrel = 1;  /* Is the parent reloc pc-relative?
		  This calculation here and HOWTO should match.  */

  if (parent_reloc)
    {
      /*  If it's 32 bit quantity then 16bit code needs to be added.  */
      int value = 0;

      if (head->type == Expr_Node_Constant)
	{
	  /* If note1 is not null code, we have to generate a right
             aligned value for the constant. Otherwise the reloc is
             a part of the basic command and the yacc file
             generates this.  */
	  value = head->value.i_value;
	}
      switch (parent_reloc)
	{
	  /*  Some relocations will need to allocate extra words.  */
	case BFD_RELOC_BFIN_16_IMM:
	case BFD_RELOC_BFIN_16_LOW:
	case BFD_RELOC_BFIN_16_HIGH:
	  note1 = conscode (gencode (value), NULL_CODE);
	  pcrel = 0;
	  break;
	case BFD_RELOC_BFIN_PLTPC:
	  note1 = conscode (gencode (value), NULL_CODE);
	  pcrel = 0;
	  break;
	case BFD_RELOC_16:
	case BFD_RELOC_BFIN_GOT:
	case BFD_RELOC_BFIN_GOT17M4:
	case BFD_RELOC_BFIN_FUNCDESC_GOT17M4:
	  note1 = conscode (gencode (value), NULL_CODE);
	  pcrel = 0;
	  break;
	case BFD_RELOC_24_PCREL:
	case BFD_RELOC_BFIN_24_PCREL_JUMP_L:
	case BFD_RELOC_BFIN_24_PCREL_CALL_X:
	  /* These offsets are even numbered pcrel.  */
	  note1 = conscode (gencode (value >> 1), NULL_CODE);
	  break;
	default:
	  note1 = NULL_CODE;
	}
    }
  if (head->type == Expr_Node_Constant)
    note = note1;
  else if (head->type == Expr_Node_Reloc)
    {
      note = note_reloc1 (gencode (0), head->value.s_value, parent_reloc, pcrel);
      if (note1 != NULL_CODE)
	note = conscode (note1, note);
    }
  else if (head->type == Expr_Node_Binop
	   && (head->value.op_value == Expr_Op_Type_Add
	       || head->value.op_value == Expr_Op_Type_Sub)
	   && head->Left_Child->type == Expr_Node_Reloc
	   && head->Right_Child->type == Expr_Node_Constant)
    {
      int val = head->Right_Child->value.i_value;
      if (head->value.op_value == Expr_Op_Type_Sub)
	val = -val;
      note = conscode (note_reloc2 (gencode (0), head->Left_Child->value.s_value,
				    parent_reloc, val, 0),
		       NULL_CODE);
      if (note1 != NULL_CODE)
	note = conscode (note1, note);
    }
  else
    {
      /* Call the recursive function.  */
      note = note_reloc1 (gencode (0), op, parent_reloc, pcrel);
      if (note1 != NULL_CODE)
	note = conscode (note1, note);
      note = conctcode (Expr_Node_Gen_Reloc_R (head), note);
    }
  return note;
}

static INSTR_T
Expr_Node_Gen_Reloc_R (Expr_Node * head)
{

  INSTR_T note = 0;
  INSTR_T note1 = 0;

  switch (head->type)
    {
    case Expr_Node_Constant:
      note = conscode (note_reloc2 (gencode (0), con, BFD_ARELOC_BFIN_CONST, head->value.i_value, 0), NULL_CODE);
      break;
    case Expr_Node_Reloc:
      note = conscode (note_reloc (gencode (0), head, BFD_ARELOC_BFIN_PUSH, 0), NULL_CODE);
      break;
    case Expr_Node_Binop:
      note1 = conctcode (Expr_Node_Gen_Reloc_R (head->Left_Child), Expr_Node_Gen_Reloc_R (head->Right_Child));
      switch (head->value.op_value)
	{
	case Expr_Op_Type_Add:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_ADD, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Sub:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_SUB, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Mult:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_MULT, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Div:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_DIV, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Mod:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_MOD, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Lshift:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_LSHIFT, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_Rshift:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_RSHIFT, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_BAND:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_AND, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_BOR:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_OR, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_BXOR:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_XOR, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_LAND:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_LAND, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_LOR:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_LOR, 0), NULL_CODE));
	  break;
	default:
	  fprintf (stderr, "%s:%d:Unknown operator found for arithmetic" " relocation", __FILE__, __LINE__);


	}
      break;
    case Expr_Node_Unop:
      note1 = conscode (Expr_Node_Gen_Reloc_R (head->Left_Child), NULL_CODE);
      switch (head->value.op_value)
	{
	case Expr_Op_Type_NEG:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_NEG, 0), NULL_CODE));
	  break;
	case Expr_Op_Type_COMP:
	  note = conctcode (note1, conscode (note_reloc1 (gencode (0), op, BFD_ARELOC_BFIN_COMP, 0), NULL_CODE));
	  break;
	default:
	  fprintf (stderr, "%s:%d:Unknown operator found for arithmetic" " relocation", __FILE__, __LINE__);
	}
      break;
    default:
      fprintf (stderr, "%s:%d:Unknown node expression found during " "arithmetic relocation generation", __FILE__, __LINE__);
    }
  return note;
}

/* Blackfin opcode generation.  */

/* These functions are called by the generated parser
   (from bfin-parse.y), the register type classification
   happens in bfin-lex.l.  */

#include "bfin-aux.h"
#include "opcode/bfin.h"

#define INIT(t)  t c_code = init_##t
#define ASSIGN(x) c_code.opcode |= ((x & c_code.mask_##x)<<c_code.bits_##x)
#define ASSIGNF(x,f) c_code.opcode |= ((x & c_code.mask_##f)<<c_code.bits_##f)
#define ASSIGN_R(x) c_code.opcode |= (((x ? (x->regno & CODE_MASK) : 0) & c_code.mask_##x)<<c_code.bits_##x)

#define HI(x) ((x >> 16) & 0xffff)
#define LO(x) ((x      ) & 0xffff)

#define GROUP(x) ((x->regno & CLASS_MASK) >> 4)

#define GEN_OPCODE32()  \
	conscode (gencode (HI (c_code.opcode)), \
	conscode (gencode (LO (c_code.opcode)), NULL_CODE))

#define GEN_OPCODE16()  \
	conscode (gencode (c_code.opcode), NULL_CODE)


/*  32 BIT INSTRUCTIONS.  */


/* DSP32 instruction generation.  */

INSTR_T
bfin_gen_dsp32mac (int op1, int MM, int mmod, int w1, int P,
	           int h01, int h11, int h00, int h10, int op0,
                   REG_T dst, REG_T src0, REG_T src1, int w0)
{
  INIT (DSP32Mac);

  ASSIGN (op0);
  ASSIGN (op1);
  ASSIGN (MM);
  ASSIGN (mmod);
  ASSIGN (w0);
  ASSIGN (w1);
  ASSIGN (h01);
  ASSIGN (h11);
  ASSIGN (h00);
  ASSIGN (h10);
  ASSIGN (P);

  /* If we have full reg assignments, mask out LSB to encode
  single or simultaneous even/odd register moves.  */
  if (P)
    {
      dst->regno &= 0x06;
    }

  ASSIGN_R (dst);
  ASSIGN_R (src0);
  ASSIGN_R (src1);

  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_dsp32mult (int op1, int MM, int mmod, int w1, int P,
	            int h01, int h11, int h00, int h10, int op0,
                    REG_T dst, REG_T src0, REG_T src1, int w0)
{
  INIT (DSP32Mult);

  ASSIGN (op0);
  ASSIGN (op1);
  ASSIGN (MM);
  ASSIGN (mmod);
  ASSIGN (w0);
  ASSIGN (w1);
  ASSIGN (h01);
  ASSIGN (h11);
  ASSIGN (h00);
  ASSIGN (h10);
  ASSIGN (P);

  if (P)
    {
      dst->regno &= 0x06;
    }

  ASSIGN_R (dst);
  ASSIGN_R (src0);
  ASSIGN_R (src1);

  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_dsp32alu (int HL, int aopcde, int aop, int s, int x,
              REG_T dst0, REG_T dst1, REG_T src0, REG_T src1)
{
  INIT (DSP32Alu);

  ASSIGN (HL);
  ASSIGN (aopcde);
  ASSIGN (aop);
  ASSIGN (s);
  ASSIGN (x);
  ASSIGN_R (dst0);
  ASSIGN_R (dst1);
  ASSIGN_R (src0);
  ASSIGN_R (src1);

  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_dsp32shift (int sopcde, REG_T dst0, REG_T src0,
                REG_T src1, int sop, int HLs)
{
  INIT (DSP32Shift);

  ASSIGN (sopcde);
  ASSIGN (sop);
  ASSIGN (HLs);

  ASSIGN_R (dst0);
  ASSIGN_R (src0);
  ASSIGN_R (src1);

  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_dsp32shiftimm (int sopcde, REG_T dst0, int immag,
                   REG_T src1, int sop, int HLs)
{
  INIT (DSP32ShiftImm);

  ASSIGN (sopcde);
  ASSIGN (sop);
  ASSIGN (HLs);

  ASSIGN_R (dst0);
  ASSIGN (immag);
  ASSIGN_R (src1);

  return GEN_OPCODE32 ();
}

/* LOOP SETUP.  */

INSTR_T
bfin_gen_loopsetup (Expr_Node * psoffset, REG_T c, int rop,
               Expr_Node * peoffset, REG_T reg)
{
  int soffset, eoffset;
  INIT (LoopSetup);

  soffset = (EXPR_VALUE (psoffset) >> 1);
  ASSIGN (soffset);
  eoffset = (EXPR_VALUE (peoffset) >> 1);
  ASSIGN (eoffset);
  ASSIGN (rop);
  ASSIGN_R (c);
  ASSIGN_R (reg);

  return
      conscode (gencode (HI (c_code.opcode)),
		conctcode (Expr_Node_Gen_Reloc (psoffset, BFD_RELOC_BFIN_5_PCREL),
			   conctcode (gencode (LO (c_code.opcode)), Expr_Node_Gen_Reloc (peoffset, BFD_RELOC_BFIN_11_PCREL))));

}

/*  Call, Link.  */

INSTR_T
bfin_gen_calla (Expr_Node * addr, int S)
{
  int val;
  int high_val;
  int rel = 0;
  INIT (CALLa);

  switch(S){
   case 0 : rel = BFD_RELOC_BFIN_24_PCREL_JUMP_L; break;
   case 1 : rel = BFD_RELOC_24_PCREL; break;
   case 2 : rel = BFD_RELOC_BFIN_PLTPC; break;
   default : break;
  }

  ASSIGN (S);

  val = EXPR_VALUE (addr) >> 1;
  high_val = val >> 16;

  return conscode (gencode (HI (c_code.opcode) | (high_val & 0xff)),
                     Expr_Node_Gen_Reloc (addr, rel));
  }

INSTR_T
bfin_gen_linkage (int R, int framesize)
{
  INIT (Linkage);

  ASSIGN (R);
  ASSIGN (framesize);

  return GEN_OPCODE32 ();
}


/* Load and Store.  */

INSTR_T
bfin_gen_ldimmhalf (REG_T reg, int H, int S, int Z, Expr_Node * phword, int rel)
{
  int grp, hword;
  unsigned val = EXPR_VALUE (phword);
  INIT (LDIMMhalf);

  ASSIGN (H);
  ASSIGN (S);
  ASSIGN (Z);

  ASSIGN_R (reg);
  grp = (GROUP (reg));
  ASSIGN (grp);
  if (rel == 2)
    {
      return conscode (gencode (HI (c_code.opcode)), Expr_Node_Gen_Reloc (phword, BFD_RELOC_BFIN_16_IMM));
    }
  else if (rel == 1)
    {
      return conscode (gencode (HI (c_code.opcode)), Expr_Node_Gen_Reloc (phword, IS_H (*reg) ? BFD_RELOC_BFIN_16_HIGH : BFD_RELOC_BFIN_16_LOW));
    }
  else
    {
      hword = val;
      ASSIGN (hword);
    }
  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_ldstidxi (REG_T ptr, REG_T reg, int W, int sz, int Z, Expr_Node * poffset)
{
  INIT (LDSTidxI);

  if (!IS_PREG (*ptr) || (!IS_DREG (*reg) && !Z))
    {
      fprintf (stderr, "Warning: possible mixup of Preg/Dreg\n");
      return 0;
    }

  ASSIGN_R (ptr);
  ASSIGN_R (reg);
  ASSIGN (W);
  ASSIGN (sz);

  ASSIGN (Z);

  if (poffset->type != Expr_Node_Constant)
    {
      /* a GOT relocation such as R0 = [P5 + symbol@GOT] */
      /* distinguish between R0 = [P5 + symbol@GOT] and
	 P5 = [P5 + _current_shared_library_p5_offset_]
      */
      if (poffset->type == Expr_Node_Reloc
	  && !strcmp (poffset->value.s_value,
		      "_current_shared_library_p5_offset_"))
	{
	  return  conscode (gencode (HI (c_code.opcode)),
			    Expr_Node_Gen_Reloc(poffset, BFD_RELOC_16));
	}
      else if (poffset->type != Expr_Node_GOT_Reloc)
	abort ();

      return conscode (gencode (HI (c_code.opcode)),
		       Expr_Node_Gen_Reloc(poffset->Left_Child,
					   poffset->value.i_value));
    }
  else
    {
      int value, offset;
      switch (sz)
	{				/* load/store access size */
	case 0:			/* 32 bit */
	  value = EXPR_VALUE (poffset) >> 2;
	  break;
	case 1:			/* 16 bit */
	  value = EXPR_VALUE (poffset) >> 1;
	  break;
	case 2:			/* 8 bit */
	  value = EXPR_VALUE (poffset);
	  break;
	default:
	  abort ();
	}

      offset = (value & 0xffff);
      ASSIGN (offset);
      return GEN_OPCODE32 ();
    }
}


INSTR_T
bfin_gen_ldst (REG_T ptr, REG_T reg, int aop, int sz, int Z, int W)
{
  INIT (LDST);

  if (!IS_PREG (*ptr) || (!IS_DREG (*reg) && !Z))
    {
      fprintf (stderr, "Warning: possible mixup of Preg/Dreg\n");
      return 0;
    }

  ASSIGN_R (ptr);
  ASSIGN_R (reg);
  ASSIGN (aop);
  ASSIGN (sz);
  ASSIGN (Z);
  ASSIGN (W);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ldstii (REG_T ptr, REG_T reg, Expr_Node * poffset, int W, int opc)
{
  int offset;
  int value = 0;
  INIT (LDSTii);

  if (!IS_PREG (*ptr))
    {
      fprintf (stderr, "Warning: possible mixup of Preg/Dreg\n");
      return 0;
    }

  switch (opc)
    {
    case 1:
    case 2:
      value = EXPR_VALUE (poffset) >> 1;
      break;
    case 0:
    case 3:
      value = EXPR_VALUE (poffset) >> 2;
      break;
    }

  ASSIGN_R (ptr);
  ASSIGN_R (reg);

  offset = value;
  ASSIGN (offset);
  ASSIGN (W);
  ASSIGNF (opc, op);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ldstiifp (REG_T sreg, Expr_Node * poffset, int W)
{
  /* Set bit 4 if it's a Preg.  */
  int reg = (sreg->regno & CODE_MASK) | (IS_PREG (*sreg) ? 0x8 : 0x0);
  int offset = ((~(EXPR_VALUE (poffset) >> 2)) & 0x1f) + 1;
  INIT (LDSTiiFP);
  ASSIGN (reg);
  ASSIGN (offset);
  ASSIGN (W);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ldstpmod (REG_T ptr, REG_T reg, int aop, int W, REG_T idx)
{
  INIT (LDSTpmod);

  ASSIGN_R (ptr);
  ASSIGN_R (reg);
  ASSIGN (aop);
  ASSIGN (W);
  ASSIGN_R (idx);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_dspldst (REG_T i, REG_T reg, int aop, int W, int m)
{
  INIT (DspLDST);

  ASSIGN_R (i);
  ASSIGN_R (reg);
  ASSIGN (aop);
  ASSIGN (W);
  ASSIGN (m);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_logi2op (int opc, int src, int dst)
{
  INIT (LOGI2op);

  ASSIGN (opc);
  ASSIGN (src);
  ASSIGN (dst);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_brcc (int T, int B, Expr_Node * poffset)
{
  int offset;
  INIT (BRCC);

  ASSIGN (T);
  ASSIGN (B);
  offset = ((EXPR_VALUE (poffset) >> 1));
  ASSIGN (offset);
  return conscode (gencode (c_code.opcode), Expr_Node_Gen_Reloc (poffset, BFD_RELOC_BFIN_10_PCREL));
}

INSTR_T
bfin_gen_ujump (Expr_Node * poffset)
{
  int offset;
  INIT (UJump);

  offset = ((EXPR_VALUE (poffset) >> 1));
  ASSIGN (offset);

  return conscode (gencode (c_code.opcode),
                   Expr_Node_Gen_Reloc (
                       poffset, BFD_RELOC_BFIN_12_PCREL_JUMP_S));
}

INSTR_T
bfin_gen_alu2op (REG_T dst, REG_T src, int opc)
{
  INIT (ALU2op);

  ASSIGN_R (dst);
  ASSIGN_R (src);
  ASSIGN (opc);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_compi2opd (REG_T dst, int src, int opc)
{
  INIT (COMPI2opD);

  ASSIGN_R (dst);
  ASSIGN (src);
  ASSIGNF (opc, op);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_compi2opp (REG_T dst, int src, int opc)
{
  INIT (COMPI2opP);

  ASSIGN_R (dst);
  ASSIGN (src);
  ASSIGNF (opc, op);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_dagmodik (REG_T i, int opc)
{
  INIT (DagMODik);

  ASSIGN_R (i);
  ASSIGNF (opc, op);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_dagmodim (REG_T i, REG_T m, int opc, int br)
{
  INIT (DagMODim);

  ASSIGN_R (i);
  ASSIGN_R (m);
  ASSIGNF (opc, op);
  ASSIGN (br);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ptr2op (REG_T dst, REG_T src, int opc)
{
  INIT (PTR2op);

  ASSIGN_R (dst);
  ASSIGN_R (src);
  ASSIGN (opc);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_comp3op (REG_T src0, REG_T src1, REG_T dst, int opc)
{
  INIT (COMP3op);

  ASSIGN_R (src0);
  ASSIGN_R (src1);
  ASSIGN_R (dst);
  ASSIGN (opc);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ccflag (REG_T x, int y, int opc, int I, int G)
{
  INIT (CCflag);

  ASSIGN_R (x);
  ASSIGN (y);
  ASSIGN (opc);
  ASSIGN (I);
  ASSIGN (G);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_ccmv (REG_T src, REG_T dst, int T)
{
  int s, d;
  INIT (CCmv);

  ASSIGN_R (src);
  ASSIGN_R (dst);
  s = (GROUP (src));
  ASSIGN (s);
  d = (GROUP (dst));
  ASSIGN (d);
  ASSIGN (T);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_cc2stat (int cbit, int opc, int D)
{
  INIT (CC2stat);

  ASSIGN (cbit);
  ASSIGNF (opc, op);
  ASSIGN (D);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_regmv (REG_T src, REG_T dst)
{
  int gs, gd;
  INIT (RegMv);

  ASSIGN_R (src);
  ASSIGN_R (dst);

  gs = (GROUP (src));
  ASSIGN (gs);
  gd = (GROUP (dst));
  ASSIGN (gd);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_cc2dreg (int opc, REG_T reg)
{
  INIT (CC2dreg);

  ASSIGNF (opc, op);
  ASSIGN_R (reg);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_progctrl (int prgfunc, int poprnd)
{
  INIT (ProgCtrl);

  ASSIGN (prgfunc);
  ASSIGN (poprnd);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_cactrl (REG_T reg, int a, int opc)
{
  INIT (CaCTRL);

  ASSIGN_R (reg);
  ASSIGN (a);
  ASSIGNF (opc, op);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_pushpopmultiple (int dr, int pr, int d, int p, int W)
{
  INIT (PushPopMultiple);

  ASSIGN (dr);
  ASSIGN (pr);
  ASSIGN (d);
  ASSIGN (p);
  ASSIGN (W);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_pushpopreg (REG_T reg, int W)
{
  int grp;
  INIT (PushPopReg);

  ASSIGN_R (reg);
  grp = (GROUP (reg));
  ASSIGN (grp);
  ASSIGN (W);

  return GEN_OPCODE16 ();
}

/* Pseudo Debugging Support.  */

INSTR_T
bfin_gen_pseudodbg (int fn, int reg, int grp)
{
  INIT (PseudoDbg);

  ASSIGN (fn);
  ASSIGN (reg);
  ASSIGN (grp);

  return GEN_OPCODE16 ();
}

INSTR_T
bfin_gen_pseudodbg_assert (int dbgop, REG_T regtest, int expected)
{
  int grp;
  INIT (PseudoDbg_Assert);

  ASSIGN (dbgop);
  ASSIGN_R (regtest);
  grp = GROUP (regtest);
  ASSIGN (grp);
  ASSIGN (expected);

  return GEN_OPCODE32 ();
}

INSTR_T
bfin_gen_pseudochr (int ch)
{
  INIT (PseudoChr);

  ASSIGN (ch);

  return GEN_OPCODE16 ();
}

/* Multiple instruction generation.  */

INSTR_T
bfin_gen_multi_instr (INSTR_T dsp32, INSTR_T dsp16_grp1, INSTR_T dsp16_grp2)
{
  INSTR_T walk;

  /* If it's a 0, convert into MNOP. */
  if (dsp32)
    {
      walk = dsp32->next;
      SET_MULTI_INSTRUCTION_BIT (dsp32);
    }
  else
    {
      dsp32 = gencode (0xc803);
      walk = gencode (0x1800);
      dsp32->next = walk;
    }

  if (!dsp16_grp1)
    {
      dsp16_grp1 = gencode (0x0000);
    }

  if (!dsp16_grp2)
    {
      dsp16_grp2 = gencode (0x0000);
    }

  walk->next = dsp16_grp1;
  dsp16_grp1->next = dsp16_grp2;
  dsp16_grp2->next = NULL_CODE;

  return dsp32;
}

INSTR_T
bfin_gen_loop (Expr_Node *exp, REG_T reg, int rop, REG_T preg)
{
  const char *loopsym;
  char *lbeginsym, *lendsym;
  Expr_Node_Value lbeginval, lendval;
  Expr_Node *lbegin, *lend;
  symbolS *sym;

  loopsym = exp->value.s_value;
  lbeginsym = (char *) xmalloc (strlen (loopsym) + strlen ("__BEGIN") + 5);
  lendsym = (char *) xmalloc (strlen (loopsym) + strlen ("__END") + 5);

  lbeginsym[0] = 0;
  lendsym[0] = 0;

  strcat (lbeginsym, "L$L$");
  strcat (lbeginsym, loopsym);
  strcat (lbeginsym, "__BEGIN");

  strcat (lendsym, "L$L$");
  strcat (lendsym, loopsym);
  strcat (lendsym, "__END");

  lbeginval.s_value = lbeginsym;
  lendval.s_value = lendsym;

  lbegin = Expr_Node_Create (Expr_Node_Reloc, lbeginval, NULL, NULL);
  lend   = Expr_Node_Create (Expr_Node_Reloc, lendval, NULL, NULL);

  sym = symbol_find(loopsym);
  if (!S_IS_LOCAL (sym) || (S_IS_LOCAL (sym) && !symbol_used_p (sym)))
    symbol_remove (sym, &symbol_rootP, &symbol_lastP);

  return bfin_gen_loopsetup (lbegin, reg, rop, lend, preg);
}

void
bfin_loop_attempt_create_label (Expr_Node *exp, int is_begin)
{
  char *name;
  name = fb_label_name (exp->value.i_value, is_begin);
  exp->value.s_value = xstrdup (name);
  exp->type = Expr_Node_Reloc;
}

void
bfin_loop_beginend (Expr_Node *exp, int begin)
{
  const char *loopsym;
  char *label_name;
  symbolS *linelabel;
  const char *suffix = begin ? "__BEGIN" : "__END";

  loopsym = exp->value.s_value;
  label_name = (char *) xmalloc (strlen (loopsym) + strlen (suffix) + 5);

  label_name[0] = 0;

  strcat (label_name, "L$L$");
  strcat (label_name, loopsym);
  strcat (label_name, suffix);

  linelabel = colon (label_name);

  /* LOOP_END follows the last instruction in the loop.
     Adjust label address.  */
  if (!begin)
    *symbol_X_add_number (linelabel) -= last_insn_size;
}

bfd_boolean
bfin_eol_in_insn (char *line)
{
   /* Allow a new-line to appear in the middle of a multi-issue instruction.  */

   char *temp = line;

  if (*line != '\n')
    return FALSE;

  /* A semi-colon followed by a newline is always the end of a line.  */
  if (line[-1] == ';')
    return FALSE;

  if (line[-1] == '|')
    return TRUE;

  /* If the || is on the next line, there might be leading whitespace.  */
  temp++;
  while (*temp == ' ' || *temp == '\t') temp++;

  if (*temp == '|')
    return TRUE;

  return FALSE;
}

bfd_boolean
bfin_start_label (char *s)
{
  while (*s != 0)
    {
      if (*s == '(' || *s == '[')
	return FALSE;
      s++;
    }

  return TRUE;
}

int
bfin_force_relocation (struct fix *fixp)
{
  if (fixp->fx_r_type ==BFD_RELOC_BFIN_16_LOW
      || fixp->fx_r_type == BFD_RELOC_BFIN_16_HIGH)
    return TRUE;

  return generic_force_reloc (fixp);
}

/* This is a stripped down version of the disassembler.  The only thing it
   does is return a mask of registers modified by an instruction.  Only
   instructions that can occur in a parallel-issue bundle are handled, and
   only the registers that can cause a conflict are recorded.  */

#define DREG_MASK(n) (0x101 << (n))
#define DREGH_MASK(n) (0x100 << (n))
#define DREGL_MASK(n) (0x001 << (n))
#define IREG_MASK(n) (1 << ((n) + 16))

static int
decode_ProgCtrl_0 (int iw0)
{
  if (iw0 == 0)
    return 0;
  abort ();
}

static int
decode_LDSTpmod_0 (int iw0)
{
  /* LDSTpmod
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 0 | 0 |.W.|.aop...|.reg.......|.idx.......|.ptr.......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int W   = ((iw0 >> LDSTpmod_W_bits) & LDSTpmod_W_mask);
  int aop = ((iw0 >> LDSTpmod_aop_bits) & LDSTpmod_aop_mask);
  int idx = ((iw0 >> LDSTpmod_idx_bits) & LDSTpmod_idx_mask);
  int ptr = ((iw0 >> LDSTpmod_ptr_bits) & LDSTpmod_ptr_mask);
  int reg = ((iw0 >> LDSTpmod_reg_bits) & LDSTpmod_reg_mask);

  if (aop == 1 && W == 0 && idx == ptr)
    return DREGL_MASK (reg);
  else if (aop == 2 && W == 0 && idx == ptr)
    return DREGH_MASK (reg);
  else if (aop == 1 && W == 1 && idx == ptr)
    return 0;
  else if (aop == 2 && W == 1 && idx == ptr)
    return 0;
  else if (aop == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && W == 0)
    return DREGL_MASK (reg);
  else if (aop == 2 && W == 0)
    return DREGH_MASK (reg);
  else if (aop == 3 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 3 && W == 1)
    return DREG_MASK (reg);
  else if (aop == 0 && W == 1)
    return 0;
  else if (aop == 1 && W == 1)
    return 0;
  else if (aop == 2 && W == 1)
    return 0;
  else
    return 0;

  return 2;
}

static int
decode_dagMODim_0 (int iw0)
{
  /* dagMODim
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |.br| 1 | 1 |.op|.m.....|.i.....|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int i  = ((iw0 >> DagMODim_i_bits) & DagMODim_i_mask);
  int opc  = ((iw0 >> DagMODim_op_bits) & DagMODim_op_mask);

  if (opc == 0 || opc == 1)
    return IREG_MASK (i);
  else
    return 0;

  return 2;
}

static int
decode_dagMODik_0 (int iw0)
{
  /* dagMODik
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |.op....|.i.....|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int i  = ((iw0 >> DagMODik_i_bits) & DagMODik_i_mask);
  return IREG_MASK (i);
}

/* GOOD */
static int
decode_dspLDST_0 (int iw0)
{
  /* dspLDST
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 0 | 1 | 1 | 1 |.W.|.aop...|.m.....|.i.....|.reg.......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int i   = ((iw0 >> DspLDST_i_bits) & DspLDST_i_mask);
  int m   = ((iw0 >> DspLDST_m_bits) & DspLDST_m_mask);
  int W   = ((iw0 >> DspLDST_W_bits) & DspLDST_W_mask);
  int aop = ((iw0 >> DspLDST_aop_bits) & DspLDST_aop_mask);
  int reg = ((iw0 >> DspLDST_reg_bits) & DspLDST_reg_mask);

  if (aop == 0 && W == 0 && m == 0)
    return DREG_MASK (reg) | IREG_MASK (i);
  else if (aop == 0 && W == 0 && m == 1)
    return DREGL_MASK (reg) | IREG_MASK (i);
  else if (aop == 0 && W == 0 && m == 2)
    return DREGH_MASK (reg) | IREG_MASK (i);
  else if (aop == 1 && W == 0 && m == 0)
    return DREG_MASK (reg) | IREG_MASK (i);
  else if (aop == 1 && W == 0 && m == 1)
    return DREGL_MASK (reg) | IREG_MASK (i);
  else if (aop == 1 && W == 0 && m == 2)
    return DREGH_MASK (reg) | IREG_MASK (i);
  else if (aop == 2 && W == 0 && m == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && W == 0 && m == 1)
    return DREGL_MASK (reg);
  else if (aop == 2 && W == 0 && m == 2)
    return DREGH_MASK (reg);
  else if (aop == 0 && W == 1 && m == 0)
    return IREG_MASK (i);
  else if (aop == 0 && W == 1 && m == 1)
    return IREG_MASK (i);
  else if (aop == 0 && W == 1 && m == 2)
    return IREG_MASK (i);
  else if (aop == 1 && W == 1 && m == 0)
    return IREG_MASK (i);
  else if (aop == 1 && W == 1 && m == 1)
    return IREG_MASK (i);
  else if (aop == 1 && W == 1 && m == 2)
    return IREG_MASK (i);
  else if (aop == 2 && W == 1 && m == 0)
    return 0;
  else if (aop == 2 && W == 1 && m == 1)
    return 0;
  else if (aop == 2 && W == 1 && m == 2)
    return 0;
  else if (aop == 3 && W == 0)
    return DREG_MASK (reg) | IREG_MASK (i);
  else if (aop == 3 && W == 1)
    return IREG_MASK (i);

  abort ();
}

/* GOOD */
static int
decode_LDST_0 (int iw0)
{
  /* LDST
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 0 | 1 |.sz....|.W.|.aop...|.Z.|.ptr.......|.reg.......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int Z   = ((iw0 >> LDST_Z_bits) & LDST_Z_mask);
  int W   = ((iw0 >> LDST_W_bits) & LDST_W_mask);
  int sz  = ((iw0 >> LDST_sz_bits) & LDST_sz_mask);
  int aop = ((iw0 >> LDST_aop_bits) & LDST_aop_mask);
  int reg = ((iw0 >> LDST_reg_bits) & LDST_reg_mask);

  if (aop == 0 && sz == 0 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 0 && sz == 0 && Z == 1 && W == 0)
    return 0;
  else if (aop == 0 && sz == 1 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 0 && sz == 1 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 0 && sz == 2 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 0 && sz == 2 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && sz == 0 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && sz == 0 && Z == 1 && W == 0)
    return 0;
  else if (aop == 1 && sz == 1 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && sz == 1 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && sz == 2 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 1 && sz == 2 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && sz == 0 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && sz == 0 && Z == 1 && W == 0)
    return 0;
  else if (aop == 2 && sz == 1 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && sz == 1 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && sz == 2 && Z == 0 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 2 && sz == 2 && Z == 1 && W == 0)
    return DREG_MASK (reg);
  else if (aop == 0 && sz == 0 && Z == 0 && W == 1)
    return 0;
  else if (aop == 0 && sz == 0 && Z == 1 && W == 1)
    return 0;
  else if (aop == 0 && sz == 1 && Z == 0 && W == 1)
    return 0;
  else if (aop == 0 && sz == 2 && Z == 0 && W == 1)
    return 0;
  else if (aop == 1 && sz == 0 && Z == 0 && W == 1)
    return 0;
  else if (aop == 1 && sz == 0 && Z == 1 && W == 1)
    return 0;
  else if (aop == 1 && sz == 1 && Z == 0 && W == 1)
    return 0;
  else if (aop == 1 && sz == 2 && Z == 0 && W == 1)
    return 0;
  else if (aop == 2 && sz == 0 && Z == 0 && W == 1)
    return 0;
  else if (aop == 2 && sz == 0 && Z == 1 && W == 1)
    return 0;
  else if (aop == 2 && sz == 1 && Z == 0 && W == 1)
    return 0;
  else if (aop == 2 && sz == 2 && Z == 0 && W == 1)
    return 0;

  abort ();
}

static int
decode_LDSTiiFP_0 (int iw0)
{
  /* LDSTiiFP
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 1 | 1 | 1 | 0 |.W.|.offset............|.reg...........|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int reg = ((iw0 >> LDSTiiFP_reg_bits) & LDSTiiFP_reg_mask);
  int W = ((iw0 >> LDSTiiFP_W_bits) & LDSTiiFP_W_mask);

  if (W == 0)
    return reg < 8 ? DREG_MASK (reg) : 0;
  else
    return 0;
}

static int
decode_LDSTii_0 (int iw0)
{
  /* LDSTii
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 0 | 1 |.W.|.op....|.offset........|.ptr.......|.reg.......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int reg = ((iw0 >> LDSTii_reg_bit) & LDSTii_reg_mask);
  int opc = ((iw0 >> LDSTii_op_bit) & LDSTii_op_mask);
  int W = ((iw0 >> LDSTii_W_bit) & LDSTii_W_mask);

  if (W == 0 && opc != 3)
    return DREG_MASK (reg);
  else if (W == 0 && opc == 3)
   return 0;
  else if (W == 1 && opc == 0)
    return 0;
  else if (W == 1 && opc == 1)
    return 0;
  else if (W == 1 && opc == 3)
    return 0;

  abort ();
}

static int
decode_dsp32mac_0 (int iw0, int iw1)
{
  int result = 0;
  /* dsp32mac
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 1 | 0 | 0 |.M.| 0 | 0 |.mmod..........|.MM|.P.|.w1|.op1...|
     |.h01|.h11|.w0|.op0...|.h00|.h10|.dst.......|.src0......|.src1..|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int op1  = ((iw0 >> (DSP32Mac_op1_bits - 16)) & DSP32Mac_op1_mask);
  int w1   = ((iw0 >> (DSP32Mac_w1_bits - 16)) & DSP32Mac_w1_mask);
  int P    = ((iw0 >> (DSP32Mac_p_bits - 16)) & DSP32Mac_p_mask);
  int mmod = ((iw0 >> (DSP32Mac_mmod_bits - 16)) & DSP32Mac_mmod_mask);
  int w0   = ((iw1 >> DSP32Mac_w0_bits) & DSP32Mac_w0_mask);
  int MM   = ((iw1 >> DSP32Mac_MM_bits) & DSP32Mac_MM_mask);
  int dst  = ((iw1 >> DSP32Mac_dst_bits) & DSP32Mac_dst_mask);
  int op0  = ((iw1 >> DSP32Mac_op0_bits) & DSP32Mac_op0_mask);

  if (w0 == 0 && w1 == 0 && op1 == 3 && op0 == 3)
    return 0;

  if (op1 == 3 && MM)
    return 0;

  if ((w1 || w0) && mmod == M_W32)
    return 0;

  if (((1 << mmod) & (P ? 0x131b : 0x1b5f)) == 0)
    return 0;

  if (w1 == 1 || op1 != 3)
    {
      if (w1)
	{
	  if (P)
	    return DREG_MASK (dst + 1);
	  else
	    return DREGH_MASK (dst);
	}
    }

  if (w0 == 1 || op0 != 3)
    {
      if (w0)
	{
	  if (P)
	    return DREG_MASK (dst);
	  else
	    return DREGL_MASK (dst);
	}
    }

  return result;
}

static int
decode_dsp32mult_0 (int iw0, int iw1)
{
  /* dsp32mult
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 1 | 0 | 0 |.M.| 0 | 1 |.mmod..........|.MM|.P.|.w1|.op1...|
     |.h01|.h11|.w0|.op0...|.h00|.h10|.dst.......|.src0......|.src1..|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int w1   = ((iw0 >> (DSP32Mac_w1_bits - 16)) & DSP32Mac_w1_mask);
  int P    = ((iw0 >> (DSP32Mac_p_bits - 16)) & DSP32Mac_p_mask);
  int mmod = ((iw0 >> (DSP32Mac_mmod_bits - 16)) & DSP32Mac_mmod_mask);
  int w0   = ((iw1 >> DSP32Mac_w0_bits) & DSP32Mac_w0_mask);
  int dst  = ((iw1 >> DSP32Mac_dst_bits) & DSP32Mac_dst_mask);
  int result = 0;

  if (w1 == 0 && w0 == 0)
    return 0;

  if (((1 << mmod) & (P ? 0x313 : 0x1b57)) == 0)
    return 0;

  if (w1)
    {
      if (P)
	return DREG_MASK (dst | 1);
      else
	return DREGH_MASK (dst);
    }

  if (w0)
    {
      if (P)
	return DREG_MASK (dst);
      else
	return DREGL_MASK (dst);
    }

  return result;
}

static int
decode_dsp32alu_0 (int iw0, int iw1)
{
  /* dsp32alu
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 1 | 0 | 0 |.M.| 1 | 0 | - | - | - |.HL|.aopcde............|
     |.aop...|.s.|.x.|.dst0......|.dst1......|.src0......|.src1......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int s    = ((iw1 >> DSP32Alu_s_bits) & DSP32Alu_s_mask);
  int x    = ((iw1 >> DSP32Alu_x_bits) & DSP32Alu_x_mask);
  int aop  = ((iw1 >> DSP32Alu_aop_bits) & DSP32Alu_aop_mask);
  int dst0 = ((iw1 >> DSP32Alu_dst0_bits) & DSP32Alu_dst0_mask);
  int dst1 = ((iw1 >> DSP32Alu_dst1_bits) & DSP32Alu_dst1_mask);
  int HL   = ((iw0 >> (DSP32Alu_HL_bits - 16)) & DSP32Alu_HL_mask);
  int aopcde = ((iw0 >> (DSP32Alu_aopcde_bits - 16)) & DSP32Alu_aopcde_mask);

  if (aop == 0 && aopcde == 9 && s == 0)
    return 0;
  else if (aop == 2 && aopcde == 9 && HL == 0 && s == 0)
    return 0;
  else if (aop >= x * 2 && aopcde == 5)
    return HL ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (HL == 0 && aopcde == 2)
    return DREGL_MASK (dst0);
  else if (HL == 1 && aopcde == 2)
    return DREGH_MASK (dst0);
  else if (HL == 0 && aopcde == 3)
    return DREGL_MASK (dst0);
  else if (HL == 1 && aopcde == 3)
    return DREGH_MASK (dst0);

  else if (aop == 0 && aopcde == 9 && s == 1)
    return 0;
  else if (aop == 1 && aopcde == 9 && s == 0)
    return 0;
  else if (aop == 2 && aopcde == 9 && s == 1)
    return 0;
  else if (aop == 3 && aopcde == 9 && s == 0)
    return 0;
  else if (aopcde == 8)
    return 0;
  else if (aop == 0 && aopcde == 11)
    return DREG_MASK (dst0);
  else if (aop == 1 && aopcde == 11)
    return HL ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (aopcde == 11)
    return 0;
  else if (aopcde == 22)
    return DREG_MASK (dst0);

  else if ((aop == 0 || aop == 1) && aopcde == 14)
    return 0;
  else if (aop == 3 && HL == 0 && aopcde == 14)
    return 0;

  else if (aop == 3 && HL == 0 && aopcde == 15)
    return DREG_MASK (dst0);

  else if (aop == 1 && aopcde == 16)
    return 0;

  else if (aop == 0 && aopcde == 16)
    return 0;

  else if (aop == 3 && HL == 0 && aopcde == 16)
    return 0;

  else if (aop == 3 && HL == 0 && aopcde == 7)
    return DREG_MASK (dst0);
  else if ((aop == 0 || aop == 1 || aop == 2) && aopcde == 7)
    return DREG_MASK (dst0);

  else if (aop == 0 && aopcde == 12)
    return DREG_MASK (dst0);
  else if (aop == 1 && aopcde == 12)
    return DREG_MASK (dst0) | DREG_MASK (dst1);
  else if (aop == 3 && aopcde == 12)
    return HL ? DREGH_MASK (dst0) : DREGL_MASK (dst0);

  else if (aopcde == 0)
    return DREG_MASK (dst0);
  else if (aopcde == 1)
    return DREG_MASK (dst0) | DREG_MASK (dst1);

  else if (aop == 0 && aopcde == 10)
    return DREGL_MASK (dst0);
  else if (aop == 1 && aopcde == 10)
    return DREGL_MASK (dst0);

  else if ((aop == 1 || aop == 0) && aopcde == 4)
    return DREG_MASK (dst0);
  else if (aop == 2 && aopcde == 4)
    return DREG_MASK (dst0) | DREG_MASK (dst1);

  else if (aop == 0 && aopcde == 17)
    return DREG_MASK (dst0) | DREG_MASK (dst1);
  else if (aop == 1 && aopcde == 17)
    return DREG_MASK (dst0) | DREG_MASK (dst1);
  else if (aop == 0 && aopcde == 18)
    return 0;
  else if (aop == 3 && aopcde == 18)
    return 0;

  else if ((aop == 0 || aop == 1 || aop == 2) && aopcde == 6)
    return DREG_MASK (dst0);

  else if ((aop == 0 || aop == 1) && aopcde == 20)
    return DREG_MASK (dst0);

  else if ((aop == 0 || aop == 1) && aopcde == 21)
    return DREG_MASK (dst0) | DREG_MASK (dst1);

  else if (aop == 0 && aopcde == 23 && HL == 1)
    return DREG_MASK (dst0);
  else if (aop == 0 && aopcde == 23 && HL == 0)
    return DREG_MASK (dst0);

  else if (aop == 0 && aopcde == 24)
    return DREG_MASK (dst0);
  else if (aop == 1 && aopcde == 24)
    return DREG_MASK (dst0) | DREG_MASK (dst1);
  else if (aopcde == 13)
    return DREG_MASK (dst0) | DREG_MASK (dst1);
  else
    return 0;

  return 4;
}

static int
decode_dsp32shift_0 (int iw0, int iw1)
{
  /* dsp32shift
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 1 | 0 | 0 |.M.| 1 | 1 | 0 | 0 | - | - |.sopcde............|
     |.sop...|.HLs...|.dst0......| - | - | - |.src0......|.src1......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int HLs  = ((iw1 >> DSP32Shift_HLs_bits) & DSP32Shift_HLs_mask);
  int sop  = ((iw1 >> DSP32Shift_sop_bits) & DSP32Shift_sop_mask);
  int src0 = ((iw1 >> DSP32Shift_src0_bits) & DSP32Shift_src0_mask);
  int src1 = ((iw1 >> DSP32Shift_src1_bits) & DSP32Shift_src1_mask);
  int dst0 = ((iw1 >> DSP32Shift_dst0_bits) & DSP32Shift_dst0_mask);
  int sopcde = ((iw0 >> (DSP32Shift_sopcde_bits - 16)) & DSP32Shift_sopcde_mask);

  if (sop == 0 && sopcde == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 3)
    return 0;
  else if (sop == 1 && sopcde == 3)
    return 0;
  else if (sop == 2 && sopcde == 3)
    return 0;
  else if (sop == 3 && sopcde == 3)
    return DREG_MASK (dst0);
  else if (sop == 0 && sopcde == 1)
    return DREG_MASK (dst0);
  else if (sop == 1 && sopcde == 1)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 1)
    return DREG_MASK (dst0);
  else if (sopcde == 2)
    return DREG_MASK (dst0);
  else if (sopcde == 4)
    return DREG_MASK (dst0);
  else if (sop == 0 && sopcde == 5)
    return DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 5)
    return DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 5)
    return DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 6)
    return DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 6)
    return DREGL_MASK (dst0);
  else if (sop == 3 && sopcde == 6)
    return DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 7)
    return DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 7)
    return DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 7)
    return DREGL_MASK (dst0);
  else if (sop == 3 && sopcde == 7)
    return DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 8)
    return DREG_MASK (src0) | DREG_MASK (src1);
#if 0
    {
      OUTS (outf, "BITMUX (");
      OUTS (outf, dregs (src0));
      OUTS (outf, ", ");
      OUTS (outf, dregs (src1));
      OUTS (outf, ", A0) (ASR)");
    }
#endif
  else if (sop == 1 && sopcde == 8)
    return DREG_MASK (src0) | DREG_MASK (src1);
#if 0
    {
      OUTS (outf, "BITMUX (");
      OUTS (outf, dregs (src0));
      OUTS (outf, ", ");
      OUTS (outf, dregs (src1));
      OUTS (outf, ", A0) (ASL)");
    }
#endif
  else if (sopcde == 9)
    return sop < 2 ? DREGL_MASK (dst0) : DREG_MASK (dst0);
  else if (sopcde == 10)
    return DREG_MASK (dst0);
  else if (sop == 0 && sopcde == 11)
    return DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 11)
    return DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 12)
    return 0;
  else if (sop == 1 && sopcde == 12)
    return DREGL_MASK (dst0);
  else if (sop == 0 && sopcde == 13)
    return DREG_MASK (dst0);
  else if (sop == 1 && sopcde == 13)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 13)
    return DREG_MASK (dst0);

  abort ();
}

static int
decode_dsp32shiftimm_0 (int iw0, int iw1)
{
  /* dsp32shiftimm
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+
     | 1 | 1 | 0 | 0 |.M.| 1 | 1 | 0 | 1 | - | - |.sopcde............|
     |.sop...|.HLs...|.dst0......|.immag.................|.src1......|
     +---+---+---+---|---+---+---+---|---+---+---+---|---+---+---+---+  */
  int sop      = ((iw1 >> DSP32ShiftImm_sop_bits) & DSP32ShiftImm_sop_mask);
  int bit8     = ((iw1 >> 8) & 0x1);
  int dst0     = ((iw1 >> DSP32ShiftImm_dst0_bits) & DSP32ShiftImm_dst0_mask);
  int sopcde   = ((iw0 >> (DSP32ShiftImm_sopcde_bits - 16)) & DSP32ShiftImm_sopcde_mask);
  int HLs      = ((iw1 >> DSP32ShiftImm_HLs_bits) & DSP32ShiftImm_HLs_mask);


  if (sop == 0 && sopcde == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 0 && bit8 == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 1 && sopcde == 0 && bit8 == 1)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 0 && bit8 == 0)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 0 && bit8 == 1)
    return HLs & 2 ? DREGH_MASK (dst0) : DREGL_MASK (dst0);
  else if (sop == 2 && sopcde == 3 && HLs == 1)
    return 0;
  else if (sop == 0 && sopcde == 3 && HLs == 0 && bit8 == 0)
    return 0;
  else if (sop == 0 && sopcde == 3 && HLs == 0 && bit8 == 1)
    return 0;
  else if (sop == 0 && sopcde == 3 && HLs == 1 && bit8 == 0)
    return 0;
  else if (sop == 0 && sopcde == 3 && HLs == 1 && bit8 == 1)
    return 0;
  else if (sop == 1 && sopcde == 3 && HLs == 0)
    return 0;
  else if (sop == 1 && sopcde == 3 && HLs == 1)
    return 0;
  else if (sop == 2 && sopcde == 3 && HLs == 0)
    return 0;
  else if (sop == 1 && sopcde == 1 && bit8 == 0)
    return DREG_MASK (dst0);
  else if (sop == 1 && sopcde == 1 && bit8 == 1)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 1 && bit8 == 1)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 1 && bit8 == 0)
    return DREG_MASK (dst0);
  else if (sop == 0 && sopcde == 1)
    return DREG_MASK (dst0);
  else if (sop == 1 && sopcde == 2)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 2 && bit8 == 1)
    return DREG_MASK (dst0);
  else if (sop == 2 && sopcde == 2 && bit8 == 0)
    return DREG_MASK (dst0);
  else if (sop == 3 && sopcde == 2)
    return DREG_MASK (dst0);
  else if (sop == 0 && sopcde == 2)
    return DREG_MASK (dst0);

  abort ();
}

int
insn_regmask (int iw0, int iw1)
{
  if ((iw0 & 0xf7ff) == 0xc003 && iw1 == 0x1800)
    return 0; /* MNOP */
  else if ((iw0 & 0xff00) == 0x0000)
    return decode_ProgCtrl_0 (iw0);
  else if ((iw0 & 0xffc0) == 0x0240)
    abort ();
  else if ((iw0 & 0xff80) == 0x0100)
    abort ();
  else if ((iw0 & 0xfe00) == 0x0400)
    abort ();
  else if ((iw0 & 0xfe00) == 0x0600)
    abort ();
  else if ((iw0 & 0xf800) == 0x0800)
    abort ();
  else if ((iw0 & 0xffe0) == 0x0200)
    abort ();
  else if ((iw0 & 0xff00) == 0x0300)
    abort ();
  else if ((iw0 & 0xf000) == 0x1000)
    abort ();
  else if ((iw0 & 0xf000) == 0x2000)
    abort ();
  else if ((iw0 & 0xf000) == 0x3000)
    abort ();
  else if ((iw0 & 0xfc00) == 0x4000)
    abort ();
  else if ((iw0 & 0xfe00) == 0x4400)
    abort ();
  else if ((iw0 & 0xf800) == 0x4800)
    abort ();
  else if ((iw0 & 0xf000) == 0x5000)
    abort ();
  else if ((iw0 & 0xf800) == 0x6000)
    abort ();
  else if ((iw0 & 0xf800) == 0x6800)
    abort ();
  else if ((iw0 & 0xf000) == 0x8000)
    return decode_LDSTpmod_0 (iw0);
  else if ((iw0 & 0xff60) == 0x9e60)
    return decode_dagMODim_0 (iw0);
  else if ((iw0 & 0xfff0) == 0x9f60)
    return decode_dagMODik_0 (iw0);
  else if ((iw0 & 0xfc00) == 0x9c00)
    return decode_dspLDST_0 (iw0);
  else if ((iw0 & 0xf000) == 0x9000)
    return decode_LDST_0 (iw0);
  else if ((iw0 & 0xfc00) == 0xb800)
    return decode_LDSTiiFP_0 (iw0);
  else if ((iw0 & 0xe000) == 0xA000)
    return decode_LDSTii_0 (iw0);
  else if ((iw0 & 0xff80) == 0xe080 && (iw1 & 0x0C00) == 0x0000)
    abort ();
  else if ((iw0 & 0xff00) == 0xe100 && (iw1 & 0x0000) == 0x0000)
    abort ();
  else if ((iw0 & 0xfe00) == 0xe200 && (iw1 & 0x0000) == 0x0000)
    abort ();
  else if ((iw0 & 0xfc00) == 0xe400 && (iw1 & 0x0000) == 0x0000)
    abort ();
  else if ((iw0 & 0xfffe) == 0xe800 && (iw1 & 0x0000) == 0x0000)
    abort ();
  else if ((iw0 & 0xf600) == 0xc000 && (iw1 & 0x0000) == 0x0000)
    return decode_dsp32mac_0 (iw0, iw1);
  else if ((iw0 & 0xf600) == 0xc200 && (iw1 & 0x0000) == 0x0000)
    return decode_dsp32mult_0 (iw0, iw1);
  else if ((iw0 & 0xf7c0) == 0xc400 && (iw1 & 0x0000) == 0x0000)
    return decode_dsp32alu_0 (iw0, iw1);
  else if ((iw0 & 0xf780) == 0xc600 && (iw1 & 0x01c0) == 0x0000)
    return decode_dsp32shift_0 (iw0, iw1);
  else if ((iw0 & 0xf780) == 0xc680 && (iw1 & 0x0000) == 0x0000)
    return decode_dsp32shiftimm_0 (iw0, iw1);
  else if ((iw0 & 0xff00) == 0xf800)
    abort ();
  else if ((iw0 & 0xFFC0) == 0xf000 && (iw1 & 0x0000) == 0x0000)
    abort ();

  abort ();
}