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
|
;;;; compiler.scm - The CHICKEN Scheme compiler
;
;
; "This is insane. What we clearly want to do is not exactly clear, and is rooted in NCOMPLR."
;
;
;--------------------------------------------------------------------------------------------
; Copyright (c) 2008-2016, The CHICKEN Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
;
;
; Supported syntax:
;
; - Declaration specifiers:
;
; ([not] extended-bindings {<name>})
; ([not] inline {<var>})
; ([not] interrupts-enabled)
; ([not] safe)
; ([not] standard-bindings {<name>})
; ([not] usual-integrations {<name>})
; (local {<name> ...})
; ([not] inline-global {<name>})
; ([number-type] <type>)
; (always-bound {<name>})
; (block)
; (block-global {<name>})
; (bound-to-procedure {<var>})
; (compile-syntax)
; (disable-interrupts)
; (emit-import-library {<module> | (<module> <filename>)})
; (export {<name>})
; (fixnum-arithmetic)
; (foreign-declare {<string>})
; (hide {<name>})
; (inline-limit <limit>)
; (keep-shadowed-macros)
; (no-argc-checks)
; (no-bound-checks)
; (no-procedure-checks)
; (no-procedure-checks-for-usual-bindings)
; (no-procedure-checks-for-toplevel-bindings)
; (profile <symbol> ...)
; (safe-globals)
; (separate)
; (type (<symbol> <typespec>) ...)
; (unit <unitname>)
; (unsafe)
; (unused <symbol> ...)
; (uses {<unitname>})
; (strict-types)
; (specialize)
; (enforce-argument-types [<symbol> ...])
;
; <type> = fixnum | generic
; - Global symbol properties:
;
; ##compiler#always-bound -> BOOL
; ##compiler#always-bound-to-procedure -> BOOL
; ##compiler#local -> BOOL
; ##compiler#visibility -> #f | 'hidden | 'exported
; ##compiler#constant -> BOOL defined as constant
; ##compiler#intrinsic -> #f | 'standard | 'extended
; ##compiler#inline -> 'no | 'yes
; ##compiler#inline-global -> 'yes | 'no | <node>
; ##compiler#profile -> BOOL
; ##compiler#unused -> BOOL
; ##compiler#foldable -> BOOL
; ##compiler#pure -> BOOL referentially transparent
; ##compiler#clean -> BOOL does not modify local state
; ##compiler#type -> TYPE
; ##compiler#type-source -> 'db | 'local | 'inference
; - Source language:
;
; <variable>
; <constant>
; (##core#declare {<spec>})
; (##core#immutable <exp>)
; (##core#quote <exp>)
; (##core#syntax <exp>)
; (##core#if <exp> <exp> [<exp>])
; (##core#let <variable> ({(<variable> <exp>)}) <body>)
; (##core#let ({(<variable> <exp>)}) <body>)
; (##core#letrec ({(<variable> <exp>)}) <body>)
; (##core#letrec* ({(<variable> <exp>)}) <body>)
; (##core#let-location <symbol> <type> [<init>] <exp>)
; (##core#lambda <variable> <body>)
; (##core#lambda ({<variable>}+ [. <variable>]) <body>)
; (##core#set! <variable> <exp>)
; (##core#begin <exp> ...)
; (##core#toplevel-begin <exp> ...)
; (##core#include <string>)
; (##core#loop-lambda <llist> <body>)
; (##core#undefined)
; (##core#primitive <name>)
; (##core#inline {<op>} <exp>)
; (##core#inline_allocate (<op> <words>) {<exp>})
; (##core#inline_ref (<name> <type>))
; (##core#inline_update (<name> <type>) <exp>)
; (##core#inline_loc_ref (<type>) <exp>)
; (##core#inline_loc_update (<type>) <exp> <exp>)
; (##core#compiletimetoo <exp>)
; (##core#compiletimeonly <exp>)
; (##core#elaborationtimetoo <exp>)
; (##core#elaborationtimeonly <exp>)
; (##core#define-foreign-variable <symbol> <type> [<string>])
; (##core#define-foreign-type <symbol> <type> [<proc1> [<proc2>]])
; (##core#foreign-lambda <type> <string> {<type>})
; (##core#foreign-lambda* <type> ({(<type> <var>)})) {<string>})
; (##core#foreign-safe-lambda <type> <string> {<type>})
; (##core#foreign-safe-lambda* <type> ({(<type> <var>)})) {<string>})
; (##core#foreign-primitive <type> ({(<type> <var>)}) {<string>})
; (##core#define-inline <name> <exp>)
; (##core#define-constant <name> <exp*>)
; (##core#foreign-callback-wrapper '<name> <qualifiers> '<type> '({<type>}) <exp>)
; (##core#define-external-variable <name> <type> <bool> [<symbol>])
; (##core#check <exp>)
; (##core#require-for-syntax <exp> ...)
; (##core#require-extension (<id> ...) <bool>)
; (##core#app <exp> {<exp>})
; (##core#define-syntax <symbol> <expr>)
; (##core#define-compiler-syntax <symbol> <expr>)
; (##core#let-compiler-syntax ((<symbol> <expr>) ...) <expr> ...)
; (##core#module <symbol> #t | (<name> | (<name> ...) ...) <body>)
; (##core#let-module-alias ((<alias> <name>) ...) <body>)
; (##core#the <type> <strict?> <exp>)
; (##core#typecase <info> <exp> (<type> <body>) ... [(else <body>)])
; (##core#debug-event {<event> <loc>})
; (<exp> {<exp>})
; - Core language:
;
; [##core#variable {<variable>}]
; [if {} <exp> <exp> <exp>)]
; [quote {<exp>}]
; [let {<variable>} <exp-v> <exp>]
; [##core#lambda {<id> <mode> (<variable>... [. <variable>]) <size>} <exp>]
; [set! {<variable>} <exp>]
; [##core#undefined {}]
; [##core#primitive {<name>}]
; [##core#inline {<op>} <exp>...]
; [##core#inline_allocate {<op> <words>} <exp>...]
; [##core#inline_ref {<name> <type>}]
; [##core#inline_update {<name> <type>} <exp>]
; [##core#inline_loc_ref {<type>} <exp>]
; [##core#inline_loc_update {<type>} <exp> <exp>]
; [##core#debug-event {<event> <loc> <ln>}]
; [##core#call {<safe-flag> [<debug-info>]} <exp-f> <exp>...]
; [##core#callunit {<unitname>} <exp>...]
; [##core#switch {<count>} <exp> <const1> <body1> ... <defaultbody>]
; [##core#cond <exp> <exp> <exp>]
; [##core#recurse {<tail-flag>} <exp1> ...]
; [##core#return <exp>]
; [##core#direct_call {<safe-flag> <debug-info> <call-id> <words>} <exp-f> <exp>...]
; [##core#direct_lambda {<id> <mode> (<variable>... [. <variable>]) <size>} <exp>]
; [##core#the {<type> <strict>} <exp>]
; [##core#the/result {<typelist>} <exp>]
; [##core#typecase {<info> (<type> ...)} <exp> <body1> ... [<elsebody>]]
; - Closure converted/prepared language:
;
; [if {} <exp> <exp> <exp>]
; [quote {<exp>}]
; [##core#bind {<count>} <exp-v>... <exp>]
; [##core#let_unboxed {<name> <utype>} <exp1> <exp2>]
; [##core#undefined {}]
; [##core#unboxed_ref {<name> [<utype>]}]
; [##core#unboxed_set! {<name> <utype>} <exp>]
; [##core#inline {<op>} <exp>...]
; [##core#inline_allocate {<op <words>} <exp>...]
; [##core#inline_ref {<name> <type>}]
; [##core#inline_update {<name> <type>} <exp>]
; [##core#inline_loc_ref {<type>} <exp>]
; [##core#inline_loc_update {<type>} <exp> <exp>]
; [##core#inline_unboxed {<op>} <exp> ...]
; [##core#debug-event {<index> <event> <loc> <ln>}]
; [##core#closure {<count>} <exp>...]
; [##core#box {} <exp>]
; [##core#unbox {} <exp>]
; [##core#ref {<index>} <exp>]
; [##core#update {<index>} <exp> <exp>]
; [##core#updatebox {} <exp> <exp>]
; [##core#update_i {<index>} <exp> <exp>]
; [##core#updatebox_i {} <exp> <exp>]
; [##core#call {<dbg-info-index> <safe-flag> [<debug-info> [<call-id> <customizable-flag>]]} <exp-f> <exp>...]
; [##core#callunit {<unitname>} <exp>...]
; [##core#cond <exp> <exp> <exp>]
; [##core#local {<index>}]
; [##core#setlocal {<index>} <exp>]
; [##core#global {<literal> <safe-flag> <block-mode> [<name>]}]
; [##core#setglobal {<literal> <block-mode> <name>} <exp>]
; [##core#setglobal_i {<literal> <block-mode> <name>} <exp>]
; [##core#literal {<literal>}]
; [##core#immediate {<type> [<immediate>]}] - type: bool/fix/nil/char
; [##core#proc {<name> [<non-internal>]}]
; [##core#recurse {<tail-flag> <call-id>} <exp1> ...]
; [##core#return <exp>]
; [##core#direct_call {<safe-flag> <debug-info> <call-id> <words>} <exp-f> <exp>...]
; Analysis database entries:
;
; <variable>:
;
; captured -> <boolean> If true: variable is used outside it's home-scope
; global -> <boolean> If true: variable does not occur in any lambda-list
; call-sites -> ((<lambda-id> <node>) ...) Known call-nodes of a named procedure
; home -> <lambda-id> Procedure which introduces this variable
; unknown -> <boolean> If true: variable cannot have a known value
; assigned -> <boolean> If true: variable is assigned somewhere
; assigned-locally -> <boolean> If true: variable has been assigned inside user lambda
; undefined -> <boolean> If true: variable is unknown yet but can be known later
; value -> <node> Variable has a known value
; local-value -> <node> Variable is declared local and has value
; potential-value -> <node> Global variable was assigned this value (used for lambda-info)
; references -> (<node> ...) Nodes that are accesses of this variable (##core#variable nodes)
; boxed -> <boolean> If true: variable has to be boxed after closure-conversion
; contractable -> <boolean> If true: variable names contractable procedure
; inlinable -> <boolean> If true: variable names potentially inlinable procedure
; collapsable -> <boolean> If true: variable refers to collapsable constant
; removable -> <boolean> If true: variable is not used
; replacable -> <variable> Variable can be replaced by another variable
; replacing -> <boolean> If true: variable can replace another variable (don't remove)
; standard-binding -> <boolean> If true: variable names a standard binding
; extended-binding -> <boolean> If true: variable names an extended binding
; unused -> <boolean> If true: variable is a formal parameter that is never used
; rest-parameter -> #f | 'list If true: variable holds rest-argument list
; constant -> <boolean> If true: variable has fixed value
; hidden-refs -> <boolean> If true: procedure that refers to hidden global variables
; inline-transient -> <boolean> If true: was introduced during inlining
;
; <lambda-id>:
;
; contains -> (<lambda-id> ...) Procedures contained in this lambda
; contained-in -> <lambda-id> Procedure containing this lambda
; has-unused-parameters -> <boolean> If true: procedure has unused formal parameters
; use-expr -> (<lambda-id> ...) Marks non-direct use-sites of common subexpression
; closure-size -> <integer> Number of free variables stored in a closure
; customizable -> <boolean> If true: all call sites are known, procedure does not escape
; simple -> <boolean> If true: procedure only calls its continuation
; explicit-rest -> <boolean> If true: procedure is called with consed rest list
; captured-variables -> (<var> ...) List of closed over variables
; inline-target -> <boolean> If true: was target of an inlining operation
(declare
(unit compiler))
(include "compiler-namespace")
(define (d arg1 . more)
(when (##sys#fudge 13) ; debug mode?
(if (null? more)
(pp arg1)
(apply print arg1 more))))
(define-syntax d (syntax-rules () ((_ . _) (void))))
(include "tweaks")
(define-inline (gensym-f-id) (gensym 'f_))
(define-foreign-variable installation-home c-string "C_INSTALL_SHARE_HOME")
(define-foreign-variable default-target-heap-size int "C_DEFAULT_TARGET_HEAP_SIZE")
(define-constant foreign-type-table-size 301)
(define-constant initial-analysis-database-size 3001)
(define-constant default-line-number-database-size 997)
(define-constant inline-table-size 301)
(define-constant constant-table-size 301)
(define-constant file-requirements-size 301)
(define-constant real-name-table-size 997)
(define-constant default-inline-max-size 20)
;;; Global variables containing compilation parameters:
(define unit-name #f)
(define number-type 'generic)
(define standard-bindings '())
(define extended-bindings '())
(define insert-timer-checks #t)
(define used-units '())
(define unsafe #f)
(define foreign-declarations '())
(define emit-trace-info #f)
(define emit-debug-info #f)
(define block-compilation #f)
(define line-number-database-size default-line-number-database-size)
(define target-heap-size #f)
(define target-stack-size #f)
(define optimize-leaf-routines #f)
(define emit-profile #f)
(define no-bound-checks #f)
(define no-argc-checks #f)
(define no-procedure-checks #f)
(define no-global-procedure-checks #f)
(define source-filename #f)
(define safe-globals-flag #f)
(define explicit-use-flag #f)
(define disable-stack-overflow-checking #f)
(define require-imports-flag #f)
(define external-protos-first #f)
(define inline-max-size default-inline-max-size)
(define emit-closure-info #t)
(define undefine-shadowed-macros #t)
(define profiled-procedures #f)
(define import-libraries '())
(define all-import-libraries #f)
(define enable-module-registration #t)
(define standalone-executable #t)
(define local-definitions #f)
(define inline-locally #f)
(define enable-inline-files #f)
(define compiler-syntax-enabled #t)
(define bootstrap-mode #f)
(define strict-variable-types #f)
(define enable-specialization #f)
;;; Other global variables:
(define verbose-mode #f)
(define original-program-size #f)
(define current-program-size 0)
(define current-analysis-database-size initial-analysis-database-size)
(define line-number-database-2 #f)
(define immutable-constants '())
(define inline-table #f)
(define inline-table-used #f)
(define constant-table #f)
(define constants-used #f)
(define broken-constant-nodes '())
(define inline-substitutions-enabled #f)
(define direct-call-ids '())
(define first-analysis #t)
(define foreign-type-table #f)
(define foreign-variables '())
(define foreign-lambda-stubs '())
(define foreign-callback-stubs '())
(define external-variables '())
(define profile-lambda-list '())
(define profile-lambda-index 0)
(define profile-info-vector-name #f)
(define external-to-pointer '())
(define real-name-table #f)
(define location-pointer-map '())
(define pending-canonicalizations '())
(define defconstant-bindings '())
(define callback-names '())
(define toplevel-scope #t)
(define toplevel-lambda-id #f)
(define csc-control-file #f)
(define data-declarations '())
(define file-requirements #f)
(define postponed-initforms '())
;;; Initialize globals:
(define (initialize-compiler)
(if line-number-database-2
(vector-fill! line-number-database-2 '())
(set! line-number-database-2 (make-vector line-number-database-size '())) )
(if inline-table
(vector-fill! inline-table '())
(set! inline-table (make-vector inline-table-size '())) )
(if constant-table
(vector-fill! constant-table '())
(set! constant-table (make-vector constant-table-size '())) )
(set! profile-info-vector-name (make-random-name 'profile-info))
(set! real-name-table (make-vector real-name-table-size '()))
(if file-requirements
(vector-fill! file-requirements '())
(set! file-requirements (make-vector file-requirements-size '())) )
(if foreign-type-table
(vector-fill! foreign-type-table '())
(set! foreign-type-table (make-vector foreign-type-table-size '())) ) )
;;; Expand macros and canonicalize expressions:
(define (canonicalize-expression exp)
(let ((compiler-syntax '()))
(define (find-id id se) ; ignores macro bindings
(cond ((null? se) #f)
((and (eq? id (caar se)) (symbol? (cdar se))) (cdar se))
(else (find-id id (cdr se)))))
(define (lookup id se)
(cond ((find-id id se))
((##sys#get id '##core#macro-alias))
(else id)))
(define (macro-alias var se)
(let ((alias (gensym var)))
(##sys#put! alias '##core#macro-alias (lookup var se))
alias) )
(define (set-real-names! as ns)
(for-each (lambda (a n) (set-real-name! a n)) as ns) )
(define (write-to-string x)
(let ([out (open-output-string)])
(write x out)
(get-output-string out) ) )
(define (unquotify x se)
(if (and (list? x)
(= 2 (length x))
(symbol? (car x))
(eq? 'quote (lookup (car x) se)))
(cadr x)
x) )
(define (resolve-variable x0 e se dest ldest h)
(let ((x (lookup x0 se)))
(d `(RESOLVE-VARIABLE: ,x0 ,x ,(map (lambda (x) (car x)) se)))
(cond ((not (symbol? x)) x0) ; syntax?
[(and constants-used (##sys#hash-table-ref constant-table x))
=> (lambda (val) (walk (car val) e se dest ldest h #f)) ]
[(and inline-table-used (##sys#hash-table-ref inline-table x))
=> (lambda (val) (walk val e se dest ldest h #f)) ]
[(assq x foreign-variables)
=> (lambda (fv)
(let* ([t (second fv)]
[ft (final-foreign-type t)]
[body `(##core#inline_ref (,(third fv) ,t))] )
(walk
(foreign-type-convert-result
(finish-foreign-result ft body)
t)
e se dest ldest h #f)))]
[(assq x location-pointer-map)
=> (lambda (a)
(let* ([t (third a)]
[ft (final-foreign-type t)]
[body `(##core#inline_loc_ref (,t) ,(second a))] )
(walk
(foreign-type-convert-result
(finish-foreign-result ft body)
t)
e se dest ldest h #f))) ]
((##sys#get x '##core#primitive))
((not (memq x e)) (##sys#alias-global-hook x #f h)) ; only if global
(else x))))
(define (emit-import-lib name il)
(let* ((fname (if all-import-libraries
(string-append (symbol->string name) ".import.scm")
(cdr il)))
(imps (##sys#compiled-module-registration (##sys#current-module)))
(oldimps
(and (file-exists? fname)
(read-file fname) ) ) )
(cond ((equal? imps oldimps)
(when verbose-mode
(print "not generating import library `" fname "' for module `"
name "' because imports did not change")) )
(else
(when verbose-mode
(print "generating import library `" fname "' for module `"
name "' ..."))
(with-output-to-file fname
(lambda ()
(print ";;;; " fname " - GENERATED BY CHICKEN "
(chicken-version) " -*- Scheme -*-\n")
(for-each pretty-print imps)
(print "\n;; END OF FILE"))))) ) )
(define (walk x e se dest ldest h outer-ln)
(cond ((symbol? x)
(cond ((keyword? x) `(quote ,x))
((memq x unlikely-variables)
(warning
(sprintf "reference to variable `~s' possibly unintended" x) )))
(resolve-variable x e se dest ldest h))
((not-pair? x)
(if (constant? x)
`(quote ,x)
(##sys#syntax-error/context "illegal atomic form" x)))
((symbol? (car x))
(let ((ln (or (get-line x) outer-ln)))
(emit-syntax-trace-info x #f)
(unless (proper-list? x)
(if ln
(##sys#syntax-error/context (sprintf "(~a) - malformed expression" ln) x)
(##sys#syntax-error/context "malformed expression" x)))
(set! ##sys#syntax-error-culprit x)
(let* ((name0 (lookup (car x) se))
(name (or (and (symbol? name0) (##sys#get name0 '##core#primitive)) name0))
(xexpanded (##sys#expand x se compiler-syntax-enabled)))
(when ln (update-line-number-database! xexpanded ln))
(cond ((not (eq? x xexpanded))
(walk xexpanded e se dest ldest h ln))
[(and inline-table-used (##sys#hash-table-ref inline-table name))
=> (lambda (val)
(walk (cons val (cdr x)) e se dest ldest h ln)) ]
[else
(case name
((##core#if)
`(if
,(walk (cadr x) e se #f #f h ln)
,(walk (caddr x) e se #f #f h ln)
,(if (null? (cdddr x))
'(##core#undefined)
(walk (cadddr x) e se #f #f h ln) ) ) )
((##core#syntax ##core#quote)
`(quote ,(##sys#strip-syntax (cadr x))))
((##core#check)
(if unsafe
''#t
(walk (cadr x) e se dest ldest h ln) ) )
((##core#the)
`(##core#the
,(##sys#strip-syntax (cadr x))
,(caddr x)
,(walk (cadddr x) e se dest ldest h ln)))
((##core#typecase)
`(##core#typecase
,(or ln (cadr x))
,(walk (caddr x) e se #f #f h ln)
,@(map (lambda (cl)
(list (##sys#strip-syntax (car cl))
(walk (cadr cl) e se dest ldest h ln)))
(cdddr x))))
((##core#immutable)
(let ((c (cadadr x)))
(cond [(assoc c immutable-constants) => cdr]
[else
(let ([var (gensym 'c)])
(set! immutable-constants (alist-cons c var immutable-constants))
(mark-variable var '##compiler#always-bound)
(hide-variable var)
var) ] ) ) )
((##core#undefined ##core#callunit ##core#primitive) x)
((##core#inline_ref)
`(##core#inline_ref
(,(caadr x) ,(##sys#strip-syntax (cadadr x)))))
((##core#inline_loc_ref)
`(##core#inline_loc_ref
,(##sys#strip-syntax (cadr x))
,(walk (caddr x) e se dest ldest h ln)))
((##core#require-for-syntax)
(let ([ids (map eval (cdr x))])
(apply ##sys#require ids)
(##sys#hash-table-update!
file-requirements 'dynamic/syntax
(cut lset-union eq? <> ids)
(lambda () ids) )
'(##core#undefined) ) )
((##core#require-extension)
(let ((imp? (caddr x)))
(walk
(let loop ([ids (##sys#strip-syntax (cadr x))])
(if (null? ids)
'(##core#undefined)
(let ((id (car ids)))
(let-values (((exp f realid)
(##sys#do-the-right-thing id #t imp?)))
(unless (or f
(and (symbol? id)
(or (feature? id)
(##sys#find-extension
(##sys#canonicalize-extension-path
id 'require-extension)
#f)) ) )
(warning
(sprintf "extension `~A' is currently not installed" realid)))
`(##core#begin ,exp ,(loop (cdr ids))) ) ) ) )
e se dest ldest h ln) ) )
((##core#let)
(let* ((bindings (cadr x))
(vars (unzip1 bindings))
(aliases (map gensym vars))
(se2 (##sys#extend-se se vars aliases)))
(set-real-names! aliases vars)
`(let
,(map (lambda (alias b)
(list alias (walk (cadr b) e se (car b) #t h ln)) )
aliases bindings)
,(walk (##sys#canonicalize-body
(cddr x) se2 compiler-syntax-enabled)
(append aliases e)
se2 dest ldest h ln) ) ) )
((##core#letrec*)
(let ((bindings (cadr x))
(body (cddr x)) )
(walk
`(##core#let
,(map (lambda (b)
(list (car b) '(##core#undefined)))
bindings)
,@(map (lambda (b)
`(##core#set! ,(car b) ,(cadr b)))
bindings)
(##core#let () ,@body) )
e se dest ldest h ln)))
((##core#letrec)
(let* ((bindings (cadr x))
(vars (unzip1 bindings))
(tmps (map gensym vars))
(body (cddr x)) )
(walk
`(##core#let
,(map (lambda (b)
(list (car b) '(##core#undefined)))
bindings)
(##core#let
,(map (lambda (t b) (list t (cadr b))) tmps bindings)
,@(map (lambda (v t)
`(##core#set! ,v ,t))
vars tmps)
(##core#let () ,@body) ) )
e se dest ldest h ln)))
((##core#lambda)
(let ((llist (cadr x))
(obody (cddr x)) )
(when (##sys#extended-lambda-list? llist)
(set!-values
(llist obody)
(##sys#expand-extended-lambda-list
llist obody ##sys#error se) ) )
(decompose-lambda-list
llist
(lambda (vars argc rest)
(let* ((aliases (map gensym vars))
(se2 (##sys#extend-se se vars aliases))
(body0 (##sys#canonicalize-body
obody se2 compiler-syntax-enabled))
(body (walk
(if emit-debug-info
`(##core#begin
(##core#debug-event "C_DEBUG_ENTRY" ',dest)
,body0)
body0)
(append aliases e) se2 #f #f dest ln))
(llist2
(build-lambda-list
aliases argc
(and rest (list-ref aliases (posq rest vars))) ) )
(l `(##core#lambda ,llist2 ,body)) )
(set-real-names! aliases vars)
(cond ((or (not dest)
ldest
(assq dest se)) ; not global?
l)
((and emit-profile
(or (eq? profiled-procedures 'all)
(and
(eq? profiled-procedures 'some)
(variable-mark dest '##compiler#profile))))
(expand-profile-lambda
(if (memq dest e) ; should normally not be the case
e
(##sys#alias-global-hook dest #f #f))
llist2 body) )
(else l)))))))
((##core#let-syntax)
(let ((se2 (append
(map (lambda (b)
(list
(car b)
se
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
(##sys#strip-syntax (car b)))))
(cadr x) )
se) ) )
(walk
(##sys#canonicalize-body (cddr x) se2 compiler-syntax-enabled)
e se2
dest ldest h ln) ) )
((##core#letrec-syntax)
(let* ((ms (map (lambda (b)
(list
(car b)
#f
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
(##sys#strip-syntax (car b)))))
(cadr x) ) )
(se2 (append ms se)) )
(for-each
(lambda (sb)
(set-car! (cdr sb) se2) )
ms)
(walk
(##sys#canonicalize-body (cddr x) se2 compiler-syntax-enabled)
e se2 dest ldest h ln)))
((##core#define-syntax)
(##sys#check-syntax
(car x) x
(if (pair? (cadr x))
'(_ (variable . lambda-list) . #(_ 1))
'(_ variable _) )
#f se)
(let* ((var (if (pair? (cadr x)) (caadr x) (cadr x)))
(body (if (pair? (cadr x))
`(##core#lambda ,(cdadr x) ,@(cddr x))
(caddr x)))
(name (lookup var se)))
(##sys#register-syntax-export name (##sys#current-module) body)
(##sys#extend-macro-environment
name
(##sys#current-environment)
(##sys#eval/meta body))
(walk
(if ##sys#enable-runtime-macros
`(##sys#extend-macro-environment
',var
(##sys#current-environment) ,body) ;XXX possibly wrong se?
'(##core#undefined) )
e se dest ldest h ln)) )
((##core#define-compiler-syntax)
(let* ((var (cadr x))
(body (caddr x))
(name (lookup var se)))
(when body
(set! compiler-syntax
(alist-cons
name
(##sys#get name '##compiler#compiler-syntax)
compiler-syntax)))
(##sys#put!
name '##compiler#compiler-syntax
(and body
(##sys#cons
(##sys#ensure-transformer
(##sys#eval/meta body)
(##sys#strip-syntax var))
(##sys#current-environment))))
(walk
(if ##sys#enable-runtime-macros
`(##sys#put!
(##core#syntax ,name)
'##compiler#compiler-syntax
,(and body
`(##sys#cons
(##sys#ensure-transformer
,body
',var)
(##sys#current-environment))))
'(##core#undefined) )
e se dest ldest h ln)))
((##core#let-compiler-syntax)
(let ((bs (map
(lambda (b)
(##sys#check-syntax
'let-compiler-syntax b '(symbol . #(_ 0 1)))
(let ((name (lookup (car b) se)))
(list
name
(and (pair? (cdr b))
(cons (##sys#ensure-transformer
(##sys#eval/meta (cadr b))
(##sys#strip-syntax (car b)))
se))
(##sys#get name '##compiler#compiler-syntax) ) ) )
(cadr x))))
(dynamic-wind
(lambda ()
(for-each
(lambda (b)
(##sys#put! (car b) '##compiler#compiler-syntax (cadr b)))
bs) )
(lambda ()
(walk
(##sys#canonicalize-body
(cddr x) se compiler-syntax-enabled)
e se dest ldest h ln) )
(lambda ()
(for-each
(lambda (b)
(##sys#put!
(car b)
'##compiler#compiler-syntax (caddr b)))
bs) ) ) ) )
((##core#include)
(walk
`(##core#begin
,@(fluid-let ((##sys#default-read-info-hook read-info-hook))
(##sys#include-forms-from-file (cadr x))))
e se dest ldest h ln))
((##core#let-module-alias)
(##sys#with-module-aliases
(map (lambda (b)
(##sys#check-syntax 'functor b '(symbol symbol))
(##sys#strip-syntax b))
(cadr x))
(lambda ()
(walk `(##core#begin ,@(cddr x)) e se dest ldest h ln))))
((##core#module)
(let* ((name (##sys#strip-syntax (cadr x)))
(exports
(or (eq? #t (caddr x))
(map (lambda (exp)
(cond ((symbol? exp) exp)
((and (pair? exp)
(let loop ((exp exp))
(or (null? exp)
(and (symbol? (car exp))
(loop (cdr exp))))))
exp)
(else
(##sys#syntax-error-hook
'module
"invalid export syntax" exp name))))
(##sys#strip-syntax (caddr x)))))
(csyntax compiler-syntax))
(when (##sys#current-module)
(##sys#syntax-error-hook
'module "modules may not be nested" name))
(let-values (((body mreg)
(parameterize ((##sys#current-module
(##sys#register-module name exports) )
(##sys#current-environment '())
(##sys#macro-environment
##sys#initial-macro-environment)
(##sys#module-alias-environment
(##sys#module-alias-environment)))
(##sys#with-property-restore
(lambda ()
(let loop ((body (cdddr x)) (xs '()))
(cond
((null? body)
(handle-exceptions ex
(begin
;; avoid backtrace
(print-error-message ex (current-error-port))
(exit 1))
(##sys#finalize-module (##sys#current-module)))
(cond ((or (assq name import-libraries) all-import-libraries)
=> (lambda (il)
(when enable-module-registration
(emit-import-lib name il))
;; Remove from list to avoid error
(when (pair? il)
(set! import-libraries
(delete il import-libraries)))
(values
(reverse xs)
'((##core#undefined)))))
((not enable-module-registration)
(values
(reverse xs)
'((##core#undefined))))
(else
(values
(reverse xs)
(if standalone-executable
'()
(##sys#compiled-module-registration
(##sys#current-module)))))))
(else
(loop
(cdr body)
(cons (walk
(car body)
e ;?
(##sys#current-environment)
#f #f h ln)
xs))))))))))
(let ((body
(canonicalize-begin-body
(append
(parameterize ((##sys#current-module #f)
(##sys#macro-environment
(##sys#meta-macro-environment)))
(map
(lambda (x)
(walk
x
e ;?
(##sys#current-meta-environment) #f #f h ln) )
mreg))
body))))
(do ((cs compiler-syntax (cdr cs)))
((eq? cs csyntax))
(##sys#put! (caar cs) '##compiler#compiler-syntax (cdar cs)))
(set! compiler-syntax csyntax)
body))))
((##core#loop-lambda) ;XXX is this really needed?
(let* ([vars (cadr x)]
[obody (cddr x)]
[aliases (map gensym vars)]
(se2 (##sys#extend-se se vars aliases))
[body
(walk
(##sys#canonicalize-body obody se2 compiler-syntax-enabled)
(append aliases e)
se2 #f #f dest ln) ] )
(set-real-names! aliases vars)
`(##core#lambda ,aliases ,body) ) )
((##core#set!)
(let* ([var0 (cadr x)]
[var (lookup var0 se)]
[ln (get-line x)]
[val (caddr x)] )
(when (memq var unlikely-variables)
(warning
(sprintf "assignment to variable `~s' possibly unintended"
var)))
(cond ((assq var foreign-variables)
=> (lambda (fv)
(let ([type (second fv)]
[tmp (gensym)] )
(walk
`(let ([,tmp ,(foreign-type-convert-argument val type)])
(##core#inline_update
(,(third fv) ,type)
,(foreign-type-check tmp type) ) )
e se #f #f h ln))))
((assq var location-pointer-map)
=> (lambda (a)
(let* ([type (third a)]
[tmp (gensym)] )
(walk
`(let ([,tmp ,(foreign-type-convert-argument val type)])
(##core#inline_loc_update
(,type)
,(second a)
,(foreign-type-check tmp type) ) )
e se #f #f h ln))))
(else
(unless (memq var e) ; global?
(set! var (or (##sys#get var '##core#primitive)
(##sys#alias-global-hook var #t dest)))
(when safe-globals-flag
(mark-variable var '##compiler#always-bound-to-procedure)
(mark-variable var '##compiler#always-bound))
(when emit-debug-info
(set! val
`(let ((,var ,val))
(##core#debug-event "C_DEBUG_GLOBAL_ASSIGN" ',var)
,var))))
(cond ((##sys#macro? var)
(warning
(sprintf "assigned global variable `~S' is syntax ~A"
var
(if ln (sprintf "(~a)" ln) "") ))
(when undefine-shadowed-macros (##sys#undefine-macro! var) ) )
((and ##sys#notices-enabled
(assq var (##sys#current-environment)))
(##sys#notice "assignment to imported value binding" var)))
(when (keyword? var)
(warning (sprintf "assignment to keyword `~S'" var) ))
`(set! ,var ,(walk val e se var0 (memq var e) h ln))))))
((##core#debug-event)
`(##core#debug-event
,(unquotify (cadr x) se)
,ln ; this arg is added - from this phase on ##core#debug-event has an additional argument!
,@(map (lambda (arg)
(unquotify (walk arg e se #f #f h ln) se))
(cddr x))))
((##core#inline)
`(##core#inline
,(unquotify (cadr x) se) ,@(mapwalk (cddr x) e se h ln)))
((##core#inline_allocate)
`(##core#inline_allocate
,(map (cut unquotify <> se) (second x))
,@(mapwalk (cddr x) e se h ln)))
((##core#inline_update)
`(##core#inline_update ,(cadr x) ,(walk (caddr x) e se #f #f h ln)) )
((##core#inline_loc_update)
`(##core#inline_loc_update
,(cadr x)
,(walk (caddr x) e se #f #f h ln)
,(walk (cadddr x) e se #f #f h ln)) )
((##core#compiletimetoo ##core#elaborationtimetoo)
(let ((exp (cadr x)))
(##sys#eval/meta exp)
(walk exp e se dest #f h ln) ) )
((##core#compiletimeonly ##core#elaborationtimeonly)
(##sys#eval/meta (cadr x))
'(##core#undefined) )
((##core#begin ##core#toplevel-begin)
(if (pair? (cdr x))
(canonicalize-begin-body
(let fold ([xs (cdr x)])
(let ([x (car xs)]
[r (cdr xs)] )
(if (null? r)
(list (walk x e se dest ldest h ln))
(cons (walk x e se #f #f h ln) (fold r)) ) ) ) )
'(##core#undefined) ) )
((##core#foreign-lambda)
(walk (expand-foreign-lambda x #f) e se dest ldest h ln) )
((##core#foreign-safe-lambda)
(walk (expand-foreign-lambda x #t) e se dest ldest h ln) )
((##core#foreign-lambda*)
(walk (expand-foreign-lambda* x #f) e se dest ldest h ln) )
((##core#foreign-safe-lambda*)
(walk (expand-foreign-lambda* x #t) e se dest ldest h ln) )
((##core#foreign-primitive)
(walk (expand-foreign-primitive x) e se dest ldest h ln) )
((##core#define-foreign-variable)
(let* ([var (##sys#strip-syntax (second x))]
[type (##sys#strip-syntax (third x))]
[name (if (pair? (cdddr x))
(fourth x)
(symbol->string var) ) ] )
(set! foreign-variables
(cons (list var type
(if (string? name)
name
(symbol->string name)))
foreign-variables))
'(##core#undefined) ) )
((##core#define-foreign-type)
(let ([name (second x)]
[type (##sys#strip-syntax (third x))]
[conv (cdddr x)] )
(cond [(pair? conv)
(let ([arg (gensym)]
[ret (gensym)] )
(##sys#hash-table-set! foreign-type-table name (vector type arg ret))
(mark-variable arg '##compiler#always-bound)
(mark-variable ret '##compiler#always-bound)
(hide-variable arg)
(hide-variable ret)
(walk
`(##core#begin
(define ,arg ,(first conv))
(define
,ret
,(if (pair? (cdr conv)) (second conv) '##sys#values)) )
e se dest ldest h ln) ) ]
[else
(##sys#hash-table-set! foreign-type-table name type)
'(##core#undefined) ] ) ) )
((##core#define-external-variable)
(let* ([sym (second x)]
[name (symbol->string sym)]
[type (third x)]
[exported (fourth x)]
[rname (make-random-name)] )
(unless exported (set! name (symbol->string (fifth x))))
(set! external-variables (cons (vector name type exported) external-variables))
(set! foreign-variables
(cons (list rname 'c-pointer (string-append "&" name))
foreign-variables) )
(set! external-to-pointer (alist-cons sym rname external-to-pointer))
'(##core#undefined) ) )
((##core#let-location)
(let* ([var (second x)]
[type (##sys#strip-syntax (third x))]
[alias (gensym)]
[store (gensym)]
[init (and (pair? (cddddr x)) (fourth x))] )
(set-real-name! alias var)
(set! location-pointer-map
(cons (list alias store type) location-pointer-map) )
(walk
`(let (,(let ([size (words (estimate-foreign-result-location-size type))])
;; Add 2 words: 1 for the header, 1 for double-alignment:
;; Note: C_a_i_bytevector takes number of words, not bytes
(list
store
`(##core#inline_allocate
("C_a_i_bytevector" ,(+ 2 size))
',size)) ) )
(##core#begin
,@(if init
`((##core#set! ,alias ,init))
'() )
,(if init (fifth x) (fourth x)) ) )
e (alist-cons var alias se)
dest ldest h ln) ) )
((##core#define-inline)
(let* ((name (second x))
(val `(##core#lambda ,@(cdaddr x))))
(##sys#hash-table-set! inline-table name val)
(set! inline-table-used #t)
'(##core#undefined)))
((##core#define-constant)
(let* ([name (second x)]
[valexp (third x)]
[val (handle-exceptions ex
;; could show line number here
(quit "error in constant evaluation of ~S for named constant `~S'"
valexp name)
(if (and (not (symbol? valexp))
(collapsable-literal? valexp))
valexp
(eval
`(##core#let
,defconstant-bindings ,valexp)) ) ) ] )
(set! constants-used #t)
(set! defconstant-bindings
(cons (list name `',val) defconstant-bindings))
(cond ((collapsable-literal? val)
(##sys#hash-table-set! constant-table name (list val))
'(##core#undefined) )
((basic-literal? val)
(let ([var (gensym "constant")])
(##sys#hash-table-set! constant-table name (list var))
(hide-variable var)
(mark-variable var '##compiler#constant)
(mark-variable var '##compiler#always-bound)
(walk `(define ,var ',val) e se #f #f h ln) ) )
(else
(quit "invalid compile-time value for named constant `~S'"
name)))))
((##core#declare)
(walk
`(##core#begin
,@(map (lambda (d)
(process-declaration
d se
(lambda (id)
(memq (lookup id se) e))))
(cdr x) ) )
e '() #f #f h ln) )
((##core#foreign-callback-wrapper)
(let-values ([(args lam) (split-at (cdr x) 4)])
(let* ([lam (car lam)]
[raw-c-name (cadr (first args))]
[name (##sys#alias-global-hook raw-c-name #t dest)]
[rtype (cadr (third args))]
[atypes (cadr (fourth args))]
[vars (second lam)] )
(if (valid-c-identifier? raw-c-name)
(set! callback-names
(cons (cons raw-c-name name) callback-names))
(quit "name `~S' of external definition is not a valid C identifier"
raw-c-name) )
(when (or (not (proper-list? vars))
(not (proper-list? atypes))
(not (= (length vars) (length atypes))) )
(syntax-error
"non-matching or invalid argument list to foreign callback-wrapper"
vars atypes) )
`(##core#foreign-callback-wrapper
,@(mapwalk args e se h ln)
,(walk `(##core#lambda
,vars
(##core#let
,(let loop ([vars vars] [types atypes])
(if (null? vars)
'()
(let ([var (car vars)]
[type (car types)] )
(cons
(list
var
(foreign-type-convert-result
(finish-foreign-result
(final-foreign-type type)
var)
type) )
(loop (cdr vars) (cdr types)) ) ) ) )
,(foreign-type-convert-argument
`(##core#let
()
,@(cond
((member
rtype
'((const nonnull-c-string)
(const nonnull-unsigned-c-string)
nonnull-unsigned-c-string
nonnull-c-string))
`((##sys#make-c-string
(##core#let
() ,@(cddr lam))
',name)))
((member
rtype
'((const c-string*)
(const unsigned-c-string*)
unsigned-c-string*
c-string*
c-string-list
c-string-list*))
(syntax-error
"not a valid result type for callback procedures"
rtype
name) )
((member
rtype
'(c-string
(const unsigned-c-string)
unsigned-c-string
(const c-string)) )
`((##core#let
((r (##core#let () ,@(cddr lam))))
(,(macro-alias 'and se)
r
(##sys#make-c-string r ',name)) ) ) )
(else (cddr lam)) ) )
rtype) ) )
e se #f #f h ln) ) ) ) )
((##core#location)
(let ([sym (cadr x)])
(if (symbol? sym)
(cond [(assq (lookup sym se) location-pointer-map)
=> (lambda (a)
(walk
`(##sys#make-locative ,(second a) 0 #f 'location)
e se #f #f h ln) ) ]
[(assq sym external-to-pointer)
=> (lambda (a) (walk (cdr a) e se #f #f h ln)) ]
[(assq sym callback-names)
`(##core#inline_ref (,(symbol->string sym) c-pointer)) ]
[else
(walk
`(##sys#make-locative ,sym 0 #f 'location)
e se #f #f h ln) ] )
(walk
`(##sys#make-locative ,sym 0 #f 'location)
e se #f #f h ln) ) ) )
(else
(let* ((x2 (fluid-let ((##sys#syntax-context
(cons name ##sys#syntax-context)))
(mapwalk x e se h ln)))
(head2 (car x2))
(old (##sys#hash-table-ref line-number-database-2 head2)) )
(when ln
(##sys#hash-table-set!
line-number-database-2
head2
(cons name (alist-cons x2 ln (if old (cdr old) '()))) ) )
x2) ) ) ] ) ) ) )
((not (proper-list? x))
(##sys#syntax-error/context "malformed expression" x) )
((constant? (car x))
(emit-syntax-trace-info x #f)
(warning "literal in operator position" x)
(mapwalk x e se h outer-ln) )
(else
(emit-syntax-trace-info x #f)
(let ((tmp (gensym)))
(walk
`(##core#let
((,tmp ,(car x)))
(,tmp ,@(cdr x)))
e se dest ldest h outer-ln)))))
(define (mapwalk xs e se h ln)
(map (lambda (x) (walk x e se #f #f h ln)) xs) )
(when (memq 'c debugging-chicken) (newline) (pretty-print exp))
(##sys#clear-trace-buffer)
;; Process visited definitions and main expression:
(walk
`(##core#begin
,@(let ([p (reverse pending-canonicalizations)])
(set! pending-canonicalizations '())
p)
,(begin
(set! extended-bindings (append internal-bindings extended-bindings))
exp) )
'() (##sys#current-environment) #f #f #f #f) ) )
(define (process-declaration spec se local?)
(define (check-decl spec minlen . maxlen)
(let ([n (length (cdr spec))])
(if (or (< n minlen) (> n (optional maxlen 99999)))
(syntax-error "invalid declaration" spec) ) ) )
(define (stripa x) ; global aliasing
(##sys#globalize x se))
(define (strip x) ; raw symbol
(##sys#strip-syntax x))
(define stripu ##sys#strip-syntax)
(define (globalize-all syms)
(filter-map
(lambda (var)
(cond ((local? var)
(note-local var)
#f)
(else (##sys#globalize var se))))
syms))
(define (note-local var)
(##sys#notice
(sprintf "ignoring declaration for locally bound variable `~a'" var)))
(call-with-current-continuation
(lambda (return)
(unless (pair? spec)
(syntax-error "invalid declaration specification" spec) )
;(pp `(DECLARE: ,(strip spec)))
(case (##sys#strip-syntax (car spec)) ; no global aliasing
((uses)
(let ((us (stripu (cdr spec))))
(apply register-feature! us)
(when (pair? us)
(##sys#hash-table-update!
file-requirements 'static
(cut lset-union eq? us <>)
(lambda () us))
(let ((units (map (lambda (u) (string->c-identifier (stringify u))) us)))
(set! used-units (append used-units units)) ) ) ) )
((unit)
(check-decl spec 1 1)
(let* ([u (stripu (cadr spec))]
[un (string->c-identifier (stringify u))] )
(when (and unit-name (not (string=? unit-name un)))
(warning "unit was already given a name (new name is ignored)") )
(set! unit-name un) ) )
((standard-bindings)
(if (null? (cdr spec))
(set! standard-bindings default-standard-bindings)
(set! standard-bindings (append (stripa (cdr spec)) standard-bindings)) ) )
((extended-bindings)
(if (null? (cdr spec))
(set! extended-bindings default-extended-bindings)
(set! extended-bindings (append (stripa (cdr spec)) extended-bindings)) ) )
((usual-integrations)
(cond [(null? (cdr spec))
(set! standard-bindings default-standard-bindings)
(set! extended-bindings default-extended-bindings) ]
[else
(let ([syms (stripa (cdr spec))])
(set! standard-bindings (lset-intersection eq? syms default-standard-bindings))
(set! extended-bindings (lset-intersection eq? syms default-extended-bindings)) ) ] ) )
((number-type)
(check-decl spec 1 1)
(set! number-type (strip (cadr spec))))
((fixnum fixnum-arithmetic) (set! number-type 'fixnum))
((generic) (set! number-type 'generic))
((unsafe) (set! unsafe #t))
((safe) (set! unsafe #f))
((no-bound-checks) (set! no-bound-checks #t))
((no-argc-checks) (set! no-argc-checks #t))
((no-procedure-checks) (set! no-procedure-checks #t))
((interrupts-enabled) (set! insert-timer-checks #t))
((disable-interrupts) (set! insert-timer-checks #f))
((always-bound)
(for-each (cut mark-variable <> '##compiler#always-bound) (stripa (cdr spec))))
((safe-globals) (set! safe-globals-flag #t))
((no-procedure-checks-for-usual-bindings)
(for-each
(cut mark-variable <> '##compiler#always-bound-to-procedure)
(append default-standard-bindings default-extended-bindings))
(for-each
(cut mark-variable <> '##compiler#always-bound)
(append default-standard-bindings default-extended-bindings)))
((no-procedure-checks-for-toplevel-bindings)
(set! no-global-procedure-checks #t))
((bound-to-procedure)
(let ((vars (globalize-all (cdr spec))))
(for-each (cut mark-variable <> '##compiler#always-bound-to-procedure) vars)
(for-each (cut mark-variable <> '##compiler#always-bound) vars)))
((foreign-declare)
(let ([fds (cdr spec)])
(if (every string? fds)
(set! foreign-declarations (append foreign-declarations fds))
(syntax-error 'declare "invalid declaration" spec) ) ) )
((block) (set! block-compilation #t))
((separate) (set! block-compilation #f))
((keep-shadowed-macros) (set! undefine-shadowed-macros #f))
((unused)
(for-each (cut mark-variable <> '##compiler#unused) (globalize-all (cdr spec))))
((enforce-argument-types)
(for-each
(cut mark-variable <> '##compiler#enforce)
(globalize-all (cdr spec))))
((not)
(check-decl spec 1)
(case (##sys#strip-syntax (second spec)) ; strip all
[(standard-bindings)
(if (null? (cddr spec))
(set! standard-bindings '())
(set! standard-bindings
(lset-difference eq? default-standard-bindings
(stripa (cddr spec))))) ]
[(extended-bindings)
(if (null? (cddr spec))
(set! extended-bindings '())
(set! extended-bindings
(lset-difference eq? default-extended-bindings
(stripa (cddr spec))) )) ]
[(inline)
(if (null? (cddr spec))
(set! inline-locally #f)
(for-each
(cut mark-variable <> '##compiler#inline 'no)
(globalize-all (cddr spec)))) ]
[(usual-integrations)
(cond [(null? (cddr spec))
(set! standard-bindings '())
(set! extended-bindings '()) ]
[else
(let ([syms (stripa (cddr spec))])
(set! standard-bindings (lset-difference eq? default-standard-bindings syms))
(set! extended-bindings (lset-difference eq? default-extended-bindings syms)) ) ] ) ]
((inline-global)
(set! enable-inline-files #t)
(when (pair? (cddr spec))
(for-each
(cut mark-variable <> '##compiler#inline-global 'no)
(globalize-all (cddr spec)))))
[else
(check-decl spec 1 1)
(let ((id (strip (cadr spec))))
(case id
[(interrupts-enabled) (set! insert-timer-checks #f)]
[(safe) (set! unsafe #t)]
[else (warning "unsupported declaration specifier" id)]))]))
((compile-syntax)
(set! ##sys#enable-runtime-macros #t))
((block-global hide)
(let ([syms (globalize-all (cdr spec))])
(if (null? syms)
(set! block-compilation #t)
(for-each hide-variable syms))))
((export)
(set! block-compilation #t)
(let ((syms (globalize-all (cdr spec))))
(for-each export-variable syms)))
((emit-external-prototypes-first)
(set! external-protos-first #t) )
((inline)
(if (null? (cdr spec))
(set! inline-locally #t)
(for-each
(cut mark-variable <> '##compiler#local)
(globalize-all (cdr spec)))))
((inline-limit)
(check-decl spec 1 1)
(let ([n (cadr spec)])
(if (number? n)
(set! inline-max-size n)
(warning
"invalid argument to `inline-limit' declaration"
spec) ) ) )
((pure)
(let ((syms (cdr spec)))
(if (every symbol? syms)
(for-each
(cut mark-variable <> '##compiler#pure #t)
(globalize-all syms))
(quit "invalid arguments to `pure' declaration: ~S" spec))))
((emit-import-library)
(set! import-libraries
(append
import-libraries
(map (lambda (il)
(cond ((symbol? il)
(cons il (string-append (symbol->string il) ".import.scm")) )
((and (list? il) (= 2 (length il))
(symbol? (car il)) (string (cadr il)))
(cons (car il) (cadr il)))
(else
(warning
"invalid import-library specification" il))))
(strip (cdr spec))))))
((profile)
(set! emit-profile #t)
(cond ((null? (cdr spec))
(set! profiled-procedures 'all) )
(else
(set! profiled-procedures 'some)
(for-each
(cut mark-variable <> '##compiler#profile)
(globalize-all (cdr spec))))))
((local)
(cond ((null? (cdr spec))
(set! local-definitions #t) )
(else
(for-each
(cut mark-variable <> '##compiler#local)
(stripa (cdr spec))))))
((inline-global)
(set! enable-inline-files #t)
(set! inline-locally #t)
(when (pair? (cdr spec))
(for-each
(cut mark-variable <> '##compiler#inline-global 'yes)
(globalize-all (cdr spec)))))
((type)
(for-each
(lambda (spec)
(if (not (and (list? spec)
(>= (length spec) 2)
(symbol? (car spec))))
(warning "illegal type declaration" (##sys#strip-syntax spec))
(let ((name (##sys#globalize (car spec) se))
(type (##sys#strip-syntax (cadr spec))))
(if (local? (car spec))
(note-local (car spec))
(let-values (((type pred pure) (validate-type type name)))
(cond (type
;; HACK: since `:' doesn't have access to the SE, we
;; fixup the procedure name if type is a named procedure type
;; (We only have access to the SE for ##sys#globalize in here).
;; Quite terrible.
(when (and (pair? type)
(eq? 'procedure (car type))
(symbol? (cadr type)))
(set-car! (cdr type) name))
(mark-variable name '##compiler#type type)
(mark-variable name '##compiler#type-source 'local)
(when pure
(mark-variable name '##compiler#pure #t))
(when pred
(mark-variable name '##compiler#predicate pred))
(when (pair? (cddr spec))
(install-specializations
name
(##sys#strip-syntax (cddr spec)))))
(else
(warning
"illegal `type' declaration"
(##sys#strip-syntax spec)))))))))
(cdr spec)))
((predicate)
(for-each
(lambda (spec)
(cond ((and (list? spec) (symbol? (car spec)) (= 2 (length spec)))
(let ((name (##sys#globalize (car spec) se))
(type (##sys#strip-syntax (cadr spec))))
(if (local? (car spec))
(note-local (car spec))
(let-values (((type pred pure) (validate-type type name)))
(if (and type (not pred))
(mark-variable name '##compiler#predicate type)
(warning "illegal `predicate' declaration" spec))))))
(else
(warning "illegal `type' declaration item" spec))))
(cdr spec)))
((specialize)
(set! enable-specialization #t))
((strict-types)
(set! strict-variable-types #t))
(else (warning "unknown declaration specifier" spec)) )
'(##core#undefined) ) ) )
;;; Expand "foreign-lambda"/"foreign-safe-lambda" forms and add item to stub-list:
(define-record-type foreign-stub
(make-foreign-stub id return-type name argument-types argument-names body cps callback)
foreign-stub?
(id foreign-stub-id) ; symbol
(return-type foreign-stub-return-type) ; type-specifier
(name foreign-stub-name) ; string or #f
(argument-types foreign-stub-argument-types) ; (type-specifier...)
(argument-names foreign-stub-argument-names) ; #f or (symbol ...)
(body foreign-stub-body) ; #f or string
(cps foreign-stub-cps) ; boolean
(callback foreign-stub-callback)) ; boolean
(define (create-foreign-stub rtype sname argtypes argnames body callback cps)
;; try to describe a foreign-lambda type specification
;; eg. (type->symbol '(c-pointer (struct "point"))) => point*
(define (type->symbol type-spec)
(let loop ([type type-spec])
(cond
((null? type) 'a)
((list? type)
(case (car type)
((c-pointer) (string->symbol (conc (loop (cdr type)) "*"))) ;; if pointer, append *
((const struct) (loop (cdr type))) ;; ignore these
(else (loop (car type)))))
((or (symbol? type) (string? type)) type)
(else 'a))))
(let* ((rtype (##sys#strip-syntax rtype))
(argtypes (##sys#strip-syntax argtypes))
[params (if argnames
(map gensym argnames)
(map (o gensym type->symbol) argtypes))]
[f-id (gensym 'stub)]
[bufvar (gensym)]
[rsize (estimate-foreign-result-size rtype)] )
(when sname (set-real-name! f-id (string->symbol sname)))
(set! foreign-lambda-stubs
(cons (make-foreign-stub f-id rtype sname argtypes argnames body cps callback)
foreign-lambda-stubs) )
(let ([rsize (if callback (+ rsize 24) rsize)] ; 24 -> has to hold cons on 64-bit platforms!
[head (if cps
`((##core#primitive ,f-id))
`(##core#inline ,f-id) ) ]
[rest (map (lambda (p t) (foreign-type-check (foreign-type-convert-argument p t) t)) params argtypes)] )
`(lambda ,params
;; Do minor GC (if callback) to make room on stack:
,@(if callback '((##sys#gc #f)) '())
,(if (zero? rsize)
(foreign-type-convert-result (append head (cons '(##core#undefined) rest)) rtype)
(let ([ft (final-foreign-type rtype)]
[ws (words rsize)] )
`(let ([,bufvar (##core#inline_allocate ("C_a_i_bytevector" ,(+ 2 ws)) ',ws)])
,(foreign-type-convert-result
(finish-foreign-result ft (append head (cons bufvar rest)))
rtype) ) ) ) ) ) ) )
(define (expand-foreign-lambda exp callback?)
(let* ([name (third exp)]
[sname (cond ((symbol? name) (symbol->string (##sys#strip-syntax name)))
((string? name) name)
(else (quit "name `~s' of foreign procedure has wrong type" name)) ) ]
[rtype (second exp)]
[argtypes (cdddr exp)] )
(create-foreign-stub rtype sname argtypes #f #f callback? callback?) ) )
(define (expand-foreign-lambda* exp callback?)
(let* ([rtype (second exp)]
[args (third exp)]
[body (apply string-append (cdddr exp))]
[argtypes (map (lambda (x) (car x)) args)]
;; C identifiers aren't hygienically renamed inside body strings
[argnames (map cadr (##sys#strip-syntax args))] )
(create-foreign-stub rtype #f argtypes argnames body callback? callback?) ) )
;; TODO: Try to fold this procedure into expand-foreign-lambda*
(define (expand-foreign-primitive exp)
(let* ([hasrtype (and (pair? (cddr exp)) (not (string? (caddr exp))))]
[rtype (if hasrtype (second exp) 'void)]
[args (##sys#strip-syntax (if hasrtype (third exp) (second exp)))]
[body (apply string-append (if hasrtype (cdddr exp) (cddr exp)))]
[argtypes (map (lambda (x) (car x)) args)]
;; C identifiers aren't hygienically renamed inside body strings
[argnames (map cadr (##sys#strip-syntax args))] )
(create-foreign-stub rtype #f argtypes argnames body #f #t) ) )
;;; Traverse expression and update line-number db with all contained calls:
(define (update-line-number-database! exp ln)
(define (mapupdate xs)
(let loop ((xs xs))
(when (pair? xs)
(walk (car xs))
(loop (cdr xs)) ) ) )
(define (walk x)
(cond ((not-pair? x))
((symbol? (car x))
(let* ((name (car x))
(old (or (##sys#hash-table-ref ##sys#line-number-database name) '())) )
(unless (assq x old)
(##sys#hash-table-set! ##sys#line-number-database name (alist-cons x ln old)) )
(mapupdate (cdr x)) ) )
(else (mapupdate x)) ) )
(walk exp) )
;;; Convert canonicalized node-graph into continuation-passing-style:
(define (perform-cps-conversion node)
(define (cps-lambda id llist subs k)
(let ([t1 (gensym 'k)])
(k (make-node
'##core#lambda (list id #t (cons t1 llist) 0)
(list (walk (car subs)
(lambda (r)
(make-node '##core#call (list #t) (list (varnode t1) r)) ) ) ) ) ) ) )
(define (node-for-var? node var)
(and (eq? (node-class node) '##core#variable)
(eq? (car (node-parameters node)) var)))
(define (walk n k)
(let ((subs (node-subexpressions n))
(params (node-parameters n))
(class (node-class n)) )
(case (node-class n)
((##core#variable quote ##core#undefined ##core#primitive) (k n))
((if) (let* ((t1 (gensym 'k))
(t2 (gensym 'r))
(k1 (lambda (r) (make-node '##core#call (list #t) (list (varnode t1) r)))) )
(make-node
'let
(list t1)
(list (make-node '##core#lambda (list (gensym-f-id) #f (list t2) 0)
(list (k (varnode t2))) )
(walk (car subs)
(lambda (v)
(make-node 'if '()
(list v
(walk (cadr subs) k1)
(walk (caddr subs) k1) ) ) ) ) ) ) ) )
((let)
(let loop ((vars params) (vals subs))
(if (null? vars)
(walk (car vals) k)
(walk (car vals)
(lambda (r)
(if (node-for-var? r (car vars)) ; Don't generate unneccessary lets
(loop (cdr vars) (cdr vals))
(make-node 'let
(list (car vars))
(list r (loop (cdr vars) (cdr vals))) )) ) ) ) ) )
((lambda ##core#lambda) (cps-lambda (gensym-f-id) (first params) subs k))
((set!) (let ((t1 (gensym 't)))
(walk (car subs)
(lambda (r)
(make-node 'let (list t1)
(list (make-node 'set! (list (first params)) (list r))
(k (varnode t1)) ) ) ) ) ) )
((##core#foreign-callback-wrapper)
(let ([id (gensym-f-id)]
[lam (first subs)] )
(set! foreign-callback-stubs
(cons (apply make-foreign-callback-stub id params) foreign-callback-stubs) )
;; mark to avoid leaf-routine optimization
(mark-variable id '##compiler#callback-lambda)
(cps-lambda id (first (node-parameters lam)) (node-subexpressions lam) k) ) )
((##core#inline ##core#inline_allocate ##core#inline_ref ##core#inline_update ##core#inline_loc_ref
##core#inline_loc_update ##core#debug-event)
(walk-inline-call class params subs k) )
((##core#call) (walk-call (car subs) (cdr subs) params k))
((##core#callunit) (walk-call-unit (first params) k))
((##core#the ##core#the/result)
;; remove "the" nodes, as they are not used after scrutiny
(walk (car subs) k))
((##core#typecase)
;; same here, the last clause is chosen, exp is dropped
(walk (last subs) k))
(else (bomb "bad node (cps)")) ) ) )
(define (walk-call fn args params k)
(let ((t0 (gensym 'k))
(t3 (gensym 'r)) )
(make-node
'let (list t0)
(list (make-node '##core#lambda (list (gensym-f-id) #f (list t3) 0)
(list (k (varnode t3))) )
(walk-arguments
args
(lambda (vars)
(walk fn
(lambda (r)
(make-node '##core#call params (cons* r (varnode t0) vars) ) ) ) ) ) ) ) ) )
(define (walk-call-unit unitname k)
(let ((t0 (gensym 'k))
(t3 (gensym 'r)) )
(make-node
'let (list t0)
(list (make-node '##core#lambda (list (gensym-f-id) #f (list t3) 0)
(list (k (varnode t3))) )
(make-node '##core#callunit (list unitname)
(list (varnode t0)) ) ) ) ) )
(define (walk-inline-call class op args k)
(walk-arguments
args
(lambda (vars)
(k (make-node class op vars)) ) ) )
(define (walk-arguments args wk)
(let loop ((args args) (vars '()))
(cond ((null? args) (wk (reverse vars)))
((atomic? (car args))
(loop (cdr args) (cons (car args) vars)) )
(else
(let ((t1 (gensym 'a)))
(walk (car args)
(lambda (r)
(if (node-for-var? r t1) ; Don't generate unneccessary lets
(loop (cdr args) (cons (varnode t1) vars) )
(make-node 'let (list t1)
(list r
(loop (cdr args)
(cons (varnode t1) vars) ) ) )) ) ) ) ) ) ) )
(define (atomic? n)
(let ((class (node-class n)))
(or (memq class '(quote ##core#variable ##core#undefined))
(and (memq class '(##core#inline_allocate
##core#inline_ref ##core#inline_update
##core#inline_loc_ref ##core#inline_loc_update))
(every atomic? (node-subexpressions n)) ) ) ) )
(walk node values) )
;;; Foreign callback stub type:
(define-record-type foreign-callback-stub
(make-foreign-callback-stub id name qualifiers return-type argument-types)
foreign-callback-stub?
(id foreign-callback-stub-id) ; symbol
(name foreign-callback-stub-name) ; string
(qualifiers foreign-callback-stub-qualifiers) ; string
(return-type foreign-callback-stub-return-type) ; type-specifier
(argument-types foreign-callback-stub-argument-types)) ; (type-specifier ...)
;;; Perform source-code analysis:
(define (analyze-expression node)
;; Avoid crowded hash tables by using previous run's size as heuristic
(let* ((db-size (fx* (fxmax current-analysis-database-size 1) 3))
(db (make-vector db-size '())))
(define (grow n)
(set! current-program-size (+ current-program-size n)) )
;; fullenv is constantly (append localenv env). It's there to avoid
;; exponential behaviour by APPEND calls when compiling deeply nested LETs
(define (walk n env localenv fullenv here call)
(let ((subs (node-subexpressions n))
(params (node-parameters n))
(class (node-class n)) )
(grow 1)
(case class
((quote ##core#undefined ##core#proc) #f)
((##core#variable)
(let ((var (first params)))
(ref var n)
(unless (memq var localenv)
(grow 1)
(cond ((memq var env)
(put! db var 'captured #t))
((not (get db var 'global))
(put! db var 'global #t) ) ) ) ) )
((##core#callunit ##core#recurse)
(grow 1)
(walkeach subs env localenv fullenv here #f) )
((##core#call)
(grow 1)
(let ([fun (car subs)])
(when (eq? '##core#variable (node-class fun))
(let ((name (first (node-parameters fun))))
(collect! db name 'call-sites (cons here n))))
(walk (first subs) env localenv fullenv here #t)
(walkeach (cdr subs) env localenv fullenv here #f) ) )
((let ##core#let)
(let ([env2 (append params fullenv)])
(let loop ([vars params] [vals subs])
(if (null? vars)
(walk (car vals) env (append params localenv) env2 here #f)
(let ([var (car vars)]
[val (car vals)] )
(put! db var 'home here)
(assign var val env2 here)
(walk val env localenv fullenv here #f)
(loop (cdr vars) (cdr vals)) ) ) ) ) )
((lambda) ; this is an intermediate lambda, slightly different
(grow 1) ; from '##core#lambda nodes (params = (LLIST));
(decompose-lambda-list ; CPS will convert this into ##core#lambda
(first params)
(lambda (vars argc rest)
(for-each
(lambda (var) (put! db var 'unknown #t))
vars)
(let ([tl toplevel-scope])
(set! toplevel-scope #f)
(walk (car subs) fullenv vars (append vars fullenv) #f #f)
(set! toplevel-scope tl) ) ) ) )
((##core#lambda ##core#direct_lambda)
(grow 1)
(decompose-lambda-list
(third params)
(lambda (vars argc rest)
(let ([id (first params)]
[size0 current-program-size] )
(when here
(collect! db here 'contains id)
(put! db id 'contained-in here) )
(for-each
(lambda (var)
(put! db var 'home here)
(put! db var 'unknown #t) )
vars)
(when rest
(put! db rest 'rest-parameter 'list) )
(when (simple-lambda-node? n) (put! db id 'simple #t))
(let ([tl toplevel-scope])
(unless toplevel-lambda-id (set! toplevel-lambda-id id))
(when (and (second params) (not (eq? toplevel-lambda-id id)))
(set! toplevel-scope #f)) ; only if non-CPS lambda
(walk (car subs) fullenv vars (append vars fullenv) id #f)
(set! toplevel-scope tl)
;; decorate ##core#call node with size
(set-car! (cdddr (node-parameters n)) (- current-program-size size0)) ) ) ) ) )
((set! ##core#set!) ;XXX ##core#set! still used?
(let* ((var (first params))
(val (car subs)) )
(when (and first-analysis (not bootstrap-mode))
(case (variable-mark var '##compiler#intrinsic)
((standard)
(warning "redefinition of standard binding" var) )
((extended)
(warning "redefinition of extended binding" var) ) ))
(put! db var 'potential-value val)
(unless (memq var localenv)
(grow 1)
(cond ((memq var env)
(put! db var 'captured #t))
((not (get db var 'global))
(put! db var 'global #t) ) ) )
(assign var val fullenv here)
(unless toplevel-scope (put! db var 'assigned-locally #t))
(put! db var 'assigned #t)
(walk (car subs) env localenv fullenv here #f) ) )
((##core#primitive ##core#inline)
(let ((id (first params)))
(when (and first-analysis here (symbol? id) (##sys#hash-table-ref real-name-table id))
(set-real-name! id here) )
(walkeach subs env localenv fullenv here #f) ) )
(else (walkeach subs env localenv fullenv here #f)) ) ) )
(define (walkeach xs env lenv fenv here call)
(for-each (lambda (x) (walk x env lenv fenv here call)) xs) )
(define (assign var val env here)
(cond ((eq? '##core#undefined (node-class val))
(put! db var 'undefined #t) )
((and (eq? '##core#variable (node-class val)) ; assignment to itself
(eq? var (first (node-parameters val))) ) )
((or (memq var env)
(variable-mark var '##compiler#constant)
(not (variable-visible? var)))
(let ((props (get-all db var 'unknown 'value))
(home (get db var 'home)) )
(unless (assq 'unknown props)
(if (assq 'value props)
(put! db var 'unknown #t)
(if (or (not home) (eq? here home))
(put! db var 'value val)
(put! db var 'unknown #t) ) ) ) ) )
((and (or local-definitions
(variable-mark var '##compiler#local))
(not (get db var 'unknown)))
(let ((home (get db var 'home)))
(cond ((get db var 'local-value)
(put! db var 'unknown #t))
((or (not home) (eq? here home))
(put! db var 'local-value val) )
(else (put! db var 'unknown #t)))))
(else (put! db var 'unknown #t)) ) )
(define (ref var node)
(collect! db var 'references node) )
(define (quick-put! plist prop val)
(set-cdr! plist (alist-cons prop val (cdr plist))) )
;; Walk toplevel expression-node:
(debugging 'p "analysis traversal phase...")
(set! current-program-size 0)
(walk node '() '() '() #f #f)
;; Complete gathered database information:
(debugging 'p "analysis gathering phase...")
(set! current-analysis-database-size 0)
(##sys#hash-table-for-each
(lambda (sym plist)
(let ([unknown #f]
[value #f]
[local-value #f]
[pvalue #f]
[references '()]
[captured #f]
[call-sites '()]
[assigned #f]
[assigned-locally #f]
[undefined #f]
[global #f]
[rest-parameter #f]
[nreferences 0]
[ncall-sites 0] )
(set! current-analysis-database-size (fx+ current-analysis-database-size 1))
(for-each
(lambda (prop)
(case (car prop)
[(unknown) (set! unknown #t)]
[(references)
(set! references (cdr prop))
(set! nreferences (length references)) ]
[(captured) (set! captured #t)]
[(potential-value) (set! pvalue (cdr prop))]
[(call-sites)
(set! call-sites (cdr prop))
(set! ncall-sites (length call-sites)) ]
[(assigned) (set! assigned #t)]
[(assigned-locally) (set! assigned-locally #t)]
[(undefined) (set! undefined #t)]
[(global) (set! global #t)]
[(value) (set! value (cdr prop))]
[(local-value) (set! local-value (cdr prop))]
[(rest-parameter) (set! rest-parameter #t)] ) )
plist)
(set! value (and (not unknown) value))
(set! local-value (and (not unknown) local-value))
;; If this is the first analysis, register known local or potentially known global
;; lambda-value id's along with their names:
(when (and first-analysis
(eq? '##core#lambda
(and-let* ([val (or value (and global pvalue))])
(node-class val) ) ) )
(set-real-name! (first (node-parameters (or value pvalue))) sym) )
;; If this is the first analysis and the variable is global and has no references
;; and is hidden then issue warning:
(when (and first-analysis
global
(null? references)
(not (variable-mark sym '##compiler#unused))
(not (variable-hidden? sym))
(not (variable-visible? sym))
(not (variable-mark sym '##compiler#constant)) )
(##sys#notice
(sprintf "global variable `~S' is only locally visible and never used"
sym) ) )
;; Make 'boxed, if 'assigned & 'captured:
(when (and assigned captured)
(quick-put! plist 'boxed #t) )
;; Make 'contractable, if it has a procedure as known value, has only one use
;; and one call-site and if the lambda has no free non-global variables
;; or is an internal lambda. Make 'inlinable if
;; use/call count is not 1:
(cond (value
(let ((valparams (node-parameters value)))
(when (and (eq? '##core#lambda (node-class value))
(or (not (second valparams))
(every
(lambda (v) (get db v 'global))
(nth-value 0 (scan-free-variables value)) ) ) )
(if (and (= 1 nreferences) (= 1 ncall-sites))
(quick-put! plist 'contractable #t)
(quick-put! plist 'inlinable #t) ) ) ) )
(local-value
;; Make 'inlinable, if it is declared local and has a value
(let ((valparams (node-parameters local-value)))
(when (eq? '##core#lambda (node-class local-value))
(let-values (((vars hvars) (scan-free-variables local-value)))
(when (and (get db sym 'global)
(pair? hvars))
(quick-put! plist 'hidden-refs #t))
(when (or (not (second valparams))
(every
(lambda (v) (get db v 'global))
vars))
(quick-put! plist 'inlinable #t) ) ) ) ) )
((variable-mark sym '##compiler#inline-global) =>
(lambda (n)
(when (node? n)
(cond (assigned
(debugging
'i
"global inlining candidate was assigned and will not be inlined"
sym)
(mark-variable sym '##compiler#inline-global 'no))
(else
(let ((lparams (node-parameters n)))
(quick-put! plist 'inlinable #t)
(quick-put! plist 'local-value n))))))))
;; Make 'collapsable, if it has a known constant value which is either collapsable or is only
;; referenced once and if no assignments are made:
(when (and value
;; (not (assq 'assigned plist)) - If it has a known value, it's assigned just once!
(eq? 'quote (node-class value)) )
(let ((val (first (node-parameters value))))
(when (or (collapsable-literal? val)
(= 1 nreferences) )
(quick-put! plist 'collapsable #t) ) ) )
;; If it has a known value that is a procedure, and if the number of call-sites is equal to the
;; number of references (does not escape), then make all formal parameters 'unused which are
;; never referenced or assigned (if no rest parameter exist):
;; - also marks the procedure as 'has-unused-parameters (if not in `callback-names')
;; - if the procedure is internal (a continuation) do NOT mark unused parameters.
;; - also: if procedure has rest-parameter and no unused params, mark f-id as 'explicit-rest.
(when value
(let ([has #f])
(when (and (eq? '##core#lambda (node-class value))
(= nreferences ncall-sites) )
(let ([lparams (node-parameters value)])
(when (second lparams)
(decompose-lambda-list
(third lparams)
(lambda (vars argc rest)
(unless rest
(for-each
(lambda (var)
(cond [(and (not (get db var 'references))
(not (get db var 'assigned)) )
(put! db var 'unused #t)
(set! has #t)
#t]
[else #f] ) )
vars) )
(cond [(and has (not (rassoc sym callback-names eq?)))
(put! db (first lparams) 'has-unused-parameters #t) ]
[rest
(put! db (first lparams) 'explicit-rest #t) ] ) ) ) ) ) ) ) )
;; Make 'removable, if it has no references and is not assigned to, and if it
;; has either a value that does not cause any side-effects or if it is 'undefined:
(when (and (not assigned)
(null? references)
(or (and value
(if (eq? '##core#variable (node-class value))
(let ((varname (first (node-parameters value))))
(or (not (get db varname 'global))
(variable-mark varname '##core#always-bound)
(intrinsic? varname)))
(not (expression-has-side-effects? value db)) ))
undefined) )
(quick-put! plist 'removable #t) )
;; Make 'replacable, if it has a variable as known value and if either that variable has
;; a known value itself, or if it is not captured and referenced only once, the target and
;; the source are never assigned and the source is non-global or we are in block-mode:
;; - The target-variable is not allowed to be global.
;; - The variable that can be substituted for the current one is marked as 'replacing.
;; This is done to prohibit beta-contraction of the replacing variable (It wouldn't be there, if
;; it was contracted).
(when (and value (not global))
(when (eq? '##core#variable (node-class value))
(let* ([name (first (node-parameters value))]
[nrefs (get db name 'references)] )
(when (and (not captured)
(or (and (not (get db name 'unknown)) (get db name 'value))
(and (not (get db name 'captured))
nrefs
(= 1 (length nrefs))
(not assigned)
(not (get db name 'assigned))
(or (not (variable-visible? name))
(not (get db name 'global))) ) ))
(quick-put! plist 'replacable name)
(put! db name 'replacing #t) ) ) ) )
;; Make 'replacable, if it has a known value of the form: '(lambda (<xvar>) (<kvar> <xvar>))' and
;; is an internally created procedure: (See above for 'replacing)
(when (and value (eq? '##core#lambda (node-class value)))
(let ([params (node-parameters value)])
(when (not (second params))
(let ([llist (third params)]
[body (first (node-subexpressions value))] )
(when (and (pair? llist)
(null? (cdr llist))
(eq? '##core#call (node-class body)) )
(let ([subs (node-subexpressions body)])
(when (= 2 (length subs))
(let ([v1 (first subs)]
[v2 (second subs)] )
(when (and (eq? '##core#variable (node-class v1))
(eq? '##core#variable (node-class v2))
(eq? (first llist) (first (node-parameters v2))) )
(let ([kvar (first (node-parameters v1))])
(quick-put! plist 'replacable kvar)
(put! db kvar 'replacing #t) ) ) ) ) ) ) ) ) ) ) ) )
db)
;; Set original program-size, if this is the first analysis-pass:
(unless original-program-size
(set! original-program-size current-program-size) )
;; return database
db) )
;;; Collect unsafe global procedure calls that are assigned:
;;; Convert closures to explicit data structures (effectively flattens function-binding
; structure):
(define (perform-closure-conversion node db)
(let ((direct-calls 0)
(customizable '())
(lexicals '()))
(define (test sym item) (get db sym item))
(define (register-customizable! var id)
(set! customizable (lset-adjoin eq? customizable var))
(put! db id 'customizable #t) )
(define (register-direct-call! id)
(set! direct-calls (add1 direct-calls))
(set! direct-call-ids (lset-adjoin eq? direct-call-ids id)) )
;; Gather free-variable information:
;; (and: - register direct calls
;; - update (by mutation) call information in "##core#call" nodes)
(define (gather n here locals)
(let ((subs (node-subexpressions n))
(params (node-parameters n)) )
(case (node-class n)
((##core#variable)
(let ((var (first params)))
(if (memq var lexicals)
(list var)
'())))
((quote ##core#undefined ##core#proc ##core#primitive)
'())
((let)
;;XXX remove this test later, shouldn't be needed:
(when (pair? (cdr params)) (bomb "let-node has invalid format" params))
(let ((c (gather (first subs) here locals))
(var (first params)))
(append c (delete var (gather (second subs) here (cons var locals)) eq?))))
((set!)
(let ((var (first params))
(c (gather (first subs) here locals)))
(if (memq var lexicals)
(cons var c)
c)))
((##core#call)
(let* ([fn (first subs)]
[mode (first params)]
[name (and (pair? (cdr params)) (second params))]
[varfn (eq? '##core#variable (node-class fn))] )
(node-parameters-set!
n
(cons mode
(if (or name varfn)
(cons name
(if varfn
(let* ([varname (first (node-parameters fn))]
[val (and (not (test varname 'unknown))
(not (eq?
'no
(variable-mark
varname '##compiler#inline)))
(or (test varname 'value)
(test varname 'local-value)))] )
(if (and val (eq? '##core#lambda (node-class val)))
(let* ([params (node-parameters val)]
[llist (third params)]
[id (first params)]
[refs (test varname 'references)]
[sites (test varname 'call-sites)]
[custom
(and refs sites
(= (length refs) (length sites))
(test varname 'value)
(proper-list? llist) ) ] )
(when (and name
(not (llist-match? llist (cdr subs))))
(quit
"~a: procedure `~a' called with wrong number of arguments"
(source-info->line name)
(if (pair? name) (cadr name) name)))
(register-direct-call! id)
(when custom (register-customizable! varname id))
(list id custom) )
'() ) )
'() ) )
'() ) ) )
(concatenate (map (lambda (n) (gather n here locals)) subs) ) ))
((##core#lambda ##core#direct_lambda)
(decompose-lambda-list
(third params)
(lambda (vars argc rest)
(let ((id (if here (first params) 'toplevel)))
(fluid-let ((lexicals (append locals lexicals)))
(let ((c (delete-duplicates (gather (first subs) id vars) eq?)))
(put! db id 'closure-size (length c))
(put! db id 'captured-variables c)
(lset-difference eq? c locals vars)))))))
(else (concatenate (map (lambda (n) (gather n here locals)) subs)) ) ) ))
;; Create explicit closures:
(define (transform n here closure)
(let ((subs (node-subexpressions n))
(params (node-parameters n))
(class (node-class n)) )
(case class
((quote ##core#undefined ##core#proc) n)
((##core#variable)
(let* ((var (first params))
(val (ref-var n here closure)) )
(if (test var 'boxed)
(make-node '##core#unbox '() (list val))
val) ) )
((if ##core#call ##core#inline ##core#inline_allocate ##core#callunit
##core#inline_ref ##core#inline_update ##core#debug-event
##core#switch ##core#cond ##core#direct_call ##core#recurse ##core#return
##core#inline_loc_ref
##core#inline_loc_update)
(make-node (node-class n) params (maptransform subs here closure)) )
((let)
(let* ([var (first params)]
[boxedvar (test var 'boxed)]
[boxedalias (gensym var)] )
(if boxedvar
(make-node
'let (list boxedalias)
(list (transform (first subs) here closure)
(make-node
'let (list var)
(list (make-node '##core#box '() (list (varnode boxedalias)))
(transform (second subs) here closure) ) ) ) )
(make-node
'let params
(maptransform subs here closure) ) ) ) )
((##core#lambda ##core#direct_lambda)
(let ([llist (third params)])
(decompose-lambda-list
llist
(lambda (vars argc rest)
(let* ([boxedvars (filter (lambda (v) (test v 'boxed)) vars)]
[boxedaliases (map cons boxedvars (map gensym boxedvars))]
[cvar (gensym 'c)]
[id (if here (first params) 'toplevel)]
[capturedvars (or (test id 'captured-variables) '())]
[csize (or (test id 'closure-size) 0)]
[info (and emit-closure-info (second params) (pair? llist))] )
;; If rest-parameter is boxed: mark it as 'boxed-rest
;; (if we don't do this than preparation will think the (boxed) alias
;; of the rest-parameter is never used)
(and-let* ([rest]
[(test rest 'boxed)]
[rp (test rest 'rest-parameter)] )
(put! db (cdr (assq rest boxedaliases)) 'boxed-rest #t) )
(make-node
'##core#closure (list (+ csize (if info 2 1)))
(cons
(make-node
class
(list id
(second params)
(cons
cvar
(build-lambda-list
(map (lambda (v)
(cond ((assq v boxedaliases) => cdr)
(else v) ) )
vars)
argc
(cond ((and rest (assq rest boxedaliases)) => cdr)
(else rest) ) ) )
(fourth params) )
(list (let ((body (transform (car subs) cvar capturedvars)))
(if (pair? boxedvars)
(fold-right
(lambda (alias val body)
(make-node 'let (list alias) (list val body)))
body
(unzip1 boxedaliases)
(map (lambda (a)
(make-node '##core#box '() (list (varnode (cdr a)))))
boxedaliases) )
body) ) ) )
(let ((cvars (map (lambda (v) (ref-var (varnode v) here closure))
capturedvars) ) )
(if info
(append
cvars
(list
(qnode
(##sys#make-lambda-info
(->string (cons (or (real-name id) '?)
(cdr llist) )))))) ; this is not always correct, due to optimizations
cvars) ) ) ) ) ) ) ) )
((set!)
(let* ([var (first params)]
[val (first subs)]
[cval (node-class val)]
[immf (or (and (eq? 'quote cval) (immediate? (first (node-parameters val))))
(eq? '##core#undefined cval) ) ] )
(cond ((posq var closure)
=> (lambda (i)
(if (test var 'boxed)
(make-node
(if immf '##core#updatebox_i '##core#updatebox)
'()
(list (make-node '##core#ref (list (add1 i)) (list (varnode here)))
(transform val here closure) ) )
;; Is the following actually used???
(make-node
(if immf '##core#update_i '##core#update)
(list (add1 i))
(list (varnode here)
(transform val here closure) ) ) ) ) )
((test var 'boxed)
(make-node
(if immf '##core#updatebox_i '##core#updatebox)
'()
(list (varnode var)
(transform val here closure) ) ) )
(else (make-node
'set! (list var)
(list (transform val here closure) ) ) ) ) ) )
((##core#primitive)
(make-node
'##core#closure (list (if emit-closure-info 2 1))
(cons (make-node '##core#proc (list (car params) #t) '())
(if emit-closure-info
(list (qnode (##sys#make-lambda-info (car params))))
'() ) ) ) )
(else (bomb "bad node (closure2)")) ) ) )
(define (maptransform xs here closure)
(map (lambda (x) (transform x here closure)) xs) )
(define (ref-var n here closure)
(let ((var (first (node-parameters n))))
(cond ((posq var closure)
=> (lambda (i)
(make-node '##core#ref (list (+ i 1))
(list (varnode here)) ) ) )
(else n) ) ) )
(debugging 'p "closure conversion gathering phase...")
(gather node #f '())
(when (pair? customizable)
(debugging 'o "customizable procedures" customizable))
(debugging 'p "closure conversion transformation phase...")
(let ((node2 (transform node #f #f)))
(unless (zero? direct-calls)
(debugging 'o "calls to known targets" direct-calls))
node2) ) )
;;; Do some preparations before code-generation can commence:
(define-record-type lambda-literal
(make-lambda-literal id external arguments argument-count rest-argument temporaries
unboxed-temporaries callee-signatures allocated directly-called
closure-size looping customizable rest-argument-mode body direct)
lambda-literal?
(id lambda-literal-id) ; symbol
(external lambda-literal-external) ; boolean
(arguments lambda-literal-arguments) ; (symbol ...)
(argument-count lambda-literal-argument-count) ; integer
(rest-argument lambda-literal-rest-argument) ; symbol | #f
(temporaries lambda-literal-temporaries) ; integer
(unboxed-temporaries lambda-literal-unboxed-temporaries) ; ((sym . utype) ...)
(callee-signatures lambda-literal-callee-signatures) ; (integer ...)
(allocated lambda-literal-allocated) ; integer
(directly-called lambda-literal-directly-called) ; boolean
(closure-size lambda-literal-closure-size) ; integer
(looping lambda-literal-looping) ; boolean
(customizable lambda-literal-customizable) ; boolean
(rest-argument-mode lambda-literal-rest-argument-mode) ; #f | LIST | NONE
(body lambda-literal-body) ; expression
(direct lambda-literal-direct)) ; boolean
(define (prepare-for-code-generation node db)
(let ((literals '())
(literal-count 0)
(lambda-info-literals '())
(lambda-info-literal-count 0)
;; Use analysis db as optimistic heuristic for procedure table size
(lambda-table (make-vector (fx* (fxmax current-analysis-database-size 1) 3) '()))
(temporaries 0)
(ubtemporaries '())
(allocated 0)
(looping 0)
(signatures '())
(fastinits 0)
(fastrefs 0)
(fastsets 0)
(dbg-index 0)
(debug-info '()))
(define (walk-var var e e-count sf)
(cond [(posq var e)
=> (lambda (i)
(make-node '##core#local (list (fx- e-count (fx+ i 1))) '()))]
[(keyword? var) (make-node '##core#literal (list (literal var)) '())]
[else (walk-global var sf)] ) )
(define (walk-global var sf)
(let* ([safe (or sf
no-bound-checks
unsafe
(variable-mark var '##compiler#always-bound)
(intrinsic? var))]
[blockvar (and (get db var 'assigned)
(not (variable-visible? var)))])
(when blockvar (set! fastrefs (add1 fastrefs)))
(make-node
'##core#global
(list (if blockvar
(blockvar-literal var)
(literal var) )
safe
blockvar
var)
'() ) ) )
(define (walk n e e-count here boxes)
(let ((subs (node-subexpressions n))
(params (node-parameters n))
(class (node-class n)) )
(case class
((##core#undefined ##core#proc) n)
((##core#variable)
(walk-var (first params) e e-count #f) )
((##core#direct_call)
(set! allocated (+ allocated (fourth params)))
(make-node class params (mapwalk subs e e-count here boxes)) )
((##core#inline_allocate)
(set! allocated (+ allocated (second params)))
(make-node class params (mapwalk subs e e-count here boxes)) )
((##core#inline_ref)
(set! allocated (+ allocated (words (estimate-foreign-result-size (second params)))))
(make-node class params '()) )
((##core#inline_loc_ref)
(set! allocated (+ allocated (words (estimate-foreign-result-size (first params)))))
(make-node class params (mapwalk subs e e-count here boxes)) )
((##core#closure)
(set! allocated (+ allocated (first params) 1))
(make-node '##core#closure params (mapwalk subs e e-count here boxes)) )
((##core#box)
(set! allocated (+ allocated 2))
(make-node '##core#box params (list (walk (first subs) e e-count here boxes))) )
((##core#updatebox)
(let* ([b (first subs)]
[subs (mapwalk subs e e-count here boxes)] )
(make-node
(cond [(and (eq? '##core#variable (node-class b))
(memq (first (node-parameters b)) boxes) )
(set! fastinits (add1 fastinits))
'##core#updatebox_i]
[else class] )
'()
subs) ) )
((##core#lambda ##core#direct_lambda)
(let ((temps temporaries)
(ubtemps ubtemporaries)
(sigs signatures)
(lping looping)
(alc allocated)
(direct (eq? class '##core#direct_lambda)) )
(set! temporaries 0)
(set! ubtemporaries '())
(set! allocated 0)
(set! signatures '())
(set! looping 0)
(decompose-lambda-list
(third params)
(lambda (vars argc rest)
(let* ([id (first params)]
[rest-mode
(and rest
(let ([rrefs (get db rest 'references)])
(cond [(get db rest 'assigned) 'list]
[(and (not (get db rest 'boxed-rest)) (or (not rrefs) (null? rrefs))) 'none]
[else (get db rest 'rest-parameter)] ) ) ) ]
[body (walk
(car subs)
(##sys#fast-reverse (if (eq? 'none rest-mode)
(butlast vars)
vars))
(if (eq? 'none rest-mode)
(fx- (length vars) 1)
(length vars))
id
'()) ] )
(when (eq? rest-mode 'none)
(debugging 'o "unused rest argument" rest id))
(when (and direct rest)
(bomb "bad direct lambda" id allocated rest) )
(##sys#hash-table-set!
lambda-table
id
(make-lambda-literal
id
(second params)
vars
argc
rest
(add1 temporaries)
ubtemporaries
signatures
allocated
(or direct (memq id direct-call-ids))
(or (get db id 'closure-size) 0)
(and (not rest)
(> looping 0)
(begin
(debugging 'o "identified direct recursive calls" id looping)
#t) )
(or direct (get db id 'customizable))
rest-mode
body
direct) )
(set! looping lping)
(set! temporaries temps)
(set! ubtemporaries ubtemps)
(set! allocated alc)
(set! signatures (lset-adjoin = sigs argc))
(make-node '##core#proc (list (first params)) '()) ) ) ) ) )
((let)
(let* ([var (first params)]
[val (first subs)]
[boxvars (if (eq? '##core#box (node-class val)) (list var) '())] )
(set! temporaries (add1 temporaries))
(make-node
'##core#bind (list 1) ; is actually never used with more than 1 variable
(list (walk val e e-count here boxes)
(walk (second subs)
(append (##sys#fast-reverse params) e) (fx+ e-count 1)
here (append boxvars boxes)) ) ) ) )
((##core#let_unboxed)
(let* ((var (first params))
(val (first subs)) )
(set! ubtemporaries (alist-cons var (second params) ubtemporaries))
(make-node
'##core#let_unboxed params
(list (walk val e e-count here boxes)
(walk (second subs) e e-count here boxes) ) ) ) )
((set!)
(let ([var (first params)]
[val (first subs)] )
(cond ((posq var e)
=> (lambda (i)
(make-node '##core#setlocal
(list (fx- e-count (fx+ i 1)))
(list (walk val e e-count here boxes)) ) ) )
(else
(let* ([cval (node-class val)]
[blockvar (not (variable-visible? var))]
[immf (or (and (eq? cval 'quote) (immediate? (first (node-parameters val))))
(eq? '##core#undefined cval) ) ] )
(when blockvar (set! fastsets (add1 fastsets)))
(make-node
(if immf '##core#setglobal_i '##core#setglobal)
(list (if blockvar
(blockvar-literal var)
(literal var) )
blockvar
var)
(list (walk (car subs) e e-count here boxes)) ) ) ) ) ) )
((##core#call)
(let* ((len (length (cdr subs)))
(p2 (pair? (cdr params)))
(name (and p2 (second params)))
(name-str (source-info->string name)))
(set! signatures (lset-adjoin = signatures len))
(when (and (>= (length params) 3) (eq? here (third params)))
(set! looping (add1 looping)) )
(if (and emit-debug-info name)
(let ((info (list dbg-index 'C_DEBUG_CALL "" name-str)))
(set! params (cons dbg-index params))
(set! debug-info (cons info debug-info))
(set! dbg-index (add1 dbg-index)))
(set! params (cons #f params)))
(make-node class params (mapwalk subs e e-count here boxes))))
((##core#recurse)
(when (first params) (set! looping (add1 looping)))
(make-node class params (mapwalk subs e e-count here boxes)) )
((quote)
(let ((c (first params)))
(cond ((and (fixnum? c) (not (big-fixnum? c)))
(immediate-literal c) )
((number? c)
(cond ((eq? 'fixnum number-type)
(cond ((and (integer? c) (not (big-fixnum? c)))
(warning
(sprintf
"coerced inexact literal number `~S' to fixnum ~S"
c (inexact->exact c)))
(immediate-literal (inexact->exact c)) )
(else (quit "cannot coerce inexact literal `~S' to fixnum" c)) ) )
(else (make-node '##core#literal (list (literal c)) '())) ) )
((immediate? c) (immediate-literal c))
(else (make-node '##core#literal (list (literal c)) '())) ) ) )
((if ##core#cond)
(let* ((test (walk (first subs) e e-count here boxes))
(t0 temporaries)
(a0 allocated)
(x1 (walk (second subs) e e-count here boxes))
(t1 temporaries)
(a1 allocated)
(x2 (walk (third subs) e e-count here boxes)))
(set! allocated (+ a0 (max (- allocated a1) (- a1 a0))))
(set! temporaries (+ t0 (max (- temporaries t1) (- t1 t0))))
(make-node class params (list test x1 x2))))
((##core#switch)
(let* ((exp (walk (first subs) e e-count here boxes))
(a0 allocated))
(make-node
class
params
(cons
exp
(let loop ((j (first params)) (subs (cdr subs)) (ma 0))
(set! allocated a0)
(if (zero? j)
(let ((def (walk (car subs) e e-count here boxes)))
(set! allocated (+ a0 (max ma (- allocated a0))))
(list def))
(let* ((const (walk (car subs) e e-count here boxes))
(body (walk (cadr subs) e e-count here boxes)))
(cons*
const body
(loop (sub1 j) (cddr subs) (max (- allocated a0) ma))))))))))
((##core#debug-event)
(let* ((i dbg-index)
(params (cons i params)))
(set! debug-info (cons params debug-info))
(set! dbg-index (add1 dbg-index))
(make-node class params '())))
(else (make-node class params (mapwalk subs e e-count here boxes)) ) ) ) )
(define (mapwalk xs e e-count here boxes)
(map (lambda (x) (walk x e e-count here boxes)) xs) )
(define (literal x)
(cond [(immediate? x) (immediate-literal x)]
;; Fixnums that don't fit in 32 bits are treated as non-immediates,
;; that's why we do the (apparently redundant) C_blockp check here.
((and (##core#inline "C_blockp" x) (##core#inline "C_lambdainfop" x))
(let ((i lambda-info-literal-count))
(set! lambda-info-literals (cons x lambda-info-literals))
(set! lambda-info-literal-count (add1 lambda-info-literal-count))
(vector i) ) )
[(posv x literals) => (lambda (p) (fx- literal-count (fx+ p 1)))]
[else (new-literal x)] ) )
(define (new-literal x)
(let ([i literal-count])
(set! literals (cons x literals))
(set! literal-count (add1 literal-count))
i) )
(define (blockvar-literal var)
(cond
((list-index (lambda (lit)
(and (block-variable-literal? lit)
(eq? var (block-variable-literal-name lit)) ) )
literals)
=> (lambda (p) (fx- literal-count (fx+ p 1))))
(else (new-literal (make-block-variable-literal var))) ) )
(define (immediate-literal x)
(if (eq? (void) x)
(make-node '##core#undefined '() '())
(make-node '##core#immediate
(cond ((fixnum? x) `(fix ,x))
((boolean? x) `(bool ,x))
((char? x) `(char ,x))
((null? x) '(nil))
((eof-object? x) '(eof))
(else (bomb "bad immediate (prepare)")) )
'() ) ) )
(debugging 'p "preparation phase...")
(let ((node2 (walk node '() 0 #f '())))
(when (positive? fastinits)
(debugging 'o "fast box initializations" fastinits))
(when (positive? fastrefs)
(debugging 'o "fast global references" fastrefs))
(when (positive? fastsets)
(debugging 'o "fast global assignments" fastsets))
(values node2
(##sys#fast-reverse literals)
(##sys#fast-reverse lambda-info-literals)
lambda-table
(##sys#fast-reverse debug-info))))
)
|