1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
|
/* -*-C-*-
Copyright (C) 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 Massachusetts Institute of Technology
This file is part of MIT/GNU Scheme.
MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.
*/
/* Compiled-code interface */
/* Some of the cmpintmd/FOO.h files use this macro to alter their
behavior when included here. */
#define IN_CMPINT_C 1
#include "scheme.h"
#include "prims.h"
#include "lookup.h"
#include "trap.h"
#include "history.h"
#include "cmpgc.h"
/* Two special classes of procedures are used in this file:
Scheme interface entries. These procedures are called from C and
ultimately invoke 'ENTER_SCHEME' to enter compiled code, or return
a status code.
Scheme interface utilities. These procedures are called from the
Scheme interface and perform tasks that the compiler does not code
inline. They are referenced from compiled Scheme code by index,
and the assembly language interface fetches them from an array.
They are defined with 'SCHEME_UTILITY_n' for some 'n', and
ultimately invoke either 'RETURN_TO_SCHEME' (in the normal case) or
'RETURN_TO_C' (in the error case). */
typedef long cache_handler_t (SCHEME_OBJECT, SCHEME_OBJECT, unsigned long);
typedef struct
{
SCHEME_OBJECT * block_address;
SCHEME_OBJECT * scan;
unsigned long n_sections;
insn_t * return_address;
unsigned long n_linked_sections;
SCHEME_OBJECT * scan0;
linkage_section_type_t type;
unsigned long n_entries;
unsigned long n_linked_entries;
} link_cc_state_t;
/* Ways to bypass the interpreter */
typedef enum
{
REFLECT_CODE_INTERNAL_APPLY,
REFLECT_CODE_RESTORE_INTERRUPT_MASK,
REFLECT_CODE_STACK_MARKER,
REFLECT_CODE_CC_BKPT
} reflect_code_t;
#define PUSH_REFLECTION(code) do \
{ \
STACK_PUSH (ULONG_TO_FIXNUM (code)); \
STACK_PUSH (reflect_to_interface); \
} while (false)
#define TC_TRAMPOLINE_HEADER TC_FIXNUM
#define TRAMPOLINE_TABLE_SIZE 4
static trampoline_type_t
trampoline_arity_table [TRAMPOLINE_TABLE_SIZE * TRAMPOLINE_TABLE_SIZE] =
{
TRAMPOLINE_K_1_0, /* 1_0 */
TRAMPOLINE_K_APPLY, /* 1_1 should not get here */
TRAMPOLINE_K_APPLY, /* 1_2 should not get here */
TRAMPOLINE_K_APPLY, /* 1_3 should not get here */
TRAMPOLINE_K_2_0, /* 2_0 */
TRAMPOLINE_K_2_1, /* 2_1 */
TRAMPOLINE_K_APPLY, /* 2_2 should not get here */
TRAMPOLINE_K_APPLY, /* 2_3 should not get here */
TRAMPOLINE_K_3_0, /* 3_0 */
TRAMPOLINE_K_3_1, /* 3_1 */
TRAMPOLINE_K_3_2, /* 3_2 */
TRAMPOLINE_K_APPLY, /* 3_3 should not get here */
TRAMPOLINE_K_4_0, /* 4_0 */
TRAMPOLINE_K_4_1, /* 4_1 */
TRAMPOLINE_K_4_2, /* 4_2 */
TRAMPOLINE_K_4_3 /* 4_3 */
};
cc_arch_t compiler_processor_type;
unsigned int compiler_interface_version;
SCHEME_OBJECT compiler_utilities;
SCHEME_OBJECT return_to_interpreter;
SCHEME_OBJECT reflect_to_interface;
static bool linking_cc_block_p = 0;
static SCHEME_OBJECT make_compiler_utilities (void);
static void open_stack_gap (unsigned long, unsigned long);
static void close_stack_gap (unsigned long, unsigned long);
static void recover_from_apply_error (SCHEME_OBJECT, unsigned long);
static long link_remaining_sections (link_cc_state_t *);
static void start_linking_cc_block (void);
static void end_linking_cc_block (link_cc_state_t *);
static void abort_linking_cc_block (void *);
static void update_cache_after_link (link_cc_state_t *);
static void start_linking_section (link_cc_state_t *);
static long link_section (link_cc_state_t *);
static bool link_section_handler
(linkage_section_type_t, cache_handler_t **, bool *);
static void back_out_of_link_section (link_cc_state_t *);
static void restore_link_cc_state (link_cc_state_t *);
static void count_linkage_sections
(SCHEME_OBJECT *, SCHEME_OBJECT *, unsigned long *, unsigned long *);
static SCHEME_OBJECT read_linkage_sections
(SCHEME_OBJECT *, SCHEME_OBJECT *, unsigned long, unsigned long);
static bool section_execute_p (SCHEME_OBJECT);
static void setup_compiled_invocation_from_primitive
(SCHEME_OBJECT, unsigned long);
static long setup_compiled_invocation (SCHEME_OBJECT, unsigned long);
static long setup_lexpr_invocation
(SCHEME_OBJECT, unsigned long, unsigned long);
static bool open_gap (unsigned long, unsigned long);
static bool unlinked_section_start_p (SCHEME_OBJECT *, SCHEME_OBJECT *);
static bool cc_block_address_closure_p (SCHEME_OBJECT *);
static void write_uuo_link (SCHEME_OBJECT, SCHEME_OBJECT *);
static long make_fake_uuo_link (SCHEME_OBJECT, SCHEME_OBJECT, unsigned long);
static long make_trampoline
(SCHEME_OBJECT *, cc_entry_type_t *, trampoline_type_t, unsigned int, ...);
static void make_trampoline_headers
(unsigned long, unsigned long,
SCHEME_OBJECT *, SCHEME_OBJECT *, unsigned long *);
static bool fill_trampoline
(SCHEME_OBJECT *, unsigned long, cc_entry_type_t *, trampoline_type_t);
static long make_redirection_trampoline
(SCHEME_OBJECT *, trampoline_type_t, SCHEME_OBJECT);
static long make_apply_trampoline
(SCHEME_OBJECT *, trampoline_type_t, SCHEME_OBJECT, unsigned long);
#ifndef UTILITY_RESULT_DEFINED
#ifdef CMPINT_USE_STRUCS
#ifdef C_FUNC_PTR_IS_CLOSURE
typedef insn_t * c_func_t;
#else
typedef void c_func_t (void);
/* From trunk, but may not be needed: */
# if 0
# ifdef __OPEN_WATCOM_14__
# define REFENTRY(name) ((void *) name)
# else
# define REFENTRY(name) ((c_func_t *) name)
# endif
# define VARENTRY(name) c_func_t * name
# define EXTENTRY(name) extern c_func_t ASM_ENTRY_POINT (name)
# endif
#endif
#define RETURN_TO_C(code) do \
{ \
(DSU_result->interface_dispatch) = interface_to_C; \
((DSU_result->extra) . code_to_interpreter) = (code); \
return; \
} while (false)
#define RETURN_TO_SCHEME(ep) do \
{ \
(DSU_result->interface_dispatch) = interface_to_scheme; \
((DSU_result->extra) . entry_point) = (ep); \
return; \
} while (false)
extern c_func_t ASM_ENTRY_POINT (interface_to_C);
extern c_func_t ASM_ENTRY_POINT (interface_to_scheme);
#define ENTER_SCHEME(ep) return (C_to_interface (ep))
extern long ASM_ENTRY_POINT (C_to_interface) (insn_t *);
#else /* !CMPINT_USE_STRUCS */
#define RETURN_TO_C(code) do \
{ \
(*DSU_result) = interface_to_C_hook; \
C_return_value = (code); \
return; \
} while (false)
#define RETURN_TO_SCHEME(ep) do \
{ \
(*DSU_result) = (ep); \
return; \
} while (false)
#define ENTER_SCHEME(ep) do \
{ \
C_to_interface (ep); \
return (C_return_value); \
} while (false)
extern utility_result_t interface_to_C_hook;
extern void ASM_ENTRY_POINT (C_to_interface) (insn_t *);
long C_return_value;
#endif /* !CMPINT_USE_STRUCS */
#endif /* !UTILITY_RESULT_DEFINED */
#define JUMP_TO_CC_ENTRY(entry) ENTER_SCHEME (CC_ENTRY_ADDRESS (entry))
#ifndef COMPILER_REGBLOCK_N_FIXED
# define COMPILER_REGBLOCK_N_FIXED REGBLOCK_MINIMUM_LENGTH
#endif
#ifndef COMPILER_REGBLOCK_N_TEMPS
# define COMPILER_REGBLOCK_N_TEMPS 0
#endif
#ifndef COMPILER_TEMP_SIZE
# define COMPILER_TEMP_SIZE ((sizeof (double)) / (sizeof (SCHEME_OBJECT)))
#endif
#ifndef COMPILER_REGBLOCK_EXTRA_SIZE
# define COMPILER_REGBLOCK_EXTRA_SIZE 0
#endif
#if (REGBLOCK_MINIMUM_LENGTH > COMPILER_REGBLOCK_N_FIXED)
# include "ERROR: cmpint.c and const.h disagree on REGBLOCK_MINIMUM_LENGTH!"
#endif
#define REGBLOCK_LENGTH \
(COMPILER_REGBLOCK_N_FIXED \
+ (COMPILER_REGBLOCK_N_TEMPS * COMPILER_TEMP_SIZE) \
+ COMPILER_REGBLOCK_EXTRA_SIZE)
#ifndef REGBLOCK_ALLOCATED_BY_INTERFACE
SCHEME_OBJECT Registers [REGBLOCK_LENGTH];
#endif
#ifndef ASM_RESET_HOOK
# define ASM_RESET_HOOK() do {} while (false)
#endif
#define SAVE_LAST_RETURN_CODE(code) do \
{ \
{ \
long SLRC_offset \
= (STACK_LOCATIVE_DIFFERENCE (stack_pointer, last_return_code)); \
assert (SLRC_offset > 0); \
STACK_PUSH (LONG_TO_FIXNUM (SLRC_offset)); \
} \
PUSH_RC (code); \
COMPILER_NEW_SUBPROBLEM (); \
} while (false)
#define RESTORE_LAST_RETURN_CODE() do \
{ \
last_return_code = (STACK_LOC (FIXNUM_TO_ULONG (GET_EXP))); \
CHECK_LAST_RETURN_CODE (); \
COMPILER_END_SUBPROBLEM (); \
} while (false)
#define CHECK_LAST_RETURN_CODE() do \
{ \
assert \
(RETURN_CODE_P \
(STACK_LOCATIVE_REFERENCE (last_return_code, \
CONTINUATION_RETURN_CODE))); \
} while (false)
/* Initialization */
void
compiler_initialize (bool fasl_p)
{
/* Called when scheme started. */
SET_PRIMITIVE (SHARP_F);
compiler_processor_type = COMPILER_PROCESSOR_TYPE;
compiler_interface_version = COMPILER_INTERFACE_VERSION;
if (fasl_p)
compiler_reset (make_compiler_utilities ());
else
{
/* Delay until after band-load, when compiler_reset will be invoked. */
compiler_utilities = SHARP_F;
return_to_interpreter = SHARP_F;
#ifdef CC_ARCH_INITIALIZE
CC_ARCH_INITIALIZE ();
#endif
}
}
#define COMPILER_UTILITIES_HEADERS(h1, h2, n) \
make_trampoline_headers (2, 2, (h1), (h2), (n))
static SCHEME_OBJECT
make_compiler_utilities (void)
{
SCHEME_OBJECT h1;
SCHEME_OBJECT h2;
unsigned long n_words;
SCHEME_OBJECT * block;
COMPILER_UTILITIES_HEADERS ((&h1), (&h2), (&n_words));
if (GC_NEEDED_P (n_words))
{
outf_fatal ("Can't allocate compiler_utilities.\n");
Microcode_Termination (TERM_NO_SPACE);
}
h1 = (OBJECT_NEW_TYPE (TC_MANIFEST_VECTOR, h1));
block = Free;
Free += n_words;
(block[0]) = h1;
(block[1]) = h2;
{
cc_entry_type_t cet;
make_cc_entry_type ((&cet), CET_RETURN_TO_INTERPRETER);
if ((fill_trampoline (block, 0, (&cet), TRAMPOLINE_K_RETURN_TO_INTERPRETER))
||
(fill_trampoline (block, 1, (&cet), TRAMPOLINE_K_REFLECT_TO_INTERFACE)))
{
outf_fatal ("\nError in make_compiler_utilities\n");
Microcode_Termination (TERM_COMPILER_DEATH);
/*NOTREACHED*/
}
}
/* These entries are no longer used, but are provided for
compatibility with the previous structure. */
{
SCHEME_OBJECT * store = (trampoline_storage (block));
(store[0]) = ((trampoline_entry_addr (block, 0)) - ((insn_t *) block));
(store[1]) = ((trampoline_entry_addr (block, 1)) - ((insn_t *) block));
}
block = (copy_to_constant_space (block, n_words));
return (MAKE_CC_BLOCK (block));
}
void
compiler_reset (SCHEME_OBJECT new_block)
{
/* Called after a disk restore */
SCHEME_OBJECT h1;
SCHEME_OBJECT h2;
unsigned long n_words;
SCHEME_OBJECT * nbp;
COMPILER_UTILITIES_HEADERS ((&h1), (&h2), (&n_words));
h1 = (OBJECT_NEW_TYPE (TC_MANIFEST_VECTOR, h1));
if (! ((CC_BLOCK_P (new_block))
&& ((MEMORY_REF (new_block, 0)) == h1)
&& ((MEMORY_REF (new_block, 1)) == h2)))
{
outf_fatal ("\nThe world image being restored is incompatible"
" with this microcode.\n");
Microcode_Termination (TERM_COMPILER_DEATH);
/*NOTREACHED*/
}
nbp = (OBJECT_ADDRESS (new_block));
compiler_utilities = new_block;
return_to_interpreter = (MAKE_CC_ENTRY (trampoline_entry_addr (nbp, 0)));
reflect_to_interface = (MAKE_CC_ENTRY (trampoline_entry_addr (nbp, 1)));
SET_CLOSURE_FREE (0);
SET_CLOSURE_SPACE (0);
SET_REFLECTOR (reflect_to_interface);
ASM_RESET_HOOK ();
}
/* Main compiled-code entry points */
#define DEFINE_SCHEME_ENTRY(pname) long pname (void)
DEFINE_SCHEME_ENTRY (enter_compiled_expression)
{
SCHEME_OBJECT entry = GET_EXP;
{
cc_entry_type_t cet;
if (read_cc_entry_type ((&cet), (CC_ENTRY_ADDRESS (entry))))
return (ERR_COMPILED_CODE_ERROR);
if (cet.marker != CET_EXPRESSION)
{
/* evaluate to self */
SET_VAL (entry);
return (PRIM_DONE);
}
}
guarantee_cc_return (0);
JUMP_TO_CC_ENTRY (entry);
}
DEFINE_SCHEME_ENTRY (apply_compiled_procedure)
{
unsigned long n_args = (POP_APPLY_FRAME_HEADER ());
SCHEME_OBJECT procedure = (STACK_POP ());
long code = (setup_compiled_invocation (procedure, n_args));
if (code != PRIM_DONE)
return (code);
JUMP_TO_CC_ENTRY (procedure);
}
DEFINE_SCHEME_ENTRY (return_to_compiled_code)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT cont = (STACK_POP ());
{
cc_entry_type_t cet;
if ((read_cc_entry_type ((&cet), (CC_ENTRY_ADDRESS (cont))))
|| (! ((cet.marker == CET_CONTINUATION)
|| (cet.marker == CET_INTERNAL_CONTINUATION)
|| (cet.marker == CET_RETURN_TO_INTERPRETER))))
{
STACK_PUSH (cont);
SAVE_CONT ();
return (ERR_INAPPLICABLE_OBJECT);
}
}
JUMP_TO_CC_ENTRY (cont);
}
}
void
guarantee_cc_return (unsigned long offset)
{
if (CC_ENTRY_P (STACK_REF (offset)))
return;
assert (RETURN_CODE_P (CONT_RET (offset)));
if (CHECK_RETURN_CODE (RC_REENTER_COMPILED_CODE, offset))
{
unsigned long lrc = (FIXNUM_TO_ULONG (CONT_EXP (offset)));
close_stack_gap (offset, CONTINUATION_SIZE);
last_return_code = (STACK_LOC (offset + lrc));
CHECK_LAST_RETURN_CODE ();
COMPILER_END_SUBPROBLEM ();
}
else
{
last_return_code = (STACK_LOC (offset));
CHECK_LAST_RETURN_CODE ();
open_stack_gap (offset, 1);
(STACK_REF (offset)) = return_to_interpreter;
}
}
void
guarantee_interp_return (void)
{
unsigned long offset = (1 + (APPLY_FRAME_SIZE ()));
if (RETURN_CODE_P (CONT_RET (offset)))
return;
assert (CC_ENTRY_P (STACK_REF (offset)));
if ((STACK_REF (offset)) == return_to_interpreter)
{
assert (RETURN_CODE_P (CONT_RET (offset + 1)));
close_stack_gap (offset, 1);
COMPILER_NEW_REDUCTION ();
}
else
{
open_stack_gap (offset, CONTINUATION_SIZE);
{
SCHEME_OBJECT * sp = stack_pointer;
stack_pointer = (STACK_LOC (offset + CONTINUATION_SIZE));
SAVE_LAST_RETURN_CODE (RC_REENTER_COMPILED_CODE);
stack_pointer = sp;
}
}
}
static void
open_stack_gap (unsigned long offset, unsigned long n_words)
{
SCHEME_OBJECT * scan_from = (STACK_LOC (0));
SCHEME_OBJECT * scan_end = (STACK_LOC (offset));
SCHEME_OBJECT * scan_to = (STACK_LOC (-n_words));
while (scan_from != scan_end)
(STACK_LOCATIVE_POP (scan_to)) = (STACK_LOCATIVE_POP (scan_from));
stack_pointer = (STACK_LOC (-n_words));
}
static void
close_stack_gap (unsigned long offset, unsigned long n_words)
{
SCHEME_OBJECT * scan_from = (STACK_LOC (offset));
SCHEME_OBJECT * scan_end = (STACK_LOC (0));
SCHEME_OBJECT * scan_to = (STACK_LOC (offset + n_words));
while (scan_from != scan_end)
(STACK_LOCATIVE_PUSH (scan_to)) = (STACK_LOCATIVE_PUSH (scan_from));
stack_pointer = (STACK_LOC (n_words));
}
static void
recover_from_apply_error (SCHEME_OBJECT procedure, unsigned long n_args)
{
STACK_PUSH (procedure);
PUSH_APPLY_FRAME_HEADER (n_args);
guarantee_interp_return ();
}
/* SCHEME_UTILITY procedures
Here's a mass of procedures that are called (via
'scheme_to_interface', an assembly language hook) by compiled code
to do various jobs. */
#define DEFINE_SCHEME_UTILITY_0(pname) \
void \
ASM_ENTRY_POINT (pname) \
(utility_result_t * DSU_result, \
unsigned long ignore1, \
unsigned long ignore2, \
unsigned long ignore3, \
unsigned long ignore4)
#define DEFINE_SCHEME_UTILITY_1(pname, av1) \
void \
ASM_ENTRY_POINT (pname) \
(utility_result_t * DSU_result, \
unsigned long av1##_raw, \
unsigned long ignore2, \
unsigned long ignore3, \
unsigned long ignore4)
#define DEFINE_SCHEME_UTILITY_2(pname, av1, av2) \
void \
ASM_ENTRY_POINT (pname) \
(utility_result_t * DSU_result, \
unsigned long av1##_raw, \
unsigned long av2##_raw, \
unsigned long ignore3, \
unsigned long ignore4)
#define DEFINE_SCHEME_UTILITY_3(pname, av1, av2, av3) \
void \
ASM_ENTRY_POINT (pname) \
(utility_result_t * DSU_result, \
unsigned long av1##_raw, \
unsigned long av2##_raw, \
unsigned long av3##_raw, \
unsigned long ignore4)
#define DEFINE_SCHEME_UTILITY_4(pname, av1, av2, av3, av4) \
void \
ASM_ENTRY_POINT (pname) \
(utility_result_t * DSU_result, \
unsigned long av1##_raw, \
unsigned long av2##_raw, \
unsigned long av3##_raw, \
unsigned long av4##_raw)
#define DECLARE_UTILITY_ARG(at1, av1) at1 av1 = ((at1) av1##_raw)
#define INVOKE_RETURN_ADDRESS() do \
{ \
if (Free >= GET_MEMTOP) \
{ \
compiler_interrupt_common (DSU_result, 0, GET_VAL); \
return; \
} \
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (STACK_POP ())); \
} while (false)
#define TAIL_CALL_1(pname, a1) do \
{ \
pname (DSU_result, ((unsigned long) (a1)), 0, 0, 0); \
return; \
} while (false)
#define TAIL_CALL_2(pname, a1, a2) do \
{ \
pname (DSU_result, \
((unsigned long) (a1)), \
((unsigned long) (a2)), \
0, \
0); \
return; \
} while (false)
DEFINE_SCHEME_UTILITY_2 (comutil_apply, procedure, frame_size)
{
DECLARE_UTILITY_ARG (SCHEME_OBJECT, procedure);
DECLARE_UTILITY_ARG (unsigned long, frame_size);
while (1)
switch (OBJECT_TYPE (procedure))
{
case TC_ENTITY:
{
SCHEME_OBJECT data = (MEMORY_REF (procedure, ENTITY_DATA));
if ((VECTOR_P (data))
&& ((VECTOR_LENGTH (data)) > frame_size)
&& ((VECTOR_REF (data, 0))
== (VECTOR_REF (fixed_objects, ARITY_DISPATCHER_TAG)))
&& ((VECTOR_REF (data, frame_size)) != SHARP_F))
{
procedure = (VECTOR_REF (data, frame_size));
break;
}
}
{
SCHEME_OBJECT operator = (MEMORY_REF (procedure, ENTITY_OPERATOR));
if (!CC_ENTRY_P (operator))
goto handle_in_interpreter;
STACK_PUSH (procedure);
procedure = operator;
frame_size += 1;
goto invoke_compiled_entry;
}
case TC_RECORD:
{
SCHEME_OBJECT applicator = record_applicator(procedure);
if (!CC_ENTRY_P (applicator))
goto handle_in_interpreter;
STACK_PUSH (procedure);
procedure = applicator;
frame_size += 1;
goto invoke_compiled_entry;
}
invoke_compiled_entry:
case TC_COMPILED_ENTRY:
{
long code
= (setup_compiled_invocation (procedure, (frame_size - 1)));
if (code != PRIM_DONE)
RETURN_TO_C (code);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
case TC_PRIMITIVE:
if (IMPLEMENTED_PRIMITIVE_P (procedure))
{
int arity = (PRIMITIVE_ARITY (procedure));
if (arity == (frame_size - 1))
TAIL_CALL_1 (comutil_primitive_apply, procedure);
else if (arity == LEXPR)
{
SET_LEXPR_ACTUALS (frame_size - 1);
TAIL_CALL_1 (comutil_primitive_lexpr_apply, procedure);
}
else
{
recover_from_apply_error (procedure, (frame_size - 1));
RETURN_TO_C (ERR_WRONG_NUMBER_OF_ARGUMENTS);
}
}
/* fall through */
handle_in_interpreter:
default:
{
recover_from_apply_error (procedure, (frame_size - 1));
RETURN_TO_C (PRIM_APPLY);
}
}
}
/* comutil_lexpr_apply is invoked to reformat the frame when compiled
code calls a known lexpr. The actual arguments are on the stack,
and it is given the number of arguments and the real entry point of
the procedure. */
DEFINE_SCHEME_UTILITY_2 (comutil_lexpr_apply, address, n_args)
{
DECLARE_UTILITY_ARG (insn_t *, address);
DECLARE_UTILITY_ARG (unsigned long, n_args);
cc_entry_type_t cet;
if (! ((!read_cc_entry_type ((&cet), address))
&& ((cet.marker) == CET_PROCEDURE)
&& (cet.args.for_procedure.rest_p)
&& (n_args >= (cet.args.for_procedure.n_required))))
{
recover_from_apply_error ((MAKE_CC_ENTRY (address)), n_args);
RETURN_TO_C (ERR_COMPILED_CODE_ERROR);
}
{
long code
= (setup_lexpr_invocation ((MAKE_CC_ENTRY (address)),
n_args,
((cet.args.for_procedure.n_required)
+ (cet.args.for_procedure.n_optional))));
if (code != PRIM_DONE)
RETURN_TO_C (code);
}
RETURN_TO_SCHEME (address);
}
/* comutil_primitive_apply is used to invoked a C primitive. Note
that some C primitives (the so called interpreter hooks) will not
return normally, but will "longjmp" to the interpreter instead.
Thus the assembly language invoking this should have set up the
appropriate locations in case this happens. After invoking the
primitive, it pops the arguments off the Scheme stack, and proceeds
by invoking the continuation on top of the stack. */
DEFINE_SCHEME_UTILITY_1 (comutil_primitive_apply, primitive)
{
DECLARE_UTILITY_ARG (SCHEME_OBJECT, primitive);
PRIMITIVE_APPLY (primitive);
POP_PRIMITIVE_FRAME (PRIMITIVE_ARITY (primitive));
INVOKE_RETURN_ADDRESS ();
}
/* comutil_primitive_lexpr_apply is like comutil_primitive_apply
except that it is used to invoke primitives that take an arbitrary
number of arguments. The number of arguments is in the
REGBLOCK_LEXPR_ACTUALS slot of the register block. */
DEFINE_SCHEME_UTILITY_1 (comutil_primitive_lexpr_apply, primitive)
{
DECLARE_UTILITY_ARG (SCHEME_OBJECT, primitive);
PRIMITIVE_APPLY (primitive);
POP_PRIMITIVE_FRAME (GET_LEXPR_ACTUALS);
INVOKE_RETURN_ADDRESS ();
}
/* comutil_error is used by compiled code to signal an error. It
expects the arguments to the error procedure to be pushed on the
stack, and is passed the number of arguments (+ 1). */
DEFINE_SCHEME_UTILITY_1 (comutil_error, frame_size)
{
DECLARE_UTILITY_ARG (unsigned long, frame_size);
TAIL_CALL_2 (comutil_apply,
(VECTOR_REF (fixed_objects, CC_ERROR_PROCEDURE)),
frame_size);
}
/* comutil_link is used to initialize all the variable cache slots for
a compiled code block. It is called at load time, by the compiled
code itself. It assumes that the return address has been saved on
the stack. If an error occurs during linking, or an interrupt must
be processed (because of the need to GC, etc.), it backs out and
sets up a return code that will invoke comp_link_caches_restart
when the error/interrupt processing is done. */
DEFINE_SCHEME_UTILITY_4 (comutil_link,
return_addr,
block_addr,
constant_addr,
n_sections)
{
DECLARE_UTILITY_ARG (insn_t *, return_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, block_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, constant_addr);
DECLARE_UTILITY_ARG (unsigned long, n_sections);
link_cc_state_t s;
(s.return_address) = return_addr;
(s.block_address) = block_addr;
(s.scan) = constant_addr;
(s.n_sections) = n_sections;
(s.n_linked_sections) = 0;
start_linking_cc_block ();
{
long result = (link_remaining_sections (&s));
end_linking_cc_block (&s);
if (result != PRIM_DONE)
RETURN_TO_C (result);
}
RETURN_TO_SCHEME (s.return_address);
}
/* comp_link_caches_restart is used to continue the linking process
started by comutil_link after the garbage collector has run. */
DEFINE_SCHEME_ENTRY (comp_link_caches_restart)
{
link_cc_state_t s;
long result;
restore_link_cc_state (&s);
SET_ENV (cc_block_environment (MAKE_CC_BLOCK (s.block_address)));
start_linking_cc_block ();
result = (link_section (&s));
if (result == PRIM_DONE)
result = (link_remaining_sections (&s));
end_linking_cc_block (&s);
if (result != PRIM_DONE)
return (result);
ENTER_SCHEME (s.return_address);
}
static long
link_remaining_sections (link_cc_state_t * s)
{
while ((s->n_linked_sections) < (s->n_sections))
{
start_linking_section (s);
{
long result = (link_section (s));
if (result != PRIM_DONE)
return (result);
}
}
return (PRIM_DONE);
}
static void
start_linking_cc_block (void)
{
bool * ap = (dstack_alloc (sizeof (bool)));
(*ap) = linking_cc_block_p;
transaction_begin ();
transaction_record_action (tat_always, abort_linking_cc_block, ap);
linking_cc_block_p = 1;
}
static void
end_linking_cc_block (link_cc_state_t * s)
{
transaction_commit ();
update_cache_after_link (s);
}
static void
abort_linking_cc_block (void * ap)
{
linking_cc_block_p = (* ((bool *) (ap)));
}
static void
update_cache_after_link (link_cc_state_t * s)
{
#if defined(FLUSH_I_CACHE_REGION) || defined(PUSH_D_CACHE_REGION)
SCHEME_OBJECT * addr = (s->block_address);
if ((cc_entry_address_to_block_address (s->return_address)) == addr)
#ifdef FLUSH_I_CACHE_REGION
FLUSH_I_CACHE_REGION (addr, (CC_BLOCK_ADDR_LENGTH (addr)));
#else
;
#endif
else
#ifdef PUSH_D_CACHE_REGION
PUSH_D_CACHE_REGION (addr, (CC_BLOCK_ADDR_LENGTH (addr)));
#else
;
#endif
#endif
}
static void
start_linking_section (link_cc_state_t * s)
{
(s->scan0) = (s->scan);
(s->n_linked_entries) = 0;
{
SCHEME_OBJECT header = (*(s->scan)++);
(s->type) = (linkage_section_type (header));
(s->n_entries) = (linkage_section_count (header));
}
}
static long
link_section (link_cc_state_t * s)
{
SCHEME_OBJECT * scan1 = ((s->scan0) + 1);
SCHEME_OBJECT * scan = (s->scan);
SCHEME_OBJECT * block_address = (s->block_address);
unsigned long n_linked = (s->n_linked_entries);
unsigned long n_entries = (s->n_entries);
cache_handler_t * handler;
bool execute_p;
unsigned long entry_size;
long result = PRIM_DONE;
DECLARE_RELOCATION_REFERENCE (ref);
if (!link_section_handler ((s->type), (&handler), (&execute_p)))
{
result = ERR_COMPILED_CODE_ERROR;
goto done;
}
if (execute_p)
{
/* Hair: START_OPERATOR_RELOCATION requires scan to be pointing
to the first word after the header. Also, it might move scan
forward. If we are just starting the link, just use scan as
the argument and let it be changed. If we are restarting, we
need to use use a temporary variable that points to the right
place. */
if (n_linked == 0)
START_OPERATOR_RELOCATION (scan, ref);
else
START_OPERATOR_RELOCATION (scan1, ref);
entry_size = UUO_LINK_SIZE;
}
else
entry_size = 1;
while (n_linked < n_entries)
{
result = ((*handler) ((execute_p
? (read_uuo_symbol (scan))
: (*scan)),
(MAKE_CC_BLOCK (block_address)),
(scan - block_address)));
if (result != PRIM_DONE)
break;
scan += entry_size;
n_linked += 1;
}
done:
/* If we failed on the first entry, back scan up to where it was
before START_OPERATOR_RELOCATION possibly changed it. */
(s->scan) = ((n_linked == 0) ? scan1 : scan);
(s->n_linked_entries) = n_linked;
(* (s->scan0)) = (make_linkage_section_marker ((s->type), n_linked));
if (result == PRIM_DONE)
(s->n_linked_sections) += 1;
else
back_out_of_link_section (s);
return (result);
}
static bool
link_section_handler (linkage_section_type_t type,
cache_handler_t ** handler_r,
bool * execute_p_r)
{
switch (type)
{
case LINKAGE_SECTION_TYPE_OPERATOR:
(*handler_r) = compiler_cache_operator;
(*execute_p_r) = true;
return (true);
case LINKAGE_SECTION_TYPE_GLOBAL_OPERATOR:
(*handler_r) = compiler_cache_global_operator;
(*execute_p_r) = true;
return (true);
case LINKAGE_SECTION_TYPE_REFERENCE:
(*handler_r) = compiler_cache_lookup;
(*execute_p_r) = false;
return (true);
case LINKAGE_SECTION_TYPE_ASSIGNMENT:
(*handler_r) = compiler_cache_assignment;
(*execute_p_r) = false;
return (true);
default:
return (false);
}
}
static void
back_out_of_link_section (link_cc_state_t * s)
{
/* Save enough state to restart. */
STACK_PUSH (MAKE_CC_ENTRY (s->return_address));
STACK_PUSH (ULONG_TO_FIXNUM ((s->n_sections) - (s->n_linked_sections)));
STACK_PUSH (ULONG_TO_FIXNUM ((s->scan0) - (s->block_address)));
STACK_PUSH (ULONG_TO_FIXNUM ((s->scan) - (s->block_address)));
STACK_PUSH (MAKE_CC_BLOCK (s->block_address));
STACK_PUSH (ULONG_TO_FIXNUM ((s->n_entries) - (s->n_linked_entries)));
STACK_PUSH (ULONG_TO_FIXNUM (s->n_entries));
SAVE_LAST_RETURN_CODE (RC_COMP_LINK_CACHES_RESTART);
}
static void
restore_link_cc_state (link_cc_state_t * s)
{
RESTORE_LAST_RETURN_CODE ();
(s->n_entries) = (OBJECT_DATUM (STACK_POP ()));
(s->n_linked_entries) = ((s->n_entries) - (OBJECT_DATUM (STACK_POP ())));
(s->block_address) = (OBJECT_ADDRESS (STACK_POP ()));
(s->scan) = ((s->block_address) + (OBJECT_DATUM (STACK_POP ())));
(s->scan0) = ((s->block_address) + (OBJECT_DATUM (STACK_POP ())));
(s->n_sections) = (OBJECT_DATUM (STACK_POP ()));
(s->return_address) = (CC_ENTRY_ADDRESS (STACK_POP ()));
(s->n_linked_sections) = 0;
(s->type) = (linkage_section_type (* (s->scan0)));
}
SCHEME_OBJECT
cc_block_linkage_info (SCHEME_OBJECT block)
{
SCHEME_OBJECT * const_addr
= (VECTOR_LOC (block, (1 + (VECTOR_LENGTH (MAKE_POINTER_OBJECT (TC_VECTOR, (VECTOR_LOC (block, 0))))))));
SCHEME_OBJECT * block_end = (CC_BLOCK_END (block));
unsigned long n_sections;
unsigned long n_words;
count_linkage_sections (const_addr, block_end, (&n_sections), (&n_words));
Primitive_GC_If_Needed (n_words);
return (read_linkage_sections (const_addr, block_end, n_sections, n_words));
}
static void
count_linkage_sections (SCHEME_OBJECT * const_addr,
SCHEME_OBJECT * block_end,
unsigned long * n_sections,
unsigned long * n_words)
{
SCHEME_OBJECT * scan = const_addr;
(*n_sections) = 0;
(*n_words) = 1;
while (unlinked_section_start_p (scan, block_end))
{
SCHEME_OBJECT h = (*scan++);
unsigned long count = (linkage_section_count (h));
(*n_sections) += 1;
/* One word for sections vector, one word for vector header,
one word for type, rest for names. */
(*n_words) += (3 + count);
scan +=
((section_execute_p (h))
? (OPERATOR_RELOCATION_OFFSET + (count * UUO_LINK_SIZE))
: count);
}
}
static SCHEME_OBJECT
read_linkage_sections (SCHEME_OBJECT * const_addr,
SCHEME_OBJECT * block_end,
unsigned long n_sections,
unsigned long n_words)
{
SCHEME_OBJECT * scan = const_addr;
SCHEME_OBJECT sections = (make_vector (n_sections, SHARP_F, false));
SCHEME_OBJECT * sp = (VECTOR_LOC (sections, 0));
SCHEME_OBJECT * spe = (VECTOR_LOC (sections, (VECTOR_LENGTH (sections))));
while (sp < spe)
{
SCHEME_OBJECT h = (*scan++);
SCHEME_OBJECT section
= (make_vector ((1 + (linkage_section_count (h))), SHARP_F, false));
SCHEME_OBJECT * p = (VECTOR_LOC (section, 0));
SCHEME_OBJECT * pe = (VECTOR_LOC (section, (VECTOR_LENGTH (section))));
(*p++) = (ULONG_TO_FIXNUM ((unsigned long) (linkage_section_type (h))));
if (section_execute_p (h))
{
scan += OPERATOR_RELOCATION_OFFSET;
while (p < pe)
{
(*p++) = (read_uuo_symbol (scan));
scan += UUO_LINK_SIZE;
}
}
else
while (p < pe)
(*p++) = (*scan++);
(*sp++) = section;
}
return (sections);
}
static bool
section_execute_p (SCHEME_OBJECT h)
{
linkage_section_type_t type = (linkage_section_type (h));
return
((type == LINKAGE_SECTION_TYPE_OPERATOR)
|| (type == LINKAGE_SECTION_TYPE_GLOBAL_OPERATOR));
}
/* Interrupt/GC from Scheme
These procedures are called from compiled code at the start
(respectively) of a procedure or continuation if an interrupt has
been detected. They must not be called unless there is an
interrupt to be serviced.
The code that handles RC_COMP_INTERRUPT_RESTART in "interp.c" will
return control to comp_interrupt_restart (below). This assumes
that the Scheme stack contains a compiled code entry address (start
of continuation, procedure, etc.). The GET_EXP saved with the
continuation is a piece of state that will be returned to
GET_VAL and GET_ENV (both) upon return. */
DEFINE_SCHEME_UTILITY_0 (comutil_interrupt_closure)
{
compiler_interrupt_common (DSU_result, 0, SHARP_F);
}
DEFINE_SCHEME_UTILITY_2 (comutil_interrupt_dlink, entry_point, dlink)
{
DECLARE_UTILITY_ARG (insn_t *, entry_point);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, dlink);
compiler_interrupt_common (DSU_result,
entry_point,
(MAKE_CC_STACK_ENV (dlink)));
}
DEFINE_SCHEME_UTILITY_1 (comutil_interrupt_procedure, entry_point)
{
DECLARE_UTILITY_ARG (insn_t *, entry_point);
compiler_interrupt_common (DSU_result, entry_point, SHARP_F);
}
/* GET_VAL has live data, and there is no entry address on the stack */
DEFINE_SCHEME_UTILITY_1 (comutil_interrupt_continuation, return_addr)
{
DECLARE_UTILITY_ARG (insn_t *, return_addr);
compiler_interrupt_common (DSU_result, return_addr, GET_VAL);
}
/* GET_ENV has live data; no entry point on the stack */
DEFINE_SCHEME_UTILITY_1 (comutil_interrupt_ic_procedure, entry_point)
{
DECLARE_UTILITY_ARG (insn_t *, entry_point);
compiler_interrupt_common (DSU_result, entry_point, GET_ENV);
}
DEFINE_SCHEME_UTILITY_0 (comutil_interrupt_continuation_2)
{
compiler_interrupt_common (DSU_result, 0, GET_VAL);
}
void
compiler_interrupt_common (utility_result_t * DSU_result,
insn_t * address,
SCHEME_OBJECT state)
{
if (!FREE_OK_P (Free))
REQUEST_GC (Free - heap_alloc_limit);
STACK_CHECK (0);
if (address != 0)
STACK_PUSH (MAKE_CC_ENTRY (address));
STACK_PUSH (state);
SAVE_LAST_RETURN_CODE (RC_COMP_INTERRUPT_RESTART);
RETURN_TO_C (PRIM_INTERRUPT);
}
DEFINE_SCHEME_ENTRY (comp_interrupt_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT state = (STACK_POP ());
SET_ENV (state);
SET_VAL (state);
}
JUMP_TO_CC_ENTRY (STACK_POP ());
}
/* Other traps */
/* Assigning a variable that contains a trap. */
DEFINE_SCHEME_UTILITY_3 (comutil_assignment_trap,
ret_addr, cache_addr, new_val)
{
DECLARE_UTILITY_ARG (insn_t *, ret_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, cache_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT, new_val);
SCHEME_OBJECT cache = (MAKE_POINTER_OBJECT (CACHE_TYPE, cache_addr));
SCHEME_OBJECT old_val;
long code = (compiler_assignment_trap (cache, new_val, (&old_val)));
if (code != PRIM_DONE)
{
SCHEME_OBJECT sra = (MAKE_CC_ENTRY (ret_addr));
SCHEME_OBJECT block = (cc_entry_to_block (sra));
STACK_PUSH (sra);
STACK_PUSH (new_val);
STACK_PUSH (cc_block_environment (block));
STACK_PUSH
(compiler_var_error (cache, block, CACHE_REFERENCES_ASSIGNMENT));
SAVE_LAST_RETURN_CODE (RC_COMP_ASSIGNMENT_TRAP_RESTART);
RETURN_TO_C (code);
}
SET_VAL (old_val);
RETURN_TO_SCHEME (ret_addr);
}
DEFINE_SCHEME_ENTRY (comp_assignment_trap_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT name = (STACK_POP ());
SCHEME_OBJECT environment = (STACK_POP ());
SCHEME_OBJECT new_val = (STACK_POP ());
SCHEME_OBJECT old_val;
long code = (assign_variable (environment, name, new_val, (&old_val)));
if (code != PRIM_DONE)
{
STACK_PUSH (new_val);
STACK_PUSH (environment);
STACK_PUSH (name);
SAVE_LAST_RETURN_CODE (RC_COMP_ASSIGNMENT_TRAP_RESTART);
return (code);
}
SET_VAL (old_val);
JUMP_TO_CC_ENTRY (STACK_POP ());
}
}
DEFINE_SCHEME_UTILITY_3 (comutil_cache_lookup_apply,
cache_addr, block_addr, frame_size)
{
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, cache_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, block_addr);
DECLARE_UTILITY_ARG (unsigned long, frame_size);
SCHEME_OBJECT cache = (MAKE_POINTER_OBJECT (CACHE_TYPE, cache_addr));
SCHEME_OBJECT value;
long code = (compiler_lookup_trap (cache, (&value)));
if (code != PRIM_DONE)
{
SCHEME_OBJECT block = (MAKE_CC_BLOCK (block_addr));
STACK_PUSH (block);
STACK_PUSH (ULONG_TO_FIXNUM (frame_size));
STACK_PUSH (cc_block_environment (block));
STACK_PUSH
(compiler_var_error (cache, block, CACHE_REFERENCES_OPERATOR));
SAVE_LAST_RETURN_CODE (RC_COMP_CACHE_REF_APPLY_RESTART);
RETURN_TO_C (code);
}
TAIL_CALL_2 (comutil_apply, value, frame_size);
}
DEFINE_SCHEME_ENTRY (comp_cache_lookup_apply_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT name = (STACK_POP ());
SCHEME_OBJECT environment = (STACK_POP ());
SCHEME_OBJECT frame_size = (STACK_POP ());
SCHEME_OBJECT block = (STACK_POP ());
SCHEME_OBJECT value;
{
long code = (lookup_variable (environment, name, (&value)));
if (code != PRIM_DONE)
{
STACK_PUSH (block);
STACK_PUSH (frame_size);
STACK_PUSH (environment);
STACK_PUSH (name);
SAVE_LAST_RETURN_CODE (RC_COMP_CACHE_REF_APPLY_RESTART);
return (code);
}
}
STACK_PUSH (value);
PUSH_APPLY_FRAME_HEADER ((FIXNUM_TO_ULONG (frame_size)) - 1);
if (CC_ENTRY_P (value))
return (apply_compiled_procedure ());
guarantee_interp_return ();
return (PRIM_APPLY);
}
}
/* Variable reference traps:
Reference to a free variable that contains a reference trap. */
DEFINE_SCHEME_UTILITY_2 (comutil_lookup_trap, ret_addr, cache_addr)
{
DECLARE_UTILITY_ARG (insn_t *, ret_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, cache_addr);
SCHEME_OBJECT cache = (MAKE_POINTER_OBJECT (CACHE_TYPE, cache_addr));
SCHEME_OBJECT val;
long code = (compiler_lookup_trap (cache, (&val)));
if (code != PRIM_DONE)
{
SCHEME_OBJECT sra = (MAKE_CC_ENTRY (ret_addr));
SCHEME_OBJECT block = (cc_entry_to_block (sra));
STACK_PUSH (sra);
STACK_PUSH (cc_block_environment (block));
STACK_PUSH (compiler_var_error (cache, block, CACHE_REFERENCES_LOOKUP));
SAVE_LAST_RETURN_CODE (RC_COMP_LOOKUP_TRAP_RESTART);
RETURN_TO_C (code);
}
SET_VAL (val);
RETURN_TO_SCHEME (ret_addr);
}
DEFINE_SCHEME_ENTRY (comp_lookup_trap_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT name = GET_EXP;
SCHEME_OBJECT environment = (STACK_POP ());
SCHEME_OBJECT val;
long code = (lookup_variable (environment, name, (&val)));
if (code != PRIM_DONE)
{
STACK_PUSH (environment);
STACK_PUSH (name);
SAVE_LAST_RETURN_CODE (RC_COMP_LOOKUP_TRAP_RESTART);
return (code);
}
SET_VAL (val);
JUMP_TO_CC_ENTRY (STACK_POP ());
}
}
DEFINE_SCHEME_UTILITY_2 (comutil_safe_lookup_trap, ret_addr, cache_addr)
{
DECLARE_UTILITY_ARG (insn_t *, ret_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, cache_addr);
SCHEME_OBJECT cache = (MAKE_POINTER_OBJECT (CACHE_TYPE, cache_addr));
SCHEME_OBJECT val;
long code = (compiler_safe_lookup_trap (cache, (&val)));
if (code != PRIM_DONE)
{
SCHEME_OBJECT sra = (MAKE_CC_ENTRY (ret_addr));
SCHEME_OBJECT block = (cc_entry_to_block (sra));
STACK_PUSH (sra);
STACK_PUSH (cc_block_environment (block));
STACK_PUSH (compiler_var_error (cache, block, CACHE_REFERENCES_LOOKUP));
SAVE_LAST_RETURN_CODE (RC_COMP_SAFE_REF_TRAP_RESTART);
RETURN_TO_C (code);
}
SET_VAL (val);
RETURN_TO_SCHEME (ret_addr);
}
DEFINE_SCHEME_ENTRY (comp_safe_lookup_trap_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT name = GET_EXP;
SCHEME_OBJECT environment = (STACK_POP ());
SCHEME_OBJECT val;
long code = (safe_lookup_variable (environment, name, (&val)));
if (code != PRIM_DONE)
{
STACK_PUSH (environment);
STACK_PUSH (name);
SAVE_LAST_RETURN_CODE (RC_COMP_SAFE_REF_TRAP_RESTART);
return (code);
}
SET_VAL (val);
JUMP_TO_CC_ENTRY (STACK_POP ());
}
}
DEFINE_SCHEME_UTILITY_2 (comutil_unassigned_p_trap, ret_addr, cache_addr)
{
DECLARE_UTILITY_ARG (insn_t *, ret_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, cache_addr);
SCHEME_OBJECT cache = (MAKE_POINTER_OBJECT (CACHE_TYPE, cache_addr));
SCHEME_OBJECT val;
long code = (compiler_unassigned_p_trap (cache, (&val)));
if (code != PRIM_DONE)
{
SCHEME_OBJECT sra = (MAKE_CC_ENTRY (ret_addr));
SCHEME_OBJECT block = (cc_entry_to_block (sra));
STACK_PUSH (sra);
STACK_PUSH (cc_block_environment (block));
STACK_PUSH (compiler_var_error (cache, block, CACHE_REFERENCES_LOOKUP));
SAVE_LAST_RETURN_CODE (RC_COMP_UNASSIGNED_TRAP_RESTART);
RETURN_TO_C (code);
}
SET_VAL (val);
RETURN_TO_SCHEME (ret_addr);
}
DEFINE_SCHEME_ENTRY (comp_unassigned_p_trap_restart)
{
RESTORE_LAST_RETURN_CODE ();
{
SCHEME_OBJECT name = GET_EXP;
SCHEME_OBJECT environment = (STACK_POP ());
SCHEME_OBJECT val;
long code = (variable_unassigned_p (environment, name, (&val)));
if (code != PRIM_DONE)
{
STACK_PUSH (environment);
STACK_PUSH (name);
SAVE_LAST_RETURN_CODE (RC_COMP_UNASSIGNED_TRAP_RESTART);
return (code);
}
SET_VAL (val);
JUMP_TO_CC_ENTRY (STACK_POP ());
}
}
/* Numeric routines
Invoke the arithmetic primitive in the fixed objects vector. The
Scheme arguments are expected on the Scheme stack. */
#define COMPILER_ARITH_PRIM(name, fobj_index, arity) \
DEFINE_SCHEME_UTILITY_0 (name) \
{ \
TAIL_CALL_2 \
(comutil_apply, (VECTOR_REF (fixed_objects, fobj_index)), (arity)); \
}
COMPILER_ARITH_PRIM (comutil_decrement, GENERIC_TRAMPOLINE_PREDECESSOR, 2)
COMPILER_ARITH_PRIM (comutil_divide, GENERIC_TRAMPOLINE_DIVIDE, 3)
COMPILER_ARITH_PRIM (comutil_equal, GENERIC_TRAMPOLINE_EQUAL_P, 3)
COMPILER_ARITH_PRIM (comutil_greater, GENERIC_TRAMPOLINE_GREATER_P, 3)
COMPILER_ARITH_PRIM (comutil_increment, GENERIC_TRAMPOLINE_SUCCESSOR, 2)
COMPILER_ARITH_PRIM (comutil_less, GENERIC_TRAMPOLINE_LESS_P, 3)
COMPILER_ARITH_PRIM (comutil_minus, GENERIC_TRAMPOLINE_SUBTRACT, 3)
COMPILER_ARITH_PRIM (comutil_modulo, GENERIC_TRAMPOLINE_MODULO, 3)
COMPILER_ARITH_PRIM (comutil_multiply, GENERIC_TRAMPOLINE_MULTIPLY, 3)
COMPILER_ARITH_PRIM (comutil_negative, GENERIC_TRAMPOLINE_NEGATIVE_P, 2)
COMPILER_ARITH_PRIM (comutil_plus, GENERIC_TRAMPOLINE_ADD, 3)
COMPILER_ARITH_PRIM (comutil_positive, GENERIC_TRAMPOLINE_POSITIVE_P, 2)
COMPILER_ARITH_PRIM (comutil_quotient, GENERIC_TRAMPOLINE_QUOTIENT, 3)
COMPILER_ARITH_PRIM (comutil_remainder, GENERIC_TRAMPOLINE_REMAINDER, 3)
COMPILER_ARITH_PRIM (comutil_zero, GENERIC_TRAMPOLINE_ZERO_P, 2)
DEFINE_SCHEME_UTILITY_2 (comutil_primitive_error, ret_addr, primitive)
{
DECLARE_UTILITY_ARG (insn_t *, ret_addr);
DECLARE_UTILITY_ARG (SCHEME_OBJECT, primitive);
STACK_PUSH (MAKE_CC_ENTRY (ret_addr));
STACK_PUSH (primitive);
SAVE_LAST_RETURN_CODE (RC_COMP_ERROR_RESTART);
RETURN_TO_C (ERR_COMPILED_CODE_ERROR);
}
DEFINE_SCHEME_ENTRY (comp_error_restart)
{
RESTORE_LAST_RETURN_CODE ();
(void) STACK_POP (); /* primitive */
JUMP_TO_CC_ENTRY (STACK_POP ());
}
void
apply_compiled_from_primitive (unsigned long n_args, SCHEME_OBJECT procedure)
{
while (true)
{
switch (OBJECT_TYPE (procedure))
{
case TC_COMPILED_ENTRY:
setup_compiled_invocation_from_primitive (procedure, n_args);
return;
case TC_ENTITY:
{
unsigned long frame_size = (n_args + 1);
SCHEME_OBJECT data = (MEMORY_REF (procedure, ENTITY_DATA));
if ((VECTOR_P (data))
&& (frame_size < (VECTOR_LENGTH (data)))
&& ((VECTOR_REF (data, 0))
== (VECTOR_REF (fixed_objects, ARITY_DISPATCHER_TAG))))
procedure = (VECTOR_REF (data, frame_size));
else
{
STACK_PUSH (procedure);
n_args += 1;
procedure = (MEMORY_REF (procedure, ENTITY_OPERATOR));
}
}
continue;
case TC_RECORD:
{
SCHEME_OBJECT applicator = record_applicator(procedure);
if (applicator == SHARP_F)
goto handle_in_interpreter;
STACK_PUSH (procedure);
n_args += 1;
procedure = applicator;
}
continue;
handle_in_interpreter:
default:
STACK_PUSH (procedure);
PUSH_APPLY_FRAME_HEADER (n_args);
PUSH_REFLECTION (REFLECT_CODE_INTERNAL_APPLY);
return;
}
}
}
void
compiled_with_interrupt_mask (unsigned long old_mask,
SCHEME_OBJECT receiver,
unsigned long new_mask)
{
STACK_PUSH (ULONG_TO_FIXNUM (old_mask));
PUSH_REFLECTION (REFLECT_CODE_RESTORE_INTERRUPT_MASK);
STACK_PUSH (ULONG_TO_FIXNUM (new_mask));
setup_compiled_invocation_from_primitive (receiver, 1);
}
void
compiled_with_stack_marker (SCHEME_OBJECT thunk)
{
PUSH_REFLECTION (REFLECT_CODE_STACK_MARKER);
setup_compiled_invocation_from_primitive (thunk, 0);
}
static void
setup_compiled_invocation_from_primitive (SCHEME_OBJECT procedure,
unsigned long n_args)
{
long code = (setup_compiled_invocation (procedure, n_args));
if (code != PRIM_DONE)
{
if (code != PRIM_APPLY_INTERRUPT)
{
prim_apply_error_code = code;
code = PRIM_APPLY_ERROR;
}
PRIMITIVE_ABORT (code);
}
/* Pun: procedure is being invoked as a return address. Assumes
that the primitive is being called from compiled code. */
STACK_PUSH (procedure);
}
/* Adjust the stack frame for applying a compiled procedure. Returns
PRIM_DONE when successful, otherwise sets up the call frame for
application by the interpreter and returns the appropriate code. */
static long
setup_compiled_invocation (SCHEME_OBJECT procedure, unsigned long n_args)
{
cc_entry_type_t cet;
unsigned long n_min;
unsigned long n_max;
if (read_cc_entry_type ((&cet), (CC_ENTRY_ADDRESS (procedure))))
{
recover_from_apply_error (procedure, n_args);
return (ERR_COMPILED_CODE_ERROR);
}
if ((cet.marker) != CET_PROCEDURE)
{
recover_from_apply_error (procedure, n_args);
return (ERR_INAPPLICABLE_OBJECT);
}
n_min = (cet.args.for_procedure.n_required);
if (n_args < n_min)
{
recover_from_apply_error (procedure, n_args);
return (ERR_WRONG_NUMBER_OF_ARGUMENTS);
}
n_max = (n_min + (cet.args.for_procedure.n_optional));
if (cet.args.for_procedure.rest_p)
return (setup_lexpr_invocation (procedure, n_args, n_max));
if (n_args == n_max)
return (PRIM_DONE);
if (n_args > n_max)
{
recover_from_apply_error (procedure, n_args);
return (ERR_WRONG_NUMBER_OF_ARGUMENTS);
}
if (open_gap (n_args, n_max))
{
recover_from_apply_error (procedure, n_args);
return (PRIM_APPLY_INTERRUPT);
}
return (PRIM_DONE);
}
static long
setup_lexpr_invocation (SCHEME_OBJECT procedure,
unsigned long n_args,
unsigned long n_max)
{
if (n_args <= n_max)
{
if (open_gap (n_args, (n_max + 1)))
{
recover_from_apply_error (procedure, n_args);
return (PRIM_APPLY_INTERRUPT);
}
(STACK_REF (n_max)) = EMPTY_LIST;
return (PRIM_DONE);
}
{
unsigned long n_words = ((n_args - n_max) * 2);
if (GC_NEEDED_P (n_words))
{
REQUEST_GC (n_words);
recover_from_apply_error (procedure, n_args);
return (PRIM_APPLY_INTERRUPT);
}
}
{
SCHEME_OBJECT rest_arg = (MAKE_POINTER_OBJECT (TC_LIST, Free));
SCHEME_OBJECT * p1 = (STACK_LOC (n_max));
{
unsigned long i;
for (i = n_max; (i < n_args); i += 1)
{
(Free[0]) = (STACK_LOCATIVE_POP (p1));
(Free[1]) = (MAKE_POINTER_OBJECT (TC_LIST, (Free + 2)));
Free += 2;
}
}
(Free[-1]) = EMPTY_LIST;
(STACK_LOCATIVE_PUSH (p1)) = rest_arg;
{
SCHEME_OBJECT * p2 = (STACK_LOC (n_max));
unsigned long i;
for (i = 0; (i < n_max); i += 1)
(STACK_LOCATIVE_PUSH (p1)) = (STACK_LOCATIVE_PUSH (p2));
}
stack_pointer = p1;
}
return (PRIM_DONE);
}
static bool
open_gap (unsigned long n_args, unsigned long n_needed)
{
unsigned long n_defaults = (n_needed - n_args);
STACK_CHECK (n_defaults);
if (PENDING_INTERRUPTS_P)
return (true);
open_stack_gap (n_args, n_defaults);
{
SCHEME_OBJECT * scan = (STACK_LOC (n_args));
SCHEME_OBJECT * end = (STACK_LOC (n_needed));
while (scan != end)
(STACK_LOCATIVE_POP (scan)) = DEFAULT_OBJECT;
}
return (false);
}
void
make_compiled_procedure_type (cc_entry_type_t * cet,
unsigned int n_required,
unsigned int n_optional,
bool rest_p)
{
(cet->marker) = CET_PROCEDURE;
(cet->args.for_procedure.n_required) = n_required;
(cet->args.for_procedure.n_optional) = n_optional;
(cet->args.for_procedure.rest_p) = rest_p;
}
void
make_compiled_continuation_type (cc_entry_type_t * cet, unsigned long offset)
{
(cet->marker) = CET_CONTINUATION;
(cet->args.for_continuation.offset) = offset;
}
void
make_cc_entry_type (cc_entry_type_t * cet, cc_entry_type_marker_t marker)
{
assert (! ((marker == CET_PROCEDURE) || (marker == CET_CONTINUATION)));
(cet->marker) = marker;
memset ((& (cet->args)), 0, (sizeof (cet->marker)));
}
SCHEME_OBJECT
cc_entry_to_block (SCHEME_OBJECT entry)
{
return (MAKE_CC_BLOCK (cc_entry_to_block_address (entry)));
}
SCHEME_OBJECT *
cc_entry_to_block_address (SCHEME_OBJECT entry)
{
return (cc_entry_address_to_block_address (CC_ENTRY_ADDRESS (entry)));
}
SCHEME_OBJECT *
cc_entry_address_to_block_address (insn_t * entry)
{
insn_t * p = entry;
while (1)
{
cc_entry_offset_t ceo;
read_cc_entry_offset ((&ceo), p);
p -= (ceo.offset);
if (! (ceo.continued_p))
{
assert ((((unsigned long) p) % (sizeof (SCHEME_OBJECT))) == 0);
assert (((SCHEME_OBJECT *) entry)
< (CC_BLOCK_ADDR_END ((SCHEME_OBJECT *) p)));
return ((SCHEME_OBJECT *) p);
}
}
}
static bool
plausible_first_cc_entry_p (insn_t * entry, insn_t * zero)
{
cc_entry_type_t cet;
cc_entry_offset_t ceo;
if (read_cc_entry_type ((&cet), entry))
return (false);
if (read_cc_entry_offset ((&ceo), entry))
return (false);
if ((ceo.offset) != (entry - zero))
return (false);
return (true);
}
int
plausible_cc_block_p (SCHEME_OBJECT * block)
{
insn_t * zero = ((insn_t *) block);
insn_t * entry = (((insn_t *) (block + 2)) + CC_ENTRY_HEADER_SIZE);
if (!plausible_first_cc_entry_p (entry, zero))
{
entry += CC_ENTRY_GC_TRAP_SIZE;
if (!plausible_first_cc_entry_p (entry, zero))
return (0);
}
{
SCHEME_OBJECT * block_end = ((CC_BLOCK_ADDR_END (block)) - 1);
return
((((HEAP_ADDRESS_P (block)) && (HEAP_ADDRESS_P (block_end)))
|| ((ADDRESS_IN_CONSTANT_P (block))
&& (ADDRESS_IN_CONSTANT_P (block_end))))
&& (ENVIRONMENT_P (*block_end)));
}
}
static bool
unlinked_section_start_p (SCHEME_OBJECT * mp, SCHEME_OBJECT * end)
{
SCHEME_OBJECT marker = (*mp);
return
((FIXNUM_P (marker))
&& (((OBJECT_DATUM (marker)) >> 16) < N_LINKAGE_SECTION_TYPES)
&& ((mp + 1 + ((OBJECT_DATUM (marker)) & 0xFFFFUL)) < end));
}
linkage_section_type_t
linkage_section_type (SCHEME_OBJECT marker)
{
unsigned long type = ((OBJECT_DATUM (marker)) >> 16);
assert (type < N_LINKAGE_SECTION_TYPES);
return ((linkage_section_type_t) type);
}
#ifndef UUO_WORDS_TO_COUNT
# define UUO_WORDS_TO_COUNT(nw) ((nw) / UUO_LINK_SIZE)
# define UUO_COUNT_TO_WORDS(nc) ((nc) * UUO_LINK_SIZE)
#endif
unsigned long
linkage_section_count (SCHEME_OBJECT marker)
{
linkage_section_type_t type = (linkage_section_type (marker));
unsigned long n_words = ((OBJECT_DATUM (marker)) & 0xFFFFUL);
return (((type == LINKAGE_SECTION_TYPE_OPERATOR)
|| (type == LINKAGE_SECTION_TYPE_GLOBAL_OPERATOR))
? (UUO_WORDS_TO_COUNT (n_words))
: n_words);
}
SCHEME_OBJECT
make_linkage_section_marker (linkage_section_type_t type, unsigned long count)
{
unsigned long n_words;
assert (type < N_LINKAGE_SECTION_TYPES);
n_words
= (((type == LINKAGE_SECTION_TYPE_OPERATOR)
|| (type == LINKAGE_SECTION_TYPE_GLOBAL_OPERATOR))
? (UUO_COUNT_TO_WORDS (count))
: count);
assert (n_words < 0x10000);
return (MAKE_OBJECT (TC_LINKAGE_SECTION,
((((unsigned long) (type)) << 16) | n_words)));
}
/* Procedures to destructure compiled entries and closures. */
/* Returns the debugging information attached to 'block'. Usually
this is a string that contains the filename where the debugging
info is stored. */
SCHEME_OBJECT
cc_block_debugging_info (SCHEME_OBJECT block)
{
return (VECTOR_REF (block, ((VECTOR_LENGTH (block)) - 2)));
}
/* Returns the environment where 'block' was evaluated. */
SCHEME_OBJECT
cc_block_environment (SCHEME_OBJECT block)
{
return (VECTOR_REF (block, ((VECTOR_LENGTH (block)) - 1)));
}
unsigned long
cc_entry_to_block_offset (SCHEME_OBJECT entry)
{
return ((CC_ENTRY_ADDRESS (entry))
- ((insn_t *) (cc_entry_to_block_address (entry))));
}
bool
cc_block_closure_p (SCHEME_OBJECT block)
{
return (cc_block_address_closure_p (OBJECT_ADDRESS (block)));
}
bool
cc_entry_closure_p (SCHEME_OBJECT entry)
{
return (cc_block_address_closure_p (cc_entry_to_block_address (entry)));
}
static bool
cc_block_address_closure_p (SCHEME_OBJECT * block_addr)
{
SCHEME_OBJECT header_word = (*block_addr);
return (((OBJECT_TYPE (header_word)) == TC_MANIFEST_CLOSURE));
}
/* Return the entry point ultimately invoked by the compiled closure
'entry'. */
SCHEME_OBJECT
cc_closure_to_entry (SCHEME_OBJECT entry)
{
return (compiled_closure_entry_to_target (CC_ENTRY_ADDRESS (entry)));
}
void
declare_compiled_code_block (SCHEME_OBJECT block)
{
#ifdef PUSH_D_CACHE_REGION
PUSH_D_CACHE_REGION ((OBJECT_ADDRESS (block)), (CC_BLOCK_END (block)));
#endif
}
void
write_variable_cache (SCHEME_OBJECT cache,
SCHEME_OBJECT block,
unsigned long offset)
{
MEMORY_SET (block, offset, ((SCHEME_OBJECT) (OBJECT_ADDRESS (cache))));
}
/* Get a compiled procedure from a cached operator reference. */
SCHEME_OBJECT
read_uuo_link (SCHEME_OBJECT block, unsigned long offset)
{
return
(MAKE_CC_ENTRY (read_uuo_target_no_reloc (MEMORY_LOC (block, offset))));
}
static void
write_uuo_link (SCHEME_OBJECT target, SCHEME_OBJECT * cache_address)
{
write_uuo_target ((CC_ENTRY_ADDRESS (target)), cache_address);
#ifdef FLUSH_I_CACHE_REGION
if (!linking_cc_block_p)
{
/* The linker will flush the whole region afterwards. */
FLUSH_I_CACHE_REGION (cache_address, UUO_LINK_SIZE);
}
#endif
}
SCHEME_OBJECT *
compiled_closure_objects (SCHEME_OBJECT * block)
{
insn_t * start = (compiled_closure_start (block));
unsigned long count = (compiled_closure_count (block));
/* Skip to end of entries. */
while (count > 0)
{
start = (compiled_closure_next (start));
count -= 1;
}
/* Skip to first object. */
return (skip_compiled_closure_padding (start));
}
bool
decode_old_style_format_word (cc_entry_type_t * cet, uint16_t fw)
{
uint16_t low = (fw & 0x00FF);
uint16_t high = ((fw & 0xFF00) >> 8);
bool rest_p = false;
if (high < 0x80)
{
if ((high == 0x00)
|| (low == 0x00)
|| (low == 0x80))
return (true);
if (low > 0x80)
{
low = (0xFF - low);
rest_p = true;
}
if (! (high <= low))
return (true);
make_compiled_procedure_type (cet, (high - 1), (low - high), rest_p);
return (false);
}
if (low < 0x80)
return (true);
if (low < 0xE0)
{
make_compiled_continuation_type
(cet,
(((low & 0x7F) << 7) | (high & 0x7F)));
return (false);
}
if (high != 0xFF)
return (true);
switch (low)
{
case 0xFF:
make_cc_entry_type (cet, CET_EXPRESSION);
break;
case 0xFE:
make_cc_entry_type (cet, CET_INTERNAL_PROCEDURE);
break;
case 0xFD:
make_cc_entry_type (cet, CET_TRAMPOLINE);
break;
case 0xFC:
make_cc_entry_type (cet, CET_INTERNAL_CONTINUATION);
break;
case 0xFB:
make_cc_entry_type (cet, CET_RETURN_TO_INTERPRETER);
break;
case 0xFA:
make_cc_entry_type (cet, CET_CLOSURE);
break;
default:
return (true);
}
return (false);
}
bool
encode_old_style_format_word (cc_entry_type_t * cet, uint16_t * fw_r)
{
unsigned int low;
unsigned int high;
switch (cet->marker)
{
case CET_PROCEDURE:
high = ((cet->args.for_procedure.n_required) + 1);
low = (high + (cet->args.for_procedure.n_optional));
if (! (low < 0x80))
return (true);
if (cet->args.for_procedure.rest_p)
low = (0xFF - low);
break;
case CET_CONTINUATION:
{
unsigned long n = (cet->args.for_continuation.offset);
if (! (n < 0x3000))
return (true);
high = ((n & 0x7F) | 0x80);
low = ((n >> 7) | 0x80);
}
break;
case CET_EXPRESSION:
low = 0xFF;
high = 0xFF;
break;
case CET_INTERNAL_PROCEDURE:
low = 0xFE;
high = 0xFF;
break;
case CET_TRAMPOLINE:
low = 0xFD;
high = 0xFF;
break;
case CET_INTERNAL_CONTINUATION:
low = 0xFC;
high = 0xFF;
break;
case CET_RETURN_TO_INTERPRETER:
low = 0xFB;
high = 0xFF;
break;
case CET_CLOSURE:
low = 0xFA;
high = 0xFF;
break;
default:
return (true);
}
(*fw_r) = ((high << 8) | low);
return (false);
}
/* Trampolines
When a free variable appears in operator position in compiled code,
there must be a directly callable procedure in the corresponding
UUO cell. If, at link time, there is no appropriate value for the
free variable, a fake compiled Scheme procedure that calls one of
these procedures will be placed into the cell instead.
The trampolines themselves are made by 'make_uuo_link',
'make_fake_uuo_link', and 'coerce_to_compiled'. The trampoline
looks like a Scheme closure, containing some code that jumps to one
of these procedures, and additional information to be used by the
procedure.
These procedures expect a single argument, the address of the
information block where they can find the relevant data: typically
the procedure to invoke and the number of arguments to invoke it
with. */
#define DEFINE_TRAMPOLINE(pname) \
DEFINE_SCHEME_UTILITY_1 (pname, TRAMP_store)
#define INIT_TRAMPOLINE_1(av1) \
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, TRAMP_store); \
SCHEME_OBJECT av1 = (TRAMP_store[0])
#define INIT_TRAMPOLINE_2(av1, av2) \
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, TRAMP_store); \
SCHEME_OBJECT av1 = (TRAMP_store[0]); \
SCHEME_OBJECT av2 = (TRAMP_store[1])
#define INIT_TRAMPOLINE_3(av1, av2, av3) \
DECLARE_UTILITY_ARG (SCHEME_OBJECT *, TRAMP_store); \
SCHEME_OBJECT av1 = (TRAMP_store[0]); \
SCHEME_OBJECT av2 = (TRAMP_store[1]); \
SCHEME_OBJECT av3 = (TRAMP_store[2])
/* This is how compiled Scheme code normally returns back to the
Scheme interpreter. It is invoked by a trampoline, which passes
the address of the (empty) trampoline storage block to it. */
DEFINE_TRAMPOLINE (comutil_return_to_interpreter)
{
RETURN_TO_C (PRIM_DONE);
}
DEFINE_TRAMPOLINE (comutil_reflect_to_interface)
{
SCHEME_OBJECT code = (STACK_POP ());
switch (OBJECT_DATUM (code))
{
case REFLECT_CODE_INTERNAL_APPLY:
{
unsigned long frame_size = (OBJECT_DATUM (STACK_POP ()));
SCHEME_OBJECT procedure = (STACK_POP ());
TAIL_CALL_2 (comutil_apply, procedure, frame_size);
}
case REFLECT_CODE_RESTORE_INTERRUPT_MASK:
SET_INTERRUPT_MASK (OBJECT_DATUM (STACK_POP ()));
INVOKE_RETURN_ADDRESS ();
case REFLECT_CODE_STACK_MARKER:
(void) STACK_POP (); /* marker1 */
(void) STACK_POP (); /* marker2 */
INVOKE_RETURN_ADDRESS ();
case REFLECT_CODE_CC_BKPT:
/* Attempt to process interrupts before really proceeding. */
if (Free >= GET_MEMTOP)
{
PUSH_REFLECTION (REFLECT_CODE_CC_BKPT);
compiler_interrupt_common (DSU_result, 0, SHARP_F);
return;
}
{
insn_t * addr;
long code = (do_bkpt_proceed (&addr));
if (code != PRIM_DONE)
{
STACK_PUSH (code);
RETURN_TO_C (code);
}
RETURN_TO_SCHEME (addr);
}
default:
STACK_PUSH (code);
RETURN_TO_C (ERR_EXTERNAL_RETURN);
}
}
DEFINE_TRAMPOLINE (comutil_operator_apply_trap)
{
INIT_TRAMPOLINE_2 (procedure, frame_header);
TAIL_CALL_2 (comutil_apply, procedure, (OBJECT_DATUM (frame_header)));
}
DEFINE_TRAMPOLINE (comutil_operator_primitive_trap)
{
INIT_TRAMPOLINE_1 (primitive);
TAIL_CALL_1 (comutil_primitive_apply, primitive);
}
DEFINE_TRAMPOLINE (comutil_operator_lexpr_trap)
{
INIT_TRAMPOLINE_2 (procedure, frame_header);
SET_LEXPR_ACTUALS (APPLY_FRAME_HEADER_N_ARGS (frame_header));
TAIL_CALL_1 (comutil_primitive_lexpr_apply, procedure);
}
/* ARITY mismatch handling
These receive the entry point as an argument and must fill the
Scheme stack with the missing default values. They are invoked by
TRAMPOLINE_K_n_m where n and m are the same as in the name of the
procedure. All the arguments are on the Scheme stack. */
DEFINE_TRAMPOLINE (comutil_operator_1_0_trap)
{
INIT_TRAMPOLINE_1 (procedure);
STACK_PUSH (DEFAULT_OBJECT);
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_2_0_trap)
{
INIT_TRAMPOLINE_1 (procedure);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_2_1_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_3_0_trap)
{
INIT_TRAMPOLINE_1 (procedure);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_3_1_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_3_2_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
SCHEME_OBJECT a2 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a2);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_4_0_trap)
{
INIT_TRAMPOLINE_1 (procedure);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_4_1_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_4_2_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
SCHEME_OBJECT a2 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a2);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
DEFINE_TRAMPOLINE (comutil_operator_4_3_trap)
{
INIT_TRAMPOLINE_1 (procedure);
{
SCHEME_OBJECT a1 = (STACK_POP ());
SCHEME_OBJECT a2 = (STACK_POP ());
SCHEME_OBJECT a3 = (STACK_POP ());
STACK_PUSH (DEFAULT_OBJECT);
STACK_PUSH (a3);
STACK_PUSH (a2);
STACK_PUSH (a1);
}
RETURN_TO_SCHEME (CC_ENTRY_ADDRESS (procedure));
}
/* The linker either couldn't find a binding or the binding was
unassigned. This must report the correct name of the missing
variable and the environment in which the lookup begins for the
error cases.
'cache' is the linker object corresponding to the operator variable
(it contains the actual value cell, the name, and linker tables).
'block' and 'offset' point to the cache cell in question. */
DEFINE_TRAMPOLINE (comutil_operator_lookup_trap)
{
INIT_TRAMPOLINE_3 (cache, block, offset);
SCHEME_OBJECT * cache_addr = (MEMORY_LOC (block, (OBJECT_DATUM (offset))));
unsigned long frame_size = (read_uuo_frame_size (cache_addr));
SCHEME_OBJECT procedure;
long code = (compiler_operator_reference_trap (cache, (&procedure)));
if (code != PRIM_DONE)
{
STACK_PUSH (MAKE_CC_ENTRY (read_uuo_target_no_reloc (cache_addr)));
/* Next three for debugger. */
STACK_PUSH (ULONG_TO_FIXNUM (frame_size));
STACK_PUSH (cc_block_environment (block));
STACK_PUSH
(compiler_var_error (cache, block, CACHE_REFERENCES_OPERATOR));
SAVE_LAST_RETURN_CODE (RC_COMP_OP_REF_TRAP_RESTART);
RETURN_TO_C (code);
}
TAIL_CALL_2 (comutil_apply, procedure, frame_size);
}
/* Re-start after processing an error/interrupt encountered in the
previous utility. Extract the new trampoline or procedure (the
user may have defined the missing variable) and invoke it. */
DEFINE_SCHEME_ENTRY (comp_op_lookup_trap_restart)
{
RESTORE_LAST_RETURN_CODE ();
/* Discard debugger info. */
stack_pointer = (STACK_LOC (3));
{
SCHEME_OBJECT * store
= (trampoline_storage (cc_entry_to_block_address (STACK_POP ())));
SCHEME_OBJECT block = (store[1]);
unsigned long offset = (OBJECT_DATUM (store[2]));
ENTER_SCHEME (read_uuo_target_no_reloc (MEMORY_LOC (block, offset)));
}
}
/* make_uuo_link is called by C and initializes a compiled procedure
cache at a location given by a block and an offset. */
long
make_uuo_link (SCHEME_OBJECT procedure,
SCHEME_OBJECT cache,
SCHEME_OBJECT block,
unsigned long offset)
{
SCHEME_OBJECT * cache_address = (MEMORY_LOC (block, offset));
unsigned long frame_size = (read_uuo_frame_size (cache_address));
trampoline_type_t kind;
long result;
SCHEME_OBJECT trampoline;
if (REFERENCE_TRAP_P (procedure))
return (make_fake_uuo_link (cache, block, offset));
loop:
switch (OBJECT_TYPE (procedure))
{
case TC_COMPILED_ENTRY:
{
insn_t * entry = (CC_ENTRY_ADDRESS (procedure));
unsigned long nargs = (frame_size - 1);
cc_entry_type_t cet;
unsigned long nmin;
unsigned long nmax;
if ((read_cc_entry_type ((&cet), entry))
|| ((cet.marker) != CET_PROCEDURE))
return (ERR_COMPILED_CODE_ERROR);
nmin = (cet.args.for_procedure.n_required);
nmax = (nmin + (cet.args.for_procedure.n_optional));
if (cet.args.for_procedure.rest_p)
kind = TRAMPOLINE_K_APPLY;
else if (nargs == nmax)
{
/* No defaulting is needed. */
write_uuo_link (procedure, cache_address);
return (PRIM_DONE);
}
else if ((nargs < nmax)
&& (nargs >= nmin)
&& (nmin < nmax)
&& (nmax <= TRAMPOLINE_TABLE_SIZE))
{
/* We have optimized defaulting for this case. */
kind
= (trampoline_arity_table
[(((nmax - 1) * TRAMPOLINE_TABLE_SIZE) + nargs)]);
assert (kind != TRAMPOLINE_K_APPLY);
frame_size = 0;
}
else
/* Use unoptimized defaulting. */
kind = TRAMPOLINE_K_APPLY;
break;
}
case TC_ENTITY:
{
SCHEME_OBJECT data = (MEMORY_REF (procedure, ENTITY_DATA));
if ((VECTOR_P (data))
&& (frame_size < (VECTOR_LENGTH (data)))
&& ((VECTOR_REF (data, frame_size)) != SHARP_F)
&& ((VECTOR_REF (data, 0))
== (VECTOR_REF (fixed_objects, ARITY_DISPATCHER_TAG))))
{
procedure = (VECTOR_REF (data, frame_size));
goto loop;
}
kind = TRAMPOLINE_K_APPLY;
break;
}
case TC_PRIMITIVE:
{
long arity = (PRIMITIVE_ARITY (procedure));
if (arity == ((long) (frame_size - 1)))
{
kind = TRAMPOLINE_K_PRIMITIVE;
frame_size = 0;
}
else if (arity == LEXPR_PRIMITIVE_ARITY)
kind = TRAMPOLINE_K_LEXPR_PRIMITIVE;
else
kind = TRAMPOLINE_K_APPLY;
break;
}
default:
kind = TRAMPOLINE_K_APPLY;
break;
}
result
= ((frame_size == 0)
? (make_redirection_trampoline ((&trampoline), kind, procedure))
: (make_apply_trampoline ((&trampoline), kind, procedure, frame_size)));
if (result == PRIM_DONE)
write_uuo_link (trampoline, cache_address);
return (result);
}
static long
make_fake_uuo_link (SCHEME_OBJECT cache,
SCHEME_OBJECT block,
unsigned long offset)
{
cc_entry_type_t cet;
SCHEME_OBJECT trampoline;
make_cc_entry_type ((&cet), CET_TRAMPOLINE);
{
long result = (make_trampoline ((&trampoline),
(&cet),
TRAMPOLINE_K_LOOKUP,
3,
cache,
block,
(ULONG_TO_FIXNUM (offset))));
if (result != PRIM_DONE)
return (result);
}
{
SCHEME_OBJECT * cache_address = (MEMORY_LOC (block, offset));
write_uuo_link (trampoline, cache_address);
}
return (PRIM_DONE);
}
long
coerce_to_compiled (SCHEME_OBJECT procedure,
unsigned int arity,
SCHEME_OBJECT * location)
{
cc_entry_type_t cet;
if (CC_ENTRY_P (procedure))
{
if (read_cc_entry_type ((&cet), (CC_ENTRY_ADDRESS (procedure))))
return (ERR_COMPILED_CODE_ERROR);
if ((cet.marker) == CET_PROCEDURE)
{
(*location) = procedure;
return (PRIM_DONE);
}
}
make_compiled_procedure_type ((&cet), arity, 0, false);
return (make_trampoline (location,
(&cet),
TRAMPOLINE_K_APPLY,
2,
procedure,
(ULONG_TO_FIXNUM (arity + 1))));
}
static long
make_trampoline (SCHEME_OBJECT * slot,
cc_entry_type_t * cet,
trampoline_type_t kind,
unsigned int n_values,
...)
{
SCHEME_OBJECT h1;
SCHEME_OBJECT h2;
unsigned long n_words;
SCHEME_OBJECT * block;
make_trampoline_headers (1, n_values, (&h1), (&h2), (&n_words));
if (GC_NEEDED_P (n_words))
{
REQUEST_GC (n_words);
return (PRIM_INTERRUPT);
}
block = Free;
Free += n_words;
(block[0]) = h1;
(block[1]) = h2;
if (fill_trampoline (block, 0, cet, kind))
return (ERR_COMPILED_CODE_ERROR);
{
SCHEME_OBJECT * p = (trampoline_storage (block));
va_list ap;
va_start (ap, n_values);
while (n_values > 0)
{
(*p++) = (va_arg (ap, SCHEME_OBJECT));
n_values -= 1;
}
va_end (ap);
}
(*slot) = (MAKE_CC_ENTRY (trampoline_entry_addr (block, 0)));
return (PRIM_DONE);
}
static void
make_trampoline_headers (unsigned long n_entries, unsigned long n_store,
SCHEME_OBJECT * h1_r, SCHEME_OBJECT * h2_r,
unsigned long * n_words_r)
{
unsigned long n1 = (trampoline_entry_size (n_entries));
unsigned long n2 = (1 + n1 + n_store);
(*h1_r) = (MAKE_OBJECT (TC_TRAMPOLINE_HEADER, n2));
(*h2_r) = (MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, n1));
(*n_words_r) = (1 + n2);
}
static bool
fill_trampoline (SCHEME_OBJECT * block,
unsigned long index,
cc_entry_type_t * cet,
trampoline_type_t kind)
{
insn_t * addr = (trampoline_entry_addr (block, index));
if (write_cc_entry_type (cet, addr))
return (true);
{
cc_entry_offset_t ceo;
(ceo.offset) = (addr - ((insn_t *) block));
(ceo.continued_p) = false;
if (write_cc_entry_offset ((&ceo), addr))
return (true);
}
return (store_trampoline_insns (addr, kind));
}
SCHEME_OBJECT *
trampoline_storage (SCHEME_OBJECT * block)
{
return (block + 2 + (OBJECT_DATUM (block[1])));
}
static long
make_redirection_trampoline (SCHEME_OBJECT * slot,
trampoline_type_t kind,
SCHEME_OBJECT procedure)
{
cc_entry_type_t cet;
make_cc_entry_type ((&cet), CET_TRAMPOLINE);
return (make_trampoline (slot, (&cet), kind, 1, procedure));
}
static long
make_apply_trampoline (SCHEME_OBJECT * slot,
trampoline_type_t kind,
SCHEME_OBJECT procedure,
unsigned long frame_size)
{
cc_entry_type_t cet;
make_cc_entry_type ((&cet), CET_TRAMPOLINE);
return (make_trampoline (slot,
(&cet),
kind,
2,
procedure,
(ULONG_TO_FIXNUM (frame_size))));
}
/* Compiled-code breakpoints */
#ifdef HAVE_BKPT_SUPPORT
#define BKPT_PROCEED_FRAME_SIZE 3
SCHEME_OBJECT
bkpt_proceed (insn_t * ep, SCHEME_OBJECT handle, SCHEME_OBJECT state)
{
if (! ((CC_ENTRY_P (STACK_REF (BKPT_PROCEED_FRAME_SIZE)))
&& ((CC_ENTRY_ADDRESS (STACK_REF (BKPT_PROCEED_FRAME_SIZE))) == ep)))
error_external_return ();
PUSH_REFLECTION (REFLECT_CODE_CC_BKPT);
stack_pointer = (STACK_LOC (-BKPT_PROCEED_FRAME_SIZE));
return (SHARP_F);
}
#else /* not HAVE_BKPT_SUPPORT */
SCHEME_OBJECT
bkpt_install (insn_t * ep)
{
return (SHARP_F);
}
SCHEME_OBJECT
bkpt_closure_install (insn_t * ep)
{
return (SHARP_F);
}
void
bkpt_remove (insn_t * ep, SCHEME_OBJECT handle)
{
error_external_return ();
}
bool
bkpt_p (insn_t * ep)
{
return (false);
}
SCHEME_OBJECT
bkpt_proceed (insn_t * ep, SCHEME_OBJECT handle, SCHEME_OBJECT state)
{
error_external_return ();
return (UNSPECIFIC);
}
long
do_bkpt_proceed (insn_t ** addr_r)
{
return (ERR_EXTERNAL_RETURN);
}
#endif /* not HAVE_BKPT_SUPPORT */
DEFINE_SCHEME_UTILITY_2 (comutil_compiled_code_bkpt, entry_addr, state)
{
DECLARE_UTILITY_ARG (insn_t *, entry_addr);
DECLARE_UTILITY_ARG (void *, state);
SCHEME_OBJECT entry = (MAKE_CC_ENTRY (entry_addr));
cc_entry_type_t cet;
SCHEME_OBJECT to_save;
SCHEME_OBJECT stack_ptr;
/* Potential bug: This does not preserve the environment for IC
procedures. There is no way to tell that we have an IC procedure
in our hands. It is not safe to preserve it in general because
the contents of the register may be stale (predate the last GC).
However, the compiler no longer generates IC procedures, and will
probably never do it again. */
if (read_cc_entry_type ((&cet), entry_addr))
to_save = SHARP_F;
else
switch (cet.marker)
{
case CET_CONTINUATION:
to_save = GET_VAL;
break;
case CET_INTERNAL_CONTINUATION:
to_save = (MAKE_CC_STACK_ENV ((SCHEME_OBJECT *) state));
break;
case CET_RETURN_TO_INTERPRETER:
to_save = GET_VAL;
break;
case CET_CLOSURE:
to_save = (MAKE_CC_ENTRY ((insn_t *) state));
break;
default:
to_save = SHARP_F;
break;
}
STACK_PUSH (entry);
stack_ptr = (MAKE_CC_STACK_ENV (stack_pointer));
STACK_PUSH (to_save);
STACK_PUSH (stack_ptr);
STACK_PUSH (entry);
TAIL_CALL_2 (comutil_apply,
(VECTOR_REF (fixed_objects, CC_BKPT_PROCEDURE)),
4);
}
DEFINE_SCHEME_UTILITY_1 (comutil_compiled_closure_bkpt, entry_addr)
{
DECLARE_UTILITY_ARG (insn_t *, entry_addr);
SCHEME_OBJECT entry = (MAKE_CC_ENTRY (entry_addr));
SCHEME_OBJECT stack_ptr;
STACK_PUSH (entry);
stack_ptr = (MAKE_CC_STACK_ENV (stack_pointer));
STACK_PUSH (SHARP_F);
STACK_PUSH (stack_ptr);
STACK_PUSH (entry);
TAIL_CALL_2 (comutil_apply,
(VECTOR_REF (fixed_objects, CC_BKPT_PROCEDURE)),
4);
}
/* Utility table used by the assembly language interface to invoke the
SCHEME_UTILITY procedures that appear in this file.
Important: Do NOT reorder this table without changing the indices
defined on the following page and the corresponding table in the
compiler. */
utility_proc_t * utility_table [] =
{
comutil_return_to_interpreter, /* 0x0 */
comutil_operator_apply_trap, /* 0x1 */
comutil_operator_apply_trap, /* 0x2 */
comutil_operator_apply_trap, /* 0x3 */
comutil_operator_apply_trap, /* 0x4 */
comutil_operator_lexpr_trap, /* 0x5 */
comutil_operator_primitive_trap, /* 0x6 */
comutil_operator_lookup_trap, /* 0x7 */
comutil_operator_1_0_trap, /* 0x8 */
comutil_operator_2_1_trap, /* 0x9 */
comutil_operator_2_0_trap, /* 0xa */
comutil_operator_3_2_trap, /* 0xb */
comutil_operator_3_1_trap, /* 0xc */
comutil_operator_3_0_trap, /* 0xd */
comutil_operator_4_3_trap, /* 0xe */
comutil_operator_4_2_trap, /* 0xf */
comutil_operator_4_1_trap, /* 0x10 */
comutil_operator_4_0_trap, /* 0x11 */
comutil_primitive_apply, /* 0x12 */
comutil_primitive_lexpr_apply, /* 0x13 */
comutil_apply, /* 0x14 */
comutil_error, /* 0x15 */
comutil_lexpr_apply, /* 0x16 */
comutil_link, /* 0x17 */
comutil_interrupt_closure, /* 0x18 */
comutil_interrupt_dlink, /* 0x19 */
comutil_interrupt_procedure, /* 0x1a */
comutil_interrupt_continuation, /* 0x1b */
comutil_interrupt_ic_procedure, /* 0x1c */
comutil_assignment_trap, /* 0x1d */
comutil_cache_lookup_apply, /* 0x1e */
comutil_lookup_trap, /* 0x1f */
comutil_safe_lookup_trap, /* 0x20 */
comutil_unassigned_p_trap, /* 0x21 */
comutil_decrement, /* 0x22 */
comutil_divide, /* 0x23 */
comutil_equal, /* 0x24 */
comutil_greater, /* 0x25 */
comutil_increment, /* 0x26 */
comutil_less, /* 0x27 */
comutil_minus, /* 0x28 */
comutil_multiply, /* 0x29 */
comutil_negative, /* 0x2a */
comutil_plus, /* 0x2b */
comutil_positive, /* 0x2c */
comutil_zero, /* 0x2d */
0, /* 0x2e */
0, /* 0x2f */
0, /* 0x30 */
0, /* 0x31 */
0, /* 0x32 */
0, /* 0x33 */
0, /* 0x34 */
0, /* 0x35 */
comutil_primitive_error, /* 0x36 */
comutil_quotient, /* 0x37 */
comutil_remainder, /* 0x38 */
comutil_modulo, /* 0x39 */
comutil_reflect_to_interface, /* 0x3a */
comutil_interrupt_continuation_2, /* 0x3b */
comutil_compiled_code_bkpt, /* 0x3c */
comutil_compiled_closure_bkpt /* 0x3d */
};
unsigned long max_trampoline
= ((sizeof (utility_table)) / (sizeof (utility_proc_t *)));
/* Support for trap handling. */
const char *
utility_index_to_name (unsigned int index)
{
return (0);
}
int
pc_to_utility_index (unsigned long pc)
{
return (-1);
}
static unsigned int n_builtins = 0;
static unsigned int s_builtins = 0;
static unsigned long * builtins = 0;
static const char ** builtin_names = 0;
void
declare_builtin (unsigned long builtin, const char * name)
{
if (n_builtins == s_builtins)
{
if (s_builtins == 0)
{
s_builtins = 30;
builtins = (malloc (s_builtins * (sizeof (unsigned long))));
builtin_names = (malloc (s_builtins * (sizeof (char *))));
}
else
{
s_builtins += s_builtins;
builtins
= (realloc (builtins, (s_builtins * (sizeof (unsigned long)))));
builtin_names
= (realloc (builtin_names, (s_builtins * (sizeof (char *)))));
}
if ((builtins == 0) || (builtin_names == 0))
{
outf_fatal ("declare_builtin: malloc/realloc failed (size = %d).\n",
s_builtins);
termination_init_error ();
}
}
{
unsigned int low = 0;
unsigned int high = n_builtins;
while (1)
{
if (low < high)
{
unsigned int middle = ((low + high) / 2);
if (builtin < (builtins[middle]))
high = middle;
else if (builtin > (builtins[middle]))
low = (middle + 1);
else
{
(builtin_names[middle]) = name;
return;
}
}
else
{
unsigned int scan = (n_builtins++);
while (low < scan)
{
(builtins [scan]) = (builtins [scan - 1]);
(builtin_names [scan]) = (builtin_names [scan - 1]);
scan -= 1;
}
(builtins [low]) = builtin;
(builtin_names [low]) = name;
return;
}
}
}
}
const char *
builtin_index_to_name (unsigned int index)
{
return ((index < n_builtins) ? (builtin_names[index]) : 0);
}
int
pc_to_builtin_index (unsigned long pc)
{
if (! ((builtins != 0)
&& (n_builtins > 0)
&& (pc >= (builtins[0]))
&& (pc < (builtins [(n_builtins - 1)]))))
return (-1);
{
unsigned int low = 0;
unsigned int high = (n_builtins - 1);
while ((low + 1) < high)
{
unsigned int middle = ((low + high) / 2);
if (pc < (builtins[middle]))
high = middle;
else if (pc > (builtins[middle]))
low = middle;
else
return (middle);
}
return ((pc == (builtins[high])) ? high : low);
}
}
#ifdef __WIN32__
#include "ntscmlib.h"
extern unsigned long * win32_catatonia_block;
#ifndef REGBLOCK_LENGTH
# define REGBLOCK_LENGTH REGBLOCK_MINIMUM_LENGTH
#endif
typedef struct register_storage
{
/* The following must be allocated consecutively */
unsigned long catatonia_block [3];
void * Regstart [32]; /* Negative byte offsets from &Registers[0] */
SCHEME_OBJECT Registers [REGBLOCK_LENGTH];
} REGMEM;
SCHEME_OBJECT * RegistersPtr = 0;
unsigned long * win32_catatonia_block = 0;
static REGMEM regmem;
void
win32_allocate_registers (void)
{
win32_catatonia_block = (regmem.catatonia_block);
Registers = (regmem.Registers);
if (!win32_system_utilities.lock_memory_area ((®mem), (sizeof (regmem))))
outf_error_line ("Unable to lock registers");
}
void
win32_deallocate_registers (void)
{
win32_system_utilities.unlock_memory_area ((®mem), (sizeof (regmem)));
}
#endif /* __WIN32__ */
|