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 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121
|
;;; cpnanopass.ss
;;; Copyright 1984-2017 Cisco Systems, Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
(let ()
(include "np-languages.ss")
(define track-dynamic-closure-counts ($make-thread-parameter #f (lambda (x) (and x #t))))
(define track-static-closure-counts
($make-thread-parameter
#f
(lambda (x)
(include "types.ss")
(cond
[(or (not x) (static-closure-info? x)) x]
[(eq? x #t) (make-static-closure-info)]
[else ($oops '$trace-static-closure-counts "~s is not a static-closure-info record or #f" x)]))))
(module ()
(include "types.ss")
(set-who! $dynamic-closure-counts
(lambda ()
(vector
(profile-counter-count #{raw-ref-count bhowt6w0coxl0s2y-1})
(profile-counter-count #{raw-create-count bhowt6w0coxl0s2y-2})
(profile-counter-count #{raw-alloc-count bhowt6w0coxl0s2y-3})
(profile-counter-count #{ref-count bhowt6w0coxl0s2y-4})
(profile-counter-count #{pair-create-count bhowt6w0coxl0s2y-5})
(profile-counter-count #{vector-create-count bhowt6w0coxl0s2y-6})
(profile-counter-count #{vector-alloc-count bhowt6w0coxl0s2y-8})
(profile-counter-count #{padded-vector-alloc-count bhowt6w0coxl0s2y-11})
(profile-counter-count #{closure-create-count bhowt6w0coxl0s2y-7})
(profile-counter-count #{closure-alloc-count bhowt6w0coxl0s2y-9})
(profile-counter-count #{padded-closure-alloc-count bhowt6w0coxl0s2y-10}))))
(set-who! $clear-dynamic-closure-counts
(lambda ()
(profile-counter-count-set! #{raw-ref-count bhowt6w0coxl0s2y-1} 0)
(profile-counter-count-set! #{raw-create-count bhowt6w0coxl0s2y-2} 0)
(profile-counter-count-set! #{raw-alloc-count bhowt6w0coxl0s2y-3} 0)
(profile-counter-count-set! #{ref-count bhowt6w0coxl0s2y-4} 0)
(profile-counter-count-set! #{pair-create-count bhowt6w0coxl0s2y-5} 0)
(profile-counter-count-set! #{vector-create-count bhowt6w0coxl0s2y-6} 0)
(profile-counter-count-set! #{vector-alloc-count bhowt6w0coxl0s2y-8} 0)
(profile-counter-count-set! #{padded-vector-alloc-count bhowt6w0coxl0s2y-11} 0)
(profile-counter-count-set! #{closure-create-count bhowt6w0coxl0s2y-7} 0)
(profile-counter-count-set! #{closure-alloc-count bhowt6w0coxl0s2y-9} 0)
(profile-counter-count-set! #{padded-closure-alloc-count bhowt6w0coxl0s2y-10} 0))))
(define-syntax traceit
(syntax-rules (x)
[(_ name) (set! name (let ([t name]) (trace-lambda name args (apply t args))))]))
(define-syntax architecture
(let ([fn (format "~a.ss" (constant architecture))])
(with-source-path 'architecture fn
(lambda (fn)
(let* ([p ($open-file-input-port 'include fn)]
[sfd ($source-file-descriptor fn p)]
[p (transcoded-port p (current-transcoder))])
(let ([do-read ($make-read p sfd 0)])
(let* ([regs (do-read)] [inst (do-read)] [asm (do-read)])
(when (eof-object? asm) ($oops #f "too few expressions in ~a" fn))
(unless (eof-object? (do-read)) ($oops #f "too many expressions in ~a" fn))
(close-input-port p)
(lambda (x)
(syntax-case x (registers instructions assembler)
[(k registers) (datum->syntax #'k regs)]
[(k instructions) (datum->syntax #'k inst)]
[(k assembler) (datum->syntax #'k asm)])))))))))
; version in cmacros uses keyword as template and should
; probably be changed to use the id
(define-syntax define-who
(lambda (x)
(syntax-case x ()
[(_ (id . args) b1 b2 ...)
(identifier? #'id)
#'(define-who id (lambda args b1 b2 ...))]
[(_ id e)
(identifier? #'id)
(with-implicit (id who)
#'(define id (let ([who 'id]) e)))])))
(module (get-passes pass xpass pass-time?)
(define-syntax passes-loc (make-compile-time-value (box '())))
(define-syntax get-passes
(lambda (x)
(lambda (r)
(syntax-case x ()
[(_) #`(unbox (quote #,(datum->syntax #'* (r #'passes-loc))))]))))
(module (pass)
(define ir-printer
(lambda (unparser)
(lambda (val*)
(safe-assert (not (null? val*)))
(pretty-print (flatten-seq (unparser (car val*)))))))
(define values-printer
(lambda (val*)
(if (null? val*)
(printf "no output\n")
(pretty-print (car val*)))))
(define-syntax pass
(syntax-rules ()
[(_ (pass-name ?arg ...) ?unparser)
(identifier? #'pass-name)
(let ([pass-name (pass-name ?arg ...)])
(lambda args (xpass pass-name (ir-printer ?unparser) args)))]
[(_ pass-name ?unparser)
(identifier? #'pass-name)
(lambda args (xpass pass-name (ir-printer ?unparser) args))]
[(_ (pass-name ?arg ...))
(identifier? #'pass-name)
(let ([pass-name (pass-name ?arg ...)])
(lambda args (xpass pass-name values-printer args)))]
[(_ pass-name)
(identifier? #'pass-name)
(lambda args (xpass pass-name values-printer args))])))
(module (xpass pass-time?)
(define-threaded pass-time?)
(define $xpass
(lambda (printer pass-name pass arg*)
(let-values ([val* (let ([th (lambda () (apply pass arg*))])
(if pass-time? ($pass-time pass-name th) (th)))])
(when (memq pass-name (tracer))
(printf "output of ~s:\n" pass-name)
(printer val*))
(apply values val*))))
(define-syntax xpass
(lambda (x)
(syntax-case x ()
[(_ pass-name ?printer ?args)
(lambda (r)
(let ([loc (r #'passes-loc)])
(set-box! loc (cons (datum pass-name) (unbox loc))))
#`($xpass ?printer 'pass-name pass-name ?args))]))))
(define flatten-seq
(lambda (x)
(define helper
(lambda (x*)
(if (null? x*)
'()
(let ([x (car x*)])
(if (and (pair? x) (eq? (car x) 'seq))
(append (helper (cdr x)) (helper (cdr x*)))
(cons (flatten-seq x) (helper (cdr x*))))))))
(cond
[(null? x) '()]
[(and (pair? x) (eq? (car x) 'seq))
(let ([x* (helper (cdr x))])
(if (fx= (length x*) 1)
(car x*)
(cons 'begin x*)))]
[(and (pair? x) (eq? (car x) 'quote)) x]
[(list? x) (map flatten-seq x)]
[else x]))))
(define compose
(lambda (v p . p*)
(let loop ([v* (list v)] [p p] [p* p*])
(if (null? p*)
(apply p v*)
(let-values ([v* (apply p v*)])
(loop v* (car p*) (cdr p*)))))))
(define-syntax with-virgin-quasiquote
(lambda (x)
(syntax-case x ()
[(k e1 e2 ...)
#`(let-syntax ([#,(datum->syntax #'k 'quasiquote)
(syntax-rules () [(_ x) `x])])
e1 e2 ...)])))
(define valid-pass?
(lambda (x)
(memq x (get-passes))))
(define last-pass ; potentially not thread-safe, but currently unused
(make-parameter #f
(lambda (x)
(unless (or (eq? x #f) (valid-pass? x))
(errorf 'last-pass "~s is not a valid pass" x))
x)))
(define tracer ; potentially not thread-safe, but currently unused
(let ([ls '()])
(case-lambda
[() ls]
[(x)
(cond
[(or (null? x) (not x)) (set! ls '())]
[(eq? x #t) (set! ls (get-passes))]
[(valid-pass? x) (set! ls (cons x ls))]
[(list? x) (for-each tracer x)]
[else (errorf 'tracer "invalid trace list or pass name: ~s" x)])])))
(define maybe-cons
(lambda (x ls)
(if x (cons x ls) ls)))
(define unannotate
(lambda (x)
(if (annotation? x)
(annotation-expression x)
x)))
(let ()
(import (nanopass) np-languages)
(define signed-32?
(let ([n (bitwise-arithmetic-shift-left 1 (fx- 32 1))])
(let ([low (- n)] [high (- n 1)])
(if (fixnum? low)
(lambda (x) (and (fixnum? x) (fx<= low x high)))
(lambda (x) (or (fixnum? x) (<= low x high)))))))
(define nodups
(lambda x**
(let ([x* (apply append x**)])
(let ([ans (andmap (lambda (x) (and (not (uvar-seen? x)) (uvar-seen! x #t) #t)) x*)])
(for-each (lambda (x) (uvar-seen! x #f)) x*)
ans))))
(define chunked-bytevector-bitcount
; assumes "chunked" bytevector a multiple of 2 in size
(let ([bitcount-bv (make-bytevector #x10000)])
(do ([i 0 (fx+ i 1)])
((fx= i #x10000))
(bytevector-u8-set! bitcount-bv i (fxbit-count i)))
(lambda (bv)
(let loop ([n (bytevector-length bv)] [count 0])
(if (fx= n 0)
count
(let ([n (fx- n 2)])
(loop n (fx+ (bytevector-u8-ref bitcount-bv
(bytevector-u16-native-ref bv n))
count))))))))
(module (empty-tree full-tree tree-extract tree-for-each tree-fold-left tree-bit-set? tree-bit-set tree-bit-unset tree-bit-count tree-same? tree-merge)
; tree -> fixnum | (tree-node tree tree)
; 0 represents any tree or subtree with no bits set, and a tree or subtree
; with no bits set is always 0
(define empty-tree 0)
; any tree or subtree with all bits set
(define full-tree #t)
(define (full-fixnum size) (fxsrl (most-positive-fixnum) (fx- (fx- (fixnum-width) 1) size)))
(define compute-split
(lambda (size)
(fxsrl size 1)
; 2015/03/15 rkd: tried the following under the theory that we'd allocate
; fewer nodes. for example, say fixmun-width is 30 and size is 80. if we
; split 40/40 we create two nodes under the current node. if instead we
; split 29/51 we create just one node and one fixnum under the current
; node. this worked as planned; however, it reduced the number of nodes
; created by only 3.3% on the x86 and made compile times slightly worse.
#;(if (fx<= size (fx* (fx- (fixnum-width) 1) 3)) (fx- (fixnum-width) 1) (fxsrl size 1))))
(meta-cond
[(fx= (optimize-level) 3)
(module (make-tree-node tree-node? tree-node-left tree-node-right)
(define make-tree-node cons)
(define tree-node? pair?)
(define tree-node-left car)
(define tree-node-right cdr))]
[else
(module (make-tree-node tree-node? tree-node-left tree-node-right)
(define-record-type tree-node
(nongenerative)
(sealed #t)
(fields left right)
(protocol
(lambda (new)
(lambda (left right)
(new left right)))))
(record-writer (record-type-descriptor tree-node)
(lambda (r p wr)
(define tree-node->s-exp
(lambda (tn)
(with-virgin-quasiquote
(let ([left (tree-node-left tn)] [right (tree-node-right tn)])
`(tree-node
,(if (tree-node? left) (tree-node->s-exp left) left)
,(if (tree-node? right) (tree-node->s-exp right) right))))))
(wr (tree-node->s-exp r) p))))])
(define tree-extract ; assumes empty-tree is 0
(lambda (st size v)
(let extract ([st st] [size size] [offset 0] [x* '()])
(cond
[(fixnum? st)
(do ([st st (fxsrl st 1)]
[offset offset (fx+ offset 1)]
[x* x* (if (fxodd? st) (cons (vector-ref v offset) x*) x*)])
((fx= st 0) x*))]
[(eq? st full-tree)
(do ([size size (fx- size 1)]
[offset offset (fx+ offset 1)]
[x* x* (cons (vector-ref v offset) x*)])
((fx= size 0) x*))]
[else
(let ([split (compute-split size)])
(extract (tree-node-right st) (fx- size split) (fx+ offset split)
(extract (tree-node-left st) split offset x*)))]))))
(define tree-for-each ; assumes empty-tree is 0
(lambda (st size start end action)
(let f ([st st] [size size] [start start] [end end] [offset 0])
(cond
[(fixnum? st)
(unless (eq? st empty-tree)
(do ([st (fxbit-field st start end) (fxsrl st 1)] [offset (fx+ offset start) (fx+ offset 1)])
((fx= st 0))
(when (fxodd? st) (action offset))))]
[(eq? st full-tree)
(do ([start start (fx+ start 1)] [offset offset (fx+ offset 1)])
((fx= start end))
(action offset))]
[else
(let ([split (compute-split size)])
(when (fx< start split)
(f (tree-node-left st) split start (fxmin end split) offset))
(when (fx> end split)
(f (tree-node-right st) (fx- size split) (fxmax (fx- start split) 0) (fx- end split) (fx+ offset split))))]))))
(define tree-fold-left ; assumes empty-tree is 0
(lambda (proc size init st)
(let f ([st st] [size size] [offset 0] [init init])
(cond
[(fixnum? st)
(do ([st st (fxsrl st 1)]
[offset offset (fx+ offset 1)]
[init init (if (fxodd? st) (proc init offset) init)])
((fx= st 0) init))]
[(eq? st full-tree)
(do ([size size (fx- size 1)]
[offset offset (fx+ offset 1)]
[init init (proc init offset)])
((fx= size 0) init))]
[else
(let ([split (compute-split size)])
(f (tree-node-left st) split offset
(f (tree-node-right st) (fx- size split) (fx+ offset split) init)))]))))
(define tree-bit-set? ; assumes empty-tree is 0
(lambda (st size bit)
(let loop ([st st] [size size] [bit bit])
(cond
[(fixnum? st)
(and (not (eqv? st empty-tree))
; fxlogbit? is unnecessarily general, so roll our own
(fxlogtest st (fxsll 1 bit)))]
[(eq? st full-tree) #t]
[else
(let ([split (compute-split size)])
(if (fx< bit split)
(loop (tree-node-left st) split bit)
(loop (tree-node-right st) (fx- size split) (fx- bit split))))]))))
(define tree-bit-set ; assumes empty-tree is 0
(lambda (st size bit)
; set bit in tree. result is eq? to tr if result is same as tr.
(cond
[(eq? st full-tree) st]
[(fx< size (fixnum-width))
(let ([st (fxlogbit1 bit st)])
(if (fx= st (full-fixnum size))
full-tree
st))]
[else
(let ([split (compute-split size)])
(if (eqv? st empty-tree)
(if (fx< bit split)
(make-tree-node (tree-bit-set empty-tree split bit) empty-tree)
(make-tree-node empty-tree (tree-bit-set empty-tree (fx- size split) (fx- bit split))))
(let ([lst (tree-node-left st)] [rst (tree-node-right st)])
(if (fx< bit split)
(let ([new-lst (tree-bit-set lst split bit)])
(if (eq? new-lst lst)
st
(if (and (eq? new-lst full-tree) (eq? rst full-tree))
full-tree
(make-tree-node new-lst rst))))
(let ([new-rst (tree-bit-set rst (fx- size split) (fx- bit split))])
(if (eq? new-rst rst)
st
(if (and (eq? lst full-tree) (eq? new-rst full-tree))
full-tree
(make-tree-node lst new-rst))))))))])))
(define tree-bit-unset ; assumes empty-tree is 0
(lambda (st size bit)
; reset bit in tree. result is eq? to tr if result is same as tr.
(cond
[(fixnum? st)
(if (eqv? st empty-tree)
empty-tree
(fxlogbit0 bit st))]
[(eq? st full-tree)
(if (fx< size (fixnum-width))
(fxlogbit0 bit (full-fixnum size))
(let ([split (compute-split size)])
(if (fx< bit split)
(make-tree-node (tree-bit-unset full-tree split bit) full-tree)
(make-tree-node full-tree (tree-bit-unset full-tree (fx- size split) (fx- bit split))))))]
[else
(let ([split (compute-split size)] [lst (tree-node-left st)] [rst (tree-node-right st)])
(if (fx< bit split)
(let ([new-lst (tree-bit-unset lst split bit)])
(if (eq? new-lst lst)
st
(if (and (eq? new-lst empty-tree) (eq? rst empty-tree))
empty-tree
(make-tree-node new-lst rst))))
(let ([new-rst (tree-bit-unset rst (fx- size split) (fx- bit split))])
(if (eq? new-rst rst)
st
(if (and (eq? lst empty-tree) (eq? new-rst empty-tree))
empty-tree
(make-tree-node lst new-rst))))))])))
(define tree-bit-count ; assumes empty-tree is 0
(lambda (st size)
(cond
[(fixnum? st) (fxbit-count st)]
[(eq? st full-tree) size]
[else
(let ([split (compute-split size)])
(fx+
(tree-bit-count (tree-node-left st) split)
(tree-bit-count (tree-node-right st) (fx- size split))))])))
(define tree-same? ; assumes empty-tree is 0
(lambda (st1 st2)
(or (eq? st1 st2) ; assuming fixnums and full trees are eq-comparable
(and (tree-node? st1)
(tree-node? st2)
(tree-same? (tree-node-left st1) (tree-node-left st2))
(tree-same? (tree-node-right st1) (tree-node-right st2))))))
(define tree-merge
; merge tr1 and tr2. result is eq? to tr1 if result is same as tr1.
(lambda (st1 st2 size)
(cond
[(or (eq? st1 st2) (eq? st2 empty-tree)) st1]
[(eq? st1 empty-tree) st2]
[(or (eq? st1 full-tree) (eq? st2 full-tree)) full-tree]
[(fixnum? st1)
(safe-assert (fixnum? st2))
(let ([st (fxlogor st1 st2)])
(if (fx= st (full-fixnum size))
full-tree
st))]
[else
(let ([lst1 (tree-node-left st1)]
[rst1 (tree-node-right st1)]
[lst2 (tree-node-left st2)]
[rst2 (tree-node-right st2)])
(let ([split (compute-split size)])
(let ([l (tree-merge lst1 lst2 split)] [r (tree-merge rst1 rst2 (fx- size split))])
(cond
[(and (eq? l lst1) (eq? r rst1)) st1]
[(and (eq? l lst2) (eq? r rst2)) st2]
[(and (eq? l full-tree) (eq? r full-tree)) full-tree]
[else (make-tree-node l r)]))))]))))
(define-syntax tc-disp
(lambda (x)
(syntax-case x ()
[(_ name)
(case (datum name)
[(%ac0) (constant tc-ac0-disp)]
[(%ac1) (constant tc-ac1-disp)]
[(%sfp) (constant tc-sfp-disp)]
[(%cp) (constant tc-cp-disp)]
[(%esp) (constant tc-esp-disp)]
[(%ap) (constant tc-ap-disp)]
[(%eap) (constant tc-eap-disp)]
[(%trap) (constant tc-trap-disp)]
[(%xp) (constant tc-xp-disp)]
[(%yp) (constant tc-yp-disp)]
[else #f])])))
(define-syntax define-reserved-registers
(lambda (x)
(syntax-case x ()
[(_ [regid alias ... callee-save? mdinfo] ...)
(syntax-case #'(regid ...) (%tc %sfp) [(%tc %sfp . others) #t] [_ #f])
#'(begin
(begin
(define regid (make-reg 'regid 'mdinfo (tc-disp regid) callee-save?))
(module (alias ...) (define x regid) (define alias x) ...))
...)])))
(define-syntax define-allocable-registers
(lambda (x)
(assert (fx<= (constant asm-arg-reg-cnt) (constant asm-arg-reg-max)))
(syntax-case x ()
[(_ regvec arg-registers extra-registers with-initialized-registers [regid reg-alias ... callee-save? mdinfo] ...)
(with-syntax ([((tc-disp ...) (arg-regid ...) (extra-regid ...))
(syntax-case #'(regid ...) (%ac0 %xp %ts %td)
[(%ac0 %xp %ts %td other ...)
(let f ([other* #'(other ...)]
[rtc-disp* '()]
[arg-offset (constant tc-arg-regs-disp)]
[rextra* '()])
(if (null? other*)
(if (fx= (length rextra*) (constant asm-arg-reg-max))
(let ([extra* (reverse rextra*)])
(list
(list*
(constant tc-ac0-disp)
(constant tc-xp-disp)
(constant tc-ts-disp)
(constant tc-td-disp)
(reverse rtc-disp*))
(list-head extra* (constant asm-arg-reg-cnt))
(list-tail extra* (constant asm-arg-reg-cnt))))
(syntax-error x (format "asm-arg-reg-max extra registers are not specified ~s" (syntax->datum rextra*))))
(let ([other (car other*)])
(if (memq (syntax->datum other) '(%ac1 %yp %cp %ret))
(f (cdr other*) (cons #`(tc-disp #,other) rtc-disp*)
arg-offset rextra*)
(f (cdr other*) (cons arg-offset rtc-disp*)
(fx+ arg-offset (constant ptr-bytes)) (cons other rextra*))))))]
[_ (syntax-error x "missing or out-of-order required registers")])]
[(regid-loc ...) (generate-temporaries #'(regid ...))])
#'(begin
(define-syntax define-squawking-parameter
(syntax-rules ()
[(_ (id (... ...)) loc)
(begin
(define loc ($make-thread-parameter #f))
(define-syntax id
(lambda (q)
(unless (identifier? q) (syntax-error q))
#`(let ([x (loc)])
(unless x (syntax-error #'#,q "uninitialized"))
x)))
(... ...))]
[(_ id loc) (define-squawking-parameter (id) loc)]))
(define-squawking-parameter (regid reg-alias ...) regid-loc)
...
(define-squawking-parameter regvec regvec-loc)
(define-squawking-parameter arg-registers arg-registers-loc)
(define-squawking-parameter extra-registers extra-registers-loc)
(define-syntax with-initialized-registers
(syntax-rules ()
[(_ b1 b2 (... ...))
(parameterize ([regid-loc (make-reg 'regid 'mdinfo tc-disp callee-save?)] ...)
(parameterize ([regvec-loc (vector regid ...)]
[arg-registers-loc (list arg-regid ...)]
[extra-registers-loc (list extra-regid ...)])
(let () b1 b2 (... ...))))]))))])))
(define-syntax define-machine-dependent-registers
(lambda (x)
(syntax-case x ()
[(_ [regid alias ... callee-save? mdinfo] ...)
#'(begin
(begin
(define regid (make-reg 'regid 'mdinfo #f callee-save?))
(module (alias ...) (define x regid) (define alias x) ...))
...)])))
(define-syntax define-registers
(lambda (x)
(syntax-case x (reserved allocable machine-dependent)
[(k (reserved [rreg rreg-alias ... rreg-callee-save? rreg-mdinfo] ...)
(allocable [areg areg-alias ... areg-callee-save? areg-mdinfo] ...)
(machine-depdendent [mdreg mdreg-alias ... mdreg-callee-save? mdreg-mdinfo] ...))
(with-implicit (k regvec arg-registers extra-registers real-register? with-initialized-registers)
#`(begin
(define-reserved-registers [rreg rreg-alias ... rreg-callee-save? rreg-mdinfo] ...)
(define-allocable-registers regvec arg-registers extra-registers with-initialized-registers [areg areg-alias ... areg-callee-save? areg-mdinfo] ...)
(define-machine-dependent-registers [mdreg mdreg-alias ... mdreg-callee-save? mdreg-mdinfo] ...)
(define-syntax real-register?
(with-syntax ([real-reg* #''(rreg ... rreg-alias ... ... areg ... areg-alias ... ... mdreg ... mdreg-alias ... ...)])
(syntax-rules ()
[(_ e) (memq e real-reg*)])))))])))
(architecture registers)
; pseudo register used for mref's with no actual index
(define %zero (make-reg 'zero #f #f #f))
; define %ref-ret to be sfp[0] on machines w/no ret register
(define-syntax %ref-ret
(lambda (x)
(meta-cond
[(real-register? '%ret) #'%ret]
[else (with-syntax ([%mref (datum->syntax x '%mref)])
#'(%mref ,%sfp 0))])))
(define make-Ldoargerr
(lambda ()
(make-libspec-label 'doargerr (lookup-libspec doargerr)
(reg-list %ret %ac0 %cp))))
(define make-Ldomvleterr
(lambda ()
(make-libspec-label 'domvleterr (lookup-libspec domvleterr)
(reg-list %ret %ac0))))
(define make-Lcall-error
(lambda ()
(make-libspec-label 'call-error (lookup-libspec call-error)
(reg-list %ret %cp))))
(module (frame-vars get-fv)
(define-threaded frame-vars)
(define get-fv
(lambda (x)
(let ([n (vector-length frame-vars)])
(when (fx>= x n)
(let ([new-vec (make-vector (fxmax (fx+ x 1) (fx* n 2)) #f)])
(let loop ([n n])
(unless (fx= n 0)
(let ([n (fx- n 1)])
(vector-set! new-vec n (vector-ref frame-vars n))
(loop n))))
(set! frame-vars new-vec))))
(or (vector-ref frame-vars x)
(let ([fv ($make-fv x)])
(vector-set! frame-vars x fv)
fv)))))
(define-syntax reg-cons*
(lambda (x)
(syntax-case x ()
[(_ ?reg ... ?reg*)
(fold-right
(lambda (reg reg*)
(if (real-register? (syntax->datum reg))
#`(cons #,reg #,reg*)
reg*))
#'?reg* #'(?reg ...))])))
(define-syntax reg-list
(syntax-rules ()
[(_ ?reg ...) (reg-cons* ?reg ... '())]))
(define-syntax with-saved-ret-reg
(lambda (x)
(syntax-case x ()
[(k ?e)
(if (real-register? '%ret)
(with-implicit (k %seq %mref)
#'(%seq
(set! ,(%mref ,%sfp 0) ,%ret)
,?e
(set! ,%ret ,(%mref ,%sfp 0))))
#'?e)])))
(module (restore-scheme-state save-scheme-state with-saved-scheme-state)
(define-syntax build-reg-list
; TODO: create reg records at compile time, and build these lists at compile time
; TODO: include ts & td
; TODO: specify three lists: things that need to be saved/restored via the thread context,
; things that need to be saved/restored somehow, and things that can be trashed
(lambda (x)
(syntax-case x (base-in in out)
[(_ orig-x (base-in base-inreg ...) (in inreg ...) (out outreg ...))
(let ([all* '(%ts %td %ac0 %ac1 %cp %xp %yp scheme-args extra-regs)]
[in* (datum (inreg ...))]
[out* (datum (outreg ...))])
(define remove*
(lambda (x* ls)
(if (null? x*)
ls
(remove* (cdr x*) (remq (car x*) ls)))))
(let ([bogus* (remove* all* in*)])
(unless (equal? bogus* '()) (syntax-error #'orig-x (format "bogus in registers ~s" bogus*))))
(let ([bogus* (remove* all* out*)])
(unless (equal? bogus* '()) (syntax-error #'orig-x (format "bogus out registers ~s" bogus*))))
(unless (equal? (remove* in* out*) out*)
(syntax-error #'orig-x "non-empty intersection"))
(let ([other* (remove* in* (remove* out* all*))])
(unless (null? other*)
(syntax-error #'orig-x (format "registers not mentioned: ~s" other*))))
(with-syntax ([(in ...) (datum->syntax #'*
(filter (lambda (x) (real-register? x))
(append (datum (base-inreg ...)) in*)))])
#`(cons* (ref-reg in) ...
#,(if (memq 'scheme-args in*)
(if (memq 'extra-regs in*)
#'(append arg-registers extra-registers)
#'arg-registers)
(if (memq 'extra-regs in*)
#'extra-registers
#''())))))])))
(define-syntax get-tcslot
(lambda (x)
(syntax-case x ()
[(_ k reg)
(with-implicit (k in-context %mref)
#'(in-context Lvalue
(%mref ,%tc ,(reg-tc-disp reg))))])))
(define-syntax $save-scheme-state
(lambda (x)
(syntax-case x ()
[(_ k orig-x in out)
(with-implicit (k quasiquote)
; although eap might be changed by dirty writes, and esp might be changed by
; one-shot continuation handling, we always write through to the tc so that
; we never need to save eap or esp and also so that eap, which serves as the
; base of the current dirty list, is always accurate, even when an invalid
; memory reference or invalid instruction occurs. so we leave eap and esp
; out of the save list (but not the restore list below).
#'(let ([regs-to-save (build-reg-list orig-x (base-in %sfp %ap %trap) in out)])
(fold-left (lambda (body reg)
`(seq (set! ,(get-tcslot k reg) ,reg) ,body))
`(nop) regs-to-save)))])))
(define-syntax $restore-scheme-state
(lambda (x)
(syntax-case x ()
[(_ k orig-x in out)
(with-implicit (k quasiquote)
#'(let ([regs-to-restore (build-reg-list orig-x (base-in %sfp %ap %trap %eap %esp) in out)])
(fold-left (lambda (body reg)
`(seq (set! ,reg ,(get-tcslot k reg)) ,body))
`(nop) regs-to-restore)))])))
(define-syntax save-scheme-state
(lambda (x)
(syntax-case x ()
[(k in out) #`($save-scheme-state k #,x in out)])))
(define-syntax restore-scheme-state
(lambda (x)
(syntax-case x ()
[(k in out) #`($restore-scheme-state k #,x in out)])))
(define-syntax with-saved-scheme-state
(lambda (x)
(syntax-case x ()
[(k in out ?body)
(with-implicit (k quasiquote %seq)
#`(%seq
,($save-scheme-state k #,x in out)
,?body
,($restore-scheme-state k #,x in out)))]))))
(define-record-type ctci ; compile-time version of code-info
(nongenerative)
(sealed #t)
(fields (mutable live) (mutable rpi*) (mutable closure-fv-names))
(protocol
(lambda (new)
(lambda ()
(new #f '() #f)))))
(define-record-type ctrpi ; compile-time version of rp-info
(nongenerative)
(sealed #t)
(fields label src sexpr mask))
(define-threaded next-lambda-seqno)
(define-record-type info-lambda (nongenerative)
(parent info)
(sealed #t)
(fields src sexpr libspec interface* (mutable dcl*) (mutable flags) (mutable fv*) (mutable name)
(mutable well-known?) (mutable closure-rep) ctci (mutable pinfo*) seqno)
(protocol
(lambda (pargs->new)
(define next-seqno
(lambda ()
(let ([seqno next-lambda-seqno])
(set! next-lambda-seqno (fx+ seqno 1))
seqno)))
(rec cons-info-lambda
(case-lambda
[(src sexpr libspec interface*) (cons-info-lambda src sexpr libspec interface* #f 0)]
[(src sexpr libspec interface* name) (cons-info-lambda src sexpr libspec interface* name 0)]
[(src sexpr libspec interface* name flags)
((pargs->new) src sexpr libspec interface*
(map (lambda (iface) (make-direct-call-label 'dcl)) interface*)
(if (eq? (subset-mode) 'system) (fxlogor flags (constant code-flag-system)) flags)
'() name #f 'closure (and (generate-inspector-information) (make-ctci)) '() (next-seqno))])))))
(define-record-type info-call (nongenerative)
(parent info)
(sealed #t)
(fields src sexpr (mutable check?) pariah? error?)
(protocol
(lambda (pargs->new)
(lambda (src sexpr check? pariah? error?)
((pargs->new) src sexpr check? pariah? error?)))))
(define-record-type info-newframe (nongenerative)
(parent info)
(sealed #t)
(fields
src
sexpr
cnfv*
nfv*
nfv**
(mutable weight)
(mutable call-live*)
(mutable frame-words)
(mutable local-save*))
(protocol
(lambda (pargs->new)
(lambda (src sexpr cnfv* nfv* nfv**)
((pargs->new) src sexpr cnfv* nfv* nfv** 0 #f #f #f)))))
(define-record-type info-kill* (nongenerative)
(parent info)
(fields kill*))
(define-record-type info-kill*-live* (nongenerative)
(parent info-kill*)
(fields live*)
(protocol
(lambda (new)
(case-lambda
[(kill* live*)
((new kill*) live*)]
[(kill*)
((new kill*) (reg-list))]))))
(define-record-type info-asmlib (nongenerative)
(parent info-kill*-live*)
(sealed #t)
(fields libspec save-ra?)
(protocol
(lambda (new)
(case-lambda
[(kill* libspec save-ra? live*)
((new kill* live*) libspec save-ra?)]
[(kill* libspec save-ra?)
((new kill*) libspec save-ra?)]))))
(module (intrinsic-info-asmlib intrinsic-return-live* intrinsic-entry-live* dorest-intrinsics)
; standing on our heads here to avoid referencing registers at
; load time...would be cleaner if registers were immutable,
; i.e., mutable fields (direct and inherited from var) were kept
; in separate tables...but that might add more cost to register
; allocation, which is already expensive.
(define-record-type intrinsic (nongenerative)
(sealed #t)
(fields libspec get-kill* get-live* get-rv*))
(define intrinsic-info-asmlib
(lambda (intrinsic save-ra?)
(make-info-asmlib ((intrinsic-get-kill* intrinsic))
(intrinsic-libspec intrinsic)
save-ra?
((intrinsic-get-live* intrinsic)))))
(define intrinsic-return-live*
; used a handful of times, just while compiling library.ss...don't bother optimizing
(lambda (intrinsic)
(fold-left (lambda (live* kill) (remq kill live*))
(vector->list regvec) ((intrinsic-get-kill* intrinsic)))))
(define intrinsic-entry-live*
; used a handful of times, just while compiling library.ss...don't bother optimizing
(lambda (intrinsic) ; return-live* - rv + live*
(fold-left (lambda (live* live) (if (memq live live*) live* (cons live live*)))
(fold-left (lambda (live* rv) (remq rv live*))
(intrinsic-return-live* intrinsic)
((intrinsic-get-rv* intrinsic)))
((intrinsic-get-live* intrinsic)))))
(define-syntax declare-intrinsic
(syntax-rules (unquote)
[(_ name entry-name (kill ...) (live ...) (rv ...))
(begin
(define name
(make-intrinsic
(lookup-libspec entry-name)
(lambda () (reg-list kill ...))
(lambda () (reg-list live ...))
(lambda () (reg-list rv ...))))
(export name))]))
; must include in kill ... any register explicitly assigned by the intrinsic
; plus additional registers as needed to avoid spilled unspillables. the
; list could be machine-dependent but at this point it doesn't matter.
(declare-intrinsic dofargint32 dofargint32 (%ts %td %xp) (%ac0) (%ac0))
(constant-case ptr-bits
[(32) (declare-intrinsic dofargint64 dofargint64 (%ts %td %xp) (%ac0) (%ac0 %ac1))]
[(64) (declare-intrinsic dofargint64 dofargint64 (%ts %td %xp) (%ac0) (%ac0))])
(declare-intrinsic dofretint32 dofretint32 (%ts %td %xp) (%ac0) (%ac0))
(constant-case ptr-bits
[(32) (declare-intrinsic dofretint64 dofretint64 (%ts %td %xp) (%ac0 %ac1) (%ac0))]
[(64) (declare-intrinsic dofretint64 dofretint64 (%ts %td %xp) (%ac0) (%ac0))])
(declare-intrinsic dofretuns32 dofretuns32 (%ts %td %xp) (%ac0) (%ac0))
(constant-case ptr-bits
[(32) (declare-intrinsic dofretuns64 dofretuns64 (%ts %td %xp) (%ac0 %ac1) (%ac0))]
[(64) (declare-intrinsic dofretuns64 dofretuns64 (%ts %td %xp) (%ac0) (%ac0))])
(declare-intrinsic dofretu8* dofretu8* (%ac0 %ts %td %cp %ac1) (%ac0) (%xp))
(declare-intrinsic dofretu16* dofretu16* (%ac0 %ts %td %cp %ac1) (%ac0) (%xp))
(declare-intrinsic dofretu32* dofretu32* (%ac0 %ts %td %cp %ac1) (%ac0) (%xp))
(declare-intrinsic get-room get-room () (%xp) (%xp))
(declare-intrinsic scan-remembered-set scan-remembered-set () () ())
(declare-intrinsic dooverflow dooverflow () () ())
(declare-intrinsic dooverflood dooverflood () (%xp) ())
; a dorest routine takes all of the register and frame arguments from the rest
; argument forward and also modifies the rest argument. for the rest argument,
; this is a wash (it's live both before and after). the others should also be
; listed as live. it's inconvenient and currently unnecessary to do so.
; (actually currently impossible to list the infinite set of frame arguments)
(define-syntax dorest-intrinsic-max (identifier-syntax 5))
(export dorest-intrinsic-max)
(define (list-xtail ls n)
(if (or (null? ls) (fx= n 0))
ls
(list-xtail (cdr ls) (fx1- n))))
(define dorest-intrinsics
(let ()
(define-syntax dorests
(lambda (x)
#`(vector #,@
(let f ([i 0])
(if (fx> i dorest-intrinsic-max)
'()
(cons #`(make-intrinsic
(lookup-libspec #,(construct-name #'k "dorest" i))
(lambda () (reg-list %ac0 %xp %ts %td))
(lambda () (reg-cons* %ac0 (list-xtail arg-registers #,i)))
(lambda () (let ([ls (list-xtail arg-registers #,i)]) (if (null? ls) '() (list (car ls))))))
(f (fx+ i 1))))))))
dorests)))
(define-record-type info-alloc (nongenerative)
(parent info)
(sealed #t)
(fields tag save-flrv? save-ra?))
(define-record-type info-foreign (nongenerative)
(parent info)
(sealed #t)
(fields conv* arg-type* result-type (mutable name))
(protocol
(lambda (pargs->new)
(lambda (conv* arg-type* result-type)
((pargs->new) conv* arg-type* result-type #f)))))
(define-record-type info-literal (nongenerative)
(parent info)
(sealed #t)
(fields indirect? type addr offset))
(define-record-type info-lea (nongenerative)
(parent info)
(sealed #t)
(fields offset))
(define-record-type info-load (nongenerative)
(parent info)
(sealed #t)
(fields type swapped?))
(define-record-type info-loadfl (nongenerative)
(parent info)
(sealed #t)
(fields flreg))
(define-record-type info-condition-code (nongenerative)
(parent info)
(sealed #t)
(fields type reversed? invertible?))
(define-record-type info-c-simple-call (nongenerative)
(parent info-kill*-live*)
(sealed #t)
(fields save-ra? entry)
(protocol
(lambda (new)
(case-lambda
[(save-ra? entry) ((new '() '()) save-ra? entry)]
[(live* save-ra? entry) ((new '() live*) save-ra? entry)]))))
(define-record-type info-c-return (nongenerative)
(parent info)
(sealed #t)
(fields offset))
(module ()
(record-writer (record-type-descriptor info-load)
(lambda (x p wr)
(fprintf p "#<info-load ~s>" (info-load-type x))))
(record-writer (record-type-descriptor info-lambda)
(lambda (x p wr)
(fprintf p "#<info-lambda ~s ~s ~s ~s ~s>"
(info-lambda-libspec x) (info-lambda-interface* x) (info-lambda-name x)
(info-lambda-well-known? x)
(info-lambda-fv* x))))
(record-writer (record-type-descriptor info-foreign)
(lambda (x p wr)
(fprintf p "#<info-foreign~@[ ~a~]>" (info-foreign-name x))))
(record-writer (record-type-descriptor info-literal)
(lambda (x p wr)
(fprintf p "#<literal ~s>" (info-literal-addr x))))
)
(define-pass cpnanopass : Lsrc (ir) -> L1 ()
(definitions
(define-syntax with-uvars
(syntax-rules ()
[(_ (x* id*) b1 b2 ...)
(and (identifier? #'x*) (identifier? #'id*))
(let ([uvar* (map prelex->uvar id*)] [name* (map prelex-name id*)])
(dynamic-wind
(lambda () (for-each prelex-name-set! id* uvar*))
(lambda () (let ([x* uvar*]) b1 b2 ...))
(lambda () (for-each prelex-name-set! id* name*))))]))
(define extract-uvar
(lambda (id)
(let ([x (prelex-name id)])
(unless (uvar? x)
(sorry! 'extract-uvar "~s is not a uvar" x))
x))))
(CaseLambdaExpr : Expr (ir x) -> CaseLambdaExpr ()
[(case-lambda ,preinfo (clause (,x** ...) ,interface* ,body*) ...)
(let ([info (make-info-lambda (preinfo-src preinfo) (preinfo-sexpr preinfo) (preinfo-lambda-libspec preinfo) interface*
(preinfo-lambda-name preinfo) (preinfo-lambda-flags preinfo))])
(when x (uvar-info-lambda-set! x info))
`(case-lambda ,info
,(map (lambda (x* interface body)
(with-uvars (uvar* x*)
(in-context CaseLambdaClause
`(clause (,uvar* ...) ,interface ,(Expr body)))))
x** interface* body*) ...))]
[(case-lambda ,preinfo ,cl* ...)
(sorry! who "found unreachable clause" ir)])
(Expr : Expr (ir) -> Expr ()
[(ref ,maybe-src ,x) (extract-uvar x)]
[(set! ,maybe-src ,x ,[e]) `(set! ,(extract-uvar x) ,e)]
[(case-lambda ,preinfo ,cl* ...) (CaseLambdaExpr ir #f)]
[(letrec ([,x* ,e*] ...) ,body)
(with-uvars (uvar* x*)
(let ([e* (map CaseLambdaExpr e* uvar*)])
`(letrec ([,uvar* ,e*] ...) ,(Expr body))))]
[(call ,preinfo ,e ,[e*] ...)
`(call ,(make-info-call (preinfo-src preinfo) (preinfo-sexpr preinfo) (fx< (optimize-level) 3) #f #f)
,(Expr e) ,e* ...)]
[(foreign (,conv* ...) ,name ,[e] (,arg-type* ...) ,result-type)
(let ([info (make-info-foreign conv* arg-type* result-type)])
(info-foreign-name-set! info name)
`(foreign ,info ,e))]
[(fcallable (,conv* ...) ,[e] (,arg-type* ...) ,result-type)
`(fcallable ,(make-info-foreign conv* arg-type* result-type) ,e)])
(CaseLambdaExpr ir #f))
(define find-matching-clause
(lambda (len x** interface* body* kfixed kvariable kfail)
(let f ([x** x**] [interface* interface*] [body* body*])
(if (null? interface*)
(kfail)
(let ([interface (car interface*)])
(if (fx< interface 0)
(let ([nfixed (fxlognot interface)])
(if (fx>= len nfixed)
(kvariable nfixed (car x**) (car body*))
(f (cdr x**) (cdr interface*) (cdr body*))))
(if (fx= interface len)
(kfixed (car x**) (car body*))
(f (cdr x**) (cdr interface*) (cdr body*)))))))))
(define-syntax define-$type-check
(lambda (x)
(syntax-case x ()
[(k L) (with-implicit (k $type-check)
#'(define $type-check
(lambda (mask type expr)
(with-output-language L
(cond
[(fx= type 0) (%inline log!test ,expr (immediate ,mask))]
[(= mask (constant byte-constant-mask)) (%inline eq? ,expr (immediate ,type))]
[else (%inline type-check? ,expr (immediate ,mask) (immediate ,type))])))))])))
(define-syntax %type-check
(lambda (x)
(syntax-case x ()
[(k mask type expr)
(with-implicit (k $type-check quasiquote)
#'($type-check (constant mask) (constant type) `expr))])))
(define-syntax %typed-object-check ; NB: caller must bind e
(lambda (x)
(syntax-case x ()
[(k mask type expr)
(with-implicit (k quasiquote %type-check %constant %mref)
#'`(if ,(%type-check mask-typed-object type-typed-object expr)
,(%type-check mask type
,(%mref expr ,(constant typed-object-type-disp)))
,(%constant sfalse)))])))
(define-syntax %seq
(lambda (x)
(syntax-case x ()
[(k e1 ... e2)
(with-implicit (k quasiquote)
#``#,(fold-right (lambda (x body) #`(seq #,x #,body))
#'e2 #'(e1 ...)))])))
(define-syntax %mref
(lambda (x)
(syntax-case x ()
[(k e0 e1 imm)
(with-implicit (k quasiquote)
#'`(mref e0 e1 imm))]
[(k e0 imm)
(with-implicit (k quasiquote)
#'`(mref e0 ,%zero imm))])))
(define-syntax %inline
(lambda (x)
(syntax-case x ()
[(k name e ...)
(with-implicit (k quasiquote)
#'`(inline ,null-info ,(%primitive name) e ...))])))
(define-syntax %lea
(lambda (x)
(syntax-case x ()
[(k base offset)
(with-implicit (k quasiquote)
#'`(inline ,(make-info-lea offset) ,%lea1 base))]
[(k base index offset)
(with-implicit (k quasiquote)
#'`(inline ,(make-info-lea offset) ,%lea2 base index))])))
(define-syntax %constant
(lambda (x)
(syntax-case x ()
[(k x)
(with-implicit (k quasiquote)
#'`(immediate ,(constant x)))])))
(define-syntax %tc-ref
(lambda (x)
(define-who field-type
(lambda (struct field)
(cond
[(assq field (getprop struct '*fields* '())) =>
(lambda (a)
(apply
(lambda (field type disp len) type)
a))]
[else ($oops who "undefined field ~s-~s" struct field)])))
(syntax-case x ()
[(k field) #'(k ,%tc field)]
[(k e-tc field)
(if (memq (field-type 'tc (datum field)) '(ptr void* uptr iptr))
(with-implicit (k %mref)
#`(%mref e-tc
#,(lookup-constant
(string->symbol
(format "tc-~a-disp" (datum field))))))
(syntax-error x "non-ptr-size tc field"))])))
(define-syntax %constant-alloc
(lambda (x)
(syntax-case x ()
[(k tag size) #'(k tag size #f #f)]
[(k tag size save-flrv?) #'(k tag size save-flrv? #f)]
[(k tag size save-flrv? save-asm-ra?)
(with-implicit (k quasiquote)
#'`(alloc
,(make-info-alloc (constant tag) save-flrv? save-asm-ra?)
(immediate ,(c-alloc-align size))))])))
(define-pass np-recognize-let : L1 (ir) -> L2 ()
(definitions
(define seqs-and-profiles?
(lambda (e)
(nanopass-case (L1 Expr) e
[(profile ,src) #t]
[(seq ,e1 ,e2) (and (seqs-and-profiles? e1) (seqs-and-profiles? e2))]
[else #f])))
(define Profile
(lambda (e)
(let f ([e e] [profile* '()])
(nanopass-case (L1 Expr) e
[(seq ,e1 ,e2)
(guard (seqs-and-profiles? e1))
(f e2 (cons e1 profile*))]
[else (values e profile*)]))))
(define build-seq (lambda (e1 e2) (with-output-language (L2 Expr) `(seq ,(Expr e1) ,e2))))
(define build-seq* (lambda (e* e) (fold-right build-seq e e*))))
(Expr : Expr (ir) -> Expr ()
[(call ,info1 ,[Profile : e profile1*] ,[e*] ...)
(nanopass-case (L1 Expr) e
[(case-lambda ,info2 (clause (,x* ...) ,interface ,[Expr : body]))
(guard (fx= (length e*) interface))
(build-seq* profile1* `(let ([,x* ,e*] ...) ,body))]
[(letrec ([,x1 ,[Expr : le*]]) ,[Profile : body profile2*])
; can't use a guard, since body isn't bound in guard.
(if (eq? body x1)
(build-seq* profile1*
(build-seq* profile2*
`(letrec ([,x1 ,le*]) (call ,info1 ,x1 ,e* ...))))
`(call ,info1 ,(build-seq* profile1* (Expr e)) ,e* ...))]
[else
`(call ,info1 ,(build-seq* profile1* (Expr e)) ,e* ...)])]))
(define-pass np-discover-names : L2 (ir) -> L3 ()
(definitions
(define ->name
(lambda (x)
(cond
[(uvar? x) (->name (uvar-name x))]
[(string? x) x]
[(symbol? x)
(let ([name ($symbol-name x)])
(if (pair? name) (cdr name) name))]
[(eq? #f x) #f]
[else (error 'np-discover-names "x is not a name" x)]))))
(Expr : Expr (ir name moi) -> Expr ()
[(letrec ([,x* ,le*] ...) ,[body])
(let ([le* (map (lambda (le x) (CaseLambdaExpr le (->name x) moi)) le* x*)])
`(letrec ([,x* ,le*] ...) ,body))]
[(let ([,x* ,e*] ...) ,[body])
(let ([e* (map (lambda (e x) (Expr e (->name x) moi)) e* x*)])
`(let ([,x* ,e*] ...) ,body))]
; handle top-level set! (i.e. $set-top-level-value)
[(call ,info ,pr (quote ,d) ,e0)
(guard (and (eq? (primref-name pr) '$set-top-level-value!) (symbol? d)))
(let ([e0 (Expr e0 (->name d) moi)])
`(call ,info ,pr (quote ,d) ,e0))]
[(call ,info ,[e0 #f moi -> e0] ,[e1* #f moi -> e1*] ...)
`(call ,info ,e0 ,e1* ...)]
[(if ,[e0 #f moi -> e0] ,[e1] ,[e2])
`(if ,e0 ,e1 ,e2)]
[(seq ,[e0 #f moi -> e0] ,[e1])
`(seq ,e0 ,e1)]
[(foreign ,info ,[e #f moi -> e])
(when name (info-foreign-name-set! info name))
`(foreign ,info ,e)]
[(fcallable ,info ,[e #f moi -> e])
(info-foreign-name-set! info name)
`(fcallable ,info ,e)]
[(set! ,x ,e0)
(let ([e0 (Expr e0 (->name x) moi)]) `(set! ,x ,e0))]
[(moi) `(quote ,moi)])
(CaseLambdaExpr : CaseLambdaExpr (ir [name #f] [moi #f]) -> CaseLambdaExpr ()
[(case-lambda ,info ,[cl #f name -> cl] ...)
(unless (info-lambda-name info) (info-lambda-name-set! info name))
`(case-lambda ,info ,cl ...)])
(CaseLambdaClause : CaseLambdaClause (ir name moi) -> CaseLambdaClause ()))
(define-pass np-convert-assignments : L3 (ir) -> L4 ()
(definitions
(define-syntax %primcall
(lambda (x)
(syntax-case x ()
[(k src sexpr prim arg ...)
(identifier? #'prim)
(with-implicit (k quasiquote)
#``(call ,(make-info-call src sexpr #f #f #f)
,(lookup-primref 3 'prim)
arg ...))])))
(define unbound-object ($unbound-object))
(define partition-assigned
(lambda (x*)
(if (null? x*)
(values '() '() '())
(let ([x (car x*)] [x* (cdr x*)])
(let-values ([(x* t* a*) (partition-assigned x*)])
(if (uvar-assigned? x)
(let ([t (make-tmp 't)])
(uvar-assigned! x #f)
(values (cons t x*) (cons t t*) (cons x a*)))
(values (cons x x*) t* a*)))))))
(define handle-assigned
(lambda (x* body k)
(let-values ([(x* t* a*) (partition-assigned x*)])
(k x* (if (null? a*)
body
(with-output-language (L4 Expr)
`(let ([,a* ,(map (lambda (t) (%primcall #f #f cons ,t (quote ,unbound-object))) t*)] ...)
,body))))))))
(Expr : Expr (ir) -> Expr ()
[,x (if (uvar-assigned? x) (%primcall #f #f car ,x) x)]
[(set! ,x ,[e]) (%primcall #f #f set-car! ,x ,e)]
[(let ([,x* ,[e*]] ...) ,[body])
(handle-assigned x* body
(lambda (x* body)
`(let ([,x* ,e*] ...) ,body)))])
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ()
[(clause (,x* ...) ,interface ,[body])
(handle-assigned x* body
(lambda (x* body)
`(clause (,x* ...) ,interface ,body)))]))
; for use only after mdcl field has been added to the call syntax
(define-syntax %primcall
(lambda (x)
(syntax-case x ()
[(k src sexpr prim arg ...)
(identifier? #'prim)
(with-implicit (k quasiquote)
#``(call ,(make-info-call src sexpr #f #f #f) #f
,(lookup-primref 3 'prim)
arg ...))])))
(define-pass np-sanitize-bindings : L4 (ir) -> L4 ()
; must come before suppress-procedure-checks and recognize-mrvs
; since it sets up uvar-info-lambda, but after convert-assignments
(definitions
(define maybe-build-let
(lambda (x* e* body)
(if (null? x*)
body
(with-output-language (L4 Expr)
`(let ([,x* ,e*] ...) ,body)))))
(define maybe-build-letrec
(lambda (x* e* body)
(if (null? x*)
body
(with-output-language (L4 Expr)
`(letrec ([,x* ,e*] ...) ,body))))))
(Expr : Expr (ir) -> Expr ()
[(let ([,x* ,[e*]] ...) ,[body])
(with-values
(let f ([x* x*] [e* e*])
(if (null? x*)
(values '() '() '() '())
(let-values ([(ex* ee* lx* le*) (f (cdr x*) (cdr e*))])
(nanopass-case (L4 Expr) (car e*)
[(case-lambda ,info ,cl ...)
(uvar-info-lambda-set! (car x*) info)
(values ex* ee* (cons (car x*) lx*) (cons (car e*) le*))]
[else (values (cons (car x*) ex*) (cons (car e*) ee*) lx* le*)]))))
(lambda (ex* ee* lx* le*)
(maybe-build-let ex* ee*
(maybe-build-letrec lx* le*
body))))]))
(define-pass np-suppress-procedure-checks : L4 (ir) -> L4 ()
; N.B. check must be done after e and e* have been evaluated, so we attach
; a flag to the call syntax rather than introducing explicit checks.
; if we could introduce explicit checks instead, we could avoid doing
; so along some branches of an if in call context, even if others
; need the check. c'est la vie.
(Proc : Expr (ir) -> * (#f)
[,x (uvar-info-lambda x)]
[(quote ,d) (procedure? d)]
[,pr #t]
[(seq ,[] ,[* suppress?]) suppress?]
[(if ,[] ,[* suppress1?] ,[* suppress2?]) (and suppress1? suppress2?)]
[(letrec ([,x* ,[]] ...) ,[* suppress?]) suppress?]
[(let ([,x* ,[]] ...) ,[* suppress?]) suppress?]
[(case-lambda ,info ,[] ...) #t]
[else #f])
(CaseLambdaExpr : CaseLambdaExpr (ir) -> * ()
[(case-lambda ,info ,[] ...) (values)])
(CaseLambdaClause : CaseLambdaClause (ir) -> * ()
[(clause (,x* ...) ,interface ,[]) (values)])
; NB: explicitly handling every form because the nanopass infrastructure can't autofill when the output is *
(Expr : Expr (ir) -> * ()
[,x (values)]
[(quote ,d) (values)]
[(case-lambda ,info ,[] ...) (values)]
[(call ,info0
(call ,info1 ,pr (quote ,d))
,[] ...)
(guard (and (eq? (primref-name pr) '$top-level-value) (symbol? d)))
(info-call-check?-set! info0 #f)
(info-call-check?-set! info1 #f)
(values)]
[(call ,info ,[* suppress?] ,[] ...)
(when suppress? (info-call-check?-set! info #f))
(values)]
[(if ,[] ,[] ,[]) (values)]
[(seq ,[] ,[]) (values)]
[,pr (values)]
[(let ([,x ,[]] ...) ,[]) (values)]
[(letrec ([,x ,[]] ...) ,[]) (values)]
[(foreign ,info ,[]) (values)]
[(fcallable ,info ,[]) (values)]
[(profile ,src) (values)]
[(pariah) (values)]
[else (sorry! who "unhandled expression ~s" ir)])
(begin (CaseLambdaExpr ir) ir))
(define-pass np-recognize-mrvs : L4 (ir) -> L4.5 ()
(definitions
(define insert-procedure-check
(lambda (check? tmp e)
(with-output-language (L4.5 Expr)
(if check?
`(seq
(if ,(%primcall #f #f procedure? ,tmp)
(quote ,(void))
,(%primcall #f #f $oops (quote #f) (quote "attempt to apply non-procedure ~s") ,tmp))
,e)
e)))))
(Expr : Expr (ir) -> Expr ()
[(call ,info ,pr ,e1 ,e2)
(guard (eq? (primref-name pr) 'call-with-values))
(let ([check? (not (all-set? (prim-mask unsafe) (primref-flags pr)))])
(Producer e1 check? (info-call-src info) (info-call-sexpr info)
(lambda (e1 src sexpr)
(Consumer e2 e1 check? src sexpr))))]
[(call ,info ,[e] ,[e*] ...) `(call ,info #f ,e ,e* ...)])
(Producer : Expr (ir check? src sexpr k) -> Expr ()
[,x (k `(call ,(make-info-call src sexpr check? #f #f) #f ,x) src sexpr)]
[(case-lambda ,info (clause (,x** ...) ,interface* ,body*) ...)
(find-matching-clause 0 x** interface* body*
(lambda (x* body) (k (Expr body) src sexpr))
(lambda (nfixed x* body) `(let ([,(car x*) (quote ())]) ,(k (Expr body) src sexpr)))
(lambda ()
(let ([tmp (make-tmp 'tp)])
(uvar-info-lambda-set! tmp info)
`(letrec ([,tmp ,(Expr ir)])
,(k tmp src sexpr)))))]
[(seq ,[Expr : e1] ,[Producer : e2]) `(seq ,e1 ,e2)]
[(let ([,x* ,[Expr : e*]] ...) ,[Producer : e]) `(let ([,x* ,e*] ...) ,e)]
[(letrec ([,x* ,[le*]] ...) ,[Producer : e]) `(letrec ([,x* ,le*] ...) ,e)]
[,pr (k `(call ,(make-info-call src sexpr #f #f #f) #f ,pr) src sexpr)]
[else (let ([tmp (make-tmp 'tp)])
; force last part of producer to be evaluated before consumer, to
; avoid interleaved evaluation of producer and consumer
`(let ([,tmp ,(Expr ir)])
,(k `(call ,(make-info-call #f #f check? #f #f) #f ,tmp) src sexpr)))])
(Consumer : Expr (ir producer-or check? src sexpr) -> Expr ()
; generate same code for single-value let-values as for let
[(case-lambda ,info (clause (,x) ,interface ,[Expr : body]))
(guard (= interface 1))
`(let ([,x ,producer-or]) ,body)]
[(case-lambda ,info (clause (,x** ...) ,interface* ,[Expr : body*]) ...)
`(mvlet ,producer-or ((,x** ...) ,interface* ,body*) ...)]
[,x (cond
[(uvar-info-lambda x) =>
(lambda (info)
(define make-tmps
(lambda (n)
(do ([n (if (fx< n 0) (fx- n) n) (fx- n 1)]
[tmp* '() (cons (make-tmp 't) tmp*)])
((fx= n 0) tmp*))))
(let ([interface* (info-lambda-interface* info)])
(let ([info* (map (lambda (dcl) (make-info-call src sexpr #f #f #f)) (info-lambda-dcl* info))]
[x* (make-list (length interface*) x)]
[x** (map make-tmps interface*)])
`(mvlet ,producer-or
((,x** ...) ,interface* (call ,info* ,(info-lambda-dcl* info) ,x* ,x** ...))
...))))]
[else (insert-procedure-check check? x `(mvcall ,(make-info-call src sexpr #f #f #f) ,producer-or ,x))])]
[(seq ,[Expr : e1] ,[Consumer : e2]) `(seq ,e1 ,e2)]
[(let ([,x* ,[Expr : e*]] ...) ,[Consumer : e]) `(let ([,x* ,e*] ...) ,e)]
[(letrec ([,x* ,[le*]] ...) ,[Consumer : e]) `(letrec ([,x* ,le*] ...) ,e)]
[,pr `(mvcall ,(make-info-call src sexpr #f #f #f) ,producer-or ,pr)]
[(quote ,d) (guard (procedure? d)) `(mvcall ,(make-info-call src sexpr #f #f #f) ,producer-or (quote ,d))]
[else (let ([tmp (make-tmp 'tc)])
; force consumer expression to be evaluated before producer body
; this includes references to top-level variables: since they can
; be altered by the producer, we can use a pvalue call
`(let ([,tmp ,(Expr ir)])
,(insert-procedure-check check? tmp
`(mvcall ,(make-info-call src sexpr #f #f #f) ,producer-or ,tmp))))]))
(define-pass np-expand-foreign : L4.5 (ir) -> L4.75 ()
(Expr : Expr (ir) -> Expr ()
[(foreign ,info ,[e])
(let ([iface (length (info-foreign-arg-type* info))]
[t (make-tmp 'tentry 'uptr)]
[t* (map (lambda (x) (make-tmp 't)) (info-foreign-arg-type* info))])
(let ([lambda-info (make-info-lambda #f #f #f (list iface) (info-foreign-name info))])
`(let ([,t ,e])
(case-lambda ,lambda-info
(clause (,t* ...) ,iface
(foreign-call ,info ,t ,t* ...))))))]
[(fcallable ,info ,[e])
(%primcall #f #f $instantiate-code-object
(fcallable ,info)
(quote 0) ; hard-wiring "cookie" to 0
,e)]))
(define-pass np-recognize-loops : L4.75 (ir) -> L4.875 ()
; TODO: also recognize andmap/for-all, ormap/exists, for-each
; and remove inline handlers
(definitions
(define make-assigned-tmp
(lambda (x)
(let ([t (make-tmp 'tloop)])
(uvar-assigned! t #t)
t))))
(Expr : Expr (ir [tail* '()]) -> Expr ()
[,x (uvar-referenced! x #t) (uvar-loop! x #f) x]
[(letrec ([,x1 (case-lambda ,info1
(clause (,x* ...) ,interface
,body))])
(call ,info2 ,mdcl ,x2 ,e* ...))
(guard (eq? x2 x1) (eq? (length e*) interface))
(uvar-referenced! x1 #f)
(uvar-loop! x1 #t)
(let ([tref?* (map uvar-referenced? tail*)])
(for-each (lambda (x) (uvar-referenced! x #f)) tail*)
(let ([e* (map (lambda (e) (Expr e '())) e*)]
[body (Expr body (cons x1 tail*))])
(let ([body-tref?* (map uvar-referenced? tail*)])
(for-each (lambda (x tref?) (when tref? (uvar-referenced! x #t))) tail* tref?*)
(if (uvar-referenced? x1)
(if (uvar-loop? x1)
(let ([t* (map make-assigned-tmp x*)])
`(let ([,t* ,e*] ...)
(loop ,x1 (,t* ...)
(let ([,x* ,t*] ...)
,body))))
(begin
(for-each (lambda (x body-tref?)
(when body-tref? (uvar-loop! x #f)))
tail* body-tref?*)
`(letrec ([,x1 (case-lambda ,info1
(clause (,x* ...) ,interface
,body))])
(call ,info2 ,mdcl ,x2 ,e* ...))))
`(let ([,x* ,e*] ...) ,body)))))]
[(letrec ([,x* ,[le*]] ...) ,[body])
`(letrec ([,x* ,le*] ...) ,body)]
[(call ,info ,mdcl ,x ,[e* '() -> e*] ...)
(guard (memq x tail*))
(uvar-referenced! x #t)
(let ([interface* (info-lambda-interface* (uvar-info-lambda x))])
(unless (and (fx= (length interface*) 1) (fx= (length e*) (car interface*)))
(uvar-loop! x #f)))
`(call ,info ,mdcl ,x ,e* ...)]
[(call ,info ,mdcl ,[e '() -> e] ,[e* '() -> e*] ...)
`(call ,info ,mdcl ,e ,e* ...)]
[(foreign-call ,info ,[e '() -> e] ,[e* '() -> e*] ...)
`(foreign-call ,info ,e ,e* ...)]
[(fcallable ,info) `(fcallable ,info)]
[(label ,l ,[body]) `(label ,l ,body)]
[(mvlet ,[e '() -> e] ((,x** ...) ,interface* ,[body*]) ...)
`(mvlet ,e ((,x** ...) ,interface* ,body*) ...)]
[(mvcall ,info ,[e1 '() -> e1] ,[e2 '() -> e2])
`(mvcall ,info ,e1 ,e2)]
[(let ([,x ,[e* '() -> e*]] ...) ,[body])
`(let ([,x ,e*] ...) ,body)]
[(case-lambda ,info ,[cl] ...) `(case-lambda ,info ,cl ...)]
[(quote ,d) `(quote ,d)]
[(if ,[e0 '() -> e0] ,[e1] ,[e2]) `(if ,e0 ,e1 ,e2)]
[(seq ,[e0 '() -> e0] ,[e1]) `(seq ,e0 ,e1)]
[(profile ,src) `(profile ,src)]
[(pariah) `(pariah)]
[,pr pr]
[else ($oops who "unexpected Expr ~s" ir)]))
(define-pass np-name-anonymous-lambda : L4.875 (ir) -> L5 ()
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ())
(Expr : Expr (ir) -> Expr ()
[(case-lambda ,info ,[cl] ...)
(let ([anon (make-tmp (or (let ([name (info-lambda-name info)])
(and name (string->symbol name)))
'anon))])
(uvar-info-lambda-set! anon info)
`(letrec ([,anon (case-lambda ,info ,cl ...)])
,anon))])
(nanopass-case (L4.875 CaseLambdaExpr) ir
[(case-lambda ,info ,[CaseLambdaClause : cl] ...) `(case-lambda ,info ,cl ...)]))
(define-pass np-convert-closures : L5 (x) -> L6 ()
(definitions
(define-record-type clinfo
(nongenerative)
(sealed #t)
(fields lid (mutable mask) (mutable fv*))
(protocol (lambda (n) (lambda (index) (n index 0 '())))))
(module (with-offsets)
(define set-offsets!
(lambda (x* index)
(do ([x* x* (cdr x*)] [index index (fx+ index 1)])
((null? x*) index)
(var-index-set! (car x*) index))))
(define-syntax with-offsets
(syntax-rules ()
[(_ index ?x* ?e1 ?e2 ...)
(identifier? #'index)
(let ([x* ?x*])
(let ([index (set-offsets! x* index)])
(let ([v (begin ?e1 ?e2 ...)])
(for-each (lambda (x) (var-index-set! x #f)) x*)
v)))])))
(define record-ref!
(lambda (x clinfo)
(let ([index (var-index x)])
(unless index (sorry! who "variable ~a lost its binding" x))
(when (fx< index (clinfo-lid clinfo))
(let ([mask (clinfo-mask clinfo)])
(unless (bitwise-bit-set? mask index)
(clinfo-mask-set! clinfo (bitwise-copy-bit mask index 1))
(clinfo-fv*-set! clinfo (cons x (clinfo-fv* clinfo))))))))))
(Expr : Expr (ir index clinfo) -> Expr ()
[,x (record-ref! x clinfo) x]
[(letrec ([,x* ,le*] ...) ,body)
(with-offsets index x*
(let loop ([le* le*] [rle* '()] [rfv** '()])
(if (null? le*)
`(closures ([,x* (,(reverse rfv**) ...) ,(reverse rle*)] ...)
,(Expr body index clinfo))
(let-values ([(le fv*) (CaseLambdaExpr (car le*) index clinfo)])
(loop (cdr le*) (cons le rle*) (cons fv* rfv**))))))]
[(let ([,x* ,[e*]] ...) ,body)
(with-offsets index x*
`(let ([,x* ,e*] ...) ,(Expr body index clinfo)))]
[(mvlet ,[e] ((,x** ...) ,interface* ,body*) ...)
`(mvlet ,e
((,x** ...)
,interface*
,(let f ([x** x**] [body* body*])
(if (null? x**)
'()
(cons
(with-offsets index (car x**)
(Expr (car body*) index clinfo))
(f (cdr x**) (cdr body*))))))
...)]
[(loop ,x (,x* ...) ,body)
(with-offsets index (cons x x*)
`(loop ,x (,x* ...) ,(Expr body index clinfo)))])
(CaseLambdaExpr : CaseLambdaExpr (ir index outer-clinfo) -> CaseLambdaExpr ()
[(case-lambda ,info ,cl* ...)
(let ([clinfo (make-clinfo index)])
(let ([cl* (map (lambda (cl) (CaseLambdaClause cl index clinfo)) cl*)])
(let ([fv* (clinfo-fv* clinfo)])
(for-each (lambda (x) (record-ref! x outer-clinfo)) fv*)
(values
`(case-lambda ,info ,cl* ...)
fv*))))])
(CaseLambdaClause : CaseLambdaClause (ir index parent-clinfo) -> CaseLambdaClause ()
[(clause (,x* ...) ,interface ,body)
(let ([clinfo (make-clinfo index)])
(with-offsets index x*
(let ([body (Expr body index clinfo)])
(let ([fv* (clinfo-fv* clinfo)])
(for-each (lambda (x) (record-ref! x parent-clinfo)) fv*)
`(clause (,x* ...) ,(if (null? fv*) #f (make-cpvar)) ,interface ,body)))))])
(let-values ([(le fv*) (CaseLambdaExpr x 0 (make-clinfo 0))])
(unless (null? fv*) (sorry! who "found unbound variables ~s" fv*))
le))
(define-pass np-optimize-direct-call : L6 (ir) -> L6 ()
(definitions
(define find-matching-clause
(lambda (len info kfixed kvariable kfail)
(if info
(let f ([interface* (info-lambda-interface* info)] [dcl* (info-lambda-dcl* info)])
(if (null? interface*)
(kfail)
(let ([interface (car interface*)])
(if (fx< interface 0)
(let ([nfixed (fxlognot interface)])
(if (fx>= len nfixed)
(kvariable nfixed (car dcl*))
(f (cdr interface*) (cdr dcl*))))
(if (fx= interface len)
(kfixed (car dcl*))
(f (cdr interface*) (cdr dcl*)))))))
(kfail)))))
(CaseLambdaExpr1 : CaseLambdaExpr (ir) -> * ()
[(case-lambda ,info ,cl* ...)
(info-lambda-well-known?-set! info #t)])
(CaseLambdaExpr2 : CaseLambdaExpr (ir) -> CaseLambdaExpr ())
(Expr : Expr (ir) -> Expr ()
[,x (let ([info (uvar-info-lambda x)])
(when info (info-lambda-well-known?-set! info #f))
x)]
[(closures ([,x* (,x** ...) ,le*] ...) ,body)
(for-each CaseLambdaExpr1 le*)
`(closures ([,x* (,x** ...) ,(map CaseLambdaExpr2 le*)] ...) ,(Expr body))]
[(loop ,x (,x* ...) ,body)
(uvar-location-set! x 'loop)
(let ([body (Expr body)])
(uvar-location-set! x #f)
`(loop ,x (,x* ...) ,body))]
[(call ,info ,mdcl ,x ,[e*] ...)
(guard (not (eq? (uvar-location x) 'loop)))
(if mdcl
(begin
; already a direct-call produced, e.g., by recognize-mrvs
(direct-call-label-referenced-set! mdcl #t)
`(call ,info ,mdcl ,x ,e* ...))
(find-matching-clause (length e*) (uvar-info-lambda x)
(lambda (dcl)
(direct-call-label-referenced-set! dcl #t)
`(call ,info ,dcl ,x ,e* ...))
(lambda (nfixed dcl)
(direct-call-label-referenced-set! dcl #t)
(let ([fixed-e* (list-head e* nfixed)] [rest-e* (list-tail e* nfixed)])
(let ([t* (map (lambda (x) (make-tmp 't)) fixed-e*)])
; evaluate fixed-e* first, before the rest list is created. rest-e* should
; be evaluated before as well assuming later passes handle calls correctly
`(let ([,t* ,fixed-e*] ...)
(call ,info ,dcl ,x ,t* ...
,(%primcall #f #f list ,rest-e* ...))))))
(lambda () `(call ,info #f ,(Expr x) ,e* ...))))])
(CaseLambdaExpr2 ir))
; this pass doesn't change the language, but it does add an extragrammatical
; restriction: each letrec is now strongly connected
(define-pass np-identify-scc : L6 (ir) -> L6 ()
(definitions
; returns a list of lists of strongly connected bindings sorted so that
; if a binding in some list binding1* binds a variable x that is in the
; free list of a binding in some other list binding2*, binding1* comes
; before binding2*.
(define-record-type binding
(fields le x x* (mutable link*) (mutable root) (mutable done))
(nongenerative)
(sealed #t)
(protocol
(lambda (new)
(lambda (le x x*)
(let ([b (new le x x* '() #f #f)])
(uvar-location-set! x b)
b)))))
(define (compute-sccs v*) ; Tarjan's algorithm
; adapted from cpletrec
(define scc* '())
(define (compute-sccs v)
(define index 0)
(define stack '())
(define (tarjan v)
(let ([v-index index])
(binding-root-set! v v-index)
(set! stack (cons v stack))
(set! index (fx+ index 1))
(for-each
(lambda (v^)
(unless (binding-done v^)
(unless (binding-root v^) (tarjan v^))
(binding-root-set! v (fxmin (binding-root v) (binding-root v^)))))
(binding-link* v))
(when (fx= (binding-root v) v-index)
(set! scc*
(cons
(let f ([ls stack])
(let ([v^ (car ls)])
(binding-done-set! v^ #t)
(cons v^ (if (eq? v^ v)
(begin (set! stack (cdr ls)) '())
(f (cdr ls))))))
scc*)))))
(tarjan v))
(for-each (lambda (v) (unless (binding-done v) (compute-sccs v))) v*)
(reverse scc*)))
(Expr : Expr (ir) -> Expr ()
[(closures ([,x* (,x** ...) ,[le*]] ...) ,[body])
; create bindings and set each uvar's location to the corresponding binding
(let ([b* (map make-binding le* x* x**)])
; establish links from each binding to the bindings of its free variables
(for-each
(lambda (b)
(binding-link*-set! b
(fold-left
(lambda (link* x)
(let ([loc (uvar-location x)])
(if (binding? loc)
(cons loc link*)
link*)))
'() (binding-x* b))))
b*)
; reset uvar locations
(for-each (lambda (b) (uvar-location-set! (binding-x b) #f)) b*)
; sort bindings into strongly connected components, then
; create one closure for each not-well-known binding,
; and one for all well-known bindings
(let f ([b** (compute-sccs b*)])
(if (null? b**)
body
(let ([b* (car b**)])
`(closures ([,(map binding-x b*) (,(map binding-x* b*) ...) ,(map binding-le b*)] ...)
,(f (cdr b**)))))))]))
(module (np-expand-closures np-expand/optimize-closures)
(define sort-bindings
; sort-bindings uses the otherwise unneeded info-lambda-seqno to put labels
; bindings in the same order whether we run np-expand/optimize-closures or
; just np-expand-closures, thus reducing code/icache layout differences and,
; when there are few other differences, eliminating spurious differences
; in run times. ultimately, we should try laying code objects out
; in some order that minimizes cache misses, whether at compile,
; load, or collection time.
(lambda (l* le*)
(define seqno
(lambda (p)
(let ([le (cdr p)])
(nanopass-case (L7 CaseLambdaExpr) le
[(case-lambda ,info ,cl* ...) (info-lambda-seqno info)]
[else 0]))))
(let ([ls (sort (lambda (x y) (< (seqno x) (seqno y))) (map cons l* le*))])
(values (map car ls) (map cdr ls)))))
(define-pass np-expand-closures : L6 (ir) -> L7 ()
(definitions
(define gl* '())
(define gle* '())
(define-record-type closure
(nongenerative)
(sealed #t)
(fields name label (mutable free*)))
(define-syntax with-uvar-location
(syntax-rules ()
[(_ ?uvar ?expr ?e)
(let ([uvar ?uvar])
(let ([old (uvar-location uvar)])
(uvar-location-set! uvar ?expr)
(let ([v ?e])
(uvar-location-set! uvar old)
v)))]))
(with-output-language (L7 Expr)
(define with-locations
(lambda (free* mcp body)
(if mcp
(let f ([free* free*] [i (constant closure-data-disp)])
(if (null? free*)
(Expr body)
(with-uvar-location (car free*) (%mref ,mcp ,i)
(f (cdr free*) (fx+ i (constant ptr-bytes))))))
(Expr body))))
(module (create-bindings create-inits)
(define (build-free-ref x) (or (uvar-location x) x))
(define create-bindings
(lambda (c* body)
(fold-right
(lambda (c body)
`(let ([,(closure-name c) ,(%constant-alloc type-closure
(fx* (fx+ (length (closure-free* c)) 1) (constant ptr-bytes)))])
,(%seq
(set! ,(%mref ,(closure-name c) ,(constant closure-code-disp))
(label-ref ,(closure-label c) ,(constant code-data-disp)))
,body)))
body
c*)))
(define create-inits
(lambda (c* body)
(fold-right
(lambda (c body)
(let f ([x* (closure-free* c)] [i (constant closure-data-disp)])
(if (null? x*)
body
(%seq
(set! ,(%mref ,(closure-name c) ,i) ,(build-free-ref (car x*)))
,(f (cdr x*) (fx+ i (constant ptr-bytes)))))))
body c*))))))
(CaseLambdaExpr : CaseLambdaExpr (ir c) -> CaseLambdaExpr ()
[(case-lambda ,info ,[cl*] ...)
(info-lambda-fv*-set! info (closure-free* c))
(info-lambda-closure-rep-set! info 'closure)
`(case-lambda ,info ,cl* ...)])
(CaseLambdaClause : CaseLambdaClause (ir c) -> CaseLambdaClause ()
[(clause (,x* ...) ,mcp ,interface ,body)
`(clause (,x* ...) ,mcp ,interface
,(with-locations (if c (closure-free* c) '()) mcp body))])
(Expr : Expr (ir) -> Expr ()
[(closures ([,x* (,x** ...) ,le*] ...) ,body)
(let* ([l* (map (lambda (x) (make-local-label (uvar-name x))) x*)]
[c* (map make-closure x* l* x**)])
(let ([le* (map CaseLambdaExpr le* c*)] [body (Expr body)])
(set! gl* (append l* gl*))
(set! gle* (append le* gle*))
(create-bindings c* (create-inits c* body))))]
[,x (or (uvar-location x) x)]
[(fcallable ,info)
(let ([label (make-local-label 'fcallable)])
(set! gl* (cons label gl*))
(set! gle* (cons (in-context CaseLambdaExpr `(fcallable ,info ,label)) gle*))
`(label-ref ,label 0))])
(nanopass-case (L6 CaseLambdaExpr) ir
[(case-lambda ,info ,[CaseLambdaClause : cl #f -> cl] ...)
(let ([l (make-local-label 'main)])
(let-values ([(gl* gle*) (sort-bindings gl* gle*)])
`(labels ([,gl* ,gle*] ... [,l (case-lambda ,info ,cl ...)]) ,l)))]))
(define-pass np-expand/optimize-closures : L6 (ir) -> L7 ()
(definitions
(module (add-original-closures! add-final-closures!
add-ref-counter add-create-and-alloc-counters
add-raw-counters with-raw-closure-ref-counter
with-was-closure-ref)
(include "types.ss")
(define add-create-and-alloc-counters
(lambda (c* e)
(if (track-dynamic-closure-counts)
(let f ([c* c*] [pair-count 0] [vector-count 0] [closure-count 0]
[vector-alloc-amount 0] [closure-alloc-amount 0]
[padded-vector-alloc-amount 0] [padded-closure-alloc-amount 0])
(if (null? c*)
(add-counter '#{pair-create-count bhowt6w0coxl0s2y-5} pair-count
(add-counter '#{vector-create-count bhowt6w0coxl0s2y-6} vector-count
(add-counter '#{closure-create-count bhowt6w0coxl0s2y-7} closure-count
(add-counter '#{vector-alloc-count bhowt6w0coxl0s2y-8} vector-alloc-amount
(add-counter '#{closure-alloc-count bhowt6w0coxl0s2y-9} closure-alloc-amount
(add-counter '#{padded-vector-alloc-count bhowt6w0coxl0s2y-11} padded-vector-alloc-amount
(add-counter '#{padded-closure-alloc-count bhowt6w0coxl0s2y-10} padded-closure-alloc-amount
e)))))))
(let ([c (car c*)])
(case (closure-type c)
[(pair) (f (cdr c*) (fx+ pair-count 1) vector-count closure-count
vector-alloc-amount closure-alloc-amount padded-vector-alloc-amount
padded-closure-alloc-amount)]
[(vector)
(let ([n (fx+ (length (closure-free* c)) 1)])
(f (cdr c*) pair-count (fx+ vector-count 1) closure-count
(fx+ vector-alloc-amount n) closure-alloc-amount
(fx+ padded-vector-alloc-amount (fxsll (fxsra (fx+ n 1) 1) 1))
padded-closure-alloc-amount))]
[(closure)
(let ([n (fx+ (length (closure-free* c)) 1)])
(f (cdr c*) pair-count vector-count (fx+ closure-count 1)
vector-alloc-amount (fx+ closure-alloc-amount n)
padded-vector-alloc-amount
(fx+ padded-closure-alloc-amount (fxsll (fxsra (fx+ n 1) 1) 1))))]
[else (f (cdr c*) pair-count vector-count closure-count
vector-alloc-amount closure-alloc-amount padded-vector-alloc-amount
padded-closure-alloc-amount)]))))
e)))
(define add-counter
(lambda (counter amount e)
(with-output-language (L7 Expr)
(%seq
,(%inline inc-profile-counter
,(%mref
(literal ,(make-info-literal #t 'object counter (constant symbol-value-disp)))
,(constant record-data-disp))
(quote ,amount))
,e))))
(define add-ref-counter
(lambda (e)
(if (track-dynamic-closure-counts)
(add-counter '#{ref-count bhowt6w0coxl0s2y-4} 1 e)
e)))
(define-syntax with-raw-closure-ref-counter
(syntax-rules ()
[(_ ?x ?e1 ?e2 ...)
(let ([expr (begin ?e1 ?e2 ...)])
(if (and (track-dynamic-closure-counts) (uvar-was-closure-ref? ?x))
(add-counter '#{raw-ref-count bhowt6w0coxl0s2y-1} 1 expr)
expr))]))
(define add-raw-counters
(lambda (free** e)
(if (track-dynamic-closure-counts)
(let f ([x** free**] [alloc 0] [raw 0])
(if (null? x**)
(add-counter '#{raw-create-count bhowt6w0coxl0s2y-2} (length free**)
(add-counter '#{raw-alloc-count bhowt6w0coxl0s2y-3} alloc
(add-counter '#{raw-ref-count bhowt6w0coxl0s2y-1} raw e)))
(let ([x* (car x**)])
(f (cdr x**) (fx+ alloc (length x*) 1)
(fold-left
(lambda (cnt x) (if (uvar-was-closure-ref? x) (fx+ cnt 1) cnt))
raw x*)))))
e)))
(define-syntax with-was-closure-ref
(syntax-rules ()
[(_ ?x* ?e1 ?e2 ...)
(let f ([x* ?x*])
(if (or (null? x*) (not (track-dynamic-closure-counts)))
(begin ?e1 ?e2 ...)
(let ([x (car x*)])
(let ([old-was-cr? (uvar-was-closure-ref? x)])
(uvar-was-closure-ref! x #t)
(let ([expr (f (cdr x*))])
(uvar-was-closure-ref! x old-was-cr?)
expr)))))]))
(define add-original-closures!
(lambda (free**)
(cond
[(track-static-closure-counts) =>
(lambda (ci)
(static-closure-info-raw-closure-count-set! ci
(fold-left (lambda (count free*)
(static-closure-info-raw-free-var-count-set! ci
(+ (static-closure-info-raw-free-var-count ci)
(length free*)))
(+ count 1))
(static-closure-info-raw-closure-count ci) free**)))])))
(define add-final-closures!
(lambda (c*)
(cond
[(track-static-closure-counts) =>
(lambda (ci)
(for-each
(lambda (c)
(let ([type (closure-type c)])
(if (closure-wk? c)
(case type
[(constant)
(static-closure-info-wk-empty-count-set! ci
(+ (static-closure-info-wk-empty-count ci) 1))]
[(singleton)
(static-closure-info-wk-single-count-set! ci
(+ (static-closure-info-wk-single-count ci) 1))]
[(pair)
(static-closure-info-wk-pair-count-set! ci
(+ (static-closure-info-wk-pair-count ci) 1))]
[(vector)
(static-closure-info-wk-vector-count-set! ci
(+ (static-closure-info-wk-vector-count ci) 1))
(static-closure-info-wk-vector-free-var-count-set! ci
(+ (static-closure-info-wk-vector-free-var-count ci)
(length (closure-free* c))))]
[(borrowed)
(static-closure-info-wk-borrowed-count-set! ci
(+ (static-closure-info-wk-borrowed-count ci) 1))]
[(closure)
(static-closure-info-nwk-closure-count-set! ci
(+ (static-closure-info-nwk-closure-count ci) 1))
(static-closure-info-nwk-closure-free-var-count-set! ci
(+ (static-closure-info-nwk-closure-free-var-count ci)
(length (closure-free* c))))]
[else (sorry! who "unexpected well-known closure type ~s" type)])
(case type
[(constant)
(static-closure-info-nwk-empty-count-set! ci
(+ (static-closure-info-nwk-empty-count ci) 1))]
[(closure)
(static-closure-info-nwk-closure-count-set! ci
(+ (static-closure-info-nwk-closure-count ci) 1))
(static-closure-info-nwk-closure-free-var-count-set! ci
(+ (static-closure-info-nwk-closure-free-var-count ci)
(length (closure-free* c))))]
[else (sorry! who "unexpected non-well-known closure type ~s" type)]))))
c*))]))))
(define gl* '())
(define gle* '())
(define-record-type binding
(fields l x x*)
(nongenerative)
(sealed #t)
(protocol
(lambda (new)
(lambda (l x x*)
(new l x x*)))))
(define binding-well-known?
(lambda (b)
(info-lambda-well-known?
(uvar-info-lambda
(binding-x b)))))
(define-record-type frob
(fields name (mutable expr) (mutable seen frob-seen? frob-seen!))
(nongenerative)
(sealed #t)
(protocol
(lambda (new)
(case-lambda
[(name expr) (new name expr #f)]
[(name expr seen) (new name expr seen)]))))
(define-record-type closure
(nongenerative)
(sealed #t)
(fields wk? name label b*
(mutable sibling*) (mutable free*) (mutable type)
(mutable seen closure-seen? closure-seen!)
(mutable borrowed-name))
(protocol
(lambda (new)
(lambda (wk? b*)
; must use name and label of first binding
(let ([b (car b*)])
(let ([c (new wk? (binding-x b) (binding-l b) b* '() '() #f #f #f)])
(for-each
(lambda (b) (uvar-location-set! (binding-x b) c))
b*)
c))))))
(module (make-bank deposit retain borrow)
; NB: borrowing is probably cubic at present
; might should represent bank as a prefix tree
(define sort-free
(lambda (free*)
(sort (lambda (x y) (fx< (var-index x) (var-index y))) free*)))
(define make-bank (lambda () '()))
(define deposit
; NB: if used when self-references are possible, remove (olosure-name c) from free*
(lambda (free* c bank)
(cons (cons (sort-free free*) c)
(cons (cons (sort-free (cons (closure-name c) free*)) c)
bank))))
(define retain
(lambda (name* bank)
(filter (lambda (a) (memq (closure-name (cdr a)) name*)) bank)))
(define borrow
; NB: if used when self-references are possible, remove (olosure-name c) from free*
(lambda (free* bank)
(let ([free* (sort-free free*)])
(cond
[(assoc free* bank) => cdr]
[else #f])))))
(module (with-offsets)
(define set-offsets!
(lambda (x* index)
(do ([x* x* (cdr x*)] [index index (fx+ index 1)])
((null? x*) index)
(var-index-set! (car x*) index))))
(define-syntax with-offsets
(syntax-rules ()
[(_ index ?x* ?e1 ?e2 ...)
(identifier? #'index)
(let ([x* ?x*])
(let ([index (set-offsets! x* index)])
(let ([v (begin ?e1 ?e2 ...)])
(for-each (lambda (x) (var-index-set! x #f)) x*)
v)))])))
(with-output-language (L7 Expr)
(module (create-bindings create-inits)
(define (build-free-ref x)
(let ([loc (uvar-location x)])
(when (eq? loc 'loop)
(sorry! who "found reference to loop variable outside call position" x))
(frob-expr loc)))
(define create-bindings
(lambda (c* body)
(fold-right
(lambda (c body)
(case (closure-type c)
; NB: the pair and vector cases can be done this way only if well-known
; NB: closures can be shared with each other and up to one non-well-known closure
[(pair)
`(let ([,(closure-name c) ,(%primcall #f #f cons ,(map build-free-ref (closure-free* c)) ...)])
,body)]
[(vector)
`(let ([,(closure-name c) ,(%primcall #f #f vector ,(map build-free-ref (closure-free* c)) ...)])
,body)]
[else
(safe-assert (eq? (closure-type c) 'closure))
`(let ([,(closure-name c) ,(%constant-alloc type-closure
(fx* (fx+ (length (closure-free* c)) 1) (constant ptr-bytes)))])
,(%seq
(set! ,(%mref ,(closure-name c) ,(constant closure-code-disp))
(label-ref ,(closure-label c) ,(constant code-data-disp)))
,body))]))
(add-create-and-alloc-counters c* body)
c*)))
(define create-inits
(lambda (c* body)
(fold-right
(lambda (c body)
(case (closure-type c)
[(closure)
(let f ([x* (closure-free* c)] [i (constant closure-data-disp)])
(if (null? x*)
body
(%seq
(set! ,(%mref ,(closure-name c) ,i) ,(build-free-ref (car x*)))
,(f (cdr x*) (fx+ i (constant ptr-bytes))))))]
[else body]))
body c*))))
(define-syntax with-frob-location
(syntax-rules ()
[(_ ?x ?expr ?e)
(let ([frob (uvar-location ?x)])
(let ([loc (frob-expr frob)])
(frob-expr-set! frob ?expr)
(let ([v ?e])
(frob-expr-set! frob loc)
v)))]))
(define with-locations
(lambda (type free* mcp body index bank)
(case type
[(singleton) (with-frob-location (car free*) mcp (Expr body index bank))]
[(pair)
(with-frob-location (car free*) (add-ref-counter (%mref ,mcp ,(constant pair-car-disp)))
(with-frob-location (cadr free*) (add-ref-counter (%mref ,mcp ,(constant pair-cdr-disp)))
(Expr body index bank)))]
[else
(safe-assert (memq type '(vector closure)))
(let f ([free* free*] [i (if (eq? type 'vector) (constant vector-data-disp) (constant closure-data-disp))])
(if (null? free*)
(Expr body index bank)
(with-frob-location (car free*) (add-ref-counter (%mref ,mcp ,i))
(f (cdr free*) (fx+ i (constant ptr-bytes))))))])))))
(CaseLambdaExpr : CaseLambdaExpr (ir index c bank) -> CaseLambdaExpr ()
[(case-lambda ,info ,cl* ...)
(info-lambda-fv*-set! info (closure-free* c))
(info-lambda-closure-rep-set! info (closure-type c))
`(case-lambda ,info
,(let ([bank (retain (closure-free* c) bank)])
(map (lambda (cl) (CaseLambdaClause cl index c bank)) cl*))
...)])
(CaseLambdaClause : CaseLambdaClause (ir index c bank) -> CaseLambdaClause ()
[(clause (,x* ...) ,mcp ,interface ,body)
(with-offsets index x*
(let ([type (if (and c mcp) (closure-type c) 'constant)])
(if (eq? type 'constant)
`(clause (,x* ...) #f ,interface ,(Expr body index bank))
`(clause (,x* ...) ,mcp ,interface
,(with-frob-location (closure-name c) mcp
(if (eq? type 'borrowed)
(with-frob-location (closure-borrowed-name c) mcp
(let ([free* (closure-free* c)])
(with-locations (if (fx= (length free*) 2) 'pair 'vector) free* mcp body index bank)))
(with-locations type (closure-free* c) mcp body index bank)))))))])
(Expr : Expr (ir index bank) -> Expr ()
[(closures ([,x* (,x** ...) ,le*] ...) ,body)
(with-offsets index x*
(safe-assert (andmap var-index x*)) ; should be bound now
(safe-assert (andmap (lambda (x*) (andmap var-index x*)) x**)) ; should either have already been bound, or are bound now
(add-original-closures! x**)
(let* ([x**-loc (map (lambda (x*) (map uvar-location x*)) x**)]
[l* (map (lambda (x) (make-local-label (uvar-name x))) x*)]
; create one closure for each not-well-known binding, and one for all well-known bindings
[c* (let-values ([(wk* !wk*) (partition binding-well-known? (map make-binding l* x* x**))])
(cond
[(null? wk*) (map (lambda (b) (make-closure #f (list b))) !wk*)]
[(null? !wk*) (list (make-closure #t wk*))]
[else
; putting one !wk* in with wk*. claim: if any of the closures is nonempty,
; all will be nonempty, so might as well allow wk* to share a !wk's closure.
; if all are empty, no harm done.
; TODO: there might be a more suitable !wk to pick than (car !wk*)
(cons
(make-closure #f (cons (car !wk*) wk*))
(map (lambda (b) (make-closure #f (list b))) (cdr !wk*)))]))]
[xc* (map uvar-location x*)])
; set up sibling* and initial free*
(for-each
(lambda (c)
(let fb ([b* (closure-b* c)] [free* '()] [sibling* '()])
(if (null? b*)
(begin
(closure-free*-set! c free*)
(closure-sibling*-set! c sibling*))
(let fx ([x* (binding-x* (car b*))] [free* free*] [sibling* sibling*])
(if (null? x*)
(fb (cdr b*) free* sibling*)
(let* ([x (car x*)] [loc (uvar-location x)])
(cond
[(not loc)
(let ([frob (make-frob x x #t)])
(uvar-location-set! x frob)
(fx (cdr x*) (cons x free*) sibling*)
(frob-seen! frob #f))]
[(frob? loc)
(if (or (frob-seen? loc) (not (frob-name loc)))
(fx (cdr x*) free* sibling*)
(begin
(frob-seen! loc #t)
(fx (cdr x*) (cons (frob-name loc) free*) sibling*)
(frob-seen! loc #f)))]
[(closure? loc)
(if (or (eq? loc c) (closure-seen? loc)) ; no reflexive links
(fx (cdr x*) free* sibling*)
(begin
(closure-seen! loc #t)
(fx (cdr x*) free* (cons (closure-name loc) sibling*))
(closure-seen! loc #f)))]
[else (sorry! who "unexpected uvar location ~s" loc)])))))))
c*)
; find closures w/free variables (non-constant closures) and propagate
(when (ormap (lambda (c) (not (null? (closure-free* c)))) c*)
(for-each
(lambda (c)
(closure-free*-set! c (append (closure-sibling* c) (closure-free* c))))
c*))
; determine each closure's representation & set uvar location frobs
(for-each
(lambda (c)
(let ([free* (closure-free* c)])
(let ([frob (cond
[(null? free*)
(closure-type-set! c 'constant)
(make-frob #f `(literal ,(make-info-literal #f 'closure (closure-label c) 0)))]
[(closure-wk? c)
(cond
[(fx= (length free*) 1)
(closure-type-set! c 'singleton)
(uvar-location (car free*))]
[(borrow free* bank) =>
(lambda (mc)
(closure-type-set! c 'borrowed)
(closure-borrowed-name-set! c (closure-name mc))
(closure-free*-set! c (closure-free* mc))
(uvar-location (closure-name mc)))]
[else
; NB: HACK
(set! bank (deposit free* c bank))
(closure-type-set! c (if (fx= (length free*) 2) 'pair 'vector))
(make-frob (closure-name c) (closure-name c))])]
[else
(closure-type-set! c 'closure)
(make-frob (closure-name c) (closure-name c))])])
(for-each
(lambda (b) (uvar-location-set! (binding-x b) frob))
(closure-b* c)))))
c*)
; NB: if we are not sharing, but we are borrowing, we need to ensure
; NB: all closure variables point to final frob, and not a closure record
; record static closure counts
(add-final-closures! c*)
; process subforms and rebuild
(fold-left (lambda (body le)
(nanopass-case (L6 CaseLambdaExpr) le
[(case-lambda ,info ,cl ...) body]))
(let ([le* (map (lambda (le xc x*) (with-was-closure-ref x* (CaseLambdaExpr le index xc bank)))
le* xc* x**)]
[body (Expr body index bank)])
(set! gl* (append l* gl*))
(set! gle* (append le* gle*))
(let ([c* (filter (lambda (c) (memq (closure-type c) '(pair closure vector))) c*)])
(let ([body (create-bindings c* (create-inits c* (add-raw-counters x** body)))])
; leave location clean for later passes
(for-each (lambda (x) (uvar-location-set! x #f)) x*)
(for-each (lambda (x* x*-loc) (for-each uvar-location-set! x* x*-loc)) x** x**-loc)
body)))
le*)))]
[,x (with-raw-closure-ref-counter x (cond [(uvar-location x) => frob-expr] [else x]))]
[(loop ,x (,x* ...) ,body)
(uvar-location-set! x 'loop)
(let ([body (with-offsets index x* (Expr body index bank))])
(uvar-location-set! x #f)
`(loop ,x (,x* ...) ,body))]
[(call ,info ,mdcl ,x ,[e*] ...)
(guard (eq? (uvar-location x) 'loop))
`(call ,info ,mdcl ,x ,e* ...)]
[(call ,info ,mdcl ,x ,[e*] ...)
(guard mdcl)
(with-raw-closure-ref-counter x
(cond
[(uvar-location x) =>
(lambda (frob)
(if (frob-name frob)
`(call ,info ,mdcl ,(frob-expr frob) ,e* ...)
`(call ,info ,mdcl #f ,e* ...)))]
[else `(call ,info ,mdcl ,x ,e* ...)]))]
[(fcallable ,info)
(let ([label (make-local-label 'fcallable)])
(set! gl* (cons label gl*))
(set! gle* (cons (in-context CaseLambdaExpr `(fcallable ,info ,label)) gle*))
`(label-ref ,label 0))]
[(let ([,x* ,[e*]] ...) ,body)
(with-offsets index x*
`(let ([,x* ,e*] ...) ,(Expr body index bank)))]
[(mvlet ,[e] ((,x** ...) ,interface* ,body*) ...)
(let f ([var** x**] [body* body*] [rbody* '()])
(if (null? var**)
`(mvlet ,e ((,x** ...) ,interface* ,(reverse rbody*)) ...)
(f (cdr var**) (cdr body*) (cons (with-offsets index (car var**) (Expr (car body*) index bank)) rbody*))))])
(nanopass-case (L6 CaseLambdaExpr) ir
[(case-lambda ,info ,[CaseLambdaClause : cl 0 #f (make-bank) -> cl] ...)
(let ([l (make-local-label 'main)])
(let-values ([(gl* gle*) (sort-bindings gl* gle*)])
`(labels ([,gl* ,gle*] ... [,l (case-lambda ,info ,cl ...)]) ,l)))])))
(define-pass np-simplify-if : L7 (ir) -> L7 ()
(definitions
(define-$type-check (L7 Expr))
(with-output-language (L7 Expr)
; (and (fixnum? x1) ... (fixnum xn) e ...) => (and (fixnum? (logor x1 ... xn)) e ...)
; restricting fixnum? arguments to vars to avoid unnecessary computation
(define process-fixnum?
(lambda (info1 pr1 e x*)
(define build-fixnum?
(lambda (x*)
`(call ,info1 #f ,pr1
,(if (fx= (length x*) 1)
(car x*)
(%primcall #f #f fxlogor ,x* ...)))))
(let f ([e e] [x* x*])
(nanopass-case (L7 Expr) e
[(if (call ,info1 ,mdcl ,pr1 ,x1) ,e2 (quote ,d))
(guard (eq? mdcl #f) (eq? (primref-name pr1) 'fixnum?) (eq? d #f))
(f e2 (cons x1 x*))]
[(call ,info1 ,mdcl ,pr1 ,x1)
(guard (eq? mdcl #f) (eq? (primref-name pr1) 'fixnum?))
(build-fixnum? (cons x1 x*))]
[else `(if ,(build-fixnum? x*) ,(Expr e) (quote #f))]))))
(define process-paired-predicate
(lambda (info1 pr1 pr2 x-arg)
(let ([pr1 (primref-name pr1)] [pr2 (primref-name pr2)])
(cond
[(and (eq? pr1 'integer?) (eq? pr2 'exact?))
`(if ,(%primcall #f #f fixnum? ,x-arg) (quote #t) ,(%primcall #f #f bignum? ,x-arg))]
[(and (eq? pr1 'port?) (eq? pr2 'binary-port?))
(%typed-object-check mask-binary-port type-binary-port ,x-arg)]
[(and (eq? pr1 'port?) (eq? pr2 'textual-port?))
(%typed-object-check mask-textual-port type-textual-port ,x-arg)]
[(and (eq? pr1 'input-port?) (eq? pr2 'binary-port?))
(%typed-object-check mask-binary-input-port type-binary-input-port ,x-arg)]
[(and (eq? pr1 'input-port?) (eq? pr2 'textual-port?))
(%typed-object-check mask-textual-input-port type-textual-input-port ,x-arg)]
[(and (eq? pr1 'output-port?) (eq? pr2 'binary-port?))
(%typed-object-check mask-binary-output-port type-binary-output-port ,x-arg)]
[(and (eq? pr1 'output-port?) (eq? pr2 'textual-port?))
(%typed-object-check mask-textual-output-port type-textual-output-port ,x-arg)]
[else #f]))))))
(Expr : Expr (ir) -> Expr ()
[(if (call ,info1 ,mdcl ,pr1 ,x1) ,e2 (quote ,d))
(guard (eq? d #f) (eq? mdcl #f))
(if (eq? (primref-name pr1) 'fixnum?)
(process-fixnum? info1 pr1 e2 (list x1))
(or (and (nanopass-case (L7 Expr) e2
[(if (call ,info5 ,mdcl5 ,pr2 ,x2) ,e2 (quote ,d))
(guard (eq? x2 x1) (eq? mdcl5 #f) (eq? d #f))
(let ([e-paired-pred (process-paired-predicate info1 pr1 pr2 x1)])
(and e-paired-pred `(if ,e-paired-pred ,(Expr e2) (quote #f))))]
[(call ,info4 ,mdcl4 ,pr2 ,x2)
(guard (eq? x2 x1) (eq? mdcl4 #f))
(process-paired-predicate info1 pr1 pr2 x1)]
[else #f]))
`(if (call ,info1 ,mdcl ,pr1 ,x1) ,(Expr e2) (quote ,d))))]))
(module (np-profile-unroll-loops)
(define-syntax mvmap
(lambda (x)
(syntax-case x ()
[(_ ?n ?proc ?ls1 ?ls2 ...)
(let ([n (datum ?n)])
(unless (and (fixnum? n) (fx>= n 0)) (syntax-error #'?n "invalid return-value count"))
(let ([foo* (make-list n)])
(with-syntax ([(ls2 ...) (generate-temporaries #'(?ls2 ...))]
[(out ...) (generate-temporaries foo*)]
[(out* ...) (generate-temporaries foo*)])
#'(let ([proc ?proc])
(let f ([ls1 ?ls1] [ls2 ?ls2] ...)
(if (null? ls1)
(let ([out '()] ...) (values out ...))
(let-values ([(out ...) (proc (car ls1) (car ls2) ...)]
[(out* ...) (f (cdr ls1) (cdr ls2) ...)])
(values (cons out out*) ...))))))))])))
(define-who loop-unroll-limit
($make-thread-parameter
0 ; NB: disabling loop unrolling for now
(lambda (x)
(cond
[(fixnum? x) x]
[else ($oops who "~s is not a fixnum" x)]))))
(define PATH-SIZE-LIMIT 100)
;; NB: this comment is no longer accurate
;; Code growth computation is a little restrictive since it's measured
;; per loop... but maybe since new-size is weighted when profiling is
;; enabled it's fine.
#;(define CODE-GROWTH-FACTOR (fx1+ (loop-unroll-limit)))
(define-syntax delay
(syntax-rules ()
[(_ x) (lambda () x)]))
(define (force x) (if (procedure? x) (x) x))
(define-who analyze-loops ;; -> (lambda () body) size new-weighted-size
(lambda (body path-size unroll-count)
(with-output-language (L7 Expr)
;; Not really a loop, just didn't want to pass around path-size and unroll-count when unnecessary
(let loop ([body body])
(if (not body)
(values #f 0 0)
(nanopass-case (L7 Expr) body
[(literal ,info) (values body 0 0)]
[(immediate ,imm) (values body 0 0)]
[(quote ,d) (values body 0 0)]
[(goto ,l) (values body 1 1)]
[(mref ,[loop : e1 -> e1-promise e1-size e1-new-size] ,[loop : e2 -> e2-promise e2-size e2-new-size] ,imm)
(values (delay `(mref ,(force e1-promise) ,(force e2-promise) ,imm))
(fx+ e1-size e2-size 1)
(fx+ e1-new-size e2-new-size 1))]
[,lvalue (values body 1 1)]
[(profile ,src) (values body 0 0)]
[(pariah) (values body 0 0)]
[(label-ref ,l ,offset) (values body 0 0)]
[,pr (values body 1 1)]
[(inline ,info ,prim ,[loop : e* -> e*-promise size* new-size*] ...)
(values (delay `(inline ,info ,prim ,(map force e*-promise) ...))
(apply fx+ size*)
(apply fx+ new-size*))]
[(values ,info ,[loop : e* -> e*-promise size* new-size*] ...)
(values (delay `(values ,info ,(map force e*-promise) ...))
(apply fx+ size*)
(apply fx+ new-size*))]
[(call ,info ,mdcl ,x ,[loop : e* -> e*-promise size* new-size*] ...)
(guard (uvar-location x))
;; NB: Magic formulas, using number assuming query-count \in [0,1000]
(let* ([src (info-call-src info)]
[query-count (if src (profile-query-weight src) #f)]
;; don't bother with unimportant loops (less than 1% count relative to max)
[query-count (if (or (not query-count) (< query-count .1)) 0 (exact (truncate (* query-count 1000))))]
;; allow path-size to increase up to 300
[adjusted-path-size-limit (fx+ PATH-SIZE-LIMIT (fx/ (or query-count 0) 5))]
;; allow unroll limit to increase up to 4
[adjusted-unroll-limit (fx+ (loop-unroll-limit) (fx/ (or query-count 0) 300))])
(if (or (fxzero? query-count)
(fxzero? (fx+ unroll-count adjusted-unroll-limit))
(fx> path-size adjusted-path-size-limit))
(begin
(values (delay `(call ,info ,mdcl ,x ,(map force e*-promise) ...))
(fx1+ (apply fx+ size*))
(fx1+ (apply fx+ new-size*))))
(let*-values ([(var*) (car (uvar-location x))]
[(loop-body-promise body-size new-size) (analyze-loops (cdr (uvar-location x)) (fx1+ path-size) (fx1- unroll-count))]
[(new-size) ((lambda (x) (if query-count (fx/ x query-count) x)) (fx+ (length e*-promise) new-size))]
[(acceptable-new-size) (fx* (fx1+ adjusted-unroll-limit) body-size)])
;; NB: trying code growth computation here, where it could be per call site.
(values
(if (<= new-size acceptable-new-size)
(delay (fold-left
(lambda (body var e-promise)
`(seq (set! ,var ,(force e-promise)) ,body))
(rename-loop-body (force loop-body-promise))
var* e*-promise))
body)
(fx1+ (apply fx+ size*))
;; pretend the new size is smaller for important loops
new-size))))]
[(call ,info ,mdcl ,pr ,e* ...)
(let-values ([(e*-promise size* new-size*) (mvmap 3 (lambda (e) (analyze-loops e (fx1+ path-size) unroll-count)) e*)])
(values (delay `(call ,info ,mdcl ,pr ,(map force e*-promise) ...))
(fx+ 2 (apply fx+ size*))
(fx+ 2 (apply fx+ new-size*))))]
[(call ,info ,mdcl ,e ,e* ...)
(let-values ([(e-promise e-size e-new-size) (loop e)]
[(e*-promise size* new-size*) (mvmap 3 (lambda (e) (analyze-loops e (fx1+ path-size) unroll-count)) e*)])
(values (delay `(call ,info ,mdcl ,(force e-promise) ,(map force e*-promise) ...))
(fx+ 5 e-size (apply fx+ size*))
(fx+ 5 e-new-size (apply fx+ new-size*))))]
[(foreign-call ,info ,[loop : e -> e-promise e-size e-new-size] ,[loop : e* -> e*-promise size* new-size*] ...)
(values (delay `(foreign-call ,info ,(force e-promise) ,(map force e*-promise) ...))
(fx+ 5 e-size (apply fx+ size*))
(fx+ 5 e-new-size (apply fx+ new-size*)))]
[(label ,l ,[loop : body -> e size new-size])
(values (delay `(label ,l ,(force e))) size new-size)]
[(mvlet ,[loop : e -> e-promise e-size e-new-size] ((,x** ...) ,interface* ,body*) ...)
(let-values ([(body*-promise body*-size body*-new-size) (mvmap 3 (lambda (e) (analyze-loops e (fx+ e-size path-size) unroll-count)) body*)])
(values (delay `(mvlet ,(force e-promise) ((,x** ...) ,interface* ,(map force body*-promise)) ...))
(fx+ e-size (apply fx+ body*-size))
(fx+ e-new-size (apply fx+ body*-new-size))))]
[(mvcall ,info ,e1 ,e2)
(let-values ([(e1-promise e1-size e1-new-size) (analyze-loops e1 (fx+ 5 e1) unroll-count)]
[(e2-promise e2-size e2-new-size) (analyze-loops e2 (fx+ 5 e2) unroll-count)])
(values (delay `(mvcall ,info ,(force e1-promise) ,(force e2-promise)))
(fx+ 5 e1-size e2-size)
(fx+ 5 e1-new-size e2-new-size)))]
[(let ([,x* ,[loop : e* -> e*-promise size* new-size*]] ...) ,body)
(let-values ([(body-promise body-size body-new-size) (analyze-loops body (fx+ path-size (apply fx+ size*)) unroll-count)])
(values (delay `(let ([,x* ,(map force e*-promise)] ...) ,(force body-promise)))
(fx+ 1 body-size (apply fx+ size*))
(fx+ 1 body-new-size (apply fx+ new-size*))))]
[(if ,[loop : e0 -> e0-promise e0-size e0-new-size] ,e1 ,e2)
(let-values ([(e1-promise e1-size e1-new-size) (analyze-loops e1 (fx+ path-size e0-size) unroll-count)]
[(e2-promise e2-size e2-new-size) (analyze-loops e2 (fx+ path-size e0-size) unroll-count)])
(values (delay `(if ,(force e0-promise) ,(force e1-promise) ,(force e2-promise)))
(fx+ e0-size e1-size e2-size)
(fx+ e0-new-size e1-new-size e2-new-size)))]
[(seq ,[loop : e0 -> e0-promise e0-size e0-new-size] ,e1)
(let-values ([(e1-promise e1-size e1-new-size) (analyze-loops e1 (fx+ path-size e0-size) unroll-count)])
(values (delay `(seq ,(force e0-promise) ,(force e1-promise)))
(fx+ e0-size e1-size)
(fx+ e0-new-size e1-new-size)))]
[(set! ,lvalue ,[loop : e -> e-promise e-size e-new-size])
(values (delay `(set! ,lvalue ,(force e-promise)))
(fx+ 1 e-size)
(fx+ 1 e-new-size))]
[(alloc ,info ,[loop : e -> e-promise e-size e-new-size])
(values (delay `(alloc ,info ,(force e-promise)))
(fx+ 1 e-size)
(fx+ 1 e-new-size))]
[(loop ,x (,x* ...) ,[loop : body -> body-promise body-size body-new-size])
;; NB: Handling of inner loops?
(values (delay `(loop ,x (,x* ...) ,(force body-promise)))
body-size
body-new-size)]
[else ($oops who "forgot a case: ~a" body)]))))))
(define-pass rename-loop-body : (L7 Expr) (ir) -> (L7 Expr) ()
(definitions
(define-syntax with-fresh
(syntax-rules ()
[(_ rename-ht x* body)
(let* ([x* x*]
[rename-ht (hashtable-copy rename-ht #t)]
[x* (let ([t* (map (lambda (x) (make-tmp (uvar-name x))) x*)])
(for-each (lambda (x t) (eq-hashtable-set! rename-ht x t)) x* t*)
t*)])
body)])))
(Lvalue : Lvalue (ir rename-ht) -> Lvalue ()
[,x (eq-hashtable-ref rename-ht x x)]
[(mref ,[e1] ,[e2] ,imm) `(mref ,e1 ,e2 ,imm)])
(Expr : Expr (ir rename-ht) -> Expr ()
[(loop ,x (,[Lvalue : x* rename-ht -> x*] ...) ,body)
;; NB: with-fresh is so well designed that it can't handle this case
(let*-values ([(x) (list x)]
[(x body) (with-fresh rename-ht x (values (car x) (Expr body rename-ht)))])
`(loop ,x (,x* ...) ,body))]
[(let ([,x* ,[e*]] ...) ,body)
(with-fresh rename-ht x*
`(let ([,x* ,e*] ...) ,(Expr body rename-ht)))]
[(mvlet ,[e] ((,x** ...) ,interface* ,body*) ...)
(let* ([x**/body* (map (lambda (x* body)
(with-fresh rename-ht x* (cons x* (Expr body rename-ht))))
x** body*)]
[x** (map car x**/body*)]
[body* (map cdr x**/body*)])
`(mvlet ,e ((,x** ...) ,interface* ,body*) ...))])
(Expr ir (make-eq-hashtable)))
(define-pass np-profile-unroll-loops : L7 (ir) -> L7 ()
(Expr : Expr (ir) -> Expr ()
[(loop ,x (,x* ...) ,body)
(uvar-location-set! x (cons x* body))
(let-values ([(e-promise size new-size) (analyze-loops body 0 (loop-unroll-limit))])
(uvar-location-set! x #f)
;; NB: Not fx
`(loop ,x (,x* ...) ,(force e-promise))
;; trying out code-growth computation higher up
#;(if (<= new-size (* size CODE-GROWTH-FACTOR))
(begin
#;(printf "Opt: ~a\n" x)
`(loop ,x (,x* ...) ,(force e-promise)))
(begin
#;(printf "New size: ~a, old size: ~a\n" new-size size)
ir)))]))
(set! $loop-unroll-limit loop-unroll-limit))
(define target-fixnum?
(if (and (= (constant most-negative-fixnum) (most-negative-fixnum))
(= (constant most-positive-fixnum) (most-positive-fixnum)))
fixnum?
(lambda (x)
(and (or (fixnum? x) (bignum? x))
(<= (constant most-negative-fixnum) x (constant most-positive-fixnum))))))
(define unfix
(lambda (imm)
(ash imm (fx- (constant fixnum-offset)))))
(define fix
(lambda (imm)
(ash imm (constant fixnum-offset))))
(define ptr->imm
(lambda (x)
(cond
[(eq? x #f) (constant sfalse)]
[(eq? x #t) (constant strue)]
[(eq? x (void)) (constant svoid)]
[(null? x) (constant snil)]
[(eof-object? x) (constant seof)]
[($unbound-object? x) (constant sunbound)]
[(bwp-object? x) (constant sbwp)]
[(target-fixnum? x) (fix x)]
[(char? x) (+ (* (constant char-factor) (char->integer x)) (constant type-char))]
[else #f])))
(define-syntax ref-reg
(lambda (x)
(syntax-case x ()
[(k reg)
(identifier? #'reg)
(if (real-register? (datum reg))
#'reg
(with-implicit (k %mref) #`(%mref ,%tc ,(tc-disp reg))))])))
; TODO: recognize a direct call when it is at the end of a sequence, closures, or let form
; TODO: push call into if? (would need to pull arguments into temporaries to ensure order of evaluation
; TODO: how does this interact with mvcall?
(module (np-expand-primitives)
(define-threaded new-l*)
(define-threaded new-le*)
(define ht2 (make-hashtable symbol-hash eq?))
(define ht3 (make-hashtable symbol-hash eq?))
(define handle-prim
(lambda (src sexpr level name e*)
(let ([handler (or (and (fx= level 3) (symbol-hashtable-ref ht3 name #f))
(symbol-hashtable-ref ht2 name #f))])
(and handler (handler src sexpr e*)))))
(define-syntax Symref
(lambda (x)
(syntax-case x ()
[(k ?sym)
(with-implicit (k quasiquote)
#'`(literal ,(make-info-literal #t 'object ?sym (constant symbol-value-disp))))])))
(define-pass np-expand-primitives : L7 (ir) -> L9 ()
(Program : Program (ir) -> Program ()
[(labels ([,l* ,le*] ...) ,l)
(fluid-let ([new-l* '()] [new-le* '()])
(let ([le* (map CaseLambdaExpr le*)])
`(labels ([,l* ,le*] ... [,new-l* ,new-le*] ...) ,l)))])
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ())
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ())
(Expr : Expr (ir) -> Expr ()
[(quote ,d)
(cond
[(ptr->imm d) => (lambda (i) `(immediate ,i))]
[else `(literal ,(make-info-literal #f 'object d 0))])]
[,pr (Symref (primref-name pr))]
[(call ,info0 ,mdcl0
(call ,info1 ,mdcl1 ,pr (quote ,d))
,[e*] ...)
(guard (and (eq? (primref-name pr) '$top-level-value) (symbol? d)))
`(call ,info0 ,mdcl0 ,(Symref d) ,e* ...)]
[(call ,info ,mdcl ,pr ,e* ...)
(cond
[(handle-prim (info-call-src info) (info-call-sexpr info) (primref-level pr) (primref-name pr) e*) => Expr]
[else
(let ([e* (map Expr e*)])
; NB: expand calls through symbol top-level values similarly
(let ([info (if (any-set? (prim-mask abort-op) (primref-flags pr))
(make-info-call (info-call-src info) (info-call-sexpr info) (info-call-check? info) #t #t)
info)])
`(call ,info ,mdcl ,(Symref (primref-name pr)) ,e* ...)))])]))
(define-who unhandled-arity
(lambda (name args)
(sorry! who "unhandled argument count ~s for ~s" (length args) 'name)))
(with-output-language (L7 Expr)
(define-$type-check (L7 Expr))
(define-syntax define-inline
(let ()
(define ctht2 (make-hashtable symbol-hash eq?))
(define ctht3 (make-hashtable symbol-hash eq?))
(define check-and-record
(lambda (level name)
(let ([a (symbol-hashtable-cell (if (fx= level 2) ctht2 ctht3) (syntax->datum name) #f)])
(when (cdr a) (syntax-error name "duplicate inline"))
(set-cdr! a #t))))
(lambda (x)
(define compute-interface
(lambda (clause)
(syntax-case clause ()
[(x e1 e2 ...) (identifier? #'x) -1]
[((x ...) e1 e2 ...) (length #'(x ...))]
[((x ... . r) e1 e2 ...) (fxlognot (length #'(x ...)))])))
(define bitmaskify
(lambda (i*)
(fold-left (lambda (mask i)
(logor mask (if (fx< i 0) (ash -1 (fxlognot i)) (ash 1 i))))
0 i*)))
(syntax-case x ()
[(k level id clause ...)
(identifier? #'id)
(let ([level (datum level)] [name (datum id)])
(unless (memv level '(2 3))
(syntax-error x (format "invalid level ~s in inline definition" level)))
(let ([pr ($sgetprop name (if (eqv? level 2) '*prim2* '*prim3*) #f)])
(include "primref.ss")
(unless pr
(syntax-error x (format "unrecognized primitive name ~s in inline definition" name)))
(let ([arity (primref-arity pr)])
(when arity
(unless (= (bitmaskify arity) (bitmaskify (map compute-interface #'(clause ...))))
(syntax-error x (format "arity mismatch for ~s" name))))))
(check-and-record level #'id)
(with-implicit (k src sexpr moi)
#`(symbol-hashtable-set! #,(if (eqv? level 2) #'ht2 #'ht3) 'id
(rec moi
(lambda (src sexpr args)
(apply (case-lambda clause ... [rest #f]) args))))))]))))
(define no-need-to-bind?
(lambda (multiple-ref? e)
(nanopass-case (L7 Expr) e
[,x (if (uvar? x) (not (uvar-assigned? x)) (eq? x %zero))]
[(immediate ,imm) #t] ; might should produce binding if imm is large
[(quote ,d) (or (not multiple-ref?) (ptr->imm d))]
[,pr (not multiple-ref?)]
[(literal ,info) (and (not multiple-ref?) (not (info-literal-indirect? info)))]
[(profile ,src) #t]
[(pariah) #t]
[else #f])))
(define binder
(lambda (multiple-ref? type e)
(if (no-need-to-bind? multiple-ref? e)
(values e values)
(let ([t (make-tmp 't type)])
(values t
(lambda (body)
`(let ([,t ,e]) ,body)))))))
(define list-binder
(lambda (multiple-ref? type e*)
(if (null? e*)
(values '() values)
(let-values ([(e dobind) (binder multiple-ref? type (car e*))]
[(e* dobind*) (list-binder multiple-ref? type (cdr e*))])
(values (cons e e*)
(lambda (body)
(dobind (dobind* body))))))))
(define-syntax $bind
(lambda (x)
(syntax-case x ()
[(_ binder multiple-ref? type (b ...) e)
(let ([t0* (generate-temporaries #'(b ...))])
(let f ([b* #'(b ...)] [t* t0*] [x* '()])
(if (null? b*)
(with-syntax ([(x ...) (reverse x*)] [(t ...) t0*])
#`(let ([x t] ...) e))
(syntax-case (car b*) ()
[x (identifier? #'x)
#`(let-values ([(#,(car t*) dobind) (binder multiple-ref? 'type x)])
(dobind #,(f (cdr b*) (cdr t*) (cons #'x x*))))]
[(x e) (identifier? #'x)
#`(let-values ([(#,(car t*) dobind) (binder multiple-ref? 'type e)])
(dobind #,(f (cdr b*) (cdr t*) (cons #'x x*))))]))))])))
(define-syntax bind
(syntax-rules ()
[(_ multiple-ref? type (b ...) e)
(identifier? #'type)
($bind binder multiple-ref? type (b ...) e)]
[(_ multiple-ref? (b ...) e)
($bind binder multiple-ref? ptr (b ...) e)]))
(define-syntax list-bind
(syntax-rules ()
[(_ multiple-ref? type (b ...) e)
(identifier? #'type)
($bind list-binder multiple-ref? type (b ...) e)]
[(_ multiple-ref? (b ...) e)
($bind list-binder multiple-ref? ptr (b ...) e)]))
(define-syntax build-libcall
(lambda (x)
(syntax-case x ()
[(k pariah? src sexpr name e ...)
(let ([libspec ($sgetprop (datum name) '*libspec* #f)])
(define interface-okay?
(lambda (interface* cnt)
(ormap
(lambda (interface)
(if (fx< interface 0)
(fx>= cnt (lognot interface))
(fx= cnt interface)))
interface*)))
(unless libspec (syntax-error x "unrecognized library routine"))
(unless (eqv? (length #'(e ...)) (libspec-interface libspec))
(syntax-error x "invalid number of arguments"))
(let ([is-pariah? (datum pariah?)])
(unless (boolean? is-pariah?)
(syntax-error x "pariah indicator must be a boolean literal"))
(when (and (libspec-error? libspec) (not is-pariah?))
(syntax-error x "pariah indicator is inconsistent with libspec-error indicator"))
(with-implicit (k quasiquote)
(with-syntax ([body #`(call ,(make-info-call src sexpr #f pariah? #,(libspec-error? libspec)) #f
(literal ,(make-info-literal #f 'library '#,(datum->syntax #'* libspec) 0))
,e ...)])
(if is-pariah?
#'`(seq (pariah) body)
#'`body)))))])))
(define constant?
(case-lambda
[(x)
(nanopass-case (L7 Expr) x
[(quote ,d) #t]
; TODO: handle immediate?
[else #f])]
[(pred? x)
(nanopass-case (L7 Expr) x
[(quote ,d) (pred? d)]
; TODO: handle immediate?
[else #f])]))
(define constant-value
(lambda (x)
(nanopass-case (L7 Expr) x
[(quote ,d) d]
; TODO: handle immediate if constant? does
[else #f])))
(define maybe-add-label
(lambda (Llib body)
(if Llib
`(label ,Llib ,body)
body)))
(define build-and
(lambda (e1 e2)
`(if ,e1 ,e2 ,(%constant sfalse))))
(define build-simple-or
(lambda (e1 e2)
`(if ,e1 ,(%constant strue) ,e2)))
(define build-fix
(lambda (e)
(%inline sll ,e ,(%constant fixnum-offset))))
(define build-unfix
(lambda (e)
(nanopass-case (L7 Expr) e
[(quote ,d) (guard (target-fixnum? d)) `(immediate ,d)]
[else (%inline sra ,e ,(%constant fixnum-offset))])))
(define build-not
(lambda (e)
`(if ,e ,(%constant sfalse) ,(%constant strue))))
(define build-null?
(lambda (e)
(%type-check mask-nil snil ,e)))
(define build-eq?
(lambda (e1 e2)
(%inline eq? ,e1 ,e2)))
(define build-eqv?
(lambda (src sexpr e1 e2)
(build-libcall #f src sexpr eqv? e1 e2)))
(define make-build-eqv?
(lambda (src sexpr)
(lambda (e1 e2)
(build-eqv? src sexpr e1 e2))))
(define fixnum-constant?
(lambda (e)
(constant? target-fixnum? e)))
(define expr->index
(lambda (e alignment limit)
(nanopass-case (L7 Expr) e
[(quote ,d)
(and (target-fixnum? d)
(>= d 0)
(< d limit)
(fxzero? (logand d (fx- alignment 1)))
d)]
[else #f])))
(define build-fixnums?
(lambda (e*)
(let ([e* (remp fixnum-constant? e*)])
(if (null? e*)
`(quote #t)
(%type-check mask-fixnum type-fixnum
,(fold-left (lambda (e1 e2) (%inline logor ,e1 ,e2))
(car e*) (cdr e*)))))))
(define build-flonums?
(lambda (e*)
(let ([e* (remp (lambda (e) (constant? flonum? e)) e*)])
(if (null? e*)
`(quote #t)
(let f ([e* e*])
(let ([e (car e*)] [e* (cdr e*)])
(let ([check (%type-check mask-flonum type-flonum ,e)])
(if (null? e*)
check
(build-and check (f e*))))))))))
(define build-chars?
(lambda (e1 e2)
(define char-constant?
(lambda (e)
(constant? char? e)))
(if (char-constant? e1)
(if (char-constant? e2)
(%constant strue)
(%type-check mask-char type-char ,e2))
(if (char-constant? e2)
(%type-check mask-char type-char ,e1)
(build-and
(%type-check mask-char type-char ,e1)
(%type-check mask-char type-char ,e2))))))
(define build-list
(lambda (e*)
(if (null? e*)
(%constant snil)
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-pair (fx* (constant size-pair) (length e*)))])
(let loop ([e* e*] [i 0])
(let ([e (car e*)] [e* (cdr e*)])
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-car-disp))) ,e)
,(if (null? e*)
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-cdr-disp))) ,(%constant snil))
,t)
(let ([next-i (fx+ i (constant size-pair))])
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-cdr-disp)))
,(%inline + ,t (immediate ,next-i)))
,(loop e* next-i))))))))))))
(define build-pair?
(lambda (e)
(%type-check mask-pair type-pair ,e)))
(define build-car
(lambda (e)
(%mref ,e ,(constant pair-car-disp))))
(define build-cdr
(lambda (e)
(%mref ,e ,(constant pair-cdr-disp))))
(define build-char->integer
(lambda (e)
(%inline srl ,e
(immediate ,(fx- (constant char-data-offset) (constant fixnum-offset))))))
(define build-integer->char
(lambda (e)
(%inline +
,(%inline sll ,e
(immediate ,(fx- (constant char-data-offset) (constant fixnum-offset))))
,(%constant type-char))))
(define build-dirty-store
(case-lambda
[(base offset e) (build-dirty-store base %zero offset e)]
[(base index offset e) (build-dirty-store base index offset e
(lambda (base index offset e) `(set! ,(%mref ,base ,index ,offset) ,e))
(lambda (s r) `(seq ,s ,r)))]
[(base index offset e build-assign build-seq)
(if (nanopass-case (L7 Expr) e
[(quote ,d) (ptr->imm d)]
[else #f])
(build-assign base index offset e)
(let ([a (if (eq? index %zero)
(%lea ,base offset)
(%lea ,base ,index offset))])
; NB: should work harder to determine cases where x can't be a fixnum
(if (nanopass-case (L7 Expr) e
[(quote ,d) #t]
[(literal ,info) #t]
[else #f])
(bind #f ([e e])
; eval a second so the address is not live across any calls
(bind #t ([a a])
(build-seq
(build-assign a %zero 0 e)
(%inline remember ,a))))
(bind #t ([e e])
; eval a second so the address is not live across any calls
(bind #t ([a a])
(build-seq
(build-assign a %zero 0 e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(%constant svoid)
,(%inline remember ,a))))))))]))
(define make-build-cas
(lambda (old-v)
(lambda (base index offset v)
`(seq
,(%inline cas ,base ,index (immediate ,offset) ,old-v ,v)
(inline ,(make-info-condition-code 'eq? #f #t) ,%condition-code)))))
(define build-cas-seq
(lambda (cas remember)
`(if ,cas
(seq ,remember ,(%constant strue))
,(%constant sfalse))))
(define build-$record
(lambda (tag args)
(bind #f (tag)
(list-bind #f (args)
(let ([n (fx+ (length args) 1)])
(bind #t ([t (%constant-alloc type-typed-object (fx* n (constant ptr-bytes)))])
`(seq
(set! ,(%mref ,t ,(constant record-type-disp)) ,tag)
,(let f ([args args] [offset (constant record-data-disp)])
(if (null? args)
t
`(seq
(set! ,(%mref ,t ,offset) ,(car args))
,(f (cdr args) (fx+ offset (constant ptr-bytes)))))))))))))
(define build-$real->flonum
(lambda (src sexpr x who)
(if (constant? flonum? x)
x
(bind #t (x)
(bind #f (who)
`(if ,(%type-check mask-flonum type-flonum ,x)
,x
,(build-libcall #t src sexpr real->flonum x who)))))))
(define build-$inexactnum-real-part
(lambda (e)
(%lea ,e (fx+ (constant inexactnum-real-disp)
(fx- (constant type-flonum) (constant typemod))))))
(define build-$inexactnum-imag-part
(lambda (e)
(%lea ,e (fx+ (constant inexactnum-imag-disp)
(fx- (constant type-flonum) (constant typemod))))))
(define make-build-fill
(lambda (elt-bytes data-disp)
(define ptr-bytes (constant ptr-bytes))
(define super-size
(lambda (e-fill)
(define-who super-size-imm
(lambda (imm)
`(immediate
,(constant-case ptr-bytes
[(4)
(case elt-bytes
[(1) (let ([imm (logand imm #xff)])
(let ([imm (logor (ash imm 8) imm)])
(logor (ash imm 16) imm)))]
[(2) (let ([imm (logand imm #xffff)])
(logor (ash imm 16) imm))]
[else (sorry! who "unexpected elt-bytes ~s" elt-bytes)])]
[(8)
(case elt-bytes
[(1) (let ([imm (logand imm #xff)])
(let ([imm (logor (ash imm 8) imm)])
(let ([imm (logor (ash imm 16) imm)])
(logor (ash imm 32) imm))))]
[(2) (let ([imm (logand imm #xffff)])
(let ([imm (logor (ash imm 16) imm)])
(logor (ash imm 32) imm)))]
[(4) (let ([imm (logand imm #xffffffff)])
(logor (ash imm 32) imm))]
[else (sorry! who "unexpected elt-bytes ~s" elt-bytes)])]))))
(define-who super-size-expr
(lambda (e-fill)
(define (double e-fill k)
(%inline logor
,(%inline sll ,e-fill (immediate ,k))
,e-fill))
(define (mask e-fill k)
(%inline logand ,e-fill (immediate ,k)))
(constant-case ptr-bytes
[(4)
(case elt-bytes
[(1) (bind #t ([e-fill (mask e-fill #xff)])
(bind #t ([e-fill (double e-fill 8)])
(double e-fill 16)))]
[(2) (bind #t ([e-fill (mask e-fill #xffff)])
(double e-fill 16))]
[else (sorry! who "unexpected elt-bytes ~s" elt-bytes)])]
[(8)
(case elt-bytes
[(1) (bind #t ([e-fill (mask e-fill #xff)])
(bind #t ([e-fill (double e-fill 8)])
(bind #t ([e-fill (double e-fill 16)])
(double e-fill 32))))]
[(2) (bind #t ([e-fill (mask e-fill #xffff)])
(bind #t ([e-fill (double e-fill 16)])
(double e-fill 32)))]
[(4) (bind #t ([e-fill (mask e-fill #xffffffff)])
(double e-fill 32))]
[else (sorry! who "unexpected elt-bytes ~s" elt-bytes)])])))
(if (fx= elt-bytes ptr-bytes)
e-fill
(nanopass-case (L7 Expr) e-fill
[(quote ,d)
(cond
[(ptr->imm d) => super-size-imm]
[else (super-size-expr e-fill)])]
[(immediate ,imm) (super-size-imm imm)]
[else (super-size-expr e-fill)]))))
(lambda (e-vec e-bytes e-fill)
; NB: caller must bind e-vec and e-fill
(safe-assert (no-need-to-bind? #t e-vec))
(safe-assert (no-need-to-bind? #f e-fill))
(nanopass-case (L7 Expr) e-bytes
[(immediate ,imm)
(guard (fixnum? imm) (fx<= 0 imm (fx* 4 ptr-bytes)))
(if (fx= imm 0)
e-vec
(bind #t ([e-fill (super-size e-fill)])
(let f ([n (if (fx>= elt-bytes ptr-bytes)
imm
(fxlogand (fx+ imm (fx- ptr-bytes 1)) (fx- ptr-bytes)))])
(let ([n (fx- n ptr-bytes)])
`(seq
(set! ,(%mref ,e-vec ,(fx+ data-disp n)) ,e-fill)
,(if (fx= n 0) e-vec (f n)))))))]
[else
(let ([Ltop (make-local-label 'Ltop)] [t (make-assigned-tmp 't 'uptr)])
(bind #t ([e-fill (super-size e-fill)])
`(let ([,t ,(if (fx>= elt-bytes ptr-bytes)
e-bytes
(nanopass-case (L7 Expr) e-bytes
[(immediate ,imm)
`(immediate ,(logand (+ imm (fx- ptr-bytes 1)) (fx- ptr-bytes)))]
[else
(%inline logand
,(%inline +
,e-bytes
(immediate ,(fx- ptr-bytes 1)))
(immediate ,(fx- ptr-bytes)))]))])
(label ,Ltop
(if ,(%inline eq? ,t (immediate 0))
,e-vec
,(%seq
(set! ,t ,(%inline - ,t (immediate ,ptr-bytes)))
(set! ,(%mref ,e-vec ,t ,data-disp) ,e-fill)
(goto ,Ltop)))))))]))))
;; NOTE: integer->ptr and unsigned->ptr DO NOT handle 64-bit integers on a 32-bit machine.
;; this is okay for $object-ref and $object-set!, which do not support moving 64-bit values
;; as single entities on a 32-bit machine, but care should be taken if these are used with
;; other primitives.
(define-who integer->ptr
(lambda (x width)
(if (fx>= (constant fixnum-bits) width)
(build-fix x)
(%seq
(set! ,%ac0 ,x)
(set! ,%xp ,(build-fix %ac0))
(set! ,%xp ,(build-unfix %xp))
(if ,(%inline eq? ,%ac0 ,%xp)
,(build-fix %ac0)
(seq
(set! ,%ac0
(inline
,(case width
[(32) (intrinsic-info-asmlib dofretint32 #f)]
[(64) (intrinsic-info-asmlib dofretint64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
,%ac0))))))
(define-who unsigned->ptr
(lambda (x width)
(if (fx>= (constant fixnum-bits) width)
(build-fix x)
`(seq
(set! ,%ac0 ,x)
(if ,(%inline u< ,(%constant most-positive-fixnum) ,%ac0)
(seq
(set! ,%ac0
(inline
,(case width
[(32) (intrinsic-info-asmlib dofretuns32 #f)]
[(64) (intrinsic-info-asmlib dofretuns64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
,%ac0)
,(build-fix %ac0))))))
(define-who i32xu32->ptr
(lambda (hi lo)
(safe-assert (eqv? (constant ptr-bits) 32))
(let ([Lbig (make-local-label 'Lbig)])
(bind #t (lo hi)
`(if ,(%inline eq? ,hi ,(%inline sra ,lo (immediate 31)))
,(bind #t ([fxlo (build-fix lo)])
`(if ,(%inline eq? ,(build-unfix fxlo) ,lo)
,fxlo
(goto ,Lbig)))
(label ,Lbig
,(%seq
(set! ,%ac0 ,lo)
(set! ,(ref-reg %ac1) ,hi)
(set! ,%ac0 (inline ,(intrinsic-info-asmlib dofretint64 #f) ,%asmlibcall))
,%ac0)))))))
(define-who u32xu32->ptr
(lambda (hi lo)
(safe-assert (eqv? (constant ptr-bits) 32))
(let ([Lbig (make-local-label 'Lbig)])
(bind #t (lo hi)
`(if ,(%inline eq? ,hi (immediate 0))
(if ,(%inline u< ,(%constant most-positive-fixnum) ,lo)
(goto ,Lbig)
,(build-fix lo))
(label ,Lbig
,(%seq
(set! ,%ac0 ,lo)
(set! ,(ref-reg %ac1) ,hi)
(set! ,%ac0 (inline ,(intrinsic-info-asmlib dofretuns64 #f) ,%asmlibcall))
,%ac0)))))))
(define-who ptr->integer
(lambda (value width)
(if (fx> (constant fixnum-bits) width)
(build-unfix value)
`(seq
(set! ,%ac0 ,value)
(if ,(%type-check mask-fixnum type-fixnum ,%ac0)
,(build-unfix %ac0)
(seq
(set! ,%ac0
(inline
,(cond
[(fx<= width 32) (intrinsic-info-asmlib dofargint32 #f)]
[(fx<= width 64) (intrinsic-info-asmlib dofargint64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
,%ac0))))))
(define ptr-type (constant-case ptr-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]
[else ($oops 'ptr-type "unknown ptr-bit size ~s" (constant ptr-bits))]))
(define-who type->width
(lambda (x)
(case x
[(integer-8 unsigned-8 char) 8]
[(integer-16 unsigned-16) 16]
[(integer-24 unsigned-24) 24]
[(integer-32 unsigned-32 single-float) 32]
[(integer-40 unsigned-40) 40]
[(integer-48 unsigned-48) 48]
[(integer-56 unsigned-56) 56]
[(integer-64 unsigned-64 double-float) 64]
[(scheme-object fixnum) (constant ptr-bits)]
[(wchar) (constant wchar-bits)]
[else ($oops who "unknown type ~s" x)])))
(define offset-expr->index+offset
(lambda (offset)
(if (fixnum-constant? offset)
(values %zero (constant-value offset))
(values (build-unfix offset) 0))))
(define-who build-int-load
(lambda (swapped? type base index offset build-int)
(case type
[(integer-8 unsigned-8)
(build-int `(inline ,(make-info-load type #f) ,%load ,base ,index (immediate ,offset)))]
[(integer-16 integer-32 unsigned-16 unsigned-32)
(build-int `(inline ,(make-info-load type swapped?) ,%load ,base ,index (immediate ,offset)))]
[(integer-64 unsigned-64)
(constant-case ptr-bits
[(32)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 4) offset)
(values offset (+ offset 4)))])
(bind #t (base index)
(build-int
`(inline ,(make-info-load 'integer-32 swapped?) ,%load ,base ,index (immediate ,hi))
`(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo)))))]
[(64)
(build-int `(inline ,(make-info-load type swapped?) ,%load ,base ,index (immediate ,offset)))])]
[(integer-24 unsigned-24)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 1) offset)
(values offset (+ offset 2)))])
(define hi-type (if (eq? type 'integer-24) 'integer-8 'unsigned-8))
(bind #t (base index)
(build-int
(%inline logor
,(%inline sll
(inline ,(make-info-load hi-type #f) ,%load ,base ,index (immediate ,hi))
(immediate 16))
(inline ,(make-info-load 'unsigned-16 swapped?) ,%load ,base ,index (immediate ,lo))))))])]
[(integer-40 unsigned-40)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 1) offset)
(values offset (+ offset 4)))])
(define hi-type (if (eq? type 'integer-40) 'integer-8 'unsigned-8))
(bind #t (base index)
(constant-case ptr-bits
[(32)
(build-int
`(inline ,(make-info-load hi-type #f) ,%load ,base ,index (immediate ,hi))
`(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo)))]
[(64)
(build-int
(%inline logor
,(%inline sll
(inline ,(make-info-load hi-type #f) ,%load ,base ,index (immediate ,hi))
(immediate 32))
(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo))))])))])]
[(integer-48 unsigned-48)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 2) offset)
(values offset (+ offset 4)))])
(define hi-type (if (eq? type 'integer-48) 'integer-16 'unsigned-16))
(bind #t (base index)
(constant-case ptr-bits
[(32)
(build-int
`(inline ,(make-info-load hi-type swapped?) ,%load ,base ,index (immediate ,hi))
`(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo)))]
[(64)
(build-int
(%inline logor
,(%inline sll
(inline ,(make-info-load hi-type swapped?) ,%load ,base ,index (immediate ,hi))
(immediate 32))
(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo))))])))])]
[(integer-56 unsigned-56)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo mi hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 3) (+ offset 1) offset)
(values offset (+ offset 4) (+ offset 6)))])
(define hi-type (if (eq? type 'integer-56) 'integer-8 'unsigned-8))
(bind #t (base index)
(constant-case ptr-bits
[(32)
(build-int
(%inline logor
,(%inline sll
(inline ,(make-info-load hi-type #f) ,%load ,base ,index (immediate ,hi))
(immediate 16))
(inline ,(make-info-load 'unsigned-16 swapped?) ,%load ,base ,index (immediate ,mi)))
`(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo)))]
[(64)
(build-int
(%inline logor
,(%inline sll
,(%inline logor
,(%inline sll
(inline ,(make-info-load hi-type #f) ,%load ,base ,index (immediate ,hi))
(immediate 16))
(inline ,(make-info-load 'unsigned-16 swapped?) ,%load ,base ,index (immediate ,mi)))
(immediate 32))
(inline ,(make-info-load 'unsigned-32 swapped?) ,%load ,base ,index (immediate ,lo))))])))])]
[else (sorry! who "unsupported type ~s" type)])))
(define-who build-object-ref
(case-lambda
[(swapped? type base offset-expr)
(let-values ([(index offset) (offset-expr->index+offset offset-expr)])
(build-object-ref swapped? type base index offset))]
[(swapped? type base index offset)
(case type
[(scheme-object) `(inline ,(make-info-load ptr-type swapped?) ,%load ,base ,index (immediate ,offset))]
[(double-float)
(if swapped?
(constant-case ptr-bits
[(32)
(bind #t (base index)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
(set! ,(%mref ,t ,(constant flonum-data-disp))
(inline ,(make-info-load 'unsigned-32 #t) ,%load ,base ,index
(immediate ,(+ offset 4))))
(set! ,(%mref ,t ,(+ (constant flonum-data-disp) 4))
(inline ,(make-info-load 'unsigned-32 #t) ,%load ,base ,index
(immediate ,offset)))
,t)))]
[(64)
(bind #f (base index)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
`(seq
(set! ,(%mref ,t ,(constant flonum-data-disp))
(inline ,(make-info-load 'unsigned-64 #t) ,%load ,base ,index
(immediate ,offset)))
,t)))])
(bind #f (base index)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
(inline ,(make-info-loadfl %flreg1) ,%load-double
,base ,index (immediate ,offset))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,t ,%zero ,(%constant flonum-data-disp))
,t))))]
[(single-float)
(if swapped?
(bind #f (base index)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
(set! ,(%mref ,t ,(constant flonum-data-disp))
(inline ,(make-info-load 'unsigned-32 #t) ,%load ,base ,index
(immediate ,offset)))
(inline ,(make-info-loadfl %flreg1) ,%load-single->double
,t ,%zero ,(%constant flonum-data-disp))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,t ,%zero ,(%constant flonum-data-disp))
,t)))
(bind #f (base index)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
(inline ,(make-info-loadfl %flreg1) ,%load-single->double
,base ,index (immediate ,offset))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,t ,%zero ,(%constant flonum-data-disp))
,t))))]
[(integer-8 integer-16 integer-24 integer-32 integer-40 integer-48 integer-56 integer-64)
(build-int-load swapped? type base index offset
(if (and (eqv? (constant ptr-bits) 32) (memq type '(integer-40 integer-48 integer-56 integer-64)))
i32xu32->ptr
(lambda (x) (integer->ptr x (type->width type)))))]
[(unsigned-8 unsigned-16 unsigned-24 unsigned-32 unsigned-40 unsigned-48 unsigned-56 unsigned-64)
(build-int-load swapped? type base index offset
(if (and (eqv? (constant ptr-bits) 32) (memq type '(unsigned-40 unsigned-48 unsigned-56 unsigned-64)))
u32xu32->ptr
(lambda (x) (unsigned->ptr x (type->width type)))))]
[(fixnum) (build-fix `(inline ,(make-info-load ptr-type swapped?) ,%load ,base ,index (immediate ,offset)))]
[else (sorry! who "unsupported type ~s" type)])]))
(define-who build-int-store
(lambda (swapped? type base index offset value)
(case type
[(integer-8 unsigned-8)
`(inline ,(make-info-load type #f) ,%store ,base ,index (immediate ,offset) ,value)]
[(integer-16 integer-32 integer-64 unsigned-16 unsigned-32 unsigned-64)
`(inline ,(make-info-load type swapped?) ,%store ,base ,index (immediate ,offset) ,value)]
[(integer-24 unsigned-24)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 1) offset)
(values offset (+ offset 2)))])
(bind #t (base index value)
(%seq
(inline ,(make-info-load 'unsigned-16 swapped?) ,%store ,base ,index (immediate ,lo) ,value)
(inline ,(make-info-load 'unsigned-8 #f) ,%store ,base ,index (immediate ,hi)
,(%inline srl ,value (immediate 16))))))])]
[(integer-40 unsigned-40)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 1) offset)
(values offset (+ offset 4)))])
(bind #t (base index value)
(%seq
(inline ,(make-info-load 'unsigned-32 swapped?) ,%store ,base ,index (immediate ,lo) ,value)
(inline ,(make-info-load 'unsigned-8 #f) ,%store ,base ,index (immediate ,hi)
,(%inline srl ,value (immediate 32))))))])]
[(integer-48 unsigned-48)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 2) offset)
(values offset (+ offset 4)))])
(bind #t (base index value)
(%seq
(inline ,(make-info-load 'unsigned-32 swapped?) ,%store ,base ,index (immediate ,lo) ,value)
(inline ,(make-info-load 'unsigned-16 swapped?) ,%store ,base ,index (immediate ,hi)
,(%inline srl ,value (immediate 32))))))])]
[(integer-56 unsigned-56)
(constant-case unaligned-integers
[(#t)
(let-values ([(lo mi hi) (if (constant-case native-endianness [(little) swapped?] [(big) (not swapped?)])
(values (+ offset 3) (+ offset 1) offset)
(values offset (+ offset 4) (+ offset 6)))])
(bind #t (base index value)
(%seq
(inline ,(make-info-load 'unsigned-32 swapped?) ,%store ,base ,index (immediate ,lo) ,value)
(inline ,(make-info-load 'unsigned-16 swapped?) ,%store ,base ,index (immediate ,mi)
,(%inline srl ,value (immediate 32)))
(inline ,(make-info-load 'unsigned-8 #f) ,%store ,base ,index (immediate ,hi)
,(%inline srl ,value (immediate 48))))))])]
[else (sorry! who "unsupported type ~s" type)])))
(define-who build-object-set!
(case-lambda
[(type base offset-expr value)
(let-values ([(index offset) (offset-expr->index+offset offset-expr)])
(build-object-set! type base index offset value))]
[(type base index offset value)
(case type
[(scheme-object) (build-dirty-store base index offset value)]
[(double-float)
(bind #f (base index)
(%seq
(inline ,(make-info-loadfl %flreg1) ,%load-double
,value ,%zero ,(%constant flonum-data-disp))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,base ,index (immediate ,offset))))]
[(single-float)
(bind #f (base index)
(%seq
(inline ,(make-info-loadfl %flreg1) ,%load-double->single
,value ,%zero ,(%constant flonum-data-disp))
(inline ,(make-info-loadfl %flreg1) ,%store-single
,base ,index (immediate ,offset))))]
; 40-bit+ only on 64-bit machines
[(integer-8 integer-16 integer-24 integer-32 integer-40 integer-48 integer-56 integer-64
unsigned-8 unsigned-16 unsigned-24 unsigned-32 unsigned-40 unsigned-48 unsigned-56 unsigned-64)
(build-int-store #f type base index offset (ptr->integer value (type->width type)))]
[(fixnum)
`(inline ,(make-info-load ptr-type #f) ,%store
,base ,index (immediate ,offset) ,(build-unfix value))]
[else (sorry! who "unrecognized type ~s" type)])]))
(define-who build-swap-object-set!
(case-lambda
[(type base offset-expr value)
(let-values ([(index offset) (offset-expr->index+offset offset-expr)])
(build-swap-object-set! type base index offset value))]
[(type base index offset value)
(case type
; only on 64-bit machines
[(double-float)
`(inline ,(make-info-load 'unsigned-64 #t) ,%store
,base ,index (immediate ,offset)
,(%mref ,value ,(constant flonum-data-disp)))]
; 40-bit+ only on 64-bit machines
[(integer-16 integer-24 integer-32 integer-40 integer-48 integer-56 integer-64
unsigned-16 unsigned-24 unsigned-32 unsigned-40 unsigned-48 unsigned-56 unsigned-64)
(build-int-store #t type base index offset (ptr->integer value (type->width type)))]
[(fixnum)
`(inline ,(make-info-load ptr-type #t) ,%store ,base ,index (immediate ,offset)
,(build-unfix value))]
[else (sorry! who "unrecognized type ~s" type)])]))
(define extract-unsigned-bitfield
(lambda (raw? start end arg)
(let* ([left (fx- (if raw? (constant ptr-bits) (constant fixnum-bits)) end)]
[right (if raw? (fx- (fx+ left start) (constant fixnum-offset)) (fx+ left start))]
[body (%inline srl
,(if (fx= left 0)
arg
(%inline sll ,arg (immediate ,left)))
(immediate ,right))])
(if (fx= start 0)
body
(%inline logand ,body (immediate ,(- (constant fixnum-factor))))))))
(define extract-signed-bitfield
(lambda (raw? start end arg)
(let* ([left (fx- (if raw? (constant ptr-bits) (constant fixnum-bits)) end)]
[right (if raw? (fx- (fx+ left start) (constant fixnum-offset)) (fx+ left start))])
(let ([body (if (fx= left 0) arg (%inline sll ,arg (immediate ,left)))])
(let ([body (if (fx= right 0) body (%inline sra ,body (immediate ,right)))])
(if (fx= start 0)
body
(%inline logand ,body (immediate ,(- (constant fixnum-factor))))))))))
(define insert-bitfield
(lambda (raw? start end bf-width arg val)
(if raw?
(cond
[(fx= start 0)
(%inline logor
,(%inline sll
,(%inline srl ,arg (immediate ,end))
(immediate ,end))
,(%inline srl
,(%inline sll ,val (immediate ,(fx- (constant fixnum-bits) end)))
(immediate ,(fx- (constant ptr-bits) end))))]
[(fx= end bf-width)
(%inline logor
,(%inline srl
,(%inline sll ,arg
(immediate ,(fx- (constant ptr-bits) start)))
(immediate ,(fx- (constant ptr-bits) start)))
,(cond
[(fx< start (constant fixnum-offset))
(%inline srl ,val
(immediate ,(fx- (constant fixnum-offset) start)))]
[(fx> start (constant fixnum-offset))
(%inline sll ,val
(immediate ,(fx- start (constant fixnum-offset))))]
[else val]))]
[else
(%inline logor
,(%inline logand ,arg
(immediate ,(lognot (ash (- (expt 2 (fx- end start)) 1) start))))
,(%inline srl
,(if (fx= (fx- end start) (constant fixnum-bits))
val
(%inline sll ,val
(immediate ,(fx- (constant fixnum-bits) (fx- end start)))))
(immediate ,(fx- (constant ptr-bits) end))))])
(cond
[(fx= start 0)
(%inline logor
,(%inline sll
,(%inline srl ,arg (immediate ,(fx+ end (constant fixnum-offset))))
(immediate ,(fx+ end (constant fixnum-offset))))
,(%inline srl
,(%inline sll ,val (immediate ,(fx- (constant fixnum-bits) end)))
(immediate ,(fx- (constant fixnum-bits) end))))]
#;[(fx= end (constant fixnum-bits)) ---] ; end < fixnum-bits
[else
(%inline logor
,(%inline logand ,arg
(immediate ,(lognot (ash (- (expt 2 (fx- end start)) 1)
(fx+ start (constant fixnum-offset))))))
,(%inline srl
,(%inline sll ,val
(immediate ,(fx- (constant fixnum-bits) (fx- end start))))
(immediate ,(fx- (constant fixnum-bits) end))))]))))
(define translate
(lambda (e current-shift target-shift)
(let ([delta (fx- current-shift target-shift)])
(if (fx= delta 0)
e
(if (fx< delta 0)
(%inline sll ,e (immediate ,(fx- delta)))
(%inline srl ,e (immediate ,delta)))))))
(define extract-length
(lambda (t/l length-offset)
(%inline logand
,(translate t/l length-offset (constant fixnum-offset))
(immediate ,(- (constant fixnum-factor))))))
(define build-type/length
(lambda (e type current-shift target-shift)
(let ([e (translate e current-shift target-shift)])
(if (eqv? type 0)
e
(%inline logor ,e (immediate ,type))))))
(define-syntax build-ref-check
(syntax-rules ()
[(_ type-disp maximum-length length-offset type mask immutable-flag)
(lambda (e-v e-i maybe-e-new)
; NB: caller must bind e-v, e-i, and maybe-e-new
(safe-assert (no-need-to-bind? #t e-v))
(safe-assert (no-need-to-bind? #t e-i))
(safe-assert (or (not maybe-e-new) (no-need-to-bind? #t maybe-e-new)))
(build-and
(%type-check mask-typed-object type-typed-object ,e-v)
(bind #t ([t (%mref ,e-v ,(constant type-disp))])
(cond
[(expr->index e-i 1 (constant maximum-length)) =>
(lambda (index)
(let ([e (%inline u<
(immediate ,(logor (ash index (constant length-offset)) (constant type) (constant immutable-flag)))
,t)])
(if (and (eqv? (constant type) (constant type-fixnum))
(eqv? (constant mask) (constant mask-fixnum)))
(build-and e (build-fixnums? (if maybe-e-new (list t maybe-e-new) (list t))))
(build-and
(%type-check mask type ,t)
(if maybe-e-new (build-and e (build-fixnums? (list maybe-e-new))) e)))))]
[else
(let ([e (%inline u< ,e-i ,(extract-length t (constant length-offset)))])
(if (and (eqv? (constant type) (constant type-fixnum))
(eqv? (constant mask) (constant mask-fixnum)))
(build-and e (build-fixnums? (if maybe-e-new (list e-i t maybe-e-new) (list e-i t))))
(build-and
(%type-check mask type ,t)
(build-and
(build-fixnums? (if maybe-e-new (list e-i maybe-e-new) (list e-i)))
e))))]))))]))
(define-syntax build-set-immutable!
(syntax-rules ()
[(_ type-disp immutable-flag)
(lambda (e-v)
(bind #t (e-v)
`(set! ,(%mref ,e-v ,(constant type-disp))
,(%inline logor
,(%mref ,e-v ,(constant type-disp))
(immediate ,(constant immutable-flag))))))]))
(define inline-args-limit 10)
(define reduce-equality
(lambda (src sexpr moi e1 e2 e*)
(and (fx<= (length e*) (fx- inline-args-limit 2))
(bind #t (e1)
(bind #f (e2)
(list-bind #f (e*)
(let compare ([src src] [e2 e2] [e* e*])
(if (null? e*)
(moi src sexpr (list e1 e2))
`(if ,(moi src sexpr (list e1 e2))
,(compare #f (car e*) (cdr e*))
(quote #f))))))))))
(define reduce-inequality
(lambda (src sexpr moi e1 e2 e*)
(and (fx<= (length e*) (fx- inline-args-limit 2))
(let f ([e2 e2] [e* e*] [re* '()])
(if (null? e*)
(bind #f ([e2 e2])
(let compare ([src src] [e* (cons e1 (reverse (cons e2 re*)))])
(let ([more-args (cddr e*)])
(if (null? more-args)
(moi src sexpr e*)
`(if ,(moi src sexpr (list (car e*) (cadr e*)))
,(compare #f (cdr e*))
(quote #f))))))
(bind #t ([e2 e2]) (f (car e*) (cdr e*) (cons e2 re*))))))))
(define reduce ; left associative as required for, e.g., fx-
(lambda (src sexpr moi e e*)
(and (fx<= (length e*) (fx- inline-args-limit 1))
(bind #f (e)
(list-bind #f ([e* e*])
(let reduce ([src src] [e e] [e* e*])
(if (null? e*)
e
(reduce #f (moi src sexpr (list e (car e*))) (cdr e*)))))))))
(module (relop-length RELOP< RELOP<= RELOP= RELOP>= RELOP>)
(define RELOP< -2)
(define RELOP<= -1)
(define RELOP= 0)
(define RELOP>= 1)
(define RELOP> 2)
(define (mirror op) (fx- op))
(define go
(lambda (op e n)
(let f ([n n] [e e])
(if (fx= n 0)
(cond
[(or (eqv? op RELOP=) (eqv? op RELOP<=)) (build-null? e)]
[(eqv? op RELOP<) `(seq ,e (quote #f))]
[(eqv? op RELOP>) (build-not (build-null? e))]
[(eqv? op RELOP>=) `(seq ,e (quote #t))]
[else (sorry! 'relop-length "unexpected op ~s" op)])
(cond
[(or (eqv? op RELOP=) (eqv? op RELOP>))
(bind #t (e)
(build-and
(build-not (build-null? e))
(f (fx- n 1) (build-cdr e))))]
[(eqv? op RELOP<)
(if (fx= n 1)
(build-null? e)
(bind #t (e)
(build-simple-or
(build-null? e)
(f (fx- n 1) (build-cdr e)))))]
[(eqv? op RELOP<=)
(bind #t (e)
(build-simple-or
(build-null? e)
(f (fx- n 1) (build-cdr e))))]
[(eqv? op RELOP>=)
(if (fx= n 1)
(build-not (build-null? e))
(bind #t (e)
(build-and
(build-not (build-null? e))
(f (fx- n 1) (build-cdr e)))))]
[else (sorry! 'relop-length "unexpected op ~s" op)])))))
(define relop-length1
(lambda (op e n)
(nanopass-case (L7 Expr) e
[(call ,info ,mdcl ,pr ,e)
(guard (and (eq? (primref-name pr) 'length) (all-set? (prim-mask unsafe) (primref-flags pr))))
(go op e n)]
[else #f])))
(define relop-length2
(lambda (op e1 e2)
(nanopass-case (L7 Expr) e2
[(quote ,d) (and (fixnum? d) (fx<= 0 d 4) (relop-length1 op e1 d))]
[else #f])))
(define relop-length
(case-lambda
[(op e) (relop-length1 op e 0)]
[(op e1 e2) (or (relop-length2 op e1 e2) (relop-length2 (mirror op) e2 e1))])))
(define make-ftype-pointer-equal?
(lambda (e1 e2)
(bind #f (e1 e2)
(%inline eq?
,(%mref ,e1 ,(constant record-data-disp))
,(%mref ,e2 ,(constant record-data-disp))))))
(define make-ftype-pointer-null?
(lambda (e)
(%inline eq?
,(%mref ,e ,(constant record-data-disp))
(immediate 0))))
(define eqvop-null-fptr
(lambda (e1 e2)
(nanopass-case (L7 Expr) e1
[(call ,info ,mdcl ,pr ,e1)
(and
(eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr))
(nanopass-case (L7 Expr) e2
[(quote ,d)
(and (eqv? d 0) (make-ftype-pointer-null? e1))]
[(call ,info ,mdcl ,pr ,e2)
(and (eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr))
(make-ftype-pointer-equal? e1 e2))]
[else #f]))]
[(quote ,d)
(and (eqv? d 0)
(nanopass-case (L7 Expr) e2
[(call ,info ,mdcl ,pr ,e2)
(and (eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr))
(make-ftype-pointer-null? e2))]
[else #f]))]
[else #f])))
(define-inline 2 values
[(e) e]
[e* `(values ,(make-info-call src sexpr #f #f #f) ,e* ...)])
(define-inline 2 eq?
[(e1 e2)
(or (relop-length RELOP= e1 e2)
(%inline eq? ,e1 ,e2))])
(define-inline 2 $keep-live
[(e) (%seq ,(%inline keep-live ,e) ,(%constant svoid))])
(let ()
(define (zgo src sexpr e e1 e2 r6rs?)
(build-simple-or
(%inline eq? ,e (immediate 0))
`(if ,(build-fixnums? (list e))
,(%constant sfalse)
,(if r6rs?
(build-libcall #t src sexpr fx=? e1 e2)
(build-libcall #t src sexpr fx= e1 e2)))))
(define (go src sexpr e1 e2 r6rs?)
(or (relop-length RELOP= e1 e2)
(cond
[(constant? (lambda (x) (eqv? x 0)) e1)
(bind #t (e2) (zgo src sexpr e2 e1 e2 r6rs?))]
[(constant? (lambda (x) (eqv? x 0)) e2)
(bind #t (e1) (zgo src sexpr e1 e1 e2 r6rs?))]
[else (bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline eq? ,e1 ,e2)
,(if r6rs?
(build-libcall #t src sexpr fx=? e1 e2)
(build-libcall #t src sexpr fx= e1 e2))))])))
(define-inline 2 fx=
[(e1 e2) (go src sexpr e1 e2 #f)]
[(e1 . e*) #f])
(define-inline 2 fx=?
[(e1 e2) (go src sexpr e1 e2 #t)]
[(e1 e2 . e*) #f]))
(let () ; level 2 fx<, fx<?, etc.
(define-syntax fx-pred
(syntax-rules ()
[(_ op r6rs:op length-op inline-op)
(let ()
(define (go src sexpr e1 e2 r6rs?)
(or (relop-length length-op e1 e2)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline inline-op ,e1 ,e2)
,(if r6rs?
(build-libcall #t src sexpr r6rs:op e1 e2)
(build-libcall #t src sexpr op e1 e2))))))
(define-inline 2 op
[(e1 e2) (go src sexpr e1 e2 #f)]
; TODO: 3-operand case requires 3-operand library routine
#;[(e1 e2 e3) (go3 src sexpr e1 e2 e3 #f)]
[(e1 . e*) #f])
(define-inline 2 r6rs:op
[(e1 e2) (go src sexpr e1 e2 #t)]
; TODO: 3-operand case requires 3-operand library routine
#; [(e1 e2 e3) (go3 src sexpr e1 e2 e3 #t)]
[(e1 e2 . e*) #f]))]))
(fx-pred fx< fx<? RELOP< <)
(fx-pred fx<= fx<=? RELOP<= <=)
(fx-pred fx>= fx>=? RELOP>= >=)
(fx-pred fx> fx>? RELOP> >))
(let () ; level 3 fx=, fx=?, etc.
(define-syntax fx-pred
(syntax-rules ()
[(_ op r6rs:op length-op inline-op)
(let ()
(define (go e1 e2)
(or (relop-length length-op e1 e2)
(%inline inline-op ,e1 ,e2)))
(define reducer
(if (eq? 'inline-op 'eq?)
reduce-equality
reduce-inequality))
(define-inline 3 op
[(e) `(seq ,e ,(%constant strue))]
[(e1 e2) (go e1 e2)]
[(e1 e2 . e*) (reducer src sexpr moi e1 e2 e*)])
(define-inline 3 r6rs:op
[(e1 e2) (go e1 e2)]
[(e1 e2 . e*) (reducer src sexpr moi e1 e2 e*)]))]))
(fx-pred fx< fx<? RELOP< <)
(fx-pred fx<= fx<=? RELOP<= <=)
(fx-pred fx= fx=? RELOP= eq?)
(fx-pred fx>= fx>=? RELOP>= >=)
(fx-pred fx> fx>? RELOP> >))
(let () ; level 3 fxlogand, ...
(define-syntax fxlogop
(syntax-rules ()
[(_ op inline-op base)
(define-inline 3 op
[() `(immediate ,(fix base))]
[(e) e]
[(e1 e2) (%inline inline-op ,e1 ,e2)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])]))
(fxlogop fxlogand logand -1)
(fxlogop fxand logand -1)
(fxlogop fxlogor logor 0)
(fxlogop fxlogior logor 0)
(fxlogop fxior logor 0)
(fxlogop fxlogxor logxor 0)
(fxlogop fxxor logxor 0))
(let ()
(define log-partition
(lambda (p base e*)
(let loop ([e* e*] [n base] [nc* '()])
(if (null? e*)
(if (and (fixnum? n) (fx= n base) (not (null? nc*)))
(values (car nc*) (cdr nc*) nc*)
(values `(immediate ,(fix n)) nc* nc*))
(let ([e (car e*)])
(if (fixnum-constant? e)
(let ([m (constant-value e)])
(loop (cdr e*) (if n (p n m) m) nc*))
(loop (cdr e*) n (cons e nc*))))))))
(let () ; level 2 fxlogor, fxlogior, fxor
(define-syntax fxlogorop
(syntax-rules ()
[(_ op)
(let ()
(define (go src sexpr e*)
(and (fx<= (length e*) inline-args-limit)
(list-bind #t (e*)
(let-values ([(e e* nc*) (log-partition logor 0 e*)])
(bind #t ([t (fold-left (lambda (e1 e2) (%inline logor ,e1 ,e2)) e e*)])
`(if ,(%type-check mask-fixnum type-fixnum ,t)
,t
,(case (length nc*)
[(1) (build-libcall #t src sexpr op (car nc*) `(immediate ,(fix 0)))]
[(2) (build-libcall #t src sexpr op (car nc*) (cadr nc*))]
; TODO: need fxargerr library routine w/who arg & rest interface
[else `(call ,(make-info-call src sexpr #f #t #t) #f ,(Symref 'op) ,nc* (... ...))]))))))) ; NB: should be error call---but is it?
(define-inline 2 op
[() `(immediate ,(fix 0))]
[e* (go src sexpr e*)]))]))
(fxlogorop fxlogor)
(fxlogorop fxlogior)
(fxlogorop fxior))
(let () ; level 2 fxlogand, ...
(define-syntax fxlogop
(syntax-rules ()
[(_ op inline-op base)
(define-inline 2 op
[() `(immediate ,(fix base))]
[e* (and (fx<= (length e*) (fx- inline-args-limit 1))
(list-bind #t (e*)
;; NB: using inline-op here because it works when target's
;; NB: fixnum range is larger than the host's fixnum range
;; NB: during cross compile
(let-values ([(e e* nc*) (log-partition inline-op base e*)])
`(if ,(build-fixnums? nc*)
,(fold-left (lambda (e1 e2) (%inline inline-op ,e1 ,e2)) e e*)
; TODO: need fxargerr library routine w/who arg & rest interface
,(case (length nc*)
[(1) (build-libcall #t src sexpr op (car nc*) `(immediate ,(fix 0)))]
[(2) (build-libcall #t src sexpr op (car nc*) (cadr nc*))]
; TODO: need fxargerr library routine w/who arg & rest interface
[else `(call ,(make-info-call src sexpr #f #t #t) #f ,(Symref 'op) ,nc* (... ...))])))))])])) ; NB: should be error call---but is it?
(fxlogop fxlogand logand -1)
(fxlogop fxand logand -1)
(fxlogop fxlogxor logxor 0)
(fxlogop fxxor logxor 0)))
(define-inline 3 fxlogtest
[(e1 e2) (%inline logtest ,e1 ,e2)])
(define-inline 2 fxlogtest
[(e1 e2)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline logtest ,e1 ,e2)
,(build-libcall #t src sexpr fxlogtest e1 e2)))])
(let ()
(define xorbits (lognot (constant mask-fixnum)))
(define-syntax fxlognotop
(syntax-rules ()
[(_ name)
(begin
(define-inline 3 name
[(e) (%inline logxor ,e (immediate ,xorbits))])
(define-inline 2 name
[(e) (bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(%inline logxor ,e (immediate ,xorbits))
,(build-libcall #t src sexpr name e)))]))]))
(fxlognotop fxlognot)
(fxlognotop fxnot))
(define-inline 3 $fxu<
[(e1 e2) (or (relop-length RELOP< e1 e2)
(%inline u< ,e1 ,e2))])
(define-inline 3 fx+
[() `(immediate 0)]
[(e) e]
[(e1 e2) (%inline + ,e1 ,e2)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 r6rs:fx+ ; limited to two arguments
[(e1 e2) (%inline + ,e1 ,e2)])
(define-inline 3 fx1+
[(e) (%inline + ,e (immediate ,(fix 1)))])
(define-inline 2 $fx+?
[(e1 e2)
(let ([Lfalse (make-local-label 'Lfalse)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #f ([t (%inline +/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Lfalse ,(%constant sfalse))
,t))
(goto ,Lfalse))))])
(let ()
(define (go src sexpr e1 e2)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #f ([t (%inline +/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx+ e1 e2))
,t))
(goto ,Llib)))))
(define-inline 2 fx+
[() `(immediate 0)]
[(e)
(bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,e
,(build-libcall #t #f sexpr fx+ e `(immediate ,(fix 0)))))]
[(e1 e2) (go src sexpr e1 e2)]
; TODO: 3-operand case requires 3-operand library routine
#;[(e1 e2 e3)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2 e3)
`(if ,(build-fixnums? (list e1 e2 e3))
,(bind #t ([t (%inline +/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx+ e1 e2 e3))
,(bind #t ([t (%inline +/ovfl ,t ,e3)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(goto ,Llib)
,t))))
(goto ,Llib))))]
[(e1 . e*) #f])
(define-inline 2 r6rs:fx+ ; limited to two arguments
[(e1 e2) (go src sexpr e1 e2)]))
(define-inline 3 fx-
[(e) (%inline - (immediate 0) ,e)]
[(e1 e2) (%inline - ,e1 ,e2)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 r6rs:fx- ; limited to one or two arguments
[(e) (%inline - (immediate 0) ,e)]
[(e1 e2) (%inline - ,e1 ,e2)])
(define-inline 3 fx1-
[(e) (%inline - ,e (immediate ,(fix 1)))])
(define-inline 2 $fx-?
[(e1 e2)
(let ([Lfalse (make-local-label 'Lfalse)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #f ([t (%inline -/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Lfalse ,(%constant sfalse))
,t))
(goto ,Lfalse))))])
(let ()
(define (go src sexpr e1 e2)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #t ([t (%inline -/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx- e1 e2))
,t))
(goto ,Llib)))))
(define-inline 2 fx-
[(e) (go src sexpr `(immediate ,(fix 0)) e)]
[(e1 e2) (go src sexpr e1 e2)]
; TODO: 3-operand case requires 3-operand library routine
#;[(e1 e2 e3)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2 e3)
`(if ,(build-fixnums? (list e1 e2 e3))
,(bind #t ([t (%inline -/ovfl ,e1 ,e2)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx- e1 e2 e3))
,(bind #t ([t (%inline -/ovfl ,t ,e3)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(goto ,Llib)
,t))))
(goto ,Llib))))]
[(e1 . e*) #f])
(define-inline 2 r6rs:fx- ; limited to one or two arguments
[(e) (go src sexpr `(immediate ,(fix 0)) e)]
[(e1 e2) (go src sexpr e1 e2)]))
(define-inline 2 fx1-
[(e) (let ([Llib (make-local-label 'Llib)])
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(bind #t ([t (%inline -/ovfl ,e (immediate ,(fix 1)))])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx1- e))
,t))
(goto ,Llib))))])
(define-inline 2 fx1+
[(e) (let ([Llib (make-local-label 'Llib)])
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(bind #f ([t (%inline +/ovfl ,e (immediate ,(fix 1)))])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx1+ e))
,t))
(goto ,Llib))))])
(let ()
(define fixnum-powers-of-two
(let f ([m 2] [e 1])
(if (<= m (constant most-positive-fixnum))
(cons (cons m e) (f (* m 2) (fx+ e 1)))
'())))
(define-inline 3 fxdiv
[(e1 e2)
(nanopass-case (L7 Expr) e2
[(quote ,d)
(let ([a (assv d fixnum-powers-of-two)])
(and a
(%inline logand
,(%inline sra ,e1 (immediate ,(cdr a)))
(immediate ,(- (constant fixnum-factor))))))]
[else #f])])
(define-inline 3 fxmod
[(e1 e2)
(nanopass-case (L7 Expr) e2
[(quote ,d)
(let ([a (assv d fixnum-powers-of-two)])
(and a (%inline logand ,e1 (immediate ,(fix (- d 1))))))]
[else #f])])
(let ()
(define (build-fx* e1 e2 ovfl?)
(define (fx*-constant e n)
(if ovfl?
(%inline */ovfl ,e (immediate ,n))
(cond
[(eqv? n 1) e]
[(eqv? n -1) (%inline - (immediate 0) ,e)]
[(eqv? n 2) (%inline sll ,e (immediate 1))]
[(eqv? n 3)
(bind #t (e)
(%inline +
,(%inline + ,e ,e)
,e))]
[(eqv? n 10)
(bind #t (e)
(%inline +
,(%inline +
,(%inline sll ,e (immediate 3))
,e)
,e))]
[(assv n fixnum-powers-of-two) =>
(lambda (a) (%inline sll ,e (immediate ,(cdr a))))]
[else (%inline * ,e (immediate ,n))])))
(nanopass-case (L7 Expr) e2
[(quote ,d) (guard (target-fixnum? d)) (fx*-constant e1 d)]
[else
(nanopass-case (L7 Expr) e1
[(quote ,d) (guard (target-fixnum? d)) (fx*-constant e2 d)]
[else
(let ([t (make-tmp 't 'uptr)])
`(let ([,t ,(build-unfix e2)])
,(if ovfl?
(%inline */ovfl ,e1 ,t)
(%inline * ,e1 ,t))))])]))
(define-inline 3 fx*
[() `(immediate ,(fix 1))]
[(e) e]
[(e1 e2) (build-fx* e1 e2 #f)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 r6rs:fx* ; limited to two arguments
[(e1 e2) (build-fx* e1 e2 #f)])
(let ()
(define (go src sexpr e1 e2)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #t ([t (build-fx* e1 e2 #t)])
`(if (inline ,(make-info-condition-code 'multiply-overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx* e1 e2))
,t))
(goto ,Llib)))))
(define-inline 2 fx*
[() `(immediate ,(fix 1))]
[(e)
(bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,e
,(build-libcall #t src sexpr fx* e `(immediate ,(fix 0)))))]
[(e1 e2) (go src sexpr e1 e2)]
; TODO: 3-operand case requires 3-operand library routine
#;[(e1 e2 e3)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e1 e2 e3)
`(if ,(build-fixnums? (list e1 e2 e3))
,(bind #t ([t (build-fx* e1 e2 #t)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Llib ,(build-libcall #t src sexpr fx* e1 e2 e3))
,(bind #t ([t (build-fx* t e3 #t)])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(goto ,Llib)
,t))))
(goto ,Llib))))]
[(e1 . e*) #f])
(define-inline 2 r6rs:fx* ; limited to two arguments
[(e1 e2) (go src sexpr e1 e2)]))
(let ()
(define build-fx/p2
(lambda (e1 p2)
(bind #t (e1)
(build-fix
(%inline sra
,(%inline + ,e1
,(%inline srl
,(if (fx= p2 1)
e1
(%inline sra ,e1 (immediate ,(fx- p2 1))))
(immediate ,(fx- (constant fixnum-bits) p2))))
(immediate ,(fx+ p2 (constant fixnum-offset))))))))
(define build-fx/
(lambda (src sexpr e1 e2)
(or (nanopass-case (L7 Expr) e2
[(quote ,d)
(let ([a (assv d fixnum-powers-of-two)])
(and a (build-fx/p2 e1 (cdr a))))]
[else #f])
(if (constant integer-divide-instruction)
(build-fix (%inline / ,e1 ,e2))
`(call ,(make-info-call src sexpr #f #f #f) #f
,(lookup-primref 3 '$fx/)
,e1 ,e2)))))
(define-inline 3 fx/
[(e) (build-fx/ src sexpr `(quote 1) e)]
[(e1 e2) (build-fx/ src sexpr e1 e2)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fxquotient
[(e) (build-fx/ src sexpr `(quote 1) e)]
[(e1 e2) (build-fx/ src sexpr e1 e2)]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fxremainder
[(e1 e2)
(bind #t (e1 e2)
(%inline - ,e1
,(build-fx*
(build-fx/ src sexpr e1 e2)
e2 #f)))]))))
(let ()
(define do-fxsll
(lambda (e1 e2)
(nanopass-case (L7 Expr) e2
[(quote ,d)
(%inline sll ,e1 (immediate ,d))]
[else
; TODO: bind-uptr might be handy here and also a make-unfix
(let ([t (make-tmp 't 'uptr)])
`(let ([,t ,(build-unfix e2)])
,(%inline sll ,e1 ,t)))])))
(define-inline 3 fxsll
[(e1 e2) (do-fxsll e1 e2)])
(define-inline 3 fxarithmetic-shift-left
[(e1 e2) (do-fxsll e1 e2)]))
(define-inline 3 fxsrl
[(e1 e2)
(%inline logand
,(nanopass-case (L7 Expr) e2
[(quote ,d)
(%inline srl ,e1 (immediate ,d))]
[else
(let ([t (make-tmp 't 'uptr)])
`(let ([,t ,(build-unfix e2)])
,(%inline srl ,e1 ,t)))])
(immediate ,(fx- (constant fixnum-factor))))])
(let ()
(define do-fxsra
(lambda (e1 e2)
(%inline logand
,(nanopass-case (L7 Expr) e2
[(quote ,d)
(%inline sra ,e1 (immediate ,d))]
[else
(let ([t (make-tmp 't 'uptr)])
`(let ([,t ,(build-unfix e2)])
,(%inline sra ,e1 ,t)))])
(immediate ,(fx- (constant fixnum-factor))))))
(define-inline 3 fxsra
[(e1 e2) (do-fxsra e1 e2)])
(define-inline 3 fxarithmetic-shift-right
[(e1 e2) (do-fxsra e1 e2)]))
(let ()
(define-syntax %safe-shift
(syntax-rules ()
[(_ src sexpr op libcall e1 e2 ?size)
(let ([size ?size])
(if (constant? (lambda (x) (and (fixnum? x) (fx<= 0 x (fx- size 1)))) e2)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1))
,(%inline logand
,(%inline op ,e1 (immediate ,(constant-value e2)))
(immediate ,(- (constant fixnum-factor))))
,(build-libcall #t src sexpr libcall e1 e2)))
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e2 (immediate ,(fix size))))
,(%inline logand
,(%inline op ,e1 ,(build-unfix e2))
(immediate ,(- (constant fixnum-factor))))
,(build-libcall #t src sexpr libcall e1 e2)))))]))
(define-inline 2 fxsrl
[(e1 e2) (%safe-shift src sexpr srl fxsrl e1 e2 (+ (constant fixnum-bits) 1))])
(define-inline 2 fxsra
[(e1 e2) (%safe-shift src sexpr sra fxsra e1 e2 (+ (constant fixnum-bits) 1))])
(define-inline 2 fxarithmetic-shift-right
[(e1 e2) (%safe-shift src sexpr sra fxarithmetic-shift-right e1 e2 (constant fixnum-bits))]))
(define-inline 3 fxarithmetic-shift
[(e1 e2)
(or (nanopass-case (L7 Expr) e2
[(quote ,d)
(and (fixnum? d)
(if ($fxu< d (constant fixnum-bits))
(%inline sll ,e1 (immediate ,d))
(and (fx< (fx- (constant fixnum-bits)) d 0)
(%inline logand
,(%inline sra ,e1 (immediate ,(fx- d)))
(immediate ,(- (constant fixnum-factor)))))))]
[else #f])
(build-libcall #f src sexpr fxarithmetic-shift e1 e2))])
(define-inline 2 fxarithmetic-shift
[(e1 e2)
(or (nanopass-case (L7 Expr) e2
[(quote ,d)
(guard (fixnum? d) (fx< (fx- (constant fixnum-bits)) d 0))
(bind #t (e1)
`(if ,(build-fixnums? (list e1))
,(%inline logand
,(%inline sra ,e1 (immediate ,(fx- d)))
(immediate ,(- (constant fixnum-factor))))
,(build-libcall #t src sexpr fxarithmetic-shift e1 e2)))]
[else #f])
(build-libcall #f src sexpr fxarithmetic-shift e1 e2))])
(let ()
(define dofxlogbit0
(lambda (e1 e2)
(if (constant? (lambda (x) (and (fixnum? x) ($fxu< x (fx- (constant fixnum-bits) 1)))) e2)
(%inline logand ,e1
(immediate ,(fix (lognot (ash 1 (constant-value e2))))))
(%inline logand ,e1
,(%inline lognot
,(%inline sll (immediate ,(fix 1))
,(build-unfix e2)))))))
(define dofxlogbit1
(lambda (e1 e2)
(if (constant? (lambda (x) (and (fixnum? x) ($fxu< x (fx- (constant fixnum-bits) 1)))) e2)
(%inline logor ,e1
(immediate ,(fix (ash 1 (constant-value e2)))))
(%inline logor ,e1
,(%inline sll (immediate ,(fix 1))
,(build-unfix e2))))))
(define-inline 3 fxlogbit0
[(e1 e2) (dofxlogbit0 e2 e1)])
(define-inline 3 fxlogbit1
[(e1 e2) (dofxlogbit1 e2 e1)])
(define-inline 3 fxcopy-bit
[(e1 e2 e3)
(and (fixnum-constant? e3)
(case (constant-value e3)
[(0) (dofxlogbit0 e1 e2)]
[(1) (dofxlogbit1 e1 e2)]
[else #f]))]))
(let ()
(define dofxlogbit0
(lambda (e1 e2 libcall)
(if (constant? (lambda (x) (and (fixnum? x) ($fxu< x (fx- (constant fixnum-bits) 1)))) e2)
(bind #t (e1)
`(if ,(build-fixnums? (list e1))
,(%inline logand ,e1
(immediate ,(fix (lognot (ash 1 (constant-value e2))))))
,(libcall e1 e2)))
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e2 (immediate ,(fix (fx- (constant fixnum-bits) 1)))))
,(%inline logand ,e1
,(%inline lognot
,(%inline sll (immediate ,(fix 1))
,(build-unfix e2))))
,(libcall e1 e2))))))
(define dofxlogbit1
(lambda (e1 e2 libcall)
(if (constant? (lambda (x) (and (fixnum? x) ($fxu< x (fx- (constant fixnum-bits) 1)))) e2)
(bind #t (e1)
`(if ,(build-fixnums? (list e1))
,(%inline logor ,e1
(immediate ,(fix (ash 1 (constant-value e2)))))
,(libcall e1 e2)))
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e2 (immediate ,(fix (fx- (constant fixnum-bits) 1)))))
,(%inline logor ,e1
,(%inline sll (immediate ,(fix 1))
,(build-unfix e2)))
,(libcall e1 e2))))))
(define-inline 2 fxlogbit0
[(e1 e2) (dofxlogbit0 e2 e1
(lambda (e2 e1)
(build-libcall #t src sexpr fxlogbit0 e1 e2)))])
(define-inline 2 fxlogbit1
[(e1 e2) (dofxlogbit1 e2 e1
(lambda (e2 e1)
(build-libcall #t src sexpr fxlogbit1 e1 e2)))])
(define-inline 2 fxcopy-bit
[(e1 e2 e3)
(and (fixnum-constant? e3)
(case (constant-value e3)
[(0) (dofxlogbit0 e1 e2
(lambda (e1 e2)
(build-libcall #t src sexpr fxcopy-bit e1 e2)))]
[(1) (dofxlogbit1 e1 e2
(lambda (e1 e2)
(build-libcall #t src sexpr fxcopy-bit e1 e2)))]
[else #f]))]))
(define-inline 3 fxzero?
[(e) (or (relop-length RELOP= e) (%inline eq? ,e (immediate 0)))])
(define-inline 3 fxpositive?
[(e) (or (relop-length RELOP> e) (%inline > ,e (immediate 0)))])
(define-inline 3 fxnonnegative?
[(e) (or (relop-length RELOP>= e) (%inline >= ,e (immediate 0)))])
(define-inline 3 fxnegative?
[(e) (or (relop-length RELOP< e) (%inline < ,e (immediate 0)))])
(define-inline 3 fxnonpositive?
[(e) (or (relop-length RELOP<= e) (%inline <= ,e (immediate 0)))])
(define-inline 3 fxeven?
[(e) (%inline eq?
,(%inline logand ,e (immediate ,(fix 1)))
(immediate ,(fix 0)))])
(define-inline 3 fxodd?
[(e) (%inline eq?
,(%inline logand ,e (immediate ,(fix 1)))
(immediate ,(fix 1)))])
(define-inline 2 fxzero?
[(e) (or (relop-length RELOP= e)
(bind #t (e)
(build-simple-or
(%inline eq? ,e (immediate 0))
`(if ,(build-fixnums? (list e))
,(%constant sfalse)
,(build-libcall #t src sexpr fxzero? e)))))])
(define-inline 2 fxpositive?
[(e) (or (relop-length RELOP> e)
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline > ,e (immediate 0))
,(build-libcall #t src sexpr fxpositive? e))))])
(define-inline 2 fxnonnegative?
[(e) (or (relop-length RELOP>= e)
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline >= ,e (immediate 0))
,(build-libcall #t src sexpr fxnonnegative? e))))])
(define-inline 2 fxnegative?
[(e) (or (relop-length RELOP< e)
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline < ,e (immediate 0))
,(build-libcall #t src sexpr fxnegative? e))))])
(define-inline 2 fxnonpositive?
[(e) (or (relop-length RELOP<= e)
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline <= ,e (immediate 0))
,(build-libcall #t src sexpr fxnonpositive? e))))])
(define-inline 2 fxeven?
[(e) (bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline eq?
,(%inline logand ,e (immediate ,(fix 1)))
(immediate ,(fix 0)))
,(build-libcall #t src sexpr fxeven? e)))])
(define-inline 2 fxodd?
[(e) (bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline eq?
,(%inline logand ,e (immediate ,(fix 1)))
(immediate ,(fix 1)))
,(build-libcall #t src sexpr fxodd? e)))])
(let ()
(define dofxlogbit?
(lambda (e1 e2)
(cond
[(constant? (lambda (x) (and (fixnum? x) (fx<= 0 x (fx- (constant fixnum-bits) 2)))) e1)
(%inline logtest ,e2 (immediate ,(fix (ash 1 (constant-value e1)))))]
[(constant? (lambda (x) (and (target-fixnum? x) (> x (fx- (constant fixnum-bits) 2)))) e1)
(%inline < ,e2 (immediate ,(fix 0)))]
[(fixnum-constant? e2)
(bind #t (e1)
`(if ,(%inline < (immediate ,(fix (fx- (constant fixnum-bits) 2))) ,e1)
,(if (< (constant-value e2) 0) (%constant strue) (%constant sfalse))
,(%inline logtest
,(%inline sra ,e2 ,(build-unfix e1))
(immediate ,(fix 1)))))]
[else
(bind #t (e1 e2)
`(if ,(%inline < (immediate ,(fix (fx- (constant fixnum-bits) 2))) ,e1)
,(%inline < ,e2 (immediate ,(fix 0)))
,(%inline logtest
,(%inline sra ,e2 ,(build-unfix e1))
(immediate ,(fix 1)))))])))
(define-inline 3 fxbit-set?
[(e1 e2) (dofxlogbit? e2 e1)])
(define-inline 3 fxlogbit?
[(e1 e2) (dofxlogbit? e1 e2)]))
(let ()
(define dofxlogbit?
(lambda (e1 e2 libcall)
(cond
[(constant? (lambda (x) (and (fixnum? x) (fx<= 0 x (fx- (constant fixnum-bits) 2)))) e1)
(bind #t (e2)
`(if ,(build-fixnums? (list e2))
,(%inline logtest ,e2
(immediate ,(fix (ash 1 (constant-value e1)))))
,(libcall e1 e2)))]
[(constant? (lambda (x) (and (target-fixnum? x) (> x (fx- (constant fixnum-bits) 2)))) e1)
(bind #t (e2)
`(if ,(build-fixnums? (list e2))
,(%inline < ,e2 (immediate ,(fix 0)))
,(libcall e1 e2)))]
[else
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e1 (immediate ,(fix (constant fixnum-bits)))))
,(%inline logtest
,(%inline sra ,e2 ,(build-unfix e1))
(immediate ,(fix 1)))
,(libcall e1 e2)))])))
(define-inline 2 fxbit-set?
[(e1 e2) (dofxlogbit? e2 e1
(lambda (e2 e1)
(build-libcall #t src sexpr fxbit-set? e1 e2)))])
(define-inline 2 fxlogbit?
[(e1 e2) (dofxlogbit? e1 e2
(lambda (e1 e2)
(build-libcall #t src sexpr fxlogbit? e1 e2)))]))
; can avoid if in fxabs with:
; t = sra(x, k) ; where k is ptr-bits - 1
; ; t is now -1 if x's sign bit set, otherwise 0
; x = xor(x, t) ; logical not if x negative, otherwise leave x alone
; x = x - t ; add 1 to complete two's complement negation if
; ; x was negative, otherwise leave x alone
; tests on i3le indicate that the if is actually faster, even in a loop
; where input alternates between positive and negative to defeat branch
; prediction.
(define-inline 3 fxabs
[(e) (bind #t (e)
`(if ,(%inline < ,e (immediate ,(fix 0)))
,(%inline - (immediate ,(fix 0)) ,e)
,e))])
;(define-inline 3 min ; needs library min
; ; must take care to be inexactness-preserving
; [(e0) e0]
; [(e0 e1)
; (bind #t (e0 e1)
; `(if ,(build-fixnums? (list e0 e1))
; (if ,(%inline < ,e0 ,e1) ,e0 ,e1)
; ,(build-libcall #t src sexpr min e0 e1)))]
; [(e0 . e*) (reduce src sexpr moi e1 e*)])
;
;(define-inline 3 max ; needs library max
; ; must take care to be inexactness-preserving
; [(e0) e0]
; [(e0 e1)
; (bind #t (e0 e1)
; `(if ,(build-fixnums? (list e0 e1))
; (if ,(%inline < ,e0 ,e1) ,e0 ,e1)
; ,(build-libcall #t src sexpr max e0 e1)))]
; [(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fxmin
[(e) e]
[(e1 e2) (bind #t (e1 e2)
`(if ,(%inline < ,e1 ,e2)
,e1
,e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fxmax
[(e) e]
[(e1 e2) (bind #t (e1 e2)
`(if ,(%inline < ,e2 ,e1)
,e1
,e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fxif
[(e1 e2 e3)
(bind #t (e1)
(%inline logor
,(%inline logand ,e2 ,e1)
,(%inline logand ,e3
,(%inline lognot ,e1))))])
(define-inline 3 fxbit-field
[(e1 e2 e3)
(and (constant? fixnum? e2) (constant? fixnum? e3)
(let ([start (constant-value e2)] [end (constant-value e3)])
(if (fx= end start)
(%seq ,e1 (immediate ,(fix 0)))
(and (and (fx>= start 0) (fx> end start) (fx< end (constant fixnum-bits)))
(extract-unsigned-bitfield #f start end e1)))))])
(define-inline 3 fxcopy-bit-field
[(e1 e2 e3 e4)
(and (constant? fixnum? e2) (constant? fixnum? e3)
(let ([start (constant-value e2)] [end (constant-value e3)])
(if (fx= end start)
e1
(and (and (fx>= start 0) (fx> end start) (fx< end (constant fixnum-bits)))
(insert-bitfield #f start end (constant fixnum-bits) e1 e4)))))])
;; could be done with one mutable variable instead of two, but this seems to generate
;; the same code as the existing compiler
(define-inline 3 fxlength
[(e)
(let ([t (make-assigned-tmp 't 'uptr)] [result (make-assigned-tmp 'result)])
`(let ([,t ,(build-unfix e)])
(seq
(if ,(%inline < ,t (immediate 0))
(set! ,t ,(%inline lognot ,t))
,(%constant svoid))
(let ([,result (immediate ,(fix 0))])
,((lambda (body)
(constant-case fixnum-bits
[(30) body]
[(61)
`(seq
(if ,(%inline < ,t (immediate #x100000000))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 32)))
(set! ,result
,(%inline + ,result (immediate ,(fix 32))))))
,body)]))
(%seq
(if ,(%inline < ,t (immediate #x10000))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 16)))
(set! ,result ,(%inline + ,result (immediate ,(fix 16))))))
(if ,(%inline < ,t (immediate #x100))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 8)))
(set! ,result ,(%inline + ,result (immediate ,(fix 8))))))
,(%inline + ,result
(inline ,(make-info-load 'unsigned-8 #f) ,%load
,(%tc-ref fxlength-bv) ,t
,(%constant bytevector-data-disp)))))))))])
(define-inline 3 fxfirst-bit-set
[(e)
(let ([t (make-assigned-tmp 't 'uptr)] [result (make-assigned-tmp 'result)])
(bind #t (e)
`(if ,(%inline eq? ,e (immediate ,(fix 0)))
(immediate ,(fix -1))
(let ([,t ,(build-unfix e)] [,result (immediate ,(fix 0))])
,((lambda (body)
(constant-case fixnum-bits
[(30) body]
[(61)
`(seq
(if ,(%inline logtest ,t (immediate #xffffffff))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 32)))
(set! ,result ,(%inline + ,result (immediate ,(fix 32))))))
,body)]))
(%seq
(if ,(%inline logtest ,t (immediate #xffff))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 16)))
(set! ,result ,(%inline + ,result (immediate ,(fix 16))))))
(if ,(%inline logtest ,t (immediate #xff))
,(%constant svoid)
(seq
(set! ,t ,(%inline srl ,t (immediate 8)))
(set! ,result ,(%inline + ,result (immediate ,(fix 8))))))
,(%inline + ,result
(inline ,(make-info-load 'unsigned-8 #f) ,%load
,(%tc-ref fxfirst-bit-set-bv)
,(%inline logand ,t (immediate #xff))
,(%constant bytevector-data-disp)))))))))])
(let ()
(define-syntax type-pred
(syntax-rules ()
[(_ name? mask type)
(define-inline 2 name?
[(e) (%type-check mask type ,e)])]))
(define-syntax typed-object-pred
(syntax-rules ()
[(_ name? mask type)
(define-inline 2 name?
[(e)
(bind #t (e)
(%typed-object-check mask type ,e))])]))
(type-pred boolean? mask-boolean type-boolean)
(type-pred bwp-object? mask-bwp sbwp)
(type-pred char? mask-char type-char)
(type-pred eof-object? mask-eof seof)
(type-pred fixnum? mask-fixnum type-fixnum)
(type-pred flonum? mask-flonum type-flonum)
(type-pred null? mask-nil snil)
(type-pred pair? mask-pair type-pair)
(type-pred procedure? mask-closure type-closure)
(type-pred symbol? mask-symbol type-symbol)
(type-pred $unbound-object? mask-unbound sunbound)
(typed-object-pred bignum? mask-bignum type-bignum)
(typed-object-pred box? mask-box type-box)
(typed-object-pred mutable-box? mask-mutable-box type-mutable-box)
(typed-object-pred immutable-box? mask-mutable-box type-immutable-box)
(typed-object-pred bytevector? mask-bytevector type-bytevector)
(typed-object-pred mutable-bytevector? mask-mutable-bytevector type-mutable-bytevector)
(typed-object-pred immutable-bytevector? mask-mutable-bytevector type-immutable-bytevector)
(typed-object-pred $code? mask-code type-code)
(typed-object-pred $exactnum? mask-exactnum type-exactnum)
(typed-object-pred fxvector? mask-fxvector type-fxvector)
(typed-object-pred mutable-fxvector? mask-mutable-fxvector type-mutable-fxvector)
(typed-object-pred immutable-fxvector? mask-mutable-fxvector type-immutable-fxvector)
(typed-object-pred $inexactnum? mask-inexactnum type-inexactnum)
(typed-object-pred $rtd-counts? mask-rtd-counts type-rtd-counts)
(typed-object-pred input-port? mask-input-port type-input-port)
(typed-object-pred output-port? mask-output-port type-output-port)
(typed-object-pred port? mask-port type-port)
(typed-object-pred ratnum? mask-ratnum type-ratnum)
(typed-object-pred $record? mask-record type-record)
(typed-object-pred string? mask-string type-string)
(typed-object-pred mutable-string? mask-mutable-string type-mutable-string)
(typed-object-pred immutable-string? mask-mutable-string type-immutable-string)
(typed-object-pred $system-code? mask-system-code type-system-code)
(typed-object-pred $tlc? mask-tlc type-tlc)
(typed-object-pred vector? mask-vector type-vector)
(typed-object-pred mutable-vector? mask-mutable-vector type-mutable-vector)
(typed-object-pred immutable-vector? mask-mutable-vector type-immutable-vector)
(typed-object-pred thread? mask-thread type-thread))
(define-inline 3 $bigpositive?
[(e) (%type-check mask-signed-bignum type-positive-bignum
,(%mref ,e ,(constant bignum-type-disp)))])
(define-inline 3 csv7:record-field-accessible?
[(e1 e2) (%seq ,e1 ,e2 ,(%constant strue))])
(define-inline 2 cflonum?
[(e) (bind #t (e)
`(if ,(%type-check mask-flonum type-flonum ,e)
,(%constant strue)
,(%typed-object-check mask-inexactnum type-inexactnum ,e)))])
(define-inline 2 $immediate?
[(e) (bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(%constant strue)
,(%type-check mask-immediate type-immediate ,e)))])
(define-inline 3 $inexactnum-real-part
[(e) (build-$inexactnum-real-part e)])
(define-inline 3 $inexactnum-imag-part
[(e) (build-$inexactnum-imag-part e)])
(define-inline 3 cfl-real-part
[(e) (bind #t (e)
`(if ,(%type-check mask-flonum type-flonum ,e)
,e
,(build-$inexactnum-real-part e)))])
(define-inline 3 cfl-imag-part
[(e) (bind #t (e)
`(if ,(%type-check mask-flonum type-flonum ,e)
(quote 0.0)
,(build-$inexactnum-imag-part e)))])
(define-inline 3 $closure-ref
[(e-v e-i)
(nanopass-case (L7 Expr) e-i
[(quote ,d)
(guard (target-fixnum? d))
(%mref ,e-v ,(+ (fix d) (constant closure-data-disp)))]
[else (%mref ,e-v ,e-i ,(constant closure-data-disp))])])
(define-inline 3 $closure-code
[(e) (%inline -
,(%mref ,e ,(constant closure-code-disp))
,(%constant code-data-disp))])
(define-inline 3 $code-free-count
[(e) (build-fix (%mref ,e ,(constant code-closure-length-disp)))])
(define-inline 2 $unbound-object
[() `(quote ,($unbound-object))])
(define-inline 2 void
[() `(quote ,(void))])
(define-inline 2 eof-object
[() `(quote #!eof)])
(define-inline 2 cons
[(e1 e2)
(bind #f (e1 e2)
(bind #t ([t (%constant-alloc type-pair (constant size-pair))])
(%seq
(set! ,(%mref ,t ,(constant pair-car-disp)) ,e1)
(set! ,(%mref ,t ,(constant pair-cdr-disp)) ,e2)
,t)))])
(define-inline 2 box
[(e)
(bind #f (e)
(bind #t ([t (%constant-alloc type-typed-object (constant size-box))])
(%seq
(set! ,(%mref ,t ,(constant box-type-disp)) ,(%constant type-box))
(set! ,(%mref ,t ,(constant box-ref-disp)) ,e)
,t)))])
(define-inline 2 box-immutable
[(e)
(bind #f (e)
(bind #t ([t (%constant-alloc type-typed-object (constant size-box))])
(%seq
(set! ,(%mref ,t ,(constant box-type-disp)) ,(%constant type-immutable-box))
(set! ,(%mref ,t ,(constant box-ref-disp)) ,e)
,t)))])
(define-inline 3 $make-tlc
[(e-ht e-keyval e-next)
(bind #f (e-ht e-keyval e-next)
(bind #t ([t (%constant-alloc type-typed-object (constant size-tlc))])
(%seq
(set! ,(%mref ,t ,(constant tlc-type-disp)) ,(%constant type-tlc))
(set! ,(%mref ,t ,(constant tlc-ht-disp)) ,e-ht)
(set! ,(%mref ,t ,(constant tlc-keyval-disp)) ,e-keyval)
(set! ,(%mref ,t ,(constant tlc-next-disp)) ,e-next)
,t)))])
(define-inline 2 list
[e* (build-list e*)])
(let ()
(define (go e e*)
(bind #f (e)
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-pair (fx* (constant size-pair) (length e*)))])
(let loop ([e e] [e* e*] [i 0])
(let ([e2 (car e*)] [e* (cdr e*)])
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-car-disp))) ,e)
,(if (null? e*)
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-cdr-disp))) ,e2)
,t)
(let ([next-i (fx+ i (constant size-pair))])
`(seq
(set! ,(%mref ,t ,(fx+ i (constant pair-cdr-disp)))
,(%inline + ,t (immediate ,next-i)))
,(loop e2 e* next-i)))))))))))
(define-inline 2 list*
[(e) e]
[(e . e*) (go e e*)])
(define-inline 2 cons*
[(e) e]
[(e . e*) (go e e*)]))
(define-inline 2 vector
[() `(quote #())]
[e*
(let ([n (length e*)])
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-vector) (fx* n (constant ptr-bytes))))])
(let loop ([e* e*] [i 0])
(if (null? e*)
`(seq
(set! ,(%mref ,t ,(constant vector-type-disp))
(immediate ,(+ (fx* n (constant vector-length-factor))
(constant type-vector))))
,t)
`(seq
(set! ,(%mref ,t ,(fx+ i (constant vector-data-disp))) ,(car e*))
,(loop (cdr e*) (fx+ i (constant ptr-bytes)))))))))])
(let ()
(define (go e*)
(let ([n (length e*)])
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-fxvector) (fx* n (constant ptr-bytes))))])
(let loop ([e* e*] [i 0])
(if (null? e*)
`(seq
(set! ,(%mref ,t ,(constant fxvector-type-disp))
(immediate ,(+ (fx* n (constant fxvector-length-factor))
(constant type-fxvector))))
,t)
`(seq
(set! ,(%mref ,t ,(fx+ i (constant fxvector-data-disp))) ,(car e*))
,(loop (cdr e*) (fx+ i (constant ptr-bytes))))))))))
(define-inline 2 fxvector
[() `(quote #vfx())]
[e* (and (andmap (lambda (x) (constant? target-fixnum? x)) e*) (go e*))])
(define-inline 3 fxvector
[() `(quote #vfx())]
[e* (go e*)]))
(let ()
(define (go e*)
(let ([n (length e*)])
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-string) (fx* n (constant string-char-bytes))))])
(let loop ([e* e*] [i 0])
(if (null? e*)
`(seq
(set! ,(%mref ,t ,(constant string-type-disp))
(immediate ,(+ (fx* n (constant string-length-factor))
(constant type-string))))
,t)
`(seq
(inline ,(make-info-load (string-char-type) #f) ,%store ,t ,%zero
(immediate ,(fx+ i (constant string-data-disp)))
,(car e*))
,(loop (cdr e*) (fx+ i (constant string-char-bytes))))))))))
(define-inline 2 string
[() `(quote "")]
[e* (and (andmap (lambda (x) (constant? char? x)) e*) (go e*))])
(define-inline 3 string
[() `(quote "")]
[e* (go e*)]))
(let () ; level 2 car, cdr, caar, etc.
(define-syntax def-c..r*
(lambda (x)
(define (go ad*)
(let ([id (datum->syntax #'* (string->symbol (format "c~{~a~}r" ad*)))])
#`(define-inline 2 #,id
[(e) (let ([Lerr (make-local-label 'Lerr)])
#,(let f ([ad* ad*])
(let ([builder (if (char=? (car ad*) #\a) #'build-car #'build-cdr)]
[ad* (cdr ad*)])
(if (null? ad*)
#`(bind #t (e)
`(if ,(build-pair? e)
,(#,builder e)
(label ,Lerr ,(build-libcall #t src sexpr #,id e))))
#`(bind #t ([t #,(f ad*)])
`(if ,(build-pair? t)
,(#,builder t)
(goto ,Lerr)))))))])))
(let f ([n 4] [ad* '()])
(let ([f (lambda (ad*)
(let ([defn (go ad*)])
(if (fx= n 1)
defn
#`(begin #,defn #,(f (fx- n 1) ad*)))))])
#`(begin
#,(f (cons #\a ad*))
#,(f (cons #\d ad*)))))))
def-c..r*)
(let () ; level 3 car, cdr, caar, etc.
(define-syntax def-c..r*
(lambda (x)
(define (go ad*)
(let ([id (datum->syntax #'* (string->symbol (format "c~{~a~}r" ad*)))])
#`(define-inline 3 #,id
[(e) #,(let f ([ad* ad*])
(let ([builder (if (char=? (car ad*) #\a) #'build-car #'build-cdr)]
[ad* (cdr ad*)])
(if (null? ad*)
#`(#,builder e)
#`(#,builder #,(f ad*)))))])))
(let f ([n 4] [ad* '()])
(let ([f (lambda (ad*)
(let ([defn (go ad*)])
(if (fx= n 1)
defn
#`(begin #,defn #,(f (fx- n 1) ad*)))))])
#`(begin
#,(f (cons #\a ad*))
#,(f (cons #\d ad*)))))))
def-c..r*)
(let () ; level 3 simple accessors, e.g., unbox, vector-length
(define-syntax inline-accessor
(syntax-rules ()
[(_ prim disp)
(define-inline 3 prim
[(e) (%mref ,e ,(constant disp))])]))
(inline-accessor unbox box-ref-disp)
(inline-accessor $symbol-name symbol-name-disp)
(inline-accessor $symbol-property-list symbol-plist-disp)
(inline-accessor $system-property-list symbol-splist-disp)
(inline-accessor $symbol-hash symbol-hash-disp)
(inline-accessor $ratio-numerator ratnum-numerator-disp)
(inline-accessor $ratio-denominator ratnum-denominator-disp)
(inline-accessor $exactnum-real-part exactnum-real-disp)
(inline-accessor $exactnum-imag-part exactnum-imag-disp)
(inline-accessor binary-port-input-buffer port-ibuffer-disp)
(inline-accessor textual-port-input-buffer port-ibuffer-disp)
(inline-accessor binary-port-output-buffer port-obuffer-disp)
(inline-accessor textual-port-output-buffer port-obuffer-disp)
(inline-accessor $code-name code-name-disp)
(inline-accessor $code-arity-mask code-arity-mask-disp)
(inline-accessor $code-info code-info-disp)
(inline-accessor $code-pinfo* code-pinfo*-disp)
(inline-accessor $continuation-link continuation-link-disp)
(inline-accessor $continuation-winders continuation-winders-disp)
(inline-accessor csv7:record-type-descriptor record-type-disp)
(inline-accessor $record-type-descriptor record-type-disp)
(inline-accessor record-rtd record-type-disp)
(inline-accessor $port-handler port-handler-disp)
(inline-accessor $port-info port-info-disp)
(inline-accessor port-name port-name-disp)
(inline-accessor $thread-tc thread-tc-disp)
)
(define-inline 2 unbox
[(e)
(bind #t (e)
`(if ,(%typed-object-check mask-box type-box ,e)
,(%mref ,e ,(constant box-ref-disp))
,(build-libcall #t src sexpr unbox e)))])
(let ()
(define-syntax def-len
(syntax-rules ()
[(_ prim type-disp length-offset)
(define-inline 3 prim
[(e) (extract-length (%mref ,e ,(constant type-disp)) (constant length-offset))])]))
(def-len vector-length vector-type-disp vector-length-offset)
(def-len fxvector-length fxvector-type-disp fxvector-length-offset)
(def-len string-length string-type-disp string-length-offset)
(def-len bytevector-length bytevector-type-disp bytevector-length-offset)
(def-len $bignum-length bignum-type-disp bignum-length-offset))
(let ()
(define-syntax def-len
(syntax-rules ()
[(_ prim mask type type-disp length-offset)
(define-inline 2 prim
[(e) (let ([Lerr (make-local-label 'Lerr)])
(bind #t (e)
`(if ,(%type-check mask-typed-object type-typed-object ,e)
,(bind #t ([t/l (%mref ,e ,(constant type-disp))])
`(if ,(%type-check mask type ,t/l)
,(extract-length t/l (constant length-offset))
(goto ,Lerr)))
(label ,Lerr ,(build-libcall #t #f sexpr prim e)))))])]))
(def-len vector-length mask-vector type-vector vector-type-disp vector-length-offset)
(def-len fxvector-length mask-fxvector type-fxvector fxvector-type-disp fxvector-length-offset)
(def-len string-length mask-string type-string string-type-disp string-length-offset)
(def-len bytevector-length mask-bytevector type-bytevector bytevector-type-disp bytevector-length-offset))
; TODO: consider adding integer-valued?, rational?, rational-valued?,
; real?, and real-valued?
(define-inline 2 integer?
[(e) (bind #t (e)
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-simple-or
(%typed-object-check mask-bignum type-bignum ,e)
(build-and
(%type-check mask-flonum type-flonum ,e)
`(call ,(make-info-call src sexpr #f #f #f) #f ,(lookup-primref 3 'flinteger?) ,e)))))])
(let ()
(define build-number?
(lambda (e)
(bind #t (e)
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-simple-or
(%type-check mask-flonum type-flonum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(%type-check mask-other-number type-other-number
,(%mref ,e ,(constant bignum-type-disp)))))))))
(define-inline 2 number?
[(e) (build-number? e)])
(define-inline 2 complex?
[(e) (build-number? e)]))
(define-inline 3 set-car!
[(e1 e2) (build-dirty-store e1 (constant pair-car-disp) e2)])
(define-inline 3 set-cdr!
[(e1 e2) (build-dirty-store e1 (constant pair-cdr-disp) e2)])
(define-inline 3 set-box!
[(e1 e2) (build-dirty-store e1 (constant box-ref-disp) e2)])
(define-inline 3 box-cas!
[(e1 e2 e3)
(bind #t (e2)
(build-dirty-store e1 %zero (constant box-ref-disp) e3 (make-build-cas e2) build-cas-seq))])
(define-inline 3 $set-symbol-name!
[(e1 e2) (build-dirty-store e1 (constant symbol-name-disp) e2)])
(define-inline 3 $set-symbol-property-list!
[(e1 e2) (build-dirty-store e1 (constant symbol-plist-disp) e2)])
(define-inline 3 $set-system-property-list!
[(e1 e2) (build-dirty-store e1 (constant symbol-splist-disp) e2)])
(define-inline 3 $set-port-info!
[(e1 e2) (build-dirty-store e1 (constant port-info-disp) e2)])
(define-inline 3 set-port-name!
[(e1 e2) (build-dirty-store e1 (constant port-name-disp) e2)])
(define-inline 2 set-box!
[(e-box e-new)
(bind #t (e-box e-new)
`(if ,(%typed-object-check mask-mutable-box type-mutable-box ,e-box)
,(build-dirty-store e-box (constant box-ref-disp) e-new)
,(build-libcall #t src sexpr set-box! e-box e-new)))])
(define-inline 2 box-cas!
[(e-box e-old e-new)
(bind #t (e-box e-old e-new)
`(if ,(%typed-object-check mask-mutable-box type-mutable-box ,e-box)
,(build-dirty-store e-box %zero (constant box-ref-disp) e-new (make-build-cas e-old) build-cas-seq)
,(build-libcall #t src sexpr box-cas! e-box e-old e-new)))])
(define-inline 2 set-car!
[(e-pair e-new)
(bind #t (e-pair e-new)
`(if ,(%type-check mask-pair type-pair ,e-pair)
,(build-dirty-store e-pair (constant pair-car-disp) e-new)
,(build-libcall #t src sexpr set-car! e-pair e-new)))])
(define-inline 2 set-cdr!
[(e-pair e-new)
(bind #t (e-pair e-new)
`(if ,(%type-check mask-pair type-pair ,e-pair)
,(build-dirty-store e-pair (constant pair-cdr-disp) e-new)
,(build-libcall #t src sexpr set-cdr! e-pair e-new)))])
(define-inline 3 $set-symbol-hash!
; no need for dirty store---e2 should be a fixnum
[(e1 e2) `(set! ,(%mref ,e1 ,(constant symbol-hash-disp)) ,e2)])
(let ()
(define-syntax define-tlc-parameter
(syntax-rules ()
[(_ name disp)
(define-inline 3 name
[(e-x) (%mref ,e-x ,(constant disp))])]
[(_ name name! disp)
(begin
(define-tlc-parameter name disp)
(define-inline 3 name!
[(e-x e-new) (build-dirty-store e-x (constant disp) e-new)]))]))
(define-tlc-parameter $tlc-keyval tlc-keyval-disp)
(define-tlc-parameter $tlc-ht tlc-ht-disp)
(define-tlc-parameter $tlc-next $set-tlc-next! tlc-next-disp))
(define-inline 2 $top-level-value
[(e) (nanopass-case (L7 Expr) e
[(quote ,d)
(guard (symbol? d))
(if (any-set? (prim-mask (or primitive system)) ($sgetprop d '*flags* 0))
(Symref d)
(bind #t (e)
(bind #t ([t (%mref ,e ,(constant symbol-value-disp))])
`(if ,(%type-check mask-unbound sunbound ,t)
,(build-libcall #t #f sexpr $top-level-value e)
,t))))]
[else
(bind #t (e)
(let ([Lfail (make-local-label 'tlv-fail)])
`(if ,(%type-check mask-symbol type-symbol ,e)
,(bind #t ([t (%mref ,e ,(constant symbol-value-disp))])
`(if ,(%type-check mask-unbound sunbound ,t)
(goto ,Lfail)
,t))
(label ,Lfail ,(build-libcall #t #f sexpr $top-level-value e)))))])])
(define-inline 3 $top-level-value
[(e) (nanopass-case (L7 Expr) e
[(quote ,d) (guard (symbol? d)) (Symref d)]
[else (%mref ,e ,(constant symbol-value-disp))])])
(let ()
(define (go e-sym e-value)
(bind #t (e-sym)
`(seq
,(build-dirty-store e-sym (constant symbol-value-disp) e-value)
(set! ,(%mref ,e-sym ,(constant symbol-pvalue-disp))
(literal
,(make-info-literal #f 'library
(lookup-libspec nonprocedure-code)
(constant code-data-disp)))))))
(define-inline 3 $set-top-level-value!
[(e-sym e-value) (go e-sym e-value)])
(define-inline 2 $set-top-level-value!
[(e-sym e-value) (and (constant? symbol? e-sym) (go e-sym e-value))]))
(define-inline 3 $top-level-bound?
[(e-sym)
(build-not
(%type-check mask-unbound sunbound
,(nanopass-case (L7 Expr) e-sym
[(quote ,d) (guard (symbol? d)) (Symref d)]
[else (%mref ,e-sym ,(constant symbol-value-disp))])))])
(let ()
(define parse-format
(lambda (who src cntl-arg args)
(nanopass-case (L7 Expr) cntl-arg
[(quote ,d)
(guard (c [(and (assertion-violation? c)
(format-condition? c)
(message-condition? c)
(irritants-condition? c))
($source-warning 'compile
src #t
"~? in call to ~s"
(condition-message c)
(condition-irritants c)
who)
#f])
(#%$parse-format-string who d (length args)))]
[else #f])))
(define fmt->expr
($make-fmt->expr
(lambda (d) `(quote ,d))
(lambda (e1 e2) `(seq ,e1 ,e2))
(lambda (src sexpr prim arg*)
`(call ,(make-info-call src sexpr #f #f #f) #f
,(lookup-primref 3 prim)
,arg* ...))))
(define build-format
(lambda (who src sexpr op-arg cntl-arg arg*)
(let ([x (parse-format who src cntl-arg arg*)])
(and x
(cond
[(and (fx= (length x) 1)
(string? (car x))
(nanopass-case (L7 Expr) op-arg
[(quote ,d) (eq? d #f)]
[else #f]))
(%primcall src sexpr string-copy (quote ,(car x)))]
[(and (nanopass-case (L7 Expr) op-arg
[(quote ,d) (not (eq? d #f))]
[else #t])
(let-values ([(op-arg dobind) (binder #t 'ptr op-arg)]
[(arg* dobind*) (list-binder #t 'ptr arg*)])
(let ([e (fmt->expr src sexpr x op-arg arg*)])
(and e (dobind (dobind* e))))))]
[else
(%primcall src sexpr $dofmt (quote ,who) ,op-arg ,cntl-arg
(quote ,x)
,(build-list arg*))])))))
(define-inline 2 errorf
[(e-who e-str . e*)
(parse-format 'errorf src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #t) #f ,(Symref 'errorf) ,e-who ,e-str ,e* ...))])
(define-inline 2 assertion-violationf
[(e-who e-str . e*)
(parse-format 'assertion-violationf src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #t) #f ,(Symref 'assertion-violationf) ,e-who ,e-str ,e* ...))])
(define-inline 2 $oops
[(e-who e-str . e*)
(parse-format '$oops src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #t) #f ,(Symref '$oops) ,e-who ,e-str ,e* ...))])
(define-inline 2 $impoops
[(e-who e-str . e*)
(parse-format '$impoops src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #t) #f ,(Symref '$impoops) ,e-who ,e-str ,e* ...))])
(define-inline 2 warningf
[(e-who e-str . e*)
(parse-format 'warningf src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #f) #f ,(Symref 'warningf) ,e-who ,e-str ,e* ...))])
(define-inline 2 $source-violation
[(e-who e-src e-start? e-str . e*)
(parse-format '$source-violation src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #t) #f ,(Symref '$source-violation)
,e-who ,e-src ,e-start? ,e-str ,e* ...))])
(define-inline 2 $source-warning
[(e-who e-src e-start? e-str . e*)
(parse-format '$source-warning src e-str e*)
`(seq (pariah) (call ,(make-info-call src sexpr #f #t #f) #f ,(Symref '$source-warning)
,e-who ,e-src ,e-start? ,e-str ,e* ...))])
(define-inline 2 fprintf
[(e-op e-str . e*)
(parse-format 'fprintf src e-str e*)
#f])
(define-inline 3 fprintf
[(e-op e-str . e*) (build-format 'fprintf src sexpr e-op e-str e*)])
(define-inline 2 printf
[(e-str . e*)
(build-format 'printf src sexpr (%tc-ref current-output) e-str e*)])
(define-inline 2 format
[(e . e*)
(nanopass-case (L7 Expr) e
[(quote ,d)
(if (string? d)
(build-format 'format src sexpr `(quote #f) e e*)
(and (not (null? e*))
(cond
[(eq? d #f) (build-format 'format src sexpr e (car e*) (cdr e*))]
[(eq? d #t) (build-format 'format src sexpr
(%tc-ref current-output)
(car e*) (cdr e*))]
[else #f])))]
[else #f])]))
(let ()
(define hand-coded-closure?
(lambda (name)
(not (memq name '(nuate nonprocedure-code error-invoke invoke)))))
(define-inline 2 $hand-coded
[(name)
(nanopass-case (L7 Expr) name
[(quote ,d)
(guard (symbol? d))
(let ([l (make-local-label 'hcl)])
(set! new-l* (cons l new-l*))
(set! new-le* (cons (with-output-language (L9 CaseLambdaExpr) `(hand-coded ,d)) new-le*))
(if (hand-coded-closure? d)
`(literal ,(make-info-literal #f 'closure l 0))
`(label-ref ,l 0)))]
[(seq (profile ,src) ,[e]) `(seq (profile ,src) ,e)]
[else ($oops '$hand-coded "~s is not a quoted symbol" name)])]))
(define-inline 2 $tc
[() %tc])
(define-inline 3 $tc-field
[(e-fld e-tc)
(nanopass-case (L7 Expr) e-fld
[(quote ,d)
(let ()
(define-syntax a
(lambda (x)
#`(case d
#,@(fold-left
(lambda (ls field)
(apply
(lambda (name type disp len)
(if (eq? type 'ptr)
(cons
(with-syntax ([name (datum->syntax #'* name)])
#'[(name) (%tc-ref ,e-tc name)])
ls)
ls))
field))
'() (getprop 'tc '*fields* '()))
[else #f])))
a)]
[else #f])]
[(e-fld e-tc e-val)
(nanopass-case (L7 Expr) e-fld
[(quote ,d)
(let ()
(define-syntax a
(lambda (x)
#`(case d
#,@(fold-left
(lambda (ls field)
(apply
(lambda (name type disp len)
(if (eq? type 'ptr)
(cons
(with-syntax ([name (datum->syntax #'* name)])
#'[(name) `(set! ,(%tc-ref ,e-tc name) ,e-val)])
ls)
ls))
field))
'() (getprop 'tc '*fields* '()))
[else #f])))
a)]
[else #f])])
(let ()
(define-syntax define-tc-parameter
(syntax-rules ()
[(_ name tc-name)
(begin
(define-inline 2 name
[() (%tc-ref tc-name)]
[(x) #f])
(define-inline 3 name
[() (%tc-ref tc-name)]
[(x) `(set! ,(%tc-ref tc-name) ,x)]))]))
(define-tc-parameter current-input-port current-input)
(define-tc-parameter current-output-port current-output)
(define-tc-parameter current-error-port current-error)
(define-tc-parameter generate-inspector-information generate-inspector-information)
(define-tc-parameter generate-procedure-source-information generate-procedure-source-information)
(define-tc-parameter generate-profile-forms generate-profile-forms)
(define-tc-parameter $compile-profile compile-profile)
(define-tc-parameter optimize-level optimize-level)
(define-tc-parameter subset-mode subset-mode)
(define-tc-parameter $suppress-primitive-inlining suppress-primitive-inlining)
(define-tc-parameter $block-counter block-counter)
(define-tc-parameter $sfd sfd)
(define-tc-parameter $current-mso current-mso)
(define-tc-parameter $target-machine target-machine)
(define-tc-parameter $current-stack-link stack-link)
(define-tc-parameter $current-winders winders)
(define-tc-parameter default-record-equal-procedure default-record-equal-procedure)
(define-tc-parameter default-record-hash-procedure default-record-hash-procedure)
)
(define-inline 3 $install-guardian
[(e-obj e-rep e-tconc)
(bind #f (e-obj e-rep e-tconc)
(bind #t ([t (%constant-alloc typemod (constant size-guardian-entry))])
(%seq
(set! ,(%mref ,t ,(constant guardian-entry-obj-disp)) ,e-obj)
(set! ,(%mref ,t ,(constant guardian-entry-rep-disp)) ,e-rep)
(set! ,(%mref ,t ,(constant guardian-entry-tconc-disp)) ,e-tconc)
(set! ,(%mref ,t ,(constant guardian-entry-next-disp)) ,(%tc-ref guardian-entries))
(set! ,(%tc-ref guardian-entries) ,t))))])
(define-inline 3 $install-ftype-guardian
[(e-obj e-tconc)
(bind #f (e-obj e-tconc)
(bind #t ([t (%constant-alloc typemod (constant size-guardian-entry))])
(%seq
(set! ,(%mref ,t ,(constant guardian-entry-obj-disp)) ,e-obj)
(set! ,(%mref ,t ,(constant guardian-entry-rep-disp)) (immediate ,(constant ftype-guardian-rep)))
(set! ,(%mref ,t ,(constant guardian-entry-tconc-disp)) ,e-tconc)
(set! ,(%mref ,t ,(constant guardian-entry-next-disp)) ,(%tc-ref guardian-entries))
(set! ,(%tc-ref guardian-entries) ,t))))])
(define-inline 2 guardian?
[(e)
(bind #t (e)
(build-and
(%type-check mask-closure type-closure ,e)
(%type-check mask-guardian-code type-guardian-code
,(%mref
,(%inline -
,(%mref ,e ,(constant closure-code-disp))
,(%constant code-data-disp))
,(constant code-type-disp)))))])
(define-inline 2 virtual-register-count
[() `(quote ,(constant virtual-register-count))])
(let ()
(define constant-ref
(lambda (e-idx)
(nanopass-case (L7 Expr) e-idx
[(quote ,d)
(guard (and (fixnum? d) ($fxu< d (constant virtual-register-count))))
(%mref ,%tc ,(fx+ (constant tc-virtual-registers-disp) (fx* d (constant ptr-bytes))))]
[else #f])))
(define constant-set
(lambda (e-idx e-val)
(let ([ref (constant-ref e-idx)])
(and ref `(set! ,ref ,e-val)))))
(define index-check
(lambda (e-idx libcall e)
`(if (if ,(%type-check mask-fixnum type-fixnum ,e-idx)
,(%inline u< ,e-idx (immediate ,(fix (constant virtual-register-count))))
,(%constant sfalse))
,e
,libcall)))
(meta-assert (= (constant log2-ptr-bytes) (constant fixnum-offset)))
(define-inline 3 virtual-register
[(e-idx)
(or (constant-ref e-idx)
(%mref ,%tc ,e-idx ,(constant tc-virtual-registers-disp)))])
(define-inline 2 virtual-register
[(e-idx)
(or (constant-ref e-idx)
(bind #t (e-idx)
(index-check e-idx
(build-libcall #t src sexpr virtual-register e-idx)
(%mref ,%tc ,e-idx ,(constant tc-virtual-registers-disp)))))])
(define-inline 3 set-virtual-register!
[(e-idx e-val)
(or (constant-set e-idx e-val)
`(set! ,(%mref ,%tc ,e-idx ,(constant tc-virtual-registers-disp)) ,e-val))])
(define-inline 2 set-virtual-register!
[(e-idx e-val)
(or (constant-set e-idx e-val)
(bind #t (e-idx)
(bind #f (e-val)
(index-check e-idx
(build-libcall #t src sexpr set-virtual-register! e-idx)
`(set! ,(%mref ,%tc ,e-idx ,(constant tc-virtual-registers-disp)) ,e-val)))))]))
(define-inline 2 $thread-list
[() `(literal ,(make-info-literal #t 'entry (lookup-c-entry thread-list) 0))])
(when-feature pthreads
(define-inline 2 $raw-tc-mutex
[() `(literal ,(make-info-literal #f 'entry (lookup-c-entry raw-tc-mutex) 0))])
(define-inline 2 $raw-collect-cond
[() `(literal ,(make-info-literal #f 'entry (lookup-c-entry raw-collect-cond) 0))]))
(define-inline 2 not
[(e) `(if ,e ,(%constant sfalse) ,(%constant strue))])
(define-inline 2 most-negative-fixnum
[() `(quote ,(constant most-negative-fixnum))])
(define-inline 2 most-positive-fixnum
[() `(quote ,(constant most-positive-fixnum))])
(define-inline 2 least-fixnum
[() `(quote ,(constant most-negative-fixnum))])
(define-inline 2 greatest-fixnum
[() `(quote ,(constant most-positive-fixnum))])
(define-inline 2 fixnum-width
[() `(quote ,(constant fixnum-bits))])
(define-inline 2 native-endianness
[() `(quote ,(constant native-endianness))])
(define-inline 2 directory-separator
[() `(quote ,(if-feature windows #\\ #\/))])
(let () ; level 2 char=?, r6rs:char=?, etc.
(define-syntax char-pred
(syntax-rules ()
[(_ op r6rs:op inline-op)
(let ()
(define (go2 src sexpr e1 e2)
(bind #t (e1 e2)
`(if ,(build-chars? e1 e2)
,(%inline inline-op ,e1 ,e2)
,(build-libcall #t src sexpr op e1 e2))))
(define (go3 src sexpr e1 e2 e3)
(and (constant? char? e1)
(constant? char? e3)
(bind #t (e2)
`(if ,(%type-check mask-char type-char ,e2)
,(build-and
(%inline inline-op ,e1 ,e2)
(%inline inline-op ,e2 ,e3))
; could also pass e2 and e3:
,(build-libcall #t src sexpr op e1 e2)))))
(define-inline 2 op
[(e1 e2) (go2 src sexpr e1 e2)]
[(e1 e2 e3) (go3 src sexpr e1 e2 e3)]
[(e1 . e*) #f])
(define-inline 2 r6rs:op
[(e1 e2) (go2 src sexpr e1 e2)]
[(e1 e2 e3) (go3 src sexpr e1 e2 e3)]
[(e1 e2 . e*) #f]))]))
(char-pred char<? r6rs:char<? <)
(char-pred char<=? r6rs:char<=? <=)
(char-pred char=? r6rs:char=? eq?)
(char-pred char>=? r6rs:char>=? >=)
(char-pred char>? r6rs:char>? >))
(let () ; level 3 char=?, r6rs:char=?, etc.
(define-syntax char-pred
(syntax-rules ()
[(_ op r6rs:op inline-op)
(let ()
(define (go2 e1 e2)
(%inline inline-op ,e1 ,e2))
(define (go3 e1 e2 e3)
(bind #t (e2)
(bind #f (e3)
(build-and
(go2 e1 e2)
(go2 e2 e3)))))
(define-inline 3 op
[(e) `(seq ,e ,(%constant strue))]
[(e1 e2) (go2 e1 e2)]
[(e1 e2 e3) (go3 e1 e2 e3)]
[(e1 . e*) #f])
(define-inline 3 r6rs:op
[(e1 e2) (go2 e1 e2)]
[(e1 e2 e3) (go3 e1 e2 e3)]
[(e1 e2 . e*) #f]))]))
(char-pred char<? r6rs:char<? <)
(char-pred char<=? r6rs:char<=? <=)
(char-pred char=? r6rs:char=? eq?)
(char-pred char>=? r6rs:char>=? >=)
(char-pred char>? r6rs:char>? >))
(define-inline 3 map
[(e-proc e-ls)
(or (nanopass-case (L7 Expr) e-proc
[,pr
(and (all-set? (prim-mask unsafe) (primref-flags pr))
(let ([name (primref-name pr)])
(or (and (eq? name 'car) (build-libcall #f src sexpr map-car e-ls))
(and (eq? name 'cdr) (build-libcall #f src sexpr map-cdr e-ls)))))]
[else #f])
(build-libcall #f src sexpr map1 e-proc e-ls))]
[(e-proc e-ls1 e-ls2)
(or (nanopass-case (L7 Expr) e-proc
[,pr
(and (eq? (primref-name pr) 'cons)
(build-libcall #f src sexpr map-cons e-ls1 e-ls2))]
[else #f])
(build-libcall #f src sexpr map2 e-proc e-ls1 e-ls2))]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 andmap
[(e-proc e-ls) (build-libcall #f src sexpr andmap1 e-proc e-ls)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 for-all
[(e-proc e-ls) (build-libcall #f src sexpr andmap1 e-proc e-ls)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 ormap
[(e-proc e-ls) (build-libcall #f src sexpr ormap1 e-proc e-ls)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 exists
[(e-proc e-ls) (build-libcall #f src sexpr ormap1 e-proc e-ls)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 fold-left
[(e-proc e-base e-ls) (build-libcall #f src sexpr fold-left1 e-proc e-base e-ls)]
[(e-proc e-base e-ls1 e-ls2) (build-libcall #f src sexpr fold-left2 e-proc e-base e-ls1 e-ls2)]
[(e-proc e-base e-ls . e-ls*) #f])
(define-inline 3 fold-right
[(e-proc e-base e-ls) (build-libcall #f src sexpr fold-right1 e-proc e-base e-ls)]
[(e-proc e-base e-ls1 e-ls2) (build-libcall #f src sexpr fold-right2 e-proc e-base e-ls1 e-ls2)]
[(e-proc e-base e-ls . e-ls*) #f])
(define-inline 3 for-each
[(e-proc e-ls) (build-libcall #f src sexpr for-each1 e-proc e-ls)]
[(e-proc e-ls1 e-ls2) (build-libcall #f src sexpr for-each2 e-proc e-ls1 e-ls2)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 vector-map
[(e-proc e-ls) (build-libcall #f src sexpr vector-map1 e-proc e-ls)]
[(e-proc e-ls1 e-ls2) (build-libcall #f src sexpr vector-map2 e-proc e-ls1 e-ls2)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 vector-for-each
[(e-proc e-ls) (build-libcall #f src sexpr vector-for-each1 e-proc e-ls)]
[(e-proc e-ls1 e-ls2) (build-libcall #f src sexpr vector-for-each2 e-proc e-ls1 e-ls2)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 string-for-each
[(e-proc e-ls) (build-libcall #f src sexpr string-for-each1 e-proc e-ls)]
[(e-proc e-ls1 e-ls2) (build-libcall #f src sexpr string-for-each2 e-proc e-ls1 e-ls2)]
[(e-proc e-ls . e-ls*) #f])
(define-inline 3 reverse
[(e) (build-libcall #f src sexpr reverse e)])
(let ()
(define inline-getprop
(lambda (plist-offset e-sym e-key e-dflt)
(let ([t-ls (make-assigned-tmp 't-ls)] [t-cdr (make-tmp 't-cdr)] [Ltop (make-local-label 'Ltop)])
(bind #t (e-key e-dflt)
; indirect symbol after evaluating e-key and e-dflt
`(let ([,t-ls ,(%mref ,e-sym ,plist-offset)])
(label ,Ltop
(if ,(%inline eq? ,t-ls ,(%constant snil))
,e-dflt
(let ([,t-cdr ,(%mref ,t-ls ,(constant pair-cdr-disp))])
(if ,(%inline eq? ,(%mref ,t-ls ,(constant pair-car-disp)) ,e-key)
,(%mref ,t-cdr ,(constant pair-car-disp))
(seq
(set! ,t-ls ,(%mref ,t-cdr ,(constant pair-cdr-disp)))
(goto ,Ltop)))))))))))
(define-inline 3 getprop
[(e-sym e-key) (inline-getprop (constant symbol-plist-disp) e-sym e-key (%constant sfalse))]
[(e-sym e-key e-dflt) (inline-getprop (constant symbol-plist-disp) e-sym e-key e-dflt)])
(define-inline 3 $sgetprop
[(e-sym e-key e-dflt) (inline-getprop (constant symbol-splist-disp) e-sym e-key e-dflt)]))
(define-inline 3 assq
[(e-key e-ls)
(let ([t-ls (make-assigned-tmp 't-ls)] [Ltop (make-local-label 'Ltop)])
(bind #t (e-key)
`(let ([,t-ls ,e-ls])
(label ,Ltop
(if ,(%inline eq? ,t-ls ,(%constant snil))
,(%constant sfalse)
,(bind #t ([t-a (%mref ,t-ls ,(constant pair-car-disp))])
`(if ,(%inline eq? ,(%mref ,t-a ,(constant pair-car-disp)) ,e-key)
,t-a
(seq
(set! ,t-ls ,(%mref ,t-ls ,(constant pair-cdr-disp)))
(goto ,Ltop)))))))))])
(define-inline 3 length
[(e-ls)
(let ([t-ls (make-assigned-tmp 't-ls)]
[t-n (make-assigned-tmp 't-n)]
[Ltop (make-local-label 'Ltop)])
(bind #t (e-ls)
`(if ,(%inline eq? ,e-ls ,(%constant snil))
(immediate ,(fix 0))
(let ([,t-ls ,e-ls] [,t-n (immediate ,(fix 0))])
(label ,Ltop
,(%seq
(set! ,t-ls ,(%mref ,t-ls ,(constant pair-cdr-disp)))
(set! ,t-n ,(%inline + ,t-n (immediate ,(fix 1))))
(if ,(%inline eq? ,t-ls ,(%constant snil))
,t-n
(goto ,Ltop))))))))])
(define-inline 3 append
; TODO: hand-coded library routine that allocates the new pairs in a block
[() (%constant snil)]
[(e-ls) e-ls]
[(e-ls1 e-ls2) (build-libcall #f src sexpr append e-ls1 e-ls2)]
[(e-ls1 e-ls2 e-ls3)
(build-libcall #f src sexpr append e-ls1
(build-libcall #f #f sexpr append e-ls2 e-ls3))]
[(e-ls . e-ls*) #f])
(define-inline 3 apply
[(e0 e1) (build-libcall #f src sexpr apply0 e0 e1)]
[(e0 e1 e2) (build-libcall #f src sexpr apply1 e0 e1 e2)]
[(e0 e1 e2 e3) (build-libcall #f src sexpr apply2 e0 e1 e2 e3)]
[(e0 e1 e2 e3 e4) (build-libcall #f src sexpr apply3 e0 e1 e2 e3 e4)]
[(e0 e1 . e*) #f])
(define-inline 2 fxsll
[(e0 e1) (build-libcall #f src sexpr fxsll e0 e1)])
(define-inline 2 fxarithmetic-shift-left
[(e0 e1) (build-libcall #f src sexpr fxarithmetic-shift-left e0 e1)])
(define-inline 3 display-string
[(e-s) (build-libcall #f src sexpr display-string e-s (%tc-ref current-output))]
[(e-s e-op) (build-libcall #f src sexpr display-string e-s e-op)])
(define-inline 3 call-with-current-continuation
[(e) (build-libcall #f src sexpr callcc e)])
(define-inline 3 call/cc
[(e) (build-libcall #f src sexpr callcc e)])
(define-inline 3 call/1cc
[(e) (build-libcall #f src sexpr call1cc e)])
(define-inline 2 $event
[() (build-libcall #f src sexpr event)])
(define-inline 3 eq-hashtable-ref
[(e1 e2 e3) (build-libcall #f src sexpr eq-hashtable-ref e1 e2 e3)])
(define-inline 3 eq-hashtable-contains?
[(e1 e2) (build-libcall #f src sexpr eq-hashtable-contains? e1 e2)])
(define-inline 3 eq-hashtable-set!
[(e1 e2 e3) (build-libcall #f src sexpr eq-hashtable-set! e1 e2 e3)])
(define-inline 3 eq-hashtable-update!
[(e1 e2 e3 e4) (build-libcall #f src sexpr eq-hashtable-update! e1 e2 e3 e4)])
(define-inline 3 eq-hashtable-cell
[(e1 e2 e3) (build-libcall #f src sexpr eq-hashtable-cell e1 e2 e3)])
(define-inline 3 eq-hashtable-delete!
[(e1 e2) (build-libcall #f src sexpr eq-hashtable-delete! e1 e2)])
(define-inline 3 symbol-hashtable-ref
[(e1 e2 e3) (build-libcall #f src sexpr symbol-hashtable-ref e1 e2 e3)])
(define-inline 3 symbol-hashtable-contains?
[(e1 e2) (build-libcall #f src sexpr symbol-hashtable-contains? e1 e2)])
(define-inline 3 symbol-hashtable-set!
[(e1 e2 e3) (build-libcall #f src sexpr symbol-hashtable-set! e1 e2 e3)])
(define-inline 3 symbol-hashtable-update!
[(e1 e2 e3 e4) (build-libcall #f src sexpr symbol-hashtable-update! e1 e2 e3 e4)])
(define-inline 3 symbol-hashtable-cell
[(e1 e2 e3) (build-libcall #f src sexpr symbol-hashtable-cell e1 e2 e3)])
(define-inline 3 symbol-hashtable-delete!
[(e1 e2) (build-libcall #f src sexpr symbol-hashtable-delete! e1 e2)])
(define-inline 2 bytevector-s8-set!
[(e1 e2 e3) (build-libcall #f src sexpr bytevector-s8-set! e1 e2 e3)])
(define-inline 2 bytevector-u8-set!
[(e1 e2 e3) (build-libcall #f src sexpr bytevector-u8-set! e1 e2 e3)])
(define-inline 3 bytevector=?
[(e1 e2) (build-libcall #f src sexpr bytevector=? e1 e2)])
(let ()
(define eqok-help?
(lambda (obj)
(or (symbol? obj)
(char? obj)
(target-fixnum? obj)
(null? obj)
(boolean? obj)
(eqv? obj "")
(eqv? obj '#())
(eqv? obj '#vu8())
(eq? obj (void))
(eof-object? obj)
(bwp-object? obj)
($unbound-object? obj)
(eqv? obj '#vfx()))))
(define eqvok-help? number?)
(define e*ok?
(lambda (e*ok-help?)
(lambda (e)
(nanopass-case (L7 Expr) e
[(quote ,d) (e*ok-help? d)]
[else #f]))))
(define eqok? (e*ok? eqok-help?))
(define eqvok? (e*ok? eqvok-help?))
(define-inline 2 eqv?
[(e1 e2) (or (eqvop-null-fptr e1 e2)
(relop-length RELOP= e1 e2)
(if (or (eqok? e1) (eqok? e2))
(build-eq? e1 e2)
(build-eqv? src sexpr e1 e2)))])
(let ()
(define xform-equal?
(lambda (src sexpr e1 e2)
(nanopass-case (L7 Expr) e1
[(quote ,d1)
(let xform ([d1 d1] [e2 e2] [n 3] [k (lambda (e n) e)])
(if (eqok-help? d1)
(k (build-eq? `(quote ,d1) e2) n)
(if (eqvok-help? d1)
(k (build-eqv? src sexpr `(quote ,d1) e2) n)
(and (fx> n 0)
(pair? d1)
(let-values ([(e2 dobind) (binder #t 'ptr e2)])
(xform (car d1) (build-car e2) (fx- n 1)
(lambda (a n)
(xform (cdr d1) (build-cdr e2) n
(lambda (d n)
(k (dobind
(build-and
(build-pair? e2)
(build-and a d)))
n))))))))))]
[else #f])))
(define-inline 2 equal?
[(e1 e2) (or (eqvop-null-fptr e1 e2)
(relop-length RELOP= e1 e2)
(xform-equal? src sexpr e1 e2)
(xform-equal? src sexpr e2 e1))]))
(let ()
(define mem*ok?
(lambda (e*ok-help?)
(lambda (x)
(nanopass-case (L7 Expr) x
[(quote ,d)
(and (list? d)
(let f ([d d])
(or (null? d)
(and (e*ok-help? (car d))
(f (cdr d))))))]
[else #f]))))
(define memqok? (mem*ok? eqok-help?))
(define memvok? (mem*ok? eqvok-help?))
(define mem*->e*?s
(lambda (build-e*? limit)
(lambda (e-key e-ls)
(nanopass-case (L7 Expr) e-ls
[(quote ,d)
(and (let f ([d d] [n 0])
(or (null? d)
(and (pair? d)
(fx< n limit)
(f (cdr d) (fx1+ n)))))
(bind #t (e-key)
(let f ([ls d])
(if (null? ls)
`(quote #f)
`(if ,(build-e*? e-key `(quote ,(car ls)))
(quote ,ls)
,(f (cdr ls)))))))]
[else #f]))))
(define memq->eq?s (mem*->e*?s build-eq? 8))
(define (memv->eqv?s src sexpr) (mem*->e*?s (make-build-eqv? src sexpr) 4))
(define do-memq
(lambda (src sexpr e-key e-ls)
(or (memq->eq?s e-key e-ls)
(let ([t-ls (make-assigned-tmp 't-ls)] [Ltop (make-local-label 'Ltop)])
(bind #t (e-key)
`(let ([,t-ls ,e-ls])
(label ,Ltop
(if ,(%inline eq? ,t-ls ,(%constant snil))
,(%constant sfalse)
(if ,(%inline eq? ,(%mref ,t-ls ,(constant pair-car-disp)) ,e-key)
,t-ls
(seq
(set! ,t-ls ,(%mref ,t-ls ,(constant pair-cdr-disp)))
(goto ,Ltop)))))))))))
(define do-memv
(lambda (src sexpr e-key e-ls)
(or ((memv->eqv?s src sexpr) e-key e-ls)
(build-libcall #f src sexpr memv e-key e-ls))))
(define-inline 3 memq
[(e-key e-ls) (do-memq src sexpr e-key e-ls)])
(define-inline 3 memv
[(e-key e-ls)
(if (or (eqok? e-key) (memqok? e-ls))
(do-memq src sexpr e-key e-ls)
(do-memv src sexpr e-key e-ls))])
(define-inline 3 member
[(e-key e-ls)
(if (or (eqok? e-key) (memqok? e-ls))
(do-memq src sexpr e-key e-ls)
(and (or (eqvok? e-key) (memvok? e-ls))
(do-memv src sexpr e-key e-ls)))])
(define-inline 2 memq
[(e-key e-ls) (memq->eq?s e-key e-ls)])
(define-inline 2 memv
[(e-key e-ls) (or (and (memqok? e-ls) (memq->eq?s e-key e-ls))
((memv->eqv?s src sexpr) e-key e-ls))])
(define-inline 2 member
[(e-key e-ls) (or (and (memqok? e-ls) (memq->eq?s e-key e-ls))
(and (memvok? e-ls) ((memv->eqv?s src sexpr) e-key e-ls)))])))
; NB: for all of the I/O routines, consider putting optimize-level 2 code out-of-line
; w/o going all the way to the port handler, i.e., always defer to library routine but
; have library routine do the checks and run the optimize-level 3 version...this could
; save a lot of code
; NB: verify that the inline checks don't always fail, i.e., don't always send us to the
; library routine
(let ()
(define (go src sexpr e-p check? update? do-libcall)
(let ([Llib (and check? (make-local-label 'Llib))])
(define maybe-add-port-check
(lambda (e-p body)
(if Llib
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-binary-input-port type-binary-input-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define maybe-add-update
(lambda (t0 e-icount body)
(if update?
`(seq
(set! ,e-icount ,(%inline + ,t0 (immediate 1)))
,body)
body)))
(bind #t (e-p)
(let ([e-icount (%mref ,e-p ,(constant port-icount-disp))])
(maybe-add-port-check e-p
(bind #t ([t0 e-icount])
`(if ,(%inline eq? ,t0 (immediate 0))
,(maybe-add-label Llib (do-libcall src sexpr e-p))
,(maybe-add-update t0 e-icount
; TODO: this doesn't completely fall away when used in effect context
(build-fix
`(inline ,(make-info-load 'unsigned-8 #f) ,%load
,t0
,(%mref ,e-p ,(constant port-ilast-disp))
(immediate 0)))))))))))
(define (unsafe-lookahead-u8-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-lookahead-u8 e-p))
(define (safe-lookahead-u8-libcall src sexpr e-p) (build-libcall #t src sexpr safe-lookahead-u8 e-p))
(define (unsafe-get-u8-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-get-u8 e-p))
(define (safe-get-u8-libcall src sexpr e-p) (build-libcall #t src sexpr safe-get-u8 e-p))
(define-inline 3 lookahead-u8
[(e-p) (go src sexpr e-p #f #f unsafe-lookahead-u8-libcall)])
(define-inline 2 lookahead-u8
[(e-p) (go src sexpr e-p #t #f safe-lookahead-u8-libcall)])
(define-inline 3 get-u8
[(e-p) (go src sexpr e-p #f #t unsafe-get-u8-libcall)])
(define-inline 2 get-u8
[(e-p) (go src sexpr e-p #t #t safe-get-u8-libcall)]))
(let ()
(define (go src sexpr e-p check? update? do-libcall)
(let ([Llib (and check? (make-local-label 'Llib))])
(define maybe-add-port-check
(lambda (e-p body)
(if Llib
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-textual-input-port type-textual-input-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define maybe-add-update
(lambda (t0 e-icount body)
(if update?
`(seq
(set! ,e-icount ,(%inline + ,t0 ,(%constant string-char-bytes)))
,body)
body)))
(bind #t (e-p)
(let ([e-icount (%mref ,e-p ,(constant port-icount-disp))])
(maybe-add-port-check e-p
(bind #t ([t0 e-icount])
`(if ,(%inline eq? ,t0 (immediate 0))
,(maybe-add-label Llib (do-libcall src sexpr e-p))
,(maybe-add-update t0 e-icount
; TODO: this doesn't completely fall away when used in effect context
`(inline ,(make-info-load (string-char-type) #f) ,%load
,t0
,(%mref ,e-p ,(constant port-ilast-disp))
(immediate 0))))))))))
(define (unsafe-lookahead-char-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-lookahead-char e-p))
(define (safe-lookahead-char-libcall src sexpr e-p) (build-libcall #t src sexpr safe-lookahead-char e-p))
(define (unsafe-peek-char-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-peek-char e-p))
(define (safe-peek-char-libcall src sexpr e-p) (build-libcall #t src sexpr safe-peek-char e-p))
(define (unsafe-get-char-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-get-char e-p))
(define (safe-get-char-libcall src sexpr e-p) (build-libcall #t src sexpr safe-get-char e-p))
(define (unsafe-read-char-libcall src sexpr e-p) (build-libcall #t src sexpr unsafe-read-char e-p))
(define (safe-read-char-libcall src sexpr e-p) (build-libcall #t src sexpr safe-read-char e-p))
(define-inline 3 lookahead-char
[(e-p) (go src sexpr e-p #f #f unsafe-lookahead-char-libcall)])
(define-inline 2 lookahead-char
[(e-p) (go src sexpr e-p #t #f safe-lookahead-char-libcall)])
(define-inline 3 peek-char
[() (go src sexpr (%tc-ref current-input) #f #f unsafe-peek-char-libcall)]
[(e-p) (go src sexpr e-p #f #f unsafe-peek-char-libcall)])
(define-inline 2 peek-char
[() (go src sexpr (%tc-ref current-input) #f #f unsafe-peek-char-libcall)]
[(e-p) (go src sexpr e-p #t #f safe-peek-char-libcall)])
(define-inline 3 get-char
[(e-p) (go src sexpr e-p #f #t unsafe-get-char-libcall)])
(define-inline 2 get-char
[(e-p) (go src sexpr e-p #t #t safe-get-char-libcall)])
(define-inline 3 read-char
[() (go src sexpr (%tc-ref current-input) #f #t unsafe-read-char-libcall)]
[(e-p) (go src sexpr e-p #f #t unsafe-read-char-libcall)])
(define-inline 2 read-char
[() (go src sexpr (%tc-ref current-input) #f #t unsafe-read-char-libcall)]
[(e-p) (go src sexpr e-p #t #t safe-read-char-libcall)]))
(let ()
(define (go src sexpr e-p e-c check-port? check-char? do-libcall)
(let ([const-char? (constant? char? e-c)])
(let ([Llib (and (or check-char? check-port? (not const-char?)) (make-local-label 'Llib))])
(define maybe-add-port-check
(lambda (e-p body)
(if check-port?
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-textual-input-port type-textual-input-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define maybe-add-eof-check
(lambda (e-c body)
(if const-char?
body
`(if ,(%inline eq? ,e-c ,(%constant seof))
(goto ,Llib)
,body))))
(define maybe-add-char-check
(lambda (e-c body)
(if check-char?
`(if ,(%type-check mask-char type-char ,e-c)
,body
(goto ,Llib))
body)))
(bind #t (e-c e-p)
(let ([e-icount (%mref ,e-p ,(constant port-icount-disp))])
(maybe-add-port-check e-p
(maybe-add-eof-check e-c
(maybe-add-char-check e-c
(bind #t ([t0 e-icount])
`(if ,(%inline eq? ,t0
,(%inline -
,(%inline +
,(%mref ,e-p ,(constant port-ibuffer-disp))
,(%constant string-data-disp))
,(%mref ,e-p ,(constant port-ilast-disp))))
,(maybe-add-label Llib (do-libcall src sexpr e-p e-c))
(set! ,e-icount ,(%inline - ,t0 ,(%constant string-char-bytes)))))))))))))
(define (unsafe-unget-char-libcall src sexpr e-p e-c) (build-libcall #t src sexpr unsafe-unget-char e-p e-c))
(define (safe-unget-char-libcall src sexpr e-p e-c) (build-libcall #t src sexpr safe-unget-char e-p e-c))
(define (unsafe-unread-char-libcall src sexpr e-p e-c) (build-libcall #t src sexpr unsafe-unread-char e-c e-p))
(define (safe-unread-char-libcall src sexpr e-p e-c) (build-libcall #t src sexpr safe-unread-char e-c e-p))
(define-inline 3 unget-char
[(e-p e-c) (go src sexpr e-p e-c #f #f unsafe-unget-char-libcall)])
(define-inline 2 unget-char
[(e-p e-c) (go src sexpr e-p e-c #t (not (constant? char? e-c)) safe-unget-char-libcall)])
(define-inline 3 unread-char
[(e-c) (go src sexpr (%tc-ref current-input) e-c #f #f unsafe-unread-char-libcall)]
[(e-c e-p) (go src sexpr e-p e-c #f #f unsafe-unread-char-libcall)])
(define-inline 2 unread-char
[(e-c) (if (constant? char? e-c)
(go src sexpr (%tc-ref current-input) e-c #f #f unsafe-unread-char-libcall)
(go src sexpr (%tc-ref current-input) e-c #f #t safe-unread-char-libcall))]
[(e-c e-p) (go src sexpr e-p e-c #t (not (constant? char? e-c)) safe-unread-char-libcall)]))
(let ()
(define octet?
(lambda (x)
(and (fixnum? x) (fx<= 0 x 255))))
(define maybe-add-octet-check
(lambda (check-octet? Llib e-o body)
(if check-octet?
`(if ,(%type-check mask-octet type-octet ,e-o)
,body
(goto ,Llib))
body)))
(let ()
(define (go src sexpr e-p e-o check-port? check-octet? do-libcall)
(let ([const-octet? (constant? octet? e-o)])
(let ([Llib (and (or check-octet? check-port? (not const-octet?)) (make-local-label 'Llib))])
(define maybe-add-port-check
(lambda (e-p body)
(if check-port?
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-binary-input-port type-binary-input-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define maybe-add-eof-check
(lambda (e-o body)
(if const-octet?
body
`(if ,(%inline eq? ,e-o ,(%constant seof))
(goto ,Llib)
,body))))
(bind #t (e-o e-p)
(let ([e-icount (%mref ,e-p ,(constant port-icount-disp))])
(maybe-add-port-check e-p
(maybe-add-eof-check e-o
(maybe-add-octet-check check-octet? Llib e-o
(bind #t ([t0 e-icount])
`(if ,(%inline eq? ,t0
,(%inline -
,(%inline +
,(%mref ,e-p ,(constant port-ibuffer-disp))
,(%constant bytevector-data-disp))
,(%mref ,e-p ,(constant port-ilast-disp))))
,(maybe-add-label Llib (do-libcall src sexpr e-p e-o))
(set! ,e-icount ,(%inline - ,t0 (immediate 1)))))))))))))
(define (unsafe-unget-u8-libcall src sexpr e-p e-o) (build-libcall #t src sexpr unsafe-unget-u8 e-p e-o))
(define (safe-unget-u8-libcall src sexpr e-p e-o) (build-libcall #t src sexpr safe-unget-u8 e-p e-o))
(define-inline 3 unget-u8
[(e-p e-o) (go src sexpr e-p e-o #f #f unsafe-unget-u8-libcall)])
(define-inline 2 unget-u8
[(e-p e-o) (go src sexpr e-p e-o #t (not (constant? octet? e-o)) safe-unget-u8-libcall)]))
(let ()
(define (go src sexpr e-p e-o check-port? check-octet? do-libcall)
(let ([Llib (and (or check-octet? check-port?) (make-local-label 'Llib))])
(define maybe-add-port-check
(lambda (e-p body)
(if check-port?
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-binary-output-port type-binary-output-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define add-update
(lambda (t0 e-ocount body)
`(seq
(set! ,e-ocount ,(%inline + ,t0 (immediate 1)))
,body)))
(bind check-octet? (e-o)
(bind #t (e-p)
(let ([e-ocount (%mref ,e-p ,(constant port-ocount-disp))])
(maybe-add-octet-check check-octet? Llib e-o
(maybe-add-port-check e-p
(bind #t ([t0 e-ocount])
`(if ,(%inline eq? ,t0 (immediate 0))
,(maybe-add-label Llib (do-libcall src sexpr e-o e-p))
,(add-update t0 e-ocount
`(inline ,(make-info-load 'unsigned-8 #f) ,%store
,t0
,(%mref ,e-p ,(constant port-olast-disp))
(immediate 0)
,(build-unfix e-o))))))))))))
(define (unsafe-put-u8-libcall src sexpr e-o e-p) (build-libcall #t src sexpr unsafe-put-u8 e-p e-o))
(define (safe-put-u8-libcall src sexpr e-o e-p) (build-libcall #t src sexpr safe-put-u8 e-p e-o))
(define-inline 3 put-u8
[(e-p e-o) (go src sexpr e-p e-o #f #f unsafe-put-u8-libcall)])
(define-inline 2 put-u8
[(e-p e-o) (go src sexpr e-p e-o #t (not (constant? octet? e-o)) safe-put-u8-libcall)])))
(let ()
(define (go src sexpr e-p e-c check-port? check-char? do-libcall)
(let ([Llib (and (or check-char? check-port?) (make-local-label 'Llib))])
(define maybe-add-char-check
(lambda (e-c body)
(if check-char?
`(if ,(%type-check mask-char type-char ,e-c)
,body
(goto ,Llib))
body)))
(define maybe-add-port-check
(lambda (e-p body)
(if check-port?
`(if (if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(%type-check mask-textual-output-port type-textual-output-port
,(%mref ,e-p ,(constant typed-object-type-disp)))
,(%constant sfalse))
,body
(goto ,Llib))
body)))
(define add-update
(lambda (t0 e-ocount body)
`(seq
(set! ,e-ocount ,(%inline + ,t0 ,(%constant string-char-bytes)))
,body)))
(bind check-char? (e-c)
(bind #t (e-p)
(let ([e-ocount (%mref ,e-p ,(constant port-ocount-disp))])
(maybe-add-char-check e-c
(maybe-add-port-check e-p
(bind #t ([t0 e-ocount])
`(if ,(%inline eq? ,t0 (immediate 0))
,(maybe-add-label Llib (do-libcall src sexpr e-c e-p))
,(add-update t0 e-ocount
`(inline ,(make-info-load (string-char-type) #f) ,%store
,t0
,(%mref ,e-p ,(constant port-olast-disp))
(immediate 0)
,e-c)))))))))))
(define (unsafe-put-char-libcall src sexpr e-c e-p) (build-libcall #t src sexpr unsafe-put-char e-p e-c))
(define (safe-put-char-libcall src sexpr e-c e-p) (build-libcall #t src sexpr safe-put-char e-p e-c))
(define (unsafe-write-char-libcall src sexpr e-c e-p) (build-libcall #t src sexpr unsafe-write-char e-c e-p))
(define (safe-write-char-libcall src sexpr e-c e-p) (build-libcall #t src sexpr safe-write-char e-c e-p))
(define (unsafe-newline-libcall src sexpr e-c e-p) (build-libcall #t src sexpr unsafe-newline e-p))
(define (safe-newline-libcall src sexpr e-c e-p) (build-libcall #t src sexpr safe-newline e-p))
(define-inline 3 put-char
[(e-p e-c) (go src sexpr e-p e-c #f #f unsafe-put-char-libcall)])
(define-inline 2 put-char
[(e-p e-c) (go src sexpr e-p e-c #t (not (constant? char? e-c)) safe-put-char-libcall)])
(define-inline 3 write-char
[(e-c) (go src sexpr (%tc-ref current-output) e-c #f #f unsafe-write-char-libcall)]
[(e-c e-p) (go src sexpr e-p e-c #f #f unsafe-write-char-libcall)])
(define-inline 2 write-char
[(e-c) (if (constant? char? e-c)
(go src sexpr (%tc-ref current-output) e-c #f #f unsafe-write-char-libcall)
(go src sexpr (%tc-ref current-output) e-c #f #t safe-write-char-libcall))]
[(e-c e-p) (go src sexpr e-p e-c #t (not (constant? char? e-c)) safe-write-char-libcall)])
(define-inline 3 newline
[() (go src sexpr (%tc-ref current-output) `(quote #\newline) #f #f unsafe-newline-libcall)]
[(e-p) (go src sexpr e-p `(quote #\newline) #f #f unsafe-newline-libcall)])
(define-inline 2 newline
[() (go src sexpr (%tc-ref current-output) `(quote #\newline) #f #f unsafe-newline-libcall)]
[(e-p) (go src sexpr e-p `(quote #\newline) #t #f safe-newline-libcall)]))
(let ()
(define build-fxop?
(lambda (op overflow-flag e1 e2 adjust k)
(let ([Lfail (make-local-label 'Lfail)])
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(bind #f ([t `(inline ,null-info ,op ,e1 ,(adjust e2))])
`(if (inline ,(make-info-condition-code overflow-flag #f #t) ,%condition-code)
(label ,Lfail ,(k e1 e2))
,t))
(goto ,Lfail))))))
(define-inline 2 +
[() `(immediate ,(fix 0))]
[(e) (build-fxop? %+/ovfl 'overflow e `(quote 0) values (lambda (e1 e2) (build-libcall #t src sexpr + e1 e2)))]
[(e1 e2) (build-fxop? %+/ovfl 'overflow e1 e2 values (lambda (e1 e2) (build-libcall #t src sexpr + e1 e2)))]
; TODO: handle 3-operand case ala fx+, w/3-operand library +
[(e1 . e*) #f])
(define-inline 2 *
[() `(immediate ,(fix 1))]
[(e) (build-fxop? %*/ovfl 'multiply-overflow e `(quote 1) build-unfix (lambda (e1 e2) (build-libcall #t src sexpr * e1 e2)))]
; TODO: swap e1 & e2 if e1 is constant
[(e1 e2) (build-fxop? %*/ovfl 'multiply-overflow e1 e2 build-unfix (lambda (e1 e2) (build-libcall #t src sexpr * e1 e2)))]
; TODO: handle 3-operand case ala fx+, w/3-operand library *
[(e1 . e*) #f])
(define-inline 2 -
[(e) (build-fxop? %-/ovfl 'overflow `(quote 0) e values (lambda (e1 e2) (build-libcall #t src sexpr - e1 e2)))]
[(e1 e2) (build-fxop? %-/ovfl 'overflow e1 e2 values (lambda (e1 e2) (build-libcall #t src sexpr - e1 e2)))]
; TODO: handle 3-operand case ala fx+, w/3-operand library -
[(e1 e2 . e*) #f]))
(let ()
(define build-fxop?
(lambda (op e k)
(let ([Lfail (make-local-label 'Lfail)])
(bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(bind #f ([t `(inline ,null-info ,op ,e (immediate ,(fix 1)))])
`(if (inline ,(make-info-condition-code 'overflow #f #t) ,%condition-code)
(label ,Lfail ,(k e))
,t))
(goto ,Lfail))))))
(define-syntax define-inline-1op
(syntax-rules ()
[(_ op name)
(define-inline 2 name
[(e) (build-fxop? op e (lambda (e) (build-libcall #t src sexpr name e)))])]))
(define-inline-1op %-/ovfl 1-)
(define-inline-1op %-/ovfl -1+)
(define-inline-1op %-/ovfl sub1)
(define-inline-1op %+/ovfl 1+)
(define-inline-1op %+/ovfl add1))
(define-inline 2 /
[(e) (build-libcall #f src sexpr / `(immediate ,(fix 1)) e)]
[(e1 e2) (build-libcall #f src sexpr / e1 e2)]
[(e1 . e*) #f])
(let ()
(define (zgo src sexpr e e1 e2)
(build-simple-or
(%inline eq? ,e (immediate 0))
`(if ,(build-fixnums? (list e))
,(%constant sfalse)
,(build-libcall #t src sexpr = e1 e2))))
(define (go src sexpr e1 e2)
(or (eqvop-null-fptr e1 e2)
(relop-length RELOP= e1 e2)
(cond
[(constant? (lambda (x) (eqv? x 0)) e1)
(bind #t (e2) (zgo src sexpr e2 e1 e2))]
[(constant? (lambda (x) (eqv? x 0)) e2)
(bind #t (e1) (zgo src sexpr e1 e1 e2))]
[else (bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline eq? ,e1 ,e2)
,(build-libcall #t src sexpr = e1 e2)))])))
(define-inline 2 =
[(e1 e2) (go src sexpr e1 e2)]
[(e1 . e*) #f])
(define-inline 2 r6rs:=
[(e1 e2) (go src sexpr e1 e2)]
[(e1 e2 . e*) #f]))
(let ()
(define-syntax define-relop-inline
(syntax-rules ()
[(_ name r6rs:name relop op)
(let ()
(define builder
(lambda (e1 e2 libcall)
(or (relop-length relop e1 e2)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline op ,e1 ,e2)
,(libcall e1 e2))))))
(define-inline 2 name
[(e1 e2)
(builder e1 e2
(lambda (e1 e2) (build-libcall #t src sexpr name e1 e2)))]
; TODO: handle 3-operand case w/3-operand library routine
[(e1 . e*) #f])
(define-inline 2 r6rs:name
[(e1 e2)
(builder e1 e2
(lambda (e1 e2) (build-libcall #t src sexpr name e1 e2)))]
; TODO: handle 3-operand case w/3-operand library routine
[(e1 e2 . e*) #f]))]))
(define-relop-inline < r6rs:< RELOP< <)
(define-relop-inline <= r6rs:<= RELOP<= <=)
(define-relop-inline >= r6rs:>= RELOP>= >=)
(define-relop-inline > r6rs:> RELOP> >))
(define-inline 3 positive? ; 3 so opt-level 2 errors come from positive?
[(e) (handle-prim src sexpr 3 '> (list e `(quote 0)))])
(define-inline 3 nonnegative? ; 3 so opt-level 2 errors come from nonnegative?
[(e) (handle-prim src sexpr 3 '>= (list e `(quote 0)))])
(define-inline 3 negative? ; 3 so opt-level 2 errors come from negative?
[(e) (handle-prim src sexpr 3 '< (list e `(quote 0)))])
(define-inline 3 nonpositive? ; 3 so opt-level 2 errors come from nonpositive?
[(e) (handle-prim src sexpr 3 '<= (list e `(quote 0)))])
(define-inline 2 zero?
[(e)
(or (relop-length RELOP= e)
(nanopass-case (L7 Expr) e
[(call ,info ,mdcl ,pr ,e)
(guard
(eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(make-ftype-pointer-null? e)]
[else
(bind #t (e)
(build-simple-or
(%inline eq? ,e (immediate ,(fix 0)))
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(%constant sfalse)
,(build-libcall #t src sexpr zero? e))))]))])
(define-inline 2 positive? [(e) (relop-length RELOP> e)])
(define-inline 2 nonnegative? [(e) (relop-length RELOP>= e)])
(define-inline 2 negative? [(e) (relop-length RELOP< e)])
(define-inline 2 nonpositive? [(e) (relop-length RELOP<= e)])
(let ()
(define-syntax define-logorop-inline
(syntax-rules ()
[(_ name ...)
(let ()
(define build-logop
(lambda (src sexpr e1 e2 libcall)
(bind #t (e1 e2)
(bind #t ([t (%inline logor ,e1 ,e2)])
`(if ,(%type-check mask-fixnum type-fixnum ,t)
,t
,(libcall src sexpr e1 e2))))))
(let ()
(define libcall (lambda (src sexpr e1 e2) (build-libcall #t src sexpr name e1 e2)))
(define-inline 2 name
[() `(immediate ,(fix 0))]
[(e) (build-logop src sexpr e `(immediate ,(fix 0)) libcall)]
[(e1 e2) (build-logop src sexpr e1 e2 libcall)]
[(e1 . e*) #f]))
...)]))
(define-logorop-inline logor logior bitwise-ior))
(let ()
(define-syntax define-logop-inline
(syntax-rules ()
[(_ op unit name ...)
(let ()
(define build-logop
(lambda (src sexpr e1 e2 libcall)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline op ,e1 ,e2)
,(libcall src sexpr e1 e2)))))
(let ()
(define libcall (lambda (src sexpr e1 e2) (build-libcall #t src sexpr name e1 e2)))
(define-inline 2 name
[() `(immediate ,(fix unit))]
[(e) (build-logop src sexpr e `(immediate ,(fix unit)) libcall)]
[(e1 e2) (build-logop src sexpr e1 e2 libcall)]
[(e1 . e*) #f]))
...)]))
(define-logop-inline logand -1 logand bitwise-and)
(define-logop-inline logxor 0 logxor bitwise-xor))
(let ()
(define build-lognot
(lambda (e libcall)
(bind #t (e)
`(if ,(%type-check mask-fixnum type-fixnum ,e)
,(%inline logxor ,e (immediate ,(fxlognot (constant mask-fixnum))))
,(libcall e)))))
(define-inline 2 lognot
[(e) (build-lognot e (lambda (e) (build-libcall #t src sexpr lognot e)))])
(define-inline 2 bitwise-not
[(e) (build-lognot e (lambda (e) (build-libcall #t src sexpr bitwise-not e)))]))
(let ()
(define build-logbit?
(lambda (e1 e2 libcall)
(or (nanopass-case (L7 Expr) e1
[(quote ,d)
(or (and (and (fixnum? d) (fx<= 0 d (fx- (constant fixnum-bits) 2)))
(bind #t (e2)
`(if ,(%type-check mask-fixnum type-fixnum ,e2)
,(%inline logtest ,e2 (immediate ,(fix (ash 1 d))))
,(libcall e1 e2))))
(and (and (target-fixnum? d) (> d (fx- (constant fixnum-bits) 2)))
(bind #t (e2)
`(if ,(%type-check mask-fixnum type-fixnum ,e2)
,(%inline < ,e2 (immediate ,(fix 0)))
,(libcall e1 e2)))))]
[else #f])
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e1 (immediate ,(fix (constant fixnum-bits)))))
,(%inline logtest
,(%inline sra ,e2 ,(build-unfix e1))
(immediate ,(fix 1)))
,(libcall e1 e2))))))
(define-inline 2 logbit?
[(e1 e2) (build-logbit? e1 e2 (lambda (e1 e2) (build-libcall #t src sexpr logbit? e1 e2)))])
(define-inline 2 bitwise-bit-set?
[(e1 e2) (build-logbit? e2 e1 (lambda (e2 e1) (build-libcall #t src sexpr bitwise-bit-set? e1 e2)))]))
(define-inline 2 logbit1
[(e1 e2) (or (nanopass-case (L7 Expr) e1
[(quote ,d)
(and (and (fixnum? d) (fx<= 0 d (fx- (constant fixnum-bits) 2)))
(bind #t (e2)
`(if ,(%type-check mask-fixnum type-fixnum ,e2)
,(%inline logor ,e2 (immediate ,(fix (ash 1 d))))
,(build-libcall #t src sexpr logbit1 e1 e2))))]
[else #f])
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e1 (immediate ,(fix (fx- (constant fixnum-bits) 1)))))
,(%inline logor ,e2
,(%inline sll (immediate ,(fix 1)) ,(build-unfix e1)))
,(build-libcall #t src sexpr logbit1 e1 e2))))])
(define-inline 2 logbit0
[(e1 e2) (or (nanopass-case (L7 Expr) e1
[(quote ,d)
(and (and (fixnum? d) (fx<= 0 d (fx- (constant fixnum-bits) 2)))
(bind #t (e2)
`(if ,(%type-check mask-fixnum type-fixnum ,e2)
,(%inline logand ,e2 (immediate ,(fix (lognot (ash 1 d)))))
,(build-libcall #t src sexpr logbit0 e1 e2))))]
[else #f])
(bind #t (e1 e2)
`(if ,(build-and
(build-fixnums? (list e1 e2))
(%inline u< ,e1 (immediate ,(fix (fx- (constant fixnum-bits) 1)))))
,(%inline logand ,e2
,(%inline lognot
,(%inline sll (immediate ,(fix 1)) ,(build-unfix e1))))
,(build-libcall #t src sexpr logbit0 e1 e2))))])
(define-inline 2 logtest
[(e1 e2) (bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
,(%inline logtest ,e1 ,e2)
,(build-libcall #t src sexpr logtest e1 e2)))])
(define-inline 3 $flhash
[(e) (bind #t (e)
(%inline logand
,(%inline srl
,(constant-case ptr-bits
[(32) (%inline +
,(%mref ,e ,(constant flonum-data-disp))
,(%mref ,e ,(fx+ (constant flonum-data-disp) 4)))]
[(64) (%mref ,e ,(constant flonum-data-disp))])
(immediate 1))
(immediate ,(- (constant fixnum-factor)))))])
(let ()
(define build-flonum-extractor
(lambda (pos size e1)
(let ([cnt (- pos (constant fixnum-offset))]
[mask (* (- (expt 2 size) 1) (expt 2 (constant fixnum-offset)))])
(%inline logand
,(let ([body `(inline ,(make-info-load 'integer-32 #f) ,%load ,e1 ,%zero
(immediate ,(constant-case native-endianness
[(little) (fx+ (constant flonum-data-disp) 4)]
[(big) (constant flonum-data-disp)])))])
(let ([body (if (fx> cnt 0)
(%inline srl ,body (immediate ,cnt))
body)])
(if (fx< cnt 0)
(%inline sll ,body (immediate ,(fx- 0 cnt)))
body)))
(immediate ,mask)))))
(define-inline 3 fllp
[(e) (build-flonum-extractor 19 12 e)])
(define-inline 3 $flonum-sign
[(e) (build-flonum-extractor 31 1 e)])
(define-inline 3 $flonum-exponent
[(e) (build-flonum-extractor 20 11 e)]))
(define-inline 3 $fleqv?
[(e1 e2)
(constant-case ptr-bits
[(32) (build-and
(%inline eq?
,(%mref ,e1 ,(constant flonum-data-disp))
,(%mref ,e2 ,(constant flonum-data-disp)))
(%inline eq?
,(%mref ,e1 ,(fx+ (constant flonum-data-disp) 4))
,(%mref ,e2 ,(fx+ (constant flonum-data-disp) 4))))]
[(64) (%inline eq?
,(%mref ,e1 ,(constant flonum-data-disp))
,(%mref ,e2 ,(constant flonum-data-disp)))]
[else ($oops 'compiler-internal
"$fleqv doesn't handle ptr-bits = ~s"
(constant ptr-bits))])])
(let ()
(define build-flop-1
; NB: e must be bound
(lambda (op e)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
`(seq (inline ,null-info ,op ,e ,t) ,t))))
(define build-flop-2
; NB: e1 and e2 must be bound
(lambda (op e1 e2)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
`(seq (inline ,null-info ,op ,e1 ,e2 ,t) ,t))))
(define build-flabs
(lambda (e)
(bind (constant-case ptr-bits [(32) #t] [(64) #f]) (e)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
,(constant-case ptr-bits
[(64)
`(set! ,(%mref ,t ,(constant flonum-data-disp))
,(%inline logand
,(%mref ,e ,(constant flonum-data-disp))
,(%inline srl (immediate -1) (immediate 1))))]
[(32)
(let ()
(constant-case native-endianness
[(big)
(begin
(define disp-high (constant flonum-data-disp))
(define disp-low (fx+ (constant flonum-data-disp) 4)))]
[(little)
(begin
(define disp-low (constant flonum-data-disp))
(define disp-high (fx+ (constant flonum-data-disp) 4)))])
(%seq
(set! ,(%mref ,t ,disp-high)
,(%inline logand
,(%mref ,e ,disp-high)
,(%inline srl (immediate -1) (immediate 1))))
(set! ,(%mref ,t ,disp-low)
,(%mref ,e ,disp-low))))])
,t)))))
(define build-flneg
(lambda (e)
(bind (constant-case ptr-bits [(32) #t] [(64) #f]) (e)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
,(constant-case ptr-bits
[(64)
`(set! ,(%mref ,t ,(constant flonum-data-disp))
,(%inline logxor
,(%mref ,e ,(constant flonum-data-disp))
,(%inline sll (immediate 1) (immediate 63))))]
[(32)
(let ()
(constant-case native-endianness
[(big)
(begin
(define disp-high (constant flonum-data-disp))
(define disp-low (fx+ (constant flonum-data-disp) 4)))]
[(little)
(begin
(define disp-low (constant flonum-data-disp))
(define disp-high (fx+ (constant flonum-data-disp) 4)))])
(%seq
(set! ,(%mref ,t ,disp-high)
,(%inline logxor
,(%mref ,e ,disp-high)
,(%inline sll (immediate 1) (immediate 31))))
(set! ,(%mref ,t ,disp-low)
,(%mref ,e ,disp-low))))])
,t)))))
;; TODO: Rather then reducing here, (which will allocate a new flonum for each interim result)
;; we could allocate a single flonum and reuse it until the final result is calculated.
;; Better yet, we could do this across nested fl operations, so that only one flonum is
;; allocated across nested fl+, fl*, fl-, fl/ etc. operation
(define-inline 3 fl+
[() `(quote 0.0)]
[(e) e]
[(e1 e2) (bind #f (e1 e2) (build-flop-2 %fl+ e1 e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fl*
[() `(quote 1.0)]
[(e) e]
[(e1 e2) (bind #f (e1 e2) (build-flop-2 %fl* e1 e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fl-
[(e) (build-flneg e)]
[(e1 e2) (bind #f (e1 e2) (build-flop-2 %fl- e1 e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 fl/
[(e) (bind #f (e) (build-flop-2 %fl/ `(quote 1.0) e))]
[(e1 e2) (bind #f (e1 e2) (build-flop-2 %fl/ e1 e2))]
[(e1 . e*) (reduce src sexpr moi e1 e*)])
(define-inline 3 flsqrt
[(e)
(constant-case architecture
[(x86 x86_64 arm32) (bind #f (e) (build-flop-1 %flsqrt e))]
[(ppc32) #f])])
(define-inline 3 flround
; NB: there is no support in SSE2 for flround, though this was added in SSE4.1
[(e) (build-libcall #f src sexpr flround e)])
(define-inline 3 flabs
[(e) (build-flabs e)])
(let ()
(define build-fl-make-rectangular
(lambda (e1 e2)
(bind #f (e1 e2)
(bind #t ([t (%constant-alloc type-typed-object (constant size-inexactnum))])
`(seq
(set! ,(%mref ,t ,(constant inexactnum-type-disp))
,(%constant type-inexactnum))
,(%seq
(inline ,(make-info-loadfl %flreg1) ,%load-double
,e1 ,%zero ,(%constant flonum-data-disp))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,t ,%zero ,(%constant inexactnum-real-disp))
(inline ,(make-info-loadfl %flreg1) ,%load-double
,e2 ,%zero ,(%constant flonum-data-disp))
(inline ,(make-info-loadfl %flreg1) ,%store-double
,t ,%zero ,(%constant inexactnum-imag-disp))
,t))))))
(define-inline 3 fl-make-rectangular
[(e1 e2) (build-fl-make-rectangular e1 e2)])
(define-inline 3 cfl-
[(e) (bind #t (e)
`(if ,(%type-check mask-flonum type-flonum ,e)
,(build-flneg e)
,(build-fl-make-rectangular
(build-flneg (build-$inexactnum-real-part e))
(build-flneg (build-$inexactnum-imag-part e)))))]
[(e1 e2) (build-libcall #f src sexpr cfl- e1 e2)]
; TODO: add 3 argument version of cfl- library function
#;[(e1 e2 e3) (build-libcall #f src sexpr cfl- e1 e2 e3)]
[(e1 e2 . e*) #f])
(define-inline 3 cfl+
[() `(quote 0.0)]
[(e) e]
[(e1 e2) (build-libcall #f src sexpr cfl+ e1 e2)]
; TODO: add 3 argument version of cfl+ library function
#;[(e1 e2 e3) (build-libcall #f src sexpr cfl+ e1 e2 e3)]
[(e1 e2 . e*) #f])
(define-inline 3 cfl*
[() `(quote 1.0)]
[(e) e]
[(e1 e2) (build-libcall #f src sexpr cfl* e1 e2)]
; TODO: add 3 argument version of cfl* library function
#;[(e1 e2 e3) (build-libcall #f src sexpr cfl* e1 e2 e3)]
[(e1 e2 . e*) #f])
(define-inline 3 cfl/
[(e) (build-libcall #f src sexpr cfl/ `(quote 1.0) e)]
[(e1 e2) (build-libcall #f src sexpr cfl/ e1 e2)]
; TODO: add 3 argument version of cfl/ library function
#;[(e1 e2 e3) (build-libcall #f src sexpr cfl/ e1 e2 e3)]
[(e1 e2 . e*) #f])
(define-inline 3 cfl-conjugate
[(e) (bind #t (e)
`(if ,(%type-check mask-flonum type-flonum ,e)
,e
,(build-fl-make-rectangular
(build-$inexactnum-real-part e)
(build-flneg (build-$inexactnum-imag-part e)))))]))
(define-inline 3 $make-exactnum
[(e1 e2) (bind #f (e1 e2)
(bind #t ([t (%constant-alloc type-typed-object (constant size-exactnum))])
(%seq
(set! ,(%mref ,t ,(constant exactnum-type-disp))
,(%constant type-exactnum))
(set! ,(%mref ,t ,(constant exactnum-real-disp)) ,e1)
(set! ,(%mref ,t ,(constant exactnum-imag-disp)) ,e2)
,t)))])
(let ()
(define (build-fl< e1 e2) (%inline fl< ,e1 ,e2))
(define (build-fl= e1 e2) (%inline fl= ,e1 ,e2))
(define (build-fl<= e1 e2) (%inline fl<= ,e1 ,e2))
(let ()
(define-syntax define-fl-cmp-inline
(lambda (x)
(syntax-case x ()
[(_ op r6rs:op builder inequality? swapped?)
(with-syntax ([(args ...) (if (datum swapped?) #'(e2 e1) #'(e1 e2))]
[reducer (if (datum inequality?)
#'reduce-inequality
#'reduce-equality)])
#'(begin
(define-inline 3 op
[(e) (bind #t (e) (build-fl= e e))]
[(e1 e2) (builder args ...)]
[(e1 e2 . e*) (reducer src sexpr moi e1 e2 e*)])
(define-inline 3 r6rs:op
[(e1 e2) (builder args ...)]
[(e1 e2 . e*) (reducer src sexpr moi e1 e2 e*)])))])))
(define-fl-cmp-inline fl= fl=? build-fl= #f #f)
(define-fl-cmp-inline fl< fl<? build-fl< #t #f)
(define-fl-cmp-inline fl> fl>? build-fl< #t #t)
(define-fl-cmp-inline fl<= fl<=? build-fl<= #t #f)
(define-fl-cmp-inline fl>= fl>=? build-fl<= #t #t))
(let ()
(define-syntax build-bind-and-check
(syntax-rules ()
[(_ src sexpr op e1 e2 body)
(bind #t (e1 e2)
`(if ,(build-and
(%type-check mask-flonum type-flonum ,e1)
(%type-check mask-flonum type-flonum ,e2))
,body
,(build-libcall #t src sexpr op e1 e2)))]))
(define-syntax define-fl-cmp-inline
(lambda (x)
(syntax-case x ()
[(_ op r6rs:op builder inequality? swapped?)
(with-syntax ([(args ...) (if (datum swapped?) #'(e2 e1) #'(e1 e2))])
#'(begin
(define-inline 2 op
[(e) #f]
[(e1 e2) (build-bind-and-check src sexpr op e1 e2 (builder args ...))]
[(e1 e2 . e*) #f])
(define-inline 2 r6rs:op
[(e1 e2) (build-bind-and-check src sexpr r6rs:op e1 e2 (builder args ...))]
[(e1 e2 . e*) #f])))])))
(define-fl-cmp-inline fl= fl=? build-fl= #f #f)
(define-fl-cmp-inline fl< fl<? build-fl< #t #f)
(define-fl-cmp-inline fl> fl>? build-fl< #t #t)
(define-fl-cmp-inline fl<= fl<=? build-fl<= #t #f)
(define-fl-cmp-inline fl>= fl>=? build-fl<= #t #t))
(let ()
(define build-cfl=
; NB: e1 and e2 must be bound
(lambda (e1 e2)
`(if ,(%type-check mask-flonum type-flonum ,e1)
(if ,(%type-check mask-flonum type-flonum ,e2)
,(build-fl= e1 e2)
,(build-and
(build-fl= `(quote 0.0) (build-$inexactnum-imag-part e2))
(build-fl= e1 (build-$inexactnum-real-part e2))))
(if ,(%type-check mask-flonum type-flonum ,e2)
,(build-and
(build-fl= `(quote 0.0) (build-$inexactnum-imag-part e1))
(build-fl= e2 (build-$inexactnum-real-part e1)))
,(build-and
(build-fl=
(build-$inexactnum-imag-part e1)
(build-$inexactnum-imag-part e2))
(build-fl=
(build-$inexactnum-real-part e1)
(build-$inexactnum-real-part e2)))))))
(define-inline 3 cfl=
[(e) (bind #f (e) (build-cfl= e e))] ; this is weird, why not just true?
[(e1 e2) (bind #f (e1 e2) (build-cfl= e1 e2))]
; TODO: should we avoid building for more then the 3 item case?
[(e1 e2 . e*) (reduce-equality src sexpr moi e1 e2 e*)])))
(let ()
(define build-flop-3
; NB: e1, e2, and e3 must be bound
(lambda (op e1 e2 e3)
(build-flop-2 op e1
(build-flop-2 op e2 e3))))
(define build-checked-flop
(case-lambda
[(e k)
(bind #t (e)
`(if ,(build-flonums? (list e))
,e
,(k e)))]
[(e1 e2 op k)
(bind #t (e1 e2)
`(if ,(build-flonums? (list e1 e2))
,(build-flop-2 op e1 e2)
,(k e1 e2)))]
[(e1 e2 e3 op k)
(bind #f (e1 e2 e3)
`(if ,(build-flonums? (list e1 e2 e3))
,(build-flop-3 op e1 e2 e3)
,(k e1 e2 e3)))]))
(define-inline 2 fl+
[() `(quote 0.0)]
[(e) (build-checked-flop e
(lambda (e)
(build-libcall #t src sexpr fl+ e `(quote 0.0))))]
[(e1 e2) (build-checked-flop e1 e2 %fl+
(lambda (e1 e2)
(build-libcall #t src sexpr fl+ e1 e2)))]
; TODO: add 3 argument fl+ library function
#;[(e1 e2 e3) (build-checked flop e1 e2 e3 %fl+
(lambda (e1 e2 e3)
(build-libcall #t src sexpr fl+ e1 e2 e3)))]
[(e1 . e*) #f])
(define-inline 2 fl*
[() `(quote 1.0)]
[(e) (build-checked-flop e
(lambda (e)
(build-libcall #t src sexpr fl* e `(quote 1.0))))]
[(e1 e2) (build-checked-flop e1 e2 %fl*
(lambda (e1 e2)
(build-libcall #t src sexpr fl* e1 e2)))]
; TODO: add 3 argument fl* library function
#;[(e1 e2 e3) (build-checked flop e1 e2 e3 %fl*
(lambda (e1 e2 e3)
(build-libcall #t src sexpr fl* e1 e2 e3)))]
[(e1 . e*) #f])
(define-inline 2 fl-
[(e)
(bind #t (e)
`(if ,(build-flonums? (list e))
,(build-flneg e)
,(build-libcall #t src sexpr flnegate e)))]
[(e1 e2) (build-checked-flop e1 e2 %fl-
(lambda (e1 e2)
(build-libcall #t src sexpr fl- e1 e2)))]
; TODO: add 3 argument fl- library function
#;[(e1 e2 e3) (build-checked flop e1 e2 e3 %fl-
(lambda (e1 e2 e3)
(build-libcall #t src sexpr fl- e1 e2 e3)))]
[(e1 . e*) #f])
(define-inline 2 fl/
[(e) (build-checked-flop `(quote 1.0) e %fl/
(lambda (e1 e2)
(build-libcall #t src sexpr fl/ e1 e2)))]
[(e1 e2) (build-checked-flop e1 e2 %fl/
(lambda (e1 e2)
(build-libcall #t src sexpr fl/ e1 e2)))]
; TODO: add 3 argument fl/ library function
#;[(e1 e2 e3) (build-checked flop e1 e2 e3 %fl/
(lambda (e1 e2 e3)
(build-libcall #t src sexpr fl/ e1 e2 e3)))]
[(e1 . e*) #f])))
; NB: assuming that we have a trunc instruction for now, will need to change to support Sparc
(define-inline 3 flonum->fixnum
[(e-x) (bind #f (e-x)
(build-fix
(%inline trunc ,e-x)))])
(let ()
(define build-fixnum->flonum
; NB: x must already be bound in order to ensure it is done before the flonum is allocated
(lambda (e-x)
(bind #t ([t (%constant-alloc type-flonum (constant size-flonum))])
(%seq
,(%inline flt ,(build-unfix e-x) ,t)
,t))))
(define-inline 3 fixnum->flonum
[(e-x) (bind #f (e-x) (build-fixnum->flonum e-x))])
(define-inline 2 real->flonum
[(e-x)
(if (constant? flonum? e-x)
e-x
(bind #t (e-x)
`(if ,(%type-check mask-fixnum type-fixnum ,e-x)
,(build-fixnum->flonum e-x)
(if ,(%type-check mask-flonum type-flonum ,e-x)
,e-x
,(build-libcall #t src sexpr real->flonum e-x `(quote real->flonum))))))]))
(define-inline 3 $real->flonum
[(x who) (build-$real->flonum src sexpr x who)])
(define-inline 2 $record
[(tag . args) (build-$record tag args)])
(define-inline 3 $object-address
[(e-ptr e-offset)
(unsigned->ptr
(%inline + ,e-ptr ,(build-unfix e-offset))
(type->width ptr-type))])
(define-inline 3 $address->object
[(e-addr e-roffset)
(bind #f (e-roffset)
(%inline -
,(ptr->integer e-addr (type->width ptr-type))
,(build-unfix e-roffset)))])
(define-inline 2 $object-ref
[(type base offset)
(nanopass-case (L7 Expr) type
[(quote ,d)
(let ([type (filter-foreign-type d)])
(and (memq type (record-datatype list))
(not (memq type '(char wchar boolean)))
(build-object-ref #f type base offset)))]
[else #f])])
(define-inline 2 $swap-object-ref
[(type base offset)
(nanopass-case (L7 Expr) type
[(quote ,d)
(let ([type (filter-foreign-type d)])
(and (memq type (record-datatype list))
(not (memq type '(char wchar boolean)))
(build-object-ref #t type base offset)))]
[else #f])])
(define-inline 3 foreign-ref
[(e-type e-addr e-offset)
(nanopass-case (L7 Expr) e-type
[(quote ,d)
(let ([type (filter-foreign-type d)])
(and (memq type (record-datatype list))
(not (memq type '(char wchar boolean)))
(bind #f (e-offset)
(build-object-ref #f type
(ptr->integer e-addr (constant ptr-bits))
e-offset))))]
[else #f])])
(define-inline 2 $object-set!
[(type base offset value)
(nanopass-case (L7 Expr) type
[(quote ,d)
(let ([type (filter-foreign-type d)])
(and (memq type (record-datatype list))
(not (memq type '(char wchar boolean)))
(or (>= (constant ptr-bits) (type->width type)) (eq? type 'double-float))
(build-object-set! type base offset value)))]
[else #f])])
(define-inline 3 foreign-set!
[(e-type e-addr e-offset e-value)
(nanopass-case (L7 Expr) e-type
[(quote ,d)
(let ([type (filter-foreign-type d)])
(and (memq type (record-datatype list))
(not (memq type '(char wchar boolean)))
(or (>= (constant ptr-bits) (type->width type)) (eq? type 'double-float))
(bind #f (e-offset e-value)
(build-object-set! type
(ptr->integer e-addr (constant ptr-bits))
e-offset
e-value))))]
[else #f])])
(define-inline 2 $make-fptr
[(e-ftype e-addr)
(nanopass-case (L7 Expr) e-addr
[(call ,info ,mdcl ,pr ,e1)
(guard
(eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(bind #f (e-ftype e1)
(bind #t ([t (%constant-alloc type-typed-object (fx* 2 (constant ptr-bytes)))])
(%seq
(set! ,(%mref ,t ,(constant record-type-disp)) ,e-ftype)
(set! ,(%mref ,t ,(constant record-data-disp))
,(%mref ,e1 ,(constant record-data-disp)))
,t)))]
[else
(bind #f (e-ftype e-addr)
(bind #t ([t (%constant-alloc type-typed-object (fx* 2 (constant ptr-bytes)))])
(%seq
(set! ,(%mref ,t ,(constant record-type-disp)) ,e-ftype)
(set! ,(%mref ,t ,(constant record-data-disp))
,(ptr->integer e-addr (constant ptr-bits)))
,t)))])])
(define-inline 3 ftype-pointer-address
[(e-fptr)
(build-object-ref #f
(constant-case ptr-bits
[(64) 'unsigned-64]
[(32) 'unsigned-32])
e-fptr %zero (constant record-data-disp))])
(define-inline 3 ftype-pointer-null?
[(e-fptr) (make-ftype-pointer-null? e-fptr)])
(define-inline 3 ftype-pointer=?
[(e1 e2) (make-ftype-pointer-equal? e1 e2)])
(let ()
(define build-fx+raw
(lambda (fx-arg raw-arg)
(if (constant? (lambda (x) (eqv? x 0)) fx-arg)
raw-arg
(%inline + ,raw-arg ,(build-unfix fx-arg)))))
(define $extract-fptr-address
(lambda (e-fptr)
(define suppress-unsafe-cast
(lambda (e-fptr)
(nanopass-case (L7 Expr) e-fptr
[(call ,info1 ,mdcl1 ,pr1 (quote ,d) (call ,info2 ,mdcl2 ,pr2 ,e))
(guard
(eq? (primref-name pr1) '$make-fptr)
(all-set? (prim-mask unsafe) (primref-flags pr2))
(eq? (primref-name pr2) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr2)))
e]
[else e-fptr])))
(nanopass-case (L7 Expr) e-fptr
; skip allocation and dereference of ftype-pointer for $fptr-fptr-ref
[(call ,info ,mdcl ,pr ,e1 ,e2 ,e3) ; e1, e2, e3 = fptr, offset, ftd
(guard
(eq? (primref-name pr) '$fptr-fptr-ref)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(let-values ([(e-index imm-offset) (offset-expr->index+offset e2)])
(bind #f (e-index e3)
`(inline ,(make-info-load ptr-type #f) ,%load
,($extract-fptr-address e1)
,e-index (immediate ,imm-offset))))]
; skip allocation and dereference of ftype-pointer for $fptr-&ref
[(call ,info ,mdcl ,pr ,e1 ,e2 ,e3) ; e1, e2, e3 = fptr, offset, ftd
(guard
(eq? (primref-name pr) '$fptr-&ref)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(build-fx+raw e2 ($extract-fptr-address e1))]
; skip allocation and dereference of ftype-pointer for $make-fptr
[(call ,info ,mdcl ,pr ,e1 ,e2) ; e1, e2 = ftd, (ptr) addr
(guard
(eq? (primref-name pr) '$make-fptr)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(nanopass-case (L7 Expr) e2
[(call ,info ,mdcl ,pr ,e3)
(guard
(eq? (primref-name pr) 'ftype-pointer-address)
(all-set? (prim-mask unsafe) (primref-flags pr)))
(bind #f (e1)
(%mref ,e3 ,(constant record-data-disp)))]
[else
(bind #f (e1)
(ptr->integer e2 (constant ptr-bits)))])]
[else
`(inline ,(make-info-load ptr-type #f) ,%load ,(suppress-unsafe-cast e-fptr) ,%zero
,(%constant record-data-disp))])))
(let ()
(define-inline 3 $fptr-offset-addr
[(e-fptr e-offset)
; bind offset before doing the load (a) to maintain applicative order---the
; load can cause an invalid memory reference---and (b) so that the raw value
; isn't live across any calls
(bind #f (e-offset)
(build-fx+raw e-offset
($extract-fptr-address e-fptr)))])
(define-inline 3 $fptr-&ref
[(e-fptr e-offset e-ftd)
; see comment in $fptr-offset-addr
(bind #f (e-offset e-ftd)
(build-$record e-ftd
(list (build-fx+raw e-offset ($extract-fptr-address e-fptr)))))]))
(define-inline 3 $fptr-fptr-ref
[(e-fptr e-offset e-ftd)
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #f (e-index)
(build-$record e-ftd
(list `(inline ,(make-info-load ptr-type #f) ,%load
,($extract-fptr-address e-fptr)
,e-index (immediate ,imm-offset))))))])
(define-inline 3 $fptr-fptr-set!
[(e-fptr e-offset e-val)
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #f ([e-addr ($extract-fptr-address e-fptr)] e-index e-val)
`(inline ,(make-info-load ptr-type #f) ,%store ,e-addr ,e-index (immediate ,imm-offset)
(inline ,(make-info-load ptr-type #f) ,%load ,e-val ,%zero
,(%constant record-data-disp)))))])
(let ()
(define $do-fptr-ref-inline
(lambda (swapped? type e-fptr e-offset)
(bind #f (e-offset)
(build-object-ref swapped? type ($extract-fptr-address e-fptr) e-offset))))
(define-syntax define-fptr-ref-inline
(lambda (x)
(define build-inline
(lambda (name type ref maybe-k)
#`(define-inline 3 #,name
[(e-fptr e-offset)
#,((lambda (body) (if maybe-k #`(#,maybe-k #,body) body))
#`($do-fptr-ref-inline #,ref #,type e-fptr e-offset))])))
(syntax-case x ()
[(_ name ?type ref) (build-inline #'name #'?type #'ref #f)]
[(_ name ?type ref ?k) (build-inline #'name #'?type #'ref #'?k)])))
(define-fptr-ref-inline $fptr-ref-integer-8 'integer-8 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-8 'unsigned-8 #f)
(define-fptr-ref-inline $fptr-ref-integer-16 'integer-16 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-16 'unsigned-16 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-16 'integer-16 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-16 'unsigned-16 #t)
(define-fptr-ref-inline $fptr-ref-integer-24 'integer-24 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-24 'unsigned-24 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-24 'integer-24 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-24 'unsigned-24 #t)
(define-fptr-ref-inline $fptr-ref-integer-32 'integer-32 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-32 'unsigned-32 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-32 'integer-32 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-32 'unsigned-32 #t)
(define-fptr-ref-inline $fptr-ref-integer-40 'integer-40 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-40 'unsigned-40 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-40 'integer-40 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-40 'unsigned-40 #t)
(define-fptr-ref-inline $fptr-ref-integer-48 'integer-48 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-48 'unsigned-48 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-48 'integer-48 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-48 'unsigned-48 #t)
(define-fptr-ref-inline $fptr-ref-integer-56 'integer-56 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-56 'unsigned-56 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-56 'integer-56 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-56 'unsigned-56 #t)
(define-fptr-ref-inline $fptr-ref-integer-64 'integer-64 #f)
(define-fptr-ref-inline $fptr-ref-unsigned-64 'unsigned-64 #f)
(define-fptr-ref-inline $fptr-ref-swap-integer-64 'integer-64 #t)
(define-fptr-ref-inline $fptr-ref-swap-unsigned-64 'unsigned-64 #t)
(define-fptr-ref-inline $fptr-ref-double-float 'double-float #f)
(define-fptr-ref-inline $fptr-ref-swap-double-float 'double-float #t)
(define-fptr-ref-inline $fptr-ref-single-float 'single-float #f)
(define-fptr-ref-inline $fptr-ref-swap-single-float 'single-float #t)
(define-fptr-ref-inline $fptr-ref-char 'unsigned-8 #f
(lambda (x) (build-integer->char x)))
(define-fptr-ref-inline $fptr-ref-wchar
(constant-case wchar-bits [(16) 'unsigned-16] [(32) 'unsigned-32])
#f
(lambda (x) (build-integer->char x)))
(define-fptr-ref-inline $fptr-ref-swap-wchar
(constant-case wchar-bits [(16) 'unsigned-16] [(32) 'unsigned-32])
#t
(lambda (x) (build-integer->char x)))
(define-fptr-ref-inline $fptr-ref-boolean
(constant-case int-bits [(32) 'unsigned-32] [(64) 'unsigned-64])
#f
(lambda (x)
`(if ,(%inline eq? ,x (immediate 0))
,(%constant sfalse)
,(%constant strue))))
(define-fptr-ref-inline $fptr-ref-swap-boolean
(constant-case int-bits [(32) 'unsigned-32] [(64) 'unsigned-64])
#t
(lambda (x)
`(if ,(%inline eq? ,x (immediate 0))
,(%constant sfalse)
,(%constant strue))))
(define-fptr-ref-inline $fptr-ref-fixnum 'fixnum #f)
(define-fptr-ref-inline $fptr-ref-swap-fixnum 'fixnum #t))
(let ()
(define $do-fptr-set!-inline
(lambda (set type e-fptr e-offset e-val)
(bind #f (e-offset)
(set type ($extract-fptr-address e-fptr) e-offset e-val))))
(define-syntax define-fptr-set!-inline
(lambda (x)
(define build-body
(lambda (type set maybe-massage-val)
#``(seq ,e-info
#,(let ([body #`($do-fptr-set!-inline #,set #,type e-fptr e-offset e-val)])
(if maybe-massage-val
#`,(bind #f (e-offset [e-val (#,maybe-massage-val e-val)]) #,body)
#`,(bind #f (e-offset e-val) #,body))))))
(define build-inline
(lambda (name check-64? body)
#`(define-inline 3 #,name
[(e-info e-fptr e-offset e-val)
#,(if check-64?
#`(and (fx>= (constant ptr-bits) 64) #,body)
body)])))
(syntax-case x ()
[(_ check-64? name ?type set)
(build-inline #'name (datum check-64?) (build-body #'?type #'set #f))]
[(_ check-64? name ?type set ?massage-value)
(build-inline #'name (datum check-64?) (build-body #'?type #'set #'?massage-value))])))
(define-fptr-set!-inline #f $fptr-set-integer-8! 'integer-8 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-unsigned-8! 'unsigned-8 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-integer-16! 'integer-16 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-unsigned-16! 'unsigned-16 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-integer-16! 'integer-16 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-unsigned-16! 'unsigned-16 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-integer-24! 'integer-24 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-unsigned-24! 'unsigned-24 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-integer-24! 'integer-24 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-unsigned-24! 'unsigned-24 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-integer-32! 'integer-32 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-unsigned-32! 'unsigned-32 build-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-integer-32! 'integer-32 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-unsigned-32! 'unsigned-32 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-integer-40! 'integer-40 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-unsigned-40! 'unsigned-40 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-integer-40! 'integer-40 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-unsigned-40! 'unsigned-40 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-integer-48! 'integer-48 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-unsigned-48! 'unsigned-48 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-integer-48! 'integer-48 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-unsigned-48! 'unsigned-48 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-integer-56! 'integer-56 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-unsigned-56! 'unsigned-56 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-integer-56! 'integer-56 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-unsigned-56! 'unsigned-56 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-integer-64! 'integer-64 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-unsigned-64! 'unsigned-64 build-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-integer-64! 'integer-64 build-swap-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-unsigned-64! 'unsigned-64 build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-double-float! 'double-float build-object-set!)
(define-fptr-set!-inline #t $fptr-set-swap-double-float! 'double-float build-swap-object-set!)
(define-fptr-set!-inline #f $fptr-set-single-float! 'single-float build-object-set!)
(define-fptr-set!-inline #f $fptr-set-char! 'unsigned-8 build-object-set!
(lambda (z) (build-char->integer z)))
(define-fptr-set!-inline #f $fptr-set-wchar!
(constant-case wchar-bits
[(16) 'unsigned-16]
[(32) 'unsigned-32])
build-object-set!
(lambda (z) (build-char->integer z)))
(define-fptr-set!-inline #f $fptr-set-swap-wchar!
(constant-case wchar-bits
[(16) 'unsigned-16]
[(32) 'unsigned-32])
build-swap-object-set!
(lambda (z) (build-char->integer z)))
(define-fptr-set!-inline #f $fptr-set-boolean!
(constant-case int-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64])
build-object-set!
(lambda (z) `(if ,z (immediate ,(fix 1)) (immediate ,(fix 0)))))
(define-fptr-set!-inline #f $fptr-set-swap-boolean!
(constant-case int-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64])
build-swap-object-set!
(lambda (z) `(if ,z (immediate ,(fix 1)) (immediate ,(fix 0)))))
(define-fptr-set!-inline #f $fptr-set-fixnum! 'fixnum build-object-set!)
(define-fptr-set!-inline #f $fptr-set-swap-fixnum! 'fixnum build-swap-object-set!))
(let ()
(define-syntax define-fptr-bits-ref-inline
(lambda (x)
(syntax-case x ()
[(_ name signed? type swapped?)
#'(define-inline 3 name
[(e-fptr e-offset e-start e-end)
(and (fixnum-constant? e-start) (fixnum-constant? e-end)
(let ([imm-start (constant-value e-start)] [imm-end (constant-value e-end)])
(and (<= (type->width 'type) (constant ptr-bits))
(and (fx>= imm-start 0) (fx> imm-end imm-start) (fx<= imm-end (constant ptr-bits)))
((if signed? fx<= fx<) (fx- imm-end imm-start) (constant fixnum-bits))
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #f (e-index)
(build-int-load swapped? 'type ($extract-fptr-address e-fptr) e-index imm-offset
(lambda (x)
((if signed? extract-signed-bitfield extract-unsigned-bitfield) #t imm-start imm-end x))))))))])])))
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-8 #t unsigned-8 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-8 #f unsigned-8 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-16 #t unsigned-16 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-16 #f unsigned-16 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-16 #t unsigned-16 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-16 #f unsigned-16 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-24 #t unsigned-24 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-24 #f unsigned-24 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-24 #t unsigned-24 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-24 #f unsigned-24 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-32 #t unsigned-32 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-32 #f unsigned-32 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-32 #t unsigned-32 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-32 #f unsigned-32 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-40 #t unsigned-40 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-40 #f unsigned-40 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-40 #t unsigned-40 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-40 #f unsigned-40 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-48 #t unsigned-48 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-48 #f unsigned-48 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-48 #t unsigned-48 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-48 #f unsigned-48 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-56 #t unsigned-56 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-56 #f unsigned-56 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-56 #t unsigned-56 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-56 #f unsigned-56 #t)
(define-fptr-bits-ref-inline $fptr-ref-ibits-unsigned-64 #t unsigned-64 #f)
(define-fptr-bits-ref-inline $fptr-ref-ubits-unsigned-64 #f unsigned-64 #f)
(define-fptr-bits-ref-inline $fptr-ref-ibits-swap-unsigned-64 #t unsigned-64 #t)
(define-fptr-bits-ref-inline $fptr-ref-ubits-swap-unsigned-64 #f unsigned-64 #t))
(let ()
(define-syntax define-fptr-bits-set-inline
(lambda (x)
(syntax-case x ()
[(_ check-64? name type swapped?)
(with-syntax ([(checks ...) #'((fixnum-constant? e-start) (fixnum-constant? e-end))])
(with-syntax ([(checks ...) (if (datum check-64?)
#'((fx>= (constant ptr-bits) 64) checks ...)
#'(checks ...))])
#`(define-inline 3 name
[(e-fptr e-offset e-start e-end e-val)
(and
checks ...
(let ([imm-start (constant-value e-start)] [imm-end (constant-value e-end)])
(and (<= (type->width 'type) (constant ptr-bits))
(and (fx>= imm-start 0) (fx> imm-end imm-start) (fx<= imm-end (constant ptr-bits)))
(fx< (fx- imm-end imm-start) (constant fixnum-bits))
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #t (e-index)
(bind #f (e-val)
(bind #t ([e-addr ($extract-fptr-address e-fptr)])
(build-int-load swapped? 'type e-addr e-index imm-offset
(lambda (x)
(build-int-store swapped? 'type e-addr e-index imm-offset
(insert-bitfield #t imm-start imm-end (type->width 'type) x
e-val)))))))))))])))])))
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-8! unsigned-8 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-16! unsigned-16 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-16! unsigned-16 #t)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-24! unsigned-24 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-24! unsigned-24 #t)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-32! unsigned-32 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-32! unsigned-32 #t)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-40! unsigned-40 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-40! unsigned-40 #t)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-48! unsigned-48 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-48! unsigned-48 #t)
(define-fptr-bits-set-inline #f $fptr-set-bits-unsigned-56! unsigned-56 #f)
(define-fptr-bits-set-inline #f $fptr-set-bits-swap-unsigned-56! unsigned-56 #t)
(define-fptr-bits-set-inline #t $fptr-set-bits-unsigned-64! unsigned-64 #f)
(define-fptr-bits-set-inline #t $fptr-set-bits-swap-unsigned-64! unsigned-64 #t))
(define-inline 3 $fptr-locked-decr!
[(e-fptr e-offset)
`(seq
,(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(%inline locked-decr!
,($extract-fptr-address e-fptr)
,e-index (immediate ,imm-offset)))
(inline ,(make-info-condition-code 'eq? #f #t) ,%condition-code))])
(define-inline 3 $fptr-locked-incr!
[(e-fptr e-offset)
`(seq
,(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(%inline locked-incr!
,($extract-fptr-address e-fptr)
,e-index (immediate ,imm-offset)))
(inline ,(make-info-condition-code 'eq? #f #t) ,%condition-code))])
(let ()
(define clear-lock
(lambda (e-fptr e-offset)
(let ([lock-type (constant-case ptr-bits [(32) 'integer-32] [(64) 'integer-64])])
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
`(inline ,(make-info-load lock-type #f) ,%store
,($extract-fptr-address e-fptr)
,e-index (immediate ,imm-offset) (immediate 0))))))
(define-inline 3 $fptr-init-lock!
[(e-fptr e-offset) (clear-lock e-fptr e-offset)])
(define-inline 3 $fptr-unlock!
[(e-fptr e-offset) (clear-lock e-fptr e-offset)]))
(define-inline 3 $fptr-lock!
[(e-fptr e-offset)
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #t ([e-base ($extract-fptr-address e-fptr)])
(%inline lock! ,e-base ,e-index (immediate ,imm-offset))))])
(define-inline 3 $fptr-spin-lock!
[(e-fptr e-offset)
(let-values ([(e-index imm-offset) (offset-expr->index+offset e-offset)])
(bind #t ([e-base ($extract-fptr-address e-fptr)])
(bind #t (e-index)
(let ([L1 (make-local-label 'L1)] [L2 (make-local-label 'L2)])
`(label ,L1
(if ,(%inline lock! ,e-base ,e-index (immediate ,imm-offset))
,(%constant svoid)
(seq
(pariah)
(label ,L2
(seq
,(%inline pause)
(if ,(%inline eq? (mref ,e-base ,e-index ,imm-offset) (immediate 0))
(goto ,L1)
(goto ,L2)))))))))))]))
(let ()
(define build-port-flags-set?
(lambda (e-p e-flags)
(%inline logtest
,(%mref ,e-p ,(constant port-type-disp))
,(nanopass-case (L7 Expr) e-flags
[(quote ,d) `(immediate ,(ash d (constant port-flags-offset)))]
[else (%inline sll ,e-flags
(immediate ,(fx- (constant port-flags-offset) (constant fixnum-offset))))]))))
(define build-port-input-empty?
(lambda (e-p)
(%inline eq?
,(%mref ,e-p ,(constant port-icount-disp))
(immediate 0))))
(define-inline 3 binary-port?
[(e-p) (build-port-flags-set? e-p `(quote ,(constant port-flag-binary)))])
(define-inline 3 textual-port?
[(e-p) (build-not (build-port-flags-set? e-p `(quote ,(constant port-flag-binary))))])
(define-inline 3 port-closed?
[(e-p) (build-port-flags-set? e-p `(quote ,(constant port-flag-closed)))])
(define-inline 3 $port-flags-set?
[(e-p e-flags) (build-port-flags-set? e-p e-flags)])
(define-inline 3 port-eof?
[(e-p)
(bind #t (e-p)
`(if ,(build-port-input-empty? e-p)
(if ,(build-port-flags-set? e-p `(quote ,(constant port-flag-eof)))
(immediate ,(constant strue))
,(build-libcall #t src sexpr unsafe-port-eof? e-p))
(immediate ,(constant sfalse))))])
(define-inline 2 port-eof?
[(e-p)
(let ([Llib (make-local-label 'Llib)])
(bind #t (e-p)
`(if ,(%type-check mask-typed-object type-typed-object ,e-p)
,(bind #t ([t0 (%mref ,e-p ,(constant typed-object-type-disp))])
`(if ,(%type-check mask-input-port type-input-port ,t0)
(if ,(build-port-input-empty? e-p)
(if ,(%inline logtest ,t0
(immediate ,(ash (constant port-flag-eof) (constant port-flags-offset))))
(immediate ,(constant strue))
(label ,Llib ,(build-libcall #t src sexpr safe-port-eof? e-p)))
(immediate ,(constant sfalse)))
(goto ,Llib)))
(goto ,Llib))))])
(define-inline 3 port-input-empty?
[(e-p) (build-port-input-empty? e-p)])
(define-inline 3 port-output-full?
[(e-p)
(%inline eq?
,(%mref ,e-p ,(constant port-ocount-disp))
(immediate 0))]))
(let ()
(define build-set-port-flags!
(lambda (e-p e-flags)
(bind #t (e-p)
`(set! ,(%mref ,e-p ,(constant port-type-disp))
,(%inline logor
,(%mref ,e-p ,(constant port-type-disp))
,(nanopass-case (L7 Expr) e-flags
[(quote ,d) `(immediate ,(ash d (constant port-flags-offset)))]
[else
(translate e-flags
(constant fixnum-offset)
(constant port-flags-offset))]))))))
(define build-reset-port-flags!
(lambda (e-p e-flags)
(bind #t (e-p)
`(set! ,(%mref ,e-p ,(constant port-type-disp))
,(%inline logand
,(%mref ,e-p ,(constant port-type-disp))
,(nanopass-case (L7 Expr) e-flags
[(quote ,d) `(immediate ,(lognot (ash d (constant port-flags-offset))))]
[else
(%inline lognot
,(translate e-flags
(constant fixnum-offset)
(constant port-flags-offset)))]))))))
(define-inline 3 $set-port-flags!
[(e-p e-flags) (build-set-port-flags! e-p e-flags)])
(define-inline 3 $reset-port-flags!
[(e-p e-flags) (build-reset-port-flags! e-p e-flags)])
(define-inline 3 mark-port-closed!
[(e-p) (build-set-port-flags! e-p `(quote ,(constant port-flag-closed)))])
(let ()
(define (go e-p e-bool flag)
(let ([e-flags `(quote ,flag)])
(nanopass-case (L7 Expr) e-bool
[(quote ,d)
((if d build-set-port-flags! build-reset-port-flags!) e-p e-flags)]
[else
(bind #t (e-p)
`(if ,e-bool
,(build-set-port-flags! e-p e-flags)
,(build-reset-port-flags! e-p e-flags)))])))
(define-inline 3 set-port-bol!
[(e-p e-bool) (go e-p e-bool (constant port-flag-bol))])
(define-inline 3 set-port-eof!
[(e-p e-bool) (go e-p e-bool (constant port-flag-eof))])))
(let ()
(define (build-port-input-size port-type e-p)
(bind #t (e-p)
(translate
(%inline -
,(%inline -
,(%mref ,e-p ,(constant port-ilast-disp))
,(%mref ,e-p ,(constant port-ibuffer-disp)))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-input-size
[(e-p) (build-port-input-size 'textual e-p)])
(define-inline 3 binary-port-input-size
[(e-p) (build-port-input-size 'binary e-p)]))
(let ()
(define (build-port-output-size port-type e-p)
(bind #t (e-p)
(translate
(%inline -
,(%inline -
,(%mref ,e-p ,(constant port-olast-disp))
,(%mref ,e-p ,(constant port-obuffer-disp)))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-output-size
[(e-p) (build-port-output-size 'textual e-p)])
(define-inline 3 binary-port-output-size
[(e-p) (build-port-output-size 'binary e-p)]))
(let ()
(define (build-port-input-index port-type e-p)
(bind #t (e-p)
(translate
; TODO: use lea2?
(%inline +
,(%inline -
,(%inline -
,(%mref ,e-p ,(constant port-ilast-disp))
,(%mref ,e-p ,(constant port-ibuffer-disp)))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
,(%mref ,e-p ,(constant port-icount-disp)))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-input-index
[(e-p) (build-port-input-index 'textual e-p)])
(define-inline 3 binary-port-input-index
[(e-p) (build-port-input-index 'binary e-p)]))
(let ()
(define (build-port-output-index port-type e-p)
(bind #t (e-p)
(translate
(%inline +
,(%inline -
,(%inline -
,(%mref ,e-p ,(constant port-olast-disp))
,(%mref ,e-p ,(constant port-obuffer-disp)))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
,(%mref ,e-p ,(constant port-ocount-disp)))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-output-index
[(e-p) (build-port-output-index 'textual e-p)])
(define-inline 3 binary-port-output-index
[(e-p) (build-port-output-index 'binary e-p)]))
(let ()
(define (build-port-input-count port-type e-p)
(bind #t (e-p)
(translate
(%inline -
(immediate 0)
,(%mref ,e-p ,(constant port-icount-disp)))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-input-count
[(e-p) (build-port-input-count 'textual e-p)])
(define-inline 3 binary-port-input-count
[(e-p) (build-port-input-count 'binary e-p)]))
(let ()
(define (build-port-output-count port-type e-p)
(bind #t (e-p)
(translate
(%inline -
(immediate 0)
,(%mref ,e-p ,(constant port-ocount-disp)))
(if (eq? port-type 'textual) (constant string-char-offset) 0)
(constant fixnum-offset))))
(define-inline 3 textual-port-output-count
[(e-p) (build-port-output-count 'textual e-p)])
(define-inline 3 binary-port-output-count
[(e-p) (build-port-output-count 'binary e-p)]))
(let ()
(define (build-set-port-input-size! port-type e-p e-x)
; actually, set last to buffer[0] + size; count to size
(bind #t (e-p)
(bind #t ([e-x (translate e-x
(constant fixnum-offset)
(if (eq? port-type 'textual) (constant string-char-offset) 0))])
`(seq
(set! ,(%mref ,e-p ,(constant port-icount-disp))
,(%inline - (immediate 0) ,e-x))
(set! ,(%mref ,e-p ,(constant port-ilast-disp))
,(%inline +
,(%inline +
,(%mref ,e-p ,(constant port-ibuffer-disp))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
,e-x))))))
(define-inline 3 set-textual-port-input-size!
[(e-p e-x) (build-set-port-input-size! 'textual e-p e-x)])
(define-inline 3 set-binary-port-input-size!
[(e-p e-x) (build-set-port-input-size! 'binary e-p e-x)]))
(let ()
(define (build-set-port-output-size! port-type e-p e-x)
; actually, set last to buffer[0] + size; count to size
(bind #t (e-p)
(bind #t ([e-x (translate e-x
(constant fixnum-offset)
(if (eq? port-type 'textual) (constant string-char-offset) 0))])
`(seq
(set! ,(%mref ,e-p ,(constant port-ocount-disp))
,(%inline - (immediate 0) ,e-x))
(set! ,(%mref ,e-p ,(constant port-olast-disp))
,(%inline +
,(%inline +
,(%mref ,e-p ,(constant port-obuffer-disp))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))
,e-x))))))
(define-inline 3 set-textual-port-output-size!
[(e-p e-x) (build-set-port-output-size! 'textual e-p e-x)])
(define-inline 3 set-binary-port-output-size!
[(e-p e-x) (build-set-port-output-size! 'binary e-p e-x)]))
(let ()
(define (build-set-port-input-index! port-type e-p e-x)
; actually, set count to index - size, where size = last - buffer[0]
(bind #t (e-p)
`(set! ,(%mref ,e-p ,(constant port-icount-disp))
,(%inline -
,(translate e-x
(constant fixnum-offset)
(if (eq? port-type 'textual) (constant string-char-offset) 0))
,(%inline -
,(%mref ,e-p ,(constant port-ilast-disp))
,(%inline +
,(%mref ,e-p ,(constant port-ibuffer-disp))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp)))))))))
(define-inline 3 set-textual-port-input-index!
[(e-p e-x) (build-set-port-input-index! 'textual e-p e-x)])
(define-inline 3 set-binary-port-input-index!
[(e-p e-x) (build-set-port-input-index! 'binary e-p e-x)]))
(let ()
(define (build-set-port-output-index! port-type e-p e-x)
; actually, set count to index - size, where size = last - buffer[0]
(bind #t (e-p)
`(set! ,(%mref ,e-p ,(constant port-ocount-disp))
,(%inline -
,(translate e-x
(constant fixnum-offset)
(if (eq? port-type 'textual) (constant string-char-offset) 0))
,(%inline -
,(%mref ,e-p ,(constant port-olast-disp))
,(%inline +
,(%mref ,e-p ,(constant port-obuffer-disp))
(immediate
,(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp)))))))))
(define-inline 3 set-textual-port-output-index!
[(e-p e-x) (build-set-port-output-index! 'textual e-p e-x)])
(define-inline 3 set-binary-port-output-index!
[(e-p e-x) (build-set-port-output-index! 'binary e-p e-x)]))
(let ()
(define (make-build-set-port-buffer! port-type ibuffer-disp icount-disp ilast-disp)
(lambda (e-p e-b new?)
(bind #t (e-p e-b)
`(seq
,(if new?
`(set! ,(%mref ,e-p ,ibuffer-disp) ,e-b)
(build-dirty-store e-p ibuffer-disp e-b))
,(bind #t ([e-length (if (eq? port-type 'textual)
(translate
(%inline logand
,(%mref ,e-b ,(constant string-type-disp))
(immediate ,(fx- (expt 2 (constant string-length-offset)))))
(constant string-length-offset)
(constant string-char-offset))
(%inline srl
,(%mref ,e-b ,(constant bytevector-type-disp))
,(%constant bytevector-length-offset)))])
`(seq
(set! ,(%mref ,e-p ,icount-disp)
,(%inline - (immediate 0) ,e-length))
(set! ,(%mref ,e-p ,ilast-disp)
,(%lea ,e-b ,e-length
(if (eq? port-type 'textual)
(constant string-data-disp)
(constant bytevector-data-disp))))))))))
(define (make-port e-name e-handler e-ib e-ob e-info flags set-ibuf! set-obuf!)
(bind #f (e-name e-handler e-info e-ib e-ob)
(bind #t ([e-p (%constant-alloc type-typed-object (constant size-port))])
(%seq
(set! ,(%mref ,e-p ,(constant port-type-disp)) (immediate ,flags))
(set! ,(%mref ,e-p ,(constant port-handler-disp)) ,e-handler)
(set! ,(%mref ,e-p ,(constant port-name-disp)) ,e-name)
(set! ,(%mref ,e-p ,(constant port-info-disp)) ,e-info)
,(set-ibuf! e-p e-ib #t)
,(set-obuf! e-p e-ob #t)
,e-p))))
(define (make-build-clear-count count-disp)
(lambda (e-p e-b new?)
`(set! ,(%mref ,e-p ,count-disp) (immediate 0))))
(let ()
(define build-set-textual-port-input-buffer!
(make-build-set-port-buffer! 'textual
(constant port-ibuffer-disp)
(constant port-icount-disp)
(constant port-ilast-disp)))
(define build-set-textual-port-output-buffer!
(make-build-set-port-buffer! 'textual
(constant port-obuffer-disp)
(constant port-ocount-disp)
(constant port-olast-disp)))
(define-inline 3 set-textual-port-input-buffer!
[(e-p e-b) (build-set-textual-port-input-buffer! e-p e-b #f)])
(define-inline 3 set-textual-port-output-buffer!
[(e-p e-b) (build-set-textual-port-output-buffer! e-p e-b #f)])
(let ()
(define (go e-name e-handler e-ib e-info)
(make-port e-name e-handler e-ib `(quote "") e-info
(fxlogor (constant type-input-port) (constant PORT-FLAG-INPUT-MODE))
build-set-textual-port-input-buffer!
(make-build-clear-count (constant port-ocount-disp))))
(define-inline 3 $make-textual-input-port
[(e-name e-handler e-ib) (go e-name e-handler e-ib `(quote #f))]
[(e-name e-handler e-ib e-info) (go e-name e-handler e-ib e-info)]))
(let ()
(define (go e-name e-handler e-ob e-info)
(make-port e-name e-handler `(quote "") e-ob e-info
(constant type-output-port)
(make-build-clear-count (constant port-icount-disp))
build-set-textual-port-output-buffer!))
(define-inline 3 $make-textual-output-port
[(e-name e-handler e-ob) (go e-name e-handler e-ob `(quote #f))]
[(e-name e-handler e-ob e-info) (go e-name e-handler e-ob e-info)]))
(let ()
(define (go e-name e-handler e-ib e-ob e-info)
(make-port e-name e-handler e-ib e-ob e-info
(constant type-io-port)
build-set-textual-port-input-buffer!
build-set-textual-port-output-buffer!))
(define-inline 3 $make-textual-input/output-port
[(e-name e-handler e-ib e-ob) (go e-name e-handler e-ib e-ob `(quote #f))]
[(e-name e-handler e-ib e-ob e-info) (go e-name e-handler e-ib e-ob e-info)])))
(let ()
(define build-set-binary-port-input-buffer!
(make-build-set-port-buffer! 'binary
(constant port-ibuffer-disp)
(constant port-icount-disp)
(constant port-ilast-disp)))
(define build-set-binary-port-output-buffer!
(make-build-set-port-buffer! 'binary
(constant port-obuffer-disp)
(constant port-ocount-disp)
(constant port-olast-disp)))
(define-inline 3 set-binary-port-input-buffer!
[(e-p e-b) (build-set-binary-port-input-buffer! e-p e-b #f)])
(define-inline 3 set-binary-port-output-buffer!
[(e-p e-b) (build-set-binary-port-output-buffer! e-p e-b #f)])
(let ()
(define (go e-name e-handler e-ib e-info)
(make-port e-name e-handler e-ib `(quote #vu8()) e-info
(fxlogor (constant type-input-port) (constant PORT-FLAG-INPUT-MODE) (constant PORT-FLAG-BINARY))
build-set-binary-port-input-buffer!
(make-build-clear-count (constant port-ocount-disp))))
(define-inline 3 $make-binary-input-port
[(e-name e-handler e-ib) (go e-name e-handler e-ib `(quote #f))]
[(e-name e-handler e-ib e-info) (go e-name e-handler e-ib e-info)]))
(let ()
(define (go e-name e-handler e-ob e-info)
(make-port e-name e-handler `(quote #vu8()) e-ob e-info
(fxlogor (constant type-output-port) (constant PORT-FLAG-BINARY))
(make-build-clear-count (constant port-icount-disp))
build-set-binary-port-output-buffer!))
(define-inline 3 $make-binary-output-port
[(e-name e-handler e-ob) (go e-name e-handler e-ob `(quote #f))]
[(e-name e-handler e-ob e-info) (go e-name e-handler e-ob e-info)]))
(let ()
(define (go e-name e-handler e-ib e-ob e-info)
(make-port e-name e-handler e-ib e-ob e-info
(fxlogor (constant type-io-port) (constant PORT-FLAG-BINARY))
build-set-binary-port-input-buffer!
build-set-binary-port-output-buffer!))
(define-inline 3 $make-binary-input/output-port
[(e-name e-handler e-ib e-ob) (go e-name e-handler e-ib e-ob `(quote #f))]
[(e-name e-handler e-ib e-ob e-info) (go e-name e-handler e-ib e-ob e-info)]))))
(let ()
(define build-fxvector-ref-check (build-ref-check fxvector-type-disp maximum-fxvector-length fxvector-length-offset type-fxvector mask-fxvector fxvector-immutable-flag))
(define build-fxvector-set!-check (build-ref-check fxvector-type-disp maximum-fxvector-length fxvector-length-offset type-mutable-fxvector mask-mutable-fxvector fxvector-immutable-flag))
(define-inline 2 $fxvector-ref-check?
[(e-fv e-i) (bind #t (e-fv e-i) (build-fxvector-ref-check e-fv e-i #f))])
(define-inline 2 $fxvector-set!-check?
[(e-fv e-i) (bind #t (e-fv e-i) (build-fxvector-set!-check e-fv e-i #f))])
(let ()
(define (go e-fv e-i)
(cond
[(expr->index e-i 1 (constant maximum-fxvector-length)) =>
(lambda (index)
(%mref ,e-fv
,(+ (fix index) (constant fxvector-data-disp))))]
[else (%mref ,e-fv ,e-i ,(constant fxvector-data-disp))]))
(define-inline 3 fxvector-ref
[(e-fv e-i) (go e-fv e-i)])
(define-inline 2 fxvector-ref
[(e-fv e-i)
(bind #t (e-fv e-i)
`(if ,(build-fxvector-ref-check e-fv e-i #f)
,(go e-fv e-i)
,(build-libcall #t src sexpr fxvector-ref e-fv e-i)))]))
(let ()
(define (go e-fv e-i e-new)
`(set!
,(cond
[(expr->index e-i 1 (constant maximum-fxvector-length)) =>
(lambda (index)
(%mref ,e-fv
,(+ (fix index) (constant fxvector-data-disp))))]
[else (%mref ,e-fv ,e-i ,(constant fxvector-data-disp))])
,e-new))
(define-inline 3 fxvector-set!
[(e-fv e-i e-new)
(go e-fv e-i e-new)])
(define-inline 2 fxvector-set!
[(e-fv e-i e-new)
(bind #t (e-fv e-i e-new)
`(if ,(build-fxvector-set!-check e-fv e-i e-new)
,(go e-fv e-i e-new)
,(build-libcall #t src sexpr fxvector-set! e-fv e-i e-new)))])
(define-inline 3 $fxvector-set-immutable!
[(e-fv) ((build-set-immutable! fxvector-type-disp fxvector-immutable-flag) e-fv)])))
(let ()
(define build-string-ref-check
(lambda (e-s e-i)
((build-ref-check string-type-disp maximum-string-length string-length-offset type-string mask-string string-immutable-flag) e-s e-i #f)))
(define build-string-set!-check
(lambda (e-s e-i)
((build-ref-check string-type-disp maximum-string-length string-length-offset type-mutable-string mask-mutable-string string-immutable-flag) e-s e-i #f)))
(define-inline 2 $string-ref-check?
[(e-s e-i) (bind #t (e-s e-i) (build-string-ref-check e-s e-i))])
(define-inline 2 $string-set!-check?
[(e-s e-i) (bind #t (e-s e-i) (build-string-set!-check e-s e-i))])
(let ()
(define (go e-s e-i)
(cond
[(expr->index e-i 1 (constant maximum-string-length)) =>
(lambda (index)
`(inline ,(make-info-load (string-char-type) #f) ,%load ,e-s ,%zero
(immediate ,(+ (* (constant string-char-bytes) index) (constant string-data-disp)))))]
[else
`(inline ,(make-info-load (string-char-type) #f) ,%load ,e-s
,(translate e-i
(constant fixnum-offset)
(constant string-char-offset))
,(%constant string-data-disp))]))
(define-inline 3 string-ref
[(e-s e-i) (go e-s e-i)])
(define-inline 2 string-ref
[(e-s e-i)
(bind #t (e-s e-i)
`(if ,(build-string-ref-check e-s e-i)
,(go e-s e-i)
,(build-libcall #t src sexpr string-ref e-s e-i)))]))
(let ()
(define (go e-s e-i e-new)
(cond
[(expr->index e-i 1 (constant maximum-string-length)) =>
(lambda (index)
`(inline ,(make-info-load (string-char-type) #f) ,%store ,e-s ,%zero
(immediate ,(+ (* (constant string-char-bytes) index) (constant string-data-disp)))
,e-new))]
[else
`(inline ,(make-info-load (string-char-type) #f) ,%store ,e-s
,(translate e-i
(constant fixnum-offset)
(constant string-char-offset))
,(%constant string-data-disp)
,e-new)]))
(define-inline 3 string-set!
[(e-s e-i e-new) (go e-s e-i e-new)])
(define-inline 2 string-set!
[(e-s e-i e-new)
(bind #t (e-s e-i e-new)
`(if ,(let ([e-ref-check (build-string-set!-check e-s e-i)])
(if (constant? char? e-new)
e-ref-check
(build-and e-ref-check (%type-check mask-char type-char ,e-new))))
,(go e-s e-i e-new)
,(build-libcall #t src sexpr string-set! e-s e-i e-new)))])
(define-inline 3 $string-set-immutable!
[(e-s) ((build-set-immutable! string-type-disp string-immutable-flag) e-s)])))
(let ()
(define build-vector-ref-check (build-ref-check vector-type-disp maximum-vector-length vector-length-offset type-vector mask-vector vector-immutable-flag))
(define build-vector-set!-check (build-ref-check vector-type-disp maximum-vector-length vector-length-offset type-mutable-vector mask-mutable-vector vector-immutable-flag))
(define-inline 2 $vector-ref-check?
[(e-v e-i) (bind #t (e-v e-i) (build-vector-ref-check e-v e-i #f))])
(define-inline 2 $vector-set!-check?
[(e-v e-i) (bind #t (e-v e-i) (build-vector-set!-check e-v e-i #f))])
(let ()
(define (go e-v e-i)
(nanopass-case (L7 Expr) e-i
[(quote ,d)
(guard (target-fixnum? d))
(%mref ,e-v ,(+ (fix d) (constant vector-data-disp)))]
[else (%mref ,e-v ,e-i ,(constant vector-data-disp))]))
(define-inline 3 vector-ref
[(e-v e-i) (go e-v e-i)])
(define-inline 2 vector-ref
[(e-v e-i)
(bind #t (e-v e-i)
`(if ,(build-vector-ref-check e-v e-i #f)
,(go e-v e-i)
,(build-libcall #t src sexpr vector-ref e-v e-i)))]))
(let ()
(define (go e-v e-i e-new)
(nanopass-case (L7 Expr) e-i
[(quote ,d)
(guard (target-fixnum? d))
(build-dirty-store e-v (+ (fix d) (constant vector-data-disp)) e-new)]
[else (build-dirty-store e-v e-i (constant vector-data-disp) e-new)]))
(define-inline 3 vector-set!
[(e-v e-i e-new) (go e-v e-i e-new)])
(define-inline 2 vector-set!
[(e-v e-i e-new)
(bind #t (e-v e-i e-new)
`(if ,(build-vector-set!-check e-v e-i #f)
,(go e-v e-i e-new)
,(build-libcall #t src sexpr vector-set! e-v e-i e-new)))])
(define-inline 3 $vector-set-immutable!
[(e-fv) ((build-set-immutable! vector-type-disp vector-immutable-flag) e-fv)]))
(let ()
(define (go e-v e-i e-old e-new)
(nanopass-case (L7 Expr) e-i
[(quote ,d)
(guard (target-fixnum? d))
(build-dirty-store e-v %zero (+ (fix d) (constant vector-data-disp)) e-new (make-build-cas e-old) build-cas-seq)]
[else (build-dirty-store e-v e-i (constant vector-data-disp) e-new (make-build-cas e-old) build-cas-seq)]))
(define-inline 3 vector-cas!
[(e-v e-i e-old e-new) (go e-v e-i e-old e-new)])
(define-inline 2 vector-cas!
[(e-v e-i e-old e-new)
(bind #t (e-v e-i e-old e-new)
`(if ,(build-vector-set!-check e-v e-i #f)
,(go e-v e-i e-old e-new)
,(build-libcall #t src sexpr vector-cas! e-v e-i e-old e-new)))]))
(let ()
(define (go e-v e-i e-new)
`(set!
,(nanopass-case (L7 Expr) e-i
[(quote ,d)
(guard (target-fixnum? d))
(%mref ,e-v ,(+ (fix d) (constant vector-data-disp)))]
[else (%mref ,e-v ,e-i ,(constant vector-data-disp))])
,e-new))
(define-inline 3 vector-set-fixnum!
[(e-v e-i e-new) (go e-v e-i e-new)])
(define-inline 2 vector-set-fixnum!
[(e-v e-i e-new)
(bind #t (e-v e-i e-new)
`(if ,(build-vector-set!-check e-v e-i e-new)
,(go e-v e-i e-new)
,(build-libcall #t src sexpr vector-set-fixnum! e-v e-i e-new)))])))
(let ()
(define build-bytevector-ref-check
(lambda (e-bits e-bv e-i check-mutable?)
(nanopass-case (L7 Expr) e-bits
[(quote ,d)
(guard (and (fixnum? d) (fx> d 0) (fx= (* (fxquotient d 8) 8) d)))
(let ([bits d] [bytes (fxquotient d 8)])
(bind #t (e-bv e-i)
(build-and
(%type-check mask-typed-object type-typed-object ,e-bv)
(bind #t ([t (%mref ,e-bv ,(constant bytevector-type-disp))])
(build-and
(if check-mutable?
(%type-check mask-mutable-bytevector type-mutable-bytevector ,t)
(%type-check mask-bytevector type-bytevector ,t))
(cond
[(expr->index e-i bytes (constant maximum-bytevector-length)) =>
(lambda (index)
(%inline u<
(immediate ,(logor (ash (+ index (fx- bytes 1)) (constant bytevector-length-offset))
(constant type-bytevector) (constant bytevector-immutable-flag)))
,t))]
[else
(build-and
($type-check (fxlogor (fix (fx- bytes 1)) (constant mask-fixnum)) (constant type-fixnum) e-i)
(%inline u<
; NB. add cannot overflow or change negative to positive when
; low-order (log2 bytes) bits of fixnum value are zero, as
; guaranteed by type-check above
,(if (fx= bytes 1)
e-i
(%inline + ,e-i (immediate ,(fix (fx- bytes 1)))))
,(%inline logand
,(translate t
(constant bytevector-length-offset)
(constant fixnum-offset))
(immediate ,(- (constant fixnum-factor))))))]))))))]
[(seq (profile ,src) ,[e]) (and e `(seq (profile ,src) ,e))]
[else #f])))
(define-inline 2 $bytevector-ref-check?
[(e-bits e-bv e-i) (build-bytevector-ref-check e-bits e-bv e-i #f)])
(define-inline 2 $bytevector-set!-check?
[(e-bits e-bv e-i) (build-bytevector-ref-check e-bits e-bv e-i #t)]))
(let ()
(define build-bytevector-fill
(let ([filler (make-build-fill 1 (constant bytevector-data-disp))])
(lambda (e-bv e-bytes e-fill)
(bind #t uptr ([e-fill (build-unfix e-fill)])
(filler e-bv e-bytes e-fill)))))
(let ()
(define do-make-bytevector
(lambda (e-length maybe-e-fill)
; NB: caller must bind maybe-e-fill
(safe-assert (or (not maybe-e-fill) (no-need-to-bind? #f maybe-e-fill)))
(if (constant? (lambda (x) (and (fixnum? x) (fx<= 0 x 10000))) e-length)
(let ([n (constant-value e-length)])
(if (fx= n 0)
`(quote ,(bytevector))
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-bytevector) n))])
`(seq
(set! ,(%mref ,t ,(constant bytevector-type-disp))
(immediate ,(fx+ (fx* n (constant bytevector-length-factor))
(constant type-bytevector))))
,(if maybe-e-fill
(build-bytevector-fill t `(immediate ,n) maybe-e-fill)
t)))))
(bind #t (e-length)
(let ([t-bytes (make-tmp 'tbytes 'uptr)] [t-vec (make-tmp 'tvec)])
`(if ,(%inline eq? ,e-length (immediate 0))
(quote ,(bytevector))
(let ([,t-bytes ,(build-unfix e-length)])
(let ([,t-vec (alloc ,(make-info-alloc (constant type-typed-object) #f #f)
,(%inline logand
,(%inline + ,t-bytes
(immediate ,(fx+ (constant header-size-bytevector)
(fx- (constant byte-alignment) 1))))
(immediate ,(- (constant byte-alignment)))))])
(seq
(set! ,(%mref ,t-vec ,(constant bytevector-type-disp))
,(build-type/length t-bytes
(constant type-bytevector)
0
(constant bytevector-length-offset)))
,(if maybe-e-fill
(build-bytevector-fill t-vec t-bytes maybe-e-fill)
t-vec))))))))))
(let ()
(define valid-length?
(lambda (e-length)
(constant?
(lambda (x)
(and (or (fixnum? x) (bignum? x))
(<= 0 x (constant maximum-bytevector-length))))
e-length)))
(define-inline 2 make-bytevector
[(e-length) (and (valid-length? e-length) (do-make-bytevector e-length #f))]
[(e-length e-fill)
(and (valid-length? e-length)
(constant? (lambda (x) (and (fixnum? x) (fx<= -128 x 255))) e-fill)
(do-make-bytevector e-length e-fill))]))
(define-inline 3 make-bytevector
[(e-length) (do-make-bytevector e-length #f)]
[(e-length e-fill) (bind #f (e-fill) (do-make-bytevector e-length e-fill))]))
(define-inline 3 bytevector-fill!
[(e-bv e-fill)
(bind #t (e-bv e-fill)
`(seq
,(build-bytevector-fill e-bv
(%inline srl
,(%mref ,e-bv ,(constant bytevector-type-disp))
,(%constant bytevector-length-offset))
e-fill)
,(%constant svoid)))]))
(let ()
(define build-bytevector
(lambda (e*)
(define (find-k n)
(let loop ([bytes (constant-case ptr-bits [(32) 4] [(64) 8])]
[type* (constant-case ptr-bits
[(32) '(unsigned-32 unsigned-16 unsigned-8)]
[(64) '(unsigned-64 unsigned-32 unsigned-16 unsigned-8)])])
(let ([bytes/2 (fxsrl bytes 1)])
(if (fx<= n bytes/2)
(loop bytes/2 (cdr type*))
(values bytes (car type*))))))
(define (build-chunk k n e*)
(define (build-shift e shift)
(if (fx= shift 0) e (%inline sll ,e (immediate ,shift))))
(let loop ([k (constant-case native-endianness
[(little) (fxmin k n)]
[(big) k])]
[e* (constant-case native-endianness
[(little) (reverse (if (fx<= n k) e* (list-head e* k)))]
[(big) e*])]
[constant-part 0]
[expression-part #f]
[expression-shift 0]
[mask? #f]) ; no need to mask the high-order byte
(if (fx= k 0)
(if expression-part
(let ([expression-part (build-shift expression-part expression-shift)])
(if (= constant-part 0)
expression-part
(%inline logor ,expression-part (immediate ,constant-part))))
`(immediate ,constant-part))
(let ([k (fx- k 1)]
[constant-part (ash constant-part 8)]
[expression-shift (fx+ expression-shift 8)])
(if (null? e*)
(loop k e* constant-part expression-part expression-shift #t)
(let ([e (car e*)] [e* (cdr e*)])
(if (fixnum-constant? e)
(loop k e* (logor constant-part (logand (constant-value e) #xff)) expression-part expression-shift #t)
(loop k e* constant-part
(let* ([e (build-unfix e)]
[e (if mask? (%inline logand ,e (immediate #xff)) e)])
(if expression-part
(%inline logor ,(build-shift expression-part expression-shift) ,e)
e))
0 #t))))))))
(let ([len (length e*)])
(if (fx= len 0)
`(quote ,(bytevector))
(list-bind #f (e*)
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-bytevector) len))])
`(seq
(set! ,(%mref ,t ,(constant bytevector-type-disp))
(immediate ,(+ (* len (constant bytevector-length-factor))
(constant type-bytevector))))
; build and store k-octet (k = 4 on 32-bit machines, k = 8 on 64-bit
; machines) chunks, taking endianness into account. for the last
; chunk, set k = 1, 2, 4, or 8 depending on the number of octets
; remaining, padding with zeros as necessary.
,(let f ([e* e*] [n (length e*)] [offset (constant bytevector-data-disp)])
(let-values ([(k type) (find-k n)])
`(seq
(inline ,(make-info-load type #f) ,%store ,t ,%zero (immediate ,offset)
,(build-chunk k n e*))
,(if (fx<= n k)
t
(f (list-tail e* k) (fx- n k) (fx+ offset k)))))))))))))
(define-inline 2 bytevector
[e* (and (andmap
(lambda (x)
(constant?
(lambda (x) (and (fixnum? x) (fx<= -128 x 255)))
x))
e*)
(build-bytevector e*))])
(define-inline 3 bytevector
[e* (build-bytevector e*)]))
(let ()
(define byte-offset
(lambda (off)
(cond
[(nanopass-case (L7 Expr) off
[(quote ,d)
(and (and (integer? d) (exact? d))
(let ([n (+ d (constant bytevector-data-disp))])
(and (target-fixnum? n)
`(quote ,n))))]
[else #f])]
[else (%inline + ,off
(quote ,(constant bytevector-data-disp)))])))
(define-inline 3 bytevector-copy!
[(bv1 off1 bv2 off2 n)
(%primcall src sexpr $byte-copy! ,bv1 ,(byte-offset off1) ,bv2 ,(byte-offset off2) ,n)]))
(define-inline 3 bytevector-truncate!
[(bv len)
(if (fixnum-constant? len)
(let ([len (constant-value len)])
(if (fx= len 0)
`(quote ,(bytevector))
(bind #t (bv)
`(seq
(set! ,(%mref ,bv ,(constant bytevector-type-disp))
(immediate ,(fx+ (fx* len (constant bytevector-length-factor))
(constant type-bytevector))))
,bv))))
(bind #t (bv len)
`(if ,(%inline eq? ,len (immediate 0))
(quote ,(bytevector))
(seq
(set! ,(%mref ,bv ,(constant bytevector-type-disp))
,(build-type/length len
(constant type-bytevector)
(constant fixnum-offset)
(constant bytevector-length-offset)))
,bv))))])
(define-inline 3 $bytevector-set-immutable!
[(bv) ((build-set-immutable! bytevector-type-disp bytevector-immutable-flag) bv)])
(let ()
(define bv-index-offset
(lambda (offset-expr)
(if (fixnum-constant? offset-expr)
(values %zero (+ (constant bytevector-data-disp) (constant-value offset-expr)))
(values (build-unfix offset-expr) (constant bytevector-data-disp)))))
(define bv-offset-okay?
(lambda (x mask)
(constant? (lambda (x) (and (target-fixnum? x) (>= x 0) (eq? (logand x mask) 0))) x)))
(let ()
(define-syntax define-bv-8-inline
(syntax-rules ()
[(_ name type)
(define-inline 2 name
[(e-bv e-offset)
(bind #t (e-bv e-offset)
`(if ,(handle-prim #f #f 3 '$bytevector-ref-check? (list `(quote 8) e-bv e-offset))
,(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-ref #f 'type e-bv e-index imm-offset))
,(build-libcall #t src sexpr name e-bv e-offset)))])]))
(define-bv-8-inline bytevector-s8-ref integer-8)
(define-bv-8-inline bytevector-u8-ref unsigned-8))
(let ()
(define-syntax define-bv-native-ref-inline
(lambda (x)
(syntax-case x ()
[(_ name type)
#'(define-inline 3 name
[(e-bv e-offset)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-ref #f 'type e-bv e-index imm-offset))])])))
(define-bv-native-ref-inline bytevector-s8-ref integer-8)
(define-bv-native-ref-inline bytevector-u8-ref unsigned-8)
(define-bv-native-ref-inline bytevector-s16-native-ref integer-16)
(define-bv-native-ref-inline bytevector-u16-native-ref unsigned-16)
(define-bv-native-ref-inline bytevector-s32-native-ref integer-32)
(define-bv-native-ref-inline bytevector-u32-native-ref unsigned-32)
(define-bv-native-ref-inline bytevector-s64-native-ref integer-64)
(define-bv-native-ref-inline bytevector-u64-native-ref unsigned-64)
(define-bv-native-ref-inline bytevector-ieee-single-native-ref single-float)
(define-bv-native-ref-inline bytevector-ieee-double-native-ref double-float))
(let ()
(define-syntax define-bv-native-int-set!-inline
(lambda (x)
(syntax-case x ()
[(_ check-64? name type)
(with-syntax ([body #'(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-set! 'type e-bv e-index imm-offset e-val))])
(with-syntax ([body (if (datum check-64?)
#'(and (>= (constant ptr-bits) 64) body)
#'body)])
#'(define-inline 3 name
[(e-bv e-offset e-val) body])))])))
(define-bv-native-int-set!-inline #f bytevector-s8-set! integer-8)
(define-bv-native-int-set!-inline #f bytevector-u8-set! unsigned-8)
(define-bv-native-int-set!-inline #f $bytevector-set! unsigned-8)
(define-bv-native-int-set!-inline #f bytevector-s16-native-set! integer-16)
(define-bv-native-int-set!-inline #f bytevector-u16-native-set! unsigned-16)
(define-bv-native-int-set!-inline #f bytevector-s32-native-set! integer-32)
(define-bv-native-int-set!-inline #f bytevector-u32-native-set! unsigned-32)
(define-bv-native-int-set!-inline #t bytevector-s64-native-set! integer-64)
(define-bv-native-int-set!-inline #t bytevector-u64-native-set! unsigned-64))
(let ()
(define-syntax define-bv-native-ieee-set!-inline
(lambda (x)
(syntax-case x ()
[(_ name type)
#'(define-inline 3 name
[(e-bv e-offset e-val)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(bind #f (e-bv e-index)
(build-object-set! 'type e-bv e-index imm-offset
(build-$real->flonum src sexpr e-val `(quote name)))))])])))
(define-bv-native-ieee-set!-inline bytevector-ieee-single-native-set! single-float)
(define-bv-native-ieee-set!-inline bytevector-ieee-double-native-set! double-float))
(let ()
(define-syntax define-bv-int-ref-inline
(lambda (x)
(define p2?
(lambda (n)
(let f ([i 1])
(or (fx= i n)
(and (not (fx> i n)) (f (fxsll i 1)))))))
(syntax-case x ()
[(_ name type mask)
#`(define-inline 3 name
[(e-bv e-offset e-eness)
(and (or (constant unaligned-integers)
(and #,(p2? (fx+ (datum mask) 1)) (bv-offset-okay? e-offset mask)))
(constant? (lambda (x) (memq x '(big little))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-ref (not (eq? (constant-value e-eness) (constant native-endianness)))
'type e-bv e-index imm-offset)))])])))
(define-bv-int-ref-inline bytevector-s16-ref integer-16 1)
(define-bv-int-ref-inline bytevector-u16-ref unsigned-16 1)
(define-bv-int-ref-inline bytevector-s24-ref integer-24 1)
(define-bv-int-ref-inline bytevector-u24-ref unsigned-24 1)
(define-bv-int-ref-inline bytevector-s32-ref integer-32 3)
(define-bv-int-ref-inline bytevector-u32-ref unsigned-32 3)
(define-bv-int-ref-inline bytevector-s40-ref integer-40 3)
(define-bv-int-ref-inline bytevector-u40-ref unsigned-40 3)
(define-bv-int-ref-inline bytevector-s48-ref integer-48 3)
(define-bv-int-ref-inline bytevector-u48-ref unsigned-48 3)
(define-bv-int-ref-inline bytevector-s56-ref integer-56 7)
(define-bv-int-ref-inline bytevector-u56-ref unsigned-56 7)
(define-bv-int-ref-inline bytevector-s64-ref integer-64 7)
(define-bv-int-ref-inline bytevector-u64-ref unsigned-64 7))
(let ()
(define-syntax define-bv-ieee-ref-inline
(lambda (x)
(syntax-case x ()
[(_ name type mask)
#'(define-inline 3 name
[(e-bv e-offset e-eness)
(and (or (constant unaligned-floats)
(bv-offset-okay? e-offset mask))
(constant? (lambda (x) (eq? x (constant native-endianness))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-ref #f 'type e-bv e-index imm-offset)))])])))
(define-bv-ieee-ref-inline bytevector-ieee-single-ref single-float 3)
(define-bv-ieee-ref-inline bytevector-ieee-double-ref double-float 7))
(let ()
(define-syntax define-bv-int-set!-inline
(lambda (x)
(syntax-case x ()
[(_ check-64? name type mask)
(with-syntax ([body #'(and (or (constant unaligned-integers)
(and mask (bv-offset-okay? e-offset mask)))
(constant? (lambda (x) (memq x '(big little))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(if (eq? (constant-value e-eness) (constant native-endianness))
(build-object-set! 'type e-bv e-index imm-offset e-value)
(build-swap-object-set! 'type e-bv e-index imm-offset e-value))))])
(with-syntax ([body (if (datum check-64?)
#'(and (>= (constant ptr-bits) 64) body)
#'body)])
#'(define-inline 3 name
[(e-bv e-offset e-value e-eness) body])))])))
(define-bv-int-set!-inline #f bytevector-s16-set! integer-16 1)
(define-bv-int-set!-inline #f bytevector-u16-set! unsigned-16 1)
(define-bv-int-set!-inline #f bytevector-s24-set! integer-24 #f)
(define-bv-int-set!-inline #f bytevector-u24-set! unsigned-24 #f)
(define-bv-int-set!-inline #f bytevector-s32-set! integer-32 3)
(define-bv-int-set!-inline #f bytevector-u32-set! unsigned-32 3)
(define-bv-int-set!-inline #t bytevector-s40-set! integer-40 #f)
(define-bv-int-set!-inline #t bytevector-u40-set! unsigned-40 #f)
(define-bv-int-set!-inline #t bytevector-s48-set! integer-48 #f)
(define-bv-int-set!-inline #t bytevector-u48-set! unsigned-48 #f)
(define-bv-int-set!-inline #t bytevector-s56-set! integer-56 #f)
(define-bv-int-set!-inline #t bytevector-u56-set! unsigned-56 #f)
(define-bv-int-set!-inline #t bytevector-s64-set! integer-64 7)
(define-bv-int-set!-inline #t bytevector-u64-set! unsigned-64 7))
(let ()
(define-syntax define-bv-ieee-set!-inline
(lambda (x)
(syntax-case x ()
[(_ name type mask)
#'(define-inline 3 name
[(e-bv e-offset e-value e-eness)
(and (or (constant unaligned-floats) (bv-offset-okay? e-offset mask))
(constant? (lambda (x) (eq? x (constant native-endianness))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(bind #f (e-bv e-index)
(build-object-set! 'type e-bv e-index imm-offset
(build-$real->flonum src sexpr e-value
`(quote name))))))])])))
(define-bv-ieee-set!-inline bytevector-ieee-single-set! single-float 3)
(define-bv-ieee-set!-inline bytevector-ieee-double-set! double-float 7))
(let ()
(define anyint-ref-helper
(lambda (type mask e-bv e-offset e-eness)
(and (or (constant unaligned-integers) (bv-offset-okay? e-offset mask))
(constant? (lambda (x) (memq x '(big little))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(build-object-ref (not (eq? (constant-value e-eness) (constant native-endianness)))
type e-bv e-index imm-offset)))))
(define-syntax define-bv-anyint-ref-inline
(syntax-rules ()
[(_ name type8 type16 type32 type64)
(define-inline 3 name
[(e-bv e-offset e-eness e-size)
(and (fixnum-constant? e-size)
(case (constant-value e-size)
[(1) (let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
`(seq
,e-eness
,(build-object-ref #f 'type8 e-bv e-index imm-offset)))]
[(2) (anyint-ref-helper 'type16 #b1 e-bv e-offset e-eness)]
[(4) (anyint-ref-helper 'type32 #b11 e-bv e-offset e-eness)]
[(8) (anyint-ref-helper 'type64 #b111 e-bv e-offset e-eness)]
[else #f]))])]))
(define-bv-anyint-ref-inline bytevector-sint-ref
integer-8 integer-16 integer-32 integer-64)
(define-bv-anyint-ref-inline bytevector-uint-ref
unsigned-8 unsigned-16 unsigned-32 unsigned-64))
(let ()
(define anyint-set!-helper
(lambda (type mask e-bv e-offset e-value e-eness)
(and (or (constant unaligned-integers) (bv-offset-okay? e-offset mask))
(constant? (lambda (x) (memq x '(big little))) e-eness)
(let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
(if (eq? (constant-value e-eness) (constant native-endianness))
(build-object-set! type e-bv e-index imm-offset e-value)
(build-swap-object-set! type e-bv e-index imm-offset e-value))))))
(define-syntax define-bv-anyint-set!-inline
(syntax-rules ()
[(_ name type8 type16 type32 type64)
(define-inline 3 name
[(e-bv e-offset e-value e-eness e-size)
(and (fixnum-constant? e-size)
(case (constant-value e-size)
[(1) (let-values ([(e-index imm-offset) (bv-index-offset e-offset)])
`(seq
,e-eness
,(build-object-set! 'type8 e-bv e-index imm-offset e-value)))]
[(2) (anyint-set!-helper 'type16 1 e-bv e-offset e-value e-eness)]
[(4) (anyint-set!-helper 'type32 3 e-bv e-offset e-value e-eness)]
[(8) (and (>= (constant ptr-bits) 64)
(anyint-set!-helper 'type64 7 e-bv e-offset e-value e-eness))]
[else #f]))])]))
(define-bv-anyint-set!-inline bytevector-sint-set!
integer-8 integer-16 integer-32 integer-64)
(define-bv-anyint-set!-inline bytevector-uint-set!
unsigned-8 unsigned-16 unsigned-32 unsigned-64)))
(let ()
(define (byte-count e-n)
(or (nanopass-case (L7 Expr) e-n
[(quote ,d)
(and (and (integer? d) (exact? d))
(let ([n (* d (constant string-char-bytes))])
(and (target-fixnum? n)
`(immediate ,(fix n)))))]
[else #f])
(%inline sll ,e-n ,(%constant string-char-offset))))
(define byte-offset
(lambda (e-off)
(or (nanopass-case (L7 Expr) e-off
[(quote ,d)
(and (and (integer? d) (exact? d))
(let ([n (+ (* d (constant string-char-bytes))
(constant string-data-disp))])
(and (target-fixnum? n)
`(immediate ,(fix n)))))]
[else #f])
(%inline +
,(%inline sll ,e-off ,(%constant string-char-offset))
(immediate ,(fix (constant string-data-disp)))))))
(define-inline 3 string-copy!
[(e-bv1 e-off1 e-bv2 e-off2 e-n)
(%primcall src sexpr $byte-copy! ,e-bv1 ,(byte-offset e-off1) ,e-bv2 ,(byte-offset e-off2) ,(byte-count e-n))]))
(define-inline 3 string-truncate!
[(e-str e-len)
(if (fixnum-constant? e-len)
(let ([len (constant-value e-len)])
(if (fx= len 0)
`(quote ,(string))
(bind #t (e-str)
`(seq
(set! ,(%mref ,e-str ,(constant string-type-disp))
(immediate ,(fx+ (fx* len (constant string-length-factor))
(constant type-string))))
,e-str))))
(bind #t (e-str e-len)
`(if ,(%inline eq? ,e-len (immediate 0))
(quote ,(string))
(seq
(set! ,(%mref ,e-str ,(constant string-type-disp))
,(build-type/length e-len
(constant type-string)
(constant fixnum-offset)
(constant string-length-offset)))
,e-str))))])
(let ()
(define build-string-fill
(make-build-fill (constant string-char-bytes) (constant string-data-disp)))
(let ()
(define do-make-string
(lambda (e-length e-fill)
; NB: caller must bind e-fill
(safe-assert (no-need-to-bind? #f e-fill))
(if (constant? (lambda (x) (and (fixnum? x) (fx<= 0 x 10000))) e-length)
(let ([n (constant-value e-length)])
(if (fx= n 0)
`(quote ,(string))
(let ([bytes (fx* n (constant string-char-bytes))])
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-string) bytes))])
`(seq
(set! ,(%mref ,t ,(constant string-type-disp))
(immediate ,(fx+ (fx* n (constant string-length-factor))
(constant type-string))))
,(build-string-fill t `(immediate ,bytes) e-fill))))))
(bind #t (e-length)
(let ([t-bytes (make-tmp 'tsize 'uptr)] [t-str (make-tmp 'tstr)])
`(if ,(%inline eq? ,e-length (immediate 0))
(quote ,(string))
(let ([,t-bytes ,(translate e-length
(constant fixnum-offset)
(constant string-char-offset))])
(let ([,t-str (alloc ,(make-info-alloc (constant type-typed-object) #f #f)
,(%inline logand
,(%inline + ,t-bytes
(immediate ,(fx+ (constant header-size-string)
(fx- (constant byte-alignment) 1))))
(immediate ,(- (constant byte-alignment)))))])
(seq
(set! ,(%mref ,t-str ,(constant string-type-disp))
,(build-type/length t-bytes
(constant type-string)
(constant string-char-offset)
(constant string-length-offset)))
,(build-string-fill t-str t-bytes e-fill))))))))))
(define default-fill `(immediate ,(ptr->imm #\nul)))
(define-inline 3 make-string
[(e-length) (do-make-string e-length default-fill)]
[(e-length e-fill) (bind #t (e-fill) (do-make-string e-length e-fill))])
(let ()
(define (valid-length? e-length)
(constant?
(lambda (x)
(and (or (fixnum? x) (bignum? x))
(<= 0 x (constant maximum-string-length))))
e-length))
(define-inline 2 make-string
[(e-length)
(and (valid-length? e-length)
(do-make-string e-length default-fill))]
[(e-length e-fill)
(and (valid-length? e-length)
(constant? char? e-fill)
(do-make-string e-length e-fill))])))
(define-inline 3 string-fill!
[(e-str e-fill)
`(seq
,(bind #t (e-str e-fill)
(build-string-fill e-str
(translate
(%inline logxor
,(%mref ,e-str ,(constant string-type-disp))
,(%constant type-string))
(constant string-length-offset)
(constant string-char-offset))
e-fill))
,(%constant svoid))]))
(let ()
(define build-fxvector-fill
(make-build-fill (constant ptr-bytes) (constant fxvector-data-disp)))
(meta-assert (= (constant log2-ptr-bytes) (constant fixnum-offset)))
(let ()
(define do-make-fxvector
(lambda (e-length e-fill)
; NB: caller must bind e-fill
(safe-assert (no-need-to-bind? #f e-fill))
(if (constant? (lambda (x) (and (fixnum? x) (fx<= 0 x 10000))) e-length)
(let ([n (constant-value e-length)])
(if (fx= n 0)
`(quote ,(fxvector))
(let ([bytes (fx* n (constant ptr-bytes))])
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-fxvector) bytes))])
`(seq
(set! ,(%mref ,t ,(constant fxvector-type-disp))
(immediate ,(fx+ (fx* n (constant fxvector-length-factor))
(constant type-fxvector))))
,(build-fxvector-fill t `(immediate ,bytes) e-fill))))))
(bind #t (e-length) ; fixnum length doubles as byte count
(let ([t-fxv (make-tmp 'tfxv)])
`(if ,(%inline eq? ,e-length (immediate 0))
(quote ,(fxvector))
(let ([,t-fxv (alloc ,(make-info-alloc (constant type-typed-object) #f #f)
,(%inline logand
,(%inline + ,e-length
(immediate ,(fx+ (constant header-size-fxvector)
(fx- (constant byte-alignment) 1))))
(immediate ,(- (constant byte-alignment)))))])
(seq
(set! ,(%mref ,t-fxv ,(constant fxvector-type-disp))
,(build-type/length e-length
(constant type-fxvector)
(constant fixnum-offset)
(constant fxvector-length-offset)))
,(build-fxvector-fill t-fxv e-length e-fill)))))))))
(define default-fill `(immediate ,(fix 0)))
(define-inline 3 make-fxvector
[(e-length) (do-make-fxvector e-length default-fill)]
[(e-length e-fill) (bind #t (e-fill) (do-make-fxvector e-length e-fill))])
(let ()
(define (valid-length? e-length)
(constant?
(lambda (x)
(and (or (fixnum? x) (bignum? x))
(<= 0 x (constant maximum-fxvector-length))))
e-length))
(define-inline 2 make-fxvector
[(e-length)
(and (valid-length? e-length)
(do-make-fxvector e-length default-fill))]
[(e-length e-fill)
(and (valid-length? e-length)
(constant? fixnum? e-fill)
(do-make-fxvector e-length e-fill))])))
(define-inline 3 fxvector-fill!
[(e-fxv e-fill)
`(seq
,(bind #t (e-fxv e-fill)
(build-fxvector-fill e-fxv
(translate
(%inline logxor
,(%mref ,e-fxv ,(constant fxvector-type-disp))
,(%constant type-fxvector))
(constant fxvector-length-offset)
(constant fixnum-offset))
e-fill))
,(%constant svoid))]))
(let ()
(define build-vector-fill
(make-build-fill (constant ptr-bytes) (constant vector-data-disp)))
(meta-assert (= (constant log2-ptr-bytes) (constant fixnum-offset)))
(let ()
(define do-make-vector
(lambda (e-length e-fill)
; NB: caller must bind e-fill
(safe-assert (no-need-to-bind? #f e-fill))
(if (constant? (lambda (x) (and (fixnum? x) (fx<= 0 x 10000))) e-length)
(let ([n (constant-value e-length)])
(if (fx= n 0)
`(quote ,(vector))
(let ([bytes (fx* n (constant ptr-bytes))])
(bind #t ([t (%constant-alloc type-typed-object
(fx+ (constant header-size-vector) bytes))])
`(seq
(set! ,(%mref ,t ,(constant vector-type-disp))
(immediate ,(+ (fx* n (constant vector-length-factor))
(constant type-vector))))
,(build-vector-fill t `(immediate ,bytes) e-fill))))))
(bind #t (e-length) ; fixnum length doubles as byte count
(let ([t-vec (make-tmp 'tvec)])
`(if ,(%inline eq? ,e-length (immediate 0))
(quote ,(vector))
(let ([,t-vec (alloc ,(make-info-alloc (constant type-typed-object) #f #f)
,(%inline logand
,(%inline + ,e-length
(immediate ,(fx+ (constant header-size-vector)
(fx- (constant byte-alignment) 1))))
(immediate ,(- (constant byte-alignment)))))])
(seq
(set! ,(%mref ,t-vec ,(constant vector-type-disp))
,(build-type/length e-length
(constant type-vector)
(constant fixnum-offset)
(constant vector-length-offset)))
,(build-vector-fill t-vec e-length e-fill)))))))))
(define default-fill `(immediate ,(fix 0)))
(define-inline 3 make-vector
[(e-length) (do-make-vector e-length default-fill)]
[(e-length e-fill) (bind #t (e-fill) (do-make-vector e-length e-fill))])
(let ()
(define (valid-length? e-length)
(constant?
(lambda (x) (and (target-fixnum? x) (>= x 0)))
e-length))
(define-inline 2 make-vector
[(e-length)
(and (valid-length? e-length)
(do-make-vector e-length default-fill))]
[(e-length e-fill)
(and (valid-length? e-length)
(constant? fixnum? e-fill)
(do-make-vector e-length e-fill))]))))
(let ()
(meta-assert (= (constant log2-ptr-bytes) (constant fixnum-offset)))
(define-inline 3 $make-eqhash-vector
[(e-length)
(let ([t-vec (make-tmp 'tvec)]
[t-idx (make-assigned-tmp 't-idx)]
[Ltop (make-local-label 'Ltop)])
`(let ([,t-idx ,e-length])
(if ,(%inline eq? ,t-idx (immediate 0))
(quote ,(vector))
(let ([,t-vec (alloc ,(make-info-alloc (constant type-typed-object) #f #f)
,(%inline logand
,(%inline + ,t-idx
(immediate ,(fx+ (constant header-size-vector)
(fx- (constant byte-alignment) 1))))
(immediate ,(- (constant byte-alignment)))))])
(seq
(set! ,(%mref ,t-vec ,(constant vector-type-disp))
,(build-type/length t-idx
(constant type-vector)
(constant fixnum-offset)
(constant vector-length-offset)))
(label ,Ltop
,(%seq
(set! ,t-idx ,(%inline - ,t-idx (immediate ,(fix 1))))
(set! ,(%mref ,t-vec ,t-idx ,(constant vector-data-disp)) ,t-idx)
(if ,(%inline eq? ,t-idx (immediate 0))
,t-vec
(goto ,Ltop)))))))))]))
(define-inline 2 $continuation?
[(e)
(bind #t (e)
(build-and
(%type-check mask-closure type-closure ,e)
(%type-check mask-continuation-code type-continuation-code
,(%mref
,(%inline -
,(%mref ,e ,(constant closure-code-disp))
,(%constant code-data-disp))
,(constant code-type-disp)))))])
(define-inline 3 $continuation-stack-length
[(e)
(translate (%mref ,e ,(constant continuation-stack-length-disp))
(constant fixnum-offset)
(constant log2-ptr-bytes))])
(define-inline 3 $continuation-stack-clength
[(e)
(translate (%mref ,e ,(constant continuation-stack-clength-disp))
(constant fixnum-offset)
(constant log2-ptr-bytes))])
(define-inline 3 $continuation-return-code
[(e)
(bind #t ([t (%inline +
,(%mref ,e ,(constant continuation-return-address-disp))
,(%constant return-address-toplink-disp))])
(%inline - ,t ,(%mref ,t 0)))])
(define-inline 3 $continuation-return-offset
[(e)
(build-fix
(%inline -
,(%mref
,(%mref ,e ,(constant continuation-return-address-disp))
,(constant return-address-toplink-disp))
,(%constant return-address-toplink-disp)))])
(define-inline 3 $continuation-return-livemask
[(e)
(%mref
,(%mref ,e ,(constant continuation-return-address-disp))
,(constant return-address-livemask-disp))])
(define-inline 3 $continuation-stack-ref
[(e-k e-i)
(%mref
,(%mref ,e-k ,(constant continuation-stack-disp))
,(translate e-i (constant fixnum-offset) (constant log2-ptr-bytes))
0)])
(define-inline 2 $foreign-char?
[(e)
(bind #t (e)
(build-and
(%type-check mask-char type-char ,e)
(%inline < ,e (immediate ,(ptr->imm (integer->char #x100))))))])
(define-inline 2 $foreign-wchar?
[(e)
(constant-case wchar-bits
[(16)
(bind #t (e)
(build-and
(%type-check mask-char type-char ,e)
(%inline < ,e (immediate ,(ptr->imm (integer->char #x10000))))))]
[(32) (%type-check mask-char type-char ,e)])])
(define-inline 2 $integer-8?
[(e)
(unless (fx>= (constant fixnum-bits) 8) ($oops '$integer-8? "unexpected fixnum-bits"))
(bind #t (e)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x80)))
(immediate ,(fix #x180)))))])
(define-inline 2 $integer-16?
[(e)
(unless (fx>= (constant fixnum-bits) 16) ($oops '$integer-16? "unexpected fixnum-bits"))
(bind #t (e)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x8000)))
(immediate ,(fix #x18000)))))])
(define-inline 2 $integer-24?
[(e)
(unless (fx>= (constant fixnum-bits) 24) ($oops '$integer-24? "unexpected fixnum-bits"))
(bind #t (e)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x800000)))
(immediate ,(fix #x1800000)))))])
(define-inline 2 $integer-32?
[(e)
(bind #t (e)
(if (fx>= (constant fixnum-bits) 32)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x80000000)))
(immediate ,(fix #x180000000))))
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant bignum-type-disp))])
`(if ,(%type-check mask-signed-bignum type-positive-bignum ,t)
,(build-libcall #f #f sexpr <= e `(quote #xffffffff))
,(build-and
(%type-check mask-signed-bignum type-negative-bignum ,t)
(build-libcall #f #f sexpr >= e `(quote #x-80000000)))))))))])
(define-inline 2 $integer-40?
[(e)
(bind #t (e)
(if (fx>= (constant fixnum-bits) 32)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x8000000000)))
(immediate ,(fix #x18000000000))))
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant bignum-type-disp))])
`(if ,(%type-check mask-signed-bignum type-positive-bignum ,t)
,(build-libcall #f #f sexpr <= e `(quote #xffffffffff))
,(build-and
(%type-check mask-signed-bignum type-negative-bignum ,t)
(build-libcall #f #f sexpr >= e `(quote #x-8000000000)))))))))])
(define-inline 2 $integer-48?
[(e)
(bind #t (e)
(if (fx>= (constant fixnum-bits) 32)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x800000000000)))
(immediate ,(fix #x1800000000000))))
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant bignum-type-disp))])
`(if ,(%type-check mask-signed-bignum type-positive-bignum ,t)
,(build-libcall #f #f sexpr <= e `(quote #xffffffffffff))
,(build-and
(%type-check mask-signed-bignum type-negative-bignum ,t)
(build-libcall #f #f sexpr >= e `(quote #x-800000000000)))))))))])
(define-inline 2 $integer-56?
[(e)
(bind #t (e)
(if (fx>= (constant fixnum-bits) 32)
(build-and
(%type-check mask-fixnum type-fixnum ,e)
(%inline u<
,(%inline + ,e (immediate ,(fix #x80000000000000)))
(immediate ,(fix #x180000000000000))))
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant bignum-type-disp))])
`(if ,(%type-check mask-signed-bignum type-positive-bignum ,t)
,(build-libcall #f #f sexpr <= e `(quote #xffffffffffffff))
,(build-and
(%type-check mask-signed-bignum type-negative-bignum ,t)
(build-libcall #f #f sexpr >= e `(quote #x-80000000000000)))))))))])
(define-inline 2 $integer-64?
[(e)
(when (fx>= (constant fixnum-bits) 64) ($oops '$integer-64? "unexpected fixnum-bits"))
(bind #t (e)
(build-simple-or
(%type-check mask-fixnum type-fixnum ,e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant bignum-type-disp))])
`(if ,(%type-check mask-signed-bignum type-positive-bignum ,t)
,(build-libcall #f #f sexpr <= e `(quote #xffffffffffffffff))
,(build-and
(%type-check mask-signed-bignum type-negative-bignum ,t)
(build-libcall #f #f sexpr >= e `(quote #x-8000000000000000))))))))])
(define-inline 3 char->integer
; assumes types are set up so that fixnum tag will be right after the shift
[(e-char) (build-char->integer e-char)])
(define-inline 2 char->integer
; assumes types are set up so that fixnum tag will be right after the shift
[(e-char)
(bind #t (e-char)
`(if ,(%type-check mask-char type-char ,e-char)
,(%inline srl ,e-char
(immediate ,(fx- (constant char-data-offset) (constant fixnum-offset))))
,(build-libcall #t src sexpr char->integer e-char)))])
(define-inline 3 char-
; assumes fixnum is zero
[(e1 e2)
(%inline srl
,(%inline - ,e1 ,e2)
(immediate ,(fx- (constant char-data-offset) (constant fixnum-offset))))])
(define-inline 3 integer->char
[(e-int) (build-integer->char e-int)])
(define-inline 3 boolean=?
[(e1 e2) (%inline eq? ,e1 ,e2)]
[(e1 e2 . e*) (reduce-equality src sexpr moi e1 e2 e*)])
(define-inline 3 symbol=?
[(e1 e2) (%inline eq? ,e1 ,e2)]
[(e1 e2 . e*) (reduce-equality src sexpr moi e1 e2 e*)])
(let ()
(define (go e flag)
(%inline logtest
,(%mref ,e ,(constant record-type-flags-disp))
(immediate ,(fix flag))))
(define-inline 3 record-type-opaque?
[(e) (go e (constant rtd-opaque))])
(define-inline 3 record-type-sealed?
[(e) (go e (constant rtd-sealed))])
(define-inline 3 record-type-generative?
[(e) (go e (constant rtd-generative))]))
(let ()
(define build-record?
(lambda (e)
(bind #t (e)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(bind #t ([t (%mref ,e ,(constant typed-object-type-disp))])
(build-and
(%type-check mask-record type-record ,t)
(build-not
(%inline logtest
,(%mref ,t ,(constant record-type-flags-disp))
(immediate ,(fix (constant rtd-opaque)))))))))))
(define build-sealed-isa?
(lambda (e e-rtd)
(bind #t (e)
(bind #f (e-rtd)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
(%inline eq?
,(%mref ,e ,(constant typed-object-type-disp))
,e-rtd))))))
(define build-unsealed-isa?
(lambda (e e-rtd)
(let ([t (make-assigned-tmp 't)] [Ltop (make-local-label 'Ltop)])
(bind #t (e e-rtd)
(build-and
(%type-check mask-typed-object type-typed-object ,e)
`(let ([,t ,(%mref ,e ,(constant typed-object-type-disp))])
,(build-simple-or
(%inline eq? ,t ,e-rtd)
(build-and
(%type-check mask-record type-record ,t)
`(label ,Ltop
(seq
(set! ,t ,(%mref ,t ,(constant record-type-parent-disp)))
,(build-simple-or
(%inline eq? ,t ,e-rtd)
`(if ,(%inline eq? ,t ,(%constant sfalse))
,(%constant sfalse)
(goto ,Ltop)))))))))))))
(define-inline 3 record?
[(e) (build-record? e)]
[(e e-rtd)
(if (constant? (lambda (x)
(and (record-type-descriptor? x)
(record-type-sealed? x)))
e-rtd)
(build-sealed-isa? e e-rtd)
(build-unsealed-isa? e e-rtd))])
(define-inline 2 r6rs:record?
[(e) (build-record? e)])
(define-inline 2 record?
[(e) (build-record? e)]
[(e e-rtd)
(nanopass-case (L7 Expr) e-rtd
[(quote ,d)
(and (record-type-descriptor? d)
(if (record-type-sealed? d)
(build-sealed-isa? e e-rtd)
(build-unsealed-isa? e e-rtd)))]
[else #f])])
(define-inline 2 $sealed-record?
[(e e-rtd) (build-sealed-isa? e e-rtd)])
(define-inline 2 eq-hashtable?
[(e) (let ([rtd (let () (include "hashtable-types.ss") (record-type-descriptor eq-ht))])
(let ([e-rtd `(quote ,rtd)])
(if (record-type-sealed? rtd)
(build-sealed-isa? e e-rtd)
(build-unsealed-isa? e e-rtd))))]))
(define-inline 2 gensym?
[(e)
(bind #t (e)
(build-and
(%type-check mask-symbol type-symbol ,e)
(bind #t ([t (%mref ,e ,(constant symbol-name-disp))])
`(if ,t
,(%type-check mask-pair type-pair ,t)
,(%constant strue)))))])
(let ()
(define build-make-symbol
(lambda (e-name)
(bind #t ([t (%constant-alloc type-symbol (constant size-symbol))])
(%seq
(set! ,(%mref ,t ,(constant symbol-name-disp)) ,e-name)
(set! ,(%mref ,t ,(constant symbol-value-disp)) ,(%constant sunbound))
(set! ,(%mref ,t ,(constant symbol-pvalue-disp))
(literal
,(make-info-literal #f 'library
(lookup-libspec nonprocedure-code)
(constant code-data-disp))))
(set! ,(%mref ,t ,(constant symbol-plist-disp)) ,(%constant snil))
(set! ,(%mref ,t ,(constant symbol-splist-disp)) ,(%constant snil))
(set! ,(%mref ,t ,(constant symbol-hash-disp)) ,(%constant sfalse))
,t))))
(define (go e-pname)
(bind #t ([t (%constant-alloc type-pair (constant size-pair))])
(%seq
(set! ,(%mref ,t ,(constant pair-cdr-disp)) ,e-pname)
(set! ,(%mref ,t ,(constant pair-car-disp)) ,(%constant sfalse))
,(build-make-symbol t))))
(define-inline 3 gensym
[() (build-make-symbol (%constant sfalse))]
[(e-pname) (bind #f (e-pname) (go e-pname))]
[(e-pname e-uname) #f])
(define-inline 2 gensym
[() (build-make-symbol (%constant sfalse))]
[(e-pname) (and (constant? string? e-pname) (go e-pname))]
[(e-pname e-uname) #f]))
(define-inline 3 symbol->string
[(e-sym)
(bind #t (e-sym)
(bind #t ([e-name (%mref ,e-sym ,(constant symbol-name-disp))])
`(if ,e-name
(if ,(%type-check mask-pair type-pair ,e-name)
,(%mref ,e-name ,(constant pair-cdr-disp))
,e-name)
,(%primcall #f sexpr $gensym->pretty-name ,e-sym))))])
(define-inline 3 $fxaddress
[(e) (%inline logand
,(let ([n (- (log2 (constant typemod)) (constant fixnum-offset))])
(if (> n 0) (%inline sra ,e (immediate ,n)) e))
(immediate ,(- (constant fixnum-factor))))])
(define-inline 3 $set-timer
[(e) (bind #f (e)
(bind #t ([t (build-fix (ref-reg %trap))])
`(seq
(set! ,(ref-reg %trap) ,(build-unfix e))
,t)))])
(define-inline 3 directory-separator?
[(e) (if-feature windows
(bind #t (e)
(build-simple-or
(%inline eq? ,e (immediate ,(ptr->imm #\/)))
(%inline eq? ,e (immediate ,(ptr->imm #\\)))))
(%inline eq? ,e (immediate ,(ptr->imm #\/))))])
(let ()
(define add-cdrs
(lambda (n e)
(if (fx= n 0)
e
(add-cdrs (fx- n 1) (%mref ,e ,(constant pair-cdr-disp))))))
(define-inline 3 list-ref
[(e-ls e-n)
(nanopass-case (L7 Expr) e-n
[(quote ,d)
(and (and (fixnum? d) (fx< d 4))
(%mref ,(add-cdrs d e-ls) ,(constant pair-car-disp)))]
[else #f])])
(define-inline 3 list-tail
[(e-ls e-n)
(nanopass-case (L7 Expr) e-n
[(quote ,d) (and (and (fixnum? d) (fx<= d 4)) (add-cdrs d e-ls))]
[else #f])]))
(let ()
(define (go0 src sexpr subtype)
(%primcall src sexpr $make-eq-hashtable
(immediate ,(fix (constant hashtable-default-size)))
(immediate ,(fix subtype))))
(define (go1 src sexpr e-size subtype)
(nanopass-case (L7 Expr) e-size
[(quote ,d)
; d must be a fixnum? for $hashtable-size-minlen and a
; target-machine fixnum for cross compiling
(and (and (fixnum? d) (target-fixnum? d) (fx>= d 0))
(%primcall src sexpr $make-eq-hashtable
(immediate ,(fix ($hashtable-size->minlen d)))
(immediate ,(fix subtype))))]
[else #f]))
(define-inline 3 make-eq-hashtable
[() (go0 src sexpr (constant eq-hashtable-subtype-normal))]
[(e-size) (go1 src sexpr e-size (constant eq-hashtable-subtype-normal))])
(define-inline 3 make-weak-eq-hashtable
[() (go0 src sexpr (constant eq-hashtable-subtype-weak))]
[(e-size) (go1 src sexpr e-size (constant eq-hashtable-subtype-weak))])
(define-inline 3 make-ephemeron-eq-hashtable
[() (go0 src sexpr (constant eq-hashtable-subtype-ephemeron))]
[(e-size) (go1 src sexpr e-size (constant eq-hashtable-subtype-ephemeron))]))
(let ()
(define-syntax def-put-x
(syntax-rules ()
[(_ name x-length)
(define-inline 3 name
[(e-bop e-x)
(bind #t (e-x)
(build-libcall #f src sexpr name e-bop e-x `(immediate 0)
(handle-prim #f #f 3 'x-length (list e-x))))]
[(e-bop e-x e-start)
(bind #t (e-x e-start)
(build-libcall #f src sexpr name e-bop e-x e-start
(%inline -
,(handle-prim #f #f 3 'x-length (list e-x))
,e-start)))]
[(e-bop e-x e-start e-count)
(build-libcall #f src sexpr name e-bop e-x e-start e-count)])]))
(def-put-x put-bytevector bytevector-length)
(def-put-x put-bytevector-some bytevector-length)
(def-put-x put-string string-length)
(def-put-x put-string-some string-length))
(define-inline 3 $read-time-stamp-counter
[()
(constant-case architecture
[(x86)
(%seq
; returns low-order 32 bits in eax, high-order in edx
(set! ,%eax (inline ,(make-info-kill* (reg-list %edx)) ,%read-time-stamp-counter))
,(u32xu32->ptr %edx %eax))]
[(x86_64)
(%seq
; returns low-order 32 bits in rax, high-order in rdx
(set! ,%rax (inline ,(make-info-kill* (reg-list %rdx)) ,%read-time-stamp-counter))
,(unsigned->ptr
(%inline logor ,(%inline sll ,%rdx (immediate 32)) ,%rax)
64))]
[(arm32) (unsigned->ptr (%inline read-time-stamp-counter) 32)]
[(ppc32)
(let ([t-hi (make-tmp 't-hi)])
`(let ([,t-hi (inline ,(make-info-kill* (reg-list %real-zero))
,%read-time-stamp-counter)])
,(u32xu32->ptr t-hi %real-zero)))])])
(define-inline 3 $read-performance-monitoring-counter
[(e)
(constant-case architecture
[(x86)
(%seq
(set! ,%eax (inline ,(make-info-kill* (reg-list %edx)) ,%read-performance-monitoring-counter ,(build-unfix e)))
,(u32xu32->ptr %edx %eax))]
[(x86_64)
(%seq
(set! ,%rax (inline ,(make-info-kill* (reg-list %rdx)) ,%read-performance-monitoring-counter ,(build-unfix e)))
,(unsigned->ptr
(%inline logor ,(%inline sll ,%rdx (immediate 32)) ,%rax)
64))]
[(arm32 ppc32) (unsigned->ptr (%inline read-performance-monitoring-counter ,(build-unfix e)) 32)])])
)) ; expand-primitives module
(define-pass np-place-overflow-and-trap : L9 (ir) -> L9.5 ()
(definitions
(define repeat? #f)
(define update-label!
(lambda (l oc tc)
(let ([orig-oc (local-label-overflow-check l)]
[orig-tc (local-label-trap-check l)])
(unless (and (eq? oc orig-oc) (eq? tc orig-tc))
(set! repeat? #t)
(local-label-overflow-check-set! l oc)
(local-label-trap-check-set! l tc)))))
(define combine-seq
(lambda (x y)
(case x
[(no) y]
[(yes) 'yes]
[else (if (eq? y 'no) 'maybe 'yes)])))
(define-pass strip-redundant-overflow-and-trap : (L9.5 Expr) (ir) -> (L9.5 Expr) ()
(definitions
(define-record-type goto (nongenerative) (fields label oc? tc?))
(define goto* '())
(define well-behaved-goto?
(lambda (goto)
(and (or (goto-oc? goto) (not (local-label-overflow-check (goto-label goto))))
(or (goto-tc? goto) (not (local-label-trap-check (goto-label goto))))))))
(Lvalue : Lvalue (ir oc? tc?) -> Lvalue ()
[(mref ,[e0] ,[e1] ,imm) `(mref ,e0 ,e1 ,imm)])
(Expr : Expr (ir oc? tc?) -> Expr ()
[(overflow-check ,[e #t tc? -> e]) (if oc? e `(overflow-check ,e))]
[(trap-check ,ioc ,[e oc? #t -> e]) (if tc? e `(trap-check ,(if oc? #f ioc) ,e))]
[(call ,info ,mdcl (literal ,info0) ,[e*] ...)
(guard oc? (eq? (info-literal-type info0) 'library)
(libspec-does-not-expect-headroom? (info-literal-addr info0)))
`(call ,info ,mdcl
(literal ,(make-info-literal #f 'library
(libspec->headroom-libspec (info-literal-addr info0))
0))
,e* ...)]
[(loop ,x (,x* ...) ,[body oc? #f -> body]) `(loop ,x (,x* ...) ,body)]
[(label ,l ,[body])
(local-label-overflow-check-set! l (and (not (eq? (local-label-overflow-check l) 'no)) oc?))
(local-label-trap-check-set! l (and (not (eq? (local-label-trap-check l) 'no)) tc?))
`(label ,l ,body)]
[(goto ,l) (set! goto* (cons (make-goto l oc? tc?) goto*)) ir])
(let ([ir (Expr ir #f #f)])
(and (andmap well-behaved-goto? goto*) ir)))
(define-pass insert-loop-traps : (L9 Expr) (ir) -> (L9.5 Expr) ()
(Expr : Expr (ir) -> Expr ()
[(loop ,x (,x* ...) ,[body]) `(loop ,x (,x* ...) (trap-check #f ,body))]))
(define has-no-headroom-libcall?
(lambda (e?)
(and e?
(nanopass-case (L9.5 Expr) e?
[(literal ,info)
(and (eq? (info-literal-type info) 'library)
(libspec-has-does-not-expect-headroom-version? (info-literal-addr info))
info)]
[else #f]))))
(with-output-language (L9.5 Expr)
(define request-trap-check (if (generate-interrupt-trap) 'yes 'no))
(define add-trap-check
(lambda (overflow? e)
(if (eq? request-trap-check 'yes)
`(trap-check ,overflow? ,e)
e)))))
(Lvalue : Lvalue (ir) -> Lvalue ('no 'no)
[(mref ,[e0 #f -> e0 oc0 tc0] ,[e1 #f -> e1 oc1 tc1] ,imm)
(values `(mref ,e0 ,e1 ,imm) (combine-seq oc0 oc1) (combine-seq tc0 tc1))])
(Expr : Expr (ir tail?) -> Expr ('no 'no)
[(goto ,l)
(if (local-label? l)
(values `(goto ,l) (local-label-overflow-check l) (local-label-trap-check l))
(values `(goto ,l) 'no 'no))]
[(values ,info ,[e* #f -> e* oc* tc*] ...)
(values `(values ,info ,e* ...) (fold-left combine-seq 'no oc*) (fold-left combine-seq 'no tc*))]
[(call ,info ,mdcl ,x ,[e* #f -> e* oc* tc*] ...)
(guard (uvar? x) (eq? (uvar-location x) 'loop))
(values `(call ,info ,mdcl ,x ,e* ...) (fold-left combine-seq 'no oc*) request-trap-check)]
[(call ,info ,mdcl ,e? ,[e* #f -> e* oc* tc*] ...)
(let-values ([(e? oc tc) (if e? (Expr e? #f) (values e? 'no 'no))])
; to save code space, we skip trap check for error calls under assumption trap checks will
; be made by the error handler. if not, could get a uninterruptible hard loop...c'est la vie
(define wrap-tc
(lambda (overflow? call)
(if (and (info-call-error? info)
(eq? (fold-left combine-seq tc tc*) 'no))
call
(add-trap-check overflow? call))))
(let ([noc? (eq? (fold-left combine-seq oc oc*) 'no)])
(cond
[(and (or tail? (and (info-call-error? info) (fx< (debug-level) 2))) noc?)
(let ([call `(call ,info ,mdcl ,e? ,e* ...)])
(if (info-call-pariah? info)
(values (wrap-tc #t call) 'no 'no)
(values call 'no request-trap-check)))]
[(and noc? (has-no-headroom-libcall? e?)) =>
(lambda (info0)
(safe-assert (not (libspec-does-not-expect-headroom? (info-literal-addr info0))))
(let ([call `(call ,info ,mdcl
(literal ,(make-info-literal #f 'library
(libspec->does-not-expect-headroom-libspec (info-literal-addr info0))
0))
,e* ...)])
(if (info-call-pariah? info)
(values (wrap-tc #t call) 'no 'no)
(values call 'no request-trap-check))))]
[else (let ([call `(call ,info ,mdcl ,e? ,e* ...)])
(if (info-call-pariah? info)
(values `(overflow-check ,(wrap-tc #f call)) 'no 'no)
(values call 'yes request-trap-check)))])))]
[(inline ,info ,prim ,[e* #f -> e* oc* tc*] ...)
(values `(inline ,info ,prim ,e* ...) (fold-left combine-seq 'no oc*) (fold-left combine-seq 'no tc*))]
[(alloc ,info ,[e #f -> e oc tc]) (values `(alloc ,info ,e) oc tc)]
[(loop ,x (,x* ...) ,body)
(uvar-location-set! x 'loop)
(let-values ([(body oc tc) (Expr body tail?)])
(uvar-location-set! x #f)
(values
(if (eq? tc 'yes)
`(loop ,x (,x* ...) ,(add-trap-check #t body))
`(loop ,x (,x* ...) ,body))
(if (eq? oc 'no) 'no 'yes)
'no))]
[(foreign-call ,info ,[e #f -> e oc tc] ,[e* #f -> e* oc* tc*] ...)
(values `(foreign-call ,info ,e ,e* ...) (fold-left combine-seq oc oc*) (fold-left combine-seq tc tc*))]
[(label ,l ,[body oc tc]) (update-label! l oc tc) (values `(label ,l ,body) oc tc)]
[(set! ,[lvalue -> lvalue oc0 tc0] ,[e #f -> e oc1 tc1])
(values `(set! ,lvalue ,e) (combine-seq oc0 oc1) (combine-seq tc0 tc1))]
[(mvlet ,[e #f -> e oc tc] ((,x** ...) ,interface* ,[body* oc* tc*]) ...)
; claiming mvlet always makes a nontail call
(values `(mvlet ,e ((,x** ...) ,interface* ,body*) ...) 'yes request-trap-check)]
[(mvcall ,info ,[e1 #f -> e1 oc1 tc1] ,[e2 #f -> e2 oc2 tc2])
; claiming mvcall always makes a nontail call
(values `(mvcall ,info ,e1 ,e2) 'yes request-trap-check)]
[(let ([,x* ,[e* #f -> e* oc* tc*]] ...) ,[body oc tc])
(values `(let ([,x* ,e*] ...) ,body) (fold-left combine-seq oc oc*) (fold-left combine-seq tc tc*))]
[(if ,[e0 #f -> e0 oc0 tc0] ,[e1 oc1 tc1] ,[e2 oc2 tc2])
(define combine-branch
(lambda (l r)
(case l
[(yes) (if (eq? r 'yes) 'yes 'maybe)]
[(no) (if (eq? r 'no) 'no 'maybe)]
[else l])))
(let ([oc (combine-seq oc0 (combine-branch oc1 oc2))]
[tc (combine-seq tc0 (combine-branch tc1 tc2))])
(define wrap-oc
(lambda (ocx e)
(if (and (eq? ocx 'yes) (not (eq? oc 'yes)))
`(overflow-check ,e)
e)))
(define wrap-tc
(lambda (tcx e)
(if (and (eq? tcx 'yes) (not (eq? tc 'yes)))
(add-trap-check #t e)
e)))
(values
`(if ,e0 ,(wrap-oc oc1 (wrap-tc tc1 e1)) ,(wrap-oc oc2 (wrap-tc tc2 e2)))
oc tc))]
[(seq ,[e0 #f -> e0 oc0 tc0] ,[e1 oc1 tc1])
(values `(seq ,e0 ,e1) (combine-seq oc0 oc1) (combine-seq tc0 tc1))])
(CaseLambdaClause : CaseLambdaClause (ir force-overflow?) -> CaseLambdaClause ()
[(clause (,x* ...) ,mcp ,interface ,body)
(safe-assert (not repeat?)) ; should always be initialized and/or reset to #f
`(clause (,x* ...) ,mcp ,interface
,(or (let f ()
(let-values ([(body oc tc) (Expr body #t)])
(if repeat?
(begin (set! repeat? #f) (f))
(strip-redundant-overflow-and-trap
(let ([body (if (eq? tc 'yes) (add-trap-check #t body) body)])
(if (or force-overflow? (eq? oc 'yes))
`(overflow-check ,body)
body))))))
; punting badly here under assumption that we currently can't even generate
; misbehaved gotos, i.e., paths ending in a goto that don't do an overflow
; or trap check where the target label expects it to have been done. if we
; ever violate this assumption on a regular basis, might want to revisit and
; do somthing better.
; ... test punt case by commenting out above for all but library.ss
`(overflow-check (trap-check #f ,(insert-loop-traps body)))))])
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(case-lambda ,info ,[cl* (let ([libspec (info-lambda-libspec info)])
(and libspec (libspec-does-not-expect-headroom? libspec))) -> cl*] ...)
`(case-lambda ,info ,cl* ...)]))
(define-pass np-rebind-on-ruined-path : L9.5 (ir) -> L9.5 ()
(definitions
(define prefix*)
(define add-prefix!
(lambda (x)
(when (uvar? x)
(unless (uvar-in-prefix? x)
(uvar-in-prefix! x #t)
(set! prefix* (cons x prefix*))))))
(define add-prefix*! (lambda (x*) (for-each add-prefix! x*)))
(define reset-prefix*!
(lambda (orig-prefix*)
(let loop ([ls prefix*] [diff* '()])
(if (eq? ls orig-prefix*)
(begin (set! prefix* ls) diff*)
(let ([x (car ls)])
(uvar-in-prefix! x #f)
(loop (cdr ls) (cons x diff*)))))))
(define-pass gather-refs : (L9.5 Expr) (e) -> (L9.5 Expr) (x*)
(definitions (define x*))
(Expr : Expr (ir) -> Expr ()
[,x (guard (uvar? x))
(cond
[(uvar-in-prefix? x)
(let ([t (make-tmp 't)])
(uvar-location-set! x t)
(uvar-in-prefix! x #f)
(set! x* (cons x x*))
t)]
[(uvar-location x)]
[else x])])
(fluid-let ([x* '()])
(let ([e (Expr e)])
(values e x*)))))
(Expr : Expr (ir) -> Expr ()
[(overflow-check (call ,info ,mdcl ,e? ,e* ...))
(guard (info-call-error? info))
`(overflow-check (call ,info ,mdcl ,e? ,e* ...))]
[(overflow-check ,e)
(if (null? prefix*)
`(overflow-check ,e)
(let-values ([(e x*) (gather-refs e)])
(let ([t* (map (lambda (x)
(let ([t (uvar-location x)])
(uvar-location-set! x #f)
t))
x*)])
`(let ([,t* ,x*] ...) (overflow-check ,e)))))]
[(set! ,x ,[e])
(guard (and (uvar? x) (not (uvar-assigned? x))))
(add-prefix! x)
`(set! ,x ,e)]
[(let ([,x* ,[e*]] ...) ,body)
(add-prefix*! x*)
`(let ([,x* ,e*] ...) ,(Expr body))]
[(if ,[e0] ,e1 ,e2)
(let ([orig-prefix* prefix*])
(let ([e1 (Expr e1)])
(let ([e1-diff-prefix* (reset-prefix*! orig-prefix*)])
(let ([e2 (Expr e2)])
(add-prefix*! e1-diff-prefix*)
`(if ,e0 ,e1 ,e2)))))]
[(seq ,[e0] ,e1) `(seq ,e0 ,(Expr e1))])
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ()
[(clause (,x* ...) ,mcp ,interface ,body)
(fluid-let ([prefix* x*])
`(clause (,x* ...) ,mcp ,interface ,(Expr body)))]))
(define-pass np-finalize-loops : L9.5 (ir) -> L9.75 ()
(Expr : Expr (ir) -> Expr ()
[(loop ,x (,x* ...) ,body)
(let ([Ltop (make-local-label (uvar-name x))])
(uvar-location-set! x (cons Ltop x*))
(let ([body (Expr body)])
(uvar-location-set! x #f)
`(label ,Ltop ,body)))]
[(call ,info ,mdcl ,x ,[e*] ...)
(guard (uvar-location x))
(let ([Ltop.x* (uvar-location x)])
(fold-left (lambda (body x e) `(seq (set! ,x ,e) ,body))
`(goto ,(car Ltop.x*)) (cdr Ltop.x*) e*))]))
(define-pass np-optimize-pred-in-value : L9.75 (ir) -> L9.75 ()
(definitions
(define bar
(lambda (e bool?)
(if (eq? bool? 'wrapper)
(with-output-language (L9.75 Expr)
`(if ,e ,(%constant strue) ,(%constant sfalse)))
e)))
(define dont
(lambda (e)
(with-values (Expr e #f) (lambda (e bool?) e)))))
(Value : Expr (ir) -> Expr ()
[else (with-values (Expr ir 'value) bar)])
(Lvalue : Lvalue (ir) -> Expr (#f))
(Expr : Expr (ir [value? #f]) -> Expr (#f)
[(immediate ,imm) (values ir (or (eq? imm (constant strue)) (eq? imm (constant sfalse))))]
[(set! ,lvalue ,[e]) (values `(set! ,lvalue ,e) #f)]
[(seq ,[dont : e0] ,[e1 bool?]) (values `(seq ,e0 ,e1) bool?)]
[(let ([,x* ,[e*]] ...) ,[e bool?]) (values `(let ([,x* ,e*] ...) ,e) bool?)]
[(inline ,info ,prim ,[e*] ...)
(guard (pred-primitive? prim))
(values `(inline ,info ,prim ,e* ...) #t)]
[(if ,[dont : e0] ,[e1 bool1?] ,[e2 bool2?])
(guard value?)
(if (and bool1? bool2?)
(values `(if ,e0 ,e1 ,e2) 'wrapper)
(values `(if ,e0 ,(bar e1 bool1?) ,(bar e2 bool2?)) #f))]))
(define-pass np-remove-complex-opera* : L9.75 (ir) -> L10 ()
; remove-complex-opera* cannot assume that assigned uvars and
; (mrefs at this point) are immutable. it must take this into
; account and avoid possible interleaved subexpression evaluation
; for calls and inline forms. it can do so by removing all lvalues
; as call/inline subexpressions, or it can be more selective and
; allow them to remain when doing so can't cause any problems.
; for example, (<lvalue1> <lvalue2>) can be left alone, and both
;
; ((begin e <lvalue1>) <lvalue2>) => (begin e (<lvalue1> <lvalue2>))
;
; and
;
; (<lvalue1> (begin e <lvalue2>)) => (begin e (<lvalue1> <lvalue2>))
;
; are safe transformations, but
;
; ((begin e1 <lvalue1>) (begin e2 <lvalue2>))
;
; cannot be turned into
;
; (begin e1 e2 (<lvalue1> <lvalue2>)).
;
; NB: remove-complex-opera* produces set! forms rather than let bindings
; since the former (but not the latter) can be pushed into both branches
; of an if without causing potentially exponential code growth
(definitions
(define local*)
(define make-tmp
(lambda (x)
(import (only np-languages make-tmp))
(let ([x (make-tmp x)])
(set! local* (cons x local*))
x)))
(define Ref
(lambda (ir setup*)
(if (var? ir)
(values ir setup*)
(let ([tmp (make-tmp 't)])
(values tmp (cons (Rhs ir tmp) setup*))))))
(define Lvalue?
(lambda (x)
(nanopass-case (L10 Triv) x
[,lvalue #t]
[else #f])))
(define Triv*
(lambda (e* k)
(let f ([e* e*] [lvalue-setup* '()] [rt* '()] [setup* '()])
(if (null? e*)
(build-seq* setup*
(build-seq* lvalue-setup*
(k (reverse rt*))))
(let-values ([(t t-setup*) (Triv (car e*) (null? lvalue-setup*))])
(if (and (null? lvalue-setup*)
(not (null? t-setup*))
(Lvalue? t)
; uvar's are singly assigned
(or (not (uvar? t)) (uvar-assigned? t)))
(f (cdr e*) t-setup* (cons t rt*) setup*)
(f (cdr e*) lvalue-setup* (cons t rt*) (append t-setup* setup*))))))))
(define build-seq* (lambda (x* y) (fold-right build-seq y x*)))
(with-output-language (L10 Expr)
(define build-seq (lambda (x y) `(seq ,x ,y)))
(define Rhs
(lambda (ir lvalue)
(Expr ir
(lambda (e)
(nanopass-case (L10 Expr) e
[,rhs `(set! ,lvalue ,rhs)]
[(values ,info ,t) `(set! ,lvalue ,t)]
[(values ,info ,t* ...)
; sets lvalue to void. otherwise, the lvalue we entered with (which
; might be referenced downstream) is never set and hence fails in the live
; analysis where it is live all the way out of the function.
`(seq
(call ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #t #t) #f
(literal ,(make-info-literal #t 'object '$oops (constant symbol-value-disp)))
,(%constant sfalse)
(literal ,(make-info-literal #f 'object
(format "returned ~r values to single value return context"
(length t*)) 0)))
(set! ,lvalue ,(%constant svoid)))]
[else (sorry! who "unexpected Rhs expression ~s" e)])))))))
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ()
[(clause (,x* ...) ,mcp ,interface ,body)
(fluid-let ([local* '()])
(let ([body (Expr body values)])
(safe-assert (nodups x* local*))
`(clause (,x* ...) (,local* ...) ,mcp ,interface
,body)))])
(Triv : Expr (ir lvalue-okay?) -> Triv (setup*)
[,x
(guard (or lvalue-okay? (and (uvar? x) (not (uvar-assigned? x))) (eq? x %zero)))
(values x '())]
[(mref ,e1 ,e2 ,imm)
(guard lvalue-okay?)
(let*-values ([(x1 setup*) (Ref e1 '())] [(x2 setup*) (Ref e2 setup*)])
(values (%mref ,x1 ,x2 ,imm) setup*))]
[(literal ,info) (values `(literal ,info) '())]
[(immediate ,imm) (values `(immediate ,imm) '())]
[(label-ref ,l ,offset) (values `(label-ref ,l ,offset) '())]
[(let ([,x* ,e*] ...) ,[t setup*])
(set! local* (append x* local*))
(safe-assert (nodups local*))
(values t
(fold-right
(lambda (ir lvalue setup*) (cons (Rhs ir lvalue) setup*))
setup* e* x*))]
[(seq ,[Expr : e0 values -> e0] ,[t setup*])
(values t (cons e0 setup*))]
[(pariah) (values (%constant svoid) (list (with-output-language (L10 Expr) `(pariah))))]
[else
(let ([tmp (make-tmp 't)])
(values tmp (list (Rhs ir tmp))))])
(Expr : Expr (ir k) -> Expr ()
[(inline ,info ,prim ,e1* ...)
(Triv* e1*
(lambda (t1*)
(k `(inline ,info ,prim ,t1* ...))))]
[(alloc ,info ,e)
(let-values ([(t setup*) (Triv e #t)])
(build-seq* setup* (k `(alloc ,info ,t))))]
[(call ,info ,mdcl ,e0? ,e1* ...)
(if e0?
(Triv* (cons e0? e1*) (lambda (t*) (k `(call ,info ,mdcl ,(car t*) ,(cdr t*) ...))))
(Triv* e1* (lambda (t*) (k `(call ,info ,mdcl #f ,t* ...)))))]
[(foreign-call ,info ,e0 ,e1* ...)
(Triv* (cons e0 e1*)
(lambda (t*)
(k `(foreign-call ,info ,(car t*) ,(cdr t*) ...))))]
[(values ,info ,e* ...)
(Triv* e*
(lambda (t*)
(k `(values ,info ,t* ...))))]
[(if ,[Expr : e0 values -> e0] ,[e1] ,[e2]) `(if ,e0 ,e1 ,e2)]
[(seq ,[Expr : e0 values -> e0] ,[e1]) `(seq ,e0 ,e1)]
[(set! ,lvalue ,e)
(let-values ([(lvalue setup*) (Triv lvalue #t)])
; must put lvalue setup* first to avoid potentially interleaved argument
; evaluation in, e.g.:
;
; (let ([p1 (cons 0 1)] [p2 (cons 0 2)])
; (let ([x (cons 0 3)])
; (set-car!
; (begin (set-car! x p1) (car x))
; (begin (set-car! x p2) (car x)))
; (eq? (car p1) p2)))
; ; after expand-primitives (essentially):
; => (let ([p1 (cons 0 1)] [p2 (cons 0 2)])
; (let ([x (cons 0 3)])
; (set!
; ,(%mref (begin (set! ,(%mref x 0) p1) ,(%mref x 0)) 0)
; (begin (set! ,(%mref x 0) p2) ,(%mref x 0)))
; (eq? ,(%mref p1 0) p2)))
; ; okay:
; => (let ([p1 (cons 0 1)] [p2 (cons 0 2)])
; (let ([x (cons 0 3)])
; ; setup* for lvalue:
; (set! ,(%mref x 0) p1)
; (set! t ,(%mref x 0))
; ; setup* for e
; (set! ,(%mref x 0) p2)
; (set! ,(%mref t 0) ,(%mref x 0))
; (eq? ,(%mref p1 0) p2)))
; ; not okay:
; => (let ([p1 (cons 0 1)] [p2 (cons 0 2)])
; (let ([x (cons 0 3)])
; ; setup* for e
; (set! ,(%mref x 0) p2)
; ; setup* for lvalue:
; (set! ,(%mref x 0) p1)
; (set! t ,(%mref x 0))
; (set!
; ,(%mref t 0)
; ; wrong x[0]
; ,(%mref x 0))
; (eq? ,(%mref p1 0) p2)))
(build-seq* setup*
`(seq
,(Rhs e lvalue)
,(k (%constant svoid)))))]
[(let ([,x* ,e*] ...) ,[body])
(set! local* (append x* local*))
(safe-assert (nodups local*))
(fold-left (lambda (t x e) (build-seq (Rhs e x) t)) body x* e*)]
[(mvlet ,[Expr : e values -> e] ((,x** ...) ,interface* ,[body*]) ...)
(set! local* (append (apply append x**) local*))
(safe-assert (nodups local*))
`(mvlet ,e ((,x** ...) ,interface* ,body*) ...)]
[(mvcall ,info ,[Expr : e1 values -> e1] ,e2)
(let-values ([(t2 setup*) (Triv e2 #t)])
(build-seq* setup* (k `(mvcall ,info ,e1 ,t2))))]
[(goto ,l) `(goto ,l)]
[(label ,l ,[body]) `(label ,l ,body)]
[(trap-check ,ioc ,[body]) `(trap-check ,ioc ,body)]
[(overflow-check ,[body]) `(overflow-check ,body)]
[(pariah) `(pariah)]
[(profile ,src) `(profile ,src)]
[else
(let-values ([(t setup*) (Triv ir #t)])
(build-seq* setup* (k t)))]))
(define-pass np-push-mrvs : L10 (ir) -> L10.5 ()
(definitions
(define local*)
(define make-tmp
(lambda (x)
(import (only np-languages make-tmp))
(let ([x (make-tmp x)])
(set! local* (cons x local*))
x)))
(define Mvcall
(lambda (info e consumer k)
(with-output-language (L10.5 Expr)
(nanopass-case (L10.5 Expr) e
[,t (k `(mvcall ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #f #f) #f ,consumer ,t ()))]
[(values ,info2 ,t* ...)
(k `(mvcall ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #f #f) #f ,consumer ,t* ... ()))]
[(mvcall ,info ,mdcl ,t0 ,t1* ... (,t* ...))
(k `(mvcall ,info ,mdcl ,t0 ,t1* ... (,t* ... ,consumer)))]
[(if ,e0 ,[e1] ,[e2]) `(if ,e0 ,e1 ,e2)]
[(seq ,e0 ,[e1]) `(seq ,e0 ,e1)]
[(mlabel ,[e] (,l* ,[e*]) ...) `(mlabel ,e (,l* ,e*) ...)]
[(label ,l ,[body]) `(label ,l ,body)]
[(trap-check ,ioc ,[body]) `(trap-check ,ioc ,body)]
[(overflow-check ,[body]) `(overflow-check ,body)]
[(pariah) `(pariah)]
[(profile ,src) `(profile ,src)]
[(goto ,l) `(goto ,l)]
[,rhs ; alloc, inline, foreign-call
(let ([tmp (make-tmp 't)])
`(seq
(set! ,tmp ,rhs)
,(k `(mvcall ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #f #f) #f ,consumer ,tmp ()))))]
[else ; set! & mvset
`(seq ,e ,(k `(mvcall ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #f #f) #f ,consumer ,(%constant svoid) ())))])))))
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ()
[(clause (,x* ...) (,local0* ...) ,mcp ,interface ,body)
(fluid-let ([local* local0*])
(let ([body (Expr body)])
(safe-assert (nodups x* local*))
`(clause (,x* ...) (,local* ...) ,mcp ,interface
,body)))])
(Rhs : Rhs (ir) -> Rhs ()
[(call ,info ,mdcl ,[t0?] ,[t1*] ...) `(mvcall ,info ,mdcl ,t0? ,t1* ... ())])
(Expr : Expr (ir) -> Expr ()
[(mvcall ,info ,[e] ,[t]) (Mvcall info e t values)]
[(set! ,[lvalue] (mvcall ,info ,[e] ,[t]))
(Mvcall info e t (lambda (rhs) `(set! ,lvalue ,rhs)))]
[(mvlet ,[e] ((,x** ...) ,interface* ,body*) ...)
(let ([label* (map (lambda (x) (make-local-label 'mv)) body*)])
(define Pvalues
(lambda (info t*)
(define build-assignments
(lambda (x* t* body)
(fold-left
(lambda (body x t)
; okay to drop t since it's a triv
(if (uvar-referenced? x)
`(seq (set! ,x ,t) ,body)
body))
body x* t*)))
(find-matching-clause (length t*) x** interface* label*
(lambda (x* label)
; mark label referenced so it won't be discarded
(local-label-iteration-set! label #t)
(build-assignments x* t* `(goto ,label)))
(lambda (nfixed x* label)
; mark label referenced so it won't be discarded
(local-label-iteration-set! label #t)
(let ([xfixed* (list-head x* nfixed)]
[tfixed* (list-head t* nfixed)]
[xvar (list-ref x* nfixed)]
[tvar* (list-tail t* nfixed)])
; the args are all trivs, otherwise this code would not properly build the rest
; list after all of the arguments have been evaluated (and it couldn't suppress
; the list creation when xvar is unreferenced)
(build-assignments xfixed* tfixed*
(if (uvar-referenced? xvar)
`(seq
,(if (null? tvar*)
`(set! ,xvar ,(%constant snil))
(let ([t (make-tmp 't)])
`(seq
(set! ,t ,(%constant-alloc type-pair
(fx* (constant size-pair) (length tvar*))))
,(let f ([tvar* tvar*] [offset 0])
(let ([tvar (car tvar*)] [tvar* (cdr tvar*)])
`(seq
(set! ,(%mref ,t
,(fx+ (constant pair-car-disp) offset))
,tvar)
,(if (null? tvar*)
`(seq
(set! ,(%mref ,t
,(fx+ (constant pair-cdr-disp) offset))
,(%constant snil))
(set! ,xvar ,t))
(let ([next-offset (fx+ offset (constant size-pair))])
`(seq
(set! ,(%mref ,t
,(fx+ (constant pair-cdr-disp) offset))
,(%lea ,t next-offset))
,(f tvar* next-offset))))))))))
(goto ,label))
`(goto ,label)))))
(lambda ()
(let ([src (and info (info-call-src info))] [sexpr (and info (info-call-sexpr info))])
`(seq
(pariah)
(mvcall ,(make-info-call src sexpr #f #t #t) #f
(literal ,(make-info-literal #t 'object '$oops (constant symbol-value-disp)))
,(%constant sfalse)
(literal ,(make-info-literal #f 'object "incorrect number of values received in multiple value context" 0))
())))))))
(let ([e (nanopass-case (L10.5 Expr) e
[,t (Pvalues #f (list t))]
[(values ,info ,t* ...) (Pvalues info t*)]
[(mvcall ,info ,mdcl ,t0? ,t1* ... (,t* ...))
(for-each (lambda (l) (local-label-iteration-set! l #t)) label*)
`(mvset ,info (,mdcl ,t0? ,t1* ...) (,t* ...) ((,x** ...) ,interface* ,label*) ...)]
[(if ,e0 ,[e1] ,[e2]) `(if ,e0 ,e1 ,e2)]
[(seq ,e0 ,[e1]) `(seq ,e0 ,e1)]
[(label ,l ,[body]) `(label ,l ,body)]
[(profile ,src) `(profile ,src)]
[(trap-check ,ioc ,[body]) `(trap-check ,ioc ,body)]
[(overflow-check ,[body]) `(overflow-check ,body)]
[(pariah) `(pariah)]
[(mlabel ,[e] (,l* ,[e*]) ...) `(mlabel ,e (,l* ,e*) ...)]
[(goto ,l) `(goto ,l)]
[,rhs ; alloc, inline, foreign-call
(let ([tmp (make-tmp 't)])
`(seq
(set! ,tmp ,rhs)
,(Pvalues #f (list tmp))))]
[else ; set! & mvset
`(seq ,e ,(Pvalues #f (list (%constant svoid))))])])
(let-values ([(label* body*)
(let loop ([label* label*] [body* body*] [rlabel* '()] [rbody* '()])
(if (null? label*)
(values rlabel* rbody*)
(let* ([label (car label*)])
(if (local-label-iteration label)
(begin
(local-label-iteration-set! label #f)
(loop (cdr label*) (cdr body*)
(cons label rlabel*)
(cons (Expr (car body*)) rbody*)))
(loop (cdr label*) (cdr body*) rlabel* rbody*)))))])
`(mlabel ,e (,label* ,body*) ...))))]))
(define-pass np-normalize-context : L10.5 (ir) -> L11 ()
(definitions
(define local*)
(define make-tmp
(lambda (x)
(import (only np-languages make-tmp))
(let ([x (make-tmp x)])
(set! local* (cons x local*))
x)))
(define rhs-inline
(lambda (lvalue info prim t*)
(with-output-language (L11 Effect)
(cond
[(pred-primitive? prim)
`(if (inline ,info ,prim ,t* ...)
(set! ,lvalue ,(%constant strue))
(set! ,lvalue ,(%constant sfalse)))]
[(effect-primitive? prim)
`(seq
(inline ,info ,prim ,t* ...)
(set! ,lvalue ,(%constant svoid)))]
[(not (value-primitive? prim)) ($oops who "unrecognized prim ~s" prim)]
[else `(set! ,lvalue (inline ,info ,prim ,t* ...))])))))
(CaseLambdaClause : CaseLambdaClause (ir) -> CaseLambdaClause ()
[(clause (,x* ...) (,local0* ...) ,mcp ,interface ,body)
(fluid-let ([local* local0*])
(let ([tlbody (Tail body)])
(safe-assert (nodups x* local*))
`(clause (,x* ...) (,local* ...) ,mcp ,interface ,tlbody)))])
(Pred : Expr (ir) -> Pred ()
(definitions
(define-syntax predicafy-triv
(syntax-rules ()
[(_ ?t)
`(if ,(%inline eq? ?t (immediate ,(constant sfalse)))
(false)
(true))]))
(define-syntax predicafy-rhs
(syntax-rules ()
[(_ ?rhs)
(let ([t (make-tmp 't)])
`(seq
(set! ,t ?rhs)
,(predicafy-triv ,t)))])))
[,x (predicafy-triv ,x)]
[(mref ,x1 ,x2 ,imm) (predicafy-triv ,(%mref ,x1 ,x2 ,imm))]
[(literal ,info)
(if (info-literal-indirect? info)
(predicafy-triv (literal ,info))
(if (and (eq? (info-literal-type info) 'object)
(eq? (info-literal-addr info) #f)
(eqv? (info-literal-offset info) 0))
`(false)
`(true)))]
[(immediate ,imm) (if (eqv? imm (constant sfalse)) `(false) `(true))]
[(label-ref ,l ,offset) `(true)]
[(mvcall ,info ,mdcl ,[t0?] ,[t1] ... (,[t*] ...))
(if (and (info-call-error? info) (fx< (debug-level) 2))
`(seq (tail (mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...))) (true))
(predicafy-rhs (mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...))))]
[(foreign-call ,info ,[t0] ,[t1] ...) (predicafy-rhs (foreign-call ,info ,t0 ,t1 ...))]
[(label ,l ,[pbody]) `(seq (label ,l) ,pbody)]
[(trap-check ,ioc ,[pbody]) `(seq (trap-check ,ioc) ,pbody)]
[(overflow-check ,[pbody]) `(seq (overflow-check) ,pbody)]
[(profile ,src) `(seq (profile ,src) (true))]
[(pariah) `(seq (pariah) (true))]
[(alloc ,info ,t) `(true)]
[(inline ,info ,prim ,[t*] ...)
(guard (value-primitive? prim))
(predicafy-rhs (inline ,info ,prim ,t* ...))]
[(inline ,info ,prim ,[t*] ...)
(guard (effect-primitive? prim))
`(seq (inline ,info ,prim ,t* ...) (true))]
[(inline ,info ,prim ,t* ...)
(guard (not (pred-primitive? prim)))
($oops who "unrecognized prim ~s" prim)]
[(set! ,[lvalue] (inline ,info ,prim ,[t*] ...))
`(seq ,(rhs-inline lvalue info prim t*) (true))]
[(set! ,[lvalue] (mvcall ,info ,mdcl ,[t0?] ,[t1] ... (,[t*] ...)))
(guard (info-call-error? info) (fx< (debug-level) 2))
(%seq
(tail (mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...)))
(true))]
[(set! ,[lvalue] ,[rhs]) `(seq (set! ,lvalue ,rhs) (true))]
[(mvset ,info (,mdcl ,[t0?] ,[t1] ...) (,[t*] ...) ((,x** ...) ,interface* ,l*) ...)
`(seq
(mvset ,info (,mdcl ,t0? ,t1 ...) (,t* ...) ((,x** ...) ,interface* ,l*) ...)
(true))]
[(values ,info ,t) (Pred t)]
[(values ,info ,t* ...)
`(seq (mvcall ,(make-info-call (info-call-src info) (info-call-sexpr info) #f #t #t) #f
(literal ,(make-info-literal #t 'object '$oops (constant symbol-value-disp)))
,(%constant sfalse)
(literal ,(make-info-literal #f 'object
(format "returned ~r values to single value return context"
(length t*)) 0))
())
(true))])
(Effect : Expr (ir) -> Effect ()
[,x `(nop)]
[(mref ,x1 ,x2 ,imm) `(nop)]
[(literal ,info) `(nop)]
[(immediate ,imm) `(nop)]
[(label-ref ,l ,offset) `(nop)]
[(alloc ,info ,t) `(nop)]
[(inline ,info ,prim ,[t*] ...)
(cond
[(primitive-pure? prim) `(nop)] ; TODO: do we get any of these when cp0 is run?
[(value-primitive? prim)
`(set! ,(make-tmp 'waste) (inline ,info ,prim ,t* ...))]
[(pred-primitive? prim)
`(if (inline ,info ,prim ,t* ...) (nop) (nop))]
[else `(inline ,info ,prim ,t* ...)])]
[(set! ,[lvalue] (inline ,info ,prim ,[t*] ...))
(rhs-inline lvalue info prim t*)]
[(set! ,[lvalue] (mvcall ,info ,mdcl ,[t0?] ,[t1] ... (,[t*] ...)))
(guard (info-call-error? info) (fx< (debug-level) 2))
`(tail (mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...)))]
[(label ,l ,[ebody]) `(seq (label ,l) ,ebody)]
[(trap-check ,ioc ,[ebody]) `(seq (trap-check ,ioc) ,ebody)]
[(overflow-check ,[ebody]) `(seq (overflow-check) ,ebody)]
[(profile ,src) `(profile ,src)]
[(pariah) `(pariah)]
[(mvcall ,info ,mdcl ,[t0?] ,[t1] ... (,[t*] ...))
(guard (info-call-error? info) (fx< (debug-level) 2))
`(tail (mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...)))]
[(mlabel ,[e] (,l* ,[e*]) ...)
(let ([join (make-local-label 'mjoin)])
`(seq
,(let f ([e e] [l* l*] [e* e*])
(if (null? l*)
e
(%seq ,e (goto ,join)
,(f `(seq (label ,(car l*)) ,(car e*)) (cdr l*) (cdr e*)))))
(label ,join)))]
[(values ,info ,t* ...) `(nop)])
(Tail : Expr (ir) -> Tail ()
[(inline ,info ,prim ,[t*] ...)
(guard (pred-primitive? prim))
`(if (inline ,info ,prim ,t* ...)
,(%constant strue)
,(%constant sfalse))]
[(inline ,info ,prim ,[t*] ...)
(guard (effect-primitive? prim))
`(seq (inline ,info ,prim ,t* ...) ,(%constant svoid))]
[(inline ,info ,prim ,t* ...)
(guard (not (value-primitive? prim)))
($oops who "unrecognized prim ~s" prim)]
[(set! ,[lvalue] (inline ,info ,prim ,[t*] ...))
`(seq ,(rhs-inline lvalue info prim t*) ,(%constant svoid))]
[(set! ,[lvalue] (mvcall ,info ,mdcl ,[t0?] ,[t1] ... (,[t*] ...)))
(guard (info-call-error? info) (fx< (debug-level) 2))
`(mvcall ,info ,mdcl ,t0? ,t1 ... (,t* ...))]
[(set! ,[lvalue] ,[rhs]) `(seq (set! ,lvalue ,rhs) ,(%constant svoid))]
[(mvset ,info (,mdcl ,[t0?] ,[t1] ...) (,[t*] ...) ((,x** ...) ,interface* ,l*) ...)
`(seq
(mvset ,info (,mdcl ,t0? ,t1 ...) (,t* ...) ((,x** ...) ,interface* ,l*) ...)
,(%constant svoid))]
[(label ,l ,[tlbody]) `(seq (label ,l) ,tlbody)]
[(trap-check ,ioc ,[tlbody]) `(seq (trap-check ,ioc) ,tlbody)]
[(overflow-check ,[tlbody]) `(seq (overflow-check) ,tlbody)]
[(profile ,src) `(seq (profile ,src) ,(%constant svoid))]
[(pariah) `(seq (pariah) ,(%constant svoid))]
[(mlabel ,[tl] (,l* ,[tl*]) ...)
(let f ([tl tl] [l* l*] [tl* tl*])
(if (null? l*)
tl
`(seq
(tail ,tl)
,(f `(seq (label ,(car l*)) ,(car tl*)) (cdr l*) (cdr tl*)))))]))
(define-pass np-insert-trap-check : L11 (ir) -> L11.5 ()
(Effect : Effect (ir) -> Effect ()
[(trap-check ,ioc)
`(seq
(set! ,(ref-reg %trap) ,(%inline -/eq ,(ref-reg %trap) (immediate 1)))
(if (inline ,(make-info-condition-code 'eq? #f #t) ,%condition-code)
,(%seq
(pariah)
(mvcall ,(make-info-call #f #f #f #t #f) #f
(literal ,(make-info-literal #f 'library
(if ioc
(lookup-does-not-expect-headroom-libspec event)
(lookup-libspec event))
0))
()))
(nop)))]))
(define-pass np-flatten-case-lambda : L11.5 (ir) -> L12 ()
(definitions
(define Ldoargerr (make-Ldoargerr))
(define Ldomvleterr (make-Ldomvleterr))
(define flatten-clauses
(lambda (info cl* dcl*)
(let ([libspec (info-lambda-libspec info)])
(with-output-language (L12 Tail)
(when libspec
(safe-assert (equal? (info-lambda-interface* info) (list (libspec-interface libspec))))
(if (null? (info-lambda-fv* info))
(when (libspec-closure? libspec)
($oops who "libspec claims closure needed, but no free variables for ~s" (libspec-name libspec)))
(unless (libspec-closure? libspec)
($oops who "libspec claims no closure needed, but has free variables ~s for ~s" (info-lambda-fv* info) (libspec-name libspec)))))
(if (or (info-lambda-well-known? info) libspec)
(let loop ([cl* cl*] [dcl* dcl*] [local* '()] [tlbody #f])
(if (null? cl*)
(values local* (or tlbody (%constant svoid)))
(if (or libspec (direct-call-label-referenced (car dcl*)))
(nanopass-case (L11.5 CaseLambdaClause) (car cl*)
[(clause (,x* ...) (,local1* ...) ,mcp ,interface ,tlbody1)
(loop (cdr cl*) (cdr dcl*) (maybe-cons mcp (append x* local1* local*))
(let ([tlbody1 `(entry-point (,x* ...) ,(car dcl*) ,mcp ,(Tail tlbody1))])
(if tlbody
`(seq (tail ,tlbody) ,tlbody1)
tlbody1)))])
(loop (cdr cl*) (cdr dcl*) local* tlbody))))
(let f ([cl* cl*] [dcl* dcl*])
(if (null? cl*)
(values '() `(seq (pariah) (goto ,Ldoargerr)))
(nanopass-case (L11.5 CaseLambdaClause) (car cl*)
[(clause (,x* ...) (,local* ...) ,mcp ,interface ,tlbody)
(let ([tlbody `(entry-point (,x* ...) ,(car dcl*) ,mcp ,(Tail tlbody))])
(if (fx< interface 0)
(let ([fixed-args (lognot interface)])
(let ([tlbody (if (uvar-referenced? (list-ref x* fixed-args))
`(seq (do-rest ,fixed-args) ,tlbody)
tlbody)])
(if (fx= fixed-args 0)
(values (maybe-cons mcp (append x* local*)) tlbody)
(let-values ([(next-local* next-tlbody) (f (cdr cl*) (cdr dcl*))])
(values
(maybe-cons mcp (append x* local* next-local*))
`(if ,(%inline u< ,%ac0
(immediate ,fixed-args))
,next-tlbody
,tlbody))))))
(let-values ([(next-local* next-tlbody) (f (cdr cl*) (cdr dcl*))])
(values
(maybe-cons mcp (append x* local* next-local*))
`(if ,(%inline eq? ,%ac0
(immediate ,interface))
,tlbody
,next-tlbody)))))]))))))))
(define flatten-mvclauses
(lambda (x** interface* l*)
(with-output-language (L12 Effect)
(if (null? x**)
(%seq
(pariah)
;; mverror point ensures that the call's return address
;; is in sfp[0], so the caller's frame is still
;; on the stack for error reporting and debugging
(mverror-point)
(goto ,Ldomvleterr))
(let ([x* (car x**)] [interface (car interface*)] [l (car l*)])
(let ([ebody `(mventry-point (,x* ...) ,l)])
(if (fx< interface 0)
(let ([fixed-args (lognot interface)])
(let ([ebody (if (uvar-referenced? (list-ref x* fixed-args))
`(seq (do-rest ,fixed-args) ,ebody)
ebody)])
(if (fx= fixed-args 0)
ebody
(let ([next-ebody (flatten-mvclauses (cdr x**) (cdr interface*) (cdr l*))])
`(if ,(%inline u< ,%ac0
(immediate ,fixed-args))
,next-ebody
,ebody)))))
(let ([next-ebody (flatten-mvclauses (cdr x**) (cdr interface*) (cdr l*))])
`(if ,(%inline eq? ,%ac0
(immediate ,interface))
,ebody
,next-ebody))))))))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(case-lambda ,info ,cl* ...)
(let-values ([(local* tlbody) (flatten-clauses info cl* (info-lambda-dcl* info))])
(safe-assert (nodups local*))
(info-lambda-dcl*-set! info (filter direct-call-label-referenced (info-lambda-dcl* info)))
`(lambda ,info (,local* ...) ,tlbody))])
(Tail : Tail (ir) -> Tail ())
(Effect : Effect (ir) -> Effect ()
[(mvset ,info (,mdcl ,[t0?] ,[t1] ...) (,[t*] ...) ((,x** ...) ,interface* ,l*) ...)
`(mvset ,info (,mdcl ,t0? ,t1 ...) (,t* ...) ((,x** ...) ...)
,(flatten-mvclauses x** interface* l*))]))
(define-pass np-impose-calling-conventions : L12 (ir) -> L13 ()
(definitions
(import (only asm-module asm-foreign-call asm-foreign-callable asm-enter))
(define newframe-info-for-mventry-point)
(define label-for-mverror-point)
(define Lcall-error (make-Lcall-error))
(define dcl*)
(define local*)
(define max-fv)
(define le-label)
(define-$type-check (L13 Pred))
(define make-tmp
(lambda (x)
(import (only np-languages make-tmp))
(let ([x (make-tmp x)])
(set! local* (cons x local*))
x)))
(define set-formal-registers!
(lambda (x*)
(let do-reg ([x* x*] [reg* arg-registers])
(if (or (null? x*) (null? reg*))
x*
(begin
(uvar-location-set! (car x*) (car reg*))
(do-reg (cdr x*) (cdr reg*)))))))
(define get-arg-regs
(lambda (t*)
(let f ([t* t*] [reg* arg-registers])
(if (or (null? t*) (null? reg*))
(values '() '() t*)
(let ([reg (car reg*)])
(let-values ([(reg* reg-t* frame-t*) (f (cdr t*) (cdr reg*))])
(values (cons reg reg*) (cons (car t*) reg-t*) frame-t*)))))))
(module (build-tail-call build-nontail-call build-mv-return)
(define symref?
(lambda (info)
(and (info-literal-indirect? info)
(eq? (info-literal-type info) 'object)
(let ([x (info-literal-addr info)])
(and (symbol? x)
(eqv? (info-literal-offset info) (constant symbol-value-disp))
x)))))
(define libref?
(lambda (info)
(and (not (info-literal-indirect? info))
(eq? (info-literal-type info) 'library)
(let ([x (info-literal-addr info)])
(and (libspec? x)
(eqv? (info-literal-offset info) 0)
x)))))
(define build-call
(with-output-language (L13 Tail)
(case-lambda
[(t rpl reg* fv* maybe-info mdcl)
(build-call t #f rpl reg* fv* maybe-info mdcl #f)]
[(t cploc rpl reg* fv* maybe-info mdcl consumer?)
(let ()
(define set-return-address
(lambda (tl)
(if rpl
(%seq (set! ,%ref-ret (label-ref ,rpl ,(constant size-rp-header))) ,tl)
(meta-cond
[(real-register? '%ret) (%seq (set! ,%ret ,(get-fv 0)) ,tl)]
[else tl]))))
(define finish-call
(lambda (argcnt? cp? t)
(safe-assert (not (eq? t (get-fv 0))))
(let ([live-reg* (reg-cons* %ret (if cp? (reg-cons* %cp reg*) reg*))]
[live-fv* (meta-cond
[(real-register? '%ret) fv*]
[else (cons (get-fv 0) fv*)])])
(if consumer?
`(jump ,t (,%ac0 ,live-reg* ... ,live-fv* ...))
(if argcnt?
`(seq
(set! ,%ac0 (immediate ,(fx+ (length reg*) (length fv*))))
(jump ,t (,%ac0 ,live-reg* ... ,live-fv* ...)))
`(jump ,t (,live-reg* ... ,live-fv* ...)))))))
(define direct-call
(lambda ()
(if rpl
`(joto ,mdcl (,fv* ...))
`(goto ,mdcl))))
(define normal-call
(lambda ()
(define cploc-is-cp?
(lambda ()
; cploc must be #f, an nfv, %cp or an mref tc[cp]
(meta-cond
[(real-register? '%cp) (eq? cploc %cp)]
[else (and cploc (not (var? cploc)))])))
(define-syntax set-cp
(syntax-rules ()
[(_ lhs rhs ?tl)
(let ([tl `?tl])
(if (cploc-is-cp?)
tl
`(seq (set! lhs rhs) ,tl)))]))
(define insert-procedure-check
(lambda (reg tlbody)
(if (and maybe-info (info-call-check? maybe-info))
`(if ,(%type-check mask-closure type-closure ,reg)
,tlbody
(seq (pariah) (goto ,Lcall-error)))
tlbody)))
(if mdcl
(set-cp ,(ref-reg %cp) ,(or cploc (Triv t))
,(set-return-address
(if (memq mdcl dcl*)
(direct-call)
(finish-call #f ; don't set the argcount, since it doesn't need to be checked
#t (in-context Triv `(label-ref ,mdcl 0))))))
(meta-cond
[(real-register? '%cp)
(set-cp ,%cp ,(or cploc (Triv t))
,(set-return-address ; must be set before potential jump to call-error
(insert-procedure-check %cp
(finish-call #t #t
(in-context Triv
(%mref ,%cp ,(constant closure-code-disp)))))))]
[else
`(seq
(set! ,%xp ,(or cploc (Triv t)))
,(set-cp ,(ref-reg %cp) ,%xp
,(set-return-address ; must be set before potential jump to call-error
(insert-procedure-check %xp
(finish-call #t #t
(in-context Triv
(%mref ,%xp ,(constant closure-code-disp))))))))]))))
(if (not t)
(set-return-address
(if (memq mdcl dcl*)
(direct-call)
(finish-call #f #f (in-context Triv `(label-ref ,mdcl 0)))))
(nanopass-case (L12 Triv) t
; if the expression in the cp position #f, and we have an mdcl, this is
; a hackish workaround for not having a good way to express maybe-Expr
[(literal ,info)
(cond
[(symref? info) =>
; okay to do pvalue call even if this is a consumer call since only primrefs
; come through as consumer symrefs
(lambda (sym)
(%seq
(set! ,%xp (literal ,(make-info-literal #f 'object sym 0)))
(set! ,(ref-reg %cp) ,(%mref ,%xp ,(constant symbol-value-disp)))
,(set-return-address
(finish-call #t #t
(in-context Triv
(%mref ,%xp ,(constant symbol-pvalue-disp)))))))]
[(libref? info) =>
(lambda (libspec)
(define set-cp
(lambda (tlbody)
(if (libspec-closure? libspec)
`(seq
(set! ,(ref-reg %cp) (literal ,info))
,tlbody)
tlbody)))
(set-cp
(set-return-address
(finish-call #f (libspec-closure? libspec)
(in-context Triv `(literal ,(make-info-literal #f 'library-code libspec (constant code-data-disp))))))))]
[else (normal-call)])]
[else (normal-call)])))])))
(define build-consumer-call
(lambda (tc cnfv rpl)
; haven't a clue which argument registers are live, so list 'em all.
; also haven't a clue which frame variables are live. really need a
; way to list all of them as well, but we count on there being enough
; other registers (e.g., ac0, xp) to get us from the producer return
; point to the consumer jump point.
(build-call tc cnfv rpl arg-registers '() #f #f #t)))
(define prepare-for-consumer-call
(lambda (mrvl)
(with-output-language (L13 Effect)
(let ([loc0 (if (null? arg-registers)
(in-context Lvalue (%mref ,%sfp 0))
(car arg-registers))])
(%seq
(set! ,loc0 ,%ac0)
(set! ,%ac0 (immediate 1))
(label ,mrvl))))))
(define store-cp?
(lambda (t)
(nanopass-case (L12 Triv) t
[(literal ,info) #f]
[else #t])))
(define build-nontail-call
(lambda (info mdcl t0 t1* tc* nfv** mrvl prepare-for-consumer? build-postlude)
(let-values ([(reg* reg-t* frame-t*) (get-arg-regs t1*)])
(let ([nfv* (fold-left (lambda (ls x) (cons (make-tmp 'nfv) ls)) '() frame-t*)]
[cnfv* (fold-right (lambda (x ls) (cons (and (store-cp? x) (make-tmp 'cnfv)) ls)) '() tc*)]
[rpl* (map (lambda (tc) (make-local-label 'rpl)) tc*)]
[rpl (make-local-label 'rpl)])
(let ([newframe-info (make-info-newframe (info-call-src info) (info-call-sexpr info) (reverse (remq #f cnfv*)) nfv* nfv**)])
(with-output-language (L13 Effect)
(define build-return-point
(lambda (rpl mrvl cnfv* call)
(%seq (tail ,call) (label ,rpl) (return-point ,newframe-info ,rpl ,mrvl (,(remq #f cnfv*) ...)))))
(define set-locs
(lambda (loc* t* ebody)
(fold-right
(lambda (loc t ebody)
(if loc
`(seq (set! ,loc ,(Triv t)) ,ebody)
ebody))
ebody loc* t*)))
((lambda (e) (if (info-call-pariah? info) (%seq (pariah) ,e) e))
(set-locs cnfv* tc*
(set-locs nfv* frame-t*
(set-locs reg* reg-t*
(%seq
(new-frame ,newframe-info ,rpl* ... ,rpl)
,((lambda (e)
(if prepare-for-consumer?
`(seq ,e ,(prepare-for-consumer-call mrvl))
e))
(if (null? tc*)
(build-return-point rpl mrvl cnfv*
(build-call t0 rpl reg* nfv* info mdcl))
(let ([this-mrvl (make-local-label 'mrvl)])
`(seq
,(let ([rpl (car rpl*)])
(build-return-point rpl this-mrvl cnfv*
(build-call t0 rpl reg* nfv* info mdcl)))
,(let f ([tc* tc*] [cnfv* cnfv*] [rpl* rpl*] [this-mrvl this-mrvl])
`(seq
,(prepare-for-consumer-call this-mrvl)
,(let ([tc (car tc*)] [tc* (cdr tc*)] [rpl* (cdr rpl*)] [cnfv (car cnfv*)] [cnfv* (cdr cnfv*)])
(if (null? tc*)
(build-return-point rpl mrvl cnfv*
(build-consumer-call tc cnfv rpl))
(let ([this-mrvl (make-local-label 'mrvl)])
`(seq
,(let ([rpl (car rpl*)])
(build-return-point rpl this-mrvl cnfv*
(build-consumer-call tc cnfv rpl)))
,(f tc* cnfv* rpl* this-mrvl)))))))))))
,(build-postlude newframe-info rpl))))))))))))
; NB: combine
(define build-nontail-call-for-tail-call-with-consumers
(lambda (info mdcl t0 t1* tc* nfv** mrvl prepare-for-consumer? build-postlude)
(let-values ([(reg* reg-t* frame-t*) (get-arg-regs t1*)])
(let ([nfv* (fold-left (lambda (ls x) (cons (make-tmp 'nfv) ls)) '() frame-t*)]
[cnfv* (fold-right (lambda (x ls) (cons (and (store-cp? x) (make-tmp 'cnfv)) ls)) '() tc*)]
[rpl* (map (lambda (tc) (make-local-label 'rpl)) (cdr tc*))]
[rpl (make-local-label 'rpl)])
(let ([newframe-info (make-info-newframe (info-call-src info) (info-call-sexpr info) (reverse (remq #f cnfv*)) nfv* nfv**)])
(with-output-language (L13 Effect)
(define build-return-point
(lambda (rpl mrvl cnfv* call)
(%seq (tail ,call) (label ,rpl) (return-point ,newframe-info ,rpl ,mrvl (,(remq #f cnfv*) ...)))))
(define set-locs
(lambda (loc* t* ebody)
(fold-right
(lambda (loc t ebody)
(if loc
`(seq (set! ,loc ,(Triv t)) ,ebody)
ebody))
ebody loc* t*)))
((lambda (e) (if (info-call-pariah? info) (%seq (pariah) ,e) e))
(set-locs cnfv* tc*
(set-locs nfv* frame-t*
(set-locs reg* reg-t*
(%seq
(new-frame ,newframe-info ,rpl* ... ,rpl)
,((lambda (e)
(if prepare-for-consumer?
`(seq ,e ,(prepare-for-consumer-call mrvl))
e))
(if (null? (cdr tc*))
(build-return-point rpl mrvl cnfv*
(build-call t0 rpl reg* nfv* info mdcl))
(let ([this-mrvl (make-local-label 'mrvl)])
`(seq
,(let ([rpl (car rpl*)])
(build-return-point rpl this-mrvl cnfv*
(build-call t0 rpl reg* nfv* info mdcl)))
,(let f ([tc* tc*] [cnfv* cnfv*] [rpl* rpl*] [this-mrvl this-mrvl])
`(seq
,(prepare-for-consumer-call this-mrvl)
,(let ([tc (car tc*)] [tc* (cdr tc*)] [rpl* (cdr rpl*)] [cnfv (car cnfv*)] [cnfv* (cdr cnfv*)])
(if (null? (cdr tc*))
(build-return-point rpl mrvl cnfv*
(build-consumer-call tc cnfv rpl))
(let ([this-mrvl (make-local-label 'mrvl)])
`(seq
,(let ([rpl (car rpl*)])
(build-return-point rpl this-mrvl cnfv*
(build-consumer-call tc cnfv rpl)))
,(f tc* cnfv* rpl* this-mrvl)))))))))))
,(build-postlude newframe-info (car (last-pair cnfv*))))))))))))))
(module (build-tail-call build-mv-return)
(with-output-language (L13 Tail)
(define set-locs
(lambda (loc* t* tlbody)
(fold-right
(lambda (loc t tlbody)
; omit set! for tail-frame optimization
(if (and (fv? loc) (uvar? t) (eq? (uvar-location t) loc))
tlbody
`(seq (set! ,loc ,(Triv t)) ,tlbody)))
tlbody loc* t*)))
(define build-shift-args
(lambda (info)
(with-output-language (L13 Effect)
(let ([Ltop (make-local-label 'Ltop)])
`(seq
(set! ,%ts ,(%inline - ,%ac0 (immediate ,(length arg-registers))))
(if ,(%inline <= ,%ts (immediate 0))
(nop)
,(%seq
(set! ,%xp ,(%inline + ,%sfp ,(%constant ptr-bytes)))
(set! ,%ts ,(%inline sll ,%ts ,(%constant log2-ptr-bytes)))
(set! ,%ts ,(%inline + ,%ts ,%xp))
(label ,Ltop)
(shift-arg ,%xp 0 ,info)
(set! ,%xp ,(%inline + ,%xp ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%xp ,%ts)
(nop)
(goto ,Ltop)))))))))
(define build-tail-call
(lambda (info mdcl t0 t1* tc*)
(if (null? tc*)
(let-values ([(reg* reg-t* frame-t*) (get-arg-regs t1*)])
(let ([fv* (let f ([frame-t* frame-t*] [i 0])
(if (null? frame-t*)
(begin (set! max-fv (fxmax max-fv i)) '())
(let ([i (fx+ i 1)])
(cons (get-fv i) (f (cdr frame-t*) i)))))])
(set-locs fv* frame-t*
(set-locs reg* reg-t*
(build-call t0 #f reg* fv* info mdcl)))))
(let ([tc (car (last-pair tc*))]
[mrvl (make-local-label 'mrvl)])
(if (store-cp? tc)
(%seq
,(build-nontail-call-for-tail-call-with-consumers info mdcl t0 t1* tc* '() mrvl #t
(lambda (newframe-info cnfv)
(safe-assert cnfv)
(%seq
(remove-frame ,newframe-info)
(restore-local-saves ,newframe-info)
(set! ,(ref-reg %cp) ,cnfv)
,(build-shift-args newframe-info))))
,(build-consumer-call tc (in-context Triv (ref-reg %cp)) #f))
(let ([tc* (list-head tc* (fx- (length tc*) 1))])
`(seq
,(build-nontail-call info mdcl t0 t1* tc* '() mrvl #t
(lambda (newframe-info rpl)
(%seq
(remove-frame ,newframe-info)
(restore-local-saves ,newframe-info)
,(build-shift-args newframe-info))))
,(build-consumer-call tc #f #f))))))))
(define build-mv-return
(lambda (t*)
(let-values ([(reg* reg-t* frame-t*) (get-arg-regs t*)])
(let ([fv* (let f ([frame-t* frame-t*] [i 0])
(if (null? frame-t*)
(begin (set! max-fv (fxmax max-fv i)) '())
(let ([i (fx+ i 1)])
(cons (get-fv i) (f (cdr frame-t*) i)))))])
(set-locs fv* frame-t*
(set-locs reg* reg-t*
`(seq
(set! ,%ac0 (immediate ,(length t*)))
,(meta-cond
[(real-register? '%ret)
(%seq
; must leave RA in %ret for values-error
(set! ,%ret ,(get-fv 0))
(jump ,(%mref ,%ret ,(constant return-address-mv-return-address-disp))
(,%ac0 ,%ret ,reg* ... ,fv* ...)))]
[else
(%seq
(set! ,%xp ,(get-fv 0))
(jump ,(%mref ,%xp ,(constant return-address-mv-return-address-disp))
(,%ac0 ,reg* ... ,(get-fv 0) ,fv* ...)))])))))))))))
(define-syntax do-return
(lambda (x)
(syntax-case x ()
[(k retval)
(with-implicit (k quasiquote)
#'`(seq
(set! ,%ac0 retval)
(jump ,(get-fv 0) (,%ac0))))])))
(define Ref
(lambda (x)
(when (uvar? x) (uvar-referenced! x #t))
x))
(module (build-foreign-call build-fcallable)
(with-output-language (L13 Effect)
(define build-unfix
(lambda (t)
(in-context Rhs
(%inline sra ,t ,(%constant fixnum-offset)))))
(define build-fix
(lambda (t)
(in-context Rhs
(%inline sll ,t ,(%constant fixnum-offset)))))
(define Scheme->C
; ASSUMPTIONS: ac0, ac1, and xp are not C argument registers
(lambda (type toC t)
(define ptr->integer
(lambda (width t k)
(if (fx>= (constant fixnum-bits) width)
(k (build-unfix t))
`(seq
(set! ,%ac0 ,t)
(if ,(%type-check mask-fixnum type-fixnum ,%ac0)
,(if (fx> width (constant ptr-bits))
(%seq
(set! ,%ac0 ,(build-unfix %ac0))
(if ,(%inline < ,%ac0 (immediate 0))
,(k %ac0 (in-context Rhs `(immediate -1)))
,(k %ac0 (in-context Rhs `(immediate 0)))))
(k (build-unfix %ac0)))
(seq
(set! ,%ac0
(inline
,(case width
[(32) (intrinsic-info-asmlib dofargint32 #f)]
[(64) (intrinsic-info-asmlib dofargint64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
,(if (fx> width (constant ptr-bits))
(k %ac0 (in-context Rhs (ref-reg %ac1)))
(k %ac0))))))))
(define build-u*
(lambda ()
(let ([x (make-tmp 't)])
`(seq
(set! ,x ,t)
(if ,(%inline eq? ,x ,(%constant sfalse))
,(toC (in-context Rhs `(immediate 0)))
,(toC (in-context Rhs (%lea ,x (constant bytevector-data-disp)))))))))
(define build-float
(lambda ()
(let ([x (make-tmp 't)])
`(seq
(set! ,x ,t)
,(toC x)))))
(nanopass-case (Ltype Type) type
[(fp-scheme-object) (toC t)]
[(fp-fixnum) (toC (build-unfix t))]
[(fp-u8*) (build-u*)]
[(fp-u16*) (build-u*)]
[(fp-u32*) (build-u*)]
[(fp-integer ,bits) (ptr->integer bits t toC)]
[(fp-unsigned ,bits) (ptr->integer bits t toC)]
[(fp-double-float) (build-float)]
[(fp-single-float) (build-float)]
[(fp-ftd ,ftd)
(let ([x (make-tmp 't)])
`(seq
(set! ,x ,t)
,(toC (in-context Rhs
(%mref ,x ,(constant record-data-disp))))))]
[(fp-ftd& ,ftd)
(let ([x (make-tmp 't)])
(%seq
(set! ,x ,t)
(set! ,x ,(%mref ,x ,(constant record-data-disp)))
,(toC x)))]
[else ($oops who "invalid parameter type specifier ~s" type)])))
(define Scheme->C-for-result
(lambda (type toC t)
(nanopass-case (Ltype Type) type
[(fp-void) (toC)]
[(fp-ftd& ,ftd)
;; pointer isn't received as a result, but instead passed
;; to the function as its first argument (or simulated as such)
(toC)]
[else
(Scheme->C type toC t)])))
(define C->Scheme
; ASSUMPTIONS: ac0, ac1, and xp are not C argument registers
(lambda (type fromC lvalue)
(define integer->ptr
; ac0 holds low 32-bits, ac1 holds high 32 bits, if needed
(lambda (width lvalue)
(if (fx>= (constant fixnum-bits) width)
`(set! ,lvalue ,(build-fix %ac0))
(let ([e1 (lambda (big)
(let ([x (make-tmp 't)])
(%seq
(set! ,x ,(build-fix %ac0))
(set! ,x ,(build-unfix x))
(if ,(%inline eq? ,x ,%ac0)
(set! ,lvalue ,(build-fix %ac0))
,big))))]
[e2 `(seq
(set! ,%ac0
(inline
,(case width
[(32) (intrinsic-info-asmlib dofretint32 #f)]
[(64) (intrinsic-info-asmlib dofretint64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
(set! ,lvalue ,%ac0))])
(if (fx> width (constant ptr-bits))
(let ([Lbig (make-local-label 'Lbig)] [t-ac1 (make-tmp 't-ac1)])
(let ([t-ac1 (make-tmp 't-ac1)])
`(seq
; TODO: unnecessary if ac1 is not a pseudo register
(set! ,t-ac1 ,(ref-reg %ac1))
(if (if ,(%inline < ,%ac0 (immediate 0))
,(%inline eq? ,t-ac1 (immediate -1))
,(%inline eq? ,t-ac1 (immediate 0)))
,(e1 `(goto ,Lbig))
(seq (label ,Lbig) ,e2)))))
(e1 e2))))))
(define unsigned->ptr
; ac0 holds low 32-bits, ac1 holds high 32 bits, if needed
(lambda (width lvalue)
(if (fx>= (constant fixnum-bits) width)
`(set! ,lvalue ,(build-fix %ac0))
(let ([e1 (lambda (big)
`(if ,(%inline u<
,(%constant most-positive-fixnum)
,%ac0)
,big
(set! ,lvalue ,(build-fix %ac0))))]
[e2 `(seq
(set! ,%ac0
(inline
,(case width
[(32) (intrinsic-info-asmlib dofretuns32 #f)]
[(64) (intrinsic-info-asmlib dofretuns64 #f)]
[else ($oops who "can't handle width ~s" width)])
,%asmlibcall))
(set! ,lvalue ,%ac0))])
(if (fx> width (constant ptr-bits))
(let ([Lbig (make-local-label 'Lbig)] [t-ac1 (make-tmp 't-ac1)])
(let ([t-ac1 (make-tmp 't-ac1)])
`(seq
; TODO: unnecessary if ac1 is not a pseudo register
(set! ,t-ac1 ,(ref-reg %ac1))
(if ,(%inline eq? ,t-ac1 (immediate 0))
,(e1 `(goto ,Lbig))
(seq (label ,Lbig) ,e2)))))
(e1 e2))))))
(define (alloc-fptr ftd)
(%seq
(set! ,%xp
,(%constant-alloc type-typed-object (fx* (constant ptr-bytes) 2) #f))
(set!
,(%mref ,%xp ,(constant record-type-disp))
(literal ,(make-info-literal #f 'object ftd 0)))
(set! ,(%mref ,%xp ,(constant record-data-disp)) ,%ac0)
(set! ,lvalue ,%xp)))
(nanopass-case (Ltype Type) type
[(fp-void) `(set! ,lvalue ,(%constant svoid))]
[(fp-scheme-object) (fromC lvalue)]
[(fp-fixnum)
(%seq
,(fromC %ac0)
(set! ,%ac0 ,(build-fix %ac0))
(set! ,lvalue ,%ac0))]
[(fp-u8*)
(%seq
,(fromC %ac0)
(set! ,%xp (inline ,(intrinsic-info-asmlib dofretu8* #f) ,%asmlibcall))
(set! ,lvalue ,%xp))]
[(fp-u16*)
(%seq
,(fromC %ac0)
(set! ,%xp (inline ,(intrinsic-info-asmlib dofretu16* #f) ,%asmlibcall))
(set! ,lvalue ,%xp))]
[(fp-u32*)
(%seq
,(fromC %ac0)
(set! ,%xp (inline ,(intrinsic-info-asmlib dofretu32* #f) ,%asmlibcall))
(set! ,lvalue ,%xp))]
[(fp-integer ,bits)
`(seq
,(if (fx> bits (constant ptr-bits))
(fromC %ac0 (in-context Lvalue (ref-reg %ac1)))
(fromC %ac0))
,(integer->ptr bits lvalue))]
[(fp-unsigned ,bits)
`(seq
,(if (fx> bits (constant ptr-bits))
(fromC %ac0 (in-context Lvalue (ref-reg %ac1)))
(fromC %ac0))
,(unsigned->ptr bits lvalue))]
[(fp-double-float)
(%seq
(set! ,%xp ,(%constant-alloc type-flonum (constant size-flonum) #t))
,(fromC %xp)
(set! ,lvalue ,%xp))]
[(fp-single-float)
(%seq
(set! ,%xp ,(%constant-alloc type-flonum (constant size-flonum) #t))
,(fromC %xp)
(set! ,lvalue ,%xp))]
[(fp-ftd ,ftd)
(%seq
,(fromC %ac0) ; C integer return might be wiped out by alloc
,(alloc-fptr ftd))]
[(fp-ftd& ,ftd)
(%seq
,(fromC %ac0)
,(alloc-fptr ftd))]
[else ($oops who "invalid result type specifier ~s" type)]))))
(define (pick-Scall result-type)
(nanopass-case (Ltype Type) result-type
[(fp-void) (lookup-c-entry Scall-any-results)]
[else (lookup-c-entry Scall-one-result)]))
(define build-foreign-call
(with-output-language (L13 Effect)
(lambda (info t0 t1* maybe-lvalue new-frame?)
(let ([arg-type* (info-foreign-arg-type* info)]
[result-type (info-foreign-result-type info)])
(let ([e (let-values ([(allocate c-args ccall c-res deallocate) (asm-foreign-call info)])
; NB. allocate must save tc if not callee-save, and ccall
; (not deallocate) must restore tc if not callee-save
(%seq
,(allocate)
; cp must hold our closure or our code object. we choose code object
(set! ,(%tc-ref cp) (label-ref ,le-label 0))
,(with-saved-scheme-state
(in) ; save just the required registers, e.g., %sfp
(out %ac0 %ac1 %cp %xp %yp %ts %td scheme-args extra-regs)
(fold-left (lambda (e t1 arg-type c-arg) `(seq ,(Scheme->C arg-type c-arg t1) ,e))
(ccall t0) t1* arg-type* c-args))
,(let ([e (deallocate)])
(if maybe-lvalue
(nanopass-case (Ltype Type) result-type
[(fp-ftd& ,ftd)
;; Don't actually return a value, because the result
;; was instead installed in the first argument.
`(seq (set! ,maybe-lvalue ,(%constant svoid)) ,e)]
[else
`(seq ,(C->Scheme result-type c-res maybe-lvalue) ,e)])
e))))])
(if new-frame?
(sorry! who "can't handle nontail foreign calls")
e))))))
(define build-fcallable
(with-output-language (L13 Tail)
(lambda (info self-label)
(define set-locs
(lambda (loc* t* ebody)
(fold-right
(lambda (loc t ebody)
(if loc (in-context Effect `(seq (set! ,loc ,t) ,ebody)) ebody))
ebody loc* t*)))
(let ([arg-type* (info-foreign-arg-type* info)]
[result-type (info-foreign-result-type info)])
(let ([x* (map (lambda (x) (make-tmp 't)) arg-type*)])
(let-values ([(reg* reg-x* frame-x*) (get-arg-regs x*)])
(let ([fv* (let f ([frame-x* frame-x*] [i 0])
(if (null? frame-x*)
(begin (set! max-fv (fxmax max-fv i)) '())
(let ([i (fx+ i 1)])
(cons (get-fv i) (f (cdr frame-x*) i)))))]
[cp-save (meta-cond
[(real-register? '%cp) (make-tmp 'cp)]
[else #f])])
; add 2 for the old RA and cchain
(set! max-fv (fx+ max-fv 2))
(let-values ([(c-init c-args c-result c-return) (asm-foreign-callable info)])
; c-init saves C callee-save registers and restores tc
; each of c-args sets a variable to one of the C arguments
; c-result converts C results to Scheme values
; c-return restores callee-save registers and returns to C
(%seq
,(c-init)
,(restore-scheme-state
(in %cp) ; to save and then restore just before S_call_help
(out %ac0 %ac1 %xp %yp %ts %td scheme-args extra-regs))
; need overflow check since we're effectively retroactively turning
; what was a foreign call into a Scheme non-tail call
(fcallable-overflow-check)
; leave room for the RA & c-chain
(set! ,%sfp ,(%inline + ,%sfp (immediate ,(fx* (constant ptr-bytes) 2))))
; stash %cp and restore later to make sure it's intact by the time
; that we get to S_call_help
,(meta-cond
[(real-register? '%cp) `(set! ,cp-save ,%cp)]
[else `(nop)])
; convert arguments
,(fold-left (lambda (e x arg-type c-arg) `(seq ,(C->Scheme arg-type c-arg x) ,e))
(set-locs fv* frame-x*
(set-locs (map (lambda (reg) (in-context Lvalue (%mref ,%tc ,(reg-tc-disp reg)))) reg*) reg-x*
`(set! ,%ac0 (immediate ,(length arg-type*)))))
x* arg-type* c-args)
; cookie (0) will be replaced by the procedure, so this
; needs to be a quote, not an immediate
(set! ,(ref-reg %ac1) (literal ,(make-info-literal #f 'object 0 0)))
(set! ,(ref-reg %ts) (label-ref ,self-label 0)) ; for locking
,(meta-cond
[(real-register? '%cp) `(set! ,%cp ,cp-save)]
[else `(nop)])
,(save-scheme-state
(in %ac0 %ac1 %ts %cp)
(out %xp %yp %td scheme-args extra-regs))
; Scall-{any,one}-results calls the Scheme implementation of the
; callable, locking this callable wrapper (as communicated in %ts)
; until just before returning
(inline ,(make-info-c-simple-call fv* #f (pick-Scall result-type)) ,%c-simple-call)
,(restore-scheme-state
(in %ac0)
(out %ac1 %cp %xp %yp %ts %td scheme-args extra-regs))
; assuming no use of %cp from here on that could get saved into `(%tc-ref cp)`:
,(Scheme->C-for-result result-type c-result %ac0)
,(c-return)))))))))))
(define handle-do-rest
(lambda (fixed-args offset save-asm-ra?)
(with-output-language (L13 Effect)
(let-values ([(arg reg* fv-start)
; not using interface
(let f ([arg-number fixed-args] [rl arg-registers])
(cond
[(null? rl)
(let ([fv-offset (fx+ (fx* arg-number (constant ptr-bytes)) offset)])
(values
(in-context Lvalue (%mref ,%sfp ,fv-offset))
'()
(fx+ fv-offset (constant ptr-bytes))))]
[(= arg-number 0) (values (car rl) (cdr rl) offset)]
[else (f (fx- arg-number 1) (cdr rl))]))])
; TODO: try to avoid using ts by starting at the end and coming back until ac0
; reaches k(sfp), so we can use ts and/or td as an argument register. (need one
; available for the memory-memory moves)
(let* ([Lstart (make-local-label 'Lstart)]
[Ldone (make-local-label 'Ldone)]
[bump-xp-and-store-cdr
`(seq
(set! ,%xp ,(%inline + ,%xp ,(%constant size-pair)))
(if ,(%inline eq? ,%xp ,%ac0)
(goto ,Ldone)
(set! ,(%mref ,%xp
,(fx- (constant pair-cdr-disp) (constant size-pair)))
,%xp)))])
(%seq
; set ac0 to number of rest elements
(set! ,%ac0 ,(%inline - ,%ac0 (immediate ,fixed-args)))
(if ,(%inline eq? ,%ac0 (immediate 0))
(set! ,arg ,(%constant snil))
,(%seq
; adjust & scale ac0 to size of rest list in bytes
(set! ,%ac0 ,(%inline sll ,%ac0 ,(%constant pair-shift)))
; allocate the space
(set! ,%xp (alloc ,(make-info-alloc (constant type-pair) #f save-asm-ra?) ,%ac0))
; point ac0 past end of list
(set! ,%ac0 ,(%inline + ,%ac0 ,%xp))
; store the first element
(set! ,(%mref ,%xp ,(constant pair-car-disp)) ,arg)
; store the list in the first-element's old home
(set! ,arg ,%xp)
; store remaining reg elements, then loop through frame elements
,(let f ([reg* reg*])
(%seq
,bump-xp-and-store-cdr
,(if (null? reg*)
(%seq
; set ts to start of the fram arguments
(set! ,%ts ,(%inline + ,%sfp (immediate ,fv-start)))
(label ,Lstart)
; copy next element from stack to list
(set! ,(%mref ,%xp ,(constant pair-car-disp))
,(%mref ,%ts 0))
,bump-xp-and-store-cdr
(set! ,%ts ,(%inline + ,%ts ,(%constant ptr-bytes)))
(goto ,Lstart))
(%seq
(set! ,(%mref ,%xp ,(constant pair-car-disp))
,(car reg*))
,(f (cdr reg*))))))
(label ,Ldone)
; store nil in the last cdr
(set! ,(%mref ,%xp
,(fx- (constant pair-cdr-disp) (constant size-pair)))
,(%constant snil))))))))))
(define make-named-info-lambda
(lambda (name interface)
(make-info-lambda #f #f #f interface name)))
(define make-do-rest
(lambda (fixed-args offset)
(with-output-language (L13 CaseLambdaExpr)
`(lambda ,(make-named-info-lambda 'dorest '()) 0 ()
,(asm-enter
(%seq
(check-live ,(intrinsic-entry-live* (vector-ref dorest-intrinsics fixed-args)) ...)
,(handle-do-rest fixed-args offset #t)
(asm-return ,(intrinsic-return-live* (vector-ref dorest-intrinsics fixed-args)) ...)))))))
(define frame-args-offset (constant ptr-bytes))
; TODO: commonize these procedures (as macros) outside of
; np-expand-hand-coded/np-impose-calling-conventions?
(define make-arg-opnd
(lambda (n)
(let ([regnum (length arg-registers)])
(if (fx<= n regnum)
(list-ref arg-registers (fx- n 1))
(with-output-language (L13 Lvalue)
(%mref ,%sfp
,(fx* (constant ptr-bytes) (fx- n regnum))))))))
(define do-call
(lambda (interface)
(with-output-language (L13 Tail)
(%seq
(set! ,%ac0 (immediate ,interface))
,(meta-cond
[(real-register? '%cp)
`(jump ,(%mref ,%cp ,(constant closure-code-disp))
(,%ac0 ,%cp ,(reg-cons* %ret arg-registers) ...))]
[else
(%seq
(set! ,%td ,(ref-reg %cp))
(jump ,(%mref ,%td ,(constant closure-code-disp))
(,%ac0 ,(reg-cons* %ret arg-registers) ...)))])))))
(with-output-language (L13 Effect)
(meta-cond
[(real-register? '%cp)
(define xp/cp %cp)
(define load-xp/cp `(nop))]
[else
(define xp/cp %xp)
(define load-xp/cp `(set! ,%xp ,(ref-reg %cp)))]))
(define-syntax %set-esp
(lambda (x)
(syntax-case x ()
[(k e)
(with-implicit (k quasiquote %mref ref-reg)
(if (real-register? '%esp)
; write-through to tc so %esp need not be saved when going to C
#'`(seq
(set! ,(ref-reg %esp) e)
(set! ,(%mref ,%tc ,(tc-disp %esp)) ,(ref-reg %esp)))
#'`(set! ,(ref-reg %esp) e)))])))
(define nuate-help
(lambda ()
; Since cp is not always a real register, and the mref form requires us to put a var of some sort
; in for its base, we need to move cp to to a real register. Unfortunately, there do not seem to be
; enough real registers available, since ac0 is in use through out, xp and td serve as temopraries, and
; we'd like to keep ts free to serve for memory to memory moves.
; Since this is the case, we need a temporary to put cp into when we are working with it and
; xp is the natural choice (or td or ts if we switched amongst their roles)
(with-output-language (L13 Tail)
; cont. in cp and xp/cp, arg count in ac0, stack base in sfp, old frame base in yp
(let ([Lmultishot (make-local-label 'Lmultishot)]
[Lcopy-values (make-local-label 'Lcopy-values)]
[Lcopyup-values (make-local-label 'Lcopyup-values)]
[Lcopydown-values (make-local-label 'Lcopydown-values)]
[Lcopy-stack (make-local-label 'Lcopy-stack)]
[Lreturn (make-local-label 'Lreturn)])
(%seq
(set! ,%td ,(%mref ,xp/cp ,(constant continuation-stack-clength-disp)))
(if ,(%inline eq?
,(%mref ,xp/cp ,(constant continuation-stack-length-disp))
,%td)
; length and clength match, so it is either mutlishot or shot1shot
(if ,(%inline eq? ,%td ,(%constant scaled-shot-1-shot-flag))
; shot 1-shot
,(%seq
(set! ,(ref-reg %cp) (literal ,(make-info-literal #t 'object '$oops
(constant symbol-value-disp))))
(set! ,(make-arg-opnd 1) ,(%constant sfalse))
(set! ,(make-arg-opnd 2)
(literal ,(make-info-literal #f 'object
"attempt to invoke shot one-shot continuation" 0)))
,(do-call 2))
; multishot
,(%seq
(label ,Lmultishot)
; split if clength > underflow-limit
(if (if ,(%inline > ,%td ,(%constant underflow-limit))
(true)
; resize unless stack-base + clength + size(values) <= esp
; this is conservative to save a few instructions: really need
; stack-base + clength <= esp and clength + size(values) < stack-size;
; also, size may include argument register values
; Carefully using ts again
,(%seq
(set! ,%ts ,(%inline sll ,%ac0 ,(%constant log2-ptr-bytes)))
(set! ,%ts ,(%inline + ,%ts ,%sfp))
(set! ,%ts ,(%inline + ,%ts ,%td))
,(%inline < ,(ref-reg %esp) ,%ts)))
,(%seq
,(with-saved-scheme-state
(in %ac0 %cp %xp %yp scheme-args)
(out %ac1 %ts %td extra-regs)
`(inline ,(make-info-c-simple-call #f (lookup-c-entry split-and-resize)) ,%c-simple-call))
(set! ,%td ,(%mref ,xp/cp ,(constant continuation-stack-clength-disp))))
(nop))
; (new) stack base in sfp, clength in ac1, old frame base in yp
; set up return address and stack link
(set! ,(%tc-ref stack-link) ,(%mref ,xp/cp ,(constant continuation-link-disp)))
; set %td to end of the destination area / base of stack values dest
(set! ,%td ,(%inline + ,%td ,%sfp))
; don't shift if no stack values
(if ,(%inline <= ,%ac0 (immediate ,(length arg-registers)))
(nop)
,(%seq
; set xp to old frame base
(set! ,%xp ,(ref-reg %yp))
; set sfp to stack values bytes
(set! ,%sfp ,(%inline - ,%ac0 (immediate ,(length arg-registers))))
(set! ,%sfp ,(%inline sll ,%sfp ,(%constant log2-ptr-bytes)))
; shift stack return values up or down
(if ,(%inline < ,%xp ,%td)
,(%seq
(label ,Lcopyup-values)
(set! ,%sfp ,(%inline - ,%sfp ,(%constant ptr-bytes)))
(set! ,(%mref ,%td ,%sfp ,frame-args-offset) ,(%mref ,%xp ,%sfp ,frame-args-offset))
(if ,(%inline eq? ,%sfp (immediate 0))
,(%seq
; restore for invariants below; td is already okay
,load-xp/cp
(set! ,%sfp ,(%tc-ref scheme-stack)))
(goto ,Lcopyup-values)))
,(%seq
(set! ,%sfp ,(%inline + ,%sfp ,%td))
(label ,Lcopydown-values)
(set! ,(%mref ,%td ,frame-args-offset) ,(%mref ,%xp ,frame-args-offset))
(set! ,%td ,(%inline + ,%td ,(%constant ptr-bytes)))
(set! ,%xp ,(%inline + ,%xp ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%td ,%sfp)
,(%seq
; restore for invariants below
,load-xp/cp
(set! ,%sfp ,(%tc-ref scheme-stack))
(set! ,%td ,(%inline + ,%sfp ,(%mref ,xp/cp ,(constant continuation-stack-clength-disp)))))
(goto ,Lcopydown-values))))))
; invariants: xp/cp = continuation, sfp = stack base, td = end of destination area
; set %xp to saved stack base
(set! ,%xp ,(%mref ,xp/cp ,(constant continuation-stack-disp)))
(label ,Lcopy-stack)
(if ,(%inline eq? ,%sfp ,%td)
(nop)
,(%seq
(set! ,(%mref ,%sfp 0) ,(%mref ,%xp 0))
(set! ,%sfp ,(%inline + ,%sfp ,(%constant ptr-bytes)))
(set! ,%xp ,(%inline + ,%xp ,(%constant ptr-bytes)))
(goto ,Lcopy-stack)))
,load-xp/cp
(goto ,Lreturn)))
; 1 shot
,(%seq
; treat as multishot if clength + size(values) > length
; conservative: some values may be in argument registers
; AWK - very carefully using ts here as we are out of other registers
(set! ,%ts ,(%inline sll ,%ac0 ,(%constant log2-ptr-bytes)))
(set! ,%ts ,(%inline + ,%ts ,%td))
(if ,(%inline < ,(%mref ,xp/cp ,(constant continuation-stack-length-disp)) ,%ts)
(goto ,Lmultishot)
,(%seq
; set up stack link
(set! ,(%tc-ref stack-link) ,(%mref ,xp/cp ,(constant continuation-link-disp)))
; place old stack in ac1 for now to cache him later (after we've removed
; the values, so that we have a place to store the length and link)
(set! ,(ref-reg %ac1) ,%sfp)
; grab saved stack
(set! ,%sfp ,(%mref ,xp/cp ,(constant continuation-stack-disp)))
; set up tc's scheme-stack variable
(set! ,(%tc-ref scheme-stack) ,%sfp)
; set up esp as stack-base + length - slop
(set! ,%ts ,(%inline - ,%sfp ,(%constant stack-slop)))
,(%set-esp ,(%inline + ,%ts ,(%mref ,xp/cp ,(constant continuation-stack-length-disp))))
; set up frame pointer to stack-base + current length
(set! ,%sfp ,(%inline + ,%sfp ,%td))
; bypass copy loop if no stack values
(if ,(%inline <= ,%ac0 (immediate ,(length arg-registers)))
(nop)
,(%seq
; set td to stack values bytes
(set! ,%td ,(%inline - ,%ac0 (immediate ,(length arg-registers))))
(set! ,%td ,(%inline sll ,%td ,(%constant log2-ptr-bytes)))
; set xp, td to top of stack values src, dest
(set! ,%xp ,(ref-reg %yp))
; move stack return values to top of saved stack segment
(label ,Lcopy-values)
(set! ,%td ,(%inline - ,%td ,(%constant ptr-bytes)))
(set! ,(%mref ,%sfp ,%td ,frame-args-offset) ,(%mref ,%xp ,%td ,frame-args-offset))
(if ,(%inline eq? ,%td (immediate 0))
,load-xp/cp ; need to load cp-reg, since xp is wiped out
(goto ,Lcopy-values))))
; place old stack in stack cache
(set! ,%td ,(ref-reg %ac1))
(set! ,(%mref ,%td 0) ,(%tc-ref scheme-stack-size))
(set! ,(%mref ,%td ,(constant ptr-bytes)) ,(%tc-ref stack-cache))
(set! ,(%tc-ref stack-cache) ,%td)
; set up tc's stack-size variable
(set! ,(%tc-ref scheme-stack-size) ,(%mref ,xp/cp ,(constant continuation-stack-length-disp)))
; mark continuation shot
(set! ,(%mref ,xp/cp ,(constant continuation-stack-length-disp)) ,(%constant scaled-shot-1-shot-flag))
(set! ,(%mref ,xp/cp ,(constant continuation-stack-clength-disp)) ,(%constant scaled-shot-1-shot-flag))
; return with 1 or multiple values
(label ,Lreturn)
(if ,(%inline eq? ,%ac0 (immediate 1))
,(%seq
(set! ,%ac0 ,(make-arg-opnd 1))
(jump ,(%mref ,xp/cp ,(constant continuation-return-address-disp)) (,%ac0)))
,(meta-cond
[(real-register? '%ret)
(%seq
(set! ,%ret ,(%mref ,xp/cp ,(constant continuation-return-address-disp)))
(jump ,(%mref ,%ret ,(constant return-address-mv-return-address-disp))
(,%ac0 ,%ret ,arg-registers ...)))]
[else
(let ([fv0 (get-fv 0)])
(%seq
(set! ,%xp ,(%mref ,xp/cp ,(constant continuation-return-address-disp)))
(set! ,fv0 ,%xp)
(jump ,(%mref ,%xp ,(constant return-address-mv-return-address-disp))
(,%ac0 ,arg-registers ... ,fv0))))]))))))))))))
(Program : Program (ir) -> Program ()
[(labels ([,l* ,le*] ...) ,l)
`(labels ([,l* ,(map CaseLambdaExpr le* l*)] ...) ,l)])
(CaseLambdaExpr : CaseLambdaExpr (ir l) -> CaseLambdaExpr ()
[(lambda ,info (,local0* ...) ,tlbody)
(fluid-let ([dcl* (info-lambda-dcl* info)] [max-fv 0] [local* local0*] [le-label l])
(let ([tlbody (Tail tlbody)])
(let ([local* (filter uvar-referenced? local*)])
(safe-assert (nodups local*))
(for-each (lambda (local) (uvar-location-set! local #f)) local*)
`(lambda ,info ,max-fv (,local* ...) ,tlbody))))]
[(fcallable ,info ,l)
(let ([lambda-info (make-info-lambda #f #f #f (list (length (info-foreign-arg-type* info)))
(info-foreign-name info) (constant code-flag-template))])
(fluid-let ([max-fv 0] [local* '()])
(let ([tlbody (build-fcallable info l)])
`(lambda ,lambda-info ,max-fv (,local* ...) ,tlbody))))]
[(hand-coded ,sym)
(case sym
[(dorest0) (make-do-rest 0 frame-args-offset)]
[(dorest1) (make-do-rest 1 frame-args-offset)]
[(dorest2) (make-do-rest 2 frame-args-offset)]
[(dorest3) (make-do-rest 3 frame-args-offset)]
[(dorest4) (make-do-rest 4 frame-args-offset)]
[(dorest5) (make-do-rest 5 frame-args-offset)]
[(callcc)
(let ([Ltop (make-local-label 'Ltop)])
`(lambda ,(make-named-info-lambda 'callcc '(1)) 0 ()
,(%seq
(set! ,(ref-reg %cp) ,(make-arg-opnd 1))
(set! ,%td ,(%tc-ref stack-link))
(set! ,%xp ,%td)
(label ,Ltop)
(set! ,%ac0 ,(%mref ,%xp ,(constant continuation-stack-clength-disp)))
(if ,(%inline eq?
,(%mref ,%xp ,(constant continuation-stack-length-disp))
,%ac0)
,(%seq
(set! ,%ac0
(literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow)
(fx+ (constant code-data-disp) (constant size-rp-header)))))
(if (if ,(%inline eq? ,%ref-ret ,%ac0)
,(%inline eq?
,(%mref ,%td ,(constant continuation-winders-disp))
,(%tc-ref winders))
(false))
,(%seq
(set! ,(make-arg-opnd 1) ,%td)
,(do-call 1))
,(%seq
(set! ,%xp ,(%constant-alloc type-closure (constant size-continuation)))
; TODO: remove next line once get-room preserves %td
(set! ,%td ,(%tc-ref stack-link))
(set! ,(%mref ,%xp ,(constant continuation-code-disp))
(literal ,(make-info-literal #f 'library (lookup-libspec nuate) (constant code-data-disp))))
(set! ,(%mref ,%xp ,(constant continuation-return-address-disp)) ,%ref-ret)
(set! ,(%mref ,%xp ,(constant continuation-winders-disp)) ,(%tc-ref winders))
(set! ,%ref-ret ,%ac0)
(set! ,(%mref ,%xp ,(constant continuation-link-disp)) ,%td)
(set! ,(%tc-ref stack-link) ,%xp)
(set! ,%ac0 ,(%tc-ref scheme-stack))
(set! ,(%tc-ref scheme-stack) ,%sfp)
(set! ,(%mref ,%xp ,(constant continuation-stack-disp)) ,%ac0)
(set! ,%ac0 ,(%inline - ,%sfp ,%ac0))
(set! ,(%mref ,%xp ,(constant continuation-stack-length-disp)) ,%ac0)
(set! ,(%mref ,%xp ,(constant continuation-stack-clength-disp)) ,%ac0)
(set! ,(%tc-ref scheme-stack-size) ,(%inline - ,(%tc-ref scheme-stack-size) ,%ac0))
(set! ,(make-arg-opnd 1) ,%xp)
,(do-call 1))))
,(%seq
(set! ,(%mref ,%xp ,(constant continuation-stack-length-disp)) ,%ac0)
(set! ,%xp ,(%mref ,%xp ,(constant continuation-link-disp)))
(goto ,Ltop))))))]
[(call1cc)
`(lambda ,(make-named-info-lambda 'call1cc '(1)) 0 ()
,(%seq
(set! ,(ref-reg %cp) ,(make-arg-opnd 1))
(set! ,%td ,(%tc-ref stack-link))
(set! ,%ac0
(literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow)
(fx+ (constant code-data-disp) (constant size-rp-header)))))
(if (if ,(%inline eq? ,%ref-ret ,%ac0)
,(%inline eq?
,(%mref ,%td ,(constant continuation-winders-disp))
,(%tc-ref winders))
(false))
,(%seq
(set! ,(make-arg-opnd 1) ,%td)
,(do-call 1))
,(%seq
(set! ,%xp ,(%constant-alloc type-closure (constant size-continuation)))
; TODO: remove next line once get-room preserves %td
(set! ,%td ,(%tc-ref stack-link))
(set! ,(%mref ,%xp ,(constant continuation-code-disp))
(literal ,(make-info-literal #f 'library (lookup-libspec nuate) (constant code-data-disp))))
(set! ,(%mref ,%xp ,(constant continuation-return-address-disp)) ,%ref-ret)
(set! ,(%mref ,%xp ,(constant continuation-winders-disp))
,(%tc-ref winders))
,(meta-cond
[(real-register? '%ret) `(set! ,%ret ,%ac0)]
[else `(nop)])
(set! ,(%mref ,%xp ,(constant continuation-link-disp)) ,%td)
(set! ,(%tc-ref stack-link) ,%xp)
(set! ,%ac0 ,(%tc-ref scheme-stack))
(set! ,(%mref ,%xp ,(constant continuation-stack-disp)) ,%ac0)
(set! ,(%mref ,%xp ,(constant continuation-stack-clength-disp))
,(%inline - ,%sfp ,%ac0))
; we need to get ourselves a new stack. we carve it out of the old
; one if the old one is large enough. if not, we look for one in
; the cache. if the cache is empty, we allocate a new stack.
(set! ,%sfp ,(%inline + ,%sfp (immediate ,(fx* (constant one-shot-headroom) 2))))
(if ,(%inline <= ,%sfp ,(ref-reg %esp))
,(%seq
(set! ,%sfp ,(%inline - ,%sfp ,(%constant one-shot-headroom)))
(set! ,%ac0 ,(%inline - ,%sfp ,%ac0))
(set! ,(%mref ,%xp ,(constant continuation-stack-length-disp)) ,%ac0)
(set! ,(%tc-ref scheme-stack) ,%sfp)
(set! ,(%tc-ref scheme-stack-size) ,(%inline - ,(%tc-ref scheme-stack-size) ,%ac0))
(set! ,(make-arg-opnd 1) ,%xp)
,(meta-cond
[(real-register? '%ret) `(nop)]
[else `(set! ,%ref-ret
(literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow)
(fx+ (constant code-data-disp) (constant size-rp-header)))))])
,(do-call 1))
,(%seq
; set continuation length to entire stack size
(set! ,(%mref ,%xp ,(constant continuation-stack-length-disp))
,(%tc-ref scheme-stack-size))
(set! ,%sfp ,(%tc-ref stack-cache))
(if ,(%inline eq? ,%sfp ,(%constant snil))
,(%seq
(set! ,%ac0 ,%xp)
(set! ,%xp ,(%constant-alloc typemod (constant default-stack-size)))
(set! ,%sfp ,%xp)
(set! ,(%tc-ref scheme-stack) ,%sfp)
(set! ,(%tc-ref scheme-stack-size) ,(%constant default-stack-size))
,(%set-esp ,(%inline + ,%sfp
(immediate ,(fx- (constant default-stack-size) (constant stack-slop)))))
(set! ,(make-arg-opnd 1) ,%ac0)
,(meta-cond
[(real-register? '%ret) `(nop)]
[else `(set! ,%ref-ret
(literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow)
(fx+ (constant code-data-disp) (constant size-rp-header)))))])
,(do-call 1))
,(%seq
(set! ,(%tc-ref stack-cache) ,(%mref ,%sfp ,(constant ptr-bytes))) ; next stack-segment
(set! ,%ac0 ,(%mref ,%sfp 0)) ; stack-segment size
(set! ,(%tc-ref scheme-stack) ,%sfp)
(set! ,(%tc-ref scheme-stack-size) ,%ac0)
,(%set-esp ,(%lea ,%ac0 ,%sfp (fx- (constant stack-slop))))
(set! ,(make-arg-opnd 1) ,%xp)
,(meta-cond
[(real-register? '%ret) `(nop)]
[else `(set! ,%ref-ret
(literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow)
(fx+ (constant code-data-disp) (constant size-rp-header)))))])
,(do-call 1)))))))))]
[(dounderflow)
(let ([Lret (make-local-label 'Lret)] [Lmvreturn (make-local-label 'Lmvreturn)])
`(lambda ,(make-named-info-lambda 'winder-dummy '()) 0 ()
,(%seq
; (asm align)
(label ,Lret)
(rp-header ,Lmvreturn 0 0)
(set! ,(make-arg-opnd 1) ,%ac0)
(set! ,%ac0 (immediate 1))
(label ,Lmvreturn)
(set! ,xp/cp ,(%tc-ref stack-link))
,(meta-cond
[(real-register? '%cp) `(nop)]
[else `(set! ,(ref-reg %cp) ,xp/cp)])
(set! ,(ref-reg %yp) ,%sfp)
,(nuate-help))))]
[(nuate)
(let ([info (make-named-info-lambda 'continuation '(-1))])
(info-lambda-flags-set! info (fxlogor (constant code-flag-continuation) (constant code-flag-system)))
`(lambda ,info 0 ()
,(%seq
,load-xp/cp
(if ,(%inline eq? ,(%tc-ref winders)
,(%mref ,xp/cp ,(constant continuation-winders-disp)))
,(%seq
(set! ,(ref-reg %yp) ,%sfp)
(set! ,%sfp ,(%tc-ref scheme-stack))
,(nuate-help))
,(%seq
(if ,(%inline eq? ,%ac0 (immediate 0))
(set! ,%xp ,(%constant snil))
,(%seq
,(handle-do-rest 0 frame-args-offset #f)
(set! ,%xp ,(make-arg-opnd 1))))
(set! ,%sfp ,(%tc-ref scheme-stack))
(set! ,(make-arg-opnd 2) ,%xp)
(set! ,(make-arg-opnd 1) ,(ref-reg %cp))
(jump (literal ,(make-info-literal #f 'library-code
(lookup-libspec dounderflow*)
(constant code-data-disp)))
(,(reg-cons* %cp arg-registers) ...)))))))]
[else `(hand-coded ,sym)])])
(Lvalue : Lvalue (ir) -> Lvalue ()
[,x (Ref x)]
[(mref ,x1 ,x2 ,imm) (%mref ,(Ref x1) ,(Ref x2) ,imm)])
(Triv : Triv (ir) -> Triv ()
[,x (Ref x)] ; TODO: cannot call ref in cata, as we don't allow top-level cata
[(mref ,x1 ,x2 ,imm) (%mref ,(Ref x1) ,(Ref x2) ,imm)])
(Rhs : Rhs (ir) -> Rhs ()
[(mvcall ,info ,mdcl ,t0? ,t1* ... (,t* ...))
($oops who "Effect is responsible for handling mvcalls")])
(Effect : Effect (ir) -> Effect ()
[(do-rest ,fixed-args)
(if (fx<= fixed-args dorest-intrinsic-max)
`(inline ,(intrinsic-info-asmlib (vector-ref dorest-intrinsics fixed-args) #f) ,%asmlibcall!)
(handle-do-rest fixed-args frame-args-offset #f))]
; TODO: get internal error when , is missing from ,l
[(mventry-point (,x* ...) ,l)
(%seq
(remove-frame ,newframe-info-for-mventry-point)
,(let f ([x* x*])
(if (null? x*)
(%seq
(restore-local-saves ,newframe-info-for-mventry-point)
(goto ,l))
(let ([x (car x*)])
(if (uvar-referenced? x)
`(seq (set! ,x ,(uvar-location x)) ,(f (cdr x*)))
(f (cdr x*)))))))]
[(mverror-point)
`(set! ,%ref-ret (label-ref ,label-for-mverror-point ,(constant size-rp-header)))]
[(mvcall ,info ,mdcl ,t0? ,t1* ... (,t* ...))
(let ([mrvl (make-local-label 'mrvl)])
(build-nontail-call info mdcl t0? t1* t* '() mrvl #f
(lambda (newframe-info rpl)
(%seq (label ,mrvl) (remove-frame ,newframe-info) (restore-local-saves ,newframe-info)))))]
[(mvset ,info (,mdcl ,t0? ,t1* ...) (,t* ...) ((,x** ...) ...) ,ebody)
(let* ([frame-x** (map (lambda (x*) (set-formal-registers! x*)) x**)]
[nfv** (map (lambda (x*) (map (lambda (x)
(let ([nfv (make-tmp 'mvset-nfv)])
(uvar-location-set! x nfv)
nfv))
x*))
frame-x**)])
(let ([mrvl (make-local-label 'mrvl)])
(build-nontail-call info mdcl t0? t1* t* nfv** mrvl #t
(lambda (newframe-info rpl)
(fluid-let ([newframe-info-for-mventry-point newframe-info]
[label-for-mverror-point rpl])
(Effect ebody))))))]
[(set! ,[lvalue] (mvcall ,info ,mdcl ,t0? ,t1* ... (,t* ...)))
(build-nontail-call info mdcl t0? t1* t* '() #f #f
(lambda (newframe-info rpl)
(let ([retval (make-tmp 'retval)])
(%seq
(remove-frame ,newframe-info)
(set! ,retval ,%ac0)
(restore-local-saves ,newframe-info)
(set! ,lvalue ,retval)))))]
[(foreign-call ,info ,[t0] ,[t1*] ...)
(build-foreign-call info t0 t1* #f #t)]
[(set! ,[lvalue] (foreign-call ,info ,[t0] ,[t1*] ...))
(build-foreign-call info t0 t1* lvalue #t)])
(Tail : Tail (ir) -> Tail ()
[(entry-point (,x* ...) ,dcl ,mcp ,tlbody)
(unless (andmap (lambda (x) (eq? (uvar-type x) 'ptr)) x*)
($oops who "can't handle anything but plain vanilla types yet"))
; clear and recompute referenced flags on entry-point formals in case tail-frame
; optimization eliminates all of the references
(when mcp (uvar-referenced! mcp #f))
(for-each (lambda (x) (uvar-referenced! x #f)) x*)
(let do-frame ([x* (set-formal-registers! x*)] [fv-idx 1])
(unless (null? x*)
(let ([x (car x*)] [fv (get-fv fv-idx)])
(uvar-location-set! x fv)
(do-frame (cdr x*) (fx+ fv-idx 1)))))
(let ()
(define bind-formals
(lambda (mcp x* tlbody)
(define add-cpset
(lambda (mcp tlbody)
(if (and mcp (uvar-referenced? mcp)) `(seq (set! ,mcp ,(ref-reg %cp)) ,tlbody) tlbody)))
; we set cp after registers and before frame vars, since it might
; or might not be a register
(let f ([x* x*] [mcp mcp])
(if (null? x*)
(add-cpset mcp tlbody)
(let ([x (car x*)])
(if (uvar-referenced? x)
(let ([loc (uvar-location x)])
(if (fv? loc)
(begin
(set! max-fv (fxmax max-fv (fv-offset loc)))
(add-cpset mcp `(seq (set! ,x ,loc) ,(f (cdr x*) #f))))
`(seq (set! ,x ,loc) ,(f (cdr x*) mcp))))
(f (cdr x*) mcp)))))))
(let ([tlbody (Tail tlbody)])
(%seq
(label ,dcl)
; TODO: don't want to save ret for leaf routines
; TODO: don't necessarily want to position ret save here
,(meta-cond
[(real-register? '%ret) `(set! ,(get-fv 0) ,%ret)]
[else `(nop)])
(overflood-check)
,(bind-formals mcp x* tlbody))))]
[(mvcall ,info ,mdcl ,t0? ,t1* ... (,t* ...))
(build-tail-call info mdcl t0? t1* t*)]
[(foreign-call ,info ,[t0] ,[t1*] ...)
`(seq
; CAUTION: fv0 must hold return address when we call into C
,(build-foreign-call info t0 t1* %ac0 #f)
(jump ,(get-fv 0) (,%ac0)))]
[,rhs (do-return ,(Rhs ir))]
[(values ,info ,[t]) (do-return ,t)]
[(values ,info ,t* ...) (build-mv-return t*)]))
(define-pass np-expand-hand-coded : L13 (ir) -> L13.5 ()
(definitions
(import (only asm-module asm-enter))
(define Ldoargerr (make-Ldoargerr))
(define-$type-check (L13.5 Pred))
(define make-info
(lambda (name interface*)
(make-info-lambda #f #f #f interface* name)))
(define make-arg-opnd
(lambda (n)
(let ([regnum (length arg-registers)])
(if (fx<= n regnum)
(list-ref arg-registers (fx- n 1))
(with-output-language (L13.5 Lvalue)
(%mref ,%sfp
,(fx* (constant ptr-bytes) (fx- n regnum))))))))
(define do-call
(lambda ()
(with-output-language (L13.5 Tail)
(meta-cond
[(real-register? '%cp)
`(jump ,(%mref ,%cp ,(constant closure-code-disp))
(,%ac0 ,%cp ,(reg-cons* %ret arg-registers) ...))]
[else
(%seq
(set! ,%td ,(ref-reg %cp))
(jump ,(%mref ,%td ,(constant closure-code-disp))
(,%ac0 ,(reg-cons* %ret arg-registers) ...)))]))))
(define (make-list*-procedure name)
(with-output-language (L13.5 CaseLambdaExpr)
(let ([Ltop (make-local-label 'ltop)])
`(lambda ,(make-info name '(-2)) 0 ()
(seq
(set! ,%ac0 ,(%inline - ,%ac0 (immediate 1)))
; TODO: would be nice to avoid cmpl here
(if ,(%inline eq? ,%ac0 (immediate 0))
(seq
(set! ,%ac0 ,(make-arg-opnd 1))
(jump ,%ref-ret (,%ac0)))
; TODO: would be nice to avoid second cmpl here
(if ,(%inline < ,%ac0 (immediate 0))
(seq (pariah) (goto ,Ldoargerr))
,(%seq
(set! ,%ac0 ,(%inline sll ,%ac0 ,(%constant pair-shift)))
(set! ,%xp (alloc ,(make-info-alloc (constant type-pair) #f #f) ,%ac0))
,(let f ([reg* arg-registers] [i 0])
(if (null? reg*)
; filled in first i pairs
; have at least two stack arguments
; ac0 is at least (i+1) * pair-size; also amount allocated
(%seq
; point xp to last pair of list
(set! ,%xp
,(%lea ,%xp ,%ac0
(fx- (constant size-pair))))
; adjust from two ptrs per pair to one ptr per stack element
(set! ,%ac0
,(%inline srl ,%ac0 (immediate 1)))
; point ac0 to second-to-last stack argument
(set! ,%ac0
,(%lea ,%sfp ,%ac0
(fx* i (fx- (constant ptr-bytes)))))
(set! ,(%mref ,%xp ,(constant pair-cdr-disp))
,(%mref ,%ac0 ,(constant ptr-bytes)))
(label ,Ltop)
(set! ,(%mref ,%xp ,(constant pair-car-disp))
,(%mref ,%ac0 0))
(set! ,%ac0 ,(%inline - ,%ac0 ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%ac0 ,%sfp)
,(%seq
(set! ,%ac0 ,(%inline - ,%xp (immediate ,(fx* i (constant size-pair)))))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,(%mref ,%xp ,(fx- (constant pair-cdr-disp) (constant size-pair)))
,%xp)
(set! ,%xp ,(%inline - ,%xp ,(%constant size-pair)))
(goto ,Ltop))))
(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-car-disp)))
,(car reg*))
(if ,(%inline eq? ,%ac0 (immediate ,(fx* (fx+ i 1) (constant size-pair))))
,(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-cdr-disp)))
,(make-arg-opnd (fx+ i 2)))
(set! ,%ac0 ,%xp)
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-cdr-disp)))
,(%inline + ,%xp
(immediate ,(fx* (fx+ i 1) (constant size-pair)))))
,(f (cdr reg*) (fx+ i 1)))))))))))))))
(module (make-do/call make-do/ret)
(define make-do
(lambda (enter e)
; ret-loc is relevant only on machines with %ret reg:
; #f => ret is known to be at sfp[0]---no need to save or restore
; non-#f => save and restore to/from ret-loc
; if C needs to know about or might change the return address, ret-loc
; must be either #f or sfp[0]. otherwise, it can be (%tc-ref ret), which
; is useful if we don't know if %ret holds the return address. in that case,
; saving %ret to (%tc-ref ret) does no harm, nor does restoring it
; from there, but it might be harmful to save %ret to sfp[0], since %ret's
; contents are unknown.
(lambda (ret-loc name entry)
(with-output-language (L13.5 CaseLambdaExpr)
`(lambda ,(make-info name '()) 0 ()
,(enter
(%seq
,(meta-cond
[(real-register? '%ret) (if ret-loc `(set! ,ret-loc ,%ret) `(nop))]
[else `(nop)])
,(with-saved-scheme-state
(in %ac0 %ac1 %cp %xp %yp %ts %td scheme-args extra-regs)
(out)
`(inline ,(make-info-c-simple-call #t entry) ,%c-simple-call))
,(meta-cond
[(real-register? '%ret) (if ret-loc `(set! ,%ret ,ret-loc) `(nop))]
[else `(nop)])
,e)))))))
(define make-do/call (make-do (lambda (e) e) (do-call)))
(define (make-do/ret entry-live* return-live*)
(with-output-language (L13.5 Tail)
(make-do (lambda (e) (asm-enter (%seq (check-live ,entry-live* ...) ,e)))
`(asm-return ,return-live* ...)))))
(define make-dofargint
(lambda (name size entry-live* return-live*)
(with-output-language (L13.5 CaseLambdaExpr)
`(lambda ,(make-info name '()) 0 ()
,(asm-enter
(%seq
(check-live ,entry-live* ...)
,(cond
[(= (constant bigit-bits) size)
(%seq
(set! ,%td ,(%mref ,%ac0 ,(constant bignum-type-disp)))
(set! ,%ac0
(inline ,(make-info-load (bigit-type) #f) ,%load
,%ac0 ,%zero
,(%constant bignum-data-disp)))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(nop)
(set! ,%ac0 ,(%inline - (immediate 0) ,%ac0))))]
[(= (* (constant bigit-bits) 2) (* (constant ptr-bits) 2) size)
(let ([ac1 (in-context Lvalue (ref-reg %ac1))])
(let ([Lnegative (make-local-label 'Lnegative)] [Lreturn (make-local-label 'Lreturn)])
(%seq
(set! ,%xp ,%ac0)
(set! ,%td ,(%mref ,%xp ,(constant bignum-type-disp)))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
,(%seq
(set! ,%ac0
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
,(%constant bignum-data-disp)))
(set! ,ac1 (immediate 0))
(goto ,Lreturn))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-negative-bignum))))
,(%seq
(set! ,%ac0
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
,(%constant bignum-data-disp)))
(set! ,ac1 (immediate 0))
(goto ,Lnegative))
,(%seq
(set! ,ac1
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
,(%constant bignum-data-disp)))
(set! ,%ac0
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(goto ,Lreturn)
(goto ,Lnegative)))))
(label ,Lnegative)
(set! ,%ac0 ,(%inline -/eq (immediate 0) ,%ac0))
(if (inline ,(make-info-condition-code 'eq? #f #t) ,%condition-code)
(set! ,ac1 ,(%inline - (immediate 0) ,ac1))
(set! ,ac1 ,(%inline lognot ,ac1)))
(label ,Lreturn))))]
[(= (* (constant bigit-bits) 2) (constant ptr-bits) size)
(let ([Lnegative (make-local-label 'Lnegative)] [Lreturn (make-local-label 'Lreturn)])
(%seq
(set! ,%xp ,%ac0)
(set! ,%td ,(%mref ,%xp ,(constant bignum-type-disp)))
(set! ,%ac0
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
,(%constant bignum-data-disp)))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(goto ,Lreturn)
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-negative-bignum))))
(goto ,Lnegative)
,(%seq
(set! ,%xp
(inline ,(make-info-load (bigit-type) #f) ,%load
,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))))
(set! ,%ac0
,(%inline sll ,%ac0 ,(%constant bigit-bits)))
(set! ,%ac0 ,(%inline logor ,%ac0 ,%xp))
(if ,(%inline eq? ,%td
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(goto ,Lreturn)
(goto ,Lnegative)))))
(label ,Lnegative)
(set! ,%ac0 ,(%inline - (immediate 0) ,%ac0))
(label ,Lreturn)))]
[else (sorry! name "cannot handle size ~s" size)])
(asm-return ,return-live* ...)))))))
(define make-dofretint
(lambda (name size entry-live* return-live*)
(with-output-language (L13.5 CaseLambdaExpr)
`(lambda ,(make-info name '()) 0 ()
,(asm-enter
(%seq
(check-live ,entry-live* ...)
,(cond
[(= (constant bigit-bits) size)
(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(if ,(%inline < ,%ac0 (immediate 0))
,(%seq
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-negative-bignum))))
(set! ,%ac0 ,(%inline - (immediate 0) ,%ac0)))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum)))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0)
(set! ,%ac0 ,%xp))]
[(= (* (constant bigit-bits) 2) (* (constant ptr-bits) 2) size)
(let ([ac1 (in-context Lvalue (ref-reg %ac1))]
[Lstore1 (make-local-label 'Lstore1)]
[Lstore2 (make-local-label 'Lstore2)])
(%seq
(if ,(%inline < ,ac1 (immediate 0))
,(%seq
(set! ,ac1 ,(%inline lognot ,ac1))
(set! ,%ac0 ,(%inline - (immediate 0) ,%ac0))
; TODO: use condition code here
(if (if ,(%inline eq? ,%ac0 (immediate 0))
,(%seq
(set! ,ac1 ,(%inline + ,ac1 (immediate 1)))
(false))
,(%inline eq? ,ac1 (immediate 0)))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-negative-bignum))))
(goto ,Lstore1))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-negative-bignum))))
(goto ,Lstore2))))
; TODO: use condition code here
(if ,(%inline eq? ,ac1 (immediate 0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(label ,Lstore1)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(label ,Lstore2)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp)
,ac1)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))
,%ac0))))
(set! ,%ac0 ,%xp)))]
[(= (* (constant bigit-bits) 2) (constant ptr-bits) size)
(let ([Lstore1 (make-local-label 'Lstore1)] [Lstore2 (make-local-label 'Lstore2)])
(%seq
(if ,(%inline < ,%ac0 (immediate 0))
,(%seq
(set! ,%ac0 ,(%inline - (immediate 0) ,%ac0))
(set! ,%td ,(%inline srl ,%ac0
,(%constant bigit-bits)))
(if ,(%inline eq? ,%td (immediate 0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-negative-bignum))))
(goto ,Lstore1))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-negative-bignum))))
(goto ,Lstore2))))
,(%seq
(set! ,%td ,(%inline srl ,%ac0
,(%constant bigit-bits)))
(if ,(%inline eq? ,%td (immediate 0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(label ,Lstore1)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(label ,Lstore2)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp)
,%td)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))
,%ac0)))))
(set! ,%ac0 ,%xp)))]
[else (sorry! name "cannot handle size ~s" size)])
(asm-return ,return-live* ...)))))))
(define make-dofretuns
(lambda (name size entry-live* return-live*)
(with-output-language (L13.5 CaseLambdaExpr)
`(lambda ,(make-info name '()) 0 ()
,(asm-enter
(%seq
(check-live ,entry-live* ...)
,(cond
[(= (constant bigit-bits) size)
(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0)
(set! ,%ac0 ,%xp))]
[(= (* (constant bigit-bits) 2) (* (constant ptr-bits) 2) size)
(let ([ac1 (in-context Lvalue (ref-reg %ac1))])
(%seq
(if ,(%inline eq? ,ac1 (immediate 0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp)
,ac1)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))
,%ac0)))
(set! ,%ac0 ,%xp)))]
[(= (* (constant bigit-bits) 2) (constant ptr-bits) size)
(%seq
(set! ,%td ,(%inline srl ,%ac0
,(%constant bigit-bits)))
(if ,(%inline eq? ,%td (immediate 0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (constant bigit-bytes))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 1 (constant bignum-length-offset))
(constant type-positive-bignum))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp) ,%ac0))
,(%seq
(set! ,%xp
,(%constant-alloc type-typed-object
(fx+ (constant header-size-bignum) (fx* (constant bigit-bytes) 2))
#f #t))
(set! ,(%mref ,%xp ,(constant bignum-type-disp))
(immediate ,(fx+ (fxsll 2 (constant bignum-length-offset))
(constant type-positive-bignum))))
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
,(%constant bignum-data-disp)
,%td)
(inline ,(make-info-load (bigit-type) #f) ,%store ,%xp ,%zero
(immediate ,(fx+ (constant bignum-data-disp) (constant bigit-bytes)))
,%ac0)))
(set! ,%ac0 ,%xp))]
[else (sorry! name "cannot handle size ~s" size)])
(asm-return ,return-live* ...)))))))
(define make-dofretu*
(lambda (name type size entry-live* return-live*)
(with-output-language (L13.5 CaseLambdaExpr)
(let ([Ltop1 (make-local-label 'Ltop1)] [Ltop2 (make-local-label 'Ltop2)])
`(lambda ,(make-info name '()) 0 ()
,(asm-enter
(%seq
(check-live ,entry-live* ...)
; argument in ac0, return value in xp
(if ,(%inline eq? ,%ac0 (immediate 0))
,(%seq
(set! ,%xp ,(%constant sfalse))
(asm-return ,return-live* ...))
,(%seq
(set! ,%td (immediate 0))
(label ,Ltop1)
(set! ,%ts
(inline ,(make-info-load type #f) ,%load ,%ac0 ,%td
(immediate 0)))
(if ,(%inline eq? ,%ts (immediate 0))
(if ,(%inline eq? ,%td (immediate 0))
,(%seq
(set! ,%xp (literal ,(make-info-literal #f 'object #vu8() 0)))
(asm-return ,return-live* ...))
,(%seq
(set! ,(ref-reg %ac1) ,%td)
(set! ,%td ,(%inline + ,%td
(immediate
,(fx+ (constant header-size-bytevector)
(fx- (constant byte-alignment) 1)))))
(set! ,%td ,(%inline logand ,%td
(immediate ,(fx- (constant byte-alignment)))))
(set! ,%xp (alloc ,(make-info-alloc (constant type-typed-object) #f #t) ,%td))
(set! ,%td ,(ref-reg %ac1))
(set! ,%td ,(%inline sll ,%td
,(%constant bytevector-length-offset)))
(set! ,%td ,(%inline logor ,%td
,(%constant type-bytevector)))
(set! ,(%mref ,%xp ,(constant bytevector-type-disp))
,%td)
(set! ,%td ,(ref-reg %ac1))
(label ,Ltop2)
(if ,(%inline eq? ,%td (immediate 0))
(asm-return ,return-live* ...)
,(%seq
(set! ,%td ,(%inline - ,%td (immediate ,size)))
(set! ,%ts
(inline ,(make-info-load type #f) ,%load ,%ac0 ,%td
(immediate 0)))
(inline ,(make-info-load type #f) ,%store ,%xp ,%td
,(%constant bytevector-data-disp)
,%ts)
(goto ,Ltop2)))))
,(%seq
(set! ,%td ,(%inline + ,%td (immediate ,size)))
(goto ,Ltop1)))))))))))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(hand-coded ,sym)
(case sym
[(values-procedure)
(let ([regnum (length arg-registers)]
[Ltop (make-local-label 'top)])
`(lambda ,(make-info "values" '(-1)) 0 ()
(if ,(%inline eq? ,%ac0 (immediate 1))
(seq
(set! ,%ac0 ,(make-arg-opnd 1))
(jump ,%ref-ret (,%ac0)))
,(meta-cond
[(real-register? '%ret)
`(jump ,(%mref ,%ret ,(constant return-address-mv-return-address-disp))
(,%ac0 ,%ret ,arg-registers ...))]
[else
(%seq
(set! ,%xp ,%ref-ret)
(jump ,(%mref ,%xp
,(constant return-address-mv-return-address-disp))
(,%ac0 ,arg-registers ... ,(get-fv 0))))]))))]
[($apply-procedure)
(let ([Lloop (make-local-label 'loop)]
[Ldone (make-local-label 'done)])
`(lambda ,(make-info "$apply" '(3)) 0 ()
,(%seq
(set! ,(ref-reg %cp) ,(make-arg-opnd 1))
(set! ,%ac0 ,(make-arg-opnd 2))
(set! ,%xp ,(make-arg-opnd 3))
;; TODO: when fixnum-offset = log2-ptr-bytes, we can avoid an sll by saving
;; %ac0 before we shift it right.
(set! ,%ac0 ,(%inline sra ,%ac0 ,(%constant fixnum-offset)))
(if ,(%inline eq? ,%ac0 (immediate 0))
(goto ,Ldone)
,(%seq
(set! ,%td ,(%inline sll ,%ac0 ,(%constant log2-ptr-bytes)))
(set! ,%td ,(%inline + ,%td ,%sfp))
(if ,(%inline > ,%td ,(ref-reg %esp))
(seq (pariah)
,(with-saved-ret-reg
(with-saved-scheme-state
(in %cp %xp %ac0)
(out %ac1 %yp %ts %td scheme-args extra-regs)
`(inline ,(make-info-c-simple-call #f (lookup-c-entry handle-apply-overflood)) ,%c-simple-call))))
(nop))
,(let load-regs ([regs arg-registers])
(if (null? regs)
(%seq
(set! ,%td ,%sfp)
(label ,Lloop)
(set! ,(%mref ,%td ,(constant ptr-bytes))
,(%mref ,%xp ,(constant pair-car-disp)))
(set! ,%xp
,(%mref ,%xp ,(constant pair-cdr-disp)))
(if ,(%type-check mask-nil snil ,%xp)
,(%seq (label ,Ldone) ,(do-call))
,(%seq
(set! ,%td ,(%inline + ,%td ,(%constant ptr-bytes)))
(goto ,Lloop))))
(%seq
(set! ,(car regs) ,(%mref ,%xp ,(constant pair-car-disp)))
(set! ,%xp ,(%mref ,%xp ,(constant pair-cdr-disp)))
(if ,(%type-check mask-nil snil ,%xp)
(goto ,Ldone)
,(load-regs (cdr regs)))))))))))]
[(list*-procedure) (make-list*-procedure "list*")]
[(cons*-procedure) (make-list*-procedure "cons*")]
[($record-procedure)
(let ([Ltop (make-local-label 'ltop)])
`(lambda ,(make-info "$record" '(-2)) 0 ()
(if ,(%inline eq? ,%ac0 (immediate 0))
(seq (pariah) (goto ,Ldoargerr))
,(%seq
(set! ,%ac0 ,(%inline sll ,%ac0 ,(%constant log2-ptr-bytes)))
(set! ,%td ,(%inline + ,%ac0 (immediate ,(fx- (constant byte-alignment) 1))))
(set! ,%td ,(%inline logand ,%td (immediate ,(- (constant byte-alignment)))))
(set! ,%xp (alloc ,(make-info-alloc (constant type-typed-object) #f #f) ,%td))
,(let f ([reg* arg-registers] [i 0])
(if (null? reg*)
(%seq
; point xp to last element of record
(set! ,%xp
,(%lea ,%xp ,%ac0 (fx- (constant ptr-bytes))))
; point ac0 to last stack argument
(set! ,%ac0
,(%lea ,%sfp ,%ac0
(fx* i (fx- (constant ptr-bytes)))))
(label ,Ltop)
(set! ,(%mref ,%xp ,(constant record-type-disp))
,(%mref ,%ac0 0))
(set! ,%ac0 ,(%inline - ,%ac0 ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%ac0 ,%sfp)
,(%seq
(set! ,%ac0 ,(%inline - ,%xp (immediate ,(fx* i (constant ptr-bytes)))))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,%xp ,(%inline - ,%xp ,(%constant ptr-bytes)))
(goto ,Ltop))))
(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant ptr-bytes)) (constant record-type-disp)))
,(car reg*))
(if ,(%inline eq? ,%ac0 (immediate ,(fx* (fx+ i 1) (constant ptr-bytes))))
,(%seq
(set! ,%ac0 ,%xp)
(jump ,%ref-ret (,%ac0)))
,(f (cdr reg*) (fx+ i 1))))))))))]
[(vector-procedure)
(let ([Ltop (make-local-label 'ltop)])
`(lambda ,(make-info "vector" '(-1)) 0 ()
(if ,(%inline eq? ,%ac0 (immediate 0))
,(%seq
(set! ,%ac0 (literal ,(make-info-literal #f 'object '#() 0)))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,%ac0 ,(%inline sll ,%ac0 ,(%constant log2-ptr-bytes)))
(set! ,%td ,(%inline + ,%ac0 (immediate ,(fx+ (constant ptr-bytes) (fx- (constant byte-alignment) 1)))))
(set! ,%td ,(%inline logand ,%td (immediate ,(- (constant byte-alignment)))))
(set! ,%xp (alloc ,(make-info-alloc (constant type-typed-object) #f #f) ,%td))
,(let ([delta (fx- (constant vector-length-offset) (constant log2-ptr-bytes))])
(safe-assert (fx>= delta 0))
(if (fx= delta 0)
(if (fx= (constant type-vector) 0)
`(set! ,(%mref ,%xp ,(constant vector-type-disp)) ,%ac0)
(%seq
(set! ,%td ,(%inline logor ,%ac0 (immediate ,(constant type-vector))))
(set! ,(%mref ,%xp ,(constant vector-type-disp)) ,%td)))
(%seq
(set! ,%td ,(%inline sll ,%ac0 (immediate ,delta)))
,(if (fx= (constant type-vector) 0)
`(set! ,(%mref ,%xp ,(constant vector-type-disp)) ,%td)
(%seq
(set! ,%td ,(%inline logor ,%td (immediate ,(constant type-vector))))
(set! ,(%mref ,%xp ,(constant vector-type-disp)) ,%td))))))
,(let f ([reg* arg-registers] [i 0])
(if (null? reg*)
(%seq
; point xp to last element of vector
(set! ,%xp ,(%inline + ,%xp ,%ac0))
; point ac0 to last stack argument
(set! ,%ac0
,(%lea ,%sfp ,%ac0
(fx* i (fx- (constant ptr-bytes)))))
(label ,Ltop)
(set! ,(%mref ,%xp ,(fx- (constant vector-data-disp) (constant ptr-bytes)))
,(%mref ,%ac0 0))
(set! ,%ac0 ,(%inline - ,%ac0 ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%ac0 ,%sfp)
,(%seq
(set! ,%ac0 ,(%inline - ,%xp (immediate ,(fx* (fx+ i 1) (constant ptr-bytes)))))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,%xp ,(%inline - ,%xp ,(%constant ptr-bytes)))
(goto ,Ltop))))
(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant ptr-bytes)) (constant vector-data-disp)))
,(car reg*))
(if ,(%inline eq? ,%ac0 (immediate ,(fx* (fx+ i 1) (constant ptr-bytes))))
,(%seq
(set! ,%ac0 ,%xp)
(jump ,%ref-ret (,%ac0)))
,(f (cdr reg*) (fx+ i 1))))))))))]
[(list-procedure)
(let ([Ltop (make-local-label 'ltop)])
`(lambda ,(make-info "list" '(-1)) 0 ()
(if ,(%inline eq? ,%ac0 (immediate 0))
(seq
(set! ,%ac0 ,(%constant snil))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,%ac0 ,(%inline sll ,%ac0 ,(%constant pair-shift)))
(set! ,%xp (alloc ,(make-info-alloc (constant type-pair) #f #f) ,%ac0))
,(let f ([reg* arg-registers] [i 0])
(if (null? reg*)
; filled in first i pairs
; have at least one stack argument
; ac0 is amount allocated, or size-pair * # elements
(%seq
; point xp to last pair of list
(set! ,%xp
,(%lea ,%xp ,%ac0 (fx- (constant size-pair))))
; adjust from two ptrs per pair to one ptr per stack element
(set! ,%ac0
,(%inline srl ,%ac0 (immediate 1)))
; point ac0 to last stack argument
(set! ,%ac0
,(%lea ,%sfp ,%ac0
(fx* i (fx- (constant ptr-bytes)))))
(set! ,(%mref ,%xp ,(constant pair-cdr-disp))
,(%constant snil))
(label ,Ltop)
(set! ,(%mref ,%xp ,(constant pair-car-disp))
,(%mref ,%ac0 0))
(set! ,%ac0 ,(%inline - ,%ac0 ,(%constant ptr-bytes)))
(if ,(%inline eq? ,%ac0 ,%sfp)
,(%seq
(set! ,%ac0 ,(%inline - ,%xp (immediate ,(fx* i (constant size-pair)))))
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,(%mref ,%xp ,(fx- (constant pair-cdr-disp) (constant size-pair)))
,%xp)
(set! ,%xp ,(%inline - ,%xp ,(%constant size-pair)))
(goto ,Ltop))))
(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-car-disp)))
,(car reg*))
(if ,(%inline eq? ,%ac0 (immediate ,(fx* (fx+ i 1) (constant size-pair))))
,(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-cdr-disp)))
,(%constant snil))
(set! ,%ac0 ,%xp)
(jump ,%ref-ret (,%ac0)))
,(%seq
(set! ,(%mref ,%xp
,(fx+ (fx* i (constant size-pair)) (constant pair-cdr-disp)))
,(%inline + ,%xp
(immediate ,(fx* (fx+ i 1) (constant size-pair)))))
,(f (cdr reg*) (fx+ i 1)))))))))))]
[($instantiate-code-object)
`(lambda ,(make-info "$instantiate-code-object" '(3)) 0 ()
,(%seq
,(with-saved-ret-reg
(%seq
,(save-scheme-state
(in scheme-args)
(out %ac0 %ac1 %cp %xp %yp %ts %td extra-regs))
(inline ,(make-info-c-simple-call #f (lookup-c-entry instantiate-code-object))
,%c-simple-call)
,(restore-scheme-state
(in %ac0)
(out %ac1 %cp %xp %yp %ts %td scheme-args extra-regs))))
(jump ,%ref-ret (,%ac0))))]
[(values-error) (make-do/call (in-context Lvalue (%tc-ref ret)) "values-error" (lookup-c-entry handle-values-error))]
[(domvleterr) (make-do/call (in-context Lvalue (%tc-ref ret)) "domvleterr" (lookup-c-entry handle-mvlet-error))]
[(doargerr) (make-do/call (in-context Lvalue (%tc-ref ret)) "doargerr" (lookup-c-entry handle-arg-error))]
[(call-error) (make-do/call (in-context Lvalue (%tc-ref ret)) "call-error" (lookup-c-entry handle-docall-error))]
[(dooverflow) ((make-do/ret (intrinsic-entry-live* dooverflow) (intrinsic-return-live* dooverflow)) #f "dooverflow" (lookup-c-entry handle-overflow))]
[(dooverflood) ((make-do/ret (intrinsic-entry-live* dooverflood) (intrinsic-return-live* dooverflood)) #f "dooverflood" (lookup-c-entry handle-overflood))]
[(scan-remembered-set) ((make-do/ret (intrinsic-entry-live* scan-remembered-set) (intrinsic-return-live* scan-remembered-set)) (in-context Lvalue (%tc-ref ret)) "scan-remembered-set" (lookup-c-entry scan-remembered-set))]
[(get-room) ((make-do/ret (intrinsic-entry-live* get-room) (intrinsic-return-live* get-room)) (in-context Lvalue (%tc-ref ret)) "get-room" (lookup-c-entry get-more-room))]
[(nonprocedure-code)
`(lambda ,(make-info "nonprocedure-code" '()) 0 ()
,(%seq
(set! ,%td ,(%mref ,%xp ,(constant symbol-value-disp)))
(if ,(%type-check mask-closure type-closure ,%td)
(seq
(set! ,(ref-reg %cp) ,%td)
(set! ,(%mref ,%xp ,(constant symbol-pvalue-disp))
,(%mref ,%td ,(constant closure-code-disp))))
,(with-saved-ret-reg
(with-saved-scheme-state
(in %ac0 %ac1 %cp %xp %yp scheme-args)
(out %ts %td extra-regs)
`(inline ,(make-info-c-simple-call #f (lookup-c-entry handle-nonprocedure-symbol))
,%c-simple-call))))
,(do-call)))]
[($foreign-entry-procedure)
`(lambda ,(make-info "$foreign-entry" '(1)) 0 ()
,(%seq
(set! ,%ac0 ,(make-arg-opnd 1))
,(with-saved-ret-reg
(with-saved-scheme-state
(in %ac0)
(out %cp %xp %yp %ac1 %ts %td scheme-args extra-regs)
`(inline ,(make-info-c-simple-call #f (lookup-c-entry foreign-entry))
,%c-simple-call)))
(jump ,%ref-ret (,%ac0))))]
[($install-library-entry-procedure)
`(lambda ,(make-info "$install-library-entry" '(2)) 0 ()
,(%seq
,(with-saved-ret-reg
(%seq
,(save-scheme-state
(in scheme-args)
(out %ac0 %ac1 %cp %xp %yp %ts %td extra-regs))
(inline ,(make-info-c-simple-call #f (lookup-c-entry install-library-entry))
,%c-simple-call)
,(restore-scheme-state
(in)
(out %ac0 %ac1 %cp %xp %yp %ts %td scheme-args extra-regs))))
(set! ,%ac0 ,(%constant svoid))
(jump ,%ref-ret (,%ac0))))]
[(bytevector=?)
(let ([bv1 (make-tmp 'bv1)] [bv2 (make-tmp 'bv2)] [idx (make-tmp 'idx)] [len2 (make-tmp 'len2)])
(define (argcnt->max-fv n) (max (- n (length arg-registers)) 0))
(let ([Ltop (make-local-label 'Ltop)] [Ltrue (make-local-label 'Ltrue)] [Lfail (make-local-label 'Lfail)])
(define iptr-bytes (in-context Triv (%constant ptr-bytes)))
`(lambda ,(make-info "bytevector=?" '(2)) ,(argcnt->max-fv 2) (,bv1 ,bv2 ,idx ,len2)
,(%seq
(set! ,bv1 ,(make-arg-opnd 1))
(set! ,bv2 ,(make-arg-opnd 2))
(if ,(%inline eq? ,bv1 ,bv2)
(goto ,Ltrue)
,(%seq
(set! ,idx ,(%inline srl
,(%mref ,bv1 ,(constant bytevector-type-disp))
,(%constant bytevector-length-offset)))
(set! ,len2 ,(%inline srl
,(%mref ,bv2 ,(constant bytevector-type-disp))
,(%constant bytevector-length-offset)))
(if ,(%inline eq? ,len2 ,idx)
,(%seq
(label ,Ltop)
(if ,(%inline >= ,idx ,iptr-bytes)
(if ,(%inline eq?
,(%mref ,bv1 ,(constant bytevector-data-disp))
,(%mref ,bv2 ,(constant bytevector-data-disp)))
,(%seq
(set! ,idx ,(%inline - ,idx ,iptr-bytes))
(set! ,bv1 ,(%inline + ,bv1 ,iptr-bytes))
(set! ,bv2 ,(%inline + ,bv2 ,iptr-bytes))
(goto ,Ltop))
(goto ,Lfail))
(if (if ,(%inline eq? ,idx (immediate 0))
(true)
,(%seq
(set! ,bv1 ,(%mref ,bv1 ,(constant bytevector-data-disp)))
(set! ,bv2 ,(%mref ,bv2 ,(constant bytevector-data-disp)))
(set! ,idx ,(%inline - ,iptr-bytes ,idx))
(set! ,idx ,(%inline sll ,idx (immediate 3)))
,(constant-case native-endianness
[(little)
(%seq
(set! ,bv1 ,(%inline sll ,bv1 ,idx))
(set! ,bv2 ,(%inline sll ,bv2 ,idx)))]
[(big)
(%seq
(set! ,bv1 ,(%inline srl ,bv1 ,idx))
(set! ,bv2 ,(%inline srl ,bv2 ,idx)))])
,(%inline eq? ,bv1 ,bv2)))
,(%seq
(label ,Ltrue)
(set! ,%ac0 ,(%constant strue))
(jump ,%ref-ret (,%ac0)))
(goto ,Lfail))))
,(%seq
(label ,Lfail)
(set! ,%ac0 ,(%constant sfalse))
(jump ,%ref-ret (,%ac0))))))))))]
[(dofargint32) (make-dofargint "dofargint32" 32 (intrinsic-entry-live* dofargint32) (intrinsic-return-live* dofargint32))]
[(dofargint64) (make-dofargint "dofargint64" 64 (intrinsic-entry-live* dofargint64) (intrinsic-return-live* dofargint64))]
[(dofretint32) (make-dofretint "doretint32" 32 (intrinsic-entry-live* dofretint32) (intrinsic-return-live* dofretint32))]
[(dofretint64) (make-dofretint "doretint64" 64 (intrinsic-entry-live* dofretint64) (intrinsic-return-live* dofretint64))]
[(dofretuns32) (make-dofretuns "doretuns32" 32 (intrinsic-entry-live* dofretuns32) (intrinsic-return-live* dofretuns32))]
[(dofretuns64) (make-dofretuns "doretuns64" 64 (intrinsic-entry-live* dofretuns64) (intrinsic-return-live* dofretuns64))]
[(dofretu8*) (make-dofretu* "dofretu8*" 'unsigned-8 1 (intrinsic-entry-live* dofretu8*) (intrinsic-return-live* dofretu8*))]
[(dofretu16*) (make-dofretu* "dofretu16*" 'unsigned-16 2 (intrinsic-entry-live* dofretu16*) (intrinsic-return-live* dofretu16*))]
[(dofretu32*) (make-dofretu* "dofretu32*" 'unsigned-32 4 (intrinsic-entry-live* dofretu32*) (intrinsic-return-live* dofretu32*))]
[(error-invoke) ; more generally "tail-reentry"
`(lambda ,(make-info "error-invoke" '()) 0 ()
,(%seq
,(%inline invoke-prelude)
,(restore-scheme-state
(in %ac0 %ac1 %cp %xp %yp scheme-args)
(out %ts %td extra-regs))
,(meta-cond
[(real-register? '%ret) `(set! ,%ret ,(%mref ,%sfp 0))]
[else `(nop)])
,(do-call)))]
[(invoke)
(let ([Lret (make-local-label 'Lret)]
[Lexit (make-local-label 'Lexit)]
[Lmvreturn (make-local-label 'Lmvreturn)])
`(lambda ,(make-info "invoke" '()) 0 ()
,(%seq
; TODO: add alignment
#;(asm align) ; must start aligned or align below may fail
,(%inline invoke-prelude)
,(restore-scheme-state
(in %ac0 %cp scheme-args)
(out %ac1 %xp %yp %ts %td extra-regs))
(new-frame ,(make-info-newframe #f #f '() '() '()) ,'() ... ,Lret)
; NB: hack!!!
(set! ,%sfp ,(%inline - ,%sfp (immediate ,(constant ptr-bytes))))
(set! ,%ref-ret (label-ref ,Lret ,(constant size-rp-header)))
(tail ,(do-call)) ; argcnt already in ac0
#;(asm align)
(label ,Lret)
(rp-header ,Lmvreturn ,(* 2 (constant ptr-bytes)) 1) ; cchain is live at sfp[ptr-bytes]
(set! ,(ref-reg %ac1) (immediate 1)) ; single-value as expected
,(save-scheme-state
(in %ac0 %ac1)
(out %cp %xp %yp %ts %td scheme-args extra-regs))
(label ,Lexit)
(inline ,(make-info-c-simple-call #f (lookup-c-entry Sreturn)) ,%c-simple-call)
(label ,Lmvreturn)
(set! ,(ref-reg %ac1) ,%ac0)
,(save-scheme-state
(in %ac0 %ac1 scheme-args)
(out %cp %xp %yp %ts %td extra-regs))
(goto ,Lexit))))]
[else ($oops who "unrecognized hand-coded name ~s" sym)])]))
(define-pass np-expose-allocation-pointer : L13.5 (ir) -> L14 ()
; NB: uses %ts when %ap is not a real register
; NB: should use an unspillable, but we don't have unspillables yet
(definitions
(define local*)
(define make-tmp
(lambda (x)
(import (only np-languages make-tmp))
(let ([x (make-tmp x)])
(set! local* (cons x local*))
x)))
(define refap (with-output-language (L14 Triv) (ref-reg %ap)))
(define refeap (with-output-language (L14 Triv) (ref-reg %eap)))
(with-output-language (L14 Effect)
(define build-alloc
(lambda (info lvalue t)
(let ([Lget-room (make-local-label 'Lget-room)])
((lambda (p)
(meta-cond
[(real-register? '%ap) (p %ap values)]
[else `(seq (set! ,%ts ,refap) ,(p %ts (lambda (e) `(seq ,e (set! ,refap ,%ts)))))]))
(lambda (ap store-ap)
(%seq
(set! ,%xp ,(%inline + ,ap (immediate ,(- (info-alloc-tag info) (constant typemod)))))
,(nanopass-case (L14 Triv) t
[(immediate ,imm)
(guard (fixnum? imm) (fx< imm (constant bytes-per-segment)))
; reset_allocation_pointer never uses the last segment of the address
; space, so we can allocate less than bytes-per-segment w/o carry check
(store-ap `(set! ,ap ,(%inline + ,ap ,t)))]
[else
(%seq
,(store-ap `(set! ,ap ,(%inline +/carry ,ap ,t)))
(if (inline ,(make-info-condition-code 'carry #f #t) ,%condition-code)
(goto ,Lget-room)
(nop)))])
(if ,(%inline u< ,refeap ,ap)
,(%seq
(label ,Lget-room)
(pariah)
,((lambda (e)
(if (info-alloc-save-flrv? info)
(%seq ,(%inline save-flrv) ,e ,(%inline restore-flrv))
e))
`(set! ,%xp (inline ,(intrinsic-info-asmlib get-room (info-alloc-save-ra? info)) ,%asmlibcall))))
(nop))
(set! ,lvalue ,%xp)))))))
(define (build-inc-cc-counter arg)
(%inline inc-cc-counter ,%tc ,(%constant tc-alloc-counter-disp) ,arg))
(define (build-shift-and-inc-cc-counter t)
(let ([tcnt (make-tmp 'tcnt)])
(%seq
(set! ,tcnt ,(%inline sra ,t ,(%constant log2-ptr-bytes)))
,(build-inc-cc-counter tcnt))))
(define alloc-helper
(lambda (info lvalue t)
(if (generate-allocation-counts)
(nanopass-case (L14 Triv) t
[(immediate ,imm)
(%seq
,(build-inc-cc-counter
(in-context Triv
`(immediate ,(fxsra imm (constant log2-ptr-bytes)))))
,(build-alloc info lvalue t))]
[else
(if (var? t)
(%seq ,(build-shift-and-inc-cc-counter t) ,(build-alloc info lvalue t))
(let ([talloc (make-tmp 'talloc)])
(%seq
(set! ,talloc ,t)
,(build-shift-and-inc-cc-counter talloc)
,(build-alloc info lvalue talloc))))])
(build-alloc info lvalue t))))))
(Effect : Effect (ir) -> Effect ()
[(inline ,info ,effect-prim ,t)
(guard (eq? effect-prim %remember))
(if (real-register? '%eap)
(%seq
(if ,(%inline u< ,refap ,refeap)
(nop)
(seq
(pariah)
(inline ,(intrinsic-info-asmlib scan-remembered-set #f) ,%asmlibcall!)))
(set! ,refeap ,(%inline - ,refeap ,(%constant ptr-bytes)))
; write through to tc so dirty-list bounds are always known in case of an
; invalid memory reference or illegal instruction
(set! (mref ,%tc ,%zero ,(tc-disp %eap)) ,refeap)
(set! ,(%mref ,refeap 0) ,t))
(%seq
(set! ,%td ,refeap)
(if ,(%inline u< ,refap ,%td)
(nop)
,(%seq
(pariah)
(inline ,(intrinsic-info-asmlib scan-remembered-set #f) ,%asmlibcall!)
(set! ,%td ,refeap)))
(set! ,%td ,(%inline - ,%td ,(%constant ptr-bytes)))
(set! ,refeap ,%td)
(set! ,(%mref ,%td 0) ,t)))]
[(set! ,lvalue (alloc ,info ,[t])) (alloc-helper info lvalue t)])
(Tail : Tail (ir) -> Tail ())
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local0* ...) ,tlbody)
(fluid-let ([local* local0*])
(let ([tlbody (Tail tlbody)])
`(lambda ,info ,max-fv (,local* ...) ,tlbody)))]))
(define-record-type goto-block
(parent block)
(fields (mutable next))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(rec make-goto-block
(case-lambda
[() (make-goto-block #f)]
[(next) ((pargs->new) next)])))))
(define-record-type if-block
(parent block)
(fields
(mutable pred)
(mutable true)
(mutable false)
(mutable live-out))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda (true false)
((pargs->new) #f true false 'uninitialized)))))
(define-record-type newframe-block
(parent block)
(fields
info
(mutable next)
(mutable rp*)
(mutable rp)
(mutable live-rp)
(mutable live-call)
(mutable live-out))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda (info next)
((pargs->new) info next #f #f 'uninitialized 'uninitialized 'uninitialized)))))
(define-record-type joto-block
(parent block)
(fields nfv* (mutable next))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda (nfv*)
((pargs->new) nfv* #f)))))
(define-record-type tail-block
(parent block)
(fields (mutable tail) (mutable exit))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda ()
((pargs->new) #f #f)))))
(define-record-type bcache
(fields effect*)
(nongenerative)
(protocol
(lambda (new)
(lambda (block)
(new (block-effect* block))))))
(define-record-type if-bcache
(parent bcache)
(fields pred)
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda (block)
((pargs->new block) (if-block-pred block))))))
(define-record-type tail-bcache
(parent bcache)
(fields tail)
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda (block)
((pargs->new block) (tail-block-tail block))))))
(define-who cache-block-info
(lambda (block)
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (make-bcache block)]
[(if-block? block) (make-if-bcache block)]
[(tail-block? block) (make-tail-bcache block)]
[else (sorry! who "unrecognized block ~s" block)])))
(define-who restore-block-info!
(lambda (block bcache)
(block-effect*-set! block (bcache-effect* bcache))
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (void)]
[(if-block? block) (if-block-pred-set! block (if-bcache-pred bcache))]
[(tail-block? block) (tail-block-tail-set! block (tail-bcache-tail bcache))]
[else (sorry! who "unrecognized block ~s" block)])))
(define-pass np-expose-basic-blocks : L14 (ir) -> L15a ()
(definitions
(define add-instr!
(lambda (block ir)
(block-effect*-set! block (cons ir (block-effect* block)))))
(define add-label-link!
(lambda (from l setter)
(let ([x (local-label-block l)])
(if (block? x)
(setter from x)
(local-label-block-set! l (cons (lambda (to) (setter from to)) (or x '())))))))
(define resolve-waiting-links!
(lambda (l to)
(let ([x (local-label-block l)])
(safe-assert (not (block? x)))
(when x (for-each (lambda (add-link!) (add-link! to)) x))
(local-label-block-set! l to))))
(define-pass build-graph : (L14 Tail) (ir) -> * (block block*)
(definitions
(define add-goto-block
(lambda (l block*)
(if (local-label? l)
(let ([block (make-goto-block)])
(add-label-link! block l goto-block-next-set!)
(values block (cons block block*)))
(let ([block (make-tail-block)])
(tail-block-tail-set! block (with-output-language (L15a Tail) `(goto ,l)))
(values block (cons block block*))))))
(define add-true/false-block
(lambda (target block* label-name)
(let ([block (make-goto-block target)])
(unless (block-label target)
(block-label-set! target (make-local-label label-name)))
(values block (cons block block*))))))
(Lvalue : Lvalue (ir target) -> * (ir)
[,x x]
[(mref ,x1 ,x2 ,imm) (with-output-language (L15a Lvalue) `(mref ,x1 ,x2 ,imm))])
(Triv : Triv (ir target) -> * (ir)
[(literal ,info) (with-output-language (L15a Triv) `(literal ,info))]
[(immediate ,imm) (with-output-language (L15a Triv) `(immediate ,imm))]
[,lvalue (Lvalue lvalue target)]
[(label-ref ,l ,offset) (with-output-language (L15a Triv) `(label-ref ,l ,offset))])
;; TODO: framework should come up with some way of handling or complaining about a
;; (maybe foo) when returning from a multiple value case.
(Rhs : Rhs (ir target) -> * (ir)
[(inline ,info ,value-prim ,[Triv : t target -> t] ...)
(with-output-language (L15a Rhs) `(inline ,info ,value-prim ,t ...))]
[,t (Triv t target)])
(Tail : Tail (ir block*) -> * (block block*)
[(goto ,l) (add-goto-block l block*)]
[(seq ,e0 ,[block block*]) (Effect e0 block block*)]
[(if ,p0 ,tl1 ,[f-block block*])
(let-values ([(t-block block*) (Tail tl1 block*)])
(Pred p0 t-block f-block block*))]
[(jump ,t (,var* ...))
(let ([block (make-tail-block)])
(tail-block-tail-set! block
(with-output-language (L15a Tail)
`(jump ,(make-live-info) ,(Triv t block) (,var* ...))))
(values block (cons block block*)))]
[(joto ,l (,nfv* ...))
(let ([block (make-joto-block nfv*)])
(add-label-link! block l joto-block-next-set!)
(values block (cons block block*)))]
[(asm-return ,reg* ...)
(let ([block (make-tail-block)])
(tail-block-tail-set! block (with-output-language (L15a Tail) `(asm-return ,reg* ...)))
(values block (cons block block*)))]
[(asm-c-return ,info ,reg* ...)
(let ([block (make-tail-block)])
(tail-block-tail-set! block (with-output-language (L15a Tail) `(asm-c-return ,info ,reg* ...)))
(values block (cons block block*)))]
[else ($oops who "unexpected Tail ~s" ir)])
(Effect : Effect (ir target block*) -> * (target block*)
[(nop) (values target block*)]
[(inline ,info ,effect-prim ,[Triv : t target -> t] ...)
(add-instr! target (with-output-language (L15a Effect) `(inline ,(make-live-info) ,info ,effect-prim ,t ...)))
(values target block*)]
[(overflow-check)
(add-instr! target (with-output-language (L15a Effect) `(overflow-check ,(make-live-info))))
(values target block*)]
[(overflood-check)
(add-instr! target (with-output-language (L15a Effect) `(overflood-check ,(make-live-info))))
(values target block*)]
[(fcallable-overflow-check)
(add-instr! target (with-output-language (L15a Effect) `(fcallable-overflow-check ,(make-live-info))))
(values target block*)]
[(new-frame ,info ,rpl* ... ,rpl)
(let ([block (make-newframe-block info target)] [l (make-local-label 'docall)])
(block-label-set! target l)
(let ([rp* (fold-left (lambda (ls rp) (cons #f ls)) '() rpl*)])
(newframe-block-rp*-set! block rp*)
(let loop ([rpl* rpl*] [rp* rp*])
(unless (null? rpl*)
(add-label-link! rp* (car rpl*) set-car!)
(loop (cdr rpl*) (cdr rp*)))))
(add-label-link! block rpl newframe-block-rp-set!)
(values block (cons block block*)))]
[(remove-frame ,info)
(add-instr! target (with-output-language (L15a Effect) `(remove-frame ,(make-live-info) ,info)))
(values target block*)]
[(restore-local-saves ,info)
(add-instr! target (with-output-language (L15a Effect) `(restore-local-saves ,(make-live-info) ,info)))
(values target block*)]
[(return-point ,info ,rpl ,mrvl (,cnfv* ...))
(add-instr! target (with-output-language (L15a Effect) `(return-point ,info ,rpl ,mrvl (,cnfv* ...))))
(block-return-point! target #t)
(values target block*)]
[(rp-header ,mrvl ,fs ,lpm)
(add-instr! target (with-output-language (L15a Effect) `(rp-header ,mrvl ,fs ,lpm)))
(block-return-point! target #t)
(values target block*)]
[(shift-arg ,reg ,imm ,info)
(add-instr! target (with-output-language (L15a Effect) `(shift-arg ,(make-live-info) ,reg ,imm ,info)))
(values target block*)]
[(pariah)
(block-pariah! target #t)
(values target block*)]
[(profile ,src)
(block-src*-set! target (cons src (block-src* target)))
(values target block*)]
[(tail ,tl) (Tail tl block*)]
[(label ,l)
(block-label-set! target l)
(resolve-waiting-links! l target)
(let ([block (make-goto-block target)])
(values block (cons block block*)))]
[(goto ,l) (add-goto-block l block*)]
[(seq ,e0 ,[block block*]) (Effect e0 block block*)]
[(set! ,[Lvalue : lvalue target -> lvalue] ,[Rhs : rhs target -> rhs])
(add-instr! target (with-output-language (L15a Effect) `(set! ,(make-live-info) ,lvalue ,rhs)))
(values target block*)]
[(if ,p0 ,e1 ,e2)
(let ([t-block (make-goto-block target)] [f-block (make-goto-block target)] [l (make-local-label 'ej)])
(let ([block* (cons* t-block f-block block*)])
(block-label-set! target l)
(let-values ([(f-block block*) (Effect e2 f-block block*)])
(let-values ([(t-block block*) (Effect e1 t-block block*)])
(Pred p0 t-block f-block block*)))))]
[(check-live ,reg* ...)
(add-instr! target (with-output-language (L15a Effect) `(check-live ,(make-live-info) ,reg* ...)))
(values target block*)]
[else ($oops who "unexpected Effect ~s" ir)])
(Pred : Pred (ir t-target f-target block*) -> * (block block*)
[(true) (add-true/false-block t-target block* 'lt)]
[(false) (add-true/false-block f-target block* 'lf)]
[(inline ,info ,pred-prim ,t* ...)
(let ([block (make-if-block t-target f-target)])
(unless (block-label t-target) (block-label-set! t-target (make-local-label 'lt)))
(unless (block-label f-target) (block-label-set! f-target (make-local-label 'lf)))
(if-block-pred-set! block
(with-output-language (L15a Pred)
`(inline ,(make-live-info) ,info ,pred-prim ,(map (lambda (t) (Triv t block)) t*) ...)))
(values block (cons block block*)))]
[(seq ,e0 ,[block block*]) (Effect e0 block block*)]
[(goto ,l) (add-goto-block l block*)]
[(if ,p0 ,p1 ,[f-block block*])
(let-values ([(t-block block*) (Pred p1 t-target f-target block*)])
(Pred p0 t-block f-block block*))]
[(mlabel ,p (,l* ,p*) ...)
(let loop ([l* l*] [p* p*] [block* block*])
(if (null? l*)
(Pred p t-target f-target block*)
(let-values ([(block block*) (Pred (car p*) t-target f-target block*)])
(let ([l (car l*)])
(resolve-waiting-links! l block)
(block-label-set! block l)
(loop (cdr l*) (cdr p*) block*)))))]
[else ($oops who "unexpected Pred ~s" ir)])
(Tail ir '())))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) ,tlbody)
(let-values ([(entry-block block*) (build-graph tlbody)])
(unless (block-label entry-block)
(let ([label (make-local-label 'entry)])
(local-label-block-set! label entry-block)
(block-label-set! entry-block label)))
; NB: if entry-block is not a dcl block, it must appear first in entry-block*,
; NB: as it is the generic entry point for the procedure
(let ([entry-block* (let ([block* (fold-left
(lambda (block* dcl)
(let ([block (local-label-block dcl)])
(if (block? block) (cons block block*) block*)))
'() (info-lambda-dcl* info))])
(if (memq entry-block block*) block* (cons entry-block block*)))])
; mark reachable blocks
(for-each
(rec mark!
(lambda (from)
(unless (block-seen? from)
(block-seen! from #t)
(cond
[(goto-block? from) (mark! (goto-block-next from))]
[(joto-block? from) (mark! (joto-block-next from))]
[(if-block? from) (mark! (if-block-true from)) (mark! (if-block-false from))]
[(newframe-block? from)
(mark! (newframe-block-next from))
(for-each mark! (newframe-block-rp* from))
(mark! (newframe-block-rp from))]
[(tail-block? from) (void)]
[else (sorry! who "unrecognized from ~s" from)]))))
entry-block*)
; discard unreachable blocks, some of of which build-graph stupidly produces
(let ([block* (filter block-seen? block*)])
(for-each (lambda (block) (block-seen! block #f)) block*)
(safe-assert (andmap block-label (append entry-block* block*)))
(safe-assert (lambda (b) (eq? (local-label-block (block-label b)) b)) (append entry-block* block*))
`(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...)))))]))
(define-pass np-add-block-source! : L15a (ir) -> L15a ()
(definitions
(define block-checksum
(lambda (block)
(fxlogor
(fxsll (fxlogand (length (block-effect* block)) (fxsrl (most-positive-fixnum) 3)) 3)
(cond
[(goto-block? block) #x001]
[(joto-block? block) #x010]
[(if-block? block) #x011]
[(newframe-block? block) #x100]
[(tail-block? block) #x101]
[else (sorry! who "unrecognized block ~s" block)])))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(for-each
(lambda (block)
(include "types.ss")
(let ([n (fx- ($block-counter) 1)])
($block-counter n)
(block-pseudo-src-set! block
(make-source ($sfd) n (block-checksum block)))))
block*)
ir]))
(define-pass np-remove-repeater-blocks! : L15a (ir) -> L15a ()
(definitions
(define path-compress!
(lambda (b)
(cond
[(block-repeater? b) (goto-block-next b)]
[(and (goto-block? b) (null? (block-effect* b)) (null? (block-src* b)))
(block-repeater! b #t)
(let ([end (path-compress! (goto-block-next b))])
(goto-block-next-set! b end)
end)]
[else b])))
(define resolve
(lambda (b)
(if (block-repeater? b)
(goto-block-next b)
b))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(for-each path-compress! block*)
(for-each
(lambda (from)
(define resolve!
(lambda (get put!)
(let ([to (get from)])
(when (block-repeater? to)
(put! from (goto-block-next to))))))
(cond
[(goto-block? from)
(unless (block-repeater? from)
(resolve! goto-block-next goto-block-next-set!))]
[(joto-block? from)
(resolve! joto-block-next joto-block-next-set!)]
[(if-block? from)
(resolve! if-block-true if-block-true-set!)
(resolve! if-block-false if-block-false-set!)]
[(newframe-block? from)
(resolve! newframe-block-next newframe-block-next-set!)
(newframe-block-rp*-set! from (map resolve (newframe-block-rp* from)))
(resolve! newframe-block-rp newframe-block-rp-set!)]
[(tail-block? from) (void)]
[else (sorry! who "unrecognized block ~s" from)]))
block*)
(for-each (lambda (dcl)
(let* ([b0 (local-label-block dcl)] [b (and b0 (resolve b0))])
(unless (eq? b b0)
(local-label-block-set! dcl b)
(block-label-set! b dcl))))
(info-lambda-dcl* info))
`(lambda ,info ,max-fv (,local* ...)
(,(map resolve entry-block*) ...)
(,(filter (lambda (b) (or (not (block-repeater? b)) (eq? (goto-block-next b) b))) block*) ...))]))
(define-pass np-propagate-pariahty! : L15a (ir) -> L15a ()
(definitions
(define propagate!
(lambda (b)
(unless (block-seen? b)
(block-seen! b #t)
(block-pariah! b #f)
(cond
[(goto-block? b) (propagate! (goto-block-next b))]
[(joto-block? b) (propagate! (joto-block-next b))]
[(if-block? b)
; could set likely branch direction before marking targets as pariahs,
; but these are all pariah blocks anyway
(propagate! (if-block-true b))
(propagate! (if-block-false b))]
[(newframe-block? b)
(propagate! (newframe-block-next b))
(for-each propagate! (newframe-block-rp* b))
(propagate! (newframe-block-rp b))]
[(tail-block? b) (void)]
[else (sorry! who "unrecognized block ~s" b)])))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(safe-assert (not (ormap block-seen? block*)))
; optimistically assume all blocks are pariahs, then un-pariah anything reachable from
; the entry block without going through a known pariah block
(for-each (lambda (b) (if (block-pariah? b) (block-seen! b #t) (block-pariah! b #t))) block*)
(for-each propagate! entry-block*)
(for-each (lambda (b) (block-seen! b #f)) block*)
ir]))
(module (np-insert-profiling)
(include "types.ss")
(define-record-type start-block
(parent block)
(fields
(mutable link*))
(nongenerative)
(sealed #t)
(protocol
(lambda (pargs->new)
(lambda ()
((pargs->new) '())))))
(define-record-type link
(fields
from
(mutable to)
(mutable weight)
(mutable mst)
(mutable counter)
(mutable op))
(nongenerative)
(sealed #t)
(protocol
(lambda (new)
(lambda (from to)
(new from to 0 #f #f #f)))))
(define-who add-link-records!
; also adds exit-block links
(lambda (start-block exit-block entry-block* block*)
(define do-link
(lambda (from to)
(let ([link (make-link from to)])
(block-in-link*-set! to (cons link (block-in-link* to)))
(unless (block-seen? to)
(block-seen! to #t)
(cond
[(goto-block? to) (goto-block-next-set! to (do-link to (goto-block-next to)))]
[(joto-block? to) (joto-block-next-set! to (do-link to (joto-block-next to)))]
[(if-block? to)
(if-block-true-set! to (do-link to (if-block-true to)))
(if-block-false-set! to (do-link to (if-block-false to)))]
[(tail-block? to) (tail-block-exit-set! to (do-link to exit-block))]
[(newframe-block? to)
(newframe-block-next-set! to (do-link to (newframe-block-next to)))
; link start-block to rp blocks since they are, in reality, extra entry points that
; need to be measured separately due to the potential for control operations
(let ([rplink* (map (lambda (rp) (do-link start-block rp)) (newframe-block-rp* to))]
[rplink (do-link start-block (newframe-block-rp to))])
(start-block-link*-set! start-block (append rplink* (cons rplink (start-block-link* start-block))))
; and also record links in newframe-block for remove-link-records!
(newframe-block-rp*-set! to rplink*)
(newframe-block-rp-set! to rplink))]
[else (sorry! who "unrecognized block ~s" to)]))
link)))
(let ([all-block* (cons* start-block exit-block block*)])
(for-each (lambda (block) (block-in-link*-set! block '())) all-block*)
(block-seen! start-block #t)
(let ([entry-link* (map (lambda (to) (do-link start-block to)) entry-block*)])
(start-block-link*-set! start-block (append entry-link* (start-block-link* start-block)))
(for-each (lambda (block) (block-seen! block #f)) all-block*)
entry-link*))))
(define-who remove-link-records!
(lambda (block*)
(for-each
(lambda (block)
(cond
[(goto-block? block) (goto-block-next-set! block (link-to (goto-block-next block)))]
[(joto-block? block) (joto-block-next-set! block (link-to (joto-block-next block)))]
[(if-block? block)
(if-block-true-set! block (link-to (if-block-true block)))
(if-block-false-set! block (link-to (if-block-false block)))]
[(tail-block? block) (tail-block-exit-set! block #f)]
[(newframe-block? block)
(newframe-block-next-set! block (link-to (newframe-block-next block)))
(newframe-block-rp*-set! block (map link-to (newframe-block-rp* block)))
(newframe-block-rp-set! block (link-to (newframe-block-rp block)))]
[else (sorry! who "unrecognized block ~s" block)])
(block-in-link*-set! block '()))
block*)))
(define weight-graph!
(lambda (start-block exit-block block*)
(define sum-link-weights
(lambda (links)
; using #3$fx+ to ensure that we wrap when we go over the fixnum range
(fold-left (lambda (n link) (#3%fx+ (link-weight link) n)) 0 links)))
(define-who process-link
(lambda (ls link)
(let ([block (link-to link)])
(cond
[(block-finished? block) ls]
[(block-seen? block) ; cycle?
(link-weight-set! link 500)
ls]
[else
(block-seen! block #t)
(let ([ls (cond
[(goto-block? block) (process-link ls (goto-block-next block))]
[(joto-block? block) (process-link ls (joto-block-next block))]
[(if-block? block) (process-link (process-link ls (if-block-false block)) (if-block-true block))]
[(tail-block? block) ls]
[(newframe-block? block) (process-link ls (newframe-block-next block))]
[else (sorry! who "unrecognized block ~s" block)])])
(block-finished! block #t)
(cons block ls))]))))
(define-who propagate-flow
(lambda (block)
(let ([sum (sum-link-weights (block-in-link* block))]
[links (cond
[(goto-block? block) (list (goto-block-next block))]
[(joto-block? block) (list (joto-block-next block))]
[(if-block? block) (list (if-block-true block) (if-block-false block))]
[(tail-block? block) (list (tail-block-exit block))]
[(newframe-block? block) (list (newframe-block-next block))]
[else (sorry! who "unrecognized block ~s" block)])])
(safe-assert (not (null? links)))
; AWK: we are missing the notion of those instructions that usually
; succeed (dooverflow, dooverflood, call-error, fx+? and fx-? in
; the original blocks.ss code)
(let-values ([(pariah* non-pariah*)
(partition (lambda (link) (block-pariah? (link-to link))) links)])
(if (null? non-pariah*)
(divide-flow sum (length pariah*) pariah*)
(divide-flow sum (length non-pariah*) non-pariah*))))))
(define divide-flow
(lambda (flow n ls)
(safe-assert (fx> n 0))
(if (fx= n 1)
(link-weight-set! (car ls) flow)
(let ([x (fxquotient flow n)])
(link-weight-set! (car ls) x)
(divide-flow (fx- flow x) (fx- n 1) (cdr ls))))))
(let ([exit->start (goto-block-next exit-block)])
(block-finished! start-block #t)
(block-finished! exit-block #t)
; DFS to find cycles & determine order to propagate flow
(link-weight-set! exit->start 1000)
(for-each propagate-flow (fold-left process-link '() (start-block-link* start-block)))
(for-each (lambda (block) (block-seen! block #f)) (cons* start-block exit-block block*)))))
(module (mst-top)
(define-who mst-top
(lambda (start-block exit-block block*)
(block-seen! start-block #t)
(block-seen! exit-block #t)
(let ([pq (pqinitialize (length block*))])
(define (mst-in-link link) (pqupdate link (link-from link) pq))
(define (mst-out-link link) (pqupdate link (link-to link) pq))
; add the exit->start link to the mst
(link-mst-set! (goto-block-next exit-block) exit-block)
(for-each mst-out-link (start-block-link* start-block))
(let mst ()
(unless (pqempty? pq)
(let ([r (pqremove pq)])
(let ([block (cdr r)] [link (car r)])
(link-mst-set! link block)
(for-each mst-in-link (block-in-link* block))
(cond
[(goto-block? block) (mst-out-link (goto-block-next block))]
[(joto-block? block) (mst-out-link (joto-block-next block))]
[(if-block? block) (mst-out-link (if-block-true block)) (mst-out-link (if-block-false block))]
[(tail-block? block) (mst-out-link (tail-block-exit block))]
[(newframe-block? block) (mst-out-link (newframe-block-next block))]
[else (sorry! who "unrecognized block ~s" block)])
(mst))))))))
(define pqinitialize
(let ([b (make-block)]) ;; add dummy first block in the priority-queue
(let ([l (make-link #f b)])
(link-weight-set! l (most-positive-fixnum))
(let ([pqfirst (cons l b)])
(lambda (size)
(cons 0 (make-vector (fx+ size 1) pqfirst)))))))
(define pqupheap
(lambda (heap k w)
(let ([y (vector-ref heap (fx/ k 2))])
(if (fx> w (link-weight (car y)))
(begin
(vector-set! heap k y)
(block-seen! (cdr y) k)
(pqupheap heap (fx/ k 2) w))
k))))
(define pqdownheap
(lambda (heap n k w)
(if (fx< (fx/ n 2) k)
k
(let ([j (fx* k 2)])
(let ([y1 (vector-ref heap j)]
[y2 (and (fx< j n) (vector-ref heap (fx+ j 1)))])
(let ([w1 (link-weight (car y1))]
[w2 (if y2 (link-weight (car y2)) (most-negative-fixnum))])
(if (fx>= w1 w2)
(if (fx>= w w1)
k
(begin
(vector-set! heap k y1)
(block-seen! (cdr y1) k)
(pqdownheap heap n j w)))
(if (fx>= w w2)
k
(begin
(vector-set! heap k y2)
(block-seen! (cdr y2) k)
(pqdownheap heap n (fx+ j 1) w))))))))))
(define pqempty?
(lambda (pq)
(fx= (car pq) 0)))
(define pqremove
(lambda (pq)
(let ([n (fx- (car pq) 1)]
[heap (cdr pq)])
(set-car! pq n)
(let ([r (vector-ref heap 1)]
[x (vector-ref heap (fx+ n 1))])
(let ([k (pqdownheap heap n 1 (link-weight (car x)))])
(vector-set! heap k x)
(block-seen! (cdr x) k))
(block-seen! (cdr r) #t)
r))))
(define pqupdate
(lambda (link block pq)
(let ([k (block-seen? block)])
(cond
[(eq? k #t) (void)]
[(eq? k #f)
(let ([n (fx+ (car pq) 1)] [heap (cdr pq)])
(set-car! pq n)
(let ([k (pqupheap heap n (link-weight link))])
(vector-set! heap k (cons link block))
(block-seen! block k)))]
[else
(let ([heap (cdr pq)])
(let ([x (vector-ref heap k)]
[w (link-weight link)])
(when (fx> w (link-weight (car x)))
(let ([k (pqupheap heap k w)])
(vector-set! heap k (cons link block))
(block-seen! block k)))))])))))
(define-who instrument
(lambda (start-block exit-block block*)
(define checks-cc?
(lambda (block)
(and (if-block? block)
(null? (block-effect* block))
(nanopass-case (L15a Pred) (if-block-pred block)
[(inline ,live-info ,info ,pred-prim ,t* ...) (eq? pred-prim %condition-code)]
[else #f]))))
(define add-counter!
(lambda (block counter)
(define add-instr!
(lambda (block ir)
(let ([effect* (block-effect* block)])
(block-effect*-set! block
(if (block-return-point? block)
; rp-header / return-point form must be first
(cons* (car effect*) ir (cdr effect*))
(cons ir effect*))))))
(with-output-language (L15a Effect)
(add-instr! block
`(inline ,(make-live-info) ,null-info ,%inc-profile-counter
(literal ,(make-info-literal #t 'object counter (constant record-data-disp)))
(immediate 1))))))
(define maybe-add-counter
(lambda (new* link)
(cond
[(link-counter link) =>
(lambda (counter)
(let ([from (link-from link)] [to (link-to link)])
(cond
[(and (fx= (length (block-in-link* to)) 1) (not (eq? to exit-block)))
(assert (not (checks-cc? to)))
(add-counter! to counter)
new*]
[(or (goto-block? from) (tail-block? from))
(assert (not (checks-cc? from)))
(add-counter! from counter)
new*]
[else
(safe-assert (not (eq? to exit-block)))
(assert (not (checks-cc? to)))
(let* ([block (make-goto-block)] [l (make-link block to)])
(let ([label (block-label to)])
(if (and (eq? from start-block) (and (direct-call-label? label) (direct-call-label-referenced label)))
(begin
; we're adding the new block between the (virtual) start block and one
; of our (referenced) dcls. we need to move the dcl label to the new
; block so the counter is incremented when we come in from the outside
(block-label-set! block label)
(local-label-block-set! label block)
(let ([label (make-local-label 'exdcl)])
(block-label-set! to label)
(local-label-block-set! label to)))
(let ([label (make-local-label 'profile)])
(block-label-set! block label)
(local-label-block-set! label block))))
(link-to-set! link block)
; set link mst for p-dot-graph/profiling's benefit
(link-mst-set! l block)
(block-in-link*-set! block (list link))
(goto-block-next-set! block l)
(block-in-link*-set! to (cons l (remq link (block-in-link* to))))
(add-counter! block counter)
(cons block new*))])))]
[else new*])))
(fold-left
(lambda (new* block)
(fold-left maybe-add-counter
new* (block-in-link* block)))
block*
(cons exit-block block*))))
(define build-pinfo
(lambda (exit-block block*)
; op -> counter | (plus-counter* . minus-counter*)
; plus-counter* -> (op ...)
; minus-counter* -> (op ...)
(define make-op
(lambda (plus minus)
; optimize ((op) . ()) => op
(if (and (null? minus) (fx= (length plus) 1))
(car plus)
(cons plus minus))))
(define-who exit-ops
(lambda (block l)
(define maybe-build-op
(lambda (link ls)
(if (eq? link l)
ls
(cons (build-op link) ls))))
(cond
[(goto-block? block) (maybe-build-op (goto-block-next block) '())]
[(joto-block? block) (maybe-build-op (joto-block-next block) '())]
[(if-block? block) (maybe-build-op (if-block-true block) (maybe-build-op (if-block-false block) '()))]
[(tail-block? block) (maybe-build-op (tail-block-exit block) '())]
[(newframe-block? block) (maybe-build-op (newframe-block-next block) '())]
[else (sorry! who "unrecognized block ~s" block)])))
(define enter-ops
(lambda (n l)
(let ([ls (block-in-link* n)])
(map build-op (if (not l) ls (remq l ls))))))
(define build-op
(lambda (l)
(cond
[(link-mst l) =>
(lambda (n)
(let ([op (if (eq? (link-to l) n)
(make-op (exit-ops n #f) (enter-ops n l))
(make-op (enter-ops n #f) (exit-ops n l)))])
(link-op-set! l op)
op))]
[else
(or (link-counter l)
(let ([counter (make-profile-counter 0)])
(link-counter-set! l counter)
(link-op-set! l counter)
counter))])))
(define (filter-src* block)
(cond
[(eq? ($compile-profile) 'source) (block-src* block)]
[(block-pseudo-src block) => list]
[else '()]))
(fold-left
(lambda (ls block)
(let ([src* (filter-src* block)])
(if (null? src*)
ls
(cons (make-rblock src* (make-op (map build-op (block-in-link* block)) '())) ls))))
'() block*)))
(module (p-graph/profiling p-dot-graph/profiling)
(define-who block-link*
(lambda (block)
(cond
[(goto-block? block) `(,(goto-block-next block))]
[(joto-block? block) `(,(joto-block-next block))]
[(if-block? block) `(,(if-block-true block) ,(if-block-false block))]
; leave out newframe-block => rp links, since we profiler uses its own start-block => rp links
[(newframe-block? block) `(,(newframe-block-next block))]
[(tail-block? block) `(,(tail-block-exit block))]
[(start-block? block) (start-block-link* block)]
[else (sorry! who "unrecognized block ~s" block)])))
(define block->pretty-name
(lambda (block)
(define block->label
(lambda (block)
(let ([label (block-label block)])
(or label
(let ([label (make-local-label 'unknown)])
(block-label-set! block label)
label)))))
(parameterize ([print-gensym 'pretty/suffix]) (format "~s" (block->label block)))))
(define p-dot-graph/profiling
(lambda (block* exit-block p)
(define print-link
(lambda (reversed?)
(lambda (link)
(let-values ([(from to) (if reversed?
(values (link-to link) (link-from link))
(values (link-from link) (link-to link)))])
(display " " p)
(display (block->pretty-string from) p)
(display " -> " p)
(display (block->pretty-string to) p)
#;(when (and (block-non-tail-call? (link-from link)) (eq? (link-to link) exit-block))
(display " [color=grey]" p))
(if (link-mst link)
(if reversed?
(display " [color=blue]" p)
(display " [color=black]" p))
(if reversed?
(display " [color=pink]" p)
(display " [color=red]" p)))
(write-char #\; p)
(newline p))
; print the tree in green
#;(when (link-mst link)
(let-values ([(from to) (if (eq? (link-mst link) (link-to link))
(values (link-from link) (link-to link))
(values (link-to link) (link-from link)))])
(display " " p)
(display (block->pretty-string from) p)
(display " -> " p)
(display (block->pretty-string to) p)
(display " [color=green];\n" p))))))
(define block->pretty-string
(lambda (block)
(list->string (subst #\_ #\. (subst #\_ #\- (string->list (block->pretty-name block)))))))
(newline p)
(display "digraph PROFILE {\n" p)
(display " node [shape = box];" p)
(let f ([block* block*] [link* '()] [in-link* '()])
(if (null? block*)
(begin
(newline p)
(newline p)
(for-each (print-link #f) link*)
(when #f (for-each (print-link #t) in-link*))
(display "}\n" p))
(let ([block (car block*)])
(display " " p)
(display (block->pretty-string block) p)
(f (cdr block*)
(append (block-link* block) link*)
(append (block-in-link* block) in-link*)))))))
(define-who p-graph/profiling
(lambda (block* name p)
(newline p)
(when name (fprintf p "~a:\n" name))
(parameterize ([print-graph #t] [print-length 6] [print-level 3] [print-gensym 'pretty/suffix])
(for-each
(lambda (block)
(fprintf p "~a: " (block->pretty-name block))
(let loop ([links (block-link* block)])
(unless (null? links)
(let ([link (car links)])
(fprintf p "~a(~d)~a"
(block->pretty-name (link-to link))
(link-weight link)
(if (link-mst link)
""
"*"))
(unless (null? (cdr links)) (display ", " p))
(loop (cdr links)))))
(fprintf p " in=~d:" (length (block-in-link* block)))
(begin
(newline p)
(for-each
(lambda (link)
(cond
[(link-counter link) (fprintf p " Bump count to ~a\n" (block->pretty-name (link-to link)))]
[(link-op link) (fprintf p " Link count to ~a computed from other counts\n" (block->pretty-name (link-to link)))])
(fprintf p " ~a -> ~a -- ~s\n" (block->pretty-name (link-from link))
(block->pretty-name (link-to link)) (link-op link)))
(block-link* block))
; We no longer have the code to report here, so we're reporting from source
(fprintf p "~{ ~s~%~}" (map unparse-L15a (block-effect* block)))
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block) (start-block? block)) (void)]
[(if-block? block) (fprintf p " ~s~%" (unparse-L15a (if-block-pred block)))]
[(tail-block? block) (fprintf p " ~s~%" (unparse-L15a (tail-block-tail block)))]
[else (sorry! who "unrecognized block ~s" block)])))
block*)))))
(define-pass np-insert-profiling : L15a (ir) -> L15a ()
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(let* ([start-block (make-start-block)]
[exit-block (make-goto-block start-block)])
(block-label-set! start-block 'start)
(block-label-set! exit-block 'exit)
(let ([entry-link* (add-link-records! start-block exit-block entry-block* block*)])
(weight-graph! start-block exit-block block*)
(mst-top start-block exit-block block*)
(info-lambda-pinfo*-set! info (append (build-pinfo exit-block block*) (info-lambda-pinfo* info)))
; now insert increments for counters added by build-pinfo
(let* ([block* (instrument start-block exit-block block*)]
[entry-block* (map link-to entry-link*)])
(safe-assert (andmap (lambda (block) (not (null? (block-in-link* block)))) block*))
(when ($assembly-output)
(let ([block* (cons start-block (append block* (list exit-block)))])
(p-graph/profiling block* (info-lambda-name info) ($assembly-output))
(p-dot-graph/profiling block* exit-block ($assembly-output))))
(remove-link-records! block*)
(for-each (lambda (block) (block-seen! block #f) (block-finished! block #f)) block*)
(safe-assert (andmap block-label (append entry-block* block*)))
(safe-assert (lambda (b) (eq? (local-label-block (block-label b)) b)) (append entry-block* block*))
`(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...)))))])))
(module (p-graph p-dot-graph)
(define block->pretty-name
(lambda (block)
(define block->label
(lambda (block)
(let ([label (block-label block)])
(or label
(let ([label (make-local-label 'unknown)])
(block-label-set! block label)
label)))))
(parameterize ([print-gensym 'pretty/suffix]) (format "~s" (block->label block)))))
(define p-dot-graph
(lambda (block* p)
(define print-link
(lambda (link)
(display " " p)
(display (car link) p)
(display " -> " p)
(display (cdr link) p)
(write-char #\; p)
(newline p)))
(define block->pretty-string
(lambda (block)
(list->string (subst #\_ #\. (subst #\_ #\- (string->list (block->pretty-name block)))))))
(define-who block-link*
(lambda (block)
(let ([block-name (block->pretty-string block)])
(map (lambda (x) (cons block-name (block->pretty-string x)))
(cond
[(goto-block? block) `(,(goto-block-next block))]
[(joto-block? block) `(,(joto-block-next block))]
[(if-block? block) `(,(if-block-true block) ,(if-block-false block))]
[(newframe-block? block) `(,(newframe-block-next block) ,@(newframe-block-rp* block) ,(newframe-block-rp block))]
[(tail-block? block) '()]
[else (sorry! who "unrecognized block ~s" block)])))))
(display "digraph BLOCKS {\n" p)
(display " node [shape = box];" p)
(let f ([block* block*] [link* '()])
(if (null? block*)
(begin
(newline p)
(newline p)
(for-each print-link link*)
(display "}\n" p))
(let ([block (car block*)])
(display " " p)
(display (block->pretty-string block) p)
(when (block-pariah? block) (display " [color=red]" p))
(f (cdr block*) (append (block-link* block) link*)))))))
(define-who p-graph
(lambda (block* name p unparser)
(when name (fprintf p "\n~a:" name))
(parameterize ([print-graph #t] [print-length 6] [print-level 3] [print-gensym 'pretty/suffix])
(for-each
(lambda (block)
(fprintf p "~a (depth = ~s~@[, pariah~]):\n" (block->pretty-name block) (block-depth block) (block-pariah? block))
(fprintf p "~{ ~s~%~}" (map unparser (block-effect* block)))
(cond
[(goto-block? block) (fprintf p " ~s\n" `(goto ,(block->pretty-name (goto-block-next block))))]
[(joto-block? block) (fprintf p " ~s\n" `(joto ,(block->pretty-name (joto-block-next block))))]
[(if-block? block) (fprintf p " ~s\n" `(if ,(unparser (if-block-pred block))
(goto ,(block->pretty-name (if-block-true block)))
(goto ,(block->pretty-name (if-block-false block)))))]
[(tail-block? block) (fprintf p " ~s\n" (unparser (tail-block-tail block)))]
[(newframe-block? block) (fprintf p " ~s\n" `(goto ,(block->pretty-name (newframe-block-next block))))]
[else (sorry! who "unrecognized block ~s" block)]))
block*)))))
(define-pass np-add-in-links! : L15a (ir) -> L15a ()
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(safe-assert (andmap (lambda (block) (eq? (block-in-link* block) '())) block*))
(for-each
(lambda (from)
(define add-in-link!
(lambda (to)
(block-in-link*-set! to (cons from (block-in-link* to)))))
(cond
[(goto-block? from) (add-in-link! (goto-block-next from))]
[(if-block? from) (add-in-link! (if-block-true from)) (add-in-link! (if-block-false from))]
[(newframe-block? from)
(add-in-link! (newframe-block-next from))
(for-each add-in-link! (newframe-block-rp* from))
(add-in-link! (newframe-block-rp from))]
[(joto-block? from) (add-in-link! (joto-block-next from))]
[(tail-block? from) (void)]
[else (sorry! who "unrecognized block ~s" from)]))
block*)
ir]))
(define-pass np-compute-loop-depth! : L15a (ir) -> L15a ()
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(safe-assert (not (ormap block-seen? block*)) (not (ormap block-finished? block*)))
(let ([lh* '()])
(for-each
(rec f
(lambda (b)
(unless (block-finished? b)
(if (block-seen? b)
(begin
(block-loop-header! b #t)
(set! lh* (cons b lh*)))
(begin
(block-seen! b #t)
(cond
[(goto-block? b) (f (goto-block-next b))]
[(joto-block? b) (f (joto-block-next b))]
[(if-block? b) (f (if-block-true b)) (f (if-block-false b))]
[(tail-block? b) (void)]
[(newframe-block? b)
(f (newframe-block-next b))
(for-each f (newframe-block-rp* b))
(f (newframe-block-rp b))]
[else (sorry! who "unrecognized block ~s" b)])
(block-seen! b #f)
(block-finished! b #t))))))
entry-block*)
(unless (null? lh*)
(fold-left (lambda (i b) (block-index-set! b i) (fx+ i 1)) 0 lh*)
(let ([tree-size (length lh*)] [blockvec (list->vector lh*)] [lb* lh*])
(define remove-block
(lambda (b tree)
(let ([index (block-index b)])
(if index (tree-bit-unset tree tree-size index) tree))))
; invert sense of block-finished so we don't have to reset
(let ([block-finished? (lambda (b) (not (block-finished? b)))]
[block-finished! (lambda (b bool) (block-finished! b (not bool)))])
(for-each
(rec f
(lambda (b)
(cond
[(block-finished? b)
(tree-fold-left (lambda (lhs index)
(let ([b (vector-ref blockvec index)])
(if (block-finished? b)
lhs
(tree-bit-set lhs tree-size index))))
tree-size empty-tree (block-loop-headers b))]
[(block-seen? b)
(safe-assert (block-index b))
(tree-bit-set empty-tree tree-size (block-index b))]
[(tail-block? b) empty-tree]
[else
(block-seen! b #t)
(let ([lhs (remove-block b
(cond
[(goto-block? b) (f (goto-block-next b))]
[(joto-block? b) (f (joto-block-next b))]
[(if-block? b)
; must follow same order as loop above so we find the same loop headers
(let ([lhs (f (if-block-true b))])
(tree-merge lhs (f (if-block-false b)) tree-size))]
[(newframe-block? b)
; must follow same order as loop above so we find the same loop headers
(fold-left (lambda (lhs b) (tree-merge lhs (f b) tree-size))
(let ([lhs (f (newframe-block-next b))]) (tree-merge lhs (f (newframe-block-rp b)) tree-size))
(newframe-block-rp* b))]
[else (sorry! who "unrecognized block ~s" b)]))])
(unless (or (block-loop-header? b) (eqv? (block-loop-headers b) empty-tree))
(set! lb* (cons b lb*)))
(block-seen! b #f)
(block-finished! b #t)
(block-loop-headers-set! b lhs)
lhs)])))
; seems like we should be able to use (reverse lh*) rather than entry-block* here
; but we end up finding different loop headers in some cases
entry-block*))
(for-each
(rec g
(lambda (b)
(if (block-seen? b)
(block-depth b)
(begin
(block-seen! b #t)
(let ([depth (tree-fold-left (lambda (depth index) (fxmax (g (vector-ref blockvec index)) depth))
tree-size 0 (block-loop-headers b))])
(let ([depth (if (block-loop-header? b) (fx+ depth 1) depth)])
(block-depth-set! b depth)
depth))))))
lb*))
(for-each (lambda (b) (block-seen! b #f)) block*)
#;(p-dot-graph block* (current-output-port))
#;(p-graph block* (info-lambda-name info) (current-output-port) unparse-L15a)))
(for-each (lambda (b) (block-finished! b #f)) block*)
ir]))
(define-pass np-weight-references! : L15a (ir) -> L15a ()
(definitions
(define weight-block!
(lambda (max-weight)
(lambda (block weight)
(let ([weight (if (and weight (not (fl= max-weight 0.0)))
(flonum->fixnum (fl/ weight (fl/ max-weight 1024.0)))
(if (block-pariah? block)
0
(expt 4 (fxmin (block-depth block) 5))))])
(block-weight-set! block weight)
(unless (fx= weight 0)
(let ()
(define fixnum (lambda (x) (if (fixnum? x) x (most-positive-fixnum))))
; refs and sets are weighted equally
(define process-var
(lambda (x)
(when (uvar? x)
(uvar-ref-weight-set! x (fixnum (+ (uvar-ref-weight x) weight))))))
(define Lvalue
(lambda (lvalue)
(nanopass-case (L15a Lvalue) lvalue
[,x (process-var x)]
[(mref ,x1 ,x2 ,imm) (process-var x1) (process-var x2)])))
(define Triv
(lambda (t)
(nanopass-case (L15a Triv) t
[,lvalue (Lvalue lvalue)]
[else (void)])))
(define Rhs
(lambda (rhs)
(nanopass-case (L15a Rhs) rhs
[,lvalue (Lvalue lvalue)]
[(inline ,info ,value-prim ,t* ...)
(for-each Triv t*)]
[else (void)])))
(define Pred
(lambda (p)
(nanopass-case (L15a Pred) p
[(inline ,live-info ,info ,pred-prim ,t* ...)
(for-each Triv t*)]
[else (sorry! who "unexpected pred ~s" p)])))
(define Tail
(lambda (tl)
(nanopass-case (L15a Tail) tl
[(jump ,live-info ,t (,var* ...)) (Triv t)]
[else (void)])))
(for-each
(lambda (instr)
(nanopass-case (L15a Effect) instr
[(set! ,live-info ,lvalue ,rhs) (Lvalue lvalue) (Rhs rhs)]
[(inline ,live-info ,info ,effect-prim ,t* ...)
(for-each Triv t*)]
[else (void)]))
(block-effect* block))
(cond
[(or (goto-block? block) (joto-block? block)) (void)]
[(if-block? block) (Pred (if-block-pred block))]
[(newframe-block? block)
(let ([newframe-info (newframe-block-info block)])
(info-newframe-weight-set! newframe-info
(fixnum (+ (info-newframe-weight newframe-info) weight))))]
[(tail-block? block) (Tail (tail-block-tail block))]
[else (sorry! who "unrecognized block ~s" block)]))))))))
; now know for each block its loop nesting depth and pariahty
; now weight calls and refs accordingly
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv (,local* ...) (,entry-block* ...) (,block* ...))
(if ($profile-block-data?)
(let* ([weight* (map (lambda (block)
(let ([psrc (block-pseudo-src block)])
(and psrc (profile-query-weight psrc))))
block*)]
[max-weight (fold-left (lambda (m block weight)
(if weight (flmax m weight) m))
0.0 block* weight*)])
(for-each (weight-block! max-weight) block* weight*))
(let ([wb (weight-block! #f)])
(for-each (lambda (block) (wb block #f)) block*)))
ir]))
; this must come before np-allocate-registers since asm-module is imported
; by the included file <architecture>-instructions.ss
(module (np-generate-code asm-module)
(define-threaded aop)
(define-threaded funcrel*)
(define-threaded current-func)
(define make-funcrel
(lambda (reloc l offset)
(let ([stuff (list offset l)])
(set! funcrel* (cons stuff funcrel*))
(cons reloc stuff))))
; TODO: generate code forward => backward and thread through a machine-state
; record that says what each register contains, including the condition-code
; register, so that we can avoid redundant loads and tests. For example,
; second set! of td in (seq (set! td ,(%mref tc 20)) ... (set! td ,(%mref tc 20)))
; should go away with no intervening assignment of td or tc[20]. Similarly,
; in (seq (mset! tc 36 (incr ,(%mref tc 36))) (if (eq? ,(%mref tc 36) 0) L1 L2),
; the test should reduce to a check of the 'z' flag.
; plain chunks arise only as the destination for a rachunk
(define-record-type chunk
(nongenerative)
(fields size code*)
(protocol
(lambda (new)
(lambda (code*) (new (asm-size* code*) code*)))))
(define-record-type lchunk
(parent chunk)
(nongenerative)
(sealed #t)
(fields l)
(protocol
(lambda (pargs->new)
(lambda (l code*)
((pargs->new code*) l)))))
(define-record-type gchunk
(parent chunk)
(nongenerative)
(sealed #t)
(fields l laddr next-offset)
(protocol
(lambda (pargs->new)
(lambda (l next-offset code*)
((pargs->new code*) l (local-label-offset l) next-offset)))))
(define-record-type cgchunk
(parent chunk)
(nongenerative)
(sealed #t)
(fields info l1 l2 laddr1 laddr2 next-offset)
(protocol
(lambda (pargs->new)
(lambda (info l1 l2 next-offset code*)
(define label-offset
(lambda (l)
(and (local-label? l) (local-label-offset l))))
((pargs->new code*) info l1 l2 (label-offset l1) (label-offset l2) next-offset)))))
; rachunks arise only during code generation to support machines like the ARM that determine
; return addresses for Scheme calls using pc-relative add or lea instructions
(define-record-type rachunk
(parent chunk)
(nongenerative)
(sealed #t)
(fields dest l incr-offset laddr next-offset)
(protocol
(lambda (pargs->new)
(lambda (dest l incr-offset next-offset code*)
((pargs->new code*) dest l incr-offset (local-label-offset l) next-offset)))))
(define-pass np-generate-code : L16 (ir) -> * (code)
(definitions
(define munge-recur?)
(define c-trace
; copied from compile.ss
(lambda (name size trace-list p)
(when p
(newline p)
(when name (fprintf p "~a: ~%" name))
(parameterize ([print-length 5] [print-level 3] [print-gensym 'pretty/suffix])
(let dump ([trace-list trace-list] [last-addr size])
(when (pair? trace-list)
(apply (lambda (addr op . args)
(if (eq? op 'label)
(begin
(fprintf p "~{~s~^, ~}:\n" addr)
(dump (cdr trace-list) last-addr))
(begin
(fprintf p "~d:~9t~a~24t" (- size last-addr) op)
(do ((args args (cdr args)))
((null? args))
(let ([arg (car args)])
(if (string? arg) (display arg p) (write arg p)))
(unless (null? (cdr args)) (display ", " p)))
(newline p)
(dump (cdr trace-list) addr))))
(car trace-list)))))
(fprintf p "~d:~9t<end~@[ ~a~]>\n" size name))))
; munge gets the code in forward order, but really wants to process it
; backwards to find the label offsets. Maybe the size would be better
; tracked by doing it more like cp2 does right now and then patching in
; the foward jumps and tightening up the code.
(define-who munge
(lambda (c* size)
(define (munge-pass c* iteration)
(define get-local-label-offset
(lambda (l)
(local-label-iteration-set! l iteration)
(local-label-offset l)))
(let f ([rc* (reverse c*)] [c* '()] [offset 0])
(if (null? rc*)
(values c* offset)
(let ([c (car rc*)] [rc* (cdr rc*)])
(cond
[(lchunk? c)
(let ([l (lchunk-l c)] [offset (fx+ offset (chunk-size c))])
(when l
(unless (eq? (get-local-label-offset l) offset)
(local-label-offset-set! l offset)
(when (fx= (local-label-iteration l) iteration)
(set! munge-recur? #t))))
(f rc* (cons c c*) offset))]
[(gchunk? c)
(let ([l (gchunk-l c)])
(if (and (eq? (get-local-label-offset l) (gchunk-laddr c))
(eq? (gchunk-next-offset c) offset))
(f rc* (cons c c*) (fx+ offset (chunk-size c)))
(let ([c (asm-jump l offset)])
(f rc* (cons c c*) (fx+ offset (chunk-size c))))))]
[(cgchunk? c)
(let ([l1 (cgchunk-l1 c)] [l2 (cgchunk-l2 c)])
(if (and (or (libspec-label? l1) (eq? (get-local-label-offset l1) (cgchunk-laddr1 c)))
(or (libspec-label? l2) (eq? (get-local-label-offset l2) (cgchunk-laddr2 c)))
(eq? (cgchunk-next-offset c) offset))
(f rc* (cons c c*) (fx+ offset (chunk-size c)))
(let ([c (asm-conditional-jump (cgchunk-info c) l1 l2 offset)])
(f rc* (cons c c*) (fx+ offset (chunk-size c))))))]
[(rachunk? c)
(let ([c (let ([l (rachunk-l c)])
(if (and (eq? (get-local-label-offset l) (rachunk-laddr c))
(eq? (rachunk-next-offset c) offset))
c
(asm-return-address (rachunk-dest c) l (rachunk-incr-offset c) offset)))])
(f rc* (cons c c*) (fx+ offset (chunk-size c))))]
; NB: generic test, so must be last!
[(chunk? c) (f rc* (cons c c*) (fx+ offset (chunk-size c)))]
[else (sorry! who "unexpected chunk ~s" c)])))))
(define (asm-fixup-opnd x)
(define-syntax tc-offset-map
(let ([q (datum->syntax #'*
(map (lambda (x) (cons (caddr x) (string->symbol (format "$~s" (car x)))))
(getprop 'tc '*fields*)))])
(lambda (x) #`'#,q)))
(if (pair? x)
(record-case x
[(library) (x) `(library ,(libspec-name x))]
[(library-code) (x) `(library-code ,(libspec-name x))]
[(entry) (i) `(entry ,(vector-ref (constant c-entry-name-vector) i))]
[(disp) (offset reg)
(cond
[(and (eq? reg %tc) (assv offset tc-offset-map)) => cdr]
[else `(disp ,offset ,(reg-name reg))])]
[(index) (offset reg1 reg2)
`(index ,offset ,(reg-name reg1) ,(reg-name reg2))]
[(reg) r (reg-name r)]
[(label) (offset l)
(if (local-label? l)
(parameterize ([print-gensym 'pretty/suffix])
(format "~s(~d)" l offset))
(format "~s" l))]
[else x])
x))
(define (extract-trace-code code*)
(let-values ([(trace* size)
(let f ([code* code*])
(if (null? code*)
(values '() 0)
(let ([code (car code*)])
(let-values ([(trace* offset) (f (cdr code*))])
(record-case code
[(asm) (op . opnd*)
(values
`((,offset ,op ,@(map asm-fixup-opnd opnd*)) ,@trace*)
offset)]
[(label) l*
(values
(if (null? l*)
trace*
`((,l* label) ,@trace*))
offset)]
[else (values trace* (fx+ (asm-size code) offset))])))))])
trace*))
(define (extract-code c*)
(let f ([c* c*])
(if (null? c*)
'()
(let ([c (car c*)])
(let ([code (append (chunk-code* (car c*)) (f (cdr c*)))])
(if (and aop (lchunk? c))
(let ([l (lchunk-l c)])
(if l (cons `(label ,l) code) code))
code))))))
(let f ([c* c*] [size size] [iteration 2])
(if munge-recur?
(begin
(set! munge-recur? #f)
(let-values ([(c* new-size) (munge-pass c* iteration)])
(f c* new-size (fx+ iteration 1))))
(let ([code* (extract-code c*)])
(if aop
(values
(remp (lambda (code) (record-case code [(asm label) stuff #t] [else #f])) code*)
(extract-trace-code code*)
size)
(values code* '() size)))))))
; TODO: teach c-mkcode & c-faslcode how to indirect labels
(define-who resolve-funcrel!
(lambda (funcrel)
(let* ([l (cadr funcrel)] [code ($c-func-code-record (local-label-func l))])
(record-case code
[(code) (func subtype free name arity-mask size code-list info)
(set-car!
funcrel
(let ([offset (local-label-offset l)])
(if offset
(fx+ (fx- size offset) (car funcrel) (constant code-data-disp))
(car funcrel))))
(set-car! (cdr funcrel) code)]
[else (sorry! who "unexpected record ~s" code)]))))
(define touch-label!
(lambda (l)
(unless (libspec-label? l) (local-label-iteration-set! l 1))))
(define LambdaBody
(lambda (entry-block* block* func)
#;(when (#%$assembly-output)
(p-dot-graph block* (current-output-port))
(p-graph block* 'whatever (current-output-port) unparse-L16))
(let ([block* (cons (car entry-block*) (remq (car entry-block*) block*))])
(for-each (lambda (block) (let ([l (block-label block)]) (when l (local-label-iteration-set! l 0) (local-label-func-set! l func)))) block*)
(fluid-let ([current-func func])
(let loop ([block* (reverse block*)] [chunk* '()] [offset 0])
(if (null? block*)
(munge chunk* offset)
(let ([block (car block*)])
(let-values ([(code* chunk* offset) (Block block chunk* offset)])
(let ([chunk (make-lchunk (block-label block) code*)])
(let ([offset (fx+ (chunk-size chunk) offset)])
(let ([l (block-label block)])
(when l
(local-label-offset-set! l offset)
(when (fx= (local-label-iteration l) 1) (set! munge-recur? #t))))
(loop (cdr block*) (cons chunk chunk*) offset)))))))))))
(define Block
(lambda (block chunk* offset)
(let f ([e* (block-effect* block)])
(if (null? e*)
(Exit block chunk* offset)
(let-values ([(code* chunk* offset) (f (cdr e*))])
(Effect (car e*) code* chunk* offset))))))
(define Exit
(lambda (block chunk* offset)
(define do-goto
(lambda (b)
(let ([l (block-label b)])
(safe-assert l)
(touch-label! l)
(let ([chunk (asm-jump l offset)])
(values '() (cons chunk chunk*) (fx+ (chunk-size chunk) offset))))))
(cond
[(goto-block? block) (do-goto (goto-block-next block))]
[(joto-block? block) (do-goto (joto-block-next block))]
[(if-block? block)
(let ([l1 (block-label (if-block-true block))] [l2 (block-label (if-block-false block))])
(safe-assert l1 l2)
(touch-label! l1)
(touch-label! l2)
(let-values ([(code* chunk) (Pred (if-block-pred block) l1 l2 offset)])
(values code* (cons chunk chunk*) (fx+ (chunk-size chunk) offset))))]
[(tail-block? block) (Tail (tail-block-tail block) chunk* offset)]
[(newframe-block? block) (do-goto (newframe-block-next block))]
[else (sorry! who "unrecognized block ~s" block)]))))
(Tail : Tail (ir chunk* offset) -> * (code* chunk* offset)
[(asm-return) (values (asm-return) chunk* offset)]
[(asm-c-return ,info) (values (asm-c-return info) chunk* offset)]
[(jump (label-ref ,l ,offset0))
(values (asm-direct-jump l offset0) chunk* offset)]
[(jump (literal ,info))
(values (asm-literal-jump info) chunk* offset)]
[(jump ,t)
(values (asm-indirect-jump t) chunk* offset)]
[(goto ,l)
(safe-assert (libspec-label? l))
(values (asm-library-jump l) chunk* offset)])
(Program : Program (ir) -> * (code)
[(labels ([,l* ,[Lambda->func : le* -> func*]] ...) ,l)
(define-syntax traceit
(syntax-rules (x)
[(_ name) (set! name (let ([t name]) (lambda args (apply t args))))]))
(fluid-let ([funcrel* '()] [aop ($assembly-output)] [munge-recur? #f])
(for-each local-label-func-set! l* func*)
(let ([ptrace* (map CaseLambdaExpr le* func*)])
(for-each resolve-funcrel! funcrel*)
(when aop
(for-each (lambda (ptrace) (ptrace aop)) ptrace*)
(flush-output-port aop))
(local-label-func l)))])
(Lambda->func : CaseLambdaExpr (ir) -> * (func)
[(lambda ,info (,entry-block* ...) (,block* ...)) (make-$c-func)])
; the final version of code* (which has things resolved)
(CaseLambdaExpr : CaseLambdaExpr (ir func) -> * ()
[(lambda ,info (,entry-block* ...) (,block* ...))
#;(let ()
(define block-printer
(lambda (unparser name block*)
(p-dot-graph block* (current-output-port))
(p-graph block* name (current-output-port) unparser)))
(block-printer unparse-L16 (info-lambda-name info) block*))
(let-values ([(code* trace* code-size) (LambdaBody entry-block* block* func)])
($c-make-code
func
(info-lambda-flags info)
(length (info-lambda-fv* info))
(info-lambda-name info)
(interface*->mask (info-lambda-interface* info))
code-size
code*
(cond
[(info-lambda-ctci info) =>
(lambda (ctci)
(include "types.ss")
(make-code-info
(info-lambda-src info)
(info-lambda-sexpr info)
(and (eq? (info-lambda-closure-rep info) 'closure)
(let f ([fv* (info-lambda-fv* info)] [n 0])
(if (null? fv*)
(make-vector n #f)
(let ([v (f (cdr fv*) (fx+ n 1))])
(cond
[(uvar-source (car fv*)) =>
(lambda (source) (vector-set! v n (unannotate source)))])
v))))
(ctci-live ctci)
(let ([v (vector-map
(let ([n (fx+ (constant code-data-disp) (constant size-rp-header) code-size)])
(lambda (ctrpi)
(make-rp-info
(fx- n (local-label-offset (ctrpi-label ctrpi)))
(ctrpi-src ctrpi)
(ctrpi-sexpr ctrpi)
(ctrpi-mask ctrpi))))
(list->vector (ctci-rpi* ctci)))])
(vector-sort! (lambda (x y) (fx< (rp-info-offset x) (rp-info-offset y))) v)
v)))]
[(and (generate-procedure-source-information)
(info-lambda-src info)) =>
(lambda (src)
(include "types.ss")
(make-code-info src #f #f #f #f))]
[else #f])
(info-lambda-pinfo* info))
(lambda (p) (c-trace (info-lambda-name info) code-size trace* p)))])
(Effect : Effect (ir code* chunk* offset) -> * (code* chunk* offset)
[(rp-header ,mrvl ,fs ,lpm) (values (asm-rp-header code* mrvl fs lpm current-func #f) chunk* offset)]
[(set! ,x (label-ref ,l ,offset1))
(guard (eq? (local-label-func l) current-func))
(let ([chunk (make-chunk code*)])
(let ([offset (fx+ (chunk-size chunk) offset)] [chunk* (cons chunk chunk*)])
(let ([chunk (asm-return-address x l offset1 offset)])
(values '() (cons chunk chunk*) (fx+ (chunk-size chunk) offset)))))]
[(set! ,lvalue (asm ,info ,proc ,t* ...)) (values (apply proc code* lvalue t*) chunk* offset)]
[(set! ,lvalue ,rhs) (values (asm-move code* lvalue rhs) chunk* offset)]
[(asm ,info ,proc ,t* ...) (values (apply proc code* t*) chunk* offset)])
(Pred : Pred (ir l1 l2 offset) -> * (code* chunk)
[(asm ,info ,proc ,t* ...) (apply proc l1 l2 offset t*)])
(Program ir))
(define-pass Triv->rand : (L16 Triv) (ir) -> * (operand)
(Triv : Triv (ir) -> * (operand)
[,x (cons 'reg x)]
[(mref ,x1 ,x2 ,imm)
(if (eq? x2 %zero)
`(disp ,imm ,x1)
`(index ,imm ,x2 ,x1))]
[(literal ,info)
`(,(if (info-literal-indirect? info) 'literal@ 'literal)
,(info-literal-offset info)
,(let ([type (info-literal-type info)])
(if (eq? type 'closure)
($c-make-closure (local-label-func (info-literal-addr info)))
`(,type ,(info-literal-addr info)))))]
[(immediate ,imm) `(imm ,imm)]
[(label-ref ,l ,offset) (make-funcrel 'literal l offset)])
(Triv ir))
(define build-mem-opnd
(lambda (base index offset)
(let ([offset (nanopass-case (L16 Triv) offset [(immediate ,imm) imm])])
(if (eq? index %zero)
`(disp ,offset ,base)
`(index ,offset ,base ,index)))))
(define asm-size*
(lambda (x*)
(fold-left (lambda (size x) (fx+ size (asm-size x))) 0 x*)))
(define-syntax Trivit
(syntax-rules ()
[(_ (x ...) b0 b1 ...) (let ([x (Triv->rand x)] ...) b0 b1 ...)]))
(define-syntax aop-cons*
(syntax-rules ()
[(_ asm e1 e2 ...)
(let ([ls (cons* e1 e2 ...)])
(if aop (cons asm ls) ls))]))
(define interface*->mask
(lambda (i*)
(fold-left (lambda (mask i)
(logor mask
(if (< i 0)
(- (ash 1 (- -1 i)))
(ash 1 i))))
0 i*)))
(architecture assembler)
(import asm-module))
(module (np-allocate-registers)
(define-threaded spillable*)
(define-threaded unspillable*)
(define-threaded max-fv)
(define-threaded max-fs@call)
(define-threaded poison-cset)
(define no-live* empty-tree)
(define union-live
; union live1 and live2. result is eq? to live1 if result is same as live1.
(lambda (live1 live2 live-size)
(tree-merge live1 live2 live-size)))
(define same-live?
(lambda (live1 live2)
(tree-same? live1 live2)))
(define live?
(lambda (live* live-size x)
(tree-bit-set? live* live-size (var-index x))))
(define get-live-vars
(lambda (live* live-size v)
(tree-extract live* live-size v)))
(define make-add-var
(lambda (live-size)
; add x to live*. result is eq? to live* if x is already in live*.
(lambda (live* x)
(let ([index (var-index x)])
(if index
(let ([new (tree-bit-set live* live-size index)])
(safe-assert (or (eq? new live*) (not (tree-same? new live*))))
new)
live*)))))
(define make-remove-var
; remove x from live*. result is eq? to live* if x is not in live*.
(lambda (live-size)
(lambda (live* x)
(let ([index (var-index x)])
(if index
(let ([new (tree-bit-unset live* live-size (var-index x))])
(safe-assert (or (eq? new live*) (not (tree-same? new live*))))
new)
live*)))))
(module (make-empty-cset make-full-cset cset-full? conflict-bit-set! conflict-bit-unset! conflict-bit-set? conflict-bit-count cset-merge! cset-copy cset-for-each extract-conflicts)
(define-record-type cset
(nongenerative)
(fields size (mutable tree)))
(define make-empty-cset
(lambda (size)
(make-cset size empty-tree)))
(define make-full-cset
(lambda (size)
(make-cset size full-tree)))
(define cset-full?
(lambda (cset)
(eq? (cset-tree cset) full-tree)))
(define conflict-bit-set!
(lambda (cset offset)
(cset-tree-set! cset
(tree-bit-set (cset-tree cset) (cset-size cset) offset))))
(define conflict-bit-unset!
(lambda (cset offset)
(cset-tree-set! cset
(tree-bit-unset (cset-tree cset) (cset-size cset) offset))))
(define conflict-bit-set?
(lambda (cset offset)
(tree-bit-set? (cset-tree cset) (cset-size cset) offset)))
(define conflict-bit-count
(lambda (cset)
(tree-bit-count (cset-tree cset) (cset-size cset))))
(define cset-merge!
(lambda (cset1 cset2)
(cset-tree-set! cset1 (tree-merge (cset-tree cset1) (cset-tree cset2) (cset-size cset1)))))
(define cset-copy
(lambda (cset)
(make-cset (cset-size cset) (cset-tree cset))))
(define cset-for-each
(lambda (cset proc)
(tree-for-each (cset-tree cset) (cset-size cset) 0 (cset-size cset) proc)))
(define extract-conflicts
(lambda (cset v)
(tree-extract (cset-tree cset) (cset-size cset) v)))
)
(define do-live-analysis!
(lambda (live-size entry-block*)
(define add-var (make-add-var live-size))
(define remove-var (make-remove-var live-size))
(define-who scan-block
; if we maintain a list of kills and a list of useless variables for
; each block, and we discover on entry to scan-block that the useless
; variables are still useless (not live in "out"), we can compute the
; new in set without scanning the block by removing the kills from
; the out set and unioning the result with the saved in set. should
; try this and see if it is enough of a win to justify the added
; complexity.
(lambda (block out)
(define Triv
(lambda (out t)
(nanopass-case (L15a Triv) t
[(mref ,x1 ,x2 ,imm) (add-var (add-var out x2) x1)]
[,x (add-var out x)]
[else out])))
(define Rhs
(lambda (out rhs)
(nanopass-case (L15a Rhs) rhs
[(inline ,info ,value-prim ,t* ...)
(let* ([out (if (info-kill*? info) (fold-left remove-var out (info-kill*-kill* info)) out)]
[out (if (info-kill*-live*? info) (fold-left add-var out (info-kill*-live*-live* info)) out)])
(fold-left Triv out t*))]
[else (Triv out rhs)])))
(define Pred
(lambda (out p)
(nanopass-case (L15a Pred) p
[(inline ,live-info ,info ,pred-prim ,t* ...)
(let* ([out (if (info-kill*? info) (fold-left remove-var out (info-kill*-kill* info)) out)]
[out (if (info-kill*-live*? info) (fold-left add-var out (info-kill*-live*-live* info)) out)])
(live-info-live-set! live-info out)
(fold-left Triv out t*))]
[else (sorry! who "unexpected pred ~s" p)])))
(define Tail
(lambda (out tl)
(nanopass-case (L15a Tail) tl
[(goto ,l)
(safe-assert (libspec-label? l))
(fold-left add-var no-live* (libspec-label-live-reg* l))]
[(asm-return ,reg* ...)
(safe-assert (eq? out no-live*))
(fold-left add-var no-live* reg*)]
[(asm-c-return ,info ,reg* ...)
(safe-assert (eq? out no-live*))
(fold-left add-var no-live* reg*)]
[(jump ,live-info ,t (,var* ...))
(let ([out (fold-left add-var out var*)])
(live-info-live-set! live-info out)
(Triv out t))]
[else (sorry! who "unexpected tail instruction ~s" tl)])))
(define Effect*
(lambda (out instr*)
(fold-left
(lambda (out instr)
(nanopass-case (L15a Effect) instr
[(set! ,live-info ,x ,rhs)
(if (var-index x)
(let ([new-out (remove-var out x)])
(if (and (eq? new-out out)
(nanopass-case (L15a Rhs) rhs
[(inline ,info ,value-prim ,t* ...) (primitive-pure? value-prim)]
[else #t]))
(begin
(live-info-useless-set! live-info #t)
out)
(begin
(live-info-useless-set! live-info #f)
(live-info-live-set! live-info new-out)
(Rhs new-out rhs))))
(begin
(live-info-live-set! live-info out)
(Rhs out rhs)))]
[(set! ,live-info (mref ,x1 ,x2 ,imm) ,rhs)
(live-info-live-set! live-info out)
(Rhs (add-var (add-var out x1) x2) rhs)]
[(inline ,live-info ,info ,effect-prim ,t* ...)
(let ([out (if (info-kill*? info) (fold-left remove-var out (info-kill*-kill* info)) out)])
(live-info-live-set! live-info out)
(let ([out (fold-left Triv out t*)])
(if (info-kill*-live*? info)
(fold-left add-var out (info-kill*-live*-live* info))
out)))]
[(remove-frame ,live-info ,info) (live-info-live-set! live-info out) out]
[(restore-local-saves ,live-info ,info) (live-info-live-set! live-info out) out]
[(shift-arg ,live-info ,reg ,imm ,info) (live-info-live-set! live-info out) out]
[(overflow-check ,live-info) (live-info-live-set! live-info out) out]
[(overflood-check ,live-info) (live-info-live-set! live-info out) out]
[(fcallable-overflow-check ,live-info) (live-info-live-set! live-info out) out]
[(check-live ,live-info ,reg* ...) (live-info-live-set! live-info out) out]
[else out]))
out instr*)))
; NB: consider storing instructions in reverse order back in expose-basic-blocks
(let ([effect* (reverse (block-effect* block))])
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (Effect* out effect*)]
[(if-block? block) (Effect* (Pred out (if-block-pred block)) effect*)]
[(tail-block? block) (Effect* (Tail out (tail-block-tail block)) effect*)]
[else (sorry! who "unrecognized block ~s" block)]))))
(define force-live-in!
(lambda (block)
(when (eq? (block-live-in block) 'uninitialized)
(if (block-seen? block)
; think we need need not recur on in-link* here even though we changed in
; - if an in-link is seen, it's already on the worklist
; - if an in-link is not seen, we must not have visited it yet or it would
; have already forced us. someone will visit it later unless it's
; orphaned, and we think we have no orphaned blocks
(block-live-in-set! block no-live*)
(begin
(block-seen! block #t)
(do-live! block))))))
(define different?
(lambda (out old-out)
(or (eq? old-out 'uninitialized)
(not (same-live? out old-out)))))
(define propagate-live!
(lambda (block out)
; NB: could record out, and if out hasn't changed, skip the scan
(let ([in (scan-block block out)])
(when (different? in (block-live-in block))
(block-live-in-set! block in)
(let f ([block* (block-in-link* block)])
(unless (null? block*)
(let ([block (car block*)])
(if (block-seen? block)
(f (cdr block*))
(begin
(block-seen! block #t)
(f (cdr block*))
(do-live! block))))))))))
(define-who do-live!
(lambda (block)
(safe-assert (block-seen? block))
(cond
[(goto-block? block)
(let ([next-block (goto-block-next block)])
(force-live-in! next-block)
(block-seen! block #f)
(propagate-live! block (block-live-in next-block)))]
[(if-block? block)
(let ([true-block (if-block-true block)] [false-block (if-block-false block)])
(force-live-in! true-block)
(force-live-in! false-block)
(block-seen! block #f)
(let ([out (union-live (block-live-in true-block) (block-live-in false-block) live-size)])
(when (different? out (if-block-live-out block))
(if-block-live-out-set! block out)
(propagate-live! block out))))]
[(joto-block? block)
(let ([next-block (joto-block-next block)])
(force-live-in! next-block)
(block-seen! block #f)
(propagate-live! block
(let loop ([nfv* (joto-block-nfv* block)] [i 1] [next (block-live-in next-block)])
(if (or (null? nfv*) (fx> i max-fv))
next
(loop (cdr nfv*) (fx+ i 1)
(let ([new-next (remove-var next (get-fv i))])
(if (eq? new-next next)
next
(add-var next (car nfv*)))))))))]
[(newframe-block? block)
(let ([next-block (newframe-block-next block)]
[rp-block* (newframe-block-rp* block)]
[rp-block (newframe-block-rp block)])
(force-live-in! next-block)
(for-each force-live-in! rp-block*)
(force-live-in! rp-block)
(block-seen! block #f)
(let ([rp (block-live-in rp-block)] [newframe-info (newframe-block-info block)])
(let ([call (if (eq? (newframe-block-live-rp block) rp)
(newframe-block-live-call block)
(begin
(newframe-block-live-rp-set! block rp)
(let ([call (add-var
(fold-left
(lambda (live* x*) (fold-left remove-var live* x*))
rp
(cons*
; could base set of registers to kill on expected return values
(reg-cons* %ret %ac0 arg-registers)
(info-newframe-cnfv* newframe-info)
(info-newframe-nfv** newframe-info)))
(get-fv 0))])
(newframe-block-live-call-set! block call)
call)))])
(let ([out (union-live
(fold-left (lambda (live b) (union-live (block-live-in b) live live-size))
(block-live-in next-block) rp-block*)
(fold-left add-var call (info-newframe-cnfv* newframe-info))
live-size)])
(when (different? out (newframe-block-live-out block))
(newframe-block-live-out-set! block out)
(propagate-live! block out))))))]
[(tail-block? block)
(block-seen! block #f)
(propagate-live! block no-live*)]
[else (sorry! who "unrecognized block ~s" block)])))
(for-each
(lambda (entry-block)
(when (eq? (block-live-in entry-block) 'uninitialized)
(block-seen! entry-block #t)
(do-live! entry-block)))
entry-block*)))
(define-who check-entry-live!
; when enabled, spits out messages about uvars and unexpected registers that are live
; on entry. there should never be any live uvars. for procedures that started life
; as ordinary lambda expressions, there shouldn't be anything but ac0, cp, and argument
; registers, which we weed out here. for library routines, there are often additional
; registers, sometimes for good reason and sometimes because we are lazy and didn't give
; outselves a mechanism to prune out unneeded saves and restores. for foreign-callable
; procedures, C argument registers and callee-save registers might show up live.
; we could enable a variant of this always that just checks normal procedures. also,
; it might be nice to make it a bit more efficient, though it probably doesn't matter.
(lambda (name live-size varvec entry-block*)
(for-each
(lambda (entry-block)
(define okay-live?
(lambda (x)
(or (fv? x)
(eq? x %ac0)
(meta-cond
[(real-register? '%cp) (eq? x %cp)]
[else #f])
(memq x arg-registers))))
(let ([undead (remp okay-live? (get-live-vars (block-live-in entry-block) live-size varvec))])
(unless (null? undead)
(printf "Warning: live on entry to ~a: ~s\n" name undead))))
entry-block*)))
(define-who record-call-live!
(lambda (block* varvec)
(for-each
(lambda (block)
(when (newframe-block? block)
(let ([newframe-info (newframe-block-info block)])
(let ([call-live* (get-live-vars (newframe-block-live-call block) (vector-length varvec) varvec)])
(for-each
(lambda (x)
(define fixnum (lambda (x) (if (fixnum? x) x (most-positive-fixnum))))
(when (uvar? x)
(uvar-spilled! x #t)
(unless (block-pariah? block)
(uvar-save-weight-set! x
(fixnum
(+ (uvar-save-weight x)
(* (info-newframe-weight newframe-info) 2)))))))
call-live*)
(info-newframe-call-live*-set! newframe-info call-live*)))))
block*)))
; maintain move sets as (var . weight) lists, sorted by weight (largest first)
; 2014/06/26: allx move set size averages .79 elements with a max of 12, so no
; need for anything fancier than this weighted version of insertion sort
(define $add-move!
(lambda (x1 x2 weight)
(when (uvar? x1)
(when (or (not (uvar-poison? x1)) (fv? x2))
(uvar-move*-set! x1
(call-with-values
(lambda ()
(let f ([move* (uvar-move* x1)])
(if (null? move*)
(values (cons x2 weight) move*)
(let ([move (car move*)] [move* (cdr move*)])
(if (eq? (car move) x2)
(values (cons (car move) (fx+ (cdr move) weight)) move*)
(let-values ([(move2 move*) (f move*)])
(if (fx> (cdr move2) (cdr move))
(values move2 (cons move move*))
(values move (cons move2 move*)))))))))
cons))))))
(define-who identify-poison!
(lambda (kspillable varvec live-size block*)
(define kpoison 0)
(define increment-live-counts!
(lambda (live)
(tree-for-each live live-size 0 kspillable
(lambda (offset)
(let ([x (vector-ref varvec offset)])
(let ([range (fx+ (uvar-live-count x) 1)])
(when (fx= range 2)
(uvar-poison! x #t)
(set! kpoison (fx+ kpoison 1)))
(uvar-live-count-set! x range)))))))
(define Effect
(lambda (live* e)
(nanopass-case (L15a Effect) e
[(set! ,live-info ,x ,rhs)
(guard (uvar? x))
(if (live-info-useless live-info)
live*
(cons (live-info-live live-info) live*))]
[else live*])))
(let ([vlive (list->vector (fold-left (lambda (live* block) (fold-left Effect live* (block-effect* block))) '() block*))])
(let ([nvlive (vector-length vlive)])
(let refine ([skip 64] [stride 64])
(do ([i (fx- skip 1) (fx+ i stride)])
((fx>= i nvlive))
(increment-live-counts! (vector-ref vlive i)))
(unless (or (fx= stride 16) (< (* (fx- kspillable kpoison) (fx* stride 2)) 1000000))
(refine (fxsrl skip 1) skip)))))))
(define-who do-spillable-conflict!
(lambda (kspillable kfv varvec live-size block*)
(define remove-var (make-remove-var live-size))
(define add-move!
(lambda (x1 x2)
(when (var-index x2)
($add-move! x1 x2 2)
($add-move! x2 x1 2))))
(define add-conflict!
(lambda (x out)
; invariants:
; all poison spillables explicitly point to all spillables
; all non-poison spillables implicitly point to all poison spillables via poison-cset
(let ([x-offset (var-index x)])
(when x-offset
(if (and (fx< x-offset kspillable) (uvar-poison? x))
(tree-for-each out live-size kspillable (fx+ kspillable kfv)
(lambda (y-offset)
; frame y -> poison spillable x
(conflict-bit-set! (var-spillable-conflict* (vector-ref varvec y-offset)) x-offset)))
(let ([cset (var-spillable-conflict* x)])
(if (fx< x-offset kspillable)
(begin
(tree-for-each out live-size 0 kspillable
(lambda (y-offset)
(let ([y (vector-ref varvec y-offset)])
(unless (uvar-poison? y)
; non-poison spillable x -> non-poison spillable y
(conflict-bit-set! cset y-offset)
; and vice versa
(conflict-bit-set! (var-spillable-conflict* y) x-offset)))))
(tree-for-each out live-size kspillable live-size
(lambda (y-offset)
(let ([y (vector-ref varvec y-offset)])
; frame or register y -> non-poison spillable x
(conflict-bit-set! (var-spillable-conflict* y) x-offset)))))
(if (fx< x-offset (fx+ kspillable kfv))
(tree-for-each out live-size 0 kspillable
(lambda (y-offset)
; frame x -> poison or non-poison spillable y
(conflict-bit-set! cset y-offset)))
(tree-for-each out live-size 0 kspillable
(lambda (y-offset)
(unless (uvar-poison? (vector-ref varvec y-offset))
; register x -> non-poison spillable y
(conflict-bit-set! cset y-offset))))))))))))
(define Rhs
(lambda (rhs live)
(nanopass-case (L15a Rhs) rhs
[(inline ,info ,value-prim ,t* ...)
(guard (info-kill*? info))
(for-each (lambda (x) (add-conflict! x live)) (info-kill*-kill* info))]
[else (void)])))
(define Effect
(lambda (e new-effect*)
(nanopass-case (L15a Effect) e
[(set! ,live-info ,x ,rhs)
(if (live-info-useless live-info)
new-effect*
(let ([live (live-info-live live-info)])
(when (var-index x)
(if (and (var? rhs) (var-index rhs))
(begin
(add-conflict! x (remove-var live rhs))
(add-move! x rhs))
(add-conflict! x live)))
(Rhs rhs live)
(cons e new-effect*)))]
[(set! ,live-info ,lvalue ,rhs) (Rhs rhs (live-info-live live-info)) (cons e new-effect*)]
[(inline ,live-info ,info ,effect-prim ,t* ...)
(guard (info-kill*? info))
(let ([live (live-info-live live-info)])
(for-each (lambda (x) (add-conflict! x live)) (info-kill*-kill* info)))
(cons e new-effect*)]
[else (cons e new-effect*)])))
(do ([i 0 (fx+ i 1)])
((fx= i kspillable))
(let ([x (vector-ref varvec i)])
(if (uvar-poison? x)
(begin
(conflict-bit-set! poison-cset i)
; leaving each poison spillable in conflict with itself, but this shouldn't matter
; since we never ask for the degree of a poison spillable
(var-spillable-conflict*-set! x (make-full-cset kspillable)))
(var-spillable-conflict*-set! x (make-empty-cset kspillable)))))
(do ([i kspillable (fx+ i 1)])
((fx= i live-size))
(var-spillable-conflict*-set! (vector-ref varvec i) (make-empty-cset kspillable)))
(for-each
(lambda (block)
(block-effect*-set! block
(fold-right Effect '() (block-effect* block))))
block*)))
(define-who show-conflicts
(lambda (name varvec unvarvec)
(define any? #f)
(printf "\n~s conflicts:" name)
(for-each
(lambda (x)
(let ([ls (append
(let ([cset (var-spillable-conflict* x)])
(if cset (extract-conflicts cset varvec) '()))
(let ([cset (var-unspillable-conflict* x)])
(if cset (extract-conflicts cset unvarvec) '())))])
(unless (null? ls) (set! any? #t) (printf "\n~s:~{ ~s~}" x ls))))
(append spillable* unspillable* (vector->list regvec) (map get-fv (iota (fx+ max-fv 1)))))
(unless any? (printf " none"))
(newline)))
(module (assign-frame! assign-new-frame!)
(define update-conflict!
(lambda (fv spill)
(let ([cset1 (var-spillable-conflict* fv)]
[cset2 (var-spillable-conflict* spill)])
(if cset1
(cset-merge! cset1 cset2)
; tempting to set to cset2 rather than (cset-copy cset2), but this would not be
; correct for local saves, which need their unaltered sets for later, and copying
; is cheap anyway.
(var-spillable-conflict*-set! fv (cset-copy cset2))))
(unless (uvar-poison? spill) (cset-merge! (var-spillable-conflict* fv) poison-cset))))
(define assign-frame!
(lambda (spill*)
(define sort-spill*
; NB: sorts based on likelihood of successfully assigning move-related vars to the same location
; NB: probably should sort based on value of assigning move-related vars to the same location,
; NB: i.e., taking into account the ref-weight
(lambda (spill*)
(map car
(list-sort
(lambda (x y) (fx> (cdr x) (cdr y)))
(map (lambda (x)
(define relevant?
(lambda (x)
(or (fv? x) (and (uvar? x) (uvar-spilled? x)))))
(do ([move* (uvar-move* x) (cdr move*)]
[w 0 (let ([move (car move*)])
(if (relevant? (car move))
(fx+ w (cdr move))
w))])
((null? move*) (cons x w))))
spill*)))))
(define find-move-related-home
(lambda (x0 succ fail)
(define conflict-fv?
(lambda (x fv)
(let ([cset (var-spillable-conflict* fv)])
(and cset (conflict-bit-set? cset (var-index x))))))
(let f ([x x0] [work* '()] [clear-seen! void])
(if (uvar-seen? x)
(if (null? work*) (begin (clear-seen!) (fail)) (f (car work*) (cdr work*) clear-seen!))
(let ([clear-seen! (lambda () (uvar-seen! x #f) (clear-seen!))])
(uvar-seen! x #t)
(let loop ([move* (uvar-move* x)] [work* work*])
(if (null? move*)
(if (null? work*) (begin (clear-seen!) (fail)) (f (car work*) (cdr work*) clear-seen!))
(let ([var (caar move*)] [move* (cdr move*)])
(define try-fv
(lambda (fv)
(if (conflict-fv? x0 fv)
(loop move* work*)
(begin
(safe-assert (not (eq? fv (get-fv 0))))
(begin (clear-seen!) (succ fv))))))
(if (fv? var)
(try-fv var)
(if (uvar? var)
(let ([fv (uvar-location var)])
(if (fv? fv)
(try-fv fv)
(loop move* (cons var work*))))
(loop move* work*)))))))))))
(define find-home!
(lambda (spill max-fv first-open)
(define return
(lambda (home max-fv first-open)
(uvar-location-set! spill home)
(update-conflict! home spill)
(values max-fv first-open)))
(find-move-related-home spill
(lambda (home) (return home max-fv first-open))
(lambda ()
(let f ([first-open first-open])
(let* ([fv (get-fv first-open)] [cset (var-spillable-conflict* fv)])
(if (and cset (cset-full? cset))
(f (fx+ first-open 1))
(let ([spill-offset (var-index spill)])
(let f ([fv-offset first-open] [fv fv] [cset cset])
(if (and cset (conflict-bit-set? cset spill-offset))
(let* ([fv-offset (fx+ fv-offset 1)] [fv (get-fv fv-offset)] [cset (var-spillable-conflict* fv)])
(f fv-offset fv cset))
(return fv (fxmax fv-offset max-fv) first-open)))))))))))
(define find-homes!
(lambda (spill* max-fv first-open)
(if (null? spill*)
max-fv
(let-values ([(max-fv first-open) (find-home! (car spill*) max-fv first-open)])
(find-homes! (cdr spill*) max-fv first-open)))))
; NOTE: call-live uvars should be sorted so that those that are call-live with few other
; variables are earlier in the list (and more likely to get a low frame location);
; additionally if they are live across many frames they should be prioritized over those
; live across only a few (only when setup-nfv?)
(set! max-fv (find-homes! (sort-spill* spill*) max-fv 1))))
(define-pass assign-new-frame! : (L15a Dummy) (ir lambda-info live-size varvec block*) -> (L15b Dummy) ()
(definitions
(define remove-var (make-remove-var live-size))
(define find-max-fv
(lambda (call-live*)
(fold-left
(lambda (call-max-fv x)
(fxmax (fv-offset (if (uvar? x) (uvar-location x) x)) call-max-fv))
-1 call-live*)))
(define cool?
(lambda (base nfv*)
(let loop ([nfv* nfv*] [offset base])
(or (null? nfv*)
(and (or (not (car nfv*))
(let ([cset (var-spillable-conflict* (get-fv offset))])
(not (and cset (conflict-bit-set? cset (var-index (car nfv*)))))))
(loop (cdr nfv*) (fx+ offset 1)))))))
(define assign-new-frame!
(lambda (cnfv* nfv** call-live*)
(define set-offsets!
(lambda (nfv* offset)
(if (null? nfv*)
(set! max-fv (fxmax offset max-fv))
(let ([nfv (car nfv*)] [home (get-fv offset)])
(uvar-location-set! nfv home)
(update-conflict! home nfv)
(set-offsets! (cdr nfv*) (fx+ offset 1))))))
(let ([arg-offset (fx+ (length cnfv*) 1)]) ; +1 for return address slot
(let loop ([base (fx+ (find-max-fv call-live*) 1)])
(let ([arg-base (fx+ base arg-offset)])
(if (and (cool? base cnfv*) (andmap (lambda (nfv*) (cool? arg-base nfv*)) nfv**))
(begin
(set! max-fs@call (fxmax max-fs@call base)) ; max frame size @ call in ptrs
(set-offsets! cnfv* base)
(for-each (lambda (nfv*) (set-offsets! nfv* arg-base)) nfv**)
base)
(loop (fx+ base 1))))))))
(define build-mask
(lambda (index*)
(define bucket-width (if (fx> (fixnum-width) 32) 32 16))
(let* ([nbits (fx+ (fold-left (lambda (m index) (fxmax m index)) -1 index*) 1)]
[nbuckets (fxdiv (fx+ nbits (fx- bucket-width 1)) bucket-width)]
[buckets (make-fxvector nbuckets 0)])
(for-each
(lambda (index)
(let-values ([(i j) (fxdiv-and-mod index bucket-width)])
(fxvector-set! buckets i (fxlogbit1 j (fxvector-ref buckets i)))))
index*)
(let f ([base 0] [len nbuckets])
(if (fx< len 2)
(if (fx= len 0)
0
(fxvector-ref buckets base))
(let ([half (fxsrl len 1)])
(logor
(bitwise-arithmetic-shift-left (f (fx+ base half) (fx- len half)) (fx* half bucket-width))
(f base half))))))))
(define build-live-pointer-mask
(lambda (live*)
(build-mask
(fold-left
(lambda (index* live)
(define (cons-fv fv index*)
(let ([offset (fv-offset fv)])
(if (fx= offset 0) ; no bit for fv0
index*
(cons (fx- offset 1) index*))))
(cond
[(fv? live) (cons-fv live index*)]
[(eq? (uvar-type live) 'ptr) (cons-fv (uvar-location live) index*)]
[else index*]))
'() live*))))
(define (process-info-newframe! info)
(unless (info-newframe-frame-words info)
(let ([call-live* (info-newframe-call-live* info)])
(info-newframe-frame-words-set! info
(let ([cnfv* (info-newframe-cnfv* info)])
(fx+ (assign-new-frame! cnfv* (cons (info-newframe-nfv* info) (info-newframe-nfv** info)) call-live*)
(length cnfv*))))
(info-newframe-local-save*-set! info
(filter (lambda (x) (and (uvar? x) (uvar-local-save? x))) call-live*)))))
(define record-inspector-info!
(lambda (src sexpr rpl call-live* lpm)
(safe-assert (if call-live* rpl (not rpl)))
(cond
[(and call-live* (info-lambda-ctci lambda-info)) =>
(lambda (ctci)
(let ([mask (build-mask
(fold-left
(lambda (i* x)
(cond
[(and (uvar? x) (uvar-iii x)) =>
(lambda (index)
(safe-assert
(let ([name.offset (vector-ref (ctci-live ctci) index)])
(logbit? (fx- (cdr name.offset) 1) lpm)))
(cons index i*))]
[else i*]))
'() call-live*))])
(when (or src sexpr (not (eqv? mask 0)))
(ctci-rpi*-set! ctci (cons (make-ctrpi rpl src sexpr mask) (ctci-rpi* ctci))))))]))))
(Pred : Pred (ir) -> Pred ())
(Tail : Tail (ir) -> Tail ()
[(jump ,live-info ,[t] (,var* ...)) `(jump ,live-info ,t)]
[(asm-return ,reg* ...) `(asm-return)]
[(asm-c-return ,info ,reg* ...) `(asm-c-return ,info)])
(Effect : Effect (ir) -> Effect ())
(foldable-Effect : Effect (ir new-effect*) -> * (new-effect*)
[(return-point ,info ,rpl ,mrvl (,cnfv* ...))
(process-info-newframe! info)
(let ([lpm (build-live-pointer-mask (append cnfv* (info-newframe-call-live* info)))])
(record-inspector-info! (info-newframe-src info) (info-newframe-sexpr info) rpl (info-newframe-call-live* info) lpm)
(with-output-language (L15b Effect)
(safe-assert (< -1 lpm (ash 1 (fx- (info-newframe-frame-words info) 1))))
(cons `(rp-header ,mrvl ,(fx* (info-newframe-frame-words info) (constant ptr-bytes)) ,lpm) new-effect*)))]
[(remove-frame ,live-info ,info)
(process-info-newframe! info)
(with-output-language (L15b Effect)
(let ([live (live-info-live live-info)])
(cons*
`(fp-offset ,live-info ,(fx- (fx* (info-newframe-frame-words info) (constant ptr-bytes))))
`(overflood-check ,(make-live-info live))
new-effect*)))]
[(restore-local-saves ,live-info ,info)
(with-output-language (L15b Effect)
(let ([live (live-info-live live-info)])
(let loop ([x* (filter (lambda (x) (live? live live-size x)) (info-newframe-local-save* info))]
[live live]
[new-effect* new-effect*])
(if (null? x*)
new-effect*
(let* ([x (car x*)] [live (remove-var live x)])
(loop (cdr x*) live
(cons `(set! ,(make-live-info live) ,x ,(uvar-location x)) new-effect*)))))))]
[(shift-arg ,live-info ,reg ,imm ,info)
(process-info-newframe! info)
(with-output-language (L15b Effect)
(let ([frame-words (info-newframe-frame-words info)])
(safe-assert (not (fx= frame-words 0)))
(let ([shift-offset (fx* frame-words (constant ptr-bytes))])
(safe-assert (fx> shift-offset 0))
(cons `(set! ,live-info (mref ,reg ,%zero ,imm) (mref ,reg ,%zero ,shift-offset)) new-effect*))))]
[(check-live ,live-info ,reg* ...)
(let ([live (fold-left (lambda (live reg)
(let ([t (remove-var live reg)])
(when (eqv? t live) (sorry! who "(check-live) ~s is not live" reg))
t))
(live-info-live live-info)
reg*)])
(unless (eqv? live no-live*)
(sorry! who "(check-live) unexpected live vars ~s" (get-live-vars live live-size varvec))))
new-effect*]
[else (cons (Effect ir) new-effect*)])
(begin
(for-each
(lambda (x)
; NB: experiment with different comparisions. might want ref weight
; NB: to be at least more than save weight to relieve register pressure.
(when (and (uvar-spilled? x) (not (uvar-poison? x)) (fx>= (uvar-ref-weight x) (uvar-save-weight x)))
(uvar-local-save! x #t)))
spillable*)
(for-each
(lambda (block)
(block-effect*-set! block
(fold-right foldable-Effect
(cond
[(or (goto-block? block) (joto-block? block)) '()]
[(if-block? block) (if-block-pred-set! block (Pred (if-block-pred block))) '()]
[(tail-block? block) (tail-block-tail-set! block (Tail (tail-block-tail block))) '()]
[(newframe-block? block)
(let ([info (newframe-block-info block)])
(process-info-newframe! info)
(safe-assert (andmap (lambda (x) (live? (newframe-block-live-call block) live-size x)) (info-newframe-local-save* info)))
(with-output-language (L15b Effect)
(let ([live (newframe-block-live-out block)])
(fold-left
(lambda (new-effect* x)
(let ([loc (uvar-location x)])
($add-move! x loc 2)
(cons `(set! ,(make-live-info live) ,loc ,x) new-effect*)))
(cons `(fp-offset ,(make-live-info live) ,(fx* (info-newframe-frame-words info) (constant ptr-bytes))) '())
(info-newframe-local-save* info)))))]
[else (sorry! who "unrecognized block ~s" block)])
(block-effect* block))))
block*)
(for-each
(lambda (x)
(when (uvar-local-save? x)
(uvar-location-set! x #f)
(uvar-spilled! x #f)
(uvar-save-weight-set! x 0)))
spillable*)
`(dummy))))
(define record-fp-offsets!
(lambda (block*)
(define-who record-fp-offsets!
(lambda (block cur-off)
(define Effect
(lambda (cur-off effect)
(nanopass-case (L15b Effect) effect
[(fp-offset ,live-info ,imm)
(let ([cur-off (fx+ cur-off imm)])
(safe-assert (fx>= cur-off 0))
cur-off)]
[else cur-off])))
(let ([block-off (block-fp-offset block)])
(if block-off
(unless (fx= cur-off block-off)
(sorry! who "conflicting fp-offset value for block ~s" block))
(let ([effect* (block-effect* block)])
(block-fp-offset-set! block cur-off)
(cond
[(goto-block? block)
(record-fp-offsets! (goto-block-next block) (fold-left Effect cur-off effect*))]
[(joto-block? block)
(record-fp-offsets! (joto-block-next block) 0)]
[(if-block? block)
(let ([cur-off (fold-left Effect cur-off effect*)])
(record-fp-offsets! (if-block-true block) cur-off)
(record-fp-offsets! (if-block-false block) cur-off))]
[(tail-block? block) (void)]
[(newframe-block? block)
(let ([cur-off (fold-left Effect cur-off effect*)])
(record-fp-offsets! (newframe-block-next block) cur-off)
(for-each (lambda (rp) (record-fp-offsets! rp cur-off)) (newframe-block-rp* block))
(record-fp-offsets! (newframe-block-rp block) cur-off))]
[else (sorry! who "unrecognized block ~s" block)]))))))
(for-each (lambda (block) (record-fp-offsets! block 0)) block*)))
(define-pass finalize-frame-locations! : (L15b Dummy) (ir block*) -> (L15c Dummy) ()
(definitions
(define var->loc
(lambda (x)
(or (and (uvar? x) (uvar-location x)) x)))
(define fv->mref
(lambda (x cur-off)
(if (fv? x)
(with-output-language (L15c Lvalue)
`(mref ,%sfp ,%zero ,(fx- (fx* (fv-offset x) (constant ptr-bytes)) cur-off)))
x))))
(Lvalue : Lvalue (ir cur-off) -> Lvalue ()
[(mref ,x0 ,x1 ,imm)
`(mref ,(fv->mref (var->loc x0) cur-off) ,(fv->mref (var->loc x1) cur-off) ,imm)]
[,x (fv->mref (var->loc x) cur-off)])
; NB: defining Triv & Rhs with cur-off argument so we actually get to our version of Lvalue
(Triv : Triv (ir cur-off) -> Triv ())
(Rhs : Rhs (ir cur-off) -> Rhs ())
(Pred : Pred (ir cur-off) -> Pred ())
(Tail : Tail (ir cur-off) -> Tail ())
(Effect : Effect (ir cur-off) -> Effect ())
(begin
(for-each
(lambda (block)
(block-effect*-set! block
(let f ([effect* (block-effect* block)] [cur-off (block-fp-offset block)])
(if (null? effect*)
(begin
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (void)]
[(if-block? block) (if-block-pred-set! block (Pred (if-block-pred block) cur-off))]
[(tail-block? block) (tail-block-tail-set! block (Tail (tail-block-tail block) cur-off))]
[else (sorry! who "unrecognized block ~s" block)])
'())
(with-output-language (L15c Effect)
(nanopass-case (L15b Effect) (car effect*)
[(fp-offset ,live-info ,imm)
(cons `(set! ,live-info ,%sfp
,(if (fx< imm 0)
; subtract just to make the generated code more clear
`(inline ,null-info ,%- ,%sfp (immediate ,(fx- imm)))
`(inline ,null-info ,%+ ,%sfp (immediate ,imm))))
(f (cdr effect*) (fx+ cur-off imm)))]
[(set! ,live-info ,x0 ,x1)
(let ([x0 (var->loc x0)] [x1 (var->loc x1)])
(if (eq? x0 x1)
(f (cdr effect*) cur-off)
(cons `(set! ,live-info ,(fv->mref x0 cur-off) ,(fv->mref x1 cur-off)) (f (cdr effect*) cur-off))))]
[else (cons (Effect (car effect*) cur-off) (f (cdr effect*) cur-off))]))))))
block*)
`(dummy)))
(module (select-instructions!)
(define make-tmp
(lambda (x)
(import (only np-languages make-unspillable))
(let ([tmp (make-unspillable x)])
(set! unspillable* (cons tmp unspillable*))
tmp)))
(define make-restricted-unspillable
(lambda (x reg*)
(import (only np-languages make-restricted-unspillable))
(safe-assert (andmap reg? reg*) (andmap var-index reg*))
(let ([tmp (make-restricted-unspillable x reg*)])
(set! unspillable* (cons tmp unspillable*))
tmp)))
(define make-precolored-unspillable
; instead of using machine registers like eax explicitly, we use an unspillable that
; conflicts with everything but the machine register. this is semantically equivalent
; for correct code but causes a spilled unspillable error if we try to use the same
; machine register for two conflicting variables
(lambda (name reg)
(or (reg-precolored reg)
(let ([tmp (make-restricted-unspillable name (remq reg (vector->list regvec)))])
(safe-assert (memq reg (vector->list regvec)))
(reg-precolored-set! reg tmp)
tmp))))
(define-syntax build-set!
(lambda (x)
(syntax-case x ()
[(k lhs rhs)
(with-implicit (k quasiquote with-output-language)
#`(with-output-language (L15d Effect)
`(set! ,(make-live-info) lhs rhs)))])))
(define imm?
(lambda (x)
(nanopass-case (L15c Triv) x
[(immediate ,imm) #t]
[(literal ,info) (not (info-literal-indirect? info))]
[(label-ref ,l ,offset) #t]
[else #f])))
(define imm0?
(lambda (x)
(nanopass-case (L15c Triv) x
[(immediate ,imm) (eqv? imm 0)]
[else #f])))
(define imm32?
(lambda (x)
(nanopass-case (L15c Triv) x
[(immediate ,imm)
(constant-case ptr-bits
[(32) #t] ; allows 2^31...2^32-1 per immediate?
[(64) (signed-32? imm)])] ; 2^31...2^32-1 aren't 32-bit values on 64-bit machines
[(literal ,info)
(constant-case ptr-bits
[(32) (not (info-literal-indirect? info))]
[(64) #f])]
[(label-ref ,l ,offset)
(constant-case ptr-bits
[(32) #t]
[(64) #f])]
[else #f])))
(define literal@?
(lambda (x)
(nanopass-case (L15c Triv) x
[(literal ,info) (info-literal-indirect? info)]
[else #f])))
(define mref?
(lambda (x)
(nanopass-case (L15c Triv) x
[(mref ,lvalue1 ,lvalue2 ,imm) #t]
[else #f])))
(define same?
(lambda (a b)
(or (eq? a b)
(nanopass-case (L15c Triv) a
[(mref ,lvalue11 ,lvalue12 ,imm1)
(nanopass-case (L15c Triv) b
[(mref ,lvalue21 ,lvalue22 ,imm2)
(and (or (and (eq? lvalue11 lvalue21) (eq? lvalue12 lvalue22))
(and (eq? lvalue11 lvalue22) (eq? lvalue12 lvalue21)))
(eqv? imm1 imm2))]
[else #f])]
[else #f]))))
(define-pass imm->imm : (L15c Triv) (ir) -> (L15d Triv) ()
(Lvalue : Lvalue (ir) -> Lvalue ()
[(mref ,lvalue1 ,lvalue2 ,imm) (sorry! who "unexpected mref ~s" ir)])
(Triv : Triv (ir) -> Triv ()))
(define-pass literal@->literal : (L15c Triv) (ir) -> (L15d Triv) ()
(Triv : Triv (ir) -> Triv ()
[(literal ,info)
`(literal
,(make-info-literal #f (info-literal-type info)
(info-literal-addr info) (info-literal-offset info)))]
[else (sorry! who "unexpected literal ~s" ir)]))
(define-pass select-instructions! : (L15c Dummy) (ir block* live-size force-overflow?) -> (L15d Dummy) ()
(definitions
(module (handle-jump handle-effect-inline handle-pred-inline handle-value-inline)
(define add-var (make-add-var live-size))
(define Triv
(lambda (out t)
(nanopass-case (L15d Triv) t
[(mref ,x1 ,x2 ,imm) (add-var (add-var out x2) x1)]
[,x (add-var out x)]
[else out])))
(define Rhs
(lambda (out rhs)
(nanopass-case (L15d Rhs) rhs
[(asm ,info ,proc ,t* ...) (fold-left Triv out t*)]
[else (Triv out rhs)])))
(define Pred
(lambda (out pred)
(nanopass-case (L15d Pred) pred
[(asm ,info ,proc ,t* ...) (fold-left Triv out t*)])))
(define Tail
(lambda (out tail)
(nanopass-case (L15d Tail) tail
[(jump ,t) (Triv out t)])))
(define unwrap
(lambda (etree effect* out)
(safe-assert (not (eq? out 'uninitialized)))
(with-values
(let f ([etree etree] [effect* effect*] [out out])
(if (pair? etree)
(let-values ([(effect* out) (f (cdr etree) effect* out)])
(f (car etree) effect* out))
(if (null? etree)
(values effect* out)
(values
(cons etree effect*)
(nanopass-case (L15d Effect) etree
[(set! ,live-info ,x ,rhs)
(live-info-live-set! live-info out)
(Rhs out rhs)]
[(set! ,live-info ,lvalue ,rhs)
(live-info-live-set! live-info out)
(Triv (Rhs out rhs) lvalue)]
[(asm ,info ,proc ,t* ...) (fold-left Triv out t*)]
[else out])))))
(lambda (effect* out) effect*))))
(define-who handle-jump
(lambda (t live)
(let-values ([(etree tail) (md-handle-jump t)])
(values (unwrap etree '() (Tail live tail)) tail))))
(define-who handle-effect-inline
(lambda (effect-prim info new-effect* t* live)
(unwrap (apply (primitive-handler effect-prim) info t*) new-effect* live)))
(define-who handle-pred-inline
(lambda (pred-prim info t* live)
(let-values ([(etree pred) (apply (primitive-handler pred-prim) info t*)])
(values (unwrap etree '() (Pred live pred)) pred))))
(define-who handle-value-inline
(lambda (lvalue value-prim info new-effect* t* live)
(unwrap (apply (primitive-handler value-prim) info lvalue t*) new-effect* live))))
(define compute-overage
(lambda (max-fs@call)
(if force-overflow?
(fxmax
(fx- (fx* max-fs@call (constant ptr-bytes)) 0)
(fx- (fx* (fx+ max-fv 1) (constant ptr-bytes)) (fx- (constant stack-slop) (fx* (constant stack-frame-limit) 2))))
(fxmax
(fx- (fx* max-fs@call (constant ptr-bytes)) (constant stack-frame-limit))
(fx- (fx* (fx+ max-fv 1) (constant ptr-bytes)) (fx- (constant stack-slop) (constant stack-frame-limit)))))))
(define overage (compute-overage max-fs@call))
(define handle-overflow-check
(lambda (reg info new-effect* live)
(let-values ([(xnew-effect* pred) (handle-pred-inline %u< null-info
(list
reg
(meta-cond
[(real-register? '%esp) %esp]
[else (with-output-language (L15c Triv)
`(mref ,%tc ,%zero ,(tc-disp %esp)))]))
live)])
(append xnew-effect*
(cons (with-output-language (L15d Effect)
`(overflow-check ,pred
,(handle-effect-inline %asmlibcall! info '() '() live)
...))
new-effect*)))))
(define maybe-incr-instr-count
(lambda (block e*)
(define checks-cc? ; copied from instrument
(lambda (block)
(and (if-block? block)
(null? (block-effect* block))
(nanopass-case (L15c Pred) (if-block-pred block)
[(inline ,live-info ,info ,pred-prim ,t* ...) (eq? pred-prim %condition-code)]
[else #f]))))
(define count
(lambda (n e)
; overflow-check counts as one instruction...close enough, since it rarely fails
(nanopass-case (L15d Effect) e
[(rp-header ,mrvl ,fs ,lpm) n]
[(move-related ,x1 ,x2) n]
[else (fx+ n 1)])))
(if (generate-instruction-counts)
(let* ([n (fold-left count (if (goto-block? block) 0 1) e*)]
[f (lambda (e*)
(handle-effect-inline %inc-cc-counter null-info e*
(list %tc
(with-output-language (L15c Triv) `(immediate ,(constant tc-instr-counter-disp)))
(with-output-language (L15c Triv) `(immediate ,n)))
(block-live-in block)))])
(if (and (not (null? e*))
(nanopass-case (L15d Effect) (car e*)
[(rp-header ,mrvl ,fs ,lpm) #t]
[else #f]))
(cons (car e*) (f (cdr e*)))
(begin
(assert (not (checks-cc? block)))
(f e*))))
e*))))
(Rhs : Rhs (ir lvalue new-effect* live) -> * (new-effect*)
[(inline ,info ,value-prim ,t* ...)
(handle-value-inline lvalue value-prim info new-effect* t* live)]
[else (handle-value-inline lvalue %move null-info new-effect* (list ir) live)])
(Tail : Tail (ir) -> Tail ()
[(jump ,live-info ,t) (handle-jump t (live-info-live live-info))]
[(goto ,l) (values '() `(goto ,l))]
[(asm-return) (values '() `(asm-return))]
[(asm-c-return ,info) (values '() `(asm-c-return ,info))])
(Effect : Effect (ir new-effect*) -> * (new-effect*)
[(set! ,live-info ,lvalue ,rhs) (Rhs rhs lvalue new-effect* (live-info-live live-info))]
[(inline ,live-info ,info ,effect-prim ,t* ...)
(handle-effect-inline effect-prim info new-effect* t* (live-info-live live-info))]
[(rp-header ,mrvl ,fs ,lpm)
(cons (with-output-language (L15d Effect) `(rp-header ,mrvl ,fs ,lpm)) new-effect*)]
[(overflow-check ,live-info)
(if (fx> 1 overage (fx- (constant stack-frame-limit) (constant stack-slop)))
(handle-overflow-check %sfp (intrinsic-info-asmlib dooverflow #f) new-effect* (live-info-live live-info))
new-effect*)]
[(overflood-check ,live-info)
(if (fx> overage 0)
; dooverflood protocol requires %xp be set where we need esp to be
(let ([uxp (make-precolored-unspillable 'uxp %xp)])
(handle-value-inline uxp %+ null-info
(handle-overflow-check uxp (intrinsic-info-asmlib dooverflood #f) new-effect* (live-info-live live-info))
(list %sfp (with-output-language (L15c Triv) `(immediate ,overage)))
(live-info-live live-info)))
new-effect*)]
[(fcallable-overflow-check ,live-info)
; max-fs@call = 2: the return address and c-chain stored by C-call->XXX
(if (fx> 1 (compute-overage 2) (fx- (constant stack-frame-limit) (constant stack-slop)))
(handle-overflow-check %sfp (intrinsic-info-asmlib dooverflow #f) new-effect* (live-info-live live-info))
new-effect*)])
(Pred : Pred (ir) -> Pred ()
[(inline ,live-info ,info ,pred-prim ,t* ...)
(handle-pred-inline pred-prim info t* (live-info-live live-info))])
(begin
(for-each
(lambda (block)
(block-effect*-set! block
(maybe-incr-instr-count block
(fold-right Effect
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) '()]
[(if-block? block)
(let-values ([(new-effect* pred) (Pred (if-block-pred block))])
(if-block-pred-set! block pred)
new-effect*)]
[(tail-block? block)
(let-values ([(new-effect* tail) (Tail (tail-block-tail block))])
(tail-block-tail-set! block tail)
new-effect*)]
[else (sorry! who "unrecognized block ~s" block)])
(block-effect* block)))))
block*)
`(dummy)))
; NB: try to reuse unspillables to reduce the number we create
(architecture instructions)
)
(define-who do-unspillable-conflict!
(lambda (kfv kspillable varvec live-size kunspillable unvarvec block*)
(define remove-var (make-remove-var live-size))
(define unspillable?
(lambda (x)
(and (uvar? x) (uvar-unspillable? x))))
(define add-unspillable
(lambda (unspillable* x)
(if (and (unspillable? x) (not (uvar-seen? x)))
(begin
(uvar-seen! x #t)
(cons x unspillable*))
unspillable*)))
(define add-move!
(lambda (x1 x2)
(when (var-index x2)
($add-move! x1 x2 2)
($add-move! x2 x1 2))))
(define add-move-hint!
(lambda (x1 x2)
(when (var-index x2)
($add-move! x1 x2 1)
($add-move! x2 x1 1))))
(define add-static-conflict!
(lambda (u reg*)
(let ([u-offset (var-index u)])
(for-each
(lambda (reg) (conflict-bit-set! (var-unspillable-conflict* reg) u-offset))
reg*))))
(define add-us->s-conflicts!
(lambda (x out) ; x is an unspillable
(let ([x-offset (var-index x)] [cset (var-spillable-conflict* x)])
(tree-for-each out live-size 0 live-size
(lambda (y-offset)
(let* ([y (vector-ref varvec y-offset)] [y-cset (var-unspillable-conflict* y)])
(when y-cset
; if y is a spillable, point the unspillable x at y
(when (fx< y-offset kspillable) (conflict-bit-set! cset y-offset))
; point y at the unspillable x
(conflict-bit-set! y-cset x-offset))))))))
(define add-us->us-conflicts!
(lambda (x unspillable*) ; x is a unspillable
(let ([x-offset (var-index x)] [cset (var-unspillable-conflict* x)])
(for-each
(lambda (y)
(let ([y-offset (var-index y)])
(conflict-bit-set! cset y-offset)
(conflict-bit-set! (var-unspillable-conflict* y) x-offset)))
unspillable*))))
(define add-s->us-conflicts!
(lambda (x unspillable*) ; x is a spillable or register
(let ([x-offset (var-index x)] [cset (var-unspillable-conflict* x)])
(for-each
(lambda (y)
(let ([y-offset (var-index y)])
; point x at unspillable y
(conflict-bit-set! cset y-offset)
; if x is a spillable, point unspillable y at x
(when (fx< x-offset kspillable) (conflict-bit-set! (var-spillable-conflict* y) x-offset))))
unspillable*))))
(define Triv
(lambda (unspillable* t)
(nanopass-case (L15d Triv) t
[(mref ,x1 ,x2 ,imm) (add-unspillable (add-unspillable unspillable* x2) x1)]
[,x (add-unspillable unspillable* x)]
[else unspillable*])))
(define Rhs
(lambda (unspillable* rhs)
(nanopass-case (L15d Rhs) rhs
[(asm ,info ,proc ,t* ...) (fold-left Triv unspillable* t*)]
[else (Triv unspillable* rhs)])))
(define Pred
(lambda (p)
(nanopass-case (L15d Pred) p
[(asm ,info ,proc ,t* ...) (fold-left Triv '() t*)]
[else (sorry! who "unexpected pred ~s" p)])))
(define Tail
(lambda (tl)
(nanopass-case (L15d Tail) tl
[(jump ,t) (Triv '() t)]
[else '()])))
(define Effect*
(lambda (e* unspillable*)
(if (null? e*)
(safe-assert (null? unspillable*))
(Effect* (cdr e*)
(nanopass-case (L15d Effect) (car e*)
[(set! ,live-info ,x ,rhs)
(let ([spillable-live (live-info-live live-info)])
(if (unspillable? x)
(let ([unspillable* (remq x unspillable*)])
(safe-assert (uvar-seen? x))
(uvar-seen! x #f)
(if (and (var? rhs) (var-index rhs))
(begin
(if (unspillable? rhs)
(begin
(add-us->us-conflicts! x (remq rhs unspillable*))
(add-us->s-conflicts! x spillable-live))
(begin
(add-us->us-conflicts! x unspillable*)
(add-us->s-conflicts! x (remove-var spillable-live rhs))))
(add-move! x rhs))
(begin
(add-us->us-conflicts! x unspillable*)
(add-us->s-conflicts! x spillable-live)))
(Rhs unspillable* rhs))
(begin
(when (var-unspillable-conflict* x)
(if (unspillable? rhs)
(begin
(add-s->us-conflicts! x (remq rhs unspillable*))
(add-move! x rhs))
(add-s->us-conflicts! x unspillable*)))
(Rhs unspillable* rhs))))]
[(set! ,live-info ,lvalue ,rhs) (Triv (Rhs unspillable* rhs) lvalue)]
[(asm ,info ,proc ,t* ...) (fold-left Triv unspillable* t*)]
[(move-related ,x1 ,x2) (add-move-hint! x1 x2) unspillable*]
[(overflow-check ,p ,e* ...) (Effect* (reverse e*) '()) (Pred p)]
[else unspillable*])))))
(for-each (lambda (x) (var-spillable-conflict*-set! x (make-empty-cset kspillable))) unspillable*)
(let ([f (lambda (x) (var-unspillable-conflict*-set! x (make-empty-cset kunspillable)))])
(vector-for-each f regvec)
(for-each f spillable*)
(vector-for-each f unvarvec))
(vector-for-each (lambda (x) (add-static-conflict! x (uvar-conflict* x))) unvarvec)
(for-each
(lambda (block)
(Effect* (reverse (block-effect* block))
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) '()]
[(if-block? block) (Pred (if-block-pred block))]
[(tail-block? block) (Tail (tail-block-tail block))]
[else (sorry! who "unrecognized block ~s" block)])))
block*)))
(define-who assign-registers!
(lambda (lambda-info varvec unvarvec)
(define k (vector-length regvec))
(define uvar-weight
(lambda (x)
(fx- (uvar-ref-weight x) (uvar-save-weight x))))
; could also be calculated when the conflict set is built, which would be more
; efficient for low-degree variables
(define compute-degrees!
(lambda (x*)
; account for uvar -> uvar conflicts
(for-each
(lambda (x)
(uvar-degree-set! x
(fx+
; spills have been trimmed from the var-spillable-conflict* sets
(conflict-bit-count (var-spillable-conflict* x))
(conflict-bit-count (var-unspillable-conflict* x)))))
x*)
; account for reg -> uvar conflicts
(vector-for-each
(lambda (reg)
(cset-for-each (var-spillable-conflict* reg)
(lambda (x-offset)
(let ([x (vector-ref varvec x-offset)])
(unless (uvar-location x)
(uvar-degree-set! x (fx+ (uvar-degree x) 1))))))
(cset-for-each (var-unspillable-conflict* reg)
(lambda (x-offset)
(let ([x (vector-ref unvarvec x-offset)])
(uvar-degree-set! x (fx+ (uvar-degree x) 1))))))
regvec)))
(define-who find-home!
(lambda (x)
(define conflict?
(lambda (reg x)
(let ([cset (if (uvar-unspillable? x) (var-unspillable-conflict* reg) (var-spillable-conflict* reg))])
(conflict-bit-set? cset (var-index x)))))
(define find-move-related-home
(lambda (x0 succ fail)
(let f ([x x0] [work* '()] [clear-seen! void])
(if (uvar-seen? x)
(if (null? work*) (begin (clear-seen!) (fail)) (f (car work*) (cdr work*) clear-seen!))
(let ([clear-seen! (lambda () (uvar-seen! x #f) (clear-seen!))])
(uvar-seen! x #t)
(let loop ([move* (uvar-move* x)] [work* work*])
(if (null? move*)
(if (null? work*) (begin (clear-seen!) (fail)) (f (car work*) (cdr work*) clear-seen!))
(let ([var (caar move*)] [move* (cdr move*)])
(define try-reg
(lambda (reg)
(if (conflict? reg x0)
(loop move* work*)
(begin (clear-seen!) (succ reg)))))
(if (reg? var)
(try-reg var)
(if (uvar? var)
(let ([reg (uvar-location var)])
(if (reg? reg)
(try-reg reg)
(loop move* (cons var work*))))
(loop move* work*)))))))))))
(define set-home!
(lambda (home)
(define update-conflict!
(lambda (reg x)
(cset-merge! (var-spillable-conflict* reg) (var-spillable-conflict* x))
(cset-merge! (var-unspillable-conflict* reg) (var-unspillable-conflict* x))))
(uvar-location-set! x home)
(update-conflict! home x)))
(find-move-related-home x
set-home!
(lambda ()
(let f ([offset (fx- k 1)])
(cond
[(fx< offset 0)
(uvar-spilled! x #t)
(when (uvar-unspillable? x)
(sorry! who "spilled unspillable ~s" x))]
[(conflict? (vector-ref regvec offset) x) (f (fx- offset 1))]
[else (set-home! (vector-ref regvec offset))]))))))
(define pick-victims
(lambda (x*)
(define low-degree? (lambda (x) (fx< (uvar-degree x) k)))
(define pick-potential-spill
; x* is already sorted by weight, so this effectively picks uvar with
; the highest degree among those with the lowest weight
(lambda (x*)
(let ([x (let f ([x* (cdr x*)] [max-degree (uvar-degree (car x*))] [max-x (car x*)])
(if (null? x*)
max-x
(let ([x (car x*)] [x* (cdr x*)])
(if (or (uvar-unspillable? x) (fx> (uvar-weight x) (uvar-weight max-x)))
max-x
(let ([degree (uvar-degree x)])
(if (fx> degree max-degree)
(f x* degree x)
(f x* max-degree max-x)))))))])
(values x (remq x x*)))))
(define remove-victim!
(lambda (victim)
(cset-for-each (var-spillable-conflict* victim)
(lambda (offset)
(let ([x (vector-ref varvec offset)])
(uvar-degree-set! x (fx- (uvar-degree x) 1)))))
(cset-for-each (var-unspillable-conflict* victim)
(lambda (offset)
(let ([x (vector-ref unvarvec offset)])
(uvar-degree-set! x (fx- (uvar-degree x) 1)))))))
(define sort-victims
; NB: sorts based on likelihood of successfully assigning move-related vars to the same register
; NB: probably should sort based on value of assigning move-related vars to the same register,
; NB: i.e., taking into account the ref-weight
(lambda (victim*)
(map car
(list-sort
(lambda (x y) (fx> (cdr x) (cdr y)))
(map (lambda (x)
(define relevant?
(lambda (x)
(or (reg? x) (and (uvar? x) (not (uvar-spilled? x))))))
(do ([move* (uvar-move* x) (cdr move*)]
[w 0 (let ([move (car move*)])
(if (relevant? (car move))
(fx+ w (cdr move))
w))])
((null? move*) (cons x w))))
victim*)))))
(let-values ([(victim* keeper*) (partition low-degree? x*)])
(if (null? victim*)
(let-values ([(victim keeper*) (pick-potential-spill x*)])
; note: victim can be an unspillable if x* contains only precolored unspillables
(remove-victim! victim)
(values (list victim) keeper*))
(begin
(unless (null? keeper*)
; tried creating a mask from victim*, logand with bv for each x, count the bits,
; and subtract from x's uvar-degree-set!. code in chaff. didn't help at this point.
; perhaps if fxbit-count were implemented better it would
(for-each remove-victim! victim*))
(values (sort-victims victim*) keeper*))))))
(let ([x* (append (sort (lambda (x y) (fx< (uvar-weight x) (uvar-weight y))) spillable*) unspillable*)])
(compute-degrees! x*)
(let f ([x* x*])
(unless (null? x*)
(let-values ([(victim* x*) (pick-victims x*)])
(f x*)
(for-each find-home! victim*)))))))
(define everybody-home?
(lambda ()
(safe-assert (andmap uvar-location unspillable*))
(andmap uvar-location spillable*)))
(define record-inspector-information!
(lambda (info)
(define get-closure-fv-names
(lambda (info ctci)
(define (get-name fv) (unannotate (uvar-source fv)))
(or (ctci-closure-fv-names ctci)
(case (info-lambda-closure-rep info)
[(pair)
(let ([p (cons (get-name (car (info-lambda-fv* info)))
(get-name (cadr (info-lambda-fv* info))))])
(ctci-closure-fv-names-set! ctci p)
p)]
[(vector)
(let ([v (list->vector (map get-name (info-lambda-fv* info)))])
(ctci-closure-fv-names-set! ctci v)
v)]
[else #f]))))
(cond
[(info-lambda-ctci info) =>
(lambda (ctci)
(ctci-live-set! ctci
(let f ([i 0] [spillable* spillable*])
(if (null? spillable*)
(make-vector i)
(let ([spillable (car spillable*)])
(cond
[(and (uvar-spilled? spillable) (uvar-source spillable)) =>
(lambda (source)
(if (eq? source (let () (include "types.ss") cpsymbol))
(case (info-lambda-closure-rep info)
[(singleton)
(cond
[(uvar-source (car (info-lambda-fv* info))) =>
(lambda (source)
(let ([v (f (fx+ i 1) (cdr spillable*))])
(uvar-iii-set! spillable i)
(vector-set! v i (cons (unannotate source) (fv-offset (uvar-location spillable))))
v))]
[else (f i (cdr spillable*))])]
[(pair vector)
(let ([v (f (fx+ i 1) (cdr spillable*))])
(uvar-iii-set! spillable i)
(vector-set! v i
(cons (get-closure-fv-names info ctci)
(fv-offset (uvar-location spillable))))
v)]
[(closure)
(let ([v (f (fx+ i 1) (cdr spillable*))])
(uvar-iii-set! spillable i)
(vector-set! v i (cons (unannotate source) (fv-offset (uvar-location spillable))))
v)]
[else (f i (cdr spillable*))])
(let ([v (f (fx+ i 1) (cdr spillable*))])
(uvar-iii-set! spillable i)
(vector-set! v i (cons (unannotate source) (fv-offset (uvar-location spillable))))
v)))]
[else (f i (cdr spillable*))]))))))])))
(define-pass finalize-register-locations! : (L15d Dummy) (ir block*) -> (L15e Dummy) ()
(definitions
(define var->loc
(lambda (x)
(if (uvar? x)
(or (uvar-location x) (sorry! who "no location assigned to uvar ~s" x))
x))))
(Lvalue : Lvalue (ir) -> Lvalue ()
[(mref ,x0 ,x1 ,imm) `(mref ,(var->loc x0) ,(var->loc x1) ,imm)]
[,x (var->loc x)])
(Pred : Pred (ir) -> Pred ())
(Tail : Tail (ir) -> Tail ())
(Effect : Effect (ir) -> Effect ()
[(set! ,live-info ,[lvalue] ,[rhs]) `(set! ,lvalue ,rhs)])
(foldable-Effect : Effect (ir new-effect*) -> * (new-effect*)
[(move-related ,x1 ,x2) new-effect*]
[(set! ,live-info ,x0 ,x1)
(let ([x0 (var->loc x0)] [x1 (var->loc x1)])
(if (eq? x0 x1)
new-effect*
(cons (Effect ir) new-effect*)))]
[else (cons (Effect ir) new-effect*)])
(begin
(for-each
(lambda (block)
(block-effect*-set! block (fold-right foldable-Effect '() (block-effect* block)))
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (void)]
[(if-block? block) (if-block-pred-set! block (Pred (if-block-pred block)))]
[(tail-block? block) (tail-block-tail-set! block (Tail (tail-block-tail block)))]
[else (sorry! who "unrecognized block ~s" block)]))
block*)
`(dummy)))
(define-pass expose-overflow-check-blocks! : (L15e Dummy) (ir entry-block0* block0*) -> (L16 Dummy) (entry-block* block*)
(definitions
(define block* block0*)
(define entry-block* entry-block0*)
(define-who redirect-link!
(lambda (old new)
(lambda (from)
(cond
[(goto-block? from)
(cond
[(eq? (goto-block-next from) old) (goto-block-next-set! from new)]
[else (sorry! who "goto-block in-link not found")])]
[(joto-block? from)
(cond
[(eq? (joto-block-next from) old) (joto-block-next-set! from new)]
[else (sorry! who "joto-block in-link not found")])]
[(if-block? from)
(cond
[(eq? (if-block-true from) old) (if-block-true-set! from new)]
[(eq? (if-block-false from) old) (if-block-false-set! from new)]
[else (sorry! who "if-block in-link not found")])]
[(newframe-block? from)
(cond
[(eq? (newframe-block-next from) old) (newframe-block-next-set! from new)]
[(eq? (newframe-block-rp from) old) (newframe-block-rp-set! from new)]
[(memq old (newframe-block-rp* from)) (newframe-block-rp*-set! from (subst new old (newframe-block-rp* from)))]
[else (sorry! who "newframe-block in-link not found")])]
[else (sorry! who "unexpected block ~s" from)]))))
(define insert-check!
(lambda (block rebefore* p ehere* eafter*)
(let ([libcall-block (make-goto-block)])
(goto-block-next-set! libcall-block block)
(block-pariah! libcall-block #t)
(let ([check-block (make-if-block block libcall-block)])
(if-block-pred-set! check-block p)
(block-effect*-set! check-block (reverse rebefore*))
(block-effect*-set! libcall-block ehere*)
(set! entry-block* (subst check-block block entry-block*))
(let ([label (block-label block)])
(block-label-set! check-block label)
(local-label-block-set! label check-block))
(let ([label (make-local-label 'post-overflow-check)])
(block-label-set! block label)
(local-label-block-set! label block))
(let ([label (make-local-label 'overflowed)])
(block-label-set! libcall-block label)
(local-label-block-set! label libcall-block))
(for-each (redirect-link! block check-block) (block-in-link* block))
(block-in-link*-set! block (list check-block libcall-block))
(set! block* (cons* check-block libcall-block block*))
(Effect* block '() eafter*)))))
(define Effect*
(lambda (block rebefore* eafter*)
(if (null? eafter*)
(block-effect*-set! block (reverse rebefore*))
(let ([e (car eafter*)] [eafter* (cdr eafter*)])
(nanopass-case (L15e Effect) e
[(overflow-check ,[Pred : p] ,[Effect : e*] ...) (insert-check! block rebefore* p e* eafter*)]
[else (Effect* block (cons (Effect e) rebefore*) eafter*)]))))))
(Pred : Pred (ir) -> Pred ())
(Tail : Tail (ir) -> Tail ())
(Effect : Effect (ir) -> Effect ())
; NB: without the begin, seems to ignore all but the first subform below
(begin
(for-each
(lambda (block)
(Effect* block '() (block-effect* block))
(cond
[(or (goto-block? block) (joto-block? block) (newframe-block? block)) (void)]
[(if-block? block) (if-block-pred-set! block (Pred (if-block-pred block)))]
[(tail-block? block) (tail-block-tail-set! block (Tail (tail-block-tail block)))]
[else (sorry! who "unrecognized block ~s" block)]))
block0*)
(values `(dummy) entry-block* block*)))
(define-syntax with-live-info-record-writer
(lambda (x)
(syntax-case x ()
[(_ live-size varvec e1 e2 ...)
#'(parameterize ([(case-lambda
[() (record-writer (record-type-descriptor live-info))]
[(x) (record-writer (record-type-descriptor live-info) x)])
(lambda (x p wr)
(when (live-info-useless x) (fprintf p "useless "))
(fprintf p "<live:")
(let ([live (live-info-live x)])
(if (eq? live 'uninitialized)
(fprintf p " uninitialized")
(for-each (lambda (x) (fprintf p " ~s" x)) (get-live-vars live live-size varvec))))
(fprintf p ">"))])
e1 e2 ...)])))
(define-pass np-allocate-registers : L15a (ir) -> L16 ()
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info ,max-fv0 (,local* ...) (,entry-block* ...) (,block* ...))
(let ()
(define block-printer
(lambda (unparser name block*)
(p-dot-graph block* (current-output-port))
(p-graph block* name (current-output-port) unparser)))
(module (RApass)
(define RAprinter
(lambda (unparser)
(lambda (val*)
(block-printer unparser (info-lambda-name info) block*))))
(define-syntax RApass
(lambda (x)
(syntax-case x ()
[(_ ?unparser pass-name ?arg ...)
#'(xpass pass-name (RAprinter ?unparser) (list ?arg ...))]))))
(safe-assert (andmap (lambda (x) (eq? (uvar-location x) #f)) local*))
(let ([kspillable (length local*)] [kfv (fx+ max-fv0 1)] [kreg (vector-length regvec)])
(fluid-let ([spillable* local*] [unspillable* '()] [max-fv max-fv0] [max-fs@call 0] [poison-cset (make-empty-cset kspillable)])
(let* ([live-size (fx+ kfv kreg kspillable)] [varvec (make-vector live-size)])
; set up var indices & varvec mapping from indices to vars
(fold-left (lambda (i x) (var-index-set! x i) (vector-set! varvec i x) (fx+ i 1)) 0 spillable*)
(do ([i 0 (fx+ i 1)]) ((fx= i kfv)) (let ([fv (get-fv i)] [i (fx+ i kspillable)]) (var-index-set! fv i) (vector-set! varvec i fv)))
(do ([i 0 (fx+ i 1)]) ((fx= i kreg)) (let ([reg (vector-ref regvec i)] [i (fx+ i kspillable kfv)]) (var-index-set! reg i) (vector-set! varvec i reg)))
(with-live-info-record-writer live-size varvec
; run intra/inter-block live analysis
(RApass unparse-L15a do-live-analysis! live-size entry-block*)
; this is worth enabling from time to time...
#;(check-entry-live! (info-lambda-name info) live-size varvec entry-block*)
; rerun intra-block live analysis and record (fv v reg v spillable) x spillable conflicts
(RApass unparse-L15a record-call-live! block* varvec)
;; NB: we could just use (vector-length varvec) to get live-size
(when (fx> kspillable 1000) ; NB: parameter?
(RApass unparse-L15a identify-poison! kspillable varvec live-size block*))
(RApass unparse-L15a do-spillable-conflict! kspillable kfv varvec live-size block*)
#;(show-conflicts (info-lambda-name info) varvec '#())
; find frame homes for call-live variables; adds new fv x spillable conflicts
(RApass unparse-L15a assign-frame! (filter uvar-spilled? spillable*))
#;(show-homes)
(RApass unparse-L15a record-inspector-information! info)
; determine frame sizes at nontail-call sites and assign homes to new-frame variables
; adds new fv x spillable conflicts
(let ([dummy (RApass unparse-L15b assign-new-frame! (with-output-language (L15a Dummy) `(dummy)) info live-size varvec block*)])
; record fp offset on entry to each block
(RApass unparse-L15b record-fp-offsets! entry-block*)
; assign frame homes to poison variables
(let ([spill* (filter (lambda (x) (and (not (uvar-location x)) (uvar-poison? x))) spillable*)])
(unless (null? spill*)
(for-each (lambda (x) (uvar-spilled! x #t)) spill*)
(RApass unparse-L15b assign-frame! spill*)))
; on entry to loop, have assigned call-live and new-frame variables to frame homes, determined frame sizes, and computed block-entry fp offsets
(let ([saved-reg-csets (vector-map (lambda (reg) (cset-copy (var-spillable-conflict* reg))) regvec)]
[bcache* (map cache-block-info block*)])
(let loop ()
(for-each
(lambda (spill)
; remove each spill from each other spillable's spillable conflict set
(unless (uvar-poison? spill)
(let ([spill-index (var-index spill)])
(cset-for-each (var-spillable-conflict* spill)
(lambda (i)
(let ([x (vector-ref varvec i)])
(unless (uvar-location x)
(conflict-bit-unset! (var-spillable-conflict* x) spill-index)))))))
; release the spill's conflict* set
(var-spillable-conflict*-set! spill #f))
(filter uvar-location spillable*))
(set! spillable* (remp uvar-location spillable*))
(let ([saved-move* (map uvar-move* spillable*)])
#;(show-homes)
(let ([dummy (RApass unparse-L15c finalize-frame-locations! dummy block*)])
(let ([dummy (RApass unparse-L15d select-instructions! dummy block* live-size
(let ([libspec (info-lambda-libspec info)])
(and libspec (libspec-does-not-expect-headroom? libspec))))])
(vector-for-each (lambda (reg) (reg-precolored-set! reg #f)) regvec)
(let* ([kunspillable (length unspillable*)] [unvarvec (make-vector kunspillable)])
; set up var indices & unvarvec mapping from indices to unspillables
(fold-left (lambda (i x) (var-index-set! x i) (vector-set! unvarvec i x) (fx+ i 1)) 0 unspillable*)
; rerun intra-block live analysis and record (reg v spillable v unspillable) x unspillable conflicts
(RApass unparse-L15d do-unspillable-conflict! kfv kspillable varvec live-size kunspillable unvarvec block*)
#;(show-conflicts (info-lambda-name info) varvec unvarvec)
(RApass unparse-L15d assign-registers! info varvec unvarvec)
; release the unspillable conflict sets
(for-each (lambda (x) (var-unspillable-conflict*-set! x #f)) spillable*)
(vector-for-each (lambda (x) (var-unspillable-conflict*-set! x #f)) regvec)
#;(show-homes unspillable*)
(if (everybody-home?)
(let ([dummy (RApass unparse-L15e finalize-register-locations! dummy block*)])
; release the spillable conflict sets
(vector-for-each (lambda (reg) (var-spillable-conflict*-set! reg #f)) regvec)
(do ([i max-fv (fx- i 1)]) ((fx< i 0)) (var-spillable-conflict*-set! (get-fv i) #f))
(let-values ([(dummy entry-block* block*)
(xpass expose-overflow-check-blocks!
(lambda (val*)
(apply (lambda (dummy entry-block* block*)
(block-printer unparse-L16 (info-lambda-name info) block*))
val*))
(list dummy entry-block* block*))])
(safe-assert (andmap block-label (append entry-block* block*)))
(safe-assert (lambda (b) (eq? (local-label-block (block-label b)) b)) (append entry-block* block*))
`(lambda ,info (,entry-block* ...) (,block* ...))))
(begin
(for-each restore-block-info! block* bcache*)
(vector-for-each var-spillable-conflict*-set! regvec saved-reg-csets)
(for-each (lambda (x) (uvar-location-set! x #f)) spillable*)
(for-each uvar-move*-set! spillable* saved-move*)
(set! unspillable* '())
(RApass unparse-L15b assign-frame! (filter uvar-spilled? spillable*))
(loop)))))))))))))))])))
; NB: commonize with earlier
(define-pass np-remove-repeater-blocks-again! : L16 (ir) -> L16 ()
(definitions
(define path-compress!
(lambda (b)
(cond
[(block-repeater? b) (goto-block-next b)]
; NB: ignoring block-src* here, post-profiling
[(and (goto-block? b) (null? (block-effect* b)))
(block-repeater! b #t)
(let ([end (path-compress! (goto-block-next b))])
(goto-block-next-set! b end)
end)]
[else b])))
(define resolve
(lambda (b)
(if (block-repeater? b)
(goto-block-next b)
b))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info (,entry-block* ...) (,block* ...))
(for-each path-compress! block*)
(for-each
(lambda (from)
(define resolve!
(lambda (get put!)
(let ([to (get from)])
(when (block-repeater? to)
(put! from (goto-block-next to))))))
(cond
[(goto-block? from)
(unless (block-repeater? from)
(resolve! goto-block-next goto-block-next-set!))]
[(joto-block? from)
(resolve! joto-block-next joto-block-next-set!)]
[(if-block? from)
(resolve! if-block-true if-block-true-set!)
(resolve! if-block-false if-block-false-set!)]
[(newframe-block? from)
(resolve! newframe-block-next newframe-block-next-set!)
(newframe-block-rp*-set! from (map resolve (newframe-block-rp* from)))
(resolve! newframe-block-rp newframe-block-rp-set!)]
[(tail-block? from) (void)]
[else (sorry! who "unrecognized block ~s" from)]))
block*)
(for-each (lambda (dcl)
(let* ([b0 (local-label-block dcl)] [b (and b0 (resolve b0))])
(unless (eq? b b0)
(local-label-block-set! dcl b)
(block-label-set! b dcl))))
(info-lambda-dcl* info))
`(lambda ,info
(,(map resolve entry-block*) ...)
(,(filter (lambda (b) (or (not (block-repeater? b)) (eq? (goto-block-next b) b))) block*) ...))]))
; NB: might instead sort blocks in np-generate-code, which is in a better position
; NB: to deal with block ordering when branch displacement sizes are limited
(define-pass np-optimize-block-order! : L16 (ir) -> L16 ()
(definitions
(define invertible?
(lambda (pred)
(nanopass-case (L16 Pred) pred
[(asm ,info ,proc ,t* ...)
(safe-assert (info-condition-code? info))
(info-condition-code-invertible? info)])))
(define block-likeliness
(lambda (b)
(or (block-weight b) 0)))
(define block-in-degree
(lambda (b)
(fold-left (lambda (n b) (if (block-seen? b) n (fx+ n 1))) 0 (block-in-link* b)))))
(CaseLambdaExpr : CaseLambdaExpr (ir) -> CaseLambdaExpr ()
[(lambda ,info (,entry-block* ...) (,block* ...))
(safe-assert (not (ormap block-seen? block*)))
(safe-assert (not (null? entry-block*)))
(let loop ([b (car entry-block*)] [w* '()] [pariah* (cdr entry-block*)] [rblock* '()])
(define next-worklist-entry
(lambda (w* pariah* rblock*)
(if (null? w*)
(if (null? pariah*)
(begin
(safe-assert (andmap block-label (append entry-block* rblock*)))
(safe-assert (lambda (b) (eq? (local-label-block (block-label b)) b)) (append entry-block* rblock*))
(for-each (lambda (b) (block-seen! b #f)) block*)
`(lambda ,info (,entry-block* ...) (,(reverse rblock*) ...)))
(loop (car pariah*) '() (cdr pariah*) rblock*))
(loop (car w*) (cdr w*) pariah* rblock*))))
(if (block-seen? b)
(next-worklist-entry w* pariah* rblock*)
(let ([rblock* (cons b rblock*)])
(block-seen! b #t)
(cond
[(goto-block? b) (loop (goto-block-next b) w* pariah* rblock*)]
[(joto-block? b) (loop (joto-block-next b) w* pariah* rblock*)]
[(if-block? b)
(let ([true (if-block-true b)] [false (if-block-false b)])
(if (block-seen? true)
(loop false w* pariah* rblock*)
(if (block-seen? false)
(loop true w* pariah* rblock*)
(if (invertible? (if-block-pred b))
(let ([llntrue (block-likeliness true)] [llnfalse (block-likeliness false)])
(if (or (and (fx= llnfalse llntrue)
(fx< (block-in-degree false) (block-in-degree true)))
(fx< llntrue llnfalse))
(if (fx< llntrue 0)
(loop false w* (cons true pariah*) rblock*)
(loop false (cons true w*) pariah* rblock*))
(if (fx< llnfalse 0)
(loop true w* (cons false pariah*) rblock*)
(loop true (cons false w*) pariah* rblock*))))
(if (fx< (block-likeliness false) 0)
(loop true w* (cons false pariah*) rblock*)
(loop true (cons false w*) pariah* rblock*))))))]
[(newframe-block? b)
(loop (newframe-block-next b)
(append (newframe-block-rp* b) (cons (newframe-block-rp b) w*))
pariah* rblock*)]
[(tail-block? b) (next-worklist-entry w* pariah* rblock*)]
[else (sorry! who "unrecognized block ~s" b)]))))]))
(define (np-after-calling-conventions ir)
(compose ir
(pass np-expand-hand-coded unparse-L13.5)
(pass np-expose-allocation-pointer unparse-L14)
(pass np-expose-basic-blocks unparse-L15a)
(pass np-remove-repeater-blocks! unparse-L15a)
(lambda (ir)
(if (and (or (eq? ($compile-profile) 'block) ($profile-block-data?)) ($sfd))
((pass np-add-block-source! unparse-L15a) ir)
ir))
(pass np-propagate-pariahty! unparse-L15a)
(lambda (ir)
(if (or (eq? ($compile-profile) 'source)
(and (eq? ($compile-profile) 'block) ($sfd)))
((pass np-insert-profiling unparse-L15a) ir)
ir))
(pass np-add-in-links! unparse-L15a)
(pass np-compute-loop-depth! unparse-L15a)
(pass np-weight-references! unparse-L15a)
np-allocate-registers ; aggregate pass...don't use pass macro, or it will show up in timings
(pass np-remove-repeater-blocks-again! unparse-L16)
(pass np-optimize-block-order! unparse-L16)
(pass np-generate-code)))
(set! $np-compile
(lambda (original-input-expression pt?)
(with-initialized-registers
(fluid-let ([frame-vars (make-vector 8 #f)]
[next-lambda-seqno 0]
[pass-time? pass-time?])
(compose original-input-expression
(pass cpnanopass unparse-L1)
(pass np-recognize-let unparse-L2)
(pass np-discover-names unparse-L3)
#;(lambda (ir) (unless (eqv? (optimize-level) 3) ((pass np-check-flags) ir)) ir)
(pass np-convert-assignments unparse-L4)
(pass np-sanitize-bindings unparse-L4)
(pass np-suppress-procedure-checks unparse-L4)
(pass np-recognize-mrvs unparse-L4.5)
(pass np-expand-foreign unparse-L4.75)
(pass np-recognize-loops unparse-L4.875)
(pass np-name-anonymous-lambda unparse-L5)
(pass np-convert-closures unparse-L6)
(pass np-optimize-direct-call unparse-L6)
(pass np-identify-scc unparse-L6)
(if ($optimize-closures)
(pass np-expand/optimize-closures unparse-L7)
(pass np-expand-closures unparse-L7))
(lambda (ir)
(if (fxzero? ($loop-unroll-limit))
ir
((pass np-profile-unroll-loops unparse-L7) ir)))
(pass np-simplify-if unparse-L7)
(pass np-expand-primitives unparse-L9)
(pass np-place-overflow-and-trap unparse-L9.5)
(pass np-rebind-on-ruined-path unparse-L9.5)
(pass np-finalize-loops unparse-L9.75)
(pass np-optimize-pred-in-value unparse-L9.75)
(pass np-remove-complex-opera* unparse-L10)
(pass np-push-mrvs unparse-L10.5)
(pass np-normalize-context unparse-L11)
(pass np-insert-trap-check unparse-L11.5)
(pass np-flatten-case-lambda unparse-L12)
(pass np-impose-calling-conventions unparse-L13)
np-after-calling-conventions)))))
(set! $np-boot-code
(lambda (which)
(with-initialized-registers
($c-func-code-record
(fluid-let ([frame-vars (make-vector 8 #f)]
[next-lambda-seqno 0]
[pass-time? #t])
(parameterize ([generate-inspector-information #f] [$compile-profile #f])
(np-after-calling-conventions
(with-output-language (L13 Program)
(let ([l (make-local-label 'Linvoke)])
`(labels ([,l (hand-coded ,which)]) ,l))))))))))
)
(set! $np-tracer tracer)
(set! $np-last-pass last-pass)
(set! $track-dynamic-closure-counts track-dynamic-closure-counts)
(set! $track-static-closure-counts track-static-closure-counts)
(set! $optimize-closures (make-parameter #t (lambda (x) (and x #t))))
)
|