1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702
|
{
Copyright (c) 2003 by Florian Klaempfl
Contains the assembler object for the ARM
This program 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit aasmcpu;
{$i fpcdefs.inc}
interface
uses
globtype,globals,verbose,
aasmbase,aasmtai,aasmdata,aasmsym,
ogbase,
symtype,
cpubase,cpuinfo,cgbase,cgutils,
sysutils;
const
{ "mov reg,reg" source operand number }
O_MOV_SOURCE = 1;
{ "mov reg,reg" source operand number }
O_MOV_DEST = 0;
{ Operand types }
OT_NONE = $00000000;
OT_BITS8 = $00000001; { size, and other attributes, of the operand }
OT_BITS16 = $00000002;
OT_BITS32 = $00000004;
OT_BITS64 = $00000008; { FPU only }
OT_BITS80 = $00000010;
OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
OT_NEAR = $00000040;
OT_SHORT = $00000080;
OT_BITSTINY = $00000100; { fpu constant }
OT_BITSSHIFTER =
$00000200;
OT_SIZE_MASK = $000003FF; { all the size attributes }
OT_NON_SIZE = $0FFFF800;
OT_OPT_SIZE = $F0000000;
OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
OT_TO = $00000200; { operand is followed by a colon }
{ reverse effect in FADD, FSUB &c }
OT_COLON = $00000400;
OT_SHIFTEROP = $00000800;
OT_REGISTER = $00001000;
OT_IMMEDIATE = $00002000;
OT_REGLIST = $00008000;
OT_IMM8 = $00002001;
OT_IMM24 = $00002002;
OT_IMM32 = $00002004;
OT_IMM64 = $00002008;
OT_IMM80 = $00002010;
OT_IMMTINY = $00002100;
OT_IMMSHIFTER= $00002200;
OT_IMMEDIATEZERO = $10002200;
OT_IMMEDIATEMM = $00002400;
OT_IMMEDIATE24 = OT_IMM24;
OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
OT_IMMEDIATEFPU = OT_IMMTINY;
OT_REGMEM = $00200000; { for r/m, ie EA, operands }
OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
OT_REG8 = $00201001;
OT_REG16 = $00201002;
OT_REG32 = $00201004;
OT_REGLO = $10201004; { lower reg (r0-r7) }
OT_REGSP = $20201004;
OT_REG64 = $00201008;
OT_VREG = $00201010; { vector register }
OT_REGF = $00201020; { coproc register }
OT_REGS = $00201040; { special register with mask }
OT_MEMORY = $00204000; { register number in 'basereg' }
OT_MEM8 = $00204001;
OT_MEM16 = $00204002;
OT_MEM32 = $00204004;
OT_MEM64 = $00204008;
OT_MEM80 = $00204010;
{ word/byte load/store }
OT_AM2 = $00010000;
{ misc ld/st operations, thumb reg indexed }
OT_AM3 = $00020000;
{ multiple ld/st operations or thumb imm indexed }
OT_AM4 = $00040000;
{ co proc. ld/st operations or thumb sp+imm indexed }
OT_AM5 = $00080000;
{ exclusive ld/st operations or thumb pc+imm indexed }
OT_AM6 = $00100000;
OT_AMMASK = $001f0000;
{ IT instruction }
OT_CONDITION = $00200000;
OT_MODEFLAGS = $00400000;
OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
OT_MEMORYAM6 = OT_MEMORY or OT_AM6;
OT_FPUREG = $01000000; { floating point stack registers }
OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
{ a mask for the following }
OT_MEM_OFFS = $00604000; { special type of EA }
{ simple [address] offset }
OT_ONENESS = $00800000; { special type of immediate operand }
{ so UNITY == IMMEDIATE | ONENESS }
OT_UNITY = $00802000; { for shift/rotate instructions }
instabentries = {$i armnop.inc}
maxinfolen = 5;
IF_NONE = $00000000;
IF_ARMMASK = $000F0000;
IF_ARM32 = $00010000;
IF_THUMB = $00020000;
IF_THUMB32 = $00040000;
IF_WIDE = $00080000;
IF_ARMvMASK = $0FF00000;
IF_ARMv4 = $00100000;
IF_ARMv4T = $00200000;
IF_ARMv5 = $00300000;
IF_ARMv5T = $00400000;
IF_ARMv5TE = $00500000;
IF_ARMv5TEJ = $00600000;
IF_ARMv6 = $00700000;
IF_ARMv6K = $00800000;
IF_ARMv6T2 = $00900000;
IF_ARMv6Z = $00A00000;
IF_ARMv6M = $00B00000;
IF_ARMv7 = $00C00000;
IF_ARMv7A = $00D00000;
IF_ARMv7R = $00E00000;
IF_ARMv7M = $00F00000;
IF_ARMv7EM = $01000000;
IF_FPMASK = $F0000000;
IF_FPA = $10000000;
IF_VFPv2 = $20000000;
IF_VFPv3 = $40000000;
IF_VFPv4 = $80000000;
{ if the instruction can change in a second pass }
IF_PASS2 = longint($80000000);
type
TInsTabCache=array[TasmOp] of longint;
PInsTabCache=^TInsTabCache;
tinsentry = record
opcode : tasmop;
ops : byte;
optypes : array[0..5] of longint;
code : array[0..maxinfolen] of char;
flags : longword;
end;
pinsentry=^tinsentry;
const
InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
var
InsTabCache : PInsTabCache;
type
taicpu = class(tai_cpu_abstract_sym)
oppostfix : TOpPostfix;
wideformat : boolean;
roundingmode : troundingmode;
procedure loadshifterop(opidx:longint;const so:tshifterop);
procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
procedure loadconditioncode(opidx:longint;const cond:tasmcond);
procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
procedure loadrealconst(opidx:longint;const _value:bestreal);
constructor op_none(op : tasmop);
constructor op_reg(op : tasmop;_op1 : tregister);
constructor op_ref(op : tasmop;const _op1 : treference);
constructor op_const(op : tasmop;_op1 : longint);
constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
constructor op_reg_reg_const_const(op : tasmop;_op1,_op2 : tregister; _op3,_op4: aint);
constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
{ SFM/LFM }
constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
{ ITxxx }
constructor op_cond(op: tasmop; cond: tasmcond);
{ CPSxx }
constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
{ MSR }
constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
{ *M*LL }
constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
constructor op_reg_realconst(op : tasmop;_op1: tregister;_op2: bestreal);
{ this is for Jmp instructions }
constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
constructor op_sym(op : tasmop;_op1 : tasmsymbol);
constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
function is_same_reg_move(regtype: Tregistertype):boolean; override;
function spilling_get_operation_type(opnr: longint): topertype;override;
function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
{ assembler }
public
{ the next will reset all instructions that can change in pass 2 }
procedure ResetPass1;override;
procedure ResetPass2;override;
function CheckIfValid:boolean;
function GetString:string;
function Pass1(objdata:TObjData):longint;override;
procedure Pass2(objdata:TObjData);override;
protected
procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
procedure ppubuildderefimploper(var o:toper);override;
procedure ppuderefoper(var o:toper);override;
private
{ pass1 info }
inIT,
lastinIT: boolean;
{ arm version info }
fArmVMask,
fArmMask : longint;
{ next fields are filled in pass1, so pass2 is faster }
inssize : shortint;
insoffset : longint;
LastInsOffset : longint; { need to be public to be reset }
insentry : PInsEntry;
procedure BuildArmMasks(objdata:TObjData);
function InsEnd:longint;
procedure create_ot(objdata:TObjData);
function Matches(p:PInsEntry):longint;
function calcsize(p:PInsEntry):shortint;
procedure gencode(objdata:TObjData);
function NeedAddrPrefix(opidx:byte):boolean;
procedure Swapoperands;
function FindInsentry(objdata:TObjData):boolean;
end;
tai_align = class(tai_align_abstract)
{ nothing to add }
end;
function spilling_create_load(const ref:treference;r:tregister):Taicpu;
function spilling_create_store(r:tregister; const ref:treference):Taicpu;
function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
function setcondition(i : taicpu;c : tasmcond) : taicpu;
{ inserts pc relative symbols at places where they are reachable
and transforms special instructions to valid instruction encodings }
procedure finalizearmcode(list,listtoinsert : TAsmList);
{ inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
procedure InsertPData;
procedure InitAsm;
procedure DoneAsm;
implementation
uses
itcpugas,aoptcpu,
systems,symdef;
procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_shifterop then
begin
clearop(opidx);
new(shifterop);
end;
shifterop^:=so;
typ:=top_shifterop;
if assigned(add_reg_instruction_hook) then
add_reg_instruction_hook(self,shifterop^.rs);
end;
end;
procedure taicpu.loadrealconst(opidx:longint;const _value:bestreal);
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_realconst then
clearop(opidx);
val_real:=_value;
typ:=top_realconst;
end;
end;
procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
var
i : byte;
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_regset then
begin
clearop(opidx);
new(regset);
end;
regset^:=s;
regtyp:=regsetregtype;
subreg:=regsetsubregtype;
usermode:=ausermode;
typ:=top_regset;
case regsetregtype of
R_INTREGISTER:
for i:=RS_R0 to RS_R15 do
begin
if assigned(add_reg_instruction_hook) and (i in regset^) then
add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
end;
R_MMREGISTER:
{ both RS_S0 and RS_D0 range from 0 to 31 }
for i:=RS_D0 to RS_D31 do
begin
if assigned(add_reg_instruction_hook) and (i in regset^) then
add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
end;
end;
end;
end;
procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_conditioncode then
clearop(opidx);
cc:=cond;
typ:=top_conditioncode;
end;
end;
procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_modeflags then
clearop(opidx);
modeflags:=flags;
typ:=top_modeflags;
end;
end;
procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
begin
allocate_oper(opidx+1);
with oper[opidx]^ do
begin
if typ<>top_specialreg then
clearop(opidx);
specialreg:=areg;
specialflags:=aflags;
typ:=top_specialreg;
end;
end;
{*****************************************************************************
taicpu Constructors
*****************************************************************************}
constructor taicpu.op_none(op : tasmop);
begin
inherited create(op);
end;
{ for pld }
constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
begin
inherited create(op);
ops:=1;
loadref(0,_op1);
end;
constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
begin
inherited create(op);
ops:=1;
loadreg(0,_op1);
end;
constructor taicpu.op_const(op : tasmop;_op1 : longint);
begin
inherited create(op);
ops:=1;
loadconst(0,aint(_op1));
end;
constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
begin
inherited create(op);
ops:=2;
loadreg(0,_op1);
loadreg(1,_op2);
end;
constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
begin
inherited create(op);
ops:=2;
loadreg(0,_op1);
loadconst(1,aint(_op2));
end;
constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
begin
inherited create(op);
ops:=1;
loadregset(0,regtype,subreg,_op1);
end;
constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
begin
inherited create(op);
ops:=2;
loadref(0,_op1);
loadregset(1,regtype,subreg,_op2);
end;
constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
begin
inherited create(op);
ops:=2;
loadreg(0,_op1);
loadref(1,_op2);
end;
constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadreg(2,_op3);
end;
constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
begin
inherited create(op);
ops:=4;
loadreg(0,_op1);
loadreg(1,_op2);
loadreg(2,_op3);
loadreg(3,_op4);
end;
constructor taicpu.op_reg_realconst(op : tasmop; _op1 : tregister; _op2 : bestreal);
begin
inherited create(op);
ops:=2;
loadreg(0,_op1);
loadrealconst(1,_op2);
end;
constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadconst(2,aint(_op3));
end;
constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadconst(1,aint(_op2));
loadconst(2,aint(_op3));
end;
constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
begin
inherited create(op);
ops:=4;
loadreg(0,_op1);
loadreg(1,_op2);
loadconst(2,aint(_op3));
loadconst(3,aint(_op4));
end;
constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadconst(1,_op2);
loadref(2,_op3);
end;
constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
begin
inherited create(op);
ops:=1;
loadconditioncode(0, cond);
end;
constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
begin
inherited create(op);
ops := 1;
loadmodeflags(0,flags);
end;
constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
begin
inherited create(op);
ops := 2;
loadmodeflags(0,flags);
loadconst(1,a);
end;
constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
begin
inherited create(op);
ops:=2;
loadspecialreg(0,specialreg,specialregflags);
loadreg(1,_op2);
end;
constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadsymbol(0,_op3,_op3ofs);
end;
constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadref(2,_op3);
end;
constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
begin
inherited create(op);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadshifterop(2,_op3);
end;
constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
begin
inherited create(op);
ops:=4;
loadreg(0,_op1);
loadreg(1,_op2);
loadreg(2,_op3);
loadshifterop(3,_op4);
end;
constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
begin
inherited create(op);
condition:=cond;
ops:=1;
loadsymbol(0,_op1,0);
end;
constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
begin
inherited create(op);
ops:=1;
loadsymbol(0,_op1,0);
end;
constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
begin
inherited create(op);
ops:=1;
loadsymbol(0,_op1,_op1ofs);
end;
constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
begin
inherited create(op);
ops:=2;
loadreg(0,_op1);
loadsymbol(1,_op2,_op2ofs);
end;
constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
begin
inherited create(op);
ops:=2;
loadsymbol(0,_op1,_op1ofs);
loadref(1,_op2);
end;
function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
begin
{ allow the register allocator to remove unnecessary moves }
result:=(
((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER)) or
((opcode in [A_VMOV]) and (regtype = R_MMREGISTER) and (oppostfix in [PF_F32,PF_F64]))
) and
((oppostfix in [PF_None,PF_D]) or (opcode = A_VMOV)) and
(condition=C_None) and
(ops=2) and
(oper[0]^.typ=top_reg) and
(oper[1]^.typ=top_reg) and
(oper[0]^.reg=oper[1]^.reg);
end;
function spilling_create_load(const ref:treference;r:tregister):Taicpu;
begin
case getregtype(r) of
R_INTREGISTER :
result:=taicpu.op_reg_ref(A_LDR,r,ref);
R_FPUREGISTER :
{ use lfm because we don't know the current internal format
and avoid exceptions
}
result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
R_MMREGISTER :
result:=taicpu.op_reg_ref(A_VLDR,r,ref);
else
internalerror(200401041);
end;
end;
function spilling_create_store(r:tregister; const ref:treference):Taicpu;
begin
case getregtype(r) of
R_INTREGISTER :
result:=taicpu.op_reg_ref(A_STR,r,ref);
R_FPUREGISTER :
{ use sfm because we don't know the current internal format
and avoid exceptions
}
result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
R_MMREGISTER :
result:=taicpu.op_reg_ref(A_VSTR,r,ref);
else
internalerror(200401041);
end;
end;
function taicpu.spilling_get_operation_type(opnr: longint): topertype;
begin
if GenerateThumbCode then
case opcode of
A_ADC,A_ADD,A_AND,A_BIC,
A_EOR,A_CLZ,A_RBIT,
A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
A_LDRSH,A_LDRT,
A_MOV,A_MVN,A_MLA,A_MUL,
A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
A_SWP,A_SWPB,
A_LDF,A_FLT,A_FIX,
A_ADF,A_DVF,A_FDV,A_FML,
A_RFS,A_RFC,A_RDF,
A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
A_LFM,
A_FLDS,A_FLDD,
A_FMRX,A_FMXR,A_FMSTAT,
A_FMSR,A_FMRS,A_FMDRR,
A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
A_FNEGS,A_FNEGD,
A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
A_SXTB16,A_UXTB16,
A_UXTB,A_UXTH,A_SXTB,A_SXTH,
A_NEG,
A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB,
A_MRS,A_MSR:
if opnr=0 then
result:=operand_readwrite
else
result:=operand_read;
A_BKPT,A_B,A_BL,A_BLX,A_BX,
A_CMN,A_CMP,A_TEQ,A_TST,
A_CMF,A_CMFE,A_WFS,A_CNF,
A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
A_FCMPZS,A_FCMPZD,
A_VCMP,A_VCMPE:
result:=operand_read;
A_SMLAL,A_UMLAL:
if opnr in [0,1] then
result:=operand_readwrite
else
result:=operand_read;
A_SMULL,A_UMULL,
A_FMRRD:
if opnr in [0,1] then
result:=operand_readwrite
else
result:=operand_read;
A_STR,A_STRB,A_STRBT,
A_STRH,A_STRT,A_STF,A_SFM,
A_FSTS,A_FSTD,
A_VSTR:
{ important is what happens with the involved registers }
if opnr=0 then
result := operand_read
else
{ check for pre/post indexed }
result := operand_read;
//Thumb2
A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI,
A_SMMLA,A_SMMLS:
if opnr in [0] then
result:=operand_readwrite
else
result:=operand_read;
A_BFC:
if opnr in [0] then
result:=operand_readwrite
else
result:=operand_read;
A_LDREX:
if opnr in [0] then
result:=operand_readwrite
else
result:=operand_read;
A_STREX:
result:=operand_write;
else
internalerror(200403151);
end
else
case opcode of
A_ADC,A_ADD,A_AND,A_BIC,A_ORN,
A_EOR,A_CLZ,A_RBIT,
A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
A_LDRSH,A_LDRT,
A_MOV,A_MVN,A_MLA,A_MUL,
A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
A_SWP,A_SWPB,
A_LDF,A_FLT,A_FIX,
A_ADF,A_DVF,A_FDV,A_FML,
A_RFS,A_RFC,A_RDF,
A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
A_LFM,
A_FLDS,A_FLDD,
A_FMRX,A_FMXR,A_FMSTAT,
A_FMSR,A_FMRS,A_FMDRR,
A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
A_FNEGS,A_FNEGD,
A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
A_SXTB16,A_UXTB16,
A_UXTB,A_UXTH,A_SXTB,A_SXTH,
A_NEG,
A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB,
A_MRS,A_MSR:
if opnr=0 then
result:=operand_write
else
result:=operand_read;
A_BKPT,A_B,A_BL,A_BLX,A_BX,
A_CMN,A_CMP,A_TEQ,A_TST,
A_CMF,A_CMFE,A_WFS,A_CNF,
A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
A_FCMPZS,A_FCMPZD,
A_VCMP,A_VCMPE:
result:=operand_read;
A_SMLAL,A_UMLAL:
if opnr in [0,1] then
result:=operand_readwrite
else
result:=operand_read;
A_SMULL,A_UMULL,
A_FMRRD:
if opnr in [0,1] then
result:=operand_write
else
result:=operand_read;
A_STR,A_STRB,A_STRBT,
A_STRH,A_STRT,A_STF,A_SFM,
A_FSTS,A_FSTD,
A_VSTR:
{ important is what happens with the involved registers }
if opnr=0 then
result := operand_read
else
{ check for pre/post indexed }
result := operand_read;
//Thumb2
A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI,
A_SMMLA,A_SMMLS:
if opnr in [0] then
result:=operand_write
else
result:=operand_read;
A_VFMA,A_VFMS,A_VFNMA,A_VFNMS,
A_BFC:
if opnr in [0] then
result:=operand_readwrite
else
result:=operand_read;
A_LDREX:
if opnr in [0] then
result:=operand_write
else
result:=operand_read;
A_STREX:
result:=operand_write;
else
internalerror(200403151);
end;
end;
function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
begin
result := operand_read;
if (oper[opnr]^.ref^.base = reg) and
(oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
result := operand_readwrite;
end;
procedure BuildInsTabCache;
var
i : longint;
begin
new(instabcache);
FillChar(instabcache^,sizeof(tinstabcache),$ff);
i:=0;
while (i<InsTabEntries) do
begin
if InsTabCache^[InsTab[i].Opcode]=-1 then
InsTabCache^[InsTab[i].Opcode]:=i;
inc(i);
end;
end;
procedure InitAsm;
begin
if not assigned(instabcache) then
BuildInsTabCache;
end;
procedure DoneAsm;
begin
if assigned(instabcache) then
begin
dispose(instabcache);
instabcache:=nil;
end;
end;
function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
begin
i.oppostfix:=pf;
result:=i;
end;
function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
begin
i.roundingmode:=rm;
result:=i;
end;
function setcondition(i : taicpu;c : tasmcond) : taicpu;
begin
i.condition:=c;
result:=i;
end;
Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
Begin
Current:=tai(Current.Next);
While Assigned(Current) And (Current.typ In SkipInstr) Do
Current:=tai(Current.Next);
Next:=Current;
If Assigned(Next) And Not(Next.typ In SkipInstr) Then
Result:=True
Else
Begin
Next:=Nil;
Result:=False;
End;
End;
(*
function armconstequal(hp1,hp2: tai): boolean;
begin
result:=false;
if hp1.typ<>hp2.typ then
exit;
case hp1.typ of
tai_const:
result:=
(tai_const(hp2).sym=tai_const(hp).sym) and
(tai_const(hp2).value=tai_const(hp).value) and
(tai(hp2.previous).typ=ait_label);
tai_const:
result:=
(tai_const(hp2).sym=tai_const(hp).sym) and
(tai_const(hp2).value=tai_const(hp).value) and
(tai(hp2.previous).typ=ait_label);
end;
end;
*)
procedure insertpcrelativedata(list,listtoinsert : TAsmList);
var
limit: longint;
{ FLD/FST VFP instructions have a limit of +/- 1024, not 4096, this
function checks the next count instructions if the limit must be
decreased }
procedure CheckLimit(hp : tai;count : integer);
var
i : Integer;
begin
for i:=1 to count do
if SimpleGetNextInstruction(hp,hp) and
(tai(hp).typ=ait_instruction) and
((taicpu(hp).opcode=A_FLDS) or
(taicpu(hp).opcode=A_FLDD) or
(taicpu(hp).opcode=A_VLDR) or
(taicpu(hp).opcode=A_LDF) or
(taicpu(hp).opcode=A_STF)) then
limit:=254;
end;
function is_case_dispatch(hp: taicpu): boolean;
begin
result:=
((taicpu(hp).opcode in [A_ADD,A_LDR]) and
not(GenerateThumbCode or GenerateThumb2Code) and
(taicpu(hp).oper[0]^.typ=top_reg) and
(taicpu(hp).oper[0]^.reg=NR_PC)) or
((taicpu(hp).opcode=A_MOV) and (GenerateThumbCode) and
(taicpu(hp).oper[0]^.typ=top_reg) and
(taicpu(hp).oper[0]^.reg=NR_PC)) or
(taicpu(hp).opcode=A_TBH) or
(taicpu(hp).opcode=A_TBB);
end;
var
curinspos,
penalty,
lastinspos,
{ increased for every data element > 4 bytes inserted }
extradataoffset,
curop : longint;
curtai,
inserttai : tai;
curdatatai,hp,hp2 : tai;
curdata : TAsmList;
l : tasmlabel;
doinsert,
removeref : boolean;
multiplier : byte;
begin
curdata:=TAsmList.create;
lastinspos:=-1;
curinspos:=0;
extradataoffset:=0;
if GenerateThumbCode then
begin
multiplier:=2;
limit:=504;
end
else
begin
limit:=1016;
multiplier:=1;
end;
curtai:=tai(list.first);
doinsert:=false;
while assigned(curtai) do
begin
{ instruction? }
case curtai.typ of
ait_instruction:
begin
{ walk through all operand of the instruction }
for curop:=0 to taicpu(curtai).ops-1 do
begin
{ reference? }
if (taicpu(curtai).oper[curop]^.typ=top_ref) then
begin
{ pc relative symbol? }
curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
if assigned(curdatatai) then
begin
{ create a new copy of a data entry on arm thumb if the entry has been inserted already
before because arm thumb does not allow pc relative negative offsets }
if (GenerateThumbCode) and
tai_label(curdatatai).inserted then
begin
current_asmdata.getjumplabel(l);
hp:=tai_label.create(l);
listtoinsert.Concat(hp);
hp2:=tai(curdatatai.Next.GetCopy);
hp2.Next:=nil;
hp2.Previous:=nil;
listtoinsert.Concat(hp2);
taicpu(curtai).oper[curop]^.ref^.symboldata:=hp;
taicpu(curtai).oper[curop]^.ref^.symbol:=l;
curdatatai:=hp;
end;
{ move only if we're at the first reference of a label }
if not(tai_label(curdatatai).moved) then
begin
tai_label(curdatatai).moved:=true;
{ check if symbol already used. }
{ if yes, reuse the symbol }
hp:=tai(curdatatai.next);
removeref:=false;
if assigned(hp) then
begin
case hp.typ of
ait_const:
begin
if (tai_const(hp).consttype=aitconst_64bit) then
inc(extradataoffset,multiplier);
end;
ait_realconst:
begin
inc(extradataoffset,multiplier*(((tai_realconst(hp).savesize-4)+3) div 4));
end;
end;
{ check if the same constant has been already inserted into the currently handled list,
if yes, reuse it }
if (hp.typ=ait_const) then
begin
hp2:=tai(curdata.first);
while assigned(hp2) do
begin
if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
then
begin
with taicpu(curtai).oper[curop]^.ref^ do
begin
symboldata:=hp2.previous;
symbol:=tai_label(hp2.previous).labsym;
end;
removeref:=true;
break;
end;
hp2:=tai(hp2.next);
end;
end;
end;
{ move or remove symbol reference }
repeat
hp:=tai(curdatatai.next);
listtoinsert.remove(curdatatai);
if removeref then
curdatatai.free
else
curdata.concat(curdatatai);
curdatatai:=hp;
until (curdatatai=nil) or (curdatatai.typ=ait_label);
if lastinspos=-1 then
lastinspos:=curinspos;
end;
end;
end;
end;
inc(curinspos,multiplier);
end;
ait_align:
begin
{ code is always 4 byte aligned, so we don't have to take care of .align 2 which would
requires also incrementing curinspos by 1 }
inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
end;
ait_const:
begin
inc(curinspos,multiplier);
if (tai_const(curtai).consttype=aitconst_64bit) then
inc(curinspos,multiplier);
end;
ait_realconst:
begin
inc(curinspos,multiplier*((tai_realconst(hp).savesize+3) div 4));
end;
end;
{ special case for case jump tables }
penalty:=0;
if SimpleGetNextInstruction(curtai,hp) and
(tai(hp).typ=ait_instruction) then
begin
case taicpu(hp).opcode of
A_MOV,
A_LDR,
A_ADD,
A_TBH,
A_TBB:
{ approximation if we hit a case jump table }
if is_case_dispatch(taicpu(hp)) then
begin
penalty:=multiplier;
hp:=tai(hp.next);
{ skip register allocations and comments inserted by the optimizer as well as a label and align
as jump tables for thumb might have }
while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc,ait_label,ait_align]) do
hp:=tai(hp.next);
while assigned(hp) and (hp.typ=ait_const) do
begin
inc(penalty,multiplier);
hp:=tai(hp.next);
end;
end;
A_IT:
begin
if GenerateThumb2Code then
penalty:=multiplier;
{ check if the next instruction fits as well
or if we splitted after the it so split before }
CheckLimit(hp,1);
end;
A_ITE,
A_ITT:
begin
if GenerateThumb2Code then
penalty:=2*multiplier;
{ check if the next two instructions fit as well
or if we splitted them so split before }
CheckLimit(hp,2);
end;
A_ITEE,
A_ITTE,
A_ITET,
A_ITTT:
begin
if GenerateThumb2Code then
penalty:=3*multiplier;
{ check if the next three instructions fit as well
or if we splitted them so split before }
CheckLimit(hp,3);
end;
A_ITEEE,
A_ITTEE,
A_ITETE,
A_ITTTE,
A_ITEET,
A_ITTET,
A_ITETT,
A_ITTTT:
begin
if GenerateThumb2Code then
penalty:=4*multiplier;
{ check if the next three instructions fit as well
or if we splitted them so split before }
CheckLimit(hp,4);
end;
end;
end;
CheckLimit(curtai,1);
{ don't miss an insert }
doinsert:=doinsert or
(not(curdata.empty) and
(curinspos-lastinspos+penalty+extradataoffset>limit));
{ split only at real instructions else the test below fails }
if doinsert and (curtai.typ=ait_instruction) and
(
{ don't split loads of pc to lr and the following move }
not(
(taicpu(curtai).opcode=A_MOV) and
(taicpu(curtai).oper[0]^.typ=top_reg) and
(taicpu(curtai).oper[0]^.reg=NR_R14) and
(taicpu(curtai).oper[1]^.typ=top_reg) and
(taicpu(curtai).oper[1]^.reg=NR_PC)
)
) and
(
{ do not insert data after a B instruction due to their limited range }
not((GenerateThumbCode) and
(taicpu(curtai).opcode=A_B)
)
) then
begin
lastinspos:=-1;
extradataoffset:=0;
if GenerateThumbCode then
limit:=502
else
limit:=1016;
{ if this is an add/tbh/tbb-based jumptable, go back to the
previous instruction, because inserting data between the
dispatch instruction and the table would mess up the
addresses }
inserttai:=curtai;
if is_case_dispatch(taicpu(inserttai)) and
((taicpu(inserttai).opcode=A_ADD) or
(taicpu(inserttai).opcode=A_TBH) or
(taicpu(inserttai).opcode=A_TBB)) then
begin
repeat
inserttai:=tai(inserttai.previous);
until inserttai.typ=ait_instruction;
{ if it's an add-based jump table, then also skip the
pc-relative load }
if taicpu(curtai).opcode=A_ADD then
repeat
inserttai:=tai(inserttai.previous);
until inserttai.typ=ait_instruction;
end
else
{ on arm thumb, insert the data always after all labels etc. following an instruction so it
is prevent that a bxx yyy; bl xxx; yyyy: sequence gets separated ( we never insert on arm thumb after
bxx) and the distance of bxx gets too long }
if GenerateThumbCode then
while assigned(tai(inserttai.Next)) and (tai(inserttai.Next).typ in SkipInstr+[ait_label]) do
inserttai:=tai(inserttai.next);
doinsert:=false;
current_asmdata.getjumplabel(l);
{ align jump in thumb .text section to 4 bytes }
if not(curdata.empty) and (GenerateThumbCode) then
curdata.Insert(tai_align.Create(4));
curdata.insert(taicpu.op_sym(A_B,l));
curdata.concat(tai_label.create(l));
{ mark all labels as inserted, arm thumb
needs this, so data referencing an already inserted label can be
duplicated because arm thumb does not allow negative pc relative offset }
hp2:=tai(curdata.first);
while assigned(hp2) do
begin
if hp2.typ=ait_label then
tai_label(hp2).inserted:=true;
hp2:=tai(hp2.next);
end;
{ continue with the last inserted label because we use later
on SimpleGetNextInstruction, so if we used curtai.next (which
is then equal curdata.last.previous) we could over see one
instruction }
hp:=tai(curdata.Last);
list.insertlistafter(inserttai,curdata);
curtai:=hp;
end
else
curtai:=tai(curtai.next);
end;
{ align jump in thumb .text section to 4 bytes }
if not(curdata.empty) and (GenerateThumbCode or GenerateThumb2Code) then
curdata.Insert(tai_align.Create(4));
list.concatlist(curdata);
curdata.free;
end;
procedure ensurethumb2encodings(list: TAsmList);
var
curtai: tai;
op2reg: TRegister;
begin
{ Do Thumb-2 16bit -> 32bit transformations }
curtai:=tai(list.first);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
begin
case taicpu(curtai).opcode of
A_ADD:
begin
{ Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
if taicpu(curtai).ops = 3 then
begin
if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
begin
if taicpu(curtai).oper[2]^.typ = top_reg then
op2reg := taicpu(curtai).oper[2]^.reg
else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
else
op2reg := NR_NO;
if op2reg <> NR_NO then
begin
if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
(taicpu(curtai).oper[1]^.reg >= NR_R8) or
(op2reg >= NR_R8) then
begin
taicpu(curtai).wideformat:=true;
{ Handle special cases where register rules are violated by optimizer/user }
{ if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
{ Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
begin
taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
taicpu(curtai).oper[1]^.reg := op2reg;
end;
end;
end;
end;
end;
end;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
procedure ensurethumbencodings(list: TAsmList);
var
curtai: tai;
begin
{ Do Thumb 16bit transformations to form valid instruction forms }
curtai:=tai(list.first);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
begin
case taicpu(curtai).opcode of
A_STM:
begin
if (taicpu(curtai).ops=2) and
(taicpu(curtai).oper[0]^.typ=top_ref) and
(taicpu(curtai).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
(taicpu(curtai).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
(taicpu(curtai).oppostfix in [PF_FD,PF_DB]) then
begin
taicpu(curtai).oppostfix:=PF_None;
taicpu(curtai).loadregset(0, taicpu(curtai).oper[1]^.regtyp, taicpu(curtai).oper[1]^.subreg, taicpu(curtai).oper[1]^.regset^);
taicpu(curtai).ops:=1;
taicpu(curtai).opcode:=A_PUSH;
end;
end;
A_LDM:
begin
if (taicpu(curtai).ops=2) and
(taicpu(curtai).oper[0]^.typ=top_ref) and
(taicpu(curtai).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
(taicpu(curtai).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
(taicpu(curtai).oppostfix in [PF_FD,PF_IA]) then
begin
taicpu(curtai).oppostfix:=PF_None;
taicpu(curtai).loadregset(0, taicpu(curtai).oper[1]^.regtyp, taicpu(curtai).oper[1]^.subreg, taicpu(curtai).oper[1]^.regset^);
taicpu(curtai).ops:=1;
taicpu(curtai).opcode:=A_POP;
end;
end;
A_ADD,
A_AND,A_EOR,A_ORR,A_BIC,
A_LSL,A_LSR,A_ASR,A_ROR,
A_ADC,A_SBC:
begin
if (taicpu(curtai).ops = 3) and
(taicpu(curtai).oper[2]^.typ=top_reg) and
(taicpu(curtai).oper[0]^.reg=taicpu(curtai).oper[1]^.reg) and
(taicpu(curtai).oper[0]^.reg<>NR_STACK_POINTER_REG) then
begin
taicpu(curtai).oper[1]^.reg:=taicpu(curtai).oper[2]^.reg;
taicpu(curtai).ops:=2;
end;
end;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
const
opTable: array[A_IT..A_ITTTT] of string =
('T','TE','TT','TEE','TTE','TET','TTT',
'TEEE','TTEE','TETE','TTTE',
'TEET','TTET','TETT','TTTT');
invertedOpTable: array[A_IT..A_ITTTT] of string =
('E','ET','EE','ETT','EET','ETE','EEE',
'ETTT','EETT','ETET','EEET',
'ETTE','EETE','ETEE','EEEE');
var
resStr : string;
i : TAsmOp;
begin
if InvertLast then
resStr := opTable[FirstOp]+invertedOpTable[LastOp]
else
resStr := opTable[FirstOp]+opTable[LastOp];
if length(resStr) > 4 then
internalerror(2012100805);
for i := low(opTable) to high(opTable) do
if opTable[i] = resStr then
exit(i);
internalerror(2012100806);
end;
procedure foldITInstructions(list: TAsmList);
var
curtai,hp1 : tai;
levels,i : LongInt;
begin
curtai:=tai(list.First);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
if IsIT(taicpu(curtai).opcode) then
begin
levels := GetITLevels(taicpu(curtai).opcode);
if levels < 4 then
begin
i:=levels;
hp1:=tai(curtai.Next);
while assigned(hp1) and
(i > 0) do
begin
if hp1.typ=ait_instruction then
begin
dec(i);
if (i = 0) and
mustbelast(hp1) then
begin
hp1:=nil;
break;
end;
end;
hp1:=tai(hp1.Next);
end;
if assigned(hp1) then
begin
// We are pointing at the first instruction after the IT block
while assigned(hp1) and
(hp1.typ<>ait_instruction) do
hp1:=tai(hp1.Next);
if assigned(hp1) and
(hp1.typ=ait_instruction) and
IsIT(taicpu(hp1).opcode) then
begin
if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
(taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
begin
taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
taicpu(hp1).opcode,
taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
list.Remove(hp1);
hp1.Free;
end;
end;
end;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
procedure fix_invalid_imms(list: TAsmList);
var
curtai: tai;
sh: byte;
begin
curtai:=tai(list.First);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
begin
if (taicpu(curtai).opcode in [A_AND,A_BIC]) and
(taicpu(curtai).ops=3) and
(taicpu(curtai).oper[2]^.typ=top_const) and
(not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
is_shifter_const((not taicpu(curtai).oper[2]^.val) and $FFFFFFFF,sh) then
begin
case taicpu(curtai).opcode of
A_AND: taicpu(curtai).opcode:=A_BIC;
A_BIC: taicpu(curtai).opcode:=A_AND;
end;
taicpu(curtai).oper[2]^.val:=(not taicpu(curtai).oper[2]^.val) and $FFFFFFFF;
end
else if (taicpu(curtai).opcode in [A_SUB,A_ADD]) and
(taicpu(curtai).ops=3) and
(taicpu(curtai).oper[2]^.typ=top_const) and
(not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
is_shifter_const(-taicpu(curtai).oper[2]^.val,sh) then
begin
case taicpu(curtai).opcode of
A_ADD: taicpu(curtai).opcode:=A_SUB;
A_SUB: taicpu(curtai).opcode:=A_ADD;
end;
taicpu(curtai).oper[2]^.val:=-taicpu(curtai).oper[2]^.val;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
procedure gather_it_info(list: TAsmList);
var
curtai: tai;
in_it: boolean;
it_count: longint;
begin
in_it:=false;
it_count:=0;
curtai:=tai(list.First);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
begin
case taicpu(curtai).opcode of
A_IT..A_ITTTT:
begin
if in_it then
Message1(asmw_e_invalid_opcode_and_operands, 'ITxx instruction is inside another ITxx instruction')
else
begin
in_it:=true;
it_count:=GetITLevels(taicpu(curtai).opcode);
end;
end;
else
begin
taicpu(curtai).inIT:=in_it;
taicpu(curtai).lastinIT:=in_it and (it_count=1);
if in_it then
begin
dec(it_count);
if it_count <= 0 then
in_it:=false;
end;
end;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
{ Expands pseudo instructions ( mov r1,r2,lsl #4 -> lsl r1,r2,#4) }
procedure expand_instructions(list: TAsmList);
var
curtai: tai;
begin
curtai:=tai(list.First);
while assigned(curtai) do
begin
case curtai.typ of
ait_instruction:
begin
case taicpu(curtai).opcode of
A_MOV:
begin
if (taicpu(curtai).ops=3) and
(taicpu(curtai).oper[2]^.typ=top_shifterop) then
begin
case taicpu(curtai).oper[2]^.shifterop^.shiftmode of
SM_LSL: taicpu(curtai).opcode:=A_LSL;
SM_LSR: taicpu(curtai).opcode:=A_LSR;
SM_ASR: taicpu(curtai).opcode:=A_ASR;
SM_ROR: taicpu(curtai).opcode:=A_ROR;
SM_RRX: taicpu(curtai).opcode:=A_RRX;
end;
if taicpu(curtai).oper[2]^.shifterop^.shiftmode=SM_RRX then
taicpu(curtai).ops:=2;
if taicpu(curtai).oper[2]^.shifterop^.rs=NR_NO then
taicpu(curtai).loadconst(2, taicpu(curtai).oper[2]^.shifterop^.shiftimm)
else
taicpu(curtai).loadreg(2, taicpu(curtai).oper[2]^.shifterop^.rs);
end;
end;
A_NEG:
begin
taicpu(curtai).opcode:=A_RSB;
taicpu(curtai).oppostfix:=PF_S; // NEG should always set flags (according to documentation NEG<c> = RSBS<c>)
if taicpu(curtai).ops=2 then
begin
taicpu(curtai).loadconst(2,0);
taicpu(curtai).ops:=3;
end
else
begin
taicpu(curtai).loadconst(1,0);
taicpu(curtai).ops:=2;
end;
end;
A_SWI:
begin
taicpu(curtai).opcode:=A_SVC;
end;
end;
end;
end;
curtai:=tai(curtai.Next);
end;
end;
procedure finalizearmcode(list, listtoinsert: TAsmList);
begin
{ Don't expand pseudo instructions when using GAS, it breaks on some thumb instructions }
if target_asm.id<>as_gas then
expand_instructions(list);
{ Do Thumb-2 16bit -> 32bit transformations }
if GenerateThumb2Code then
begin
ensurethumbencodings(list);
ensurethumb2encodings(list);
foldITInstructions(list);
end
else if GenerateThumbCode then
ensurethumbencodings(list);
gather_it_info(list);
fix_invalid_imms(list);
insertpcrelativedata(list, listtoinsert);
end;
procedure InsertPData;
var
prolog: TAsmList;
begin
prolog:=TAsmList.create;
new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
prolog.concat(Tai_const.Create_32bit(0));
prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_METADATA,0,voidpointertype));
{ dummy function }
prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
current_asmdata.asmlists[al_start].insertList(prolog);
prolog.Free;
new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
end;
(*
Floating point instruction format information, taken from the linux kernel
ARM Floating Point Instruction Classes
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
|c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
|c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
|c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
CPDT data transfer instructions
LDF, STF, LFM (copro 2), SFM (copro 2)
CPDO dyadic arithmetic instructions
ADF, MUF, SUF, RSF, DVF, RDF,
POW, RPW, RMF, FML, FDV, FRD, POL
CPDO monadic arithmetic instructions
MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
CPRT joint arithmetic/data transfer instructions
FIX (arithmetic followed by load/store)
FLT (load/store followed by arithmetic)
CMF, CNF CMFE, CNFE (comparisons)
WFS, RFS (write/read floating point status register)
WFC, RFC (write/read floating point control register)
cond condition codes
P pre/post index bit: 0 = postindex, 1 = preindex
U up/down bit: 0 = stack grows down, 1 = stack grows up
W write back bit: 1 = update base register (Rn)
L load/store bit: 0 = store, 1 = load
Rn base register
Rd destination/source register
Fd floating point destination register
Fn floating point source register
Fm floating point source register or floating point constant
uv transfer length (TABLE 1)
wx register count (TABLE 2)
abcd arithmetic opcode (TABLES 3 & 4)
ef destination size (rounding precision) (TABLE 5)
gh rounding mode (TABLE 6)
j dyadic/monadic bit: 0 = dyadic, 1 = monadic
i constant bit: 1 = constant (TABLE 6)
*/
/*
TABLE 1
+-------------------------+---+---+---------+---------+
| Precision | u | v | FPSR.EP | length |
+-------------------------+---+---+---------+---------+
| Single | 0 | 0 | x | 1 words |
| Double | 1 | 1 | x | 2 words |
| Extended | 1 | 1 | x | 3 words |
| Packed decimal | 1 | 1 | 0 | 3 words |
| Expanded packed decimal | 1 | 1 | 1 | 4 words |
+-------------------------+---+---+---------+---------+
Note: x = don't care
*/
/*
TABLE 2
+---+---+---------------------------------+
| w | x | Number of registers to transfer |
+---+---+---------------------------------+
| 0 | 1 | 1 |
| 1 | 0 | 2 |
| 1 | 1 | 3 |
| 0 | 0 | 4 |
+---+---+---------------------------------+
*/
/*
TABLE 3: Dyadic Floating Point Opcodes
+---+---+---+---+----------+-----------------------+-----------------------+
| a | b | c | d | Mnemonic | Description | Operation |
+---+---+---+---+----------+-----------------------+-----------------------+
| 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
| 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
| 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
| 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
| 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
| 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
| 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
| 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
| 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
| 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
| 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
| 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
| 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
| 1 | 1 | 0 | 1 | | undefined instruction | trap |
| 1 | 1 | 1 | 0 | | undefined instruction | trap |
| 1 | 1 | 1 | 1 | | undefined instruction | trap |
+---+---+---+---+----------+-----------------------+-----------------------+
Note: POW, RPW, POL are deprecated, and are available for backwards
compatibility only.
*/
/*
TABLE 4: Monadic Floating Point Opcodes
+---+---+---+---+----------+-----------------------+-----------------------+
| a | b | c | d | Mnemonic | Description | Operation |
+---+---+---+---+----------+-----------------------+-----------------------+
| 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
| 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
| 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
| 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
| 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
| 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
| 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
| 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
| 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
| 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
| 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
| 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
| 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
| 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
| 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
| 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
+---+---+---+---+----------+-----------------------+-----------------------+
Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
available for backwards compatibility only.
*/
/*
TABLE 5
+-------------------------+---+---+
| Rounding Precision | e | f |
+-------------------------+---+---+
| IEEE Single precision | 0 | 0 |
| IEEE Double precision | 0 | 1 |
| IEEE Extended precision | 1 | 0 |
| undefined (trap) | 1 | 1 |
+-------------------------+---+---+
*/
/*
TABLE 5
+---------------------------------+---+---+
| Rounding Mode | g | h |
+---------------------------------+---+---+
| Round to nearest (default) | 0 | 0 |
| Round toward plus infinity | 0 | 1 |
| Round toward negative infinity | 1 | 0 |
| Round toward zero | 1 | 1 |
+---------------------------------+---+---+
*)
function taicpu.GetString:string;
var
i : longint;
s : string;
addsize : boolean;
begin
s:='['+gas_op2str[opcode];
for i:=0 to ops-1 do
begin
with oper[i]^ do
begin
if i=0 then
s:=s+' '
else
s:=s+',';
{ type }
addsize:=false;
if (ot and OT_VREG)=OT_VREG then
s:=s+'vreg'
else
if (ot and OT_FPUREG)=OT_FPUREG then
s:=s+'fpureg'
else
if (ot and OT_REGS)=OT_REGS then
s:=s+'sreg'
else
if (ot and OT_REGF)=OT_REGF then
s:=s+'creg'
else
if (ot and OT_REGISTER)=OT_REGISTER then
begin
s:=s+'reg';
addsize:=true;
end
else
if (ot and OT_REGLIST)=OT_REGLIST then
begin
s:=s+'reglist';
addsize:=false;
end
else
if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
begin
s:=s+'imm';
addsize:=true;
end
else
if (ot and OT_MEMORY)=OT_MEMORY then
begin
s:=s+'mem';
addsize:=true;
if (ot and OT_AM2)<>0 then
s:=s+' am2 '
else if (ot and OT_AM6)<>0 then
s:=s+' am2 ';
end
else
if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
begin
s:=s+'shifterop';
addsize:=false;
end
else
s:=s+'???';
{ size }
if addsize then
begin
if (ot and OT_BITS8)<>0 then
s:=s+'8'
else
if (ot and OT_BITS16)<>0 then
s:=s+'24'
else
if (ot and OT_BITS32)<>0 then
s:=s+'32'
else
if (ot and OT_BITSSHIFTER)<>0 then
s:=s+'shifter'
else
s:=s+'??';
{ signed }
if (ot and OT_SIGNED)<>0 then
s:=s+'s';
end;
end;
end;
GetString:=s+']';
end;
procedure taicpu.ResetPass1;
begin
{ we need to reset everything here, because the choosen insentry
can be invalid for a new situation where the previously optimized
insentry is not correct }
InsEntry:=nil;
InsSize:=0;
LastInsOffset:=-1;
end;
procedure taicpu.ResetPass2;
begin
{ we are here in a second pass, check if the instruction can be optimized }
if assigned(InsEntry) and
((InsEntry^.flags and IF_PASS2)<>0) then
begin
InsEntry:=nil;
InsSize:=0;
end;
LastInsOffset:=-1;
end;
function taicpu.CheckIfValid:boolean;
begin
Result:=False; { unimplemented }
end;
function taicpu.Pass1(objdata:TObjData):longint;
var
ldr2op : array[PF_B..PF_T] of tasmop = (
A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
str2op : array[PF_B..PF_T] of tasmop = (
A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
begin
Pass1:=0;
{ Save the old offset and set the new offset }
InsOffset:=ObjData.CurrObjSec.Size;
{ Error? }
if (Insentry=nil) and (InsSize=-1) then
exit;
{ set the file postion }
current_filepos:=fileinfo;
{ tranlate LDR+postfix to complete opcode }
if (opcode=A_LDR) and (oppostfix=PF_D) then
begin
opcode:=A_LDRD;
oppostfix:=PF_None;
end
else if (opcode=A_LDR) and (oppostfix<>PF_None) then
begin
if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
opcode:=ldr2op[oppostfix]
else
internalerror(2005091001);
if opcode=A_None then
internalerror(2005091004);
{ postfix has been added to opcode }
oppostfix:=PF_None;
end
else if (opcode=A_STR) and (oppostfix=PF_D) then
begin
opcode:=A_STRD;
oppostfix:=PF_None;
end
else if (opcode=A_STR) and (oppostfix<>PF_None) then
begin
if (oppostfix in [low(str2op)..high(str2op)]) then
opcode:=str2op[oppostfix]
else
internalerror(2005091002);
if opcode=A_None then
internalerror(2005091003);
{ postfix has been added to opcode }
oppostfix:=PF_None;
end;
{ Get InsEntry }
if FindInsEntry(objdata) then
begin
InsSize:=4;
if insentry^.code[0] in [#$60..#$6C] then
InsSize:=2;
LastInsOffset:=InsOffset;
Pass1:=InsSize;
exit;
end;
LastInsOffset:=-1;
end;
procedure taicpu.Pass2(objdata:TObjData);
begin
{ error in pass1 ? }
if insentry=nil then
exit;
current_filepos:=fileinfo;
{ Generate the instruction }
GenCode(objdata);
end;
procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
begin
end;
procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
begin
end;
procedure taicpu.ppubuildderefimploper(var o:toper);
begin
end;
procedure taicpu.ppuderefoper(var o:toper);
begin
end;
procedure taicpu.BuildArmMasks(objdata:TObjData);
const
Masks: array[tcputype] of longint =
(
IF_NONE,
IF_ARMv4,
IF_ARMv4,
IF_ARMv4T or IF_ARMv4,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z,
IF_ARMv4T or IF_ARMv5T or IF_ARMv6M,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A,
IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A or IF_ARMv7R,
IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M,
IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M or IF_ARMv7EM
);
FPUMasks: array[tfputype] of longword =
(
IF_NONE,
IF_NONE,
IF_NONE,
IF_FPA,
IF_FPA,
IF_FPA,
IF_VFPv2,
IF_VFPv2 or IF_VFPv3,
IF_VFPv2 or IF_VFPv3,
IF_NONE,
IF_VFPv2 or IF_VFPv3 or IF_VFPv4
);
begin
fArmVMask:=Masks[current_settings.cputype] or FPUMasks[current_settings.fputype];
if objdata.ThumbFunc then
//if current_settings.instructionset=is_thumb then
begin
fArmMask:=IF_THUMB;
if CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype] then
fArmMask:=fArmMask or IF_THUMB32;
end
else
fArmMask:=IF_ARM32;
end;
function taicpu.InsEnd:longint;
begin
Result:=0; { unimplemented }
end;
procedure taicpu.create_ot(objdata:TObjData);
var
i,l,relsize : longint;
dummy : byte;
currsym : TObjSymbol;
begin
if ops=0 then
exit;
{ update oper[].ot field }
for i:=0 to ops-1 do
with oper[i]^ do
begin
case typ of
top_regset:
begin
ot:=OT_REGLIST;
end;
top_reg :
begin
case getregtype(reg) of
R_INTREGISTER:
begin
ot:=OT_REG32 or OT_SHIFTEROP;
if getsupreg(reg)<8 then
ot:=ot or OT_REGLO
else if reg=NR_STACK_POINTER_REG then
ot:=ot or OT_REGSP;
end;
R_FPUREGISTER:
ot:=OT_FPUREG;
R_MMREGISTER:
ot:=OT_VREG;
R_SPECIALREGISTER:
ot:=OT_REGF;
else
internalerror(2005090901);
end;
end;
top_ref :
begin
if ref^.refaddr=addr_no then
begin
{ create ot field }
{ we should get the size here dependend on the
instruction }
if (ot and OT_SIZE_MASK)=0 then
ot:=OT_MEMORY or OT_BITS32
else
ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
ot:=ot or OT_MEM_OFFS;
{ if we need to fix a reference, we do it here }
{ pc relative addressing }
if (ref^.base=NR_NO) and
(ref^.index=NR_NO) and
(ref^.shiftmode=SM_None)
{ at least we should check if the destination symbol
is in a text section }
{ and
(ref^.symbol^.owner="text") } then
ref^.base:=NR_PC;
{ determine possible address modes }
if GenerateThumbCode or
GenerateThumb2Code then
begin
if (ref^.addressmode<>AM_OFFSET) then
ot:=ot or OT_AM2
else if (ref^.base=NR_PC) then
ot:=ot or OT_AM6
else if (ref^.base=NR_STACK_POINTER_REG) then
ot:=ot or OT_AM5
else if ref^.index=NR_NO then
ot:=ot or OT_AM4
else
ot:=ot or OT_AM3;
end;
if (ref^.base<>NR_NO) and
(opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
(
(ref^.addressmode=AM_OFFSET) and
(ref^.index=NR_NO) and
(ref^.shiftmode=SM_None) and
(ref^.offset=0)
) then
ot:=ot or OT_AM6
else if (ref^.base<>NR_NO) and
(
(
(ref^.index=NR_NO) and
(ref^.shiftmode=SM_None) and
(ref^.offset>=-4097) and
(ref^.offset<=4097)
) or
(
(ref^.shiftmode=SM_None) and
(ref^.offset=0)
) or
(
(ref^.index<>NR_NO) and
(ref^.shiftmode<>SM_None) and
(ref^.shiftimm<=32) and
(ref^.offset=0)
)
) then
ot:=ot or OT_AM2;
if (ref^.index<>NR_NO) and
(oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
PF_IAD,PF_DBD,PF_FDD,PF_EAD,
PF_IAS,PF_DBS,PF_FDS,PF_EAS,
PF_IAX,PF_DBX,PF_FDX,PF_EAX]) and
(
(ref^.base=NR_NO) and
(ref^.shiftmode=SM_None) and
(ref^.offset=0)
) then
ot:=ot or OT_AM4;
end
else
begin
l:=ref^.offset;
currsym:=ObjData.symbolref(ref^.symbol);
if assigned(currsym) then
inc(l,currsym.address);
relsize:=(InsOffset+2)-l;
if (relsize<-33554428) or (relsize>33554428) then
ot:=OT_IMM32
else
ot:=OT_IMM24;
end;
end;
top_local :
begin
{ we should get the size here dependend on the
instruction }
if (ot and OT_SIZE_MASK)=0 then
ot:=OT_MEMORY or OT_BITS32
else
ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
end;
top_const :
begin
ot:=OT_IMMEDIATE;
if (val=0) then
ot:=ot_immediatezero
else if is_shifter_const(val,dummy) then
ot:=OT_IMMSHIFTER
else if GenerateThumb2Code and is_thumb32_imm(val) then
ot:=OT_IMMSHIFTER
else
ot:=OT_IMM32
end;
top_none :
begin
{ generated when there was an error in the
assembler reader. It never happends when generating
assembler }
end;
top_shifterop:
begin
ot:=OT_SHIFTEROP;
end;
top_conditioncode:
begin
ot:=OT_CONDITION;
end;
top_specialreg:
begin
ot:=OT_REGS;
end;
top_modeflags:
begin
ot:=OT_MODEFLAGS;
end;
top_realconst:
begin
ot:=OT_IMMEDIATEMM;
end;
else
internalerror(2004022623);
end;
end;
end;
function taicpu.Matches(p:PInsEntry):longint;
{ * IF_SM stands for Size Match: any operand whose size is not
* explicitly specified by the template is `really' intended to be
* the same size as the first size-specified operand.
* Non-specification is tolerated in the input instruction, but
* _wrong_ specification is not.
*
* IF_SM2 invokes Size Match on only the first _two_ operands, for
* three-operand instructions such as SHLD: it implies that the
* first two operands must match in size, but that the third is
* required to be _unspecified_.
*
* IF_SB invokes Size Byte: operands with unspecified size in the
* template are really bytes, and so no non-byte specification in
* the input instruction will be tolerated. IF_SW similarly invokes
* Size Word, and IF_SD invokes Size Doubleword.
*
* (The default state if neither IF_SM nor IF_SM2 is specified is
* that any operand with unspecified size in the template is
* required to have unspecified size in the instruction too...)
}
var
i{,j,asize,oprs} : longint;
{siz : array[0..3] of longint;}
begin
Matches:=100;
{ Check the opcode and operands }
if (p^.opcode<>opcode) or (p^.ops<>ops) then
begin
Matches:=0;
exit;
end;
{ check ARM instruction version }
if (p^.flags and fArmVMask)=0 then
begin
Matches:=0;
exit;
end;
{ check ARM instruction type }
if (p^.flags and fArmMask)=0 then
begin
Matches:=0;
exit;
end;
{ Check wideformat flag }
if wideformat and ((p^.flags and IF_WIDE)=0) then
begin
matches:=0;
exit;
end;
{ Check that no spurious colons or TOs are present }
for i:=0 to p^.ops-1 do
if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
begin
Matches:=0;
exit;
end;
{ Check that the operand flags all match up }
for i:=0 to p^.ops-1 do
begin
if ((p^.optypes[i] and (not oper[i]^.ot)) or
((p^.optypes[i] and OT_SIZE_MASK) and
((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
begin
if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
(oper[i]^.ot and OT_SIZE_MASK))<>0 then
begin
Matches:=0;
exit;
end
else if ((p^.optypes[i] and OT_OPT_SIZE)<>0) and
((p^.optypes[i] and OT_OPT_SIZE)<>(oper[i]^.ot and OT_OPT_SIZE)) then
begin
Matches:=0;
exit;
end
else
Matches:=1;
end;
end;
{ check postfixes:
the existance of a certain postfix requires a
particular code }
{ update condition flags
or floating point single }
if (oppostfix=PF_S) and
not(p^.code[0] in [#$04..#$0F,#$14..#$16,#$29,#$30,#$60..#$6B,#$80..#$82,#$A0..#$A2,#$44,#$94,#$42,#$92]) then
begin
Matches:=0;
exit;
end;
{ floating point size }
if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
not(p^.code[0] in [
// FPA
#$A0..#$A2,
// old-school VFP
#$42,#$92,
// vldm/vstm
#$44,#$94]) then
begin
Matches:=0;
exit;
end;
{ multiple load/store address modes }
if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
not(p^.code[0] in [
// ldr,str,ldrb,strb
#$17,
// stm,ldm
#$26,#$69,#$8C,
// vldm/vstm
#$44,#$94
]) then
begin
Matches:=0;
exit;
end;
{ we shouldn't see any opsize prefixes here }
if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
begin
Matches:=0;
exit;
end;
if (roundingmode<>RM_None) and not(p^.code[0] in []) then
begin
Matches:=0;
exit;
end;
{ Check thumb flags }
if p^.code[0] in [#$60..#$61] then
begin
if (p^.code[0]=#$60) and
(GenerateThumb2Code and
((not inIT) and (oppostfix<>PF_S)) or
(inIT and (condition=C_None))) then
begin
Matches:=0;
exit;
end
else if (p^.code[0]=#$61) and
(oppostfix=PF_S) then
begin
Matches:=0;
exit;
end;
end
else if p^.code[0]=#$62 then
begin
if (GenerateThumb2Code and
(condition<>C_None) and
(not inIT) and
(not lastinIT)) then
begin
Matches:=0;
exit;
end;
end
else if p^.code[0]=#$63 then
begin
if inIT then
begin
Matches:=0;
exit;
end;
end
else if p^.code[0]=#$64 then
begin
if (opcode=A_MUL) then
begin
if (ops=3) and
((oper[2]^.typ<>top_reg) or
(oper[0]^.reg<>oper[2]^.reg)) then
begin
matches:=0;
exit;
end;
end;
end
else if p^.code[0]=#$6B then
begin
if inIT or
(oppostfix<>PF_S) then
begin
Matches:=0;
exit;
end;
end;
{ Check operand sizes }
{ as default an untyped size can get all the sizes, this is different
from nasm, but else we need to do a lot checking which opcodes want
size or not with the automatic size generation }
(*
asize:=longint($ffffffff);
if (p^.flags and IF_SB)<>0 then
asize:=OT_BITS8
else if (p^.flags and IF_SW)<>0 then
asize:=OT_BITS16
else if (p^.flags and IF_SD)<>0 then
asize:=OT_BITS32;
if (p^.flags and IF_ARMASK)<>0 then
begin
siz[0]:=0;
siz[1]:=0;
siz[2]:=0;
if (p^.flags and IF_AR0)<>0 then
siz[0]:=asize
else if (p^.flags and IF_AR1)<>0 then
siz[1]:=asize
else if (p^.flags and IF_AR2)<>0 then
siz[2]:=asize;
end
else
begin
{ we can leave because the size for all operands is forced to be
the same
but not if IF_SB IF_SW or IF_SD is set PM }
if asize=-1 then
exit;
siz[0]:=asize;
siz[1]:=asize;
siz[2]:=asize;
end;
if (p^.flags and (IF_SM or IF_SM2))<>0 then
begin
if (p^.flags and IF_SM2)<>0 then
oprs:=2
else
oprs:=p^.ops;
for i:=0 to oprs-1 do
if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
begin
for j:=0 to oprs-1 do
siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
break;
end;
end
else
oprs:=2;
{ Check operand sizes }
for i:=0 to p^.ops-1 do
begin
if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
{ Immediates can always include smaller size }
((oper[i]^.ot and OT_IMMEDIATE)=0) and
(((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
Matches:=2;
end;
*)
end;
function taicpu.calcsize(p:PInsEntry):shortint;
begin
result:=4;
end;
function taicpu.NeedAddrPrefix(opidx:byte):boolean;
begin
Result:=False; { unimplemented }
end;
procedure taicpu.Swapoperands;
begin
end;
function taicpu.FindInsentry(objdata:TObjData):boolean;
var
i : longint;
begin
result:=false;
{ Things which may only be done once, not when a second pass is done to
optimize }
if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
begin
{ create the .ot fields }
create_ot(objdata);
BuildArmMasks(objdata);
{ set the file postion }
current_filepos:=fileinfo;
end
else
begin
{ we've already an insentry so it's valid }
result:=true;
exit;
end;
{ Lookup opcode in the table }
InsSize:=-1;
i:=instabcache^[opcode];
if i=-1 then
begin
Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
exit;
end;
insentry:=@instab[i];
while (insentry^.opcode=opcode) do
begin
if matches(insentry)=100 then
begin
result:=true;
exit;
end;
inc(i);
insentry:=@instab[i];
end;
Message1(asmw_e_invalid_opcode_and_operands,GetString);
{ No instruction found, set insentry to nil and inssize to -1 }
insentry:=nil;
inssize:=-1;
end;
procedure taicpu.gencode(objdata:TObjData);
const
CondVal : array[TAsmCond] of byte=(
$E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
$B, $C, $D, $E, 0);
var
bytes, rd, rm, rn, d, m, n : dword;
bytelen : longint;
dp_operation : boolean;
i_field : byte;
currsym : TObjSymbol;
offset : longint;
refoper : poper;
msb : longint;
r: byte;
singlerec : tcompsinglerec;
doublerec : tcompdoublerec;
procedure setshifterop(op : byte);
var
r : byte;
imm : dword;
count : integer;
begin
case oper[op]^.typ of
top_const:
begin
i_field:=1;
if oper[op]^.val and $ff=oper[op]^.val then
bytes:=bytes or dword(oper[op]^.val)
else
begin
{ calc rotate and adjust imm }
count:=0;
r:=0;
imm:=dword(oper[op]^.val);
repeat
imm:=RolDWord(imm, 2);
inc(r);
inc(count);
if count > 32 then
begin
message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
exit;
end;
until (imm and $ff)=imm;
bytes:=bytes or (r shl 8) or imm;
end;
end;
top_reg:
begin
i_field:=0;
bytes:=bytes or getsupreg(oper[op]^.reg);
{ does a real shifter op follow? }
if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
with oper[op+1]^.shifterop^ do
begin
bytes:=bytes or ((shiftimm and $1F) shl 7);
if shiftmode<>SM_RRX then
bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
else
bytes:=bytes or (3 shl 5);
if getregtype(rs) <> R_INVALIDREGISTER then
begin
bytes:=bytes or (1 shl 4);
bytes:=bytes or (getsupreg(rs) shl 8);
end
end;
end;
else
internalerror(2005091103);
end;
end;
function MakeRegList(reglist: tcpuregisterset): word;
var
i, w: integer;
begin
result:=0;
w:=0;
for i:=RS_R0 to RS_R15 do
begin
if i in reglist then
result:=result or (1 shl w);
inc(w);
end;
end;
function getcoproc(reg: tregister): byte;
begin
if reg=NR_p15 then
result:=15
else
begin
Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
result:=0;
end;
end;
function getcoprocreg(reg: tregister): byte;
var
tmpr: tregister;
begin
{ FIXME: temp variable r is needed here to avoid Internal error 20060521 }
{ while compiling the compiler. }
tmpr:=NR_CR0;
result:=getsupreg(reg)-getsupreg(tmpr);
end;
function getmmreg(reg: tregister): byte;
begin
case reg of
NR_D0: result:=0;
NR_D1: result:=1;
NR_D2: result:=2;
NR_D3: result:=3;
NR_D4: result:=4;
NR_D5: result:=5;
NR_D6: result:=6;
NR_D7: result:=7;
NR_D8: result:=8;
NR_D9: result:=9;
NR_D10: result:=10;
NR_D11: result:=11;
NR_D12: result:=12;
NR_D13: result:=13;
NR_D14: result:=14;
NR_D15: result:=15;
NR_D16: result:=16;
NR_D17: result:=17;
NR_D18: result:=18;
NR_D19: result:=19;
NR_D20: result:=20;
NR_D21: result:=21;
NR_D22: result:=22;
NR_D23: result:=23;
NR_D24: result:=24;
NR_D25: result:=25;
NR_D26: result:=26;
NR_D27: result:=27;
NR_D28: result:=28;
NR_D29: result:=29;
NR_D30: result:=30;
NR_D31: result:=31;
NR_S0: result:=0;
NR_S1: result:=1;
NR_S2: result:=2;
NR_S3: result:=3;
NR_S4: result:=4;
NR_S5: result:=5;
NR_S6: result:=6;
NR_S7: result:=7;
NR_S8: result:=8;
NR_S9: result:=9;
NR_S10: result:=10;
NR_S11: result:=11;
NR_S12: result:=12;
NR_S13: result:=13;
NR_S14: result:=14;
NR_S15: result:=15;
NR_S16: result:=16;
NR_S17: result:=17;
NR_S18: result:=18;
NR_S19: result:=19;
NR_S20: result:=20;
NR_S21: result:=21;
NR_S22: result:=22;
NR_S23: result:=23;
NR_S24: result:=24;
NR_S25: result:=25;
NR_S26: result:=26;
NR_S27: result:=27;
NR_S28: result:=28;
NR_S29: result:=29;
NR_S30: result:=30;
NR_S31: result:=31;
else
result:=0;
end;
end;
procedure encodethumbimm(imm: longword);
var
imm12, tmp: tcgint;
shift: integer;
found: boolean;
begin
found:=true;
if (imm and $FF) = imm then
imm12:=imm
else if ((imm shr 16)=(imm and $FFFF)) and
((imm and $FF00FF00) = 0) then
imm12:=(imm and $ff) or ($1 shl 8)
else if ((imm shr 16)=(imm and $FFFF)) and
((imm and $00FF00FF) = 0) then
imm12:=((imm shr 8) and $ff) or ($2 shl 8)
else if ((imm shr 16)=(imm and $FFFF)) and
(((imm shr 8) and $FF)=(imm and $FF)) then
imm12:=(imm and $ff) or ($3 shl 8)
else
begin
found:=false;
imm12:=0;
for shift:=1 to 31 do
begin
tmp:=RolDWord(imm,shift);
if ((tmp and $FF)=tmp) and
((tmp and $80)=$80) then
begin
imm12:=(tmp and $7F) or (shift shl 7);
found:=true;
break;
end;
end;
end;
if found then
begin
bytes:=bytes or (imm12 and $FF);
bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
end
else
Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
end;
procedure setthumbshift(op: byte; is_sat: boolean = false);
var
shift,typ: byte;
begin
shift:=0;
typ:=0;
case oper[op]^.shifterop^.shiftmode of
SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
SM_RRX: begin typ:=3; shift:=0; end;
end;
if is_sat then
begin
bytes:=bytes or ((typ and 1) shl 5);
bytes:=bytes or ((typ shr 1) shl 21);
end
else
bytes:=bytes or (typ shl 4);
bytes:=bytes or (shift and $3) shl 6;
bytes:=bytes or ((shift and $1C) shr 2) shl 12;
end;
begin
bytes:=$0;
bytelen:=4;
i_field:=0;
{ evaluate and set condition code }
bytes:=bytes or (CondVal[condition] shl 28);
{ condition code allowed? }
{ setup rest of the instruction }
case insentry^.code[0] of
#$01: // B/BL
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
{ set offset }
if oper[0]^.typ=top_const then
bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
else
begin
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
bytes:=bytes or (((oper[0]^.ref^.offset-8) shr 2) and $ffffff);
if (opcode<>A_BL) or (condition<>C_None) then
objdata.writereloc(aint(bytes),4,currsym,RELOC_RELATIVE_24)
else
objdata.writereloc(aint(bytes),4,currsym,RELOC_RELATIVE_CALL);
exit;
end;
end;
#$02:
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
{ set code }
bytes:=bytes or (oper[0]^.val and $FFFFFF);
end;
#$03:
begin // BLX/BX
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
bytes:=bytes or getsupreg(oper[0]^.reg);
end;
#$04..#$07: // SUB
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
{ set Rn }
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
{ create shifter op }
setshifterop(2);
{ set I field }
bytes:=bytes or (i_field shl 25);
{ set S if necessary }
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$08,#$0A,#$0B: // MOV
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
{ create shifter op }
setshifterop(1);
{ set I field }
bytes:=bytes or (i_field shl 25);
{ set S if necessary }
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$0C,#$0E,#$0F: // CMP
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
{ create shifter op }
setshifterop(1);
{ set I field }
bytes:=bytes or (i_field shl 25);
{ always set S bit }
bytes:=bytes or (1 shl 20);
end;
#$10: // MRS
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
case oper[1]^.reg of
NR_APSR,NR_CPSR:;
NR_SPSR:
begin
bytes:=bytes or (1 shl 22);
end;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end;
#$12,#$13: // MSR
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
{ set destination }
if oper[0]^.typ=top_specialreg then
begin
if (oper[0]^.specialreg<>NR_CPSR) and
(oper[0]^.specialreg<>NR_SPSR) then
Message1(asmw_e_invalid_opcode_and_operands, '"Invalid special reg"');
if srC in oper[0]^.specialflags then
bytes:=bytes or (1 shl 16);
if srX in oper[0]^.specialflags then
bytes:=bytes or (1 shl 17);
if srS in oper[0]^.specialflags then
bytes:=bytes or (1 shl 18);
if srF in oper[0]^.specialflags then
bytes:=bytes or (1 shl 19);
{ Set R bit }
if oper[0]^.specialreg=NR_SPSR then
bytes:=bytes or (1 shl 22);
end
else
case oper[0]^.reg of
NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
NR_APSR_g: bytes:=bytes or (1 shl 18);
NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
else
Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
end;
setshifterop(1);
end;
#$14: // MUL/MLA r1,r2,r3
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]);
{ set regs }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
bytes:=bytes or getsupreg(oper[1]^.reg);
bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
if oppostfix in [PF_S] then
bytes:=bytes or (1 shl 20);
end;
#$15: // MUL/MLA r1,r2,r3,r4
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
bytes:=bytes or getsupreg(oper[1]^.reg);
bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
if ops>3 then
bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
else
bytes:=bytes or ord(insentry^.code[4]) shl 12;
if oppostfix in [PF_R,PF_X] then
bytes:=bytes or (1 shl 5);
if oppostfix in [PF_S] then
bytes:=bytes or (1 shl 20);
end;
#$16: // MULL r1,r2,r3,r4
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
if (ops=3) and (opcode=A_PKHTB) then
begin
bytes:=bytes or getsupreg(oper[1]^.reg);
bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
end
else
begin
bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
bytes:=bytes or getsupreg(oper[2]^.reg);
end;
if ops=4 then
begin
if oper[3]^.typ=top_shifterop then
begin
if opcode in [A_PKHBT,A_PKHTB] then
begin
if ((opcode=A_PKHTB) and
(oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
((opcode=A_PKHBT) and
(oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
(oper[3]^.shifterop^.rs<>NR_NO) then
Message1(asmw_e_invalid_opcode_and_operands,GetString);
bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
end
else
begin
if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
(oper[3]^.shifterop^.rs<>NR_NO) or
(not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
Message1(asmw_e_invalid_opcode_and_operands,GetString);
bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
end;
end
else
bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
end;
if PF_S=oppostfix then
bytes:=bytes or (1 shl 20);
if PF_X=oppostfix then
bytes:=bytes or (1 shl 5);
end;
#$17: // LDR/STR
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[1]^.ref^.offset;
if offset>=0 then
{ set U flag }
bytes:=bytes or (1 shl 23)
else
offset:=-offset;
bytes:=bytes or (offset and $FFF);
end
else
begin
{ set U flag }
if oper[1]^.ref^.signindex>=0 then
bytes:=bytes or (1 shl 23);
{ set I flag }
bytes:=bytes or (1 shl 25);
bytes:=bytes or getsupreg(oper[1]^.ref^.index);
{ set shift }
with oper[1]^.ref^ do
if shiftmode<>SM_None then
begin
bytes:=bytes or ((shiftimm and $1F) shl 7);
if shiftmode<>SM_RRX then
bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
else
bytes:=bytes or (3 shl 5);
end
end;
{ set W bit }
if oper[1]^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 21);
{ set P bit if necessary }
if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
bytes:=bytes or (1 shl 24);
end;
#$18: // LDREX/STREX
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
if (ops=3) then
begin
if opcode<>A_LDREXD then
bytes:=bytes or getsupreg(oper[1]^.reg);
bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
end
else if (ops=4) then // STREXD
begin
if opcode<>A_LDREXD then
bytes:=bytes or getsupreg(oper[1]^.reg);
bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
end
else
bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
end;
#$19: // LDRD/STRD
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
refoper:=oper[1];
if ops=3 then
refoper:=oper[2];
bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
begin
bytes:=bytes or (1 shl 22);
{ set offset }
offset:=0;
currsym:=objdata.symbolref(refoper^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+refoper^.ref^.offset;
if offset>=0 then
{ set U flag }
bytes:=bytes or (1 shl 23)
else
offset:=-offset;
bytes:=bytes or (offset and $F);
bytes:=bytes or ((offset and $F0) shl 4);
end
else
begin
{ set U flag }
if refoper^.ref^.signindex>=0 then
bytes:=bytes or (1 shl 23);
bytes:=bytes or getsupreg(refoper^.ref^.index);
end;
{ set W bit }
if refoper^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 21);
{ set P bit if necessary }
if refoper^.ref^.addressmode<>AM_POSTINDEXED then
bytes:=bytes or (1 shl 24);
end;
#$1A: // QADD/QSUB
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
end;
#$1B:
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.reg);
if ops=3 then
begin
if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
(oper[2]^.shifterop^.rs<>NR_NO) or
(not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
Message1(asmw_e_invalid_opcode_and_operands,GetString);
bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
end;
end;
#$1C: // MCR/MRC
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs and operands }
bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
bytes:=bytes or ((oper[1]^.val and $7) shl 21);
bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
bytes:=bytes or getcoprocreg(oper[4]^.reg);
if ops > 5 then
bytes:=bytes or ((oper[5]^.val and $7) shl 5);
end;
#$1D: // MCRR/MRRC
begin
{ set instruction code }
bytes:=bytes or ord(insentry^.code[1]) shl 24;
bytes:=bytes or ord(insentry^.code[2]) shl 16;
bytes:=bytes or ord(insentry^.code[3]) shl 4;
{ set regs and operands }
bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
bytes:=bytes or ((oper[1]^.val and $7) shl 4);
bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
bytes:=bytes or getcoprocreg(oper[4]^.reg);
end;
#$1E: // LDRHT/STRHT
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
refoper:=oper[1];
bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
begin
bytes:=bytes or (1 shl 22);
{ set offset }
offset:=0;
currsym:=objdata.symbolref(refoper^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+refoper^.ref^.offset;
if offset>=0 then
{ set U flag }
bytes:=bytes or (1 shl 23)
else
offset:=-offset;
bytes:=bytes or (offset and $F);
bytes:=bytes or ((offset and $F0) shl 4);
end
else
begin
{ set U flag }
if refoper^.ref^.signindex>=0 then
bytes:=bytes or (1 shl 23);
bytes:=bytes or getsupreg(refoper^.ref^.index);
end;
end;
#$22: // LDRH/STRH
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 16);
bytes:=bytes or ord(insentry^.code[2]);
{ src/dest register (Rd) }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
{ base register (Rn) }
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
begin
bytes:=bytes or (1 shl 22); // with immediate offset
offset:=oper[1]^.ref^.offset;
if offset>=0 then
{ set U flag }
bytes:=bytes or (1 shl 23)
else
offset:=-offset;
bytes:=bytes or (offset and $F);
bytes:=bytes or ((offset and $F0) shl 4);
end
else
begin
{ set U flag }
if oper[1]^.ref^.signindex>=0 then
bytes:=bytes or (1 shl 23);
bytes:=bytes or getsupreg(oper[1]^.ref^.index);
end;
{ set W bit }
if oper[1]^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 21);
{ set P bit if necessary }
if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
bytes:=bytes or (1 shl 24);
end;
#$25: // PLD/PLI
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[0]^.ref^.offset;
if offset>=0 then
begin
{ set U flag }
bytes:=bytes or (1 shl 23);
bytes:=bytes or offset
end
else
begin
offset:=-offset;
bytes:=bytes or offset
end;
end
else
begin
bytes:=bytes or (1 shl 25);
{ set U flag }
if oper[0]^.ref^.signindex>=0 then
bytes:=bytes or (1 shl 23);
bytes:=bytes or getsupreg(oper[0]^.ref^.index);
{ set shift }
with oper[0]^.ref^ do
if shiftmode<>SM_None then
begin
bytes:=bytes or ((shiftimm and $1F) shl 7);
if shiftmode<>SM_RRX then
bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
else
bytes:=bytes or (3 shl 5);
end
end;
end;
#$26: // LDM/STM
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 20);
if ops>1 then
begin
if oper[0]^.typ=top_ref then
begin
{ set W bit }
if oper[0]^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 21);
{ set Rn }
bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
end
else { typ=top_reg }
begin
{ set Rn }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
end;
if oper[1]^.usermode then
begin
if (oper[0]^.typ=top_ref) then
begin
if (opcode=A_LDM) and
(RS_PC in oper[1]^.regset^) then
begin
// Valid exception return
end
else
Message(asmw_e_invalid_opcode_and_operands);
end;
bytes:=bytes or (1 shl 22);
end;
{ reglist }
bytes:=bytes or MakeRegList(oper[1]^.regset^);
end
else
begin
{ push/pop }
{ Set W and Rn to SP }
if opcode=A_PUSH then
bytes:=bytes or (1 shl 21);
bytes:=bytes or ($D shl 16);
{ reglist }
bytes:=bytes or MakeRegList(oper[0]^.regset^);
end;
{ set P bit }
if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
or (opcode=A_PUSH) then
bytes:=bytes or (1 shl 24);
{ set U bit }
if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
or (opcode=A_POP) then
bytes:=bytes or (1 shl 23);
end;
#$27: // SWP/SWPB
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 20);
bytes:=bytes or (ord(insentry^.code[2]) shl 4);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or getsupreg(oper[1]^.reg);
if ops=3 then
bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
end;
#$28: // BX/BLX
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
{ set offset }
if oper[0]^.typ=top_const then
bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
else
begin
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
begin
bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
end
else
begin
offset:=((currsym.offset-insoffset-8) and $3fffffe);
{ Turn BLX into BL if the destination isn't odd, could happen with recursion }
if not odd(offset shr 1) then
bytes:=(bytes and $EB000000) or $EB000000;
bytes:=bytes or ((offset shr 2) and $ffffff);
bytes:=bytes or ((offset shr 1) and $1) shl 24;
end;
end;
end;
#$29: // SUB
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
{ set S if necessary }
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$2A:
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set opers }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
if opcode in [A_SSAT, A_SSAT16] then
bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
else
bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
bytes:=bytes or getsupreg(oper[2]^.reg);
if (ops>3) and
(oper[3]^.typ=top_shifterop) and
(oper[3]^.shifterop^.rs=NR_NO) then
begin
bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
if oper[3]^.shifterop^.shiftmode=SM_ASR then
bytes:=bytes or (1 shl 6)
else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
Message1(asmw_e_invalid_opcode_and_operands,GetString);
end;
end;
#$2B: // SETEND
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set endian specifier }
bytes:=bytes or ((oper[0]^.val and 1) shl 9);
end;
#$2C: // MOVW
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
{ set imm }
bytes:=bytes or (oper[1]^.val and $FFF);
bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
end;
#$2D: // BFX
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=3 then
begin
msb:=(oper[1]^.val+oper[2]^.val-1);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
{ set immediates }
bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
bytes:=bytes or ((msb and $1F) shl 16);
end
else
begin
if opcode in [A_BFC,A_BFI] then
msb:=(oper[2]^.val+oper[3]^.val-1)
else
msb:=oper[3]^.val-1;
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or getsupreg(oper[1]^.reg);
{ set immediates }
bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
bytes:=bytes or ((msb and $1F) shl 16);
end;
end;
#$2E: // Cache stuff
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set code }
bytes:=bytes or (oper[0]^.val and $F);
end;
#$2F: // Nop
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
end;
#$30: // Shifts
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set destination }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or getsupreg(oper[1]^.reg);
if ops>2 then
begin
{ set shift }
if oper[2]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
else
bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
end;
{ set S if necessary }
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$31: // BKPT
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 0);
{ set imm }
bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
bytes:=bytes or (oper[0]^.val and $F);
end;
#$32: // CLZ/REV
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or getsupreg(oper[1]^.reg);
end;
#$33:
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
if oper[1]^.typ=top_ref then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[1]^.ref^.offset;
if offset>=0 then
begin
{ set U flag }
bytes:=bytes or (1 shl 23);
bytes:=bytes or offset
end
else
begin
bytes:=bytes or (1 shl 22);
offset:=-offset;
bytes:=bytes or offset
end;
end
else
begin
if is_shifter_const(oper[1]^.val,r) then
begin
setshifterop(1);
bytes:=bytes or (1 shl 23);
end
else
begin
bytes:=bytes or (1 shl 22);
oper[1]^.val:=-oper[1]^.val;
setshifterop(1);
end;
end;
end;
#$40,#$90: // VMOV
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set regs }
Rd:=0;
Rn:=0;
Rm:=0;
case oppostfix of
PF_None:
begin
if ops=4 then
begin
if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
(getregtype(oper[2]^.reg)=R_INTREGISTER) then
begin
Rd:=getmmreg(oper[0]^.reg);
Rm:=getsupreg(oper[2]^.reg);
Rn:=getsupreg(oper[3]^.reg);
end
else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
(getregtype(oper[2]^.reg)=R_MMREGISTER) then
begin
Rm:=getsupreg(oper[0]^.reg);
Rn:=getsupreg(oper[1]^.reg);
Rd:=getmmreg(oper[2]^.reg);
end
else
message(asmw_e_invalid_opcode_and_operands);
bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
bytes:=bytes or ((Rd and $1) shl 5);
bytes:=bytes or (Rm shl 12);
bytes:=bytes or (Rn shl 16);
end
else if ops=3 then
begin
if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
(getregtype(oper[1]^.reg)=R_INTREGISTER) then
begin
Rd:=getmmreg(oper[0]^.reg);
Rm:=getsupreg(oper[1]^.reg);
Rn:=getsupreg(oper[2]^.reg);
end
else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
(getregtype(oper[2]^.reg)=R_MMREGISTER) then
begin
Rm:=getsupreg(oper[0]^.reg);
Rn:=getsupreg(oper[1]^.reg);
Rd:=getmmreg(oper[2]^.reg);
end
else
message(asmw_e_invalid_opcode_and_operands);
bytes:=bytes or ((Rd and $F) shl 0);
bytes:=bytes or ((Rd and $10) shl 1);
bytes:=bytes or (Rm shl 12);
bytes:=bytes or (Rn shl 16);
end
else if ops=2 then
begin
if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
(getregtype(oper[1]^.reg)=R_INTREGISTER) then
begin
Rd:=getmmreg(oper[0]^.reg);
Rm:=getsupreg(oper[1]^.reg);
end
else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
(getregtype(oper[1]^.reg)=R_MMREGISTER) then
begin
Rm:=getsupreg(oper[0]^.reg);
Rd:=getmmreg(oper[1]^.reg);
end
else
message(asmw_e_invalid_opcode_and_operands);
bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
bytes:=bytes or ((Rd and $1) shl 7);
bytes:=bytes or (Rm shl 12);
end;
end;
PF_F32:
begin
if (getregtype(oper[0]^.reg)<>R_MMREGISTER) then
Message(asmw_e_invalid_opcode_and_operands);
case oper[1]^.typ of
top_realconst:
begin
if not(IsVFPFloatImmediate(s32real,oper[1]^.val_real)) then
Message(asmw_e_invalid_opcode_and_operands);
singlerec.value:=oper[1]^.val_real;
singlerec:=tcompsinglerec(NtoLE(DWord(singlerec)));
bytes:=bytes or ((singlerec.bytes[2] shr 3) and $f);
bytes:=bytes or (DWord((singlerec.bytes[2] shr 7) and $1) shl 16) or (DWord(singlerec.bytes[3] and $3) shl 17) or (DWord((singlerec.bytes[3] shr 7) and $1) shl 19);
end;
top_reg:
begin
if getregtype(oper[1]^.reg)<>R_MMREGISTER then
Message(asmw_e_invalid_opcode_and_operands);
Rm:=getmmreg(oper[1]^.reg);
bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
bytes:=bytes or ((Rm and $1) shl 5);
end;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
Rd:=getmmreg(oper[0]^.reg);
bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
bytes:=bytes or ((Rd and $1) shl 22);
end;
PF_F64:
begin
if (getregtype(oper[0]^.reg)<>R_MMREGISTER) then
Message(asmw_e_invalid_opcode_and_operands);
case oper[1]^.typ of
top_realconst:
begin
if not(IsVFPFloatImmediate(s64real,oper[1]^.val_real)) then
Message(asmw_e_invalid_opcode_and_operands);
doublerec.value:=oper[1]^.val_real;
doublerec:=tcompdoublerec(NtoLE(QWord(doublerec)));
// 32c: eeb41b00 vmov.f64 d1, #64 ; 0x40
// 32c: eeb61b00 vmov.f64 d1, #96 ; 0x60
bytes:=bytes or (doublerec.bytes[6] and $f);
bytes:=bytes or (DWord((doublerec.bytes[6] shr 4) and $7) shl 16) or (DWord((doublerec.bytes[7] shr 7) and $1) shl 19);
end;
top_reg:
begin
if getregtype(oper[1]^.reg)<>R_MMREGISTER then
Message(asmw_e_invalid_opcode_and_operands);
Rm:=getmmreg(oper[1]^.reg);
bytes:=bytes or (Rm and $F);
bytes:=bytes or ((Rm and $10) shl 1);
end;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
Rd:=getmmreg(oper[0]^.reg);
bytes:=bytes or (1 shl 8);
bytes:=bytes or ((Rd and $F) shl 12);
bytes:=bytes or (((Rd and $10) shr 4) shl 22);
end;
end;
end;
#$41,#$91: // VMRS/VMSR
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set regs }
if (opcode=A_VMRS) or
(opcode=A_FMRX) then
begin
case oper[1]^.reg of
NR_FPSID: Rn:=$0;
NR_FPSCR: Rn:=$1;
NR_MVFR1: Rn:=$6;
NR_MVFR0: Rn:=$7;
NR_FPEXC: Rn:=$8;
else
Rn:=0;
message(asmw_e_invalid_opcode_and_operands);
end;
bytes:=bytes or (Rn shl 16);
if oper[0]^.reg=NR_APSR_nzcv then
bytes:=bytes or ($F shl 12)
else
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
end
else
begin
case oper[0]^.reg of
NR_FPSID: Rn:=$0;
NR_FPSCR: Rn:=$1;
NR_FPEXC: Rn:=$8;
else
Rn:=0;
message(asmw_e_invalid_opcode_and_operands);
end;
bytes:=bytes or (Rn shl 16);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
end;
end;
#$42,#$92: // VMUL
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set regs }
if ops=3 then
begin
Rd:=getmmreg(oper[0]^.reg);
Rn:=getmmreg(oper[1]^.reg);
Rm:=getmmreg(oper[2]^.reg);
end
else if ops=1 then
begin
Rd:=getmmreg(oper[0]^.reg);
Rn:=0;
Rm:=0;
end
else if oper[1]^.typ=top_const then
begin
Rd:=getmmreg(oper[0]^.reg);
Rn:=0;
Rm:=0;
end
else
begin
Rd:=getmmreg(oper[0]^.reg);
Rn:=0;
Rm:=getmmreg(oper[1]^.reg);
end;
if (oppostfix=PF_F32) or (insentry^.code[5]=#1) then
begin
D:=rd and $1; Rd:=Rd shr 1;
N:=rn and $1; Rn:=Rn shr 1;
M:=rm and $1; Rm:=Rm shr 1;
end
else
begin
D:=(rd shr 4) and $1; Rd:=Rd and $F;
N:=(rn shr 4) and $1; Rn:=Rn and $F;
M:=(rm shr 4) and $1; Rm:=Rm and $F;
bytes:=bytes or (1 shl 8);
end;
bytes:=bytes or (Rd shl 12);
bytes:=bytes or (Rn shl 16);
bytes:=bytes or (Rm shl 0);
bytes:=bytes or (D shl 22);
bytes:=bytes or (N shl 7);
bytes:=bytes or (M shl 5);
end;
#$43,#$93: // VCVT
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set regs }
Rd:=getmmreg(oper[0]^.reg);
Rm:=getmmreg(oper[1]^.reg);
if (ops=2) and
(oppostfix in [PF_F32F64,PF_F64F32]) then
begin
if oppostfix=PF_F32F64 then
begin
bytes:=bytes or (1 shl 8);
D:=rd and $1; Rd:=Rd shr 1;
M:=(rm shr 4) and $1; Rm:=Rm and $F;
end
else
begin
D:=(rd shr 4) and $1; Rd:=Rd and $F;
M:=rm and $1; Rm:=Rm shr 1;
end;
bytes:=bytes and $FFF0FFFF;
bytes:=bytes or ($7 shl 16);
bytes:=bytes or (Rd shl 12);
bytes:=bytes or (Rm shl 0);
bytes:=bytes or (D shl 22);
bytes:=bytes or (M shl 5);
end
else if (ops=2) and
(oppostfix=PF_None) then
begin
d:=0;
case getsubreg(oper[0]^.reg) of
R_SUBNONE:
rd:=getsupreg(oper[0]^.reg);
R_SUBFS:
begin
rd:=getmmreg(oper[0]^.reg);
d:=rd and 1;
rd:=rd shr 1;
end;
R_SUBFD:
begin
rd:=getmmreg(oper[0]^.reg);
d:=(rd shr 4) and 1;
rd:=rd and $F;
end;
end;
m:=0;
case getsubreg(oper[1]^.reg) of
R_SUBNONE:
rm:=getsupreg(oper[1]^.reg);
R_SUBFS:
begin
rm:=getmmreg(oper[1]^.reg);
m:=rm and 1;
rm:=rm shr 1;
end;
R_SUBFD:
begin
rm:=getmmreg(oper[1]^.reg);
m:=(rm shr 4) and 1;
rm:=rm and $F;
end;
end;
bytes:=bytes or (Rd shl 12);
bytes:=bytes or (Rm shl 0);
bytes:=bytes or (D shl 22);
bytes:=bytes or (M shl 5);
end
else if ops=2 then
begin
case oppostfix of
PF_S32F64,
PF_U32F64,
PF_F64S32,
PF_F64U32:
bytes:=bytes or (1 shl 8);
end;
if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
begin
case oppostfix of
PF_S32F64,
PF_S32F32:
bytes:=bytes or (1 shl 16);
end;
bytes:=bytes or (1 shl 18);
D:=rd and $1; Rd:=Rd shr 1;
if oppostfix in [PF_S32F64,PF_U32F64] then
begin
M:=(rm shr 4) and $1; Rm:=Rm and $F;
end
else
begin
M:=rm and $1; Rm:=Rm shr 1;
end;
end
else
begin
case oppostfix of
PF_F64S32,
PF_F32S32:
bytes:=bytes or (1 shl 7);
else
bytes:=bytes and $FFFFFF7F;
end;
M:=rm and $1; Rm:=Rm shr 1;
if oppostfix in [PF_F64S32,PF_F64U32] then
begin
D:=(rd shr 4) and $1; Rd:=Rd and $F;
end
else
begin
D:=rd and $1; Rd:=Rd shr 1;
end
end;
bytes:=bytes or (Rd shl 12);
bytes:=bytes or (Rm shl 0);
bytes:=bytes or (D shl 22);
bytes:=bytes or (M shl 5);
end
else
begin
if rd<>rm then
message(asmw_e_invalid_opcode_and_operands);
case oppostfix of
PF_S32F32,PF_U32F32,
PF_F32S32,PF_F32U32,
PF_S32F64,PF_U32F64,
PF_F64S32,PF_F64U32:
begin
if not (oper[2]^.val in [1..32]) then
message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
bytes:=bytes or (1 shl 7);
rn:=32;
end;
PF_S16F64,PF_U16F64,
PF_F64S16,PF_F64U16,
PF_S16F32,PF_U16F32,
PF_F32S16,PF_F32U16:
begin
if not (oper[2]^.val in [0..16]) then
message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
rn:=16;
end;
else
Rn:=0;
message(asmw_e_invalid_opcode_and_operands);
end;
case oppostfix of
PF_S16F64,PF_U16F64,
PF_S32F64,PF_U32F64,
PF_F64S16,PF_F64U16,
PF_F64S32,PF_F64U32:
begin
bytes:=bytes or (1 shl 8);
D:=(rd shr 4) and $1; Rd:=Rd and $F;
end;
else
begin
D:=rd and $1; Rd:=Rd shr 1;
end;
end;
case oppostfix of
PF_U16F64,PF_U16F32,
PF_U32F32,PF_U32F64,
PF_F64U16,PF_F32U16,
PF_F32U32,PF_F64U32:
bytes:=bytes or (1 shl 16);
end;
if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
bytes:=bytes or (1 shl 18);
bytes:=bytes or (Rd shl 12);
bytes:=bytes or (D shl 22);
rn:=rn-oper[2]^.val;
bytes:=bytes or ((rn and $1) shl 5);
bytes:=bytes or ((rn and $1E) shr 1);
end;
end;
#$44,#$94: // VLDM/VSTM/VPUSH/VPOP
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
{ set regs }
if ops=2 then
begin
if oper[0]^.typ=top_ref then
begin
Rn:=getsupreg(oper[0]^.ref^.index);
if oper[0]^.ref^.addressmode<>AM_OFFSET then
begin
{ set W }
bytes:=bytes or (1 shl 21);
end
else if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
end
else
begin
Rn:=getsupreg(oper[0]^.reg);
if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
end;
bytes:=bytes or (Rn shl 16);
{ Set PU bits }
case oppostfix of
PF_None,
PF_IA,PF_IAS,PF_IAD,PF_IAX:
bytes:=bytes or (1 shl 23);
PF_DB,PF_DBS,PF_DBD,PF_DBX:
bytes:=bytes or (2 shl 23);
end;
case oppostfix of
PF_IAX,PF_DBX,PF_FDX,PF_EAX:
begin
bytes:=bytes or (1 shl 8);
bytes:=bytes or (1 shl 0); // Offset is odd
end;
end;
dp_operation:=(oper[1]^.subreg=R_SUBFD);
if oper[1]^.regset^=[] then
message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
rd:=0;
for r:=0 to 31 do
if r in oper[1]^.regset^ then
begin
rd:=r;
break;
end;
rn:=32-rd;
for r:=rd+1 to 31 do
if not(r in oper[1]^.regset^) then
begin
rn:=r-rd;
break;
end;
if dp_operation then
begin
bytes:=bytes or (1 shl 8);
bytes:=bytes or (rn*2);
bytes:=bytes or ((rd and $F) shl 12);
bytes:=bytes or (((rd and $10) shr 4) shl 22);
end
else
begin
bytes:=bytes or rn;
bytes:=bytes or ((rd and $1) shl 22);
bytes:=bytes or (((rd and $1E) shr 1) shl 12);
end;
end
else { VPUSH/VPOP }
begin
dp_operation:=(oper[0]^.subreg=R_SUBFD);
if oper[0]^.regset^=[] then
message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
rd:=0;
for r:=0 to 31 do
if r in oper[0]^.regset^ then
begin
rd:=r;
break;
end;
rn:=32-rd;
for r:=rd+1 to 31 do
if not(r in oper[0]^.regset^) then
begin
rn:=r-rd;
break;
end;
if dp_operation then
begin
bytes:=bytes or (1 shl 8);
bytes:=bytes or (rn*2);
bytes:=bytes or ((rd and $F) shl 12);
bytes:=bytes or (((rd and $10) shr 4) shl 22);
end
else
begin
bytes:=bytes or rn;
bytes:=bytes or ((rd and $1) shl 22);
bytes:=bytes or (((rd and $1E) shr 1) shl 12);
end;
end;
end;
#$45,#$95: // VLDR/VSTR
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
{ set regs }
rd:=getmmreg(oper[0]^.reg);
if getsubreg(oper[0]^.reg)=R_SUBFD then
begin
bytes:=bytes or (1 shl 8);
bytes:=bytes or ((rd and $F) shl 12);
bytes:=bytes or (((rd and $10) shr 4) shl 22);
end
else
begin
bytes:=bytes or (((rd and $1E) shr 1) shl 12);
bytes:=bytes or ((rd and $1) shl 22);
end;
{ set ref }
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[1]^.ref^.offset;
offset:=offset div 4;
if offset>=0 then
begin
{ set U flag }
bytes:=bytes or (1 shl 23);
bytes:=bytes or offset
end
else
begin
offset:=-offset;
bytes:=bytes or offset
end;
end
else
message(asmw_e_invalid_opcode_and_operands);
end;
#$46: { System instructions }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
{ set regs }
if (oper[0]^.typ=top_modeflags) then
begin
if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 8);
if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
end;
if (ops=2) then
bytes:=bytes or (oper[1]^.val and $1F)
else if (ops=1) and
(oper[0]^.typ=top_const) then
bytes:=bytes or (oper[0]^.val and $1F);
end;
#$60: { Thumb }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
if (oper[1]^.typ=top_reg) then
bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
else
bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
end
else if ops=3 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
if (oper[2]^.typ=top_reg) then
bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
else
bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
end
else if ops=1 then
begin
if oper[0]^.typ=top_const then
bytes:=bytes or (oper[0]^.val and $FF);
end;
end;
#$61: { Thumb }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
end
else if ops=1 then
begin
if oper[0]^.typ=top_const then
bytes:=bytes or (oper[0]^.val and $FF);
end;
end;
#$62..#$63: { Thumb branches }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
if insentry^.code[0]=#$63 then
bytes:=bytes or (CondVal[condition] shl 8);
if oper[0]^.typ=top_const then
begin
if insentry^.code[0]=#$63 then
bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
else
bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
end
else if oper[0]^.typ=top_reg then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
end
else if oper[0]^.typ=top_ref then
begin
offset:=0;
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[0]^.ref^.offset;
if insentry^.code[0]=#$63 then
bytes:=bytes or (((offset+4) shr 1) and $FF)
else
bytes:=bytes or (((offset+4) shr 1) and $7FF);
end
end;
#$64: { Thumb: Special encodings }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
case opcode of
A_SUB:
begin
if (ops=3) and
(oper[2]^.typ=top_const) then
bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
else if (ops=2) and
(oper[1]^.typ=top_const) then
bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
end;
A_MUL:
if (ops in [2,3]) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
end;
A_ADD:
begin
if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
end
else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
(oper[2]^.typ=top_const) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
end
else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
(oper[2]^.typ=top_reg) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
end
else
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
end;
end;
end;
end;
#$65: { Thumb load/store }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
end;
#$66: { Thumb load/store }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-(insoffset+4) and (not longword(3));
offset:=(offset+oper[1]^.ref^.offset);
bytes:=bytes or (((offset shr ord(insentry^.code[3])) and $1F) shl 6);
end;
#$67: { Thumb load/store }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
if oper[1]^.typ=top_ref then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-(insoffset+4) and (not longword(3));
offset:=(offset+oper[1]^.ref^.offset);
bytes:=bytes or ((offset shr ord(insentry^.code[3])) and $FF);
end
else
bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
end;
#$68: { Thumb CB[N]Z }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
{ set opers }
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
if oper[1]^.typ=top_ref then
begin
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[1]^.ref^.offset;
offset:=offset div 2;
end
else
offset:=oper[1]^.val div 2;
bytes:=bytes or ((offset) and $1F) shl 3;
bytes:=bytes or ((offset shr 5) and 1) shl 9;
end;
#$69: { Thumb: Push/Pop/Stm/Ldm }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
case opcode of
A_PUSH:
begin
for r:=0 to 7 do
if r in oper[0]^.regset^ then
bytes:=bytes or (1 shl r);
if RS_R14 in oper[0]^.regset^ then
bytes:=bytes or (1 shl 8);
end;
A_POP:
begin
for r:=0 to 7 do
if r in oper[0]^.regset^ then
bytes:=bytes or (1 shl r);
if RS_R15 in oper[0]^.regset^ then
bytes:=bytes or (1 shl 8);
end;
A_STM:
begin
for r:=0 to 7 do
if r in oper[1]^.regset^ then
bytes:=bytes or (1 shl r);
if oper[0]^.typ=top_ref then
bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 8)
else
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
end;
A_LDM:
begin
for r:=0 to 7 do
if r in oper[1]^.regset^ then
bytes:=bytes or (1 shl r);
if oper[0]^.typ=top_ref then
bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 8)
else
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
end;
end;
end;
#$6A: { Thumb: IT }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or (ord(insentry^.code[2]) shl 0);
bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
i_field:=(bytes shr 4) and 1;
i_field:=(i_field shl 1) or i_field;
i_field:=(i_field shl 2) or i_field;
bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
end;
#$6B: { Thumb: Data processing (misc) }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
{ set regs }
if ops>=2 then
begin
if oper[1]^.typ=top_const then
begin
bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
bytes:=bytes or (oper[1]^.val and $FF);
end
else if oper[1]^.typ=top_reg then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
end;
end
else if ops=1 then
begin
if oper[0]^.typ=top_const then
bytes:=bytes or (oper[0]^.val and $FF);
end;
end;
#$6C: { Thumb: CPS }
begin
bytelen:=2;
bytes:=0;
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 8);
bytes:=bytes or ord(insentry^.code[2]);
if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 2);
if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 1);
if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 0);
end;
#$80: { Thumb-2: Dataprocessing }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=1 then
begin
if oper[0]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
else if oper[0]^.typ=top_const then
bytes:=bytes or (oper[0]^.val and $F);
end
else if (ops=2) and
(opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
if oper[1]^.typ=top_const then
encodethumbimm(oper[1]^.val)
else if oper[1]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
end
else if (ops=3) and
(opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
if oper[2]^.typ=top_shifterop then
setthumbshift(2)
else if oper[2]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
end
else if (ops=2) and
(opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
end
else if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
if oper[1]^.typ=top_const then
encodethumbimm(oper[1]^.val)
else if oper[1]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
end
else if ops=3 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
if oper[2]^.typ=top_const then
encodethumbimm(oper[2]^.val)
else if oper[2]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
end
else if ops=4 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
if oper[3]^.typ=top_shifterop then
setthumbshift(3)
else if oper[3]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
end;
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20)
else if oppostfix=PF_X then
bytes:=bytes or (1 shl 4)
else if oppostfix=PF_R then
bytes:=bytes or (1 shl 4);
end;
#$81: { Thumb-2: Dataprocessing misc }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=3 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
if oper[2]^.typ=top_const then
begin
bytes:=bytes or (oper[2]^.val and $FF);
bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
end;
end
else if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
offset:=0;
if oper[1]^.typ=top_const then
begin
offset:=oper[1]^.val;
end
else if oper[1]^.typ=top_ref then
begin
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[1]^.ref^.offset;
offset:=offset;
end;
bytes:=bytes or (offset and $FF);
bytes:=bytes or ((offset and $700) shr 8) shl 12;
bytes:=bytes or ((offset and $800) shr 11) shl 26;
bytes:=bytes or ((offset and $F000) shr 12) shl 16;
end;
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$82: { Thumb-2: Shifts }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
if oper[1]^.typ=top_reg then
begin
offset:=2;
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
end
else
begin
offset:=1;
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
end;
if oper[offset]^.typ=top_const then
begin
bytes:=bytes or (oper[offset]^.val and $3) shl 6;
bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
end
else if oper[offset]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
if (ops>=(offset+2)) and
(oper[offset+1]^.typ=top_const) then
bytes:=bytes or (oper[offset+1]^.val and $1F);
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$84: { Thumb-2: Shifts(width-1) }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
if oper[1]^.typ=top_reg then
begin
offset:=2;
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
end
else
offset:=1;
if oper[offset]^.typ=top_const then
begin
bytes:=bytes or (oper[offset]^.val and $3) shl 6;
bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
end;
if (ops>=(offset+2)) and
(oper[offset+1]^.typ=top_const) then
begin
if opcode in [A_BFI,A_BFC] then
i_field:=oper[offset+1]^.val+oper[offset]^.val-1
else
i_field:=oper[offset+1]^.val-1;
bytes:=bytes or (i_field and $1F);
end;
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20);
end;
#$83: { Thumb-2: Saturation }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (oper[1]^.val and $1F);
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
if ops=4 then
setthumbshift(3,true);
end;
#$85: { Thumb-2: Long multiplications }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=4 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
end;
if oppostfix=PF_S then
bytes:=bytes or (1 shl 20)
else if oppostfix=PF_X then
bytes:=bytes or (1 shl 4);
end;
#$86: { Thumb-2: Extension ops }
begin
bytes:=0;
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=2 then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
end
else if ops=3 then
begin
if oper[2]^.typ=top_shifterop then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
end
else
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
end;
end
else if ops=4 then
begin
if oper[3]^.typ=top_shifterop then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
end;
end;
end;
#$87: { Thumb-2: PLD/PLI }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=offset+oper[0]^.ref^.offset;
if offset>=0 then
begin
{ set U flag }
bytes:=bytes or (1 shl 23);
bytes:=bytes or (offset and $FFF);
end
else
begin
bytes:=bytes or ($3 shl 10);
offset:=-offset;
bytes:=bytes or (offset and $FF);
end;
end
else
begin
bytes:=bytes or getsupreg(oper[0]^.ref^.index);
{ set shift }
with oper[0]^.ref^ do
if shiftmode=SM_LSL then
bytes:=bytes or ((shiftimm and $1F) shl 4);
end;
end;
#$88: { Thumb-2: LDR/STR }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or (ord(insentry^.code[4]) shl 0);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
if offset>=0 then
begin
if (offset>255) and
(not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT])) then
bytes:=bytes or (1 shl 23);
{ set U flag }
if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
begin
bytes:=bytes or (1 shl 9);
bytes:=bytes or (1 shl 11);
end;
bytes:=bytes or offset
end
else
begin
bytes:=bytes or (1 shl 11);
offset:=-offset;
bytes:=bytes or offset
end;
end
else
begin
{ set I flag }
bytes:=bytes or (1 shl 25);
bytes:=bytes or getsupreg(oper[1]^.ref^.index);
{ set shift }
with oper[1]^.ref^ do
if shiftmode<>SM_None then
bytes:=bytes or ((shiftimm and $1F) shl 4);
end;
if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
begin
{ set W bit }
if oper[1]^.ref^.addressmode<>AM_OFFSET then
bytes:=bytes or (1 shl 8);
{ set P bit if necessary }
if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
bytes:=bytes or (1 shl 10);
end;
end;
#$89: { Thumb-2: LDRD/STRD }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or (ord(insentry^.code[4]) shl 0);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=(offset+oper[2]^.ref^.offset) div 4;
if offset>=0 then
begin
{ set U flag }
bytes:=bytes or (1 shl 23);
bytes:=bytes or offset
end
else
begin
offset:=-offset;
bytes:=bytes or offset
end;
end
else
begin
message(asmw_e_invalid_opcode_and_operands);
end;
{ set W bit }
if oper[2]^.ref^.addressmode<>AM_OFFSET then
bytes:=bytes or (1 shl 21);
{ set P bit if necessary }
if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
bytes:=bytes or (1 shl 24);
end;
#$8A: { Thumb-2: LDREX }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or (ord(insentry^.code[4]) shl 0);
{ set Rn and Rd }
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
if (ops=2) and (opcode in [A_LDREX]) then
begin
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=(offset+oper[1]^.ref^.offset) div 4;
if offset>=0 then
begin
bytes:=bytes or offset
end
else
begin
message(asmw_e_invalid_opcode_and_operands);
end;
end
else
begin
message(asmw_e_invalid_opcode_and_operands);
end;
end
else if (ops=2) then
begin
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
end
else
begin
bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
end;
end;
#$8B: { Thumb-2: STREX }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or (ord(insentry^.code[4]) shl 0);
{ set Rn and Rd }
if (ops=3) and (opcode in [A_STREX]) then
begin
bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
begin
{ set offset }
offset:=0;
currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
if assigned(currsym) then
offset:=currsym.offset-insoffset-8;
offset:=(offset+oper[2]^.ref^.offset) div 4;
if offset>=0 then
begin
bytes:=bytes or offset
end
else
begin
message(asmw_e_invalid_opcode_and_operands);
end;
end
else
begin
message(asmw_e_invalid_opcode_and_operands);
end;
end
else if (ops=3) then
begin
bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
end
else
begin
bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
end;
end;
#$8C: { Thumb-2: LDM/STM }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or (ord(insentry^.code[4]) shl 0);
if oper[0]^.typ=top_reg then
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
else
begin
bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
if oper[0]^.ref^.addressmode<>AM_OFFSET then
bytes:=bytes or (1 shl 21);
end;
for r:=0 to 15 do
if r in oper[1]^.regset^ then
bytes:=bytes or (1 shl r);
case oppostfix of
PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
end;
end;
#$8D: { Thumb-2: BL/BLX }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 8);
{ set offset }
if oper[0]^.typ=top_const then
offset:=(oper[0]^.val shr 1) and $FFFFFF
else
begin
currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
begin
objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
offset:=$FFFFFE
end
else
offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
end;
bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
bytes:=bytes or ((offset shr 23) and $1) shl 26;
end;
#$8E: { Thumb-2: TBB/TBH }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
{ set Rn and Rm }
bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
message(asmw_e_invalid_effective_address)
else
begin
bytes:=bytes or getsupreg(oper[0]^.ref^.index);
if (opcode=A_TBH) and
(oper[0]^.ref^.shiftmode<>SM_LSL) and
(oper[0]^.ref^.shiftimm<>1) then
message(asmw_e_invalid_effective_address);
end;
end;
#$8F: { Thumb-2: CPSxx }
begin
{ set opcode }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if (oper[0]^.typ=top_modeflags) then
begin
if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 5);
end;
if (ops=2) then
bytes:=bytes or (oper[1]^.val and $1F)
else if (ops=1) and
(oper[0]^.typ=top_const) then
bytes:=bytes or (oper[0]^.val and $1F);
end;
#$96: { Thumb-2: MSR/MRS }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if opcode=A_MRS then
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
case oper[1]^.reg of
NR_MSP: bytes:=bytes or $08;
NR_PSP: bytes:=bytes or $09;
NR_IPSR: bytes:=bytes or $05;
NR_EPSR: bytes:=bytes or $06;
NR_APSR: bytes:=bytes or $00;
NR_PRIMASK: bytes:=bytes or $10;
NR_BASEPRI: bytes:=bytes or $11;
NR_BASEPRI_MAX: bytes:=bytes or $12;
NR_FAULTMASK: bytes:=bytes or $13;
NR_CONTROL: bytes:=bytes or $14;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end
else
begin
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
case oper[0]^.reg of
NR_APSR,
NR_APSR_nzcvqg: bytes:=bytes or $C00;
NR_APSR_g: bytes:=bytes or $400;
NR_APSR_nzcvq: bytes:=bytes or $800;
NR_MSP: bytes:=bytes or $08;
NR_PSP: bytes:=bytes or $09;
NR_PRIMASK: bytes:=bytes or $10;
NR_BASEPRI: bytes:=bytes or $11;
NR_BASEPRI_MAX: bytes:=bytes or $12;
NR_FAULTMASK: bytes:=bytes or $13;
NR_CONTROL: bytes:=bytes or $14;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end;
end;
#$A0: { FPA: CPDT(LDF/STF) }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or (ord(insentry^.code[3]) shl 8);
bytes:=bytes or ord(insentry^.code[4]);
if ops=2 then
begin
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
if oper[1]^.ref^.offset>=0 then
bytes:=bytes or (1 shl 23);
if oper[1]^.ref^.addressmode<>AM_OFFSET then
bytes:=bytes or (1 shl 21);
if oper[1]^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 24);
case oppostfix of
PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
end;
end
else
begin
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
case oper[1]^.val of
1: bytes:=bytes or (1 shl 15);
2: bytes:=bytes or (1 shl 22);
3: bytes:=bytes or (1 shl 22) or (1 shl 15);
4: ;
else
message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
end;
bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
if oper[2]^.ref^.offset>=0 then
bytes:=bytes or (1 shl 23);
if oper[2]^.ref^.addressmode<>AM_OFFSET then
bytes:=bytes or (1 shl 21);
if oper[2]^.ref^.addressmode=AM_PREINDEXED then
bytes:=bytes or (1 shl 24);
end;
end;
#$A1: { FPA: CPDO }
begin
{ set instruction code }
bytes:=bytes or ($E shl 24);
bytes:=bytes or (ord(insentry^.code[1]) shl 15);
bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
bytes:=bytes or (1 shl 8);
bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
if ops=2 then
begin
if oper[1]^.typ=top_reg then
bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
else
case oper[1]^.val of
0: bytes:=bytes or $8;
1: bytes:=bytes or $9;
2: bytes:=bytes or $A;
3: bytes:=bytes or $B;
4: bytes:=bytes or $C;
5: bytes:=bytes or $D;
//0.5: bytes:=bytes or $E;
10: bytes:=bytes or $F;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end
else
begin
bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
if oper[2]^.typ=top_reg then
bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
else
case oper[2]^.val of
0: bytes:=bytes or $8;
1: bytes:=bytes or $9;
2: bytes:=bytes or $A;
3: bytes:=bytes or $B;
4: bytes:=bytes or $C;
5: bytes:=bytes or $D;
//0.5: bytes:=bytes or $E;
10: bytes:=bytes or $F;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end;
case roundingmode of
RM_P: bytes:=bytes or (1 shl 5);
RM_M: bytes:=bytes or (2 shl 5);
RM_Z: bytes:=bytes or (3 shl 5);
end;
case oppostfix of
PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
else
message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
end;
end;
#$A2: { FPA: CPDO }
begin
{ set instruction code }
bytes:=bytes or (ord(insentry^.code[1]) shl 24);
bytes:=bytes or (ord(insentry^.code[2]) shl 16);
bytes:=bytes or ($11 shl 4);
case opcode of
A_FLT:
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
case roundingmode of
RM_P: bytes:=bytes or (1 shl 5);
RM_M: bytes:=bytes or (2 shl 5);
RM_Z: bytes:=bytes or (3 shl 5);
end;
case oppostfix of
PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
else
message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
end;
end;
A_FIX:
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
case roundingmode of
RM_P: bytes:=bytes or (1 shl 5);
RM_M: bytes:=bytes or (2 shl 5);
RM_Z: bytes:=bytes or (3 shl 5);
end;
end;
A_WFS,A_RFS,A_WFC,A_RFC:
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
end;
A_CMF,A_CNF,A_CMFE,A_CNFE:
begin
bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
if oper[1]^.typ=top_reg then
bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
else
case oper[1]^.val of
0: bytes:=bytes or $8;
1: bytes:=bytes or $9;
2: bytes:=bytes or $A;
3: bytes:=bytes or $B;
4: bytes:=bytes or $C;
5: bytes:=bytes or $D;
//0.5: bytes:=bytes or $E;
10: bytes:=bytes or $F;
else
Message(asmw_e_invalid_opcode_and_operands);
end;
end;
end;
end;
#$fe: // No written data
begin
exit;
end;
#$ff:
internalerror(2005091101);
else
begin
writeln(ord(insentry^.code[0]), ' - ', opcode);
internalerror(2005091102);
end;
end;
{ Todo: Decide whether the code above should take care of writing data in an order that makes senes }
if (insentry^.code[0] in [#$80..#$96]) and (bytelen=4) then
bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
{ we're finished, write code }
objdata.writebytes(bytes,bytelen);
end;
begin
cai_align:=tai_align;
end.
|