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 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305 18306 18307 18308 18309 18310 18311 18312 18313 18314 18315 18316 18317 18318 18319 18320 18321 18322 18323 18324 18325 18326 18327 18328 18329 18330 18331 18332 18333 18334 18335 18336 18337 18338 18339 18340 18341 18342 18343 18344 18345 18346 18347 18348 18349 18350 18351 18352 18353 18354 18355 18356 18357 18358 18359 18360 18361 18362 18363 18364 18365 18366 18367 18368 18369 18370 18371 18372 18373 18374 18375 18376 18377 18378 18379 18380 18381 18382 18383 18384 18385 18386 18387 18388 18389 18390 18391 18392 18393 18394 18395 18396 18397 18398 18399 18400 18401 18402 18403 18404 18405 18406 18407 18408 18409 18410 18411 18412 18413 18414 18415 18416 18417 18418 18419 18420 18421 18422 18423 18424 18425 18426 18427 18428 18429 18430 18431 18432 18433 18434 18435 18436 18437 18438 18439 18440 18441 18442 18443 18444 18445 18446 18447 18448 18449 18450 18451 18452 18453 18454 18455 18456 18457 18458 18459 18460 18461 18462 18463 18464 18465 18466 18467 18468 18469 18470 18471 18472 18473 18474 18475 18476 18477 18478 18479 18480 18481 18482 18483 18484 18485 18486 18487 18488 18489 18490 18491 18492 18493 18494 18495 18496 18497 18498 18499 18500 18501 18502 18503 18504 18505 18506 18507 18508 18509 18510 18511 18512 18513 18514 18515 18516 18517 18518 18519 18520 18521 18522 18523 18524 18525 18526 18527 18528 18529 18530 18531 18532 18533 18534 18535 18536 18537 18538 18539 18540 18541 18542 18543 18544 18545 18546 18547 18548 18549 18550 18551 18552 18553 18554 18555 18556 18557 18558 18559 18560 18561 18562 18563 18564 18565 18566 18567 18568 18569 18570 18571 18572 18573 18574 18575 18576 18577 18578 18579 18580 18581 18582 18583 18584 18585 18586 18587 18588 18589 18590 18591 18592 18593 18594 18595 18596 18597 18598 18599 18600 18601 18602 18603 18604 18605 18606 18607 18608 18609 18610 18611 18612 18613 18614 18615 18616 18617 18618 18619 18620 18621 18622 18623 18624 18625 18626 18627 18628 18629 18630 18631 18632 18633 18634 18635 18636 18637 18638 18639 18640 18641 18642 18643 18644 18645 18646 18647 18648 18649 18650 18651 18652 18653 18654 18655 18656 18657 18658 18659 18660 18661 18662 18663 18664 18665 18666 18667 18668 18669 18670 18671 18672 18673 18674 18675 18676 18677 18678 18679 18680 18681 18682 18683 18684 18685 18686 18687 18688 18689 18690 18691 18692 18693 18694 18695 18696 18697 18698 18699 18700 18701 18702 18703 18704 18705 18706 18707 18708 18709 18710 18711 18712 18713 18714 18715 18716 18717 18718 18719 18720 18721 18722 18723 18724 18725 18726 18727 18728 18729 18730 18731 18732 18733 18734 18735 18736 18737 18738 18739 18740 18741 18742 18743 18744 18745 18746 18747 18748 18749 18750 18751 18752 18753 18754 18755 18756 18757 18758 18759 18760 18761 18762 18763 18764 18765 18766 18767 18768 18769 18770 18771 18772 18773 18774 18775 18776 18777 18778 18779 18780 18781 18782 18783 18784 18785 18786 18787 18788 18789 18790 18791 18792 18793 18794 18795 18796 18797 18798 18799 18800 18801 18802 18803 18804 18805 18806 18807 18808 18809 18810 18811 18812 18813 18814 18815 18816 18817 18818 18819 18820 18821 18822 18823 18824 18825 18826 18827 18828 18829 18830 18831 18832 18833 18834 18835 18836 18837 18838 18839 18840 18841 18842 18843 18844 18845 18846 18847 18848 18849 18850 18851 18852 18853 18854 18855 18856 18857 18858 18859 18860 18861 18862 18863 18864 18865 18866 18867 18868 18869 18870 18871 18872 18873 18874 18875 18876 18877 18878 18879 18880 18881 18882 18883 18884 18885 18886 18887 18888 18889 18890 18891 18892 18893 18894 18895 18896 18897 18898 18899 18900 18901 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 18912 18913 18914 18915 18916 18917 18918 18919 18920 18921 18922 18923 18924 18925 18926 18927 18928 18929 18930 18931 18932 18933 18934 18935 18936 18937 18938 18939 18940 18941 18942 18943 18944 18945 18946 18947 18948 18949 18950 18951 18952 18953 18954 18955 18956 18957 18958 18959 18960 18961 18962 18963 18964 18965 18966 18967 18968 18969 18970 18971 18972 18973 18974 18975 18976 18977 18978 18979 18980 18981 18982 18983 18984 18985 18986 18987 18988 18989 18990 18991 18992 18993 18994 18995 18996 18997 18998 18999 19000 19001 19002 19003 19004 19005 19006 19007 19008 19009 19010 19011 19012 19013 19014 19015 19016 19017 19018 19019 19020 19021 19022 19023 19024 19025 19026 19027 19028 19029 19030 19031 19032 19033 19034 19035 19036 19037 19038 19039 19040 19041 19042 19043 19044 19045 19046 19047 19048 19049 19050 19051 19052 19053 19054 19055 19056 19057 19058 19059 19060 19061 19062 19063 19064 19065 19066 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090 19091 19092 19093 19094 19095 19096 19097 19098 19099 19100 19101 19102 19103 19104 19105 19106 19107 19108 19109 19110 19111 19112 19113 19114 19115 19116 19117 19118 19119 19120 19121 19122 19123 19124 19125 19126 19127 19128 19129 19130 19131 19132 19133 19134 19135 19136 19137 19138 19139 19140 19141 19142 19143 19144 19145 19146 19147 19148 19149 19150 19151 19152 19153 19154 19155 19156 19157 19158 19159 19160 19161 19162 19163 19164 19165 19166 19167 19168 19169 19170 19171 19172 19173 19174 19175 19176 19177 19178 19179 19180 19181 19182 19183 19184 19185 19186 19187 19188 19189 19190 19191 19192 19193 19194 19195 19196 19197 19198 19199 19200 19201 19202 19203 19204 19205 19206 19207 19208 19209 19210 19211 19212 19213 19214 19215 19216 19217 19218 19219 19220 19221 19222 19223 19224 19225 19226 19227 19228 19229 19230 19231 19232 19233 19234 19235 19236 19237 19238 19239 19240 19241 19242 19243 19244 19245 19246 19247 19248 19249 19250 19251 19252 19253 19254 19255 19256 19257 19258 19259 19260 19261 19262 19263 19264 19265 19266 19267 19268 19269 19270 19271 19272 19273 19274 19275 19276 19277 19278 19279 19280 19281 19282 19283 19284 19285 19286 19287 19288 19289 19290 19291 19292 19293 19294 19295 19296 19297 19298 19299 19300 19301 19302 19303 19304 19305 19306 19307 19308 19309 19310 19311 19312 19313 19314 19315 19316 19317 19318 19319 19320 19321 19322 19323 19324 19325 19326 19327 19328 19329 19330 19331 19332 19333 19334 19335 19336 19337 19338 19339 19340 19341 19342 19343 19344 19345 19346 19347 19348 19349 19350 19351 19352 19353 19354 19355 19356 19357 19358 19359 19360 19361 19362 19363 19364 19365 19366 19367 19368 19369 19370 19371 19372 19373 19374 19375 19376 19377 19378 19379 19380 19381 19382 19383 19384 19385 19386 19387 19388 19389 19390 19391 19392 19393 19394 19395 19396 19397 19398 19399 19400 19401 19402 19403 19404 19405 19406 19407 19408 19409 19410 19411 19412 19413 19414 19415 19416 19417 19418 19419 19420 19421 19422 19423 19424 19425 19426 19427 19428 19429 19430 19431 19432 19433 19434 19435 19436 19437 19438 19439 19440 19441 19442 19443 19444 19445 19446 19447 19448 19449 19450 19451 19452 19453 19454 19455 19456 19457 19458 19459 19460 19461 19462 19463 19464 19465 19466 19467 19468 19469 19470 19471 19472 19473 19474 19475 19476 19477 19478 19479 19480 19481 19482 19483 19484 19485 19486 19487 19488 19489 19490 19491 19492 19493 19494 19495 19496 19497 19498 19499 19500 19501 19502 19503 19504 19505 19506 19507 19508 19509 19510 19511 19512 19513 19514 19515 19516 19517 19518 19519 19520 19521 19522 19523 19524 19525 19526 19527 19528 19529 19530 19531 19532 19533 19534 19535 19536 19537 19538 19539 19540 19541 19542 19543 19544 19545 19546 19547 19548 19549 19550 19551 19552 19553 19554 19555 19556 19557 19558 19559 19560 19561 19562 19563 19564 19565 19566 19567 19568 19569 19570 19571 19572 19573 19574 19575 19576 19577 19578 19579 19580 19581 19582 19583 19584 19585 19586 19587 19588 19589 19590 19591 19592 19593 19594 19595 19596 19597 19598 19599 19600 19601 19602 19603 19604 19605 19606 19607 19608 19609 19610 19611 19612 19613 19614 19615 19616 19617 19618 19619 19620 19621 19622 19623 19624 19625 19626 19627 19628 19629 19630 19631 19632 19633 19634 19635 19636 19637 19638 19639 19640 19641 19642 19643 19644 19645 19646 19647 19648 19649 19650 19651 19652 19653 19654 19655 19656 19657 19658 19659 19660 19661 19662 19663 19664 19665 19666 19667 19668 19669 19670 19671 19672 19673 19674 19675 19676 19677 19678 19679 19680 19681 19682 19683 19684 19685 19686 19687 19688 19689 19690 19691 19692 19693 19694 19695 19696 19697 19698 19699 19700 19701 19702 19703 19704 19705 19706 19707 19708 19709 19710 19711 19712 19713 19714 19715 19716 19717 19718 19719 19720 19721 19722 19723 19724 19725 19726 19727 19728 19729 19730 19731 19732 19733 19734 19735 19736 19737 19738 19739 19740 19741 19742 19743 19744 19745 19746 19747 19748 19749 19750 19751 19752 19753 19754 19755 19756 19757 19758 19759 19760 19761 19762 19763 19764 19765 19766 19767 19768 19769 19770 19771 19772 19773 19774 19775 19776 19777 19778 19779 19780 19781 19782 19783 19784 19785 19786 19787 19788 19789 19790 19791 19792 19793 19794 19795 19796 19797 19798 19799 19800 19801 19802 19803 19804 19805 19806 19807 19808 19809 19810 19811 19812 19813 19814 19815 19816 19817 19818 19819 19820 19821 19822 19823 19824 19825 19826 19827 19828 19829 19830 19831 19832 19833 19834 19835 19836 19837 19838 19839 19840 19841 19842 19843 19844 19845 19846 19847 19848 19849 19850 19851 19852 19853 19854 19855 19856 19857 19858 19859 19860 19861 19862 19863 19864 19865 19866 19867 19868 19869 19870 19871 19872 19873 19874 19875 19876 19877 19878 19879 19880 19881 19882 19883 19884 19885 19886 19887 19888 19889 19890 19891 19892 19893 19894 19895 19896 19897 19898 19899 19900 19901 19902 19903 19904 19905 19906 19907 19908 19909 19910 19911 19912 19913 19914 19915 19916 19917 19918 19919 19920 19921 19922 19923 19924 19925 19926 19927 19928 19929 19930 19931 19932 19933 19934 19935 19936 19937 19938 19939 19940 19941 19942 19943 19944 19945 19946 19947 19948 19949 19950 19951 19952 19953 19954 19955 19956 19957 19958 19959 19960 19961 19962 19963 19964 19965 19966 19967 19968 19969 19970 19971 19972 19973 19974 19975 19976 19977 19978 19979 19980 19981 19982 19983 19984 19985 19986 19987 19988 19989 19990 19991 19992 19993 19994 19995 19996 19997 19998 19999 20000 20001 20002 20003 20004 20005 20006 20007 20008 20009 20010 20011 20012 20013 20014 20015 20016 20017 20018 20019 20020 20021 20022 20023 20024 20025 20026 20027 20028 20029 20030 20031 20032 20033 20034 20035 20036 20037 20038 20039 20040 20041 20042 20043 20044 20045 20046 20047 20048 20049 20050 20051 20052 20053 20054 20055 20056 20057 20058 20059 20060 20061 20062 20063 20064 20065 20066 20067 20068 20069 20070 20071 20072 20073 20074 20075 20076 20077 20078 20079 20080 20081 20082 20083 20084 20085 20086 20087 20088 20089 20090 20091 20092 20093 20094 20095 20096 20097 20098 20099 20100 20101 20102 20103 20104 20105 20106 20107 20108 20109 20110 20111 20112 20113 20114 20115 20116 20117 20118 20119 20120 20121 20122 20123 20124 20125 20126 20127 20128 20129 20130 20131 20132 20133 20134 20135 20136 20137 20138 20139 20140 20141 20142 20143 20144 20145 20146 20147 20148 20149 20150 20151 20152 20153 20154 20155 20156 20157 20158 20159 20160 20161 20162 20163 20164 20165 20166 20167 20168 20169 20170 20171 20172 20173 20174 20175 20176 20177 20178 20179 20180 20181 20182 20183 20184 20185 20186 20187 20188 20189 20190 20191 20192 20193 20194 20195 20196 20197 20198 20199 20200 20201 20202 20203 20204 20205 20206 20207 20208 20209 20210 20211 20212 20213 20214 20215 20216 20217 20218 20219 20220 20221 20222 20223 20224 20225 20226 20227 20228 20229 20230 20231 20232 20233 20234 20235 20236 20237 20238 20239 20240 20241 20242 20243 20244 20245 20246 20247 20248 20249 20250 20251 20252 20253 20254 20255 20256 20257 20258 20259 20260 20261 20262 20263 20264 20265 20266 20267 20268 20269 20270 20271 20272 20273 20274 20275 20276 20277 20278 20279 20280 20281 20282 20283 20284 20285 20286 20287 20288 20289 20290 20291 20292 20293 20294 20295 20296 20297 20298 20299 20300 20301 20302 20303 20304 20305 20306 20307 20308 20309 20310 20311 20312 20313 20314 20315 20316 20317 20318 20319 20320 20321 20322 20323 20324 20325 20326 20327 20328 20329 20330 20331 20332 20333 20334 20335 20336 20337 20338 20339 20340 20341 20342 20343 20344 20345 20346 20347 20348 20349 20350 20351 20352 20353 20354 20355 20356 20357 20358 20359 20360 20361 20362 20363 20364 20365 20366 20367 20368 20369 20370 20371 20372 20373 20374 20375 20376 20377 20378 20379 20380 20381 20382 20383 20384 20385 20386 20387 20388 20389 20390 20391 20392 20393 20394 20395 20396 20397 20398 20399 20400 20401 20402 20403 20404 20405 20406 20407 20408 20409 20410 20411 20412 20413 20414 20415 20416 20417 20418 20419 20420 20421 20422 20423 20424 20425 20426 20427 20428 20429 20430 20431 20432 20433 20434 20435 20436 20437 20438 20439 20440 20441 20442 20443 20444 20445 20446 20447 20448 20449 20450 20451 20452 20453 20454 20455 20456 20457 20458 20459 20460 20461 20462 20463 20464 20465 20466 20467 20468 20469 20470 20471 20472 20473 20474 20475 20476 20477 20478 20479 20480 20481 20482 20483 20484 20485 20486 20487 20488 20489 20490 20491 20492 20493 20494 20495 20496 20497 20498 20499 20500 20501 20502 20503 20504 20505 20506 20507 20508 20509 20510 20511 20512 20513 20514 20515 20516 20517 20518 20519 20520 20521 20522 20523 20524 20525 20526 20527 20528 20529 20530 20531 20532 20533 20534 20535 20536 20537 20538 20539 20540 20541 20542 20543 20544 20545 20546 20547 20548 20549 20550 20551 20552 20553 20554 20555 20556 20557 20558 20559 20560 20561 20562 20563 20564 20565 20566 20567 20568 20569 20570 20571 20572 20573 20574 20575 20576 20577 20578 20579 20580 20581 20582 20583 20584 20585 20586 20587 20588 20589 20590 20591 20592 20593 20594 20595 20596 20597 20598 20599 20600 20601 20602 20603 20604 20605 20606 20607 20608 20609 20610 20611 20612 20613 20614 20615 20616 20617 20618 20619 20620 20621 20622 20623 20624 20625 20626 20627 20628 20629 20630 20631 20632 20633 20634 20635 20636 20637 20638 20639 20640 20641 20642 20643 20644 20645 20646 20647 20648 20649 20650 20651 20652 20653 20654 20655 20656 20657 20658 20659 20660 20661 20662 20663 20664 20665 20666 20667 20668 20669 20670 20671 20672 20673 20674 20675 20676 20677 20678 20679 20680 20681 20682 20683 20684 20685 20686 20687 20688 20689 20690 20691 20692 20693 20694 20695 20696 20697 20698 20699 20700 20701 20702 20703 20704 20705 20706 20707 20708 20709 20710 20711 20712 20713 20714 20715 20716 20717 20718 20719 20720 20721 20722 20723 20724 20725 20726 20727 20728 20729 20730 20731 20732 20733 20734 20735 20736 20737 20738 20739 20740 20741 20742 20743 20744 20745 20746 20747 20748 20749 20750 20751 20752 20753 20754 20755 20756 20757 20758 20759 20760 20761 20762 20763 20764 20765 20766 20767 20768 20769 20770 20771 20772 20773 20774 20775 20776 20777 20778 20779 20780 20781 20782 20783 20784 20785 20786 20787 20788 20789 20790 20791 20792 20793 20794 20795 20796 20797 20798 20799 20800 20801 20802 20803 20804 20805 20806 20807 20808 20809 20810 20811 20812 20813 20814 20815 20816 20817 20818 20819 20820 20821 20822 20823 20824 20825 20826 20827 20828 20829 20830 20831 20832 20833 20834 20835 20836 20837 20838 20839 20840 20841 20842 20843 20844 20845 20846 20847 20848 20849 20850 20851 20852 20853 20854 20855 20856 20857 20858 20859 20860 20861 20862 20863 20864 20865 20866 20867 20868 20869 20870 20871 20872 20873 20874 20875 20876 20877 20878 20879 20880 20881 20882 20883 20884 20885 20886 20887 20888 20889 20890 20891 20892 20893 20894 20895 20896 20897 20898 20899 20900 20901 20902 20903 20904 20905 20906 20907 20908 20909 20910 20911 20912 20913 20914 20915 20916 20917 20918 20919 20920 20921 20922 20923 20924 20925 20926 20927 20928 20929 20930 20931 20932 20933 20934 20935 20936 20937 20938 20939 20940 20941 20942 20943 20944 20945 20946 20947 20948 20949 20950 20951 20952 20953 20954 20955 20956 20957 20958 20959 20960 20961 20962 20963 20964 20965 20966 20967 20968 20969 20970 20971 20972 20973 20974 20975 20976 20977 20978 20979 20980 20981 20982 20983 20984 20985 20986 20987 20988 20989 20990 20991 20992 20993 20994 20995 20996 20997 20998 20999 21000 21001 21002 21003 21004 21005 21006 21007 21008 21009 21010 21011 21012 21013 21014 21015 21016 21017 21018 21019 21020 21021 21022 21023 21024 21025 21026 21027 21028 21029 21030 21031 21032 21033 21034 21035 21036 21037 21038 21039 21040 21041 21042 21043 21044 21045 21046 21047 21048 21049 21050 21051 21052 21053 21054 21055 21056 21057 21058 21059 21060 21061 21062 21063 21064 21065 21066 21067 21068 21069 21070 21071 21072 21073 21074 21075 21076 21077 21078 21079 21080 21081 21082 21083 21084 21085 21086 21087 21088 21089 21090 21091 21092 21093 21094 21095 21096 21097 21098 21099 21100 21101 21102 21103 21104 21105 21106 21107 21108 21109 21110 21111 21112 21113 21114 21115 21116 21117 21118 21119 21120 21121 21122 21123 21124 21125 21126 21127 21128 21129 21130 21131 21132 21133 21134 21135 21136 21137 21138 21139 21140 21141 21142 21143 21144 21145 21146 21147 21148 21149 21150 21151 21152 21153 21154 21155 21156 21157 21158 21159 21160 21161 21162 21163 21164 21165 21166 21167 21168 21169 21170 21171 21172 21173 21174 21175 21176 21177 21178 21179 21180 21181 21182 21183 21184 21185 21186 21187 21188 21189 21190 21191 21192 21193 21194 21195 21196 21197 21198 21199 21200 21201 21202 21203 21204 21205 21206 21207 21208 21209 21210 21211 21212 21213 21214 21215 21216 21217 21218 21219 21220 21221 21222 21223 21224 21225 21226 21227 21228 21229 21230 21231 21232 21233 21234 21235 21236 21237 21238 21239 21240 21241 21242 21243 21244 21245 21246 21247 21248 21249 21250 21251 21252 21253 21254 21255 21256 21257 21258 21259 21260 21261 21262 21263 21264 21265 21266 21267 21268 21269 21270 21271 21272 21273 21274 21275 21276 21277 21278 21279 21280 21281 21282 21283 21284 21285 21286 21287 21288 21289 21290 21291 21292 21293 21294 21295 21296 21297 21298 21299 21300 21301 21302 21303 21304 21305 21306 21307 21308 21309 21310 21311 21312 21313 21314 21315 21316 21317 21318 21319 21320 21321 21322 21323 21324 21325 21326 21327 21328 21329 21330 21331 21332 21333 21334 21335 21336 21337 21338 21339 21340 21341 21342 21343 21344 21345 21346 21347 21348 21349 21350 21351 21352 21353 21354 21355 21356 21357 21358 21359 21360 21361 21362 21363 21364 21365 21366 21367 21368 21369 21370 21371 21372 21373 21374 21375 21376 21377 21378 21379 21380 21381 21382 21383 21384 21385 21386 21387 21388 21389 21390 21391 21392 21393 21394 21395 21396 21397 21398 21399 21400 21401 21402 21403 21404 21405 21406 21407 21408 21409 21410 21411 21412 21413 21414 21415 21416 21417 21418 21419 21420 21421 21422 21423 21424 21425 21426 21427 21428 21429 21430 21431 21432 21433 21434 21435 21436 21437 21438 21439 21440 21441 21442 21443 21444 21445 21446 21447 21448 21449 21450 21451 21452 21453 21454 21455 21456 21457 21458 21459 21460 21461 21462 21463 21464 21465 21466 21467 21468 21469 21470 21471 21472 21473 21474 21475 21476 21477 21478 21479 21480 21481 21482 21483 21484 21485 21486 21487 21488 21489 21490 21491 21492 21493 21494 21495 21496 21497 21498 21499 21500 21501 21502 21503 21504 21505 21506 21507 21508 21509 21510 21511 21512 21513 21514 21515 21516 21517 21518 21519 21520 21521 21522 21523 21524 21525 21526 21527 21528 21529 21530 21531 21532 21533 21534 21535 21536 21537 21538 21539 21540 21541 21542 21543 21544 21545 21546 21547 21548 21549 21550 21551 21552 21553 21554 21555 21556 21557 21558 21559 21560 21561 21562 21563 21564 21565 21566 21567 21568 21569 21570 21571 21572 21573 21574 21575 21576 21577 21578 21579 21580 21581 21582 21583 21584 21585 21586 21587 21588 21589 21590 21591 21592 21593 21594 21595 21596 21597 21598 21599 21600 21601 21602 21603 21604 21605 21606 21607 21608 21609 21610 21611 21612 21613 21614 21615 21616 21617 21618 21619 21620 21621 21622 21623 21624 21625 21626 21627 21628 21629 21630 21631 21632 21633 21634 21635 21636 21637 21638 21639 21640 21641 21642 21643 21644 21645 21646 21647 21648 21649 21650 21651 21652 21653 21654 21655 21656 21657 21658 21659 21660 21661 21662 21663 21664 21665 21666 21667 21668 21669 21670 21671 21672 21673 21674 21675 21676 21677 21678 21679 21680 21681 21682 21683 21684 21685 21686 21687 21688 21689 21690 21691 21692 21693 21694 21695 21696 21697 21698 21699 21700 21701 21702 21703 21704 21705 21706 21707 21708 21709 21710 21711 21712 21713 21714 21715 21716 21717 21718 21719 21720 21721 21722 21723 21724 21725 21726 21727 21728 21729 21730 21731 21732 21733 21734 21735 21736 21737 21738 21739 21740 21741 21742 21743 21744 21745 21746 21747 21748 21749 21750 21751 21752 21753 21754 21755 21756 21757 21758 21759 21760 21761 21762 21763 21764 21765 21766 21767 21768 21769 21770 21771 21772 21773 21774 21775 21776 21777 21778 21779 21780 21781 21782 21783 21784 21785 21786 21787 21788 21789 21790 21791 21792 21793 21794 21795 21796 21797 21798 21799 21800 21801 21802 21803 21804 21805 21806 21807 21808 21809 21810 21811 21812 21813 21814 21815 21816 21817 21818 21819 21820 21821 21822 21823 21824 21825 21826 21827 21828 21829 21830 21831 21832 21833 21834 21835 21836 21837 21838 21839 21840 21841 21842 21843 21844 21845 21846 21847 21848 21849 21850 21851 21852 21853 21854 21855 21856 21857 21858 21859 21860 21861 21862 21863 21864 21865 21866 21867 21868 21869 21870 21871 21872 21873 21874 21875 21876 21877 21878 21879 21880 21881 21882 21883 21884 21885 21886 21887 21888 21889 21890 21891 21892 21893 21894 21895 21896 21897 21898 21899 21900 21901 21902 21903 21904 21905 21906 21907 21908 21909 21910 21911 21912 21913 21914 21915 21916 21917 21918 21919 21920 21921 21922 21923 21924 21925 21926 21927 21928 21929 21930 21931 21932 21933 21934 21935 21936 21937 21938 21939 21940 21941 21942 21943 21944 21945 21946 21947 21948 21949 21950 21951 21952 21953 21954 21955 21956 21957 21958 21959 21960 21961 21962 21963 21964 21965 21966 21967 21968 21969 21970 21971 21972 21973 21974 21975 21976 21977 21978 21979 21980 21981 21982 21983 21984 21985 21986 21987 21988 21989 21990 21991 21992 21993 21994 21995 21996 21997 21998 21999 22000 22001 22002 22003 22004 22005 22006 22007 22008 22009 22010 22011 22012 22013 22014 22015 22016 22017 22018 22019 22020 22021 22022 22023 22024 22025 22026 22027 22028 22029 22030 22031 22032 22033 22034 22035 22036 22037 22038 22039 22040 22041 22042 22043 22044 22045 22046 22047 22048 22049 22050 22051 22052 22053 22054 22055 22056 22057 22058 22059 22060 22061 22062 22063 22064 22065 22066 22067 22068 22069 22070 22071 22072 22073 22074 22075 22076 22077 22078 22079 22080 22081 22082 22083 22084 22085 22086 22087 22088 22089 22090 22091 22092 22093 22094 22095 22096 22097 22098 22099 22100 22101 22102 22103 22104 22105 22106 22107 22108 22109 22110 22111 22112 22113 22114 22115 22116 22117 22118 22119 22120 22121 22122 22123 22124 22125 22126 22127 22128 22129 22130 22131 22132 22133 22134 22135 22136 22137 22138 22139 22140 22141 22142 22143 22144 22145 22146 22147 22148 22149 22150 22151 22152 22153 22154 22155 22156 22157 22158 22159 22160 22161 22162 22163 22164 22165 22166 22167 22168 22169 22170 22171 22172 22173 22174 22175 22176 22177 22178 22179 22180 22181 22182 22183 22184 22185 22186 22187 22188 22189 22190 22191 22192 22193 22194 22195 22196 22197 22198 22199 22200 22201 22202 22203 22204 22205 22206 22207 22208 22209 22210 22211 22212 22213 22214 22215 22216 22217 22218 22219 22220 22221 22222 22223 22224 22225 22226 22227 22228 22229 22230 22231 22232 22233 22234 22235 22236 22237 22238 22239 22240 22241 22242 22243 22244 22245 22246 22247 22248 22249 22250 22251 22252 22253 22254 22255 22256 22257 22258 22259 22260 22261 22262 22263 22264 22265 22266 22267 22268 22269 22270 22271 22272 22273 22274 22275 22276 22277 22278 22279 22280 22281 22282 22283 22284 22285 22286 22287 22288 22289 22290 22291 22292 22293 22294 22295 22296 22297 22298 22299 22300 22301 22302 22303 22304 22305 22306 22307 22308 22309 22310 22311 22312 22313 22314 22315 22316 22317 22318 22319 22320 22321 22322 22323 22324 22325 22326 22327 22328 22329 22330 22331 22332 22333 22334 22335 22336 22337 22338 22339 22340 22341 22342 22343 22344 22345 22346 22347 22348 22349 22350 22351 22352 22353 22354 22355 22356 22357 22358 22359 22360 22361 22362 22363 22364 22365 22366 22367 22368 22369 22370 22371 22372 22373 22374 22375 22376 22377 22378 22379 22380 22381 22382 22383 22384 22385 22386 22387 22388 22389 22390 22391 22392 22393 22394 22395 22396 22397 22398 22399 22400 22401 22402 22403 22404 22405 22406 22407 22408 22409 22410 22411 22412 22413 22414 22415 22416 22417 22418 22419 22420 22421 22422 22423 22424 22425 22426 22427 22428 22429 22430 22431 22432 22433 22434 22435 22436 22437 22438 22439 22440 22441 22442 22443 22444 22445 22446 22447 22448 22449 22450 22451 22452 22453 22454 22455 22456 22457 22458 22459 22460 22461 22462 22463 22464 22465 22466 22467 22468 22469 22470 22471 22472 22473 22474 22475 22476 22477 22478 22479 22480 22481 22482 22483 22484 22485 22486 22487 22488 22489 22490 22491 22492 22493 22494 22495 22496 22497 22498 22499 22500 22501 22502 22503 22504 22505 22506 22507 22508 22509 22510 22511 22512 22513 22514 22515 22516 22517 22518 22519 22520 22521 22522 22523 22524 22525 22526 22527 22528 22529 22530 22531 22532 22533 22534 22535 22536 22537 22538 22539 22540 22541 22542 22543 22544 22545 22546 22547 22548 22549 22550 22551 22552 22553 22554 22555 22556 22557 22558 22559 22560 22561 22562 22563 22564 22565 22566 22567 22568 22569 22570 22571 22572 22573 22574 22575 22576 22577 22578 22579 22580 22581 22582 22583 22584 22585 22586 22587 22588 22589 22590 22591 22592 22593 22594 22595 22596 22597 22598 22599 22600 22601 22602 22603 22604 22605 22606 22607 22608 22609 22610 22611 22612 22613 22614 22615 22616 22617 22618 22619 22620 22621 22622 22623 22624 22625 22626 22627 22628 22629 22630 22631 22632 22633 22634 22635 22636 22637 22638 22639 22640 22641 22642 22643 22644 22645 22646 22647 22648 22649 22650 22651 22652 22653 22654 22655 22656 22657 22658 22659 22660 22661 22662 22663 22664 22665 22666 22667 22668 22669 22670 22671 22672 22673 22674 22675 22676 22677 22678 22679 22680 22681 22682 22683 22684 22685 22686 22687 22688 22689 22690 22691 22692 22693 22694 22695 22696 22697 22698 22699 22700 22701 22702 22703 22704 22705 22706 22707 22708 22709 22710 22711 22712 22713 22714 22715 22716 22717 22718 22719 22720 22721 22722 22723 22724 22725 22726 22727 22728 22729 22730 22731 22732 22733 22734 22735 22736 22737 22738 22739 22740 22741 22742 22743 22744 22745 22746 22747 22748 22749 22750 22751 22752 22753 22754 22755 22756 22757 22758 22759 22760 22761 22762 22763 22764 22765 22766 22767 22768 22769 22770 22771 22772 22773 22774 22775 22776 22777 22778 22779 22780 22781 22782 22783 22784 22785 22786 22787 22788 22789 22790 22791 22792 22793 22794 22795 22796 22797 22798 22799 22800 22801 22802 22803 22804 22805 22806 22807 22808 22809 22810 22811 22812 22813 22814 22815 22816 22817 22818 22819 22820 22821 22822 22823 22824 22825 22826 22827 22828 22829 22830 22831 22832 22833 22834 22835 22836 22837 22838 22839 22840 22841 22842 22843 22844 22845 22846 22847 22848 22849 22850 22851 22852 22853 22854 22855 22856 22857 22858 22859 22860 22861 22862 22863 22864 22865 22866 22867 22868 22869 22870 22871 22872 22873 22874 22875 22876 22877 22878 22879 22880 22881 22882 22883 22884 22885 22886 22887 22888 22889 22890 22891 22892 22893 22894 22895 22896 22897 22898 22899 22900 22901 22902 22903 22904 22905 22906 22907 22908 22909 22910 22911 22912 22913 22914 22915 22916 22917 22918 22919 22920 22921 22922 22923 22924 22925 22926 22927 22928 22929 22930 22931 22932 22933 22934 22935 22936 22937 22938 22939 22940 22941 22942 22943 22944 22945 22946 22947 22948 22949 22950 22951 22952 22953 22954 22955 22956 22957 22958 22959 22960 22961 22962 22963 22964 22965 22966 22967 22968 22969 22970 22971 22972 22973 22974 22975 22976 22977 22978 22979 22980 22981 22982 22983 22984 22985 22986 22987 22988 22989 22990 22991 22992 22993 22994 22995 22996 22997 22998 22999 23000 23001 23002 23003 23004 23005 23006 23007 23008 23009 23010 23011 23012 23013 23014 23015 23016 23017 23018 23019 23020 23021 23022 23023 23024 23025 23026 23027 23028 23029 23030 23031 23032 23033 23034 23035 23036 23037 23038 23039 23040 23041 23042 23043 23044 23045 23046 23047 23048 23049 23050 23051 23052 23053 23054 23055 23056 23057 23058 23059 23060 23061 23062 23063 23064 23065 23066 23067 23068 23069 23070 23071 23072 23073 23074 23075 23076 23077 23078 23079 23080 23081 23082 23083 23084 23085 23086 23087 23088 23089 23090 23091 23092 23093 23094 23095 23096 23097 23098 23099 23100 23101 23102 23103 23104 23105 23106 23107 23108 23109 23110 23111 23112 23113 23114 23115 23116 23117 23118 23119 23120 23121 23122 23123 23124 23125 23126 23127 23128 23129 23130 23131 23132 23133 23134 23135 23136 23137 23138 23139 23140 23141 23142 23143 23144 23145 23146 23147 23148 23149 23150 23151 23152 23153 23154 23155 23156 23157 23158 23159 23160 23161 23162 23163 23164 23165 23166 23167 23168 23169 23170 23171 23172 23173 23174 23175 23176 23177 23178 23179 23180 23181 23182 23183 23184 23185 23186 23187 23188 23189 23190 23191 23192 23193 23194 23195 23196 23197 23198 23199 23200 23201 23202 23203 23204 23205 23206 23207 23208 23209 23210 23211 23212 23213 23214 23215 23216 23217 23218 23219 23220 23221 23222 23223 23224 23225 23226 23227 23228 23229 23230 23231 23232 23233 23234 23235 23236 23237 23238 23239 23240 23241 23242 23243 23244 23245 23246 23247 23248 23249 23250 23251 23252 23253 23254 23255 23256 23257 23258 23259 23260 23261 23262 23263 23264 23265 23266 23267 23268 23269 23270 23271 23272 23273 23274 23275 23276 23277 23278 23279 23280 23281 23282 23283 23284 23285 23286 23287 23288 23289 23290 23291 23292 23293 23294 23295 23296 23297 23298 23299 23300 23301 23302 23303 23304 23305 23306 23307 23308 23309 23310 23311 23312 23313 23314 23315 23316 23317 23318 23319 23320 23321 23322 23323 23324 23325 23326 23327 23328 23329 23330 23331 23332 23333 23334 23335 23336 23337 23338 23339 23340 23341 23342 23343 23344 23345 23346 23347 23348 23349 23350 23351 23352 23353 23354 23355 23356 23357 23358 23359 23360 23361 23362 23363 23364 23365 23366 23367 23368 23369 23370 23371 23372 23373 23374 23375 23376 23377 23378 23379 23380 23381 23382 23383 23384 23385 23386 23387 23388 23389 23390 23391 23392 23393 23394 23395 23396 23397 23398 23399 23400 23401 23402 23403 23404 23405 23406 23407 23408 23409 23410 23411 23412 23413 23414 23415 23416 23417 23418 23419 23420 23421 23422 23423 23424 23425 23426 23427 23428 23429 23430 23431 23432 23433 23434 23435 23436 23437 23438 23439 23440 23441 23442 23443 23444 23445 23446 23447 23448 23449 23450 23451 23452 23453 23454 23455 23456 23457 23458 23459 23460 23461 23462 23463 23464 23465 23466 23467 23468 23469 23470 23471 23472 23473 23474 23475 23476 23477 23478 23479 23480 23481 23482 23483 23484 23485 23486 23487 23488 23489 23490 23491 23492 23493 23494 23495 23496 23497 23498 23499 23500 23501 23502 23503 23504 23505 23506 23507 23508 23509 23510 23511 23512 23513 23514 23515 23516 23517 23518 23519 23520 23521 23522 23523 23524 23525 23526 23527 23528 23529 23530 23531 23532 23533 23534 23535 23536 23537 23538 23539 23540 23541 23542 23543 23544 23545 23546 23547 23548 23549 23550 23551 23552 23553 23554 23555 23556 23557 23558 23559 23560 23561 23562 23563 23564 23565 23566 23567 23568 23569 23570 23571 23572 23573 23574 23575 23576 23577 23578 23579 23580 23581 23582 23583 23584 23585 23586 23587 23588 23589 23590 23591 23592 23593 23594 23595 23596 23597 23598 23599 23600 23601 23602 23603 23604 23605 23606 23607 23608 23609 23610 23611 23612 23613 23614 23615 23616 23617 23618 23619 23620 23621 23622 23623 23624 23625 23626 23627 23628 23629 23630 23631 23632 23633 23634 23635 23636 23637 23638 23639 23640 23641 23642 23643 23644 23645 23646 23647 23648 23649 23650 23651 23652 23653 23654 23655 23656 23657 23658 23659 23660 23661 23662 23663 23664 23665 23666 23667 23668 23669 23670 23671 23672 23673 23674 23675 23676 23677 23678 23679 23680 23681 23682 23683 23684 23685 23686 23687 23688 23689 23690 23691 23692 23693 23694 23695 23696 23697 23698 23699 23700 23701 23702 23703 23704 23705 23706 23707 23708 23709 23710 23711 23712 23713 23714 23715 23716 23717 23718 23719 23720 23721 23722 23723 23724 23725 23726 23727 23728 23729 23730 23731 23732 23733 23734 23735 23736 23737 23738 23739 23740 23741 23742 23743 23744 23745 23746 23747 23748 23749 23750 23751 23752 23753 23754 23755 23756 23757 23758 23759 23760 23761 23762 23763 23764 23765 23766 23767 23768 23769 23770 23771 23772 23773 23774 23775 23776 23777 23778 23779 23780 23781 23782 23783 23784 23785 23786 23787 23788 23789 23790 23791 23792 23793 23794 23795 23796 23797 23798
|
; Complete source for Twobit and Sparc assembler in one file.
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; See 'twobit-benchmark', at end.
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Completely fundamental pathname manipulation.
; This takes zero or more directory components and a file name and
; constructs a filename relative to the current directory.
(define (make-relative-filename . components)
(define (construct l)
(if (null? (cdr l))
l
(cons (car l)
(cons "/" (construct (cdr l))))))
(if (null? (cdr components))
(car components)
(apply string-append (construct components))))
; This takes one or more directory components and constructs a
; directory name with proper termination (a crock -- we can finess
; this later).
(define (pathname-append . components)
(define (construct l)
(cond ((null? (cdr l))
l)
((string=? (car l) "")
(construct (cdr l)))
((char=? #\/ (string-ref (car l) (- (string-length (car l)) 1)))
(cons (car l) (construct (cdr l))))
(else
(cons (car l)
(cons "/" (construct (cdr l)))))))
(let ((n (if (null? (cdr components))
(car components)
(apply string-append (construct components)))))
(if (not (char=? #\/ (string-ref n (- (string-length n) 1))))
(string-append n "/")
n)))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Nbuild parameters for SPARC Larceny.
(define (make-nbuild-parameter dir source? verbose? hostdir hostname)
(let ((parameters
`((compiler . ,(pathname-append dir "Compiler"))
(util . ,(pathname-append dir "Util"))
(build . ,(pathname-append dir "Rts" "Build"))
(source . ,(pathname-append dir "Lib"))
(common-source . ,(pathname-append dir "Lib" "Common"))
(repl-source . ,(pathname-append dir "Repl"))
(interp-source . ,(pathname-append dir "Eval"))
(machine-source . ,(pathname-append dir "Lib" "Sparc"))
(common-asm . ,(pathname-append dir "Asm" "Common"))
(sparc-asm . ,(pathname-append dir "Asm" "Sparc"))
(target-machine . SPARC)
(endianness . big)
(word-size . 32)
(always-source? . ,source?)
(verbose-load? . ,verbose?)
(compatibility . ,(pathname-append dir "Compat" hostdir))
(host-system . ,hostname)
)))
(lambda (key)
(let ((probe (assq key parameters)))
(if probe
(cdr probe)
#f)))))
(define nbuild-parameter
(make-nbuild-parameter "" #f #f "Larceny" "Larceny"))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Useful list functions.
;
; Notes:
; * Reduce, reduce-right, fold-right, fold-left are compatible with MIT Scheme.
; * Make-list is compatible with MIT Scheme and Chez Scheme.
; * These are not (yet) compatible with Shivers's proposed list functions.
; * remq, remv, remove, remq!, remv!, remov!, every?, and some? are in the
; basic library.
; Destructively remove all associations whose key matches `key' from `alist'.
(define (aremq! key alist)
(cond ((null? alist) alist)
((eq? key (caar alist))
(aremq! key (cdr alist)))
(else
(set-cdr! alist (aremq! key (cdr alist)))
alist)))
(define (aremv! key alist)
(cond ((null? alist) alist)
((eqv? key (caar alist))
(aremv! key (cdr alist)))
(else
(set-cdr! alist (aremv! key (cdr alist)))
alist)))
(define (aremove! key alist)
(cond ((null? alist) alist)
((equal? key (caar alist))
(aremove! key (cdr alist)))
(else
(set-cdr! alist (aremove! key (cdr alist)))
alist)))
; Return a list of elements of `list' selected by the predicate.
(define (filter select? list)
(cond ((null? list) list)
((select? (car list))
(cons (car list) (filter select? (cdr list))))
(else
(filter select? (cdr list)))))
; Return the first element of `list' selected by the predicate.
(define (find selected? list)
(cond ((null? list) #f)
((selected? (car list)) (car list))
(else (find selected? (cdr list)))))
; Return a list with all duplicates (according to predicate) removed.
(define (remove-duplicates list same?)
(define (member? x list)
(cond ((null? list) #f)
((same? x (car list)) #t)
(else (member? x (cdr list)))))
(cond ((null? list) list)
((member? (car list) (cdr list))
(remove-duplicates (cdr list) same?))
(else
(cons (car list) (remove-duplicates (cdr list) same?)))))
; Return the least element of `list' according to some total order.
(define (least less? list)
(reduce (lambda (a b) (if (less? a b) a b)) #f list))
; Return the greatest element of `list' according to some total order.
(define (greatest greater? list)
(reduce (lambda (a b) (if (greater? a b) a b)) #f list))
; (mappend p l) = (apply append (map p l))
(define (mappend proc l)
(apply append (map proc l)))
; (make-list n) => (a1 ... an) for some ai
; (make-list n x) => (a1 ... an) where ai = x
(define (make-list nelem . rest)
(let ((val (if (null? rest) #f (car rest))))
(define (loop n l)
(if (zero? n)
l
(loop (- n 1) (cons val l))))
(loop nelem '())))
; (reduce p x ()) => x
; (reduce p x (a)) => a
; (reduce p x (a b ...)) => (p (p a b) ...))
(define (reduce proc initial l)
(define (loop val l)
(if (null? l)
val
(loop (proc val (car l)) (cdr l))))
(cond ((null? l) initial)
((null? (cdr l)) (car l))
(else (loop (car l) (cdr l)))))
; (reduce-right p x ()) => x
; (reduce-right p x (a)) => a
; (reduce-right p x (a b ...)) => (p a (p b ...))
(define (reduce-right proc initial l)
(define (loop l)
(if (null? (cdr l))
(car l)
(proc (car l) (loop (cdr l)))))
(cond ((null? l) initial)
((null? (cdr l)) (car l))
(else (loop l))))
; (fold-left p x (a b ...)) => (p (p (p x a) b) ...)
(define (fold-left proc initial l)
(if (null? l)
initial
(fold-left proc (proc initial (car l)) (cdr l))))
; (fold-right p x (a b ...)) => (p a (p b (p ... x)))
(define (fold-right proc initial l)
(if (null? l)
initial
(proc (car l) (fold-right proc initial (cdr l)))))
; (iota n) => (0 1 2 ... n-1)
(define (iota n)
(let loop ((n (- n 1)) (r '()))
(let ((r (cons n r)))
(if (= n 0)
r
(loop (- n 1) r)))))
; (list-head (a1 ... an) m) => (a1 ... am) for m <= n
(define (list-head l n)
(if (zero? n)
'()
(cons (car l) (list-head (cdr l) (- n 1)))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Larceny -- compatibility library for Twobit running under Larceny.
(define ($$trace x) #t)
(define host-system 'larceny)
; Temporary?
(define (.check! flag exn . args)
(if (not flag)
(apply error "Runtime check exception: " exn args)))
; The compatibility library loads Auxlib if compat:initialize is called
; without arguments. Compat:load will load fasl files when appropriate.
(define (compat:initialize . rest)
(if (null? rest)
(let ((dir (nbuild-parameter 'compatibility)))
(compat:load (string-append dir "compat2.sch"))
(compat:load (string-append dir "../../Auxlib/list.sch"))
(compat:load (string-append dir "../../Auxlib/pp.sch")))))
(define (with-optimization level thunk)
(thunk))
; Calls thunk1, and if thunk1 causes an error to be signalled, calls thunk2.
(define (call-with-error-control thunk1 thunk2)
(let ((eh (error-handler)))
(error-handler (lambda args
(error-handler eh)
(thunk2)
(apply eh args)))
(thunk1)
(error-handler eh)))
(define (larc-new-extension fn ext)
(let* ((l (string-length fn))
(x (let loop ((i (- l 1)))
(cond ((< i 0) #f)
((char=? (string-ref fn i) #\.) (+ i 1))
(else (loop (- i 1)))))))
(if (not x)
(string-append fn "." ext)
(string-append (substring fn 0 x) ext))))
(define (compat:load filename)
(define (loadit fn)
(if (nbuild-parameter 'verbose-load?)
(format #t "~a~%" fn))
(load fn))
(if (nbuild-parameter 'always-source?)
(loadit filename)
(let ((fn (larc-new-extension filename "fasl")))
(if (and (file-exists? fn)
(compat:file-newer? fn filename))
(loadit fn)
(loadit filename)))))
(define (compat:file-newer? a b)
(let* ((ta (file-modification-time a))
(tb (file-modification-time b))
(limit (vector-length ta)))
(let loop ((i 0))
(cond ((= i limit)
#f)
((= (vector-ref ta i) (vector-ref tb i))
(loop (+ i 1)))
(else
(> (vector-ref ta i) (vector-ref tb i)))))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Larceny -- second part of compatibility code
; This file ought to be compiled, but doesn't have to be.
;
; 12 April 1999
(define host-system 'larceny) ; Don't remove this!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; A well-defined sorting procedure.
(define compat:sort (lambda (list less?) (sort list less?)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Well-defined character codes.
; Returns the UCS-2 code for a character.
(define compat:char->integer char->integer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Input and output
(define (write-lop item port)
(lowlevel-write item port)
(newline port)
(newline port))
(define write-fasl-datum lowlevel-write)
; The power of self-hosting ;-)
(define (misc->bytevector x)
(let ((bv (bytevector-like-copy x)))
(typetag-set! bv $tag.bytevector-typetag)
bv))
(define string->bytevector misc->bytevector)
(define bignum->bytevector misc->bytevector)
(define (flonum->bytevector x)
(clear-first-word (misc->bytevector x)))
(define (compnum->bytevector x)
(clear-first-word (misc->bytevector x)))
; Clears garbage word of compnum/flonum; makes regression testing much
; easier.
(define (clear-first-word bv)
(bytevector-like-set! bv 0 0)
(bytevector-like-set! bv 1 0)
(bytevector-like-set! bv 2 0)
(bytevector-like-set! bv 3 0)
bv)
(define (list->bytevector l)
(let ((b (make-bytevector (length l))))
(do ((i 0 (+ i 1))
(l l (cdr l)))
((null? l) b)
(bytevector-set! b i (car l)))))
(define bytevector-word-ref
(let ((two^8 (expt 2 8))
(two^16 (expt 2 16))
(two^24 (expt 2 24)))
(lambda (bv i)
(+ (* (bytevector-ref bv i) two^24)
(* (bytevector-ref bv (+ i 1)) two^16)
(* (bytevector-ref bv (+ i 2)) two^8)
(bytevector-ref bv (+ i 3))))))
(define (twobit-format fmt . rest)
(let ((out (open-output-string)))
(apply format out fmt rest)
(get-output-string out)))
; This needs to be a random number in both a weaker and stronger sense
; than `random': it doesn't need to be a truly random number, so a sequence
; of calls can return a non-random sequence, but if two processes generate
; two sequences, then those sequences should not be the same.
;
; Gross, huh?
(define (an-arbitrary-number)
(system "echo \\\"`date`\\\" > a-random-number")
(let ((x (string-hash (call-with-input-file "a-random-number" read))))
(delete-file "a-random-number")
x))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Miscellaneous
(define cerror error)
; eof
; Copyright 1991 Wiliam Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Sets represented as lists.
;
; 5 April 1999.
(define (empty-set) '())
(define (empty-set? x) (null? x))
(define (make-set x)
(define (loop x y)
(cond ((null? x) y)
((member (car x) y) (loop (cdr x) y))
(else (loop (cdr x) (cons (car x) y)))))
(loop x '()))
(define (set-equal? x y)
(and (subset? x y) (subset? y x)))
(define (subset? x y)
(every? (lambda (x) (member x y))
x))
; To get around MacScheme's limit on the number of arguments.
(define apply-union)
(define union
(letrec ((union2
(lambda (x y)
(cond ((null? x) y)
((member (car x) y)
(union2 (cdr x) y))
(else (union2 (cdr x) (cons (car x) y)))))))
(set! apply-union
(lambda (sets)
(do ((sets sets (cdr sets))
(result '() (union2 (car sets) result)))
((null? sets)
result))))
(lambda args
(cond ((null? args) '())
((null? (cdr args)) (car args))
((null? (cddr args)) (union2 (car args) (cadr args)))
(else (union2 (union2 (car args)
(cadr args))
(apply union (cddr args))))))))
(define intersection
(letrec ((intersection2
(lambda (x y)
(cond ((null? x) '())
((member (car x) y)
(cons (car x) (intersection2 (cdr x) y)))
(else (intersection2 (cdr x) y))))))
(lambda args
(cond ((null? args) '())
((null? (cdr args)) (car args))
((null? (cddr args)) (intersection2 (car args) (cadr args)))
(else (intersection2 (intersection2 (car args)
(cadr args))
(apply intersection (cddr args))))))))
(define (difference x y)
(cond ((null? x) '())
((member (car x) y)
(difference (cdr x) y))
(else (cons (car x) (difference (cdr x) y)))))
; Reasonably portable hashing on EQ?, EQV?, EQUAL?.
; Requires bignums, SYMBOL-HASH.
;
; Given any Scheme object, returns a non-negative exact integer
; less than 2^24.
(define object-hash (lambda (x) 0)) ; hash on EQ?, EQV?
(define equal-hash (lambda (x) 0)) ; hash on EQUAL?
(let ((n 16777216)
(n-1 16777215)
(adj:fixnum 9000000)
(adj:negative 8000000)
(adj:large 7900000)
(adj:ratnum 7800000)
(adj:complex 7700000)
(adj:flonum 7000000)
(adj:compnum 6900000)
(adj:char 6111000)
(adj:string 5022200)
(adj:vector 4003330)
(adj:misc 3000444)
(adj:pair 2555000)
(adj:proc 2321001)
(adj:iport 2321002)
(adj:oport 2321003)
(adj:weird 2321004)
(budget0 32))
(define (combine hash adjustment)
(modulo (+ hash hash hash adjustment) 16777216))
(define (hash-on-equal x budget)
(if (> budget 0)
(cond ((string? x)
(string-hash x))
((pair? x)
(let ((budget (quotient budget 2)))
(combine (hash-on-equal (car x) budget)
(hash-on-equal (cdr x) budget))))
((vector? x)
(let ((n (vector-length x))
(budget (quotient budget 4)))
(if (> n 0)
(combine
(combine (hash-on-equal (vector-ref x 0) budget)
(hash-on-equal (vector-ref x (- n 1)) budget))
(hash-on-equal (vector-ref x (quotient n 2))
(+ budget budget)))
adj:vector)))
(else
(object-hash x)))
adj:weird))
(set! object-hash
(lambda (x)
(cond ((symbol? x)
(symbol-hash x))
((number? x)
(if (exact? x)
(cond ((integer? x)
(cond ((negative? x)
(combine (object-hash (- x)) adj:negative))
((< x n)
(combine x adj:fixnum))
(else
(combine (modulo x n) adj:large))))
((rational? x)
(combine (combine (object-hash (numerator x))
adj:ratnum)
(object-hash (denominator x))))
((real? x)
adj:weird)
((complex? x)
(combine (combine (object-hash (real-part x))
adj:complex)
(object-hash (imag-part x))))
(else
adj:weird))
(cond (#t
; We can't really do anything with inexact numbers
; unless infinities and NaNs behave reasonably.
adj:flonum)
((rational? x)
(combine
(combine (object-hash
(inexact->exact (numerator x)))
adj:flonum)
(object-hash (inexact->exact (denominator x)))))
((real? x)
adj:weird)
((complex? x)
(combine (combine (object-hash (real-part x))
adj:compnum)
(object-hash (imag-part x))))
(else adj:weird))))
((char? x)
(combine (char->integer x) adj:char))
((string? x)
(combine (string-length x) adj:string))
((vector? x)
(combine (vector-length x) adj:vector))
((eq? x #t)
(combine 1 adj:misc))
((eq? x #f)
(combine 2 adj:misc))
((null? x)
(combine 3 adj:misc))
((pair? x)
adj:pair)
((procedure? x)
adj:proc)
((input-port? x)
adj:iport)
((output-port? x)
adj:oport)
(else
adj:weird))))
(set! equal-hash
(lambda (x)
(hash-on-equal x budget0)))); Hash tables.
; Requires CALL-WITHOUT-INTERRUPTS.
; This code should be thread-safe provided VECTOR-REF is atomic.
;
; (make-hashtable <hash-function> <bucket-searcher> <size>)
;
; Returns a newly allocated mutable hash table
; using <hash-function> as the hash function
; and <bucket-searcher>, e.g. ASSQ, ASSV, ASSOC, to search a bucket
; with <size> buckets at first, expanding the number of buckets as needed.
; The <hash-function> must accept a key and return a non-negative exact
; integer.
;
; (make-hashtable <hash-function> <bucket-searcher>)
;
; Equivalent to (make-hashtable <hash-function> <bucket-searcher> n)
; for some value of n chosen by the implementation.
;
; (make-hashtable <hash-function>)
;
; Equivalent to (make-hashtable <hash-function> assv).
;
; (make-hashtable)
;
; Equivalent to (make-hashtable object-hash assv).
;
; (hashtable-contains? <hashtable> <key>)
;
; Returns true iff the <hashtable> contains an entry for <key>.
;
; (hashtable-fetch <hashtable> <key> <flag>)
;
; Returns the value associated with <key> in the <hashtable> if the
; <hashtable> contains <key>; otherwise returns <flag>.
;
; (hashtable-get <hashtable> <key>)
;
; Equivalent to (hashtable-fetch <hashtable> <key> #f)
;
; (hashtable-put! <hashtable> <key> <value>)
;
; Changes the <hashtable> to associate <key> with <value>, replacing
; any existing association for <key>.
;
; (hashtable-remove! <hashtable> <key>)
;
; Removes any association for <key> within the <hashtable>.
;
; (hashtable-clear! <hashtable>)
;
; Removes all associations from the <hashtable>.
;
; (hashtable-size <hashtable>)
;
; Returns the number of keys contained within the <hashtable>.
;
; (hashtable-for-each <procedure> <hashtable>)
;
; The <procedure> must accept two arguments, a key and the value
; associated with that key. Calls the <procedure> once for each
; key-value association. The order of these calls is indeterminate.
;
; (hashtable-map <procedure> <hashtable>)
;
; The <procedure> must accept two arguments, a key and the value
; associated with that key. Calls the <procedure> once for each
; key-value association, and returns a list of the results. The
; order of the calls is indeterminate.
;
; (hashtable-copy <hashtable>)
;
; Returns a copy of the <hashtable>.
; These global variables are assigned new values later.
(define make-hashtable (lambda args '*))
(define hashtable-contains? (lambda (ht key) #f))
(define hashtable-fetch (lambda (ht key flag) flag))
(define hashtable-get (lambda (ht key) (hashtable-fetch ht key #f)))
(define hashtable-put! (lambda (ht key val) '*))
(define hashtable-remove! (lambda (ht key) '*))
(define hashtable-clear! (lambda (ht) '*))
(define hashtable-size (lambda (ht) 0))
(define hashtable-for-each (lambda (ht proc) '*))
(define hashtable-map (lambda (ht proc) '()))
(define hashtable-copy (lambda (ht) ht))
; Implementation.
; A hashtable is represented as a vector of the form
;
; #(("HASHTABLE") <count> <hasher> <searcher> <buckets>)
;
; where <count> is the number of associations within the hashtable,
; <hasher> is the hash function, <searcher> is the bucket searcher,
; and <buckets> is a vector of buckets.
;
; The <hasher> and <searcher> fields are constant, but
; the <count> and <buckets> fields are mutable.
;
; For thread-safe operation, the mutators must modify both
; as an atomic operation. Other operations do not require
; critical sections provided VECTOR-REF is an atomic operation
; and the operation does not modify the hashtable, does not
; reference the <count> field, and fetches the <buckets>
; field exactly once.
(let ((doc (list "HASHTABLE"))
(count (lambda (ht) (vector-ref ht 1)))
(count! (lambda (ht n) (vector-set! ht 1 n)))
(hasher (lambda (ht) (vector-ref ht 2)))
(searcher (lambda (ht) (vector-ref ht 3)))
(buckets (lambda (ht) (vector-ref ht 4)))
(buckets! (lambda (ht v) (vector-set! ht 4 v)))
(defaultn 10))
(let ((hashtable? (lambda (ht)
(and (vector? ht)
(= 5 (vector-length ht))
(eq? doc (vector-ref ht 0)))))
(hashtable-error (lambda (x)
(display "ERROR: Bad hash table: ")
(newline)
(write x)
(newline))))
; Internal operations.
(define (make-ht hashfun searcher size)
(vector doc 0 hashfun searcher (make-vector size '())))
; Substitute x for the first occurrence of y within the list z.
; y is known to occur within z.
(define (substitute1 x y z)
(cond ((eq? y (car z))
(cons x (cdr z)))
(else
(cons (car z)
(substitute1 x y (cdr z))))))
; Remove the first occurrence of x from y.
; x is known to occur within y.
(define (remq1 x y)
(cond ((eq? x (car y))
(cdr y))
(else
(cons (car y)
(remq1 x (cdr y))))))
(define (resize ht0)
(call-without-interrupts
(lambda ()
(let ((ht (make-ht (hasher ht0)
(searcher ht0)
(+ 1 (* 2 (count ht0))))))
(ht-for-each (lambda (key val)
(put! ht key val))
ht0)
(buckets! ht0 (buckets ht))))))
; Returns the contents of the hashtable as a vector of pairs.
(define (contents ht)
(let* ((v (buckets ht))
(n (vector-length v))
(z (make-vector (count ht) '())))
(define (loop i bucket j)
(if (null? bucket)
(if (= i n)
(if (= j (vector-length z))
z
(begin (display "BUG in hashtable")
(newline)
'#()))
(loop (+ i 1)
(vector-ref v i)
j))
(let ((entry (car bucket)))
(vector-set! z j (cons (car entry) (cdr entry)))
(loop i
(cdr bucket)
(+ j 1)))))
(loop 0 '() 0)))
(define (contains? ht key)
(if (hashtable? ht)
(let* ((v (buckets ht))
(n (vector-length v))
(h (modulo ((hasher ht) key) n))
(b (vector-ref v h)))
(if ((searcher ht) key b)
#t
#f))
(hashtable-error ht)))
(define (fetch ht key flag)
(if (hashtable? ht)
(let* ((v (buckets ht))
(n (vector-length v))
(h (modulo ((hasher ht) key) n))
(b (vector-ref v h))
(probe ((searcher ht) key b)))
(if probe
(cdr probe)
flag))
(hashtable-error ht)))
(define (put! ht key val)
(if (hashtable? ht)
(call-without-interrupts
(lambda ()
(let* ((v (buckets ht))
(n (vector-length v))
(h (modulo ((hasher ht) key) n))
(b (vector-ref v h))
(probe ((searcher ht) key b)))
(if probe
; Using SET-CDR! on the probe would make it necessary
; to synchronize the CONTENTS routine.
(vector-set! v h (substitute1 (cons key val) probe b))
(begin (count! ht (+ (count ht) 1))
(vector-set! v h (cons (cons key val) b))
(if (> (count ht) n)
(resize ht)))))
#f))
(hashtable-error ht)))
(define (remove! ht key)
(if (hashtable? ht)
(call-without-interrupts
(lambda ()
(let* ((v (buckets ht))
(n (vector-length v))
(h (modulo ((hasher ht) key) n))
(b (vector-ref v h))
(probe ((searcher ht) key b)))
(if probe
(begin (count! ht (- (count ht) 1))
(vector-set! v h (remq1 probe b))
(if (< (* 2 (+ defaultn (count ht))) n)
(resize ht))))
#f)))
(hashtable-error ht)))
(define (clear! ht)
(if (hashtable? ht)
(call-without-interrupts
(lambda ()
(begin (count! ht 0)
(buckets! ht (make-vector defaultn '()))
#f)))
(hashtable-error ht)))
(define (size ht)
(if (hashtable? ht)
(count ht)
(hashtable-error ht)))
; This code must be written so that the procedure can modify the
; hashtable without breaking any invariants.
(define (ht-for-each f ht)
(if (hashtable? ht)
(let* ((v (contents ht))
(n (vector-length v)))
(do ((j 0 (+ j 1)))
((= j n))
(let ((x (vector-ref v j)))
(f (car x) (cdr x)))))
(hashtable-error ht)))
(define (ht-map f ht)
(if (hashtable? ht)
(let* ((v (contents ht))
(n (vector-length v)))
(do ((j 0 (+ j 1))
(results '() (let ((x (vector-ref v j)))
(cons (f (car x) (cdr x))
results))))
((= j n)
(reverse results))))
(hashtable-error ht)))
(define (ht-copy ht)
(if (hashtable? ht)
(let* ((newtable (make-hashtable (hasher ht) (searcher ht) 0))
(v (buckets ht))
(n (vector-length v))
(newvector (make-vector n '())))
(count! newtable (count ht))
(buckets! newtable newvector)
(do ((i 0 (+ i 1)))
((= i n))
(vector-set! newvector i (append (vector-ref v i) '())))
newtable)
(hashtable-error ht)))
; External entry points.
(set! make-hashtable
(lambda args
(let* ((hashfun (if (null? args) object-hash (car args)))
(searcher (if (or (null? args) (null? (cdr args)))
assv
(cadr args)))
(size (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
defaultn
(caddr args))))
(make-ht hashfun searcher size))))
(set! hashtable-contains? (lambda (ht key) (contains? ht key)))
(set! hashtable-fetch (lambda (ht key flag) (fetch ht key flag)))
(set! hashtable-get (lambda (ht key) (fetch ht key #f)))
(set! hashtable-put! (lambda (ht key val) (put! ht key val)))
(set! hashtable-remove! (lambda (ht key) (remove! ht key)))
(set! hashtable-clear! (lambda (ht) (clear! ht)))
(set! hashtable-size (lambda (ht) (size ht)))
(set! hashtable-for-each (lambda (ht proc) (ht-for-each ht proc)))
(set! hashtable-map (lambda (ht proc) (ht-map ht proc)))
(set! hashtable-copy (lambda (ht) (ht-copy ht)))
#f))
; Hash trees: a functional data structure analogous to hash tables.
;
; (make-hashtree <hash-function> <bucket-searcher>)
;
; Returns a newly allocated mutable hash table
; using <hash-function> as the hash function
; and <bucket-searcher>, e.g. ASSQ, ASSV, ASSOC, to search a bucket.
; The <hash-function> must accept a key and return a non-negative exact
; integer.
;
; (make-hashtree <hash-function>)
;
; Equivalent to (make-hashtree <hash-function> assv).
;
; (make-hashtree)
;
; Equivalent to (make-hashtree object-hash assv).
;
; (hashtree-contains? <hashtree> <key>)
;
; Returns true iff the <hashtree> contains an entry for <key>.
;
; (hashtree-fetch <hashtree> <key> <flag>)
;
; Returns the value associated with <key> in the <hashtree> if the
; <hashtree> contains <key>; otherwise returns <flag>.
;
; (hashtree-get <hashtree> <key>)
;
; Equivalent to (hashtree-fetch <hashtree> <key> #f)
;
; (hashtree-put <hashtree> <key> <value>)
;
; Returns a new hashtree that is like <hashtree> except that
; <key> is associated with <value>.
;
; (hashtree-remove <hashtree> <key>)
;
; Returns a new hashtree that is like <hashtree> except that
; <key> is not associated with any value.
;
; (hashtree-size <hashtree>)
;
; Returns the number of keys contained within the <hashtree>.
;
; (hashtree-for-each <procedure> <hashtree>)
;
; The <procedure> must accept two arguments, a key and the value
; associated with that key. Calls the <procedure> once for each
; key-value association. The order of these calls is indeterminate.
;
; (hashtree-map <procedure> <hashtree>)
;
; The <procedure> must accept two arguments, a key and the value
; associated with that key. Calls the <procedure> once for each
; key-value association, and returns a list of the results. The
; order of the calls is indeterminate.
; These global variables are assigned new values later.
(define make-hashtree (lambda args '*))
(define hashtree-contains? (lambda (ht key) #f))
(define hashtree-fetch (lambda (ht key flag) flag))
(define hashtree-get (lambda (ht key) (hashtree-fetch ht key #f)))
(define hashtree-put (lambda (ht key val) '*))
(define hashtree-remove (lambda (ht key) '*))
(define hashtree-size (lambda (ht) 0))
(define hashtree-for-each (lambda (ht proc) '*))
(define hashtree-map (lambda (ht proc) '()))
; Implementation.
; A hashtree is represented as a vector of the form
;
; #(("hashtree") <count> <hasher> <searcher> <buckets>)
;
; where <count> is the number of associations within the hashtree,
; <hasher> is the hash function, <searcher> is the bucket searcher,
; and <buckets> is generated by the following grammar:
;
; <buckets> ::= ()
; | (<fixnum> <associations> <buckets> <buckets>)
; <alist> ::= (<associations>)
; <associations> ::=
; | <association> <associations>
; <association> ::= (<key> . <value>)
;
; If <buckets> is of the form (n alist buckets1 buckets2),
; then n is the hash code of all keys in alist, all keys in buckets1
; have a hash code less than n, and all keys in buckets2 have a hash
; code greater than n.
(let ((doc (list "hashtree"))
(count (lambda (ht) (vector-ref ht 1)))
(hasher (lambda (ht) (vector-ref ht 2)))
(searcher (lambda (ht) (vector-ref ht 3)))
(buckets (lambda (ht) (vector-ref ht 4)))
(make-empty-buckets (lambda () '()))
(make-buckets
(lambda (h alist buckets1 buckets2)
(list h alist buckets1 buckets2)))
(buckets-empty? (lambda (buckets) (null? buckets)))
(buckets-n (lambda (buckets) (car buckets)))
(buckets-alist (lambda (buckets) (cadr buckets)))
(buckets-left (lambda (buckets) (caddr buckets)))
(buckets-right (lambda (buckets) (cadddr buckets))))
(let ((hashtree? (lambda (ht)
(and (vector? ht)
(= 5 (vector-length ht))
(eq? doc (vector-ref ht 0)))))
(hashtree-error (lambda (x)
(display "ERROR: Bad hash tree: ")
(newline)
(write x)
(newline))))
; Internal operations.
(define (make-ht count hashfun searcher buckets)
(vector doc count hashfun searcher buckets))
; Substitute x for the first occurrence of y within the list z.
; y is known to occur within z.
(define (substitute1 x y z)
(cond ((eq? y (car z))
(cons x (cdr z)))
(else
(cons (car z)
(substitute1 x y (cdr z))))))
; Remove the first occurrence of x from y.
; x is known to occur within y.
(define (remq1 x y)
(cond ((eq? x (car y))
(cdr y))
(else
(cons (car y)
(remq1 x (cdr y))))))
; Returns the contents of the hashtree as a list of pairs.
(define (contents ht)
(let* ((t (buckets ht)))
(define (contents t alist)
(if (buckets-empty? t)
alist
(contents (buckets-left t)
(contents (buckets-right t)
(append-reverse (buckets-alist t)
alist)))))
(define (append-reverse x y)
(if (null? x)
y
(append-reverse (cdr x)
(cons (car x) y))))
; Creating a new hashtree from a list that is almost sorted
; in hash code order would create an extremely unbalanced
; hashtree, so this routine randomizes the order a bit.
(define (randomize1 alist alist1 alist2 alist3)
(if (null? alist)
(randomize-combine alist1 alist2 alist3)
(randomize2 (cdr alist)
(cons (car alist) alist1)
alist2
alist3)))
(define (randomize2 alist alist1 alist2 alist3)
(if (null? alist)
(randomize-combine alist1 alist2 alist3)
(randomize3 (cdr alist)
alist1
(cons (car alist) alist2)
alist3)))
(define (randomize3 alist alist1 alist2 alist3)
(if (null? alist)
(randomize-combine alist1 alist2 alist3)
(randomize1 (cdr alist)
alist1
alist2
(cons (car alist) alist3))))
(define (randomize-combine alist1 alist2 alist3)
(cond ((null? alist2)
alist1)
((null? alist3)
(append-reverse alist2 alist1))
(else
(append-reverse
(randomize1 alist3 '() '() '())
(append-reverse
(randomize1 alist1 '() '() '())
(randomize1 alist2 '() '() '()))))))
(randomize1 (contents t '()) '() '() '())))
(define (contains? ht key)
(if (hashtree? ht)
(let* ((t (buckets ht))
(h ((hasher ht) key)))
(if ((searcher ht) key (find-bucket t h))
#t
#f))
(hashtree-error ht)))
(define (fetch ht key flag)
(if (hashtree? ht)
(let* ((t (buckets ht))
(h ((hasher ht) key))
(probe ((searcher ht) key (find-bucket t h))))
(if probe
(cdr probe)
flag))
(hashtree-error ht)))
; Given a <buckets> t and a hash code h, returns the alist for h.
(define (find-bucket t h)
(if (buckets-empty? t)
'()
(let ((n (buckets-n t)))
(cond ((< h n)
(find-bucket (buckets-left t) h))
((< n h)
(find-bucket (buckets-right t) h))
(else
(buckets-alist t))))))
(define (put ht key val)
(if (hashtree? ht)
(let ((t (buckets ht))
(h ((hasher ht) key))
(association (cons key val))
(c (count ht)))
(define (put t h)
(if (buckets-empty? t)
(begin (set! c (+ c 1))
(make-buckets h (list association) t t))
(let ((n (buckets-n t))
(alist (buckets-alist t))
(left (buckets-left t))
(right (buckets-right t)))
(cond ((< h n)
(make-buckets n
alist
(put (buckets-left t) h)
right))
((< n h)
(make-buckets n
alist
left
(put (buckets-right t) h)))
(else
(let ((probe ((searcher ht) key alist)))
(if probe
(make-buckets n
(substitute1 association
probe
alist)
left
right)
(begin
(set! c (+ c 1))
(make-buckets n
(cons association alist)
left
right)))))))))
(let ((buckets (put t h)))
(make-ht c (hasher ht) (searcher ht) buckets)))
(hashtree-error ht)))
(define (remove ht key)
(if (hashtree? ht)
(let ((t (buckets ht))
(h ((hasher ht) key))
(c (count ht)))
(define (remove t h)
(if (buckets-empty? t)
t
(let ((n (buckets-n t))
(alist (buckets-alist t))
(left (buckets-left t))
(right (buckets-right t)))
(cond ((< h n)
(make-buckets n
alist
(remove left h)
right))
((< n h)
(make-buckets n
alist
left
(remove right h)))
(else
(let ((probe ((searcher ht) key alist)))
(if probe
(begin (set! c (- c 1))
(make-buckets n
(remq1 probe alist)
left
right))
t)))))))
(let ((buckets (remove t h)))
(make-ht c (hasher ht) (searcher ht) buckets)))
(hashtree-error ht)))
(define (size ht)
(if (hashtree? ht)
(count ht)
(hashtree-error ht)))
(define (ht-for-each f ht)
(if (hashtree? ht)
(for-each (lambda (association)
(f (car association)
(cdr association)))
(contents ht))
(hashtree-error ht)))
(define (ht-map f ht)
(if (hashtree? ht)
(map (lambda (association)
(f (car association)
(cdr association)))
(contents ht))
(hashtree-error ht)))
; External entry points.
(set! make-hashtree
(lambda args
(let* ((hashfun (if (null? args) object-hash (car args)))
(searcher (if (or (null? args) (null? (cdr args)))
assv
(cadr args))))
(make-ht 0 hashfun searcher (make-empty-buckets)))))
(set! hashtree-contains? (lambda (ht key) (contains? ht key)))
(set! hashtree-fetch (lambda (ht key flag) (fetch ht key flag)))
(set! hashtree-get (lambda (ht key) (fetch ht key #f)))
(set! hashtree-put (lambda (ht key val) (put ht key val)))
(set! hashtree-remove (lambda (ht key) (remove ht key)))
(set! hashtree-size (lambda (ht) (size ht)))
(set! hashtree-for-each (lambda (ht proc) (ht-for-each ht proc)))
(set! hashtree-map (lambda (ht proc) (ht-map ht proc)))
#f))
; Copyright 1994 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 24 April 1999
;
; Compiler switches needed by Twobit.
(define make-twobit-flag)
(define display-twobit-flag)
(define make-twobit-flag
(lambda (name)
(define (twobit-warning)
(display "Error: incorrect arguments to ")
(write name)
(newline)
(reset))
(define (display-flag state)
(display (if state " + " " - "))
(display name)
(display " is ")
(display (if state "on" "off"))
(newline))
(let ((state #t))
(lambda args
(cond ((null? args) state)
((and (null? (cdr args))
(boolean? (car args)))
(set! state (car args))
state)
((and (null? (cdr args))
(eq? (car args) 'display))
(display-flag state))
(else (twobit-warning)))))))
(define (display-twobit-flag flag)
(flag 'display))
; Debugging and convenience.
(define issue-warnings
(make-twobit-flag 'issue-warnings))
(define include-source-code
(make-twobit-flag 'include-source-code))
(define include-variable-names
(make-twobit-flag 'include-variable-names))
(define include-procedure-names
(make-twobit-flag 'include-procedure-names))
; Space efficiency.
; This switch isn't fully implemented yet. If it is true, then
; Twobit will generate flat closures and will go to some trouble
; to zero stale registers and stack slots.
; Don't turn this switch off unless space is more important than speed.
(define avoid-space-leaks
(make-twobit-flag 'avoid-space-leaks))
; Major optimizations.
(define integrate-usual-procedures
(make-twobit-flag 'integrate-usual-procedures))
(define control-optimization
(make-twobit-flag 'control-optimization))
(define parallel-assignment-optimization
(make-twobit-flag 'parallel-assignment-optimization))
(define lambda-optimization
(make-twobit-flag 'lambda-optimization))
(define benchmark-mode
(make-twobit-flag 'benchmark-mode))
(define benchmark-block-mode
(make-twobit-flag 'benchmark-block-mode))
(define global-optimization
(make-twobit-flag 'global-optimization))
(define interprocedural-inlining
(make-twobit-flag 'interprocedural-inlining))
(define interprocedural-constant-propagation
(make-twobit-flag 'interprocedural-constant-propagation))
(define common-subexpression-elimination
(make-twobit-flag 'common-subexpression-elimination))
(define representation-inference
(make-twobit-flag 'representation-inference))
(define local-optimization
(make-twobit-flag 'local-optimization))
; For backwards compatibility, until I can change the code.
(define (ignore-space-leaks . args)
(if (null? args)
(not (avoid-space-leaks))
(avoid-space-leaks (not (car args)))))
(define lambda-optimizations lambda-optimization)
(define local-optimizations local-optimization)
(define (set-compiler-flags! how)
(case how
((no-optimization)
(set-compiler-flags! 'standard)
(avoid-space-leaks #t)
(integrate-usual-procedures #f)
(control-optimization #f)
(parallel-assignment-optimization #f)
(lambda-optimization #f)
(benchmark-mode #f)
(benchmark-block-mode #f)
(global-optimization #f)
(interprocedural-inlining #f)
(interprocedural-constant-propagation #f)
(common-subexpression-elimination #f)
(representation-inference #f)
(local-optimization #f))
((standard)
(issue-warnings #t)
(include-source-code #f)
(include-procedure-names #t)
(include-variable-names #t)
(avoid-space-leaks #f)
(runtime-safety-checking #t)
(integrate-usual-procedures #f)
(control-optimization #t)
(parallel-assignment-optimization #t)
(lambda-optimization #t)
(benchmark-mode #f)
(benchmark-block-mode #f)
(global-optimization #t)
(interprocedural-inlining #t)
(interprocedural-constant-propagation #t)
(common-subexpression-elimination #t)
(representation-inference #t)
(local-optimization #t))
((fast-safe)
(let ((bbmode (benchmark-block-mode)))
(set-compiler-flags! 'standard)
(integrate-usual-procedures #t)
(benchmark-mode #t)
(benchmark-block-mode bbmode)))
((fast-unsafe)
(set-compiler-flags! 'fast-safe)
(runtime-safety-checking #f))
(else
(error "set-compiler-flags!: unknown mode " how))))
(define (display-twobit-flags which)
(case which
((debugging)
(display-twobit-flag issue-warnings)
(display-twobit-flag include-procedure-names)
(display-twobit-flag include-variable-names)
(display-twobit-flag include-source-code))
((safety)
(display-twobit-flag avoid-space-leaks))
((optimization)
(display-twobit-flag integrate-usual-procedures)
(display-twobit-flag control-optimization)
(display-twobit-flag parallel-assignment-optimization)
(display-twobit-flag lambda-optimization)
(display-twobit-flag benchmark-mode)
(display-twobit-flag benchmark-block-mode)
(display-twobit-flag global-optimization)
(if (global-optimization)
(begin (display " ")
(display-twobit-flag interprocedural-inlining)
(display " ")
(display-twobit-flag interprocedural-constant-propagation)
(display " ")
(display-twobit-flag common-subexpression-elimination)
(display " ")
(display-twobit-flag representation-inference)))
(display-twobit-flag local-optimization))
(else
; The switch might mean something to the assembler, but not to Twobit
#t)))
; eof
; Copyright 1991 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 14 April 1999 / wdc
($$trace "pass1.aux")
;***************************************************************
;
; Each definition in this section should be overridden by an assignment
; in a target-specific file.
;
; If a lambda expression has more than @maxargs-with-rest-arg@ required
; arguments followed by a rest argument, then the macro expander will
; rewrite the lambda expression as a lambda expression with only one
; argument (a rest argument) whose body is a LET that binds the arguments
; of the original lambda expression.
(define @maxargs-with-rest-arg@
1000000) ; infinity
(define (prim-entry name) #f) ; no integrable procedures
(define (prim-arity name) 0) ; all of which take 0 arguments
(define (prim-opcodename name) name) ; and go by their source names
; End of definitions to be overridden by target-specific assignments.
;
;***************************************************************
; Miscellaneous routines.
(define (m-warn msg . more)
(if (issue-warnings)
(begin
(display "WARNING from macro expander:")
(newline)
(display msg)
(newline)
(for-each (lambda (x) (write x) (newline))
more))))
(define (m-error msg . more)
(display "ERROR detected during macro expansion:")
(newline)
(display msg)
(newline)
(for-each (lambda (x) (write x) (newline))
more)
(m-quit (make-constant #f)))
(define (m-bug msg . more)
(display "BUG in macro expander: ")
(newline)
(display msg)
(newline)
(for-each (lambda (x) (write x) (newline))
more)
(m-quit (make-constant #f)))
; Given a <formals>, returns a list of bound variables.
'
(define (make-null-terminated x)
(cond ((null? x) '())
((pair? x)
(cons (car x) (make-null-terminated (cdr x))))
(else (list x))))
; Returns the length of the given list, or -1 if the argument
; is not a list. Does not check for circular lists.
(define (safe-length x)
(define (loop x n)
(cond ((null? x) n)
((pair? x) (loop (cdr x) (+ n 1)))
(else -1)))
(loop x 0))
; Given a unary predicate and a list, returns a list of those
; elements for which the predicate is true.
(define (filter1 p x)
(cond ((null? x) '())
((p (car x)) (cons (car x) (filter1 p (cdr x))))
(else (filter1 p (cdr x)))))
; Given a unary predicate and a list, returns #t if the
; predicate is true of every element of the list.
(define (every1? p x)
(cond ((null? x) #t)
((p (car x)) (every1? p (cdr x)))
(else #f)))
; Binary union of two sets represented as lists, using equal?.
(define (union2 x y)
(cond ((null? x) y)
((member (car x) y)
(union2 (cdr x) y))
(else (union2 (cdr x) (cons (car x) y)))))
; Given an association list, copies the association pairs.
(define (copy-alist alist)
(map (lambda (x) (cons (car x) (cdr x)))
alist))
; Removes a value from a list. May destroy the list.
'
(define remq!
(letrec ((loop (lambda (x y prev)
(cond ((null? y) #t)
((eq? x (car y))
(set-cdr! prev (cdr y))
(loop x (cdr prev) prev))
(else
(loop x (cdr y) y))))))
(lambda (x y)
(cond ((null? y) '())
((eq? x (car y))
(remq! x (cdr y)))
(else
(loop x (cdr y) y)
y)))))
; Procedure-specific source code transformations.
; The transformer is passed a source code expression and a predicate
; and returns one of:
;
; the original source code expression
; a new source code expression to use in place of the original
; #f to indicate that the procedure is being called
; with an incorrect number of arguments or
; with an incorrect operand
;
; The original source code expression is guaranteed to be a list whose
; car is the name associated with the transformer.
; The predicate takes an identifier (a symbol) and returns true iff
; that identifier is bound to something other than its global binding.
;
; Since the procedures and their transformations are target-specific,
; they are defined in another file, in the Target subdirectory.
; FIXME:
; I think this is now used in only one place, in simplify-if.
(define (integrable? name)
(and (integrate-usual-procedures)
(prim-entry name)))
; MAKE-READABLE strips the referencing information
; and replaces (begin I) by I.
; If the optional argument is true, then it also reconstructs LET.
(define (make-readable exp . rest)
(let ((fancy? (and (not (null? rest))
(car rest))))
(define (make-readable exp)
(case (car exp)
((quote) (make-readable-quote exp))
((lambda) `(lambda ,(lambda.args exp)
,@(map (lambda (def)
`(define ,(def.lhs def)
,(make-readable (def.rhs def))))
(lambda.defs exp))
,(make-readable (lambda.body exp))))
((set!) `(set! ,(assignment.lhs exp)
,(make-readable (assignment.rhs exp))))
((if) `(if ,(make-readable (if.test exp))
,(make-readable (if.then exp))
,(make-readable (if.else exp))))
((begin) (if (variable? exp)
(variable.name exp)
`(begin ,@(map make-readable (begin.exprs exp)))))
(else (make-readable-call exp))))
(define (make-readable-quote exp)
(let ((x (constant.value exp)))
(if (and fancy?
(or (boolean? x)
(number? x)
(char? x)
(string? x)))
x
exp)))
(define (make-readable-call exp)
(let ((proc (call.proc exp)))
(if (and fancy?
(lambda? proc)
(list? (lambda.args proc)))
;(make-readable-let* exp '() '() '())
(make-readable-let exp)
`(,(make-readable (call.proc exp))
,@(map make-readable (call.args exp))))))
(define (make-readable-let exp)
(let* ((L (call.proc exp))
(formals (lambda.args L))
(args (map make-readable (call.args exp)))
(body (make-readable (lambda.body L))))
(if (and (null? (lambda.defs L))
(= (length args) 1)
(pair? body)
(or (and (eq? (car body) 'let)
(= (length (cadr body)) 1))
(eq? (car body) 'let*)))
`(let* ((,(car formals) ,(car args))
,@(cadr body))
,@(cddr body))
`(let ,(map list
(lambda.args L)
args)
,@(map (lambda (def)
`(define ,(def.lhs def)
,(make-readable (def.rhs def))))
(lambda.defs L))
,body))))
(define (make-readable-let* exp vars inits defs)
(if (and (null? defs)
(call? exp)
(lambda? (call.proc exp))
(= 1 (length (lambda.args (call.proc exp)))))
(let ((proc (call.proc exp))
(arg (car (call.args exp))))
(if (and (call? arg)
(lambda? (call.proc arg))
(= 1 (length (lambda.args (call.proc arg))))
(null? (lambda.defs (call.proc arg))))
(make-readable-let*
(make-call proc (list (lambda.body (call.proc arg))))
(cons (car (lambda.args (call.proc arg))) vars)
(cons (make-readable (car (call.args arg))) inits)
'())
(make-readable-let* (lambda.body proc)
(cons (car (lambda.args proc)) vars)
(cons (make-readable (car (call.args exp)))
inits)
(map (lambda (def)
`(define ,(def.lhs def)
,(make-readable (def.rhs def))))
(reverse (lambda.defs proc))))))
(cond ((or (not (null? vars))
(not (null? defs)))
`(let* ,(map list
(reverse vars)
(reverse inits))
,@defs
,(make-readable exp)))
((and (call? exp)
(lambda? (call.proc exp)))
(let ((proc (call.proc exp)))
`(let ,(map list
(lambda.args proc)
(map make-readable (call.args exp)))
,@(map (lambda (def)
`(define ,(def.lhs def)
,(make-readable (def.rhs def))))
(lambda.defs proc))
,(make-readable (lambda.body proc)))))
(else
(make-readable exp)))))
(make-readable exp)))
; For testing.
; MAKE-UNREADABLE does the reverse.
; It assumes there are no internal definitions.
(define (make-unreadable exp)
(cond ((symbol? exp) (list 'begin exp))
((pair? exp)
(case (car exp)
((quote) exp)
((lambda) (list 'lambda
(cadr exp)
'(begin)
(list '() '() '() '())
(make-unreadable (cons 'begin (cddr exp)))))
((set!) (list 'set! (cadr exp) (make-unreadable (caddr exp))))
((if) (list 'if
(make-unreadable (cadr exp))
(make-unreadable (caddr exp))
(if (= (length exp) 3)
'(unspecified)
(make-unreadable (cadddr exp)))))
((begin) (if (= (length exp) 2)
(make-unreadable (cadr exp))
(cons 'begin (map make-unreadable (cdr exp)))))
(else (map make-unreadable exp))))
(else (list 'quote exp))))
; Copyright 1991 William D Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 12 April 1999.
;
; Procedures for fetching and clobbering parts of expressions.
($$trace "pass2.aux")
(define (constant? exp) (eq? (car exp) 'quote))
(define (variable? exp)
(and (eq? (car exp) 'begin)
(null? (cddr exp))))
(define (lambda? exp) (eq? (car exp) 'lambda))
(define (call? exp) (pair? (car exp)))
(define (assignment? exp) (eq? (car exp) 'set!))
(define (conditional? exp) (eq? (car exp) 'if))
(define (begin? exp)
(and (eq? (car exp) 'begin)
(not (null? (cddr exp)))))
(define (make-constant value) (list 'quote value))
(define (make-variable name) (list 'begin name))
(define (make-lambda formals defs R F G decls doc body)
(list 'lambda
formals
(cons 'begin defs)
(list 'quote (list R F G decls doc))
body))
(define (make-call proc args) (cons proc (append args '())))
(define (make-assignment lhs rhs) (list 'set! lhs rhs))
(define (make-conditional e0 e1 e2) (list 'if e0 e1 e2))
(define (make-begin exprs)
(if (null? (cdr exprs))
(car exprs)
(cons 'begin (append exprs '()))))
(define (make-definition lhs rhs) (list 'define lhs rhs))
(define (constant.value exp) (cadr exp))
(define (variable.name exp) (cadr exp))
(define (lambda.args exp) (cadr exp))
(define (lambda.defs exp) (cdr (caddr exp)))
(define (lambda.R exp) (car (cadr (cadddr exp))))
(define (lambda.F exp) (cadr (cadr (cadddr exp))))
(define (lambda.G exp) (caddr (cadr (cadddr exp))))
(define (lambda.decls exp) (cadddr (cadr (cadddr exp))))
(define (lambda.doc exp) (car (cddddr (cadr (cadddr exp)))))
(define (lambda.body exp) (car (cddddr exp)))
(define (call.proc exp) (car exp))
(define (call.args exp) (cdr exp))
(define (assignment.lhs exp) (cadr exp))
(define (assignment.rhs exp) (caddr exp))
(define (if.test exp) (cadr exp))
(define (if.then exp) (caddr exp))
(define (if.else exp) (cadddr exp))
(define (begin.exprs exp) (cdr exp))
(define (def.lhs exp) (cadr exp))
(define (def.rhs exp) (caddr exp))
(define (variable-set! exp newexp)
(set-car! exp (car newexp))
(set-cdr! exp (append (cdr newexp) '())))
(define (lambda.args-set! exp args) (set-car! (cdr exp) args))
(define (lambda.defs-set! exp defs) (set-cdr! (caddr exp) defs))
(define (lambda.R-set! exp R) (set-car! (cadr (cadddr exp)) R))
(define (lambda.F-set! exp F) (set-car! (cdr (cadr (cadddr exp))) F))
(define (lambda.G-set! exp G) (set-car! (cddr (cadr (cadddr exp))) G))
(define (lambda.decls-set! exp decls) (set-car! (cdddr (cadr (cadddr exp))) decls))
(define (lambda.doc-set! exp doc) (set-car! (cddddr (cadr (cadddr exp))) doc))
(define (lambda.body-set! exp exp0) (set-car! (cddddr exp) exp0))
(define (call.proc-set! exp exp0) (set-car! exp exp0))
(define (call.args-set! exp exprs) (set-cdr! exp exprs))
(define (assignment.rhs-set! exp exp0) (set-car! (cddr exp) exp0))
(define (if.test-set! exp exp0) (set-car! (cdr exp) exp0))
(define (if.then-set! exp exp0) (set-car! (cddr exp) exp0))
(define (if.else-set! exp exp0) (set-car! (cdddr exp) exp0))
(define (begin.exprs-set! exp exprs) (set-cdr! exp exprs))
(define expression-set! variable-set!) ; used only by pass 3
; FIXME: This duplicates information in Lib/procinfo.sch.
(define (make-doc name arity formals source-code filename filepos)
(vector name source-code arity filename filepos formals))
(define (doc.name d) (vector-ref d 0))
(define (doc.code d) (vector-ref d 1))
(define (doc.arity d) (vector-ref d 2))
(define (doc.file d) (vector-ref d 3))
(define (doc.filepos d) (vector-ref d 4))
(define (doc.formals d) (vector-ref d 5))
(define (doc.name-set! d x) (if d (vector-set! d 0 x)))
(define (doc.code-set! d x) (if d (vector-set! d 1 x)))
(define (doc.arity-set! d x) (if d (vector-set! d 2 x)))
(define (doc.file-set! d x) (if d (vector-set! d 3 x)))
(define (doc.filepos-set! d x) (if d (vector-set! d 4 x)))
(define (doc.formals-set! d x) (if d (vector-set! d 5 x)))
(define (doc-copy d) (list->vector (vector->list d)))
(define (ignored? name) (eq? name name:IGNORED))
; Fairly harmless bug: rest arguments aren't getting flagged.
(define (flag-as-ignored name L)
(define (loop name formals)
(cond ((null? formals)
;(pass2-error p2error:violation-of-invariant name formals)
#t)
((symbol? formals) #t)
((eq? name (car formals))
(set-car! formals name:IGNORED)
(if (not (local? (lambda.R L) name:IGNORED))
(lambda.R-set! L
(cons (make-R-entry name:IGNORED '() '() '())
(lambda.R L)))))
(else (loop name (cdr formals)))))
(loop name (lambda.args L)))
(define (make-null-terminated formals)
(cond ((null? formals) '())
((symbol? formals) (list formals))
(else (cons (car formals)
(make-null-terminated (cdr formals))))))
(define (list-head x n)
(cond ((zero? n) '())
(else (cons (car x) (list-head (cdr x) (- n 1))))))
(define (remq x y)
(cond ((null? y) '())
((eq? x (car y)) (remq x (cdr y)))
(else (cons (car y) (remq x (cdr y))))))
(define (make-call-to-LIST args)
(cond ((null? args) (make-constant '()))
((null? (cdr args))
(make-call (make-variable name:CONS)
(list (car args) (make-constant '()))))
(else (make-call (make-variable name:LIST) args))))
(define (pass2-error i . etc)
(apply cerror (cons (vector-ref pass2-error-messages i) etc)))
(define pass2-error-messages
'#("System error: violation of an invariant in pass 2"
"Wrong number of arguments to known procedure"))
(define p2error:violation-of-invariant 0)
(define p2error:wna 1)
; Procedures for fetching referencing information from R-tables.
(define (make-R-entry name refs assigns calls)
(list name refs assigns calls))
(define (R-entry.name x) (car x))
(define (R-entry.references x) (cadr x))
(define (R-entry.assignments x) (caddr x))
(define (R-entry.calls x) (cadddr x))
(define (R-entry.references-set! x refs) (set-car! (cdr x) refs))
(define (R-entry.assignments-set! x assignments) (set-car! (cddr x) assignments))
(define (R-entry.calls-set! x calls) (set-car! (cdddr x) calls))
(define (local? R I)
(assq I R))
(define (R-entry R I)
(assq I R))
(define (R-lookup R I)
(or (assq I R)
(pass2-error p2error:violation-of-invariant R I)))
(define (references R I)
(cadr (R-lookup R I)))
(define (assignments R I)
(caddr (R-lookup R I)))
(define (calls R I)
(cadddr (R-lookup R I)))
(define (references-set! R I X)
(set-car! (cdr (R-lookup R I)) X))
(define (assignments-set! R I X)
(set-car! (cddr (R-lookup R I)) X))
(define (calls-set! R I X)
(set-car! (cdddr (R-lookup R I)) X))
; A notepad is a vector of the form #(L0 (L1 ...) (L2 ...) (I ...)),
; where the components are:
; element 0: a parent lambda expression (or #f if there is no enclosing
; parent, or we want to pretend that there isn't).
; element 1: a list of lambda expressions that the parent lambda
; expression encloses immediately.
; element 2: a subset of that list that does not escape.
; element 3: a list of free variables.
(define (make-notepad L)
(vector L '() '() '()))
(define (notepad.parent np) (vector-ref np 0))
(define (notepad.lambdas np) (vector-ref np 1))
(define (notepad.nonescaping np) (vector-ref np 2))
(define (notepad.vars np) (vector-ref np 3))
(define (notepad.lambdas-set! np x) (vector-set! np 1 x))
(define (notepad.nonescaping-set! np x) (vector-set! np 2 x))
(define (notepad.vars-set! np x) (vector-set! np 3 x))
(define (notepad-lambda-add! np L)
(notepad.lambdas-set! np (cons L (notepad.lambdas np))))
(define (notepad-nonescaping-add! np L)
(notepad.nonescaping-set! np (cons L (notepad.nonescaping np))))
(define (notepad-var-add! np I)
(let ((vars (notepad.vars np)))
(if (not (memq I vars))
(notepad.vars-set! np (cons I vars)))))
; Given a notepad, returns the list of variables that are closed
; over by some nested lambda expression that escapes.
(define (notepad-captured-variables np)
(let ((nonescaping (notepad.nonescaping np)))
(apply-union
(map (lambda (L)
(if (memq L nonescaping)
(lambda.G L)
(lambda.F L)))
(notepad.lambdas np)))))
; Given a notepad, returns a list of free variables computed
; as the union of the immediate free variables with the free
; variables of nested lambda expressions.
(define (notepad-free-variables np)
(do ((lambdas (notepad.lambdas np) (cdr lambdas))
(fv (notepad.vars np)
(let ((L (car lambdas)))
(union (difference (lambda.F L)
(make-null-terminated (lambda.args L)))
fv))))
((null? lambdas) fv)))
; Copyright 1992 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 13 December 1998
; Implementation-dependent parameters and preferences that determine
; how identifiers are represented in the output of the macro expander.
;
; The basic problem is that there are no reserved words, so the
; syntactic keywords of core Scheme that are used to express the
; output need to be represented by data that cannot appear in the
; input. This file defines those data.
($$trace "prefs")
; FIXME: The following definitions are currently ignored.
; The following definitions assume that identifiers of mixed case
; cannot appear in the input.
(define begin1 (string->symbol "Begin"))
(define define1 (string->symbol "Define"))
(define quote1 (string->symbol "Quote"))
(define lambda1 (string->symbol "Lambda"))
(define if1 (string->symbol "If"))
(define set!1 (string->symbol "Set!"))
; The following defines an implementation-dependent expression
; that evaluates to an undefined (not unspecified!) value, for
; use in expanding the (define x) syntax.
(define undefined1 (list (string->symbol "Undefined")))
; End of FIXME.
; A variable is renamed by suffixing a vertical bar followed by a unique
; integer. In IEEE and R4RS Scheme, a vertical bar cannot appear as part
; of an identifier, but presumably this is enforced by the reader and not
; by the compiler. Any other character that cannot appear as part of an
; identifier may be used instead of the vertical bar.
(define renaming-prefix-character #\.)
(define renaming-suffix-character #\|)
(define renaming-prefix (string renaming-prefix-character))
(define renaming-suffix (string renaming-suffix-character))
; Patches for Twobit. Here temporarily.
(define (make-toplevel-definition id exp)
(if (lambda? exp)
(doc.name-set! (lambda.doc exp) id))
(make-begin
(list (make-assignment id exp)
(make-constant id))))
(define (make-undefined)
(make-call (make-variable 'undefined) '()))
(define (make-unspecified)
(make-call (make-variable 'unspecified) '()))
; Copyright 1992 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 9 December 1998
; Syntactic environments.
;
; A syntactic environment maps identifiers to denotations,
; where a denotation is one of
;
; (special <special>)
; (macro <rules> <env>)
; (inline <rules> <env>)
; (identifier <id> <references> <assignments> <calls>)
;
; and where <special> is one of
;
; quote
; lambda
; if
; set!
; begin
; define
; define-syntax
; let-syntax
; letrec-syntax
; syntax-rules
;
; and where <rules> is a compiled <transformer spec> (see R4RS),
; <env> is a syntactic environment, and <id> is an identifier.
;
; An inline denotation is like a macro denotation, except that it
; is not an error when none of the rules match the use. Inline
; denotations are created by DEFINE-INLINE.
; The standard syntactic environment should not include any
; identifier denotations; space leaks will result if it does.
($$trace "syntaxenv")
(define standard-syntactic-environment
`((quote . (special quote))
(lambda . (special lambda))
(if . (special if))
(set! . (special set!))
(begin . (special begin))
(define . (special define))
(define-inline . (special define-inline))
(define-syntax . (special define-syntax))
(let-syntax . (special let-syntax))
(letrec-syntax . (special letrec-syntax))
(syntax-rules . (special syntax-rules))
))
; Unforgeable synonyms for lambda and set!, used to expand definitions.
(define lambda0 (string->symbol " lambda "))
(define set!0 (string->symbol " set! "))
(define (syntactic-copy env)
(copy-alist env))
(define (make-basic-syntactic-environment)
(cons (cons lambda0
(cdr (assq 'lambda standard-syntactic-environment)))
(cons (cons set!0
(cdr (assq 'set! standard-syntactic-environment)))
(syntactic-copy standard-syntactic-environment))))
; The global-syntactic-environment will always be a nonempty
; association list since there is no way to remove the entry
; for lambda0. That entry is used as a header by destructive
; operations.
(define global-syntactic-environment
(make-basic-syntactic-environment))
(define (global-syntactic-environment-set! env)
(set-cdr! global-syntactic-environment env)
#t)
(define (syntactic-bind-globally! id denotation)
(if (and (identifier-denotation? denotation)
(eq? id (identifier-name denotation)))
(letrec ((remove-bindings-for-id
(lambda (bindings)
(cond ((null? bindings) '())
((eq? (caar bindings) id)
(remove-bindings-for-id (cdr bindings)))
(else (cons (car bindings)
(remove-bindings-for-id (cdr bindings))))))))
(global-syntactic-environment-set!
(remove-bindings-for-id (cdr global-syntactic-environment))))
(let ((x (assq id global-syntactic-environment)))
(if x
(begin (set-cdr! x denotation) #t)
(global-syntactic-environment-set!
(cons (cons id denotation)
(cdr global-syntactic-environment)))))))
(define (syntactic-divert env1 env2)
(append env2 env1))
(define (syntactic-extend env ids denotations)
(syntactic-divert env (map cons ids denotations)))
(define (syntactic-lookup env id)
(let ((entry (assq id env)))
(if entry
(cdr entry)
(make-identifier-denotation id))))
(define (syntactic-assign! env id denotation)
(let ((entry (assq id env)))
(if entry
(set-cdr! entry denotation)
(m-bug "Bug detected in syntactic-assign!" env id denotation))))
; Denotations.
(define denotation-class car)
(define (special-denotation? denotation)
(eq? (denotation-class denotation) 'special))
(define (macro-denotation? denotation)
(eq? (denotation-class denotation) 'macro))
(define (inline-denotation? denotation)
(eq? (denotation-class denotation) 'inline))
(define (identifier-denotation? denotation)
(eq? (denotation-class denotation) 'identifier))
(define (make-macro-denotation rules env)
(list 'macro rules env))
(define (make-inline-denotation id rules env)
(list 'inline rules env id))
(define (make-identifier-denotation id)
(list 'identifier id '() '() '()))
(define macro-rules cadr)
(define macro-env caddr)
(define inline-rules macro-rules)
(define inline-env macro-env)
(define inline-name cadddr)
(define identifier-name cadr)
(define identifier-R-entry cdr)
(define (same-denotation? d1 d2)
(or (eq? d1 d2)
(and (identifier-denotation? d1)
(identifier-denotation? d2)
(eq? (identifier-name d1)
(identifier-name d2)))))
(define denotation-of-quote
(syntactic-lookup standard-syntactic-environment 'quote))
(define denotation-of-lambda
(syntactic-lookup standard-syntactic-environment 'lambda))
(define denotation-of-if
(syntactic-lookup standard-syntactic-environment 'if))
(define denotation-of-set!
(syntactic-lookup standard-syntactic-environment 'set!))
(define denotation-of-begin
(syntactic-lookup standard-syntactic-environment 'begin))
(define denotation-of-define
(syntactic-lookup standard-syntactic-environment 'define))
(define denotation-of-define-inline
(syntactic-lookup standard-syntactic-environment 'define-inline))
(define denotation-of-define-syntax
(syntactic-lookup standard-syntactic-environment 'define-syntax))
(define denotation-of-let-syntax
(syntactic-lookup standard-syntactic-environment 'let-syntax))
(define denotation-of-letrec-syntax
(syntactic-lookup standard-syntactic-environment 'letrec-syntax))
(define denotation-of-syntax-rules
(syntactic-lookup standard-syntactic-environment 'syntax-rules))
(define denotation-of-...
(syntactic-lookup standard-syntactic-environment '...))
(define denotation-of-transformer
(syntactic-lookup standard-syntactic-environment 'transformer))
; Given a syntactic environment env to be extended, an alist returned
; by rename-vars, and a syntactic environment env2, extends env by
; binding the fresh identifiers to the denotations of the original
; identifiers in env2.
(define (syntactic-alias env alist env2)
(syntactic-divert
env
(map (lambda (name-pair)
(let ((old-name (car name-pair))
(new-name (cdr name-pair)))
(cons new-name
(syntactic-lookup env2 old-name))))
alist)))
; Given a syntactic environment and an alist returned by rename-vars,
; extends the environment by binding the old identifiers to the fresh
; identifiers.
; For Twobit, it also binds the fresh identifiers to their denotations.
; This is ok so long as the fresh identifiers are not legal Scheme
; identifiers.
(define (syntactic-rename env alist)
(if (null? alist)
env
(let* ((old (caar alist))
(new (cdar alist))
(denotation (make-identifier-denotation new)))
(syntactic-rename
(cons (cons old denotation)
(cons (cons new denotation)
env))
(cdr alist)))))
; Renaming of variables.
(define renaming-counter 0)
(define (make-rename-procedure)
(set! renaming-counter (+ renaming-counter 1))
(let ((suffix (string-append renaming-suffix (number->string renaming-counter))))
(lambda (sym)
(if (symbol? sym)
(let ((s (symbol->string sym)))
(if (and (positive? (string-length s))
(char=? (string-ref s 0) renaming-prefix-character))
(string->symbol (string-append s suffix))
(string->symbol (string-append renaming-prefix s suffix))))
(m-warn "Illegal use of rename procedure" 'ok:FIXME sym)))))
; Given a datum, strips the suffixes from any symbols that appear within
; the datum, trying not to copy any more of the datum than necessary.
(define (m-strip x)
(define (original-symbol x)
(define (loop sym s i n)
(cond ((= i n) sym)
((char=? (string-ref s i)
renaming-suffix-character)
(string->symbol (substring s 1 i)))
(else
(loop sym s (+ i 1) n))))
(let ((s (symbol->string x)))
(if (and (positive? (string-length s))
(char=? (string-ref s 0) renaming-prefix-character))
(loop x s 0 (string-length s))
x)))
(cond ((symbol? x)
(original-symbol x))
((pair? x)
(let ((a (m-strip (car x)))
(b (m-strip (cdr x))))
(if (and (eq? a (car x))
(eq? b (cdr x)))
x
(cons a b))))
((vector? x)
(let* ((v (vector->list x))
(v2 (map m-strip v)))
(if (equal? v v2)
x
(list->vector v2))))
(else x)))
; Given a list of identifiers, or a formal parameter "list",
; returns an alist that associates each identifier with a fresh identifier.
(define (rename-vars original-vars)
(let ((rename (make-rename-procedure)))
(define (loop vars newvars)
(cond ((null? vars) (reverse newvars))
((pair? vars)
(let ((var (car vars)))
(if (symbol? var)
(loop (cdr vars)
(cons (cons var (rename var))
newvars))
(m-error "Illegal variable" var))))
((symbol? vars)
(loop (list vars) newvars))
(else (m-error "Malformed parameter list" original-vars))))
(loop original-vars '())))
; Given a <formals> and an alist returned by rename-vars that contains
; a new name for each formal identifier in <formals>, renames the
; formal identifiers.
(define (rename-formals formals alist)
(cond ((null? formals) '())
((pair? formals)
(cons (cdr (assq (car formals) alist))
(rename-formals (cdr formals) alist)))
(else (cdr (assq formals alist)))))
; Copyright 1992 William Clinger
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful purpose, and to redistribute this software
; is granted subject to the restriction that all copies made of this
; software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 23 November 1998
; Compiler for a <transformer spec>.
;
; References:
;
; The Revised^4 Report on the Algorithmic Language Scheme.
; Clinger and Rees [editors]. To appear in Lisp Pointers.
; Also available as a technical report from U of Oregon,
; MIT AI Lab, and Cornell.
;
; Macros That Work. Clinger and Rees. POPL '91.
;
; The input is a <transformer spec> and a syntactic environment.
; Syntactic environments are described in another file.
;
; The supported syntax differs from the R4RS in that vectors are
; allowed as patterns and as templates and are not allowed as
; pattern or template data.
;
; <transformer spec> --> (syntax-rules <literals> <rules>)
; <rules> --> () | (<rule> . <rules>)
; <rule> --> (<pattern> <template>)
; <pattern> --> <pattern_var> ; a <symbol> not in <literals>
; | <symbol> ; a <symbol> in <literals>
; | ()
; | (<pattern> . <pattern>)
; | (<ellipsis_pattern>)
; | #(<pattern>*) ; extends R4RS
; | #(<pattern>* <ellipsis_pattern>) ; extends R4RS
; | <pattern_datum>
; <template> --> <pattern_var>
; | <symbol>
; | ()
; | (<template2> . <template2>)
; | #(<template>*) ; extends R4RS
; | <pattern_datum>
; <template2> --> <template> | <ellipsis_template>
; <pattern_datum> --> <string> ; no <vector>
; | <character>
; | <boolean>
; | <number>
; <ellipsis_pattern> --> <pattern> ...
; <ellipsis_template> --> <template> ...
; <pattern_var> --> <symbol> ; not in <literals>
; <literals> --> () | (<symbol> . <literals>)
;
; Definitions.
;
; scope of an ellipsis
;
; Within a pattern or template, the scope of an ellipsis
; (...) is the pattern or template that appears to its left.
;
; rank of a pattern variable
;
; The rank of a pattern variable is the number of ellipses
; within whose scope it appears in the pattern.
;
; rank of a subtemplate
;
; The rank of a subtemplate is the number of ellipses within
; whose scope it appears in the template.
;
; template rank of an occurrence of a pattern variable
;
; The template rank of an occurrence of a pattern variable
; within a template is the rank of that occurrence, viewed
; as a subtemplate.
;
; variables bound by a pattern
;
; The variables bound by a pattern are the pattern variables
; that appear within it.
;
; referenced variables of a subtemplate
;
; The referenced variables of a subtemplate are the pattern
; variables that appear within it.
;
; variables opened by an ellipsis template
;
; The variables opened by an ellipsis template are the
; referenced pattern variables whose rank is greater than
; the rank of the ellipsis template.
;
;
; Restrictions.
;
; No pattern variable appears more than once within a pattern.
;
; For every occurrence of a pattern variable within a template,
; the template rank of the occurrence must be greater than or
; equal to the pattern variable's rank.
;
; Every ellipsis template must open at least one variable.
;
; For every ellipsis template, the variables opened by an
; ellipsis template must all be bound to sequences of the
; same length.
;
;
; The compiled form of a <rule> is
;
; <rule> --> (<pattern> <template> <inserted>)
; <pattern> --> <pattern_var>
; | <symbol>
; | ()
; | (<pattern> . <pattern>)
; | <ellipsis_pattern>
; | #(<pattern>)
; | <pattern_datum>
; <template> --> <pattern_var>
; | <symbol>
; | ()
; | (<template2> . <template2>)
; | #(<pattern>)
; | <pattern_datum>
; <template2> --> <template> | <ellipsis_template>
; <pattern_datum> --> <string>
; | <character>
; | <boolean>
; | <number>
; <pattern_var> --> #(<V> <symbol> <rank>)
; <ellipsis_pattern> --> #(<E> <pattern> <pattern_vars>)
; <ellipsis_template> --> #(<E> <template> <pattern_vars>)
; <inserted> --> () | (<symbol> . <inserted>)
; <pattern_vars> --> () | (<pattern_var> . <pattern_vars>)
; <rank> --> <exact non-negative integer>
;
; where <V> and <E> are unforgeable values.
; The pattern variables associated with an ellipsis pattern
; are the variables bound by the pattern, and the pattern
; variables associated with an ellipsis template are the
; variables opened by the ellipsis template.
;
;
; What's wrong with the above?
; If the template contains a big chunk that contains no pattern variables
; or inserted identifiers, then the big chunk will be copied unnecessarily.
; That shouldn't matter very often.
($$trace "syntaxrules")
(define pattern-variable-flag (list 'v))
(define ellipsis-pattern-flag (list 'e))
(define ellipsis-template-flag ellipsis-pattern-flag)
(define (make-patternvar v rank)
(vector pattern-variable-flag v rank))
(define (make-ellipsis-pattern P vars)
(vector ellipsis-pattern-flag P vars))
(define (make-ellipsis-template T vars)
(vector ellipsis-template-flag T vars))
(define (patternvar? x)
(and (vector? x)
(= (vector-length x) 3)
(eq? (vector-ref x 0) pattern-variable-flag)))
(define (ellipsis-pattern? x)
(and (vector? x)
(= (vector-length x) 3)
(eq? (vector-ref x 0) ellipsis-pattern-flag)))
(define (ellipsis-template? x)
(and (vector? x)
(= (vector-length x) 3)
(eq? (vector-ref x 0) ellipsis-template-flag)))
(define (patternvar-name V) (vector-ref V 1))
(define (patternvar-rank V) (vector-ref V 2))
(define (ellipsis-pattern P) (vector-ref P 1))
(define (ellipsis-pattern-vars P) (vector-ref P 2))
(define (ellipsis-template T) (vector-ref T 1))
(define (ellipsis-template-vars T) (vector-ref T 2))
(define (pattern-variable v vars)
(cond ((null? vars) #f)
((eq? v (patternvar-name (car vars)))
(car vars))
(else (pattern-variable v (cdr vars)))))
; Given a <transformer spec> and a syntactic environment,
; returns a macro denotation.
;
; A macro denotation is of the form
;
; (macro (<rule> ...) env)
;
; where each <rule> has been compiled as described above.
(define (m-compile-transformer-spec spec env)
(if (and (> (safe-length spec) 1)
(eq? (syntactic-lookup env (car spec))
denotation-of-syntax-rules))
(let ((literals (cadr spec))
(rules (cddr spec)))
(if (or (not (list? literals))
(not (every1? (lambda (rule)
(and (= (safe-length rule) 2)
(pair? (car rule))))
rules)))
(m-error "Malformed syntax-rules" spec))
(list 'macro
(map (lambda (rule)
(m-compile-rule rule literals env))
rules)
env))
(m-error "Malformed syntax-rules" spec)))
(define (m-compile-rule rule literals env)
(m-compile-pattern (cdr (car rule))
literals
env
(lambda (compiled-rule patternvars)
; FIXME
; should check uniqueness of pattern variables here
(cons compiled-rule
(m-compile-template
(cadr rule)
patternvars
env)))))
(define (m-compile-pattern P literals env k)
(define (loop P vars rank k)
(cond ((symbol? P)
(if (memq P literals)
(k P vars)
(let ((var (make-patternvar P rank)))
(k var (cons var vars)))))
((null? P) (k '() vars))
((pair? P)
(if (and (pair? (cdr P))
(symbol? (cadr P))
(same-denotation? (syntactic-lookup env (cadr P))
denotation-of-...))
(if (null? (cddr P))
(loop (car P)
'()
(+ rank 1)
(lambda (P vars1)
(k (make-ellipsis-pattern P vars1)
(union2 vars1 vars))))
(m-error "Malformed pattern" P))
(loop (car P)
vars
rank
(lambda (P1 vars)
(loop (cdr P)
vars
rank
(lambda (P2 vars)
(k (cons P1 P2) vars)))))))
((vector? P)
(loop (vector->list P)
vars
rank
(lambda (P vars)
(k (vector P) vars))))
(else (k P vars))))
(loop P '() 0 k))
(define (m-compile-template T vars env)
(define (loop T inserted referenced rank escaped? k)
(cond ((symbol? T)
(let ((x (pattern-variable T vars)))
(if x
(if (>= rank (patternvar-rank x))
(k x inserted (cons x referenced))
(m-error
"Too few ellipses follow pattern variable in template"
(patternvar-name x)))
(k T (cons T inserted) referenced))))
((null? T) (k '() inserted referenced))
((pair? T)
(cond ((and (not escaped?)
(symbol? (car T))
(same-denotation? (syntactic-lookup env (car T))
denotation-of-...)
(pair? (cdr T))
(null? (cddr T)))
(loop (cadr T) inserted referenced rank #t k))
((and (not escaped?)
(pair? (cdr T))
(symbol? (cadr T))
(same-denotation? (syntactic-lookup env (cadr T))
denotation-of-...))
(loop1 T inserted referenced rank escaped? k))
(else
(loop (car T)
inserted
referenced
rank
escaped?
(lambda (T1 inserted referenced)
(loop (cdr T)
inserted
referenced
rank
escaped?
(lambda (T2 inserted referenced)
(k (cons T1 T2) inserted referenced))))))))
((vector? T)
(loop (vector->list T)
inserted
referenced
rank
escaped?
(lambda (T inserted referenced)
(k (vector T) inserted referenced))))
(else (k T inserted referenced))))
(define (loop1 T inserted referenced rank escaped? k)
(loop (car T)
inserted
'()
(+ rank 1)
escaped?
(lambda (T1 inserted referenced1)
(loop (cddr T)
inserted
(append referenced1 referenced)
rank
escaped?
(lambda (T2 inserted referenced)
(k (cons (make-ellipsis-template
T1
(filter1 (lambda (var)
(> (patternvar-rank var)
rank))
referenced1))
T2)
inserted
referenced))))))
(loop T
'()
'()
0
#f
(lambda (T inserted referenced)
(list T inserted))))
; The pattern matcher.
;
; Given an input, a pattern, and two syntactic environments,
; returns a pattern variable environment (represented as an alist)
; if the input matches the pattern, otherwise returns #f.
(define empty-pattern-variable-environment
(list (make-patternvar (string->symbol "") 0)))
(define (m-match F P env-def env-use)
(define (match F P answer rank)
(cond ((null? P)
(and (null? F) answer))
((pair? P)
(and (pair? F)
(let ((answer (match (car F) (car P) answer rank)))
(and answer (match (cdr F) (cdr P) answer rank)))))
((symbol? P)
(and (symbol? F)
(same-denotation? (syntactic-lookup env-def P)
(syntactic-lookup env-use F))
answer))
((patternvar? P)
(cons (cons P F) answer))
((ellipsis-pattern? P)
(match1 F P answer (+ rank 1)))
((vector? P)
(and (vector? F)
(match (vector->list F) (vector-ref P 0) answer rank)))
(else (and (equal? F P) answer))))
(define (match1 F P answer rank)
(cond ((not (list? F)) #f)
((null? F)
(append (map (lambda (var) (cons var '()))
(ellipsis-pattern-vars P))
answer))
(else
(let* ((P1 (ellipsis-pattern P))
(answers (map (lambda (F) (match F P1 answer rank))
F)))
(if (every1? (lambda (answer) answer) answers)
(append (map (lambda (var)
(cons var
(map (lambda (answer)
(cdr (assq var answer)))
answers)))
(ellipsis-pattern-vars P))
answer)
#f)))))
(match F P empty-pattern-variable-environment 0))
(define (m-rewrite T alist)
(define (rewrite T alist rank)
(cond ((null? T) '())
((pair? T)
((if (ellipsis-pattern? (car T))
append
cons)
(rewrite (car T) alist rank)
(rewrite (cdr T) alist rank)))
((symbol? T) (cdr (assq T alist)))
((patternvar? T) (cdr (assq T alist)))
((ellipsis-template? T)
(rewrite1 T alist (+ rank 1)))
((vector? T)
(list->vector (rewrite (vector-ref T 0) alist rank)))
(else T)))
(define (rewrite1 T alist rank)
(let* ((T1 (ellipsis-template T))
(vars (ellipsis-template-vars T))
(rows (map (lambda (var) (cdr (assq var alist)))
vars)))
(map (lambda (alist) (rewrite T1 alist rank))
(make-columns vars rows alist))))
(define (make-columns vars rows alist)
(define (loop rows)
(if (null? (car rows))
'()
(cons (append (map (lambda (var row)
(cons var (car row)))
vars
rows)
alist)
(loop (map cdr rows)))))
(if (or (null? (cdr rows))
(apply = (map length rows)))
(loop rows)
(m-error "Use of macro is not consistent with definition"
vars
rows)))
(rewrite T alist 0))
; Given a use of a macro, the syntactic environment of the use,
; a continuation that expects a transcribed expression and
; a new environment in which to continue expansion, and a boolean
; that is true if this transcription is for an inline procedure,
; does the right thing.
(define (m-transcribe0 exp env-use k inline?)
(let* ((m (syntactic-lookup env-use (car exp)))
(rules (macro-rules m))
(env-def (macro-env m))
(F (cdr exp)))
(define (loop rules)
(if (null? rules)
(if inline?
(k exp env-use)
(m-error "Use of macro does not match definition" exp))
(let* ((rule (car rules))
(pattern (car rule))
(alist (m-match F pattern env-def env-use)))
(if alist
(let* ((template (cadr rule))
(inserted (caddr rule))
(alist2 (rename-vars inserted))
(newexp (m-rewrite template (append alist2 alist))))
(k newexp
(syntactic-alias env-use alist2 env-def)))
(loop (cdr rules))))))
(if (procedure? rules)
(m-transcribe-low-level exp env-use k rules env-def)
(loop rules))))
(define (m-transcribe exp env-use k)
(m-transcribe0 exp env-use k #f))
(define (m-transcribe-inline exp env-use k)
(m-transcribe0 exp env-use k #t))
; Copyright 1998 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Low-level macro facility based on explicit renaming. See
; William D Clinger. Hygienic macros through explicit renaming.
; In Lisp Pointers IV(4), 25-28, December 1991.
($$trace "lowlevel")
(define (m-transcribe-low-level exp env-use k transformer env-def)
(let ((rename0 (make-rename-procedure))
(renamed '())
(ok #t))
(define (lookup sym)
(let loop ((alist renamed))
(cond ((null? alist)
(syntactic-lookup env-use sym))
((eq? sym (cdr (car alist)))
(syntactic-lookup env-def (car (car alist))))
(else
(loop (cdr alist))))))
(let ((rename
(lambda (sym)
(if ok
(let ((probe (assq sym renamed)))
(if probe
(cdr probe)
(let ((sym2 (rename0 sym)))
(set! renamed (cons (cons sym sym2) renamed))
sym2)))
(m-error "Illegal use of a rename procedure" sym))))
(compare
(lambda (sym1 sym2)
(same-denotation? (lookup sym1) (lookup sym2)))))
(let ((exp2 (transformer exp rename compare)))
(set! ok #f)
(k exp2
(syntactic-alias env-use renamed env-def))))))
(define identifier? symbol?)
(define (identifier->symbol id)
(m-strip id))
; Copyright 1992 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 22 April 1999
($$trace "expand")
; This procedure sets the default scope of global macro definitions.
(define define-syntax-scope
(let ((flag 'letrec))
(lambda args
(cond ((null? args) flag)
((not (null? (cdr args)))
(apply m-warn
"Too many arguments passed to define-syntax-scope"
args))
((memq (car args) '(letrec letrec* let*))
(set! flag (car args)))
(else (m-warn "Unrecognized argument to define-syntax-scope"
(car args)))))))
; The main entry point.
; The outermost lambda allows known procedures to be lifted outside
; all local variables.
(define (macro-expand def-or-exp)
(call-with-current-continuation
(lambda (k)
(set! m-quit k)
(set! renaming-counter 0)
(make-call
(make-lambda '() ; formals
'() ; definitions
'() ; R
'() ; F
'() ; G
'() ; declarations
#f ; documentation
(desugar-definitions def-or-exp
global-syntactic-environment
make-toplevel-definition))
'()))))
(define (desugar-definitions exp env make-toplevel-definition)
(letrec
((define-loop
(lambda (exp rest first env)
(cond ((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-begin)
(pair? (cdr exp)))
(define-loop (cadr exp) (append (cddr exp) rest) first env))
((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-define))
(let ((exp (desugar-define exp env)))
(cond ((and (null? first) (null? rest))
exp)
((null? rest)
(make-begin (reverse (cons exp first))))
(else (define-loop (car rest)
(cdr rest)
(cons exp first)
env)))))
((and (pair? exp)
(symbol? (car exp))
(or (eq? (syntactic-lookup env (car exp))
denotation-of-define-syntax)
(eq? (syntactic-lookup env (car exp))
denotation-of-define-inline))
(null? first))
(define-syntax-loop exp rest env))
((and (pair? exp)
(symbol? (car exp))
(macro-denotation? (syntactic-lookup env (car exp))))
(m-transcribe exp
env
(lambda (exp env)
(define-loop exp rest first env))))
((and (null? first) (null? rest))
(m-expand exp env))
((null? rest)
(make-begin (reverse (cons (m-expand exp env) first))))
(else (make-begin
(append (reverse first)
(map (lambda (exp) (m-expand exp env))
(cons exp rest))))))))
(define-syntax-loop
(lambda (exp rest env)
(cond ((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-begin)
(pair? (cdr exp)))
(define-syntax-loop (cadr exp) (append (cddr exp) rest) env))
((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-define-syntax))
(if (pair? (cdr exp))
(redefinition (cadr exp)))
(if (null? rest)
(m-define-syntax exp env)
(begin (m-define-syntax exp env)
(define-syntax-loop (car rest) (cdr rest) env))))
((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-define-inline))
(if (pair? (cdr exp))
(redefinition (cadr exp)))
(if (null? rest)
(m-define-inline exp env)
(begin (m-define-inline exp env)
(define-syntax-loop (car rest) (cdr rest) env))))
((and (pair? exp)
(symbol? (car exp))
(macro-denotation? (syntactic-lookup env (car exp))))
(m-transcribe exp
env
(lambda (exp env)
(define-syntax-loop exp rest env))))
((and (pair? exp)
(symbol? (car exp))
(eq? (syntactic-lookup env (car exp))
denotation-of-define))
(define-loop exp rest '() env))
((null? rest)
(m-expand exp env))
(else (make-begin
(map (lambda (exp) (m-expand exp env))
(cons exp rest)))))))
(desugar-define
(lambda (exp env)
(cond
((null? (cdr exp)) (m-error "Malformed definition" exp))
; (define foo) syntax is transformed into (define foo (undefined)).
((null? (cddr exp))
(let ((id (cadr exp)))
(if (or (null? pass1-block-inlines)
(not (memq id pass1-block-inlines)))
(begin
(redefinition id)
(syntactic-bind-globally! id (make-identifier-denotation id))))
(make-toplevel-definition id (make-undefined))))
((pair? (cadr exp))
(desugar-define
(let* ((def (car exp))
(pattern (cadr exp))
(f (car pattern))
(args (cdr pattern))
(body (cddr exp)))
(if (and (symbol? (car (cadr exp)))
(benchmark-mode)
(list? (cadr exp)))
`(,def ,f
(,lambda0 ,args
((,lambda0 (,f)
(,set!0 ,f (,lambda0 ,args ,@body))
,pattern)
0)))
`(,def ,f (,lambda0 ,args ,@body))))
env))
((> (length exp) 3) (m-error "Malformed definition" exp))
(else (let ((id (cadr exp)))
(if (or (null? pass1-block-inlines)
(not (memq id pass1-block-inlines)))
(begin
(redefinition id)
(syntactic-bind-globally! id (make-identifier-denotation id))))
(make-toplevel-definition id (m-expand (caddr exp) env)))))))
(redefinition
(lambda (id)
(if (symbol? id)
(if (not (identifier-denotation?
(syntactic-lookup global-syntactic-environment id)))
(if (issue-warnings)
(m-warn "Redefining " id)))
(m-error "Malformed variable or keyword" id)))))
; body of letrec
(define-loop exp '() '() env)))
; Given an expression and a syntactic environment,
; returns an expression in core Scheme.
(define (m-expand exp env)
(cond ((not (pair? exp))
(m-atom exp env))
((not (symbol? (car exp)))
(m-application exp env))
(else
(let ((keyword (syntactic-lookup env (car exp))))
(case (denotation-class keyword)
((special)
(cond
((eq? keyword denotation-of-quote) (m-quote exp))
((eq? keyword denotation-of-lambda) (m-lambda exp env))
((eq? keyword denotation-of-if) (m-if exp env))
((eq? keyword denotation-of-set!) (m-set exp env))
((eq? keyword denotation-of-begin) (m-begin exp env))
((eq? keyword denotation-of-let-syntax)
(m-let-syntax exp env))
((eq? keyword denotation-of-letrec-syntax)
(m-letrec-syntax exp env))
((or (eq? keyword denotation-of-define)
(eq? keyword denotation-of-define-syntax)
(eq? keyword denotation-of-define-inline))
(m-error "Definition out of context" exp))
(else (m-bug "Bug detected in m-expand" exp env))))
((macro) (m-macro exp env))
((inline) (m-inline exp env))
((identifier) (m-application exp env))
(else (m-bug "Bug detected in m-expand" exp env)))))))
(define (m-atom exp env)
(cond ((not (symbol? exp))
; Here exp ought to be a boolean, number, character, or string.
; I'll warn about other things but treat them as if quoted.
;
; I'm turning off some of the warnings because notably procedures
; and #!unspecified can occur in loaded files and it's a major
; pain if a warning is printed for each. --lars
(if (and (not (boolean? exp))
(not (number? exp))
(not (char? exp))
(not (string? exp))
(not (procedure? exp))
(not (eq? exp (unspecified))))
(m-warn "Malformed constant -- should be quoted" exp))
(make-constant exp))
(else (let ((denotation (syntactic-lookup env exp)))
(case (denotation-class denotation)
((special macro)
(m-warn "Syntactic keyword used as a variable" exp)
; Syntactic keywords used as variables are treated as #t.
(make-constant #t))
((inline)
(make-variable (inline-name denotation)))
((identifier)
(let ((var (make-variable (identifier-name denotation)))
(R-entry (identifier-R-entry denotation)))
(R-entry.references-set!
R-entry
(cons var (R-entry.references R-entry)))
var))
(else (m-bug "Bug detected by m-atom" exp env)))))))
(define (m-quote exp)
(if (and (pair? (cdr exp))
(null? (cddr exp)))
(make-constant (m-strip (cadr exp)))
(m-error "Malformed quoted constant" exp)))
(define (m-lambda exp env)
(if (> (safe-length exp) 2)
(let* ((formals (cadr exp))
(alist (rename-vars formals))
(env (syntactic-rename env alist))
(body (cddr exp)))
(do ((alist alist (cdr alist)))
((null? alist))
(if (assq (caar alist) (cdr alist))
(m-error "Malformed parameter list" formals)))
; To simplify the run-time system, there's a limit on how many
; fixed arguments can be followed by a rest argument.
; That limit is removed here.
; Bug: documentation slot isn't right when this happens.
; Bug: this generates extremely inefficient code.
(if (and (not (list? formals))
(> (length alist) @maxargs-with-rest-arg@))
(let ((TEMP (car (rename-vars '(temp)))))
(m-lambda
`(,lambda0 ,TEMP
((,lambda0 ,(map car alist)
,@(cddr exp))
,@(do ((actuals '() (cons (list name:CAR path)
actuals))
(path TEMP (list name:CDR path))
(formals formals (cdr formals)))
((symbol? formals)
(append (reverse actuals) (list path))))))
env))
(make-lambda (rename-formals formals alist)
'() ; no definitions yet
(map (lambda (entry)
(cdr (syntactic-lookup env (cdr entry))))
alist) ; R
'() ; F
'() ; G
'() ; decls
(make-doc #f
(if (list? formals)
(length alist)
(exact->inexact (- (length alist) 1)))
(if (include-variable-names)
formals
#f)
(if (include-source-code)
exp
#f)
source-file-name
source-file-position)
(m-body body env))))
(m-error "Malformed lambda expression" exp)))
(define (m-body body env)
(define (loop body env defs)
(if (null? body)
(m-error "Empty body"))
(let ((exp (car body)))
(if (and (pair? exp)
(symbol? (car exp)))
(let ((denotation (syntactic-lookup env (car exp))))
(case (denotation-class denotation)
((special)
(cond ((eq? denotation denotation-of-begin)
(loop (append (cdr exp) (cdr body)) env defs))
((eq? denotation denotation-of-define)
(loop (cdr body) env (cons exp defs)))
(else (finalize-body body env defs))))
((macro)
(m-transcribe exp
env
(lambda (exp env)
(loop (cons exp (cdr body))
env
defs))))
((inline identifier)
(finalize-body body env defs))
(else (m-bug "Bug detected in m-body" body env))))
(finalize-body body env defs))))
(loop body env '()))
(define (finalize-body body env defs)
(if (null? defs)
(let ((body (map (lambda (exp) (m-expand exp env))
body)))
(if (null? (cdr body))
(car body)
(make-begin body)))
(let ()
(define (sort-defs defs)
(let* ((augmented
(map (lambda (def)
(let ((rhs (cadr def)))
(if (not (pair? rhs))
(cons 'trivial def)
(let ((denotation
(syntactic-lookup env (car rhs))))
(cond ((eq? denotation
denotation-of-lambda)
(cons 'procedure def))
((eq? denotation
denotation-of-quote)
(cons 'trivial def))
(else
(cons 'miscellaneous def)))))))
defs))
(sorted (twobit-sort (lambda (x y)
(or (eq? (car x) 'procedure)
(eq? (car y) 'miscellaneous)))
augmented)))
(map cdr sorted)))
(define (desugar-definition def)
(if (> (safe-length def) 2)
(cond ((pair? (cadr def))
(desugar-definition
`(,(car def)
,(car (cadr def))
(,lambda0
,(cdr (cadr def))
,@(cddr def)))))
((and (= (length def) 3)
(symbol? (cadr def)))
(cdr def))
(else (m-error "Malformed definition" def)))
(m-error "Malformed definition" def)))
(define (expand-letrec bindings body)
(make-call
(m-expand
`(,lambda0 ,(map car bindings)
,@(map (lambda (binding)
`(,set!0 ,(car binding)
,(cadr binding)))
bindings)
,@body)
env)
(map (lambda (binding) (make-unspecified)) bindings)))
(expand-letrec (sort-defs (map desugar-definition
(reverse defs)))
body))))
(define (m-if exp env)
(let ((n (safe-length exp)))
(if (or (= n 3) (= n 4))
(make-conditional (m-expand (cadr exp) env)
(m-expand (caddr exp) env)
(if (= n 3)
(make-unspecified)
(m-expand (cadddr exp) env)))
(m-error "Malformed if expression" exp))))
(define (m-set exp env)
(if (= (safe-length exp) 3)
(let ((lhs (m-expand (cadr exp) env))
(rhs (m-expand (caddr exp) env)))
(if (variable? lhs)
(let* ((x (variable.name lhs))
(assignment (make-assignment x rhs))
(denotation (syntactic-lookup env x)))
(if (identifier-denotation? denotation)
(let ((R-entry (identifier-R-entry denotation)))
(R-entry.references-set!
R-entry
(remq lhs (R-entry.references R-entry)))
(R-entry.assignments-set!
R-entry
(cons assignment (R-entry.assignments R-entry)))))
(if (and (lambda? rhs)
(include-procedure-names))
(let ((doc (lambda.doc rhs)))
(doc.name-set! doc x)))
(if pass1-block-compiling?
(set! pass1-block-assignments
(cons x pass1-block-assignments)))
assignment)
(m-error "Malformed assignment" exp)))
(m-error "Malformed assignment" exp)))
(define (m-begin exp env)
(cond ((> (safe-length exp) 1)
(make-begin (map (lambda (exp) (m-expand exp env)) (cdr exp))))
((= (safe-length exp) 1)
(m-warn "Non-standard begin expression" exp)
(make-unspecified))
(else
(m-error "Malformed begin expression" exp))))
(define (m-application exp env)
(if (> (safe-length exp) 0)
(let* ((proc (m-expand (car exp) env))
(args (map (lambda (exp) (m-expand exp env))
(cdr exp)))
(call (make-call proc args)))
(if (variable? proc)
(let* ((procname (variable.name proc))
(entry
(and (not (null? args))
(constant? (car args))
(integrate-usual-procedures)
(every1? constant? args)
(let ((entry (constant-folding-entry procname)))
(and entry
(let ((predicates
(constant-folding-predicates entry)))
(and (= (length args)
(length predicates))
(let loop ((args args)
(predicates predicates))
(cond ((null? args) entry)
(((car predicates)
(constant.value (car args)))
(loop (cdr args)
(cdr predicates)))
(else #f))))))))))
(if entry
(make-constant (apply (constant-folding-folder entry)
(map constant.value args)))
(let ((denotation (syntactic-lookup env procname)))
(if (identifier-denotation? denotation)
(let ((R-entry (identifier-R-entry denotation)))
(R-entry.calls-set!
R-entry
(cons call (R-entry.calls R-entry)))))
call)))
call))
(m-error "Malformed application" exp)))
; The environment argument should always be global here.
(define (m-define-inline exp env)
(cond ((and (= (safe-length exp) 3)
(symbol? (cadr exp)))
(let ((name (cadr exp)))
(m-define-syntax1 name
(caddr exp)
env
(define-syntax-scope))
(let ((denotation
(syntactic-lookup global-syntactic-environment name)))
(syntactic-bind-globally!
name
(make-inline-denotation name
(macro-rules denotation)
(macro-env denotation))))
(make-constant name)))
(else
(m-error "Malformed define-inline" exp))))
; The environment argument should always be global here.
(define (m-define-syntax exp env)
(cond ((and (= (safe-length exp) 3)
(symbol? (cadr exp)))
(m-define-syntax1 (cadr exp)
(caddr exp)
env
(define-syntax-scope)))
((and (= (safe-length exp) 4)
(symbol? (cadr exp))
; FIXME: should use denotations here
(memq (caddr exp) '(letrec letrec* let*)))
(m-define-syntax1 (cadr exp)
(cadddr exp)
env
(caddr exp)))
(else (m-error "Malformed define-syntax" exp))))
(define (m-define-syntax1 keyword spec env scope)
(if (and (pair? spec)
(symbol? (car spec)))
(let* ((transformer-keyword (car spec))
(denotation (syntactic-lookup env transformer-keyword)))
(cond ((eq? denotation denotation-of-syntax-rules)
(case scope
((letrec) (m-define-syntax-letrec keyword spec env))
((letrec*) (m-define-syntax-letrec* keyword spec env))
((let*) (m-define-syntax-let* keyword spec env))
(else (m-bug "Weird scope" scope))))
((same-denotation? denotation denotation-of-transformer)
; FIXME: no error checking here
(syntactic-bind-globally!
keyword
(make-macro-denotation (eval (cadr spec)) env)))
(else
(m-error "Malformed syntax transformer" spec))))
(m-error "Malformed syntax transformer" spec))
(make-constant keyword))
(define (m-define-syntax-letrec keyword spec env)
(syntactic-bind-globally!
keyword
(m-compile-transformer-spec spec env)))
(define (m-define-syntax-letrec* keyword spec env)
(let* ((env (syntactic-extend (syntactic-copy env)
(list keyword)
'((fake denotation))))
(transformer (m-compile-transformer-spec spec env)))
(syntactic-assign! env keyword transformer)
(syntactic-bind-globally! keyword transformer)))
(define (m-define-syntax-let* keyword spec env)
(syntactic-bind-globally!
keyword
(m-compile-transformer-spec spec (syntactic-copy env))))
(define (m-let-syntax exp env)
(if (and (> (safe-length exp) 2)
(every1? (lambda (binding)
(and (pair? binding)
(symbol? (car binding))
(pair? (cdr binding))
(null? (cddr binding))))
(cadr exp)))
(m-body (cddr exp)
(syntactic-extend env
(map car (cadr exp))
(map (lambda (spec)
(m-compile-transformer-spec
spec
env))
(map cadr (cadr exp)))))
(m-error "Malformed let-syntax" exp)))
(define (m-letrec-syntax exp env)
(if (and (> (safe-length exp) 2)
(every1? (lambda (binding)
(and (pair? binding)
(symbol? (car binding))
(pair? (cdr binding))
(null? (cddr binding))))
(cadr exp)))
(let ((env (syntactic-extend env
(map car (cadr exp))
(map (lambda (id)
'(fake denotation))
(cadr exp)))))
(for-each (lambda (id spec)
(syntactic-assign!
env
id
(m-compile-transformer-spec spec env)))
(map car (cadr exp))
(map cadr (cadr exp)))
(m-body (cddr exp) env))
(m-error "Malformed let-syntax" exp)))
(define (m-macro exp env)
(m-transcribe exp
env
(lambda (exp env)
(m-expand exp env))))
(define (m-inline exp env)
(if (integrate-usual-procedures)
(m-transcribe-inline exp
env
(lambda (newexp env)
(if (eq? exp newexp)
(m-application exp env)
(m-expand newexp env))))
(m-application exp env)))
(define m-quit ; assigned by macro-expand
(lambda (v) v))
; To do:
; Clean up alist hacking et cetera.
; Declarations.
; Integrable procedures.
; New semantics for body of LET-SYNTAX and LETREC-SYNTAX.
; Copyright 1992 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 5 April 1999.
($$trace "usual")
; The usual macros, adapted from Jonathan's Version 2 implementation.
; DEFINE is handled primitively, since top-level DEFINE has a side
; effect on the global syntactic environment, and internal definitions
; have to be handled specially anyway.
;
; Some extensions are noted, as are some optimizations.
;
; The LETREC* scope rule is used here to protect these macros against
; redefinition of LAMBDA etc. The scope rule is changed to LETREC at
; the end of this file.
(define-syntax-scope 'letrec*)
(for-each (lambda (form)
(macro-expand form))
'(
; Named LET is defined later, after LETREC has been defined.
(define-syntax let
(syntax-rules ()
((let ((?name ?val) ...) ?body ?body1 ...)
((lambda (?name ...) ?body ?body1 ...) ?val ...))))
(define-syntax let*
(syntax-rules ()
((let* () ?body ?body1 ...)
(let () ?body ?body1 ...))
((let* ((?name1 ?val1) (?name ?val) ...) ?body ?body1 ...)
(let ((?name1 ?val1)) (let* ((?name ?val) ...) ?body ?body1 ...)))))
; Internal definitions have to be handled specially anyway,
; so we might as well rely on them here.
(define-syntax letrec
(syntax-rules (lambda quote)
((letrec ((?name ?val) ...) ?body ?body2 ...)
((lambda ()
(define ?name ?val) ...
?body ?body2 ...)))))
; This definition of named LET extends the prior definition of LET.
; The first rule is non-circular, thanks to the LET* scope that is
; specified for this use of DEFINE-SYNTAX.
(define-syntax let let*
(syntax-rules ()
((let (?bindings ...) . ?body)
(let (?bindings ...) . ?body))
((let ?tag ((?name ?val) ...) ?body ?body1 ...)
(let ((?name ?val) ...)
(letrec ((?tag (lambda (?name ...) ?body ?body1 ...)))
(?tag ?name ...))))))
(define-syntax and
(syntax-rules ()
((and) #t)
((and ?e) ?e)
((and ?e1 ?e2 ?e3 ...)
(if ?e1 (and ?e2 ?e3 ...) #f))))
(define-syntax or
(syntax-rules ()
((or) #f)
((or ?e) ?e)
((or ?e1 ?e2 ?e3 ...)
(let ((temp ?e1))
(if temp temp (or ?e2 ?e3 ...))))))
(define-syntax cond
(syntax-rules (else =>)
((cond (else ?result ?result2 ...))
(begin ?result ?result2 ...))
((cond (?test => ?result))
(let ((temp ?test))
(if temp (?result temp))))
((cond (?test)) ?test)
((cond (?test ?result ?result2 ...))
(if ?test (begin ?result ?result2 ...)))
((cond (?test => ?result) ?clause ?clause2 ...)
(let ((temp ?test))
(if temp (?result temp) (cond ?clause ?clause2 ...))))
((cond (?test) ?clause ?clause2 ...)
(or ?test (cond ?clause ?clause2 ...)))
((cond (?test ?result ?result2 ...)
?clause ?clause2 ...)
(if ?test
(begin ?result ?result2 ...)
(cond ?clause ?clause2 ...)))))
; The R4RS says a <step> may be omitted.
; That's a good excuse for a macro-defining macro that uses LETREC-SYNTAX
; and the ... escape.
(define-syntax do
(syntax-rules ()
((do (?bindings0 ...) (?test) ?body0 ...)
(do (?bindings0 ...) (?test (if #f #f)) ?body0 ...))
((do (?bindings0 ...) ?clause0 ?body0 ...)
(letrec-syntax
((do-aux
(... (syntax-rules ()
((do-aux () ((?name ?init ?step) ...) ?clause ?body ...)
(letrec ((loop (lambda (?name ...)
(cond ?clause
(else
(begin #t ?body ...)
(loop ?step ...))))))
(loop ?init ...)))
((do-aux ((?name ?init ?step) ?todo ...)
(?bindings ...)
?clause
?body ...)
(do-aux (?todo ...)
(?bindings ... (?name ?init ?step))
?clause
?body ...))
((do-aux ((?name ?init) ?todo ...)
(?bindings ...)
?clause
?body ...)
(do-aux (?todo ...)
(?bindings ... (?name ?init ?name))
?clause
?body ...))))))
(do-aux (?bindings0 ...) () ?clause0 ?body0 ...)))))
(define-syntax delay
(syntax-rules ()
((delay ?e) (.make-promise (lambda () ?e)))))
; Another use of LETREC-SYNTAX and the escape extension.
(define-syntax case
(syntax-rules (else)
((case ?e1 (else ?body ?body2 ...))
(begin ?e1 ?body ?body2 ...))
((case ?e1 (?z ?body ?body2 ...))
(if (memv ?e1 '?z) (begin ?body ?body2 ...)))
((case ?e1 ?clause1 ?clause2 ?clause3 ...)
(letrec-syntax
((case-aux
(... (syntax-rules (else)
((case-aux ?temp (else ?body ?body2 ...))
(begin ?body ?body2 ...))
((case-aux ?temp ((?z ...) ?body ?body2 ...))
(if (memv ?temp '(?z ...)) (begin ?body ?body2 ...)))
((case-aux ?temp ((?z ...) ?body ?body2 ...) ?c1 ?c2 ...)
(if (memv ?temp '(?z ...))
(begin ?body ?body2 ...)
(case-aux ?temp ?c1 ?c2 ...)))
; a popular extension
((case-aux ?temp (?z ?body ...) ?c1 ...)
(case-aux ?temp ((?z) ?body ...) ?c1 ...))))))
(let ((temp ?e1))
(case-aux temp ?clause1 ?clause2 ?clause3 ...))))))
; A complete implementation of quasiquote, obtained by translating
; Jonathan Rees's implementation that was posted to RRRS-AUTHORS
; on 22 December 1986.
; Unfortunately, the use of LETREC scope means that it is vulnerable
; to top-level redefinitions of QUOTE etc. That could be fixed, but
; it has hair enough already.
(begin
(define-syntax .finalize-quasiquote letrec
(syntax-rules (quote unquote unquote-splicing)
((.finalize-quasiquote quote ?arg ?return)
(.interpret-continuation ?return (quote ?arg)))
((.finalize-quasiquote unquote ?arg ?return)
(.interpret-continuation ?return ?arg))
((.finalize-quasiquote unquote-splicing ?arg ?return)
(syntax-error ",@ in illegal context" ?arg))
((.finalize-quasiquote ?mode ?arg ?return)
(.interpret-continuation ?return (?mode . ?arg)))))
; The first two "arguments" to .descend-quasiquote and to
; .descend-quasiquote-pair are always identical.
(define-syntax .descend-quasiquote letrec
(syntax-rules (quasiquote unquote unquote-splicing)
((.descend-quasiquote `?y ?x ?level ?return)
(.descend-quasiquote-pair ?x ?x (?level) ?return))
((.descend-quasiquote ,?y ?x () ?return)
(.interpret-continuation ?return unquote ?y))
((.descend-quasiquote ,?y ?x (?level) ?return)
(.descend-quasiquote-pair ?x ?x ?level ?return))
((.descend-quasiquote ,@?y ?x () ?return)
(.interpret-continuation ?return unquote-splicing ?y))
((.descend-quasiquote ,@?y ?x (?level) ?return)
(.descend-quasiquote-pair ?x ?x ?level ?return))
((.descend-quasiquote (?y . ?z) ?x ?level ?return)
(.descend-quasiquote-pair ?x ?x ?level ?return))
((.descend-quasiquote #(?y ...) ?x ?level ?return)
(.descend-quasiquote-vector ?x ?x ?level ?return))
((.descend-quasiquote ?y ?x ?level ?return)
(.interpret-continuation ?return quote ?x))))
(define-syntax .descend-quasiquote-pair letrec
(syntax-rules (quote unquote unquote-splicing)
((.descend-quasiquote-pair (?carx . ?cdrx) ?x ?level ?return)
(.descend-quasiquote ?carx ?carx ?level (1 ?cdrx ?x ?level ?return)))))
(define-syntax .descend-quasiquote-vector letrec
(syntax-rules (quote)
((.descend-quasiquote-vector #(?y ...) ?x ?level ?return)
(.descend-quasiquote (?y ...) (?y ...) ?level (6 ?x ?return)))))
; Representations for continuations used here.
; Continuation types 0, 1, 2, and 6 take a mode and an expression.
; Continuation types -1, 3, 4, 5, and 7 take just an expression.
;
; (-1)
; means no continuation
; (0)
; means to call .finalize-quasiquote with no further continuation
; (1 ?cdrx ?x ?level ?return)
; means a return from the call to .descend-quasiquote from
; .descend-quasiquote-pair
; (2 ?car-mode ?car-arg ?x ?return)
; means a return from the second call to .descend-quasiquote in
; in Jonathan's code for .descend-quasiquote-pair
; (3 ?car-arg ?return)
; means take the result and return an append of ?car-arg with it
; (4 ?cdr-mode ?cdr-arg ?return)
; means take the result and call .finalize-quasiquote on ?cdr-mode
; and ?cdr-arg with a continuation of type 5
; (5 ?car-result ?return)
; means take the result and return a cons of ?car-result onto it
; (6 ?x ?return)
; means a return from the call to .descend-quasiquote from
; .descend-quasiquote-vector
; (7 ?return)
; means take the result and return a call of list->vector on it
(define-syntax .interpret-continuation letrec
(syntax-rules (quote unquote unquote-splicing)
((.interpret-continuation (-1) ?e) ?e)
((.interpret-continuation (0) ?mode ?arg)
(.finalize-quasiquote ?mode ?arg (-1)))
((.interpret-continuation (1 ?cdrx ?x ?level ?return) ?car-mode ?car-arg)
(.descend-quasiquote ?cdrx
?cdrx
?level
(2 ?car-mode ?car-arg ?x ?return)))
((.interpret-continuation (2 quote ?car-arg ?x ?return) quote ?cdr-arg)
(.interpret-continuation ?return quote ?x))
((.interpret-continuation (2 unquote-splicing ?car-arg ?x ?return) quote ())
(.interpret-continuation ?return unquote ?car-arg))
((.interpret-continuation (2 unquote-splicing ?car-arg ?x ?return)
?cdr-mode ?cdr-arg)
(.finalize-quasiquote ?cdr-mode ?cdr-arg (3 ?car-arg ?return)))
((.interpret-continuation (2 ?car-mode ?car-arg ?x ?return)
?cdr-mode ?cdr-arg)
(.finalize-quasiquote ?car-mode ?car-arg (4 ?cdr-mode ?cdr-arg ?return)))
((.interpret-continuation (3 ?car-arg ?return) ?e)
(.interpret-continuation ?return append (?car-arg ?e)))
((.interpret-continuation (4 ?cdr-mode ?cdr-arg ?return) ?e1)
(.finalize-quasiquote ?cdr-mode ?cdr-arg (5 ?e1 ?return)))
((.interpret-continuation (5 ?e1 ?return) ?e2)
(.interpret-continuation ?return .cons (?e1 ?e2)))
((.interpret-continuation (6 ?x ?return) quote ?arg)
(.interpret-continuation ?return quote ?x))
((.interpret-continuation (6 ?x ?return) ?mode ?arg)
(.finalize-quasiquote ?mode ?arg (7 ?return)))
((.interpret-continuation (7 ?return) ?e)
(.interpret-continuation ?return .list->vector (?e)))))
(define-syntax quasiquote letrec
(syntax-rules ()
((quasiquote ?x)
(.descend-quasiquote ?x ?x () (0)))))
)
(define-syntax let*-syntax
(syntax-rules ()
((let*-syntax () ?body)
(let-syntax () ?body))
((let*-syntax ((?name1 ?val1) (?name ?val) ...) ?body)
(let-syntax ((?name1 ?val1)) (let*-syntax ((?name ?val) ...) ?body)))))
))
(define-syntax-scope 'letrec)
(define standard-syntactic-environment
(syntactic-copy global-syntactic-environment))
(define (make-standard-syntactic-environment)
(syntactic-copy standard-syntactic-environment))
; Copyright 1998 William Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 25 April 1999
;
; Given an expression in the subset of Scheme used as an intermediate language
; by Twobit, returns a newly allocated copy of the expression in which the
; local variables have been renamed and the referencing information has been
; recomputed.
(define (copy-exp exp)
(define special-names (cons name:IGNORED argument-registers))
(define original-names (make-hashtable symbol-hash assq))
(define renaming-counter 0)
(define (rename-vars vars)
(let ((rename (make-rename-procedure)))
(map (lambda (var)
(cond ((memq var special-names)
var)
((hashtable-get original-names var)
(rename var))
(else
(hashtable-put! original-names var #t)
var)))
vars)))
(define (rename-formals formals newnames)
(cond ((null? formals) '())
((symbol? formals) (car newnames))
((memq (car formals) special-names)
(cons (car formals)
(rename-formals (cdr formals)
(cdr newnames))))
(else (cons (car newnames)
(rename-formals (cdr formals)
(cdr newnames))))))
; Environments that map symbols to arbitrary information.
; This data type is mutable, and uses the shallow binding technique.
(define (make-env) (make-hashtable symbol-hash assq))
(define (env-bind! env sym info)
(let ((stack (hashtable-get env sym)))
(hashtable-put! env sym (cons info stack))))
(define (env-unbind! env sym)
(let ((stack (hashtable-get env sym)))
(hashtable-put! env sym (cdr stack))))
(define (env-lookup env sym default)
(let ((stack (hashtable-get env sym)))
(if stack
(car stack)
default)))
(define (env-bind-multiple! env symbols infos)
(for-each (lambda (sym info) (env-bind! env sym info))
symbols
infos))
(define (env-unbind-multiple! env symbols)
(for-each (lambda (sym) (env-unbind! env sym))
symbols))
;
(define (lexical-lookup R-table name)
(assq name R-table))
(define (copy exp env notepad R-table)
(cond ((constant? exp) exp)
((lambda? exp)
(let* ((bvl (make-null-terminated (lambda.args exp)))
(newnames (rename-vars bvl))
(procnames (map def.lhs (lambda.defs exp)))
(newprocnames (rename-vars procnames))
(refinfo (map (lambda (var)
(make-R-entry var '() '() '()))
(append newnames newprocnames)))
(newexp
(make-lambda
(rename-formals (lambda.args exp) newnames)
'()
refinfo
'()
'()
(lambda.decls exp)
(lambda.doc exp)
(lambda.body exp))))
(env-bind-multiple! env procnames newprocnames)
(env-bind-multiple! env bvl newnames)
(for-each (lambda (entry)
(env-bind! R-table (R-entry.name entry) entry))
refinfo)
(notepad-lambda-add! notepad newexp)
(let ((newnotepad (make-notepad notepad)))
(for-each (lambda (name rhs)
(lambda.defs-set!
newexp
(cons (make-definition
name
(copy rhs env newnotepad R-table))
(lambda.defs newexp))))
(reverse newprocnames)
(map def.rhs
(reverse (lambda.defs exp))))
(lambda.body-set!
newexp
(copy (lambda.body exp) env newnotepad R-table))
(lambda.F-set! newexp (notepad-free-variables newnotepad))
(lambda.G-set! newexp (notepad-captured-variables newnotepad)))
(env-unbind-multiple! env procnames)
(env-unbind-multiple! env bvl)
(for-each (lambda (entry)
(env-unbind! R-table (R-entry.name entry)))
refinfo)
newexp))
((assignment? exp)
(let* ((oldname (assignment.lhs exp))
(name (env-lookup env oldname oldname))
(varinfo (env-lookup R-table name #f))
(newexp
(make-assignment name
(copy (assignment.rhs exp) env notepad R-table))))
(notepad-var-add! notepad name)
(if varinfo
(R-entry.assignments-set!
varinfo
(cons newexp (R-entry.assignments varinfo))))
newexp))
((conditional? exp)
(make-conditional (copy (if.test exp) env notepad R-table)
(copy (if.then exp) env notepad R-table)
(copy (if.else exp) env notepad R-table)))
((begin? exp)
(make-begin (map (lambda (exp) (copy exp env notepad R-table))
(begin.exprs exp))))
((variable? exp)
(let* ((oldname (variable.name exp))
(name (env-lookup env oldname oldname))
(varinfo (env-lookup R-table name #f))
(newexp (make-variable name)))
(notepad-var-add! notepad name)
(if varinfo
(R-entry.references-set!
varinfo
(cons newexp (R-entry.references varinfo))))
newexp))
((call? exp)
(let ((newexp (make-call (copy (call.proc exp) env notepad R-table)
(map (lambda (exp)
(copy exp env notepad R-table))
(call.args exp)))))
(if (variable? (call.proc newexp))
(let ((varinfo
(env-lookup R-table
(variable.name
(call.proc newexp))
#f)))
(if varinfo
(R-entry.calls-set!
varinfo
(cons newexp (R-entry.calls varinfo))))))
(if (lambda? (call.proc newexp))
(notepad-nonescaping-add! notepad (call.proc newexp)))
newexp))
(else ???)))
(copy exp (make-env) (make-notepad #f) (make-env)))
; For debugging.
; Given an expression, traverses the expression to confirm
; that the referencing invariants are correct.
(define (check-referencing-invariants exp . flags)
(let ((check-free-variables? (memq 'free flags))
(check-referencing? (memq 'reference flags))
(first-violation? #t))
; env is the list of enclosing lambda expressions,
; beginning with the innermost.
(define (check exp env)
(cond ((constant? exp) (return exp #t))
((lambda? exp)
(let ((env (cons exp env)))
(return exp
(and (every? (lambda (exp)
(check exp env))
(map def.rhs (lambda.defs exp)))
(check (lambda.body exp) env)
(if (and check-free-variables?
(not (null? env)))
(subset? (difference
(lambda.F exp)
(make-null-terminated
(lambda.args exp)))
(lambda.F (car env)))
#t)
(if check-referencing?
(let ((env (cons exp env))
(R (lambda.R exp)))
(every? (lambda (formal)
(or (ignored? formal)
(R-entry R formal)))
(make-null-terminated
(lambda.args exp))))
#t)))))
((variable? exp)
(return exp
(and (if (and check-free-variables?
(not (null? env)))
(memq (variable.name exp)
(lambda.F (car env)))
#t)
(if check-referencing?
(let ((Rinfo (lookup env (variable.name exp))))
(if Rinfo
(memq exp (R-entry.references Rinfo))
#t))
#t))))
((assignment? exp)
(return exp
(and (check (assignment.rhs exp) env)
(if (and check-free-variables?
(not (null? env)))
(memq (assignment.lhs exp)
(lambda.F (car env)))
#t)
(if check-referencing?
(let ((Rinfo (lookup env (assignment.lhs exp))))
(if Rinfo
(memq exp (R-entry.assignments Rinfo))
#t))
#t))))
((conditional? exp)
(return exp
(and (check (if.test exp) env)
(check (if.then exp) env)
(check (if.else exp) env))))
((begin? exp)
(return exp
(every? (lambda (exp) (check exp env))
(begin.exprs exp))))
((call? exp)
(return exp
(and (check (call.proc exp) env)
(every? (lambda (exp) (check exp env))
(call.args exp))
(if (and check-referencing?
(variable? (call.proc exp)))
(let ((Rinfo (lookup env
(variable.name
(call.proc exp)))))
(if Rinfo
(memq exp (R-entry.calls Rinfo))
#t))
#t))))
(else ???)))
(define (return exp flag)
(cond (flag
#t)
(first-violation?
(set! first-violation? #f)
(display "Violation of referencing invariants")
(newline)
(pretty-print (make-readable exp))
#f)
(else (pretty-print (make-readable exp))
#f)))
(define (lookup env I)
(if (null? env)
#f
(let ((Rinfo (R-entry (lambda.R (car env)) I)))
(or Rinfo
(lookup (cdr env) I)))))
(if (null? flags)
(begin (set! check-free-variables? #t)
(set! check-referencing? #t)))
(check exp '())))
; Calculating the free variable information for an expression
; as output by pass 2. This should be faster than computing both
; the free variables and the referencing information.
(define (compute-free-variables! exp)
(define empty-set (make-set '()))
(define (singleton x) (list x))
(define (union2 x y) (union x y))
(define (union3 x y z) (union x y z))
(define (set->list set) set)
(define (free exp)
(cond ((constant? exp) empty-set)
((lambda? exp)
(let* ((defs (lambda.defs exp))
(formals (make-set
(make-null-terminated (lambda.args exp))))
(defined (make-set (map def.lhs defs)))
(Fdefs
(apply-union
(map (lambda (def)
(free (def.rhs def)))
defs)))
(Fbody (free (lambda.body exp)))
(F (union2 Fdefs Fbody)))
(lambda.F-set! exp (set->list F))
(lambda.G-set! exp (set->list F))
(difference F (union2 formals defined))))
((assignment? exp)
(union2 (make-set (list (assignment.lhs exp)))
(free (assignment.rhs exp))))
((conditional? exp)
(union3 (free (if.test exp))
(free (if.then exp))
(free (if.else exp))))
((begin? exp)
(apply-union
(map (lambda (exp) (free exp))
(begin.exprs exp))))
((variable? exp)
(singleton (variable.name exp)))
((call? exp)
(union2 (free (call.proc exp))
(apply-union
(map (lambda (exp) (free exp))
(call.args exp)))))
(else ???)))
(free exp))
; As above, but representing sets as hashtrees.
; This is commented out because it is much slower than the implementation
; above. Because the set of free variables is represented as a list
; within a lambda expression, this implementation must convert the
; representation for every lambda expression, which is quite expensive
; for A-normal form.
(begin
'
(define (compute-free-variables! exp)
(define empty-set (make-hashtree symbol-hash assq))
(define (singleton x)
(hashtree-put empty-set x #t))
(define (make-set values)
(if (null? values)
empty-set
(hashtree-put (make-set (cdr values))
(car values)
#t)))
(define (union2 x y)
(hashtree-for-each (lambda (key val)
(set! x (hashtree-put x key #t)))
y)
x)
(define (union3 x y z)
(union2 (union2 x y) z))
(define (apply-union sets)
(cond ((null? sets)
(make-set '()))
((null? (cdr sets))
(car sets))
(else
(union2 (car sets)
(apply-union (cdr sets))))))
(define (difference x y)
(hashtree-for-each (lambda (key val)
(set! x (hashtree-remove x key)))
y)
x)
(define (set->list set)
(hashtree-map (lambda (sym val) sym) set))
(define (free exp)
(cond ((constant? exp) empty-set)
((lambda? exp)
(let* ((defs (lambda.defs exp))
(formals (make-set
(make-null-terminated (lambda.args exp))))
(defined (make-set (map def.lhs defs)))
(Fdefs
(apply-union
(map (lambda (def)
(free (def.rhs def)))
defs)))
(Fbody (free (lambda.body exp)))
(F (union2 Fdefs Fbody)))
(lambda.F-set! exp (set->list F))
(lambda.G-set! exp (set->list F))
(difference F (union2 formals defined))))
((assignment? exp)
(union2 (make-set (list (assignment.lhs exp)))
(free (assignment.rhs exp))))
((conditional? exp)
(union3 (free (if.test exp))
(free (if.then exp))
(free (if.else exp))))
((begin? exp)
(apply-union
(map (lambda (exp) (free exp))
(begin.exprs exp))))
((variable? exp)
(singleton (variable.name exp)))
((call? exp)
(union2 (free (call.proc exp))
(apply-union
(map (lambda (exp) (free exp))
(call.args exp)))))
(else ???)))
(hashtree-map (lambda (sym val) sym)
(free exp)))
#t); Copyright 1991 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 24 April 1999
;
; First pass of the Twobit compiler:
; macro expansion, syntax checking, alpha conversion,
; preliminary annotation.
;
; The input to this pass is a Scheme definition or expression.
; The output is an expression in the subset of Scheme described
; by the following grammar, where the output satisfies certain
; additional invariants described below.
;
; "X ..." means zero or more occurrences of X.
;
; L --> (lambda (I_1 ...)
; (begin D ...)
; (quote (R F G <decls> <doc>)
; E)
; | (lambda (I_1 ... . I_rest)
; (begin D ...)
; (quote (R F <decls> <doc>))
; E)
; D --> (define I L)
; E --> (quote K) ; constants
; | (begin I) ; variable references
; | L ; lambda expressions
; | (E0 E1 ...) ; calls
; | (set! I E) ; assignments
; | (if E0 E1 E2) ; conditionals
; | (begin E0 E1 E2 ...) ; sequential expressions
; I --> <identifier>
;
; R --> ((I <references> <assignments> <calls>) ...)
; F --> (I ...)
; G --> (I ...)
;
; Invariants that hold for the output:
; * There are no internal definitions.
; * No identifier containing an upper case letter is bound anywhere.
; (Change the "name:..." variables if upper case is preferred.)
; * No identifier is bound in more than one place.
; * Each R contains one entry for every identifier bound in the
; formal argument list and the internal definition list that
; precede it. Each entry contains a list of pointers to all
; references to the identifier, a list of pointers to all
; assignments to the identifier, and a list of pointers to all
; calls to the identifier.
; * Except for constants, the expression does not share structure
; with the original input or itself, except that the references
; and assignments in R are guaranteed to share structure with
; the expression. Thus the expression may be side effected, and
; side effects to references or assignments obtained through R
; are guaranteed to change the references or assignments pointed
; to by R.
; * F and G are garbage.
($$trace "pass1")
(define source-file-name #f)
(define source-file-position #f)
(define pass1-block-compiling? #f)
(define pass1-block-assignments '())
(define pass1-block-inlines '())
(define (pass1 def-or-exp . rest)
(set! source-file-name #f)
(set! source-file-position #f)
(set! pass1-block-compiling? #f)
(set! pass1-block-assignments '())
(set! pass1-block-inlines '())
(if (not (null? rest))
(begin (set! source-file-name (car rest))
(if (not (null? (cdr rest)))
(set! source-file-position (cadr rest)))))
(set! renaming-counter 0)
(macro-expand def-or-exp))
; Compiles a whole sequence of top-level forms on the assumption
; that no variable that is defined by a form in the sequence is
; ever defined or assigned outside of the sequence.
;
; This is a crock in three parts:
;
; 1. Macro-expand each form and record assignments.
; 2. Find the top-level variables that are defined but not
; assigned, give them local names, generate a DEFINE-INLINE
; for each of the top-level procedures, and macro-expand
; each form again.
; 3. Wrap the whole mess in an appropriate LET and recompute
; the referencing information by copying it.
;
; Note that macros get expanded twice, and that all DEFINE-SYNTAX
; macros are considered local to the forms.
; FIXME: Need to turn off warning messages.
(define (pass1-block forms . rest)
(define (part1)
(set! pass1-block-compiling? #t)
(set! pass1-block-assignments '())
(set! pass1-block-inlines '())
(set! renaming-counter 0)
(let ((env0 (syntactic-copy global-syntactic-environment))
(bmode (benchmark-mode))
(wmode (issue-warnings))
(defined '()))
(define (make-toplevel-definition id exp)
(cond ((memq id defined)
(set! pass1-block-assignments
(cons id pass1-block-assignments)))
((or (constant? exp)
(and (lambda? exp)
(list? (lambda.args exp))))
(set! defined (cons id defined))))
(make-begin
(list (make-assignment id exp)
(make-constant id))))
(benchmark-mode #f)
(issue-warnings #f)
(for-each (lambda (form)
(desugar-definitions form
global-syntactic-environment
make-toplevel-definition))
forms)
(set! global-syntactic-environment env0)
(benchmark-mode bmode)
(issue-warnings wmode)
(part2 (filter (lambda (id)
(not (memq id pass1-block-assignments)))
(reverse defined)))))
(define (part2 defined)
(set! pass1-block-compiling? #f)
(set! pass1-block-assignments '())
(set! pass1-block-inlines '())
(set! renaming-counter 0)
(let* ((rename (make-rename-procedure))
(alist (map (lambda (id)
(cons id (rename id)))
defined))
(definitions0 '()) ; for constants
(definitions1 '())) ; for lambda expressions
(define (make-toplevel-definition id exp)
(if (lambda? exp)
(doc.name-set! (lambda.doc exp) id))
(let ((probe (assq id alist)))
(if probe
(let ((id1 (cdr probe)))
(cond ((constant? exp)
(set! definitions0
(cons (make-assignment id exp)
definitions0))
(make-constant id))
((lambda? exp)
(set! definitions1
(cons (make-assignment id1 exp)
definitions1))
(make-assignment
id
(make-lambda (lambda.args exp)
'() ; no definitions
'() ; R
'() ; F
'() ; G
'() ; decls
(lambda.doc exp)
(make-call
(make-variable id1)
(map make-variable
(lambda.args exp))))))
(else
(m-error "Inconsistent macro expansion"
(make-readable exp)))))
(make-assignment id exp))))
(let ((env0 (syntactic-copy global-syntactic-environment))
(bmode (benchmark-mode))
(wmode (issue-warnings)))
(issue-warnings #f)
(for-each (lambda (pair)
(let ((id0 (car pair))
(id1 (cdr pair)))
(syntactic-bind-globally!
id0
(make-inline-denotation
id0
(lambda (exp rename compare)
; Deliberately non-hygienic!
(cons id1 (cdr exp)))
global-syntactic-environment))
(set! pass1-block-inlines
(cons id0 pass1-block-inlines))))
alist)
(benchmark-mode #f)
(issue-warnings wmode)
(let ((forms
(do ((forms forms (cdr forms))
(newforms '()
(cons (desugar-definitions
(car forms)
global-syntactic-environment
make-toplevel-definition)
newforms)))
((null? forms)
(reverse newforms)))))
(benchmark-mode bmode)
(set! global-syntactic-environment env0)
(part3 alist definitions0 definitions1 forms)))))
(define (part3 alist definitions0 definitions1 forms)
(set! pass1-block-compiling? #f)
(set! pass1-block-assignments '())
(set! pass1-block-inlines '())
(let* ((constnames0 (map assignment.lhs definitions0))
(constnames1 (map (lambda (id0)
(cdr (assq id0 alist)))
constnames0))
(procnames1 (map assignment.lhs definitions1)))
(copy-exp
(make-call
(make-lambda
constnames1
'() ; no definitions
'() ; R
'() ; F
'() ; G
'() ; decls
#f ; doc
(make-begin
(list
(make-begin
(cons (make-constant #f)
(reverse
(map (lambda (id)
(make-assignment id (make-variable (cdr (assq id alist)))))
constnames0))))
(make-call
(make-lambda
constnames0
'() ; no definitions
'() ; R
'() ; F
'() ; G
'() ; decls
#f ; doc
(make-call
(make-lambda
(map assignment.lhs definitions1)
'() ; no definitions
'() ; R
'() ; F
'() ; G
'() ; decls
#f ; doc
(make-begin (cons (make-constant #f)
(append definitions1 forms))))
(map (lambda (ignored) (make-unspecified))
definitions1)))
(map make-variable constnames1))
)))
(map assignment.rhs definitions0)))))
(set! source-file-name #f)
(set! source-file-position #f)
(if (not (null? rest))
(begin (set! source-file-name (car rest))
(if (not (null? (cdr rest)))
(set! source-file-position (cadr rest)))))
(part1))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 7 June 1999.
;
; Support for intraprocedural value numbering:
; set of available expressions
; miscellaneous
;
; The set of available expressions is represented as a
; mutable abstract data type Available with these operations:
;
; make-available-table: -> Available
; copy-available-table: Available -> Available
; available-expression: Available x Expr -> (symbol + {#f})
; available-variable: Available x symbol -> Expr
; available-extend!: Available x symbol x Expr x Killer ->
; available-kill!: Available x Killer ->
;
; where Expr is of the form
;
; Expr --> W
; | (W_0 W_1 ...)
;
; W --> (quote K)
; | (begin I)
;
; and Killer is a fixnum, as defined later in this file.
;
; (make-available-table)
; returns an empty table of available expressions.
; (copy-available-table available)
; copies the given table.
; (available-expression available E)
; returns the name of E if it is available in the table, else #f.
; (available-variable available T)
; returns a constant or variable to use in place of T, else #f.
; (available-extend! available T E K)
; adds the binding (T E) to the table, with Killer K.
; If E is a variable and this binding is never killed, then copy
; propagation will replace uses of T by uses of E; otherwise
; commoning will replace uses of E by uses of T, until the
; binding is killed.
; (available-kill! available K)
; removes all bindings whose Killer intersects K.
;
; (available-extend! available T E K) is very fast if the previous
; operation on the table was (available-expression available E).
; Implementation.
;
; Quick and dirty.
; The available expressions are represented as a vector of 2 association
; lists. The first list is used for common subexpression elimination,
; and the second is used for copy and constant propagation.
;
; Each element of the first list is a binding of
; a symbol T to an expression E, with killer K,
; represented by the list (E T K).
;
; Each element of the second list is a binding of
; a symbol T to an expression E, with killer K,
; represented by the list (T E K).
; The expression E will be a constant or variable.
(define (make-available-table)
(vector '() '()))
(define (copy-available-table available)
(vector (vector-ref available 0)
(vector-ref available 1)))
(define (available-expression available E)
(let ((binding (assoc E (vector-ref available 0))))
(if binding
(cadr binding)
#f)))
(define (available-variable available T)
(let ((binding (assq T (vector-ref available 1))))
(if binding
(cadr binding)
#f)))
(define (available-extend! available T E K)
(cond ((constant? E)
(vector-set! available
1
(cons (list T E K)
(vector-ref available 1))))
((and (variable? E)
(eq? K available:killer:none))
(vector-set! available
1
(cons (list T E K)
(vector-ref available 1))))
(else
(vector-set! available
0
(cons (list E T K)
(vector-ref available 0))))))
(define (available-kill! available K)
(vector-set! available
0
(filter (lambda (binding)
(zero?
(logand K
(caddr binding))))
(vector-ref available 0)))
(vector-set! available
1
(filter (lambda (binding)
(zero?
(logand K
(caddr binding))))
(vector-ref available 1))))
(define (available-intersect! available0 available1 available2)
(vector-set! available0
0
(intersection (vector-ref available1 0)
(vector-ref available2 0)))
(vector-set! available0
1
(intersection (vector-ref available1 1)
(vector-ref available2 1))))
; The Killer concrete data type, represented as a fixnum.
;
; The set of side effects that can kill an available expression
; are a subset of
;
; assignments to global variables
; uses of SET-CAR!
; uses of SET-CDR!
; uses of STRING-SET!
; uses of VECTOR-SET!
;
; This list is not complete. If we were trying to perform common
; subexpression elimination on calls to PEEK-CHAR, for example,
; then those calls would be killed by reads.
(define available:killer:globals 2)
(define available:killer:car 4)
(define available:killer:cdr 8)
(define available:killer:string 16) ; also bytevectors etc
(define available:killer:vector 32) ; also structures etc
(define available:killer:cell 64)
(define available:killer:io 128)
(define available:killer:none 0) ; none of the above
(define available:killer:all 1022) ; all of the above
(define available:killer:immortal 0) ; never killed
(define available:killer:dead 1023) ; never available
(define (available:killer-combine k1 k2)
(logior k1 k2))
; Miscellaneous.
; A simple lambda expression has no internal definitions at its head
; and no declarations aside from A-normal form.
(define (simple-lambda? L)
(and (null? (lambda.defs L))
(every? (lambda (decl)
(eq? decl A-normal-form-declaration))
(lambda.decls L))))
; A real call is a call whose procedure expression is
; neither a lambda expression nor a primop.
(define (real-call? E)
(and (call? E)
(let ((proc (call.proc E)))
(and (not (lambda? proc))
(or (not (variable? proc))
(let ((f (variable.name proc)))
(or (not (integrate-usual-procedures))
(not (prim-entry f)))))))))
(define (prim-call E)
(and (call? E)
(let ((proc (call.proc E)))
(and (variable? proc)
(integrate-usual-procedures)
(prim-entry (variable.name proc))))))
(define (no-side-effects? E)
(or (constant? E)
(variable? E)
(lambda? E)
(and (conditional? E)
(no-side-effects? (if.test E))
(no-side-effects? (if.then E))
(no-side-effects? (if.else E)))
(and (call? E)
(let ((proc (call.proc E)))
(and (variable? proc)
(integrate-usual-procedures)
(let ((entry (prim-entry (variable.name proc))))
(and entry
(not (eq? available:killer:dead
(prim-lives-until entry))))))))))
; Given a local variable, the expression within its scope, and
; a list of local variables that are known to be used only once,
; returns #t if the variable is used only once.
;
; The purpose of this routine is to recognize temporaries that
; may once have had two or more uses because of CSE, but now have
; only one use because of further CSE followed by dead code elimination.
(define (temporary-used-once? T E used-once)
(cond ((call? E)
(let ((proc (call.proc E))
(args (call.args E)))
(or (and (lambda? proc)
(not (memq T (lambda.F proc)))
(and (pair? args)
(null? (cdr args))
(temporary-used-once? T (car args) used-once)))
(do ((exprs (cons proc (call.args E))
(cdr exprs))
(n 0
(let ((exp (car exprs)))
(cond ((constant? exp)
n)
((variable? exp)
(if (eq? T (variable.name exp))
(+ n 1)
n))
(else
; Terminate the loop and return #f.
2)))))
((or (null? exprs)
(> n 1))
(= n 1))))))
(else
(memq T used-once))))
; Register bindings.
(define (make-regbinding lhs rhs use)
(list lhs rhs use))
(define (regbinding.lhs x) (car x))
(define (regbinding.rhs x) (cadr x))
(define (regbinding.use x) (caddr x))
; Given a list of register bindings, an expression E and its free variables F,
; returns two values:
; E with the register bindings wrapped around it
; the free variables of the wrapped expression
(define (wrap-with-register-bindings regbindings E F)
(if (null? regbindings)
(values E F)
(let* ((regbinding (car regbindings))
(R (regbinding.lhs regbinding))
(x (regbinding.rhs regbinding)))
(wrap-with-register-bindings
(cdr regbindings)
(make-call (make-lambda (list R)
'()
'()
F
F
(list A-normal-form-declaration)
#f
E)
(list (make-variable x)))
(union (list x)
(difference F (list R)))))))
; Returns two values:
; the subset of regbindings that have x as their right hand side
; the rest of regbindings
(define (register-bindings regbindings x)
(define (loop regbindings to-x others)
(cond ((null? regbindings)
(values to-x others))
((eq? x (regbinding.rhs (car regbindings)))
(loop (cdr regbindings)
(cons (car regbindings) to-x)
others))
(else
(loop (cdr regbindings)
to-x
(cons (car regbindings) others)))))
(loop regbindings '() '()))
; This procedure is called when the compiler can tell that an assertion
; is never true.
(define (declaration-error E)
(if (issue-warnings)
(begin (display "WARNING: Assertion is false: ")
(write (make-readable E #t))
(newline))))
; Representations, which form a subtype hierarchy.
;
; <rep> ::= <fixnum> | (<fixnum> <datum> ...)
;
; (<rep> <datum> ...) is a subtype of <rep>, but the non-fixnum
; representations are otherwise interpreted by arbitrary code.
(define *nreps* 0)
(define *rep-encodings* '())
(define *rep-decodings* '())
(define *rep-subtypes* '())
(define *rep-joins* (make-bytevector 0))
(define *rep-meets* (make-bytevector 0))
(define *rep-joins-special* '#())
(define *rep-meets-special* '#())
(define (representation-error msg . stuff)
(apply error
(if (string? msg)
(string-append "Bug in flow analysis: " msg)
msg)
stuff))
(define (symbol->rep sym)
(let ((probe (assq sym *rep-encodings*)))
(if probe
(cdr probe)
(let ((rep *nreps*))
(set! *nreps* (+ *nreps* 1))
(if (> *nreps* 255)
(representation-error "Too many representation types"))
(set! *rep-encodings*
(cons (cons sym rep)
*rep-encodings*))
(set! *rep-decodings*
(cons (cons rep sym)
*rep-decodings*))
rep))))
(define (rep->symbol rep)
(if (pair? rep)
(cons (rep->symbol (car rep)) (cdr rep))
(let ((probe (assv rep *rep-decodings*)))
(if probe
(cdr probe)
'unknown))))
(define (representation-table table)
(map (lambda (row)
(map (lambda (x)
(if (list? x)
(map symbol->rep x)
x))
row))
table))
; DEFINE-SUBTYPE is how representation types are defined.
(define (define-subtype sym1 sym2)
(let* ((rep2 (symbol->rep sym2))
(rep1 (symbol->rep sym1)))
(set! *rep-subtypes*
(cons (cons rep1 rep2)
*rep-subtypes*))
sym1))
; COMPUTE-TYPE-STRUCTURE! must be called before DEFINE-INTERSECTION.
(define (define-intersection sym1 sym2 sym3)
(let ((rep1 (symbol->rep sym1))
(rep2 (symbol->rep sym2))
(rep3 (symbol->rep sym3)))
(representation-aset! *rep-meets* rep1 rep2 rep3)
(representation-aset! *rep-meets* rep2 rep1 rep3)))
;
(define (representation-aref bv i j)
(bytevector-ref bv (+ (* *nreps* i) j)))
(define (representation-aset! bv i j x)
(bytevector-set! bv (+ (* *nreps* i) j) x))
(define (compute-unions!)
; Always define a bottom element.
(for-each (lambda (sym)
(define-subtype 'bottom sym))
(map car *rep-encodings*))
(let* ((debugging? #f)
(n *nreps*)
(n^2 (* n n))
(matrix (make-bytevector n^2)))
; This code assumes there will always be a top element.
(define (lub rep1 rep2 subtype?)
(do ((i 0 (+ i 1))
(bounds '()
(if (and (subtype? rep1 i)
(subtype? rep2 i))
(cons i bounds)
bounds)))
((= i n)
(car (twobit-sort subtype? bounds)))))
(define (join i j)
(lub i j (lambda (rep1 rep2)
(= 1 (representation-aref matrix rep1 rep2)))))
(define (compute-transitive-closure!)
(let ((changed? #f))
(define (loop)
(do ((i 0 (+ i 1)))
((= i n))
(do ((k 0 (+ k 1)))
((= k n))
(do ((j 0 (+ j 1))
(sum 0
(logior sum
(logand
(representation-aref matrix i j)
(representation-aref matrix j k)))))
((= j n)
(if (> sum 0)
(let ((x (representation-aref matrix i k)))
(if (zero? x)
(begin
(set! changed? #t)
(representation-aset! matrix i k 1)))))))))
(if changed?
(begin (set! changed? #f)
(loop))))
(loop)))
(define (compute-joins!)
(let ((default (lambda (x y)
(error "Compiler bug: special meet or join" x y))))
(set! *rep-joins-special* (make-vector n default))
(set! *rep-meets-special* (make-vector n default)))
(set! *rep-joins* (make-bytevector n^2))
(set! *rep-meets* (make-bytevector n^2))
(do ((i 0 (+ i 1)))
((= i n))
(do ((j 0 (+ j 1)))
((= j n))
(representation-aset! *rep-joins*
i
j
(join i j)))))
(do ((i 0 (+ i 1)))
((= i n))
(do ((j 0 (+ j 1)))
((= j n))
(representation-aset! matrix i j 0))
(representation-aset! matrix i i 1))
(for-each (lambda (subtype)
(let ((rep1 (car subtype))
(rep2 (cdr subtype)))
(representation-aset! matrix rep1 rep2 1)))
*rep-subtypes*)
(compute-transitive-closure!)
(if debugging?
(do ((i 0 (+ i 1)))
((= i n))
(do ((j 0 (+ j 1)))
((= j n))
(write-char #\space)
(write (representation-aref matrix i j)))
(newline)))
(compute-joins!)
(set! *rep-subtypes* '())))
; Intersections are not dual to unions because a conservative analysis
; must always err on the side of the larger subtype.
; COMPUTE-UNIONS! must be called before COMPUTE-INTERSECTIONS!.
(define (compute-intersections!)
(let ((n *nreps*))
(define (meet i j)
(let ((k (representation-union i j)))
(if (= i k)
j
i)))
(do ((i 0 (+ i 1)))
((= i n))
(do ((j 0 (+ j 1)))
((= j n))
(representation-aset! *rep-meets*
i
j
(meet i j))))))
(define (compute-type-structure!)
(compute-unions!)
(compute-intersections!))
(define (representation-subtype? rep1 rep2)
(equal? rep2 (representation-union rep1 rep2)))
(define (representation-union rep1 rep2)
(if (fixnum? rep1)
(if (fixnum? rep2)
(representation-aref *rep-joins* rep1 rep2)
(representation-union rep1 (car rep2)))
(if (fixnum? rep2)
(representation-union (car rep1) rep2)
(let ((r1 (car rep1))
(r2 (car rep2)))
(if (= r1 r2)
((vector-ref *rep-joins-special* r1) rep1 rep2)
(representation-union r1 r2))))))
(define (representation-intersection rep1 rep2)
(if (fixnum? rep1)
(if (fixnum? rep2)
(representation-aref *rep-meets* rep1 rep2)
(representation-intersection rep1 (car rep2)))
(if (fixnum? rep2)
(representation-intersection (car rep1) rep2)
(let ((r1 (car rep1))
(r2 (car rep2)))
(if (= r1 r2)
((vector-ref *rep-meets-special* r1) rep1 rep2)
(representation-intersection r1 r2))))))
; For debugging.
(define (display-unions-and-intersections)
(let* ((column-width 10)
(columns/row (quotient 80 column-width)))
(define (display-symbol sym)
(let* ((s (symbol->string sym))
(n (string-length s)))
(if (< n column-width)
(begin (display s)
(display (make-string (- column-width n) #\space)))
(begin (display (substring s 0 (- column-width 1)))
(write-char #\space)))))
; Display columns i to n.
(define (display-matrix f i n)
(display (make-string column-width #\space))
(do ((i i (+ i 1)))
((= i n))
(display-symbol (rep->symbol i)))
(newline)
(newline)
(do ((k 0 (+ k 1)))
((= k *nreps*))
(display-symbol (rep->symbol k))
(do ((i i (+ i 1)))
((= i n))
(display-symbol (rep->symbol (f k i))))
(newline))
(newline)
(newline))
(display "Unions:")
(newline)
(newline)
(do ((i 0 (+ i columns/row)))
((>= i *nreps*))
(display-matrix representation-union
i
(min *nreps* (+ i columns/row))))
(display "Intersections:")
(newline)
(newline)
(do ((i 0 (+ i columns/row)))
((>= i *nreps*))
(display-matrix representation-intersection
i
(min *nreps* (+ i columns/row))))))
; Operations that can be specialized.
;
; Format: (<name> (<arg-rep> ...) <specific-name>)
(define (rep-specific? f rs)
(rep-match f rs rep-specific caddr))
; Operations whose result has some specific representation.
;
; Format: (<name> (<arg-rep> ...) (<result-rep>))
(define (rep-result? f rs)
(rep-match f rs rep-result caaddr))
; Unary predicates that give information about representation.
;
; Format: (<name> <rep-if-true> <rep-if-false>)
(define (rep-if-true f rs)
(rep-match f rs rep-informing caddr))
(define (rep-if-false f rs)
(rep-match f rs rep-informing cadddr))
; Given the name of an integrable primitive,
; the representations of its arguments,
; a representation table, and a selector function
; finds the most type-specific row of the table that matches both
; the name of the primitive and the representations of its arguments,
; and returns the result of applying the selector to that row.
; If no row matches, then REP-MATCH returns #f.
;
; FIXME: This should be more efficient, and should prefer the most
; specific matches.
(define (rep-match f rs table selector)
(let ((n (length rs)))
(let loop ((entries table))
(cond ((null? entries)
#f)
((eq? f (car (car entries)))
(let ((rs0 (cadr (car entries))))
(if (and (= n (length rs0))
(every? (lambda (r1+r2)
(let ((r1 (car r1+r2))
(r2 (cdr r1+r2)))
(representation-subtype? r1 r2)))
(map cons rs rs0)))
(selector (car entries))
(loop (cdr entries)))))
(else
(loop (cdr entries)))))))
; Abstract interpretation with respect to types and constraints.
; Returns a representation type.
(define (aeval E types constraints)
(cond ((call? E)
(let ((proc (call.proc E)))
(if (variable? proc)
(let* ((op (variable.name proc))
(argtypes (map (lambda (E)
(aeval E types constraints))
(call.args E)))
(type (rep-result? op argtypes)))
(if type
type
rep:object))
rep:object)))
((variable? E)
(representation-typeof (variable.name E) types constraints))
((constant? E)
(representation-of-value (constant.value E)))
(else
rep:object)))
; If x has representation type t0 in the hash table,
; and some further constraints
;
; x = (op y1 ... yn)
; x : t1
; ...
; x : tk
;
; then
;
; typeof (x) = op (typeof (y1), ..., typeof (yn))
; & t0 & t1 & ... & tk
;
; where & means intersection and op is the abstraction of op.
;
; Also if T : true and T = E then E may give information about
; the types of other variables. Similarly for T : false.
(define (representation-typeof name types constraints)
(let ((t0 (hashtable-fetch types name rep:object))
(cs (hashtable-fetch (constraints.table constraints) name '())))
(define (loop type cs)
(if (null? cs)
type
(let* ((c (car cs))
(cs (cdr cs))
(E (constraint.rhs c)))
(cond ((constant? E)
(loop (representation-intersection type
(constant.value E))
cs))
((call? E)
(loop (representation-intersection
type (aeval E types constraints))
cs))
(else
(loop type cs))))))
(loop t0 cs)))
; Constraints.
;
; The constraints used by this analysis consist of type constraints
; together with the available expressions used for commoning.
;
; (T E K) T = E until killed by an effect in K
; (T '<rep> K) T : <rep> until killed by an effect in K
(define (make-constraint T E K)
(list T E K))
(define (constraint.lhs c)
(car c))
(define (constraint.rhs c)
(cadr c))
(define (constraint.killer c)
(caddr c))
(define (make-type-constraint T type K)
(make-constraint T
(make-constant type)
K))
; If the new constraint is of the form T = E until killed by K,
; then there shouldn't be any prior constraints.
;
; Otherwise the new constraint is of the form T : t until killed by K.
; Suppose the prior constraints are
; T = E until killed by K
; T : t1 until killed by K1
; ...
; T : tn until killed by Kn
;
; If there exists i such that ti is a subtype of t and Ki a subset of K,
; then the new constraint adds no new information and should be ignored.
; Otherwise compute t' = t1 & ... & tn and K' = K1 | ... | Kn, where
; & indicates intersection and | indicates union.
; If K = K' then add the new constraint T : t' until killed by K;
; otherwise add two new constraints:
; T : t' until killed by K'
; T : t until killed by K
(define (constraints-add! types constraints new)
(let* ((debugging? #f)
(T (constraint.lhs new))
(E (constraint.rhs new))
(K (constraint.killer new))
(cs (constraints-for-variable constraints T)))
(define (loop type K cs newcs)
(if (null? cs)
(cons (make-type-constraint T type K) newcs)
(let* ((c2 (car cs))
(cs (cdr cs))
(E2 (constraint.rhs c2))
(K2 (constraint.killer c2)))
(if (constant? E2)
(let* ((type2 (constant.value E2))
(type3 (representation-intersection type type2)))
(cond ((eq? type2 type3)
(if (= K2 (logand K K2))
(append newcs cs)
(loop (representation-intersection type type2)
(available:killer-combine K K2)
cs
(cons c2 newcs))))
((representation-subtype? type type3)
(if (= K (logand K K2))
(loop type K cs newcs)
(loop type K cs (cons c2 newcs))))
(else
(loop type3
(available:killer-combine K K2)
cs
(cons c2 newcs)))))
(let* ((op (variable.name (call.proc E2)))
(args (call.args E2))
(argtypes (map (lambda (exp)
(aeval exp types constraints))
args)))
(cond ((representation-subtype? type rep:true)
(let ((reps (rep-if-true op argtypes)))
(if reps
(record-new-reps! args argtypes reps K2))))
((representation-subtype? type rep:false)
(let ((reps (rep-if-false op argtypes)))
(if reps
(record-new-reps! args argtypes reps K2)))))
(loop type K cs (cons c2 newcs)))))))
(define (record-new-reps! args argtypes reps K2)
(if debugging?
(begin (write (list (map make-readable args)
(map rep->symbol argtypes)
(map rep->symbol reps)))
(newline)))
(for-each (lambda (arg type0 type1)
(if (not (representation-subtype? type0 type1))
(if (variable? arg)
(let ((name (variable.name arg)))
; FIXME: In this context, a variable
; should always be local so the hashtable
; operation isn't necessary.
(if (hashtable-get types name)
(constraints-add!
types
constraints
(make-type-constraint
name
type1
(available:killer-combine K K2)))
(cerror
"Compiler bug: unexpected global: "
name))))))
args argtypes reps))
(if (not (zero? K))
(constraints-add-killedby! constraints T K))
(let* ((table (constraints.table constraints))
(cs (hashtable-fetch table T '())))
(cond ((constant? E)
; It's a type constraint.
(let ((type (constant.value E)))
(if debugging?
(begin (display T)
(display " : ")
(display (rep->symbol type))
(newline)))
(let ((cs (loop type K cs '())))
(hashtable-put! table T cs)
constraints)))
(else
(if debugging?
(begin (display T)
(display " = ")
(display (make-readable E #t))
(newline)))
(if (not (null? cs))
(begin
(display "Compiler bug: ")
(write T)
(display " has unexpectedly nonempty constraints")
(newline)))
(hashtable-put! table T (list (list T E K)))
constraints)))))
; Sets of constraints.
;
; The set of constraints is represented as (<hashtable> <killedby>),
; where <hashtable> is a hashtable mapping variables to lists of
; constraints as above, and <killedby> is a vector mapping basic killers
; to lists of variables that need to be examined for constraints that
; are killed by that basic killer.
(define number-of-basic-killers
(do ((i 0 (+ i 1))
(k 1 (+ k k)))
((> k available:killer:dead)
i)))
(define (constraints.table constraints) (car constraints))
(define (constraints.killed constraints) (cadr constraints))
(define (make-constraints-table)
(list (make-hashtable symbol-hash assq)
(make-vector number-of-basic-killers '())))
(define (copy-constraints-table constraints)
(list (hashtable-copy (constraints.table constraints))
(list->vector (vector->list (constraints.killed constraints)))))
(define (constraints-for-variable constraints T)
(hashtable-fetch (constraints.table constraints) T '()))
(define (constraints-add-killedby! constraints T K0)
(if (not (zero? K0))
(let ((v (constraints.killed constraints)))
(do ((i 0 (+ i 1))
(k 1 (+ k k)))
((= i number-of-basic-killers))
(if (not (zero? (logand k K0)))
(vector-set! v i (cons T (vector-ref v i))))))))
(define (constraints-kill! constraints K)
(if (not (zero? K))
(let ((table (constraints.table constraints))
(killed (constraints.killed constraints)))
(define (examine! T)
(let ((cs (filter (lambda (c)
(zero? (logand (constraint.killer c) K)))
(hashtable-fetch table T '()))))
(if (null? cs)
(hashtable-remove! table T)
(hashtable-put! table T cs))))
(do ((i 0 (+ i 1))
(j 1 (+ j j)))
((= i number-of-basic-killers))
(if (not (zero? (logand j K)))
(begin (for-each examine! (vector-ref killed i))
(vector-set! killed i '())))))))
(define (constraints-intersect! constraints0 constraints1 constraints2)
(let ((table0 (constraints.table constraints0))
(table1 (constraints.table constraints1))
(table2 (constraints.table constraints2)))
(if (eq? table0 table1)
; FIXME: Which is more efficient: to update the killed vector,
; or not to update it? Both are safe.
(hashtable-for-each (lambda (T cs)
(if (not (null? cs))
(hashtable-put!
table0
T
(cs-intersect
(hashtable-fetch table2 T '())
cs))))
table1)
; This case shouldn't ever happen, so it can be slow.
(begin
(constraints-intersect! constraints0 constraints0 constraints1)
(constraints-intersect! constraints0 constraints0 constraints2)))))
(define (cs-intersect cs1 cs2)
(define (loop cs init rep Krep)
(if (null? cs)
(values init rep Krep)
(let* ((c (car cs))
(cs (cdr cs))
(E2 (constraint.rhs c))
(K2 (constraint.killer c)))
(cond ((constant? E2)
(loop cs
init
(representation-intersection rep (constant.value E2))
(available:killer-combine Krep K2)))
((call? E2)
(if init
(begin (display "Compiler bug in cs-intersect")
(break))
(loop cs c rep Krep)))
(else
(error "Compiler bug in cs-intersect"))))))
(call-with-values
(lambda ()
(loop cs1 #f rep:object available:killer:none))
(lambda (c1 rep1 Krep1)
(call-with-values
(lambda ()
(loop cs2 #f rep:object available:killer:none))
(lambda (c2 rep2 Krep2)
(let ((c (if (equal? c1 c2) c1 #f))
(rep (representation-union rep1 rep2))
(Krep (available:killer-combine Krep1 Krep2)))
(if (eq? rep rep:object)
(if c (list c) '())
(let ((T (constraint.lhs (car cs1))))
(if c
(list c (make-type-constraint T rep Krep))
(list (make-type-constraint T rep Krep)))))))))))
; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
(define $gc.ephemeral 0)
(define $gc.tenuring 1)
(define $gc.full 2)
(define $mstat.wallocated-hi 0)
(define $mstat.wallocated-lo 1)
(define $mstat.wcollected-hi 2)
(define $mstat.wcollected-lo 3)
(define $mstat.wcopied-hi 4)
(define $mstat.wcopied-lo 5)
(define $mstat.gctime 6)
(define $mstat.wlive 7)
(define $mstat.gc-last-gen 8)
(define $mstat.gc-last-type 9)
(define $mstat.generations 10)
(define $mstat.g-gc-count 0)
(define $mstat.g-prom-count 1)
(define $mstat.g-gctime 2)
(define $mstat.g-wlive 3)
(define $mstat.g-np-youngp 4)
(define $mstat.g-np-oldp 5)
(define $mstat.g-np-j 6)
(define $mstat.g-np-k 7)
(define $mstat.g-alloc 8)
(define $mstat.g-target 9)
(define $mstat.g-promtime 10)
(define $mstat.remsets 11)
(define $mstat.r-apool 0)
(define $mstat.r-upool 1)
(define $mstat.r-ahash 2)
(define $mstat.r-uhash 3)
(define $mstat.r-hrec-hi 4)
(define $mstat.r-hrec-lo 5)
(define $mstat.r-hrem-hi 6)
(define $mstat.r-hrem-lo 7)
(define $mstat.r-hscan-hi 8)
(define $mstat.r-hscan-lo 9)
(define $mstat.r-wscan-hi 10)
(define $mstat.r-wscan-lo 11)
(define $mstat.r-ssbrec-hi 12)
(define $mstat.r-ssbrec-lo 13)
(define $mstat.r-np-p 14)
(define $mstat.fflushed-hi 12)
(define $mstat.fflushed-lo 13)
(define $mstat.wflushed-hi 14)
(define $mstat.wflushed-lo 15)
(define $mstat.stk-created 16)
(define $mstat.frestored-hi 17)
(define $mstat.frestored-lo 18)
(define $mstat.words-heap 19)
(define $mstat.words-remset 20)
(define $mstat.words-rts 21)
(define $mstat.swb-assign 22)
(define $mstat.swb-lhs-ok 23)
(define $mstat.swb-rhs-const 24)
(define $mstat.swb-not-xgen 25)
(define $mstat.swb-trans 26)
(define $mstat.rtime 27)
(define $mstat.stime 28)
(define $mstat.utime 29)
(define $mstat.minfaults 30)
(define $mstat.majfaults 31)
(define $mstat.np-remsetp 32)
(define $mstat.max-heap 33)
(define $mstat.promtime 34)
(define $mstat.wmoved-hi 35)
(define $mstat.wmoved-lo 36)
(define $mstat.vsize 37)
(define $g.reg0 12)
(define $r.reg8 44)
(define $r.reg9 48)
(define $r.reg10 52)
(define $r.reg11 56)
(define $r.reg12 60)
(define $r.reg13 64)
(define $r.reg14 68)
(define $r.reg15 72)
(define $r.reg16 76)
(define $r.reg17 80)
(define $r.reg18 84)
(define $r.reg19 88)
(define $r.reg20 92)
(define $r.reg21 96)
(define $r.reg22 100)
(define $r.reg23 104)
(define $r.reg24 108)
(define $r.reg25 112)
(define $r.reg26 116)
(define $r.reg27 120)
(define $r.reg28 124)
(define $r.reg29 128)
(define $r.reg30 132)
(define $r.reg31 136)
(define $g.stkbot 180)
(define $g.gccnt 420)
(define $m.alloc 1024)
(define $m.alloci 1032)
(define $m.gc 1040)
(define $m.addtrans 1048)
(define $m.stkoflow 1056)
(define $m.stkuflow 1072)
(define $m.creg 1080)
(define $m.creg-set! 1088)
(define $m.add 1096)
(define $m.subtract 1104)
(define $m.multiply 1112)
(define $m.quotient 1120)
(define $m.remainder 1128)
(define $m.divide 1136)
(define $m.modulo 1144)
(define $m.negate 1152)
(define $m.numeq 1160)
(define $m.numlt 1168)
(define $m.numle 1176)
(define $m.numgt 1184)
(define $m.numge 1192)
(define $m.zerop 1200)
(define $m.complexp 1208)
(define $m.realp 1216)
(define $m.rationalp 1224)
(define $m.integerp 1232)
(define $m.exactp 1240)
(define $m.inexactp 1248)
(define $m.exact->inexact 1256)
(define $m.inexact->exact 1264)
(define $m.make-rectangular 1272)
(define $m.real-part 1280)
(define $m.imag-part 1288)
(define $m.sqrt 1296)
(define $m.round 1304)
(define $m.truncate 1312)
(define $m.apply 1320)
(define $m.varargs 1328)
(define $m.typetag 1336)
(define $m.typetag-set 1344)
(define $m.break 1352)
(define $m.eqv 1360)
(define $m.partial-list->vector 1368)
(define $m.timer-exception 1376)
(define $m.exception 1384)
(define $m.singlestep 1392)
(define $m.syscall 1400)
(define $m.bvlcmp 1408)
(define $m.enable-interrupts 1416)
(define $m.disable-interrupts 1424)
(define $m.alloc-bv 1432)
(define $m.global-ex 1440)
(define $m.invoke-ex 1448)
(define $m.global-invoke-ex 1456)
(define $m.argc-ex 1464)
; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
(define $r.g0 0)
(define $r.g1 1)
(define $r.g2 2)
(define $r.g3 3)
(define $r.g4 4)
(define $r.g5 5)
(define $r.g6 6)
(define $r.g7 7)
(define $r.o0 8)
(define $r.o1 9)
(define $r.o2 10)
(define $r.o3 11)
(define $r.o4 12)
(define $r.o5 13)
(define $r.o6 14)
(define $r.o7 15)
(define $r.l0 16)
(define $r.l1 17)
(define $r.l2 18)
(define $r.l3 19)
(define $r.l4 20)
(define $r.l5 21)
(define $r.l6 22)
(define $r.l7 23)
(define $r.i0 24)
(define $r.i1 25)
(define $r.i2 26)
(define $r.i3 27)
(define $r.i4 28)
(define $r.i5 29)
(define $r.i6 30)
(define $r.i7 31)
(define $r.result $r.o0)
(define $r.argreg2 $r.o1)
(define $r.argreg3 $r.o2)
(define $r.stkp $r.o3)
(define $r.stklim $r.i0)
(define $r.tmp1 $r.o4)
(define $r.tmp2 $r.o5)
(define $r.tmp0 $r.g1)
(define $r.e-top $r.i0)
(define $r.e-limit $r.o3)
(define $r.timer $r.i4)
(define $r.millicode $r.i7)
(define $r.globals $r.i7)
(define $r.reg0 $r.l0)
(define $r.reg1 $r.l1)
(define $r.reg2 $r.l2)
(define $r.reg3 $r.l3)
(define $r.reg4 $r.l4)
(define $r.reg5 $r.l5)
(define $r.reg6 $r.l6)
(define $r.reg7 $r.l7)
; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
(define $ex.car 0)
(define $ex.cdr 1)
(define $ex.setcar 2)
(define $ex.setcdr 3)
(define $ex.add 10)
(define $ex.sub 11)
(define $ex.mul 12)
(define $ex.div 13)
(define $ex.lessp 14)
(define $ex.lesseqp 15)
(define $ex.equalp 16)
(define $ex.greatereqp 17)
(define $ex.greaterp 18)
(define $ex.quotient 19)
(define $ex.remainder 20)
(define $ex.modulo 21)
(define $ex.logior 22)
(define $ex.logand 23)
(define $ex.logxor 24)
(define $ex.lognot 25)
(define $ex.lsh 26)
(define $ex.rsha 27)
(define $ex.rshl 28)
(define $ex.e2i 29)
(define $ex.i2e 30)
(define $ex.exactp 31)
(define $ex.inexactp 32)
(define $ex.round 33)
(define $ex.trunc 34)
(define $ex.zerop 35)
(define $ex.neg 36)
(define $ex.abs 37)
(define $ex.realpart 38)
(define $ex.imagpart 39)
(define $ex.vref 40)
(define $ex.vset 41)
(define $ex.vlen 42)
(define $ex.pref 50)
(define $ex.pset 51)
(define $ex.plen 52)
(define $ex.sref 60)
(define $ex.sset 61)
(define $ex.slen 62)
(define $ex.bvref 70)
(define $ex.bvset 71)
(define $ex.bvlen 72)
(define $ex.bvlref 80)
(define $ex.bvlset 81)
(define $ex.bvllen 82)
(define $ex.vlref 90)
(define $ex.vlset 91)
(define $ex.vllen 92)
(define $ex.typetag 100)
(define $ex.typetagset 101)
(define $ex.apply 102)
(define $ex.argc 103)
(define $ex.vargc 104)
(define $ex.nonproc 105)
(define $ex.undef-global 106)
(define $ex.dump 107)
(define $ex.dumpfail 108)
(define $ex.timer 109)
(define $ex.unsupported 110)
(define $ex.int2char 111)
(define $ex.char2int 112)
(define $ex.mkbvl 113)
(define $ex.mkvl 114)
(define $ex.char<? 115)
(define $ex.char<=? 116)
(define $ex.char=? 117)
(define $ex.char>? 118)
(define $ex.char>=? 119)
(define $ex.bvfill 120)
(define $ex.enable-interrupts 121)
(define $ex.keyboard-interrupt 122)
(define $ex.arithmetic-exception 123)
(define $ex.global-invoke 124)
(define $ex.fx+ 140)
(define $ex.fx- 141)
(define $ex.fx-- 142)
(define $ex.fx= 143)
(define $ex.fx< 144)
(define $ex.fx<= 145)
(define $ex.fx> 146)
(define $ex.fx>= 147)
(define $ex.fxpositive? 148)
(define $ex.fxnegative? 149)
(define $ex.fxzero? 150)
(define $ex.fx* 151)
; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
(define $tag.tagmask 7)
(define $tag.pair-tag 1)
(define $tag.vector-tag 3)
(define $tag.bytevector-tag 5)
(define $tag.procedure-tag 7)
(define $imm.vector-header 162)
(define $imm.bytevector-header 194)
(define $imm.procedure-header 254)
(define $imm.true 6)
(define $imm.false 2)
(define $imm.null 10)
(define $imm.unspecified 278)
(define $imm.eof 534)
(define $imm.undefined 790)
(define $imm.character 38)
(define $tag.vector-typetag 0)
(define $tag.rectnum-typetag 4)
(define $tag.ratnum-typetag 8)
(define $tag.symbol-typetag 12)
(define $tag.port-typetag 16)
(define $tag.structure-typetag 20)
(define $tag.bytevector-typetag 0)
(define $tag.string-typetag 4)
(define $tag.flonum-typetag 8)
(define $tag.compnum-typetag 12)
(define $tag.bignum-typetag 16)
(define $hdr.port 178)
(define $hdr.struct 182)
(define $p.codevector -3)
(define $p.constvector 1)
(define $p.linkoffset 5)
(define $p.reg0 5)
(define $p.codeoffset -1)
; Copyright 1991 William Clinger
;
; Relatively target-independent information for Twobit's backend.
;
; 24 April 1999 / wdc
;
; Most of the definitions in this file can be extended or overridden by
; target-specific definitions.
(define twobit-sort
(lambda (less? list) (compat:sort list less?)))
(define renaming-prefix ".")
; The prefix used for cells introduced by the compiler.
(define cell-prefix (string-append renaming-prefix "CELL:"))
; Names of global procedures that cannot be redefined or assigned
; by ordinary code.
; The expansion of quasiquote uses .cons and .list directly, so these
; should not be changed willy-nilly.
; Others may be used directly by a DEFINE-INLINE.
(define name:CHECK! '.check!)
(define name:CONS '.cons)
(define name:LIST '.list)
(define name:MAKE-CELL '.make-cell)
(define name:CELL-REF '.cell-ref)
(define name:CELL-SET! '.cell-set!)
(define name:IGNORED (string->symbol "IGNORED"))
(define name:CAR '.car)
(define name:CDR '.cdr)
;(begin (eval `(define ,name:CONS cons))
; (eval `(define ,name:LIST list))
; (eval `(define ,name:MAKE-CELL list))
; (eval `(define ,name:CELL-REF car))
; (eval `(define ,name:CELL-SET! set-car!)))
; If (INTEGRATE-USUAL-PROCEDURES) is true, then control optimization
; recognizes calls to these procedures.
(define name:NOT 'not)
(define name:MEMQ 'memq)
(define name:MEMV 'memv)
; If (INTEGRATE-USUAL-PROCEDURES) is true, then control optimization
; recognizes calls to these procedures and also creates calls to them.
(define name:EQ? 'eq?)
(define name:EQV? 'eqv?)
; Control optimization creates calls to these procedures,
; which do not need to check their arguments.
(define name:FIXNUM? 'fixnum?)
(define name:CHAR? 'char?)
(define name:SYMBOL? 'symbol?)
(define name:FX< '<:fix:fix)
(define name:FX- 'fx-) ; non-checking version
(define name:CHAR->INTEGER 'char->integer) ; non-checking version
(define name:VECTOR-REF 'vector-ref:trusted)
; Constant folding.
; Prototype, will probably change in the future.
(define (constant-folding-entry name)
(assq name $usual-constant-folding-procedures$))
(define constant-folding-predicates cadr)
(define constant-folding-folder caddr)
(define $usual-constant-folding-procedures$
(let ((always? (lambda (x) #t))
(charcode? (lambda (n)
(and (number? n)
(exact? n)
(<= 0 n)
(< n 128))))
(ratnum? (lambda (n)
(and (number? n)
(exact? n)
(rational? n))))
; smallint? is defined later.
(smallint? (lambda (n) (smallint? n))))
`(
; This makes some assumptions about the host system.
(integer->char (,charcode?) ,integer->char)
(char->integer (,char?) ,char->integer)
(zero? (,ratnum?) ,zero?)
(< (,ratnum? ,ratnum?) ,<)
(<= (,ratnum? ,ratnum?) ,<=)
(= (,ratnum? ,ratnum?) ,=)
(>= (,ratnum? ,ratnum?) ,>=)
(> (,ratnum? ,ratnum?) ,>)
(+ (,ratnum? ,ratnum?) ,+)
(- (,ratnum? ,ratnum?) ,-)
(* (,ratnum? ,ratnum?) ,*)
(-- (,ratnum?) ,(lambda (x) (- 0 x)))
(eq? (,always? ,always?) ,eq?)
(eqv? (,always? ,always?) ,eqv?)
(equal? (,always? ,always?) ,equal?)
(memq (,always? ,list?) ,memq)
(memv (,always? ,list?) ,memv)
(member (,always? ,list?) ,member)
(assq (,always? ,list?) ,assq)
(assv (,always? ,list?) ,assv)
(assoc (,always? ,list?) ,assoc)
(length (,list?) ,length)
(fixnum? (,smallint?) ,smallint?)
(=:fix:fix (,smallint? ,smallint?) ,=)
(<:fix:fix (,smallint? ,smallint?) ,<)
(<=:fix:fix (,smallint? ,smallint?) ,<=)
(>:fix:fix (,smallint? ,smallint?) ,>)
(>=:fix:fix (,smallint? ,smallint?) ,>=)
)))
(begin '
(define (.check! flag exn . args)
(if (not flag)
(apply error "Runtime check exception: " exn args)))
#t)
; Order matters. If f and g are both inlined, and the definition of g
; uses f, then f should be defined before g.
(for-each pass1
`(
(define-inline car
(syntax-rules ()
((car x0)
(let ((x x0))
(.check! (pair? x) ,$ex.car x)
(car:pair x)))))
(define-inline cdr
(syntax-rules ()
((car x0)
(let ((x x0))
(.check! (pair? x) ,$ex.cdr x)
(cdr:pair x)))))
(define-inline vector-length
(syntax-rules ()
((vector-length v0)
(let ((v v0))
(.check! (vector? v) ,$ex.vlen v)
(vector-length:vec v)))))
(define-inline vector-ref
(syntax-rules ()
((vector-ref v0 i0)
(let ((v v0)
(i i0))
(.check! (fixnum? i) ,$ex.vref v i)
(.check! (vector? v) ,$ex.vref v i)
(.check! (<:fix:fix i (vector-length:vec v)) ,$ex.vref v i)
(.check! (>=:fix:fix i 0) ,$ex.vref v i)
(vector-ref:trusted v i)))))
(define-inline vector-set!
(syntax-rules ()
((vector-set! v0 i0 x0)
(let ((v v0)
(i i0)
(x x0))
(.check! (fixnum? i) ,$ex.vset v i x)
(.check! (vector? v) ,$ex.vset v i x)
(.check! (<:fix:fix i (vector-length:vec v)) ,$ex.vset v i x)
(.check! (>=:fix:fix i 0) ,$ex.vset v i x)
(vector-set!:trusted v i x)))))
; This transformation must make sure the entire list is freshly
; allocated when an argument to LIST returns more than once.
(define-inline list
(syntax-rules ()
((list)
'())
((list ?e)
(cons ?e '()))
((list ?e1 ?e2 ...)
(let* ((t1 ?e1)
(t2 (list ?e2 ...)))
(cons t1 t2)))))
; This transformation must make sure the entire list is freshly
; allocated when an argument to VECTOR returns more than once.
(define-inline vector
(syntax-rules ()
((vector)
'#())
((vector ?e)
(make-vector 1 ?e))
((vector ?e1 ?e2 ...)
(letrec-syntax
((vector-aux1
(... (syntax-rules ()
((vector-aux1 () ?n ?exps ?indexes ?temps)
(vector-aux2 ?n ?exps ?indexes ?temps))
((vector-aux1 (?exp1 ?exp2 ...) ?n ?exps ?indexes ?temps)
(vector-aux1 (?exp2 ...)
(+ ?n 1)
(?exp1 . ?exps)
(?n . ?indexes)
(t . ?temps))))))
(vector-aux2
(... (syntax-rules ()
((vector-aux2 ?n (?exp1 ?exp2 ...) (?n1 ?n2 ...) (?t1 ?t2 ...))
(let* ((?t1 ?exp1)
(?t2 ?exp2)
...
(v (make-vector ?n ?t1)))
(vector-set! v ?n2 ?t2)
...
v))))))
(vector-aux1 (?e1 ?e2 ...) 0 () () ())))))
(define-inline cadddr
(syntax-rules ()
((cadddr ?e)
(car (cdr (cdr (cdr ?e)))))))
(define-inline cddddr
(syntax-rules ()
((cddddr ?e)
(cdr (cdr (cdr (cdr ?e)))))))
(define-inline cdddr
(syntax-rules ()
((cdddr ?e)
(cdr (cdr (cdr ?e))))))
(define-inline caddr
(syntax-rules ()
((caddr ?e)
(car (cdr (cdr ?e))))))
(define-inline cddr
(syntax-rules ()
((cddr ?e)
(cdr (cdr ?e)))))
(define-inline cdar
(syntax-rules ()
((cdar ?e)
(cdr (car ?e)))))
(define-inline cadr
(syntax-rules ()
((cadr ?e)
(car (cdr ?e)))))
(define-inline caar
(syntax-rules ()
((caar ?e)
(car (car ?e)))))
(define-inline make-vector
(syntax-rules ()
((make-vector ?n)
(make-vector ?n '()))))
(define-inline make-string
(syntax-rules ()
((make-string ?n)
(make-string ?n #\space))))
(define-inline =
(syntax-rules ()
((= ?e1 ?e2 ?e3 ?e4 ...)
(let ((t ?e2))
(and (= ?e1 t)
(= t ?e3 ?e4 ...))))))
(define-inline <
(syntax-rules ()
((< ?e1 ?e2 ?e3 ?e4 ...)
(let ((t ?e2))
(and (< ?e1 t)
(< t ?e3 ?e4 ...))))))
(define-inline >
(syntax-rules ()
((> ?e1 ?e2 ?e3 ?e4 ...)
(let ((t ?e2))
(and (> ?e1 t)
(> t ?e3 ?e4 ...))))))
(define-inline <=
(syntax-rules ()
((<= ?e1 ?e2 ?e3 ?e4 ...)
(let ((t ?e2))
(and (<= ?e1 t)
(<= t ?e3 ?e4 ...))))))
(define-inline >=
(syntax-rules ()
((>= ?e1 ?e2 ?e3 ?e4 ...)
(let ((t ?e2))
(and (>= ?e1 t)
(>= t ?e3 ?e4 ...))))))
(define-inline +
(syntax-rules ()
((+)
0)
((+ ?e)
?e)
((+ ?e1 ?e2 ?e3 ?e4 ...)
(+ (+ ?e1 ?e2) ?e3 ?e4 ...))))
(define-inline *
(syntax-rules ()
((*)
1)
((* ?e)
?e)
((* ?e1 ?e2 ?e3 ?e4 ...)
(* (* ?e1 ?e2) ?e3 ?e4 ...))))
(define-inline -
(syntax-rules ()
((- ?e)
(- 0 ?e))
((- ?e1 ?e2 ?e3 ?e4 ...)
(- (- ?e1 ?e2) ?e3 ?e4 ...))))
(define-inline /
(syntax-rules ()
((/ ?e)
(/ 1 ?e))
((/ ?e1 ?e2 ?e3 ?e4 ...)
(/ (/ ?e1 ?e2) ?e3 ?e4 ...))))
(define-inline abs
(syntax-rules ()
((abs ?z)
(let ((temp ?z))
(if (< temp 0)
(-- temp)
temp)))))
(define-inline negative?
(syntax-rules ()
((negative? ?x)
(< ?x 0))))
(define-inline positive?
(syntax-rules ()
((positive? ?x)
(> ?x 0))))
(define-inline eqv?
(transformer
(lambda (exp rename compare)
(let ((arg1 (cadr exp))
(arg2 (caddr exp)))
(define (constant? exp)
(or (boolean? exp)
(char? exp)
(and (pair? exp)
(= (length exp) 2)
(identifier? (car exp))
(compare (car exp) (rename 'quote))
(symbol? (cadr exp)))))
(if (or (constant? arg1)
(constant? arg2))
(cons (rename 'eq?) (cdr exp))
exp)))))
(define-inline memq
(syntax-rules (quote)
((memq ?expr '(?datum ...))
(letrec-syntax
((memq0
(... (syntax-rules (quote)
((memq0 '?xx '(?d ...))
(let ((t1 '(?d ...)))
(memq1 '?xx t1 (?d ...))))
((memq0 ?e '(?d ...))
(let ((t0 ?e)
(t1 '(?d ...)))
(memq1 t0 t1 (?d ...)))))))
(memq1
(... (syntax-rules ()
((memq1 ?t0 ?t1 ())
#f)
((memq1 ?t0 ?t1 (?d1 ?d2 ...))
(if (eq? ?t0 '?d1)
?t1
(let ((?t1 (cdr ?t1)))
(memq1 ?t0 ?t1 (?d2 ...)))))))))
(memq0 ?expr '(?datum ...))))))
(define-inline memv
(transformer
(lambda (exp rename compare)
(let ((arg1 (cadr exp))
(arg2 (caddr exp)))
(if (or (boolean? arg1)
(fixnum? arg1)
(char? arg1)
(and (pair? arg1)
(= (length arg1) 2)
(identifier? (car arg1))
(compare (car arg1) (rename 'quote))
(symbol? (cadr arg1)))
(and (pair? arg2)
(= (length arg2) 2)
(identifier? (car arg2))
(compare (car arg2) (rename 'quote))
(every1? (lambda (x)
(or (boolean? x)
(fixnum? x)
(char? x)
(symbol? x)))
(cadr arg2))))
(cons (rename 'memq) (cdr exp))
exp)))))
(define-inline assv
(transformer
(lambda (exp rename compare)
(let ((arg1 (cadr exp))
(arg2 (caddr exp)))
(if (or (boolean? arg1)
(char? arg1)
(and (pair? arg1)
(= (length arg1) 2)
(identifier? (car arg1))
(compare (car arg1) (rename 'quote))
(symbol? (cadr arg1)))
(and (pair? arg2)
(= (length arg2) 2)
(identifier? (car arg2))
(compare (car arg2) (rename 'quote))
(every1? (lambda (y)
(and (pair? y)
(let ((x (car y)))
(or (boolean? x)
(char? x)
(symbol? x)))))
(cadr arg2))))
(cons (rename 'assq) (cdr exp))
exp)))))
(define-inline map
(syntax-rules (lambda)
((map ?proc ?exp1 ?exp2 ...)
(letrec-syntax
((loop
(... (syntax-rules (lambda)
((loop 1 () (?y1 ?y2 ...) ?f ?exprs)
(loop 2 (?y1 ?y2 ...) ?f ?exprs))
((loop 1 (?a1 ?a2 ...) (?y2 ...) ?f ?exprs)
(loop 1 (?a2 ...) (y1 ?y2 ...) ?f ?exprs))
((loop 2 ?ys (lambda ?formals ?body) ?exprs)
(loop 3 ?ys (lambda ?formals ?body) ?exprs))
((loop 2 ?ys (?f1 . ?f2) ?exprs)
(let ((f (?f1 . ?f2)))
(loop 3 ?ys f ?exprs)))
; ?f must be a constant or variable.
((loop 2 ?ys ?f ?exprs)
(loop 3 ?ys ?f ?exprs))
((loop 3 (?y1 ?y2 ...) ?f (?e1 ?e2 ...))
(do ((?y1 ?e1 (cdr ?y1))
(?y2 ?e2 (cdr ?y2))
...
(results '() (cons (?f (car ?y1) (car ?y2) ...)
results)))
((or (null? ?y1) (null? ?y2) ...)
(reverse results))))))))
(loop 1 (?exp1 ?exp2 ...) () ?proc (?exp1 ?exp2 ...))))))
(define-inline for-each
(syntax-rules (lambda)
((for-each ?proc ?exp1 ?exp2 ...)
(letrec-syntax
((loop
(... (syntax-rules (lambda)
((loop 1 () (?y1 ?y2 ...) ?f ?exprs)
(loop 2 (?y1 ?y2 ...) ?f ?exprs))
((loop 1 (?a1 ?a2 ...) (?y2 ...) ?f ?exprs)
(loop 1 (?a2 ...) (y1 ?y2 ...) ?f ?exprs))
((loop 2 ?ys (lambda ?formals ?body) ?exprs)
(loop 3 ?ys (lambda ?formals ?body) ?exprs))
((loop 2 ?ys (?f1 . ?f2) ?exprs)
(let ((f (?f1 . ?f2)))
(loop 3 ?ys f ?exprs)))
; ?f must be a constant or variable.
((loop 2 ?ys ?f ?exprs)
(loop 3 ?ys ?f ?exprs))
((loop 3 (?y1 ?y2 ...) ?f (?e1 ?e2 ...))
(do ((?y1 ?e1 (cdr ?y1))
(?y2 ?e2 (cdr ?y2))
...)
((or (null? ?y1) (null? ?y2) ...)
(if #f #f))
(?f (car ?y1) (car ?y2) ...)))))))
(loop 1 (?exp1 ?exp2 ...) () ?proc (?exp1 ?exp2 ...))))))
))
(define extended-syntactic-environment
(syntactic-copy global-syntactic-environment))
(define (make-extended-syntactic-environment)
(syntactic-copy extended-syntactic-environment))
; MacScheme machine assembly instructions.
(define instruction.op car)
(define instruction.arg1 cadr)
(define instruction.arg2 caddr)
(define instruction.arg3 cadddr)
; Opcode table.
(define *mnemonic-names* '()) ; For readify-lap
(begin
'
(define *last-reserved-mnemonic* 32767) ; For consistency check
'
(define make-mnemonic
(let ((count 0))
(lambda (name)
(set! count (+ count 1))
(if (= count *last-reserved-mnemonic*)
(error "Error in make-mnemonic: conflict: " name))
(set! *mnemonic-names* (cons (cons count name) *mnemonic-names*))
count)))
'
(define (reserved-mnemonic name value)
(if (and (> value 0) (< value *last-reserved-mnemonic*))
(set! *last-reserved-mnemonic* value))
(set! *mnemonic-names* (cons (cons value name) *mnemonic-names*))
value)
#t)
(define make-mnemonic
(let ((count 0))
(lambda (name)
(set! count (+ count 1))
(set! *mnemonic-names* (cons (cons count name) *mnemonic-names*))
count)))
(define (reserved-mnemonic name ignored)
(make-mnemonic name))
(define $.linearize (reserved-mnemonic '.linearize -1)) ; unused?
(define $.label (reserved-mnemonic '.label 63))
(define $.proc (reserved-mnemonic '.proc 62)) ; proc entry point
(define $.cont (reserved-mnemonic '.cont 61)) ; return point
(define $.align (reserved-mnemonic '.align 60)) ; align code stream
(define $.asm (reserved-mnemonic '.asm 59)) ; in-line native code
(define $.proc-doc ; internal def proc info
(reserved-mnemonic '.proc-doc 58))
(define $.end ; end of code vector
(reserved-mnemonic '.end 57)) ; (asm internal)
(define $.singlestep ; insert singlestep point
(reserved-mnemonic '.singlestep 56)) ; (asm internal)
(define $.entry (reserved-mnemonic '.entry 55)) ; procedure entry point
; (asm internal)
(define $op1 (make-mnemonic 'op1)) ; op prim
(define $op2 (make-mnemonic 'op2)) ; op2 prim,k
(define $op3 (make-mnemonic 'op3)) ; op3 prim,k1,k2
(define $op2imm (make-mnemonic 'op2imm)) ; op2imm prim,x
(define $const (make-mnemonic 'const)) ; const x
(define $global (make-mnemonic 'global)) ; global x
(define $setglbl (make-mnemonic 'setglbl)) ; setglbl x
(define $lexical (make-mnemonic 'lexical)) ; lexical m,n
(define $setlex (make-mnemonic 'setlex)) ; setlex m,n
(define $stack (make-mnemonic 'stack)) ; stack n
(define $setstk (make-mnemonic 'setstk)) ; setstk n
(define $load (make-mnemonic 'load)) ; load k,n
(define $store (make-mnemonic 'store)) ; store k,n
(define $reg (make-mnemonic 'reg)) ; reg k
(define $setreg (make-mnemonic 'setreg)) ; setreg k
(define $movereg (make-mnemonic 'movereg)) ; movereg k1,k2
(define $lambda (make-mnemonic 'lambda)) ; lambda x,n,doc
(define $lexes (make-mnemonic 'lexes)) ; lexes n,doc
(define $args= (make-mnemonic 'args=)) ; args= k
(define $args>= (make-mnemonic 'args>=)) ; args>= k
(define $invoke (make-mnemonic 'invoke)) ; invoke k
(define $save (make-mnemonic 'save)) ; save L,k
(define $setrtn (make-mnemonic 'setrtn)) ; setrtn L
(define $restore (make-mnemonic 'restore)) ; restore n ; deprecated
(define $pop (make-mnemonic 'pop)) ; pop k
(define $popstk (make-mnemonic 'popstk)) ; popstk ; for students
(define $return (make-mnemonic 'return)) ; return
(define $mvrtn (make-mnemonic 'mvrtn)) ; mvrtn ; NYI
(define $apply (make-mnemonic 'apply)) ; apply
(define $nop (make-mnemonic 'nop)) ; nop
(define $jump (make-mnemonic 'jump)) ; jump m,o
(define $skip (make-mnemonic 'skip)) ; skip L ; forward
(define $branch (make-mnemonic 'branch)) ; branch L
(define $branchf (make-mnemonic 'branchf)) ; branchf L
(define $check (make-mnemonic 'check)) ; check k1,k2,k3,L
(define $trap (make-mnemonic 'trap)) ; trap k1,k2,k3,exn
; A peephole optimizer may define more instructions in some
; target-specific file.
; eof
; Copyright 1991 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Larceny -- target-specific information for Twobit's SPARC backend.
;
; 11 June 1999 / wdc
; The maximum number of fixed arguments that may be followed by a rest
; argument. This limitation is removed by the macro expander.
(define @maxargs-with-rest-arg@ 30)
; The number of MacScheme machine registers.
; (They do not necessarily correspond to hardware registers.)
(define *nregs* 32)
(define *lastreg* (- *nregs* 1))
(define *fullregs* (quotient *nregs* 2))
; The number of argument registers that are represented by hardware
; registers.
(define *nhwregs* 8)
; Variable names that indicate register targets.
(define *regnames*
(do ((alist '() (cons (cons (string->symbol
(string-append ".REG" (number->string r)))
r)
alist))
(r (- *nhwregs* 1) (- r 1)))
((<= r 0)
alist)))
; A non-inclusive upper bound for the instruction encodings.
(define *number-of-mnemonics* 72)
; Integrable procedures and procedure-specific source code transformations.
; Every integrable procedure that takes a varying number of arguments must
; supply a transformation procedure to map calls into the fixed arity
; required by the MacScheme machine instructions.
; The table of integrable procedures.
; Each entry is a list of the following items:
;
; procedure name
; arity (or -1 for special primops like .check!)
; procedure name to be used by the disassembler
; predicate for immediate operands (or #f)
; primop code in the MacScheme machine (not used by Larceny)
; the effects that kill this primop's result
; the effects of this primop that kill available expressions
(define (prim-entry name)
(assq name $usual-integrable-procedures$))
(define prim-arity cadr)
(define prim-opcodename caddr)
(define prim-immediate? cadddr)
(define (prim-primcode entry)
(car (cddddr entry)))
; This predicate returns #t iff its argument will be represented
; as a fixnum on the target machine.
(define smallint?
(let* ((least (- (expt 2 29)))
(greatest (- (- least) 1)))
(lambda (x)
(and (number? x)
(exact? x)
(integer? x)
(<= least x greatest)))))
(define (sparc-imm? x)
(and (fixnum? x)
(<= -1024 x 1023)))
(define (sparc-eq-imm? x)
(or (sparc-imm? x)
(eq? x #t)
(eq? x #f)
(eq? x '())))
(define (valid-typetag? x)
(and (fixnum? x)
(<= 0 x 7)))
(define (fixnum-primitives) #t)
(define (flonum-primitives) #t)
; The table of primitives has been extended with
; kill information used for commoning.
(define (prim-lives-until entry)
(list-ref entry 5))
(define (prim-kills entry)
(list-ref entry 6))
(define $usual-integrable-procedures$
(let ((:globals available:killer:globals)
(:car available:killer:car)
(:cdr available:killer:cdr)
(:string available:killer:string)
(:vector available:killer:vector)
(:cell available:killer:cell)
(:io available:killer:io)
(:none available:killer:none) ; none of the above
(:all available:killer:all) ; all of the above
(:immortal available:killer:immortal) ; never killed
(:dead available:killer:dead) ; never available
)
; external arity internal immediate ignored killed kills
; name name predicate by what
; kind of
; effect
`((break 0 break #f 3 ,:dead ,:all)
(creg 0 creg #f 7 ,:dead ,:all)
(unspecified 0 unspecified #f -1 ,:dead ,:none)
(undefined 0 undefined #f 8 ,:dead ,:none)
(eof-object 0 eof-object #f -1 ,:dead ,:none)
(enable-interrupts 1 enable-interrupts #f -1 ,:dead ,:all)
(disable-interrupts 0 disable-interrupts #f -1 ,:dead ,:all)
(typetag 1 typetag #f #x11 ,:dead ,:none)
(not 1 not #f #x18 ,:immortal ,:none)
(null? 1 null? #f #x19 ,:immortal ,:none)
(pair? 1 pair? #f #x1a ,:immortal ,:none)
(eof-object? 1 eof-object? #f -1 ,:immortal ,:none)
(port? 1 port? #f -1 ,:dead ,:none)
(structure? 1 structure? #f -1 ,:dead ,:none)
(car 1 car #f #x1b ,:car ,:none)
(,name:CAR 1 car #f #x1b ,:car ,:none)
(cdr 1 cdr #f #x1c ,:cdr ,:none)
(,name:CDR 1 cdr #f #x1c ,:cdr ,:none)
(symbol? 1 symbol? #f #x1f ,:immortal ,:none)
(number? 1 complex? #f #x20 ,:immortal ,:none)
(complex? 1 complex? #f #x20 ,:immortal ,:none)
(real? 1 rational? #f #x21 ,:immortal ,:none)
(rational? 1 rational? #f #x21 ,:immortal ,:none)
(integer? 1 integer? #f #x22 ,:immortal ,:none)
(fixnum? 1 fixnum? #f #x23 ,:immortal ,:none)
(flonum? 1 flonum? #f -1 ,:immortal ,:none)
(compnum? 1 compnum? #f -1 ,:immortal ,:none)
(exact? 1 exact? #f #x24 ,:immortal ,:none)
(inexact? 1 inexact? #f #x25 ,:immortal ,:none)
(exact->inexact 1 exact->inexact #f #x26 ,:immortal ,:none)
(inexact->exact 1 inexact->exact #f #x27 ,:immortal ,:none)
(round 1 round #f #x28 ,:immortal ,:none)
(truncate 1 truncate #f #x29 ,:immortal ,:none)
(zero? 1 zero? #f #x2c ,:immortal ,:none)
(-- 1 -- #f #x2d ,:immortal ,:none)
(lognot 1 lognot #f #x2f ,:immortal ,:none)
(real-part 1 real-part #f #x3e ,:immortal ,:none)
(imag-part 1 imag-part #f #x3f ,:immortal ,:none)
(char? 1 char? #f #x40 ,:immortal ,:none)
(char->integer 1 char->integer #f #x41 ,:immortal ,:none)
(integer->char 1 integer->char #f #x42 ,:immortal ,:none)
(string? 1 string? #f #x50 ,:immortal ,:none)
(string-length 1 string-length #f #x51 ,:immortal ,:none)
(vector? 1 vector? #f #x52 ,:immortal ,:none)
(vector-length 1 vector-length #f #x53 ,:immortal ,:none)
(bytevector? 1 bytevector? #f #x54 ,:immortal ,:none)
(bytevector-length 1 bytevector-length #f #x55 ,:immortal ,:none)
(bytevector-fill! 2 bytevector-fill! #f -1 ,:dead ,:string)
(make-bytevector 1 make-bytevector #f #x56 ,:dead ,:none)
(procedure? 1 procedure? #f #x58 ,:immortal ,:none)
(procedure-length 1 procedure-length #f #x59 ,:dead ,:none)
(make-procedure 1 make-procedure #f #x5a ,:dead ,:none)
(creg-set! 1 creg-set! #f #x71 ,:dead ,:none)
(,name:MAKE-CELL 1 make-cell #f #x7e ,:dead ,:none)
(,name:CELL-REF 1 cell-ref #f #x7f ,:cell ,:none)
(,name:CELL-SET! 2 cell-set! #f #xdf ,:dead ,:cell)
(typetag-set! 2 typetag-set! ,valid-typetag? #xa0 ,:dead ,:all)
(eq? 2 eq? ,sparc-eq-imm? #xa1 ,:immortal ,:none)
(eqv? 2 eqv? #f #xa2 ,:immortal ,:none)
(cons 2 cons #f #xa8 ,:dead ,:none)
(,name:CONS 2 cons #f #xa8 ,:dead ,:none)
(set-car! 2 set-car! #f #xa9 ,:dead ,:car)
(set-cdr! 2 set-cdr! #f #xaa ,:dead ,:cdr)
(+ 2 + ,sparc-imm? #xb0 ,:immortal ,:none)
(- 2 - ,sparc-imm? #xb1 ,:immortal ,:none)
(* 2 * ,sparc-imm? #xb2 ,:immortal ,:none)
(/ 2 / #f #xb3 ,:immortal ,:none)
(quotient 2 quotient #f #xb4 ,:immortal ,:none)
(< 2 < ,sparc-imm? #xb5 ,:immortal ,:none)
(<= 2 <= ,sparc-imm? #xb6 ,:immortal ,:none)
(= 2 = ,sparc-imm? #xb7 ,:immortal ,:none)
(> 2 > ,sparc-imm? #xb8 ,:immortal ,:none)
(>= 2 >= ,sparc-imm? #xb9 ,:immortal ,:none)
(logand 2 logand #f #xc0 ,:immortal ,:none)
(logior 2 logior #f #xc1 ,:immortal ,:none)
(logxor 2 logxor #f #xc2 ,:immortal ,:none)
(lsh 2 lsh #f #xc3 ,:immortal ,:none)
(rsha 2 rsha #f -1 ,:immortal ,:none)
(rshl 2 rshl #f -1 ,:immortal ,:none)
(rot 2 rot #f #xc4 ,:immortal ,:none)
(make-string 2 make-string #f -1 ,:dead ,:none)
(string-ref 2 string-ref ,sparc-imm? #xd1 ,:string ,:none)
(string-set! 3 string-set! ,sparc-imm? -1 ,:dead ,:string)
(make-vector 2 make-vector #f #xd2 ,:dead ,:none)
(vector-ref 2 vector-ref ,sparc-imm? #xd3 ,:vector ,:none)
(bytevector-ref 2 bytevector-ref ,sparc-imm? #xd5 ,:string ,:none)
(procedure-ref 2 procedure-ref #f #xd7 ,:dead ,:none)
(char<? 2 char<? ,char? #xe0 ,:immortal ,:none)
(char<=? 2 char<=? ,char? #xe1 ,:immortal ,:none)
(char=? 2 char=? ,char? #xe2 ,:immortal ,:none)
(char>? 2 char>? ,char? #xe3 ,:immortal ,:none)
(char>=? 2 char>=? ,char? #xe4 ,:immortal ,:none)
(sys$partial-list->vector 2 sys$partial-list->vector #f -1 ,:dead ,:all)
(vector-set! 3 vector-set! #f #xf1 ,:dead ,:vector)
(bytevector-set! 3 bytevector-set! #f #xf2 ,:dead ,:string)
(procedure-set! 3 procedure-set! #f #xf3 ,:dead ,:all)
(bytevector-like? 1 bytevector-like? #f -1 ,:immortal ,:none)
(vector-like? 1 vector-like? #f -1 ,:immortal ,:none)
(bytevector-like-ref 2 bytevector-like-ref #f -1 ,:string ,:none)
(bytevector-like-set! 3 bytevector-like-set! #f -1 ,:dead ,:string)
(sys$bvlcmp 2 sys$bvlcmp #f -1 ,:dead ,:all)
(vector-like-ref 2 vector-like-ref #f -1 ,:vector ,:none)
(vector-like-set! 3 vector-like-set! #f -1 ,:dead ,:vector)
(vector-like-length 1 vector-like-length #f -1 ,:immortal ,:none)
(bytevector-like-length 1 bytevector-like-length #f -1 ,:immortal ,:none)
(remainder 2 remainder #f -1 ,:immortal ,:none)
(sys$read-char 1 sys$read-char #f -1 ,:dead ,:io)
(gc-counter 0 gc-counter #f -1 ,:dead ,:none)
,@(if (fixnum-primitives)
`((most-positive-fixnum
0 most-positive-fixnum
#f -1 ,:immortal ,:none)
(most-negative-fixnum
0 most-negative-fixnum
#f -1 ,:immortal ,:none)
(fx+ 2 fx+ ,sparc-imm? -1 ,:immortal ,:none)
(fx- 2 fx- ,sparc-imm? -1 ,:immortal ,:none)
(fx-- 1 fx-- #f -1 ,:immortal ,:none)
(fx* 2 fx* #f -1 ,:immortal ,:none)
(fx= 2 fx= ,sparc-imm? -1 ,:immortal ,:none)
(fx< 2 fx< ,sparc-imm? -1 ,:immortal ,:none)
(fx<= 2 fx<= ,sparc-imm? -1 ,:immortal ,:none)
(fx> 2 fx> ,sparc-imm? -1 ,:immortal ,:none)
(fx>= 2 fx>= ,sparc-imm? -1 ,:immortal ,:none)
(fxzero? 1 fxzero? #f -1 ,:immortal ,:none)
(fxpositive? 1 fxpositive? #f -1 ,:immortal ,:none)
(fxnegative? 1 fxnegative? #f -1 ,:immortal ,:none))
'())
,@(if (flonum-primitives)
`((fl+ 2 + #f -1 ,:immortal ,:none)
(fl- 2 - #f -1 ,:immortal ,:none)
(fl-- 1 -- #f -1 ,:immortal ,:none)
(fl* 2 * #f -1 ,:immortal ,:none)
(fl= 2 = #f -1 ,:immortal ,:none)
(fl< 2 < #f -1 ,:immortal ,:none)
(fl<= 2 <= #f -1 ,:immortal ,:none)
(fl> 2 > #f -1 ,:immortal ,:none)
(fl>= 2 >= #f -1 ,:immortal ,:none))
'())
; Added for CSE, representation analysis.
(,name:CHECK! -1 check! #f -1 ,:dead ,:none)
(vector-length:vec 1 vector-length:vec #f -1 ,:immortal ,:none)
(vector-ref:trusted 2 vector-ref:trusted ,sparc-imm? -1 ,:vector ,:none)
(vector-set!:trusted 3 vector-set!:trusted #f -1 ,:dead ,:vector)
(car:pair 1 car:pair #f -1 ,:car ,:none)
(cdr:pair 1 cdr:pair #f -1 ,:cdr ,:none)
(=:fix:fix 2 =:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
(<:fix:fix 2 <:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
(<=:fix:fix 2 <=:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
(>=:fix:fix 2 >=:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
(>:fix:fix 2 >:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
; Not yet implemented.
(+:idx:idx 2 +:idx:idx #f -1 ,:immortal ,:none)
(+:fix:fix 2 +:idx:idx #f -1 ,:immortal ,:none)
(+:exi:exi 2 +:idx:idx #f -1 ,:immortal ,:none)
(+:flo:flo 2 +:idx:idx #f -1 ,:immortal ,:none)
(=:flo:flo 2 =:flo:flo #f -1 ,:immortal ,:none)
(=:obj:flo 2 =:obj:flo #f -1 ,:immortal ,:none)
(=:flo:obj 2 =:flo:obj #f -1 ,:immortal ,:none)
)))
; Not used by the Sparc assembler; for information only.
(define $immediate-primops$
'((typetag-set! #x80)
(eq? #x81)
(+ #x82)
(- #x83)
(< #x84)
(<= #x85)
(= #x86)
(> #x87)
(>= #x88)
(char<? #x89)
(char<=? #x8a)
(char=? #x8b)
(char>? #x8c)
(char>=? #x8d)
(string-ref #x90)
(vector-ref #x91)
(bytevector-ref #x92)
(bytevector-like-ref -1)
(vector-like-ref -1)
(fx+ -1)
(fx- -1)
(fx-- -1)
(fx= -1)
(fx< -1)
(fx<= -1)
(fx> -1)
(fx>= -1)))
; Operations introduced by peephole optimizer.
(define $reg/op1/branchf ; reg/op1/branchf prim,k1,L
(make-mnemonic 'reg/op1/branchf))
(define $reg/op2/branchf ; reg/op2/branchf prim,k1,k2,L
(make-mnemonic 'reg/op2/branchf))
(define $reg/op2imm/branchf ; reg/op2imm/branchf prim,k1,x,L
(make-mnemonic 'reg/op2imm/branchf))
(define $reg/op1/check ; reg/op1/check prim,k1,k2,k3,k4,exn
(make-mnemonic 'reg/op1/check))
(define $reg/op2/check ; reg/op2/check prim,k1,k2,k3,k4,k5,exn
(make-mnemonic 'reg/op2/check))
(define $reg/op2imm/check ; reg/op2imm/check prim,k1,x,k2,k3,k4,exn
(make-mnemonic 'reg/op2imm/check))
(define $reg/op1/setreg ; reg/op1/setreg prim,k1,kr
(make-mnemonic 'reg/op1/setreg))
(define $reg/op2/setreg ; reg/op2/setreg prim,k1,k2,kr
(make-mnemonic 'reg/op2/setreg))
(define $reg/op2imm/setreg ; reg/op2imm/setreg prim,k1,x,kr
(make-mnemonic 'reg/op2imm/setreg))
(define $reg/branchf ; reg/branchf k, L
(make-mnemonic 'reg/branchf))
(define $reg/return ; reg/return k
(make-mnemonic 'reg/return))
(define $reg/setglbl ; reg/setglbl k,x
(make-mnemonic 'reg/setglbl))
(define $reg/op3 ; reg/op3 prim,k1,k2,k3
(make-mnemonic 'reg/op3))
(define $const/setreg ; const/setreg const,k
(make-mnemonic 'const/setreg))
(define $const/return ; const/return const
(make-mnemonic 'const/return))
(define $global/setreg ; global/setreg x,k
(make-mnemonic 'global/setreg))
(define $setrtn/branch ; setrtn/branch L,doc
(make-mnemonic 'setrtn/branch))
(define $setrtn/invoke ; setrtn/invoke L
(make-mnemonic 'setrtn/invoke))
(define $global/invoke ; global/invoke global,n
(make-mnemonic 'global/invoke))
; misc
(define $cons 'cons)
(define $car:pair 'car)
(define $cdr:pair 'cdr)
; eof
; Target-specific representations.
;
; A few of these representation types must be specified for every target:
; rep:object
; rep:procedure
; rep:true
; rep:false
; rep:bottom
(define-subtype 'true 'object) ; values that count as true
(define-subtype 'eqtype 'object) ; can use EQ? instead of EQV?
(define-subtype 'nonpointer 'eqtype) ; can omit write barrier
(define-subtype 'eqtype1 'eqtype) ; eqtypes excluding #f
(define-subtype 'boolean 'nonpointer)
(define-subtype 'truth 'eqtype1) ; { #t }
(define-subtype 'truth 'boolean)
(define-subtype 'false 'boolean) ; { #f }
(define-subtype 'eqtype1 'true)
(define-subtype 'procedure 'true)
(define-subtype 'vector 'true)
(define-subtype 'bytevector 'true)
(define-subtype 'string 'true)
(define-subtype 'pair 'true)
(define-subtype 'emptylist 'eqtype1)
(define-subtype 'emptylist 'nonpointer)
(define-subtype 'symbol 'eqtype1)
(define-subtype 'char 'eqtype1)
(define-subtype 'char 'nonpointer)
(define-subtype 'number 'true)
(define-subtype 'inexact 'number)
(define-subtype 'flonum 'inexact)
(define-subtype 'integer 'number)
(define-subtype 'exact 'number)
(define-subtype 'exactint 'integer)
(define-subtype 'exactint 'exact)
(define-subtype 'fixnum 'exactint)
(define-subtype '!fixnum 'fixnum) ; 0 <= n
(define-subtype 'fixnum! 'fixnum) ; n <= largest index
(define-subtype 'index '!fixnum)
(define-subtype 'index 'fixnum!)
(define-subtype 'zero 'index)
(define-subtype 'fixnum 'eqtype1)
(define-subtype 'fixnum 'nonpointer)
(compute-type-structure!)
; If the intersection of rep1 and rep2 is known precisely,
; but neither is a subtype of the other, then their intersection
; should be declared explicitly.
; Otherwise a conservative approximation will be used.
(define-intersection 'true 'eqtype 'eqtype1)
(define-intersection 'true 'boolean 'truth)
(define-intersection 'exact 'integer 'exactint)
(define-intersection '!fixnum 'fixnum! 'index)
;(display-unions-and-intersections)
; Parameters.
(define rep:min_fixnum (- (expt 2 29)))
(define rep:max_fixnum (- (expt 2 29) 1))
(define rep:max_index (- (expt 2 24) 1))
; The representations we'll recognize for now.
(define rep:object (symbol->rep 'object))
(define rep:true (symbol->rep 'true))
(define rep:truth (symbol->rep 'truth))
(define rep:false (symbol->rep 'false))
(define rep:boolean (symbol->rep 'boolean))
(define rep:pair (symbol->rep 'pair))
(define rep:symbol (symbol->rep 'symbol))
(define rep:number (symbol->rep 'number))
(define rep:zero (symbol->rep 'zero))
(define rep:index (symbol->rep 'index))
(define rep:fixnum (symbol->rep 'fixnum))
(define rep:exactint (symbol->rep 'exactint))
(define rep:flonum (symbol->rep 'flonum))
(define rep:exact (symbol->rep 'exact))
(define rep:inexact (symbol->rep 'inexact))
(define rep:integer (symbol->rep 'integer))
;(define rep:real (symbol->rep 'real))
(define rep:char (symbol->rep 'char))
(define rep:string (symbol->rep 'string))
(define rep:vector (symbol->rep 'vector))
(define rep:procedure (symbol->rep 'procedure))
(define rep:bottom (symbol->rep 'bottom))
; Given the value of a quoted constant, return its representation.
(define (representation-of-value x)
(cond ((boolean? x)
(if x
rep:truth
rep:false))
((pair? x)
rep:pair)
((symbol? x)
rep:symbol)
((number? x)
(cond ((and (exact? x)
(integer? x))
(cond ((zero? x)
rep:zero)
((<= 0 x rep:max_index)
rep:index)
((<= rep:min_fixnum
x
rep:max_fixnum)
rep:fixnum)
(else
rep:exactint)))
((and (inexact? x)
(real? x))
rep:flonum)
(else
; We're not tracking other numbers yet.
rep:number)))
((char? x)
rep:char)
((string? x)
rep:string)
((vector? x)
rep:vector)
; Everything counts as true except for #f.
(else
rep:true)))
; Tables that express the representation-specific operations,
; and the information about representations that are implied
; by certain operations.
; FIXME: Currently way incomplete, but good enough for testing.
(define rep-specific
(representation-table
; When the procedure in the first column is called with
; arguments described in the middle column, then the procedure
; in the last column can be called instead.
'(
;(+ (index index) +:idx:idx)
;(+ (fixnum fixnum) +:fix:fix)
;(- (index index) -:idx:idx)
;(- (fixnum fixnum) -:fix:fix)
(= (fixnum fixnum) =:fix:fix)
(< (fixnum fixnum) <:fix:fix)
(<= (fixnum fixnum) <=:fix:fix)
(> (fixnum fixnum) >:fix:fix)
(>= (fixnum fixnum) >=:fix:fix)
;(+ (flonum flonum) +:flo:flo)
;(- (flonum flonum) -:flo:flo)
;(= (flonum flonum) =:flo:flo)
;(< (flonum flonum) <:flo:flo)
;(<= (flonum flonum) <=:flo:flo)
;(> (flonum flonum) >:flo:flo)
;(>= (flonum flonum) >=:flo:flo)
;(vector-set!:trusted (vector fixnum nonpointer) vector-set!:trusted:imm)
)))
(define rep-result
(representation-table
; When the procedure in the first column is called with
; arguments described in the middle column, then the result
; is described by the last column.
'((fixnum? (fixnum) (truth))
(vector? (vector) (truth))
(<= (zero !fixnum) (truth))
(>= (!fixnum zero) (truth))
(<=:fix:fix (zero !fixnum) (truth))
(>=:fix:fix (!fixnum zero) (truth))
(+ (index index) (!fixnum))
(+ (fixnum fixnum) (exactint))
(- (index index) (fixnum!))
(- (fixnum fixnum) (exactint))
(+ (flonum flonum) (flonum))
(- (flonum flonum) (flonum))
;(+:idx:idx (index index) (!fixnum))
;(-:idx:idx (index index) (fixnum!))
;(+:fix:fix (index index) (exactint))
;(+:fix:fix (fixnum fixnum) (exactint))
;(-:idx:idx (index index) (fixnum))
;(-:fix:fix (fixnum fixnum) (exactint))
(make-vector (object object) (vector))
(vector-length:vec (vector) (index))
(cons (object object) (pair))
; Is it really all that useful to know that the result
; of these comparisons is a boolean?
(= (number number) (boolean))
(< (number number) (boolean))
(<= (number number) (boolean))
(> (number number) (boolean))
(>= (number number) (boolean))
(=:fix:fix (fixnum fixnum) (boolean))
(<:fix:fix (fixnum fixnum) (boolean))
(<=:fix:fix (fixnum fixnum) (boolean))
(>:fix:fix (fixnum fixnum) (boolean))
(>=:fix:fix (fixnum fixnum) (boolean))
)))
(define rep-informing
(representation-table
; When the predicate in the first column is called in the test position
; of a conditional expression, on arguments described by the second
; column, then the arguments are described by the third column if the
; predicate returns true, and by the fourth column if the predicate
; returns false.
'(
(fixnum? (object) (fixnum) (object))
(flonum? (object) (flonum) (object))
(vector? (object) (vector) (object))
(pair? (object) (pair) (object))
(= (exactint index) (index index) (exactint index))
(= (index exactint) (index index) (index exactint))
(= (exactint !fixnum) (!fixnum !fixnum) (exactint !fixnum))
(= (!fixnum exactint) (!fixnum !fixnum) (!fixnum exactint))
(= (exactint fixnum!) (fixnum! fixnum!) (exactint fixnum!))
(= (fixnum! exactint) (fixnum! fixnum!) (fixnum! exactint))
(< (!fixnum fixnum!) (index index) (!fixnum fixnum!))
(< (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
(< (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
(< (fixnum! !fixnum) (fixnum! !fixnum) (index index))
(<= (!fixnum fixnum!) (index index) (!fixnum fixnum!))
(<= (fixnum! !fixnum) (fixnum! !fixnum) (index index))
(<= (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
(<= (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
(> (!fixnum fixnum!) (!fixnum fixnum!) (index index))
(> (fixnum! !fixnum) (index index) (fixnum! !fixnum))
(> (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
(> (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
(>= (!fixnum fixnum!) (!fixnum fixnum!) (index index))
(>= (fixnum! !fixnum) (index index) (fixnum! !fixnum))
(>= (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
(>= (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
(=:fix:fix (exactint index) (index index) (exactint index))
(=:fix:fix (index exactint) (index index) (index exactint))
(=:fix:fix (exactint !fixnum) (!fixnum !fixnum) (exactint !fixnum))
(=:fix:fix (!fixnum exactint) (!fixnum !fixnum) (!fixnum exactint))
(=:fix:fix (exactint fixnum!) (fixnum! fixnum!) (exactint fixnum!))
(=:fix:fix (fixnum! exactint) (fixnum! fixnum!) (fixnum! exactint))
(<:fix:fix (!fixnum fixnum!) (index index) (!fixnum fixnum!))
(<:fix:fix (fixnum! !fixnum) (fixnum! !fixnum) (index index))
(<:fix:fix (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
(<:fix:fix (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
(<=:fix:fix (!fixnum fixnum!) (index index) (!fixnum fixnum!))
(<=:fix:fix (fixnum! !fixnum) (fixnum! !fixnum) (index index))
(<=:fix:fix (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
(<=:fix:fix (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
(>:fix:fix (!fixnum fixnum!) (!fixnum fixnum!) (index index))
(>:fix:fix (fixnum! !fixnum) (index index) (fixnum! !fixnum))
(>:fix:fix (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
(>:fix:fix (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
(>=:fix:fix (!fixnum fixnum!) (!fixnum fixnum!) (index index))
(>=:fix:fix (fixnum! !fixnum) (index index) (fixnum! !fixnum))
(>=:fix:fix (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
(>=:fix:fix (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
)))
; Copyright 1991 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 25 April 1999.
;
; Second pass of the Twobit compiler:
; single assignment analysis, local source transformations,
; assignment elimination, and lambda lifting.
; The code for assignment elimination and lambda lifting
; are in a separate file.
;
; This pass operates as a source-to-source transformation on
; expressions written in the subset of Scheme described by the
; following grammar, where the input and output expressions
; satisfy certain additional invariants described below.
;
; "X ..." means zero or more occurrences of X.
;
; L --> (lambda (I_1 ...)
; (begin D ...)
; (quote (R F G <decls> <doc>)
; E)
; | (lambda (I_1 ... . I_rest)
; (begin D ...)
; (quote (R F G <decls> <doc>))
; E)
; D --> (define I L)
; E --> (quote K) ; constants
; | (begin I) ; variable references
; | L ; lambda expressions
; | (E0 E1 ...) ; calls
; | (set! I E) ; assignments
; | (if E0 E1 E2) ; conditionals
; | (begin E0 E1 E2 ...) ; sequential expressions
; I --> <identifier>
;
; R --> ((I <references> <assignments> <calls>) ...)
; F --> (I ...)
; G --> (I ...)
;
; Invariants that hold for the input only:
; * There are no internal definitions.
; * No identifier containing an upper case letter is bound anywhere.
; (Change the "name:..." variables if upper case is preferred.)
; * No identifier is bound in more than one place.
; * Each R contains one entry for every identifier bound in the
; formal argument list and the internal definition list that
; precede it. Each entry contains a list of pointers to all
; references to the identifier, a list of pointers to all
; assignments to the identifier, and a list of pointers to all
; calls to the identifier.
; * Except for constants, the expression does not share structure
; with the original input or itself, except that the references
; and assignments in R are guaranteed to share structure with
; the expression. Thus the expression may be side effected, and
; side effects to references or assignments obtained through R
; are guaranteed to change the references or assignments pointed
; to by R.
;
; Invariants that hold for the output only:
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * Each R contains one entry for every identifier bound in the
; formal argument list and the internal definition list that
; precede it. Each entry contains a list of pointers to all
; references to the identifier, a list of pointers to all
; assignments to the identifier, and a list of pointers to all
; calls to the identifier.
; * For each lambda expression, the associated F is a list of all
; the identifiers that occur free in the body of that lambda
; expression, and possibly a few extra identifiers that were
; once free but have been removed by optimization.
; * For each lambda expression, the associated G is a subset of F
; that contains every identifier that occurs free within some
; inner lambda expression that escapes, and possibly a few that
; don't. (Assignment-elimination does not calculate G exactly.)
; * Variables named IGNORED are neither referenced nor assigned.
; * Except for constants, the expression does not share structure
; with the original input or itself, except that the references
; and assignments in R are guaranteed to share structure with
; the expression. Thus the expression may be side effected, and
; side effects to references or assignments obtained through R
; are guaranteed to change the references or assignments pointed
; to by R.
(define (pass2 exp)
(simplify exp (make-notepad #f)))
; Given an expression and a "notepad" data structure that conveys
; inherited attributes, performs the appropriate optimizations and
; destructively modifies the notepad to record various attributes
; that it synthesizes while traversing the expression. In particular,
; any nested lambda expressions and any variable references will be
; noted in the notepad.
(define (simplify exp notepad)
(case (car exp)
((quote) exp)
((lambda) (simplify-lambda exp notepad))
((set!) (simplify-assignment exp notepad))
((if) (simplify-conditional exp notepad))
((begin) (if (variable? exp)
(begin (notepad-var-add! notepad (variable.name exp))
exp)
(simplify-sequential exp notepad)))
(else (simplify-call exp notepad))))
; Most optimization occurs here.
; The right hand sides of internal definitions are simplified,
; as is the body.
; Internal definitions of enclosed lambda expressions may
; then be lifted to this one.
; Single assignment analysis creates internal definitions.
; Single assignment elimination converts single assignments
; to bindings where possible, and renames arguments whose value
; is ignored.
; Assignment elimination then replaces all remaining assigned
; variables by heap-allocated cells.
(define (simplify-lambda exp notepad)
(notepad-lambda-add! notepad exp)
(let ((defs (lambda.defs exp))
(body (lambda.body exp))
(newnotepad (make-notepad exp)))
(for-each (lambda (def)
(simplify-lambda (def.rhs def) newnotepad))
defs)
(lambda.body-set! exp (simplify body newnotepad))
(lambda.F-set! exp (notepad-free-variables newnotepad))
(lambda.G-set! exp (notepad-captured-variables newnotepad))
(single-assignment-analysis exp newnotepad)
(let ((known-lambdas (notepad.nonescaping newnotepad)))
(for-each (lambda (L)
(if (memq L known-lambdas)
(lambda-lifting L exp)
(lambda-lifting L L)))
(notepad.lambdas newnotepad))))
(single-assignment-elimination exp notepad)
(assignment-elimination exp)
(if (not (notepad.parent notepad))
; This is an outermost lambda expression.
(lambda-lifting exp exp))
exp)
; SIMPLIFY-ASSIGNMENT performs this transformation:
;
; (set! I (begin ... E))
; -> (begin ... (set! I E))
(define (simplify-assignment exp notepad)
(notepad-var-add! notepad (assignment.lhs exp))
(let ((rhs (simplify (assignment.rhs exp) notepad)))
(cond ((begin? rhs)
(let ((exprs (reverse (begin.exprs rhs))))
(assignment.rhs-set! exp (car exprs))
(post-simplify-begin
(make-begin (reverse (cons exp (cdr exprs))))
notepad)))
(else (assignment.rhs-set! exp rhs) exp))))
(define (simplify-sequential exp notepad)
(let ((exprs (map (lambda (exp) (simplify exp notepad))
(begin.exprs exp))))
(begin.exprs-set! exp exprs)
(post-simplify-begin exp notepad)))
; Given (BEGIN E0 E1 E2 ...) where the E_i are simplified expressions,
; flattens any nested BEGINs and removes trivial expressions that
; don't appear in the last position. The second argument is used only
; if a lambda expression is removed.
; This procedure is careful to return E instead of (BEGIN E).
; Fairly harmless bug: a variable reference removed by this procedure
; may remain on the notepad when it shouldn't.
(define (post-simplify-begin exp notepad)
(let ((unspecified-expression (make-unspecified)))
; (flatten exprs '()) returns the flattened exprs in reverse order.
(define (flatten exprs flattened)
(cond ((null? exprs) flattened)
((begin? (car exprs))
(flatten (cdr exprs)
(flatten (begin.exprs (car exprs)) flattened)))
(else (flatten (cdr exprs) (cons (car exprs) flattened)))))
(define (filter exprs filtered)
(if (null? exprs)
filtered
(let ((exp (car exprs)))
(cond ((constant? exp) (filter (cdr exprs) filtered))
((variable? exp) (filter (cdr exprs) filtered))
((lambda? exp)
(notepad.lambdas-set!
notepad
(remq exp (notepad.lambdas notepad)))
(filter (cdr exprs) filtered))
((equal? exp unspecified-expression)
(filter (cdr exprs) filtered))
(else (filter (cdr exprs) (cons exp filtered)))))))
(let ((exprs (flatten (begin.exprs exp) '())))
(begin.exprs-set! exp (filter (cdr exprs) (list (car exprs))))
(if (null? (cdr (begin.exprs exp)))
(car (begin.exprs exp))
exp))))
; SIMPLIFY-CALL performs this transformation:
;
; (... (begin ... E) ...)
; -> (begin ... (... E ...))
;
; It also takes care of LET transformations.
(define (simplify-call exp notepad)
(define (loop args newargs exprs)
(cond ((null? args)
(finish newargs exprs))
((begin? (car args))
(let ((newexprs (reverse (begin.exprs (car args)))))
(loop (cdr args)
(cons (car newexprs) newargs)
(append (cdr newexprs) exprs))))
(else (loop (cdr args) (cons (car args) newargs) exprs))))
(define (finish newargs exprs)
(call.args-set! exp (reverse newargs))
(let* ((newexp
(if (lambda? (call.proc exp))
(simplify-let exp notepad)
(begin
(call.proc-set! exp
(simplify (call.proc exp) notepad))
exp)))
(newexp
(if (and (call? newexp)
(variable? (call.proc newexp)))
(let* ((procname (variable.name (call.proc newexp)))
(args (call.args newexp))
(entry
(and (not (null? args))
(constant? (car args))
(integrate-usual-procedures)
(every? constant? args)
(let ((entry (constant-folding-entry procname)))
(and entry
(let ((predicates
(constant-folding-predicates entry)))
(and (= (length args)
(length predicates))
(let loop ((args args)
(predicates predicates))
(cond ((null? args) entry)
(((car predicates)
(constant.value
(car args)))
(loop (cdr args)
(cdr predicates)))
(else #f))))))))))
(if entry
(make-constant (apply (constant-folding-folder entry)
(map constant.value args)))
newexp))
newexp)))
(cond ((and (call? newexp)
(begin? (call.proc newexp)))
(let ((exprs0 (reverse (begin.exprs (call.proc newexp)))))
(call.proc-set! newexp (car exprs0))
(post-simplify-begin
(make-begin (reverse
(cons newexp
(append (cdr exprs0) exprs))))
notepad)))
((null? exprs)
newexp)
(else
(post-simplify-begin
(make-begin (reverse (cons newexp exprs)))
notepad)))))
(call.args-set! exp (map (lambda (arg) (simplify arg notepad))
(call.args exp)))
(loop (call.args exp) '() '()))
; SIMPLIFY-LET performs these transformations:
;
; ((lambda (I_1 ... I_k . I_rest) ---) E1 ... Ek Ek+1 ...)
; -> ((lambda (I_1 ... I_k I_rest) ---) E1 ... Ek (LIST Ek+1 ...))
;
; ((lambda (I1 I2 ...) (begin D ...) (quote ...) E) L ...)
; -> ((lambda (I2 ...) (begin (define I1 L) D ...) (quote ...) E) ...)
;
; provided I1 is not assigned and each reference to I1 is in call position.
;
; ((lambda (I1)
; (begin)
; (quote ((I1 ((begin I1)) () ())))
; (begin I1))
; E1)
;
; -> E1
;
; ((lambda (I1)
; (begin)
; (quote ((I1 ((begin I1)) () ())))
; (if (begin I1) E2 E3))
; E1)
;
; -> (if E1 E2 E3)
;
; (Together with SIMPLIFY-CONDITIONAL, this cleans up the output of the OR
; macro and enables certain control optimizations.)
;
; ((lambda (I1 I2 ...)
; (begin D ...)
; (quote (... (I <references> () <calls>) ...) ...)
; E)
; K ...)
; -> ((lambda (I2 ...)
; (begin D' ...)
; (quote (... ...) ...)
; E')
; ...)
;
; where D' ... and E' ... are obtained from D ... and E ...
; by replacing all references to I1 by K. This transformation
; applies if K is a constant that can be duplicated without changing
; its EQV? behavior.
;
; ((lambda () (begin) (quote ...) E)) -> E
;
; ((lambda (IGNORED I2 ...) ---) E1 E2 ...)
; -> (begin E1 ((lambda (I2 ...) ---) E2 ...))
;
; (Single assignment analysis, performed by the simplifier for lambda
; expressions, detects unused arguments and replaces them in the argument
; list by the special identifier IGNORED.)
(define (simplify-let exp notepad)
(define proc (call.proc exp))
; Loop1 operates before simplification of the lambda body.
(define (loop1 formals actuals processed-formals processed-actuals)
(cond ((null? formals)
(if (not (null? actuals))
(pass2-error p2error:wna exp))
(return1 processed-formals processed-actuals))
((symbol? formals)
(return1 (cons formals processed-formals)
(cons (make-call-to-LIST actuals) processed-actuals)))
((null? actuals)
(pass2-error p2error:wna exp)
(return1 processed-formals
processed-actuals))
((and (lambda? (car actuals))
(let ((Rinfo (R-lookup (lambda.R proc) (car formals))))
(and (null? (R-entry.assignments Rinfo))
(= (length (R-entry.references Rinfo))
(length (R-entry.calls Rinfo))))))
(let ((I (car formals))
(L (car actuals)))
(notepad-nonescaping-add! notepad L)
(lambda.defs-set! proc
(cons (make-definition I L)
(lambda.defs proc)))
(standardize-known-calls L
(R-entry.calls
(R-lookup (lambda.R proc) I)))
(lambda.F-set! proc (union (lambda.F proc)
(free-variables L)))
(lambda.G-set! proc (union (lambda.G proc) (lambda.G L))))
(loop1 (cdr formals)
(cdr actuals)
processed-formals
processed-actuals))
((and (constant? (car actuals))
(let ((x (constant.value (car actuals))))
(or (boolean? x)
(number? x)
(symbol? x)
(char? x))))
(let* ((I (car formals))
(Rinfo (R-lookup (lambda.R proc) I)))
(if (null? (R-entry.assignments Rinfo))
(begin
(for-each (lambda (ref)
(variable-set! ref (car actuals)))
(R-entry.references Rinfo))
(lambda.R-set! proc (remq Rinfo (lambda.R proc)))
(lambda.F-set! proc (remq I (lambda.F proc)))
(lambda.G-set! proc (remq I (lambda.G proc)))
(loop1 (cdr formals)
(cdr actuals)
processed-formals
processed-actuals))
(loop1 (cdr formals)
(cdr actuals)
(cons (car formals) processed-formals)
(cons (car actuals) processed-actuals)))))
(else (if (null? actuals)
(pass2-error p2error:wna exp))
(loop1 (cdr formals)
(cdr actuals)
(cons (car formals) processed-formals)
(cons (car actuals) processed-actuals)))))
(define (return1 rev-formals rev-actuals)
(let ((formals (reverse rev-formals))
(actuals (reverse rev-actuals)))
(lambda.args-set! proc formals)
(if (and (not (null? formals))
(null? (cdr formals))
(let* ((x (car formals))
(R (lambda.R proc))
(refs (references R x)))
(and (= 1 (length refs))
(null? (assignments R x)))))
(let ((x (car formals))
(body (lambda.body proc)))
(cond ((and (variable? body)
(eq? x (variable.name body)))
(simplify (car actuals) notepad))
((and (conditional? body)
(let ((B0 (if.test body)))
(variable? B0)
(eq? x (variable.name B0))))
(if.test-set! body (car actuals))
(simplify body notepad))
(else
(return1-finish formals actuals))))
(return1-finish formals actuals))))
(define (return1-finish formals actuals)
(simplify-lambda proc notepad)
(loop2 formals actuals '() '() '()))
; Loop2 operates after simplification of the lambda body.
(define (loop2 formals actuals processed-formals processed-actuals for-effect)
(cond ((null? formals)
(return2 processed-formals processed-actuals for-effect))
((ignored? (car formals))
(loop2 (cdr formals)
(cdr actuals)
processed-formals
processed-actuals
(cons (car actuals) for-effect)))
(else (loop2 (cdr formals)
(cdr actuals)
(cons (car formals) processed-formals)
(cons (car actuals) processed-actuals)
for-effect))))
(define (return2 rev-formals rev-actuals rev-for-effect)
(let ((formals (reverse rev-formals))
(actuals (reverse rev-actuals))
(for-effect (reverse rev-for-effect)))
(lambda.args-set! proc formals)
(call.args-set! exp actuals)
(let ((exp (if (and (null? actuals)
(or (null? (lambda.defs proc))
(and (notepad.parent notepad)
(POLICY:LIFT? proc
(notepad.parent notepad)
(map (lambda (def) '())
(lambda.defs proc))))))
(begin (for-each (lambda (I)
(notepad-var-add! notepad I))
(lambda.F proc))
(if (not (null? (lambda.defs proc)))
(let ((parent (notepad.parent notepad))
(defs (lambda.defs proc))
(R (lambda.R proc)))
(lambda.defs-set!
parent
(append defs (lambda.defs parent)))
(lambda.defs-set! proc '())
(lambda.R-set!
parent
(append (map (lambda (def)
(R-lookup R (def.lhs def)))
defs)
(lambda.R parent)))))
(lambda.body proc))
exp)))
(if (null? for-effect)
exp
(post-simplify-begin (make-begin (append for-effect (list exp)))
notepad)))))
(notepad-nonescaping-add! notepad proc)
(loop1 (lambda.args proc) (call.args exp) '() '()))
; Single assignment analysis performs the transformation
;
; (lambda (... I ...)
; (begin D ...)
; (quote (... (I <references> ((set! I L)) <calls>) ...) ...)
; (begin (set! I L) E1 ...))
; -> (lambda (... IGNORED ...)
; (begin (define I L) D ...)
; (quote (... (I <references> () <calls>) ...) ...)
; (begin E1 ...))
;
; For best results, pass 1 should sort internal definitions and LETRECs so
; that procedure definitions/bindings come first.
;
; This procedure operates by side effect.
(define (single-assignment-analysis L notepad)
(let ((formals (lambda.args L))
(defs (lambda.defs L))
(R (lambda.R L))
(body (lambda.body L)))
(define (finish! exprs escapees)
(begin.exprs-set! body
(append (reverse escapees)
exprs))
(lambda.body-set! L (post-simplify-begin body '())))
(if (begin? body)
(let loop ((exprs (begin.exprs body))
(escapees '()))
(let ((first (car exprs)))
(if (and (assignment? first)
(not (null? (cdr exprs))))
(let ((I (assignment.lhs first))
(rhs (assignment.rhs first)))
(if (and (lambda? rhs)
(local? R I)
(= 1 (length (assignments R I))))
(if (= (length (calls R I))
(length (references R I)))
(begin (notepad-nonescaping-add! notepad rhs)
(flag-as-ignored I L)
(lambda.defs-set! L
(cons (make-definition I rhs)
(lambda.defs L)))
(assignments-set! R I '())
(standardize-known-calls
rhs
(R-entry.calls (R-lookup R I)))
(loop (cdr exprs) escapees))
(loop (cdr exprs)
(cons (car exprs) escapees)))
(finish! exprs escapees)))
(finish! exprs escapees)))))))
(define (standardize-known-calls L calls)
(let ((formals (lambda.args L)))
(cond ((not (list? formals))
(let* ((newformals (make-null-terminated formals))
(n (- (length newformals) 1)))
(lambda.args-set! L newformals)
(for-each (lambda (call)
(if (>= (length (call.args call)) n)
(call.args-set!
call
(append (list-head (call.args call) n)
(list
(make-call-to-LIST
(list-tail (call.args call) n)))))
(pass2-error p2error:wna call)))
calls)))
(else (let ((n (length formals)))
(for-each (lambda (call)
(if (not (= (length (call.args call)) n))
(pass2-error p2error:wna call)))
calls))))))
; Copyright 1991 William D Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 13 November 1998
;
; Second pass of the Twobit compiler, part 2:
; single assignment elimination, assignment elimination,
; and lambda lifting.
;
; See part 1 for further documentation.
; Single assignment elimination performs the transformation
;
; (lambda (... I1 ... In ...)
; (begin D ...)
; (begin (set! I1 E1)
; ...
; (set! In En)
; E ...))
; -> (lambda (... IGNORED ... IGNORED ...)
; (let* ((I1 E1) ... (In En))
; (begin D ...)
; (begin E ...)))
;
; provided for each k:
;
; 1. Ik does not occur in E1, ..., Ek.
; 2. Either E1 through Ek contain no procedure calls
; or Ik is not referenced by an escaping lambda expression.
; 3. Ik is assigned only once.
;
; I doubt whether the third condition is really necessary, but
; dropping it would involve a more complex calculation of the
; revised referencing information.
;
; A more precise description of the transformation:
;
; (lambda (... I1 ... In ...)
; (begin (define F1 L1) ...)
; (quote (... (I1 <references> ((set! I1 E1)) <calls>) ...
; (In <references> ((set! In En)) <calls>)
; (F1 <references> () <calls>) ...) ...)
; (begin (set! I1 E1) ... (set! In En) E ...))
; -> (lambda (... IGNORED ... IGNORED ...)
; (begin)
; (quote (...) ...)
; ((lambda (I1)
; (begin)
; (quote ((I1 <references> () <calls>)) ...)
; ...
; ((lambda (In)
; (begin (define F1 L1) ...)
; (quote (... (In <references> () <calls>)
; (F1 <references> () <calls>) ...) ...)
; (begin E ...))
; En)
; ...)
; E1))
;
; For best results, pass 1 should sort internal definitions and LETRECs
; so that procedure definitions/bindings come first, followed by
; definitions/bindings whose right hand side contains no calls,
; followed by definitions/bindings of variables that do not escape,
; followed by all other definitions/bindings.
;
; Pass 1 can't tell which variables escape, however. Pass 2 can't tell
; which variables escape either until all enclosed lambda expressions
; have been simplified and the first transformation above has been
; performed. That is why single assignment analysis precedes single
; assignment elimination. As implemented here, an assignment that does
; not satisfy the conditions above will prevent the transformation from
; being applied to any subsequent assignments.
;
; This procedure operates by side effect.
(define (single-assignment-elimination L notepad)
(if (begin? (lambda.body L))
(let* ((formals (make-null-terminated (lambda.args L)))
(defined (map def.lhs (lambda.defs L)))
(escaping (intersection formals
(notepad-captured-variables notepad)))
(R (lambda.R L)))
; Given:
; exprs that remain in the body;
; assigns that will be replaced by let* variables;
; call-has-occurred?, a boolean;
; free variables of the assigns;
; Performs the transformation described above.
(define (loop exprs assigns call-has-occurred? free)
(cond ((null? (cdr exprs))
(return exprs assigns))
((assignment? (car exprs))
(let ((I1 (assignment.lhs (car exprs)))
(E1 (assignment.rhs (car exprs))))
(if (and (memq I1 formals)
(= (length (assignments R I1)) 1)
(not (and call-has-occurred?
(memq I1 escaping))))
(let* ((free-in-E1 (free-variables E1))
(newfree (union free-in-E1 free)))
(if (or (memq I1 newfree)
(not
(empty-set?
(intersection free-in-E1 defined))))
(return exprs assigns)
(loop (cdr exprs)
(cons (car exprs) assigns)
(or call-has-occurred?
(might-return-twice? E1))
newfree)))
(return exprs assigns))))
(else (return exprs assigns))))
(define (return exprs assigns)
(if (not (null? assigns))
(let ((I (assignment.lhs (car assigns)))
(E (assignment.rhs (car assigns)))
(defs (lambda.defs L))
(F (lambda.F L))
(G (lambda.G L)))
(flag-as-ignored I L)
(assignments-set! R I '())
(let ((L2 (make-lambda (list I)
defs
(cons (R-entry R I)
(map (lambda (def)
(R-entry R (def.lhs def)))
defs))
F
G
(lambda.decls L)
(lambda.doc L)
(make-begin exprs))))
(lambda.defs-set! L '())
(for-each (lambda (entry)
(lambda.R-set! L (remq entry R)))
(lambda.R L2))
(return-loop (cdr assigns) (make-call L2 (list E)))))))
(define (return-loop assigns body)
(if (null? assigns)
(let ((L3 (call.proc body)))
(lambda.body-set! L body)
(lambda-lifting L3 L))
(let* ((I (assignment.lhs (car assigns)))
(E (assignment.rhs (car assigns)))
(L3 (call.proc body))
(F (remq I (lambda.F L3)))
(G (remq I (lambda.G L3))))
(flag-as-ignored I L)
(assignments-set! R I '())
(let ((L2 (make-lambda (list I)
'()
(list (R-entry R I))
F
G
(lambda.decls L)
(lambda.doc L)
body)))
(lambda.R-set! L (remq (R-entry R I) R))
(lambda-lifting L3 L2)
(return-loop (cdr assigns) (make-call L2 (list E)))))))
(loop (begin.exprs (lambda.body L)) '() #f '())))
L)
; Temporary definitions.
(define (free-variables exp)
(case (car exp)
((quote) '())
((lambda) (difference (lambda.F exp)
(make-null-terminated (lambda.args exp))))
((set!) (union (list (assignment.lhs exp))
(free-variables (assignment.rhs exp))))
((if) (union (free-variables (if.test exp))
(free-variables (if.then exp))
(free-variables (if.else exp))))
((begin) (if (variable? exp)
(list (variable.name exp))
(apply union (map free-variables (begin.exprs exp)))))
(else (apply union (map free-variables exp)))))
(define (might-return-twice? exp)
(case (car exp)
((quote) #f)
((lambda) #f)
((set!) (might-return-twice? (assignment.rhs exp)))
((if) (or (might-return-twice? (if.test exp))
(might-return-twice? (if.then exp))
(might-return-twice? (if.else exp))))
((begin) (if (variable? exp)
#f
(some? might-return-twice? (begin.exprs exp))))
(else #t)))
; Assignment elimination replaces variables that appear on the left
; hand side of an assignment by data structures. This is necessary
; to avoid some nasty complications with lambda lifting.
;
; This procedure operates by side effect.
(define (assignment-elimination L)
(let ((R (lambda.R L)))
; Given a list of entries, return those for assigned variables.
(define (loop entries assigned)
(cond ((null? entries)
(if (not (null? assigned))
(eliminate assigned)))
((not (null? (R-entry.assignments (car entries))))
(loop (cdr entries) (cons (car entries) assigned)))
((null? (R-entry.references (car entries)))
(flag-as-ignored (R-entry.name (car entries)) L)
(loop (cdr entries) assigned))
(else (loop (cdr entries) assigned))))
; Given a list of entries for assigned variables I1 ...,
; remove the assignments by replacing the body by a LET of the form
; ((LAMBDA (V1 ...) ...) (MAKE-CELL I1) ...), by replacing references
; by calls to CELL-REF, and by replacing assignments by calls to
; CELL-SET!.
(define (eliminate assigned)
(let* ((oldnames (map R-entry.name assigned))
(newnames (map generate-new-name oldnames)))
(let ((augmented-entries (map list newnames assigned))
(renaming-alist (map cons oldnames newnames))
(defs (lambda.defs L)))
(for-each cellify! augmented-entries)
(for-each (lambda (def)
(do ((free (lambda.F (def.rhs def)) (cdr free)))
((null? free))
(let ((z (assq (car free) renaming-alist)))
(if z
(set-car! free (cdr z))))))
defs)
(let ((newbody
(make-call
(make-lambda (map car augmented-entries)
defs
(union (map (lambda (def)
(R-entry R (def.lhs def)))
defs)
(map new-reference-info augmented-entries))
(union (list name:CELL-REF name:CELL-SET!)
newnames
(difference (lambda.F L) oldnames))
(union (list name:CELL-REF name:CELL-SET!)
newnames
(difference (lambda.G L) oldnames))
(lambda.decls L)
(lambda.doc L)
(lambda.body L))
(map (lambda (name)
(make-call (make-variable name:MAKE-CELL)
(list (make-variable name))))
(map R-entry.name assigned)))))
(lambda.F-set! L (union (list name:MAKE-CELL name:CELL-REF name:CELL-SET!)
(difference (lambda.F L)
(map def.lhs (lambda.defs L)))))
(lambda.defs-set! L '())
(for-each update-old-reference-info!
(map (lambda (arg)
(car (call.args arg)))
(call.args newbody)))
(lambda.body-set! L newbody)
(lambda-lifting (call.proc newbody) L)))))
(define (generate-new-name name)
(string->symbol (string-append cell-prefix (symbol->string name))))
; In addition to replacing references and assignments involving the
; old variable by calls to CELL-REF and CELL-SET! on the new, CELLIFY!
; uses the old entry to collect the referencing information for the
; new variable.
(define (cellify! augmented-entry)
(let ((newname (car augmented-entry))
(entry (cadr augmented-entry)))
(do ((refs (R-entry.references entry)
(cdr refs)))
((null? refs))
(let* ((reference (car refs))
(newref (make-variable newname)))
(set-car! reference (make-variable name:CELL-REF))
(set-car! (cdr reference) newref)
(set-car! refs newref)))
(do ((assigns (R-entry.assignments entry)
(cdr assigns)))
((null? assigns))
(let* ((assignment (car assigns))
(newref (make-variable newname)))
(set-car! assignment (make-variable name:CELL-SET!))
(set-car! (cdr assignment) newref)
(R-entry.references-set! entry
(cons newref
(R-entry.references entry)))))
(R-entry.assignments-set! entry '())))
; This procedure creates a brand new entry for a new variable, extracting
; the references stored in the old entry by CELLIFY!.
(define (new-reference-info augmented-entry)
(make-R-entry (car augmented-entry)
(R-entry.references (cadr augmented-entry))
'()
'()))
; This procedure updates the old entry to reflect the fact that it is
; now referenced once and never assigned.
(define (update-old-reference-info! ref)
(references-set! R (variable.name ref) (list ref))
(assignments-set! R (variable.name ref) '())
(calls-set! R (variable.name ref) '()))
(loop R '())))
; Lambda lifting raises internal definitions to outer scopes to avoid
; having to choose between creating a closure or losing tail recursion.
; If L is not #f, then L2 is a lambda expression nested within L.
; Any internal definitions that occur within L2 may be lifted to L
; by adding extra arguments to the defined procedure and to all calls to it.
; Lambda lifting is not a clear win, because the extra arguments could
; easily become more expensive than creating a closure and referring
; to the non-local arguments through the closure. The heuristics used
; to decide whether to lift a group of internal definitions are isolated
; within the POLICY:LIFT? procedure.
; L2 can be the same as L, so the order of side effects is critical.
(define (lambda-lifting L2 L)
; The call to sort is optional. It gets the added arguments into
; the same order they appear in the formals list, which is an
; advantage for register targeting.
(define (lift L2 L args-to-add)
(let ((formals (make-null-terminated (lambda.args L2))))
(do ((defs (lambda.defs L2) (cdr defs))
(args-to-add args-to-add (cdr args-to-add)))
((null? defs))
(let* ((def (car defs))
(entry (R-lookup (lambda.R L2) (def.lhs def)))
(calls (R-entry.calls entry))
(added (twobit-sort (lambda (x y)
(let ((xx (memq x formals))
(yy (memq y formals)))
(if (and xx yy)
(> (length xx) (length yy))
#t)))
(car args-to-add)))
(L3 (def.rhs def)))
; The flow equation guarantees that these added arguments
; will occur free by the time this round of lifting is done.
(lambda.F-set! L3 (union added (lambda.F L3)))
(lambda.args-set! L3 (append added (lambda.args L3)))
(for-each (lambda (call)
(let ((newargs (map make-variable added)))
; The referencing information is made obsolete here!
(call.args-set! call
(append newargs (call.args call)))))
calls)
(lambda.R-set! L2 (remq entry (lambda.R L2)))
(lambda.R-set! L (cons entry (lambda.R L)))
))
(if (not (eq? L2 L))
(begin
(lambda.defs-set! L (append (lambda.defs L2) (lambda.defs L)))
(lambda.defs-set! L2 '())))))
(if L
(if (not (null? (lambda.defs L2)))
(let ((args-to-add (compute-added-arguments
(lambda.defs L2)
(make-null-terminated (lambda.args L2)))))
(if (POLICY:LIFT? L2 L args-to-add)
(lift L2 L args-to-add))))))
; Given a list of definitions ((define f1 ...) ...) and a set of formals
; N over which the definitions may be lifted, returns a list of the
; subsets of N that need to be added to each procedure definition
; as new arguments.
;
; Algorithm: Let F_i be the variables that occur free in the body of
; the lambda expression associated with f_i. Construct the call graph.
; Solve the flow equations
;
; A_i = (F_i /\ N) \/ (\/ {A_j | A_i calls A_j})
;
; where /\ is intersection and \/ is union.
(define (compute-added-arguments defs formals)
(let ((procs (map def.lhs defs))
(freevars (map lambda.F (map def.rhs defs))))
(let ((callgraph (map (lambda (names)
(map (lambda (name)
(position name procs))
(intersection names procs)))
freevars))
(added_0 (map (lambda (names)
(intersection names formals))
freevars)))
(vector->list
(compute-fixedpoint
(make-vector (length procs) '())
(list->vector (map (lambda (term0 indexes)
(lambda (approximations)
(union term0
(apply union
(map (lambda (i)
(vector-ref approximations i))
indexes)))))
added_0
callgraph))
set-equal?)))))
(define (position x l)
(cond ((eq? x (car l)) 0)
(else (+ 1 (position x (cdr l))))))
; Given a vector of starting approximations,
; a vector of functions that compute a next approximation
; as a function of the vector of approximations,
; and an equality predicate,
; returns a vector of fixed points.
(define (compute-fixedpoint v functions equiv?)
(define (loop i flag)
(if (negative? i)
(if flag
(loop (- (vector-length v) 1) #f)
v)
(let ((next_i ((vector-ref functions i) v)))
(if (equiv? next_i (vector-ref v i))
(loop (- i 1) flag)
(begin (vector-set! v i next_i)
(loop (- i 1) #t))))))
(loop (- (vector-length v) 1) #f))
; Given a lambda expression L2, its parent lambda expression
; L (which may be the same as L2, or #f), and a list of the
; lists of arguments that would need to be added to known
; local procedures, returns #t iff lambda lifting should be done.
;
; Here are some heuristics:
;
; Don't lift if it means adding too many arguments.
; Don't lift large groups of definitions.
; In questionable cases it is better to lift to an outer
; lambda expression that already contains internal
; definitions than to one that doesn't.
; It is better not to lift if the body contains a lambda
; expression that has to be closed anyway.
(define (POLICY:LIFT? L2 L args-to-add)
(and (lambda-optimizations)
(not (lambda? (lambda.body L2)))
(every? (lambda (addlist)
(< (length addlist) 6))
args-to-add)))
; Copyright 1991 William D Clinger (for SIMPLIFY-CONDITIONAL)
; Copyright 1999 William D Clinger (for everything else)
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 11 April 1999.
;
; Some source transformations on IF expressions:
;
; (if '#f E1 E2) E2
; (if 'K E1 E2) E1 K != #f
; (if (if B0 '#f '#f) E1 E2) (begin B0 E2)
; (if (if B0 '#f 'K ) E1 E2) (if B0 E2 E1) K != #f
; (if (if B0 'K '#f) E1 E2) (if B0 E1 E2) K != #f
; (if (if B0 'K1 'K2) E1 E2) (begin B0 E1) K1, K2 != #f
; (if (if B0 (if B1 #t #f) B2) E1 E2) (if (if B0 B1 B2) E1 E2)
; (if (if B0 B1 (if B2 #t #f)) E1 E2) (if (if B0 B1 B2) E1 E2)
; (if (if X X B0 ) E1 E2) (if (if X #t B0) E1 E2) X a variable
; (if (if X B0 X ) E1 E2) (if (if X B0 #f) E1 E2) X a variable
; (if ((lambda (X) (if ((lambda (X)
; (if X X B2)) B0) (if X #t (if B2 #t #f))) B0)
; E1 E2) E1 E2)
; (if (begin ... B0) E1 E2) (begin ... (if B0 E1 E2))
; (if (not E0) E1 E2) (if E0 E2 E1) not is integrable
;
; FIXME: Three of the transformations above are intended to clean up
; the output of the OR macro. It isn't yet clear how well this works.
(define (simplify-conditional exp notepad)
(define (coercion-to-boolean? exp)
(and (conditional? exp)
(let ((E1 (if.then exp))
(E2 (if.else exp)))
(and (constant? E1)
(eq? #t (constant.value E1))
(constant? E2)
(eq? #f (constant.value E2))))))
(if (not (control-optimization))
(begin (if.test-set! exp (simplify (if.test exp) notepad))
(if.then-set! exp (simplify (if.then exp) notepad))
(if.else-set! exp (simplify (if.else exp) notepad))
exp)
(let* ((test (if.test exp)))
(if (and (call? test)
(lambda? (call.proc test))
(let* ((L (call.proc test))
(body (lambda.body L)))
(and (conditional? body)
(let ((R (lambda.R L))
(B0 (if.test body))
(B1 (if.then body)))
(and (variable? B0)
(variable? B1)
(let ((x (variable.name B0)))
(and (eq? x (variable.name B1))
(local? R x)
(= 1 (length R))
(= 1 (length (call.args test))))))))))
(let* ((L (call.proc test))
(R (lambda.R L))
(body (lambda.body L))
(ref (if.then body))
(x (variable.name ref))
(entry (R-entry R x)))
(if.then-set! body (make-constant #t))
(if.else-set! body
(make-conditional (if.else body)
(make-constant #t)
(make-constant #f)))
(R-entry.references-set! entry
(remq ref
(R-entry.references entry)))
(simplify-conditional exp notepad))
(let loop ((test (simplify (if.test exp) notepad)))
(if.test-set! exp test)
(cond ((constant? test)
(simplify (if (constant.value test)
(if.then exp)
(if.else exp))
notepad))
((and (conditional? test)
(constant? (if.then test))
(constant? (if.else test)))
(cond ((and (constant.value (if.then test))
(constant.value (if.else test)))
(post-simplify-begin
(make-begin (list (if.test test)
(simplify (if.then exp)
notepad)))
notepad))
((and (not (constant.value (if.then test)))
(not (constant.value (if.else test))))
(post-simplify-begin
(make-begin (list (if.test test)
(simplify (if.else exp)
notepad)))
notepad))
(else (if (not (constant.value (if.then test)))
(let ((temp (if.then exp)))
(if.then-set! exp (if.else exp))
(if.else-set! exp temp)))
(if.test-set! exp (if.test test))
(loop (if.test exp)))))
((and (conditional? test)
(or (coercion-to-boolean? (if.then test))
(coercion-to-boolean? (if.else test))))
(if (coercion-to-boolean? (if.then test))
(if.then-set! test (if.test (if.then test)))
(if.else-set! test (if.test (if.else test))))
(loop test))
((and (conditional? test)
(variable? (if.test test))
(let ((x (variable.name (if.test test))))
(or (and (variable? (if.then test))
(eq? x (variable.name (if.then test)))
1)
(and (variable? (if.else test))
(eq? x (variable.name (if.else test)))
2))))
=>
(lambda (n)
(case n
((1) (if.then-set! test (make-constant #t)))
((2) (if.else-set! test (make-constant #f))))
(loop test)))
((begin? test)
(let ((exprs (reverse (begin.exprs test))))
(if.test-set! exp (car exprs))
(post-simplify-begin
(make-begin (reverse (cons (loop (car exprs))
(cdr exprs))))
notepad)))
((and (call? test)
(variable? (call.proc test))
(eq? (variable.name (call.proc test)) name:NOT)
(integrable? name:NOT)
(integrate-usual-procedures)
(= (length (call.args test)) 1))
(let ((temp (if.then exp)))
(if.then-set! exp (if.else exp))
(if.else-set! exp temp))
(loop (car (call.args test))))
(else
(simplify-case exp notepad))))))))
; Given a conditional expression whose test has been simplified,
; simplifies the then and else parts while applying optimizations
; for CASE expressions.
; Precondition: (control-optimization) is true.
(define (simplify-case exp notepad)
(let ((E0 (if.test exp)))
(if (and (call? E0)
(variable? (call.proc E0))
(let ((name (variable.name (call.proc E0))))
; FIXME: Should ensure that the name is integrable,
; but MEMQ and MEMV probably aren't according to the
; INTEGRABLE? predicate.
(or (eq? name name:EQ?)
(eq? name name:EQV?)
(eq? name name:MEMQ)
(eq? name name:MEMV)))
(integrate-usual-procedures)
(= (length (call.args E0)) 2)
(variable? (car (call.args E0)))
(constant? (cadr (call.args E0))))
(simplify-case-clauses (variable.name (car (call.args E0)))
exp
notepad)
(begin (if.then-set! exp (simplify (if.then exp) notepad))
(if.else-set! exp (simplify (if.else exp) notepad))
exp))))
; Code generation for case expressions.
;
; A case expression turns into a conditional expression
; of the form
;
; CASE{I} ::= E | (if (PRED I K) E CASE{I})
; PRED ::= memv | memq | eqv? | eq?
;
; The memq and eq? predicates are used when the constant
; is a (list of) boolean, fixnum, char, empty list, or symbol.
; The constants will almost always be of these types.
;
; The first step is to remove duplicated constants and to
; collect all the case clauses, sorting them into the following
; categories based on their simplified list of constants:
; constants are fixnums
; constants are characters
; constants are symbols
; constants are of mixed or other type
; After duplicated constants have been removed, the predicates
; for these clauses can be tested in any order.
; Given the name of an arbitrary variable, an expression that
; has not yet been simplified or can safely be simplified again,
; and a notepad, returns the expression after simplification.
; If the expression is equivalent to a case expression that dispatches
; on the given variable, then case-optimization will be applied.
(define (simplify-case-clauses var0 E notepad)
(define notepad2 (make-notepad (notepad.parent notepad)))
(define (collect-clauses E fix chr sym other constants)
(if (not (conditional? E))
(analyze (simplify E notepad2)
fix chr sym other constants)
(let ((test (simplify (if.test E) notepad2))
(code (simplify (if.then E) notepad2)))
(if.test-set! E test)
(if.then-set! E code)
(if (not (call? test))
(finish E fix chr sym other constants)
(let ((proc (call.proc test))
(args (call.args test)))
(if (not (and (variable? proc)
(let ((name (variable.name proc)))
; FIXME: See note above.
(or (eq? name name:EQ?)
(eq? name name:EQV?)
(eq? name name:MEMQ)
(eq? name name:MEMV)))
(= (length args) 2)
(variable? (car args))
(eq? (variable.name (car args)) var0)
(constant? (cadr args))))
(finish E fix chr sym other constants)
(let ((pred (variable.name proc))
(datum (constant.value (cadr args))))
; FIXME
(if (or (and (or (eq? pred name:MEMV)
(eq? pred name:MEMQ))
(not (list? datum)))
(and (eq? pred name:EQ?)
(not (eqv-is-ok? datum)))
(and (eq? pred name:MEMQ)
(not (every? (lambda (datum)
(eqv-is-ok? datum))
datum))))
(finish E fix chr sym other constants)
(call-with-values
(lambda ()
(remove-duplicates (if (or (eq? pred name:EQV?)
(eq? pred name:EQ?))
(list datum)
datum)
constants))
(lambda (data constants)
(let ((clause (list data code))
(E2 (if.else E)))
(cond ((every? smallint? data)
(collect-clauses E2
(cons clause fix)
chr
sym
other
constants))
((every? char? data)
(collect-clauses E2
fix
(cons clause chr)
sym
other
constants))
((every? symbol? data)
(collect-clauses E2
fix
chr
(cons clause sym)
other
constants))
(else
(collect-clauses E2
fix
chr
sym
(cons clause other)
constants))))))))))))))
(define (remove-duplicates data set)
(let loop ((originals data)
(data '())
(set set))
(if (null? originals)
(values data set)
(let ((x (car originals))
(originals (cdr originals)))
(if (memv x set)
(loop originals data set)
(loop originals (cons x data) (cons x set)))))))
(define (finish E fix chr sym other constants)
(if.else-set! E (simplify (if.else E) notepad2))
(analyze E fix chr sym other constants))
(define (analyze default fix chr sym other constants)
(notepad-var-add! notepad2 var0)
(for-each (lambda (L)
(notepad-lambda-add! notepad L))
(notepad.lambdas notepad2))
(for-each (lambda (L)
(notepad-nonescaping-add! notepad L))
(notepad.nonescaping notepad2))
(for-each (lambda (var)
(notepad-var-add! notepad var))
(append (list name:FIXNUM?
name:CHAR?
name:SYMBOL?
name:FX<
name:FX-
name:CHAR->INTEGER
name:VECTOR-REF)
(notepad.vars notepad2)))
(analyze-clauses (notepad.vars notepad2)
var0
default
(reverse fix)
(reverse chr)
(reverse sym)
(reverse other)
constants))
(collect-clauses E '() '() '() '() '()))
; Returns true if EQ? and EQV? behave the same on x.
(define (eqv-is-ok? x)
(or (smallint? x)
(char? x)
(symbol? x)
(boolean? x)))
; Returns true if EQ? and EQV? behave the same on x.
(define (eq-is-ok? x)
(eqv-is-ok? x))
; Any case expression that dispatches on a variable var0 and whose
; constants are disjoint can be compiled as
;
; (let ((n (cond ((eq? var0 'K1) ...) ; miscellaneous constants
; ...
; ((fixnum? var0)
; <dispatch-on-fixnum>)
; ((char? var0)
; <dispatch-on-char>)
; ((symbol? var0)
; <dispatch-on-symbols>)
; (else 0))))
; <dispatch-on-case-number>)
;
; where the <dispatch-on-case-number> uses binary search within
; the interval [0, p+1), where p is the number of non-default cases.
;
; On the SPARC, sequential search is faster if there are fewer than
; 8 constants, and sequential search uses less than half the space
; if there are fewer than 10 constants. Most target machines should
; similar, so I'm hard-wiring this constant.
; FIXME: The hardwired constant is annoying.
(define (analyze-clauses F var0 default fix chr sym other constants)
(cond ((or (and (null? fix)
(null? chr))
(< (length constants) 12))
(implement-clauses-by-sequential-search var0
default
(append fix chr sym other)))
(else
(implement-clauses F var0 default fix chr sym other constants))))
; Implements the general technique described above.
(define (implement-clauses F var0 default fix chr sym other constants)
(let* ((name:n ((make-rename-procedure) 'n))
; Referencing information is destroyed by pass 2.
(entry (make-R-entry name:n '() '() '()))
(F (union (make-set (list name:n)) F))
(L (make-lambda
(list name:n)
'()
'() ; entry
F
'()
'()
#f
(implement-case-dispatch
name:n
(cons default
(map cadr
; The order here must match the order
; used by IMPLEMENT-DISPATCH.
(append other fix chr sym)))))))
(make-call L
(list (implement-dispatch 0
var0
(map car other)
(map car fix)
(map car chr)
(map car sym))))))
(define (implement-case-dispatch var0 exprs)
(implement-intervals var0
(map (lambda (n code)
(list n (+ n 1) code))
(iota (length exprs))
exprs)))
; Given the number of prior clauses,
; the variable on which to dispatch,
; a list of constant lists for mixed or miscellaneous clauses,
; a list of constant lists for the fixnum clauses,
; a list of constant lists for the character clauses, and
; a list of constant lists for the symbol clauses,
; returns code that computes the index of the selected clause.
; The mixed/miscellaneous clauses must be tested first because
; Twobit's SMALLINT? predicate might not be true of all fixnums
; on the target machine, which means that Twobit might classify
; some fixnums as miscellaneous.
(define (implement-dispatch prior var0 other fix chr sym)
(cond ((not (null? other))
(implement-dispatch-other
(implement-dispatch (+ prior (length other))
var0 fix chr sym '())
prior var other))
((not (null? fix))
(make-conditional (make-call (make-variable name:FIXNUM?)
(list (make-variable var0)))
(implement-dispatch-fixnum prior var0 fix)
(implement-dispatch (+ prior (length fix))
var0 '() chr sym other)))
((not (null? chr))
(make-conditional (make-call (make-variable name:CHAR?)
(list (make-variable var0)))
(implement-dispatch-char prior var0 chr)
(implement-dispatch (+ prior (length chr))
var0 fix '() sym other)))
((not (null? sym))
(make-conditional (make-call (make-variable name:SYMBOL?)
(list (make-variable var0)))
(implement-dispatch-symbol prior var0 sym)
(implement-dispatch (+ prior (length sym))
var0 fix chr '() other)))
(else
(make-constant 0))))
; The value of var0 will be known to be a fixnum.
; Can use table lookup, binary search, or sequential search.
; FIXME: Never uses sequential search, which is best when
; there are only a few constants, with gaps between them.
(define (implement-dispatch-fixnum prior var0 lists)
(define (calculate-intervals n lists)
(define (loop n lists intervals)
(if (null? lists)
(twobit-sort (lambda (interval1 interval2)
(< (car interval1) (car interval2)))
intervals)
(let ((constants (twobit-sort < (car lists))))
(loop (+ n 1)
(cdr lists)
(append (extract-intervals n constants)
intervals)))))
(loop n lists '()))
(define (extract-intervals n constants)
(if (null? constants)
'()
(let ((k0 (car constants)))
(do ((constants (cdr constants) (cdr constants))
(k1 (+ k0 1) (+ k1 1)))
((or (null? constants)
(not (= k1 (car constants))))
(cons (list k0 k1 (make-constant n))
(extract-intervals n constants)))))))
(define (complete-intervals intervals)
(cond ((null? intervals)
intervals)
((null? (cdr intervals))
intervals)
(else
(let* ((i1 (car intervals))
(i2 (cadr intervals))
(end1 (cadr i1))
(start2 (car i2))
(intervals (complete-intervals (cdr intervals))))
(if (= end1 start2)
(cons i1 intervals)
(cons i1
(cons (list end1 start2 (make-constant 0))
intervals)))))))
(let* ((intervals (complete-intervals
(calculate-intervals (+ prior 1) lists)))
(lo (car (car intervals)))
(hi (car (car (reverse intervals))))
(p (length intervals)))
(make-conditional
(make-call (make-variable name:FX<)
(list (make-variable var0)
(make-constant lo)))
(make-constant 0)
(make-conditional
(make-call (make-variable name:FX<)
(list (make-variable var0)
(make-constant (+ hi 1))))
; The static cost of table lookup is about hi - lo words.
; The static cost of binary search is about 5 SPARC instructions
; per interval.
(if (< (- hi lo) (* 5 p))
(implement-table-lookup var0 (+ prior 1) lists lo hi)
(implement-intervals var0 intervals))
(make-constant 0)))))
(define (implement-dispatch-char prior var0 lists)
(let* ((lists (map (lambda (constants)
(map compat:char->integer constants))
lists))
(name:n ((make-rename-procedure) 'n))
; Referencing information is destroyed by pass 2.
;(entry (make-R-entry name:n '() '() '()))
(F (list name:n name:EQ? name:FX< name:FX- name:VECTOR-REF))
(L (make-lambda
(list name:n)
'()
'() ; entry
F
'()
'()
#f
(implement-dispatch-fixnum prior name:n lists))))
(make-call L
(make-call (make-variable name:CHAR->INTEGER)
(list (make-variable var0))))))
(define (implement-dispatch-symbol prior var0 lists)
(implement-dispatch-other (make-constant 0) prior var0 lists))
(define (implement-dispatch-other default prior var0 lists)
(if (null? lists)
default
(let* ((constants (car lists))
(lists (cdr lists))
(n (+ prior 1)))
(make-conditional (make-call-to-memv var0 constants)
(make-constant n)
(implement-dispatch-other default n var0 lists)))))
(define (make-call-to-memv var0 constants)
(cond ((null? constants)
(make-constant #f))
((null? (cdr constants))
(make-call-to-eqv var0 (car constants)))
(else
(make-conditional (make-call-to-eqv var0 (car constants))
(make-constant #t)
(make-call-to-memv var0 (cdr constants))))))
(define (make-call-to-eqv var0 constant)
(make-call (make-variable
(if (eq-is-ok? constant)
name:EQ?
name:EQV?))
(list (make-variable var0)
(make-constant constant))))
; Given a variable whose value is known to be a fixnum,
; the clause index for the first fixnum clause,
; an ordered list of lists of constants for fixnum-only clauses,
; and the least and greatest constants in those lists,
; returns code for a table lookup.
(define (implement-table-lookup var0 index lists lo hi)
(let ((v (make-vector (+ 1 (- hi lo)) 0)))
(do ((index index (+ index 1))
(lists lists (cdr lists)))
((null? lists))
(for-each (lambda (k)
(vector-set! v (- k lo) index))
(car lists)))
(make-call (make-variable name:VECTOR-REF)
(list (make-constant v)
(make-call (make-variable name:FX-)
(list (make-variable var0)
(make-constant lo)))))))
; Given a variable whose value is known to lie within the
; half-open interval [m0, mk), and an ordered complete
; list of intervals of the form
;
; ((m0 m1 code0)
; (m1 m2 code1)
; ...
; (m{k-1} mk code{k-1})
; )
;
; returns an expression that finds the unique i such that
; var0 lies within [mi, m{i+1}), and then executes code{i}.
(define (implement-intervals var0 intervals)
(if (null? (cdr intervals))
(caddr (car intervals))
(let ((n (quotient (length intervals) 2)))
(do ((n n (- n 1))
(intervals1 '() (cons (car intervals2) intervals1))
(intervals2 intervals (cdr intervals2)))
((zero? n)
(let ((intervals1 (reverse intervals1))
(m (car (car intervals2))))
(make-conditional (make-call (make-variable name:FX<)
(list
(make-variable var0)
(make-constant m)))
(implement-intervals var0 intervals1)
(implement-intervals var0 intervals2))))))))
; The brute force approach.
; Given the variable on which the dispatch is being performed, and
; actual (simplified) code for the default clause and
; for all other clauses,
; returns code to perform the dispatch by sequential search.
(define *memq-threshold* 20)
(define *memv-threshold* 4)
(define (implement-clauses-by-sequential-search var0 default clauses)
(if (null? clauses)
default
(let* ((case1 (car clauses))
(clauses (cdr clauses))
(constants1 (car case1))
(code1 (cadr case1)))
(make-conditional (make-call-to-memv var0 constants1)
code1
(implement-clauses-by-sequential-search
var0 default clauses)))))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 13 April 1999.
;
; The tail and non-tail call graphs of known and unknown procedures.
;
; Given an expression E returned by pass 2 of Twobit,
; returns a list of the following form:
;
; ((#t L () <tailcalls> <nontailcalls> <size> #f)
; (<name> L <vars> <tailcalls> <nontailcalls> <size> #f)
; ...)
;
; where
;
; Each L is a lambda expression that occurs within E
; as either an escaping lambda expression or as a known
; procedure. If L is a known procedure, then <name> is
; its name; otherwise <name> is #f.
;
; <vars> is a list of the non-global variables within whose
; scope L occurs.
;
; <tailcalls> is a complete list of names of known local procedures
; that L calls tail-recursively, disregarding calls from other known
; procedures or escaping lambda expressions that occur within L.
;
; <nontailcalls> is a complete list of names of known local procedures
; that L calls non-tail-recursively, disregarding calls from other
; known procedures or escaping lambda expressions that occur within L.
;
; <size> is a measure of the size of L, including known procedures
; and escaping lambda expressions that occur within L.
(define (callgraphnode.name x) (car x))
(define (callgraphnode.code x) (cadr x))
(define (callgraphnode.vars x) (caddr x))
(define (callgraphnode.tailcalls x) (cadddr x))
(define (callgraphnode.nontailcalls x) (car (cddddr x)))
(define (callgraphnode.size x) (cadr (cddddr x)))
(define (callgraphnode.info x) (caddr (cddddr x)))
(define (callgraphnode.size! x v) (set-car! (cdr (cddddr x)) v) #f)
(define (callgraphnode.info! x v) (set-car! (cddr (cddddr x)) v) #f)
(define (callgraph exp)
; Returns (union (list x) z).
(define (adjoin x z)
(if (memq x z)
z
(cons x z)))
(let ((result '()))
; Given a <name> as described above, a lambda expression, a list
; of variables that are in scope, and a list of names of known
; local procedure that are in scope, computes an entry for L and
; entries for any nested known procedures or escaping lambda
; expressions, and adds them to the result.
(define (add-vertex! name L vars known)
(let ((tailcalls '())
(nontailcalls '())
(size 0))
; Given an expression, a list of variables that are in scope,
; a list of names of known local procedures that are in scope,
; and a boolean indicating whether the expression occurs in a
; tail context, adds any tail or non-tail calls to known
; procedures that occur within the expression to the list
; variables declared above.
(define (graph! exp vars known tail?)
(set! size (+ size 1))
(case (car exp)
((quote) #f)
((lambda) (add-vertex! #f exp vars known)
(set! size
(+ size
(callgraphnode.size (car result)))))
((set!) (graph! (assignment.rhs exp) vars known #f))
((if) (graph! (if.test exp) vars known #f)
(graph! (if.then exp) vars known tail?)
(graph! (if.else exp) vars known tail?))
((begin) (if (not (variable? exp))
(do ((exprs (begin.exprs exp) (cdr exprs)))
((null? (cdr exprs))
(graph! (car exprs) vars known tail?))
(graph! (car exprs) vars known #f))))
(else (let ((proc (call.proc exp)))
(cond ((variable? proc)
(let ((name (variable.name proc)))
(if (memq name known)
(if tail?
(set! tailcalls
(adjoin name tailcalls))
(set! nontailcalls
(adjoin name nontailcalls))))))
((lambda? proc)
(graph-lambda! proc vars known tail?))
(else
(graph! proc vars known #f)))
(for-each (lambda (exp)
(graph! exp vars known #f))
(call.args exp))))))
(define (graph-lambda! L vars known tail?)
(let* ((defs (lambda.defs L))
(newknown (map def.lhs defs))
(vars (append newknown
(make-null-terminated
(lambda.args L))
vars))
(known (append newknown known)))
(for-each (lambda (def)
(add-vertex! (def.lhs def)
(def.rhs def)
vars
known)
(set! size
(+ size
(callgraphnode.size (car result)))))
defs)
(graph! (lambda.body L) vars known tail?)))
(graph-lambda! L vars known #t)
(set! result
(cons (list name L vars tailcalls nontailcalls size #f)
result))))
(add-vertex! #t
(make-lambda '() '() '() '() '() '() '() exp)
'()
'())
result))
; Displays the callgraph, for debugging.
(define (view-callgraph g)
(for-each (lambda (entry)
(let ((name (callgraphnode.name entry))
(exp (callgraphnode.code entry))
(vars (callgraphnode.vars entry))
(tail (callgraphnode.tailcalls entry))
(nt (callgraphnode.nontailcalls entry))
(size (callgraphnode.size entry)))
(cond ((symbol? name)
(write name))
(name
(display "TOP LEVEL EXPRESSION"))
(else
(display "ESCAPING LAMBDA EXPRESSION")))
(display ":")
(newline)
(display "Size: ")
(write size)
(newline)
;(newline)
;(display "Variables in scope: ")
;(write vars)
;(newline)
(display "Tail calls: ")
(write tail)
(newline)
(display "Non-tail calls: ")
(write nt)
(newline)
;(newline)
;(pretty-print (make-readable exp))
;(newline)
;(newline)
(newline)))
g))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 14 April 1999.
;
; Inlining of known local procedures.
;
; First find the known and escaping procedures and compute the call graph.
;
; If a known local procedure is not called at all, then delete its code.
;
; If a known local procedure is called exactly once,
; then inline its code at the call site and delete the
; known local procedure. Change the size of the code
; at the call site by adding the size of the inlined code.
;
; Divide the remaining known and escaping procedures into categories:
; 1. makes no calls to known local procedures
; 2. known procedures that call known procedures;
; within this category, try to sort so that procedures do not
; call procedures that come later in the sequence; or sort by
; number of calls and/or size
; 3. escaping procedures that call known procedures
;
; Approve each procedure in category 1 for inlining if its code size
; is less than some threshold.
;
; For each procedure in categories 2 and 3, traverse its code, inlining
; where it seems like a good idea. The compiler should be more aggressive
; about inlining non-tail calls than tail calls because:
;
; Inlining a non-tail call can eliminate a stack frame
; or expose the inlined code to loop optimizations.
;
; The main reason for inlining a tail call is to enable
; intraprocedural optimizations or to unroll a loop.
;
; After inlining has been performed on a known local procedure,
; then approve it for inlining if its size is less than some threshold.
;
; FIXME:
; This strategy avoids infinite unrolling, but it also avoids finite
; unrolling of loops.
; Parameters to control inlining.
; These can be tuned later.
(define *tail-threshold* 10)
(define *nontail-threshold* 20)
(define *multiplier* 300)
; Given a callgraph, performs inlining of known local procedures
; by side effect. The original expression must then be copied to
; reinstate Twobit's invariants.
; FIXME: This code doesn't yet do the right thing with known local
; procedures that aren't called or are called in exactly one place.
(define (inline-using-callgraph! g)
(let ((known (make-hashtable))
(category2 '())
(category3 '()))
(for-each (lambda (node)
(let ((name (callgraphnode.name node))
(tcalls (callgraphnode.tailcalls node))
(ncalls (callgraphnode.nontailcalls node)))
(if (symbol? name)
(hashtable-put! known name node))
(if (and (null? tcalls)
(null? ncalls))
(if (< (callgraphnode.size node)
*nontail-threshold*)
(callgraphnode.info! node #t))
(if (symbol? name)
(set! category2 (cons node category2))
(set! category3 (cons node category3))))))
g)
(set! category2 (twobit-sort (lambda (x y)
(< (callgraphnode.size x)
(callgraphnode.size y)))
category2))
(for-each (lambda (node)
(inline-node! node known))
category2)
(for-each (lambda (node)
(inline-node! node known))
category3)
; FIXME:
; Inlining destroys the callgraph, so maybe this cleanup is useless.
(hashtable-for-each (lambda (name node) (callgraphnode.info! node #f))
known)))
; Given a node of the callgraph and a hash table of nodes for
; known local procedures, performs inlining by side effect.
(define (inline-node! node known)
(let* ((debugging? #f)
(name (callgraphnode.name node))
(exp (callgraphnode.code node))
(size0 (callgraphnode.size node))
(budget (quotient (* (- *multiplier* 100) size0) 100))
(tail-threshold *tail-threshold*)
(nontail-threshold *nontail-threshold*))
; Given an expression,
; a boolean indicating whether the expression is in a tail context,
; a list of procedures that should not be inlined,
; and a size budget,
; performs inlining by side effect and returns the unused budget.
(define (inline exp tail? budget)
(if (positive? budget)
(case (car exp)
((quote lambda)
budget)
((set!)
(inline (assignment.rhs exp) #f budget))
((if)
(let* ((budget (inline (if.test exp) #f budget))
(budget (inline (if.then exp) tail? budget))
(budget (inline (if.else exp) tail? budget)))
budget))
((begin)
(if (variable? exp)
budget
(do ((exprs (begin.exprs exp) (cdr exprs))
(budget budget
(inline (car exprs) #f budget)))
((null? (cdr exprs))
(inline (car exprs) tail? budget)))))
(else
(let ((budget (do ((exprs (call.args exp) (cdr exprs))
(budget budget
(inline (car exprs) #f budget)))
((null? exprs)
budget))))
(let ((proc (call.proc exp)))
(cond ((variable? proc)
(let* ((procname (variable.name proc))
(procnode (hashtable-get known procname)))
(if procnode
(let ((size (callgraphnode.size procnode))
(info (callgraphnode.info procnode)))
(if (and info
(<= size budget)
(<= size
(if tail?
tail-threshold
nontail-threshold)))
(begin
(if debugging?
(begin
(display " Inlining ")
(write (variable.name proc))
(newline)))
(call.proc-set!
exp
(copy-exp
(callgraphnode.code procnode)))
(callgraphnode.size!
node
(+ (callgraphnode.size node) size))
(- budget size))
(begin
(if (and #f debugging?)
(begin
(display " Declining to inline ")
(write (variable.name proc))
(newline)))
budget)))
budget)))
((lambda? proc)
(inline (lambda.body proc) tail? budget))
(else
(inline proc #f budget)))))))
-1))
(if (and #f debugging?)
(begin
(display "Processing ")
(write name)
(newline)))
(let ((budget (inline (if (lambda? exp)
(lambda.body exp)
exp)
#t
budget)))
(if (and (negative? budget)
debugging?)
; This shouldn't happen very often.
(begin (display "Ran out of inlining budget for ")
(write (callgraphnode.name node))
(newline)))
(if (<= (callgraphnode.size node) nontail-threshold)
(callgraphnode.info! node #t))
#f)))
; For testing.
(define (test-inlining test0)
(begin (define exp0 (begin (display "Compiling...")
(newline)
(pass2 (pass1 test0))))
(define g0 (begin (display "Computing call graph...")
(newline)
(callgraph exp0))))
(display "Inlining...")
(newline)
(inline-using-callgraph! g0)
(pretty-print (make-readable (copy-exp exp0))))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 14 April 1999.
;
; Interprocedural constant propagation and folding.
;
; Constant propagation must converge before constant folding can be
; performed. Constant folding creates more constants that can be
; propagated, so these two optimizations must be iterated, but it
; is safe to stop at any time.
;
; Abstract interpretation for constant folding.
;
; The abstract values are
; bottom (represented here by #f)
; constants (represented by quoted literals)
; top (represented here by #t)
;
; Let [[ E ]] be the abstract interpretation of E over that domain
; of abstract values, with respect to some arbitrary set of abstract
; values for local variables.
;
; If a is a global variable or a formal parameter of an escaping
; lambda expression, then [[ a ]] = #t.
;
; If x is the ith formal parameter of a known local procedure f,
; then [[ x ]] = \join_{(f E1 ... En)} [[ Ei ]].
;
; [[ K ]] = K
; [[ L ]] = #t
; [[ (begin E1 ... En) ]] = [[ En ]]
; [[ (set! I E) ]] = #f
;
; If [[ E0 ]] = #t, then [[ (if E0 E1 E2) ]] = [[ E1 ]] \join [[ E2 ]]
; else if [[ E0 ]] = K, then [[ (if E0 E1 E2) ]] = [[ E1 ]]
; or [[ (if E0 E1 E2) ]] = [[ E2 ]]
; depending upon K
; else [[ (if E0 E1 E2) ]] = #f
;
; If f is a known local procedure with body E,
; then [[ (f E1 ... En) ]] = [[ E ]]
;
; If g is a foldable integrable procedure, then:
; if there is some i for which [[ Ei ]] = #t,
; then [[ (g E1 ... En) ]] = #t
; else if [[ E1 ]] = K1, ..., [[ En ]] = Kn,
; then [[ (g E1 ... En) ]] = (g K1 ... Kn)
; else [[ (g E1 ... En) ]] = #f
;
; Symbolic representations of abstract values.
; (Can be thought of as mappings from abstract environments to
; abstract values.)
;
; <symbolic> ::= #t | ( <expressions> )
; <expressions> ::= <empty> | <expression> <expressions>
; Parameter to limit constant propagation and folding.
; This parameter can be tuned later.
(define *constant-propagation-limit* 5)
; Given an expression as output by pass 2, performs constant
; propagation and folding.
(define (constant-propagation exp)
(define (constant-propagation exp i)
(if (< i *constant-propagation-limit*)
(begin
;(display "Performing constant propagation and folding...")
;(newline)
(let* ((g (callgraph exp))
(L (callgraphnode.code (car g)))
(variables (constant-propagation-using-callgraph g))
(changed? (constant-folding! L variables)))
(if changed?
(constant-propagation (lambda.body L) (+ i 1))
(lambda.body L))))))
(constant-propagation exp 0))
; Given a callgraph, returns a hashtable of abstract values for
; all local variables.
(define (constant-propagation-using-callgraph g)
(let ((debugging? #f)
(folding? (integrate-usual-procedures))
(known (make-hashtable))
(variables (make-hashtable))
(counter 0))
; Computes joins of abstract values.
(define (join x y)
(cond ((boolean? x)
(if x #t y))
((boolean? y)
(join y x))
((equal? x y)
x)
(else #t)))
; Given a <symbolic> and a vector of abstract values,
; evaluates the <symbolic> and returns its abstract value.
(define (aeval rep env)
(cond ((eq? rep #t)
#t)
((null? rep)
#f)
((null? (cdr rep))
(aeval1 (car rep) env))
(else
(join (aeval1 (car rep) env)
(aeval (cdr rep) env)))))
(define (aeval1 exp env)
(case (car exp)
((quote)
exp)
((lambda)
#t)
((set!)
#f)
((begin)
(if (variable? exp)
(let* ((name (variable.name exp))
(i (hashtable-get variables name)))
(if i
(vector-ref env i)
#t))
(aeval1-error)))
((if)
(let* ((val0 (aeval1 (if.test exp) env))
(val1 (aeval1 (if.then exp) env))
(val2 (aeval1 (if.else exp) env)))
(cond ((eq? val0 #t)
(join val1 val2))
((pair? val0)
(if (constant.value val0)
val1
val2))
(else
#f))))
(else
(do ((exprs (reverse (call.args exp)) (cdr exprs))
(vals '() (cons (aeval1 (car exprs) env) vals)))
((null? exprs)
(let ((proc (call.proc exp)))
(cond ((variable? proc)
(let* ((procname (variable.name proc))
(procnode (hashtable-get known procname))
(entry (if folding?
(constant-folding-entry procname)
#f)))
(cond (procnode
(vector-ref env
(hashtable-get variables
procname)))
(entry
; FIXME: No constant folding
#t)
(else (aeval1-error)))))
(else
(aeval1-error)))))))))
(define (aeval1-error)
(error "Compiler bug: constant propagation (aeval1)"))
; Combines two <symbolic>s.
(define (combine-symbolic rep1 rep2)
(cond ((eq? rep1 #t) #t)
((eq? rep2 #t) #t)
(else
(append rep1 rep2))))
; Given an expression, returns a <symbolic> that represents
; a list of expressions whose abstract values can be joined
; to obtain the abstract value of the given expression.
; As a side effect, enters local variables into variables.
(define (collect! exp)
(case (car exp)
((quote)
(list exp))
((lambda)
#t)
((set!)
(collect! (assignment.rhs exp))
'())
((begin)
(if (variable? exp)
(list exp)
(do ((exprs (begin.exprs exp) (cdr exprs)))
((null? (cdr exprs))
(collect! (car exprs)))
(collect! (car exprs)))))
((if)
(collect! (if.test exp))
(collect! (if.then exp))
(collect! (if.else exp))
#t)
(else
(do ((exprs (reverse (call.args exp)) (cdr exprs))
(reps '() (cons (collect! (car exprs)) reps)))
((null? exprs)
(let ((proc (call.proc exp)))
(define (put-args! args reps)
(cond ((pair? args)
(let ((v (car args))
(rep (car reps)))
(hashtable-put! variables v rep)
(put-args! (cdr args) (cdr reps))))
((symbol? args)
(hashtable-put! variables args #t))
(else #f)))
(cond ((variable? proc)
(let* ((procname (variable.name proc))
(procnode (hashtable-get known procname))
(entry (if folding?
(constant-folding-entry procname)
#f)))
(cond (procnode
(for-each (lambda (v rep)
(hashtable-put!
variables
v
(combine-symbolic
rep (hashtable-get variables v))))
(lambda.args
(callgraphnode.code procnode))
reps)
(list (make-variable procname)))
(entry
; FIXME: No constant folding
#t)
(else #t))))
((lambda? proc)
(put-args! (lambda.args proc) reps)
(collect! (lambda.body proc)))
(else
(collect! proc)
#t))))))))
(for-each (lambda (node)
(let* ((name (callgraphnode.name node))
(code (callgraphnode.code node))
(known? (symbol? name))
(rep (if known? '() #t)))
(if known?
(hashtable-put! known name node))
(if (lambda? code)
(for-each (lambda (var)
(hashtable-put! variables var rep))
(make-null-terminated (lambda.args code))))))
g)
(for-each (lambda (node)
(let ((name (callgraphnode.name node))
(code (callgraphnode.code node)))
(cond ((symbol? name)
(hashtable-put! variables
name
(collect! (lambda.body code))))
(else
(collect! (lambda.body code))))))
g)
(if (and #f debugging?)
(begin
(hashtable-for-each (lambda (v rep)
(write v)
(display ": ")
(write rep)
(newline))
variables)
(display "----------------------------------------")
(newline)))
;(trace aeval aeval1)
(let* ((n (hashtable-size variables))
(vars (hashtable-map (lambda (v rep) v) variables))
(reps (map (lambda (v) (hashtable-get variables v)) vars))
(init (make-vector n #f))
(next (make-vector n)))
(do ((i 0 (+ i 1))
(vars vars (cdr vars))
(reps reps (cdr reps)))
((= i n))
(hashtable-put! variables (car vars) i)
(vector-set! next
i
(let ((rep (car reps)))
(lambda (env)
(aeval rep env)))))
(compute-fixedpoint init next equal?)
(for-each (lambda (v)
(let* ((i (hashtable-get variables v))
(aval (vector-ref init i)))
(hashtable-put! variables v aval)
(if (and debugging?
(not (eq? aval #t)))
(begin (write v)
(display ": ")
(write aval)
(newline)))))
vars)
variables)))
; Given a lambda expression, performs constant propagation, folding,
; and simplifications by side effect, using the abstract values in the
; hash table of variables.
; Returns #t if any new constants were created by constant folding,
; otherwise returns #f.
(define (constant-folding! L variables)
(let ((debugging? #f)
(msg1 " Propagating constant value for ")
(msg2 " Folding: ")
(msg3 " ==> ")
(folding? (integrate-usual-procedures))
(changed? #f))
; Given a known lambda expression L, its original formal parameters,
; and a list of all calls to L, deletes arguments that are now
; ignored because of constant propagation.
(define (delete-ignored-args! L formals0 calls)
(let ((formals1 (lambda.args L)))
(for-each (lambda (call)
(do ((formals0 formals0 (cdr formals0))
(formals1 formals1 (cdr formals1))
(args (call.args call)
(cdr args))
(newargs '()
(if (and (eq? (car formals1) name:IGNORED)
(pair?
(hashtable-get variables
(car formals0))))
newargs
(cons (car args) newargs))))
((null? formals0)
(call.args-set! call (reverse newargs)))))
calls)
(do ((formals0 formals0 (cdr formals0))
(formals1 formals1 (cdr formals1))
(formals2 '()
(if (and (not (eq? (car formals0)
(car formals1)))
(eq? (car formals1) name:IGNORED)
(pair?
(hashtable-get variables
(car formals0))))
formals2
(cons (car formals1) formals2))))
((null? formals0)
(lambda.args-set! L (reverse formals2))))))
(define (fold! exp)
(case (car exp)
((quote) exp)
((lambda)
(let ((Rinfo (lambda.R exp))
(known (map def.lhs (lambda.defs exp))))
(for-each (lambda (entry)
(let* ((v (R-entry.name entry))
(aval (hashtable-fetch variables v #t)))
(if (and (pair? aval)
(not (memq v known)))
(let ((x (constant.value aval)))
(if (or (boolean? x)
(null? x)
(symbol? x)
(number? x)
(char? x)
(and (vector? x)
(zero? (vector-length x))))
(let ((refs (R-entry.references entry)))
(for-each (lambda (ref)
(variable-set! ref aval))
refs)
; Do not try to use Rinfo in place of
; (lambda.R exp) below!
(lambda.R-set!
exp
(remq entry (lambda.R exp)))
(flag-as-ignored v exp)
(if debugging?
(begin (display msg1)
(write v)
(display ": ")
(write aval)
(newline)))))))))
Rinfo)
(for-each (lambda (def)
(let* ((name (def.lhs def))
(rhs (def.rhs def))
(entry (R-lookup Rinfo name))
(calls (R-entry.calls entry)))
(if (null? calls)
(begin (lambda.defs-set!
exp
(remq def (lambda.defs exp)))
; Do not try to use Rinfo in place of
; (lambda.R exp) below!
(lambda.R-set!
exp
(remq entry (lambda.R exp))))
(let* ((formals0 (append (lambda.args rhs) '()))
(L (fold! rhs))
(formals1 (lambda.args L)))
(if (not (equal? formals0 formals1))
(delete-ignored-args! L formals0 calls))))))
(lambda.defs exp))
(lambda.body-set!
exp
(fold! (lambda.body exp)))
exp))
((set!)
(assignment.rhs-set! exp (fold! (assignment.rhs exp)))
exp)
((begin)
(if (variable? exp)
exp
(post-simplify-begin (make-begin (map fold! (begin.exprs exp)))
(make-notepad #f))))
((if)
(let ((exp0 (fold! (if.test exp)))
(exp1 (fold! (if.then exp)))
(exp2 (fold! (if.else exp))))
(if (constant? exp0)
(let ((newexp (if (constant.value exp0)
exp1
exp2)))
(if debugging?
(begin (display msg2)
(write (make-readable exp))
(display msg3)
(write (make-readable newexp))
(newline)))
(set! changed? #t)
newexp)
(make-conditional exp0 exp1 exp2))))
(else
(let ((args (map fold! (call.args exp)))
(proc (fold! (call.proc exp))))
(cond ((and folding?
(variable? proc)
(every? constant? args)
(let ((entry
(constant-folding-entry (variable.name proc))))
(and entry
(let ((preds
(constant-folding-predicates entry)))
(and (= (length args) (length preds))
(every?
(lambda (x) x)
(map (lambda (f v) (f v))
(constant-folding-predicates entry)
(map constant.value args))))))))
(set! changed? #t)
(let ((result
(make-constant
(apply (constant-folding-folder
(constant-folding-entry
(variable.name proc)))
(map constant.value args)))))
(if debugging?
(begin (display msg2)
(write (make-readable (make-call proc args)))
(display msg3)
(write result)
(newline)))
result))
((and (lambda? proc)
(list? (lambda.args proc)))
; FIXME: Folding should be done even if there is
; a rest argument.
(let loop ((formals (reverse (lambda.args proc)))
(actuals (reverse args))
(processed-formals '())
(processed-actuals '())
(for-effect '()))
(cond ((null? formals)
(lambda.args-set! proc processed-formals)
(call.args-set! exp processed-actuals)
(let ((call (if (and (null? processed-formals)
(null? (lambda.defs proc)))
(lambda.body proc)
exp)))
(if (null? for-effect)
call
(post-simplify-begin
(make-begin
(reverse (cons call for-effect)))
(make-notepad #f)))))
((ignored? (car formals))
(loop (cdr formals)
(cdr actuals)
processed-formals
processed-actuals
(cons (car actuals) for-effect)))
(else
(loop (cdr formals)
(cdr actuals)
(cons (car formals) processed-formals)
(cons (car actuals) processed-actuals)
for-effect)))))
(else
(call.proc-set! exp proc)
(call.args-set! exp args)
exp))))))
(fold! L)
changed?))
; Copyright 1998 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 7 June 1999.
;
; Conversion to A-normal form, with heuristics for
; choosing a good order of evaluation.
;
; This pass operates as a source-to-source transformation on
; expressions written in the subset of Scheme described by the
; following grammar, where the input and output expressions
; satisfy certain additional invariants described below.
;
; "X ..." means zero or more occurrences of X.
;
; L --> (lambda (I_1 ...)
; (begin D ...)
; (quote (R F G <decls> <doc>)
; E)
; | (lambda (I_1 ... . I_rest)
; (begin D ...)
; (quote (R F G <decls> <doc>))
; E)
; D --> (define I L)
; E --> (quote K) ; constants
; | (begin I) ; variable references
; | L ; lambda expressions
; | (E0 E1 ...) ; calls
; | (set! I E) ; assignments
; | (if E0 E1 E2) ; conditionals
; | (begin E0 E1 E2 ...) ; sequential expressions
; I --> <identifier>
;
; R --> ((I <references> <assignments> <calls>) ...)
; F --> (I ...)
; G --> (I ...)
;
; Invariants that hold for the input only:
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * For each lambda expression, the associated F is a list of all
; the identifiers that occur free in the body of that lambda
; expression, and possibly a few extra identifiers that were
; once free but have been removed by optimization.
; * For each lambda expression, the associated G is a subset of F
; that contains every identifier that occurs free within some
; inner lambda expression that escapes, and possibly a few that
; don't. (Assignment-elimination does not calculate G exactly.)
; * Variables named IGNORED are neither referenced nor assigned.
;
; Invariants that hold for the output only:
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * R, F, and G are garbage.
; * There are no sequential expressions.
; * The output is an expression E with syntax
;
; E --> A
; | (L)
; | (L A)
;
; A --> W
; | L
; | (W_0 W_1 ...)
; | (set! I W)
; | (if W E1 E2)
;
; W --> (quote K)
; | (begin I)
;
; In other words:
; An expression is a LET* such that the rhs of every binding is
; a conditional with the test already evaluated, or
; an expression that can be evaluated in one step
; (treating function calls as a single step)
;
; A-normal form corresponds to the control flow graph for a lambda
; expression.
; Algorithm: repeated use of these rules:
;
; (E0 E1 ...) ((lambda (T0 T1 ...) (T0 T1 ...))
; E0 E1 ...)
; (set! I E) ((lambda (T) (set! I T)) E)
; (if E0 E1 E2) ((lambda (T) (if T E1 E2)) E0)
; (begin E0 E1 E2 ...) ((lambda (T) (begin E1 E2 ...)) E0)
;
; ((lambda (I1 I2 I3 ...) E) ((lambda (I1)
; E1 E2 E3) ((lambda (I2 I3 ...) E)
; E2 E3))
; E1)
;
; ((lambda (I2) E) ((lambda (I1)
; ((lambda (I1) E2) ((lambda (I2) E)
; E1)) E2)
; E1)
;
; In other words:
; Introduce a temporary name for every expression except:
; tail expressions
; the alternatives of a non-tail conditional
; Convert every LET into a LET*.
; Get rid of LET* on the right hand side of a binding.
; Given an expression E in the representation output by pass 2,
; returns an A-normal form for E in that representation.
; Except for quoted values, the A-normal form does not share
; mutable structure with the original expression E.
;
; KNOWN BUG:
;
; If you call A-normal on a form that has already been converted
; to A-normal form, then the same temporaries will be generated
; twice. An optional argument lets you specify a different prefix
; for temporaries the second time around. Example:
;
; (A-normal-form (A-normal-form E ".T")
; ".U")
; This is the declaration that is used to indicate A-normal form.
(define A-normal-form-declaration (list 'anf))
(define (A-normal-form E . rest)
(define (A-normal-form E)
(anf-make-let* (anf E '() '())))
; New temporaries.
(define temp-counter 0)
(define temp-prefix
(if (or (null? rest)
(not (string? (car rest))))
(string-append renaming-prefix "T")
(car rest)))
(define (newtemp)
(set! temp-counter (+ temp-counter 1))
(string->symbol
(string-append temp-prefix
(number->string temp-counter))))
; Given an expression E as output by pass 2,
; a list of surrounding LET* bindings,
; and an ordered list of likely register variables,
; return a non-empty list of LET* bindings
; whose first binding associates a dummy variable
; with an A-expression giving the value for E.
(define (anf E bindings regvars)
(case (car E)
((quote) (anf-bind-dummy E bindings))
((begin) (if (variable? E)
(anf-bind-dummy E bindings)
(anf-sequential E bindings regvars)))
((lambda) (anf-lambda E bindings regvars))
((set!) (anf-assignment E bindings regvars))
((if) (anf-conditional E bindings regvars))
(else (anf-call E bindings regvars))))
(define anf:dummy (string->symbol "RESULT"))
(define (anf-bind-dummy E bindings)
(cons (list anf:dummy E)
bindings))
; Unlike anf-bind-dummy, anf-bind-name and anf-bind convert
; their expression argument to A-normal form.
; Don't change anf-bind to call anf-bind-name, because that
; would name the temporaries in an aesthetically bad order.
(define (anf-bind-name name E bindings regvars)
(let ((bindings (anf E bindings regvars)))
(cons (list name (cadr (car bindings)))
(cdr bindings))))
(define (anf-bind E bindings regvars)
(let ((bindings (anf E bindings regvars)))
(cons (list (newtemp) (cadr (car bindings)))
(cdr bindings))))
(define (anf-result bindings)
(make-variable (car (car bindings))))
(define (anf-make-let* bindings)
(define (loop bindings body)
(if (null? bindings)
body
(let ((T1 (car (car bindings)))
(E1 (cadr (car bindings))))
(loop (cdr bindings)
(make-call (make-lambda (list T1)
'()
'()
'()
'()
(list A-normal-form-declaration)
'()
body)
(list E1))))))
(loop (cdr bindings)
(cadr (car bindings))))
(define (anf-sequential E bindings regvars)
(do ((bindings bindings
(anf-bind (car exprs) bindings regvars))
(exprs (begin.exprs E)
(cdr exprs)))
((null? (cdr exprs))
(anf (car exprs) bindings regvars))))
; Heuristic: the formal parameters of an escaping lambda or
; known local procedure are kept in REG1, REG2, et cetera.
(define (anf-lambda L bindings regvars)
(anf-bind-dummy
(make-lambda (lambda.args L)
(map (lambda (def)
(make-definition
(def.lhs def)
(A-normal-form (def.rhs def))))
(lambda.defs L))
'()
'()
'()
(cons A-normal-form-declaration
(lambda.decls L))
(lambda.doc L)
(anf-make-let*
(anf (lambda.body L)
'()
(make-null-terminated (lambda.args L)))))
bindings))
(define (anf-assignment E bindings regvars)
(let ((I (assignment.lhs E))
(E1 (assignment.rhs E)))
(if (variable? E1)
(anf-bind-dummy E bindings)
(let* ((bindings (anf-bind E1 bindings regvars))
(T1 (anf-result bindings)))
(anf-bind-dummy (make-assignment I T1) bindings)))))
(define (anf-conditional E bindings regvars)
(let ((E0 (if.test E))
(E1 (if.then E))
(E2 (if.else E)))
(if (variable? E0)
(let ((E1 (anf-make-let* (anf E1 '() regvars)))
(E2 (anf-make-let* (anf E2 '() regvars))))
(anf-bind-dummy
(make-conditional E0 E1 E2)
bindings))
(let* ((bindings (anf-bind E0 bindings regvars))
(E1 (anf-make-let* (anf E1 '() regvars)))
(E2 (anf-make-let* (anf E2 '() regvars))))
(anf-bind-dummy
(make-conditional (anf-result bindings) E1 E2)
bindings)))))
(define (anf-call E bindings regvars)
(let* ((proc (call.proc E))
(args (call.args E)))
; Evaluates the exprs and returns both a list of bindings and
; a list of the temporaries that name the results of the exprs.
; If rename-always? is true, then temporaries are generated even
; for constants and temporaries.
(define (loop exprs bindings names rename-always?)
(if (null? exprs)
(values bindings (reverse names))
(let ((E (car exprs)))
(if (or rename-always?
(not (or (constant? E)
(variable? E))))
(let* ((bindings
(anf-bind (car exprs) bindings regvars)))
(loop (cdr exprs)
bindings
(cons (anf-result bindings) names)
rename-always?))
(loop (cdr exprs)
bindings
(cons E names)
rename-always?)))))
; Evaluates the exprs, binding them to the vars, and returns
; a list of bindings.
;
; Although LET variables are likely to be kept in registers,
; trying to guess which register will be allocated is likely
; to do more harm than good.
(define (let-loop exprs bindings regvars vars)
(if (null? exprs)
(if (null? (lambda.defs proc))
(anf (lambda.body proc)
bindings
regvars)
(let ((bindings
(anf-bind
(make-lambda '()
(lambda.defs proc)
'()
'()
'()
(cons A-normal-form-declaration
(lambda.decls proc))
(lambda.doc proc)
(lambda.body proc))
bindings
'())))
(anf-bind-dummy
(make-call (anf-result bindings) '())
bindings)))
(let-loop (cdr exprs)
(anf-bind-name (car vars)
(car exprs)
bindings
regvars)
regvars
(cdr vars))))
(cond ((lambda? proc)
(let ((formals (lambda.args proc)))
(if (list? formals)
(let* ((pi (anf-order-of-evaluation args regvars #f))
(exprs (permute args pi))
(names (permute (lambda.args proc) pi)))
(let-loop (reverse exprs) bindings regvars (reverse names)))
(anf-call (normalize-let E) bindings regvars))))
((not (variable? proc))
(let ((pi (anf-order-of-evaluation args regvars #f)))
(call-with-values
(lambda () (loop (permute args pi) bindings '() #t))
(lambda (bindings names)
(let ((bindings (anf-bind proc bindings regvars)))
(anf-bind-dummy
(make-call (anf-result bindings)
(unpermute names pi))
bindings))))))
((and (integrate-usual-procedures)
(prim-entry (variable.name proc)))
(let ((pi (anf-order-of-evaluation args regvars #t)))
(call-with-values
(lambda () (loop (permute args pi) bindings '() #t))
(lambda (bindings names)
(anf-bind-dummy
(make-call proc (unpermute names pi))
bindings)))))
((memq (variable.name proc) regvars)
(let* ((exprs (cons proc args))
(pi (anf-order-of-evaluation
exprs
(cons name:IGNORED regvars)
#f)))
(call-with-values
(lambda () (loop (permute exprs pi) bindings '() #t))
(lambda (bindings names)
(let ((names (unpermute names pi)))
(anf-bind-dummy
(make-call (car names) (cdr names))
bindings))))))
(else
(let ((pi (anf-order-of-evaluation args regvars #f)))
(call-with-values
(lambda () (loop (permute args pi) bindings '() #t))
(lambda (bindings names)
(anf-bind-dummy
(make-call proc (unpermute names pi))
bindings))))))))
; Given a list of expressions, a list of likely register contents,
; and a switch telling whether these are arguments for a primop
; or something else (such as the arguments for a real call),
; try to choose a good order in which to evaluate the expressions.
;
; Heuristic: If none of the expressions is a call to a non-primop,
; then parallel assignment optimization gives a good order if the
; regvars are right, and should do no worse than a random order if
; the regvars are wrong.
;
; Heuristic: If the expressions are arguments to a primop, and
; none are a call to a non-primop, then the register contents
; are irrelevant, and the first argument should be evaluated last.
;
; Heuristic: If one or more of the expressions is a call to a
; non-primop, then the following should be a good order:
;
; expressions that are neither a constant, variable, or a call
; calls to non-primops
; constants and variables
(define (anf-order-of-evaluation exprs regvars for-primop?)
(define (ordering targets exprs alist)
(let ((para
(parallel-assignment targets alist exprs)))
(or para
; Evaluate left to right until a parallel assignment is found.
(cons (car targets)
(ordering (cdr targets)
(cdr exprs)
alist)))))
(if (parallel-assignment-optimization)
(cond ((null? exprs) '())
((null? (cdr exprs)) '(0))
(else
(let* ((contains-call? #f)
(vexprs (list->vector exprs))
(vindexes (list->vector
(iota (vector-length vexprs))))
(contains-call? #f)
(categories
(list->vector
(map (lambda (E)
(cond ((constant? E)
2)
((variable? E)
2)
((complicated? E)
(set! contains-call? #t)
1)
(else
0)))
exprs))))
(cond (contains-call?
(twobit-sort (lambda (i j)
(< (vector-ref categories i)
(vector-ref categories j)))
(iota (length exprs))))
(for-primop?
(reverse (iota (length exprs))))
(else
(let ((targets (iota (length exprs))))
(define (pairup regvars targets)
(if (or (null? targets)
(null? regvars))
'()
(cons (cons (car regvars)
(car targets))
(pairup (cdr regvars)
(cdr targets)))))
(ordering targets
exprs
(pairup regvars targets))))))))
(iota (length exprs))))
(define (permute things pi)
(let ((v (list->vector things)))
(map (lambda (i) (vector-ref v i))
pi)))
(define (unpermute things pi)
(let* ((v0 (list->vector things))
(v1 (make-vector (vector-length v0))))
(do ((pi pi (cdr pi))
(k 0 (+ k 1)))
((null? pi)
(vector->list v1))
(vector-set! v1 (car pi) (vector-ref v0 k)))))
; Given a call whose procedure is a lambda expression that has
; a rest argument, return a genuine let expression.
(define (normalize-let-error exp)
(if (issue-warnings)
(begin (display "WARNING from compiler: ")
(display "Wrong number of arguments ")
(display "to lambda expression")
(newline)
(pretty-print (make-readable exp) #t)
(newline))))
(define (normalize-let exp)
(let* ((L (call.proc exp)))
(let loop ((formals (lambda.args L))
(args (call.args exp))
(newformals '())
(newargs '()))
(cond ((null? formals)
(if (null? args)
(begin (lambda.args-set! L (reverse newformals))
(call.args-set! exp (reverse newargs)))
(begin (normalize-let-error exp)
(loop (list (newtemp))
args
newformals
newargs))))
((pair? formals)
(if (pair? args)
(loop (cdr formals)
(cdr args)
(cons (car formals) newformals)
(cons (car args) newargs))
(begin (normalize-let-error exp)
(loop formals
(cons (make-constant 0)
args)
newformals
newargs))))
(else
(loop (list formals)
(list (make-call-to-list args))
newformals
newargs))))))
; For heuristic use only.
; An expression is complicated unless it can probably be evaluated
; without saving and restoring any registers, even if it occurs in
; a non-tail position.
(define (complicated? exp)
; Let's not spend all day on this.
(let ((budget 10))
(define (complicated? exp)
(set! budget (- budget 1))
(if (zero? budget)
#t
(case (car exp)
((quote) #f)
((lambda) #f)
((set!) (complicated? (assignment.rhs exp)))
((if) (or (complicated? (if.test exp))
(complicated? (if.then exp))
(complicated? (if.else exp))))
((begin) (if (variable? exp)
#f
(some? complicated?
(begin.exprs exp))))
(else (let ((proc (call.proc exp)))
(if (and (variable? proc)
(integrate-usual-procedures)
(prim-entry (variable.name proc)))
(some? complicated?
(call.args exp))
#t))))))
(complicated? exp)))
(A-normal-form E))
(define (post-simplify-anf L0 T1 E0 E1 free regbindings L2)
(define (return-normally)
(values (make-call L0 (list E1))
free
regbindings))
(return-normally))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 7 June 1999.
;
; Intraprocedural common subexpression elimination, constant propagation,
; copy propagation, dead code elimination, and register targeting.
;
; (intraprocedural-commoning E 'commoning)
;
; Given an A-normal form E (alpha-converted, with correct free
; variables and referencing information), returns an optimized
; A-normal form with correct free variables but incorrect referencing
; information.
;
; (intraprocedural-commoning E 'target-registers)
;
; Given an A-normal form E (alpha-converted, with correct free
; variables and referencing information), returns an A-normal form
; with correct free variables but incorrect referencing information,
; and in which MacScheme machine register names are used as temporary
; variables. The result is alpha-converted except for register names.
;
; (intraprocedural-commoning E 'commoning 'target-registers)
; (intraprocedural-commoning E)
;
; Given an A-normal form as described above, returns an optimized
; form in which register names are used as temporary variables.
; Semantics of .check!:
;
; (.check! b exn x ...) faults with code exn and arguments x ...
; if b is #f.
; The list of argument registers.
; This can't go in pass3commoning.aux.sch because that file must be
; loaded before the target-specific file that defines *nregs*.
(define argument-registers
(do ((n (- *nregs* 2) (- n 1))
(regs '()
(cons (string->symbol
(string-append ".REG" (number->string n)))
regs)))
((zero? n)
regs)))
(define (intraprocedural-commoning E . flags)
(define target-registers? (or (null? flags) (memq 'target-registers flags)))
(define commoning? (or (null? flags) (memq 'commoning flags)))
(define debugging? #f)
(call-with-current-continuation
(lambda (return)
(define (error . stuff)
(display "Bug detected during intraprocedural optimization")
(newline)
(for-each (lambda (s)
(display s) (newline))
stuff)
(return (make-constant #f)))
; Given an expression, an environment, the available expressions,
; and an ordered list of likely register variables (used heuristically),
; returns the transformed expression and its set of free variables.
(define (scan-body E env available regvars)
; The local variables are those that are bound by a LET within
; this procedure. The formals of a lambda expression and the
; known local procedures are counted as non-global, not local,
; because there is no let-binding for a formal that can be
; renamed during register targeting.
; For each local variable, we keep track of how many times it
; is referenced. This information is not accurate until we
; are backing out of the recursion, and does not have to be.
(define local-variables (make-hashtable symbol-hash assq))
(define (local-variable? sym)
(hashtable-get local-variables sym))
(define (local-variable-not-used? sym)
(= 0 (hashtable-fetch local-variables sym -1)))
(define (local-variable-used-once? sym)
(= 1 (hashtable-fetch local-variables sym 0)))
(define (record-local-variable! sym)
(hashtable-put! local-variables sym 0))
(define (used-local-variable! sym)
(adjust-local-variable! sym 1))
(define (adjust-local-variable! sym n)
(let ((m (hashtable-get local-variables sym)))
(if debugging?
(if (and m (> m 0))
(begin (write (list sym (+ m n)))
(newline))))
(if m
(hashtable-put! local-variables
sym
(+ m n)))))
(define (closed-over-local-variable! sym)
; Set its reference count to infinity so it won't be optimized away.
; FIXME: One million isn't infinity.
(hashtable-put! local-variables sym 1000000))
(define (used-variable! sym)
(used-local-variable! sym))
(define (abandon-expression! E)
(cond ((variable? E)
(adjust-local-variable! (variable.name E) -1))
((conditional? E)
(abandon-expression! (if.test E))
(abandon-expression! (if.then E))
(abandon-expression! (if.else E)))
((call? E)
(for-each (lambda (exp)
(if (variable? exp)
(let ((name (variable.name exp)))
(if (local-variable? name)
(adjust-local-variable! name -1)))))
(cons (call.proc E)
(call.args E))))))
; Environments are represented as hashtrees.
(define (make-empty-environment)
(make-hashtree symbol-hash assq))
(define (environment-extend env sym)
(hashtree-put env sym #t))
(define (environment-extend* env symbols)
(if (null? symbols)
env
(environment-extend* (hashtree-put env (car symbols) #t)
(cdr symbols))))
(define (environment-lookup env sym)
(hashtree-get env sym))
(define (global? x)
(cond ((local-variable? x)
#f)
((environment-lookup env x)
#f)
(else
#t)))
;
(define (available-add! available T E)
(cond ((constant? E)
(available-extend! available T E available:killer:immortal))
((variable? E)
(available-extend! available
T
E
(if (global? (variable.name E))
available:killer:globals
available:killer:immortal)))
(else
(let ((entry (prim-call E)))
(if entry
(let ((killer (prim-lives-until entry)))
(if (not (eq? killer available:killer:dead))
(do ((args (call.args E) (cdr args))
(k killer
(let ((arg (car args)))
(if (and (variable? arg)
(global? (variable.name arg)))
available:killer:globals
k))))
((null? args)
(available-extend!
available
T
E
(logior killer k)))))))))))
; Given an expression E,
; an environment containing all variables that are in scope,
; and a table of available expressions,
; returns multiple values:
; the transformed E
; the free variables of E
; the register bindings to be inserted; each binding has the form
; (R x (begin R)), where (begin R) is a reference to R.
;
; Side effects E.
(define (scan E env available)
(if (not (call? E))
(scan-rhs E env available)
(let ((proc (call.proc E)))
(if (not (lambda? proc))
(scan-rhs E env available)
(let ((vars (lambda.args proc)))
(cond ((null? vars)
(scan-let0 E env available))
((null? (cdr vars))
(scan-binding E env available))
(else
(error (make-readable E)))))))))
; E has the form of (let ((T1 E1)) E0).
(define (scan-binding E env available)
(let* ((L (call.proc E))
(T1 (car (lambda.args L)))
(E1 (car (call.args E)))
(E0 (lambda.body L)))
(record-local-variable! T1)
(call-with-values
(lambda () (scan-rhs E1 env available))
(lambda (E1 F1 regbindings1)
(available-add! available T1 E1)
(let* ((env (let ((formals
(make-null-terminated (lambda.args L))))
(environment-extend*
(environment-extend* env formals)
(map def.lhs (lambda.defs L)))))
(Fdefs (scan-defs L env available)))
(call-with-values
(lambda () (scan E0 env available))
(lambda (E0 F0 regbindings0)
(lambda.body-set! L E0)
(if target-registers?
(scan-binding-phase2
L T1 E0 E1 F0 F1 Fdefs regbindings0 regbindings1)
(scan-binding-phase3
L E0 E1 (union F0 Fdefs)
F1 regbindings0 regbindings1)))))))))
; Given the lambda expression for a let expression that binds
; a single variable T1, the transformed body E0 and right hand side E1,
; their sets of free variables F0 and F1, the set of free variables
; for the internal definitions of L, and the sets of register
; bindings that need to be wrapped around E0 and E1, returns the
; transformed let expression, its free variables, and register
; bindings.
;
; This phase is concerned exclusively with register bindings,
; and is bypassed unless the target-registers flag is specified.
(define (scan-binding-phase2
L T1 E0 E1 F0 F1 Fdefs regbindings0 regbindings1)
; T1 can't be a register because we haven't
; yet inserted register bindings that high up.
; Classify the register bindings that need to wrapped around E0:
; 1. those that have T1 as their rhs
; 2. those whose lhs is a register that is likely to hold
; a variable that occurs free in E1
; 3. all others
(define (phase2a)
(do ((rvars regvars (cdr rvars))
(regs argument-registers (cdr regs))
(regs1 '() (if (memq (car rvars) F1)
(cons (car regs) regs1)
regs1)))
((or (null? rvars)
(null? regs))
; regs1 is the set of registers that are live for E1
(let loop ((regbindings regbindings0)
(rb1 '())
(rb2 '())
(rb3 '()))
(if (null? regbindings)
(phase2b rb1 rb2 rb3)
(let* ((binding (car regbindings))
(regbindings (cdr regbindings))
(lhs (regbinding.lhs binding))
(rhs (regbinding.rhs binding)))
(cond ((eq? rhs T1)
(loop regbindings
(cons binding rb1)
rb2
rb3))
((memq lhs regs1)
(loop regbindings
rb1
(cons binding rb2)
rb3))
(else
(loop regbindings
rb1
rb2
(cons binding rb3))))))))))
; Determine which categories of register bindings should be
; wrapped around E0.
; Always wrap the register bindings in category 2.
; If E1 is a conditional or a real call, then wrap category 3.
; If T1 might be used more than once, then wrap category 1.
(define (phase2b rb1 rb2 rb3)
(if (or (conditional? E1)
(real-call? E1))
(phase2c (append rb2 rb3) rb1 '())
(phase2c rb2 rb1 rb3)))
(define (phase2c towrap rb1 regbindings0)
(cond ((and (not (null? rb1))
(local-variable-used-once? T1))
(phase2d towrap rb1 regbindings0))
(else
(phase2e (append rb1 towrap) regbindings0))))
; T1 is used only once, and there is a register binding (R T1).
; Change T1 to R.
(define (phase2d towrap regbindings-T1 regbindings0)
(if (not (null? (cdr regbindings-T1)))
(error "incorrect number of uses" T1))
(let* ((regbinding (car regbindings-T1))
(R (regbinding.lhs regbinding)))
(lambda.args-set! L (list R))
(phase2e towrap regbindings0)))
; Wrap the selected register bindings around E0.
(define (phase2e towrap regbindings0)
(call-with-values
(lambda ()
(wrap-with-register-bindings towrap E0 F0))
(lambda (E0 F0)
(let ((F (union Fdefs F0)))
(scan-binding-phase3
L E0 E1 F F1 regbindings0 regbindings1)))))
(phase2a))
; This phase, with arguments as above, constructs the result.
(define (scan-binding-phase3 L E0 E1 F F1 regbindings0 regbindings1)
(let* ((args (lambda.args L))
(T1 (car args))
(free (union F1 (difference F args)))
(simple-let? (simple-lambda? L))
(regbindings
; At least one of regbindings0 and regbindings1
; is the empty list.
(cond ((null? regbindings0)
regbindings1)
((null? regbindings1)
regbindings0)
(else
(error 'scan-binding 'regbindings)))))
(lambda.body-set! L E0)
(lambda.F-set! L F)
(lambda.G-set! L F)
(cond ((and simple-let?
(not (memq T1 F))
(no-side-effects? E1))
(abandon-expression! E1)
(values E0 F regbindings0))
((and target-registers?
simple-let?
(local-variable-used-once? T1))
(post-simplify-anf L T1 E0 E1 free regbindings #f))
(else
(values (make-call L (list E1))
free
regbindings)))))
(define (scan-let0 E env available)
(let ((L (call.proc E)))
(if (simple-lambda? L)
(scan (lambda.body L) env available)
(let ((T1 (make-variable name:IGNORED)))
(lambda.args-set! L (list T1))
(call-with-values
(lambda () (scan (make-call L (list (make-constant 0)))
env
available))
(lambda (E F regbindings)
(lambda.args-set! L '())
(values (make-call L '())
F
regbindings)))))))
; Optimizes the internal definitions of L and returns their
; free variables.
(define (scan-defs L env available)
(let loop ((defs (lambda.defs L))
(newdefs '())
(Fdefs '()))
(if (null? defs)
(begin (lambda.defs-set! L (reverse newdefs))
Fdefs)
(let ((def (car defs)))
(call-with-values
(lambda ()
(let* ((Ldef (def.rhs def))
(Lformals (make-null-terminated (lambda.args Ldef)))
(Lenv (environment-extend*
(environment-extend* env Lformals)
(map def.lhs (lambda.defs Ldef)))))
(scan Ldef Lenv available)))
(lambda (rhs Frhs empty)
(if (not (null? empty))
(error 'scan-binding 'def))
(loop (cdr defs)
(cons (make-definition (def.lhs def) rhs)
newdefs)
(union Frhs Fdefs))))))))
; Given the right-hand side of a let-binding, an environment,
; and a table of available expressions, returns the transformed
; expression, its free variables, and the register bindings that
; need to be wrapped around it.
(define (scan-rhs E env available)
(cond
((constant? E)
(values E (empty-set) '()))
((variable? E)
(let* ((name (variable.name E))
(Enew (and commoning?
(if (global? name)
(let ((T (available-expression
available E)))
(if T
(make-variable T)
#f))
(available-variable available name)))))
(if Enew
(scan-rhs Enew env available)
(begin (used-variable! name)
(values E (list name) '())))))
((lambda? E)
(let* ((formals (make-null-terminated (lambda.args E)))
(env (environment-extend*
(environment-extend* env formals)
(map def.lhs (lambda.defs E))))
(Fdefs (scan-defs E env available)))
(call-with-values
(lambda ()
(let ((available (copy-available-table available)))
(available-kill! available available:killer:all)
(scan-body (lambda.body E)
env
available
formals)))
(lambda (E0 F0 regbindings0)
(call-with-values
(lambda ()
(wrap-with-register-bindings regbindings0 E0 F0))
(lambda (E0 F0)
(lambda.body-set! E E0)
(let ((F (union Fdefs F0)))
(for-each (lambda (x)
(closed-over-local-variable! x))
F)
(lambda.F-set! E F)
(lambda.G-set! E F)
(values E
(difference F
(make-null-terminated
(lambda.args E)))
'()))))))))
((conditional? E)
(let ((E0 (if.test E))
(E1 (if.then E))
(E2 (if.else E)))
(if (constant? E0)
; FIXME: E1 and E2 might not be a legal rhs,
; so we can't just return the simplified E1 or E2.
(let ((E1 (if (constant.value E0) E1 E2)))
(call-with-values
(lambda () (scan E1 env available))
(lambda (E1 F1 regbindings1)
(cond ((or (not (call? E1))
(not (lambda? (call.proc E1))))
(values E1 F1 regbindings1))
(else
; FIXME: Must return a valid rhs.
(values (make-conditional
(make-constant #t)
E1
(make-constant 0))
F1
regbindings1))))))
(call-with-values
(lambda () (scan E0 env available))
(lambda (E0 F0 regbindings0)
(if (not (null? regbindings0))
(error 'scan-rhs 'if))
(if (not (eq? E0 (if.test E)))
(scan-rhs (make-conditional E0 E1 E2)
env available)
(let ((available1
(copy-available-table available))
(available2
(copy-available-table available)))
(if (variable? E0)
(let ((T0 (variable.name E0)))
(available-add!
available2 T0 (make-constant #f)))
(error (make-readable E #t)))
(call-with-values
(lambda () (scan E1 env available1))
(lambda (E1 F1 regbindings1)
(call-with-values
(lambda ()
(wrap-with-register-bindings
regbindings1 E1 F1))
(lambda (E1 F1)
(call-with-values
(lambda () (scan E2 env available2))
(lambda (E2 F2 regbindings2)
(call-with-values
(lambda ()
(wrap-with-register-bindings
regbindings2 E2 F2))
(lambda (E2 F2)
(let ((E (make-conditional
E0 E1 E2))
(F (union F0 F1 F2)))
(available-intersect!
available
available1
available2)
(values E F '())))))))))))))))))
((assignment? E)
(call-with-values
(lambda () (scan-rhs (assignment.rhs E) env available))
(lambda (E1 F1 regbindings1)
(if (not (null? regbindings1))
(error 'scan-rhs 'set!))
(available-kill! available available:killer:globals)
(values (make-assignment (assignment.lhs E) E1)
(union (list (assignment.lhs E)) F1)
'()))))
((begin? E)
; Shouldn't occur in A-normal form.
(error 'scan-rhs 'begin))
((real-call? E)
(let* ((E0 (call.proc E))
(args (call.args E))
(regcontents (append regvars
(map (lambda (x) #f) args))))
(let loop ((args args)
(regs argument-registers)
(regcontents regcontents)
(newargs '())
(regbindings '())
(F (if (variable? E0)
(let ((f (variable.name E0)))
(used-variable! f)
(list f))
(empty-set))))
(cond ((null? args)
(available-kill! available available:killer:all)
(values (make-call E0 (reverse newargs))
F
regbindings))
((null? regs)
(let ((arg (car args)))
(loop (cdr args)
'()
(cdr regcontents)
(cons arg newargs)
regbindings
(if (variable? arg)
(let ((name (variable.name arg)))
(used-variable! name)
(union (list name) F))
F))))
((and commoning?
(variable? (car args))
(available-variable
available
(variable.name (car args))))
(let* ((name (variable.name (car args)))
(Enew (available-variable available name)))
(loop (cons Enew (cdr args))
regs regcontents newargs regbindings F)))
((and target-registers?
(variable? (car args))
(let ((x (variable.name (car args))))
; We haven't yet recorded this use.
(or (local-variable-not-used? x)
(and (memq x regvars)
(not (eq? x (car regcontents)))))))
(let* ((x (variable.name (car args)))
(R (car regs))
(newarg (make-variable R)))
(used-variable! x)
(loop (cdr args)
(cdr regs)
(cdr regcontents)
(cons newarg newargs)
(cons (make-regbinding R x newarg)
regbindings)
(union (list R) F))))
(else
(let ((E1 (car args)))
(loop (cdr args)
(cdr regs)
(cdr regcontents)
(cons E1 newargs)
regbindings
(if (variable? E1)
(let ((name (variable.name E1)))
(used-variable! name)
(union (list name) F))
F))))))))
((call? E)
; Must be a call to a primop.
(let* ((E0 (call.proc E))
(f0 (variable.name E0)))
(let loop ((args (call.args E))
(newargs '())
(F (list f0)))
(cond ((null? args)
(let* ((E (make-call E0 (reverse newargs)))
(T (and commoning?
(available-expression
available E))))
(if T
(begin (abandon-expression! E)
(scan-rhs (make-variable T) env available))
(begin
(available-kill!
available
(prim-kills (prim-entry f0)))
(cond ((eq? f0 name:check!)
(let ((x (car (call.args E))))
(cond ((not (runtime-safety-checking))
(abandon-expression! E)
;(values x '() '())
(scan-rhs x env available))
((variable? x)
(available-add!
available
(variable.name x)
(make-constant #t))
(values E F '()))
((constant.value x)
(abandon-expression! E)
(values x '() '()))
(else
(declaration-error E)
(values E F '())))))
(else
(values E F '())))))))
((variable? (car args))
(let* ((E1 (car args))
(x (variable.name E1))
(Enew
(and commoning?
(available-variable available x))))
(if Enew
; All of the arguments are constants or
; variables, so if the variable is replaced
; here it will be replaced throughout the call.
(loop (cons Enew (cdr args))
newargs
(remq x F))
(begin
(used-variable! x)
(loop (cdr args)
(cons (car args) newargs)
(union (list x) F))))))
(else
(loop (cdr args)
(cons (car args) newargs)
F))))))
(else
(error 'scan-rhs (make-readable E)))))
(call-with-values
(lambda () (scan E env available))
(lambda (E F regbindings)
(call-with-values
(lambda () (wrap-with-register-bindings regbindings E F))
(lambda (E F)
(values E F '()))))))
(call-with-values
(lambda ()
(scan-body E
(make-hashtree symbol-hash assq)
(make-available-table)
'()))
(lambda (E F regbindings)
(if (not (null? regbindings))
(error 'scan-body))
E)))))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 16 June 1999.
;
; Intraprocedural representation inference.
(define (representation-analysis exp)
(let* ((debugging? #f)
(integrate-usual? (integrate-usual-procedures))
(known (make-hashtable symbol-hash assq))
(types (make-hashtable symbol-hash assq))
(g (callgraph exp))
(schedule (list (callgraphnode.code (car g))))
(changed? #f)
(mutate? #f))
; known is a hashtable that maps the name of a known local procedure
; to a list of the form (tv1 ... tvN), where tv1, ..., tvN
; are type variables that stand for the representation types of its
; arguments. The type variable that stands for the representation
; type of the result of the procedure has the same name as the
; procedure itself.
; types is a hashtable that maps local variables and the names
; of known local procedures to an approximation of their
; representation type.
; For a known local procedure, the representation type is for the
; result of the procedure, not the procedure itself.
; schedule is a stack of work that needs to be done.
; Each entry in the stack is either an escaping lambda expression
; or the name of a known local procedure.
(define (schedule! job)
(if (not (memq job schedule))
(begin (set! schedule (cons job schedule))
(if (not (symbol? job))
(callgraphnode.info! (lookup-node job) #t)))))
; Schedules a known local procedure.
(define (schedule-known-procedure! name)
; Mark every known procedure that can actually be called.
(callgraphnode.info! (assq name g) #t)
(schedule! name))
; Schedule all code that calls the given known local procedure.
(define (schedule-callers! name)
(for-each (lambda (node)
(if (and (callgraphnode.info node)
(or (memq name (callgraphnode.tailcalls node))
(memq name (callgraphnode.nontailcalls node))))
(let ((caller (callgraphnode.name node)))
(if caller
(schedule! caller)
(schedule! (callgraphnode.code node))))))
g))
; Schedules local procedures of a lambda expression.
(define (schedule-local-procedures! L)
(for-each (lambda (def)
(let ((name (def.lhs def)))
(if (known-procedure-is-callable? name)
(schedule! name))))
(lambda.defs L)))
; Returns true iff the given known procedure is known to be callable.
(define (known-procedure-is-callable? name)
(callgraphnode.info (assq name g)))
; Sets CHANGED? to #t and returns #t if the type variable's
; approximation has changed; otherwise returns #f.
(define (update-typevar! tv type)
(let* ((type0 (hashtable-get types tv))
(type0 (or type0
(begin (hashtable-put! types tv rep:bottom)
rep:bottom)))
(type1 (representation-union type0 type)))
(if (eq? type0 type1)
#f
(begin (hashtable-put! types tv type1)
(set! changed? #t)
(if (and debugging? mutate?)
(begin (display "******** Changing type of ")
(display tv)
(display " from ")
(display (rep->symbol type0))
(display " to ")
(display (rep->symbol type1))
(newline)))
#t))))
; GIven the name of a known local procedure, returns its code.
(define (lookup-code name)
(callgraphnode.code (assq name g)))
; Given a lambda expression, either escaping or the code for
; a known local procedure, returns its node in the call graph.
(define (lookup-node L)
(let loop ((g g))
(cond ((null? g)
(error "Unknown lambda expression" (make-readable L #t)))
((eq? L (callgraphnode.code (car g)))
(car g))
(else
(loop (cdr g))))))
; Given: a type variable, expression, and a set of constraints.
; Side effects:
; Update the representation types of all variables that are
; bound within the expression.
; Update the representation types of all arguments to known
; local procedures that are called within the expression.
; If the representation type of an argument to a known local
; procedure changes, then schedule that procedure's code
; for analysis.
; Update the constraint set to reflect the constraints that
; hold following execution of the expression.
; If mutate? is true, then transform the expression to rely
; on the representation types that have been inferred.
; Return: type of the expression under the current assumptions
; and constraints.
(define (analyze exp constraints)
(if (and #f debugging?)
(begin (display "Analyzing: ")
(newline)
(pretty-print (make-readable exp #t))
(newline)))
(case (car exp)
((quote)
(representation-of-value (constant.value exp)))
((begin)
(let* ((name (variable.name exp)))
(representation-typeof name types constraints)))
((lambda)
(schedule! exp)
rep:procedure)
((set!)
(analyze (assignment.rhs exp) constraints)
(constraints-kill! constraints available:killer:globals)
rep:object)
((if)
(let* ((E0 (if.test exp))
(E1 (if.then exp))
(E2 (if.else exp))
(type0 (analyze E0 constraints)))
(if mutate?
(cond ((representation-subtype? type0 rep:true)
(if.test-set! exp (make-constant #t)))
((representation-subtype? type0 rep:false)
(if.test-set! exp (make-constant #f)))))
(cond ((representation-subtype? type0 rep:true)
(analyze E1 constraints))
((representation-subtype? type0 rep:false)
(analyze E2 constraints))
((variable? E0)
(let* ((T0 (variable.name E0))
(ignored (analyze E0 constraints))
(constraints1 (copy-constraints-table constraints))
(constraints2 (copy-constraints-table constraints)))
(constraints-add! types
constraints1
(make-type-constraint
T0 rep:true available:killer:immortal))
(constraints-add! types
constraints2
(make-type-constraint
T0 rep:false available:killer:immortal))
(let* ((type1 (analyze E1 constraints1))
(type2 (analyze E2 constraints2))
(type (representation-union type1 type2)))
(constraints-intersect! constraints
constraints1
constraints2)
type)))
(else
(representation-error "Bad ANF" (make-readable exp #t))))))
(else
(let ((proc (call.proc exp))
(args (call.args exp)))
(cond ((lambda? proc)
(cond ((null? args)
(analyze-let0 exp constraints))
((null? (cdr args))
(analyze-let1 exp constraints))
(else
(error "Compiler bug: pass3rep"))))
((variable? proc)
(let* ((procname (variable.name proc)))
(cond ((hashtable-get known procname)
=>
(lambda (vars)
(analyze-known-call exp constraints vars)))
(integrate-usual?
(let ((entry (prim-entry procname)))
(if entry
(analyze-primop-call exp constraints entry)
(analyze-unknown-call exp constraints))))
(else
(analyze-unknown-call exp constraints)))))
(else
(analyze-unknown-call exp constraints)))))))
(define (analyze-let0 exp constraints)
(let ((proc (call.proc exp)))
(schedule-local-procedures! proc)
(if (null? (lambda.args proc))
(analyze (lambda.body exp) constraints)
(analyze-unknown-call exp constraints))))
(define (analyze-let1 exp constraints)
(let* ((proc (call.proc exp))
(vars (lambda.args proc)))
(schedule-local-procedures! proc)
(if (and (pair? vars)
(null? (cdr vars)))
(let* ((T1 (car vars))
(E1 (car (call.args exp))))
(if (and integrate-usual? (call? E1))
(let ((proc (call.proc E1))
(args (call.args E1)))
(if (variable? proc)
(let* ((op (variable.name proc))
(entry (prim-entry op))
(K1 (if entry
(prim-lives-until entry)
available:killer:dead)))
(if (not (= K1 available:killer:dead))
; Must copy the call to avoid problems
; with side effects when mutate? is true.
(constraints-add!
types
constraints
(make-constraint T1
(make-call proc args)
K1)))))))
(update-typevar! T1 (analyze E1 constraints))
(analyze (lambda.body proc) constraints))
(analyze-unknown-call exp constraints))))
(define (analyze-primop-call exp constraints entry)
(let* ((op (prim-opcodename entry))
(args (call.args exp))
(argtypes (map (lambda (arg) (analyze arg constraints))
args))
(type (rep-result? op argtypes)))
(constraints-kill! constraints (prim-kills entry))
(cond ((and (eq? op 'check!)
(variable? (car args)))
(let ((varname (variable.name (car args))))
(if (and mutate?
(representation-subtype? (car argtypes) rep:true))
(call.args-set! exp
(cons (make-constant #t) (cdr args))))
(constraints-add! types
constraints
(make-type-constraint
varname
rep:true
available:killer:immortal))))
((and mutate? (rep-specific? op argtypes))
=>
(lambda (newop)
(call.proc-set! exp (make-variable newop)))))
(or type rep:object)))
(define (analyze-known-call exp constraints vars)
(let* ((procname (variable.name (call.proc exp)))
(args (call.args exp))
(argtypes (map (lambda (arg) (analyze arg constraints))
args)))
(if (not (known-procedure-is-callable? procname))
(schedule-known-procedure! procname))
(for-each (lambda (var type)
(if (update-typevar! var type)
(schedule-known-procedure! procname)))
vars
argtypes)
; FIXME: We aren't analyzing the effects of known local procedures.
(constraints-kill! constraints available:killer:all)
(hashtable-get types procname)))
(define (analyze-unknown-call exp constraints)
(analyze (call.proc exp) constraints)
(for-each (lambda (arg) (analyze arg constraints))
(call.args exp))
(constraints-kill! constraints available:killer:all)
rep:object)
(define (analyze-known-local-procedure name)
(if debugging?
(begin (display "Analyzing ")
(display name)
(newline)))
(let ((L (lookup-code name))
(constraints (make-constraints-table)))
(schedule-local-procedures! L)
(let ((type (analyze (lambda.body L) constraints)))
(if (update-typevar! name type)
(schedule-callers! name))
type)))
(define (analyze-unknown-lambda L)
(if debugging?
(begin (display "Analyzing escaping lambda expression")
(newline)))
(schedule-local-procedures! L)
(let ((vars (make-null-terminated (lambda.args L))))
(for-each (lambda (var)
(hashtable-put! types var rep:object))
vars)
(analyze (lambda.body L)
(make-constraints-table))))
; For debugging.
(define (display-types)
(hashtable-for-each (lambda (f vars)
(write f)
(display " : returns ")
(write (rep->symbol (hashtable-get types f)))
(newline)
(for-each (lambda (x)
(display " ")
(write x)
(display ": ")
(write (rep->symbol
(hashtable-get types x)))
(newline))
vars))
known))
(define (display-all-types)
(let* ((vars (hashtable-map (lambda (x type) x) types))
(vars (twobit-sort (lambda (var1 var2)
(string<=? (symbol->string var1)
(symbol->string var2)))
vars)))
(for-each (lambda (x)
(write x)
(display ": ")
(write (rep->symbol
(hashtable-get types x)))
(newline))
vars)))
'
(if debugging?
(begin (pretty-print (make-readable (car schedule) #t))
(newline)))
(if debugging?
(view-callgraph g))
(for-each (lambda (node)
(let* ((name (callgraphnode.name node))
(code (callgraphnode.code node))
(vars (make-null-terminated (lambda.args code)))
(known? (symbol? name))
(rep (if known? rep:bottom rep:object)))
(callgraphnode.info! node #f)
(if known?
(begin (hashtable-put! known name vars)
(hashtable-put! types name rep)))
(for-each (lambda (var)
(hashtable-put! types var rep))
vars)))
g)
(let loop ()
(cond ((not (null? schedule))
(let ((job (car schedule)))
(set! schedule (cdr schedule))
(if (symbol? job)
(analyze-known-local-procedure job)
(analyze-unknown-lambda job))
(loop)))
(changed?
(set! changed? #f)
(set! schedule (list (callgraphnode.code (car g))))
(if debugging?
(begin (display-all-types) (newline)))
(loop))))
(if debugging?
(display-types))
(set! mutate? #t)
; We don't want to analyze known procedures that are never called.
(set! schedule
(cons (callgraphnode.code (car g))
(map callgraphnode.name
(filter (lambda (node)
(let* ((name (callgraphnode.name node))
(known? (symbol? name))
(marked?
(known-procedure-is-callable? name)))
(callgraphnode.info! node #f)
(and known? marked?)))
g))))
(let loop ()
(if (not (null? schedule))
(let ((job (car schedule)))
(set! schedule (cdr schedule))
(if (symbol? job)
(analyze-known-local-procedure job)
(analyze-unknown-lambda job))
(loop))))
(if changed?
(error "Compiler bug in representation inference"))
(if debugging?
(pretty-print (make-readable (callgraphnode.code (car g)) #t)))
exp))
; Copyright 1999 William D Clinger.
;
; Permission to copy this software, in whole or in part, to use this
; software for any lawful noncommercial purpose, and to redistribute
; this software is granted subject to the restriction that all copies
; made of this software must include this copyright notice in full.
;
; I also request that you send me a copy of any improvements that you
; make to this software so that they may be incorporated within it to
; the benefit of the Scheme community.
;
; 11 June 1999.
;
; The third "pass" of the Twobit compiler actually consists of several
; passes, which are related by the common theme of flow analysis:
; interprocedural inlining of known local procedures
; interprocedural constant propagation and folding
; intraprocedural commoning, copy propagation, and dead code elimination
; representation inference (not yet implemented)
; register targeting
;
; This pass operates as source-to-source transformations on
; expressions written in the subset of Scheme described by the
; following grammar:
;
; "X ..." means zero or more occurrences of X.
;
; L --> (lambda (I_1 ...)
; (begin D ...)
; (quote (R F G <decls> <doc>)
; E)
; | (lambda (I_1 ... . I_rest)
; (begin D ...)
; (quote (R F G <decls> <doc>))
; E)
; D --> (define I L)
; E --> (quote K) ; constants
; | (begin I) ; variable references
; | L ; lambda expressions
; | (E0 E1 ...) ; calls
; | (set! I E) ; assignments
; | (if E0 E1 E2) ; conditionals
; | (begin E0 E1 E2 ...) ; sequential expressions
; I --> <identifier>
;
; R --> ((I <references> <assignments> <calls>) ...)
; F --> (I ...)
; G --> (I ...)
;
; Invariants that hold for the input only:
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * R, F, and G are garbage.
; * Variables named IGNORED are neither referenced nor assigned.
; * The expression does not share structure with the original input,
; but might share structure with itself.
;
; Invariants that hold for the output only:
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * R is garbage.
; * For each lambda expression, the associated F is a list of all
; the identifiers that occur free in the body of that lambda
; expression, and possibly a few extra identifiers that were
; once free but have been removed by optimization.
; * If a lambda expression is declared to be in A-normal form (see
; pass3anormal.sch), then it really is in A-normal form.
;
; The phases of pass 3 interact with the referencing information R
; and the free variables F as follows:
;
; Inlining ignores R, ignores F, destroys R, destroys F.
; Constant propagation uses R, ignores F, preserves R, preserves F.
; Conversion to ANF ignores R, ignores F, destroys R, destroys F.
; Commoning ignores R, ignores F, destroys R, computes F.
; Register targeting ignores R, ignores F, destroys R, computes F.
(define (pass3 exp)
(define (phase1 exp)
(if (interprocedural-inlining)
(let ((g (callgraph exp)))
(inline-using-callgraph! g)
exp)
exp))
(define (phase2 exp)
(if (interprocedural-constant-propagation)
(constant-propagation (copy-exp exp))
exp))
(define (phase3 exp)
(if (common-subexpression-elimination)
(let* ((exp (if (interprocedural-constant-propagation)
exp
; alpha-conversion
(copy-exp exp)))
(exp (a-normal-form exp)))
(if (representation-inference)
(intraprocedural-commoning exp 'commoning)
(intraprocedural-commoning exp)))
exp))
(define (phase4 exp)
(if (representation-inference)
(let ((exp (cond ((common-subexpression-elimination)
exp)
((interprocedural-constant-propagation)
(a-normal-form exp))
(else
; alpha-conversion
(a-normal-form (copy-exp exp))))))
(intraprocedural-commoning
(representation-analysis exp)))
exp))
(define (finish exp)
(if (and (not (interprocedural-constant-propagation))
(not (common-subexpression-elimination)))
(begin (compute-free-variables! exp)
exp)
;(make-begin (list (make-constant 'anf) exp))))
exp))
(define (verify exp)
(check-referencing-invariants exp 'free)
exp)
(if (global-optimization)
(verify (finish (phase4 (phase3 (phase2 (phase1 exp))))))
(begin (compute-free-variables! exp)
(verify exp))))
; Copyright 1991 Lightship Software, Incorporated.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 4 June 1999
; Implements the following abstract data types.
;
; labels
; (init-labels)
; (make-label)
; cg-label-counter
;
; assembly streams
; (make-assembly-stream)
; (assembly-stream-code as)
; (gen! as . instruction)
; (gen-instruction! as instruction)
; (gen-save! as frame)
; (gen-restore! as frame)
; (gen-pop! as frame)
; (gen-setstk! as frame v)
; (gen-store! as frame r v)
; (gen-load! as frame r v)
; (gen-stack! as frame v)
;
; temporaries
; (init-temps)
; (newtemp)
; (newtemps)
; newtemp-counter
;
; register environments
; (cgreg-initial)
; (cgreg-copy regs)
; (cgreg-tos regs)
; (cgreg-liveregs regs)
; (cgreg-live regs r)
; (cgreg-vars regs)
; (cgreg-bind! regs r v)
; (cgreg-bindregs! regs vars)
; (cgreg-rename! regs alist)
; (cgreg-release! regs r)
; (cgreg-clear! regs)
; (cgreg-lookup regs var)
; (cgreg-lookup-reg regs r)
; (cgreg-join! regs1 regs2)
;
; stack frame environments
; (cgframe-initial)
; (cgframe-size-cell frame)
; (cgframe-size frame)
; (cgframe-copy frame)
; (cgframe-join! frame1 frame2)
; (cgframe-update-stale! frame)
; (cgframe-used! frame)
; (cgframe-bind! frame n v instruction)
; (cgframe-touch! frame v)
; (cgframe-rename! frame alist)
; (cgframe-release! frame v)
; (cgframe-lookup frame v)
; (cgframe-spilled? frame v)
;
; environments
; (entry.name entry)
; (entry.kind entry)
; (entry.rib entry)
; (entry.offset entry)
; (entry.label entry)
; (entry.regnum entry)
; (entry.arity entry)
; (entry.op entry)
; (entry.imm entry)
; (cgenv-initial)
; (cgenv-lookup env id)
; (cgenv-extend env vars procs)
; (cgenv-bindprocs env procs)
; (var-lookup var regs frame env)
; Labels.
(define (init-labels)
(set! cg-label-counter 1000))
(define (make-label)
(set! cg-label-counter (+ cg-label-counter 1))
cg-label-counter)
(define cg-label-counter 1000)
; an assembly stream into which instructions should be emitted
; an expression
; the desired target register ('result, a register number, or '#f)
; a register environment [cgreg]
; a stack-frame environment [cgframe]
; contains size of frame, current top of frame
; a compile-time environment [cgenv]
; a flag indicating whether the expression is in tail position
; Assembly streams, into which instructions are emitted by side effect.
; Represented as a list of two things:
;
; Assembly code, represented as a pair whose car is a nonempty list
; whose cdr is a possibly empty list of MacScheme machine assembly
; instructions, and whose cdr is the last pair of the car.
;
; Any Scheme object that the code generator wants to associate with
; this code.
(define (make-assembly-stream)
(let ((code (list (list 0))))
(set-cdr! code (car code))
(list code #f)))
(define (assembly-stream-code output)
(if (local-optimizations)
(filter-basic-blocks (cdar (car output)))
(cdar (car output))))
(define (assembly-stream-info output)
(cadr output))
(define (assembly-stream-info! output x)
(set-car! (cdr output) x)
#f)
(define (gen-instruction! output instruction)
(let ((pair (list instruction))
(code (car output)))
(set-cdr! (cdr code) pair)
(set-cdr! code pair)
output))
;
(define (gen! output . instruction)
(gen-instruction! output instruction))
(define (gen-save! output frame t0)
(let ((size (cgframe-size-cell frame)))
(gen-instruction! output (cons $save size))
(gen-store! output frame 0 t0)
(cgframe:stale-set! frame '())))
(define (gen-restore! output frame)
(let ((size (cgframe-size-cell frame)))
(gen-instruction! output (cons $restore size))))
(define (gen-pop! output frame)
(let ((size (cgframe-size-cell frame)))
(gen-instruction! output (cons $pop size))))
(define (gen-setstk! output frame tempname)
(let ((instruction (list $nop $setstk -1)))
(cgframe-bind! frame tempname instruction)
(gen-instruction! output instruction)))
(define (gen-store! output frame r tempname)
(let ((instruction (list $nop $store r -1)))
(cgframe-bind! frame tempname instruction)
(gen-instruction! output instruction)))
(define (gen-load! output frame r tempname)
(cgframe-touch! frame tempname)
(let ((n (entry.slotnum (cgframe-lookup frame tempname))))
(gen! output $load r n)))
(define (gen-stack! output frame tempname)
(cgframe-touch! frame tempname)
(let ((n (entry.slotnum (cgframe-lookup frame tempname))))
(gen! output $stack n)))
; Returns a temporary name.
; Temporaries are compared using EQ?, so the use of small
; exact integers as temporary names is implementation-dependent.
(define (init-temps)
(set! newtemp-counter 5000))
(define (newtemp)
(set! newtemp-counter
(+ newtemp-counter 1))
newtemp-counter)
(define newtemp-counter 5000)
(define (newtemps n)
(if (zero? n)
'()
(cons (newtemp)
(newtemps (- n 1)))))
; New representation of
; Register environments.
; Represented as a list of three items:
; an exact integer, one more than the highest index of a live register
; a mutable vector with *nregs* elements of the form
; #f (the register is dead)
; #t (the register is live)
; v (the register contains variable v)
; t (the register contains temporary variable t)
; a mutable vector of booleans: true if the register might be stale
(define (cgreg-makeregs n v1 v2) (list n v1 v2))
(define (cgreg-liveregs regs)
(car regs))
(define (cgreg-contents regs)
(cadr regs))
(define (cgreg-stale regs)
(caddr regs))
(define (cgreg-liveregs-set! regs n)
(set-car! regs n)
regs)
(define (cgreg-initial)
(let ((v1 (make-vector *nregs* #f))
(v2 (make-vector *nregs* #f)))
(cgreg-makeregs 0 v1 v2)))
(define (cgreg-copy regs)
(let* ((newregs (cgreg-initial))
(v1a (cgreg-contents regs))
(v2a (cgreg-stale regs))
(v1 (cgreg-contents newregs))
(v2 (cgreg-stale newregs))
(n (vector-length v1a)))
(cgreg-liveregs-set! newregs (cgreg-liveregs regs))
(do ((i 0 (+ i 1)))
((= i n)
newregs)
(vector-set! v1 i (vector-ref v1a i))
(vector-set! v2 i (vector-ref v2a i)))))
(define (cgreg-tos regs)
(- (cgreg-liveregs regs) 1))
(define (cgreg-live regs r)
(if (eq? r 'result)
(cgreg-tos regs)
(max r (cgreg-tos regs))))
(define (cgreg-vars regs)
(let ((m (cgreg-liveregs regs))
(v (cgreg-contents regs)))
(do ((i (- m 1) (- i 1))
(vars '()
(cons (vector-ref v i)
vars)))
((< i 0)
vars))))
(define (cgreg-bind! regs r t)
(let ((m (cgreg-liveregs regs))
(v (cgreg-contents regs)))
(vector-set! v r t)
(if (>= r m)
(cgreg-liveregs-set! regs (+ r 1)))))
(define (cgreg-bindregs! regs vars)
(do ((m (cgreg-liveregs regs) (+ m 1))
(v (cgreg-contents regs))
(vars vars (cdr vars)))
((null? vars)
(cgreg-liveregs-set! regs m)
regs)
(vector-set! v m (car vars))))
(define (cgreg-rename! regs alist)
(do ((i (- (cgreg-liveregs regs) 1) (- i 1))
(v (cgreg-contents regs)))
((negative? i))
(let ((var (vector-ref v i)))
(if var
(let ((probe (assv var alist)))
(if probe
(vector-set! v i (cdr probe))))))))
(define (cgreg-release! regs r)
(let ((m (cgreg-liveregs regs))
(v (cgreg-contents regs)))
(vector-set! v r #f)
(vector-set! (cgreg-stale regs) r #t)
(if (= r (- m 1))
(do ((m r (- m 1)))
((or (negative? m)
(vector-ref v m))
(cgreg-liveregs-set! regs (+ m 1)))))))
(define (cgreg-release-except! regs vars)
(do ((i (- (cgreg-liveregs regs) 1) (- i 1))
(v (cgreg-contents regs)))
((negative? i))
(let ((var (vector-ref v i)))
(if (and var (not (memq var vars)))
(cgreg-release! regs i)))))
(define (cgreg-clear! regs)
(let ((m (cgreg-liveregs regs))
(v1 (cgreg-contents regs))
(v2 (cgreg-stale regs)))
(do ((r 0 (+ r 1)))
((= r m)
(cgreg-liveregs-set! regs 0))
(vector-set! v1 r #f)
(vector-set! v2 r #t))))
(define (cgreg-lookup regs var)
(let ((m (cgreg-liveregs regs))
(v (cgreg-contents regs)))
(define (loop i)
(cond ((< i 0)
#f)
((eq? var (vector-ref v i))
(list var 'register i '(object)))
(else
(loop (- i 1)))))
(loop (- m 1))))
(define (cgreg-lookup-reg regs r)
(let ((m (cgreg-liveregs regs))
(v (cgreg-contents regs)))
(if (<= m r)
#f
(vector-ref v r))))
(define (cgreg-join! regs1 regs2)
(let ((m1 (cgreg-liveregs regs1))
(m2 (cgreg-liveregs regs2))
(v1 (cgreg-contents regs1))
(v2 (cgreg-contents regs2))
(stale1 (cgreg-stale regs1)))
(do ((i (- (max m1 m2) 1) (- i 1)))
((< i 0)
(cgreg-liveregs-set! regs1 (min m1 m2)))
(let ((x1 (vector-ref v1 i))
(x2 (vector-ref v2 i)))
(cond ((eq? x1 x2)
#t)
((not x1)
(if x2
(vector-set! stale1 i #t)))
(else
(vector-set! v1 i #f)
(vector-set! stale1 i #t)))))))
; New representation of
; Stack-frame environments.
; Represented as a three-element list.
;
; Its car is a list whose car is a list of slot entries, each
; of the form
; (v n instruction stale)
; where
; v is the name of a variable or temporary,
; n is #f or a slot number,
; instruction is a possibly phantom store or setstk instruction
; that stores v into slot n, and
; stale is a list of stale slot entries, each of the form
; (#t . n)
; or (#f . -1)
; where slot n had been allocated, initialized, and released
; before the store or setstk instruction was generated.
; Slot entries are updated by side effect.
;
; Its cadr is the list of currently stale slots.
;
; Its caddr is a list of variables that are free in the continuation,
; or #f if that information is unknown.
; This information allows a direct-style code generator to know when
; a slot becomes stale.
;
; Its cadddr is the size of the stack frame, which can be
; increased but not decreased. The cdddr of the stack frame
; environment is shared with the save instruction that
; created the frame. What a horrible crock!
; This stuff is private to the implementation of stack-frame
; environments.
(define cgframe:slots car)
(define cgframe:stale cadr)
(define cgframe:livevars caddr)
(define cgframe:slot.name car)
(define cgframe:slot.offset cadr)
(define cgframe:slot.instruction caddr)
(define cgframe:slot.stale cadddr)
(define cgframe:slots-set! set-car!)
(define (cgframe:stale-set! frame stale)
(set-car! (cdr frame) stale))
(define (cgframe:livevars-set! frame vars)
(set-car! (cddr frame) vars))
(define cgframe:slot.name-set! set-car!)
(define (cgframe:slot.offset-set! entry n)
(let ((instruction (caddr entry)))
(if (or (not (eq? #f (cadr entry)))
(not (eq? $nop (car instruction))))
(error "Compiler bug: cgframe" entry)
(begin
(set-car! (cdr entry) n)
(set-car! instruction (cadr instruction))
(set-cdr! instruction (cddr instruction))
(if (eq? $setstk (car instruction))
(set-car! (cdr instruction) n)
(set-car! (cddr instruction) n))))))
; Reserves a slot offset that was unused where the instruction
; of the slot entry was generated, and returns that offset.
(define (cgframe:unused-slot frame entry)
(let* ((stale (cgframe:slot.stale entry))
(probe (assq #t stale)))
(if probe
(let ((n (cdr probe)))
(if (zero? n)
(cgframe-used! frame))
(set-car! probe #f)
n)
(let* ((cell (cgframe-size-cell frame))
(n (+ 1 (car cell))))
(set-car! cell n)
(if (zero? n)
(cgframe:unused-slot frame entry)
n)))))
; Public entry points.
; The runtime system requires slot 0 of a frame to contain
; a closure whose code pointer contains the return address
; of the frame.
; To prevent slot 0 from being used for some other purpose,
; we rely on a complex trick: Slot 0 is initially stale.
; Gen-save! generates a store instruction for register 0,
; with slot 0 as the only stale slot for that instruction;
; then gen-save! clears the frame's set of stale slots, which
; prevents other store instructions from using slot 0.
(define (cgframe-initial)
(list '()
(list (cons #t 0))
'#f
-1))
(define cgframe-livevars cgframe:livevars)
(define cgframe-livevars-set! cgframe:livevars-set!)
(define (cgframe-size-cell frame)
(cdddr frame))
(define (cgframe-size frame)
(car (cgframe-size-cell frame)))
(define (cgframe-used! frame)
(if (negative? (cgframe-size frame))
(set-car! (cgframe-size-cell frame) 0)))
; Called only by gen-store!, gen-setstk!
(define (cgframe-bind! frame var instruction)
(cgframe:slots-set! frame
(cons (list var #f instruction (cgframe:stale frame))
(cgframe:slots frame))))
; Called only by gen-load!, gen-stack!
(define (cgframe-touch! frame var)
(let ((entry (assq var (cgframe:slots frame))))
(if entry
(let ((n (cgframe:slot.offset entry)))
(if (eq? #f n)
(let ((n (cgframe:unused-slot frame entry)))
(cgframe:slot.offset-set! entry n))))
(error "Compiler bug: cgframe-touch!" frame var))))
(define (cgframe-rename! frame alist)
(for-each (lambda (entry)
(let ((probe (assq (cgframe:slot.name entry) alist)))
(if probe
(cgframe:slot.name-set! entry (cdr probe)))))
(cgframe:slots frame)))
(define (cgframe-release! frame var)
(let* ((slots (cgframe:slots frame))
(entry (assq var slots)))
(if entry
(begin (cgframe:slots-set! frame (remq entry slots))
(let ((n (cgframe:slot.offset entry)))
(if (and (not (eq? #f n))
(not (zero? n)))
(cgframe:stale-set!
frame
(cons (cons #t n)
(cgframe:stale frame)))))))))
(define (cgframe-release-except! frame vars)
(let loop ((slots (reverse (cgframe:slots frame)))
(newslots '())
(stale (cgframe:stale frame)))
(if (null? slots)
(begin (cgframe:slots-set! frame newslots)
(cgframe:stale-set! frame stale))
(let ((slot (car slots)))
(if (memq (cgframe:slot.name slot) vars)
(loop (cdr slots)
(cons slot newslots)
stale)
(let ((n (cgframe:slot.offset slot)))
(cond ((eq? n #f)
(loop (cdr slots)
newslots
stale))
((zero? n)
(loop (cdr slots)
(cons slot newslots)
stale))
(else
(loop (cdr slots)
newslots
(cons (cons #t n) stale))))))))))
(define (cgframe-lookup frame var)
(let ((entry (assq var (cgframe:slots frame))))
(if entry
(let ((n (cgframe:slot.offset entry)))
(if (eq? #f n)
(cgframe-touch! frame var))
(list var 'frame (cgframe:slot.offset entry) '(object)))
#f)))
(define (cgframe-spilled? frame var)
(let ((entry (assq var (cgframe:slots frame))))
(if entry
(let ((n (cgframe:slot.offset entry)))
(not (eq? #f n)))
#f)))
; For a conditional expression, the then and else parts must be
; evaluated using separate copies of the frame environment,
; and those copies must be resolved at the join point. The
; nature of the resolution depends upon whether the conditional
; expression is in a tail position.
;
; Critical invariant:
; Any store instructions that are generated within either arm of the
; conditional involve variables and temporaries that are local to the
; conditional.
;
; If the conditional expression is in a tail position, then a slot
; that is stale after the test can be allocated independently by the
; two arms of the conditional. If the conditional expression is in a
; non-tail position, then the slot can be allocated independently
; provided it is not a candidate destination for any previous emitted
; store instruction.
(define (cgframe-copy frame)
(cons (car frame)
(cons (cadr frame)
(cons (caddr frame)
(cdddr frame)))))
(define (cgframe-update-stale! frame)
(let* ((n (cgframe-size frame))
(v (make-vector (+ 1 n) #t))
(stale (cgframe:stale frame)))
(for-each (lambda (x)
(if (car x)
(let ((i (cdr x)))
(if (<= i n)
(vector-set! v i #f)))))
stale)
(for-each (lambda (slot)
(let ((offset (cgframe:slot.offset slot)))
(if offset
(vector-set! v offset #f)
(for-each (lambda (stale)
(if (car stale)
(let ((i (cdr stale)))
(if (< i n)
(vector-set! v i #f)))))
(cgframe:slot.stale slot)))))
(cgframe:slots frame))
(do ((i n (- i 1))
(stale (filter car stale)
(if (vector-ref v i)
(cons (cons #t i) stale)
stale)))
((<= i 0)
(cgframe:stale-set! frame stale)))))
(define (cgframe-join! frame1 frame2)
(let* ((slots1 (cgframe:slots frame1))
(slots2 (cgframe:slots frame2))
(slots (intersection slots1 slots2))
(deadslots (append (difference slots1 slots)
(difference slots2 slots)))
(deadoffsets (make-set
(filter (lambda (x) (not (eq? x #f)))
(map cgframe:slot.offset deadslots))))
(stale1 (cgframe:stale frame1))
(stale2 (cgframe:stale frame2))
(stale (intersection stale1 stale2))
(stale (append (map (lambda (n) (cons #t n))
deadoffsets)
stale)))
(cgframe:slots-set! frame1 slots)
(cgframe:stale-set! frame1 stale)))
; Environments.
;
; Each identifier has one of the following kinds of entry.
;
; (<name> register <number> (object))
; (<name> frame <slot> (object))
; (<name> lexical <rib> <offset> (object))
; (<name> procedure <rib> <label> (object))
; (<name> integrable <arity> <op> <imm> (object))
; (<name> global (object))
;
; Implementation.
;
; An environment is represented as a list of the form
;
; ((<entry> ...) ; lexical rib
; ...)
;
; where each <entry> has one of the forms
;
; (<name> lexical <offset> (object))
; (<name> procedure <rib> <label> (object))
; (<name> integrable <arity> <op> <imm> (object))
(define entry.name car)
(define entry.kind cadr)
(define entry.rib caddr)
(define entry.offset cadddr)
(define entry.label cadddr)
(define entry.regnum caddr)
(define entry.slotnum caddr)
(define entry.arity caddr)
(define entry.op cadddr)
(define (entry.imm entry) (car (cddddr entry)))
(define (cgenv-initial integrable)
(list (map (lambda (x)
(list (car x)
'integrable
(cadr x)
(caddr x)
(cadddr x)
'(object)))
integrable)))
(define (cgenv-lookup env id)
(define (loop ribs m)
(if (null? ribs)
(cons id '(global (object)))
(let ((x (assq id (car ribs))))
(if x
(case (cadr x)
((lexical)
(cons id
(cons (cadr x)
(cons m (cddr x)))))
((procedure)
(cons id
(cons (cadr x)
(cons m (cddr x)))))
((integrable)
(if (integrate-usual-procedures)
x
(loop '() m)))
(else ???))
(loop (cdr ribs) (+ m 1))))))
(loop env 0))
(define (cgenv-extend env vars procs)
(cons (do ((n 0 (+ n 1))
(vars vars (cdr vars))
(rib (map (lambda (id)
(list id 'procedure (make-label) '(object)))
procs)
(cons (list (car vars) 'lexical n '(object)) rib)))
((null? vars) rib))
env))
(define (cgenv-bindprocs env procs)
(cons (append (map (lambda (id)
(list id 'procedure (make-label) '(object)))
procs)
(car env))
(cdr env)))
(define (var-lookup var regs frame env)
(or (cgreg-lookup regs var)
(cgframe-lookup frame var)
(cgenv-lookup env var)))
; Compositions.
(define compile
(lambda (x)
(pass4 (pass3 (pass2 (pass1 x))) $usual-integrable-procedures$)))
(define compile-block
(lambda (x)
(pass4 (pass3 (pass2 (pass1-block x))) $usual-integrable-procedures$)))
; For testing.
(define foo
(lambda (x)
(pretty-print (compile x))))
; Find the smallest number of registers such that
; adding more registers does not affect the code
; generated for x (from 4 to 32 registers).
(define (minregs x)
(define (defregs R)
(set! *nregs* R)
(set! *lastreg* (- *nregs* 1))
(set! *fullregs* (quotient *nregs* 2)))
(defregs 32)
(let ((code (assemble (compile x))))
(define (binary-search m1 m2)
(if (= (+ m1 1) m2)
m2
(let ((midpt (quotient (+ m1 m2) 2)))
(defregs midpt)
(if (equal? code (assemble (compile x)))
(binary-search m1 midpt)
(binary-search midpt m2)))))
(defregs 4)
(let ((newcode (assemble (compile x))))
(if (equal? code newcode)
4
(binary-search 4 32)))))
; Minimums:
; browse 10
; triangle 5
; traverse 10
; destruct 6
; puzzle 8,8,10,7
; tak 6
; fft 28 (changing the named lets to macros didn't matter)
; Copyright 1991 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 7 June 1999.
;
; Fourth pass of the Twobit compiler:
; code generation for the MacScheme machine.
;
; This pass operates on input expressions described by the
; following grammar and the invariants that follow it.
;
; "X ..." means zero or more occurrences of X.
;
; L --> (lambda (I_1 ...)
; (begin D ...)
; (quote (R F G <decls> <doc>)
; E)
; | (lambda (I_1 ... . I_rest)
; (begin D ...)
; (quote (R F G <decls> <doc>))
; E)
; D --> (define I L)
; E --> (quote K) ; constants
; | (begin I) ; variable references
; | L ; lambda expressions
; | (E0 E1 ...) ; calls
; | (set! I E) ; assignments
; | (if E0 E1 E2) ; conditionals
; | (begin E0 E1 E2 ...) ; sequential expressions
; I --> <identifier>
;
; R --> ((I <references> <assignments> <calls>) ...)
; F --> (I ...)
; G --> (I ...)
;
; Invariants that hold for the input
; * There are no assignments except to global variables.
; * If I is declared by an internal definition, then the right hand
; side of the internal definition is a lambda expression and I
; is referenced only in the procedure position of a call.
; * Every procedure defined by an internal definition takes a
; fixed number of arguments.
; * Every call to a procedure defined by an internal definition
; passes the correct number of arguments.
; * For each lambda expression, the associated F is a list of all
; the identifiers that occur free in the body of that lambda
; expression, and possibly a few extra identifiers that were
; once free but have been removed by optimization.
; * For each lambda expression, the associated G is a subset of F
; that contains every identifier that occurs free within some
; inner lambda expression that escapes, and possibly a few that
; don't. (Assignment-elimination does not calculate G exactly.)
; * Variables named IGNORED are neither referenced nor assigned.
; * Any lambda expression that is declared to be in A-normal form
; really is in A-normal form.
;
;
; Stack frames are created by "save" instructions.
; A save instruction is generated
;
; * at the beginning of each lambda body
; * at the beginning of the code for each arm of a conditional,
; provided:
; the conditional is in a tail position
; the frames that were allocated by the save instructions
; that dominate the arms of the conditional have not been
; used (those save instructions will be eliminated during
; assembly)
;
; The operand of a save instruction, and of its matching pop instructions,
; increases automatically as frame slots are allocated.
;
; The code generated to return from a procedure is
;
; pop n
; return
;
; The code generated for a tail call is
;
; pop n
; invoke ...
;
; Invariant: When the code generator reserves an argument register
; to hold a value, that value is named, and is stored into the current
; stack frame. These store instructions are eliminated during assembly
; unless there is a matching load instruction. If all of the instructions
; that store into a stack frame are eliminated, then the stack frame
; itself is eliminated.
; Exception: An argument register may be used without naming or storing
; its value provided the register is not in use and no expressions are
; evaluated while it contains the unnamed and unstored value.
(define (pass4 exp integrable)
(init-labels)
(init-temps)
(let ((output (make-assembly-stream))
(frame (cgframe-initial))
(regs (cgreg-initial))
(t0 (newtemp)))
(assembly-stream-info! output (make-hashtable equal-hash assoc))
(cgreg-bind! regs 0 t0)
(gen-save! output frame t0)
(cg0 output
exp
'result
regs
frame
(cgenv-initial integrable)
#t)
(pass4-code output)))
(define (pass4-code output)
(hashtable-for-each (lambda (situation label)
(cg-trap output situation label))
(assembly-stream-info output))
(assembly-stream-code output))
; Given:
; an assembly stream into which instructions should be emitted
; an expression
; the target register
; ('result, a register number, or '#f; tail position implies 'result)
; a register environment [cgreg]
; a stack-frame environment [cgframe]
; a compile-time environment [cgenv]
; a flag indicating whether the expression is in tail position
; Returns:
; the target register ('result or a register number)
; Side effects:
; may change the register and stack-frame environments
; may increase the size of the stack frame, which changes previously
; emitted instructions
; writes instructions to the assembly stream
(define (cg0 output exp target regs frame env tail?)
(case (car exp)
((quote) (gen! output $const (constant.value exp))
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
((lambda) (cg-lambda output exp regs frame env)
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
((set!) (cg0 output (assignment.rhs exp) 'result regs frame env #f)
(cg-assignment-result output exp target regs frame env tail?))
((if) (cg-if output exp target regs frame env tail?))
((begin) (if (variable? exp)
(cg-variable output exp target regs frame env tail?)
(cg-sequential output exp target regs frame env tail?)))
(else (cg-call output exp target regs frame env tail?))))
; Lambda expressions that evaluate to closures.
; This is hard because the MacScheme machine's lambda instruction
; closes over the values that are in argument registers 0 through r
; (where r can be larger than *nregs*).
; The set of free variables is calculated and then sorted to minimize
; register shuffling.
;
; Returns: nothing.
(define (cg-lambda output exp regs frame env)
(let* ((args (lambda.args exp))
(vars (make-null-terminated args))
(free (difference (lambda.F exp) vars))
(free (cg-sort-vars free regs frame env))
(newenv (cgenv-extend env (cons #t free) '()))
(newoutput (make-assembly-stream)))
(assembly-stream-info! newoutput (make-hashtable equal-hash assoc))
(gen! newoutput $.proc)
(if (list? args)
(gen! newoutput $args= (length args))
(gen! newoutput $args>= (- (length vars) 1)))
(cg-known-lambda newoutput exp newenv)
(cg-eval-vars output free regs frame env)
; FIXME
'
(if (not (ignore-space-leaks))
; FIXME: Is this the right constant?
(begin (gen! output $const #f)
(gen! output $setreg 0)))
(gen! output
$lambda
(pass4-code newoutput)
(length free)
(lambda.doc exp))
; FIXME
'
(if (not (ignore-space-leaks))
; FIXME: This load forces a stack frame to be allocated.
(gen-load! output frame 0 (cgreg-lookup-reg regs 0)))))
; Given a list of free variables, filters out the ones that
; need to be copied into a closure, and sorts them into an order
; that reduces register shuffling. Returns a sorted version of
; the list in which the first element (element 0) should go
; into register 1, the second into register 2, and so on.
(define (cg-sort-vars free regs frame env)
(let* ((free (filter (lambda (var)
(case (entry.kind
(var-lookup var regs frame env))
((register frame)
#t)
((lexical)
(not (ignore-space-leaks)))
(else #f)))
free))
(n (length free))
(m (min n (- *nregs* 1)))
(vec (make-vector m #f)))
(define (loop1 free free-notregister)
(if (null? free)
(loop2 0 free-notregister)
(let* ((var (car free))
(entry (cgreg-lookup regs var)))
(if entry
(let ((r (entry.regnum entry)))
(if (<= r n)
(begin (vector-set! vec (- r 1) var)
(loop1 (cdr free)
free-notregister))
(loop1 (cdr free)
(cons var free-notregister))))
(loop1 (cdr free)
(cons var free-notregister))))))
(define (loop2 i free)
(cond ((null? free)
(vector->list vec))
((= i m)
(append (vector->list vec) free))
((vector-ref vec i)
(loop2 (+ i 1) free))
(else
(vector-set! vec i (car free))
(loop2 (+ i 1) (cdr free)))))
(loop1 free '())))
; Fetches the given list of free variables into the corresponding
; registers in preparation for a $lambda or $lexes instruction.
(define (cg-eval-vars output free regs frame env)
(let ((n (length free))
(R-1 (- *nregs* 1)))
(if (>= n R-1)
(begin (gen! output $const '())
(gen! output $setreg R-1)
(cgreg-release! regs R-1)))
(do ((r n (- r 1))
(vars (reverse free) (cdr vars)))
((zero? r))
(let* ((v (car vars))
(entry (var-lookup v regs frame env)))
(case (entry.kind entry)
((register)
(let ((r1 (entry.regnum entry)))
(if (not (eqv? r r1))
(if (< r R-1)
(begin (gen! output $movereg r1 r)
(cgreg-bind! regs r v))
(gen! output $reg r1 v)))))
((frame)
(if (< r R-1)
(begin (gen-load! output frame r v)
(cgreg-bind! regs r v))
(gen-stack! output frame v)))
((lexical)
(gen! output $lexical
(entry.rib entry)
(entry.offset entry)
v)
(if (< r R-1)
(begin (gen! output $setreg r)
(cgreg-bind! regs r v)
(gen-store! output frame r v))))
(else
(error "Bug in cg-close-lambda")))
(if (>= r R-1)
(begin (gen! output $op2 $cons R-1)
(gen! output $setreg R-1)))))))
; Lambda expressions that appear on the rhs of a definition are
; compiled here. They don't need an args= instruction at their head.
;
; Returns: nothing.
(define (cg-known-lambda output exp env)
(let* ((vars (make-null-terminated (lambda.args exp)))
(regs (cgreg-initial))
(frame (cgframe-initial))
(t0 (newtemp)))
(if (member A-normal-form-declaration (lambda.decls exp))
(cgframe-livevars-set! frame '()))
(cgreg-bind! regs 0 t0)
(gen-save! output frame t0)
(do ((r 1 (+ r 1))
(vars vars (cdr vars)))
((or (null? vars)
(= r *lastreg*))
(if (not (null? vars))
(begin (gen! output $movereg *lastreg* 1)
(cgreg-release! regs 1)
(do ((vars vars (cdr vars)))
((null? vars))
(gen! output $reg 1)
(gen! output $op1 $car:pair)
(gen-setstk! output frame (car vars))
(gen! output $reg 1)
(gen! output $op1 $cdr:pair)
(gen! output $setreg 1)))))
(cgreg-bind! regs r (car vars))
(gen-store! output frame r (car vars)))
(cg-body output
exp
'result
regs
frame
env
#t)))
; Compiles a let or lambda body.
; The arguments of the lambda expression L are already in
; registers or the stack frame, as specified by regs and frame.
;
; The problem here is that the free variables of an internal
; definition must be in a heap-allocated environment, so any
; such variables in registers must be copied to the heap.
;
; Returns: destination register.
(define (cg-body output L target regs frame env tail?)
(let* ((exp (lambda.body L))
(defs (lambda.defs L))
(free (apply-union
(map (lambda (def)
(let ((L (def.rhs def)))
(difference (lambda.F L)
(lambda.args L))))
defs))))
(cond ((or (null? defs) (constant? exp) (variable? exp))
(cg0 output exp target regs frame env tail?))
((lambda? exp)
(let* ((free (cg-sort-vars
(union free
(difference
(lambda.F exp)
(make-null-terminated (lambda.args exp))))
regs frame env))
(newenv1 (cgenv-extend env
(cons #t free)
(map def.lhs defs)))
(args (lambda.args exp))
(vars (make-null-terminated args))
(newoutput (make-assembly-stream)))
(assembly-stream-info! newoutput (make-hashtable equal-hash assoc))
(gen! newoutput $.proc)
(if (list? args)
(gen! newoutput $args= (length args))
(gen! newoutput $args>= (- (length vars) 1)))
(cg-known-lambda newoutput exp newenv1)
(cg-defs newoutput defs newenv1)
(cg-eval-vars output free regs frame env)
(gen! output
$lambda
(pass4-code newoutput)
(length free)
(lambda.doc exp))
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target))))
((every? (lambda (def)
(every? (lambda (v)
(case (entry.kind
(var-lookup v regs frame env))
((register frame) #f)
(else #t)))
(let ((Ldef (def.rhs def)))
(difference (lambda.F Ldef)
(lambda.args Ldef)))))
defs)
(let* ((newenv (cgenv-bindprocs env (map def.lhs defs)))
(L (make-label))
(r (cg0 output exp target regs frame newenv tail?)))
(if (not tail?)
(gen! output $skip L (cgreg-live regs r)))
(cg-defs output defs newenv)
(if (not tail?)
(gen! output $.label L))
r))
(else
(let ((free (cg-sort-vars free regs frame env)))
(cg-eval-vars output free regs frame env)
; FIXME: Have to restore it too!
'
(if (not (ignore-space-leaks))
; FIXME: Is this constant the right one?
(begin (gen! output $const #f)
(gen! output $setreg 0)))
(let ((t0 (cgreg-lookup-reg regs 0))
(t1 (newtemp))
(newenv (cgenv-extend env
(cons #t free)
(map def.lhs defs)))
(L (make-label)))
(gen! output $lexes (length free) free)
(gen! output $setreg 0)
(cgreg-bind! regs 0 t1)
(if tail?
(begin (cgframe-release! frame t0)
(gen-store! output frame 0 t1)
(cg0 output exp 'result regs frame newenv #t)
(cg-defs output defs newenv)
'result)
(begin (gen-store! output frame 0 t1)
(cg0 output exp 'result regs frame newenv #f)
(gen! output $skip L (cgreg-tos regs))
(cg-defs output defs newenv)
(gen! output $.label L)
(gen-load! output frame 0 t0)
(cgreg-bind! regs 0 t0)
(cgframe-release! frame t1)
(cg-move output frame regs 'result target)))))))))
(define (cg-defs output defs env)
(for-each (lambda (def)
(gen! output $.align 4)
(gen! output $.label
(entry.label
(cgenv-lookup env (def.lhs def))))
(gen! output $.proc)
(gen! output $.proc-doc (lambda.doc (def.rhs def)))
(cg-known-lambda output
(def.rhs def)
env))
defs))
; The right hand side has already been evaluated into the result register.
(define (cg-assignment-result output exp target regs frame env tail?)
(gen! output $setglbl (assignment.lhs exp))
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
(define (cg-if output exp target regs frame env tail?)
; The test can be a constant, because it is awkward
; to remove constant tests from an A-normal form.
(if (constant? (if.test exp))
(cg0 output
(if (constant.value (if.test exp))
(if.then exp)
(if.else exp))
target regs frame env tail?)
(begin
(cg0 output (if.test exp) 'result regs frame env #f)
(cg-if-result output exp target regs frame env tail?))))
; The test expression has already been evaluated into the result register.
(define (cg-if-result output exp target regs frame env tail?)
(let ((L1 (make-label))
(L2 (make-label)))
(gen! output $branchf L1 (cgreg-tos regs))
(let* ((regs2 (cgreg-copy regs))
(frame1 (if (and tail?
(negative? (cgframe-size frame)))
(cgframe-initial)
frame))
(frame2 (if (eq? frame frame1)
(cgframe-copy frame1)
(cgframe-initial)))
(t0 (cgreg-lookup-reg regs 0)))
(if (not (eq? frame frame1))
(let ((live (cgframe-livevars frame)))
(cgframe-livevars-set! frame1 live)
(cgframe-livevars-set! frame2 live)
(gen-save! output frame1 t0)
(cg-saveregs output regs frame1)))
(let ((r (cg0 output (if.then exp) target regs frame1 env tail?)))
(if (not tail?)
(gen! output $skip L2 (cgreg-live regs r)))
(gen! output $.label L1)
(if (not (eq? frame frame1))
(begin (gen-save! output frame2 t0)
(cg-saveregs output regs2 frame2))
(cgframe-update-stale! frame2))
(cg0 output (if.else exp) r regs2 frame2 env tail?)
(if (not tail?)
(begin (gen! output $.label L2)
(cgreg-join! regs regs2)
(cgframe-join! frame1 frame2)))
(if (and (not target)
(not (eq? r 'result))
(not (cgreg-lookup-reg regs r)))
(cg-move output frame regs r 'result)
r)))))
(define (cg-variable output exp target regs frame env tail?)
(define (return id)
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(if (and target
(not (eq? 'result target)))
(begin (gen! output $setreg target)
(cgreg-bind! regs target id)
(gen-store! output frame target id)
target)
'result)))
; Same as return, but doesn't emit a store instruction.
(define (return-nostore id)
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(if (and target
(not (eq? 'result target)))
(begin (gen! output $setreg target)
(cgreg-bind! regs target id)
target)
'result)))
(let* ((id (variable.name exp))
(entry (var-lookup id regs frame env)))
(case (entry.kind entry)
((global integrable)
(gen! output $global id)
(return (newtemp)))
((lexical)
(let ((m (entry.rib entry))
(n (entry.offset entry)))
(gen! output $lexical m n id)
(if (or (zero? m)
(negative? (cgframe-size frame)))
(return-nostore id)
(return id))))
((procedure) (error "Bug in cg-variable" exp))
((register)
(let ((r (entry.regnum entry)))
(if (or tail?
(and target (not (eqv? target r))))
(begin (gen! output $reg (entry.regnum entry) id)
(return-nostore id))
r)))
((frame)
(cond ((eq? target 'result)
(gen-stack! output frame id)
(return id))
(target
; Must be non-tail.
(gen-load! output frame target id)
(cgreg-bind! regs target id)
target)
(else
; Must be non-tail.
(let ((r (choose-register regs frame)))
(gen-load! output frame r id)
(cgreg-bind! regs r id)
r))))
(else (error "Bug in cg-variable" exp)))))
(define (cg-sequential output exp target regs frame env tail?)
(cg-sequential-loop output (begin.exprs exp) target regs frame env tail?))
(define (cg-sequential-loop output exprs target regs frame env tail?)
(cond ((null? exprs)
(gen! output $const unspecified)
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
((null? (cdr exprs))
(cg0 output (car exprs) target regs frame env tail?))
(else (cg0 output (car exprs) #f regs frame env #f)
(cg-sequential-loop output
(cdr exprs)
target regs frame env tail?))))
(define (cg-saveregs output regs frame)
(do ((i 1 (+ i 1))
(vars (cdr (cgreg-vars regs)) (cdr vars)))
((null? vars))
(let ((t (car vars)))
(if t
(gen-store! output frame i t)))))
(define (cg-move output frame regs src dst)
(define (bind dst)
(let ((temp (newtemp)))
(cgreg-bind! regs dst temp)
(gen-store! output frame dst temp)
dst))
(cond ((not dst)
src)
((eqv? src dst)
dst)
((eq? dst 'result)
(gen! output $reg src)
dst)
((eq? src 'result)
(gen! output $setreg dst)
(bind dst))
((and (not (zero? src))
(not (zero? dst)))
(gen! output $movereg src dst)
(bind dst))
(else
(gen! output $reg src)
(gen! output $setreg dst)
(bind dst))))
; On-the-fly register allocator.
; Tries to allocate:
; a hardware register that isn't being used
; a hardware register whose contents have already been spilled
; a software register that isn't being used, unless a stack
; frame has already been created, in which case it is better to use
; a hardware register that is in use and hasn't yet been spilled
;
; All else equal, it is better to allocate a higher-numbered register
; because the lower-numbered registers are targets when arguments
; are being evaluated.
;
; Invariant: Every register that is returned by this allocator
; is either not in use or has been spilled.
(define (choose-register regs frame)
(car (choose-registers regs frame 1)))
(define (choose-registers regs frame n)
; Find unused hardware registers.
(define (loop1 i n good)
(cond ((zero? n)
good)
((zero? i)
(if (negative? (cgframe-size frame))
(hardcase)
(loop2 (- *nhwregs* 1) n good)))
(else
(if (cgreg-lookup-reg regs i)
(loop1 (- i 1) n good)
(loop1 (- i 1)
(- n 1)
(cons i good))))))
; Find already spilled hardware registers.
(define (loop2 i n good)
(cond ((zero? n)
good)
((zero? i)
(hardcase))
(else
(let ((t (cgreg-lookup-reg regs i)))
(if (and t (cgframe-spilled? frame t))
(loop2 (- i 1)
(- n 1)
(cons i good))
(loop2 (- i 1) n good))))))
; This is ridiculous.
; Fortunately the correctness of the compiler is independent
; of the predicate used for this sort.
(define (hardcase)
(let* ((frame-exists? (not (negative? (cgframe-size frame))))
(stufftosort
(map (lambda (r)
(let* ((t (cgreg-lookup-reg regs r))
(spilled?
(and t
(cgframe-spilled? frame t))))
(list r t spilled?)))
(cdr (iota *nregs*))))
(registers
(twobit-sort
(lambda (x1 x2)
(let ((r1 (car x1))
(r2 (car x2))
(t1 (cadr x1))
(t2 (cadr x2)))
(cond ((< r1 *nhwregs*)
(cond ((not t1) #t)
((< r2 *nhwregs*)
(cond ((not t2) #f)
((caddr x1) #t)
((caddr x2) #f)
(else #t)))
(frame-exists? #t)
(t2 #t)
(else #f)))
((< r2 *nhwregs*)
(cond (frame-exists? #f)
(t1 #f)
(t2 #t)
(else #f)))
(t1
(if (and (caddr x1)
t2
(not (caddr x2)))
#t
#f))
(else #t))))
stufftosort)))
; FIXME: What was this for?
'
(for-each (lambda (register)
(let ((t (cadr register))
(spilled? (caddr register)))
(if (and t (not spilled?))
(cgframe-touch! frame t))))
registers)
(do ((sorted (map car registers) (cdr sorted))
(rs '() (cons (car sorted) rs))
(n n (- n 1)))
((zero? n)
(reverse rs)))))
(if (< n *nregs*)
(loop1 (- *nhwregs* 1) n '())
(error (string-append "Compiler bug: can't allocate "
(number->string n)
" registers on this target."))))
; Copyright 1991 William Clinger
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 21 May 1999.
; Procedure calls.
(define (cg-call output exp target regs frame env tail?)
(let ((proc (call.proc exp)))
(cond ((and (lambda? proc)
(list? (lambda.args proc)))
(cg-let output exp target regs frame env tail?))
((not (variable? proc))
(cg-unknown-call output exp target regs frame env tail?))
(else (let ((entry
(var-lookup (variable.name proc) regs frame env)))
(case (entry.kind entry)
((global lexical frame register)
(cg-unknown-call output
exp
target regs frame env tail?))
((integrable)
(cg-integrable-call output
exp
target regs frame env tail?))
((procedure)
(cg-known-call output
exp
target regs frame env tail?))
(else (error "Bug in cg-call" exp))))))))
(define (cg-unknown-call output exp target regs frame env tail?)
(let* ((proc (call.proc exp))
(args (call.args exp))
(n (length args))
(L (make-label)))
(cond ((>= (+ n 1) *lastreg*)
(cg-big-call output exp target regs frame env tail?))
(else
(let ((r0 (cgreg-lookup-reg regs 0)))
(if (variable? proc)
(let ((entry (cgreg-lookup regs (variable.name proc))))
(if (and entry
(<= (entry.regnum entry) n))
(begin (cg-arguments output
(iota1 (+ n 1))
(append args (list proc))
regs frame env)
(gen! output $reg (+ n 1)))
(begin (cg-arguments output
(iota1 n)
args
regs frame env)
(cg0 output proc 'result regs frame env #f)))
(if tail?
(gen-pop! output frame)
(begin (cgframe-used! frame)
(gen! output $setrtn L)))
(gen! output $invoke n))
(begin (cg-arguments output
(iota1 (+ n 1))
(append args (list proc))
regs frame env)
(gen! output $reg (+ n 1))
(if tail?
(gen-pop! output frame)
(begin (cgframe-used! frame)
(gen! output $setrtn L)))
(gen! output $invoke n)))
(if tail?
'result
(begin (gen! output $.align 4)
(gen! output $.label L)
(gen! output $.cont)
(cgreg-clear! regs)
(cgreg-bind! regs 0 r0)
(gen-load! output frame 0 r0)
(cg-move output frame regs 'result target))))))))
(define (cg-known-call output exp target regs frame env tail?)
(let* ((args (call.args exp))
(n (length args))
(L (make-label)))
(cond ((>= (+ n 1) *lastreg*)
(cg-big-call output exp target regs frame env tail?))
(else
(let ((r0 (cgreg-lookup-reg regs 0)))
(cg-arguments output (iota1 n) args regs frame env)
(if tail?
(gen-pop! output frame)
(begin (cgframe-used! frame)
(gen! output $setrtn L)))
(let* ((entry (cgenv-lookup env (variable.name (call.proc exp))))
(label (entry.label entry))
(m (entry.rib entry)))
(if (zero? m)
(gen! output $branch label n)
(gen! output $jump m label n)))
(if tail?
'result
(begin (gen! output $.align 4)
(gen! output $.label L)
(gen! output $.cont)
(cgreg-clear! regs)
(cgreg-bind! regs 0 r0)
(gen-load! output frame 0 r0)
(cg-move output frame regs 'result target))))))))
; Any call can be compiled as follows, even if there are no free registers.
;
; Let T0, T1, ..., Tn be newly allocated stack temporaries.
;
; <arg0>
; setstk T0
; <arg1> -|
; setstk T1 |
; ... |- evaluate args into stack frame
; <argn> |
; setstk Tn -|
; const ()
; setreg R-1
; stack Tn -|
; op2 cons,R-1 |
; setreg R-1 |
; ... |- cons up overflow args
; stack T_{R-1} |
; op2 cons,R-1 |
; setreg R-1 -|
; stack T_{R-2} -|
; setreg R-2 |
; ... |- pop remaining args into registers
; stack T1 |
; setreg 1 -|
; stack T0
; invoke n
(define (cg-big-call output exp target regs frame env tail?)
(let* ((proc (call.proc exp))
(args (call.args exp))
(n (length args))
(argslots (newtemps n))
(procslot (newtemp))
(r0 (cgreg-lookup-reg regs 0))
(R-1 (- *nregs* 1))
(entry (if (variable? proc)
(let ((entry
(var-lookup (variable.name proc)
regs frame env)))
(if (eq? (entry.kind entry) 'procedure)
entry
#f))
#f))
(L (make-label)))
(if (not entry)
(begin
(cg0 output proc 'result regs frame env #f)
(gen-setstk! output frame procslot)))
(for-each (lambda (arg argslot)
(cg0 output arg 'result regs frame env #f)
(gen-setstk! output frame argslot))
args
argslots)
(cgreg-clear! regs)
(gen! output $const '())
(gen! output $setreg R-1)
(do ((i n (- i 1))
(slots (reverse argslots) (cdr slots)))
((zero? i))
(if (< i R-1)
(gen-load! output frame i (car slots))
(begin (gen-stack! output frame (car slots))
(gen! output $op2 $cons R-1)
(gen! output $setreg R-1))))
(if (not entry)
(gen-stack! output frame procslot))
(if tail?
(gen-pop! output frame)
(begin (cgframe-used! frame)
(gen! output $setrtn L)))
(if entry
(let ((label (entry.label entry))
(m (entry.rib entry)))
(if (zero? m)
(gen! output $branch label n)
(gen! output $jump m label n)))
(gen! output $invoke n))
(if tail?
'result
(begin (gen! output $.align 4)
(gen! output $.label L)
(gen! output $.cont)
(cgreg-clear! regs) ; redundant, see above
(cgreg-bind! regs 0 r0)
(gen-load! output frame 0 r0)
(cg-move output frame regs 'result target)))))
(define (cg-integrable-call output exp target regs frame env tail?)
(let ((args (call.args exp))
(entry (var-lookup (variable.name (call.proc exp)) regs frame env)))
(if (= (entry.arity entry) (length args))
(begin (case (entry.arity entry)
((0) (gen! output $op1 (entry.op entry)))
((1) (cg0 output (car args) 'result regs frame env #f)
(gen! output $op1 (entry.op entry)))
((2) (cg-integrable-call2 output
entry
args
regs frame env))
((3) (cg-integrable-call3 output
entry
args
regs frame env))
(else (error "Bug detected by cg-integrable-call"
(make-readable exp))))
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
(if (negative? (entry.arity entry))
(cg-special output exp target regs frame env tail?)
(error "Wrong number of arguments to integrable procedure"
(make-readable exp))))))
(define (cg-integrable-call2 output entry args regs frame env)
(let ((op (entry.op entry)))
(if (and (entry.imm entry)
(constant? (cadr args))
((entry.imm entry) (constant.value (cadr args))))
(begin (cg0 output (car args) 'result regs frame env #f)
(gen! output $op2imm
op
(constant.value (cadr args))))
(let* ((reg2 (cg0 output (cadr args) #f regs frame env #f))
(r2 (choose-register regs frame))
(t2 (if (eq? reg2 'result)
(let ((t2 (newtemp)))
(gen! output $setreg r2)
(cgreg-bind! regs r2 t2)
(gen-store! output frame r2 t2)
t2)
(cgreg-lookup-reg regs reg2))))
(cg0 output (car args) 'result regs frame env #f)
(let* ((r2 (or (let ((entry (cgreg-lookup regs t2)))
(if entry
(entry.regnum entry)
#f))
(let ((r2 (choose-register regs frame)))
(cgreg-bind! regs r2 t2)
(gen-load! output frame r2 t2)
r2))))
(gen! output $op2 (entry.op entry) r2)
(if (eq? reg2 'result)
(begin (cgreg-release! regs r2)
(cgframe-release! frame t2)))))))
'result)
(define (cg-integrable-call3 output entry args regs frame env)
(let* ((reg2 (cg0 output (cadr args) #f regs frame env #f))
(r2 (choose-register regs frame))
(t2 (if (eq? reg2 'result)
(let ((t2 (newtemp)))
(gen! output $setreg r2)
(cgreg-bind! regs r2 t2)
(gen-store! output frame r2 t2)
t2)
(cgreg-lookup-reg regs reg2)))
(reg3 (cg0 output (caddr args) #f regs frame env #f))
(spillregs (choose-registers regs frame 2))
(t3 (if (eq? reg3 'result)
(let ((t3 (newtemp))
(r3 (if (eq? t2 (cgreg-lookup-reg
regs (car spillregs)))
(cadr spillregs)
(car spillregs))))
(gen! output $setreg r3)
(cgreg-bind! regs r3 t3)
(gen-store! output frame r3 t3)
t3)
(cgreg-lookup-reg regs reg3))))
(cg0 output (car args) 'result regs frame env #f)
(let* ((spillregs (choose-registers regs frame 2))
(r2 (or (let ((entry (cgreg-lookup regs t2)))
(if entry
(entry.regnum entry)
#f))
(let ((r2 (car spillregs)))
(cgreg-bind! regs r2 t2)
(gen-load! output frame r2 t2)
r2)))
(r3 (or (let ((entry (cgreg-lookup regs t3)))
(if entry
(entry.regnum entry)
#f))
(let ((r3 (if (eq? r2 (car spillregs))
(cadr spillregs)
(car spillregs))))
(cgreg-bind! regs r3 t3)
(gen-load! output frame r3 t3)
r3))))
(gen! output $op3 (entry.op entry) r2 r3)
(if (eq? reg2 'result)
(begin (cgreg-release! regs r2)
(cgframe-release! frame t2)))
(if (eq? reg3 'result)
(begin (cgreg-release! regs r3)
(cgframe-release! frame t3)))))
'result)
; Given a short list of expressions that can be evaluated in any order,
; evaluates the first into the result register and the others into any
; register, and returns an ordered list of the registers that contain
; the arguments that follow the first.
; The number of expressions must be less than the number of argument
; registers.
(define (cg-primop-args output args regs frame env)
; Given a list of expressions to evaluate, a list of variables
; and temporary names for arguments that have already been
; evaluated, in reverse order, and a mask of booleans that
; indicate which temporaries should be released before returning,
; returns the correct result.
(define (eval-loop args temps mask)
(if (null? args)
(eval-first-into-result temps mask)
(let ((reg (cg0 output (car args) #f regs frame env #f)))
(if (eq? reg 'result)
(let* ((r (choose-register regs frame))
(t (newtemp)))
(gen! output $setreg r)
(cgreg-bind! regs r t)
(gen-store! output frame r t)
(eval-loop (cdr args)
(cons t temps)
(cons #t mask)))
(eval-loop (cdr args)
(cons (cgreg-lookup-reg regs reg) temps)
(cons #f mask))))))
(define (eval-first-into-result temps mask)
(cg0 output (car args) 'result regs frame env #f)
(finish-loop (choose-registers regs frame (length temps))
temps
mask
'()))
; Given a sufficient number of disjoint registers, a list of
; variable and temporary names that may need to be loaded into
; registers, a mask of booleans that indicates which temporaries
; should be released, and a list of registers in forward order,
; returns the correct result.
(define (finish-loop disjoint temps mask registers)
(if (null? temps)
registers
(let* ((t (car temps))
(entry (cgreg-lookup regs t)))
(if entry
(let ((r (entry.regnum entry)))
(if (car mask)
(begin (cgreg-release! regs r)
(cgframe-release! frame t)))
(finish-loop disjoint
(cdr temps)
(cdr mask)
(cons r registers)))
(let ((r (car disjoint)))
(if (memv r registers)
(finish-loop (cdr disjoint) temps mask registers)
(begin (gen-load! output frame r t)
(cgreg-bind! regs r t)
(if (car mask)
(begin (cgreg-release! regs r)
(cgframe-release! frame t)))
(finish-loop disjoint
(cdr temps)
(cdr mask)
(cons r registers)))))))))
(if (< (length args) *nregs*)
(eval-loop (cdr args) '() '())
(error "Bug detected by cg-primop-args" args)))
; Parallel assignment.
; Given a list of target registers, a list of expressions, and a
; compile-time environment, generates code to evaluate the expressions
; into the registers.
;
; Argument evaluation proceeds as follows:
;
; 1. Evaluate all but one of the complicated arguments.
; 2. Evaluate remaining arguments.
; 3. Load spilled arguments from stack.
(define (cg-arguments output targets args regs frame env)
; Sorts the args and their targets into complicated and
; uncomplicated args and targets.
; Then it calls evalargs.
(define (sortargs targets args targets1 args1 targets2 args2)
(if (null? args)
(evalargs targets1 args1 targets2 args2)
(let ((target (car targets))
(arg (car args))
(targets (cdr targets))
(args (cdr args)))
(if (complicated? arg env)
(sortargs targets
args
(cons target targets1)
(cons arg args1)
targets2
args2)
(sortargs targets
args
targets1
args1
(cons target targets2)
(cons arg args2))))))
; Given the complicated args1 and their targets1,
; and the uncomplicated args2 and their targets2,
; evaluates all the arguments into their target registers.
(define (evalargs targets1 args1 targets2 args2)
(let* ((temps1 (newtemps (length targets1)))
(temps2 (newtemps (length targets2))))
(if (not (null? args1))
(for-each (lambda (arg temp)
(cg0 output arg 'result regs frame env #f)
(gen-setstk! output frame temp))
(cdr args1)
(cdr temps1)))
(if (not (null? args1))
(evalargs0 (cons (car targets1) targets2)
(cons (car args1) args2)
(cons (car temps1) temps2))
(evalargs0 targets2 args2 temps2))
(for-each (lambda (r t)
(let ((temp (cgreg-lookup-reg regs r)))
(if (not (eq? temp t))
(let ((entry (var-lookup t regs frame env)))
(case (entry.kind entry)
((register)
(gen! output $movereg (entry.regnum entry) r))
((frame)
(gen-load! output frame r t)))
(cgreg-bind! regs r t)))
(cgframe-release! frame t)))
(append targets1 targets2)
(append temps1 temps2))))
(define (evalargs0 targets args temps)
(if (not (null? targets))
(let ((para (let* ((regvars (map (lambda (reg)
(cgreg-lookup-reg regs reg))
targets)))
(parallel-assignment targets
(map cons regvars targets)
args))))
(if para
(let ((targets para)
(args (cg-permute args targets para))
(temps (cg-permute temps targets para)))
(for-each (lambda (arg r t)
(cg0 output arg r regs frame env #f)
(cgreg-bind! regs r t)
(gen-store! output frame r t))
args
para
temps))
(let ((r (choose-register regs frame))
(t (car temps)))
(cg0 output (car args) r regs frame env #f)
(cgreg-bind! regs r t)
(gen-store! output frame r t)
(evalargs0 (cdr targets)
(cdr args)
(cdr temps)))))))
(if (parallel-assignment-optimization)
(sortargs (reverse targets) (reverse args) '() '() '() '())
(cg-evalargs output targets args regs frame env)))
; Left-to-right evaluation of arguments directly into targets.
(define (cg-evalargs output targets args regs frame env)
(let ((temps (newtemps (length targets))))
(for-each (lambda (arg r t)
(cg0 output arg r regs frame env #f)
(cgreg-bind! regs r t)
(gen-store! output frame r t))
args
targets
temps)
(for-each (lambda (r t)
(let ((temp (cgreg-lookup-reg regs r)))
(if (not (eq? temp t))
(begin (gen-load! output frame r t)
(cgreg-bind! regs r t)))
(cgframe-release! frame t)))
targets
temps)))
; For heuristic use only.
; An expression is complicated unless it can probably be evaluated
; without saving and restoring any registers, even if it occurs in
; a non-tail position.
(define (complicated? exp env)
(case (car exp)
((quote) #f)
((lambda) #t)
((set!) (complicated? (assignment.rhs exp) env))
((if) (or (complicated? (if.test exp) env)
(complicated? (if.then exp) env)
(complicated? (if.else exp) env)))
((begin) (if (variable? exp)
#f
(some? (lambda (exp)
(complicated? exp env))
(begin.exprs exp))))
(else (let ((proc (call.proc exp)))
(if (and (variable? proc)
(let ((entry
(cgenv-lookup env (variable.name proc))))
(eq? (entry.kind entry) 'integrable)))
(some? (lambda (exp)
(complicated? exp env))
(call.args exp))
#t)))))
; Returns a permutation of the src list, permuted the same way the
; key list was permuted to obtain newkey.
(define (cg-permute src key newkey)
(let ((alist (map cons key (iota (length key)))))
(do ((newkey newkey (cdr newkey))
(dest '()
(cons (list-ref src (cdr (assq (car newkey) alist)))
dest)))
((null? newkey) (reverse dest)))))
; Given a list of register numbers,
; an association list with entries of the form (name . regnum) giving
; the variable names by which those registers are known in code,
; and a list of expressions giving new values for those registers,
; returns an ordering of the register assignments that implements a
; parallel assignment if one can be found, otherwise returns #f.
(define parallel-assignment
(lambda (regnums alist exps)
(if (null? regnums)
#t
(let ((x (toposort (dependency-graph regnums alist exps))))
(if x (reverse x) #f)))))
(define dependency-graph
(lambda (regnums alist exps)
(let ((names (map car alist)))
(do ((regnums regnums (cdr regnums))
(exps exps (cdr exps))
(l '() (cons (cons (car regnums)
(map (lambda (var) (cdr (assq var alist)))
(intersection (freevariables (car exps))
names)))
l)))
((null? regnums) l)))))
; Given a nonempty graph represented as a list of the form
; ((node1 . <list of nodes that node1 is less than or equal to>)
; (node2 . <list of nodes that node2 is less than or equal to>)
; ...)
; returns a topological sort of the nodes if one can be found,
; otherwise returns #f.
(define toposort
(lambda (graph)
(cond ((null? (cdr graph)) (list (caar graph)))
(else (toposort2 graph '())))))
(define toposort2
(lambda (totry tried)
(cond ((null? totry) #f)
((or (null? (cdr (car totry)))
(and (null? (cddr (car totry)))
(eq? (cadr (car totry))
(car (car totry)))))
(if (and (null? (cdr totry)) (null? tried))
(list (caar totry))
(let* ((node (caar totry))
(x (toposort2 (map (lambda (y)
(cons (car y) (remove node (cdr y))))
(append (cdr totry) tried))
'())))
(if x
(cons node x)
#f))))
(else (toposort2 (cdr totry) (cons (car totry) tried))))))
(define iota (lambda (n) (iota2 n '())))
(define iota1 (lambda (n) (cdr (iota2 (+ n 1) '()))))
(define iota2
(lambda (n l)
(if (zero? n)
l
(let ((n (- n 1)))
(iota2 n (cons n l))))))
(define (freevariables exp)
(freevars2 exp '()))
(define (freevars2 exp env)
(cond ((symbol? exp)
(if (memq exp env) '() (list exp)))
((not (pair? exp)) '())
(else (let ((keyword (car exp)))
(cond ((eq? keyword 'quote) '())
((eq? keyword 'lambda)
(let ((env (append (make-null-terminated (cadr exp))
env)))
(apply-union
(map (lambda (x) (freevars2 x env))
(cddr exp)))))
((memq keyword '(if set! begin))
(apply-union
(map (lambda (x) (freevars2 x env))
(cdr exp))))
(else (apply-union
(map (lambda (x) (freevars2 x env))
exp))))))))
; Copyright 1991 William Clinger (cg-let and cg-let-body)
; Copyright 1999 William Clinger (everything else)
;
; 10 June 1999.
; Generates code for a let expression.
(define (cg-let output exp target regs frame env tail?)
(let* ((proc (call.proc exp))
(vars (lambda.args proc))
(n (length vars))
(free (lambda.F proc))
(live (cgframe-livevars frame)))
(if (and (null? (lambda.defs proc))
(= n 1))
(cg-let1 output exp target regs frame env tail?)
(let* ((args (call.args exp))
(temps (newtemps n))
(alist (map cons temps vars)))
(for-each (lambda (arg t)
(let ((r (choose-register regs frame)))
(cg0 output arg r regs frame env #f)
(cgreg-bind! regs r t)
(gen-store! output frame r t)))
args
temps)
(cgreg-rename! regs alist)
(cgframe-rename! frame alist)
(cg-let-release! free live regs frame tail?)
(cg-let-body output proc target regs frame env tail?)))))
; Given the free variables of a let body, and the variables that are
; live after the let expression, and the usual regs, frame, and tail?
; arguments, releases any registers and frame slots that don't need
; to be preserved across the body of the let.
(define (cg-let-release! free live regs frame tail?)
; The tail case is easy because there are no live temporaries,
; and there are no free variables in the context.
; The non-tail case assumes A-normal form.
(cond (tail?
(let ((keepers (cons (cgreg-lookup-reg regs 0) free)))
(cgreg-release-except! regs keepers)
(cgframe-release-except! frame keepers)))
(live
(let ((keepers (cons (cgreg-lookup-reg regs 0)
(union live free))))
(cgreg-release-except! regs keepers)
(cgframe-release-except! frame keepers)))))
; Generates code for the body of a let.
(define (cg-let-body output L target regs frame env tail?)
(let ((vars (lambda.args L))
(free (lambda.F L))
(live (cgframe-livevars frame)))
(let ((r (cg-body output L target regs frame env tail?)))
(for-each (lambda (v)
(let ((entry (cgreg-lookup regs v)))
(if entry
(cgreg-release! regs (entry.regnum entry)))
(cgframe-release! frame v)))
vars)
(if (and (not target)
(not (eq? r 'result))
(not (cgreg-lookup-reg regs r)))
(cg-move output frame regs r 'result)
r))))
; Generates code for a let expression that binds exactly one variable
; and has no internal definitions. These let expressions are very
; common in A-normal form, and there are many special cases with
; respect to register allocation and order of evaluation.
(define (cg-let1 output exp target regs frame env tail?)
(let* ((proc (call.proc exp))
(v (car (lambda.args proc)))
(arg (car (call.args exp)))
(free (lambda.F proc))
(live (cgframe-livevars frame))
(body (lambda.body proc)))
(define (evaluate-into-register r)
(cg0 output arg r regs frame env #f)
(cgreg-bind! regs r v)
(gen-store! output frame r v)
r)
(define (release-registers!)
(cgframe-livevars-set! frame live)
(cg-let-release! free live regs frame tail?))
(define (finish)
(release-registers!)
(cg-let-body output proc target regs frame env tail?))
(if live
(cgframe-livevars-set! frame (union live free)))
(cond ((assq v *regnames*)
(evaluate-into-register (cdr (assq v *regnames*)))
(finish))
((not (memq v free))
(cg0 output arg #f regs frame env #f)
(finish))
(live
(cg0 output arg 'result regs frame env #f)
(release-registers!)
(cg-let1-result output exp target regs frame env tail?))
(else
(evaluate-into-register (choose-register regs frame))
(finish)))))
; Given a let expression that binds one variable whose value has already
; been evaluated into the result register, generates code for the rest
; of the let expression.
; The main difficulty is an unfortunate interaction between A-normal
; form and the MacScheme machine architecture: We don't want to move
; a value from the result register into a general register if it has
; only one use and can remain in the result register until that use.
(define (cg-let1-result output exp target regs frame env tail?)
(let* ((proc (call.proc exp))
(v (car (lambda.args proc)))
(free (lambda.F proc))
(live (cgframe-livevars frame))
(body (lambda.body proc))
(pattern (cg-let-used-once v body)))
(define (move-to-register r)
(gen! output $setreg r)
(cgreg-bind! regs r v)
(gen-store! output frame r v)
r)
(define (release-registers!)
(cgframe-livevars-set! frame live)
(cg-let-release! free live regs frame tail?))
; FIXME: The live variables must be correct in the frame.
(case pattern
((if)
(cg-if-result output body target regs frame env tail?))
((let-if)
(if live
(cgframe-livevars-set! frame (union live free)))
(cg-if-result output
(car (call.args body))
'result regs frame env #f)
(release-registers!)
(cg-let1-result output body target regs frame env tail?))
((set!)
(cg-assignment-result output
body target regs frame env tail?))
((let-set!)
(cg-assignment-result output
(car (call.args body))
'result regs frame env #f)
(cg-let1-result output body target regs frame env tail?))
((primop)
(cg-primop-result output body target regs frame env tail?))
((let-primop)
(cg-primop-result output
(car (call.args body))
'result regs frame env #f)
(cg-let1-result output body target regs frame env tail?))
; FIXME
((_called)
(cg-call-result output body target regs frame env tail?))
; FIXME
((_let-called)
(cg-call-result output
(car (call.args body))
'result regs frame env #f)
(cg-let1-result output body target regs frame env tail?))
(else
; FIXME: The first case was handled by cg-let1.
(cond ((assq v *regnames*)
(move-to-register (cdr (assq v *regnames*))))
((memq v free)
(move-to-register (choose-register regs frame))))
(cg-let-body output proc target regs frame env tail?)))))
; Given a call to a primop whose first argument has already been
; evaluated into the result register and whose remaining arguments
; consist of constants and variable references, generates code for
; the call.
(define (cg-primop-result output exp target regs frame env tail?)
(let ((args (call.args exp))
(entry (var-lookup (variable.name (call.proc exp)) regs frame env)))
(if (= (entry.arity entry) (length args))
(begin (case (entry.arity entry)
((0) (gen! output $op1 (entry.op entry)))
((1) (gen! output $op1 (entry.op entry)))
((2) (cg-primop2-result! output entry args regs frame env))
((3) (let ((rs (cg-result-args output args regs frame env)))
(gen! output
$op3 (entry.op entry) (car rs) (cadr rs))))
(else (error "Bug detected by cg-primop-result"
(make-readable exp))))
(if tail?
(begin (gen-pop! output frame)
(gen! output $return)
'result)
(cg-move output frame regs 'result target)))
(if (negative? (entry.arity entry))
(cg-special-result output exp target regs frame env tail?)
(error "Wrong number of arguments to integrable procedure"
(make-readable exp))))))
(define (cg-primop2-result! output entry args regs frame env)
(let ((op (entry.op entry))
(arg2 (cadr args)))
(if (and (constant? arg2)
(entry.imm entry)
((entry.imm entry) (constant.value arg2)))
(gen! output $op2imm op (constant.value arg2))
(let ((rs (cg-result-args output args regs frame env)))
(gen! output $op2 op (car rs))))))
; Given a short list of constants and variable references to be evaluated
; into arbitrary general registers, evaluates them into registers without
; disturbing the result register and returns a list of the registers into
; which they are evaluated. Before returning, any registers that were
; allocated by this routine are released.
(define (cg-result-args output args regs frame env)
; Given a list of unevaluated arguments,
; a longer list of disjoint general registers,
; the register that holds the first evaluated argument,
; a list of registers in reverse order that hold other arguments,
; and a list of registers to be released afterwards,
; generates code to evaluate the arguments,
; deallocates any registers that were evaluated to hold the arguments,
; and returns the list of registers that contain the arguments.
(define (loop args registers rr rs temps)
(if (null? args)
(begin (if (not (eq? rr 'result))
(gen! output $reg rr))
(for-each (lambda (r) (cgreg-release! regs r))
temps)
(reverse rs))
(let ((arg (car args)))
(cond ((constant? arg)
(let ((r (car registers)))
(gen! output $const/setreg (constant.value arg) r)
(cgreg-bind! regs r #t)
(loop (cdr args)
(cdr registers)
rr
(cons r rs)
(cons r temps))))
((variable? arg)
(let* ((id (variable.name arg))
(entry (var-lookup id regs frame env)))
(case (entry.kind entry)
((global integrable)
(if (eq? rr 'result)
(save-result! args registers rr rs temps)
(let ((r (car registers)))
(gen! output $global id)
(gen! output $setreg r)
(cgreg-bind! regs r id)
(loop (cdr args)
(cdr registers)
rr
(cons r rs)
(cons r temps)))))
((lexical)
(if (eq? rr 'result)
(save-result! args registers rr rs temps)
(let ((m (entry.rib entry))
(n (entry.offset entry))
(r (car registers)))
(gen! output $lexical m n id)
(gen! output $setreg r)
(cgreg-bind! regs r id)
(loop (cdr args)
(cdr registers)
rr
(cons r rs)
(cons r temps)))))
((procedure) (error "Bug in cg-variable" arg))
((register)
(let ((r (entry.regnum entry)))
(loop (cdr args)
registers
rr
(cons r rs)
temps)))
((frame)
(let ((r (car registers)))
(gen-load! output frame r id)
(cgreg-bind! regs r id)
(loop (cdr args)
(cdr registers)
rr
(cons r rs)
(cons r temps))))
(else (error "Bug in cg-result-args" arg)))))
(else
(error "Bug in cg-result-args"))))))
(define (save-result! args registers rr rs temps)
(let ((r (car registers)))
(gen! output $setreg r)
(loop args
(cdr registers)
r
rs
temps)))
(loop (cdr args)
(choose-registers regs frame (length args))
'result '() '()))
; Given a local variable T1 and an expression in A-normal form,
; cg-let-used-once returns a symbol if the local variable is used
; exactly once in the expression and the expression matches one of
; the patterns below. Otherwise returns #f. The symbol that is
; returned is the name of the pattern that is matched.
;
; pattern symbol returned
;
; (if T1 ... ...) if
;
; (<primop> T1 ...) primop
;
; (T1 ...) called
;
; (set! ... T1) set!
;
; (let ((T2 (if T1 ... ...))) let-if
; E3)
;
; (let ((T2 (<primop> T1 ...))) let-primop
; E3)
;
; (let ((T2 (T1 ...))) let-called
; E3)
;
; (let ((T2 (set! ... T1))) let-set!
; E3)
;
; This implementation sometimes returns #f incorrectly, but it always
; returns an answer in constant time (assuming A-normal form).
(define (cg-let-used-once T1 exp)
(define budget 20)
(define (cg-let-used-once T1 exp)
(define (used? T1 exp)
(set! budget (- budget 1))
(cond ((negative? budget) #t)
((constant? exp) #f)
((variable? exp)
(eq? T1 (variable.name exp)))
((lambda? exp)
(memq T1 (lambda.F exp)))
((assignment? exp)
(used? T1 (assignment.rhs exp)))
((call? exp)
(or (used? T1 (call.proc exp))
(used-in-args? T1 (call.args exp))))
((conditional? exp)
(or (used? T1 (if.test exp))
(used? T1 (if.then exp))
(used? T1 (if.else exp))))
(else #t)))
(define (used-in-args? T1 args)
(if (null? args)
#f
(or (used? T1 (car args))
(used-in-args? T1 (cdr args)))))
(set! budget (- budget 1))
(cond ((negative? budget) #f)
((call? exp)
(let ((proc (call.proc exp))
(args (call.args exp)))
(cond ((variable? proc)
(let ((f (variable.name proc)))
(cond ((eq? f T1)
(and (not (used-in-args? T1 args))
'called))
((and (integrable? f)
(not (null? args))
(variable? (car args))
(eq? T1 (variable.name (car args))))
(and (not (used-in-args? T1 (cdr args)))
'primop))
(else #f))))
((lambda? proc)
(and (not (memq T1 (lambda.F proc)))
(not (null? args))
(null? (cdr args))
(case (cg-let-used-once T1 (car args))
((if) 'let-if)
((primop) 'let-primop)
((called) 'let-called)
((set!) 'let-set!)
(else #f))))
(else #f))))
((conditional? exp)
(let ((E0 (if.test exp)))
(and (variable? E0)
(eq? T1 (variable.name E0))
(not (used? T1 (if.then exp)))
(not (used? T1 (if.else exp)))
'if)))
((assignment? exp)
(let ((rhs (assignment.rhs exp)))
(and (variable? rhs)
(eq? T1 (variable.name rhs))
'set!)))
(else #f)))
(cg-let-used-once T1 exp))
; Given the name of a let-body pattern, an expression that matches that
; pattern, and an expression to be substituted for the let variable,
; returns the transformed expression.
; FIXME: No longer used.
(define (cg-let-transform pattern exp E1)
(case pattern
((if)
(make-conditional E1 (if.then exp) (if.else exp)))
((primop)
(make-call (call.proc exp)
(cons E1 (cdr (call.args exp)))))
((called)
(make-call E1 (call.args exp)))
((set!)
(make-assignment (assignment.lhs exp) E1))
((let-if let-primop let-called let-set!)
(make-call (call.proc exp)
(list (cg-let-transform (case pattern
((let-if) 'if)
((let-primop) 'primop)
((let-called) 'called)
((let-set!) 'set!))
(car (call.args exp))
E1))))
(else
(error "Unrecognized pattern in cg-let-transform" pattern)))); Copyright 1999 William Clinger
;
; Code for special primitives, used to generate runtime safety checks,
; efficient code for call-with-values, and other weird things.
;
; 4 June 1999.
(define (cg-special output exp target regs frame env tail?)
(let ((name (variable.name (call.proc exp))))
(cond ((eq? name name:CHECK!)
(if (runtime-safety-checking)
(cg-check output exp target regs frame env tail?)))
(else
(error "Compiler bug: cg-special" (make-readable exp))))))
(define (cg-special-result output exp target regs frame env tail?)
(let ((name (variable.name (call.proc exp))))
(cond ((eq? name name:CHECK!)
(if (runtime-safety-checking)
(cg-check-result output exp target regs frame env tail?)))
(else
(error "Compiler bug: cg-special" (make-readable exp))))))
(define (cg-check output exp target regs frame env tail?)
(cg0 output (car (call.args exp)) 'result regs frame env #f)
(cg-check-result output exp target regs frame env tail?))
(define (cg-check-result output exp target regs frame env tail?)
(let* ((args (call.args exp))
(nargs (length args))
(valexps (cddr args)))
(if (and (<= 2 nargs 5)
(constant? (cadr args))
(every? (lambda (exp)
(or (constant? exp)
(variable? exp)))
valexps))
(let* ((exn (constant.value (cadr args)))
(vars (filter variable? valexps))
(rs (cg-result-args output
(cons (car args) vars)
regs frame env)))
; Construct the trap situation:
; the exception number followed by an ordered list of
; register numbers and constant expressions.
(let loop ((registers rs)
(exps valexps)
(operands '()))
(cond ((null? exps)
(let* ((situation (cons exn (reverse operands)))
(ht (assembly-stream-info output))
(L1 (or (hashtable-get ht situation)
(let ((L1 (make-label)))
(hashtable-put! ht situation L1)
L1))))
(define (translate r)
(if (number? r) r 0))
(case (length operands)
((0) (gen! output $check 0 0 0 L1))
((1) (gen! output $check
(translate (car operands))
0 0 L1))
((2) (gen! output $check
(translate (car operands))
(translate (cadr operands))
0 L1))
((3) (gen! output $check
(translate (car operands))
(translate (cadr operands))
(translate (caddr operands))
L1)))))
((constant? (car exps))
(loop registers
(cdr exps)
(cons (car exps) operands)))
(else
(loop (cdr registers)
(cdr exps)
(cons (car registers) operands))))))
(error "Compiler bug: runtime check" (make-readable exp)))))
; Given an assembly stream and the description of a trap as recorded
; by cg-check above, generates a non-continuable trap at that label for
; that trap, passing the operands to the exception handler.
(define (cg-trap output situation L1)
(let* ((exn (car situation))
(operands (cdr situation)))
(gen! output $.label L1)
(let ((liveregs (filter number? operands)))
(define (loop operands registers r)
(cond ((null? operands)
(case (length registers)
((0) (gen! output $trap 0 0 0 exn))
((1) (gen! output $trap (car registers) 0 0 exn))
((2) (gen! output $trap
(car registers)
(cadr registers)
0
exn))
((3) (gen! output $trap
(car registers)
(cadr registers)
(caddr registers)
exn))
(else "Compiler bug: trap")))
((number? (car operands))
(loop (cdr operands)
(cons (car operands) registers)
r))
((memv r liveregs)
(loop operands registers (+ r 1)))
(else
(gen! output $const (constant.value (car operands)))
(gen! output $setreg r)
(loop (cdr operands)
(cons r registers)
(+ r 1)))))
(loop (reverse operands) '() 1))))
; Given a short list of expressions that can be evaluated in any order,
; evaluates the first into the result register and the others into any
; register, and returns an ordered list of the registers that contain
; the arguments that follow the first.
; The number of expressions must be less than the number of argument
; registers.
; FIXME: No longer used.
(define (cg-check-args output args regs frame env)
; Given a list of expressions to evaluate, a list of variables
; and temporary names for arguments that have already been
; evaluated, in reverse order, and a mask of booleans that
; indicate which temporaries should be released before returning,
; returns the correct result.
(define (eval-loop args temps mask)
(if (null? args)
(eval-first-into-result temps mask)
(let ((reg (cg0 output (car args) #f regs frame env #f)))
(if (eq? reg 'result)
(let* ((r (choose-register regs frame))
(t (newtemp)))
(gen! output $setreg r)
(cgreg-bind! regs r t)
(gen-store! output frame r t)
(eval-loop (cdr args)
(cons t temps)
(cons #t mask)))
(eval-loop (cdr args)
(cons (cgreg-lookup-reg regs reg) temps)
(cons #f mask))))))
(define (eval-first-into-result temps mask)
(cg0 output (car args) 'result regs frame env #f)
(finish-loop (choose-registers regs frame (length temps))
temps
mask
'()))
; Given a sufficient number of disjoint registers, a list of
; variable and temporary names that may need to be loaded into
; registers, a mask of booleans that indicates which temporaries
; should be released, and a list of registers in forward order,
; returns the correct result.
(define (finish-loop disjoint temps mask registers)
(if (null? temps)
registers
(let* ((t (car temps))
(entry (cgreg-lookup regs t)))
(if entry
(let ((r (entry.regnum entry)))
(if (car mask)
(begin (cgreg-release! regs r)
(cgframe-release! frame t)))
(finish-loop disjoint
(cdr temps)
(cdr mask)
(cons r registers)))
(let ((r (car disjoint)))
(if (memv r registers)
(finish-loop (cdr disjoint) temps mask registers)
(begin (gen-load! output frame r t)
(cgreg-bind! regs r t)
(if (car mask)
(begin (cgreg-release! regs r)
(cgframe-release! frame t)))
(finish-loop disjoint
(cdr temps)
(cdr mask)
(cons r registers)))))))))
(if (< (length args) *nregs*)
(eval-loop (cdr args) '() '())
(error "Bug detected by cg-primop-args" args)))
; Copyright 1998 William Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 5 June 1999.
;
; Local optimizations for MacScheme machine assembly code.
;
; Branch tensioning.
; Suppress nop instructions.
; Suppress save, restore, and pop instructions whose operand is -1.
; Suppress redundant stores.
; Suppress definitions (primarily loads) of dead registers.
;
; Note: Twobit never generates a locally redundant load or store,
; so this code must be tested with a different code generator.
;
; To perform these optimizations, the basic block must be traversed
; both forwards and backwards.
; The forward traversal keeps track of registers that were defined
; by a load.
; The backward traversal keeps track of live registers.
(define filter-basic-blocks
(let* ((suppression-message
"Local optimization detected a useless instruction.")
; Each instruction is mapping to an encoding of the actions
; to be performed when it is encountered during the forward
; or backward traversal.
(forward:normal 0)
(forward:nop 1)
(forward:ends-block 2)
(forward:interesting 3)
(forward:kills-all-registers 4)
(forward:nop-if-arg1-is-negative 5)
(backward:normal 0)
(backward:ends-block 1)
(backward:begins-block 2)
(backward:uses-arg1 4)
(backward:uses-arg2 8)
(backward:uses-arg3 16)
(backward:kills-arg1 32)
(backward:kills-arg2 64)
(backward:uses-many 128)
; largest mnemonic + 1
(dispatch-table-size *number-of-mnemonics*)
; Dispatch table for the forwards traversal.
(forward-table (make-bytevector dispatch-table-size))
; Dispatch table for the backwards traversal.
(backward-table (make-bytevector dispatch-table-size)))
(do ((i 0 (+ i 1)))
((= i dispatch-table-size))
(bytevector-set! forward-table i forward:normal)
(bytevector-set! backward-table i backward:normal))
(bytevector-set! forward-table $nop forward:nop)
(bytevector-set! forward-table $invoke forward:ends-block)
(bytevector-set! forward-table $return forward:ends-block)
(bytevector-set! forward-table $skip forward:ends-block)
(bytevector-set! forward-table $branch forward:ends-block)
(bytevector-set! forward-table $branchf forward:ends-block)
(bytevector-set! forward-table $jump forward:ends-block)
(bytevector-set! forward-table $.align forward:ends-block)
(bytevector-set! forward-table $.proc forward:ends-block)
(bytevector-set! forward-table $.cont forward:ends-block)
(bytevector-set! forward-table $.label forward:ends-block)
(bytevector-set! forward-table $store forward:interesting)
(bytevector-set! forward-table $load forward:interesting)
(bytevector-set! forward-table $setstk forward:interesting)
(bytevector-set! forward-table $setreg forward:interesting)
(bytevector-set! forward-table $movereg forward:interesting)
(bytevector-set! forward-table $const/setreg
forward:interesting)
(bytevector-set! forward-table $args>= forward:kills-all-registers)
(bytevector-set! forward-table $popstk forward:kills-all-registers)
; These instructions also kill all registers.
(bytevector-set! forward-table $save forward:nop-if-arg1-is-negative)
(bytevector-set! forward-table $restore forward:nop-if-arg1-is-negative)
(bytevector-set! forward-table $pop forward:nop-if-arg1-is-negative)
(bytevector-set! backward-table $invoke backward:ends-block)
(bytevector-set! backward-table $return backward:ends-block)
(bytevector-set! backward-table $skip backward:ends-block)
(bytevector-set! backward-table $branch backward:ends-block)
(bytevector-set! backward-table $branchf backward:ends-block)
(bytevector-set! backward-table $jump backward:begins-block) ; [sic]
(bytevector-set! backward-table $.align backward:begins-block)
(bytevector-set! backward-table $.proc backward:begins-block)
(bytevector-set! backward-table $.cont backward:begins-block)
(bytevector-set! backward-table $.label backward:begins-block)
(bytevector-set! backward-table $op2 backward:uses-arg2)
(bytevector-set! backward-table $op3 (logior backward:uses-arg2
backward:uses-arg3))
(bytevector-set! backward-table $check (logior
backward:uses-arg1
(logior backward:uses-arg2
backward:uses-arg3)))
(bytevector-set! backward-table $trap (logior
backward:uses-arg1
(logior backward:uses-arg2
backward:uses-arg3)))
(bytevector-set! backward-table $store backward:uses-arg1)
(bytevector-set! backward-table $reg backward:uses-arg1)
(bytevector-set! backward-table $load backward:kills-arg1)
(bytevector-set! backward-table $setreg backward:kills-arg1)
(bytevector-set! backward-table $movereg (logior backward:uses-arg1
backward:kills-arg2))
(bytevector-set! backward-table $const/setreg
backward:kills-arg2)
(bytevector-set! backward-table $lambda backward:uses-many)
(bytevector-set! backward-table $lexes backward:uses-many)
(bytevector-set! backward-table $args>= backward:uses-many)
(lambda (instructions)
(let* ((*nregs* *nregs*) ; locals might be faster than globals
; During the forwards traversal:
; (vector-ref registers i) = #f
; means the content of register i is unknown
; (vector-ref registers i) = j
; means register was defined by load i,j
;
; During the backwards traversal:
; (vector-ref registers i) = #f means register i is dead
; (vector-ref registers i) = #t means register i is live
(registers (make-vector *nregs* #f))
; During the forwards traversal, the label of a block that
; falls through into another block or consists of a skip
; to another block is mapped to another label.
; This mapping is implemented by a hash table.
; Before the backwards traversal, the transitive closure
; is computed. The graph has no cycles, and the maximum
; out-degree is 1, so this is easy.
(label-table (make-hashtable (lambda (n) n) assv)))
(define (compute-transitive-closure!)
(define (lookup x)
(let ((y (hashtable-get label-table x)))
(if y
(lookup y)
x)))
(hashtable-for-each (lambda (x y)
(hashtable-put! label-table x (lookup y)))
label-table))
; Don't use this procedure until the preceding procedure
; has been called.
(define (lookup-label x)
(hashtable-fetch label-table x x))
(define (vector-fill! v x)
(subvector-fill! v 0 (vector-length v) x))
(define (subvector-fill! v i j x)
(if (< i j)
(begin (vector-set! v i x)
(subvector-fill! v (+ i 1) j x))))
(define (kill-stack! j)
(do ((i 0 (+ i 1)))
((= i *nregs*))
(let ((x (vector-ref registers i)))
(if (and x (= x j))
(vector-set! registers i #f)))))
; Dispatch procedure for the forwards traversal.
(define (forwards instructions filtered)
(if (null? instructions)
(begin (vector-fill! registers #f)
(vector-set! registers 0 #t)
(compute-transitive-closure!)
(backwards0 filtered '()))
(let* ((instruction (car instructions))
(instructions (cdr instructions))
(op (instruction.op instruction))
(flags (bytevector-ref forward-table op)))
(cond ((eqv? flags forward:normal)
(forwards instructions (cons instruction filtered)))
((eqv? flags forward:nop)
(forwards instructions filtered))
((eqv? flags forward:nop-if-arg1-is-negative)
(if (negative? (instruction.arg1 instruction))
(forwards instructions filtered)
(begin (vector-fill! registers #f)
(forwards instructions
(cons instruction filtered)))))
((eqv? flags forward:kills-all-registers)
(vector-fill! registers #f)
(forwards instructions
(cons instruction filtered)))
((eqv? flags forward:ends-block)
(vector-fill! registers #f)
(if (eqv? op $.label)
(forwards-label instruction
instructions
filtered)
(forwards instructions
(cons instruction filtered))))
((eqv? flags forward:interesting)
(cond ((eqv? op $setreg)
(vector-set! registers
(instruction.arg1 instruction)
#f)
(forwards instructions
(cons instruction filtered)))
((eqv? op $const/setreg)
(vector-set! registers
(instruction.arg2 instruction)
#f)
(forwards instructions
(cons instruction filtered)))
((eqv? op $movereg)
(vector-set! registers
(instruction.arg2 instruction)
#f)
(forwards instructions
(cons instruction filtered)))
((eqv? op $setstk)
(kill-stack! (instruction.arg1 instruction))
(forwards instructions
(cons instruction filtered)))
((eqv? op $load)
(let ((i (instruction.arg1 instruction))
(j (instruction.arg2 instruction)))
(if (eqv? (vector-ref registers i) j)
; Suppress redundant load.
; Should never happen with Twobit.
(suppress-forwards instruction
instructions
filtered)
(begin (vector-set! registers i j)
(forwards instructions
(cons instruction
filtered))))))
((eqv? op $store)
(let ((i (instruction.arg1 instruction))
(j (instruction.arg2 instruction)))
(if (eqv? (vector-ref registers i) j)
; Suppress redundant store.
; Should never happen with Twobit.
(suppress-forwards instruction
instructions
filtered)
(begin (kill-stack! j)
(forwards instructions
(cons instruction
filtered))))))
(else
(local-optimization-error op))))
(else
(local-optimization-error op))))))
; Enters labels into a table for branch tensioning.
(define (forwards-label instruction1 instructions filtered)
(let ((label1 (instruction.arg1 instruction1)))
(if (null? instructions)
; This is ok provided the label is unreachable.
(forwards instructions (cdr filtered))
(let loop ((instructions instructions)
(filtered (cons instruction1 filtered)))
(let* ((instruction (car instructions))
(op (instruction.op instruction))
(flags (bytevector-ref forward-table op)))
(cond ((eqv? flags forward:nop)
(loop (cdr instructions) filtered))
((and (eqv? flags forward:nop-if-arg1-is-negative)
(negative? (instruction.arg1 instruction)))
(loop (cdr instructions) filtered))
((eqv? op $.label)
(let ((label2 (instruction.arg1 instruction)))
(hashtable-put! label-table label1 label2)
(forwards-label instruction
(cdr instructions)
(cdr filtered))))
((eqv? op $skip)
(let ((label2 (instruction.arg1 instruction)))
(hashtable-put! label-table label1 label2)
; We can't get rid of the skip instruction
; because control might fall into this block,
; but we can get rid of the label.
(forwards instructions (cdr filtered))))
(else
(forwards instructions filtered))))))))
; Dispatch procedure for the backwards traversal.
(define (backwards instructions filtered)
(if (null? instructions)
filtered
(let* ((instruction (car instructions))
(instructions (cdr instructions))
(op (instruction.op instruction))
(flags (bytevector-ref backward-table op)))
(cond ((eqv? flags backward:normal)
(backwards instructions (cons instruction filtered)))
((eqv? flags backward:ends-block)
(backwards0 (cons instruction instructions)
filtered))
((eqv? flags backward:begins-block)
(backwards0 instructions
(cons instruction filtered)))
((eqv? flags backward:uses-many)
(cond ((or (eqv? op $lambda)
(eqv? op $lexes))
(let ((live
(if (eqv? op $lexes)
(instruction.arg1 instruction)
(instruction.arg2 instruction))))
(subvector-fill! registers
0
(min *nregs* (+ 1 live))
#t)
(backwards instructions
(cons instruction filtered))))
((eqv? op $args>=)
(vector-fill! registers #t)
(backwards instructions
(cons instruction filtered)))
(else
(local-optimization-error op))))
((and (eqv? (logand flags backward:kills-arg1)
backward:kills-arg1)
(not (vector-ref registers
(instruction.arg1 instruction))))
; Suppress initialization of dead register.
(suppress-backwards instruction
instructions
filtered))
((and (eqv? (logand flags backward:kills-arg2)
backward:kills-arg2)
(not (vector-ref registers
(instruction.arg2 instruction))))
; Suppress initialization of dead register.
(suppress-backwards instruction
instructions
filtered))
((and (eqv? op $movereg)
(= (instruction.arg1 instruction)
(instruction.arg2 instruction)))
(backwards instructions filtered))
(else
(let ((filtered (cons instruction filtered)))
(if (eqv? (logand flags backward:kills-arg1)
backward:kills-arg1)
(vector-set! registers
(instruction.arg1 instruction)
#f))
(if (eqv? (logand flags backward:kills-arg2)
backward:kills-arg2)
(vector-set! registers
(instruction.arg2 instruction)
#f))
(if (eqv? (logand flags backward:uses-arg1)
backward:uses-arg1)
(vector-set! registers
(instruction.arg1 instruction)
#t))
(if (eqv? (logand flags backward:uses-arg2)
backward:uses-arg2)
(vector-set! registers
(instruction.arg2 instruction)
#t))
(if (eqv? (logand flags backward:uses-arg3)
backward:uses-arg3)
(vector-set! registers
(instruction.arg3 instruction)
#t))
(backwards instructions filtered)))))))
; Given a list of instructions in reverse order, whose first
; element is the last instruction of a basic block,
; and a filtered list of instructions in forward order,
; returns a filtered list of instructions in the correct order.
(define (backwards0 instructions filtered)
(if (null? instructions)
filtered
(let* ((instruction (car instructions))
(mnemonic (instruction.op instruction)))
(cond ((or (eqv? mnemonic $.label)
(eqv? mnemonic $.proc)
(eqv? mnemonic $.cont)
(eqv? mnemonic $.align))
(backwards0 (cdr instructions)
(cons instruction filtered)))
; all registers are dead at a $return
((eqv? mnemonic $return)
(vector-fill! registers #f)
(vector-set! registers 0 #t)
(backwards (cdr instructions)
(cons instruction filtered)))
; all but the argument registers are dead at an $invoke
((eqv? mnemonic $invoke)
(let ((n+1 (min *nregs*
(+ (instruction.arg1 instruction) 1))))
(subvector-fill! registers 0 n+1 #t)
(subvector-fill! registers n+1 *nregs* #f)
(backwards (cdr instructions)
(cons instruction filtered))))
; the compiler says which registers are live at the
; target of $skip, $branch, $branchf, or $jump
((or (eqv? mnemonic $skip)
(eqv? mnemonic $branch))
(let* ((live (instruction.arg2 instruction))
(n+1 (min *nregs* (+ live 1))))
(subvector-fill! registers 0 n+1 #t)
(subvector-fill! registers n+1 *nregs* #f)
(let ((instruction
; FIXME
(list mnemonic
(lookup-label
(instruction.arg1 instruction))
live)))
(backwards (cdr instructions)
(cons instruction filtered)))))
((eqv? mnemonic $jump)
(let ((n+1 (min *nregs*
(+ (instruction.arg3 instruction) 1))))
(subvector-fill! registers 0 n+1 #t)
(subvector-fill! registers n+1 *nregs* #f)
(backwards (cdr instructions)
(cons instruction filtered))))
; the live registers at the target of a $branchf must be
; combined with the live registers at the $branchf
((eqv? mnemonic $branchf)
(let* ((live (instruction.arg2 instruction))
(n+1 (min *nregs* (+ live 1))))
(subvector-fill! registers 0 n+1 #t)
(let ((instruction
; FIXME
(list mnemonic
(lookup-label
(instruction.arg1 instruction))
live)))
(backwards (cdr instructions)
(cons instruction filtered)))))
(else (backwards instructions filtered))))))
(define (suppress-forwards instruction instructions filtered)
(if (issue-warnings)
'(begin (display suppression-message)
(newline)))
(forwards instructions filtered))
(define (suppress-backwards instruction instructions filtered)
(if (issue-warnings)
'(begin (display suppression-message)
(newline)))
(backwards instructions filtered))
(define (local-optimization-error op)
(error "Compiler bug: local optimization" op))
(vector-fill! registers #f)
(forwards instructions '())))))
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 28 April 1999
;
; compile313 -- compilation parameters and driver procedures.
; File types -- these may differ between operating systems.
(define *scheme-file-types* '(".sch" ".scm"))
(define *lap-file-type* ".lap")
(define *mal-file-type* ".mal")
(define *lop-file-type* ".lop")
(define *fasl-file-type* ".fasl")
; Compile and assemble a scheme source file and produce a fastload file.
(define (compile-file infilename . rest)
(define (doit)
(let ((outfilename
(if (not (null? rest))
(car rest)
(rewrite-file-type infilename
*scheme-file-types*
*fasl-file-type*)))
(user
(assembly-user-data)))
(if (and (not (integrate-usual-procedures))
(issue-warnings))
(begin
(display "WARNING from compiler: ")
(display "integrate-usual-procedures is turned off")
(newline)
(display "Performance is likely to be poor.")
(newline)))
(if (benchmark-block-mode)
(process-file-block infilename
outfilename
dump-fasl-segment-to-port
(lambda (forms)
(assemble (compile-block forms) user)))
(process-file infilename
outfilename
dump-fasl-segment-to-port
(lambda (expr)
(assemble (compile expr) user))))
(unspecified)))
(if (eq? (nbuild-parameter 'target-machine) 'standard-c)
(error "Compile-file not supported on this target architecture.")
(doit)))
; Assemble a MAL or LOP file and produce a FASL file.
(define (assemble-file infilename . rest)
(define (doit)
(let ((outfilename
(if (not (null? rest))
(car rest)
(rewrite-file-type infilename
(list *lap-file-type* *mal-file-type*)
*fasl-file-type*)))
(malfile?
(file-type=? infilename *mal-file-type*))
(user
(assembly-user-data)))
(process-file infilename
outfilename
dump-fasl-segment-to-port
(lambda (x) (assemble (if malfile? (eval x) x) user)))
(unspecified)))
(if (eq? (nbuild-parameter 'target-machine) 'standard-c)
(error "Assemble-file not supported on this target architecture.")
(doit)))
; Compile and assemble a single expression; return the LOP segment.
(define compile-expression
(let ()
(define (compile-expression expr env)
(let ((syntax-env
(case (environment-tag env)
((0 1) (make-standard-syntactic-environment))
((2) global-syntactic-environment)
(else
(error "Invalid environment for compile-expression: " env)
#t))))
(let ((current-env global-syntactic-environment))
(dynamic-wind
(lambda ()
(set! global-syntactic-environment syntax-env))
(lambda ()
(assemble (compile expr)))
(lambda ()
(set! global-syntactic-environment current-env))))))
compile-expression))
(define macro-expand-expression
(let ()
(define (macro-expand-expression expr env)
(let ((syntax-env
(case (environment-tag env)
((0 1) (make-standard-syntactic-environment))
((2) global-syntactic-environment)
(else
(error "Invalid environment for compile-expression: " env)
#t))))
(let ((current-env global-syntactic-environment))
(dynamic-wind
(lambda ()
(set! global-syntactic-environment syntax-env))
(lambda ()
(make-readable
(macro-expand expr)))
(lambda ()
(set! global-syntactic-environment current-env))))))
macro-expand-expression))
; Compile a scheme source file to a LAP file.
(define (compile313 infilename . rest)
(let ((outfilename
(if (not (null? rest))
(car rest)
(rewrite-file-type infilename
*scheme-file-types*
*lap-file-type*)))
(write-lap
(lambda (item port)
(write item port)
(newline port)
(newline port))))
(if (benchmark-block-mode)
(process-file-block infilename outfilename write-lap compile-block)
(process-file infilename outfilename write-lap compile))
(unspecified)))
; Assemble a LAP or MAL file to a LOP file.
(define (assemble313 file . rest)
(let ((outputfile
(if (not (null? rest))
(car rest)
(rewrite-file-type file
(list *lap-file-type* *mal-file-type*)
*lop-file-type*)))
(malfile?
(file-type=? file *mal-file-type*))
(user
(assembly-user-data)))
(process-file file
outputfile
write-lop
(lambda (x) (assemble (if malfile? (eval x) x) user)))
(unspecified)))
; Compile and assemble a Scheme source file to a LOP file.
(define (compile-and-assemble313 input-file . rest)
(let ((output-file
(if (not (null? rest))
(car rest)
(rewrite-file-type input-file
*scheme-file-types*
*lop-file-type*)))
(user
(assembly-user-data)))
(if (benchmark-block-mode)
(process-file-block input-file
output-file
write-lop
(lambda (x) (assemble (compile-block x) user)))
(process-file input-file
output-file
write-lop
(lambda (x) (assemble (compile x) user))))
(unspecified)))
; Convert a LOP file to a FASL file.
(define (make-fasl infilename . rest)
(define (doit)
(let ((outfilename
(if (not (null? rest))
(car rest)
(rewrite-file-type infilename
*lop-file-type*
*fasl-file-type*))))
(process-file infilename
outfilename
dump-fasl-segment-to-port
(lambda (x) x))
(unspecified)))
(if (eq? (nbuild-parameter 'target-machine) 'standard-c)
(error "Make-fasl not supported on this target architecture.")
(doit)))
; Disassemble a procedure's code vector.
(define (disassemble item . rest)
(let ((output-port (if (null? rest)
(current-output-port)
(car rest))))
(disassemble-item item #f output-port)
(unspecified)))
; The item can be either a procedure or a pair (assumed to be a segment).
(define (disassemble-item item segment-no port)
(define (print . rest)
(for-each (lambda (x) (display x port)) rest)
(newline port))
(define (print-constvector cv)
(do ((i 0 (+ i 1)))
((= i (vector-length cv)))
(print "------------------------------------------")
(print "Constant vector element # " i)
(case (car (vector-ref cv i))
((codevector)
(print "Code vector")
(print-instructions (disassemble-codevector
(cadr (vector-ref cv i)))
port))
((constantvector)
(print "Constant vector")
(print-constvector (cadr (vector-ref cv i))))
((global)
(print "Global: " (cadr (vector-ref cv i))))
((data)
(print "Data: " (cadr (vector-ref cv i)))))))
(define (print-segment segment)
(print "Segment # " segment-no)
(print-instructions (disassemble-codevector (car segment)) port)
(print-constvector (cdr segment))
(print "========================================"))
(cond ((procedure? item)
(print-instructions (disassemble-codevector (procedure-ref item 0))
port))
((and (pair? item)
(bytevector? (car item))
(vector? (cdr item)))
(print-segment item))
(else
(error "disassemble-item: " item " is not disassemblable."))))
; Disassemble a ".lop" or ".fasl" file; dump output to screen or
; other (optional) file.
(define (disassemble-file file . rest)
(define (doit input-port output-port)
(display "; From " output-port)
(display file output-port)
(newline output-port)
(do ((segment-no 0 (+ segment-no 1))
(segment (read input-port) (read input-port)))
((eof-object? segment))
(disassemble-item segment segment-no output-port)))
; disassemble313
(call-with-input-file
file
(lambda (input-port)
(if (null? rest)
(doit input-port (current-output-port))
(begin
(delete-file (car rest))
(call-with-output-file
(car rest)
(lambda (output-port) (doit input-port output-port)))))))
(unspecified))
; Display and manipulate the compiler switches.
(define (compiler-switches . rest)
(define (slow-code)
(set-compiler-flags! 'no-optimization)
(set-assembler-flags! 'no-optimization))
(define (standard-code)
(set-compiler-flags! 'standard)
(set-assembler-flags! 'standard))
(define (fast-safe-code)
(set-compiler-flags! 'fast-safe)
(set-assembler-flags! 'fast-safe))
(define (fast-unsafe-code)
(set-compiler-flags! 'fast-unsafe)
(set-assembler-flags! 'fast-unsafe))
(cond ((null? rest)
(display "Debugging:")
(newline)
(display-twobit-flags 'debugging)
(display-assembler-flags 'debugging)
(newline)
(display "Safety:")
(newline)
(display-twobit-flags 'safety)
(display-assembler-flags 'safety)
(newline)
(display "Speed:")
(newline)
(display-twobit-flags 'optimization)
(display-assembler-flags 'optimization)
(if #f #f))
((null? (cdr rest))
(case (car rest)
((0 slow) (slow-code))
((1 standard) (standard-code))
((2 fast-safe) (fast-safe-code))
((3 fast-unsafe) (fast-unsafe-code))
((default
factory-settings) (fast-safe-code)
(include-source-code #t)
(benchmark-mode #f)
(benchmark-block-mode #f)
(common-subexpression-elimination #f)
(representation-inference #f))
(else
(error "Unrecognized flag " (car rest) " to compiler-switches.")))
(unspecified))
(else
(error "Too many arguments to compiler-switches."))))
; Read and process one file, producing another.
; Preserves the global syntactic environment.
(define (process-file infilename outfilename writer processer)
(define (doit)
(delete-file outfilename)
(call-with-output-file
outfilename
(lambda (outport)
(call-with-input-file
infilename
(lambda (inport)
(let loop ((x (read inport)))
(if (eof-object? x)
#t
(begin (writer (processer x) outport)
(loop (read inport))))))))))
(let ((current-syntactic-environment
(syntactic-copy global-syntactic-environment)))
(dynamic-wind
(lambda () #t)
(lambda () (doit))
(lambda ()
(set! global-syntactic-environment
current-syntactic-environment)))))
; Same as above, but passes a list of the entire file's contents
; to the processer.
; FIXME: Both versions of PROCESS-FILE always delete the output file.
; Shouldn't it be left alone if the input file can't be opened?
(define (process-file-block infilename outfilename writer processer)
(define (doit)
(delete-file outfilename)
(call-with-output-file
outfilename
(lambda (outport)
(call-with-input-file
infilename
(lambda (inport)
(do ((x (read inport) (read inport))
(forms '() (cons x forms)))
((eof-object? x)
(writer (processer (reverse forms)) outport))))))))
(let ((current-syntactic-environment
(syntactic-copy global-syntactic-environment)))
(dynamic-wind
(lambda () #t)
(lambda () (doit))
(lambda ()
(set! global-syntactic-environment
current-syntactic-environment)))))
; Given a file name with some type, produce another with some other type.
(define (rewrite-file-type filename matches new)
(if (not (pair? matches))
(rewrite-file-type filename (list matches) new)
(let ((j (string-length filename)))
(let loop ((m matches))
(cond ((null? m)
(string-append filename new))
(else
(let* ((n (car m))
(l (string-length n)))
(if (file-type=? filename n)
(string-append (substring filename 0 (- j l)) new)
(loop (cdr m))))))))))
(define (file-type=? file-name type-name)
(let ((fl (string-length file-name))
(tl (string-length type-name)))
(and (>= fl tl)
(string-ci=? type-name
(substring file-name (- fl tl) fl)))))
; eof
; Copyright 1998 William Clinger.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Procedures that make .LAP structures human-readable
(define (readify-lap code)
(map (lambda (x)
(let ((iname (cdr (assv (car x) *mnemonic-names*))))
(if (not (= (car x) $lambda))
(cons iname (cdr x))
(list iname (readify-lap (cadr x)) (caddr x)))))
code))
(define (readify-file f . o)
(define (doit)
(let ((i (open-input-file f)))
(let loop ((x (read i)))
(if (not (eof-object? x))
(begin (pretty-print (readify-lap x))
(loop (read i)))))))
(if (null? o)
(doit)
(begin (delete-file (car o))
(with-output-to-file (car o) doit))))
; eof
; Copyright 1991 Lightship Software, Incorporated.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Target-independent part of the assembler.
;
; This is a simple, table-driven, one-pass assembler.
; Part of it assumes a big-endian target machine.
;
; The input to this pass is a list of symbolic MacScheme machine
; instructions and pseudo-instructions. Each symbolic MacScheme
; machine instruction or pseudo-instruction is a list whose car
; is a small non-negative fixnum that acts as the mnemonic for the
; instruction. The rest of the list is interpreted as indicated
; by the mnemonic.
;
; The output is a pair consisting of machine code (a bytevector or
; string) and a constant vector.
;
; This assembler is table-driven, and may be customized to emit
; machine code for different target machines. The table consists
; of a vector of procedures indexed by mnemonics. Each procedure
; in the table should take two arguments: an assembly structure
; and a source instruction. The procedure should just assemble
; the instruction using the operations defined below.
;
; The table and target can be changed by redefining the following
; five procedures.
(define (assembly-table) (error "No assembly table defined."))
(define (assembly-start as) #t)
(define (assembly-end as segment) segment)
(define (assembly-user-data) #f)
; The main entry point.
(define (assemble source . rest)
(let* ((user (if (null? rest) (assembly-user-data) (car rest)))
(as (make-assembly-structure source (assembly-table) user)))
(assembly-start as)
(assemble1 as
(lambda (as)
(let ((segment (assemble-pasteup as)))
(assemble-finalize! as)
(assembly-end as segment)))
#f)))
; The following procedures are to be called by table routines.
;
; The assembly source for nested lambda expressions should be
; assembled by calling this procedure. This allows an inner
; lambda to refer to labels defined by outer lambdas.
;
; We delay the assembly of the nested lambda until after the outer lambda
; has been finalized so that all labels in the outer lambda are known
; to the inner lambda.
;
; The continuation procedure k is called to backpatch the constant
; vector of the outer lambda after the inner lambda has been
; finalized. This is necessary because of the delayed evaluation: the
; outer lambda holds code and constants for the inner lambda in its
; constant vector.
(define (assemble-nested-lambda as source doc k . rest)
(let* ((user (if (null? rest) #f (car rest)))
(nested-as (make-assembly-structure source (as-table as) user)))
(as-parent! nested-as as)
(as-nested! as (cons (lambda ()
(assemble1 nested-as
(lambda (nested-as)
(let ((segment
(assemble-pasteup nested-as)))
(assemble-finalize! nested-as)
(k nested-as segment)))
doc))
(as-nested as)))))
(define operand0 car) ; the mnemonic
(define operand1 cadr)
(define operand2 caddr)
(define operand3 cadddr)
(define (operand4 i) (car (cddddr i)))
; Emits the bits contained in the bytevector bv.
(define (emit! as bv)
(as-code! as (cons bv (as-code as)))
(as-lc! as (+ (as-lc as) (bytevector-length bv))))
; Emits the characters contained in the string s as code (for C generation).
(define (emit-string! as s)
(as-code! as (cons s (as-code as)))
(as-lc! as (+ (as-lc as) (string-length s))))
; Given any Scheme object that may legally be quoted, returns an
; index into the constant vector for that constant.
(define (emit-constant as x)
(do ((i 0 (+ i 1))
(y (as-constants as) (cdr y)))
((or (null? y) (equal? x (car y)))
(if (null? y)
(as-constants! as (append! (as-constants as) (list x))))
i)))
(define (emit-datum as x)
(emit-constant as (list 'data x)))
(define (emit-global as x)
(emit-constant as (list 'global x)))
(define (emit-codevector as x)
(emit-constants as (list 'codevector x)))
(define (emit-constantvector as x)
(emit-constants as (list 'constantvector x)))
; Set-constant changes the datum stored, without affecting the tag.
; It can operate on the list form because the pair stored in the list
; is shared between the list and any vector created from the list.
(define (set-constant! as n datum)
(let ((pair (list-ref (as-constants as) n)))
(set-car! (cdr pair) datum)))
; Guarantees that the constants will not share structure
; with any others, and will occupy consecutive positions
; in the constant vector. Returns the index of the first
; constant.
(define (emit-constants as x . rest)
(let* ((constants (as-constants as))
(i (length constants)))
(as-constants! as (append! constants (cons x rest)))
i))
; Defines the given label using the current location counter.
(define (emit-label! as L)
(set-cdr! L (as-lc as)))
; Adds the integer n to the size code bytes beginning at the
; given byte offset from the current value of the location counter.
(define (emit-fixup! as offset size n)
(as-fixups! as (cons (list (+ offset (as-lc as)) size n)
(as-fixups as))))
; Adds the value of the label L to the size code bytes beginning
; at the given byte offset from the current location counter.
(define (emit-fixup-label! as offset size L)
(as-fixups! as (cons (list (+ offset (as-lc as)) size (list L))
(as-fixups as))))
; Allows the procedure proc of two arguments (code vector and current
; location counter) to modify the code vector at will, at fixup time.
(define (emit-fixup-proc! as proc)
(as-fixups! as (cons (list (as-lc as) 0 proc)
(as-fixups as))))
; Labels.
; The current value of the location counter.
(define (here as) (as-lc as))
; Given a MAL label (a number), create an assembler label.
(define (make-asm-label as label)
(let ((probe (find-label as label)))
(if probe
probe
(let ((l (cons label #f)))
(as-labels! as (cons l (as-labels as)))
l))))
; This can use hashed lookup.
(define (find-label as L)
(define (lookup-label-loop x labels parent)
(let ((entry (assq x labels)))
(cond (entry)
((not parent) #f)
(else
(lookup-label-loop x (as-labels parent) (as-parent parent))))))
(lookup-label-loop L (as-labels as) (as-parent as)))
; Create a new assembler label, distinguishable from a MAL label.
(define new-label
(let ((n 0))
(lambda ()
(set! n (- n 1))
(cons n #f))))
; Given a value name (a number), return the label value or #f.
(define (label-value as L) (cdr L))
; For peephole optimization.
(define (next-instruction as)
(let ((source (as-source as)))
(if (null? source)
'(-1)
(car source))))
(define (consume-next-instruction! as)
(as-source! as (cdr (as-source as))))
(define (push-instruction as instruction)
(as-source! as (cons instruction (as-source as))))
; For use by the machine assembler: assoc lists connected to as structure.
(define (assembler-value as key)
(let ((probe (assq key (as-values as))))
(if probe
(cdr probe)
#f)))
(define (assembler-value! as key value)
(let ((probe (assq key (as-values as))))
(if probe
(set-cdr! probe value)
(as-values! as (cons (cons key value) (as-values as))))))
; For documentation.
;
; The value must be a documentation structure (a vector).
(define (add-documentation as doc)
(let* ((existing-constants (cadr (car (as-constants as))))
(new-constants
(twobit-sort (lambda (a b)
(< (car a) (car b)))
(cond ((not existing-constants)
(list (cons (here as) doc)))
((pair? existing-constants)
(cons (cons (here as) doc)
existing-constants))
(else
(list (cons (here as) doc)
(cons 0 existing-constants)))))))
(set-car! (cdar (as-constants as)) new-constants)))
; This is called when a value is too large to be handled by the assembler.
; Info is a string, expr an assembler expression, and val the resulting
; value. The default behavior is to signal an error.
(define (asm-value-too-large as info expr val)
(if (as-retry as)
((as-retry as))
(asm-error info ": Value too large: " expr " = " val)))
; The implementations of asm-error and disasm-error depend on the host
; system. Sigh.
(define (asm-error msg . rest)
(cond ((eq? host-system 'chez)
(error 'assembler "~a" (list msg rest)))
(else
(apply error msg rest))))
(define (disasm-error msg . rest)
(cond ((eq? host-system 'chez)
(error 'disassembler "~a" (list msg rest)))
(else
(apply error msg rest))))
; The remaining procedures in this file are local to the assembler.
; An assembly structure is a vector consisting of
;
; table (a table of assembly routines)
; source (a list of symbolic instructions)
; lc (location counter; an integer)
; code (a list of bytevectors)
; constants (a list)
; labels (an alist of labels and values)
; fixups (an alist of locations, sizes, and labels or fixnums)
; nested (a list of assembly procedures for nested lambdas)
; values (an assoc list)
; parent (an assembly structure or #f)
; retry (a thunk or #f)
; user-data (anything)
;
; In fixups, labels are of the form (<L>) to distinguish them from fixnums.
(define (label? x) (and (pair? x) (fixnum? (car x))))
(define label.ident car)
(define (make-assembly-structure source table user-data)
(vector table
source
0
'()
'()
'()
'()
'()
'()
#f
#f
user-data))
(define (as-reset! as source)
(as-source! as source)
(as-lc! as 0)
(as-code! as '())
(as-constants! as '())
(as-labels! as '())
(as-fixups! as '())
(as-nested! as '())
(as-values! as '())
(as-retry! as #f))
(define (as-table as) (vector-ref as 0))
(define (as-source as) (vector-ref as 1))
(define (as-lc as) (vector-ref as 2))
(define (as-code as) (vector-ref as 3))
(define (as-constants as) (vector-ref as 4))
(define (as-labels as) (vector-ref as 5))
(define (as-fixups as) (vector-ref as 6))
(define (as-nested as) (vector-ref as 7))
(define (as-values as) (vector-ref as 8))
(define (as-parent as) (vector-ref as 9))
(define (as-retry as) (vector-ref as 10))
(define (as-user as) (vector-ref as 11))
(define (as-source! as x) (vector-set! as 1 x))
(define (as-lc! as x) (vector-set! as 2 x))
(define (as-code! as x) (vector-set! as 3 x))
(define (as-constants! as x) (vector-set! as 4 x))
(define (as-labels! as x) (vector-set! as 5 x))
(define (as-fixups! as x) (vector-set! as 6 x))
(define (as-nested! as x) (vector-set! as 7 x))
(define (as-values! as x) (vector-set! as 8 x))
(define (as-parent! as x) (vector-set! as 9 x))
(define (as-retry! as x) (vector-set! as 10 x))
(define (as-user! as x) (vector-set! as 11 x))
; The guts of the assembler.
(define (assemble1 as finalize doc)
(let ((assembly-table (as-table as))
(peep? (peephole-optimization))
(step? (single-stepping))
(step-instr (list $.singlestep))
(end-instr (list $.end)))
(define (loop)
(let ((source (as-source as)))
(if (null? source)
(begin ((vector-ref assembly-table $.end) end-instr as)
(finalize as))
(begin (if step?
((vector-ref assembly-table $.singlestep)
step-instr
as))
(if peep?
(let peeploop ((src1 source))
(peep as)
(let ((src2 (as-source as)))
(if (not (eq? src1 src2))
(peeploop src2)))))
(let ((source (as-source as)))
(as-source! as (cdr source))
((vector-ref assembly-table (caar source))
(car source)
as)
(loop))))))
(define (doit)
(emit-datum as doc)
(loop))
(let* ((source (as-source as))
(r (call-with-current-continuation
(lambda (k)
(as-retry! as (lambda () (k 'retry)))
(doit)))))
(if (eq? r 'retry)
(let ((old (short-effective-addresses)))
(as-reset! as source)
(dynamic-wind
(lambda ()
(short-effective-addresses #f))
doit
(lambda ()
(short-effective-addresses old))))
r))))
(define (assemble-pasteup as)
(define (pasteup-code)
(let ((code (make-bytevector (as-lc as)))
(constants (list->vector (as-constants as))))
; The bytevectors: byte 0 is most significant.
(define (paste-code! bvs i)
(if (not (null? bvs))
(let* ((bv (car bvs))
(n (bytevector-length bv)))
(do ((i i (- i 1))
(j (- n 1) (- j 1))) ; (j 0 (+ j 1))
((< j 0) ; (= j n)
(paste-code! (cdr bvs) i))
(bytevector-set! code i (bytevector-ref bv j))))))
(paste-code! (as-code as) (- (as-lc as) 1))
(as-code! as (list code))
(cons code constants)))
(define (pasteup-strings)
(let ((code (make-string (as-lc as)))
(constants (list->vector (as-constants as))))
(define (paste-code! strs i)
(if (not (null? strs))
(let* ((s (car strs))
(n (string-length s)))
(do ((i i (- i 1))
(j (- n 1) (- j 1))) ; (j 0 (+ j 1))
((< j 0) ; (= j n)
(paste-code! (cdr strs) i))
(string-set! code i (string-ref s j))))))
(paste-code! (as-code as) (- (as-lc as) 1))
(as-code! as (list code))
(cons code constants)))
(if (bytevector? (car (as-code as)))
(pasteup-code)
(pasteup-strings)))
(define (assemble-finalize! as)
(let ((code (car (as-code as))))
(define (apply-fixups! fixups)
(if (not (null? fixups))
(let* ((fixup (car fixups))
(i (car fixup))
(size (cadr fixup))
(adjustment (caddr fixup)) ; may be procedure
(n (if (label? adjustment)
(lookup-label adjustment)
adjustment)))
(case size
((0) (fixup-proc code i n))
((1) (fixup1 code i n))
((2) (fixup2 code i n))
((3) (fixup3 code i n))
((4) (fixup4 code i n))
(else ???))
(apply-fixups! (cdr fixups)))))
(define (lookup-label L)
(or (label-value as (label.ident L))
(asm-error "Assembler error -- undefined label " L)))
(apply-fixups! (reverse! (as-fixups as)))
(for-each (lambda (nested-as-proc)
(nested-as-proc))
(as-nested as))))
; These fixup routines assume a big-endian target machine.
(define (fixup1 code i n)
(bytevector-set! code i (+ n (bytevector-ref code i))))
(define (fixup2 code i n)
(let* ((x (+ (* 256 (bytevector-ref code i))
(bytevector-ref code (+ i 1))))
(y (+ x n))
(y0 (modulo y 256))
(y1 (modulo (quotient (- y y0) 256) 256)))
(bytevector-set! code i y1)
(bytevector-set! code (+ i 1) y0)))
(define (fixup3 code i n)
(let* ((x (+ (* 65536 (bytevector-ref code i))
(* 256 (bytevector-ref code (+ i 1)))
(bytevector-ref code (+ i 2))))
(y (+ x n))
(y0 (modulo y 256))
(y1 (modulo (quotient (- y y0) 256) 256))
(y2 (modulo (quotient (- y (* 256 y1) y0) 256) 256)))
(bytevector-set! code i y2)
(bytevector-set! code (+ i 1) y1)
(bytevector-set! code (+ i 2) y0)))
(define (fixup4 code i n)
(let* ((x (+ (* 16777216 (bytevector-ref code i))
(* 65536 (bytevector-ref code (+ i 1)))
(* 256 (bytevector-ref code (+ i 2)))
(bytevector-ref code (+ i 3))))
(y (+ x n))
(y0 (modulo y 256))
(y1 (modulo (quotient (- y y0) 256) 256))
(y2 (modulo (quotient (- y (* 256 y1) y0) 256) 256))
(y3 (modulo (quotient (- y (* 65536 y2)
(* 256 y1)
y0)
256)
256)))
(bytevector-set! code i y3)
(bytevector-set! code (+ i 1) y2)
(bytevector-set! code (+ i 2) y1)
(bytevector-set! code (+ i 3) y0)))
(define (fixup-proc code i p)
(p code i))
; For testing.
(define (view-segment segment)
(define (display-bytevector bv)
(let ((n (bytevector-length bv)))
(do ((i 0 (+ i 1)))
((= i n))
(if (zero? (remainder i 4))
(write-char #\space))
(if (zero? (remainder i 8))
(write-char #\space))
(if (zero? (remainder i 32))
(newline))
(let ((byte (bytevector-ref bv i)))
(write-char
(string-ref (number->string (quotient byte 16) 16) 0))
(write-char
(string-ref (number->string (remainder byte 16) 16) 0))))))
(if (and (pair? segment)
(bytevector? (car segment))
(vector? (cdr segment)))
(begin (display-bytevector (car segment))
(newline)
(write (cdr segment))
(newline)
(do ((constants (vector->list (cdr segment))
(cdr constants)))
((or (null? constants)
(null? (cdr constants))))
(if (and (bytevector? (car constants))
(vector? (cadr constants)))
(view-segment (cons (car constants)
(cadr constants))))))))
; emit is a procedure that takes an as and emits instructions into it.
(define (test-asm emit)
(let ((as (make-assembly-structure #f #f #f)))
(emit as)
(let ((segment (assemble-pasteup as)))
(assemble-finalize! as)
(disassemble segment))))
(define (compile&assemble x)
(view-segment (assemble (compile x))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Common assembler -- miscellaneous utility procedures.
; Given any Scheme object, return its printable representation as a string.
; This code is largely portable (see comments).
(define (format-object x)
(define (format-list x)
(define (loop x)
(cond ((null? x)
'(")"))
((null? (cdr x))
(list (format-object (car x)) ")"))
(else
(cons (format-object (car x))
(cons " "
(loop (cdr x)))))))
(apply string-append (cons "(" (loop x))))
(define (format-improper-list x)
(define (loop x)
(if (pair? (cdr x))
(cons (format-object (car x))
(cons " "
(loop (cdr x))))
(list (format-object (car x))
" . "
(format-object (cdr x))
")")))
(apply string-append (cons "(" (loop x))))
(cond ((null? x) "()")
((not x) "#f")
((eq? x #t) "#t")
((symbol? x) (symbol->string x))
((number? x) (number->string x))
((char? x) (string x))
((string? x) x)
((procedure? x) "#<procedure>")
((bytevector? x) "#<bytevector>") ; Larceny
((eof-object? x) "#<eof>")
((port? x) "#<port>")
((eq? x (unspecified)) "#!unspecified") ; Larceny
((eq? x (undefined)) "#!undefined") ; Larceny
((vector? x)
(string-append "#" (format-list (vector->list x))))
((list? x)
(format-list x))
((pair? x)
(format-improper-list x))
(else "#<weird>")))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Larceny assembler -- 32-bit big-endian utility procedures.
;
; 32-bit numbers are represented as 4-byte bytevectors where byte 3
; is the least significant and byte 0 is the most significant.
;
; Logically, the 'big' end is on the left and the 'little' end
; is on the right, so a left shift shifts towards the 'big' end.
;
; Performance: poor, for good reasons. See asmutil32.sch.
; Identifies the code loaded.
(define asm:endianness 'big)
; Given four bytes, create a length-4 bytevector.
; N1 is the most significant byte, n4 the least significant.
(define (asm:bv n1 n2 n3 n4)
(let ((bv (make-bytevector 4)))
(bytevector-set! bv 0 n1)
(bytevector-set! bv 1 n2)
(bytevector-set! bv 2 n3)
(bytevector-set! bv 3 n4)
bv))
; Given a length-4 bytevector, convert it to an integer.
(define (asm:bv->int bv)
(let ((i (+ (* (+ (* (+ (* (bytevector-ref bv 0) 256)
(bytevector-ref bv 1))
256)
(bytevector-ref bv 2))
256)
(bytevector-ref bv 3))))
(if (> (bytevector-ref bv 0) 127)
(- i)
i)))
; Shift the bits of m left by n bits, shifting in zeroes at the right end.
; Returns a length-4 bytevector.
;
; M may be an exact integer or a length-4 bytevector.
; N must be an exact nonnegative integer; it's interpreted modulo 33.
(define (asm:lsh m n)
(if (not (bytevector? m))
(asm:lsh (asm:int->bv m) n)
(let ((m (bytevector-copy m))
(n (remainder n 33)))
(if (>= n 8)
(let ((k (quotient n 8)))
(do ((i 0 (+ i 1)))
((= (+ i k) 4)
(do ((i i (+ i 1)))
((= i 4))
(bytevector-set! m i 0)))
(bytevector-set! m i (bytevector-ref m (+ i k))))))
(let* ((d0 (bytevector-ref m 0))
(d1 (bytevector-ref m 1))
(d2 (bytevector-ref m 2))
(d3 (bytevector-ref m 3))
(n (remainder n 8))
(n- (- 8 n)))
(asm:bv (logand (logior (lsh d0 n) (rshl d1 n-)) 255)
(logand (logior (lsh d1 n) (rshl d2 n-)) 255)
(logand (logior (lsh d2 n) (rshl d3 n-)) 255)
(logand (lsh d3 n) 255))))))
; Shift the bits of m right by n bits, shifting in zeroes at the high end.
; Returns a length-4 bytevector.
;
; M may be an exact integer or a length-4 bytevector.
; N must be an exact nonnegative integer; it's interpreted modulo 33.
(define (asm:rshl m n)
(if (not (bytevector? m))
(asm:rshl (asm:int->bv m) n)
(let ((m (bytevector-copy m))
(n (remainder n 33)))
(if (>= n 8)
(let ((k (quotient n 8)))
(do ((i 3 (- i 1)))
((< (- i k) 0)
(do ((i i (- i 1)))
((< i 0))
(bytevector-set! m i 0)))
(bytevector-set! m i (bytevector-ref m (- i k))))))
(let* ((d0 (bytevector-ref m 0))
(d1 (bytevector-ref m 1))
(d2 (bytevector-ref m 2))
(d3 (bytevector-ref m 3))
(n (remainder n 8))
(n- (- 8 n)))
(asm:bv (rshl d0 n)
(logand (logior (rshl d1 n) (lsh d0 n-)) 255)
(logand (logior (rshl d2 n) (lsh d1 n-)) 255)
(logand (logior (rshl d3 n) (lsh d2 n-)) 255))))))
; Shift the bits of m right by n bits, shifting in the sign bit at the
; high end. Returns a length-4 bytevector.
;
; M may be an exact integer or a length-4 bytevector.
; N must be an exact nonnegative integer; it's interpreted modulo 33.
(define asm:rsha
(let ((ones (asm:bv #xff #xff #xff #xff)))
(lambda (m n)
(let* ((m (if (bytevector? m) m (asm:int->bv m)))
(n (remainder n 33))
(h (rshl (bytevector-ref m 0) 7))
(k (asm:rshl m n)))
; (format #t "~a ~a ~a~%" h (bytevector-ref m 0) n)
; (prnx (asm:lsh ones (- 32 n))) (newline)
(if (zero? h)
k
(asm:logior k (asm:lsh ones (- 32 n))))))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Larceny assembler -- 32-bit endianness-independent utility procedures.
;
; 32-bit numbers are represented as 4-byte bytevectors where the
; exact layout depends on whether the little-endian or big-endian
; module has been loaded. One of them must be loaded prior to loading
; this module.
;
; Logically, the 'big' end is on the left and the 'little' end
; is on the right, so a left shift shifts towards the big end.
;
; Generally, performance is not a major issue in this module. The
; assemblers should use more specialized code for truly good performance.
; These procedures are mainly suitable for one-time construction of
; instruction templates, and during development.
;
; Endian-ness specific operations are in asmutil32be.sch and asmutil32le.sch:
;
; (asm:bv n0 n1 n2 n3) ; Construct bytevector
; (asm:bv->int bv) ; Convert bytevector to integer
; (asm:lsh m k) ; Shift left logical k bits
; (asm:rshl m k) ; Shift right logical k bits
; (asm:rsha m k) ; Shirt right arithmetic k bits
; Convert an integer to a length-4 bytevector using two's complement
; representation for negative numbers.
; Returns length-4 bytevector.
;
; The procedure handles numbers in the range -2^31..2^32-1 [sic].
; It is an error for the number to be outside this range.
;
; FIXME: quotient/remainder may be slow; we could have special fixnum
; case that uses shifts (that could be in-lined as macro). It could
; work for negative numbers too.
; FIXME: should probably check that the number is within range.
(define asm:int->bv
(let ((two^32 (expt 2 32)))
(lambda (m)
(let* ((m (if (< m 0) (+ two^32 m) m))
(b0 (remainder m 256))
(m (quotient m 256))
(b1 (remainder m 256))
(m (quotient m 256))
(b2 (remainder m 256))
(m (quotient m 256))
(b3 (remainder m 256)))
(asm:bv b3 b2 b1 b0)))))
; `Or' the bits of multiple operands together.
; Each operand may be an exact integer or a length-4 bytevector.
; Returns a length-4 bytevector.
(define (asm:logior . ops)
(let ((r (asm:bv 0 0 0 0)))
(do ((ops ops (cdr ops)))
((null? ops) r)
(let* ((op (car ops))
(op (if (bytevector? op) op (asm:int->bv op))))
(bytevector-set! r 0 (logior (bytevector-ref r 0)
(bytevector-ref op 0)))
(bytevector-set! r 1 (logior (bytevector-ref r 1)
(bytevector-ref op 1)))
(bytevector-set! r 2 (logior (bytevector-ref r 2)
(bytevector-ref op 2)))
(bytevector-set! r 3 (logior (bytevector-ref r 3)
(bytevector-ref op 3)))))))
; `And' the bits of two operands together.
; Either may be an exact integer or length-4 bytevector.
; Returns length-4 bytevector.
(define (asm:logand op1 op2)
(let ((op1 (if (bytevector? op1) op1 (asm:int->bv op1)))
(op2 (if (bytevector? op2) op2 (asm:int->bv op2)))
(bv (make-bytevector 4)))
(bytevector-set! bv 0 (logand (bytevector-ref op1 0)
(bytevector-ref op2 0)))
(bytevector-set! bv 1 (logand (bytevector-ref op1 1)
(bytevector-ref op2 1)))
(bytevector-set! bv 2 (logand (bytevector-ref op1 2)
(bytevector-ref op2 2)))
(bytevector-set! bv 3 (logand (bytevector-ref op1 3)
(bytevector-ref op2 3)))
bv))
; Extract the n low-order bits of m.
; m may be an exact integer or a length-4 bytevector.
; n must be an exact nonnegative integer, interpreted modulo 32.
; Returns length-4 bytevector.
;
; Does not depend on endian-ness.
(define asm:lobits
(let ((v (make-vector 33)))
(do ((i 0 (+ i 1)))
((= i 33))
(vector-set! v i (asm:int->bv (- (expt 2 i) 1))))
(lambda (m n)
(asm:logand m (vector-ref v (remainder n 33))))))
; Extract the n high-order bits of m.
; m may be an exact integer or a length-4 bytevector.
; n must be an exact nonnegative integer, interpreted modulo 33.
; Returns length-4 bytevector with the high-order bits of m at low end.
;
; Does not depend on endian-ness.
(define (asm:hibits m n)
(asm:rshl m (- 32 (remainder n 33))))
; Test that the given number (not! bytevector) m fits in an n-bit
; signed slot.
;
; Does not depend on endian-ness.
(define asm:fits?
(let ((v (make-vector 33)))
(do ((i 0 (+ i 1)))
((= i 33))
(vector-set! v i (expt 2 i)))
(lambda (m n)
(<= (- (vector-ref v (- n 1))) m (- (vector-ref v (- n 1)) 1)))))
; Test that the given number (not! bytevector) m fits in an n-bit
; unsigned slot.
;
; Does not depend on endian-ness.
(define asm:fits-unsigned?
(let ((v (make-vector 33)))
(do ((i 0 (+ i 1)))
((= i 33))
(vector-set! v i (expt 2 i)))
(lambda (m n)
(<= 0 m (- (vector-ref v n) 1)))))
; Add two operands (numbers or bytevectors).
;
; Does not depend on endian-ness.
(define (asm:add a b)
(asm:int->bv (+ (if (bytevector? a) (asm:bv->int a) a)
(if (bytevector? b) (asm:bv->int b) b))))
; Given an unsigned 32-bit number, return it as a signed number
; as appropriate.
;
; Does not depend on endian-ness.
(define (asm:signed n)
(if (< n 2147483647)
n
(- n 4294967296)))
(define (asm:print-bv bv)
(define hex "0123456789abcdef")
(define (pdig k)
(display (string-ref hex (quotient k 16)))
(display (string-ref hex (remainder k 16)))
(display " "))
(if (eq? asm:endianness 'little)
(do ((i 3 (- i 1)))
((< i 0))
(pdig (bytevector-ref bv i)))
(do ((i 0 (+ i 1)))
((= i 4))
(pdig (bytevector-ref bv i)))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Procedure that writes fastload segment.
;
; The procedure 'dump-fasl-segment-to-port' takes a segment and an output
; port as arguments and dumps the segment in fastload format on that port.
; The port must be a binary (untranslated) port.
;
; A fastload segment looks like a Scheme expression, and in fact,
; fastload files can mix compiled and uncompiled expressions. A compiled
; expression (as created by dump-fasl-segment-to-port) is a list with
; a literal procedure in the operator position and no arguments.
;
; A literal procedure is a three-element list prefixed by #^P. The three
; elements are code (a bytevector), constants (a regular vector), and
; R0/static link slot (always #f).
;
; A bytevector is a string prefixed by #^B. The string may contain
; control characters; \ and " must be quoted as usual.
;
; A global variable reference in the constant vector is a symbol prefixed
; by #^G. On reading, the reference is replaced by (a pointer to) the
; actual cell.
;
; This code is highly bummed. The procedure write-bytevector-like has the
; same meaning as display, but in Larceny, the former is currently much
; faster than the latter.
(define (dump-fasl-segment-to-port segment outp . rest)
(let* ((omit-code? (not (null? rest)))
(controllify
(lambda (char)
(integer->char (- (char->integer char) (char->integer #\@)))))
(CTRLP (controllify #\P))
(CTRLB (controllify #\B))
(CTRLG (controllify #\G))
(DOUBLEQUOTE (char->integer #\"))
(BACKSLASH (char->integer #\\))
(len 1024))
(define buffer (make-string len #\&))
(define ptr 0)
(define (flush)
(if (< ptr len)
(write-bytevector-like (substring buffer 0 ptr) outp)
(write-bytevector-like buffer outp))
(set! ptr 0))
(define (putc c)
(if (= ptr len) (flush))
(string-set! buffer ptr c)
(set! ptr (+ ptr 1)))
(define (putb b)
(if (= ptr len) (flush))
(string-set! buffer ptr (integer->char b))
(set! ptr (+ ptr 1)))
(define (puts s)
(let ((ls (string-length s)))
(if (>= (+ ptr ls) len)
(begin (flush)
(write-bytevector-like s outp))
(do ((i (- ls 1) (- i 1))
(p (+ ptr ls -1) (- p 1)))
((< i 0)
(set! ptr (+ ptr ls)))
(string-set! buffer p (string-ref s i))))))
(define (putd d)
(flush)
(write-fasl-datum d outp))
(define (dump-codevec bv)
(if omit-code?
(puts "#f")
(begin
(putc #\#)
(putc CTRLB)
(putc #\")
(let ((limit (bytevector-length bv)))
(do ((i 0 (+ i 1)))
((= i limit) (putc #\")
(putc #\newline))
(let ((c (bytevector-ref bv i)))
(cond ((= c DOUBLEQUOTE) (putc #\\))
((= c BACKSLASH) (putc #\\)))
(putb c)))))))
(define (dump-constvec cv)
(puts "#(")
(for-each (lambda (const)
(putc #\space)
(case (car const)
((data)
(putd (cadr const)))
((constantvector)
(dump-constvec (cadr const)))
((codevector)
(dump-codevec (cadr const)))
((global)
(putc #\#)
(putc CTRLG)
(putd (cadr const)))
((bits)
(error "BITS attribute is not supported in fasl files."))
(else
(error "Faulty .lop file."))))
(vector->list cv))
(puts ")")
(putc #\newline))
(define (dump-fasl-segment segment)
(if (not omit-code?) (putc #\())
(putc #\#)
(putc CTRLP)
(putc #\()
(dump-codevec (car segment))
(putc #\space)
(dump-constvec (cdr segment))
(puts " #f)")
(if (not omit-code?) (putc #\)))
(putc #\newline))
(dump-fasl-segment segment)
(flush)))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Bootstrap heap dumper.
;
; Usage: (build-heap-image outputfile inputfile-list)
;
; Each input file is a sequence of segments, the structure of which
; depends on the target architecture, but at least segment.code and
; segment.constants exist as accessors.
;
; The code is a bytevector. The constant vector contains tagged
; entries (represented using length-2 lists), where the tags are
; `data', `codevector', `constantvector', `global', or `bits'.
;
; `build-heap-image' reads its file arguments into the heap, creates
; thunks from the segments, and creates a list of the thunks. It also
; creates a list of all symbols present in the loaded files. Finally,
; it generates an initialization procedure (the LAP of which is hardcoded
; into this file; see below). A pointer to this procedure is installed
; in the SCHEME_ENTRY root pointer; hence, this procedure (a thunk, as
; it were) is called when the heap image is loaded.
;
; The initialization procedure calls each procedure in the thunk list in
; order. It then invokes the procedure `go', which takes one argument:
; the list of symbols. Typically, `go' will initialize the symbol table
; and other system tables and then call `main', but this is by no means
; required.
;
; The Scheme assembler must be co-resident, since it is used by
; `build-heap-image' procedure to assemble the final startup code. This
; could be avoided by pre-assembling the code and patching it here, but
; the way it is now, this procedure is entirely portable -- no target
; dependencies.
;
; The code is structured to allow most procedures to be overridden for
; target architectures with more complex needs (notably the C backend).
(define generate-global-symbols
(make-twobit-flag 'generate-global-symbols))
(generate-global-symbols #t)
(define heap.version-number 9) ; Heap version number
(define heap.root-names ; Roots in heap version 9
'(result argreg2 argreg3
reg0 reg1 reg2 reg3 reg3 reg5 reg6 reg7 reg8 reg9 reg10 reg11 reg12
reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg20 reg21 reg22 reg23
reg24 reg25 reg26 reg27 reg28 reg29 reg30 reg31
cont startup callouts schcall-arg4 alloci-tmp))
(define (build-heap-image output-file input-files)
(define tmp-file "HEAPDATA.dat")
(define (process-input-files heap)
(let loop ((files input-files) (inits '()))
(cond ((null? files)
(heap.thunks! heap (apply append inits)))
(else
(let ((filename (car files)))
(display "Loading ")
(display filename)
(newline)
(loop (cdr files)
(append inits (list (dump-file! heap filename)))))))))
(delete-file tmp-file)
(let ((heap (make-heap #f (open-output-file tmp-file))))
(before-all-files heap output-file input-files)
(process-input-files heap)
(heap.set-root! heap
'startup
(dump-startup-procedure! heap))
(heap.set-root! heap
'callouts
(dump-global! heap 'millicode-support))
(write-header heap output-file)
(after-all-files heap output-file input-files)
(close-output-port (heap.output-port heap))
(append-file-shell-command tmp-file output-file)
(load-map heap)
(unspecified)))
(define (before-all-files heap output-file-name input-file-names) #t)
(define (after-all-files heap output-file-name input-file-names) #t)
; Public
;
; A 'heap' is a data structure with the following public fields; none
; of them are constant unless so annotated:
;
; version a fixnum (constant) - heap type version number
; roots an assoc list that maps root names to values
; top an exact nonnegative integer: the address of the
; next byte to be emitted
; symbol-table a symbol table abstract data type
; extra any value - a client-extension field
; output-port an output port (for the data stream)
; thunks a list of codevector addresses
;
; Bytes are emitted with the heap.byte! and heap.word! procedures,
; which emit a byte and a 4-byte word respectively. These update
; the top field.
(define (make-heap extra output-port)
(vector heap.version-number ; version
'() ; roots
0 ; top
(make-heap-symbol-table) ; symtab
extra ; extra
output-port ; output port
'() ; thunks
))
(define (heap.version h) (vector-ref h 0))
(define (heap.roots h) (vector-ref h 1))
(define (heap.top h) (vector-ref h 2))
(define (heap.symbol-table h) (vector-ref h 3))
(define (heap.extra h) (vector-ref h 4))
(define (heap.output-port h) (vector-ref h 5))
(define (heap.thunks h) (vector-ref h 6))
(define (heap.roots! h x) (vector-set! h 1 x))
(define (heap.top! h x) (vector-set! h 2 x))
(define (heap.thunks! h x) (vector-set! h 6 x))
; Symbol table.
;
; The symbol table maps names to symbol structures, and a symbol
; structure contains information about that symbol.
;
; The structure has four fields:
; name a symbol - the print name
; symloc a fixnum or null - if fixnum, the location in the
; heap of the symbol structure.
; valloc a fixnum or null - if fixnum, the location in the
; heap of the global variable cell that has this
; symbol for its name.
; valno a fixnum or null - if fixnum, the serial number of
; the global variable cell (largely obsolete).
;
; Note therefore that the symbol table maintains information about
; whether the symbol is used as a symbol (in a datum), as a global
; variable, or both.
(define (make-heap-symbol-table)
(vector '() 0))
(define (symtab.symbols st) (vector-ref st 0))
(define (symtab.cell-no st) (vector-ref st 1))
(define (symtab.symbols! st x) (vector-set! st 0 x))
(define (symtab.cell-no! st x) (vector-set! st 1 x))
(define (make-symcell name)
(vector name '() '() '()))
(define (symcell.name sc) (vector-ref sc 0)) ; name
(define (symcell.symloc sc) (vector-ref sc 1)) ; symbol location (if any)
(define (symcell.valloc sc) (vector-ref sc 2)) ; value cell location (ditto)
(define (symcell.valno sc) (vector-ref sc 3)) ; value cell number (ditto)
(define (symcell.symloc! sc x) (vector-set! sc 1 x))
(define (symcell.valloc! sc x) (vector-set! sc 2 x))
(define (symcell.valno! sc x) (vector-set! sc 3 x))
; Find a symcell in the table, or make a new one if there's none.
(define (symbol-cell h name)
(let ((symtab (heap.symbol-table h)))
(let loop ((symbols (symtab.symbols symtab)))
(cond ((null? symbols)
(let ((new-sym (make-symcell name)))
(symtab.symbols! symtab (cons new-sym
(symtab.symbols symtab)))
new-sym))
((eq? name (symcell.name (car symbols)))
(car symbols))
(else
(loop (cdr symbols)))))))
; Fundamental data emitters
(define twofiftysix^3 (* 256 256 256))
(define twofiftysix^2 (* 256 256))
(define twofiftysix 256)
(define (heap.word-be! h w)
(heap.byte! h (quotient w twofiftysix^3))
(heap.byte! h (quotient (remainder w twofiftysix^3) twofiftysix^2))
(heap.byte! h (quotient (remainder w twofiftysix^2) twofiftysix))
(heap.byte! h (remainder w twofiftysix)))
(define (heap.word-el! h w)
(heap.byte! h (remainder w twofiftysix))
(heap.byte! h (quotient (remainder w twofiftysix^2) twofiftysix))
(heap.byte! h (quotient (remainder w twofiftysix^3) twofiftysix^2))
(heap.byte! h (quotient w twofiftysix^3)))
(define heap.word! heap.word-be!)
(define (dumpheap.set-endianness! which)
(case which
((big) (set! heap.word! heap.word-be!))
((little) (set! heap.word! heap.word-el!))
(else ???)))
(define (heap.byte! h b)
(write-char (integer->char b) (heap.output-port h))
(heap.top! h (+ 1 (heap.top h))))
; Useful abstractions and constants.
(define (heap.header-word! h immediate length)
(heap.word! h (+ (* length 256) immediate)))
(define (heap.adjust! h)
(let ((p (heap.top h)))
(let loop ((i (- (* 8 (quotient (+ p 7) 8)) p)))
(if (zero? i)
'()
(begin (heap.byte! h 0)
(loop (- i 1)))))))
(define heap.largest-fixnum (- (expt 2 29) 1))
(define heap.smallest-fixnum (- (expt 2 29)))
(define (heap.set-root! h name value)
(heap.roots! h (cons (cons name value) (heap.roots h))))
;;; The segment.* procedures may be overridden by custom code.
(define segment.code car)
(define segment.constants cdr)
;;; The dump-*! procedures may be overridden by custom code.
; Load a LOP file into the heap, create a thunk in the heap to hold the
; code and constant vector, and return the list of thunk addresses in
; the order dumped.
(define (dump-file! h filename)
(before-dump-file h filename)
(call-with-input-file filename
(lambda (in)
(do ((segment (read in) (read in))
(thunks '() (cons (dump-segment! h segment) thunks)))
((eof-object? segment)
(after-dump-file h filename)
(reverse thunks))))))
(define (before-dump-file h filename) #t)
(define (after-dump-file h filename) #t)
; Dump a segment and return the heap address of the resulting thunk.
(define (dump-segment! h segment)
(let* ((the-code (dump-codevector! h (segment.code segment)))
(the-consts (dump-constantvector! h (segment.constants segment))))
(dump-thunk! h the-code the-consts)))
(define (dump-tagged-item! h item)
(case (car item)
((codevector)
(dump-codevector! h (cadr item)))
((constantvector)
(dump-constantvector! h (cadr item)))
((data)
(dump-datum! h (cadr item)))
((global)
(dump-global! h (cadr item)))
((bits)
(cadr item))
(else
(error 'dump-tagged-item! "Unknown item ~a" item))))
(define (dump-datum! h datum)
(define (fixnum? x)
(and (integer? x)
(exact? x)
(<= heap.smallest-fixnum x heap.largest-fixnum)))
(define (bignum? x)
(and (integer? x)
(exact? x)
(or (> x heap.largest-fixnum)
(< x heap.smallest-fixnum))))
(define (ratnum? x)
(and (rational? x) (exact? x) (not (integer? x))))
(define (flonum? x)
(and (real? x) (inexact? x)))
(define (compnum? x)
(and (complex? x) (inexact? x) (not (real? x))))
(define (rectnum? x)
(and (complex? x) (exact? x) (not (real? x))))
(cond ((fixnum? datum)
(dump-fixnum! h datum))
((bignum? datum)
(dump-bignum! h datum))
((ratnum? datum)
(dump-ratnum! h datum))
((flonum? datum)
(dump-flonum! h datum))
((compnum? datum)
(dump-compnum! h datum))
((rectnum? datum)
(dump-rectnum! h datum))
((char? datum)
(dump-char! h datum))
((null? datum)
$imm.null)
((eq? datum #t)
$imm.true)
((eq? datum #f)
$imm.false)
((equal? datum (unspecified))
$imm.unspecified)
((equal? datum (undefined))
$imm.undefined)
((vector? datum)
(dump-vector! h datum $tag.vector-typetag))
((bytevector? datum)
(dump-bytevector! h datum $tag.bytevector-typetag))
((pair? datum)
(dump-pair! h datum))
((string? datum)
(dump-string! h datum))
((symbol? datum)
(dump-symbol! h datum))
(else
(error 'dump-datum! "Unsupported type of datum ~a" datum))))
; Returns the two's complement representation as a positive number.
(define (dump-fixnum! h f)
(if (negative? f)
(- #x100000000 (* (abs f) 4))
(* 4 f)))
(define (dump-char! h c)
(+ (* (char->integer c) twofiftysix^2) $imm.character))
(define (dump-bignum! h b)
(dump-bytevector! h (bignum->bytevector b) $tag.bignum-typetag))
(define (dump-ratnum! h r)
(dump-vector! h
(vector (numerator r) (denominator r))
$tag.ratnum-typetag))
(define (dump-flonum! h f)
(dump-bytevector! h (flonum->bytevector f) $tag.flonum-typetag))
(define (dump-compnum! h c)
(dump-bytevector! h (compnum->bytevector c) $tag.compnum-typetag))
(define (dump-rectnum! h r)
(dump-vector! h
(vector (real-part r) (imag-part r))
$tag.rectnum-typetag))
(define (dump-string! h s)
(dump-bytevector! h (string->bytevector s) $tag.string-typetag))
(define (dump-pair! h p)
(let ((the-car (dump-datum! h (car p)))
(the-cdr (dump-datum! h (cdr p))))
(let ((base (heap.top h)))
(heap.word! h the-car)
(heap.word! h the-cdr)
(+ base $tag.pair-tag))))
(define (dump-bytevector! h bv variation)
(let ((base (heap.top h))
(l (bytevector-length bv)))
(heap.header-word! h (+ $imm.bytevector-header variation) l)
(let loop ((i 0))
(if (< i l)
(begin (heap.byte! h (bytevector-ref bv i))
(loop (+ i 1)))
(begin (heap.adjust! h)
(+ base $tag.bytevector-tag))))))
(define (dump-vector! h v variation)
(dump-vector-like! h v dump-datum! variation))
(define (dump-vector-like! h cv recur! variation)
(let* ((l (vector-length cv))
(v (make-vector l '())))
(let loop ((i 0))
(if (< i l)
(begin (vector-set! v i (recur! h (vector-ref cv i)))
(loop (+ i 1)))
(let ((base (heap.top h)))
(heap.header-word! h (+ $imm.vector-header variation) (* l 4))
(let loop ((i 0))
(if (< i l)
(begin (heap.word! h (vector-ref v i))
(loop (+ i 1)))
(begin (heap.adjust! h)
(+ base $tag.vector-tag)))))))))
(define (dump-codevector! h cv)
(dump-bytevector! h cv $tag.bytevector-typetag))
(define (dump-constantvector! h cv)
(dump-vector-like! h cv dump-tagged-item! $tag.vector-typetag))
(define (dump-symbol! h s)
(let ((x (symbol-cell h s)))
(if (null? (symcell.symloc x))
(symcell.symloc! x (create-symbol! h s)))
(symcell.symloc x)))
(define (dump-global! h g)
(let ((x (symbol-cell h g)))
(if (null? (symcell.valloc x))
(let ((cell (create-cell! h g)))
(symcell.valloc! x (car cell))
(symcell.valno! x (cdr cell))))
(symcell.valloc x)))
(define (dump-thunk! h code constants)
(let ((base (heap.top h)))
(heap.header-word! h $imm.procedure-header 8)
(heap.word! h code)
(heap.word! h constants)
(heap.adjust! h)
(+ base $tag.procedure-tag)))
; The car's are all heap pointers, so they should not be messed with.
; The cdr must be dumped, and then the pair.
(define (dump-list-spine! h l)
(if (null? l)
$imm.null
(let ((the-car (car l))
(the-cdr (dump-list-spine! h (cdr l))))
(let ((base (heap.top h)))
(heap.word! h the-car)
(heap.word! h the-cdr)
(+ base $tag.pair-tag)))))
(define (dump-startup-procedure! h)
(let ((thunks (dump-list-spine! h (heap.thunks h)))
(symbols (dump-list-spine! h (symbol-locations h))))
(dump-segment! h (construct-startup-procedure symbols thunks))))
; The initialization procedure. The lists are magically patched into
; the constant vector after the procedure has been assembled but before
; it is dumped into the heap. See below.
;
; (define (init-proc argv)
; (let loop ((l <list-of-thunks>))
; (if (null? l)
; (go <list-of-symbols> argv)
; (begin ((car l))
; (loop (cdr l))))))
(define init-proc
`((,$.proc)
(,$args= 1)
(,$reg 1) ; argv into
(,$setreg 2) ; register 2
(,$const (thunks)) ; dummy list of thunks.
(,$setreg 1)
(,$.label 0)
(,$reg 1)
(,$op1 null?) ; (null? l)
(,$branchf 2)
(,$const (symbols)) ; dummy list of symbols
(,$setreg 1)
(,$global go)
;(,$op1 break)
(,$invoke 2) ; (go <list of symbols> argv)
(,$.label 2)
(,$save 2)
(,$store 0 0)
(,$store 1 1)
(,$store 2 2)
(,$setrtn 3)
(,$reg 1)
(,$op1 car)
(,$invoke 0) ; ((car l))
(,$.label 3)
(,$.cont)
(,$restore 2)
(,$pop 2)
(,$reg 1)
(,$op1 cdr)
(,$setreg 1)
(,$branch 0))) ; (loop (cdr l))
;;; Non-overridable code beyond this point
; Stuff a new symbol into the heap, return its location.
(define (create-symbol! h s)
(dump-vector-like!
h
(vector `(bits ,(dump-string! h (symbol->string s)))
'(data 0)
'(data ()))
dump-tagged-item!
$tag.symbol-typetag))
; Stuff a value cell into the heap, return a pair of its location
; and its cell number.
(define (create-cell! h s)
(let* ((symtab (heap.symbol-table h))
(n (symtab.cell-no symtab))
(p (dump-pair! h (cons (undefined)
(if (generate-global-symbols)
s
n)))))
(symtab.cell-no! symtab (+ n 1))
(cons p n)))
(define (construct-startup-procedure symbol-list-addr init-list-addr)
; Given some value which might appear in the constant vector,
; replace the entries matching that value with a new value.
(define (patch-constant-vector! v old new)
(let loop ((i (- (vector-length v) 1)))
(if (>= i 0)
(begin (if (equal? (vector-ref v i) old)
(vector-set! v i new))
(loop (- i 1))))))
; Assemble the startup thunk, patch it, and return it.
(display "Assembling final procedure") (newline)
(let ((e (single-stepping)))
(single-stepping #f)
(let ((segment (assemble init-proc)))
(single-stepping e)
(patch-constant-vector! (segment.constants segment)
'(data (thunks))
`(bits ,init-list-addr))
(patch-constant-vector! (segment.constants segment)
'(data (symbols))
`(bits ,symbol-list-addr))
segment)))
; Return a list of symbol locations for symbols in the heap, in order.
(define (symbol-locations h)
(let loop ((symbols (symtab.symbols (heap.symbol-table h))) (res '()))
(cond ((null? symbols)
(reverse res))
((not (null? (symcell.symloc (car symbols))))
(loop (cdr symbols)
(cons (symcell.symloc (car symbols)) res)))
(else
(loop (cdr symbols) res)))))
; Return list of variable name to cell number mappings for global vars.
(define (load-map h)
(let loop ((symbols (symtab.symbols (heap.symbol-table h))) (res '()))
(cond ((null? symbols)
(reverse res))
((not (null? (symcell.valloc (car symbols))))
(loop (cdr symbols)
(cons (cons (symcell.name (car symbols))
(symcell.valno (car symbols)))
res)))
(else
(loop (cdr symbols) res)))))
(define (write-header h output-file)
(delete-file output-file)
(call-with-output-file output-file
(lambda (out)
(define (write-word w)
(display (integer->char (quotient w twofiftysix^3)) out)
(display (integer->char (quotient (remainder w twofiftysix^3)
twofiftysix^2))
out)
(display (integer->char (quotient (remainder w twofiftysix^2)
twofiftysix))
out)
(display (integer->char (remainder w twofiftysix)) out))
(define (write-roots)
(let ((assigned-roots (heap.roots h)))
(for-each (lambda (root-name)
(let ((probe (assq root-name assigned-roots)))
(if probe
(write-word (cdr probe))
(write-word $imm.false))))
heap.root-names)))
(write-word heap.version-number)
(write-roots)
(write-word (quotient (heap.top h) 4)))))
; This is a gross hack that happens to work very well.
(define (append-file-shell-command file-to-append file-to-append-to)
(define (message)
(display "You must execute the command") (newline)
(display " cat ") (display file-to-append)
(display " >> ") (display file-to-append-to) (newline)
(display "to create the final heap image.") (newline))
(case host-system
((chez larceny)
(display "Creating final image in \"")
(display file-to-append-to) (display "\"...") (newline)
(if (zero? (system (string-append "cat " file-to-append " >> "
file-to-append-to)))
(delete-file file-to-append)
(begin (display "Failed to create image!")
(newline))))
(else
(message))))
; eof
; Copyright 1991 Lightship Software, Incorporated.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 11 June 1999 / wdc
;
; Asm/Sparc/pass5p2.sch -- Sparc machine assembler, top level
; Overrides the procedure of the same name in Asm/Common/pass5p1.sch.
(define (assembly-table) $sparc-assembly-table$)
; Controls listing of instructions during assembly.
(define listify? #f)
; Table of assembler procedures.
(define $sparc-assembly-table$
(make-vector
*number-of-mnemonics*
(lambda (instruction as)
(asm-error "Unrecognized mnemonic " instruction))))
(define (define-instruction i proc)
(vector-set! $sparc-assembly-table$ i proc)
#t)
(define (list-instruction name instruction)
(if listify?
(begin (display list-indentation)
(display " ")
(display name)
(display (make-string (max (- 12 (string-length name)) 1)
#\space))
(if (not (null? (cdr instruction)))
(begin (write (cadr instruction))
(do ((operands (cddr instruction)
(cdr operands)))
((null? operands))
(write-char #\,)
(write (car operands)))))
(newline)
(flush-output-port))))
(define (list-label instruction)
(if listify?
(begin (display list-indentation)
(write-char #\L)
(write (cadr instruction))
(newline))))
(define (list-lambda-start instruction)
(list-instruction "lambda" (list $lambda '* (operand2 instruction)))
(set! list-indentation (string-append list-indentation "| ")))
(define (list-lambda-end)
(set! list-indentation
(substring list-indentation
0
(- (string-length list-indentation) 4))))
(define list-indentation "")
; Utilities
; Pseudo-instructions.
(define-instruction $.label
(lambda (instruction as)
(list-label instruction)
(sparc.label as (make-asm-label as (operand1 instruction)))))
(define-instruction $.proc
(lambda (instruction as)
(list-instruction ".proc" instruction)
#t))
(define-instruction $.proc-doc
(lambda (instruction as)
(list-instruction ".proc-doc" instruction)
(add-documentation as (operand1 instruction))
#t))
(define-instruction $.cont
(lambda (instruction as)
(list-instruction ".cont" instruction)
#t))
(define-instruction $.align
(lambda (instruction as)
(list-instruction ".align" instruction)
#t))
(define-instruction $.end
(lambda (instruction as)
#t))
(define-instruction $.singlestep
(lambda (instruction as)
(let ((instr (car (as-source as))))
(define (special?)
(let ((op (operand0 instr)))
(or (= op $.label)
(= op $.proc)
(= op $.cont)
(= op $.align)
(and (= op $load) (= 0 (operand1 instr))))))
(define (readify-instr)
(if (= (operand0 instr) $lambda)
(list 'lambda '(...) (caddr instr) (cadddr instr))
(car (readify-lap (list instr)))))
(if (not (special?))
(let ((repr (format-object (readify-instr)))
(funky? (= (operand0 instr) $restore)))
(let ((o (emit-datum as repr)))
(emit-singlestep-instr! as funky? 0 o)))))))
; Instructions.
(define-instruction $op1
(lambda (instruction as)
(list-instruction "op1" instruction)
(emit-primop.1arg! as (operand1 instruction))))
(define-instruction $op2
(lambda (instruction as)
(list-instruction "op2" instruction)
(emit-primop.2arg! as
(operand1 instruction)
(regname (operand2 instruction)))))
(define-instruction $op3
(lambda (instruction as)
(list-instruction "op3" instruction)
(emit-primop.3arg! as
(operand1 instruction)
(regname (operand2 instruction))
(regname (operand3 instruction)))))
(define-instruction $op2imm
(lambda (instruction as)
(list-instruction "op2imm" instruction)
(let ((op (case (operand1 instruction)
((+) 'internal:+/imm)
((-) 'internal:-/imm)
((fx+) 'internal:fx+/imm)
((fx-) 'internal:fx-/imm)
((fx=) 'internal:fx=/imm)
((fx<) 'internal:fx</imm)
((fx<=) 'internal:fx<=/imm)
((fx>) 'internal:fx>/imm)
((fx>=) 'internal:fx>=/imm)
((=:fix:fix) 'internal:=:fix:fix/imm)
((<:fix:fix) 'internal:<:fix:fix/imm)
((<=:fix:fix) 'internal:<=:fix:fix/imm)
((>:fix:fix) 'internal:>:fix:fix/imm)
((>=:fix:fix) 'internal:>=:fix:fix/imm)
(else #f))))
(if op
(emit-primop.4arg! as op $r.result (operand2 instruction) $r.result)
(begin
(emit-constant->register as (operand2 instruction) $r.argreg2)
(emit-primop.2arg! as
(operand1 instruction)
$r.argreg2))))))
(define-instruction $const
(lambda (instruction as)
(list-instruction "const" instruction)
(emit-constant->register as (operand1 instruction) $r.result)))
(define-instruction $global
(lambda (instruction as)
(list-instruction "global" instruction)
(emit-global->register! as
(emit-global as (operand1 instruction))
$r.result)))
(define-instruction $setglbl
(lambda (instruction as)
(list-instruction "setglbl" instruction)
(emit-register->global! as
$r.result
(emit-global as (operand1 instruction)))))
; FIXME: A problem is that the listing is messed up because of the delayed
; assembly; somehow we should fix this by putting an identifying label
; in the listing and emitting this label later, with the code.
(define-instruction $lambda
(lambda (instruction as)
(let ((code-offset #f)
(const-offset #f))
(list-lambda-start instruction)
(assemble-nested-lambda as
(operand1 instruction)
(operand3 instruction) ; documentation
(lambda (nested-as segment)
(set-constant! as code-offset (car segment))
(set-constant! as const-offset (cdr segment))))
(list-lambda-end)
(set! code-offset (emit-codevector as 0))
(set! const-offset (emit-constantvector as 0))
(emit-lambda! as
code-offset
const-offset
(operand2 instruction)))))
(define-instruction $lexes
(lambda (instruction as)
(list-instruction "lexes" instruction)
(emit-lexes! as (operand1 instruction))))
(define-instruction $args=
(lambda (instruction as)
(list-instruction "args=" instruction)
(emit-args=! as (operand1 instruction))))
(define-instruction $args>=
(lambda (instruction as)
(list-instruction "args>=" instruction)
(emit-args>=! as (operand1 instruction))))
(define-instruction $invoke
(lambda (instruction as)
(list-instruction "invoke" instruction)
(emit-invoke as (operand1 instruction) #f $m.invoke-ex)))
(define-instruction $restore
(lambda (instruction as)
(if (not (negative? (operand1 instruction)))
(begin
(list-instruction "restore" instruction)
(emit-restore! as (operand1 instruction))))))
(define-instruction $pop
(lambda (instruction as)
(if (not (negative? (operand1 instruction)))
(begin
(list-instruction "pop" instruction)
(let ((next (next-instruction as)))
(if (and (peephole-optimization)
(eqv? $return (operand0 next)))
(begin (list-instruction "return" next)
(consume-next-instruction! as)
(emit-pop! as (operand1 instruction) #t))
(emit-pop! as (operand1 instruction) #f)))))))
(define-instruction $stack
(lambda (instruction as)
(list-instruction "stack" instruction)
(emit-load! as (operand1 instruction) $r.result)))
(define-instruction $setstk
(lambda (instruction as)
(list-instruction "setstk" instruction)
(emit-store! as $r.result (operand1 instruction))))
(define-instruction $load
(lambda (instruction as)
(list-instruction "load" instruction)
(emit-load! as (operand2 instruction) (regname (operand1 instruction)))))
(define-instruction $store
(lambda (instruction as)
(list-instruction "store" instruction)
(emit-store! as (regname (operand1 instruction)) (operand2 instruction))))
(define-instruction $lexical
(lambda (instruction as)
(list-instruction "lexical" instruction)
(emit-lexical! as (operand1 instruction) (operand2 instruction))))
(define-instruction $setlex
(lambda (instruction as)
(list-instruction "setlex" instruction)
(emit-setlex! as (operand1 instruction) (operand2 instruction))))
(define-instruction $reg
(lambda (instruction as)
(list-instruction "reg" instruction)
(emit-register->register! as (regname (operand1 instruction)) $r.result)))
(define-instruction $setreg
(lambda (instruction as)
(list-instruction "setreg" instruction)
(emit-register->register! as $r.result (regname (operand1 instruction)))))
(define-instruction $movereg
(lambda (instruction as)
(list-instruction "movereg" instruction)
(emit-register->register! as
(regname (operand1 instruction))
(regname (operand2 instruction)))))
(define-instruction $return
(lambda (instruction as)
(list-instruction "return" instruction)
(emit-return! as)))
(define-instruction $reg/return
(lambda (instruction as)
(list-instruction "reg/return" instruction)
(emit-return-reg! as (regname (operand1 instruction)))))
(define-instruction $const/return
(lambda (instruction as)
(list-instruction "const/return" instruction)
(emit-return-const! as (operand1 instruction))))
(define-instruction $nop
(lambda (instruction as)
(list-instruction "nop" instruction)))
(define-instruction $save
(lambda (instruction as)
(if (not (negative? (operand1 instruction)))
(begin
(list-instruction "save" instruction)
(let* ((n (operand1 instruction))
(v (make-vector (+ n 1) #t)))
(emit-save0! as n)
(if (peephole-optimization)
(let loop ((instruction (next-instruction as)))
(if (eqv? $store (operand0 instruction))
(begin (list-instruction "store" instruction)
(emit-store! as
(regname (operand1 instruction))
(operand2 instruction))
(consume-next-instruction! as)
(vector-set! v (operand2 instruction) #f)
(loop (next-instruction as))))))
(emit-save1! as v))))))
(define-instruction $setrtn
(lambda (instruction as)
(list-instruction "setrtn" instruction)
(emit-setrtn! as (make-asm-label as (operand1 instruction)))))
(define-instruction $apply
(lambda (instruction as)
(list-instruction "apply" instruction)
(emit-apply! as
(regname (operand1 instruction))
(regname (operand2 instruction)))))
(define-instruction $jump
(lambda (instruction as)
(list-instruction "jump" instruction)
(emit-jump! as
(operand1 instruction)
(make-asm-label as (operand2 instruction)))))
(define-instruction $skip
(lambda (instruction as)
(list-instruction "skip" instruction)
(emit-branch! as #f (make-asm-label as (operand1 instruction)))))
(define-instruction $branch
(lambda (instruction as)
(list-instruction "branch" instruction)
(emit-branch! as #t (make-asm-label as (operand1 instruction)))))
(define-instruction $branchf
(lambda (instruction as)
(list-instruction "branchf" instruction)
(emit-branchf! as (make-asm-label as (operand1 instruction)))))
(define-instruction $check
(lambda (instruction as)
(list-instruction "check" instruction)
(if (not (unsafe-code))
(emit-check! as $r.result
(make-asm-label as (operand4 instruction))
(list (regname (operand1 instruction))
(regname (operand2 instruction))
(regname (operand3 instruction)))))))
(define-instruction $trap
(lambda (instruction as)
(list-instruction "trap" instruction)
(emit-trap! as
(regname (operand1 instruction))
(regname (operand2 instruction))
(regname (operand3 instruction))
(operand4 instruction))))
(define-instruction $const/setreg
(lambda (instruction as)
(list-instruction "const/setreg" instruction)
(let ((x (operand1 instruction))
(r (operand2 instruction)))
(if (hwreg? r)
(emit-constant->register as x (regname r))
(begin (emit-constant->register as x $r.tmp0)
(emit-register->register! as $r.tmp0 (regname r)))))))
; Operations introduced by the peephole optimizer.
(define (peep-regname r)
(if (eq? r 'RESULT) $r.result (regname r)))
(define-instruction $reg/op1/branchf
(lambda (instruction as)
(list-instruction "reg/op1/branchf" instruction)
(emit-primop.3arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(make-asm-label as (operand3 instruction)))))
(define-instruction $reg/op2/branchf
(lambda (instruction as)
(list-instruction "reg/op2/branchf" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(peep-regname (operand3 instruction))
(make-asm-label as (operand4 instruction)))))
(define-instruction $reg/op2imm/branchf
(lambda (instruction as)
(list-instruction "reg/op2imm/branchf" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(operand3 instruction)
(make-asm-label as (operand4 instruction)))))
; These three are like the corresponding branchf sequences except that
; there is a strong prediction that the branch will not be taken.
(define-instruction $reg/op1/check
(lambda (instruction as)
(list-instruction "reg/op1/check" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(make-asm-label as (operand3 instruction))
(map peep-regname (operand4 instruction)))))
(define-instruction $reg/op2/check
(lambda (instruction as)
(list-instruction "reg/op2/check" instruction)
(emit-primop.5arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(peep-regname (operand3 instruction))
(make-asm-label as (operand4 instruction))
(map peep-regname (operand5 instruction)))))
(define-instruction $reg/op2imm/check
(lambda (instruction as)
(list-instruction "reg/op2imm/check" instruction)
(emit-primop.5arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(operand3 instruction)
(make-asm-label as (operand4 instruction))
(map peep-regname (operand5 instruction)))))
;
(define-instruction $reg/op1/setreg
(lambda (instruction as)
(list-instruction "reg/op1/setreg" instruction)
(emit-primop.3arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(peep-regname (operand3 instruction)))))
(define-instruction $reg/op2/setreg
(lambda (instruction as)
(list-instruction "reg/op2/setreg" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(peep-regname (operand3 instruction))
(peep-regname (operand4 instruction)))))
(define-instruction $reg/op2imm/setreg
(lambda (instruction as)
(list-instruction "reg/op2imm/setreg" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(operand3 instruction)
(peep-regname (operand4 instruction)))))
(define-instruction $reg/op3
(lambda (instruction as)
(list-instruction "reg/op3" instruction)
(emit-primop.4arg! as
(operand1 instruction)
(peep-regname (operand2 instruction))
(peep-regname (operand3 instruction))
(peep-regname (operand4 instruction)))))
(define-instruction $reg/branchf
(lambda (instruction as)
(list-instruction "reg/branchf" instruction)
(emit-branchfreg! as
(regname (operand1 instruction))
(make-asm-label as (operand2 instruction)))))
(define-instruction $setrtn/branch
(lambda (instruction as)
(list-instruction "setrtn/branch" instruction)
(emit-branch-with-setrtn! as (make-asm-label as (operand1 instruction)))))
(define-instruction $setrtn/invoke
(lambda (instruction as)
(list-instruction "setrtn/invoke" instruction)
(emit-invoke as (operand1 instruction) #t $m.invoke-ex)))
(define-instruction $global/setreg
(lambda (instruction as)
(list-instruction "global/setreg" instruction)
(emit-global->register! as
(emit-global as (operand1 instruction))
(regname (operand2 instruction)))))
(define-instruction $global/invoke
(lambda (instruction as)
(list-instruction "global/invoke" instruction)
(emit-load-global as
(emit-global as (operand1 instruction))
$r.result
#f)
(emit-invoke as (operand2 instruction) #f $m.global-invoke-ex)))
(define-instruction $reg/setglbl
(lambda (instruction as)
(list-instruction "reg/setglbl" instruction)
(emit-register->global! as
(regname (operand1 instruction))
(emit-global as (operand2 instruction)))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 9 May 1999.
;
; Asm/Sparc/peepopt.sch -- MAL peephole optimizer, for the SPARC assembler.
;
; The procedure `peep' is called on the as structure before every
; instruction is assembled. It may replace the prefix of the instruction
; stream by some other instruction sequence.
;
; Invariant: if the peephole optimizer doesn't change anything, then
;
; (let ((x (as-source as)))
; (peep as)
; (eq? x (as-source as))) => #t
;
; Note this still isn't right -- it should be integrated with pass5p2 --
; but it's a step in the right direction.
(define *peephole-table* (make-vector *number-of-mnemonics* #f))
(define (define-peephole n p)
(vector-set! *peephole-table* n p)
(unspecified))
(define (peep as)
(let ((t0 (as-source as)))
(if (not (null? t0))
(let ((i1 (car t0)))
(let ((p (vector-ref *peephole-table* (car i1))))
(if p
(let* ((t1 (if (null? t0) t0 (cdr t0)))
(i2 (if (null? t1) '(-1 0 0 0) (car t1)))
(t2 (if (null? t1) t1 (cdr t1)))
(i3 (if (null? t2) '(-1 0 0 0) (car t2)))
(t3 (if (null? t2) t2 (cdr t2))))
(p as i1 i2 i3 t1 t2 t3))))))))
(define-peephole $reg
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $return)
(reg-return as i1 i2 t2))
((= (car i2) $setglbl)
(reg-setglbl as i1 i2 t2))
((= (car i2) $op1)
(cond ((= (car i3) $setreg)
(reg-op1-setreg as i1 i2 i3 t2 t3))
((= (car i3) $branchf)
(reg-op1-branchf as i1 i2 i3 t3))
((= (car i3) $check)
(reg-op1-check as i1 i2 i3 t3))
(else
(reg-op1 as i1 i2 t2))))
((= (car i2) $op2)
(cond ((= (car i3) $setreg)
(reg-op2-setreg as i1 i2 i3 t2 t3))
((= (car i3) $branchf)
(reg-op2-branchf as i1 i2 i3 t3))
((= (car i3) $check)
(reg-op2-check as i1 i2 i3 t3))
(else
(reg-op2 as i1 i2 t2))))
((= (car i2) $op2imm)
(cond ((= (car i3) $setreg)
(reg-op2imm-setreg as i1 i2 i3 t2 t3))
((= (car i3) $branchf)
(reg-op2imm-branchf as i1 i2 i3 t3))
((= (car i3) $check)
(reg-op2imm-check as i1 i2 i3 t3))
(else
(reg-op2imm as i1 i2 t2))))
((= (car i2) $op3)
(reg-op3 as i1 i2 t2))
((= (car i2) $setreg)
(reg-setreg as i1 i2 t2))
((= (car i2) $branchf)
(reg-branchf as i1 i2 t2)))))
(define-peephole $op1
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $branchf)
(op1-branchf as i1 i2 t2))
((= (car i2) $setreg)
(op1-setreg as i1 i2 t2))
((= (car i2) $check)
(op1-check as i1 i2 t2)))))
(define-peephole $op2
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $branchf)
(op2-branchf as i1 i2 t2))
((= (car i2) $setreg)
(op2-setreg as i1 i2 t2))
((= (car i2) $check)
(op2-check as i1 i2 t2)))))
(define-peephole $op2imm
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $branchf)
(op2imm-branchf as i1 i2 t2))
((= (car i2) $setreg)
(op2imm-setreg as i1 i2 t2))
((= (car i2) $check)
(op2imm-check as i1 i2 t2)))))
(define-peephole $const
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $setreg)
(const-setreg as i1 i2 t2))
((= (car i2) $op2)
(const-op2 as i1 i2 t2))
((= (car i2) $return)
(const-return as i1 i2 t2)))))
(define-peephole $setrtn
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $branch)
(cond ((= (car i3) $.align)
(if (not (null? t3))
(let ((i4 (car t3))
(t4 (cdr t3)))
(cond ((= (car i4) $.label)
(setrtn-branch as i1 i2 i3 i4 t4))))))))
((= (car i2) $invoke)
(cond ((= (car i3) $.align)
(if (not (null? t3))
(let ((i4 (car t3))
(t4 (cdr t3)))
(cond ((= (car i4) $.label)
(setrtn-invoke as i1 i2 i3 i4 t4)))))))))))
(define-peephole $branch
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $.align)
(cond ((= (car i3) $.label)
(branch-and-label as i1 i2 i3 t3)))))))
(define-peephole $global
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $setreg)
(global-setreg as i1 i2 t2))
((= (car i2) $invoke)
(global-invoke as i1 i2 t2))
((= (car i2) $setrtn)
(cond ((= (car i3) $invoke)
(global-setrtn-invoke as i1 i2 i3 t3)))))))
(define-peephole $reg/op1/check
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $reg)
(cond ((= (car i3) $op1)
(if (not (null? t3))
(let ((i4 (car t3))
(t4 (cdr t3)))
(cond ((= (car i4) $setreg)
(reg/op1/check-reg-op1-setreg
as i1 i2 i3 i4 t4)))))))))))
(define-peephole $reg/op2/check
(lambda (as i1 i2 i3 t1 t2 t3)
(cond ((= (car i2) $reg)
(cond ((= (car i3) $op2imm)
(if (not (null? t3))
(let ((i4 (car t3))
(t4 (cdr t3)))
(cond ((= (car i4) $check)
(reg/op2/check-reg-op2imm-check
as i1 i2 i3 i4 t4)))))))))))
; Worker procedures.
(define (reg-return as i:reg i:return tail)
(let ((rs (operand1 i:reg)))
(if (hwreg? rs)
(as-source! as (cons (list $reg/return rs) tail)))))
(define (reg-op1-setreg as i:reg i:op1 i:setreg tail-1 tail)
(let ((rs (operand1 i:reg))
(rd (operand1 i:setreg))
(op (operand1 i:op1)))
(if (hwreg? rs)
(if (hwreg? rd)
(peep-reg/op1/setreg as op rs rd tail)
(peep-reg/op1/setreg as op rs 'RESULT tail-1)))))
(define (reg-op1 as i:reg i:op1 tail)
(let ((rs (operand1 i:reg))
(op (operand1 i:op1)))
(if (hwreg? rs)
(peep-reg/op1/setreg as op rs 'RESULT tail))))
(define (op1-setreg as i:op1 i:setreg tail)
(let ((op (operand1 i:op1))
(rd (operand1 i:setreg)))
(if (hwreg? rd)
(peep-reg/op1/setreg as op 'RESULT rd tail))))
(define (peep-reg/op1/setreg as op rs rd tail)
(let ((op (case op
((car) 'internal:car)
((cdr) 'internal:cdr)
((car:pair) 'internal:car:pair)
((cdr:pair) 'internal:cdr:pair)
((cell-ref) 'internal:cell-ref)
((vector-length) 'internal:vector-length)
((vector-length:vec) 'internal:vector-length:vec)
((string-length) 'internal:string-length)
((--) 'internal:--)
((fx--) 'internal:fx--)
((fxpositive?) 'internal:fxpositive?)
((fxnegative?) 'internal:fxnegative?)
((fxzero?) 'internal:fxzero?)
(else #f))))
(if op
(as-source! as (cons (list $reg/op1/setreg op rs rd) tail)))))
(define (reg-op2-setreg as i:reg i:op2 i:setreg tail-1 tail)
(let ((rs1 (operand1 i:reg))
(rs2 (operand2 i:op2))
(op (operand1 i:op2))
(rd (operand1 i:setreg)))
(if (hwreg? rs1)
(if (hwreg? rd)
(peep-reg/op2/setreg as op rs1 rs2 rd tail)
(peep-reg/op2/setreg as op rs1 rs2 'RESULT tail-1)))))
(define (reg-op2 as i:reg i:op2 tail)
(let ((rs1 (operand1 i:reg))
(rs2 (operand2 i:op2))
(op (operand1 i:op2)))
(if (hwreg? rs1)
(peep-reg/op2/setreg as op rs1 rs2 'RESULT tail))))
(define (op2-setreg as i:op2 i:setreg tail)
(let ((op (operand1 i:op2))
(rs2 (operand2 i:op2))
(rd (operand1 i:setreg)))
(if (hwreg? rd)
(peep-reg/op2/setreg as op 'RESULT rs2 rd tail))))
(define (peep-reg/op2/setreg as op rs1 rs2 rd tail)
(let ((op (case op
((+) 'internal:+)
((-) 'internal:-)
((fx+) 'internal:fx+)
((fx-) 'internal:fx-)
((fx=) 'internal:fx=)
((fx>) 'internal:fx>)
((fx>=) 'internal:fx>=)
((fx<) 'internal:fx<)
((fx<=) 'internal:fx<=)
((eq?) 'internal:eq?)
((cons) 'internal:cons)
((vector-ref) 'internal:vector-ref)
((vector-ref:trusted) 'internal:vector-ref:trusted)
((string-ref) 'internal:string-ref)
((set-car!) 'internal:set-car!)
((set-cdr!) 'internal:set-cdr!)
((cell-set!) 'internal:cell-set!)
(else #f))))
(if op
(as-source! as (cons (list $reg/op2/setreg op rs1 rs2 rd) tail)))))
(define (reg-op2imm-setreg as i:reg i:op2imm i:setreg tail-1 tail)
(let ((rs (operand1 i:reg))
(imm (operand2 i:op2imm))
(op (operand1 i:op2imm))
(rd (operand1 i:setreg)))
(if (hwreg? rs)
(if (hwreg? rd)
(peep-reg/op2imm/setreg as op rs imm rd tail)
(peep-reg/op2imm/setreg as op rs imm 'RESULT tail-1)))))
(define (reg-op2imm as i:reg i:op2imm tail)
(let ((rs (operand1 i:reg))
(imm (operand2 i:op2imm))
(op (operand1 i:op2imm)))
(if (hwreg? rs)
(peep-reg/op2imm/setreg as op rs imm 'RESULT tail))))
(define (op2imm-setreg as i:op2imm i:setreg tail)
(let ((op (operand1 i:op2imm))
(imm (operand2 i:op2imm))
(rd (operand1 i:setreg)))
(if (hwreg? rd)
(peep-reg/op2imm/setreg as op 'RESULT imm rd tail))))
(define (peep-reg/op2imm/setreg as op rs imm rd tail)
(let ((op (case op
((+) 'internal:+/imm)
((-) 'internal:-/imm)
((fx+) 'internal:fx+/imm)
((fx-) 'internal:fx-/imm)
((fx=) 'internal:fx=/imm)
((fx<) 'internal:fx</imm)
((fx<=) 'internal:fx<=/imm)
((fx>) 'internal:fx>/imm)
((fx>=) 'internal:fx>=/imm)
((eq?) 'internal:eq?/imm)
((vector-ref) 'internal:vector-ref/imm)
((string-ref) 'internal:string-ref/imm)
(else #f))))
(if op
(as-source! as (cons (list $reg/op2imm/setreg op rs imm rd) tail)))))
(define (reg-op1-branchf as i:reg i:op1 i:branchf tail)
(let ((rs (operand1 i:reg))
(op (operand1 i:op1))
(L (operand1 i:branchf)))
(if (hwreg? rs)
(peep-reg/op1/branchf as op rs L tail))))
(define (op1-branchf as i:op1 i:branchf tail)
(let ((op (operand1 i:op1))
(L (operand1 i:branchf)))
(peep-reg/op1/branchf as op 'RESULT L tail)))
(define (peep-reg/op1/branchf as op rs L tail)
(let ((op (case op
((null?) 'internal:branchf-null?)
((pair?) 'internal:branchf-pair?)
((zero?) 'internal:branchf-zero?)
((eof-object?) 'internal:branchf-eof-object?)
((fixnum?) 'internal:branchf-fixnum?)
((char?) 'internal:branchf-char?)
((fxzero?) 'internal:branchf-fxzero?)
((fxnegative?) 'internal:branchf-fxnegative?)
((fxpositive?) 'internal:branchf-fxpositive?)
(else #f))))
(if op
(as-source! as (cons (list $reg/op1/branchf op rs L) tail)))))
(define (reg-op2-branchf as i:reg i:op2 i:branchf tail)
(let ((rs1 (operand1 i:reg))
(rs2 (operand2 i:op2))
(op (operand1 i:op2))
(L (operand1 i:branchf)))
(if (hwreg? rs1)
(peep-reg/op2/branchf as op rs1 rs2 L tail))))
(define (op2-branchf as i:op2 i:branchf tail)
(let ((op (operand1 i:op2))
(rs2 (operand2 i:op2))
(L (operand1 i:branchf)))
(peep-reg/op2/branchf as op 'RESULT rs2 L tail)))
(define (peep-reg/op2/branchf as op rs1 rs2 L tail)
(let ((op (case op
((<) 'internal:branchf-<)
((>) 'internal:branchf->)
((>=) 'internal:branchf->=)
((<=) 'internal:branchf-<=)
((=) 'internal:branchf-=)
((eq?) 'internal:branchf-eq?)
((char=?) 'internal:branchf-char=?)
((char>=?) 'internal:branchf-char>=?)
((char>?) 'internal:branchf-char>?)
((char<=?) 'internal:branchf-char<=?)
((char<?) 'internal:branchf-char<?)
((fx=) 'internal:branchf-fx=)
((fx>) 'internal:branchf-fx>)
((fx>=) 'internal:branchf-fx>=)
((fx<) 'internal:branchf-fx<)
((fx<=) 'internal:branchf-fx<=)
(else #f))))
(if op
(as-source! as
(cons (list $reg/op2/branchf op rs1 rs2 L)
tail)))))
(define (reg-op2imm-branchf as i:reg i:op2imm i:branchf tail)
(let ((rs (operand1 i:reg))
(imm (operand2 i:op2imm))
(op (operand1 i:op2imm))
(L (operand1 i:branchf)))
(if (hwreg? rs)
(peep-reg/op2imm/branchf as op rs imm L tail))))
(define (op2imm-branchf as i:op2imm i:branchf tail)
(let ((op (operand1 i:op2imm))
(imm (operand2 i:op2imm))
(L (operand1 i:branchf)))
(peep-reg/op2imm/branchf as op 'RESULT imm L tail)))
(define (peep-reg/op2imm/branchf as op rs imm L tail)
(let ((op (case op
((<) 'internal:branchf-</imm)
((>) 'internal:branchf->/imm)
((>=) 'internal:branchf->=/imm)
((<=) 'internal:branchf-<=/imm)
((=) 'internal:branchf-=/imm)
((eq?) 'internal:branchf-eq?/imm)
((char=?) 'internal:branchf-char=?/imm)
((char>=?) 'internal:branchf-char>=?/imm)
((char>?) 'internal:branchf-char>?/imm)
((char<=?) 'internal:branchf-char<=?/imm)
((char<?) 'internal:branchf-char<?/imm)
((fx=) 'internal:branchf-fx=/imm)
((fx>) 'internal:branchf-fx>/imm)
((fx>=) 'internal:branchf-fx>=/imm)
((fx<) 'internal:branchf-fx</imm)
((fx<=) 'internal:branchf-fx<=/imm)
(else #f))))
(if op
(as-source! as
(cons (list $reg/op2imm/branchf op rs imm L)
tail)))))
; Check optimization.
(define (reg-op1-check as i:reg i:op1 i:check tail)
(let ((rs (operand1 i:reg))
(op (operand1 i:op1)))
(if (hwreg? rs)
(peep-reg/op1/check as
op
rs
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail))))
(define (op1-check as i:op1 i:check tail)
(let ((op (operand1 i:op1)))
(peep-reg/op1/check as
op
'RESULT
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail)))
(define (peep-reg/op1/check as op rs L1 liveregs tail)
(let ((op (case op
((fixnum?) 'internal:check-fixnum?)
((pair?) 'internal:check-pair?)
((vector?) 'internal:check-vector?)
(else #f))))
(if op
(as-source! as
(cons (list $reg/op1/check op rs L1 liveregs)
tail)))))
(define (reg-op2-check as i:reg i:op2 i:check tail)
(let ((rs1 (operand1 i:reg))
(rs2 (operand2 i:op2))
(op (operand1 i:op2)))
(if (hwreg? rs1)
(peep-reg/op2/check as
op
rs1
rs2
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail))))
(define (op2-check as i:op2 i:check tail)
(let ((rs2 (operand2 i:op2))
(op (operand1 i:op2)))
(peep-reg/op2/check as
op
'RESULT
rs2
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail)))
(define (peep-reg/op2/check as op rs1 rs2 L1 liveregs tail)
(let ((op (case op
((<:fix:fix) 'internal:check-<:fix:fix)
((<=:fix:fix) 'internal:check-<=:fix:fix)
((>=:fix:fix) 'internal:check->=:fix:fix)
(else #f))))
(if op
(as-source! as
(cons (list $reg/op2/check op rs1 rs2 L1 liveregs)
tail)))))
(define (reg-op2imm-check as i:reg i:op2imm i:check tail)
(let ((rs1 (operand1 i:reg))
(op (operand1 i:op2imm))
(imm (operand2 i:op2imm)))
(if (hwreg? rs1)
(peep-reg/op2imm/check as
op
rs1
imm
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail))))
(define (op2imm-check as i:op2imm i:check tail)
(let ((op (operand1 i:op2imm))
(imm (operand2 i:op2imm)))
(peep-reg/op2imm/check as
op
'RESULT
imm
(operand4 i:check)
(list (operand1 i:check)
(operand2 i:check)
(operand3 i:check))
tail)))
(define (peep-reg/op2imm/check as op rs1 imm L1 liveregs tail)
(let ((op (case op
((<:fix:fix) 'internal:check-<:fix:fix/imm)
((<=:fix:fix) 'internal:check-<=:fix:fix/imm)
((>=:fix:fix) 'internal:check->=:fix:fix/imm)
(else #f))))
(if op
(as-source! as
(cons (list $reg/op2imm/check op rs1 imm L1 liveregs)
tail)))))
(define (reg/op1/check-reg-op1-setreg as i:ro1check i:reg i:op1 i:setreg tail)
(let ((o1 (operand1 i:ro1check))
(r1 (operand2 i:ro1check))
(r2 (operand1 i:reg))
(o2 (operand1 i:op1))
(r3 (operand1 i:setreg)))
(if (and (eq? o1 'internal:check-vector?)
(eq? r1 r2)
(eq? o2 'vector-length:vec)
(hwreg? r1)
(hwreg? r3))
(as-source! as
(cons (list $reg/op2/check
'internal:check-vector?/vector-length:vec
r1
r3
(operand3 i:ro1check)
(operand4 i:ro1check))
tail)))))
; Range checks of the form 0 <= i < n can be performed by a single check.
; This peephole optimization recognizes
; reg rs1
; op2 <:fix:fix,rs2
; check r1,r2,r3,L
; reg rs1 ; must match earlier reg
; op2imm >=:fix:fix,0
; check r1,r2,r3,L ; label must match earlier check
(define (reg/op2/check-reg-op2imm-check
as i:ro2check i:reg i:op2imm i:check tail)
(let ((o1 (operand1 i:ro2check))
(rs1 (operand2 i:ro2check))
(rs2 (operand3 i:ro2check))
(L1 (operand4 i:ro2check))
(live (operand5 i:ro2check))
(rs3 (operand1 i:reg))
(o2 (operand1 i:op2imm))
(x (operand2 i:op2imm))
(L2 (operand4 i:check)))
(if (and (eq? o1 'internal:check-<:fix:fix)
(eq? o2 '>=:fix:fix)
(eq? rs1 rs3)
(eq? x 0)
(eq? L1 L2))
(as-source! as
(cons (list $reg/op2/check 'internal:check-range
rs1 rs2 L1 live)
tail)))))
; End of check optimization.
(define (reg-op3 as i:reg i:op3 tail)
(let ((rs1 (operand1 i:reg))
(rs2 (operand2 i:op3))
(rs3 (operand3 i:op3))
(op (operand1 i:op3)))
(if (hwreg? rs1)
(let ((op (case op
((vector-set!) 'internal:vector-set!)
((string-set!) 'internal:string-set!)
(else #f))))
(if op
(as-source! as (cons (list $reg/op3 op rs1 rs2 rs3) tail)))))))
; Reg-setreg is not restricted to hardware registers, as $movereg is
; a standard instruction.
(define (reg-setreg as i:reg i:setreg tail)
(let ((rs (operand1 i:reg))
(rd (operand1 i:setreg)))
(if (= rs rd)
(as-source! as tail)
(as-source! as (cons (list $movereg rs rd) tail)))))
(define (reg-branchf as i:reg i:branchf tail)
(let ((rs (operand1 i:reg))
(L (operand1 i:branchf)))
(if (hwreg? rs)
(as-source! as (cons (list $reg/branchf rs L) tail)))))
(define (const-setreg as i:const i:setreg tail)
(let ((c (operand1 i:const))
(rd (operand1 i:setreg)))
(if (hwreg? rd)
(as-source! as (cons (list $const/setreg c rd) tail)))))
; Make-vector on vectors of known short length.
(define (const-op2 as i:const i:op2 tail)
(let ((vn '#(make-vector:0 make-vector:1 make-vector:2 make-vector:3
make-vector:4 make-vector:5 make-vector:6 make-vector:7
make-vector:8 make-vector:9))
(c (operand1 i:const))
(op (operand1 i:op2))
(r (operand2 i:op2)))
(if (and (eq? op 'make-vector)
(fixnum? c)
(<= 0 c 9))
(as-source! as (cons (list $op2 (vector-ref vn c) r) tail)))))
; Constants that can be synthesized in a single instruction can be
; moved into RESULT in the delay slot of the return instruction.
(define (const-return as i:const i:return tail)
(let ((c (operand1 i:const)))
(if (or (and (number? c) (immediate-int? c))
(null? c)
(boolean? c))
(as-source! as (cons (list $const/return c) tail)))))
; This allows the use of hardware 'call' instructions.
; (setrtn Lx)
; (branch Ly k)
; (.align k) Ignored on SPARC
; (.label Lx)
; => (setrtn/branch Ly k)
; (.label Lx)
(define (setrtn-branch as i:setrtn i:branch i:align i:label tail)
(let ((return-label (operand1 i:setrtn))
(branch-ops (cdr i:branch))
(label (operand1 i:label)))
(if (= return-label label)
(as-source! as (cons (cons $setrtn/branch branch-ops)
(cons i:label
tail))))))
; Ditto for 'invoke'.
;
; Disabled because it does _not_ pay off on the SPARC currently --
; probably, the dependency created between 'jmpl' and 'st' is not
; handled well on the test machine (an Ultrasparc). Might work
; better if the return address were to be kept in a register always.
(define (setrtn-invoke as i:setrtn i:invoke i:align i:label tail)
(let ((return-label (operand1 i:setrtn))
(invoke-ops (operand1 i:invoke))
(label (operand1 i:label)))
(if (and #f ; DISABLED
(= return-label label))
(as-source! as (cons (cons $setrtn/invoke invoke-ops)
(cons i:label
tail))))))
; Gets rid of spurious branch-to-next-instruction
; (branch Lx k)
; (.align y)
; (.label Lx)
; => (.align y)
; (.label Lx)
(define (branch-and-label as i:branch i:align i:label tail)
(let ((branch-label (operand1 i:branch))
(label (operand1 i:label)))
(if (= branch-label label)
(as-source! as (cons i:align (cons i:label tail))))))
(define (global-setreg as i:global i:setreg tail)
(let ((global (operand1 i:global))
(rd (operand1 i:setreg)))
(if (hwreg? rd)
(as-source! as (cons (list $global/setreg global rd) tail)))))
; Obscure guard: unsafe-code = #t implies that global/invoke will not
; check the value of the global variable, yet unsafe-code and
; catch-undefined-globals are supposed to be independent.
(define (global-invoke as i:global i:invoke tail)
(let ((global (operand1 i:global))
(argc (operand1 i:invoke)))
(if (not (and (unsafe-code) (catch-undefined-globals)))
(as-source! as (cons (list $global/invoke global argc) tail)))))
; Obscure guard: see comment for previous procedure.
; FIXME! This implementation is temporary until setrtn-invoke is enabled.
(define (global-setrtn-invoke as i:global i:setrtn i:invoke tail)
(let ((global (operand1 i:global))
(argc (operand1 i:invoke)))
(if (not (and (unsafe-code) (catch-undefined-globals)))
(as-source! as (cons i:setrtn
(cons (list $global/invoke global argc)
tail))))))
(define (reg-setglbl as i:reg i:setglbl tail)
(let ((rs (operand1 i:reg))
(global (operand1 i:setglbl)))
(if (hwreg? rs)
(as-source! as (cons (list $reg/setglbl rs global) tail)))))
; Test code
(define (peeptest istream)
(let ((as (make-assembly-structure istream)))
(let loop ((l '()))
(if (null? (as-source as))
(reverse l)
(begin (peep as)
(let ((a (car (as-source as))))
(as-source! as (cdr (as-source as)))
(loop (cons a l))))))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; SPARC assembler machine parameters & utility procedures.
;
; 13 May 1999 / wdc
; Round up to nearest 8.
(define (roundup8 n)
(* (quotient (+ n 7) 8) 8))
; Given an integer code for a register, return its register label.
; This register label is the register number for a h.w. register and the
; offsets from GLOBALS[ r0 ] for a s.w. register.
(define regname
(let ((v (vector $r.reg0 $r.reg1 $r.reg2 $r.reg3 $r.reg4 $r.reg5
$r.reg6 $r.reg7 $r.reg8 $r.reg9 $r.reg10 $r.reg11
$r.reg12 $r.reg13 $r.reg14 $r.reg15 $r.reg16 $r.reg17
$r.reg18 $r.reg19 $r.reg20 $r.reg21 $r.reg22 $r.reg23
$r.reg24 $r.reg25 $r.reg26 $r.reg27 $r.reg28 $r.reg29
$r.reg30 $r.reg31)))
(lambda (r)
(vector-ref v r))))
; Is a general-purpose register mapped to a hardware register?
; This is fragile! FIXME.
(define (hardware-mapped? r)
(or (and (>= r $r.reg0) (<= r $r.reg7))
(= r $r.argreg2)
(= r $r.argreg3)
(= r $r.result)
(= r $r.g0)
(= r $r.tmp0)
(= r $r.tmp1)
(= r $r.tmp2)))
; Used by peephole optimizer
(define (hwreg? x)
(<= 0 x 7))
(define (immediate-int? x)
(and (exact? x)
(integer? x)
(<= -1024 x 1023)))
; Given an exact integer, can it be represented as a fixnum?
(define fixnum-range?
(let ((-two^29 (- (expt 2 29)))
(two^29-1 (- (expt 2 29) 1)))
(lambda (x)
(<= -two^29 x two^29-1))))
; Does the integer x fit in the immediate field of an instruction?
(define (immediate-literal? x)
(<= -4096 x 4095))
; Return the offset in the %GLOBALS table of the given memory-mapped
; register. A memory-mapped register is represented by an integer which
; is its offet, so just return the value.
(define (swreg-global-offset r) r)
; Return a bit representation of a character constant.
(define (char->immediate c)
(+ (* (char->integer c) 65536) $imm.character))
; Convert an integer to a fixnum.
(define (thefixnum x) (* x 4))
; The offset of data slot 'n' within a procedure structure, not adjusting
; for tag. The proc is a header followed by code, const, and then data.
(define (procedure-slot-offset n)
(+ 12 (* n 4)))
; Src is a register, hwreg is a hardware register. If src is a
; hardware register, return src. Otherwise, emit an instruction to load
; src into hwreg and return hwreg.
(define (force-hwreg! as src hwreg)
(if (hardware-mapped? src)
src
(emit-load-reg! as src hwreg)))
; Given an arbitrary constant opd, generate code to load it into a
; register r.
(define (emit-constant->register as opd r)
(cond ((and (integer? opd) (exact? opd))
(if (fixnum-range? opd)
(emit-immediate->register! as (thefixnum opd) r)
(emit-const->register! as (emit-datum as opd) r)))
((boolean? opd)
(emit-immediate->register! as
(if (eq? opd #t)
$imm.true
$imm.false)
r))
((equal? opd (eof-object))
(emit-immediate->register! as $imm.eof r))
((equal? opd (unspecified))
(emit-immediate->register! as $imm.unspecified r))
((equal? opd (undefined))
(emit-immediate->register! as $imm.undefined r))
((null? opd)
(emit-immediate->register! as $imm.null r))
((char? opd)
(emit-immediate->register! as (char->immediate opd) r))
(else
(emit-const->register! as (emit-datum as opd) r))))
; Stuff a bitpattern or symbolic expression into a register.
; (CONST, for immediate constants.)
;
; FIXME(?): if this had access to eval-expr (currently hidden inside the
; sparc assembler) it could attempt to evaluate symbolic expressions,
; thereby selecting better code sequences when possible.
(define (emit-immediate->register! as i r)
(let ((dest (if (not (hardware-mapped? r)) $r.tmp0 r)))
(cond ((and (number? i) (immediate-literal? i))
(sparc.set as i dest))
((and (number? i) (zero? (remainder (abs i) 1024)))
(sparc.sethi as `(hi ,i) dest))
(else
(sparc.sethi as `(hi ,i) dest)
(sparc.ori as dest `(lo ,i) dest)))
(if (not (hardware-mapped? r))
(emit-store-reg! as r dest))))
; Reference the constants vector and put the constant reference in a register.
; `offset' is an integer offset into the constants vector (a constant) for
; the current procedure.
; Destroys $r.tmp0 and $r.tmp1, but either can be the destination register.
; (CONST, for structured constants, GLOBAL, SETGLBL, LAMBDA).
(define (emit-const->register! as offset r)
(let ((cvlabel (+ 4 (- (* offset 4) $tag.vector-tag))))
(cond ((hardware-mapped? r)
(sparc.ldi as $r.reg0 $p.constvector $r.tmp0)
(if (asm:fits? cvlabel 13)
(sparc.ldi as $r.tmp0 cvlabel r)
(begin (sparc.sethi as `(hi ,cvlabel) $r.tmp1)
(sparc.addr as $r.tmp0 $r.tmp1 $r.tmp0)
(sparc.ldi as $r.tmp0 `(lo ,cvlabel) r))))
(else
(emit-const->register! as offset $r.tmp0)
(emit-store-reg! as $r.tmp0 r)))))
; Emit single instruction to load sw-mapped reg into another reg, and return
; the destination reg.
(define (emit-load-reg! as from to)
(if (or (hardware-mapped? from) (not (hardware-mapped? to)))
(asm-error "emit-load-reg: " from to)
(begin (sparc.ldi as $r.globals (swreg-global-offset from) to)
to)))
(define (emit-store-reg! as from to)
(if (or (not (hardware-mapped? from)) (hardware-mapped? to))
(asm-error "emit-store-reg: " from to)
(begin (sparc.sti as from (swreg-global-offset to) $r.globals)
to)))
; Generic move-reg-to-HW-reg
(define (emit-move2hwreg! as from to)
(if (hardware-mapped? from)
(sparc.move as from to)
(emit-load-reg! as from to))
to)
; Evaluation of condition code for value or control.
;
; branchf.a is an annulled conditional branch that tests the condition codes
; and branches if some condition is false.
; rd is #f or a hardware register.
; target is #f or a label.
; Exactly one of rd and target must be #f.
;
; (Why isn't this split into two separate procedures? Because dozens of
; this procedure's callers have the value/control duality, and it saves
; space to put the test here instead of putting it in each caller.)
(define (emit-evaluate-cc! as branchf.a rd target)
(if target
(begin (branchf.a as target)
(sparc.slot as))
(let ((target (new-label)))
(branchf.a as target)
(sparc.set as $imm.false rd)
(sparc.set as $imm.true rd)
(sparc.label as target))))
; Code for runtime safety checking.
(define (emit-check! as rs0 L1 liveregs)
(sparc.cmpi as rs0 $imm.false)
(emit-checkcc! as sparc.be L1 liveregs))
; FIXME: This should call the exception handler for non-continuable exceptions.
(define (emit-trap! as rs1 rs2 rs3 exn)
(if (not (= rs3 $r.reg0))
(emit-move2hwreg! as rs3 $r.argreg3))
(if (not (= rs2 $r.reg0))
(emit-move2hwreg! as rs2 $r.argreg2))
(if (not (= rs1 $r.reg0))
(emit-move2hwreg! as rs1 $r.result))
(millicode-call/numarg-in-reg as $m.exception (thefixnum exn) $r.tmp0))
; Given:
; an annulled conditional branch that branches
; if the check is ok
; a non-annulled conditional branch that branches
; if the check is not ok
; #f, or a procedure that takes an assembly segment as
; argument and emits an instruction that goes into
; the delay slot of either branch
; three registers whose contents should be passed to the
; exception handler if the check is not ok
; the exception code
; Emits code to call the millicode exception routine with
; the given exception code if the condition is false.
;
; FIXME: The nop can often be replaced by the instruction that
; follows it.
(begin
'
(define (emit-checkcc-and-fill-slot!
as branch-ok.a branch-bad slot-filler L1)
(let* ((situation (list exn rs1 rs2 rs3))
(L1 (exception-label as situation)))
(if L1
(begin (branch-bad as L1)
(if slot-filler
(slot-filler as)
(sparc.nop as)))
(let* ((L1 (new-label))
(L2 (new-label)))
(exception-label-set! as situation L1)
(branch-ok.a as L2)
(if slot-filler
(slot-filler as)
(sparc.slot as))
(sparc.label as L1)
(cond ((= rs3 $r.reg0)
#f)
((hardware-mapped? $r.argreg3)
(emit-move2hwreg! as rs3 $r.argreg3))
((hardware-mapped? rs3)
(emit-store-reg! as rs3 $r.argreg3))
(else
(emit-move2hwreg! as rs3 $r.tmp0)
(emit-store-reg! as $r.tmp0 $r.argreg3)))
(if (not (= rs2 $r.reg0))
(emit-move2hwreg! as rs2 $r.argreg2))
(if (not (= rs1 $r.reg0))
(emit-move2hwreg! as rs1 $r.result))
; FIXME: This should be a non-continuable exception.
(sparc.jmpli as $r.millicode $m.exception $r.o7)
(emit-immediate->register! as (thefixnum exn) $r.tmp0)
(sparc.label as L2)))))
#f
)
(define (emit-checkcc! as branch-bad L1 liveregs)
(branch-bad as L1)
(apply sparc.slot2 as liveregs))
; Generation of millicode calls for non-continuable exceptions.
(begin
'
; To create only one millicode call per code segment per non-continuable
; exception situation, we use the "as-user" feature of assembly segments.
; Could use a hash table here.
(define (exception-label as situation)
(let ((user-data (as-user as)))
(if user-data
(let ((exception-labels (assq 'exception-labels user-data)))
(if exception-labels
(let ((probe (assoc situation (cdr exception-labels))))
(if probe
(cdr probe)
#f))
#f))
#f)))
'
(define (exception-label-set! as situation label)
(let ((user-data (as-user as)))
(if user-data
(let ((exception-labels (assq 'exception-labels user-data)))
(if exception-labels
(let ((probe (assoc situation (cdr exception-labels))))
(if probe
(error "COMPILER BUG: Exception situation defined twice")
(set-cdr! exception-labels
(cons (cons situation label)
(cdr exception-labels)))))
(begin (as-user! as
(cons (list 'exception-labels)
user-data))
(exception-label-set! as situation label))))
(begin (as-user! as '())
(exception-label-set! as situation label)))))
#f
)
; Millicode calling
(define (millicode-call/0arg as mproc)
(sparc.jmpli as $r.millicode mproc $r.o7)
(sparc.nop as))
(define (millicode-call/1arg as mproc r)
(sparc.jmpli as $r.millicode mproc $r.o7)
(emit-move2hwreg! as r $r.argreg2))
(define (millicode-call/1arg-in-result as mproc r)
(millicode-call/1arg-in-reg as mproc r $r.result))
(define (millicode-call/1arg-in-reg as mproc rs rd)
(sparc.jmpli as $r.millicode mproc $r.o7)
(emit-move2hwreg! as rs rd))
(define (millicode-call/numarg-in-result as mproc num)
(sparc.jmpli as $r.millicode mproc $r.o7)
(sparc.set as num $r.result))
(define (millicode-call/numarg-in-reg as mproc num reg)
(if (not (hardware-mapped? reg))
(asm-error "millicode-call/numarg-in-reg requires HW register: " reg))
(sparc.jmpli as $r.millicode mproc $r.o7)
(sparc.set as num reg))
(define (millicode-call/2arg as mproc r1 r2)
(emit-move2hwreg! as r1 $r.argreg2)
(sparc.jmpli as $r.millicode mproc $r.o7)
(emit-move2hwreg! as r2 $r.argreg3))
; NOTE: Don't use TMP0 since TMP0 is sometimes a millicode argument
; register (for example to m_exception).
;
; NOTE: Don't use sparc.set rather than sethi/ori; we need to know that
; two instructions get generated.
;
; FIXME: Should calculate the value if possible to get better precision
; and to avoid generating a fixup. See emit-return-address! in gen-msi.sch.
(define (millicode-call/ret as mproc label)
(cond ((short-effective-addresses)
(sparc.jmpli as $r.millicode mproc $r.o7)
(sparc.addi as $r.o7 `(- ,label (- ,(here as) 4) 8) $r.o7))
(else
(let ((val `(- ,label (+ ,(here as) 8) 8)))
(sparc.sethi as `(hi ,val) $r.tmp1)
(sparc.ori as $r.tmp1 `(lo ,val) $r.tmp1)
(sparc.jmpli as $r.millicode mproc $r.o7)
(sparc.addr as $r.o7 $r.tmp1 $r.o7)))))
(define (check-timer as DESTINATION RETRY)
(sparc.subicc as $r.timer 1 $r.timer)
(sparc.bne.a as DESTINATION)
(sparc.slot as)
(millicode-call/ret as $m.timer-exception RETRY))
; When the destination and retry labels are the same, and follow the
; timer check immediately, then this code saves two static instructions.
(define (check-timer0 as)
(sparc.subicc as $r.timer 1 $r.timer)
(sparc.bne.a as (+ (here as) 16))
(sparc.slot as)
(sparc.jmpli as $r.millicode $m.timer-exception $r.o7)
(sparc.nop as))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 9 May 1999 / wdc
;
; SPARC machine assembler.
;
; The procedure `sparc-instruction' takes an instruction class keyword and
; some operands and returns an assembler procedure for the instruction
; denoted by the class and the operands.
;
; All assembler procedures for SPARC mnemonics are defined in sparcasm2.sch.
;
; The SPARC has 32-bit, big-endian words. All instructions are 1 word.
; This assembler currently accepts a subset of the SPARC v8 instruction set.
;
; Each assembler procedure takes an `as' assembly structure (see
; Asm/Common/pass5p1.sch) and operands relevant to the instruction, and
; side-effects the assembly structure by emitting bits for the instruction
; and any necessary fixups. There are separate instruction mnemonics and
; assembler procedures for instructions which in the SPARC instruction set
; are normally considered the "same". For example, the `add' instruction is
; split into two operations here: `sparc.addr' takes a register as operand2,
; and `sparc.addi' takes an immediate. We could remove this restriction
; by using objects with identity rather than numbers for registers, but it
; does not seem to be an important problem.
;
; Operands that denote values (addresses, immediates, offsets) may be
; expressed using symbolic expressions. These expressions must conform
; to the following grammar:
;
; <expr> --> ( <number> . <obj> ) ; label
; | <number> ; literal value (exact integer)
; | (+ <expr> ... ) ; sum
; | (- <expr> ... ) ; difference
; | (hi <expr>) ; high 22 bits
; | (lo <expr>) ; low 10 bits
;
; Each assembler procedure will check that its value operand(s) fit in
; their instruction fields. It is a fatal error for an operand not
; to fit, and the assembler calls `asm-error' to signal this error.
; However, in some cases the assembler will instead call the error
; procedure `asm-value-too-large', which allows the higher-level assembler
; to retry the assembly with different settings (typically, by splitting
; a jump instruction into an offset calculation and a jump).
;
; Note: the idiom that is seen in this file,
; (emit-fixup-proc! as (lambda (b l) (fixup b l)))
; when `fixup' is a local procedure, avoids allocation of the closure
; except in the cases where the fixup is in fact needed, for gains in
; speed and reduction in allocation. (Ask me if you want numbers.)
;
; If FILL-DELAY-SLOTS returns true, then this assembler supports two
; distinct mechanisms for filling branch delay slots.
;
; An annulled conditional branch or an un-annulled unconditional branch
; may be followed by the strange instruction SPARC.SLOT, which turns into
; a nop in the delay slot that may be replaced by copying the instruction
; at the target of the branch into the delay slot and increasing the branch
; offset by 4.
;
; An un-annulled conditional branch whose target depends upon a known set
; of general registers, and does not depend upon the condition codes, may
; be followed by the strange instruction SPARC.SLOT2, which takes any
; number of registers as operands. This strange instruction turns into
; nothing at all if the following instruction has no side effects except
; to the condition codes and/or to a destination register that is distinct
; from the specified registers plus the stack pointer and %o7; otherwise
; the SPARC.SLOT2 instruction becomes a nop in the delay slot. The
; implementation of this uses a buffer that must be cleared when a label
; is emitted or when the current offset is obtained.
(define sparc-instruction)
(let ((original-emit-label! emit-label!)
(original-here here))
(set! emit-label!
(lambda (as L)
(assembler-value! as 'slot2-info #f)
(original-emit-label! as L)))
(set! here
(lambda (as)
(assembler-value! as 'slot2-info #f)
(original-here as)))
'emit-label!)
(let ((emit! (lambda (as bits)
(assembler-value! as 'slot2-info #f)
(emit! as bits)))
(emit-fixup-proc! (lambda (as proc)
(assembler-value! as 'slot2-info #f)
(emit-fixup-proc! as proc)))
(goes-in-delay-slot2? (lambda (as rd)
(let ((regs (assembler-value as 'slot2-info)))
(and regs
(fill-delay-slots)
(not (= rd $r.stkp))
(not (= rd $r.o7))
(not (memv rd regs)))))))
(define ibit (asm:bv 0 0 #x20 0)) ; immediate bit: 2^13
(define abit (asm:bv #x20 0 0 0)) ; annul bit: 2^29
(define zero (asm:bv 0 0 0 0)) ; all zero bits
(define two^32 (expt 2 32))
; Constant expression evaluation. If the expression cannot be
; evaluated, eval-expr returns #f, otherwise a number.
; The symbol table lookup must fail by returning #f.
(define (eval-expr as e)
(define (complement x)
(modulo (+ two^32 x) two^32))
(define (hibits e)
(cond ((not e) e)
((< e 0)
(complement (quotient (complement e) 1024)))
(else
(quotient e 1024))))
(define (lobits e)
(cond ((not e) e)
((< e 0)
(remainder (complement e) 1024))
(else
(remainder e 1024))))
(define (evaluate e)
(cond ((integer? e) e)
((label? e) (label-value as e))
((eq? 'hi (car e)) (hibits (evaluate (cadr e))))
((eq? 'lo (car e)) (lobits (evaluate (cadr e))))
((eq? '+ (car e))
(let loop ((e (cdr e)) (s 0))
(if (null? e) s
(let ((op (evaluate (car e))))
(if (not op) op
(loop (cdr e) (+ s op)))))))
((eq? '- (car e))
(let loop ((e (cdr e)) (d #f))
(if (null? e) d
(let ((op (evaluate (car e))))
(if (not op) op
(loop (cdr e) (if d (- d op) op)))))))
(else
(signal-error 'badexpr e))))
(evaluate e))
; Common error handling.
(define (signal-error code . rest)
(define msg "SPARC assembler: ")
(case code
((badexpr)
(asm-error msg "invalid expression " (car rest)))
((toolarge)
(asm-error msg "value too large in " (car rest) ": "
(cadr rest) " = " (caddr rest)))
((fixup)
(asm-error msg "fixup failed in " (car rest) " for " (cadr rest)))
((unaligned)
(asm-error msg "unaligned target in " (car rest) ": " (cadr rest)))
(else
(error "Invalid error code in assembler: " code))))
; The following procedures construct instructions by depositing field
; values directly into bytevectors; the location parameter in the dep-*!
; procedures is the address in the bytevector of the most significant byte.
(define (copy! bv k bits)
(bytevector-set! bv k (bytevector-ref bits 0))
(bytevector-set! bv (+ k 1) (bytevector-ref bits 1))
(bytevector-set! bv (+ k 2) (bytevector-ref bits 2))
(bytevector-set! bv (+ k 3) (bytevector-ref bits 3))
bv)
(define (copy bits)
(let ((bv (make-bytevector 4)))
(bytevector-set! bv 0 (bytevector-ref bits 0))
(bytevector-set! bv 1 (bytevector-ref bits 1))
(bytevector-set! bv 2 (bytevector-ref bits 2))
(bytevector-set! bv 3 (bytevector-ref bits 3))
bv))
(define (copy-instr bv from to)
(bytevector-set! bv to (bytevector-ref bv from))
(bytevector-set! bv (+ to 1) (bytevector-ref bv (+ from 1)))
(bytevector-set! bv (+ to 2) (bytevector-ref bv (+ from 2)))
(bytevector-set! bv (+ to 3) (bytevector-ref bv (+ from 3))))
(define (dep-rs1! bits k rs1)
(bytevector-set! bits (+ k 1)
(logior (bytevector-ref bits (+ k 1))
(rshl rs1 2)))
(bytevector-set! bits (+ k 2)
(logior (bytevector-ref bits (+ k 2))
(lsh (logand rs1 3) 6))))
(define (dep-rs2! bits k rs2)
(bytevector-set! bits (+ k 3)
(logior (bytevector-ref bits (+ k 3)) rs2)))
(define (dep-rd! bits k rd)
(bytevector-set! bits k
(logior (bytevector-ref bits k) (lsh rd 1))))
(define (dep-imm! bits k imm)
(cond ((fixnum? imm)
(bytevector-set! bits (+ k 3) (logand imm 255))
(bytevector-set! bits (+ k 2)
(logior (bytevector-ref bits (+ k 2))
(logand (rsha imm 8) 31))))
((bytevector? imm)
(bytevector-set! bits (+ k 3) (bytevector-ref imm 0))
(bytevector-set! bits (+ k 2)
(logior (bytevector-ref bits (+ k 2))
(logand (bytevector-ref imm 1)
31))))
(else
(dep-imm! bits k (asm:int->bv imm)))))
(define (dep-branch-offset! bits k offs)
(cond ((fixnum? offs)
(if (not (= (logand offs 3) 0))
(signal-error 'unaligned "branch" offs))
(dep-imm22! bits k (rsha offs 2)))
((bytevector? offs)
(if (not (= (logand (bytevector-ref offs 3) 3) 0))
(signal-error 'unaligned "branch" (asm:bv->int offs)))
(dep-imm22! bits k (asm:rsha offs 2)))
(else
(dep-branch-offset! bits k (asm:int->bv offs)))))
(define (dep-imm22! bits k imm)
(cond ((fixnum? imm)
(bytevector-set! bits (+ k 3) (logand imm 255))
(bytevector-set! bits (+ k 2)
(logand (rsha imm 8) 255))
(bytevector-set! bits (+ k 1)
(logior (bytevector-ref bits (+ k 1))
(logand (rsha imm 16) 63))))
((bytevector? imm)
(bytevector-set! bits (+ k 3) (bytevector-ref imm 3))
(bytevector-set! bits (+ k 2) (bytevector-ref imm 2))
(bytevector-set! bits (+ k 1)
(logior (bytevector-ref bits (+ k 1))
(logand (bytevector-ref imm 1)
63))))
(else
(dep-imm22! bits k (asm:int->bv imm)))))
(define (dep-call-offset! bits k offs)
(cond ((fixnum? offs)
(if (not (= (logand offs 3) 0))
(signal-error 'unaligned "call" offs))
(bytevector-set! bits (+ k 3) (logand (rsha offs 2) 255))
(bytevector-set! bits (+ k 2) (logand (rsha offs 10) 255))
(bytevector-set! bits (+ k 1) (logand (rsha offs 18) 255))
(bytevector-set! bits k (logior (bytevector-ref bits k)
(logand (rsha offs 26) 63))))
((bytevector? offs)
(if (not (= (logand (bytevector-ref offs 3) 3) 0))
(signal-error 'unaligned "call" (asm:bv->int offs)))
(let ((offs (asm:rsha offs 2)))
(bytevector-set! bits (+ k 3) (bytevector-ref offs 3))
(bytevector-set! bits (+ k 2) (bytevector-ref offs 2))
(bytevector-set! bits (+ k 1) (bytevector-ref offs 1))
(bytevector-set! bits k (logior (bytevector-ref bits k)
(logand (bytevector-ref offs 0)
63)))))
(else
(dep-call-offset! bits k (asm:int->bv offs)))))
; Add 1 to an instruction (to bump a branch offset by 4).
; FIXME: should check for field overflow.
(define (add1 bv loc)
(let* ((r0 (+ (bytevector-ref bv (+ loc 3)) 1))
(d0 (logand r0 255))
(c0 (rshl r0 8)))
(bytevector-set! bv (+ loc 3) d0)
(let* ((r1 (+ (bytevector-ref bv (+ loc 2)) c0))
(d1 (logand r1 255))
(c1 (rshl r1 8)))
(bytevector-set! bv (+ loc 2) d1)
(let* ((r2 (+ (bytevector-ref bv (+ loc 1)) c1))
(d2 (logand r2 255)))
(bytevector-set! bv (+ loc 1) d2)))))
; For delay slot filling -- uses the assembler value scratchpad in
; the as structure. Delay slot filling is discussed in the comments
; for `branch' and `class-slot', below.
(define (remember-branch-target as obj)
(assembler-value! as 'branch-target obj))
(define (recover-branch-target as)
(assembler-value as 'branch-target))
; Mark the instruction at the current address as not being eligible
; for being lifted into a branch delay slot.
;
; FIXME: should perhaps be a hash table; see BOOT-STATUS file for details.
(define (not-a-delay-slot-instruction as)
(assembler-value! as 'not-dsi
(cons (here as)
(or (assembler-value as 'not-dsi) '()))))
(define (is-a-delay-slot-instruction? as bv addr)
(and (not (memv addr (or (assembler-value as 'not-dsi) '())))
(< addr (bytevector-length bv))))
; SETHI, etc.
(define (class-sethi bits)
(let ((bits (asm:lsh bits 22)))
(lambda (as val rd)
(define (fixup bv loc)
(dep-imm22! bv loc
(or (eval-expr as val)
(signal-error 'fixup "sethi" val))))
(define (fixup2 bv loc)
(copy! bv loc bits)
(dep-rd! bv loc rd)
(fixup bv loc))
(if (goes-in-delay-slot2? as rd)
(emit-fixup-proc! as
(lambda (b l)
(fixup2 b (- l 4))))
(let ((bits (copy bits))
(e (eval-expr as val)))
(if e
(dep-imm22! bits 0 e)
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(dep-rd! bits 0 rd)
(emit! as bits))))))
; NOP is a peculiar sethi
(define (class-nop i)
(let ((instr (class-sethi i)))
(lambda (as)
(instr as 0 $r.g0))))
; Branches
(define (class00b i) (branch #b010 i zero)) ; Un-annulled IU branches.
(define (class00a i) (branch #b010 i abit)) ; Annulled IU branches.
(define (classf00b i) (branch #b110 i zero)) ; Un-annulled FP branches.
(define (classf00a i) (branch #b110 i abit)) ; Annulled FP branches.
; The `type' parameter is #b010 for IU branches, #b110 for FP branches.
; The `bits' parameter is the bits for the cond field.
; The `annul' parameter is either `zero' or `abit' (see top of file).
;
; Annuled branches require special treatement for delay slot
; filling based on the `slot' pseudo-instruction.
;
; Strategy: when a branch with the annul bit set is assembled, remember
; its target in a one-element cache in the AS structure. When a slot
; instruction is found (it has its own class) then the cached
; value (possibly a delayed expression) is gotten, and a fixup for the
; slot is registered. When the fixup is later evaluated, the branch
; target instruction can be found, examined, and evaluated.
;
; The cached value is always valid when the slot instruction is assembled,
; because a slot instruction is always directly preceded by an annulled
; branch (which will always set the cache).
(define (branch type bits annul)
; The delay slot should be filled if this is an annulled branch
; or an unconditional branch.
(let ((fill-delay-slot? (or (not (eq? annul zero))
(eq? bits #b1000)))
(bits (asm:logior (asm:lsh bits 25) (asm:lsh type 22) annul)))
(lambda (as target0)
(let ((target `(- ,target0 ,(here as))))
(define (expr)
(let ((e (eval-expr as target)))
(cond ((not e)
e)
((not (zero? (logand e 3)))
(signal-error 'unaligned "branch" target0))
((asm:fits? e 24)
e)
(else
(asm-value-too-large as "branch" target e)))))
(define (fixup bv loc)
(let ((e (expr)))
(if e
(dep-branch-offset! bv loc e)
(signal-error 'fixup "branch" target0))))
(if fill-delay-slot?
(remember-branch-target as target0)
(remember-branch-target as #f)) ; Clears the cache.
(not-a-delay-slot-instruction as)
(let ((bits (copy bits))
(e (expr)))
(if e
(dep-branch-offset! bits 0 e)
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(emit! as bits))))))
; Branch delay slot pseudo-instruction.
;
; Get the branch target expression from the cache in the AS structure,
; and if it is not #f, register a fixup procedure for the delay slot that
; will copy the target instruction to the slot and add 4 to the branch
; offset (unless that will overflow the offset or the instruction at the
; target is not suitable for lifting).
;
; It's important that this fixup run _after_ any fixups for the branch
; instruction itself!
(define (class-slot)
(let ((nop-instr (class-nop #b100)))
(lambda (as)
; The branch target is the expression denoting the target location.
(define branch-target (recover-branch-target as))
(define (fixup bv loc)
(let ((bt (or (eval-expr as branch-target)
(asm-error "Branch fixup: can't happen: "
branch-target))))
(if (is-a-delay-slot-instruction? as bv bt)
(begin
(copy-instr bv bt loc)
(add1 bv (- loc 4))))))
(if (and branch-target (fill-delay-slots))
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(nop-instr as))))
; Branch delay slot pseudo-instruction 2.
;
; Emit a nop, but record the information that will allow this nop to be
; replaced by a sufficiently harmless ALU instruction.
(define (class-slot2)
(let ((nop-instr (class-nop #b100)))
(lambda (as . regs)
(nop-instr as)
(assembler-value! as 'slot2-info regs))))
; ALU stuff, register operand, rdy, wryr. Also: jump.
(define (class10r bits . extra)
(cond ((and (not (null? extra)) (eq? (car extra) 'rdy))
(let ((op (class10r bits)))
(lambda (as rd)
(op as 0 0 rd))))
((and (not (null? extra)) (eq? (car extra) 'wry))
(let ((op (class10r bits)))
(lambda (as rs)
(op as rs 0 0))))
(else
(let ((bits (asm:logior (asm:lsh #b10 30) (asm:lsh bits 19)))
(jump? (and (not (null? extra)) (eq? (car extra) 'jump))))
(lambda (as rs1 rs2 rd)
(let ((bits (copy bits)))
(dep-rs1! bits 0 rs1)
(dep-rs2! bits 0 rs2)
(dep-rd! bits 0 rd)
(cond (jump?
(not-a-delay-slot-instruction as)
(emit! as bits))
((goes-in-delay-slot2? as rd)
(emit-fixup-proc!
as
(lambda (bv loc)
(copy! bv (- loc 4) bits))))
(else
(emit! as bits)))))))))
; ALU stuff, immediate operand, wryi. Also: jump.
(define (class10i bits . extra)
(if (and (not (null? extra)) (eq? (car extra) 'wry))
(let ((op (class10i bits)))
(lambda (as src)
(op as 0 src 0)))
(let ((bits (asm:logior (asm:lsh #b10 30) (asm:lsh bits 19) ibit))
(jump? (and (not (null? extra)) (eq? (car extra) 'jump))))
(lambda (as rs1 e rd)
(define (expr)
(let ((imm (eval-expr as e)))
(cond ((not imm)
imm)
((asm:fits? imm 13)
imm)
(jump?
(asm-value-too-large as "`jmpli'" e imm))
(else
(asm-value-too-large as "ALU instruction" e imm)))))
(define (fixup bv loc)
(let ((e (expr)))
(if e
(dep-imm! bv loc e)
(signal-error 'fixup "ALU instruction" e))))
(let ((bits (copy bits))
(e (expr)))
(if e
(dep-imm! bits 0 e)
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(dep-rs1! bits 0 rs1)
(dep-rd! bits 0 rd)
(cond (jump?
(not-a-delay-slot-instruction as)
(emit! as bits))
((goes-in-delay-slot2? as rd)
(emit-fixup-proc!
as
(lambda (bv loc)
(copy! bv (- loc 4) bits))))
(else
(emit! as bits))))))))
; Memory stuff, register operand.
(define (class11r bits)
(let ((bits (asm:logior (asm:lsh #b11 30) (asm:lsh bits 19))))
(lambda (as rs1 rs2 rd)
(let ((bits (copy bits)))
(dep-rs1! bits 0 rs1)
(dep-rs2! bits 0 rs2)
(dep-rd! bits 0 rd)
(emit! as bits)))))
; Memory stuff, immediate operand.
(define (class11i bits)
(let ((bits (asm:logior (asm:lsh #b11 30) (asm:lsh bits 19) ibit)))
(lambda (as rs1 e rd)
(define (expr)
(let ((imm (eval-expr as e)))
(cond ((not imm) imm)
((asm:fits? imm 13) imm)
(else
(signal-error 'toolarge "Memory instruction" e imm)))))
(define (fixup bv loc)
(let ((e (expr)))
(if e
(dep-imm! bv loc e)
(signal-error 'fixup "Memory instruction" e))))
(let ((bits (copy bits))
(e (expr)))
(dep-rs1! bits 0 rs1)
(dep-rd! bits 0 rd)
(if e
(dep-imm! bits 0 e)
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(emit! as bits)))))
; For store instructions. The syntax is (st a b c) meaning m[ b+c ] <- a.
; However, on the Sparc, the destination (rd) field is the source of
; a store, so we transform the instruction into (st c b a) and pass it
; to the real store procedure.
(define (class11sr bits)
(let ((store-instr (class11r bits)))
(lambda (as a b c)
(store-instr as c b a))))
(define (class11si bits)
(let ((store-instr (class11i bits)))
(lambda (as a b c)
(store-instr as c b a))))
; Call is a class all by itself.
(define (class-call)
(let ((code (asm:lsh #b01 30)))
(lambda (as target0)
(let ((target `(- ,target0 ,(here as))))
(define (fixup bv loc)
(let ((e (eval-expr as target)))
(if e
(dep-call-offset! bv loc e)
(signal-error 'fixup "call" target0))))
(let ((bits (copy code))
(e (eval-expr as target)))
(not-a-delay-slot-instruction as)
(if e
(dep-call-offset! bits 0 e)
(emit-fixup-proc! as (lambda (b l) (fixup b l))))
(emit! as bits))))))
(define (class-label)
(lambda (as label)
(emit-label! as label)))
; FP operation, don't set CC.
(define (class-fpop1 i) (fpop #b110100 i))
; FP operation, set CC
(define (class-fpop2 i) (fpop #b110101 i))
(define (fpop type opf)
(let ((bits (asm:logior (asm:lsh #b10 30)
(asm:lsh type 19)
(asm:lsh opf 5))))
(lambda (as rs1 rs2 rd)
(let ((bits (copy bits)))
(dep-rs1! bits 0 rs1)
(dep-rs2! bits 0 rs2)
(dep-rd! bits 0 rd)
(emit! as bits)))))
(set! sparc-instruction
(lambda (kwd . ops)
(case kwd
((i11) (apply class11i ops))
((r11) (apply class11r ops))
((si11) (apply class11si ops))
((sr11) (apply class11sr ops))
((sethi) (apply class-sethi ops))
((r10) (apply class10r ops))
((i10) (apply class10i ops))
((b00) (apply class00b ops))
((a00) (apply class00a ops))
((call) (apply class-call ops))
((label) (apply class-label ops))
((nop) (apply class-nop ops))
((slot) (apply class-slot ops))
((slot2) (apply class-slot2 ops))
((fb00) (apply classf00b ops))
((fa00) (apply classf00a ops))
((fp) (apply class-fpop1 ops))
((fpcc) (apply class-fpop2 ops))
(else
(asm-error "sparc-instruction: unrecognized class: " kwd)))))
'sparc-instruction)
; eof
; Instruction mnemonics
(define sparc.lddi (sparc-instruction 'i11 #b000011))
(define sparc.lddr (sparc-instruction 'r11 #b000011))
(define sparc.ldi (sparc-instruction 'i11 #b000000))
(define sparc.ldr (sparc-instruction 'r11 #b000000))
(define sparc.ldhi (sparc-instruction 'i11 #b000010))
(define sparc.ldhr (sparc-instruction 'r11 #b000010))
(define sparc.ldbi (sparc-instruction 'i11 #b000001))
(define sparc.ldbr (sparc-instruction 'r11 #b000001))
(define sparc.lddfi (sparc-instruction 'i11 #b100011))
(define sparc.lddfr (sparc-instruction 'r11 #b100011))
(define sparc.stdi (sparc-instruction 'si11 #b000111))
(define sparc.stdr (sparc-instruction 'sr11 #b000111))
(define sparc.sti (sparc-instruction 'si11 #b000100))
(define sparc.str (sparc-instruction 'sr11 #b000100))
(define sparc.sthi (sparc-instruction 'si11 #b000110))
(define sparc.sthr (sparc-instruction 'sr11 #b000110))
(define sparc.stbi (sparc-instruction 'si11 #b000101))
(define sparc.stbr (sparc-instruction 'sr11 #b000101))
(define sparc.stdfi (sparc-instruction 'si11 #b100111))
(define sparc.stdfr (sparc-instruction 'sr11 #b100111))
(define sparc.sethi (sparc-instruction 'sethi #b100))
(define sparc.andr (sparc-instruction 'r10 #b000001))
(define sparc.andrcc (sparc-instruction 'r10 #b010001))
(define sparc.andi (sparc-instruction 'i10 #b000001))
(define sparc.andicc (sparc-instruction 'i10 #b010001))
(define sparc.orr (sparc-instruction 'r10 #b000010))
(define sparc.orrcc (sparc-instruction 'r10 #b010010))
(define sparc.ori (sparc-instruction 'i10 #b000010))
(define sparc.oricc (sparc-instruction 'i10 #b010010))
(define sparc.xorr (sparc-instruction 'r10 #b000011))
(define sparc.xorrcc (sparc-instruction 'r10 #b010011))
(define sparc.xori (sparc-instruction 'i10 #b000011))
(define sparc.xoricc (sparc-instruction 'i10 #b010011))
(define sparc.sllr (sparc-instruction 'r10 #b100101))
(define sparc.slli (sparc-instruction 'i10 #b100101))
(define sparc.srlr (sparc-instruction 'r10 #b100110))
(define sparc.srli (sparc-instruction 'i10 #b100110))
(define sparc.srar (sparc-instruction 'r10 #b100111))
(define sparc.srai (sparc-instruction 'i10 #b100111))
(define sparc.addr (sparc-instruction 'r10 #b000000))
(define sparc.addrcc (sparc-instruction 'r10 #b010000))
(define sparc.addi (sparc-instruction 'i10 #b000000))
(define sparc.addicc (sparc-instruction 'i10 #b010000))
(define sparc.taddrcc (sparc-instruction 'r10 #b100000))
(define sparc.taddicc (sparc-instruction 'i10 #b100000))
(define sparc.subr (sparc-instruction 'r10 #b000100))
(define sparc.subrcc (sparc-instruction 'r10 #b010100))
(define sparc.subi (sparc-instruction 'i10 #b000100))
(define sparc.subicc (sparc-instruction 'i10 #b010100))
(define sparc.tsubrcc (sparc-instruction 'r10 #b100001))
(define sparc.tsubicc (sparc-instruction 'i10 #b100001))
(define sparc.smulr (sparc-instruction 'r10 #b001011))
(define sparc.smulrcc (sparc-instruction 'r10 #b011011))
(define sparc.smuli (sparc-instruction 'i10 #b001011))
(define sparc.smulicc (sparc-instruction 'i10 #b011011))
(define sparc.sdivr (sparc-instruction 'r10 #b001111))
(define sparc.sdivrcc (sparc-instruction 'r10 #b011111))
(define sparc.sdivi (sparc-instruction 'i10 #b001111))
(define sparc.sdivicc (sparc-instruction 'i10 #b011111))
(define sparc.b (sparc-instruction 'b00 #b1000))
(define sparc.b.a (sparc-instruction 'a00 #b1000))
(define sparc.bne (sparc-instruction 'b00 #b1001))
(define sparc.bne.a (sparc-instruction 'a00 #b1001))
(define sparc.be (sparc-instruction 'b00 #b0001))
(define sparc.be.a (sparc-instruction 'a00 #b0001))
(define sparc.bg (sparc-instruction 'b00 #b1010))
(define sparc.bg.a (sparc-instruction 'a00 #b1010))
(define sparc.ble (sparc-instruction 'b00 #b0010))
(define sparc.ble.a (sparc-instruction 'a00 #b0010))
(define sparc.bge (sparc-instruction 'b00 #b1011))
(define sparc.bge.a (sparc-instruction 'a00 #b1011))
(define sparc.bl (sparc-instruction 'b00 #b0011))
(define sparc.bl.a (sparc-instruction 'a00 #b0011))
(define sparc.bgu (sparc-instruction 'b00 #b1100))
(define sparc.bgu.a (sparc-instruction 'a00 #b1100))
(define sparc.bleu (sparc-instruction 'b00 #b0100))
(define sparc.bleu.a (sparc-instruction 'a00 #b0100))
(define sparc.bcc (sparc-instruction 'b00 #b1101))
(define sparc.bcc.a (sparc-instruction 'a00 #b1101))
(define sparc.bcs (sparc-instruction 'b00 #b0101))
(define sparc.bcs.a (sparc-instruction 'a00 #b0101))
(define sparc.bpos (sparc-instruction 'b00 #b1110))
(define sparc.bpos.a (sparc-instruction 'a00 #b1110))
(define sparc.bneg (sparc-instruction 'b00 #b0110))
(define sparc.bneg.a (sparc-instruction 'a00 #b0110))
(define sparc.bvc (sparc-instruction 'b00 #b1111))
(define sparc.bvc.a (sparc-instruction 'a00 #b1111))
(define sparc.bvs (sparc-instruction 'b00 #b0111))
(define sparc.bvs.a (sparc-instruction 'a00 #b0111))
(define sparc.call (sparc-instruction 'call))
(define sparc.jmplr (sparc-instruction 'r10 #b111000 'jump))
(define sparc.jmpli (sparc-instruction 'i10 #b111000 'jump))
(define sparc.nop (sparc-instruction 'nop #b100))
(define sparc.ornr (sparc-instruction 'r10 #b000110))
(define sparc.orni (sparc-instruction 'i10 #b000110))
(define sparc.ornrcc (sparc-instruction 'r10 #b010110))
(define sparc.ornicc (sparc-instruction 'i10 #b010110))
(define sparc.andni (sparc-instruction 'i10 #b000101))
(define sparc.andnr (sparc-instruction 'r10 #b000101))
(define sparc.andnicc (sparc-instruction 'i10 #b010101))
(define sparc.andnrcc (sparc-instruction 'r10 #b010101))
(define sparc.rdy (sparc-instruction 'r10 #b101000 'rdy))
(define sparc.wryr (sparc-instruction 'r10 #b110000 'wry))
(define sparc.wryi (sparc-instruction 'i10 #b110000 'wry))
(define sparc.fb (sparc-instruction 'fb00 #b1000))
(define sparc.fb.a (sparc-instruction 'fa00 #b1000))
(define sparc.fbn (sparc-instruction 'fb00 #b0000))
(define sparc.fbn.a (sparc-instruction 'fa00 #b0000))
(define sparc.fbu (sparc-instruction 'fb00 #b0111))
(define sparc.fbu.a (sparc-instruction 'fa00 #b0111))
(define sparc.fbg (sparc-instruction 'fb00 #b0110))
(define sparc.fbg.a (sparc-instruction 'fa00 #b0110))
(define sparc.fbug (sparc-instruction 'fb00 #b0101))
(define sparc.fbug.a (sparc-instruction 'fa00 #b0101))
(define sparc.fbl (sparc-instruction 'fb00 #b0100))
(define sparc.fbl.a (sparc-instruction 'fa00 #b0100))
(define sparc.fbul (sparc-instruction 'fb00 #b0011))
(define sparc.fbul.a (sparc-instruction 'fa00 #b0011))
(define sparc.fblg (sparc-instruction 'fb00 #b0010))
(define sparc.fblg.a (sparc-instruction 'fa00 #b0010))
(define sparc.fbne (sparc-instruction 'fb00 #b0001))
(define sparc.fbne.a (sparc-instruction 'fa00 #b0001))
(define sparc.fbe (sparc-instruction 'fb00 #b1001))
(define sparc.fbe.a (sparc-instruction 'fa00 #b1001))
(define sparc.fbue (sparc-instruction 'fb00 #b1010))
(define sparc.fbue.a (sparc-instruction 'fa00 #b1010))
(define sparc.fbge (sparc-instruction 'fb00 #b1011))
(define sparc.fbge.a (sparc-instruction 'fa00 #b1011))
(define sparc.fbuge (sparc-instruction 'fb00 #b1100))
(define sparc.fbuge.a (sparc-instruction 'fa00 #b1100))
(define sparc.fble (sparc-instruction 'fb00 #b1101))
(define sparc.fble.a (sparc-instruction 'fa00 #b1101))
(define sparc.fbule (sparc-instruction 'fb00 #b1110))
(define sparc.fbule.a (sparc-instruction 'fa00 #b1110))
(define sparc.fbo (sparc-instruction 'fb00 #b1111))
(define sparc.fbo.a (sparc-instruction 'fa00 #b1111))
(define sparc.faddd (sparc-instruction 'fp #b001000010))
(define sparc.fsubd (sparc-instruction 'fp #b001000110))
(define sparc.fmuld (sparc-instruction 'fp #b001001010))
(define sparc.fdivd (sparc-instruction 'fp #b001001110))
(define sparc%fnegs (sparc-instruction 'fp #b000000101)) ; See below
(define sparc%fmovs (sparc-instruction 'fp #b000000001)) ; See below
(define sparc%fabss (sparc-instruction 'fp #b000001001)) ; See below
(define sparc%fcmpdcc (sparc-instruction 'fpcc #b001010010)) ; See below
; Strange instructions.
(define sparc.slot (sparc-instruction 'slot))
(define sparc.slot2 (sparc-instruction 'slot2))
(define sparc.label (sparc-instruction 'label))
; Aliases.
(define sparc.bnz sparc.bne)
(define sparc.bnz.a sparc.bne.a)
(define sparc.bz sparc.be)
(define sparc.bz.a sparc.be.a)
(define sparc.bgeu sparc.bcc)
(define sparc.bgeu.a sparc.bcc.a)
(define sparc.blu sparc.bcs)
(define sparc.blu.a sparc.bcs.a)
; Abstractions.
(define (sparc.cmpr as r1 r2) (sparc.subrcc as r1 r2 $r.g0))
(define (sparc.cmpi as r imm) (sparc.subicc as r imm $r.g0))
(define (sparc.move as rs rd) (sparc.orr as $r.g0 rs rd))
(define (sparc.set as imm rd) (sparc.ori as $r.g0 imm rd))
(define (sparc.btsti as rs imm) (sparc.andicc as rs imm $r.g0))
(define (sparc.clr as rd) (sparc.move as $r.g0 rd))
(define (sparc.deccc as rs . rest)
(let ((k (cond ((null? rest) 1)
((null? (cdr rest)) (car rest))
(else (asm-error "sparc.deccc: too many operands: " rest)))))
(sparc.subicc as rs k rs)))
; Floating-point abstractions
;
; For fmovd, fnegd, and fabsd, we must synthesize the instruction from
; fmovs, fnegs, and fabss -- SPARC V8 has only the latter. (SPARC V9 add
; the former.)
(define (sparc.fmovd as rs rd)
(sparc%fmovs as rs 0 rd)
(sparc%fmovs as (+ rs 1) 0 (+ rd 1)))
(define (sparc.fnegd as rs rd)
(sparc%fnegs as rs 0 rd)
(if (not (= rs rd))
(sparc%fmovs as (+ rs 1) 0 (+ rd 1))))
(define (sparc.fabsd as rs rd)
(sparc%fabss as rs 0 rd)
(if (not (= rs rd))
(sparc%fmovs as (+ rs 1) 0 (+ rd 1))))
(define (sparc.fcmpd as rs1 rs2)
(sparc%fcmpdcc as rs1 rs2 0))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; Asm/Sparc/gen-msi.sch -- SPARC assembler code emitters for
; core MacScheme instructions
;
; 9 May 1999 / wdc
; SETGLBL
;
; RS must be a hardware register.
;
; A global cell is a pair, where the car holds the value.
(define (emit-register->global! as rs offset)
(cond ((= rs $r.result)
(sparc.move as $r.result $r.argreg2)
(emit-const->register! as offset $r.result)
(if (write-barrier)
(sparc.jmpli as $r.millicode $m.addtrans $r.o7))
(sparc.sti as $r.argreg2 (- $tag.pair-tag) $r.result))
(else
(emit-const->register! as offset $r.result)
(sparc.sti as rs (- $tag.pair-tag) $r.result)
(if (write-barrier)
(millicode-call/1arg as $m.addtrans rs)))))
; GLOBAL
;
; A global cell is a pair, where the car holds the value.
; If (catch-undefined-globals) is true, then code will be emitted to
; check whether the global is #!undefined when loaded. If it is,
; an exception will be taken, with the global in question in $r.result.
(define (emit-global->register! as offset r)
(emit-load-global as offset r (catch-undefined-globals)))
; This leaves the cell in ARGREG2. That fact is utilized by global/invoke
; to signal an appropriate error message.
(define (emit-load-global as offset r check?)
(define (emit-undef-check! as r)
(if check?
(let ((GLOBAL-OK (new-label)))
(sparc.cmpi as r $imm.undefined)
(sparc.bne.a as GLOBAL-OK)
(sparc.slot as)
(millicode-call/0arg as $m.global-ex) ; Cell in ARGREG2.
(sparc.label as GLOBAL-OK))))
(emit-const->register! as offset $r.argreg2) ; Load cell.
(if (hardware-mapped? r)
(begin (sparc.ldi as $r.argreg2 (- $tag.pair-tag) r)
(emit-undef-check! as r))
(begin (sparc.ldi as $r.argreg2 (- $tag.pair-tag) $r.tmp0)
(emit-store-reg! as $r.tmp0 r)
(emit-undef-check! as $r.tmp0))))
; MOVEREG
(define (emit-register->register! as from to)
(if (not (= from to))
(cond ((and (hardware-mapped? from) (hardware-mapped? to))
(sparc.move as from to))
((hardware-mapped? from)
(emit-store-reg! as from to))
((hardware-mapped? to)
(emit-load-reg! as from to))
(else
(emit-load-reg! as from $r.tmp0)
(emit-store-reg! as $r.tmp0 to)))))
; ARGS=
(define (emit-args=! as n)
(if (not (unsafe-code))
(let ((L2 (new-label)))
(sparc.cmpi as $r.result (thefixnum n)) ; FIXME: limit 1023 args
(sparc.be.a as L2)
(sparc.slot as)
(millicode-call/numarg-in-reg as $m.argc-ex (thefixnum n) $r.argreg2)
(sparc.label as L2))))
; ARGS>=
;
; The cases for 0 and 1 rest arguments are handled in-line; all other
; cases, including too few, are handled in millicode (really: a C call-out).
;
; The fast path only applies when we don't have to mess with the last
; register, hence the test.
(define (emit-args>=! as n)
(let ((L0 (new-label))
(L99 (new-label))
(L98 (new-label)))
(if (< n (- *lastreg* 1))
(let ((dest (regname (+ n 1))))
(sparc.cmpi as $r.result (thefixnum n)) ; n args
(if (hardware-mapped? dest)
(begin
(sparc.be.a as L99)
(sparc.set as $imm.null dest))
(begin
(sparc.set as $imm.null $r.tmp0)
(sparc.be.a as L99)
(sparc.sti as $r.tmp0 (swreg-global-offset dest) $r.globals)))
(sparc.cmpi as $r.result (thefixnum (+ n 1))) ; n+1 args
(sparc.bne.a as L98)
(sparc.nop as)
(millicode-call/numarg-in-result as $m.alloc 8)
(let ((src1 (force-hwreg! as dest $r.tmp1)))
(sparc.set as $imm.null $r.tmp0)
(sparc.sti as src1 0 $r.result)
(sparc.sti as $r.tmp0 4 $r.result)
(sparc.addi as $r.result $tag.pair-tag $r.result)
(sparc.b as L99)
(if (hardware-mapped? dest)
(sparc.move as $r.result dest)
(sparc.sti as $r.result (swreg-global-offset dest)
$r.globals)))))
; General case
(sparc.label as L98)
(sparc.move as $r.reg0 $r.argreg3) ; FIXME in Sparc/mcode.s
(millicode-call/numarg-in-reg as $m.varargs (thefixnum n) $r.argreg2)
(sparc.label as L99)))
; INVOKE
; SETRTN/INVOKE
;
; Bummed. Can still do better when the procedure to call is in a general
; register (avoids the redundant move to RESULT preceding INVOKE).
;
; Note we must set up the argument count even in unsafe mode, because we
; may be calling code that was not compiled unsafe.
(define (emit-invoke as n setrtn? mc-exception)
(let ((START (new-label))
(TIMER-OK (new-label))
(PROC-OK (new-label)))
(cond ((not (unsafe-code))
(sparc.label as START)
(sparc.subicc as $r.timer 1 $r.timer)
(sparc.bne as TIMER-OK)
(sparc.andi as $r.result $tag.tagmask $r.tmp0)
(millicode-call/ret as $m.timer-exception START)
(sparc.label as TIMER-OK)
(sparc.cmpi as $r.tmp0 $tag.procedure-tag)
(sparc.be.a as PROC-OK)
(sparc.ldi as $r.result $p.codevector $r.tmp0)
(millicode-call/ret as mc-exception START)
(sparc.label as PROC-OK))
(else
(sparc.label as START)
(sparc.subicc as $r.timer 1 $r.timer)
(sparc.bne.a as TIMER-OK)
(sparc.ldi as $r.result $p.codevector $r.tmp0)
(millicode-call/ret as $m.timer-exception START)
(sparc.label as TIMER-OK)))
(sparc.move as $r.result $r.reg0)
;; FIXME: limit 1023 args
(cond (setrtn?
(sparc.set as (thefixnum n) $r.result)
(sparc.jmpli as $r.tmp0 $p.codeoffset $r.o7)
(sparc.sti as $r.o7 4 $r.stkp))
(else
(sparc.jmpli as $r.tmp0 $p.codeoffset $r.g0)
(sparc.set as (thefixnum n) $r.result)))))
; SAVE -- for new compiler
;
; Create stack frame. To avoid confusing the garbage collector, the
; slots must be initialized to something definite unless they will
; immediately be initialized by a MacScheme machine store instruction.
; The creation is done by emit-save0!, and the initialization is done
; by emit-save1!.
(define (emit-save0! as n)
(let* ((L1 (new-label))
(L0 (new-label))
(framesize (+ 8 (* (+ n 1) 4)))
(realsize (roundup8 (+ framesize 4))))
(sparc.label as L0)
(sparc.subi as $r.stkp realsize $r.stkp)
(sparc.cmpr as $r.stklim $r.stkp)
(sparc.ble.a as L1)
(sparc.set as framesize $r.tmp0)
(sparc.addi as $r.stkp realsize $r.stkp)
(millicode-call/ret as $m.stkoflow L0)
(sparc.label as L1)
; initialize size and return fields of stack frame
(sparc.sti as $r.tmp0 0 $r.stkp)
(sparc.sti as $r.g0 4 $r.stkp)))
; Given a vector v of booleans, initializes slot i of the stack frame
; if and only if (vector-ref v i).
(define (emit-save1! as v)
(let ((n (vector-length v)))
(let loop ((i 0) (offset 12))
(cond ((= i n)
#t)
((vector-ref v i)
(sparc.sti as $r.g0 offset $r.stkp)
(loop (+ i 1) (+ offset 4)))
(else
(loop (+ i 1) (+ offset 4)))))))
; RESTORE
;
; Restore registers from stack frame
; FIXME: Use ldd/std here; see comments for emit-save!, above.
; We pop only actual registers.
(define (emit-restore! as n)
(let ((n (min n 31)))
(do ((i 0 (+ i 1))
(offset 12 (+ offset 4)))
((> i n))
(let ((r (regname i)))
(if (hardware-mapped? r)
(sparc.ldi as $r.stkp offset r)
(begin (sparc.ldi as $r.stkp offset $r.tmp0)
(emit-store-reg! as $r.tmp0 r)))))))
; POP -- for new compiler
;
; Pop frame.
; If returning?, then emit the return as well and put the pop
; in its delay slot.
(define (emit-pop! as n returning?)
(let* ((framesize (+ 8 (* (+ n 1) 4)))
(realsize (roundup8 (+ framesize 4))))
(if returning?
(begin (sparc.ldi as $r.stkp (+ realsize 4) $r.o7)
(sparc.jmpli as $r.o7 8 $r.g0)
(sparc.addi as $r.stkp realsize $r.stkp))
(sparc.addi as $r.stkp realsize $r.stkp))))
; SETRTN
;
; Change the return address in the stack frame.
(define (emit-setrtn! as label)
(emit-return-address! as label)
(sparc.sti as $r.o7 4 $r.stkp))
; APPLY
;
; `apply' falls into millicode.
;
; The timer check is performed here because it is not very easy for the
; millicode to do this.
(define (emit-apply! as r1 r2)
(let ((L0 (new-label)))
(check-timer0 as)
(sparc.label as L0)
(emit-move2hwreg! as r1 $r.argreg2)
(emit-move2hwreg! as r2 $r.argreg3)
(millicode-call/0arg as $m.apply)))
; LOAD
(define (emit-load! as slot dest-reg)
(if (hardware-mapped? dest-reg)
(sparc.ldi as $r.stkp (+ 12 (* slot 4)) dest-reg)
(begin (sparc.ldi as $r.stkp (+ 12 (* slot 4)) $r.tmp0)
(emit-store-reg! as $r.tmp0 dest-reg))))
; STORE
(define (emit-store! as k n)
(if (hardware-mapped? k)
(sparc.sti as k (+ 12 (* n 4)) $r.stkp)
(begin (emit-load-reg! as k $r.tmp0)
(sparc.sti as $r.tmp0 (+ 12 (* n 4)) $r.stkp))))
; LEXICAL
(define (emit-lexical! as m n)
(let ((base (emit-follow-chain! as m)))
(sparc.ldi as base (- (procedure-slot-offset n) $tag.procedure-tag)
$r.result)))
; SETLEX
; FIXME: should allow an in-line barrier
(define (emit-setlex! as m n)
(let ((base (emit-follow-chain! as m)))
(sparc.sti as $r.result (- (procedure-slot-offset n) $tag.procedure-tag)
base)
(if (write-barrier)
(begin
(sparc.move as $r.result $r.argreg2)
(millicode-call/1arg-in-result as $m.addtrans base)))))
; Follow static links.
;
; By using and leaving the result in ARGREG3 rather than in RESULT,
; we save a temporary register.
(define (emit-follow-chain! as m)
(let loop ((q m))
(cond ((not (zero? q))
(sparc.ldi as
(if (= q m) $r.reg0 $r.argreg3)
$p.linkoffset
$r.argreg3)
(loop (- q 1)))
((zero? m)
$r.reg0)
(else
$r.argreg3))))
; RETURN
(define (emit-return! as)
(sparc.ldi as $r.stkp 4 $r.o7)
(sparc.jmpli as $r.o7 8 $r.g0)
(sparc.nop as))
; RETURN-REG k
(define (emit-return-reg! as r)
(sparc.ldi as $r.stkp 4 $r.o7)
(sparc.jmpli as $r.o7 8 $r.g0)
(sparc.move as r $r.result))
; RETURN-CONST k
;
; The constant c must be synthesizable in a single instruction.
(define (emit-return-const! as c)
(sparc.ldi as $r.stkp 4 $r.o7)
(sparc.jmpli as $r.o7 8 $r.g0)
(emit-constant->register as c $r.result))
; MVRTN
(define (emit-mvrtn! as)
(asm-error "multiple-value return has not been implemented (yet)."))
; LEXES
(define (emit-lexes! as n-slots)
(emit-alloc-proc! as n-slots)
(sparc.ldi as $r.reg0 $p.codevector $r.tmp0)
(sparc.ldi as $r.reg0 $p.constvector $r.tmp1)
(sparc.sti as $r.tmp0 $p.codevector $r.result)
(sparc.sti as $r.tmp1 $p.constvector $r.result)
(emit-init-proc-slots! as n-slots))
; LAMBDA
(define (emit-lambda! as code-offs0 const-offs0 n-slots)
(let* ((code-offs (+ 4 (- (* 4 code-offs0) $tag.vector-tag)))
(const-offs (+ 4 (- (* 4 const-offs0) $tag.vector-tag)))
(fits? (asm:fits? const-offs 13)))
(emit-alloc-proc! as n-slots)
(if fits?
(begin (sparc.ldi as $r.reg0 $p.constvector $r.tmp0)
(sparc.ldi as $r.tmp0 code-offs $r.tmp1))
(emit-const->register! as code-offs0 $r.tmp1))
(sparc.sti as $r.tmp1 $p.codevector $r.result)
(if fits?
(begin (sparc.ldi as $r.reg0 $p.constvector $r.tmp0)
(sparc.ldi as $r.tmp0 const-offs $r.tmp1))
(emit-const->register! as const-offs0 $r.tmp1))
(sparc.sti as $r.tmp1 $p.constvector $r.result)
(emit-init-proc-slots! as n-slots)))
; Allocate procedure with room for n register slots; return tagged pointer.
(define emit-alloc-proc!
(let ((two^12 (expt 2 12)))
(lambda (as n)
(millicode-call/numarg-in-result as $m.alloc (* (+ n 4) 4))
(let ((header (+ (* (* (+ n 3) 4) 256) $imm.procedure-header)))
(emit-immediate->register! as header $r.tmp0)
(sparc.sti as $r.tmp0 0 $r.result)
(sparc.addi as $r.result $tag.procedure-tag $r.result)))))
; Initialize data slots in procedure from current registers as specified for
; `lamba' and `lexes'. If there are more data slots than registers, then
; we must generate code to cdr down the list in the last register to obtain
; the rest of the data. The list is expected to have at least the minimal
; length.
;
; The tagged pointer to the procedure is in $r.result.
(define (emit-init-proc-slots! as n)
(define (save-registers lo hi offset)
(do ((lo lo (+ lo 1))
(offset offset (+ offset 4)))
((> lo hi))
(let ((r (force-hwreg! as (regname lo) $r.tmp0)))
(sparc.sti as r offset $r.result))))
(define (save-list lo hi offset)
(emit-load-reg! as $r.reg31 $r.tmp0)
(do ((lo lo (+ lo 1))
(offset offset (+ offset 4)))
((> lo hi))
(sparc.ldi as $r.tmp0 (- $tag.pair-tag) $r.tmp1)
(sparc.sti as $r.tmp1 offset $r.result)
(if (< lo hi)
(begin
(sparc.ldi as $r.tmp0 (+ (- $tag.pair-tag) 4) $r.tmp0)))))
(cond ((< n *lastreg*)
(save-registers 0 n $p.reg0))
(else
(save-registers 0 (- *lastreg* 1) $p.reg0)
(save-list *lastreg* n (+ $p.reg0 (* *lastreg* 4))))))
; BRANCH
(define (emit-branch! as check-timer? label)
(if check-timer?
(check-timer as label label)
(begin (sparc.b as label)
(sparc.slot as))))
; BRANCHF
(define (emit-branchf! as label)
(emit-branchfreg! as $r.result label))
; BRANCHFREG -- introduced by peephole optimization.
(define (emit-branchfreg! as hwreg label)
(sparc.cmpi as hwreg $imm.false)
(sparc.be.a as label)
(sparc.slot as))
; BRANCH-WITH-SETRTN -- introduced by peephole optimization
(define (emit-branch-with-setrtn! as label)
(check-timer0 as)
(sparc.call as label)
(sparc.sti as $r.o7 4 $r.stkp))
; JUMP
;
; Given the finalization order (outer is finalized before inner is assembled)
; the label value will always be available when a jump is assembled. The
; only exception is when m = 0, but does this ever happen? This code handles
; the case anyway.
(define (emit-jump! as m label)
(let* ((r (emit-follow-chain! as m))
(labelv (label-value as label))
(v (if (number? labelv)
(+ labelv $p.codeoffset)
(list '+ label $p.codeoffset))))
(sparc.ldi as r $p.codevector $r.tmp0)
(if (and (number? v) (immediate-literal? v))
(sparc.jmpli as $r.tmp0 v $r.g0)
(begin (emit-immediate->register! as v $r.tmp1)
(sparc.jmplr as $r.tmp0 $r.tmp1 $r.g0)))
(sparc.move as r $r.reg0)))
; .SINGLESTEP
;
; Single step: jump to millicode; pass index of documentation string in
; %TMP0. Some instructions execute when reg0 is not a valid pointer to
; the current procedure (because this is just after returning); in this
; case we restore reg0 from the stack location given by 'funkyloc'.
(define (emit-singlestep-instr! as funky? funkyloc cvlabel)
(if funky?
(sparc.ldi as $r.stkp (+ (thefixnum funkyloc) 12) $r.reg0))
(millicode-call/numarg-in-reg as $m.singlestep
(thefixnum cvlabel)
$r.argreg2))
; Emit the effective address of a label-8 into %o7.
;
; There are multiple ways to do this. If the call causes an expensive
; bubble in the pipeline it is probably much less expensive to grub
; the code vector address out of the procedure in REG0 and calculate it
; that way. FIXME: We need to benchmark these options.
;
; In general the point is moot as the common-case sequence
; setrtn L1
; invoke n
; L1:
; should be peephole-optimized into the obvious fast code.
(define (emit-return-address! as label)
(let* ((loc (here as))
(lloc (label-value as label)))
(define (emit-short val)
(sparc.call as (+ loc 8))
(sparc.addi as $r.o7 val $r.o7))
(define (emit-long val)
; Don't use sparc.set: we need to know that two instructions get
; generated.
(sparc.sethi as `(hi ,val) $r.tmp0)
(sparc.ori as $r.tmp0 `(lo ,val) $r.tmp0)
(sparc.call as (+ loc 16))
(sparc.addr as $r.o7 $r.tmp0 $r.o7))
(cond (lloc
(let ((target-rel-addr (- lloc loc 8)))
(if (immediate-literal? target-rel-addr)
(emit-short target-rel-addr)
(emit-long (- target-rel-addr 8)))))
((short-effective-addresses)
(emit-short `(- ,label ,loc 8)))
(else
(emit-long `(- ,label ,loc 16))))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 22 April 1999 / wdc
;
; SPARC code generation macros for primitives, part 1:
; primitives defined in Compiler/sparc.imp.sch.
; These extend Asm/Common/pass5p1.sch.
(define (operand5 instruction)
(car (cddddr (cdr instruction))))
(define (operand6 instruction)
(cadr (cddddr (cdr instruction))))
(define (operand7 instruction)
(caddr (cddddr (cdr instruction))))
; Primop emitters.
(define (emit-primop.1arg! as op)
((find-primop op) as))
(define (emit-primop.2arg! as op r)
((find-primop op) as r))
(define (emit-primop.3arg! as a1 a2 a3)
((find-primop a1) as a2 a3))
(define (emit-primop.4arg! as a1 a2 a3 a4)
((find-primop a1) as a2 a3 a4))
(define (emit-primop.5arg! as a1 a2 a3 a4 a5)
((find-primop a1) as a2 a3 a4 a5))
(define (emit-primop.6arg! as a1 a2 a3 a4 a5 a6)
((find-primop a1) as a2 a3 a4 a5 a6))
(define (emit-primop.7arg! as a1 a2 a3 a4 a5 a6 a7)
((find-primop a1) as a2 a3 a4 a5 a6 a7))
; Hash table of primops
(define primop-vector (make-vector 256 '()))
(define (define-primop name proc)
(let ((h (logand (symbol-hash name) 255)))
(vector-set! primop-vector h (cons (cons name proc)
(vector-ref primop-vector h)))
name))
(define (find-primop name)
(let ((h (logand (symbol-hash name) 255)))
(cdr (assq name (vector-ref primop-vector h)))))
(define (for-each-primop proc)
(do ((i 0 (+ i 1)))
((= i (vector-length primop-vector)))
(for-each (lambda (p)
(proc (cdr p)))
(vector-ref primop-vector i))))
; Primops
(define-primop 'unspecified
(lambda (as)
(emit-immediate->register! as $imm.unspecified $r.result)))
(define-primop 'undefined
(lambda (as)
(emit-immediate->register! as $imm.undefined $r.result)))
(define-primop 'eof-object
(lambda (as)
(emit-immediate->register! as $imm.eof $r.result)))
(define-primop 'enable-interrupts
(lambda (as)
(millicode-call/0arg as $m.enable-interrupts)))
(define-primop 'disable-interrupts
(lambda (as)
(millicode-call/0arg as $m.disable-interrupts)))
(define-primop 'gc-counter
(lambda (as)
(sparc.ldi as $r.globals $g.gccnt $r.result)))
(define-primop 'zero?
(lambda (as)
(emit-cmp-primop! as sparc.be.a $m.zerop $r.g0)))
(define-primop '=
(lambda (as r)
(emit-cmp-primop! as sparc.be.a $m.numeq r)))
(define-primop '<
(lambda (as r)
(emit-cmp-primop! as sparc.bl.a $m.numlt r)))
(define-primop '<=
(lambda (as r)
(emit-cmp-primop! as sparc.ble.a $m.numle r)))
(define-primop '>
(lambda (as r)
(emit-cmp-primop! as sparc.bg.a $m.numgt r)))
(define-primop '>=
(lambda (as r)
(emit-cmp-primop! as sparc.bge.a $m.numge r)))
(define-primop 'complex?
(lambda (as)
(millicode-call/0arg as $m.complexp)))
(define-primop 'real?
(lambda (as)
(millicode-call/0arg as $m.realp)))
(define-primop 'rational?
(lambda (as)
(millicode-call/0arg as $m.rationalp)))
(define-primop 'integer?
(lambda (as)
(millicode-call/0arg as $m.integerp)))
(define-primop 'exact?
(lambda (as)
(millicode-call/0arg as $m.exactp)))
(define-primop 'inexact?
(lambda (as)
(millicode-call/0arg as $m.inexactp)))
(define-primop 'fixnum?
(lambda (as)
(sparc.btsti as $r.result 3)
(emit-set-boolean! as)))
(define-primop '+
(lambda (as r)
(emit-primop.4arg! as 'internal:+ $r.result r $r.result)))
(define-primop '-
(lambda (as r)
(emit-primop.4arg! as 'internal:- $r.result r $r.result)))
(define-primop '*
(lambda (as rs2)
(emit-multiply-code as rs2 #f)))
(define (emit-multiply-code as rs2 fixnum-arithmetic?)
(if (and (unsafe-code) fixnum-arithmetic?)
(begin
(sparc.srai as $r.result 2 $r.tmp0)
(sparc.smulr as $r.tmp0 rs2 $r.result))
(let ((rs2 (force-hwreg! as rs2 $r.argreg2))
(Lstart (new-label))
(Ltagok (new-label))
(Loflo (new-label))
(Ldone (new-label)))
(sparc.label as Lstart)
(sparc.orr as $r.result rs2 $r.tmp0)
(sparc.btsti as $r.tmp0 3)
(sparc.be.a as Ltagok)
(sparc.srai as $r.result 2 $r.tmp0)
(sparc.label as Loflo)
(if (not (= rs2 $r.argreg2)) (sparc.move as rs2 $r.argreg2))
(if (not fixnum-arithmetic?)
(begin
(millicode-call/ret as $m.multiply Ldone))
(begin
(sparc.set as (thefixnum $ex.fx*) $r.tmp0)
(millicode-call/ret as $m.exception Lstart)))
(sparc.label as Ltagok)
(sparc.smulr as $r.tmp0 rs2 $r.tmp0)
(sparc.rdy as $r.tmp1)
(sparc.srai as $r.tmp0 31 $r.tmp2)
(sparc.cmpr as $r.tmp1 $r.tmp2)
(sparc.bne.a as Loflo)
(sparc.slot as)
(sparc.move as $r.tmp0 $r.result)
(sparc.label as Ldone))))
(define-primop '/
(lambda (as r)
(millicode-call/1arg as $m.divide r)))
(define-primop 'quotient
(lambda (as r)
(millicode-call/1arg as $m.quotient r)))
(define-primop 'remainder
(lambda (as r)
(millicode-call/1arg as $m.remainder r)))
(define-primop '--
(lambda (as)
(emit-negate as $r.result $r.result)))
(define-primop 'round
(lambda (as)
(millicode-call/0arg as $m.round)))
(define-primop 'truncate
(lambda (as)
(millicode-call/0arg as $m.truncate)))
(define-primop 'lognot
(lambda (as)
(if (not (unsafe-code))
(emit-assert-fixnum! as $r.result $ex.lognot))
(sparc.ornr as $r.g0 $r.result $r.result) ; argument order matters
(sparc.xori as $r.result 3 $r.result)))
(define-primop 'logand
(lambda (as x)
(logical-op as $r.result x $r.result sparc.andr $ex.logand)))
(define-primop 'logior
(lambda (as x)
(logical-op as $r.result x $r.result sparc.orr $ex.logior)))
(define-primop 'logxor
(lambda (as x)
(logical-op as $r.result x $r.result sparc.xorr $ex.logxor)))
; Fixnum shifts.
;
; Only positive shifts are meaningful.
; FIXME: These are incompatible with MacScheme and MIT Scheme.
; FIXME: need to return to start of sequence after fault.
(define-primop 'lsh
(lambda (as x)
(emit-shift-operation as $ex.lsh $r.result x $r.result)))
(define-primop 'rshl
(lambda (as x)
(emit-shift-operation as $ex.rshl $r.result x $r.result)))
(define-primop 'rsha
(lambda (as x)
(emit-shift-operation as $ex.rsha $r.result x $r.result)))
; fixnums only.
; FIXME: for symmetry with shifts there should be rotl and rotr (?)
; or perhaps rot should only ever rotate one way.
; FIXME: implement.
(define-primop 'rot
(lambda (as x)
(asm-error "Sparcasm: ROT primop is not implemented.")))
(define-primop 'null?
(lambda (as)
(sparc.cmpi as $r.result $imm.null)
(emit-set-boolean! as)))
(define-primop 'pair?
(lambda (as)
(emit-single-tagcheck->bool! as $tag.pair-tag)))
(define-primop 'eof-object?
(lambda (as)
(sparc.cmpi as $r.result $imm.eof)
(emit-set-boolean! as)))
; Tests the specific representation, not 'flonum or compnum with 0i'.
(define-primop 'flonum?
(lambda (as)
(emit-double-tagcheck->bool! as $tag.bytevector-tag
(+ $imm.bytevector-header
$tag.flonum-typetag))))
(define-primop 'compnum?
(lambda (as)
(emit-double-tagcheck->bool! as $tag.bytevector-tag
(+ $imm.bytevector-header
$tag.compnum-typetag))))
(define-primop 'symbol?
(lambda (as)
(emit-double-tagcheck->bool! as $tag.vector-tag
(+ $imm.vector-header
$tag.symbol-typetag))))
(define-primop 'port?
(lambda (as)
(emit-double-tagcheck->bool! as $tag.vector-tag
(+ $imm.vector-header
$tag.port-typetag))))
(define-primop 'structure?
(lambda (as)
(emit-double-tagcheck->bool! as $tag.vector-tag
(+ $imm.vector-header
$tag.structure-typetag))))
(define-primop 'char?
(lambda (as)
(sparc.andi as $r.result #xFF $r.tmp0)
(sparc.cmpi as $r.tmp0 $imm.character)
(emit-set-boolean! as)))
(define-primop 'string?
(lambda (as)
(emit-double-tagcheck->bool! as
$tag.bytevector-tag
(+ $imm.bytevector-header
$tag.string-typetag))))
(define-primop 'bytevector?
(lambda (as)
(emit-double-tagcheck->bool! as
$tag.bytevector-tag
(+ $imm.bytevector-header
$tag.bytevector-typetag))))
(define-primop 'bytevector-like?
(lambda (as)
(emit-single-tagcheck->bool! as $tag.bytevector-tag)))
(define-primop 'vector?
(lambda (as)
(emit-double-tagcheck->bool! as
$tag.vector-tag
(+ $imm.vector-header
$tag.vector-typetag))))
(define-primop 'vector-like?
(lambda (as)
(emit-single-tagcheck->bool! as $tag.vector-tag)))
(define-primop 'procedure?
(lambda (as)
(emit-single-tagcheck->bool! as $tag.procedure-tag)))
(define-primop 'cons
(lambda (as r)
(emit-primop.4arg! as 'internal:cons $r.result r $r.result)))
(define-primop 'car
(lambda (as)
(emit-primop.3arg! as 'internal:car $r.result $r.result)))
(define-primop 'cdr
(lambda (as)
(emit-primop.3arg! as 'internal:cdr $r.result $r.result)))
(define-primop 'car:pair
(lambda (as)
(sparc.ldi as $r.result (- $tag.pair-tag) $r.result)))
(define-primop 'cdr:pair
(lambda (as)
(sparc.ldi as $r.result (- 4 $tag.pair-tag) $r.result)))
(define-primop 'set-car!
(lambda (as x)
(if (not (unsafe-code))
(emit-single-tagcheck-assert! as $tag.pair-tag $ex.car #f))
(emit-setcar/setcdr! as $r.result x 0)))
(define-primop 'set-cdr!
(lambda (as x)
(if (not (unsafe-code))
(emit-single-tagcheck-assert! as $tag.pair-tag $ex.cdr #f))
(emit-setcar/setcdr! as $r.result x 4)))
; Cells are internal data structures, represented using pairs.
; No error checking is done on cell references.
(define-primop 'make-cell
(lambda (as)
(emit-primop.4arg! as 'internal:cons $r.result $r.g0 $r.result)))
(define-primop 'cell-ref
(lambda (as)
(emit-primop.3arg! as 'internal:cell-ref $r.result $r.result)))
(define-primop 'cell-set!
(lambda (as r)
(emit-setcar/setcdr! as $r.result r 0)))
(define-primop 'syscall
(lambda (as)
(millicode-call/0arg as $m.syscall)))
(define-primop 'break
(lambda (as)
(millicode-call/0arg as $m.break)))
(define-primop 'creg
(lambda (as)
(millicode-call/0arg as $m.creg)))
(define-primop 'creg-set!
(lambda (as)
(millicode-call/0arg as $m.creg-set!)))
(define-primop 'typetag
(lambda (as)
(millicode-call/0arg as $m.typetag)))
(define-primop 'typetag-set!
(lambda (as r)
(millicode-call/1arg as $m.typetag-set r)))
(define-primop 'exact->inexact
(lambda (as)
(millicode-call/0arg as $m.exact->inexact)))
(define-primop 'inexact->exact
(lambda (as)
(millicode-call/0arg as $m.inexact->exact)))
(define-primop 'real-part
(lambda (as)
(millicode-call/0arg as $m.real-part)))
(define-primop 'imag-part
(lambda (as)
(millicode-call/0arg as $m.imag-part)))
(define-primop 'char->integer
(lambda (as)
(if (not (unsafe-code))
(emit-assert-char! as $ex.char2int #f))
(sparc.srli as $r.result 14 $r.result)))
(define-primop 'integer->char
(lambda (as)
(if (not (unsafe-code))
(emit-assert-fixnum! as $r.result $ex.int2char))
(sparc.andi as $r.result #x3FF $r.result)
(sparc.slli as $r.result 14 $r.result)
(sparc.ori as $r.result $imm.character $r.result)))
(define-primop 'not
(lambda (as)
(sparc.cmpi as $r.result $imm.false)
(emit-set-boolean! as)))
(define-primop 'eq?
(lambda (as x)
(emit-primop.4arg! as 'internal:eq? $r.result x $r.result)))
(define-primop 'eqv?
(lambda (as x)
(let ((tmp (force-hwreg! as x $r.tmp0))
(L1 (new-label)))
(sparc.cmpr as $r.result tmp)
(sparc.be.a as L1)
(sparc.set as $imm.true $r.result)
(millicode-call/1arg as $m.eqv tmp)
(sparc.label as L1))))
(define-primop 'make-bytevector
(lambda (as)
(if (not (unsafe-code))
(emit-assert-positive-fixnum! as $r.result $ex.mkbvl))
(emit-allocate-bytevector as
(+ $imm.bytevector-header
$tag.bytevector-typetag)
#f)
(sparc.addi as $r.result $tag.bytevector-tag $r.result)))
(define-primop 'bytevector-fill!
(lambda (as rs2)
(let* ((fault (emit-double-tagcheck-assert! as
$tag.bytevector-tag
(+ $imm.bytevector-header
$tag.bytevector-typetag)
$ex.bvfill
rs2))
(rs2 (force-hwreg! as rs2 $r.argreg2)))
(sparc.btsti as rs2 3)
(sparc.bne as fault)
(sparc.srai as rs2 2 $r.tmp2)
(sparc.ldi as $r.result (- $tag.bytevector-tag) $r.tmp0)
(sparc.addi as $r.result (- 4 $tag.bytevector-tag) $r.tmp1)
(sparc.srai as $r.tmp0 8 $r.tmp0)
(emit-bytevector-fill as $r.tmp0 $r.tmp1 $r.tmp2))))
(define-primop 'bytevector-length
(lambda (as)
(emit-get-length! as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.bytevector-typetag)
$ex.bvlen
$r.result
$r.result)))
(define-primop 'bytevector-like-length
(lambda (as)
(emit-get-length! as
$tag.bytevector-tag
#f
$ex.bvllen
$r.result
$r.result)))
(define-primop 'bytevector-ref
(lambda (as r)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert!
as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.bytevector-typetag)
$ex.bvref
r)
#f)))
(emit-bytevector-like-ref! as $r.result r $r.result fault #f #t))))
(define-primop 'bytevector-like-ref
(lambda (as r)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.bytevector-tag
$ex.bvlref
r)
#f)))
(emit-bytevector-like-ref! as $r.result r $r.result fault #f #f))))
(define-primop 'bytevector-set!
(lambda (as r1 r2)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert!
as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.bytevector-typetag)
$ex.bvset
r1)
#f)))
(emit-bytevector-like-set! as r1 r2 fault #t))))
(define-primop 'bytevector-like-set!
(lambda (as r1 r2)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.bytevector-tag
$ex.bvlset
r1)
#f)))
(emit-bytevector-like-set! as r1 r2 fault #f))))
(define-primop 'sys$bvlcmp
(lambda (as x)
(millicode-call/1arg as $m.bvlcmp x)))
; Strings
; RESULT must have nonnegative fixnum.
; RS2 must have character.
(define-primop 'make-string
(lambda (as rs2)
(let ((FAULT (new-label))
(START (new-label)))
(sparc.label as START)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2)))
(if (not (unsafe-code))
(let ((L1 (new-label))
(L2 (new-label)))
(sparc.tsubrcc as $r.result $r.g0 $r.g0)
(sparc.bvc.a as L1)
(sparc.andi as rs2 255 $r.tmp0)
(sparc.label as FAULT)
(if (not (= rs2 $r.argreg2))
(sparc.move as rs2 $r.argreg2))
(sparc.set as (thefixnum $ex.mkbvl) $r.tmp0) ; Wrong code.
(millicode-call/ret as $m.exception START)
(sparc.label as L1)
(sparc.bl as FAULT)
(sparc.cmpi as $r.tmp0 $imm.character)
(sparc.bne as FAULT)
(sparc.move as $r.result $r.argreg3))
(begin
(sparc.move as $r.result $r.argreg3)))
(emit-allocate-bytevector as
(+ $imm.bytevector-header
$tag.string-typetag)
$r.argreg3)
(sparc.srai as rs2 16 $r.tmp1)
(sparc.addi as $r.result 4 $r.result)
(sparc.srai as $r.argreg3 2 $r.tmp0)
(emit-bytevector-fill as $r.tmp0 $r.result $r.tmp1)
(sparc.addi as $r.result (- $tag.bytevector-tag 4) $r.result)))))
(define-primop 'string-length
(lambda (as)
(emit-primop.3arg! as 'internal:string-length $r.result $r.result)))
(define-primop 'string-ref
(lambda (as r)
(emit-primop.4arg! as 'internal:string-ref $r.result r $r.result)))
(define-primop 'string-set!
(lambda (as r1 r2)
(emit-string-set! as $r.result r1 r2)))
(define-primop 'sys$partial-list->vector
(lambda (as r)
(millicode-call/1arg as $m.partial-list->vector r)))
(define-primop 'make-procedure
(lambda (as)
(emit-make-vector-like! as
'()
$imm.procedure-header
$tag.procedure-tag)))
(define-primop 'make-vector
(lambda (as r)
(emit-make-vector-like! as
r
(+ $imm.vector-header $tag.vector-typetag)
$tag.vector-tag)))
(define-primop 'make-vector:0
(lambda (as r) (make-vector-n as 0 r)))
(define-primop 'make-vector:1
(lambda (as r) (make-vector-n as 1 r)))
(define-primop 'make-vector:2
(lambda (as r) (make-vector-n as 2 r)))
(define-primop 'make-vector:3
(lambda (as r) (make-vector-n as 3 r)))
(define-primop 'make-vector:4
(lambda (as r) (make-vector-n as 4 r)))
(define-primop 'make-vector:5
(lambda (as r) (make-vector-n as 5 r)))
(define-primop 'make-vector:6
(lambda (as r) (make-vector-n as 6 r)))
(define-primop 'make-vector:7
(lambda (as r) (make-vector-n as 7 r)))
(define-primop 'make-vector:8
(lambda (as r) (make-vector-n as 8 r)))
(define-primop 'make-vector:9
(lambda (as r) (make-vector-n as 9 r)))
(define-primop 'vector-length
(lambda (as)
(emit-primop.3arg! as 'internal:vector-length $r.result $r.result)))
(define-primop 'vector-like-length
(lambda (as)
(emit-get-length! as $tag.vector-tag #f $ex.vllen $r.result $r.result)))
(define-primop 'vector-length:vec
(lambda (as)
(emit-get-length-trusted! as $tag.vector-tag $r.result $r.result)))
(define-primop 'procedure-length
(lambda (as)
(emit-get-length! as $tag.procedure-tag #f $ex.plen $r.result $r.result)))
(define-primop 'vector-ref
(lambda (as r)
(emit-primop.4arg! as 'internal:vector-ref $r.result r $r.result)))
(define-primop 'vector-like-ref
(lambda (as r)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.vector-tag
$ex.vlref
r)
#f)))
(emit-vector-like-ref!
as $r.result r $r.result fault $tag.vector-tag #f))))
(define-primop 'vector-ref:trusted
(lambda (as rs2)
(emit-vector-like-ref-trusted!
as $r.result rs2 $r.result $tag.vector-tag)))
(define-primop 'procedure-ref
(lambda (as r)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.procedure-tag
$ex.pref
r)
#f)))
(emit-vector-like-ref!
as $r.result r $r.result fault $tag.procedure-tag #f))))
(define-primop 'vector-set!
(lambda (as r1 r2)
(emit-primop.4arg! as 'internal:vector-set! $r.result r1 r2)))
(define-primop 'vector-like-set!
(lambda (as r1 r2)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.vector-tag
$ex.vlset
r1)
#f)))
(emit-vector-like-set! as $r.result r1 r2 fault $tag.vector-tag #f))))
(define-primop 'vector-set!:trusted
(lambda (as rs2 rs3)
(emit-vector-like-set-trusted! as $r.result rs2 rs3 $tag.vector-tag)))
(define-primop 'procedure-set!
(lambda (as r1 r2)
(let ((fault (if (not (unsafe-code))
(emit-single-tagcheck-assert! as
$tag.procedure-tag
$ex.pset
r1)
#f)))
(emit-vector-like-set! as $r.result r1 r2 fault $tag.procedure-tag #f))))
(define-primop 'char<?
(lambda (as x)
(emit-char-cmp as x sparc.bl.a $ex.char<?)))
(define-primop 'char<=?
(lambda (as x)
(emit-char-cmp as x sparc.ble.a $ex.char<=?)))
(define-primop 'char=?
(lambda (as x)
(emit-char-cmp as x sparc.be.a $ex.char=?)))
(define-primop 'char>?
(lambda (as x)
(emit-char-cmp as x sparc.bg.a $ex.char>?)))
(define-primop 'char>=?
(lambda (as x)
(emit-char-cmp as x sparc.bge.a $ex.char>=?)))
; Experimental (for performance).
; This makes massive assumptions about the layout of the port structure:
; A port is a vector-like where
; #0 = port.input?
; #4 = port.buffer
; #7 = port.rd-lim
; #8 = port.rd-ptr
; See Lib/iosys.sch for more information.
(define-primop 'sys$read-char
(lambda (as)
(let ((Lfinish (new-label))
(Lend (new-label)))
(if (not (unsafe-code))
(begin
(sparc.andi as $r.result $tag.tagmask $r.tmp0) ; mask argument tag
(sparc.cmpi as $r.tmp0 $tag.vector-tag); vector-like?
(sparc.bne as Lfinish) ; skip if not vector-like
(sparc.nop as)
(sparc.ldbi as $r.RESULT 0 $r.tmp1))) ; header byte
(sparc.ldi as $r.RESULT 1 $r.tmp2) ; port.input? or garbage
(if (not (unsafe-code))
(begin
(sparc.cmpi as $r.tmp1 $hdr.port) ; port?
(sparc.bne as Lfinish))) ; skip if not port
(sparc.cmpi as $r.tmp2 $imm.false) ; [slot] input port?
(sparc.be as Lfinish) ; skip if not active port
(sparc.ldi as $r.RESULT (+ 1 32) $r.tmp1) ; [slot] port.rd-ptr
(sparc.ldi as $r.RESULT (+ 1 28) $r.tmp2) ; port.rd-lim
(sparc.ldi as $r.RESULT (+ 1 16) $r.tmp0) ; port.buffer
(sparc.cmpr as $r.tmp1 $r.tmp2) ; rd-ptr < rd-lim?
(sparc.bge as Lfinish) ; skip if rd-ptr >= rd-lim
(sparc.subi as $r.tmp0 1 $r.tmp0) ; [slot] addr of string@0
(sparc.srai as $r.tmp1 2 $r.tmp2) ; rd-ptr as native int
(sparc.ldbr as $r.tmp0 $r.tmp2 $r.tmp2) ; get byte from string
(sparc.addi as $r.tmp1 4 $r.tmp1) ; bump rd-ptr
(sparc.sti as $r.tmp1 (+ 1 32) $r.RESULT) ; store rd-ptr in port
(sparc.slli as $r.tmp2 16 $r.tmp2) ; convert to char #1
(sparc.b as Lend)
(sparc.ori as $r.tmp2 $imm.character $r.RESULT) ; [slot] convert to char
(sparc.label as Lfinish)
(sparc.set as $imm.false $r.RESULT) ; failed
(sparc.label as Lend))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 9 May 1999 / wdc
;
; SPARC code generation macros for primitives, part 2:
; primitives introduced by peephole optimization.
(define-primop 'internal:car
(lambda (as src1 dest)
(internal-primop-invariant2 'internal:car src1 dest)
(if (not (unsafe-code))
(emit-single-tagcheck-assert-reg! as
$tag.pair-tag src1 #f $ex.car))
(sparc.ldi as src1 (- $tag.pair-tag) dest)))
(define-primop 'internal:cdr
(lambda (as src1 dest)
(internal-primop-invariant2 'internal:cdr src1 dest)
(if (not (unsafe-code))
(emit-single-tagcheck-assert-reg! as
$tag.pair-tag src1 #f $ex.cdr))
(sparc.ldi as src1 (- 4 $tag.pair-tag) dest)))
(define-primop 'internal:cell-ref
(lambda (as src1 dest)
(internal-primop-invariant2 'internal:cell-ref src1 dest)
(sparc.ldi as src1 (- $tag.pair-tag) dest)))
(define-primop 'internal:set-car!
(lambda (as rs1 rs2 dest-ignored)
(internal-primop-invariant2 'internal:set-car! rs1 dest-ignored)
(if (not (unsafe-code))
(emit-single-tagcheck-assert-reg! as $tag.pair-tag rs1 rs2 $ex.car))
(emit-setcar/setcdr! as rs1 rs2 0)))
(define-primop 'internal:set-cdr!
(lambda (as rs1 rs2 dest-ignored)
(internal-primop-invariant2 'internal:set-cdr! rs1 dest-ignored)
(if (not (unsafe-code))
(emit-single-tagcheck-assert-reg! as $tag.pair-tag rs1 rs2 $ex.cdr))
(emit-setcar/setcdr! as rs1 rs2 4)))
(define-primop 'internal:cell-set!
(lambda (as rs1 rs2 dest-ignored)
(internal-primop-invariant2 'internal:cell-set! rs1 dest-ignored)
(emit-setcar/setcdr! as rs1 rs2 0)))
; CONS
;
; One instruction reduced here translates into about 2.5KB reduction in the
; size of the basic heap image. :-)
;
; In the out-of-line case, if rd != RESULT then a garbage value is left
; in RESULT, but it always looks like a fixnum, so it's OK.
(define-primop 'internal:cons
(lambda (as rs1 rs2 rd)
(if (inline-allocation)
(let ((ENOUGH-MEMORY (new-label))
(START (new-label)))
(sparc.label as START)
(sparc.addi as $r.e-top 8 $r.e-top)
(sparc.cmpr as $r.e-top $r.e-limit)
(sparc.ble.a as ENOUGH-MEMORY)
(sparc.sti as rs1 -8 $r.e-top)
(millicode-call/ret as $m.gc START)
(sparc.label as ENOUGH-MEMORY)
(sparc.sti as (force-hwreg! as rs2 $r.tmp0) -4 $r.e-top)
(sparc.subi as $r.e-top (- 8 $tag.pair-tag) rd))
(begin
(if (= rs1 $r.result)
(sparc.move as $r.result $r.argreg2))
(millicode-call/numarg-in-result as $m.alloc 8)
(if (= rs1 $r.result)
(sparc.sti as $r.argreg2 0 $r.result)
(sparc.sti as rs1 0 $r.result))
(sparc.sti as (force-hwreg! as rs2 $r.tmp1) 4 $r.result)
(sparc.addi as $r.result $tag.pair-tag rd)))))
(define-primop 'internal:car:pair
(lambda (as src1 dest)
(internal-primop-invariant2 'internal:car src1 dest)
(sparc.ldi as src1 (- $tag.pair-tag) dest)))
(define-primop 'internal:cdr:pair
(lambda (as src1 dest)
(internal-primop-invariant2 'internal:cdr src1 dest)
(sparc.ldi as src1 (- 4 $tag.pair-tag) dest)))
; Vector operations.
(define-primop 'internal:vector-length
(lambda (as rs rd)
(internal-primop-invariant2 'internal:vector-length rs rd)
(emit-get-length! as
$tag.vector-tag
(+ $imm.vector-header $tag.vector-typetag)
$ex.vlen
rs
rd)))
(define-primop 'internal:vector-ref
(lambda (as rs1 rs2 rd)
(internal-primop-invariant2 'internal:vector-ref rs1 rd)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert-reg/reg!
as
$tag.vector-tag
(+ $imm.vector-header $tag.vector-typetag)
rs1
rs2
$ex.vref))))
(emit-vector-like-ref! as rs1 rs2 rd fault $tag.vector-tag #t))))
(define-primop 'internal:vector-ref/imm
(lambda (as rs1 imm rd)
(internal-primop-invariant2 'internal:vector-ref/imm rs1 rd)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert-reg/imm!
as
$tag.vector-tag
(+ $imm.vector-header $tag.vector-typetag)
rs1
imm
$ex.vref))))
(emit-vector-like-ref/imm! as rs1 imm rd fault $tag.vector-tag #t))))
(define-primop 'internal:vector-set!
(lambda (as rs1 rs2 rs3)
(internal-primop-invariant1 'internal:vector-set! rs1)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert-reg/reg!
as
$tag.vector-tag
(+ $imm.vector-header $tag.vector-typetag)
rs1
rs2
$ex.vset))))
(emit-vector-like-set! as rs1 rs2 rs3 fault $tag.vector-tag #t))))
(define-primop 'internal:vector-length:vec
(lambda (as rs1 dst)
(internal-primop-invariant2 'internal:vector-length:vec rs1 dst)
(emit-get-length-trusted! as $tag.vector-tag rs1 dst)))
(define-primop 'internal:vector-ref:trusted
(lambda (as rs1 rs2 dst)
(emit-vector-like-ref-trusted! as rs1 rs2 dst $tag.vector-tag)))
(define-primop 'internal:vector-set!:trusted
(lambda (as rs1 rs2 rs3)
(emit-vector-like-ref-trusted! as rs1 rs2 rs3 $tag.vector-tag)))
; Strings.
(define-primop 'internal:string-length
(lambda (as rs rd)
(internal-primop-invariant2 'internal:string-length rs rd)
(emit-get-length! as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.string-typetag)
$ex.slen
rs
rd)))
(define-primop 'internal:string-ref
(lambda (as rs1 rs2 rd)
(internal-primop-invariant2 'internal:string-ref rs1 rd)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert-reg/reg!
as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.string-typetag)
rs1
rs2
$ex.sref))))
(emit-bytevector-like-ref! as rs1 rs2 rd fault #t #t))))
(define-primop 'internal:string-ref/imm
(lambda (as rs1 imm rd)
(internal-primop-invariant2 'internal:string-ref/imm rs1 rd)
(let ((fault (if (not (unsafe-code))
(emit-double-tagcheck-assert-reg/imm!
as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.string-typetag)
rs1
imm
$ex.sref))))
(emit-bytevector-like-ref/imm! as rs1 imm rd fault #t #t))))
(define-primop 'internal:string-set!
(lambda (as rs1 rs2 rs3)
(internal-primop-invariant1 'internal:string-set! rs1)
(emit-string-set! as rs1 rs2 rs3)))
(define-primop 'internal:+
(lambda (as src1 src2 dest)
(internal-primop-invariant2 'internal:+ src1 dest)
(emit-arith-primop! as sparc.taddrcc sparc.subr $m.add src1 src2 dest #t)))
(define-primop 'internal:+/imm
(lambda (as src1 imm dest)
(internal-primop-invariant2 'internal:+/imm src1 dest)
(emit-arith-primop! as sparc.taddicc sparc.subi $m.add src1 imm dest #f)))
(define-primop 'internal:-
(lambda (as src1 src2 dest)
(internal-primop-invariant2 'internal:- src1 dest)
(emit-arith-primop! as sparc.tsubrcc sparc.addr $m.subtract
src1 src2 dest #t)))
(define-primop 'internal:-/imm
(lambda (as src1 imm dest)
(internal-primop-invariant2 'internal:-/imm src1 dest)
(emit-arith-primop! as sparc.tsubicc sparc.addi $m.subtract
src1 imm dest #f)))
(define-primop 'internal:--
(lambda (as rs rd)
(internal-primop-invariant2 'internal:-- rs rd)
(emit-negate as rs rd)))
(define-primop 'internal:branchf-null?
(lambda (as reg label)
(internal-primop-invariant1 'internal:branchf-null? reg)
(sparc.cmpi as reg $imm.null)
(sparc.bne.a as label)
(sparc.slot as)))
(define-primop 'internal:branchf-pair?
(lambda (as reg label)
(internal-primop-invariant1 'internal:branchf-pair? reg)
(sparc.andi as reg $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 $tag.pair-tag)
(sparc.bne.a as label)
(sparc.slot as)))
(define-primop 'internal:branchf-zero?
(lambda (as reg label)
(internal-primop-invariant1 'internal:brancf-zero? reg)
(emit-bcmp-primop! as sparc.bne.a reg $r.g0 label $m.zerop #t)))
(define-primop 'internal:branchf-eof-object?
(lambda (as rs label)
(internal-primop-invariant1 'internal:branchf-eof-object? rs)
(sparc.cmpi as rs $imm.eof)
(sparc.bne.a as label)
(sparc.slot as)))
(define-primop 'internal:branchf-fixnum?
(lambda (as rs label)
(internal-primop-invariant1 'internal:branchf-fixnum? rs)
(sparc.btsti as rs 3)
(sparc.bne.a as label)
(sparc.slot as)))
(define-primop 'internal:branchf-char?
(lambda (as rs label)
(internal-primop-invariant1 'internal:branchf-char? rs)
(sparc.andi as rs 255 $r.tmp0)
(sparc.cmpi as $r.tmp0 $imm.character)
(sparc.bne.a as label)
(sparc.slot as)))
(define-primop 'internal:branchf-=
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-= src1)
(emit-bcmp-primop! as sparc.bne.a src1 src2 label $m.numeq #t)))
(define-primop 'internal:branchf-<
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-< src1)
(emit-bcmp-primop! as sparc.bge.a src1 src2 label $m.numlt #t)))
(define-primop 'internal:branchf-<=
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-<= src1)
(emit-bcmp-primop! as sparc.bg.a src1 src2 label $m.numle #t)))
(define-primop 'internal:branchf->
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-> src1)
(emit-bcmp-primop! as sparc.ble.a src1 src2 label $m.numgt #t)))
(define-primop 'internal:branchf->=
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf->= src1)
(emit-bcmp-primop! as sparc.bl.a src1 src2 label $m.numge #t)))
(define-primop 'internal:branchf-=/imm
(lambda (as src1 imm label)
(internal-primop-invariant1 'internal:branchf-=/imm src1)
(emit-bcmp-primop! as sparc.bne.a src1 imm label $m.numeq #f)))
(define-primop 'internal:branchf-</imm
(lambda (as src1 imm label)
(internal-primop-invariant1 'internal:branchf-</imm src1)
(emit-bcmp-primop! as sparc.bge.a src1 imm label $m.numlt #f)))
(define-primop 'internal:branchf-<=/imm
(lambda (as src1 imm label)
(internal-primop-invariant1 'internal:branchf-<=/imm src1)
(emit-bcmp-primop! as sparc.bg.a src1 imm label $m.numle #f)))
(define-primop 'internal:branchf->/imm
(lambda (as src1 imm label)
(internal-primop-invariant1 'internal:branchf->/imm src1)
(emit-bcmp-primop! as sparc.ble.a src1 imm label $m.numgt #f)))
(define-primop 'internal:branchf->=/imm
(lambda (as src1 imm label)
(internal-primop-invariant1 'internal:branchf->=/imm src1)
(emit-bcmp-primop! as sparc.bl.a src1 imm label $m.numge #f)))
(define-primop 'internal:branchf-char=?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-char=? src1)
(emit-char-bcmp-primop! as sparc.bne.a src1 src2 label $ex.char=?)))
(define-primop 'internal:branchf-char<=?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-char<=? src1)
(emit-char-bcmp-primop! as sparc.bg.a src1 src2 label $ex.char<=?)))
(define-primop 'internal:branchf-char<?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-char<? src1)
(emit-char-bcmp-primop! as sparc.bge.a src1 src2 label $ex.char<?)))
(define-primop 'internal:branchf-char>=?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-char>=? src1)
(emit-char-bcmp-primop! as sparc.bl.a src1 src2 label $ex.char>=?)))
(define-primop 'internal:branchf-char>?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-char>=? src1)
(emit-char-bcmp-primop! as sparc.ble.a src1 src2 label $ex.char>?)))
(define-primop 'internal:branchf-char=?/imm
(lambda (as src imm label)
(internal-primop-invariant1 'internal:branchf-char=?/imm src)
(emit-char-bcmp-primop! as sparc.bne.a src imm label $ex.char=?)))
(define-primop 'internal:branchf-char>=?/imm
(lambda (as src imm label)
(internal-primop-invariant1 'internal:branchf-char>=?/imm src)
(emit-char-bcmp-primop! as sparc.bl.a src imm label $ex.char>=?)))
(define-primop 'internal:branchf-char>?/imm
(lambda (as src imm label)
(internal-primop-invariant1 'internal:branchf-char>?/imm src)
(emit-char-bcmp-primop! as sparc.ble.a src imm label $ex.char>?)))
(define-primop 'internal:branchf-char<=?/imm
(lambda (as src imm label)
(internal-primop-invariant1 'internal:branchf-char<=?/imm src)
(emit-char-bcmp-primop! as sparc.bg.a src imm label $ex.char<=?)))
(define-primop 'internal:branchf-char<?/imm
(lambda (as src imm label)
(internal-primop-invariant1 'internal:branchf-char<?/imm src)
(emit-char-bcmp-primop! as sparc.bge.a src imm label $ex.char<?)))
(define-primop 'internal:eq?
(lambda (as src1 src2 dest)
(internal-primop-invariant2 'internal:eq? src1 dest)
(let ((tmp (force-hwreg! as src2 $r.tmp0)))
(sparc.cmpr as src1 tmp)
(emit-set-boolean-reg! as dest))))
(define-primop 'internal:eq?/imm
(lambda (as rs imm rd)
(internal-primop-invariant2 'internal:eq?/imm rs rd)
(cond ((fixnum? imm) (sparc.cmpi as rs (thefixnum imm)))
((eq? imm #t) (sparc.cmpi as rs $imm.true))
((eq? imm #f) (sparc.cmpi as rs $imm.false))
((null? imm) (sparc.cmpi as rs $imm.null))
(else ???))
(emit-set-boolean-reg! as rd)))
(define-primop 'internal:branchf-eq?
(lambda (as src1 src2 label)
(internal-primop-invariant1 'internal:branchf-eq? src1)
(let ((src2 (force-hwreg! as src2 $r.tmp0)))
(sparc.cmpr as src1 src2)
(sparc.bne.a as label)
(sparc.slot as))))
(define-primop 'internal:branchf-eq?/imm
(lambda (as rs imm label)
(internal-primop-invariant1 'internal:branchf-eq?/imm rs)
(cond ((fixnum? imm) (sparc.cmpi as rs (thefixnum imm)))
((eq? imm #t) (sparc.cmpi as rs $imm.true))
((eq? imm #f) (sparc.cmpi as rs $imm.false))
((null? imm) (sparc.cmpi as rs $imm.null))
(else ???))
(sparc.bne.a as label)
(sparc.slot as)))
; Unary predicates followed by a check.
(define-primop 'internal:check-fixnum?
(lambda (as src L1 liveregs)
(sparc.btsti as src 3)
(emit-checkcc! as sparc.bne L1 liveregs)))
(define-primop 'internal:check-pair?
(lambda (as src L1 liveregs)
(sparc.andi as src $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 $tag.pair-tag)
(emit-checkcc! as sparc.bne L1 liveregs)))
(define-primop 'internal:check-vector?
(lambda (as src L1 liveregs)
(sparc.andi as src $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 $tag.vector-tag)
(sparc.bne as L1)
(sparc.nop as)
(sparc.ldi as src (- $tag.vector-tag) $r.tmp0)
(sparc.andi as $r.tmp0 255 $r.tmp1)
(sparc.cmpi as $r.tmp1 $imm.vector-header)
(emit-checkcc! as sparc.bne L1 liveregs)))
(define-primop 'internal:check-vector?/vector-length:vec
(lambda (as src dst L1 liveregs)
(sparc.andi as src $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 $tag.vector-tag)
(sparc.bne as L1)
(sparc.nop as)
(sparc.ldi as src (- $tag.vector-tag) $r.tmp0)
(sparc.andi as $r.tmp0 255 $r.tmp1)
(sparc.cmpi as $r.tmp1 $imm.vector-header)
(sparc.bne as L1)
(apply sparc.slot2 as liveregs)
(sparc.srli as $r.tmp0 8 dst)))
(define (internal-primop-invariant2 name a b)
(if (not (and (hardware-mapped? a) (hardware-mapped? b)))
(asm-error "SPARC assembler internal invariant violated by " name
" on operands " a " and " b)))
(define (internal-primop-invariant1 name a)
(if (not (hardware-mapped? a))
(asm-error "SPARC assembler internal invariant violated by " name
" on operand " a)))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; SPARC code generation macros for primitives, part 3a:
; helper procedures for scalars.
; LOGAND, LOGIOR, LOGXOR: logical operations on fixnums.
;
; Input: Registers rs1 and rs2, both of which can be general registers.
; In addition, rs1 can be RESULT, and rs2 can be ARGREG2.
; Output: Register dest, which can be a general register or RESULT.
(define (logical-op as rs1 rs2 dest op excode)
(define (fail rs1 rs2 L0)
(if (not (= rs1 $r.result)) (sparc.move as rs1 $r.result))
(if (not (= rs2 $r.argreg2)) (sparc.move as rs2 $r.argreg2))
(sparc.set as (thefixnum excode) $r.tmp0)
(millicode-call/ret as $m.exception L0))
(let ((L0 (new-label))
(L1 (new-label)))
(sparc.label as L0)
(let ((rs1 (force-hwreg! as rs1 $r.result))
(rs2 (force-hwreg! as rs2 $r.argreg2))
(u (unsafe-code))
(d (hardware-mapped? dest)))
(cond ((and u d)
(op as rs1 rs2 dest))
((and u (not d))
(op as rs1 rs2 $r.tmp0)
(emit-store-reg! as $r.tmp0 dest))
((and (not u) d)
(sparc.orr as rs1 rs2 $r.tmp0)
(sparc.btsti as $r.tmp0 3)
(sparc.bz.a as L1)
(op as rs1 rs2 dest)
(fail rs1 rs2 L0)
(sparc.label as L1))
(else
(sparc.orr as rs1 rs2 $r.tmp0)
(sparc.btsti as $r.tmp0 3)
(sparc.bz.a as L1)
(op as rs1 rs2 $r.tmp0)
(fail rs1 rs2 L0)
(sparc.label as L1)
(emit-store-reg! as $r.tmp0 dest))))))
; LSH, RSHA, RSHL: Bitwise shifts on fixnums.
;
; Notes for future contemplation:
; - The semantics do not match those of MIT Scheme or MacScheme: only
; positive shifts are allowed.
; - The names do not match the fixnum-specific procedures of Chez Scheme
; that have the same semantics: fxsll, fxsra, fxsrl.
; - This code checks that the second argument is in range; if it did
; not, then we could get a MOD for free. Probably too hardware-dependent
; to worry about.
; - The range 0..31 for the shift count is curious given that the fixnum
; is 30-bit.
(define (emit-shift-operation as exn rs1 rs2 rd)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2)))
(if (not (unsafe-code))
(let ((L0 (new-label))
(FAULT (new-label))
(START (new-label)))
(sparc.label as START)
(sparc.btsti as rs1 3) ; RS1 fixnum?
(sparc.be.a as L0)
(sparc.andi as rs2 #x7c $r.g0) ; RS2 fixnum and 0 <= RS2 < 32?
(sparc.label as FAULT)
(if (not (= rs1 $r.result))
(sparc.move as rs1 $r.result))
(if (not (= rs2 $r.argreg2))
(emit-move2hwreg! as rs2 $r.argreg2))
(sparc.set as (thefixnum exn) $r.tmp0)
(millicode-call/ret as $m.exception START)
(sparc.label as L0)
(sparc.bne as FAULT)
(sparc.srai as rs2 2 $r.tmp1))
(begin
(sparc.srai as rs2 2 $r.tmp1)))
(cond ((= exn $ex.lsh)
(sparc.sllr as rs1 $r.tmp1 rd))
((= exn $ex.rshl)
(sparc.srlr as rs1 $r.tmp1 rd)
(sparc.andni as rd 3 rd))
((= exn $ex.rsha)
(sparc.srar as rs1 $r.tmp1 rd)
(sparc.andni as rd 3 rd))
(else ???))))
; Set result on condition code.
;
; The processor's zero bit has been affected by a previous instruction.
; If the bit is set, store #t in RESULT, otherwise store #f in RESULT.
(define (emit-set-boolean! as)
(emit-set-boolean-reg! as $r.result))
; Set on condition code.
;
; The processor's zero bit has been affected by a previous instruction.
; If the bit is set, store #t in the processor register 'dest', otherwise
; store #f in 'dest'.
(define (emit-set-boolean-reg! as dest)
(let ((L1 (new-label)))
(sparc.set as $imm.true dest)
(sparc.bne.a as L1)
(sparc.set as $imm.false dest)
(sparc.label as L1)))
; Representation predicate.
(define (emit-single-tagcheck->bool! as tag)
(sparc.andi as $r.result $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 tag)
(emit-set-boolean! as))
(define (emit-single-tagcheck-assert! as tag1 excode reg2)
(emit-single-tagcheck-assert-reg! as tag1 $r.result reg2 excode))
(define (emit-single-tagcheck-assert-reg! as tag1 reg reg2 excode)
(let ((L0 (new-label))
(L1 (new-label))
(FAULT (new-label)))
(sparc.label as L0)
(sparc.andi as reg $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 tag1)
(fault-if-ne as excode #f #f reg reg2 L0)))
; Assert that a machine register has a fixnum in it.
; Returns the label of the fault code.
(define (emit-assert-fixnum! as reg excode)
(let ((L0 (new-label))
(L1 (new-label))
(FAULT (new-label)))
(sparc.label as L0)
(sparc.btsti as reg 3)
(fault-if-ne as excode #f #f reg #f L0)))
; Assert that RESULT has a character in it.
; Returns the label of the fault code.
(define (emit-assert-char! as excode fault-label)
(let ((L0 (new-label))
(L1 (new-label))
(FAULT (new-label)))
(sparc.label as L0)
(sparc.andi as $r.result #xFF $r.tmp0)
(sparc.cmpi as $r.tmp0 $imm.character)
(fault-if-ne as excode #f fault-label #f #f L0)))
; Generate code for fault handling if the zero flag is not set.
; - excode is the nativeint exception code.
; - cont-label, if not #f, is the label to go to if there is no fault.
; - fault-label, if not #f, is the label of an existing fault handler.
; - reg1, if not #f, is the number of a register which must be
; moved into RESULT before the fault handler is called.
; - reg2, if not #f, is the number of a register which must be moved
; into ARGREG2 before the fault handler is called.
; - ret-label, if not #f, is the return address to be set up before calling
; the fault handler.
;
; Ret-label and fault-label cannot simultaneously be non-#f; in this case
; the ret-label is ignored (since the existing fault handler most likely
; sets up the return in the desired manner).
(define (fault-if-ne as excode cont-label fault-label reg1 reg2 ret-label)
(if fault-label
(begin
(if (and reg2 (not (= reg2 $r.argreg2)))
(emit-move2hwreg! as reg2 $r.argreg2))
(sparc.bne as fault-label)
(if (and reg1 (not (= reg1 $r.result)))
(sparc.move as reg1 $r.result)
(sparc.nop as))
fault-label)
(let ((FAULT (new-label))
(L1 (new-label)))
(sparc.be.a as (or cont-label L1))
(sparc.slot as)
(sparc.label as FAULT)
(if (and reg1 (not (= reg1 $r.result)))
(sparc.move as reg1 $r.result))
(if (and reg2 (not (= reg2 $r.argreg2)))
(emit-move2hwreg! as reg2 $r.argreg2))
(sparc.set as (thefixnum excode) $r.tmp0)
(millicode-call/ret as $m.exception (or ret-label L1))
(if (or (not cont-label) (not ret-label))
(sparc.label as L1))
FAULT)))
; This is more expensive than what is good for it (5 cycles in the usual case),
; but there does not seem to be a better way.
(define (emit-assert-positive-fixnum! as reg excode)
(let ((L1 (new-label))
(L2 (new-label))
(L3 (new-label)))
(sparc.label as L2)
(sparc.tsubrcc as reg $r.g0 $r.g0)
(sparc.bvc as L1)
(sparc.nop as)
(sparc.label as L3)
(if (not (= reg $r.result))
(sparc.move as reg $r.result))
(sparc.set as (thefixnum excode) $r.tmp0)
(millicode-call/ret as $m.exception l2)
(sparc.label as L1)
(sparc.bl as L3)
(sparc.nop as)
L3))
; Arithmetic comparison with boolean result.
(define (emit-cmp-primop! as branch_t.a generic r)
(let ((Ltagok (new-label))
(Lcont (new-label))
(r (force-hwreg! as r $r.argreg2)))
(sparc.tsubrcc as $r.result r $r.g0)
(sparc.bvc.a as Ltagok)
(sparc.set as $imm.false $r.result)
(if (not (= r $r.argreg2))
(sparc.move as r $r.argreg2))
(millicode-call/ret as generic Lcont)
(sparc.label as Ltagok)
(branch_t.a as Lcont)
(sparc.set as $imm.true $r.result)
(sparc.label as Lcont)))
; Arithmetic comparison and branch.
;
; This code does not use the chained branch trick (DCTI) that was documented
; in the Sparc v8 manual and deprecated in the v9 manual. This code executes
; _much_ faster on the Ultra than the code using DCTI, even though it executes
; the same instructions.
;
; Parameters and preconditions.
; Src1 is a general register, RESULT, ARGREG2, or ARGREG3.
; Src2 is a general register, RESULT, ARGREG2, ARGREG3, or an immediate.
; Src2 is an immediate iff src2isreg = #f.
; Branch_f.a is a branch on condition code that branches if the condition
; is not true.
; Generic is the millicode table offset of the generic procedure.
(define (emit-bcmp-primop! as branch_f.a src1 src2 Lfalse generic src2isreg)
(let ((Ltagok (new-label))
(Ltrue (new-label))
(op2 (if src2isreg
(force-hwreg! as src2 $r.tmp1)
(thefixnum src2)))
(sub (if src2isreg sparc.tsubrcc sparc.tsubicc))
(mov (if src2isreg sparc.move sparc.set)))
(sub as src1 op2 $r.g0)
(sparc.bvc.a as Ltagok)
(sparc.slot as)
; Not both fixnums.
; Must move src1 to result if src1 is not result.
; Must move src2 to argreg2 if src2 is not argreg2.
(let ((move-res (not (= src1 $r.result)))
(move-arg2 (or (not src2isreg) (not (= op2 $r.argreg2)))))
(if (and move-arg2 move-res)
(mov as op2 $r.argreg2))
(sparc.jmpli as $r.millicode generic $r.o7)
(cond (move-res (sparc.move as src1 $r.result))
(move-arg2 (mov as op2 $r.argreg2))
(else (sparc.nop as)))
(sparc.cmpi as $r.result $imm.false)
(sparc.bne.a as Ltrue)
(sparc.slot as)
(sparc.b as Lfalse)
(sparc.slot as))
(sparc.label as Ltagok)
(branch_f.a as Lfalse)
(sparc.slot as)
(sparc.label as Ltrue)))
; Generic arithmetic for + and -.
; Some rules:
; We have two HW registers src1 and dest.
; If src2isreg is #t then src2 may be a HW reg or a SW reg
; If src2isreg is #f then src2 is an immediate fixnum, not shifted.
; Src1 and dest may be RESULT, but src2 may not.
; Src2 may be ARGREG2, the others may not.
;
; FIXME! This is incomprehensible.
; New code below.
'(define (emit-arith-primop! as op invop generic src1 src2 dest src2isreg)
(let ((L1 (new-label))
(op2 (if src2isreg
(force-hwreg! as src2 $r.tmp1)
(thefixnum src2))))
(if (and src2isreg (= op2 dest))
(begin (op as src1 op2 $r.tmp0)
(sparc.bvc.a as L1)
(sparc.move as $r.tmp0 dest))
(begin (op as src1 op2 dest)
(sparc.bvc.a as L1)
(sparc.slot as)
(invop as dest op2 dest)))
(let ((n (+ (if (not (= src1 $r.result)) 1 0)
(if (or (not src2isreg) (not (= op2 $r.argreg2))) 1 0)))
(mov2 (if src2isreg sparc.move sparc.set)))
(if (= n 2)
(mov2 as op2 $r.argreg2))
(sparc.jmpli as $r.millicode generic $r.o7)
(cond ((= n 0) (sparc.nop as))
((= n 1) (mov2 as op2 $r.argreg2))
(else (sparc.move as src1 $r.result)))
; Generic arithmetic leaves stuff in RESULT, must move to dest if
; dest is not RESULT.
(if (not (= dest $r.result))
(sparc.move as $r.result dest))
(sparc.label as L1))))
; Comprehensible, but longer.
;
; Important to be careful not to clobber arguments, and not to leave garbage
; in rd, if millicode is called.
;
; op is the appropriate operation.
; invop is the appropriate inverse operation.
; RS1 can be any general hw register or RESULT.
; RS2/IMM can be any general register or ARGREG2 (op2isreg=#t), or
; an immediate (op2isreg=#f)
; RD can be any general hw register or RESULT.
;
; FIXME: split this into two procedures.
(define (emit-arith-primop! as op invop generic rs1 rs2/imm rd op2isreg)
(let ((L1 (new-label)))
(if op2isreg
(let ((rs2 (force-hwreg! as rs2/imm $r.argreg2)))
(cond ((or (= rs1 rs2 rd)
(and (= rs2 rd)
(= generic $m.subtract)))
(op as rs1 rs2 $r.tmp0)
(sparc.bvc.a as L1)
(sparc.move as $r.tmp0 rd))
((= rs1 rd)
(op as rs1 rs2 rs1)
(sparc.bvc.a as L1)
(sparc.slot as)
(invop as rs1 rs2 rs1))
((= rs2 rd)
(op as rs1 rs2 rs2)
(sparc.bvc.a as L1)
(sparc.slot as)
(invop as rs2 rs1 rs2))
(else
(op as rs1 rs2 rd)
(sparc.bvc.a as L1)
(sparc.slot as)
(if (and (not (= rd $r.result)) (not (= rd $r.argreg2)))
(sparc.clr as rd))))
(cond ((and (= rs1 $r.result) (= rs2 $r.argreg2))
;; Could peephole the INVOP or CLR into the slot here.
(millicode-call/0arg as generic))
((= rs1 $r.result)
(millicode-call/1arg as generic rs2))
((= rs2 $r.argreg2)
(millicode-call/1arg-in-result as generic rs1))
(else
(sparc.move as rs2 $r.argreg2)
(millicode-call/1arg-in-result as generic rs1))))
(let ((imm (thefixnum rs2/imm)))
(op as rs1 imm rd)
(sparc.bvc.a as L1)
(sparc.slot as)
(invop as rd imm rd)
(if (not (= rs1 $r.result))
(sparc.move as rs1 $r.result))
(millicode-call/numarg-in-reg as generic imm $r.argreg2)))
(if (not (= rd $r.result))
(sparc.move as $r.result rd))
(sparc.label as L1)))
; Important to be careful not to leave garbage in rd if millicode is called.
(define (emit-negate as rs rd)
(let ((L1 (new-label)))
(cond ((= rs rd)
(sparc.tsubrcc as $r.g0 rs rs)
(sparc.bvc.a as L1)
(sparc.slot as)
(if (= rs $r.result)
(begin
(sparc.jmpli as $r.millicode $m.negate $r.o7)
(sparc.subr as $r.g0 $r.result $r.result))
(begin
(sparc.subr as $r.g0 rs rs)
(sparc.jmpli as $r.millicode $m.negate $r.o7)
(sparc.move as rs $r.result))))
(else
(sparc.tsubrcc as $r.g0 rs rd)
(sparc.bvc.a as L1)
(sparc.slot as)
(cond ((= rs $r.result)
(sparc.jmpli as $r.millicode $m.negate $r.o7)
(sparc.clr as rd))
((= rd $r.result)
(sparc.jmpli as $r.millicode $m.negate $r.o7)
(sparc.move as rs $r.result))
(else
(sparc.clr as rd)
(sparc.jmpli as $r.millicode $m.negate $r.o7)
(sparc.move as rs $r.result)))))
(if (not (= rd $r.result))
(sparc.move as $r.result rd))
(sparc.label as L1)))
; Character comparison.
; r is a register or a character constant.
(define (emit-char-cmp as r btrue.a excode)
(emit-charcmp! as (lambda ()
(let ((l2 (new-label)))
(sparc.set as $imm.false $r.result)
(btrue.a as L2)
(sparc.set as $imm.true $r.result)
(sparc.label as L2)))
$r.result
r
excode))
; op1 is a hw register
; op2 is a register or a character constant
(define (emit-char-bcmp-primop! as bfalse.a op1 op2 L0 excode)
(emit-charcmp! as (lambda ()
(bfalse.a as L0)
(sparc.slot as))
op1
op2
excode))
; We check the tags of both by xoring them and seeing if the low byte is 0.
; If so, then we can subtract one from the other (tag and all) and check the
; condition codes.
;
; The branch-on-true instruction must have the annull bit set. (???)
;
; op1 is a hw register
; op2 is a register or a character constant.
(define (emit-charcmp! as tail op1 op2 excode)
(let ((op2 (if (char? op2)
op2
(force-hwreg! as op2 $r.argreg2))))
(cond ((not (unsafe-code))
(let ((L0 (new-label))
(L1 (new-label))
(FAULT (new-label)))
(sparc.label as L0)
(cond ((char? op2)
(sparc.xori as op1 $imm.character $r.tmp0)
(sparc.btsti as $r.tmp0 #xFF)
(sparc.srli as op1 16 $r.tmp0)
(sparc.be.a as L1)
(sparc.cmpi as $r.tmp0 (char->integer op2)))
(else
(sparc.andi as op1 #xFF $r.tmp0)
(sparc.andi as op2 #xFF $r.tmp1)
(sparc.cmpr as $r.tmp0 $r.tmp1)
(sparc.bne as FAULT)
(sparc.cmpi as $r.tmp0 $imm.character)
(sparc.be.a as L1)
(sparc.cmpr as op1 op2)))
(sparc.label as FAULT)
(if (not (eqv? op1 $r.result))
(sparc.move as op1 $r.result))
(cond ((char? op2)
(emit-immediate->register! as
(char->immediate op2)
$r.argreg2))
((not (eqv? op2 $r.argreg2))
(sparc.move as op2 $r.argreg2)))
(sparc.set as (thefixnum excode) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1)))
((not (char? op2))
(sparc.cmpr as op1 op2))
(else
(sparc.srli as op1 16 $r.tmp0)
(sparc.cmpi as $r.tmp0 (char->integer op2))))
(tail)))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; SPARC code generation macros for primitives, part 3b:
; helper procedures for data structures.
; SET-CAR!, SET-CDR!, CELL-SET!
;
; Input: RS1: a hardware register; has pair pointer (tag check must be
; performed by the caller).
; RS2: any register; has value to store.
; Output: None.
;
; Having rs1 != RESULT is pretty silly with the current write barrier
; but will be less silly with the new barrier.
(define (emit-setcar/setcdr! as rs1 rs2 offs)
(cond ((and (write-barrier) (hardware-mapped? rs2))
(sparc.sti as rs2 (- offs $tag.pair-tag) rs1)
(if (not (= rs1 $r.result))
(sparc.move as rs1 $r.result))
(millicode-call/1arg as $m.addtrans rs2))
((write-barrier)
(emit-move2hwreg! as rs2 $r.argreg2)
(sparc.sti as $r.argreg2 (- offs $tag.pair-tag) rs1)
(millicode-call/1arg-in-result as $m.addtrans rs1))
((hardware-mapped? rs2)
(sparc.sti as rs2 (- offs $tag.pair-tag) rs1))
(else
(emit-move2hwreg! as rs2 $r.argreg2)
(sparc.sti as $r.argreg2 (- offs $tag.pair-tag) rs1))))
; Representation predicate.
;
; RESULT has an object. If the tag of RESULT is 'tag1' and the
; header byte of the object is 'tag2' then set RESULT to #t, else
; set it to #f.
(define (emit-double-tagcheck->bool! as tag1 tag2)
(let ((L1 (new-label)))
(sparc.andi as $r.result $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 tag1)
(sparc.bne.a as L1)
(sparc.set as $imm.false $r.result)
(sparc.ldbi as $r.result (+ (- tag1) 3) $r.tmp0)
(sparc.set as $imm.true $r.result)
(sparc.cmpi as $r.tmp0 tag2)
(sparc.bne.a as L1)
(sparc.set as $imm.false $r.result)
(sparc.label as L1)))
; Check structure tag.
;
; RS1 has an object. If the tag of RS1 is not 'tag1', or if the tag is
; 'tag1' but the header byte of the object header is not 'tag2', then an
; exception with code 'excode' is signalled. The exception call is set
; up to return to the first instruction of the emitted code.
;
; If RS1 is not RESULT then it is moved to RESULT before the exception
; is signalled.
;
; If RS2/IMM is not #f, then it is a register or immediate that is moved
; to ARGREG2 before the exception is signalled; it is an immediate iff
; imm? = #t.
;
; RS1 must be a hardware register.
; RS2/IMM is a general register, ARGREG2, an immediate, or #f.
; RS3 is a general register, ARGREG3, or #f.
;
; The procedure returns the label of the fault address. If the execution
; falls off the end of the emitted instruction sequence, then the following
; are true:
; - the tag of the object in RS1 was 'tag1' and its header byte was 'tag2'
; - the object header word is in TMP0.
(define (double-tagcheck-assert as tag1 tag2 rs1 rs2/imm rs3 excode imm?)
(let ((L0 (new-label))
(L1 (new-label))
(FAULT (new-label)))
(sparc.label as L0)
(sparc.andi as rs1 $tag.tagmask $r.tmp0)
(sparc.cmpi as $r.tmp0 tag1)
(sparc.be.a as L1)
(sparc.ldi as rs1 (- tag1) $r.tmp0)
(sparc.label as FAULT)
(if (not (= rs1 $r.result))
(sparc.move as rs1 $r.result))
(if rs2/imm
(cond (imm?
(sparc.set as (thefixnum rs2/imm) $r.argreg2))
((= rs2/imm $r.argreg2))
(else
(emit-move2hwreg! as rs2/imm $r.argreg2))))
(if (and rs3 (not (= rs3 $r.argreg3)))
(emit-move2hwreg! as rs3 $r.argreg3))
(sparc.set as (thefixnum excode) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1)
(sparc.andi as $r.tmp0 255 $r.tmp1)
(sparc.cmpi as $r.tmp1 tag2)
(sparc.bne.a as FAULT)
(sparc.slot as)
FAULT))
(define (emit-double-tagcheck-assert! as tag1 tag2 excode reg2)
(double-tagcheck-assert as tag1 tag2 $r.result reg2 #f excode #f))
(define (emit-double-tagcheck-assert-reg/reg! as tag1 tag2 rs1 rs2 excode)
(double-tagcheck-assert as tag1 tag2 rs1 rs2 #f excode #f))
(define (emit-double-tagcheck-assert-reg/imm! as tag1 tag2 rs1 imm excode)
(double-tagcheck-assert as tag1 tag2 rs1 imm #f excode #t))
; Get the length of a vector or bytevector structure, with tag checking
; included.
;
; Input: RS and RD are both hardware registers.
(define (emit-get-length! as tag1 tag2 excode rs rd)
(if (not (unsafe-code))
(if tag2
(emit-double-tagcheck-assert-reg/reg! as tag1 tag2 rs rd excode)
(emit-single-tagcheck-assert-reg! as tag1 rs rd excode)))
(emit-get-length-trusted! as tag1 rs rd))
; Get the length of a vector or bytevector structure, without tag checking.
;
; Input: RS and RD are both hardware registers.
(define (emit-get-length-trusted! as tag1 rs rd)
(sparc.ldi as rs (- tag1) $r.tmp0)
(sparc.srli as $r.tmp0 8 rd)
(if (= tag1 $tag.bytevector-tag)
(sparc.slli as rd 2 rd)))
; Allocate a bytevector, leave untagged pointer in RESULT.
(define (emit-allocate-bytevector as hdr preserved-result)
; Preserve the length field, then calculate the number of words
; to allocate. The value `28' is an adjustment of 3 (for rounding
; up) plus another 4 bytes for the header, all represented as a fixnum.
(if (not preserved-result)
(sparc.move as $r.result $r.argreg2))
(sparc.addi as $r.result 28 $r.result)
(sparc.andi as $r.result (asm:signed #xFFFFFFF0) $r.result)
; Allocate space
(sparc.jmpli as $r.millicode $m.alloc-bv $r.o7)
(sparc.srai as $r.result 2 $r.result)
; Setup the header.
(if (not preserved-result)
(sparc.slli as $r.argreg2 6 $r.tmp0)
(sparc.slli as preserved-result 6 $r.tmp0))
(sparc.addi as $r.tmp0 hdr $r.tmp0)
(sparc.sti as $r.tmp0 0 $r.result))
; Given a nativeint count, a pointer to the first element of a
; bytevector-like structure, and a byte value, fill the bytevector
; with the byte value.
(define (emit-bytevector-fill as r-bytecount r-pointer r-value)
(let ((L2 (new-label))
(L1 (new-label)))
(sparc.label as L2)
(sparc.deccc as r-bytecount)
(sparc.bge.a as L2)
(sparc.stbr as r-value r-bytecount r-pointer)
(sparc.label as L1)))
; BYTEVECTOR-REF, BYTEVECTOR-LIKE-REF, STRING-REF.
;
; The pointer in RS1 is known to be bytevector-like. RS2 is the fixnum
; index into the structure. Get the RS2'th element and place it in RD.
;
; RS1 and RD are hardware registers.
; RS2 is a general register or ARGREG2.
; 'fault' is defined iff (unsafe-code) = #f
; header is in TMP0 iff (unsafe-code) = #f and 'header-loaded?' = #t
; if 'charize?' is #t then store result as char, otherwise as fixnum.
(define (emit-bytevector-like-ref! as rs1 rs2 rd fault charize? header-loaded?)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2)))
(if (not (unsafe-code))
(begin
; check that index is fixnum
(sparc.btsti as rs2 3)
(sparc.bne as fault)
(if (not header-loaded?)
(sparc.ldi as rs1 (- $tag.bytevector-tag) $r.tmp0))
; check length
(sparc.srai as rs2 2 $r.tmp1)
(sparc.srli as $r.tmp0 8 $r.tmp0)
(sparc.cmpr as $r.tmp0 $r.tmp1)
(sparc.bleu as fault)
; No NOP or SLOT -- the SUBI below goes into the slot.
)
(begin
(sparc.srai as rs2 2 $r.tmp1)))
; Pointer is in RS1.
; Shifted index is in TMP1.
(sparc.addi as rs1 (- 4 $tag.bytevector-tag) $r.tmp0)
(sparc.ldbr as $r.tmp0 $r.tmp1 $r.tmp0)
(if (not charize?)
(sparc.slli as $r.tmp0 2 rd)
(begin (sparc.slli as $r.tmp0 16 rd)
(sparc.ori as rd $imm.character rd)))))
; As above, but RS2 is replaced by an immediate, IMM.
;
; The immediate, represented as a fixnum, is guaranteed fit in the
; instruction's immediate field.
(define (emit-bytevector-like-ref/imm! as rs1 imm rd fault charize?
header-loaded?)
(if (not (unsafe-code))
(begin
(if (not header-loaded?)
(sparc.ldi as rs1 (- $tag.bytevector-tag) $r.tmp0))
; Range check.
(sparc.srli as $r.tmp0 8 $r.tmp0)
(sparc.cmpi as $r.tmp0 imm)
(sparc.bleu.a as fault)
(sparc.slot as)))
; Pointer is in RS1.
(let ((adjusted-offset (+ (- 4 $tag.bytevector-tag) imm)))
(if (immediate-literal? adjusted-offset)
(begin
(sparc.ldbi as rs1 adjusted-offset $r.tmp0))
(begin
(sparc.addi as rs1 (- 4 $tag.bytevector-tag) $r.tmp0)
(sparc.ldbr as $r.tmp0 imm $r.tmp0)))
(if (not charize?)
(sparc.slli as $r.tmp0 2 rd)
(begin (sparc.slli as $r.tmp0 16 rd)
(sparc.ori as rd $imm.character rd)))))
; BYTEVECTOR-SET!, BYTEVECTOR-LIKE-SET!
;
; Input: RESULT -- a pointer to a bytevector-like structure.
; TMP0 -- the header iff (unsafe-code) = #f and header-loaded? = #t
; IDX -- a register that holds the second argument
; BYTE -- a register that holds the third argument
; Output: Nothing.
;
; 'Fault' is the address of the error code iff (unsafe-code) = #f
;
; FIXME:
; - Argument values passed to error handler appear to be bogus
; (error message is very strange).
; - There's no check that the value actually fits in a byte.
; - Uses ARGREG3 and and TMP2.
(define (emit-bytevector-like-set! as idx byte fault header-loaded?)
(let ((r1 (force-hwreg! as idx $r.tmp1))
(r2 (force-hwreg! as byte $r.argreg3)))
(if (not (unsafe-code))
(begin
(if (not header-loaded?)
(sparc.ldi as $r.result (- $tag.bytevector-tag) $r.tmp0))
; Both index and byte must be fixnums.
; Can't use tsubcc because the computation may really overflow.
(sparc.orr as r1 r2 $r.tmp2)
(sparc.btsti as $r.tmp2 3)
(sparc.bnz as fault)
; No NOP -- next instruction is OK in slot.
; Index must be in range.
(sparc.srli as $r.tmp0 8 $r.tmp0) ; limit - in slot
(sparc.srai as r1 2 $r.tmp1) ; index
(sparc.cmpr as $r.tmp1 $r.tmp0)
(sparc.bgeu as fault)
; No NOP -- next instruction is OK in slot.
)
(begin
(sparc.srai as r1 2 $r.tmp1)))
(sparc.srli as r2 2 $r.tmp0)
; Using ARGREG2 as the destination is OK because the resulting pointer
; value always looks like a fixnum. By doing so, we avoid needing TMP2.
(sparc.addi as $r.result (- 4 $tag.bytevector-tag) $r.argreg2)
(sparc.stbr as $r.tmp0 $r.tmp1 $r.argreg2)))
; STRING-SET!
(define (emit-string-set! as rs1 rs2 rs3)
(let* ((rs2 (force-hwreg! as rs2 $r.argreg2))
(rs3 (force-hwreg! as rs3 $r.argreg3))
(FAULT (if (not (unsafe-code))
(double-tagcheck-assert
as
$tag.bytevector-tag
(+ $imm.bytevector-header $tag.string-typetag)
rs1 rs2 rs3
$ex.sset
#f))))
; Header is in TMP0; TMP1 and TMP2 are free.
(if (not (unsafe-code))
(begin
; RS2 must be a fixnum.
(sparc.btsti as rs2 3)
(sparc.bne as FAULT)
; Index (in RS2) must be valid; header is in tmp0.
(sparc.srli as $r.tmp0 8 $r.tmp0) ; limit
(sparc.srai as rs2 2 $r.tmp1) ; index
(sparc.cmpr as $r.tmp1 $r.tmp0)
(sparc.bgeu as FAULT)
; RS3 must be a character.
(sparc.andi as rs3 #xFF $r.tmp0)
(sparc.cmpi as $r.tmp0 $imm.character)
(sparc.bne as FAULT)
; No NOP -- the SRLI below goes in the slot
)
(begin
(sparc.srai as rs2 2 $r.tmp1)))
; tmp1 has nativeint index.
; rs3/argreg3 has character.
; tmp0 is garbage.
(sparc.subi as $r.tmp1 (- $tag.bytevector-tag 4) $r.tmp1)
(sparc.srli as rs3 16 $r.tmp0)
(sparc.stbr as $r.tmp0 rs1 $r.tmp1)))
; VECTORS and PROCEDURES
; Allocate short vectors of known length; faster than the general case.
; FIXME: can also allocate in-line.
(define (make-vector-n as length r)
(sparc.jmpli as $r.millicode $m.alloc $r.o7)
(sparc.set as (thefixnum (+ length 1)) $r.result)
(emit-immediate->register! as (+ (* 256 (thefixnum length))
$imm.vector-header
$tag.vector-typetag)
$r.tmp0)
(sparc.sti as $r.tmp0 0 $r.result)
(let ((dest (force-hwreg! as r $r.argreg2)))
(do ((i 0 (+ i 1)))
((= i length))
(sparc.sti as dest (* (+ i 1) 4) $r.result)))
(sparc.addi as $r.result $tag.vector-tag $r.result))
; emit-make-vector-like! assumes argreg3 is not destroyed by alloci.
; FIXME: bug: $ex.mkvl is not right if the operation is make-procedure
; or make-vector.
(define (emit-make-vector-like! as r hdr ptrtag)
(let ((FAULT (emit-assert-positive-fixnum! as $r.result $ex.mkvl)))
(sparc.move as $r.result $r.argreg3)
(sparc.addi as $r.result 4 $r.result)
(sparc.jmpli as $r.millicode $m.alloci $r.o7)
(if (null? r)
(sparc.set as $imm.null $r.argreg2)
(emit-move2hwreg! as r $r.argreg2))
(sparc.slli as $r.argreg3 8 $r.tmp0)
(sparc.addi as $r.tmp0 hdr $r.tmp0)
(sparc.sti as $r.tmp0 0 $r.result)
(sparc.addi as $r.result ptrtag $r.result)))
; VECTOR-REF, VECTOR-LIKE-REF, PROCEDURE-REF
;
; FAULT is valid iff (unsafe-code) = #f
; Header is in TMP0 iff (unsafe-code) = #f and header-loaded? = #t.
(define (emit-vector-like-ref! as rs1 rs2 rd FAULT tag header-loaded?)
(let ((index (force-hwreg! as rs2 $r.argreg2)))
(if (not (unsafe-code))
(begin
(if (not header-loaded?)
(sparc.ldi as rs1 (- tag) $r.tmp0))
; Index must be fixnum.
(sparc.btsti as index 3)
(sparc.bne as FAULT)
; Index must be within bounds.
(sparc.srai as $r.tmp0 8 $r.tmp0)
(sparc.cmpr as $r.tmp0 index)
(sparc.bleu as FAULT)
; No NOP; the following instruction is valid in the slot.
))
(emit-vector-like-ref-trusted! as rs1 index rd tag)))
(define (emit-vector-like-ref-trusted! as rs1 rs2 rd tag)
(let ((index (force-hwreg! as rs2 $r.argreg2)))
(sparc.addi as rs1 (- 4 tag) $r.tmp0)
(sparc.ldr as $r.tmp0 index rd)))
; VECTOR-REF/IMM, VECTOR-LIKE-REF/IMM, PROCEDURE-REF/IMM
;
; 'rs1' is a hardware register containing a vectorish pointer (to a
; vector-like or procedure).
; 'imm' is a fixnum s.t. (immediate-literal? imm) => #t.
; 'rd' is a hardware register.
; 'FAULT' is the label of the error code iff (unsafe-code) => #f
; 'tag' is the tag of the pointer in rs1.
; 'header-loaded?' is #t iff the structure header word is in $r.tmp0.
(define (emit-vector-like-ref/imm! as rs1 imm rd FAULT tag header-loaded?)
(if (not (unsafe-code))
(begin
(if (not header-loaded?) (sparc.ldi as rs1 (- tag) $r.tmp0))
; Check bounds.
(sparc.srai as $r.tmp0 10 $r.tmp0)
(sparc.cmpi as $r.tmp0 imm)
(sparc.bleu as FAULT)
(sparc.nop as)))
(emit-vector-like-ref/imm-trusted! as rs1 imm rd tag))
; 'rs1' is a hardware register containing a vectorish pointer (to a
; vector-like or procedure).
; 'imm' is a fixnum s.t. (immediate-literal? imm) => #t.
; 'rd' is a hardware register.
; 'tag' is the tag of the pointer in rs1.
(define (emit-vector-like-ref/imm-trusted! as rs1 imm rd tag)
(let* ((offset (* imm 4)) ; words->bytes
(adjusted-offset (+ (- 4 tag) offset)))
(if (immediate-literal? adjusted-offset)
(begin
(sparc.ldi as rs1 adjusted-offset rd))
(begin
(sparc.addi as rs1 (- 4 tag) $r.tmp0)
(sparc.ldi as $r.tmp0 offset rd)))))
; VECTOR-SET!, VECTOR-LIKE-SET!, PROCEDURE-SET!
;
; It is assumed that the pointer in RESULT is valid. We must check the index
; in register x for validity and then perform the side effect (by calling
; millicode). The tag is the pointer tag to be adjusted for.
;
; The use of vector-set is ok even if it is a procedure.
; fault is valid iff (unsafe-code) = #f
; header is in tmp0 iff (unsafe-code) = #f and header-loaded? = #t
(define (emit-vector-like-set! as rs1 rs2 rs3 fault tag header-loaded?)
(let ((rs2 (force-hwreg! as rs2 $r.tmp1))
(rs3 (force-hwreg! as rs3 $r.argreg2)))
(if (not (unsafe-code))
(begin
(if (not header-loaded?)
(sparc.ldi as $r.result (- tag) $r.tmp0))
(sparc.btsti as rs2 3)
(sparc.bne as fault)
(sparc.srai as $r.tmp0 8 $r.tmp0)
(sparc.cmpr as $r.tmp0 rs2)
(sparc.bleu as fault)))
(emit-vector-like-set-trusted! as rs1 rs2 rs3 tag)))
; rs1 must be a hardware register.
; tag is the pointer tag to be adjusted for.
(define (emit-vector-like-set-trusted! as rs1 rs2 rs3 tag)
(let ((rs2 (force-hwreg! as rs2 $r.tmp1))
(rs3 (force-hwreg! as rs3 $r.argreg2)))
;; The ADDR can go in the delay slot of a preceding BLEU.
(sparc.addr as rs1 rs2 $r.tmp0)
(cond ((not (write-barrier))
(sparc.sti as rs3 (- 4 tag) $r.tmp0))
((= rs1 $r.result)
(cond ((= rs3 $r.argreg2)
(sparc.jmpli as $r.millicode $m.addtrans $r.o7)
(sparc.sti as rs3 (- 4 tag) $r.tmp0))
(else
(sparc.sti as rs3 (- 4 tag) $r.tmp0)
(millicode-call/1arg as $m.addtrans rs3))))
(else
(cond ((= rs3 $r.argreg2)
(sparc.sti as rs3 (- 4 tag) $r.tmp0)
(millicode-call/1arg-in-result as $m.addtrans rs1))
(else
(sparc.sti as rs3 (- 4 tag) $r.tmp0)
(sparc.move as rs1 $r.result)
(millicode-call/1arg as $m.addtrans rs3)))))))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; 9 May 1999 / wdc
;
; SPARC code generation macros for primitives, part 3:
; fixnum-specific operations.
;
; Constraints for all the primops.
;
; RS1 is a general hardware register or RESULT.
; RS2 is a general register or ARGREG2.
; IMM is an exact integer in the range -1024 .. 1023.
; RD is a general hardware register or RESULT.
; FIXME
; Missing fxquotient, fxremainder
; When new pass1 in place:
; Must add code to pass1 to allow n-ary calls to be rewritten as binary
; Must add compiler macro for fxabs.
; most-negative-fixnum, most-positive-fixnum.
(define-primop 'most-negative-fixnum
(lambda (as)
(emit-immediate->register! as (asm:signed #x80000000) $r.result)))
(define-primop 'most-positive-fixnum
(lambda (as)
(emit-immediate->register! as (asm:signed #x7FFFFFFC) $r.result)))
; fx+, fx- w/o immediates
(define-primop 'fx+
(lambda (as rs2)
(emit-fixnum-arithmetic as sparc.taddrcc sparc.addr $r.result rs2 $r.result
$ex.fx+)))
(define-primop 'internal:fx+
(lambda (as rs1 rs2 rd)
(emit-fixnum-arithmetic as sparc.taddrcc sparc.addr rs1 rs2 rd $ex.fx+)))
(define-primop 'fx-
(lambda (as rs2)
(emit-fixnum-arithmetic as sparc.tsubrcc sparc.subr $r.result rs2 $r.result
$ex.fx-)))
(define-primop 'internal:fx-
(lambda (as rs1 rs2 rd)
(emit-fixnum-arithmetic as sparc.tsubrcc sparc.subr rs1 rs2 rd $ex.fx-)))
(define-primop 'fx--
(lambda (as)
(emit-fixnum-arithmetic as sparc.tsubrcc sparc.subr
$r.g0 $r.result $r.result $ex.fx--)))
(define-primop 'internal:fx--
(lambda (as rs rd)
(emit-fixnum-arithmetic as sparc.tsubrcc sparc.subr $r.g0 rs rd $ex.fx--)))
(define (emit-fixnum-arithmetic as op-check op-nocheck rs1 rs2 rd exn)
(if (unsafe-code)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2)))
(op-nocheck as rs1 rs2 rd))
(let ((rs2 (force-hwreg! as rs2 $r.argreg2))
(L0 (new-label))
(L1 (new-label)))
(sparc.label as L0)
(op-check as rs1 rs2 $r.tmp0)
(sparc.bvc.a as L1)
(sparc.move as $r.tmp0 rd)
(if (not (= exn $ex.fx--))
(begin
(if (not (= rs1 $r.result)) (sparc.move as rs1 $r.result))
(if (not (= rs2 $r.argreg2)) (sparc.move as rs2 $r.argreg2)))
(begin
(if (not (= rs2 $r.result)) (sparc.move as rs2 $r.result))))
(sparc.set as (thefixnum exn) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1))))
; fx* w/o immediate
(define-primop 'fx*
(lambda (as rs2)
(emit-multiply-code as rs2 #t)))
; fx+, fx- w/immediates
(define-primop 'internal:fx+/imm
(lambda (as rs imm rd)
(emit-fixnum-arithmetic/imm as sparc.taddicc sparc.addi
rs imm rd $ex.fx+)))
(define-primop 'internal:fx-/imm
(lambda (as rs imm rd)
(emit-fixnum-arithmetic/imm as sparc.tsubicc sparc.subi
rs imm rd $ex.fx-)))
(define (emit-fixnum-arithmetic/imm as op-check op-nocheck rs imm rd exn)
(if (unsafe-code)
(op-nocheck as rs (thefixnum imm) rd)
(let ((L0 (new-label))
(L1 (new-label)))
(sparc.label as L0)
(op-check as rs (thefixnum imm) $r.tmp0)
(sparc.bvc.a as L1)
(sparc.move as $r.tmp0 rd)
(if (not (= rs $r.result)) (sparc.move as rs $r.result))
(sparc.set as (thefixnum imm) $r.argreg2)
(sparc.set as (thefixnum exn) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1))))
; fx=, fx<, fx<=, fx>, fx>=, fxpositive?, fxnegative?, fxzero? w/o immediates
(define-primop 'fx=
(lambda (as rs2)
(emit-fixnum-compare as sparc.bne.a $r.result rs2 $r.result $ex.fx= #f)))
(define-primop 'fx<
(lambda (as rs2)
(emit-fixnum-compare as sparc.bge.a $r.result rs2 $r.result $ex.fx< #f)))
(define-primop 'fx<=
(lambda (as rs2)
(emit-fixnum-compare as sparc.bg.a $r.result rs2 $r.result $ex.fx<= #f)))
(define-primop 'fx>
(lambda (as rs2)
(emit-fixnum-compare as sparc.ble.a $r.result rs2 $r.result $ex.fx> #f)))
(define-primop 'fx>=
(lambda (as rs2)
(emit-fixnum-compare as sparc.bl.a $r.result rs2 $r.result $ex.fx>= #f)))
(define-primop 'internal:fx=
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare as sparc.bne.a rs1 rs2 rd $ex.fx= #f)))
(define-primop 'internal:fx<
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare as sparc.bge.a rs1 rs2 rd $ex.fx< #f)))
(define-primop 'internal:fx<=
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare as sparc.bg.a rs1 rs2 rd $ex.fx<= #f)))
(define-primop 'internal:fx>
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare as sparc.ble.a rs1 rs2 rd $ex.fx> #f)))
(define-primop 'internal:fx>=
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare as sparc.bl.a rs1 rs2 rd $ex.fx>= #f)))
; Use '/imm' code for these because the generated code is better.
(define-primop 'fxpositive?
(lambda (as)
(emit-fixnum-compare/imm as sparc.ble.a $r.result 0 $r.result
$ex.fxpositive? #f)))
(define-primop 'fxnegative?
(lambda (as)
(emit-fixnum-compare/imm as sparc.bge.a $r.result 0 $r.result
$ex.fxnegative? #f)))
(define-primop 'fxzero?
(lambda (as)
(emit-fixnum-compare/imm as sparc.bne.a $r.result 0 $r.result
$ex.fxzero? #f)))
(define-primop 'internal:fxpositive?
(lambda (as rs rd)
(emit-fixnum-compare/imm as sparc.ble.a rs 0 rd $ex.fxpositive? #f)))
(define-primop 'internal:fxnegative?
(lambda (as rs rd)
(emit-fixnum-compare/imm as sparc.bge.a rs 0 rd $ex.fxnegative? #f)))
(define-primop 'internal:fxzero?
(lambda (as rs rd)
(emit-fixnum-compare/imm as sparc.bne.a rs 0 rd $ex.fxzero? #f)))
; fx=, fx<, fx<=, fx>, fx>= w/immediates
(define-primop 'internal:fx=/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm as sparc.bne.a rs imm rd $ex.fx= #f)))
(define-primop 'internal:fx</imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm as sparc.bge.a rs imm rd $ex.fx< #f)))
(define-primop 'internal:fx<=/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm as sparc.bg.a rs imm rd $ex.fx<= #f)))
(define-primop 'internal:fx>/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm as sparc.ble.a rs imm rd $ex.fx> #f)))
(define-primop 'internal:fx>=/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm as sparc.bl.a rs imm rd $ex.fx>= #f)))
; fx=, fx<, fx<=, fx>, fx>=, fxpositive?, fxnegative?, fxzero? w/o immediates
; for control.
(define-primop 'internal:branchf-fx=
(lambda (as rs1 rs2 L)
(emit-fixnum-compare as sparc.bne.a rs1 rs2 #f $ex.fx= L)))
(define-primop 'internal:branchf-fx<
(lambda (as rs1 rs2 L)
(emit-fixnum-compare as sparc.bge.a rs1 rs2 #f $ex.fx< L)))
(define-primop 'internal:branchf-fx<=
(lambda (as rs1 rs2 L)
(emit-fixnum-compare as sparc.bg.a rs1 rs2 #f $ex.fx<= L)))
(define-primop 'internal:branchf-fx>
(lambda (as rs1 rs2 L)
(emit-fixnum-compare as sparc.ble.a rs1 rs2 #f $ex.fx> L)))
(define-primop 'internal:branchf-fx>=
(lambda (as rs1 rs2 L)
(emit-fixnum-compare as sparc.bl.a rs1 rs2 #f $ex.fx>= L)))
(define-primop 'internal:branchf-fxpositive?
(lambda (as rs1 L)
(emit-fixnum-compare/imm as sparc.ble.a rs1 0 #f $ex.fxpositive? L)))
(define-primop 'internal:branchf-fxnegative?
(lambda (as rs1 L)
(emit-fixnum-compare/imm as sparc.bge.a rs1 0 #f $ex.fxnegative? L)))
(define-primop 'internal:branchf-fxzero?
(lambda (as rs1 L)
(emit-fixnum-compare/imm as sparc.bne.a rs1 0 #f $ex.fxzero? L)))
; fx=, fx<, fx<=, fx>, fx>= w/immediates for control.
(define-primop 'internal:branchf-fx=/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm as sparc.bne.a rs imm #f $ex.fx= L)))
(define-primop 'internal:branchf-fx</imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm as sparc.bge.a rs imm #f $ex.fx< L)))
(define-primop 'internal:branchf-fx<=/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm as sparc.bg.a rs imm #f $ex.fx<= L)))
(define-primop 'internal:branchf-fx>/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm as sparc.ble.a rs imm #f $ex.fx> L)))
(define-primop 'internal:branchf-fx>=/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm as sparc.bl.a rs imm #f $ex.fx>= L)))
; Trusted fixnum comparisons.
(define-primop '=:fix:fix
(lambda (as rs2)
(emit-fixnum-compare-trusted as sparc.bne.a $r.result rs2 $r.result #f)))
(define-primop '<:fix:fix
(lambda (as rs2)
(emit-fixnum-compare-trusted as sparc.bge.a $r.result rs2 $r.result #f)))
(define-primop '<=:fix:fix
(lambda (as rs2)
(emit-fixnum-compare-trusted as sparc.bg.a $r.result rs2 $r.result #f)))
(define-primop '>:fix:fix
(lambda (as rs2)
(emit-fixnum-compare-trusted as sparc.ble.a $r.result rs2 $r.result #f)))
(define-primop '>=:fix:fix
(lambda (as rs2)
(emit-fixnum-compare-trusted as sparc.bl.a $r.result rs2 $r.result #f)))
(define-primop 'internal:=:fix:fix
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare-trusted as sparc.bne.a rs1 rs2 rd #f)))
(define-primop 'internal:<:fix:fix
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare-trusted as sparc.bge.a rs1 rs2 rd #f)))
(define-primop 'internal:<=:fix:fix
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare-trusted as sparc.bg.a rs1 rs2 rd #f)))
(define-primop 'internal:>:fix:fix
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare-trusted as sparc.ble.a rs1 rs2 rd #f)))
(define-primop 'internal:>=:fix:fix
(lambda (as rs1 rs2 rd)
(emit-fixnum-compare-trusted as sparc.bl.a rs1 rs2 rd #f)))
; With immediates.
(define-primop 'internal:=:fix:fix/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm-trusted as sparc.bne.a rs imm rd #f)))
(define-primop 'internal:<:fix:fix/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm-trusted as sparc.bge.a rs imm rd #f)))
(define-primop 'internal:<=:fix:fix/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm-trusted as sparc.bg.a rs imm rd #f)))
(define-primop 'internal:>:fix:fix/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm-trusted as sparc.ble.a rs imm rd #f)))
(define-primop 'internal:>=:fix:fix/imm
(lambda (as rs imm rd)
(emit-fixnum-compare/imm-trusted as sparc.bl.a rs imm rd #f)))
; Without immediates, for control.
(define-primop 'internal:branchf-=:fix:fix
(lambda (as rs1 rs2 L)
(emit-fixnum-compare-trusted as sparc.bne.a rs1 rs2 #f L)))
(define-primop 'internal:branchf-<:fix:fix
(lambda (as rs1 rs2 L)
(emit-fixnum-compare-trusted as sparc.bge.a rs1 rs2 #f L)))
(define-primop 'internal:branchf-<=:fix:fix
(lambda (as rs1 rs2 L)
(emit-fixnum-compare-trusted as sparc.bg.a rs1 rs2 #f L)))
(define-primop 'internal:branchf->:fix:fix
(lambda (as rs1 rs2 L)
(emit-fixnum-compare-trusted as sparc.ble.a rs1 rs2 #f L)))
(define-primop 'internal:branchf->=:fix:fix
(lambda (as rs1 rs2 L)
(emit-fixnum-compare-trusted as sparc.bl.a rs1 rs2 #f L)))
; With immediates, for control.
(define-primop 'internal:branchf-=:fix:fix/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm-trusted as sparc.bne.a rs imm #f L)))
(define-primop 'internal:branchf-<:fix:fix/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm-trusted as sparc.bge.a rs imm #f L)))
(define-primop 'internal:branchf-<=:fix:fix/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm-trusted as sparc.bg.a rs imm #f L)))
(define-primop 'internal:branchf->:fix:fix/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm-trusted as sparc.ble.a rs imm #f L)))
(define-primop 'internal:branchf->=:fix:fix/imm
(lambda (as rs imm L)
(emit-fixnum-compare/imm-trusted as sparc.bl.a rs imm #f L)))
; Range check: 0 <= src1 < src2
(define-primop 'internal:check-range
(lambda (as src1 src2 L1 livregs)
(let ((src2 (force-hwreg! as src2 $r.argreg2)))
(emit-fixnum-compare-check
as src2 src1 sparc.bleu L1 livregs))))
; Trusted fixnum comparisons followed by a check.
(define-primop 'internal:check-=:fix:fix
(lambda (as src1 src2 L1 liveregs)
(emit-fixnum-compare-check
as src1 src2 sparc.bne L1 liveregs)))
(define-primop 'internal:check-<:fix:fix
(lambda (as src1 src2 L1 liveregs)
(emit-fixnum-compare-check
as src1 src2 sparc.bge L1 liveregs)))
(define-primop 'internal:check-<=:fix:fix
(lambda (as src1 src2 L1 liveregs)
(emit-fixnum-compare-check
as src1 src2 sparc.bg L1 liveregs)))
(define-primop 'internal:check->:fix:fix
(lambda (as src1 src2 L1 liveregs)
(emit-fixnum-compare-check
as src1 src2 sparc.ble L1 liveregs)))
(define-primop 'internal:check->=:fix:fix
(lambda (as src1 src2 L1 liveregs)
(emit-fixnum-compare-check
as src1 src2 sparc.bl L1 liveregs)))
(define-primop 'internal:check-=:fix:fix/imm
(lambda (as src1 imm L1 liveregs)
(emit-fixnum-compare/imm-check
as src1 imm sparc.bne L1 liveregs)))
(define-primop 'internal:check-<:fix:fix/imm
(lambda (as src1 imm L1 liveregs)
(emit-fixnum-compare/imm-check
as src1 imm sparc.bge L1 liveregs)))
(define-primop 'internal:check-<=:fix:fix/imm
(lambda (as src1 imm L1 liveregs)
(emit-fixnum-compare/imm-check
as src1 imm sparc.bg L1 liveregs)))
(define-primop 'internal:check->:fix:fix/imm
(lambda (as src1 imm L1 liveregs)
(emit-fixnum-compare/imm-check
as src1 imm sparc.ble L1 liveregs)))
(define-primop 'internal:check->=:fix:fix/imm
(lambda (as src1 imm L1 liveregs)
(emit-fixnum-compare/imm-check
as src1 imm sparc.bl L1 liveregs)))
; Below, 'target' is a label or #f. If #f, RD must be a general hardware
; register or RESULT, and a boolean result is generated in RD.
(define (emit-fixnum-compare as branchf.a rs1 rs2 rd exn target)
(if (unsafe-code)
(emit-fixnum-compare-trusted as branchf.a rs1 rs2 rd target)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2))
(L0 (new-label))
(L1 (new-label)))
(sparc.label as L0)
(sparc.orr as rs1 rs2 $r.tmp0)
(sparc.btsti as $r.tmp0 3)
(sparc.be.a as L1)
(sparc.cmpr as rs1 rs2)
(if (not (= rs1 $r.result)) (sparc.move as rs1 $r.result))
(if (not (= rs2 $r.argreg2)) (sparc.move as rs2 $r.argreg2))
(sparc.set as (thefixnum exn) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1)
(emit-evaluate-cc! as branchf.a rd target))))
; Below, 'target' is a label or #f. If #f, RD must be a general hardware
; register or RESULT, and a boolean result is generated in RD.
(define (emit-fixnum-compare-trusted as branchf.a rs1 rs2 rd target)
(let ((rs2 (force-hwreg! as rs2 $r.argreg2)))
(sparc.cmpr as rs1 rs2)
(emit-evaluate-cc! as branchf.a rd target)))
; rs must be a hardware register.
(define (emit-fixnum-compare/imm as branchf.a rs imm rd exn target)
(if (unsafe-code)
(emit-fixnum-compare/imm-trusted as branchf.a rs imm rd target)
(let ((L0 (new-label))
(L1 (new-label)))
(sparc.label as L0)
(sparc.btsti as rs 3)
(sparc.be.a as L1)
(sparc.cmpi as rs (thefixnum imm))
(if (not (= rs $r.result)) (sparc.move as rs $r.result))
(sparc.set as (thefixnum imm) $r.argreg2)
(sparc.set as (thefixnum exn) $r.tmp0)
(millicode-call/ret as $m.exception L0)
(sparc.label as L1)))
(emit-evaluate-cc! as branchf.a rd target))
; rs must be a hardware register.
(define (emit-fixnum-compare/imm-trusted as branchf.a rs imm rd target)
(sparc.cmpi as rs (thefixnum imm))
(emit-evaluate-cc! as branchf.a rd target))
; Range checks.
(define (emit-fixnum-compare-check
as src1 src2 branch-bad L1 liveregs)
(internal-primop-invariant1 'emit-fixnum-compare-check src1)
(let ((src2 (force-hwreg! as src2 $r.argreg2)))
(sparc.cmpr as src1 src2)
(emit-checkcc! as branch-bad L1 liveregs)))
(define (emit-fixnum-compare/imm-check
as src1 imm branch-bad L1 liveregs)
(internal-primop-invariant1 'emit-fixnum-compare/imm-check src1)
(sparc.cmpi as src1 imm)
(emit-checkcc! as branch-bad L1 liveregs))
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; SPARC machine assembler flags.
;
; 12 April 1999
; INTERNAL!
(define short-effective-addresses
(make-twobit-flag 'short-effective-addresses))
(define runtime-safety-checking
(make-twobit-flag 'runtime-safety-checking))
(define catch-undefined-globals
(make-twobit-flag 'catch-undefined-globals))
(define inline-allocation
(make-twobit-flag 'inline-allocation))
;(define inline-assignment
; (make-twobit-flag 'inline-assignment))
(define write-barrier
(make-twobit-flag 'write-barrier))
(define peephole-optimization
(make-twobit-flag 'peephole-optimization))
(define single-stepping
(make-twobit-flag 'single-stepping))
(define fill-delay-slots
(make-twobit-flag 'fill-delay-slots))
; For backward compatibility.
;(define unsafe-code
; (make-twobit-flag 'unsafe-code))
(define (unsafe-code . args)
(if (null? args)
(not (runtime-safety-checking))
(runtime-safety-checking (not (car args)))))
(define (display-assembler-flags which)
(case which
((debugging)
(display-twobit-flag single-stepping))
((safety)
(display-twobit-flag write-barrier)
;(display-twobit-flag unsafe-code)
(display-twobit-flag runtime-safety-checking)
(if (runtime-safety-checking)
(begin (display " ")
(display-twobit-flag catch-undefined-globals))))
((optimization)
(display-twobit-flag peephole-optimization)
(display-twobit-flag inline-allocation)
; (display-twobit-flag inline-assignment)
(display-twobit-flag fill-delay-slots))
(else #t)))
(define (set-assembler-flags! mode)
(case mode
((no-optimization)
(set-assembler-flags! 'standard)
(peephole-optimization #f)
(fill-delay-slots #f))
((standard)
(short-effective-addresses #t)
(catch-undefined-globals #t)
(inline-allocation #f)
; (inline-assignment #f)
(peephole-optimization #t)
(runtime-safety-checking #t)
(write-barrier #t)
(single-stepping #f)
(fill-delay-slots #t))
((fast-safe default)
(set-assembler-flags! 'standard)
; (inline-assignment #t)
(inline-allocation #t))
((fast-unsafe)
(set-assembler-flags! 'fast-safe)
(catch-undefined-globals #f)
(runtime-safety-checking #f))
(else
(error "set-assembler-flags!: unknown mode " mode))))
(set-assembler-flags! 'default)
; eof
; Copyright 1998 Lars T Hansen.
;
; $Id: twobit.sch,v 1.3 1999/08/23 19:14:26 lth Exp $
;
; SPARC disassembler.
;
; (disassemble-instruction instruction address)
; => decoded-instruction
;
; (disassemble-codevector codevector)
; => decoded-instruction-list
;
; (print-instructions decoded-instruction-list)
; => unspecified
; Also takes an optional port and optionally the symbol "native-names".
;
; (format-instruction decoded-instruction address larceny-names?)
; => string
;
; A `decoded-instruction' is a list where the car is a mnemonic and
; the operands are appropriate for that mnemonic.
;
; A `mnemonic' is an exact nonnegative integer. It encodes the name of
; the instruction as well as its attributes (operand pattern and instruction
; type). See below for specific operations on mnemonics.
(define (disassemble-codevector cv)
(define (loop addr ilist)
(if (< addr 0)
ilist
(loop (- addr 4)
(cons (disassemble-instruction (bytevector-word-ref cv addr)
addr)
ilist))))
(loop (- (bytevector-length cv) 4) '()))
(define disassemble-instruction) ; Defined below.
; Mnemonics
(define *asm-annul* 1)
(define *asm-immed* 2)
(define *asm-store* 4)
(define *asm-load* 8)
(define *asm-branch* 16)
(define *asm-freg* 32)
(define *asm-fpop* 64)
(define *asm-no-op2* 128)
(define *asm-no-op3* 256)
(define *asm-bits*
`((a . ,*asm-annul*) (i . ,*asm-immed*) (s . ,*asm-store*)
(l . ,*asm-load*) (b . ,*asm-branch*) (f . ,*asm-freg*)
(fpop . ,*asm-fpop*) (no-op2 . ,*asm-no-op2*) (no-op3 . ,*asm-no-op3*)))
(define *asm-mnemonic-table* '())
(define mnemonic
(let ((n 0))
(lambda (name . rest)
(let* ((probe (assq name *asm-mnemonic-table*))
(code (* 1024
(if probe
(cdr probe)
(let ((code n))
(set! n (+ n 1))
(set! *asm-mnemonic-table*
(cons (cons name code)
*asm-mnemonic-table*))
code)))))
(for-each (lambda (x)
(set! code (+ code (cdr (assq x *asm-bits*)))))
rest)
code))))
(define (mnemonic:name mnemonic)
(let ((mnemonic (quotient mnemonic 1024)))
(let loop ((t *asm-mnemonic-table*))
(cond ((null? t) #f)
((= (cdar t) mnemonic) (caar t))
(else (loop (cdr t)))))))
(define (mnemonic=? m name)
(= (quotient m 1024) (quotient (mnemonic name) 1024)))
(define (mnemonic:test bit)
(lambda (mnemonic)
(not (zero? (logand mnemonic bit)))))
(define (mnemonic:test-not bit)
(lambda (mnemonic)
(zero? (logand mnemonic bit))))
(define mnemonic:annul? (mnemonic:test *asm-annul*))
(define mnemonic:immediate? (mnemonic:test *asm-immed*))
(define mnemonic:store? (mnemonic:test *asm-store*))
(define mnemonic:load? (mnemonic:test *asm-load*))
(define mnemonic:branch? (mnemonic:test *asm-branch*))
(define mnemonic:freg? (mnemonic:test *asm-freg*))
(define mnemonic:fpop? (mnemonic:test *asm-fpop*))
(define mnemonic:op2? (mnemonic:test-not *asm-no-op2*))
(define mnemonic:op3? (mnemonic:test-not *asm-no-op3*))
; Instruction disassembler.
(let ()
;; Useful constants
(define two^3 (expt 2 3))
(define two^5 (expt 2 5))
(define two^6 (expt 2 6))
(define two^8 (expt 2 8))
(define two^9 (expt 2 9))
(define two^12 (expt 2 12))
(define two^13 (expt 2 13))
(define two^14 (expt 2 14))
(define two^16 (expt 2 16))
(define two^19 (expt 2 19))
(define two^21 (expt 2 21))
(define two^22 (expt 2 22))
(define two^24 (expt 2 24))
(define two^25 (expt 2 25))
(define two^29 (expt 2 29))
(define two^30 (expt 2 30))
(define two^32 (expt 2 32))
;; Class 0 has branches and weirdness, like sethi and nop.
;; We dispatch first on the op2 field and then on the op3 field.
(define class00
(let ((b-table
(vector (mnemonic 'bn 'b)
(mnemonic 'be 'b)
(mnemonic 'ble 'b)
(mnemonic 'bl 'b)
(mnemonic 'bleu 'b)
(mnemonic 'bcs 'b)
(mnemonic 'bneg 'b)
(mnemonic 'bvs 'b)
(mnemonic 'ba 'b)
(mnemonic 'bne 'b)
(mnemonic 'bg 'b)
(mnemonic 'bge 'b)
(mnemonic 'bgu 'b)
(mnemonic 'bcc 'b)
(mnemonic 'bpos 'b)
(mnemonic 'bvc 'b)
(mnemonic 'bn 'a 'b)
(mnemonic 'be 'a 'b)
(mnemonic 'ble 'a 'b)
(mnemonic 'bl 'a 'b)
(mnemonic 'bleu 'a 'b)
(mnemonic 'bcs 'a 'b)
(mnemonic 'bneg 'a 'b)
(mnemonic 'bvs 'a 'b)
(mnemonic 'ba 'a 'b)
(mnemonic 'bne 'a 'b)
(mnemonic 'bg 'a 'b)
(mnemonic 'bge 'a 'b)
(mnemonic 'bgu 'a 'b)
(mnemonic 'bcc 'a 'b)
(mnemonic 'bpos 'a 'b)
(mnemonic 'bvc 'a 'b)))
(fb-table
(vector (mnemonic 'fbn 'b)
(mnemonic 'fbne 'b)
(mnemonic 'fblg 'b)
(mnemonic 'fbul 'b)
(mnemonic 'fbl 'b)
(mnemonic 'fbug 'b)
(mnemonic 'fbg 'b)
(mnemonic 'fbu 'b)
(mnemonic 'fba 'b)
(mnemonic 'fbe 'b)
(mnemonic 'fbue 'b)
(mnemonic 'fbge 'b)
(mnemonic 'fbuge 'b)
(mnemonic 'fble 'b)
(mnemonic 'fbule 'b)
(mnemonic 'fbo 'b)
(mnemonic 'fbn 'a 'b)
(mnemonic 'fbne 'a 'b)
(mnemonic 'fblg 'a 'b)
(mnemonic 'fbul 'a 'b)
(mnemonic 'fbl 'a 'b)
(mnemonic 'fbug 'a 'b)
(mnemonic 'fbg 'a 'b)
(mnemonic 'fbu 'a 'b)
(mnemonic 'fba 'a 'b)
(mnemonic 'fbe 'a 'b)
(mnemonic 'fbue 'a 'b)
(mnemonic 'fbge 'a 'b)
(mnemonic 'fbuge 'a 'b)
(mnemonic 'fble 'a 'b)
(mnemonic 'fbule 'a 'b)
(mnemonic 'fbo 'a 'b)))
(nop (mnemonic 'nop))
(sethi (mnemonic 'sethi)))
(lambda (ip instr)
(let ((op2 (op2field instr)))
(cond ((= op2 #b100)
(if (zero? (rdfield instr))
`(,nop)
`(,sethi ,(imm22field instr) ,(rdfield instr))))
((= op2 #b010)
`(,(vector-ref b-table (rdfield instr))
,(* 4 (imm22field instr))))
((= op2 #b110)
`(,(vector-ref fb-table (rdfield instr))
,(* 4 (imm22field instr))))
(else
(disasm-error "Can't disassemble " (number->string instr 16)
" at ip=" ip
" with op2=" op2)))))))
;; Class 1 is the call instruction; there's no choice.
(define (class01 ip instr)
`(,(mnemonic 'call) ,(* 4 (imm30field instr))))
;; Class 2 is for the ALU. Dispatch on op3 field.
(define class10
(let ((op3-table
`#((,(mnemonic 'add) ,(mnemonic 'add 'i))
(,(mnemonic 'and) ,(mnemonic 'and 'i))
(,(mnemonic 'or) ,(mnemonic 'or 'i))
(,(mnemonic 'xor) ,(mnemonic 'xor 'i))
(,(mnemonic 'sub) ,(mnemonic 'sub 'i))
(,(mnemonic 'andn) ,(mnemonic 'andn 'i))
(,(mnemonic 'orn) ,(mnemonic 'orn 'i))
(,(mnemonic 'xnor) ,(mnemonic 'xnor 'i))
(0 0)
(0 0)
(0 0) ; 10
(,(mnemonic 'smul) ,(mnemonic 'smul 'i))
(0 0)
(0 0)
(0 0)
(,(mnemonic 'sdiv) ,(mnemonic 'sdiv 'i))
(,(mnemonic 'addcc) ,(mnemonic 'addcc 'i))
(,(mnemonic 'andcc) ,(mnemonic 'andcc 'i))
(,(mnemonic 'orcc) ,(mnemonic 'orcc 'i))
(,(mnemonic 'xorcc) ,(mnemonic 'xorcc 'i))
(,(mnemonic 'subcc) ,(mnemonic 'subcc 'i)) ; 20
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(,(mnemonic 'smulcc) ,(mnemonic 'smulcc 'i))
(0 0)
(0 0)
(0 0) ; 30
(,(mnemonic 'sdivcc) ,(mnemonic 'sdivcc 'i))
(,(mnemonic 'taddcc) ,(mnemonic 'taddcc 'i))
(,(mnemonic 'tsubcc) ,(mnemonic 'tsubcc 'i))
(0 0)
(0 0)
(0 0)
(,(mnemonic 'sll) ,(mnemonic 'sll 'i))
(,(mnemonic 'srl) ,(mnemonic 'srl 'i))
(,(mnemonic 'sra) ,(mnemonic 'sra 'i))
(,(mnemonic 'rd) 0) ; 40
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(,(mnemonic 'wr) ,(mnemonic 'wr 'i))
(0 0)
(0 0) ; 50
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(,(mnemonic 'jmpl) ,(mnemonic 'jmpl 'i))
(0 0)
(0 0)
(0 0)
(,(mnemonic 'save) ,(mnemonic 'save 'i)) ; 60
(,(mnemonic 'restore) ,(mnemonic 'restore 'i))
(0 0)
(0 0))))
(lambda (ip instr)
(let ((op3 (op3field instr)))
(if (or (= op3 #b110100) (= op3 #b110101))
(fpop-instruction ip instr)
(nice-instruction op3-table ip instr))))))
;; Class 3 is memory stuff.
(define class11
(let ((op3-table
`#((,(mnemonic 'ld 'l) ,(mnemonic 'ld 'i 'l))
(,(mnemonic 'ldb 'l) ,(mnemonic 'ldb 'i 'l))
(,(mnemonic 'ldh 'l) ,(mnemonic 'ldh 'i 'l))
(,(mnemonic 'ldd 'l) ,(mnemonic 'ldd 'i 'l))
(,(mnemonic 'st 's) ,(mnemonic 'st 'i 's))
(,(mnemonic 'stb 's) ,(mnemonic 'stb 'i 's))
(,(mnemonic 'sth 's) ,(mnemonic 'sth 'i 's))
(,(mnemonic 'std 's) ,(mnemonic 'std 'i 's))
(0 0)
(0 0)
(0 0) ; 10
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0) ; 20
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0) ; 30
(0 0)
(,(mnemonic 'ldf 'f 'l) ,(mnemonic 'ldf 'i 'f 'l))
(0 0)
(0 0)
(,(mnemonic 'lddf 'f 'l) ,(mnemonic 'lddf 'i 'f 'l))
(,(mnemonic 'stf 'f 's) ,(mnemonic 'stf 'i 'f 's))
(0 0)
(0 0)
(,(mnemonic 'stdf 'f 's) ,(mnemonic 'stdf 'i 'f 's))
(0 0) ; 40
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0) ; 50
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0)
(0 0) ; 60
(0 0)
(0 0)
(0 0))))
(lambda (ip instr)
(nice-instruction op3-table ip instr))))
;; For classes 2 and 3
(define (nice-instruction op3-table ip instr)
(let* ((op3 (op3field instr))
(imm (ifield instr))
(rd (rdfield instr))
(rs1 (rs1field instr))
(src2 (if (zero? imm)
(rs2field instr)
(imm13field instr))))
(let ((op ((if (zero? imm) car cadr) (vector-ref op3-table op3))))
`(,op ,rs1 ,src2 ,rd))))
;; Floating-point operate instructions
(define (fpop-instruction ip instr)
(let ((rd (rdfield instr))
(rs1 (rs1field instr))
(rs2 (rs2field instr))
(fpop (fpop-field instr)))
`(,(cdr (assv fpop fpop-names)) ,rs1 ,rs2 ,rd)))
(define fpop-names
`((#b000000001 . ,(mnemonic 'fmovs 'fpop 'no-op2))
(#b000000101 . ,(mnemonic 'fnegs 'fpop 'no-op2))
(#b000001001 . ,(mnemonic 'fabss 'fpop 'no-op2))
(#b001000010 . ,(mnemonic 'faddd 'fpop))
(#b001000110 . ,(mnemonic 'fsubd 'fpop))
(#b001001010 . ,(mnemonic 'fmuld 'fpop))
(#b001001110 . ,(mnemonic 'fdivd 'fpop))
(#b001010010 . ,(mnemonic 'fcmpd 'fpop 'no-op3))))
;; The following procedures pick apart an instruction
(define (op2field instr)
(remainder (quotient instr two^22) two^3))
(define (op3field instr)
(remainder (quotient instr two^19) two^6))
(define (ifield instr)
(remainder (quotient instr two^13) 2))
(define (rs2field instr)
(remainder instr two^5))
(define (rs1field instr)
(remainder (quotient instr two^14) two^5))
(define (rdfield instr)
(remainder (quotient instr two^25) two^5))
(define (imm13field instr)
(let ((x (remainder instr two^13)))
(if (not (zero? (quotient x two^12)))
(- x two^13)
x)))
(define (imm22field instr)
(let ((x (remainder instr two^22)))
(if (not (zero? (quotient x two^21)))
(- x two^22)
x)))
(define (imm30field instr)
(let ((x (remainder instr two^30)))
(if (not (zero? (quotient x two^29)))
(- x two^30)
x)))
(define (fpop-field instr)
(remainder (quotient instr two^5) two^9))
(set! disassemble-instruction
(let ((class-table (vector class00 class01 class10 class11)))
(lambda (instr addr)
((vector-ref class-table (quotient instr two^30)) addr instr))))
'disassemble-instruction)
; Instruction printer
;
; It assumes that the first instruction comes from address 0, and prints
; addresses (and relative addresses) based on that assumption.
;
; If the optional symbol native-names is supplied, then SPARC register
; names is used, and millicode calls are not annotated with millicode names.
(define (print-instructions ilist . rest)
(define port (current-output-port))
(define larceny-names? #t)
(define (print-ilist ilist a)
(if (null? ilist)
'()
(begin (display (format-instruction (car ilist) a larceny-names?)
port)
(newline port)
(print-ilist (cdr ilist) (+ a 4)))))
(do ((rest rest (cdr rest)))
((null? rest))
(cond ((port? (car rest))
(set! port (car rest)))
((eq? (car rest) 'native-names)
(set! larceny-names? #f))))
(print-ilist ilist 0))
(define format-instruction) ; Defined below
(define *format-instructions-pretty* #t)
; Instruction formatter.
(let ()
(define use-larceny-registers #t)
(define sparc-register-table
(vector "%g0" "%g1" "%g2" "%g3" "%g4" "%g5" "%g6" "%g7"
"%o0" "%o1" "%o2" "%o3" "%o4" "%o5" "%o6" "%o7"
"%l0" "%l1" "%l2" "%l3" "%l4" "%l5" "%l6" "%l7"
"%i0" "%i1" "%i2" "%i3" "%i4" "%i5" "%i6" "%i7"))
(define larceny-register-table
(make-vector 32 #f))
(define (larceny-register-name reg . rest)
(if (null? rest)
(or (and use-larceny-registers
(vector-ref larceny-register-table reg))
(vector-ref sparc-register-table reg))
(vector-set! larceny-register-table reg (car rest))))
(define millicode-procs '())
(define (float-register-name reg)
(string-append "%f" (number->string reg)))
(define op car)
(define op1 cadr)
(define op2 caddr)
(define op3 cadddr)
(define tabstring (string #\tab))
(define (heximm n)
(if (>= n 16)
(string-append tabstring "! 0x" (number->string n 16))
""))
(define (millicode-name offset . rest)
(if (null? rest)
(let ((probe (assv offset millicode-procs)))
(if probe
(cdr probe)
"[unknown]"))
(set! millicode-procs
(cons (cons offset (car rest)) millicode-procs))))
(define (millicode-call offset)
(string-append tabstring "! " (millicode-name offset)))
(define (plus/minus n)
(cond ((< n 0)
(string-append " - " (number->string (abs n))))
((and (= n 0) *format-instructions-pretty*) "")
(else
(string-append " + " (number->string n)))))
(define (srcreg instr extractor)
(if (mnemonic:freg? (op instr))
(float-register-name (extractor instr))
(larceny-register-name (extractor instr))))
(define (sethi instr)
(string-append (number->string (* (op1 instr) 1024)) ", "
(larceny-register-name (op2 instr))
(heximm (* (op1 instr) 1024))))
(define (rrr instr)
(string-append (larceny-register-name (op1 instr)) ", "
(larceny-register-name (op2 instr)) ", "
(larceny-register-name (op3 instr))))
(define (rir instr)
(string-append (larceny-register-name (op1 instr)) ", "
(number->string (op2 instr)) ", "
(larceny-register-name (op3 instr))
(heximm (op2 instr))))
(define (sir instr)
(string-append (srcreg instr op3) ", [ "
(larceny-register-name (op1 instr))
(plus/minus (op2 instr)) " ]"))
(define (srr instr)
(string-append (srcreg instr op3) ", [ "
(larceny-register-name (op1 instr)) "+"
(larceny-register-name (op2 instr)) " ]"))
(define (lir instr)
(string-append "[ " (larceny-register-name (op1 instr))
(plus/minus (op2 instr)) " ], "
(srcreg instr op3)))
(define (lrr instr)
(string-append "[ " (larceny-register-name (op1 instr)) "+"
(larceny-register-name (op2 instr)) " ], "
(srcreg instr op3)))
(define (bimm instr addr)
(string-append "#" (number->string (+ (op1 instr) addr))))
(define (jmpli instr)
(string-append (larceny-register-name (op1 instr))
(plus/minus (op2 instr)) ", "
(larceny-register-name (op3 instr))
(if (and (= (op1 instr) $r.globals)
use-larceny-registers)
(millicode-call (op2 instr))
(heximm (op2 instr)))))
(define (jmplr instr)
(string-append (larceny-register-name (op1 instr)) "+"
(larceny-register-name (op2 instr)) ", "
(larceny-register-name (op3 instr))))
(define (call instr addr)
(string-append "#" (number->string (+ (op1 instr) addr))))
(define (rd instr)
(string-append "%y, " (srcreg instr op3)))
(define (wr instr imm?)
(if imm?
(string-append (larceny-register-name (op1 instr)) ", "
(number->string (op2 instr)) ", %y"
(larceny-register-name (op3 instr)))
(string-append (larceny-register-name (op1 instr)) ", "
(larceny-register-name (op2 instr)) ", %y")))
(define (fpop instr op2-used? op3-used?)
(string-append (float-register-name (op1 instr)) ", "
(cond ((and op2-used? op3-used?)
(string-append
(float-register-name (op2 instr)) ", "
(float-register-name (op3 instr))))
(op2-used?
(float-register-name (op2 instr)))
(else
(float-register-name (op3 instr))))))
;; If we want to handle instruction aliases (clr, mov, etc) then
;; the structure of this procedure must change, because as it is,
;; the printing of the name is independent of the operand values.
(define (format-instr i a larceny-names?)
(set! use-larceny-registers larceny-names?)
(let ((m (car i)))
(string-append (number->string a)
tabstring
(symbol->string (mnemonic:name m))
(if (mnemonic:annul? m) ",a" "")
tabstring
(cond ((mnemonic:store? m)
(if (mnemonic:immediate? m) (sir i) (srr i)))
((mnemonic:load? m)
(if (mnemonic:immediate? m) (lir i) (lrr i)))
((mnemonic:fpop? m)
(fpop i (mnemonic:op2? m) (mnemonic:op3? m)))
((mnemonic:branch? m) (bimm i a))
((mnemonic=? m 'sethi) (sethi i))
((mnemonic=? m 'nop) "")
((mnemonic=? m 'jmpl)
(if (mnemonic:immediate? m) (jmpli i) (jmplr i)))
((mnemonic=? m 'call) (call i a))
((mnemonic=? m 'rd) (rd i))
((mnemonic=? m 'wr) (wr i (mnemonic:immediate? m)))
((mnemonic:immediate? m) (rir i))
(else (rrr i))))))
(larceny-register-name $r.tmp0 "%tmp0")
(larceny-register-name $r.result "%result")
(larceny-register-name $r.argreg2 "%argreg2")
(larceny-register-name $r.argreg3 "%argreg3")
(larceny-register-name $r.tmp1 "%tmp1")
(larceny-register-name $r.tmp2 "%tmp2")
(larceny-register-name $r.reg0 "%r0")
(larceny-register-name $r.reg1 "%r1")
(larceny-register-name $r.reg2 "%r2")
(larceny-register-name $r.reg3 "%r3")
(larceny-register-name $r.reg4 "%r4")
(larceny-register-name $r.reg5 "%r5")
(larceny-register-name $r.reg6 "%r6")
(larceny-register-name $r.reg7 "%r7")
(larceny-register-name $r.e-top "%etop")
(larceny-register-name $r.e-limit "%elim")
(larceny-register-name $r.timer "%timer")
(larceny-register-name $r.millicode "%millicode")
(larceny-register-name $r.globals "%globals")
(larceny-register-name $r.stkp "%stkp") ; note: after elim
(millicode-name $m.alloc "alloc")
(millicode-name $m.alloci "alloci")
(millicode-name $m.gc "gc")
(millicode-name $m.addtrans "addtrans")
(millicode-name $m.stkoflow "stkoflow")
(millicode-name $m.stkuflow "stkuflow")
(millicode-name $m.creg "creg")
(millicode-name $m.creg-set! "creg-set!")
(millicode-name $m.add "+")
(millicode-name $m.subtract "- (binary)")
(millicode-name $m.multiply "*")
(millicode-name $m.quotient "quotient")
(millicode-name $m.remainder "remainder")
(millicode-name $m.divide "/")
(millicode-name $m.modulo "modulo")
(millicode-name $m.negate "- (unary)")
(millicode-name $m.numeq "=")
(millicode-name $m.numlt "<")
(millicode-name $m.numle "<=")
(millicode-name $m.numgt ">")
(millicode-name $m.numge ">=")
(millicode-name $m.zerop "zero?")
(millicode-name $m.complexp "complex?")
(millicode-name $m.realp "real?")
(millicode-name $m.rationalp "rational?")
(millicode-name $m.integerp "integer?")
(millicode-name $m.exactp "exact?")
(millicode-name $m.inexactp "inexact?")
(millicode-name $m.exact->inexact "exact->inexact")
(millicode-name $m.inexact->exact "inexact->exact")
(millicode-name $m.make-rectangular "make-rectangular")
(millicode-name $m.real-part "real-part")
(millicode-name $m.imag-part "imag-part")
(millicode-name $m.sqrt "sqrt")
(millicode-name $m.round "round")
(millicode-name $m.truncate "truncate")
(millicode-name $m.apply "apply")
(millicode-name $m.varargs "varargs")
(millicode-name $m.typetag "typetag")
(millicode-name $m.typetag-set "typetag-set")
(millicode-name $m.break "break")
(millicode-name $m.eqv "eqv?")
(millicode-name $m.partial-list->vector "partial-list->vector")
(millicode-name $m.timer-exception "timer-exception")
(millicode-name $m.exception "exception")
(millicode-name $m.singlestep "singlestep")
(millicode-name $m.syscall "syscall")
(millicode-name $m.bvlcmp "bvlcmp")
(millicode-name $m.enable-interrupts "enable-interrupts")
(millicode-name $m.disable-interrupts "disable-interrupts")
(millicode-name $m.alloc-bv "alloc-bv")
(millicode-name $m.global-ex "global-exception")
(millicode-name $m.invoke-ex "invoke-exception")
(millicode-name $m.global-invoke-ex "global-invoke-exception")
(millicode-name $m.argc-ex "argc-exception")
(set! format-instruction format-instr)
'format-instruction)
; eof
; ----------------------------------------------------------------------
(define (twobit-benchmark type . rest)
(let ((k (if (null? rest) 1 (car rest))))
(run-benchmark
"twobit"
k
(lambda ()
(case type
((long)
(compiler-switches 'fast-safe)
(benchmark-block-mode #f)
(compile-file "benchmarks/twobit-input-long.sch"))
((short)
(compiler-switches 'fast-safe)
(benchmark-block-mode #t)
(compile-file "benchmarks/twobit-input-short.sch"))
(else
(error "Benchmark type must be `long' or `short': " type))))
(lambda (result)
#t))))
; eof
|