1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344
|
% Copyright 2005-2018 Cisco Systems, Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
\chapter{System Operations\label{CHPTSYSTEM}}
This chapter describes operations for
handling exceptions, interrupts, environments,
compilation and evaluation, profiling,
controlling the operation of the system,
timing and statistics,
defining and setting parameters,
and
querying the operating system environment.
\schemeinit
(load "docond.ss")
\endschemeinit
\section{Exceptions\label{SECTSYSTEMEXCEPTIONS}}
\index{exception handling}{\ChezScheme} provides some extensions to the
Revised$^6$ Report exception-handling mechanism, including mechanisms
for producing formatted error messages, displaying conditions,
and redefining the base exception handler.
These extensions are described in this section.
%----------------------------------------------------------------------------
\entryheader
\formdef{warning}{\categoryprocedure}{(warning \var{who} \var{msg} \var{irritant} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\scheme{warning} raises a continuable exception with condition type
\scheme{&warning} and should be used to describe situations for which the
\scheme{&warning} condition type is appropriate, typically a situation
that should not prevent the program from continuing but might result
in a more serious problem at some later point.
The continuation object with which the exception is raised also includes
a \scheme{&who} condition whose who field is \var{who} if \var{who} is
not \scheme{#f}, a \scheme{&message} condition whose message field is
\var{msg}, and an \scheme{&irritants} condition whose irritants field
is \scheme{(\var{irritant} \dots)}.
\var{who} must be a string, a symbol, or \scheme{#f} identifying the procedure
or syntactic form reporting the warning upon whose behalf the warning is being
reported.
It is usually best to identify a procedure the programmer has called rather
than some other procedure the programmer may not be aware is involved in
carrying out the operation.
\var{msg} must be a string and should describe the exceptional situation.
The irritants may be any Scheme objects and should include values that may
have caused or been materially involved in the exceptional situation.
%----------------------------------------------------------------------------
\entryheader
\formdef{assertion-violationf}{\categoryprocedure}{(assertion-violationf \var{who} \var{msg} \var{irritant} \dots)}
\returns does not return
\formdef{errorf}{\categoryprocedure}{(errorf \var{who} \var{msg} \var{irritant} \dots)}
\returns does not return
\formdef{warningf}{\categoryprocedure}{(warningf \var{who} \var{msg} \var{irritant} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\index{formatted error messages}%
These procedures are like \scheme{assertion-violation}, \scheme{error},
and \scheme{warning} except
that \var{msg} is assumed to be a format string, as if in a call to
\scheme{format} (Section~\ref{SECTFORMAT}), with
\scheme{\var{irritant} \dots} treated as the additional arguments to
\scheme{format}.
This allows programs to control the appearance of the error message, at
least when the default exception handler is in place.
For each of these procedures, the continuation object with which the exception
is raised includes a \scheme{&format} condition to signify that the string
contained in the condition object's \scheme{&message} condition is a
\scheme{format} string and the objects contained in the condition object's
\scheme{&irritants} condition should be treated as the additional
\scheme{format} arguments.
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &format &condition
make-format-condition format-condition?)}
\endentryheader
\noindent
Presence of this condition type within a compound condition indicates
that the string provided by the \scheme{&message} condition, if
present, is a \scheme{format} string and the list of objects provided by
the \scheme{&irritants} condition, if present, should be treated as
additional \scheme{format} arguments.
\showit
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &source &condition
make-source-condition source-condition?
(form source-condition-form))}
\endentryheader
\noindent
This condition type can be included within a compound condition when a
source expression can be identified in situations in which a
\scheme{&syntax} condition would be inappropriate, such as when a
run-time assertion violation is detected.
The \scheme{form} argument should be an s-expression or syntax object
representing the source expression.
\showit
%----------------------------------------------------------------------------
\entryheader
\conditionformdef{(define-condition-type &continuation &condition
make-continuation-condition continuation-condition?
(continuation condition-continuation))}
\endentryheader
\noindent
This condition type can be included within a compound condition to indicate
the current continuation at the point where the exception described by the
condition occurred.
The continuation of a failed \scheme{assert} or a call to
\scheme{assertion-violation}, \scheme{assertion-violationf},
\scheme{error}, \scheme{errorf}, or \scheme{syntax-error} is now included
via this condition type in the conditions passed to \scheme{raise}.
The \scheme{continuation} argument should be a continuation.
\showit
%----------------------------------------------------------------------------
\entryheader
\formdef{display-condition}{\categoryprocedure}{(display-condition \var{obj})}
\formdef{display-condition}{\categoryprocedure}{(display-condition \var{obj} \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
If \var{textual-output-port} is not supplied, it defaults to the current output port.
This procedure displays a message to the effect that an exception
has occurred with value \var{obj}.
If \var{obj} is a condition (Chapter~\ref{TSPL:CHPTEXCEPTIONS} of
{\TSPLFOUR}), it displays information encapsulated within the condition,
handling messages, \var{who} conditions, irritants, source information,
etc., as appropriate.
%----------------------------------------------------------------------------
\entryheader
\formdef{default-exception-handler}{\categoryprocedure}{(default-exception-handler \var{obj})}
\returns unspecified
\listlibraries
\endentryheader
This procedure is the default value of the \scheme{base-exception-handler}
parameter called on a condition when no other exception handler has been
defined or when all dynamically established exception handlers have chosen
not to handle the condition.
It first displays \var{obj}, as if with \scheme{display-condition}, to the
console error port.
For non-serious warning conditions, it returns immediately after displaying
the condition.
For serious or other non-warning conditions, it
saves the condition in the parameter \scheme{debug-condition}, where
\scheme{debug} (Section~\ref{SECTDEBUGINTERACTIVE}) can retrieve it and
allow it to be inspected.
If the \scheme{debug-on-exception} parameter is set to \scheme{#f} (the
default unless the \index{\scheme{--debug-on-exception} command-line
option}\scheme{--debug-on-exception} command-line option is provided), the
handler prints a message instructing the user to type \scheme{(debug)} to
enter the debugger, then resets to the current caf\'e.
Otherwise, the handler invokes \scheme{debug} directly and resets if
\scheme{debug} returns.
If an I/O exception occurs while attempting to display the condition,
the default exception handler resets (as if by calling \scheme{reset}).
The intent is to avoid an infinite regression (ultimately ending
in exhaustion of memory) in which the process repeatedly recurs
back to the default exception handler trying to write to a console-error
port (typically stderr) that is no longer writable, e.g., due to
the other end of a pipe or socket having been closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{debug-on-exception}{\categoryglobalparameter}{debug-on-exception}
\listlibraries
\endentryheader
The value of this parameter determines whether the default exception handler
immediately enters the debugger immediately when it receives a serious or
non-warning condition.
If the \index{\scheme{--debug-on-exception} command-line option}\scheme{--debug-on-exception}
command-line option (Section~\ref{SECTUSEINTERACTION}) has been provided, the
initial value of this parameter is \scheme{#t}.
Otherwise, the initial value is \scheme{#f}.
%----------------------------------------------------------------------------
\entryheader
\formdef{base-exception-handler}{\categorythreadparameter}{base-exception-handler}
\listlibraries
\endentryheader
The value of this parameter must be a procedure, and the procedure
should accept one argument.
The default value of \scheme{base-exception-handler} is
the procedure \scheme{default-exception-handler}.
The value of this parameter is invoked whenever no exception handler
established by a program has chosen to handle an exception.
%----------------------------------------------------------------------------
\entryheader
\formdef{debug-condition}{\categorythreadparameter}{debug-condition}
\listlibraries
\endentryheader
This parameter is used by the default exception handler to hold the
last serious or non-warning condition received by the handler, where
it can be inspected via the \scheme{debug} procedure
(Section~\ref{SECTDEBUGINTERACTIVE}).
It can also be invoked by user code to store or retrieve a
condition.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-exception-state}{\categorythreadparameter}{current-exception-state}
\listlibraries
\endentryheader
\scheme{current-exception-state} may be used to get or set
the current exception state.
When called without arguments, \scheme{current-exception-state} returns
an \emph{exception state} comprising the current stack of handlers established
by \scheme{with-exception-handler} and \scheme{guard}.
When called with a single argument, which must be an exception state,
\scheme{current-exception-state} sets the exception state.
%----------------------------------------------------------------------------
\entryheader
\formdef{create-exception-state}{\categoryprocedure}{(create-exception-state)}
\formdef{create-exception-state}{\categoryprocedure}{(create-exception-state \var{procedure})}
\listlibraries
\endentryheader
\scheme{create-exception-state} creates an exception
state whose stack of exception handlers is empty except for, in effect,
an infinite number of occurrences of \emph{handler} at its
base.
\var{handler} must be a procedure, and should accept one argument.
If not provided, \var{handler} defaults to a procedure equivalent
to the value of the following expression.
\schemedisplay
(lambda (x) ((base-exception-handler) x))
\endschemedisplay
\section{Interrupts\label{SECTSYSTEMINTERRUPTS}}
\index{interrupts}{\ChezScheme} allows programs to control
the action of the Scheme system when various events
occur, including an interrupt from the
keyboard, the expiration of an internal timer set by \scheme{set-timer},
a breakpoint caused by a call to \scheme{break}, or a request from the
storage manager to initiate a garbage collection.
These mechanisms are described in this section, except for the
collect request mechanism, which is described in Section~\ref{SECTSMGMTGC}.
Timer, keyboard, and collect-request interrupts are supported via a counter
that is decremented approximately once for each call to a nonleaf procedure.
(A leaf procedure is one that does not itself make any calls.)
When no timer is running, this counter is set to a default value (1000
in Version~9) when a program starts or after an interrupt occurs.
If a timer is set (via \scheme{set-timer}), the counter is set to the
minimum of the default value and the number of ticks to which the timer is
set.
When the counter reaches zero, the system looks to see if the timer
is set and has expired or if a keyboard or collect request interrupt
has occurred.
If so, the current procedure call is pended (``put on hold'') while the
appropriate interrupt handler is invoked to handle the interrupt.
When (if) the interrupt handler returns, the pended call takes place.
Thus, timer, keyboard, and collect-request interrupts effectively occur
synchronously with respect to the procedure call mechanism, and
keyboard and collect request interrupts may be delayed by a number
of calls equal to the default timer value.
Calls to the break handler occur immediately
whenever \scheme{break} is called.
%----------------------------------------------------------------------------
\entryheader
\formdef{break}{\categoryprocedure}{(break \var{who} \var{msg} \var{irritant} \dots)}
\formdef{break}{\categoryprocedure}{(break \var{who})}
\formdef{break}{\categoryprocedure}{(break)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
The arguments to \scheme{break} follow the protocol described above for
\scheme{errorf}.
The default break handler (see \scheme{break-handler}) displays a message and
invokes the \index{debugger}debugger.
The format string and objects may be omitted, in which case the
message issued by the default break handler identifies the break
using the \var{who} argument but provides no more information
about the break.
If the \var{who} argument is omitted as well, no message is generated.
The default break handler returns normally if the debugger
exits normally.
%----------------------------------------------------------------------------
\entryheader
\formdef{break-handler}{\categorythreadparameter}{break-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure.
The current break handler is called by \scheme{break}, which passes
along its arguments.
See \scheme{break} for a description of the default break
handler.
The example below shows how to disable breaks.
\schemedisplay
(break-handler (lambda args (void)))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{keyboard-interrupt-handler}{\categorythreadparameter}{keyboard-interrupt-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure.
The keyboard-interrupt handler is called (with no arguments) when
a keyboard interrupt occurs.
The default keyboard-interrupt handler invokes the interactive
\index{debugger}debugger.
If the debugger exits normally the interrupted computation is
resumed.
The example below shows how to install a keyboard-interrupt handler
that resets without invoking the debugger.
\schemedisplay
(keyboard-interrupt-handler
(lambda ()
(newline (console-output-port))
(reset)))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader\label{desc:set-timer}
\formdef{set-timer}{\categoryprocedure}{(set-timer \var{n})}
\returns previous current timer value
\listlibraries
\endentryheader
\noindent
\index{timer interrupts}\var{n} must be a nonnegative integer.
When \var{n} is nonzero, \scheme{set-timer} starts an internal timer with
an initial value of \var{n}.
When \var{n} ticks elapse, a timer interrupt occurs, resulting in
invocation of the timer interrupt handler.
Each tick corresponds roughly to one nonleaf procedure call (see the
introduction to this section); thus, ticks are not
uniform time units but instead depend heavily on how much work is done
by each procedure call.
When \var{n} is zero, \scheme{set-timer} turns the timer off.
The value returned in either case is the value of the timer before the
call to \scheme{set-timer}.
A return value of 0 should not be taken to imply that the timer was not on;
the return value may also be 0 if the timer was just about to fire when
the call to \scheme{set-timer} occurred.
The engine mechanism (Section~\ref{SECTENGINES}) is built on top of the
timer interrupt so timer interrupts should not be used with engines.
%----------------------------------------------------------------------------
\entryheader
\formdef{timer-interrupt-handler}{\categorythreadparameter}{timer-interrupt-handler}
\listlibraries
\endentryheader
\noindent
\index{timer interrupts}The value of this parameter must be a procedure.
The timer interrupt handler is called by the system when the internal timer
(set by \scheme{set-timer}) expires.
The default handler raises an exception with condition type \scheme{&assertion}
to say that the handler has not
been defined; any program that uses the timer should redefine the
handler before setting the timer.
%----------------------------------------------------------------------------
\entryheader
\formdef{disable-interrupts}{\categoryprocedure}{(disable-interrupts)}
\formdef{enable-interrupts}{\categoryprocedure}{(enable-interrupts)}
\returns disable count
\listlibraries
\endentryheader
\noindent
\scheme{disable-interrupts} disables the handling of interrupts,
including timer, keyboard, and collect request interrupts.
\scheme{enable-interrupts} re-enables these interrupts.
The system maintains a disable count that starts at zero; when zero,
interrupts are enabled.
Each call to \scheme{disable-interrupts} increments the count,
effectively disabling interrupts.
Each call to \scheme{enable-interrupts} decrements the count, if
not already zero, effectively enabling interrupts.
For example, two calls to \scheme{disable-interrupts} followed by one call to
\scheme{enable-interrupts} leaves interrupts disabled.
Calls to \scheme{enable-interrupts} when the count is already zero
(and interrupts are enabled) have no effect.
The value returned by either procedure is the number of calls to
\scheme{enable-interrupts} required to enable interrupts.
Great care should be exercised when using these procedures, since disabling
interrupts inhibits the normal processing of keyboard interrupts,
timer interrupts, and, perhaps most importantly, collect request interrupts.
Since garbage collection does not happen automatically when interrupts are
disabled, it is possible for the storage allocator to run out of space
unnecessarily should interrupts be disabled for a long period of time.
The \scheme{with-interrupts-disabled} syntactic form should be used instead of
these more primitive procedures whenever possible,
since \scheme{with-interrupts-disabled} ensures that interrupts are re-enabled
whenever a nonlocal exit occurs, such as when an exception is handled by
the default exception handler.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-interrupts-disabled}{\categorysyntax}{(with-interrupts-disabled \var{body_1} \var{body_2} \dots)}
\formdef{critical-section}{\categorysyntax}{(critical-section \var{body_1} \var{body_2} \dots)}
\returns the values of the body \scheme{\var{body_1} \var{body_2} \dots}
\listlibraries
\endentryheader
\noindent
\scheme{with-interrupts-disabled} evaluates the body
\scheme{\var{body_1} \var{body_2} \dots} with interrupts disabled.
That is, upon entry, interrupts are disabled, and
upon exit, interrupts are re-enabled.
Thus, \scheme{with-interrupts-disabled} allows the implementation of indivisible
operations in nonthreaded versions of {\ChezScheme} or within a single thread
in threaded versions of {\ChezScheme}.
\scheme{critical-section} is the same as \scheme{with-interrupts-disabled} and
is provided for backward compatibility.
\scheme{with-interrupts-disabled} can be defined as follows.
\schemedisplay
(define-syntax with-interrupts-disabled
(syntax-rules ()
[(_ b1 b2 ...)
(dynamic-wind
disable-interrupts
(lambda () b1 b2 ...)
enable-interrupts)]))
\endschemedisplay
\noindent
The use of \scheme{dynamic-wind} ensures that interrupts are
disabled whenever the body of the \scheme{with-interrupts-disabled} expression
is active and re-enabled whenever it is not.
Since calls to \scheme{disable-interrupts} are counted (see the
discussion under \scheme{disable-interrupts} and
\scheme{enable-interrupts} above), \scheme{with-interrupts-disabled}
expressions may be nested with the desired effect.
%----------------------------------------------------------------------------
\entryheader
\formdef{register-signal-handler}{\categoryprocedure}{(register-signal-handler \var{sig} \var{procedure})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{register-signal-handler} is used to
establish a signal handler for a given low-level signal.
\var{sig} must be an exact integer identifying a valid signal, and
\var{procedure} should accept one argument.
See your host system's \scheme{<signal.h>} or documentation for a list
of valid signals and their numbers.
After a signal handler for a given signal has been registered, receipt
of the specified signal results in a call to the handler.
The handler is passed the signal number, allowing the same handler to
be used for different signals while differentiating among them.
Signals handled in this fashion are treated like keyboard interrupts in
that the handler is not called immediately when the signal is delivered
to the process, but rather at some procedure call boundary after the
signal is delivered.
It is generally not a good idea, therefore, to establish handlers for
memory faults, illegal instructions, and the like, since the code that
causes the fault or illegal instruction will continue to execute
(presumably erroneously) for some time before the handler is invoked.
A finite amount of storage is used to buffer as-yet unhandled
signals, after which additional signals are dropped.
\scheme{register-signal-handler} is supported only on Unix-based
systems.
\section{Environments\label{SECTMISCENVIRONMENTS}}
Environments are first-class objects containing identifier bindings.
They are similar to modules but, unlike modules, may be manipulated
at run time.
Environments may be provided as optional arguments to \scheme{eval},
\scheme{expand}, and the procedures that define, assign, or
reference top-level values.
There are several built-in environments, and new environments can
be created by copying existing environments or selected bindings
from existing environments.
Environments can be mutable or immutable.
A mutable environment can be extended with new bindings, its
existing bindings can be modified, and its variables can be assigned.
An immutable environment cannot be modified in any of these ways.
%----------------------------------------------------------------------------
\entryheader
\formdef{environment?}{\categoryprocedure}{(environment? \var{obj})}
\returns \scheme{#t} if \var{obj} is an environment, otherwise \scheme{#f}
\listlibraries
\endnoskipentryheader
\schemedisplay
(environment? (interaction-environment)) ;=> #t
(environment? 'interaction-environment) ;=> #f
(environment? (copy-environment (scheme-environment))) ;=> #t
(environment? (environment '(prefix (rnrs) $rnrs-))) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{environment-mutable?}{\categoryprocedure}{(environment-mutable? \var{env})}
\returns \scheme{#t} if \var{env} is mutable, otherwise \scheme{#f}
\listlibraries
\endnoskipentryheader
\schemedisplay
(environment-mutable? (interaction-environment)) ;=> #t
(environment-mutable? (scheme-environment)) ;=> #f
(environment-mutable? (copy-environment (scheme-environment))) ;=> #t
(environment-mutable? (environment '(prefix (rnrs) $rnrs-))) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-environment}{\categoryprocedure}{(scheme-environment)}
\returns an environment
\listlibraries
\endentryheader
\noindent
\scheme{scheme-environment} returns an environment containing
the initial top-level bindings.
This environment corresponds to the \scheme{scheme} module.
The environment returned by this procedure is immutable.
\schemedisplay
(define cons 3)
(top-level-value 'cons (scheme-environment)) ;=> #<procedure cons>
(set-top-level-value! 'cons 3 (scheme-environment)) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{ieee-environment}{\categoryprocedure}{(ieee-environment)}
\returns an IEEE/ANSI standard compatibility environment
\listlibraries
\endentryheader
\noindent
\scheme{ieee-environment} returns an environment containing
bindings for the keywords and variables whose meanings are
defined by the IEEE/ANSI Standard for Scheme~\cite{IEEE:1178}.
The bindings for each of the identifiers in the IEEE environment are those
of the corresponding Revised$^6$ Report library, so this does not provide
full backward compatibility.
The environment returned by this procedure is immutable.
\schemedisplay
(define cons 3)
(top-level-value 'cons (ieee-environment)) ;=> #<procedure cons>
(set-top-level-value! 'cons 3 (ieee-environment)) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{interaction-environment}{\categorythreadparameter}{interaction-environment}
\listlibraries
\endentryheader
\noindent
The original value of \scheme{interaction-environment} is the default
top-level environment.
It is initially set to a mutable copy of
\scheme{(scheme-environment)} and which may be extended or otherwise
altered by top-level definitions and assignments.
It may be set to any environment, mutable or not, to change the
default top-level evaluation environment.
An expression's top-level bindings resolve to the environment that is
in effect when the expression is expanded, and changing the value
of this parameter has no effect on running code.
Changes affect only code that is subsequently expanded, e.g., as the
result of a call to \scheme{eval}, \scheme{load}, or
\scheme{compile-file}.
\schemedisplay
(define cons 3)
cons ;=> 3
(top-level-value 'cons (interaction-environment)) ;=> 3
(interaction-environment (scheme-environment))
cons ;=> #<procedure cons>
(set! cons 3) ;=> \var{exception: attempt to assign immutable variable}
(define cons 3) ;=> \var{exception: invalid definition in immutable environment}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env})}
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env} \var{mutable?})}
\formdef{copy-environment}{\categoryprocedure}{(copy-environment \var{env} \var{mutable?} \var{syms})}
\returns a new environment
\listlibraries
\endentryheader
\scheme{copy-environment} returns a copy of \var{env}, i.e., a new
environment that contains the same bindings as \var{env}.
The environment is mutable if \var{mutable?} is omitted or true;
if \var{mutable?} is false, the environment is immutable.
The set of bindings copied from \var{env} to the new environment
is determined by \var{syms}, which defaults to the value of
\scheme{(environment-symbols \var{env})}.
The binding, if any, for each element of \var{syms} is copied to the
new environment, and no other bindings are present in the new
environment.
In the current implementation, the storage space used by an environment
is never collected, so repeated use of \scheme{copy-environment} will
eventually cause the system to run out of memory.
\schemedisplay
(define e (copy-environment (scheme-environment)))
(eval '(define cons +) e)
(eval '(cons 3 4) e) ;=> 7
(eval '(cons 3 4) (scheme-environment)) ;=> (3 . 4)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{environment-symbols}{\categoryprocedure}{(environment-symbols \var{env})}
\returns a list of symbols
\listlibraries
\endentryheader
This procedure returns a list of symbols representing the identifiers
bound in environment \var{env}.
It is primarily useful in building the list of symbols to be copied
from one environment to another.
\schemedisplay
(define listless-environment
(copy-environment
(scheme-environment)
#t
(remq 'list (environment-symbols (scheme-environment)))))
(eval '(let ([x (cons 3 4)]) x) listless-environment) ;=> (3 . 4)
(eval '(list 3 4) listless-environment) ;=> \var{exception}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{apropos-list}{\categoryprocedure}{(apropos-list \var{s})}
\formdef{apropos-list}{\categoryprocedure}{(apropos-list \var{s} \var{env})}
\returns see below
\listlibraries
\endentryheader
This procedure returns a selected list of symbols and pairs.
Each symbol in the list represents an identifier bound in \var{env}.
Each pair represents a set of identifiers exported by a
predefined library or a library previously defined or loaded
into the system.
The car of the pair is the library name, and the cdr is a list
of symbols.
If \var{s} is a string, only entries whose names have \var{s} as a
substring are included, and if \var{s} is a symbol, only those whose names
have the name of \var{s} as a substring are selected.
If no environment is provided, it defaults to the value of
\scheme{interaction-environment}.
\schemedisplay
(library (a) (export a-vector-sortof) (import (rnrs))
(define a-vector-sortof '(vector 1 2 3)))
(apropos-list 'vector-sort) ;=>
(vector-sort vector-sort!
((a) a-vector-sortof)
((chezscheme) vector-sort vector-sort!)
((rnrs) vector-sort vector-sort!)
((rnrs sorting) vector-sort vector-sort!)
((scheme) vector-sort vector-sort!))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{apropos}{\categoryprocedure}{(apropos \var{s})}
\formdef{apropos}{\categoryprocedure}{(apropos \var{s} \var{env})}
\returns unspecified
\listlibraries
\endentryheader
\scheme{apropos} is like \scheme{apropos-list} except the information is
displayed to the current output port, as shown in the following
transcript.
\schemedisplay
> (library (a) (export a-vector-sortof) (import (rnrs))
(define a-vector-sortof '(vector 1 2 3)))
> (apropos 'vector-sort)
interaction environment:
vector-sort, vector-sort!
(a):
a-vector-sortof
(chezscheme):
vector-sort, vector-sort!
(rnrs):
vector-sort, vector-sort!
(rnrs sorting):
vector-sort, vector-sort!
(scheme):
vector-sort, vector-sort!
\endschemedisplay
\section{Compilation, Evaluation, and Loading\label{SECTMISCCOMPILEEVAL}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{eval}{\categoryprocedure}{(eval \var{obj})}
\formdef{eval}{\categoryprocedure}{(eval \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{eval} treats \var{obj} as the representation of an expression.
It evaluates the expression in environment \var{env} and returns
its value.
If no environment is provided, it defaults to the environment
returned by \scheme{interaction-environment}.
Single-argument \scheme{eval} is a {\ChezScheme} extension.
{\ChezScheme} also permits \var{obj} to be the representation of a
nonexpression form, i.e., a definition, whenever the environment
is mutable.
{\ChezScheme} further allows \var{obj} to be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}), and the default evaluators
make use of annotations to incorporate source-file
information in error messages and associate source-file
information with compiled code.
In {\ChezScheme}, \scheme{eval} is actually a wrapper that simply
passes its arguments to the current evaluator.
(See \scheme{current-eval}.)
The default evaluator is \scheme{compile}, which expands the
expression via the current expander (see
\scheme{current-expand}), compiles it,
executes the resulting code, and returns its value.
If the environment argument, \var{env}, is present,
\scheme{compile} passes it along to the current expander,
which is \scheme{sc-expand} by default.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-eval}{\categorythreadparameter}{current-eval}
\listlibraries
\endentryheader
\noindent
\scheme{current-eval} determines the evaluation procedure used by the
procedures \index{\scheme{eval}}\scheme{eval}, \scheme{load}, and
\scheme{new-cafe}.
\scheme{current-eval} is initially bound to the value of
\index{\scheme{compile}}\scheme{compile}.
(In {\PetiteChezScheme}, it is initially bound to the value of
\index{\scheme{interpret}}\scheme{interpret}.)
The evaluation procedureshould expect one or two arguments: an object
to evaluate and an optional environment.
The second argument might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}).
\schemedisplay
(current-eval interpret)
(+ 1 1) ;=> 2
(current-eval (lambda (x . ignore) x))
(+ 1 1) ;=> (+ 1 1)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{compile}{\categoryprocedure}{(compile \var{obj})}
\formdef{compile}{\categoryprocedure}{(compile \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endentryheader
\noindent
\var{obj}, which can be an annotation (Section~\ref{SECTSYNTAXANNOTATIONS})
or unannotated value, is treated as a Scheme expression, expanded with the
current expander (the value of \scheme{current-expand}) in the specified
environment (or the interaction environment, if no environment
is provided), compiled to machine code, and executed.
\scheme{compile} is the default value of the \scheme{current-eval}
parameter.
%----------------------------------------------------------------------------
\entryheader
\formdef{interpret}{\categoryprocedure}{(interpret \var{obj})}
\formdef{interpret}{\categoryprocedure}{(interpret \var{obj} \var{env})}
\returns value of the Scheme form represented by \var{obj}
\listlibraries
\endentryheader
\noindent
\scheme{interpret} is like \scheme{compile}, except that the expression
is interpreted rather than compiled.
\scheme{interpret} may be used as a replacement for \scheme{compile},
with the following caveats:
\begin{itemize}
\item
Interpreted code runs significantly slower.
\item
Inspector information is not generated for
interpreted code, so the inspector is not as useful for interpreted
code as it is for compiled code.
\item
Foreign procedure expressions cannot be
interpreted, so the interpreter invokes the compiler for all
foreign procedure expressions (this is done transparently).
\end{itemize}
\noindent
\scheme{interpret} is sometimes faster than \scheme{compile} when the
form to be evaluated is short running, since it avoids some of the
work done by \scheme{compile} prior to evaluation.
%----------------------------------------------------------------------------
\entryheader
\formdef{load}{\categoryprocedure}{(load \var{path})}
\formdef{load}{\categoryprocedure}{(load \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{load} reads and evaluates the contents of the file specified by
\var{path}.
The file may contain source or object code.
By default, \scheme{load} employs \scheme{eval} to evaluate each source
expression found in a source file.
If \var{eval-proc} is specified, \scheme{load} uses this procedure instead.
\var{eval-proc} must accept one argument, the expression to evaluate.
The expression passed to \var{eval-proc} might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}) or an unannotated value.
The \var{eval-proc} argument
facilitates the implementation of embedded Scheme-like languages
and the use of alternate
evaluation mechanisms to be used for Scheme programs.
\var{eval-proc} can be put to other uses as well.
For example,
\schemedisplay
(load "myfile.ss"
(lambda (x)
(pretty-print
(if (annotation? x)
(annotation-stripped x)
x))
(newline)
(eval x)))
\endschemedisplay
\noindent
pretty-prints each expression before evaluating it.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{load-library}{\categoryprocedure}{(load-library \var{path})}
\formdef{load-library}{\categoryprocedure}{(load-library \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\scheme{load-library} is identical to \scheme{load} except
that it treats the input file as if it were prefixed by an implicit
\scheme{#!r6rs}.
This effectively disables any non-R6RS lexical
syntax except where subsequently overridden by \scheme{#!chezscheme}.
%----------------------------------------------------------------------------
\entryheader
\formdef{load-program}{\categoryprocedure}{(load-program \var{path})}
\formdef{load-program}{\categoryprocedure}{(load-program \var{path} \var{eval-proc})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{load-program} reads and evaluates the contents of the file specified by
\var{path}.
The file may contain source or object code.
If it contains source code, \scheme{load-program} wraps
the code in a \scheme{top-level-program} form so that the file's
content is treated as an RNRS top-level program
(Section~\ref{TSPL:SECTLIBPROGRAMS} of {\TSPLFOUR}).
By default, \scheme{load-program} employs \scheme{eval} to evaluate each source
expression found in the file.
If \var{eval-proc} is specified, \scheme{load-program} uses this procedure instead.
\var{eval-proc} must accept one argument, the expression to evaluate.
The expression passed to \var{eval-proc} might be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}) or an unannotated value.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{verify-loadability}{\categoryprocedure}{(verify-loadability \var{situation} \var{input} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{situation} must be one of the symbols \scheme{visit}, \scheme{revisit}, or \scheme{load}.
Each \var{input} must be a string pathname or a pair of a string pathname and a library search path.
Each of the pathnames should name a file containing object code for a set of libraries and
top-level programs, such as would be produced by
\index{\scheme{compile-program}}\scheme{compile-program},
\index{\scheme{compile-library}}\scheme{compile-library},
\index{\scheme{compile-whole-program}}\scheme{compile-whole-program},
or
\index{\scheme{compile-whole-library}}\scheme{compile-whole-library}.
A library search path must be a suitable argument for
\index{\scheme{library-directories}}\scheme{library-directories}.
\scheme{verify-loadability} verifies, without actually loading any
code or definining any libraries, whether the object files named
by the specified pathnames and their library dependencies, direct
or indirect, are present, readable, and mutually compatible.
The type of dependencies for each named object file is determined
by the \var{situation} argument: compile-time dependencies for
\var{visit}, run-time dependencies for \var{revisit} and both for
\var{load}.
For each input pathname that is paired with a search path,
the \scheme{library-directories} parameter is parameterized to the
library search path during the recursive search for dependencies
of the programs and libraries found in the object file named by the
pathname.
If \scheme{verify-loadabilty} finds a problem, such as a missing
library dependency or compilation-instance mismatch, it raises an
exception with an appropriate condition.
Otherwise, it returns an unspecified value.
Since \scheme{verify-loadability} does not load or run any code
from the files it processes, it cannot determine whether errors
unrelated to missing or unreadable files or mutual compatibility
will occur when the files are actually loaded.
%----------------------------------------------------------------------------
\entryheader
\formdef{load-compiled-from-port}{\categoryprocedure}{(load-compiled-from-port \var{input-port})}
\returns result of the last compiled expression
\listlibraries
\endentryheader
\noindent
\scheme{load-compiled-from-port} reads and evaluates the object-code contents
of \var{input-port} as previously created by functions like \scheme{compile-file},
\scheme{compile-script}, \scheme{compile-library}, and
\scheme{compile-to-port}.
The return value is the value of the last expression whose compiled
form is in \var{input-port}. If \var{input-port} is empty, then the
result value is unspecified.
The port is left at end-of-file but is not closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{visit-compiled-from-port}{\categoryprocedure}{(visit-compiled-from-port \var{input-port})}
\returns result of the last compiled expression processed
\listlibraries
\endentryheader
\noindent
\scheme{visit-compiled-from-port} reads and evaluates the object-code contents
of \var{input-port} as previously created by functions like \scheme{compile-file},
\scheme{compile-script}, \scheme{compile-library}, and
\scheme{compile-to-port}. In the process, it skips any revisit (run-time-only) code.
The return value is the value of the last expression whose last non-revisit compiled
form is in \var{input-port}. If there are no such forms, then the
result value is unspecified.
The port is left at end-of-file but is not closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{revisit-compiled-from-port}{\categoryprocedure}{(revisit-compiled-from-port \var{input-port})}
\returns result of the last compiled expression processed
\listlibraries
\endentryheader
\noindent
\scheme{revisit-compiled-from-port} reads and evaluates the object-code contents
of \var{input-port} as previously created by functions like \scheme{compile-file},
\scheme{compile-script}, \scheme{compile-library}, and
\scheme{compile-to-port}. In the process, it skips any visit (compile-time-only) code.
The return value is the value of the last expression whose last non-visit compiled
form is in \var{input-port}. If there are no such forms, then the
result value is unspecified.
The port is left at end-of-file but is not closed.
%----------------------------------------------------------------------------
\entryheader
\formdef{visit}{\categoryprocedure}{(visit \var{path})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{visit} reads the named file, which must contain compiled object
code compatible with the current machine type and version, and it
runs those portions of the compiled object code that
establish compile-time information or correspond to expressions
identified as ``visit'' time by \scheme{eval-when} forms contained in
the original source file.
For example, assume the file \scheme{t1.ss} contains the following
forms:
\schemedisplay
(define-syntax a (identifier-syntax 3))
(module m (x) (define x 4))
(define y 5)
\endschemedisplay
If \scheme{t1.ss} is compiled to \scheme{t1.so}, applying \scheme{load}
to \scheme{t1.so} has the effect of defining all three identifiers.
Applying \scheme{visit} to \scheme{t1.so}, however, has the effect of
installing the transformer for \scheme{a}, installing the interface for
\scheme{m} (for use by \scheme{import}), and recording \scheme{y} as
a variable.
\scheme{visit} is useful when separately compiling one file that depends
on bindings defined in another without actually loading and evaluating
the code in the supporting file.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{revisit}{\categoryprocedure}{(revisit \var{path})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{revisit} reads the named file, which must contain compiled object
code compatible with the current machine type and version, and it
runs those portions of the compiled object code that compute
run-time values or correspond to expressions identified as ``revisit'' time by
\scheme{eval-when} forms contained in the original source file.
Continuing the example given for \scheme{visit} above,
applying \scheme{revisit} to the object file, \scheme{t1.so}, has
the effect of establishing the values of the variable \scheme{x}
exported from \scheme{m} and the top-level variable \scheme{y},
without installing either the interface for \scheme{m} or
the transformer for \scheme{a}.
\scheme{revisit} is useful for loading compiled application code without
loading unnecessary compile-time information.
Care must be taken when using this feature if the application calls
\scheme{eval} or uses \scheme{top-level-value},
\scheme{set-top-level-value!}, or \scheme{top-level-syntax} to access
top-level bindings at run-time, since these procedures use compile-time
information to resolve top-level bindings.
\index{\scheme{source-directories}}%
The parameter \scheme{source-directories} (Section~\ref{SECTSYSTEMSOURCE})
determines the set of directories searched for source files not identified
by absolute path names.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-file}{\categoryprocedure}{(compile-file \var{input-filename})}
\formdef{compile-file}{\categoryprocedure}{(compile-file \var{input-filename} \var{output-filename})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{input-filename} and \var{output-filename} must be strings.
\var{input-filename} must name an existing, readable file.
It must contain a sequence of zero or more source expressions;
if this is not the case, \scheme{compile-file} raises an exception
with condition type \scheme{&syntax}.
The normal evaluation process proceeds in two steps: \index{compilation}compilation and
execution.
\scheme{compile-file} performs the compilation process for an entire source
file, producing an object file.
When the object file is subsequently loaded (see \index{\scheme{load}}\scheme{load}), the
compilation process is not necessary, and the file typically loads
several times faster.
If the optional \var{output-filename} argument is omitted, the
actual input and output filenames are determined as follows.
If \var{input-filename} has no extension, the input filename
is \var{input-filename} followed by \scheme{.ss} and the
output filename is \var{input-filename} followed by \scheme{.so}.
If \var{input-filename} has the extension \scheme{.so}, the
input filename is \var{input-filename} and the output filename
is \var{input-filename} followed by \scheme{.so}.
Otherwise, the input filename is \var{input-filename} and the
output filename is \var{input-filename} without its extension,
followed by \scheme{.so}.
For example, \scheme{(compile-file "myfile")} produces an object file
with the name \scheme{"myfile.so"} from the source file named
\scheme{"myfile.ss"}, \scheme{(compile-file "myfile.sls")} produces an
object file with the name \scheme{"myfile.so"} from the source file named
\scheme{"myfile.sls"}, and
\scheme{(compile-file "myfile1" "myfile2")} produces an object file with
the name \scheme{"myfile2"} from the source file name \scheme{"myfile1"}.
Before compiling a file, \scheme{compile-file} saves the values of the
following parameters:
\schemedisplay
optimize-level
debug-level
run-cp0
cp0-effort-limit
cp0-score-limit
cp0-outer-unroll-limit
generate-inspector-information
generate-procedure-source-information
compile-profile
generate-covin-files
generate-interrupt-trap
enable-cross-library-optimization
\endschemedisplay
It restores the values after the file has been compiled.
This allows the programmer to control the values of these parameters on
a per-file basis, e.g., via an \scheme{eval-when} with situation
\scheme{compile} embedded in the source file.
For example, if
\schemedisplay
(eval-when (compile) (optimize-level 3))
\endschemedisplay
appears at the top of a source file, the optimization level is set
to 3 just while the remainder of file is compiled.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-script}{\categoryprocedure}{(compile-script \var{input-filename})}
\formdef{compile-script}{\categoryprocedure}{(compile-script \var{input-filename} \var{output-filename})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{input-filename} and \var{output-filename} must be strings.
\scheme{compile-script} is like \scheme{compile-file} but differs in
that it copies the leading \scheme{#!} line from the
source-file script into the object file.
\scheme{compile-script} permits compiled script files to be created from
source script to reduce script load time.
As with source-code scripts, compiled scripts may be run with the
\index{\scheme{--script} command-line option}\scheme{--script}
command-line option.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-library}{\categoryprocedure}{(compile-library \var{input-filename})}
\formdef{compile-library}{\categoryprocedure}{(compile-library \var{input-filename} \var{output-filename})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{input-filename} and \var{output-filename} must be strings.
\scheme{compile-library} is identical to \scheme{compile-file} except
that it treats the input file as if it were prefixed by an implicit
\scheme{#!r6rs}.
This effectively disables any non-R6RS lexical
syntax except where subsequently overridden by \scheme{#!chezscheme}.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-program}{\categoryprocedure}{(compile-program \var{input-filename})}
\formdef{compile-program}{\categoryprocedure}{(compile-program \var{input-filename} \var{output-filename})}
\returns a list of libraries invoked by the program
\listlibraries
\endentryheader
\noindent
\var{input-filename} and \var{output-filename} must be strings.
\scheme{compile-program} is like \scheme{compile-script} but differs in
that it implements the semantics of RNRS top-level programs, while
\scheme{compile-script} implements the semantics of the interactive
top-level.
The resulting compiled program will also run faster than if compiled
via \scheme{compile-file} or \scheme{compile-script}.
\scheme{compile-program} returns a list of libraries directly
invoked by the compiled top-level program, excluding built-in
libraries like \scheme{(rnrs)} and \scheme{(chezscheme)}.
The procedure \scheme{library-requirements} may be used to determine
the indirect requirements, i.e., additional libraries required by
the directly invoked libraries.
When combined with \scheme{library-object-filename}, this information can
be used to determine the set of files that must be distributed with the
compiled program file.
A program invokes a library only if it references one or more variables
exported from the library.
The set of libraries invoked by a top-level program, and hence
loaded when the program is loaded, might be smaller than the set
imported by the program, and it might be larger than the set
directly imported by the program.
As with source-code top-level programs, compiled top-level programs may be
run with the
\index{\scheme{--program} command-line option}\scheme{--program}
command-line option.
%----------------------------------------------------------------------------
\entryheader
\formdef{maybe-compile-file}{\categoryprocedure}{(maybe-compile-file \var{input-filename})}
\formdef{maybe-compile-file}{\categoryprocedure}{(maybe-compile-file \var{input-filename} \var{output-filename})}
\formdef{maybe-compile-library}{\categoryprocedure}{(maybe-compile-library \var{input-filename})}
\formdef{maybe-compile-library}{\categoryprocedure}{(maybe-compile-library \var{input-filename} \var{output-filename})}
\formdef{maybe-compile-program}{\categoryprocedure}{(maybe-compile-program \var{input-filename})}
\formdef{maybe-compile-program}{\categoryprocedure}{(maybe-compile-program \var{input-filename} \var{output-filename})}
\returns see below
\listlibraries
\endentryheader
These procedures are like their non-\scheme{maybe} counterparts but
compile the source file only if the object file is out-of-date.
An object file \var{X} is considered out-of-date if it does not exist or
if it is older than the source file or any files included (via \scheme{include})
when \var{X} was created.
When the value of the parameter \scheme{compile-imported-libraries}
is \scheme{#t}, \var{X} is also considered out-of-date if the object
file for any library imported when \var{X} was compiled is out-of-date.
If \scheme{maybe-compile-file} determines that compilation is necessary,
it compiles the source file by passing \scheme{compile-file} the
input and output filenames.
\scheme{compile-library} does so by similarly invoking the value of the
\scheme{compile-library-handler} parameter, and
\scheme{compile-program} does so by similarly invoking the value of the
\scheme{compile-program-handler} parameter.
When \var{output-filename} is not specified, the input and output
filenames are determined in the same manner as for \scheme{compile-file}.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-library-handler}{\categorythreadparameter}{compile-library-handler}
\listlibraries
\endentryheader
This parameter must be set to a procedure, and the procedure should
accept two string arguments naming a source file and an object file.
The procedure should typically invoke \scheme{compile-library} and
pass it the two arguments, but it can also use one of the other
file or port compilation procedures.
For example, it might read the source file using its own parser and
use \index{\scheme{compile-to-file}}\scheme{compile-to-file} to finish
the compilation process.
The procedure can perform other actions as well, such as parameterizing
compilation parameters, establishing guards, or gathering statistics.
The default value of this parameter simply invokes
\scheme{compile-library} on the two string arguments without taking
any other action.
The value of this parameter is called by \scheme{maybe-compile-library}
when the object file is out-of-date.
It is also called by the expander to compile an
imported library when \scheme{compile-imported-libraries} is \scheme{#t}
and the expander determines the object file is out-of-date.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-program-handler}{\categorythreadparameter}{compile-program-handler}
\listlibraries
\endentryheader
This parameter must be set to a procedure, and the procedure should
accept two string arguments naming a source file and an object file.
The procedure should typically invoke \scheme{compile-program} and
pass it the two arguments, but it can also use one of the other
file or port compilation procedures.
For example, it might read the source file using its own parser and
use \index{\scheme{compile-to-file}}\scheme{compile-to-file} to finish
the compilation process.
The procedure can perform other actions as well, such as parameterizing
compilation parameters, establishing guards, or gathering statistics.
The default value of this parameter simply invokes
\scheme{compile-program} on the two string arguments without taking
any other action and returns the list of libraries returned by
\scheme{compile-program}.
The value of this parameter is called by \scheme{maybe-compile-program}
when the object file is out-of-date.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-whole-program}{\categoryprocedure}{(compile-whole-program \var{input-filename} \var{output-filename})}
\formdef{compile-whole-program}{\categoryprocedure}{(compile-whole-program \var{input-filename} \var{output-filename} \var{libs-visible?})}
\returns a list of libraries left to be loaded at run time
\listlibraries
\endentryheader
\scheme{compile-whole-program} accepts as input a filename naming
a ``whole program optimization'' (wpo) file for a top-level program
and produces an object file incorporating the program and each
library upon which it depends, provided that a wpo file for the
library can be found.
If a wpo file for a required library cannot be found, but an object
file for the library can, the library is not incorporated in the
resulting object file.
Such libraries are left to be loaded at run time.
\scheme{compile-whole-program} returns a list of such libraries.
If there are no such libraries, the resulting object file is
self-contained and \scheme{compile-whole-program} returns the empty
list.
The libraries incorporated into the resulting object file are visible (for
use by \scheme{environment} and \scheme{eval}) if the \var{libs-visible?}
argument is supplied and non-false.
Any library incorporated into the resulting object file and required by
an object file left to be loaded at run time is also visible, as are any
libraries the object file depends upon, regardless of the value of
\var{libs-visible?}.
\scheme{compile-whole-program} linearizes the initialization code for the
set of incorporated libraries in a way that respects static
dependencies among the libraries but not necessary dynamic dependencies
deriving from initialization-time uses of \scheme{environment}
or \scheme{eval}.
Additional static dependencies can be added in most cases to force
an ordering that allows the dynamic imports to succeed,
though not in general since a different order might be required each
time the program is run.
Adding a static dependency of one library on a second requires
adding an import of the second in the first as well as a run-time
reference to one of the variables exported by the second in the
body of the first.
\var{input-filename} and \var{output-filename} must be strings.
\var{input-filename} must identify a wpo file, and a wpo or object
file must also be present for each required library somewhere in
the directories specified by the \scheme{library-directories}
parameter.
To the extent possible given the specified set of visible libraries
and requirements of libraries to be loaded at run time,
\scheme{compile-whole-program} discards unused code and optimizes
across program and library boundaries, potentially reducing program
load time, run time, and memory requirements.
Some optimization also occurs even across the boundaries of libraries
that are not incorporated into the output, though this optimization
is limited in nature.
\index{\scheme{generate-wpo-files}}%
The procedures \scheme{compile-file}, \scheme{compile-program}, \scheme{compile-library},
\scheme{compile-script}, and \scheme{compile-whole-library} produce wpo files as well as ordinary
object files when the \scheme{generate-wpo-files} parameter is set
to \scheme{#t} (the default is \scheme{#f}).
\scheme{compile-port} and \scheme{compile-to-port} do so when passed
an optional wpo port.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-whole-library}{\categoryprocedure}{(compile-whole-library \var{input-filename} \var{output-filename})}
\returns a list of libraries left to be loaded at run time
\listlibraries
\endentryheader
\scheme{compile-whole-library} is like \scheme{compile-whole-program},
except \var{input-filename} must specify a wpo file for a library,
all libraries are automatically made visible, and a new wpo file is
produced (when \scheme{generate-wpo-files} is \scheme{#t}) as well
as an object file for the resulting combination of libraries.
The comment in the description of \scheme{compile-whole-program}
about the effect of initialization-code linearization on dynamic
dependencies applies to \scheme{compile-whole-library} as well.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-port}{\categoryprocedure}{(compile-port \var{input-port} \var{output-port})}
\formdef{compile-port}{\categoryprocedure}{(compile-port \var{input-port} \var{output-port} \var{sfd})}
\formdef{compile-port}{\categoryprocedure}{(compile-port \var{input-port} \var{output-port} \var{sfd} \var{wpo-port})}
\formdef{compile-port}{\categoryprocedure}{(compile-port \var{input-port} \var{output-port} \var{sfd} \var{wpo-port} \var{covop})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{input-port} must be a textual input port.
\var{output-port} and, if present and non-false, \var{wpo-port} must be binary output ports.
If present and non-false, \var{sfd} must be a source-file descriptor.
If present and non-false, \var{covop} must be a textual output port.
\scheme{compile-port} is like \scheme{compile-file} except that it takes
input from an arbitrary textual input port and sends output to an arbitrary
binary output port.
If \var{sfd} is supplied, it is passed to the reader so that source information
can be associated with the expressions read from \var{input-port}.
It is also used to associate block-profiling information with the input
file name encapsulated within \var{sfd}.
If \var{wpo-port} is supplied, \scheme{compile-port} sends whole-program optimization information
to \var{wpo-port} for use by \scheme{compile-whole-program}, as if
(and regardless of whether) \scheme{generate-wpo-files} is set.
If \var{covop} is supplied, \scheme{compile-port} sends coverage information to
\var{covop}, as if (and regardless of whether) \scheme{generate-covin-files} is set.
The ports are closed automatically after compilation under the assumption
the program that opens the ports and invokes \scheme{compile-port}
will take care of closing the ports.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-to-port}{\categoryprocedure}{(compile-to-port \var{obj-list} \var{output-port})}
\formdef{compile-to-port}{\categoryprocedure}{(compile-to-port \var{obj-list} \var{output-port} \var{sfd})}
\formdef{compile-to-port}{\categoryprocedure}{(compile-to-port \var{obj-list} \var{output-port} \var{sfd} \var{wpo-port})}
\formdef{compile-to-port}{\categoryprocedure}{(compile-to-port \var{obj-list} \var{output-port} \var{sfd} \var{wpo-port} \var{covop})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{obj-list} must be a list containing a sequence of
objects that represent syntactically valid expressions, each possibly
annotated (Section~\ref{SECTSYNTAXANNOTATIONS}).
If any of the objects does not represent a syntactically valid
expression, \scheme{compile-to-port} raises an exception with
condition type \scheme{&syntax}.
\var{output-port} and, if present, \var{wpo-port} must be binary output ports.
If present, \var{sfd} must be a source-file descriptor.
\scheme{compile-to-port} is like \scheme{compile-file} except that it takes
input from a list of objects and sends output to an arbitrary binary
output port.
\var{sfd} is used to associate block-profiling information with the
input file name encapsulated within \var{sfd}.
If \var{wpo-port} is present, \var{compile-to-port} sends whole-program optimization information
to \var{wpo-port} for use by \scheme{compile-whole-program}, as if
(and regardless of whether) \scheme{generate-wpo-files} is set.
If \var{covop} is present, \var{compile-to-port} sends coverage information to
\var{covop}, as if (and regardless of whether) \scheme{generate-covin-files} is set.
The ports are not closed automatically after compilation under the assumption
the program that opens the port and invokes \scheme{compile-to-port}
will take care of closing the port.
When \var{obj-list} contains a single list-structured element whose
first-element is the symbol \scheme{top-level-program},
\scheme{compile-to-port} returns a list of the libraries the top-level
program requires at run time, as with \scheme{compile-program}.
Otherwise, the return value is unspecified.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-to-file}{\categoryprocedure}{(compile-to-file \var{obj-list} \var{output-file})}
\formdef{compile-to-file}{\categoryprocedure}{(compile-to-file \var{obj-list} \var{output-file} \var{sfd})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{obj-list} must be a list containing a sequence of
objects that represent syntactically valid expressions, each possibly
annotated (Section~\ref{SECTSYNTAXANNOTATIONS}).
If any of the objects does not represent a syntactically valid
expression, \scheme{compile-to-file} raises an exception with
condition type \scheme{&syntax}.
\var{output-file} must be a string.
If present, \var{sfd} must be a source-file descriptor.
\scheme{compile-to-file} is like \scheme{compile-file} except that it takes
input from a list of objects.
\var{sfd} is used to associate block-profiling information with the
input file name encapsulated within \var{sfd}.
When \var{obj-list} contains a single list-structured element whose
first-element is the symbol \scheme{top-level-program},
\scheme{compile-to-file} returns a list of the libraries the top-level
program requires at run time, as with \scheme{compile-program}.
Otherwise, the return value is unspecified.
%----------------------------------------------------------------------------
\entryheader
\formdef{concatenate-object-files}{\categoryprocedure}{(concatenate-object-files \var{out-file} \var{in-file_1} \var{in-file_2} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\var{out-file} and each \var{in-file} must be strings.
\scheme{concatenate-object-files} combines the header information
contained in the object files named by each \var{in-file}. It then
writes the combined header information to the file named by
\var{out-file}, followed by the remaining object code from each
input file in turn.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-boot-file}{\categoryprocedure}{(make-boot-file \var{output-filename} \var{base-boot-list} \var{input-filename} \dots)}
\returns unspecified
\listlibraries
\endentryheader
\var{output-filename}, \var{input-filename}, and the elements of
\var{base-boot-list} must be strings.
\scheme{make-boot-file} writes a boot header to the file named by
\var{output-filename}, followed by the object code for each
\var{input-filename} in turn.
If an input file is not already compiled, \scheme{make-boot-file} compiles
the file as it proceeds.
The boot header identifies the elements of \var{base-boot-list} as
alternative boot files upon which the new boot file depends.
If the list of strings naming base boot files is empty, the first named
input file should be a base boot file, i.e., petite.boot or some boot file
derived from petite.boot.
\index{\scheme{--boot} command-line option}%
\index{\scheme{-b} command-line option}%
Boot files are loaded explicitly via the \scheme{--boot} or \scheme{-b}
command-line options or implicitly based on the name of the executable
(Section~\ref{SECTUSECOMMANDLINE}).
See Section~\ref{SECTUSEAPPLICATIONS} for more information on boot files
and the use of \scheme{make-boot-file}.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-boot-header}{\categoryprocedure}{(make-boot-header \var{output-filename} \var{base-boot_1} \var{base-boot_2}\dots)}
\returns unspecified
\listlibraries
\endentryheader
This procedure has been subsumed by \scheme{make-boot-file} and is provided for
backward compatibility.
The call
\schemedisplay
(make-boot-header \var{output-filename} \var{base-boot_1} \var{base-boot_2} \dots)
\endschemedisplay
is equivalent to
\schemedisplay
(make-boot-file \var{output-filename} '(\var{base-boot_1} \var{base-boot_2} \dots))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{strip-fasl-file}{\categoryprocedure}{(strip-fasl-file \var{input-path} \var{output-path} \var{options})}
\returns unspecified
\listlibraries
\endentryheader
\var{input-path} and \var{output-path} must be strings.
\var{input-path} must name an existing, readable file containing
object code produced by \scheme{compile-file}, one of the other
file-compiling procedures, or an earlier run of \scheme{strip-fasl-file}.
\var{options} must be an enumeration set over the symbols constituting
valid strip options, as described in the \scheme{fasl-strip-options}
entry below.
The new procedure \scheme{strip-fasl-file} allows the removal of
source information of various sorts from a compiled object (fasl)
file produced by \scheme{compile-file} or one of the other file
compiling procedures.
It also allows removal of library visit code from object files
containing compiled libraries.
Visit code is the code for macro transformers and meta definitions
required to compile (but not run) dependent libraries.
On most platforms, the input and output paths can be the same,
in which case the input file is replaced with a new file containing
the stripped object code.
Using the same path will likely fail on Windows file systems,
which do not generally permit an open file to be removed.
If \var{options} is empty, the output file is effectively equivalent
to the input file, though it will not necessarily be identical.
%----------------------------------------------------------------------------
\entryheader
\formdef{fasl-strip-options}{\categorysyntax}{(fasl-strip-options \var{symbol} \dots)}
\returns a fasl-strip-options enumeration set
\listlibraries
\endentryheader
\noindent
Fasl-strip-options enumeration sets are passed to \scheme{strip-fasl-file}
to determine what is stripped.
The available options are described below.
\begin{description}
\item[\scheme{inspector-source}:]
Strip inspector source information.
This includes source expressions that might otherwise be available
for procedures and continuations with the ``code'' and ``call''
commands and messages in the interactive and object inspectors.
It also includes filename and position information that might
otherwise be available for the same via the ``file'' command and
``source'' messages.
\item[\scheme{source-annotations}:]
Strip source annotations, which typically appear only on syntax objects,
e.g., identifiers, in the templates of macro transformers.
\item[\scheme{profile-source}:]
Strip source file and character position information from profiled
code objects.
This does not remove the profile counters or eliminate the overhead
for incrementing them at run time.
\item[\scheme{compile-time-information}: ]
This strips compile-time information from compiled libraries, potentially
reducing the size of the resulting file but making it impossible to
use the file to compile dependent code.
This option is useful for creating smaller object files to ship
as part of a binary-only package.
\end{description}
%----------------------------------------------------------------------------
\entryheader
\formdef{machine-type}{\categoryprocedure}{(machine-type)}
\returns the current machine type
\listlibraries
\endentryheader
\noindent
Consult the release notes for the current version of {\ChezScheme}
for a list of supported machine types.
%----------------------------------------------------------------------------
\entryheader\label{desc:expand}
\formdef{expand}{\categoryprocedure}{(expand \var{obj})}
\formdef{expand}{\categoryprocedure}{(expand \var{obj} \var{env})}
\returns expansion of the Scheme form represented by \var{obj}
\listlibraries
\endentryheader
\noindent
\scheme{expand} treats \var{obj} as the representation of an expression.
It expands the expression in environment \var{env} and returns
an object representing the expanded form.
If no environment is provided, it defaults to the environment
returned by \scheme{interaction-environment}.
\var{obj} can be an annotation
(Section~\ref{SECTSYNTAXANNOTATIONS}), and the default expander
makes use of annotations to incorporate source-file
information in error messages.
\scheme{expand} actually passes its arguments to the current expander
(see \scheme{current-expand}), initially \scheme{sc-expand}.
\index{\scheme{expand-output}}%
See also \scheme{expand-output} (page~\pageref{desc:expand-output})
which can be used to request that the compiler or interpreter show
expander output.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-expand}{\categorythreadparameter}{current-expand}
\listlibraries
\endentryheader
\noindent
\scheme{current-expand} determines the expansion procedure used by
the compiler, interpreter, and direct calls to
\index{\scheme{expand}}\scheme{expand}
to expand syntactic extensions.
\scheme{current-expand} is initially bound to the value of
\index{\scheme{sc-expand}}\scheme{sc-expand}.
It may be set another procedure, but since the format of
expanded code expected by the compiler and interpreter is not publicly
documented, only \scheme{sc-expand} produces correct output, so the
other procedure must ultimately be defined in terms of
\scheme{sc-expand}.
The first argument to the expansion procedure represents the input
expression.
It can be an annotation (Section~\ref{SECTSYNTAXANNOTATIONS}) or an
unannotated value.
the second argument is an environment.
Additional arguments might be passed to the expansion procedure
by the compiler, interpreter, and \scheme{expand}; their number
and roles are unspecified.
%----------------------------------------------------------------------------
\entryheader
\formdef{sc-expand}{\categoryprocedure}{(sc-expand \var{obj})}
\formdef{sc-expand}{\categoryprocedure}{(sc-expand \var{obj} \var{env})}
\returns the expanded form of \var{obj}
\listlibraries
\endentryheader
\noindent
The procedure
\scheme{sc-expand} is used to expand programs written using
\scheme{syntax-case} macros.
\scheme{sc-expand} is the default expander, i.e., the initial
value of \scheme{current-expand}.
\var{obj} represents the program to be expanded, and
\var{env} must be an environment.
\var{obj} can be an annotation (Section~\ref{SECTSYNTAXANNOTATIONS})
or unannotated value.
If not provided, \var{env} defaults to the environment returned by
\scheme{interaction-environment}.
%----------------------------------------------------------------------------
\entryheader\label{desc:expand/optimize}
\formdef{expand/optimize}{\categoryprocedure}{(expand/optimize \var{obj})}
\formdef{expand/optimize}{\categoryprocedure}{(expand/optimize \var{obj} \var{env})}
\returns result of expanding and optimizing form represented by \var{obj}
\listlibraries
\endentryheader
\scheme{expand/optimize} treats \var{obj} as the representation of
an expression.
\var{obj} can be an annotation (Section~\ref{SECTSYNTAXANNOTATIONS})
or unannotated value.
\scheme{expand/optimize} expands the expression in environment \var{env}
and passes the expression through the source optimizer \scheme{cp0}
(unless \scheme{cp0} is disabled via \scheme{run-cp0}).
It also simplifies \scheme{letrec} and \scheme{letrec*} expressions within
the expression and makes their undefined checks explicit.
It returns an object representing the expanded, simplified, and optimized form.
If no environment is provided, it defaults to the environment
returned by \scheme{interaction-environment}.
\scheme{expand/optimize} is primarily useful for understanding what
\scheme{cp0} does and does not optimize.
Many optimizations are performed later in the compiler,
so \scheme{expand/optimize} does not give a complete picture of
optimizations performed.
\schemedisplay
(expand/optimize
'(let ([y '(3 . 4)])
(+ (car y) (cdr y)))) ;=> 7
(print-gensym #f)
(expand/optimize
'(let ([y '(3 . 4)])
(lambda (x)
(* (+ (car y) (cdr y)) x)))) ;=> (lambda (x) (#2%* 7 x))
(expand/optimize
'(let ([n (expt 2 10)])
(define even?
(lambda (x) (or (zero? x) (not (odd? x)))))
(define odd?
(lambda (x) (not (even? (- x 1)))))
(define f
(lambda (x)
(lambda (y)
(lambda (z)
(if (= z 0) (omega) (+ x y z))))))
(define omega
(lambda ()
((lambda (x) (x x)) (lambda (x) (x x)))))
(let ([g (f 1)] [m (f n)])
(let ([h (if (> ((g 2) 3) 5)
(lambda (x) (+ x 1))
odd?)])
(h n))))) ;=> 1025
\endschemedisplay
\index{\scheme{expand/optimize-output}}%
See also \scheme{expand/optimize-output} (page~\pageref{desc:expand/optimize-output})
which can be used to request that the compiler or interpreter show
source-optimizer output.
%----------------------------------------------------------------------------
\entryheader
\formdef{eval-when}{\categorysyntax}{(eval-when \var{situations} \var{form_1} \var{form_2} \dots)}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{situations} must be a list containing some combination of the symbols
\scheme{eval}, \scheme{compile}, \scheme{load}, \scheme{visit}, and
\scheme{revisit}.
When source files are loaded (see \scheme{load}), the forms in the file
are read, compiled, and executed sequentially, so that each form in
the file is fully evaluated before the next one is read.
When a source file is compiled (see \scheme{compile-file}), however, the
forms are read and compiled, \emph{but not executed}, in sequence.
This distinction matters only when the execution of one
form in the file affects the compilation of later forms, e.g.,
when the form results in the definition of a module or syntactic form or
sets a compilation parameter such as \scheme{optimize-level} or
\scheme{case-sensitive}.
For example, assume that a file contains the following two forms:
\schemedisplay
(define-syntax reverse-define
(syntax-rules ()
[(_ e x) (define x e)]))
(reverse-define 3 three)
\endschemedisplay
Loading this from source has the effect of defining
\scheme{reverse-define} as a syntactic form and binding the identifier
\scheme{three} to 3.
The situation may be different if the file is compiled with
\scheme{compile-file}, however.
Unless the system or programmer takes steps to assure that the first
form is fully executed before the second expression is compiled,
the syntax expander will not recognize \scheme{reverse-define} as a syntactic
form and will generate code for a procedure call to \scheme{reverse-define}
instead of generating code to define \scheme{three} to be 3.
When the object file is subsequently loaded, the attempt to reference
either \scheme{reverse-define} or \scheme{three} will fail.
As it happens, when a \scheme{define-syntax}, \scheme{module},
\scheme{import}, or \scheme{import-only} form appears at top level, as in the
example above, the compiler does indeed arrange to evaluate it before
going on to compile the remainder of the file.
If the compiler encounters a variable definition for an identifier that
was previously something else, it records that fact as well.
The compiler also generates the
appropriate code so that the bindings will be present as well when
the object file is subsequently loaded.
This solves most, but not all, problems of this nature, since most are
related to the use of \scheme{define-syntax} and modules.
Some problems are not so straightforwardly handled, however.
For example, assume that the file contains the following definitions
for \index{\scheme{nodups?}}\scheme{nodups?} and \index{\scheme{mvlet}}\scheme{mvlet}.
\schemedisplay
(define nodups?
(lambda (ids)
(define bound-id-member?
(lambda (id ids)
(and (not (null? ids))
(or (bound-identifier=? id (car ids))
(bound-id-member? id (cdr ids))))))
(or (null? ids)
(and (not (bound-id-member? (car ids) (cdr ids)))
(nodups? (cdr ids))))))
(define-syntax mvlet
(lambda (x)
(syntax-case x ()
[(_ ((x ...) expr) b1 b2 ...)
(and (andmap identifier? #'(x ...))
(nodups? #'(x ...)))
#'(call-with-values
(lambda () expr)
(lambda (x ...) b1 b2 ...))])))
(mvlet ((a b c) (values 1 2 3))
(list (* a a) (* b b) (* c c)))
\endschemedisplay
\noindent
When loaded directly, this results in the definition of
\scheme{nodups?} as a procedure and \scheme{mvlet} as a syntactic
abstraction before evaluation of the \scheme{mvlet} expression.
Because \scheme{nodups?} is defined before the \scheme{mvlet}
expression is expanded, the call to \scheme{nodups?} during the
expansion of \scheme{mvlet} causes no difficulty.
If instead this file were compiled, using \scheme{compile-file}, the
compiler would arrange to define \scheme{mvlet} before continuing
with the expansion and evaluation of the \scheme{mvlet} expression,
but it would not arrange to define \scheme{nodups?}.
Thus the expansion of the \scheme{mvlet} expression would fail.
In this case it does not help to evaluate the syntactic extension alone.
A solution in this case would be to move the definition of
\scheme{nodups?} inside the definition for \scheme{mvlet}, just as
the definition for \scheme{bound-id-member?} is placed within
\scheme{nodups?}, but this does not work for help routines shared
among several syntactic definitions.
Another solution is to label the \scheme{nodups?} definition a
``meta'' definition (see Section~\ref{SECTSYNTAXMETA}) but this
does not work for helpers that are used both by syntactic
abstractions and by run-time code.
A somewhat simpler problem occurs when setting parameters that affect
compilation, such as \scheme{optimize-level} and
\scheme{case-sensitive?}.
If not set prior to compilation, their settings usually will not have
the desired effect.
\scheme{eval-when} offers a solution to these problems by allowing the
programmer to explicitly control what forms should or should not
be evaluated during compilation.
\scheme{eval-when} is a syntactic form and is handled directly by the
expander.
The action of \scheme{eval-when} depends upon the \var{situations} argument
and whether or not the forms \scheme{\var{form_1} \var{form_2} \dots}
are being compiled via \scheme{compile-file} or are being evaluated
directly.
Let's consider each of the possible situation specifiers
\scheme{eval}, \scheme{compile}, \scheme{load}, \scheme{visit}, and
\scheme{revisit} in turn.
\begin{description}
\item[\scheme{eval}:]
The \scheme{eval} specifier is relevant only when the \scheme{eval-when}
form is being
evaluated directly, i.e., if it is typed at the keyboard or loaded from a
source file.
Its presence causes \scheme{\var{form_1} \var{form_2} \dots} to be
expanded and this expansion to be included in the expansion of the
\scheme{eval-when} form.
Thus, the forms will be evaluated directly as if not contained within an
\scheme{eval-when} form.
\item[\scheme{compile}:]
The \scheme{compile} specifier is relevant only when the \scheme{eval-when}
form appears in a file currently being compiled.
(Its presence is simply ignored otherwise.)
Its presence forces \scheme{\var{form_1} \var{form_2} \dots} to be
expanded and evaluated immediately.
\item[\scheme{load}:]
The \scheme{load} specifier is also relevant only when the \scheme{eval-when}
form appears
in a file currently being compiled.
Its presence causes \scheme{\var{form_1} \var{form_2} \dots} to be
expanded and this expansion to be included in the expansion of the
\scheme{eval-when} form.
Any code necessary to record binding information and evaluate syntax
transformers for definitions contained in the forms is marked for
execution when the file is ``visited,'' and any code necessary to
compute the values of variable definitions and the expressions contained
within the forms is marked for execution when the file is ``revisited.''
\item[\scheme{visit}:]
The \scheme{visit} specifier is also relevant only when the \scheme{eval-when}
form appears
in a file currently being compiled.
Its presence causes \scheme{\var{form_1} \var{form_2} \dots} to be
expanded and this expansion to be included in the expansion of the
\scheme{eval-when} form, with an annotation that the forms are to be
executed when the file is ``visited.''
\item[\scheme{revisit}:]
The \scheme{revisit} specifier is also relevant only when the \scheme{eval-when}
form appears
in a file currently being compiled.
Its presence causes \scheme{\var{form_1} \var{form_2} \dots} to be
expanded and this expansion to be included in the expansion of the
\scheme{eval-when} form, with an annotation that the forms are to be
executed when the file is ``revisited.''
\end{description}
\noindent
A file is considered ``visited'' when it is brought in by either
\scheme{load} or \scheme{visit} and ``revisited'' when it is brought in
by either \scheme{load} or \scheme{revisit}.
Top-level expressions are treated as if they are wrapped in an
\scheme{eval-when} with situations \scheme{load} and \scheme{eval}.
This means that, by default, forms typed at the keyboard or
loaded from a source file are evaluated, and forms appearing in a
file to be compiled are not evaluated directly but are compiled for
execution when the resulting object file is subsequently loaded.
The treatment of top-level definitions is slightly more involved.
All definitions result in changes to the compile-time environment.
For example, an identifier defined by \scheme{define} is recorded
as a variable, and an identifier defined by \scheme{define-syntax}
is recorded as a keyword and associated with the value of its
right-hand-side (transformer) expression.
These changes are made at eval, compile, and load
time as if the definitions were wrapped in an \scheme{eval-when} with
situations \scheme{eval}, \scheme{load}, and \scheme{compile}.
(This behavior can be altered by changing the value of the
parameter \scheme{eval-syntax-expanders-when}.)
Some definitions also result in changes to the run-time environment.
For example, a variable is associated with the value of its
right-hand-side expression.
These changes are made just at evaluation and load time as if the
definitions were wrapped in an \scheme{eval-when} with situations
\scheme{eval} and \scheme{load}.
The treatment of local expressions or definitions (those not at top level)
that are wrapped in an \scheme{eval-when} depends only upon whether the
situation \scheme{eval} is present in the list of situations.
If the situation \scheme{eval} is present, the definitions and expressions
are evaluated as if they were not wrapped in an \scheme{eval-when} form,
i.e., the \scheme{eval-when} form is treated as a \scheme{begin} form.
If the situation \scheme{eval} is not present, the forms are ignored;
in a definition context, the \scheme{eval-when} form is treated as an
empty \scheme{begin}, and in an expression context, the \scheme{eval-when}
form is treated as a constant with an unspecified value.
Since top-level syntax bindings are established, by default, at compile
time as well as eval and load time, top-level variable bindings needed
by syntax transformers should be wrapped in an \scheme{eval-when} form
with situations \scheme{compile}, \scheme{load}, and \scheme{eval}.
We can thus \scheme{nodups?} problem above by enclosing the definition
of \scheme{nodups?} in an \scheme{eval-when} as follows.
\schemedisplay
(eval-when (compile load eval)
(define nodups?
(lambda (ids)
(define bound-id-member?
(lambda (id ids)
(and (not (null? ids))
(or (bound-identifier=? id (car ids))
(bound-id-member? id (cdr ids))))))
(or (null? ids)
(and (not (bound-id-member? (car ids) (cdr ids)))
(nodups? (cdr ids)))))))
\endschemedisplay
\noindent
This forces it to be evaluated before it is needed during the expansion
of the \scheme{mvlet} expression.
Just as it is useful to add \scheme{compile} to the default
\scheme{load} and \scheme{eval} situations, omitting options is also
useful.
Omitting one or more of \scheme{compile}, \scheme{load}, and
\scheme{eval} has the effect of preventing the evaluation at the given
time.
Omitting all of the options has the effect of inhibiting evaluation
altogether.
One common combination of situations is \scheme{(compile eval)}, which by the
inclusion of \scheme{compile} causes the expression to be evaluated at
compile time, and by the omission of \scheme{load} inhibits the generation
of code by the compiler for execution when the file is subsequently loaded.
This is typically used for the definition of syntactic extensions used only
within the file in which they appear; in this case their presence in the
object file is not necessary.
It is also used to set compilation parameters that are intended to be in
effect whether the file is loaded from source or compiled via
\scheme{compile-file}
\schemedisplay
(eval-when (compile eval) (case-sensitive #t))
\endschemedisplay
Another common situations list is \scheme{(compile)}, which might be
used to set compilation options to be used only when the file is
compiled via \scheme{compile-file}.
\schemedisplay
(eval-when (compile) (optimize-level 3))
\endschemedisplay
Finally, one other common combination is \scheme{(load eval)}, which might
be useful for inhibiting the double evaluation (during the compilation of
a file and again when the resulting object file is loaded) of syntax
definitions when the syntactic extensions are not needed within
the file in which their definitions appear.
The behavior of \scheme{eval-when} is usually intuitive but can be
understood precisely as follows.
The \scheme{syntax-case} expander, which handles \scheme{eval-when}
forms, maintains two state sets, one for compile-time forms and
one for run-time forms.
The set of possible states in each set are ``L'' for \scheme{load},
``C'' for \scheme{compile}, ``V'' for \scheme{visit}, ``R'' for
\scheme{revisit}, and ``E'' for \scheme{eval}.
When compiling a file, the compile-time set initially contains ``L''
and ``C'' and the run-time set initially contains only ``L.''
When not compiling a file (as when a form is evaluated by the
read-eval-print loop or loaded from a source file), both sets
initially contain only ``E.''
The subforms of an \scheme{eval-when} form at top level are expanded with
new compile- and run-time sets determined by the current sets and
the situations listed in the \scheme{eval-when} form.
Each element of the current set contributes zero or more elements to the
new set depending upon the given situations according to the following
table.
\begin{tabular}{cccccc}
& \scheme{load}~ & ~\scheme{compile}~ & ~\scheme{visit}~ & ~\scheme{revisit}~ & ~\scheme{eval}\\
L & L & C & V & R & --- \\
C & --- & --- & --- & --- & C \\
V & V & C & V & --- & --- \\
R & R & C & --- & R & --- \\
E & --- & --- & --- & --- & E \\
\end{tabular}
For example, if the current compile-time state set is \{L\}
and the situations are \scheme{load} and \scheme{compile}, the new compile-time
state set is \{L,~C\}, since L/\scheme{load}
contributes ``L'' and L/\scheme{compile} contributes ``C.''
The state sets determine how forms are treated by the expander.
Compile-time forms such as syntax definitions are evaluated at a time
or times determined by the compile-time state set, and run-time forms
are evaluated at a time or times determined by the run-time state set.
A form is evaluated immediately if ``C'' is in the state set.
Code is generated to evaluate the form at visit or revisit
time if ``V'' or ``R'' is present.
If ``L'' is present in the compile-time set, it is treated as ``V;''
likewise, if ``L'' is present in the run-time set, it is treated as
``R.''
If more than one of states is present in the state set, the
form is evaluated at each specified time.
``E'' can appear in the state set only when not compiling a file, i.e.,
when the expander is invoked from an evaluator such as \scheme{compile}
or \scheme{interpret}.
When it does appear, the expanded form is returned from the expander to be
processed by the evaluator, e.g., \scheme{compile} or \scheme{interpret},
that invoked the expander.
The value of the parameter \scheme{eval-syntax-expanders-when} actually determines
the initial compile-time state set.
The parameter is bound to a list of situations, which defaults to
\scheme{(compile load eval)}.
When compiling a file, \scheme{compile} contributes ``C'' to the
state set, \scheme{load} contributes ``L,'' \scheme{visit} contributes
``V,'' \scheme{revisit} contributes ``R,'' and \scheme{eval}
contributes nothing.
When not compiling a file, \scheme{eval} contributes ``E'' to the
state set, and the other situations contribute nothing.
There is no corresponding parameter for controlling the initial value
of the run-time state set.
\label{eval-when-tlp}%
For RNRS top-level programs, \scheme{eval-when} is essentially ineffective.
The entire program is treated as a single expression, so \scheme{eval-when}
becomes a local \scheme{eval-when} for which only the \scheme{eval}
situation has any relevance.
As for any local \scheme{eval-when} form, the subforms are ignored if
the \scheme{eval} situation is not present; otherwise, they are treated as
if the \scheme{eval-when} wrapper were absent.
%----------------------------------------------------------------------------
\entryheader
\formdef{eval-syntax-expanders-when}{\categorythreadparameter}{eval-syntax-expanders-when}
\listlibraries
\endentryheader
\noindent
This parameter must be set to a list representing a set of
\scheme{eval-when} situations, e.g., a list containing at most one
occurrence of each of the symbols \scheme{eval}, \scheme{compile},
\scheme{load}, \scheme{visit}, and \scheme{revisit}.
It is used to determine the evaluation time of syntax
definitions, module forms, and import forms are expanded.
(See the discussion of \scheme{eval-when} above.)
The default value is \scheme{(compile load eval)}, which causes
compile-time information in a file to be established when the file is
loaded from source, when it is compiled via \scheme{compile-file},
and when a compiled version of the file is loaded via \scheme{load}
or \scheme{visit}.
\section{Source Directories and Files\label{SECTSYSTEMSOURCE}}
%----------------------------------------------------------------------------
\entryheader
\formdef{source-directories}{\categoryglobalparameter}{source-directories}
\listlibraries
\endentryheader
\noindent
The value of \scheme{source-directories} must be a list of strings, each
of which names a directory path.
\scheme{source-directories} determines the set of directories searched
for source or object files when a file is loaded via \scheme{load}, \scheme{load-library},
\scheme{load-program}, \scheme{include},
\scheme{visit}, or \scheme{revisit},
when a syntax error occurs, or when a source
file is opened in the interactive inspector.
The default value is the list \scheme{(".")}, which means source files
will be found only in or relative to the current directory, unless named
with an absolute path.
This parameter is never altered by the system, with one exception.
The expander temporarily adds (via \scheme{parameterize}) the directory
in which a library file resides to the front of the \scheme{source-directories}
list when it compiles (when \scheme{compile-imported-libraries} is true) or loads the library from source, which it does
only if the library is not already defined.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-source-path}{\categoryprocedure}{(with-source-path \var{who} \var{name} \var{procedure})}
\listlibraries
\endentryheader
\noindent
The procedure \scheme{with-source-path} searches through the current
source-directories path, in order, for a file with the specified
\var{name} and invokes \var{procedure} on the result.
If no such file is found, an exception is raised with condition types
\scheme{&assertion} and \scheme{&who} with \var{who} as
who value.
If \var{name} is an absolute pathname or one beginning with \scheme{./}
(or \scheme{.\} under Windows) or \scheme{../} (or \scheme{..\} under
Windows), or if the list of source directories
contains only \scheme{"."}, the default, or \scheme{""}, which is
equivalent to \scheme{"."}, no searching is performed and \var{name} is
returned.
\var{who} must be a symbol, \var{name} must be a string, and
\var{procedure} should accept one argument.
The following examples assumes that the file ``pie'' exists
in the directory ``../spam'' but not in ``../ham'' or the current
directory.
\schemedisplay
(define find-file
(lambda (fn)
(with-source-path 'find-file fn values)))
(find-file "pie") ;=> "pie"
(source-directories '("." "../ham"))
(find-file "pie") ;=> \var{exception in find-file: pie not found}
(source-directories '("." "../spam"))
(find-file "pie") ;=> "../spam/pie"
(source-directories '("." "../ham"))
(find-file "/pie") ;=> "/pie"
(source-directories '("." "../ham"))
(find-file "./pie") ;=> "./pie"
(source-directories '("." "../spam"))
(find-file "../pie") ;=> "../ham/pie"
\endschemedisplay
\section{Compiler Controls\label{SECTMISCOPTIMIZE}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{optimize-level}{\categorythreadparameter}{optimize-level}
\listlibraries
\endnoskipentryheader
\noindent
This parameter can take on one of the four values 0, 1, 2, and 3.
In theory, this parameter controls the amount of optimization
performed by the compiler.
In practice, it does so only indirectly, and the only difference
is between optimize level 3, at which the compiler generates
``unsafe'' code, and optimize levels 0--2, at which the compiler
generates ``safe'' code.
Safe code performs full type and bounds checking so that, for example,
an attempt to apply a non-procedure, an attempt to take the car of a
non-pair, or an attempt to reference beyond the end of a vector each
result in an exception being raised.
With unsafe code, the same situations may result in invalid memory
references, corruption of the Scheme heap (which may cause
seemingly unrelated problems later), system crashes, or other undesirable
behaviors.
Unsafe code is typically faster, but optimize-level 3 should be used with
caution and only on sections of well-tested code that must run as quickly
as possible.
While the compiler produces the same code for optimize levels 0--2,
user-defined macro transformers can differentiate among the different
levels if desired.
One way to use optimize levels is on a per-file
basis, using \index{\scheme{eval-when}}\scheme{eval-when} to force the use of a particular
optimize level at compile time.
For example, placing:
\schemedisplay
(eval-when (compile) (optimize-level 3))
\endschemedisplay
\noindent
at the front of a file will cause all of the forms in the file to be
compiled at optimize level 3 when the file is compiled (using
\index{\scheme{compile-file}}\scheme{compile-file}) but does not affect the optimize level used
when the file is loaded from source.
Since \scheme{compile-file} parameterizes \scheme{optimize-level} (see \scheme{parameterize}),
the above
expression does not permanently alter the optimize level in the
system in which the \scheme{compile-file} is performed.
The optimize level can also be set via the
\index{\scheme{--optimize-level} command-line option}\scheme{--optimize-level}
command-line option (Section~\ref{SECTUSECOMMANDLINE}).
This option is particularly useful for running RNRS top-level programs
at optimize-level~3 via the
\index{\scheme{--program} command-line option}\scheme{--program} command-line option,
since \scheme{eval-when} is ineffective for RNRS top-level programs as described
on page~\pageref{eval-when-tlp}.
%----------------------------------------------------------------------------
\entryheader\label{desc:hash-primitive}
\xformdef{$primitive (~#%~)}{$primitive (~#%~)@\scheme{$primitive} (~\scheme{#%}~)}{\categorysyntax}{($primitive \var{variable})}
\xformdef{!L#% ($primitive)}{#% ($primitive)@\scheme{#%} (\scheme{$primitive})}{\categorysyntax}{#%\var{variable}}
\xformdef{$primitive (~#2%~)}{$primitive (~#2%~)@\scheme{$primitive} (~\scheme{#2%}~)}{\categorysyntax}{($primitive 2 \var{variable})}
\xformdef{!M#2% ($primitive)}{#% ($primitive)@\scheme{#2%} (\scheme{$primitive})}{\categorysyntax}{#2%\var{variable}}
\xformdef{$primitive (~#3%~)}{$primitive (~#3%~)@\scheme{$primitive} (~\scheme{#3%}~)}{\categorysyntax}{($primitive 3 \var{variable})}
\xformdef{!N#3% ($primitive)}{#% ($primitive)@\scheme{#3%} (\scheme{$primitive})}{\categorysyntax}{#3%\var{variable}}
\returns the primitive value for \var{variable}
\libraryexport{$primitive}\listlibraries
\endentryheader
\noindent
\var{variable} must name a primitive procedure.
The \scheme{$primitive} syntactic form allows control over the
optimize level at the granularity of individual primitive references,
and it can be used to access the original value
of a primitive, regardless of the lexical context or the current
top-level binding for the variable originally bound to the primitive.
The expression \scheme{($primitive \var{variable})} may
be abbreviated as \scheme{#%\var{variable}}.
The reader expands \scheme{#%} followed by an object
into a \scheme{$primitive} expression, much as it expands \scheme{'\var{object}}
into a \scheme{quote} expression.
If a \scheme{2} or \scheme{3} appears in the form or between the
\scheme{#} and \scheme{%} in the abbreviated form, the compiler treats
an application of the primitive as if it were compiled
at the corresponding optimize level (see the \scheme{optimize-level}
parameter).
If no number appears in the form, an application of the primitive is
treated as an optimize-level 3 application if the current optimize
level is 3;
otherwise, it is treated as an optimize-level 2 application.
\schemedisplay
(#%car '(a b c)) ;=> a
(let ([car cdr]) (car '(a b c))) ;=> (b c)
(let ([car cdr]) (#%car '(a b c))) ;=> a
(begin (set! car cdr) (#%car '(a b c))) ;=> a
\endschemedisplay
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{debug-level}{\categorythreadparameter}{debug-level}
\listlibraries
\endnoskipentryheader
\noindent
This parameter can take on one of the four values 0, 1, 2, and 3.
It is used to tell the compiler how important the preservation of
debugging information is, with 0 being least important and 3 being
most important.
The default value is 1.
As of Version~9.0, it is used solely to determine whether an
error-causing call encountered in nontail position is treated as
if it were in tail position (thus causing the caller's frame not
to appear in a stack backtrace); this occurs at debug levels below~2.
%----------------------------------------------------------------------------
\entryheader
\formdef{generate-interrupt-trap}{\categorythreadparameter}{generate-interrupt-trap}
\listlibraries
\endentryheader
\noindent
To support interrupts, including keyboard, timer, and collect request
interrupts, the compiler inserts a short sequence of instructions at the
entry to each nonleaf procedure (Section~\ref{SECTSYSTEMINTERRUPTS}).
This small overhead may be eliminated by setting
\scheme{generate-interrupt-trap} to \scheme{#f}.
The default value of this parameter is \scheme{#t}.
It is rarely a good idea to compile code without interrupt trap
generation, since a tight loop in the generated code may completely
prevent interrupts from being serviced, including the collect request
interrupt that causes garbage collections to occur automatically.
Disabling trap generation may be useful, however, for routines that act
simply as ``wrappers'' for other routines for which code is presumably
generated with interrupt trap generation enabled.
It may also be useful for short performance-critical routines with
embedded loops or recursions that are known to be short running and
that make no other calls.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-interpret-simple}{\categorythreadparameter}{compile-interpret-simple}
\listlibraries
\endentryheader
\noindent
At all optimize levels, when the value of
\scheme{compile-interpret-simple} is set to a true value (the default),
\index{\scheme{compile}}\scheme{compile} interprets simple
expressions.
A simple expression is one that creates no procedures.
This can save a significant amount of time over the course of many
calls to \scheme{compile} or \scheme{eval} (with \scheme{current-eval}
set to \scheme{compile}, its default value).
When set to false, \scheme{compile} compiles all expressions.
%----------------------------------------------------------------------------
\entryheader\label{desc:generate-inspector-information}
\formdef{generate-inspector-information}{\categorythreadparameter}{generate-inspector-information}
\listlibraries
\endentryheader
\noindent
When this parameter is set to a true value (the default), information
about the source and contents of procedures and continuations is
generated during compilation and retained in tables associated with
each code segment.
This information allows the inspector to provide more complete
information, at the expense of using more memory and producing
larger object files (via \scheme{compile-file}).
Although compilation and loading may be slower when inspector
information is generated, the speed of the compiled code is not
affected.
If this parameter is changed during the compilation of a file, the
original value will be restored.
For example, if:
\schemedisplay
(eval-when (compile) (generate-inspector-information #f))
\endschemedisplay
\noindent
is included in a file, generation of inspector information will be
disabled only for the remainder of that particular file.
%----------------------------------------------------------------------------
\entryheader\label{desc:generate-procedure-source-information}
\formdef{generate-procedure-source-information}{\categorythreadparameter}{generate-procedure-source-information}
\listlibraries
\endentryheader
\noindent
When \scheme{generate-inspector-information} is set to \scheme{#f} and
this parameter is set to \scheme{#t}, then a source location is preserved
for a procedure, even though other inspector information is not preserved.
Source information provides a small amount of debugging support at a
much lower cost in memory and object-file size than full inspector information.
If this parameter is changed during the compilation of a file, the
original value will be restored.
%----------------------------------------------------------------------------
\entryheader
\formdef{enable-cross-library-optimization}{\categorythreadparameter}{enable-cross-library-optimization}
\listlibraries
\endentryheader
This parameter controls whether information is included with the
object code for a compiled library to enable propagation of constants
and inlining of procedures defined in the library into dependent
libraries.
When set to \scheme{#t} (the default), this information is included;
when set to \scheme{#f}, the information is not included.
Setting the parameter to \scheme{#f} potentially reduces the sizes
of the resulting object files and the exposure of near-source
information via the object file.
%----------------------------------------------------------------------------
\entryheader
\formdef{generate-wpo-files}{\categorythreadparameter}{generate-wpo-files}
\listlibraries
\endentryheader
\index{\scheme{compile-whole-program}}%
When this parameter is set to \scheme{#t} (the default is \scheme{#f}),
\scheme{compile-file}, \scheme{compile-library}, \scheme{compile-program},
and \scheme{compile-script} produce whole-program optimization (wpo)
files for use by \scheme{compile-whole-program}.
The name of the \scheme{wpo} file is derived from the output-file
name by replacing the object-file extension (normally \scheme{.so})
with \scheme{.wpo}, or adding the extension \scheme{.wpo} if the
object filename has no extension or has the extension \scheme{.wpo}.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-file-message}{\categorythreadparameter}{compile-file-message}
\listlibraries
\endentryheader
\noindent
When this parameter is set to true, the default, \scheme{compile-file},
\scheme{compile-library}, \scheme{compile-program}, and
\scheme{compile-script} print a message of the form:
\schemedisplay
compiling \var{input-path} with output to \var{output-path}
\endschemedisplay
When the parameter is set to \scheme{#f}, the message is not printed.
%----------------------------------------------------------------------------
\entryheader\label{desc:run-cp0}
\formdef{run-cp0}{\categorythreadparameter}{run-cp0}
\formdef{cp0-effort-limit}{\categorythreadparameter}{cp0-effort-limit}
\formdef{cp0-score-limit}{\categorythreadparameter}{cp0-score-limit}
\formdef{cp0-outer-unroll-limit}{\categorythreadparameter}{cp0-outer-unroll-limit}
\listlibraries
\endentryheader
\noindent
These parameters control the operation of \scheme{cp0}, a source
optimization pass that runs after macro expansion and prior
to most other compiler passes.
\scheme{cp0} performs procedure inlining, in which the code of one
procedure is inlined at points where it is called by other procedures,
as well as copy propagation, constant folding, useless code
elimination, and several related optimizations.
The algorithm used by the optimizer is described in detail in the paper
``Fast and effective procedure inlining''~\cite{waddell:sas97}.
When \scheme{cp0} is enabled, the programmer can count on the compiler
to fold constants, eliminate unnecessary \scheme{let} bindings, and
eliminate unnecessary and inaccessible code.
This is particularly useful when writing macros, since the programmer
can usually handle only the general case and let the compiler simplify
the code when possible.
For example, the programmer can define \scheme{case} as follows:
\schemedisplay
(define-syntax case
(syntax-rules ()
[(_ e [(k ...) a1 a2 ...] ... [else b1 b2 ...])
(let ([t e])
(cond
[(memv t '(k ...)) a1 a2 ...]
...
[else b1 b2 ...]))]
[(_ e [(k ...) a1 a2 ...] ...)
(let ([t e])
(cond
[(memv t '(k ...)) a1 a2 ...]
...))]))
\endschemedisplay
and count on the introduce \scheme{let} expression to be eliminated
if \scheme{e} turns out to be an unassigned variable, and count on
the entire \scheme{case} expression to be folded if \scheme{e} turns
out to be a constant.
It is possible to see what \scheme{cp0} does with an expression
via the procedure \index{\scheme{expand/optimize}}\scheme{expand/optimize},
which expands its argument and passes the result through \scheme{cp0}, as
illustrated by the following transcript.
\schemedisplay
> (print-gensym #f)
> (expand/optimize
'(lambda (x)
(case x [(a) 1] [(b c) 2] [(d) 3] [else 4])))
(lambda (x)
(if (#2%memv x '(a))
1
(if (#2%memv x '(b c)) 2 (if (#2%memv x '(d)) 3 4))))
> (expand/optimize
'(+ (let ([f (lambda (x)
(case x [(a) 1] [(b c) 2] [(d) 3] [else 4]))])
(f 'b))
15))
17
\endschemedisplay
In the first example, the \scheme{let} expression produced by \scheme{case}
is eliminated, and in the second, the entire expression is optimized down
to the constant \scheme{17}.
Although not shown by \scheme{expand/optimize}, the \scheme{memv} calls
in the output code for the first example will be replaced by calls to the
less expensive \scheme{eq?} by a later pass of the compiler.
Additional examples are given in the description
of \scheme{expand/optimize}.
The value of \scheme{run-cp0} must be a procedure.
Whenever the compiler is invoked on a Scheme form, the value \var{p}
of this parameter is called to determine whether and how
\scheme{cp0} is run.
\var{p} receives two arguments: \var{cp0}, the entry point into
\scheme{cp0}, and \var{x}, the form being compiled.
The default value of \scheme{run-cp0} simply invokes \var{cp0} on
\var{x}, then \var{cp0} again on the result.
The second run is useful in some cases because the first run
may not eliminate bindings for certain variables that appear to be
referenced but are not actually referenced after inlining.
The marginal benefit of the second run is usually minimal, but so is the
cost.
\noindent
Interesting variants include
\schemedisplay
(run-cp0 (lambda (cp0 x) x))
\endschemedisplay
\noindent
which bypasses (disables) \scheme{cp0}, and
\schemedisplay
(run-cp0 (lambda (cp0 x) (cp0 x)))
\endschemedisplay
\noindent
which runs \scheme{cp0} just once.
The value of \scheme{cp0-effort-limit} determines the maximum amount
of effort spent on each inlining attempt.
The time spent optimizing a program is a linear function of this limit and the
number of calls in the program's source, so small values for this parameter
enforce a tighter bound on compile time.
When set to zero, inlining is disabled except when the name of a procedure
is referenced only once.
The value of \scheme{cp0-score-limit} determines the maximum amount of
code produced per inlining attempt.
Small values for this parameter limit the amount of overall code expansion.
These parameters must be set to nonnegative fixnum values.
The parameter \scheme{cp0-outer-unroll-limit}
controls the amount of inlining performed by the optimizer for
recursive procedures.
With the parameter's value set to the default value of \scheme{0}, recursive
procedures are not inlined.
A nonzero value for the outer unroll limit allows calls external to
a recursive procedure to be inlined.
For example, the expression
\schemedisplay
(letrec ([fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1)))))])
(fact 10))
\endschemedisplay
\noindent
would be left unchanged with the outer unroll limit set to zero, but would
be converted into
\schemedisplay
(letrec ([fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1)))))])
(* 10 (fact 9)))
\endschemedisplay
\noindent
with the outer unroll limit set to one.
Interesting effects can be had by varying several of these parameters at
once.
For example, setting the
effort and outer unroll limits to large values and the score limit
to \scheme{1} has the effect of inlining even complex recursive procedures
whose values turn out to be constant at compile time without risking
any code expansion.
For example,
\schemedisplay
(letrec ([fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1)))))])
(fact 10))
\endschemedisplay
\noindent
would be reduced to \scheme{3628800}, but
\schemedisplay
(letrec ([fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1)))))])
(fact z))
\endschemedisplay
\noindent
would be left unchanged, although the optimizer may take a while to
reach this decision if the effort and outer unroll limits are large.
%----------------------------------------------------------------------------
\entryheader
\formdef{commonization-level}{\categorythreadparameter}{commonization-level}
\listlibraries
\endentryheader
After running the main source optimization pass (cp0) for the last time, the
compiler optionally runs a \emph{commonization} pass.
The pass commonizes the code for lambda expressions that have
identical structure by abstracting differences at certain leaves
of the program, namely constants, references to unassigned variables,
and references to primitives.
The parameter \scheme{commonization-level} controls whether commonization
is run and, if so, how aggressive it is.
Its value must be a nonnegative exact integer ranging from 0 through 9.
When the parameter is set to 0, the default, commonization is not run.
Otherwise, higher values result in more commonization.
Commonization can undo some of the effects of cp0's inlining, can
add run-time overhead, and can complicate debugging, particularly
at higher commonization levels, which is why it is disabled by
default.
On the other hand, for macros or other meta programs that can
generate large, mostly similar lambda expressions, enabling
commonization can result in significant savings in object-code size
and even reduce run-time overhead by making more efficient use of
instruction caches.
%----------------------------------------------------------------------------
\entryheader
\formdef{undefined-variable-warnings}{\categorythreadparameter}{undefined-variable-warnings}
\listlibraries
\endentryheader
When \scheme{undefined-variable-warnings} is set to \scheme{#t}, the
compiler issues a warning message whenever it cannot determine that
a variable bound by \scheme{letrec}, \scheme{letrec*}, or an internal
definition will not be referenced before it is defined.
The default value is \scheme{#f}.
Regardless of the setting of this parameter, the compiler inserts code
to check for the error, except at optimize level 3.
The check is fairly inexpensive and does not typically inhibit inlining
or other optimizations.
In code that must be carefully tuned, however, it is sometimes useful
to reorder bindings or make other changes to eliminate the checks.
Enabling undefined-variable warnings can facilitate this process.
The checks are also visible in the output of \scheme{expand/optimize}.
%----------------------------------------------------------------------------
\entryheader\label{desc:expand-output}\label{desc:expand/optimize-output}
\formdef{expand-output}{\categorythreadparameter}{expand-output}
\formdef{expand/optimize-output}{\categorythreadparameter}{expand/optimize-output}
\listlibraries
\endentryheader
The parameters \scheme{expand-output} and \scheme{expand/optimize-output}
can be used to request that the compiler and interpreter print
expander and source-optimizer output produced during the compilation or
interpretation process.
Each parameter must be set to either \scheme{#f} (the default) or a
textual output port.
When \scheme{expand-output} is set to a textual output port, the output
of the expander is printed to the port as a side effect of running
\scheme{compile}, \scheme{interpret}, or any of the file compiling
primitives, e.g., \scheme{compile-file} or \scheme{compile-library}.
Similarly, when \scheme{expand/optimize-output} is set to a textual
output port, the output of the source optimizer is printed.
\index{\scheme{expand}}\index{\scheme{expand/optimize}}%
See also \scheme{expand} (page~\pageref{desc:expand}) and
\scheme{expand-optimize} (page~\pageref{desc:expand/optimize}), which
can be used to run the expander or the expander and source optimizer
directly on an individual form.
%----------------------------------------------------------------------------
\entryheader
\formdef{pariah}{\categorysyntax}{(pariah \var{expr_1} \var{expr_2} \dots)}
\returns the values of the last subexpression
\listlibraries
\endentryheader
A \scheme{pariah} expression is just like a \scheme{begin} expression
except that it informs the compiler that the code is expected to
be executed infrequently.
The compiler uses this information to optimize code layout, register
assignments, and other aspects of the generated code.
The \scheme{pariah} form can be used in performance-critical code
to mark the branches of a conditional (e.g., \scheme{if}, \scheme{cond},
or \scheme{case}) that are less likely to be executed than the
others.
\section{Profiling\label{SECTMISCPROFILE}}
\index{profiling}\index{block profiling}\index{source profiling}%
{ChezScheme} supports two forms of profiling: source profiling and
block profiling.
With source profiling enabled, the compiler instruments the code
it produces to count the number of times each source-code expression
is executed.
This information can be
displayed in HTML format, or it can be packaged in a list or
source table for arbitrary user-defined processing.
It can also be dumped to a file to be loaded subsequently into the
compiler's database of profile information for use in source-level
optimizations, such as reordering the clauses of a \scheme{case}
or \scheme{exclusive-cond} form.
In connection with coverage-information (covin) files generated by the
compiler when
\index{\scheme{generate-covin-files}}\scheme{generate-covin-files}
is \scheme{#t}, profile information can also be used to gauge coverage
of a source-code base by a set of tests.
The association between source-code expressions and profile counts
is usually established via annotations produced by the reader and
present in the input to the expander (Section~\ref{SECTSYNTAXANNOTATIONS}).
It is also possible to explicitly identify source positions
to be assigned profile counts via \scheme{profile} expressions.
A \scheme{profile} expression has one subform, a source object, and
returns an unspecified value.
Its only effect is to cause the number of times the expression is
executed to be accounted to the source object.
In cases where source positions explicitly identified by \scheme{profile}
forms are the only ones whose execution counts should be tracked,
the parameter \scheme{generate-profile-forms} can be set to \scheme{#f}
to inhibit the expander's implicit generation of \scheme{profile} forms
for all annotated source expressions.
It is also possible to obtain finer control over implicit generation of
\scheme{profile} forms by marking which annotations that should and
should not be used for profiling (Section~\ref{SECTSYNTAXANNOTATIONS}).
With block profiling enabled, the compiler similarly instruments the
code it produces to count the number of times each ``basic block''
in the code it produces is executed.
Basic blocks are the building blocks of the code produced by many
compilers, including {\ChezScheme}'s compiler, and are sequences
of straight-line code entered only at the top and exited only at
the bottom.
Counting the number of times each basic block is executed is
equivalent to counting the number of times each instruction is
executed, but more efficient.
Block-profile information cannot be viewed, but it can be dumped
to a file to be loaded subsequently into the compiler's database of
profile information for use in block- and instruction-level
optimizations.
These optimizations include reordering blocks to push less frequently
used sequences of code out-of-line, so they will not occupy space
in the instruction cache, and giving registers to variables that are
used in more frequently executed instructions.
Source profiling involves at least the following steps:
\begin{itemize}
\item compile the code with source profiling enabled,
\item run the compiled code to generate source-profile information, and
\item dump the profile information.
\end{itemize}
\index{\scheme{compile-profile}}%
Source profiling is enabled by setting the parameter
\scheme{compile-profile} to the symbol \scheme{source}
or to the boolean value \scheme{#t}.
The profile information can be dumped via:
\begin{description}
\item[\scheme{profile-dump-html}]\index{\scheme{profile-dump-html}}
in HTML format to allow the programmer to visualize how
often each expression is executed using a color-coding system that
makes it easy to spot ``hot spots,''
\item[\scheme{profile-dump-list}]\index{\scheme{profile-dump-list}}
in a form suitable for user-defined post-processing,
\item[\scheme{profile-dump}]\index{\scheme{profile-dump}}
in a form suitable for off-line processing by one of the methods
above or by some custom means, or
\item[\scheme{profile-dump-data}]\index{\scheme{profile-dump-data}}
in a form suitable for loading into the compiler's database.
\end{description}
If the information is intended to be fed back into the compiler for
optimization, the following additional steps are required, either
in the same or a different Scheme process:
\begin{itemize}
\item load the profile information into the compiler's profile
database, and
\item recompile the code.
\end{itemize}
\index{\scheme{profile-load-data}}%
Profile information dumped by \scheme{profile-dump-data} is loaded
into the compiler's profile database via \scheme{profile-load-data}.
Profiling information is \emph{not} available to the compiler unless
it is explicitly dumped via \scheme{profile-dump-data} and loaded
via \scheme{profile-load-data}.
When block-profile information is to be used for optimization,
the steps are similar:
\begin{itemize}
\item compile the code with block profiling enabled,
\item run the code to generate block-profile information,
\item dump the profile information,
\item load the profile information, and
\item recompile the code.
\end{itemize}
\index{\scheme{profile-dump-data}}%
\index{\scheme{profile-load-data}}%
Block profiling is enabled by setting the parameter
\scheme{compile-profile} to the symbol \scheme{block}
or to the boolean value \scheme{#t}.
The profile information must be dumped via \scheme{profile-dump-data}
and loaded via \scheme{profile-load-data}.
As with source profile information, block profile information can be
loaded in the same or in a different Scheme process as the one that
dumped the information.
For block optimization, the code to be recompiled must be identical.
In general, this means the files involved must not have been modified,
and nothing else can change that indirectly affects the code produced
by the compiler, e.g., settings for compiler parameters such as
\scheme{optimize-level} or the contents of configuration files read
by macros at compile time.
Otherwise, the set of blocks or the instructions within them might
be different, in which case the block profile information will not
line up properly and the compiler will raise an exception.
For the same reason, when both source profiling and block profiling
information is to be used for optimization, the source information
must be gathered first and loaded before both the first and second
compilation runs involved in block profiling.
That is, the following steps must be used:
\begin{itemize}
\item[1] compile the code with source profiling enabled,
\item[2] run the code to generate source-profile information,
\item[2] dump the source-profile information,
\item[3] load the source-profile information,
\item[3] recompile the code with block profiling enabled,
\item[4] run the code to generate block-profile information,
\item[4] dump the block-profile information,
\item[5] load the source- and block-profile information, and
\item[5] recompile the code.
\end{itemize}
The numbers labeling each step indicate both the order of the steps
and those that must be performed in the same Scheme process.
(All of the steps can be performed in the same Scheme process, if
desired.)
Both source and block profiling are disabled when \scheme{compile-profile}
is set to \scheme{#f}, its default value.
The following example highlights the use of source profiling for
identifying hot spots in the code.
Let's assume that the file /tmp/fatfib/fatfib.ss contains the
following source code.
\schemedisplay
(define fat+
(lambda (x y)
(if (zero? y)
x
(fat+ (1+ x) (1- y)))))
(define fatfib
(lambda (x)
(if (< x 2)
1
(fat+ (fatfib (1- x)) (fatfib (1- (1- x)))))))
\endschemedisplay
We can load fatfib.ss with profiling enabled as follows.
\schemedisplay
(parameterize ([compile-profile 'source])
(load "/tmp/fatfib/fatfib.ss"))
\endschemedisplay
We then run the application as usual.
\schemedisplay
(fatfib 20) ;=> 10946
\endschemedisplay
After the run (or multiple runs), we
dump the profile information as a set of html files using
\scheme{profile-dump-html}.
\schemedisplay
(profile-dump-html)
\endschemedisplay
This creates a file named profile.html containing a summary of the profile
information gathered during the run.
If we view this file in a browser, we should see something like the
following.
\iflatex
\begin{center}
\includegraphics[width=.9\textwidth]{canned/profilehtml}
\end{center}
\fi
\ifhtml
\raw{\raw{<img src="canned/profilehtml.png" alt="profile.html listing">}}
\fi
The most frequently executed code is highlighted in colors closer to
red in the visible spectrum, while
the least frequently executed code is highlighted in colors closer to
violet.
Each of the entries in the lists of files and hot spots are links into
additional generated files, one per source file (provided
\scheme{profile-dump-html} was able to locate an unmodified copy of
the source file).
In this case, there is only one, fatfib.ss.html.
If we move to that file, we should see something like this:
\iflatex
\begin{center}
\includegraphics[width=.9\textwidth]{canned/fatfibhtml}
\end{center}
\fi
\ifhtml
\raw{\raw{<img src="canned/fatfibhtml.png" alt="fatfib.html listing">}}
\fi
As in the summary, the code is color-coded according to frequency
of execution.
Hovering over a color-coded section of code should cause a pop-up
box to appear with the starting position and count of the source
expression.
If a portion of source code is not color-coded or is identified
via the starting position as having inherited its color from some
enclosing expression, it may have been recognized as dead code by
the compiler or garbage collector and discarded, or the expander
might not have been able to track it through the macro-expansion
process.
\scheme{profile-dump} and \scheme{profile-dump-list} may be used to
generate a list of profile entries, which may then be analyzed manually
or via a custom profile-viewing application.
%----------------------------------------------------------------------------
\entryheader
\formdef{compile-profile}{\categorythreadparameter}{compile-profile}
\listlibraries
\endentryheader
When this parameter is set to the symbol \scheme{source} or the
boolean value \scheme{#t}, the compiler instruments the code it
generates with instructions that count the number of times each
section of source code is executed.
When set to the symbol \scheme{block}, the compiler similarly
instruments the code it generates with instructions that count the
number of times each block of code is executed.
When set to \scheme{#f} (the default), the compiler does not insert
these instructions.
The general description of profiling above describes how the source
and block profile information can be viewed or used for optimization.
The code generated when \scheme{compile-profile} is non-false is
larger and less efficient, so this parameter should be set only
when profile information is needed.
The profile counters for code compiled when profile instrumentation
is enabled are retained indefinitely, even if the code with which
they are associated is reclaimed by the garbage collector.
This results in more complete and accurate profile data but can lead
to space leaks in programs that dynamically generate or load code.
Such programs can avoid the potential space leak by releasing the
counters explicitly via the procedure
\index{\scheme{profile-release-counters}}\scheme{profile-release-counters}.
%----------------------------------------------------------------------------
\entryheader
\formdef{generate-covin-files}{\categorythreadparameter}{generate-covin-files}
\listlibraries
\endentryheader
When this parameter is set to \scheme{#t}, the compiler generates
``coverage-information'' (covin) files that can be used in connection with
profile information to measure coverage of a source-code base by a
set of tests.
One covin file is created for each object file, with the object-file
extension replaced by the extension \scheme{.covin}.
Each covin file contains the printed representation of a source table
(Section~\ref{SECTSYNTAXSOURCETABLES}), compressed using the compression
format and level specified by \scheme{compress-format} and
\scheme{compress-level}.
This information can be read via
\index{\scheme{get-source-table!}}\scheme{get-source-table!} and used
as a universe of source expressions to identify source expressions
that are not evaluated during the running of a set of tests.
\entryheader
\formdef{profile}{\categorysyntax}{(profile \var{source-object})}
\returns unspecified
\listlibraries
\endentryheader
A \scheme{profile} form has the effect of accounting to the source
position identified by \var{source-object} the number of times the
\scheme{profile} form is executed.
Profile forms are generated implicitly by the expander for source
expressions in annotated input, e.g., input read by the compiler or
interpreter from a Scheme source file, so this form is typically
useful only when unannotated source code is produced by the front
end for some language that targets Scheme.
\entryheader
\formdef{generate-profile-forms}{\categorythreadparameter}{(generate-profile-forms)}
\listlibraries
\endentryheader
When this parameter is set to \scheme{#t}, the default, the expander
implicitly introduces \scheme{profile} forms for each annotated input
expression, unless the annotation has not been marked for use in
profiling (Section~\ref{SECTSYNTAXANNOTATIONS}).
It can be set to \scheme{#f} to inhibit the expander's implicit
generation of \scheme{profile} forms, typically when explicit
\scheme{profile} forms are already present for all source positions
that should be profiled.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-clear}{\categoryprocedure}{(profile-clear)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
Calling this procedure causes profile information to be cleared, i.e.,
the counts associated with each section of code are set to zero.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-release-counters}{\categoryprocedure}{(profile-release-counters)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
Calling this procedure causes profile information associated with reclaimed
code objects to be dropped.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-dump}{\categoryprocedure}{(profile-dump)}
\returns a list of pairs of source-object and count
\listlibraries
\endentryheader
This procedure produces a dump of all
profile information gathered since startup or the last call to
\scheme{profile-clear}.
It returns a list of pairs, where the car of each pair
is a source object (Section~\ref{SECTSYNTAXANNOTATIONS}) and the
cdr is an exact nonnegative integer count.
The list might contain more than one entry per source object due
to macro expansion and procedure inlining, and it might contain
more than one (non-eq) source object per file and source position
due to separate compilation.
In such cases, the counts are not overlapping and can be summed
together to obtain the full count.
The advantage of \scheme{profile-dump} over \scheme{profile-dump-list}
is that \scheme{profile-dump} performs only minimal processing and
preserves complete source objects, including their embedded source-file
descriptors.
It might be used, for example, to dump profile information to a
fasl file on one machine for subsequent processing on another.
\index{\scheme{with-profile-tracker}}\scheme{with-profile-tracker}
can be used to obtain the same set of counts as a source table.
%----------------------------------------------------------------------------
\entryheader
\formdef{with-profile-tracker}{\categoryprocedure}{(with-profile-tracker \var{thunk})}
\formdef{with-profile-tracker}{\categoryprocedure}{(with-profile-tracker \var{preserve-existing?} \var{thunk})}
\returns a source table and the values returned by \var{thunk}
\listlibraries
\endentryheader
\var{thunk} must be a procedure and should accept zero arguments.
It may return any number of values.
\scheme{with-profile-tracker} invokes \var{thunk} without arguments.
If \var{thunk} returns $n$ values \scheme{\var{x_1}, \var{x_2}, \dots, \var{x_n}}, \scheme{with-profile-tracker}
returns $n+1$ values \scheme{\var{st}, \var{x_1}, \var{x_2}, \dots, \var{x_n}}, where \var{st} is a
source table associating source objects with profile counts.
If \var{preserve-existing?} is absent or \scheme{#f}, each count
represents the number of times the source expression represented
by the associated source object is evaluated during the invocation
of \var{thunk}.
Otherwise, each count represents the number of times the source
expression represented by the associated source object is evaluated
before or during the invocation of \var{thunk}.
Profile data otherwise cleared by a call to
\index{\scheme{profile-clear}}\scheme{profile-clear} or
\index{\scheme{profile-release-counters}}\scheme{profile-release-counters}
during the invocation of \var{thunk} is included in the
resulting table.
That is, invoking these procedures while \var{thunk} is running has
no effect on the resulting counts.
On the other hand, profile data cleared before \scheme{with-profile-tracker}
is invoked is not included in the resulting table.
The idiom \scheme{(with-profile-tracker #t values)} can be used to obtain
the current set of profile counts as a source table.
%----------------------------------------------------------------------------
\entryheader
\formdef{source-table-dump}{\categoryprocedure}{(source-table-dump \var{source-table})}
\returns a list of pairs of source objects and their associated values in \var{source-table}
\listlibraries
\endentryheader
This procedure can be used to convert a source-table produced by
\index{\scheme{with-profile-tracker}}\scheme{with-profile-tracker} or some other mechanism into the form returned
by \index{\scheme{profile-dump}}\scheme{profile-dump} for use as an argument to
\index{\scheme{profile-dump-html}}\scheme{profile-dump-html},
\index{\scheme{profile-dump-list}}\scheme{profile-dump-list},
or
\index{\scheme{profile-dump-data}}\scheme{profile-dump-data}.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-dump-html}{\categoryprocedure}{(profile-dump-html)}
\formdef{profile-dump-html}{\categoryprocedure}{(profile-dump-html \var{prefix})}
\formdef{profile-dump-html}{\categoryprocedure}{(profile-dump-html \var{prefix} \var{dump})}
\returns unspecified
\listlibraries
\endentryheader
This procedure produces one or more HTML files, including
profile.html, which contains color-coded summary information,
and one file \var{source}.html for each source
file \var{source} containing a color-coded copy of the
source code, as described in the lead-in to this section.
If \var{prefix} is specified, it must be a string and is prepended
to the names of the generated HTML files.
For example, if \var{prefix} is \scheme{"/tmp/"}, the generated
files are placed in the directory /tmp.
The raw profile information is obtained from \var{dump}, which
defaults to the value returned by \scheme{profile-dump}.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-palette}{\categorythreadparameter}{(profile-palette)}
\listlibraries
\endentryheader
This value of this parameter must be a nonempty vector of at least
three pairs.
The car of each pair is a background color and the cdr is a foreground
(text) color.
Each color must be a string, and each string should contain an HTML
cascading style sheet (CSS) color specifier.
The first pair is used for unprofiled code, and the second is used
for unexecuted profiled code.
The third is used for code that is executed least frequently, the fourth
for code executed next-least frequently, and so on, with the last
being used for code that is executed most frequently.
Programmers may wish to supply their own palette to enhance visibility
or to change the number of colors used.
By default, a black background is used for unprofiled code, and a gray
background is used for unexecuted profiled code.
Background colors ranging from purple to red are used for executed
profiled code, depending on frequency of execution, with red for the most
frequently executed code.
\schemedisplay
(profile-palette) ;=>
#(("#111111" . "white") ("#607D8B" . "white")
("#9C27B0" . "black") ("#673AB7" . "white")
("#3F51B5" . "white") ("#2196F3" . "black")
("#00BCD4" . "black") ("#4CAF50" . "black")
("#CDDC39" . "black") ("#FFEB3B" . "black")
("#FFC107" . "black") ("#FF9800" . "black")
("#F44336" . "white"))
(profile-palette
; set palette with rainbow colors and black text
; for all but unprofiled or unexecuted code
'#(("#000000" . "white") ; black
("#666666" . "white") ; gray
("#8B00FF" . "black") ; violet
("#6600FF" . "black") ; indigo
("#0000FF" . "black") ; blue
("#00FF00" . "black") ; green
("#FFFF00" . "black") ; yellow
("#FF7F00" . "black") ; orange
("#FF0000" . "black"))) ; red
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-line-number-color}{\categorythreadparameter}{(profile-line-number-color)}
\listlibraries
\endentryheader
This value of this parameter must be a string or \scheme{#f}.
If it is a string, the string should contain an HTML cascading style sheet (CSS)
color specifier.
If the parameter is set to a string, \scheme{profile-dump-html} includes line numbers
in its html rendering of each source file, using the specified color.
If the parameter is set to \scheme{#f}, no line numbers are included.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-dump-list}{\categoryprocedure}{(profile-dump-list)}
\formdef{profile-dump-list}{\categoryprocedure}{(profile-dump-list \var{warn?})}
\formdef{profile-dump-list}{\categoryprocedure}{(profile-dump-list \var{warn?} \var{dump})}
\returns a list of profile entries (see below)
\listlibraries
\endentryheader
This procedure produces a dump of all
profile information present in \var{dump}, which defaults to
the value returned by \scheme{profile-dump}.
It returns a list of entries, each of which is itself a list containing the
following elements identifying one block of code and how many times it
has been executed.
\begin{itemize}
\item execution count
\item pathname
\item beginning file position in characters (inclusive)
\item ending file position in characters (exclusive)
\item line number of beginning file position
\item character position of beginning file position
\end{itemize}
\scheme{profile-dump-list} may be unable to locate an unmodified copy
of the file in the current source directories
or at the absolute address, if an absolute address was used when
the file was compiled or loaded.
If this happens, the line number and character position of the beginning
file position are \scheme{#f} and the pathname is the pathname originally
used.
A warning is also issued (an exception with condition type
\scheme{&warning} is raised) unless the \scheme{warn?} argument is provided
and is false.
Otherwise, the pathname is the path to an unmodified copy of the source
and the line and character positions are set to exact nonnegative integers.
In either case, the execution count, beginning file position, and ending
file position are all exact nonnegative integers, and the pathname is a string.
For source positions in files that cannot be found, the list might
contain more than one entry per position due to macro expansion,
procedure inlining, and separate compilation.
In such cases, the counts are not overlapping and can be summed
together to obtain the full count.
The information returned by \scheme{profile-dump-list} can be used to
implement a custom viewer or used as input for offline analysis of
profile information.
The advantage of \scheme{profile-dump-list} over \scheme{profile-dump}
is that it attempts to determine the line number and character
position for each source point and, if successful, aggregates
multiple counts for the source point into a single entry.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-dump-data}{\categoryprocedure}{(profile-dump-data \var{path})}
\formdef{profile-dump-data}{\categoryprocedure}{(profile-dump-data \var{path} \var{dump})}
\returns unspecified
\listlibraries
\endentryheader
\var{path} must be a string.
This procedure writes, in a machine-readable form consumable by
\scheme{profile-load-data}, profile counts represented by \var{dump}
to the file named by \var{path}, replacing the file if it already exists.
\var{dump} defaults to the value returned by \scheme{profile-dump}.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-load-data}{\categoryprocedure}{(profile-load-data \var{path} \dots)}
\returns unspecified
\listlibraries
\endentryheader
Each \var{path} must be a string.
This procedure reads profile information from the files named by
\scheme{\var{path} \dots} and stores it in the compiler's internal
database of profile information.
The contents of the files must have been created originally by
\scheme{profile-dump-data} using the same version of {\ChezScheme}.
The database stores a weight for each source expression or block
rather than the actual count.
When a single file is loaded into the database, the weight is the
proportion of the actual count over the maximum count for all
expressions or blocks represented in the file.
When more than one file is loaded, either by one or multiple calls
to \scheme{profile-load-data}, the weights are averaged.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-query-weight}{\categoryprocedure}{(profile-query-weight \var{obj})}
\returns \var{obj}'s profile weight, or \scheme{#f} if \var{obj} is not in the database
\listlibraries
\endentryheader
The compiler's profile database maps source objects
(Section~\ref{SECTSYNTAXANNOTATIONS}) to weights.
If \var{obj} is a source object, the \scheme{profile-query-weight} returns
the weight associated with the source object or \scheme{#f} if the database
does not have a weight recorded for the source object.
\var{obj} can also be an annotation or syntax object, in which case
\scheme{profile-query-weight} first extracts the source object, if any,
using \scheme{syntax->annotation} and \scheme{annotation-source},
returning \scheme{#f} if no source-object is found.
A weight is a flonum in the range 0.0 to 1.0, inclusive, and denotes the
ratio of the actual count to the maximum count as described in the
description of \scheme{profile-load-data}.
\scheme{profile-query-weight} can be used by a macro to determine
the relative frequency with which its subexpressions were executed
in the run or runs that generated the information in the database.
This information can be used to guide the generation of code that
is likely to be more efficient.
For example, the \scheme{case} macro uses profile information, when
available, to order the clauses so that those whose keys matched
more frequently are tested before those whose keys matched less
frequently.
%----------------------------------------------------------------------------
\entryheader
\formdef{profile-clear-database}{\categoryprocedure}{(profile-clear-database)}
\returns unspecified
\listlibraries
\endentryheader
This procedure clears the compiler's profile database.
It has no impact on the counts associated with individual sections
of instrumented code; \scheme{profile-clear} can be used to reset
those counts.
\section{Waiter Customization\label{SECTMISCWAITERS}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{new-cafe}{\categoryprocedure}{(new-cafe)}
\formdef{new-cafe}{\categoryprocedure}{(new-cafe \var{eval-proc})}
\returns see below
\listlibraries
\endnoskipentryheader
\noindent
\index{waiter}\index{cafe@caf\'e}{\ChezScheme} interacts with the user
through a \emph{waiter}, or read-eval-print loop (REPL).
The waiter operates within a context called a \emph{caf\'e}.
When the system starts up, the user is placed in a caf\'e and
given a waiter.
\scheme{new-cafe} opens a new Scheme caf\'e, stacked on top of the old one.
In addition to starting the waiter, \scheme{new-cafe} sets up the caf\'e's
reset and exit handlers (see \scheme{reset-handler} and \scheme{exit-handler}).
Exiting a caf\'e resumes the continuation of the call
to \scheme{new-cafe} that created the caf\'e.
Exiting from the initial caf\'e leaves Scheme altogether.
A caf\'e may be exited from either by an explicit call to \scheme{exit} or
by receipt of end-of-file (``control-D'' on Unix systems) in response
to the waiter's prompt.
In the former case, any values passed to \scheme{exit} are returned from
\scheme{new-cafe}.
If the optional \var{eval-proc} argument is specified, \var{eval-proc}
is used to evaluate forms entered from the console.
Otherwise, the value of the parameter \scheme{current-eval} is used.
\var{eval-proc} must accept one argument, the expression to evaluate.
Interesting values for \var{eval-proc} include \index{\scheme{expand}}\scheme{expand},
which causes the macro expanded value of each expression entered to
be printed and \scheme{(lambda (x) x)}, which simply causes each expression
entered to be printed.
An arbitrary procedure of one argument may be used to facilitate
testing of a program on a series of input values.
\schemedisplay
> (new-cafe (lambda (x) x))
>> 3
3
>> (a . (b . (c . ())))
(a b c)
\endschemedisplay
\schemedisplay
(define sum
(lambda (ls)
(if (null? ls)
0
(+ (car ls) (sum (cdr ls))))))
> (new-cafe sum)
>> (1 2 3)
6
\endschemedisplay
The default waiter reader (see \scheme{waiter-prompt-and-read}) displays
the current waiter prompt (see \scheme{waiter-prompt-string})
to the current value of \index{\scheme{console-output-port}}\scheme{console-output-port} and
reads
from the current value of \index{\scheme{console-input-port}}\scheme{console-input-port}.
The default waiter printer (see \scheme{waiter-write}) sends output
to the current value of \index{\scheme{console-output-port}}\scheme{console-output-port}.
These parameters, along with \scheme{current-eval},
can be modified to change the behavior of the waiter.
%----------------------------------------------------------------------------
\entryheader
\formdef{waiter-prompt-string}{\categorythreadparameter}{waiter-prompt-string}
\listlibraries
\endentryheader
\noindent
The value of \scheme{waiter-prompt-string} must be a string.
It is used by the default waiter prompter (see the parameter
\scheme{waiter-prompt-and-read}) to print a prompt.
Nested caf\'es
are marked by repeating the prompt string once for each nesting level.
\schemedisplay
> (waiter-prompt-string)
">"
> (waiter-prompt-string "%")
% (waiter-prompt-string)
"%"
% (new-cafe)
%% (waiter-prompt-string)
"%"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{waiter-prompt-and-read}{\categorythreadparameter}{waiter-prompt-and-read}
\listlibraries
\endentryheader
\noindent
\scheme{waiter-prompt-and-read} must be set to a procedure.
It is used by the waiter to
print a prompt and read an expression.
The value of \scheme{waiter-prompt-and-read} is called by the waiter with a
positive integer that indicates the caf\'e nesting level.
It should return an expression to be evaluated by the current
evaluator (see \scheme{new-cafe} and \scheme{current-eval}).
%----------------------------------------------------------------------------
\entryheader
\formdef{default-prompt-and-read}{\categoryprocedure}{(default-prompt-and-read \var{level})}
\listlibraries
\endentryheader
\var{level} must be a positive integer indicating the cafe\'e nesting
level as described above.
This procedure is the default value of the \scheme{waiter-prompt-and-read}
parameter whenever the expression editor
(Section~\ref{SECTUSEEXPEDITOR}, Chapter~\ref{CHPTEXPEDITOR}) is
\emph{not} enabled.
It might be defined as follows.
\schemedisplay
(define default-prompt-and-read
(lambda (n)
(unless (and (integer? n) (>= n 0))
(assertion-violationf 'default-prompt-and-read
"~s is not a nonnegative integer"
n))
(let ([prompt (waiter-prompt-string)])
(unless (string=? prompt "")
(do ([n n (- n 1)])
((= n 0)
(write-char #\space (console-output-port))
(flush-output-port (console-output-port)))
(display prompt (console-output-port))))
(let ([x (read (console-input-port))])
(when (and (eof-object? x) (not (string=? prompt "")))
(newline (console-output-port))
(flush-output-port (console-output-port)))
x))))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{waiter-write}{\categorythreadparameter}{waiter-write}
\listlibraries
\endentryheader
\noindent
The value of \scheme{waiter-write} must be a procedure.
The waiter uses the value of \scheme{waiter-write} to print the results
of each expression read and evaluated by the waiter.
The following example installs a procedure equivalent to the default
\scheme{waiter-write}:
\schemedisplay
(waiter-write
(lambda (x)
(unless (eq? x (void))
(pretty-print x (console-output-port)))
(flush-output-port (console-output-port))))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{reset}{\categoryprocedure}{(reset)}
\returns does not return
\listlibraries
\endentryheader
\noindent
\scheme{reset} invokes the current reset handler (see \scheme{reset-handler})
without arguments.
%----------------------------------------------------------------------------
\entryheader
\formdef{reset-handler}{\categorythreadparameter}{reset-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure and should accept zero
arguments.
The current reset handler is called by \scheme{reset}.
The default reset handler resets to the current caf\'e.
%----------------------------------------------------------------------------
\entryheader
\formdef{exit}{\categoryprocedure}{(exit \var{obj} \dots)}
\returns does not return
\listlibraries
\endentryheader
\noindent
\scheme{exit} invokes the current exit handler (see
\scheme{exit-handler}), passing along its arguments, if any.
%----------------------------------------------------------------------------
\entryheader
\formdef{exit-handler}{\categorythreadparameter}{exit-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure and should accept any
number of arguments.
The current exit handler is called by \scheme{exit}.
The default exit handler exits from the current caf\'e,
returning its arguments as the values of the call to
\scheme{new-cafe} that created the current caf\'e.
If the current caf\'e is the original caf\'e, or if \scheme{exit}
is called from a script, \scheme{exit} exits from Scheme.
In this case, the exit code for the Scheme process is 0 if
no arguments were supplied or if the first argument is void,
the value of the first argument cast to a C int if
it is an exact integer of the host machine's bit width, and 1 otherwise.
%----------------------------------------------------------------------------
\entryheader
\formdef{abort}{\categoryprocedure}{(abort)}
\formdef{abort}{\categoryprocedure}{(abort \var{obj})}
\returns does not return
\listlibraries
\endentryheader
\noindent
\scheme{abort} invokes the current abort handler (see \scheme{abort-handler}),
passing along its argument, if any.
%----------------------------------------------------------------------------
\entryheader
\formdef{abort-handler}{\categorythreadparameter}{abort-handler}
\listlibraries
\endentryheader
\noindent
The value of this parameter must be a procedure and should accept either
zero arguments or one argument.
The current abort handler is called by \scheme{abort}.
The default abort handler exits the Scheme process.
The exit code for the Scheme process is -1 if no arguments were supplied,
0 if the first argument is void, the value of the first argument if it is
a 32-bit exact integer, and -1 otherwise.
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-start}{\categoryglobalparameter}{scheme-start}
\listlibraries
\endentryheader
\noindent
The value of \scheme{scheme-start} is a procedure that determines the
system's action upon start-up.
The procedure receives zero or more arguments, which are strings
representing the file names (or command-line arguments not recognized
by the Scheme executable) after given on the command line.
The default value first loads the files named by the arguments, then
starts up the initial caf\'e:
\schemedisplay
(lambda fns
(for-each load fns)
(new-cafe))
\endschemedisplay
\noindent
\scheme{scheme-start} may be altered to start up an application or to
perform customization prior to normal system start-up.
To have any effect, this parameter must be set within a boot file.
(See Chapter~\ref{CHPTUSE}.)
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-script}{\categoryglobalparameter}{scheme-script}
\listlibraries
\endentryheader
\noindent
\index{\scheme{--script} command-line option}%
\index{\scheme{command-line}}%
\index{\scheme{command-line-arguments}}%
The value of \scheme{scheme-script} is a procedure that determines the
system's action upon start-up,
when the \scheme{--script} option is used.
The procedure receives one or more arguments.
The first is a string identifying the script filename and the remainder
are strings representing the remaining file names (or command-line
arguments not recognized by the Scheme executable) given on the command
line.
The default value of this parameter is a procedure that sets the
\scheme{command-line} and \scheme{command-line-arguments} parameters,
loads the script using \scheme{load}, and returns void, which is
translated into a 0 exit status for the script process.
\schemedisplay
(lambda (fn . fns)
(command-line (cons fn fns))
(command-line-arguments fns)
(load fn))
\endschemedisplay
\noindent
\scheme{scheme-script} may be altered to start up an application or to
perform customization prior to normal system start-up.
To have any effect, this parameter must be set within a boot file.
(See Chapter~\ref{CHPTUSE}.)
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-program}{\categoryglobalparameter}{scheme-program}
\listlibraries
\endentryheader
\noindent
\index{\scheme{--program} command-line option}%
\index{\scheme{command-line}}%
\index{\scheme{command-line-arguments}}%
The value of \scheme{scheme-program} is a procedure that determines the
system's action upon start-up
when the \scheme{--program} (RNRS top-level program) option is used.
The procedure receives one or more arguments.
The first is a string identifying the program filename and the remainder
are strings representing the remaining file names (or command-line
arguments not recognized by the Scheme executable) given on the command
line.
The default value of this parameter is a procedure that sets the
\scheme{command-line} and \scheme{command-line-arguments} parameters,
loads the program using \scheme{load-program}, and returns void, which is
translated into a 0 exit status for the script process.
\schemedisplay
(lambda (fn . fns)
(command-line (cons fn fns))
(command-line-arguments fns)
(load-program fn))
\endschemedisplay
\noindent
\scheme{scheme-program} may be altered to start up an application or to
perform customization prior to normal system start-up.
To have any effect, this parameter must be set within a boot file.
(See Chapter~\ref{CHPTUSE}.)
%----------------------------------------------------------------------------
\entryheader
\formdef{command-line}{\categoryglobalparameter}{command-line}
\listlibraries
\endentryheader
\index{\scheme{--script} command-line option}%
This parameter is set by the default values of \scheme{scheme-script}
and \scheme{scheme-program}
to a list representing the command line, with the script name followed
by the command-line arguments, when the \scheme{--script} or
\scheme{--program} option is used on system startup.
%----------------------------------------------------------------------------
\entryheader
\formdef{command-line-arguments}{\categoryglobalparameter}{command-line-arguments}
\listlibraries
\endentryheader
\index{\scheme{--script} command-line option}%
This parameter is set by the default values of \scheme{scheme-script}
and \scheme{scheme-program}
to a list of the command-line arguments when the \scheme{--script}
or \scheme{--program} option is used on system startup.
%----------------------------------------------------------------------------
\entryheader
\formdef{suppress-greeting}{\categoryglobalparameter}{suppress-greeting}
\listlibraries
\endentryheader
\noindent
The value of \scheme{suppress-greeting} is a boolean value that determines
whether {\ChezScheme} prints an identifying banner and copyright notice.
The parameter defaults to \scheme{#f} but may be set to \scheme{#t} for
use in batch processing applications where the banner would be disruptive.
To have any effect, this parameter must be set within a boot file.
(See Chapter~\ref{CHPTUSE}.)
\section{Transcript Files\label{SECTMISCTRANSCRIPTS}}
A \index{transcript}transcript file is a record of an interactive session.
It is also useful as a ``quick-and-dirty'' alternative to opening an
output file and using explicit output operations.
%----------------------------------------------------------------------------
\entryheader\label{desc:transcript-on}
\formdef{transcript-on}{\categoryprocedure}{(transcript-on \var{path})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{transcript-on} opens the file named by \var{path} for output,
and it copies to this file all input from the current input port and
all output to the current output port.
An exception is raised with condition-type \scheme{i/o-filename} if the
file cannot be opened for output.
%----------------------------------------------------------------------------
\entryheader
\formdef{transcript-off}{\categoryprocedure}{(transcript-off)}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\scheme{transcript-off} ends transcription and closes the transcript file.
%----------------------------------------------------------------------------
\entryheader
\formdef{transcript-cafe}{\categoryprocedure}{(transcript-cafe \var{path})}
\listlibraries
\endentryheader
\noindent
\var{path} must be a string.
\scheme{transcript-cafe} opens a transcript file as with
\scheme{transcript-on} and
enters a new caf\'e; exiting
from this caf\'e (see \scheme{exit}) also ends transcription and closes the
transcript file.
Invoking \scheme{transcript-off} while in a transcript caf\'e ends transcription
and closes the transcript file but does not cause an exit from the
caf\'e.
\section{Times and Dates\label{SECTSYSTEMTIMESNDATES}}
This section documents procedures for handling times and dates. Most of
the procedures described here are proposed in
\hyperlink{http://srfi.schemers.org/srfi-19/srfi-19.html}{SRFI~19}:
Time Data Types and Procedures, by Will Fitzgerald.
Times are represented by time objects.
Time objects record the nanosecond and second of a particular time
or duration, along with a \emph{time type} that identifies the nature
of the time object.
The time type is one of the following symbols:
\begin{description}
\item[\scheme{time-utc}:]
The time elapsed since the ``epoch:'' 00:00:00 UTC, January 1, 1970,
subject to adjustment, e.g., to correct for leap seconds.
\item[\scheme{time-monotonic}:]
The time elapsed since some arbitrary point in the past, ideally
not subject to adjustment.
\item[\scheme{time-duration}:]
The time elapsed between two times.
When used as an argument to \scheme{current-time}, it behaves like
\scheme{time-monotonic}, but may also used to represent the result
of subtracting two time objects.
\item[\scheme{time-process}:]
The amount of CPU time used by the current process.
\item[\scheme{time-thread}:]
The amount of CPU time used by the current thread.
It is the same as \scheme{time-process} if
not running threaded or if the system does not allow individual
thread times to be determined.
\item[\scheme{time-collector-cpu}:]
The portion of the current process's CPU time consumed by the
garbage collector.
\item[\scheme{time-collector-real}:]
The portion of the current process's real time consumed by the
garbage collector.
\end{description}
A time-object second is an exact integer (possibly negative),
and a nanosecond is an exact nonnegative integer less than $10^9$.
The second and nanosecond of a time object may be converted to
an aggregate nanosecond value by scaling the
seconds by $10^9$ and adding the nanoseconds.
Thus, if the second and nanosecond of a time object are 5 and 10,
the time object represents 5000000010 nanoseconds (5.000000010 seconds).
If the second and nanosecond are -5 and 10, the time object
represents -4999999990 nanoseconds (-4.999999990 seconds).
Dates are represented by date objects.
A date object records the nanosecond, second, minute, hour, day, month,
and year of a particular date, along with an offset that identifies the
time zone.
As for time objects, a nanosecond is an exact integer less than $10^9$.
A date-object second is, however, an exact nonnegative integer
less than 62.
(The values 61 and 62 allow for leap seconds.)
A minute is an exact nonnegative integer less than 60, and
an hour is an exact nonnegative integer less than 24.
A day is an exact nonnegative integer in ranging from 1 representing
the first day of the month to $n$, where $n$ is the number of
days in the date's month and year.
A month is an exact nonnegative integer ranging from 1 through 12,
where 1 represents January, 2 represents February, and so on.
A year must be an exact integer.
Years less than 1970 or greater than 2038 may not be supported
depending on limitations of the underlying implementation.
A time-zone offset represents the time-zone offset, in seconds, from UTC.
It is an exact integer in the range $-86400$ to $+86400$, inclusive.
For example, Eastern Standard Time (EST), which is 5 hours east, has
offset $5\times 3600 = -18000$.
The offset for Eastern Daylight Time (EDT) is $-14400$.
UTC is represented by offset zero.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-time}{\categoryprocedure}{(current-time)}
\formdef{current-time}{\categoryprocedure}{(current-time \var{time-type})}
\returns a time object representing the current time
\listlibraries
\endentryheader
\var{time-type} must be one of the time-type symbols listed above
and defaults to \scheme{time-utc}.
\schemedisplay
(current-time) ;=> #<time-utc 1198815722.473668000>
(current-time 'time-process) ;=> #<time-process 0.120534264>
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{make-time}{\categoryprocedure}{(make-time \var{type} \var{nsec} \var{sec})}
\returns a time object
\listlibraries
\endentryheader
\var{type} must be one of the time-type symbols listed above.
\var{nsec} represents nanoseconds and must be an exact nonnegative
integer less than $10^9$.
\var{sec} represents seconds and must be an exact integer.
\schemedisplay
(make-time 'time-utc 787511000 1198783214)
(make-time 'time-duration 10 5)
(make-time 'time-duration 10 -5)
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{time?}{\categoryprocedure}{(time? \var{obj})}
\returns \scheme{#t} if \var{obj} is a time object, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noskip\schemedisplay
(time? (current-time)) ;=> #t
(time? (make-time 'time-utc 0 0)) ;=> #t
(time? "1400 hours") ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{time-type}{\categoryprocedure}{(time-type \var{time})}
\returns the time type of \var{time}
\formdef{time-nanosecond}{\categoryprocedure}{(time-nanosecond \var{time})}
\returns the nanosecond of \var{time}
\formdef{time-second}{\categoryprocedure}{(time-second \var{time})}
\returns the second of \var{time}
\listlibraries
\endentryheader
\var{time} must be a time object.
\schemedisplay
(time-type (current-time)) ;=> time-utc
(time-type (current-time 'time-process)) ;=> time-process
(time-type (make-time 'time-duration 0 50)) ;=> time-duration
(time-second (current-time)) ;=> 1198816497
(time-nanosecond (current-time)) ;=> 2399000
(time-second (make-time 'time-duration 10 -5)) ;=> -5
(time-nanosecond (make-time 'time-duration 10 -5)) ;=> 10
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{set-time-type!}{\categoryprocedure}{(set-time-type! \var{time} \var{type})}
\returns unspecified
\formdef{set-time-nanosecond!}{\categoryprocedure}{(set-time-nanosecond! \var{time} \var{nsec})}
\returns unspecified
\formdef{set-time-second!}{\categoryprocedure}{(set-time-second! \var{time} \var{sec})}
\returns unspecified
\listlibraries
\endentryheader
\var{time} must be a time object.
\var{type} must be one of the time-type symbols listed above.
\var{nsec} represents nanoseconds and must be an exact nonnegative
integer less than $10^9$.
\var{sec} represents seconds and must be an exact integer.
Each of these procedures modifies the time object, changing one aspect
while leaving the others unaffected.
For example, \scheme{set-time-nanosecond!} changes the nanosecond of
\var{time} without changing the second or type.
In particular, no conversion of values is performed when the type of a time
object is changed.
%----------------------------------------------------------------------------
\entryheader
\formdef{time=?}{\categoryprocedure}{(time=? \var{time_1} \var{time_2})}
\formdef{time<?}{\categoryprocedure}{(time<? \var{time_1} \var{time_2})}
\formdef{time<=?}{\categoryprocedure}{(time<=? \var{time_1} \var{time_2})}
\formdef{time>=?}{\categoryprocedure}{(time>=? \var{time_1} \var{time_2})}
\formdef{time>?}{\categoryprocedure}{(time>? \var{time_1} \var{time_2})}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endentryheader
\var{time_1} and \var{time_2} must be time objects and must have
the same type.
\schemedisplay
(let ([t (current-time)])
(time=? t t)) ;=> #t
(let ([t (current-time)])
(let loop ()
(when (time=? (current-time) t))
(loop))
(time>? (current-time) t)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{copy-time}{\categoryprocedure}{(copy-time \var{time})}
\returns a copy of \var{time}
\listlibraries
\endentryheader
\schemedisplay
(define t1 (current-time))
(define t2 (copy-time t1))
(eq? t2 t1) ;=> #f
(eqv? (time-second t2) (time-second t1)) ;=> #t
(eqv? (time-nanosecond t2) (time-nanosecond t1)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{time-difference}{\categoryprocedure}{(time-difference \var{time_1} \var{time_2})}
\returns the result of subtracting \var{time_2} from \var{time_1}
\formdef{time-difference!}{\categoryprocedure}{(time-difference! \var{time_1} \var{time_2})}
\returns the result of subtracting \var{time_2} from \var{time_1}
\formdef{add-duration}{\categoryprocedure}{(add-duration \var{time} \var{time_d})}
\returns the result of adding \var{time_d} to \scheme{time}
\formdef{add-duration!}{\categoryprocedure}{(add-duration! \var{time} \var{time_d})}
\returns the result of adding \var{time_d} to \scheme{time}
\formdef{subtract-duration}{\categoryprocedure}{(subtract-duration \var{time} \var{time_d})}
\returns the result of subtracting \var{time_d} from \scheme{time}
\formdef{subtract-duration!}{\categoryprocedure}{(subtract-duration! \var{time} \var{time_d})}
\returns the result of subtracting \var{time_d} from \scheme{time}
\listlibraries
\endentryheader
For \scheme{time-difference}, \var{time_1} and \var{time_2} must
have the same time type, and the result is a time object with
time type \scheme{time-duration}.
For \scheme{add-duration}, \scheme{add-duration!},
\scheme{subtract-duration}, and \scheme{subtract-duration!},
\var{time_d} must have time type \scheme{time-duration},
and the result is a time object with the same time type as
\var{time}.
\scheme{time-difference!}, \scheme{add-duration!}, and
\scheme{subtract-duration!} are potentially destructive, i.e., each
might modify and return its first argument, or it might allocate a
new time object.
\schemedisplay
(let ([delay (make-time 'time-duration 0 1)])
(let ([t1 (current-time 'time-monotonic)])
(sleep delay)
(let ([t2 (current-time 'time-monotonic)])
(let ([t3 (time-difference t2 t1)])
(and
(eq? (time-type t3) 'time-duration)
(time>=? t3 delay)
(time=? (add-duration t1 t3) t2)
(time=? (subtract-duration t2 t3) t1)))))) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{current-date}{\categoryprocedure}{(current-date)}
\formdef{current-date}{\categoryprocedure}{(current-date \var{offset})}
\returns a date object representing the current date
\listlibraries
\endentryheader
\var{offset} represents the time-zone offset in seconds east of UTC,
as described above.
It must be an exact integer in the range $-86400$ to
$+86400$, inclusive and defaults to the local time-zone offset.
UTC may be obtained by passing an offset of zero.
If \var{offset} is not provided, then the current time zone's offset
is used, and \scheme{date-dst?} and \scheme{date-zone-name} report
information about the time zone. If \var{offset} is provided, then
\scheme{date-dst?} and \scheme{date-zone-name} on the resulting date
object produce \scheme{#f}.
The following examples assume the local time zone is EST.
\schemedisplay
(current-date) ;=> #<date Thu Dec 27 23:23:20 2007>
(current-date 0) ;=> #<date Fri Dec 28 04:23:20 2007>
(date-zone-name (current-date)) ;=> "EST" \var{or other system-provided string}
(date-zone-name (current-date 0)) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{make-date}{\categoryprocedure}{(make-date \var{nsec} \var{sec} \var{min} \var{hour} \var{day} \var{mon} \var{year})}
\formdef{make-date}{\categoryprocedure}{(make-date \var{nsec} \var{sec} \var{min} \var{hour} \var{day} \var{mon} \var{year} \var{offset})}
\returns a date object
\listlibraries
\endentryheader
\var{nsec} represents nanoseconds and must be an exact nonnegative integer
less than $10^9$.
\var{sec} represents seconds and must be an exact nonnegative integer
less than 62.
\var{min} represents minutes and must be an exact nonnegative integer
less than 60.
\var{hour} must be an exact nonnegative integer less than 24.
\var{day} must be an exact integer, $1\leq day\leq 31$.
(The actual upper limit may be less depending on the month and year.)
\var{mon} represents the month must be an exact integer, $1\leq mon\leq 12$.
\var{year} must be an exact integer.
It should be at least 1970.
\var{offset} represents the time-zone offset in seconds east of UTC,
as described above.
It must be an exact integer in the range $-86400$ to $+86400$, inclusive.
UTC may be specified by passing an offset of zero.
If \var{offset} is not provided, then the current time zone's offset
is used, and \scheme{date-dst?} and \scheme{date-zone-name} report
information about the time zone. If \var{offset} is provided, then
\scheme{date-dst?} and \scheme{date-zone-name} on the resulting date
object produce \scheme{#f}.
\schemedisplay
(make-date 0 0 0 0 1 1 1970 0) ;=> #<date Thu Jan 1 00:00:00 1970>
(make-date 0 30 7 9 23 9 2007 -14400) ;=> #<date Sun Sep 23 09:07:30 2007>
(date-zone-name (make-date 0 30 7 9 23 9 2007 -14400)) ;=> #f
(string? (date-zone-name (make-date 0 30 7 9 23 9 2007))) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{date?}{\categoryprocedure}{(date? \var{obj})}
\returns \scheme{#t} if \var{obj} is a date object, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noskip\schemedisplay
(date? (current-date))
(date? (make-date 0 30 7 9 23 9 2007 -14400))
(date? "Sun Sep 23 09:07:30 2007") ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{date-nanosecond}{\categoryprocedure}{(date-nanosecond \var{date})}
\returns the nanosecond of \var{date}
\formdef{date-second}{\categoryprocedure}{(date-second \var{date})}
\returns the second of \var{date}
\formdef{date-minute}{\categoryprocedure}{(date-minute \var{date})}
\returns the minute of \var{date}
\formdef{date-hour}{\categoryprocedure}{(date-hour \var{date})}
\returns the hour of \var{date}
\formdef{date-day}{\categoryprocedure}{(date-day \var{date})}
\returns the day of \var{date}
\formdef{date-month}{\categoryprocedure}{(date-month \var{date})}
\returns the month of \var{date}
\formdef{date-year}{\categoryprocedure}{(date-year \var{date})}
\returns the year of \var{date}
\formdef{date-zone-offset}{\categoryprocedure}{(date-zone-offset \var{date})}
\returns the time-zone offset of \var{date}
\listlibraries
\endentryheader
\var{date} must be a date object.
\schemedisplay
(define d (make-date 0 30 7 9 23 9 2007 -14400))
(date-nanosecond d) ;=> 0
(date-second d) ;=> 30
(date-minute d) ;=> 7
(date-hour d) ;=> 9
(date-day d) ;=> 23
(date-month d) ;=> 9
(date-year d) ;=> 2007
(date-zone-offset d) ;=> -14400
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{date-week-day}{\categoryprocedure}{(date-week-day \var{date})}
\returns the week-day of \var{date}
\formdef{date-year-day}{\categoryprocedure}{(date-year-day \var{date})}
\returns the year-day of \var{date}
\listlibraries
\endentryheader
These procedures allow the day-of-week or day-of-year to be determined for
the date represented by \var{date}.
A week-day is an exact nonnegative integer less than 7, where
0 represents Sunday, 1 represents Monday, and so on.
A year-day is an exact nonnegative integer less than 367, where
0 represents the first day of the year (January 1), 1 the
second day, 2 the third, and so on.
\schemedisplay
(define d1 (make-date 0 0 0 0 1 1 1970 -18000))
d1 ;=> #<date Thu Jan 1 00:00:00 1970>
(date-week-day d1) ;=> 4
(date-year-day d1) ;=> 0
(define d2 (make-date 0 30 7 9 23 9 2007 -14400))
d2 ;=> #<date Sun Sep 23 09:07:30 2007>
(date-week-day d2) ;=> 0
(date-year-day d2) ;=> 265
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{date-dst?}{\categoryprocedure}{(date-dst? \var{date})}
\returns whether \var{date} is in Daylight Saving Time
\formdef{date-zone-name}{\categoryprocedure}{(date-zone-name \var{date})}
\returns \scheme{#f} or a string naming the time zone of \var{date}
\listlibraries
\endentryheader
These procedures report time-zone information for
the date represented by \var{date} for a date object that
is constructed without an explicit time-zone offset. When
a date object is created instead with explicit time-zone offset,
these procedures produce \scheme{#f}.
Daylight Saving Time status for the current time zone and a name
string for the time zone are computed using platform-specific routines.
In particular, the format of the zone name is platform-specific.
\schemedisplay
(define d (make-date 0 30 7 9 23 9 2007))
(date-zone-offset d) ;=> -14400 \var{assuming Eastern U.S. time zone}
(date-dst? d) ;=> #t
(date-zone-name d) ;=> "EDT" \var{or some system-provided string}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{time-utc->date}{\categoryprocedure}{(time-utc->date \var{time})}
\formdef{time-utc->date}{\categoryprocedure}{(time-utc->date \var{time} \var{offset})}
\returns a date object corresponding to \var{time}
\formdef{date->time-utc}{\categoryprocedure}{(date->time-utc \var{date})}
\returns a time object corresponding to \var{date}
\listlibraries
\endnoskipentryheader
These procedures are used to convert between time and date objects.
The \var{time} argument to \scheme{time-utc->date} must have time-type
\scheme{utc}, and \scheme{date->time-utc} always returns a time
object with time-type \scheme{utc}.
For \scheme{time-utc->date},
\var{offset} represents the time-zone offset in seconds east of UTC,
as described at the beginning of this section.
It must be an exact integer in the range $-86400$ to
$+86400$, inclusive and defaults to the local time-zone offset.
UTC may be obtained by passing an offset of zero.
If \var{offset} is not provided to \scheme{time-utc->date}, then the current time zone's offset
is used, and \scheme{date-dst?} and \scheme{date-zone-name} report
information about the time zone. If \var{offset} is provided, then
\scheme{date-dst?} and \scheme{date-zone-name} on the resulting date
object produce \scheme{#f}.
\schemedisplay
(define d (make-date 0 30 7 9 23 9 2007 -14400))
(date->time-utc d) ;=> #<time-utc 1190552850.000000000>
(define t (make-time 'time-utc 0 1190552850))
(time-utc->date t) ;=> #<date Sun Sep 23 09:07:30 2007>
(time-utc->date t 0) ;=> #<date Sun Sep 23 13:07:30 2007>
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{date-and-time}{\categoryprocedure}{(date-and-time)}
\formdef{date-and-time}{\categoryprocedure}{(date-and-time \var{date})}
\returns a string giving the current date and time
\listlibraries
\endnoskipentryheader
The string is always in the format illustrated by the examples below and
always has length 24.
\schemedisplay
(date-and-time) ;=> "Fri Jul 13 13:13:13 2001"
(define d (make-date 0 0 0 0 1 1 2007 0))
(date-and-time d) ;=> "Mon Jan 01 00:00:00 2007"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{sleep}{\categoryprocedure}{(sleep \var{time})}
\returns unspecified
\listlibraries
\endnoskipentryheader
\var{time} must be a time object with type \scheme{time-duration}.
\var{sleep} causes the invoking thread to suspend operation for
approximately the amount of time indicated by the time object, unless
the process receives a signal that interrupts the sleep operation.
The actual time slept depends on the granularity of the system clock
and how busy the system is running other threads and processes.
\section{Timing and Statistics\label{SECTMISCSTATISTICS}}
This section documents procedures for timing computations.
The \scheme{current-time} procedure described in
Section~\ref{SECTSYSTEMTIMESNDATES} may also be used to
time computations.
%----------------------------------------------------------------------------
\entryheader
\formdef{time}{\categorysyntax}{(time \var{expr})}
\returns the values of \var{expr}
\listlibraries
\endnoskipentryheader
\noindent
\scheme{time} evaluates \var{expr} and, as a side-effect, prints (to the
console-output port) the amount of cpu time, the amount of real time,
the number of bytes allocated, and the amount of collection overhead
associated with evaluating \var{expr}.
\schemedisplay
> (time (collect))
(time (collect))
1 collection
1 ms elapsed cpu time, including 1 ms collecting
1 ms elapsed real time, including 1 ms collecting
160 bytes allocated, including 8184 bytes reclaimed
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{display-statistics}{\categoryprocedure}{(display-statistics)}
\formdef{display-statistics}{\categoryprocedure}{(display-statistics \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
This procedure displays a running total of the amount of
cpu time, real time, bytes allocated, and collection overhead.
If \var{textual-output-port} is not supplied, it defaults to the current output port.
%----------------------------------------------------------------------------
\entryheader
\formdef{cpu-time}{\categoryprocedure}{(cpu-time)}
\returns the amount of cpu time consumed since system start-up
\listlibraries
\endentryheader
\noindent
The amount is in milliseconds.
The amount includes ``system'' as well as ``user'' time, i.e., time
spent in the kernel on behalf of the process as well as time spent in
the process itself.
See also \scheme{current-time}, which returns more precise information.
%----------------------------------------------------------------------------
\entryheader
\formdef{real-time}{\categoryprocedure}{(real-time)}
\returns the amount of real time that has elapsed since system start-up
\listlibraries
\endentryheader
\noindent
The amount is in milliseconds.
See also \scheme{current-time}, which returns more precise information.
%----------------------------------------------------------------------------
\entryheader
\formdef{bytes-allocated}{\categoryprocedure}{(bytes-allocated)}
\formdef{bytes-allocated}{\categoryprocedure}{(bytes-allocated \var{g})}
\returns the number of bytes currently allocated
\listlibraries
\endentryheader
If \var{g} is supplied, \scheme{bytes-allocated} returns the number of
bytes currently allocated for Scheme objects in the specified generation.
\var{g} must be a nonnegative exact integer no greater than the
maximum nonstatic generation, i.e., the
value returned by \scheme{collect-maximum-generation}, or the symbol
\scheme{static}.
If \var{g} is not supplied, \scheme{bytes-allocated} returns the total
number of bytes allocated in all generations.
%----------------------------------------------------------------------------
\entryheader
\formdef{initial-bytes-allocated}{\categoryprocedure}{(initial-bytes-allocated)}
\returns the total number of bytes allocated after loading boot files
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{bytes-deallocated}{\categoryprocedure}{(bytes-deallocated)}
\returns the total number of bytes deallocated by the garbage collector
\listlibraries
\endentryheader
The total number of bytes allocated by the current process, whether
still in use or not, can be obtained by summing
\scheme{(bytes-deallocated)} and \scheme{(bytes-allocated)}
and possibly subtracting \scheme{(initial-bytes-allocated)}.
%----------------------------------------------------------------------------
\entryheader
\formdef{current-memory-bytes}{\categoryprocedure}{(current-memory-bytes)}
\returns the total number of bytes currently allocated, including overhead
\listlibraries
\endentryheader
\scheme{current-memory-bytes} returns the total size of the heap
in bytes, including not only the bytes occupied for Scheme objects
but also various forms of overhead, including fragmentation and
reserved but not currently occupied memory, and is thus an accurate
measure of the amount of heap memory currently reserved from the
operating system for the current process.
%----------------------------------------------------------------------------
\entryheader
\formdef{maximum-memory-bytes}{\categoryprocedure}{(maximum-memory-bytes)}
\returns the maximum number of bytes ever allocated, including overhead
\listlibraries
\endentryheader
\scheme{maximum-memory-bytes} returns the maximum size of the heap
in bytes, i.e., the maximum value that \scheme{current-memory-bytes}
returned or could have returned, since the last call to
\scheme{reset-maximum-memory-bytes!} or, if there has been no such
call, since the process started.
%----------------------------------------------------------------------------
\entryheader
\formdef{reset-maximum-memory-bytes!}{\categoryprocedure}{(reset-maximum-memory-bytes!)}
\returns unspecified
\listlibraries
\endentryheader
\scheme{reset-maximum-memory-bytes!} resets the maximum recorded size
of the heap to the current size of the heap.
%----------------------------------------------------------------------------
\entryheader
\formdef{collections}{\categoryprocedure}{(collections)}
\returns the number garbage collections so far
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{statistics}{\categoryprocedure}{(statistics)}
\returns a sstats record containing current statistics
\listlibraries
\endentryheader
\noindent
\scheme{statistics} packages together various timing and allocation
statistics into a single \scheme{sstats} record.
A \scheme{sstats} record has the following fields:
\begin{description}
\item[\scheme{cpu},] the cpu time consumed,
\item[\scheme{real},] the elapsed real time,
\item[\scheme{bytes},] the number of bytes allocated,
\item[\scheme{gc-count},] the number of collections,
\item[\scheme{gc-cpu},] the cpu time consumed during collections,
\item[\scheme{gc-real},] the elapsed real time during collections, and
\item[\scheme{gc-bytes},] the number of bytes reclaimed by the collector.
\end{description}
\noindent
All values are computed since system start-up.
The time values are time objects (Section~\ref{SECTSYSTEMTIMESNDATES}),
and the bytes and count values are exact integers.
\scheme{statistics} might be defined as follows:
\schemedisplay
(define statistics
(lambda ()
(make-sstats
(current-time 'time-thread)
(current-time 'time-monotonic)
(- (+ (bytes-allocated) (bytes-deallocated))
(initial-bytes-allocated))
(collections)
(current-time 'time-collector-cpu)
(current-time 'time-collector-real)
(bytes-deallocated))))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{make-sstats}{\categoryprocedure}{(make-sstats \var{cpu} \var{real} \var{bytes} \var{gc-count} \var{gc-cpu} \var{gc-real} \var{gc-bytes})}
\returns a sstats record
\listlibraries
\endentryheader
The time arguments (\var{cpu}, \var{real}, \var{gc-cpu}, and \var{gc-real}) must be time objects.
The other arguments must be exact integers.
%----------------------------------------------------------------------------
\entryheader
\formdef{sstats?}{\categoryprocedure}{(sstats? \var{obj})}
\returns \scheme{#t} if \var{obj} is a sstats record, otherwise \scheme{#f}
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{sstats-cpu}{\categoryprocedure}{(sstats-cpu \var{s})}
\formdef{sstats-real}{\categoryprocedure}{(sstats-real \var{s})}
\formdef{sstats-bytes}{\categoryprocedure}{(sstats-bytes \var{s})}
\formdef{sstats-gc-count}{\categoryprocedure}{(sstats-gc-count \var{s})}
\formdef{sstats-gc-cpu}{\categoryprocedure}{(sstats-gc-cpu \var{s})}
\formdef{sstats-gc-real}{\categoryprocedure}{(sstats-gc-real \var{s})}
\formdef{sstats-gc-bytes}{\categoryprocedure}{(sstats-gc-bytes \var{s})}
\returns the value of the corresponding field of \var{s}
\listlibraries
\endentryheader
\noindent
\var{s} must be a sstats record.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-sstats-cpu!}{\categoryprocedure}{(set-sstats-cpu! \var{s} \var{new-value})}
\formdef{set-sstats-real!}{\categoryprocedure}{(set-sstats-real! \var{s} \var{new-value})}
\formdef{set-sstats-bytes!}{\categoryprocedure}{(set-sstats-bytes! \var{s} \var{new-value})}
\formdef{set-sstats-gc-count!}{\categoryprocedure}{(set-sstats-gc-count! \var{s} \var{new-value})}
\formdef{set-sstats-gc-cpu!}{\categoryprocedure}{(set-sstats-gc-cpu! \var{s} \var{new-value})}
\formdef{set-sstats-gc-real!}{\categoryprocedure}{(set-sstats-gc-real! \var{s} \var{new-value})}
\formdef{set-sstats-gc-bytes!}{\categoryprocedure}{(set-sstats-gc-bytes! \var{s} \var{new-value})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{s} must be a sstats record, the \var{new-value} arguments for the time fields
(\var{cpu}, \var{real}, \var{gc-cpu}, and \var{gc-real})
must be time objects, and
the other \var{new-value} arguments must be exact integers.
Each procedure sets the value of the corresponding field of \var{s} to
\var{new-value}.
%----------------------------------------------------------------------------
\entryheader
\formdef{sstats-difference}{\categoryprocedure}{(sstats-difference \var{s_1} \var{s_2})}
\returns a sstats record representing the difference between \var{s_1} and \var{s_2}
\listlibraries
\endentryheader
\noindent
\var{s_1} and \var{s_2} must be sstats records.
\scheme{sstats-difference} subtracts each field of \var{s_2} from the
corresponding field of \var{s_1} to produce the resulting \scheme{sstats}
record.
%----------------------------------------------------------------------------
\entryheader
\formdef{sstats-print}{\categoryprocedure}{(sstats-print \var{s})}
\formdef{sstats-print}{\categoryprocedure}{(sstats-print \var{s} \var{textual-output-port})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{s} must be a \scheme{sstats} record.
If \var{textual-output-port} is not supplied, it defaults to the current output port.
\scheme{sstats-print} displays the fields of \scheme{s} in a manner similar
to \scheme{display-statistics} and \scheme{time}.
%----------------------------------------------------------------------------
\entryheader
\formdef{enable-object-counts}{\categoryglobalparameter}{enable-object-counts}
\listlibraries
\endentryheader
The value of \scheme{enable-object-counts} is a boolean value that
determines whether the collector records object counts as it runs and
hence whether the object counts returned by the procedure
\scheme{object-counts} are accurate.
The parameter is set to \scheme{#f} by default, since enabling object
counts adds overhead to collection.
Counts for the static generation are always correct.
Counts for a nonstatic generation $n$ are correct immediately after a
collection of generation $m\ge n$ (regardless of whether the target
generation is $m$ or $m+1$) if \scheme{enable-object-counts}
was set to \scheme{#t} during the collection.
One strategy for collecting object counts with minimal overhead is
to enable object counts only while collecting the maximum nonstatic
generation and to obtain the object counts immediately after that
collection.
\entryheader
\formdef{object-counts}{\categoryprocedure}{(object-counts)}
\returns see below
\listlibraries
\endentryheader
The procedure \scheme{object-counts} returns a nested association list
representing object counts and bytes allocated for each heap-allocated
primitive type and record type with at least one live instance in one
or more generations.
(Heap-allocated primitive types include, e.g., pairs and vectors, but
not, e.g., fixnums or characters.)
Object counts are gathered by the collector only when
\scheme{enable-object-counts} is \scheme{#t}.
The description of \scheme{enable-object-counts} details the
circumstances under which the counts are accurate.
The association list returned by \scheme{object-counts} has the following
structure:
\schemedisplay
((\var{type} (\var{generation} \var{count} . \var{bytes}) \dots) \dots)
\endschemedisplay
\var{type} is either the name of a primitive type, represented as a
symbol, e.g., \scheme{pair}, or a record-type descriptor (rtd).
\var{generation} is a nonnegative fixnum between 0 and the value
of \scheme{(collect-maximum-generation)}, inclusive, or the symbol
\scheme{static} representing the static generation.
\var{count} and \var{bytes} are nonnegative fixnums.
\schemedisplay
(collect-request-handler void)
(enable-object-counts #t)
(define-record-type frob (fields x))
(define x (make-frob (make-frob #f)))
(collect 3 3)
(cdr (assoc 3
(cdr (assoc (record-type-descriptor frob)
(object-counts))))) ;=> (2 . 16)
\endschemedisplay
\section{Cost Centers\label{SECTMISCCOSTCENTERS}}
Cost centers are used to track the bytes allocated, instructions executed,
and/or cpu time elapsed while evaluating selected sections of code.
Cost centers are created via the procedure \scheme{make-cost-center}, and
costs are tracked via the procedure \scheme{with-cost-center}.
Allocation and instruction counts are tracked only for code instrumented
for that purpose.
This instrumentation is controlled by two parameters: \scheme{generate-allocation-counts}
and \scheme{generate-instruction-counts}.
Instrumentation is disabled by default.
Built in procedures are not instrumented, nor is interpreted code or
non-Scheme code.
Elapsed time is tracked only when the optional \scheme{timed?} argument to
\scheme{with-cost-center} is provided and is not false.
The \scheme{with-cost-center} procedure accurately tracks costs, subject
to the caveats above, even when reentered with the same cost center, used
simultaneously in multiple threads, and exited or reentered one or more
times via continuation invocation.
%----------------------------------------------------------------------------
\entryheader
\formdef{generate-allocation-counts}{\categorythreadparameter}{generate-allocation-counts}
\listlibraries
\endnoskipentryheader
When this parameter has a true value, the compiler inserts a short sequence of
instructions at each allocation point in generated code to track the amount of
allocation that occurs.
This parameter is initially false.
%----------------------------------------------------------------------------
\entryheader
\formdef{generate-instruction-counts}{\categorythreadparameter}{generate-instruction-counts}
\listlibraries
\endnoskipentryheader
When this parameter has a true value, the compiler inserts a short
sequence of instructions in each block of generated code to track the
number of instructions executed by that block.
This parameter is initially false.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-cost-center}{\categoryprocedure}{(make-cost-center)}
\returns a new cost center
\listlibraries
\endentryheader
The recorded costs of the new cost center are initialized to zero.
%----------------------------------------------------------------------------
\entryheader
\formdef{cost-center?}{\categoryprocedure}{(cost-center? \var{obj})}
\returns \scheme{#t} if \var{obj} is a cost center, otherwise \scheme{#f}
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{with-cost-center}{\categoryprocedure}{(with-cost-center \var{cost-center} \var{thunk})}
\formdef{with-cost-center}{\categoryprocedure}{(with-cost-center \var{timed?} \var{cost-center} \var{thunk})}
\returns see below
\listlibraries
\endentryheader
\var{thunk} must be a procedure that accepts zero arguments.
\scheme{with-cost-center} invokes \var{thunk} without arguments and
returns its values.
It also tracks, dynamically, the bytes allocated, instructions executed,
and cpu time elapsed while evaluating the invocation of \var{thunk} and
adds the tracked costs to the cost center's running record of these costs.
As described above, allocation counts are tracked only for code
compiled with the parameter \scheme{generate-allocation-counts} set
to true, and instruction counts are tracked only for code compiled
with \scheme{generate-instruction-counts} set to true.
Cpu time is tracked only if \var{timed?} is provided and not false and
includes cpu time spent in instrumented, uninstrumented, and non-Scheme
code.
%----------------------------------------------------------------------------
\entryheader
\formdef{cost-center-instruction-count}{\categoryprocedure}{(cost-center-instruction-count \var{cost-center})}
\returns the number of instructions tracked by \var{cost-center}
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{cost-center-allocation-count}{\categoryprocedure}{(cost-center-allocation-count \var{cost-center})}
\returns the number of allocated bytes tracked by \var{cost-center}
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{cost-center-time}{\categoryprocedure}{(cost-center-time \var{cost-center})}
\returns the cpu time tracked by \var{cost-center}
\listlibraries
\endentryheader
The cpu time is returned as a time object with time-type \scheme{time-duration}.
%----------------------------------------------------------------------------
\entryheader
\formdef{reset-cost-center!}{\categoryprocedure}{(reset-cost-center! \var{cost-center})}
\returns unspecified
\listlibraries
\endentryheader
This procedure resets the costs recorded by \var{cost-center} to zero.
\section{Parameters\label{SECTPARAMETERS}}
This section describes mechanisms for creating and manipulating parameters.
New parameters may be created conveniently with \scheme{make-parameter}.
Nothing distinguishes parameters from other
procedures, however, except for their behavior.
If more complicated actions must be taken when a parameter is invoked
than can be accommodated easily through the \scheme{make-parameter} mechanism,
the parameter may be defined directly with \scheme{case-lambda}.
%----------------------------------------------------------------------------
\entryheader
\formdef{make-parameter}{\categoryprocedure}{(make-parameter \var{object})}
\formdef{make-parameter}{\categoryprocedure}{(make-parameter \var{object} \var{procedure})}
\returns a parameter (procedure)
\listlibraries
\endentryheader
\noindent
\scheme{make-parameter} accepts one or two arguments.
The first argument is the initial value of the internal variable, and
the second, if present, is a \emph{filter} applied to the initial value
and all subsequent values.
The filter should accept one argument.
If the value is not appropriate, the filter should raise an exception or
convert the value into a more appropriate form.
For example, the default value of \scheme{print-length} is defined as
follows:
\schemedisplay
(define print-length
(make-parameter
#f
(lambda (x)
(unless (or (not x) (and (fixnum? x) (fx>= x 0)))
(assertion-violationf 'print-length
"~s is not a positive fixnum or #f"
x))
x)))
\endschemedisplay
\schemedisplay
(print-length) ;=> #f
(print-length 3)
(print-length) ;=> 3
(format "~s" '(1 2 3 4 5 6)) ;=> "(1 2 3 ...)"
(print-length #f)
(format "~s" '(1 2 3 4 5 6)) ;=> "(1 2 3 4 5 6)"
\endschemedisplay
The definition of \scheme{make-parameter} is straightforward using
\index{\scheme{case-lambda}}\scheme{case-lambda}:
\schemedisplay
(define make-parameter
(case-lambda
[(init guard)
(let ([v (guard init)])
(case-lambda
[() v]
[(u) (set! v (guard u))]))]
[(init)
(make-parameter init (lambda (x) x))]))
\endschemedisplay
In threaded versions of {\ChezScheme}, \scheme{make-parameter} creates
global parameters.
The procedure \scheme{make-thread-parameter}, described in
Section~\ref{SECTTHREADPARAMETERS}, may be used to make thread
parameters.
%----------------------------------------------------------------------------
\entryheader
\formdef{parameterize}{\categorysyntax}{(parameterize ((\var{param} \var{expr}) \dots) \var{body_1} \var{body_2} \dots)}
\returns the values of the body \scheme{\var{body_1} \var{body_2} \dots}
\listlibraries
\endentryheader
\noindent
Using the syntactic form \scheme{parameterize}, the values of parameters can
be changed in a manner analogous to \scheme{fluid-let} for ordinary variables.
Each \var{param} is set to the value of the corresponding
\var{expr} while the body is evaluated.
When control leaves the body by normal return or by the invocation of a
continuation created outside of the body, the parameters are restored to
their original values.
If control returns to the body via a continuation created during the
execution of the body, the parameters are again set to their temporary
values.
\schemedisplay
(define test
(make-parameter 0))
(test) ;=> 0
(test 1)
(test) ;=> 1
(parameterize ([test 2])
(test)) ;=> 2
(test) ;=> 1
(parameterize ([test 2])
(test 3)
(test)) ;=> 3
(test) ;=> 1
(define k (lambda (x) x))
(begin (set! k (call/cc k))
'k) ;=> k
(parameterize ([test 2])
(test (call/cc k))
(test)) ;=> k
(test) ;=> 1
(k 3) ;=> 3
(test) ;=> 1
\endschemedisplay
The definition of \scheme{parameterize} is similar to the definition of
\scheme{fluid-let} (page~\pageref{defn:fluid-let}):
\schemedisplay
(define-syntax parameterize
(lambda (x)
(syntax-case x ()
[(_ () b1 b2 ...) #'(begin b1 b2 ...)]
[(_ ((x e) ...) b1 b2 ...)
(with-syntax ([(p ...) (generate-temporaries #'(x ...))]
[(y ...) (generate-temporaries #'(x ...))])
#'(let ([p x] ... [y e] ...)
(let ([swap (lambda ()
(let ([t (p)]) (p y) (set! y t))
...)])
(dynamic-wind swap (lambda () b1 b2 ...) swap))))])))
\endschemedisplay
\section{Virtual registers\label{SECTVIRTUALREGISTERS}}
A limited set of \emph{virtual registers} is supported by the compiler
for use by programs that require high-speed, global, and mutable storage
locations.
Referencing or assigning a virtual register is potentially faster and
never slower than accessing an assignable local or global variable,
and the code sequences for doing so are generally smaller.
Assignment is potentially significantly faster because there is no need
to track pointers from the virtual registers to young objects, as there
is for variable locations that might reside in older generations.
On threaded versions of the system, virtual registers are ``per thread''
and thus serve as thread-local storage in a manner that is less expensive
than thread parameters.
The interface consists of three procedures: \scheme{virtual-register-count},
which returns the number of virtual registers, \scheme{set-virtual-register!},
which sets the value of a specified virtual register, and
\scheme{virtual-register}, which retrieves the value of a specified
virtual register.
A virtual register is specified by a nonnegative fixnum index less than
the number of virtual registers.
To get optimal performance for \scheme{set-virtual-register!}
and \scheme{virtual-register}, the index should be a constant
embedded right in the call (or propagatable via optimization to the
call).
To avoid putting these constants in the source code, programmers should
consider using identifier macros to give names to virtual registers, e.g.:
\schemedisplay
(define-syntax current-state
(identifier-syntax
[id (virtual-register 0)]
[(set! id e) (set-virtual-register! 0 e)]))
(set! current-state 'start)
current-state ;=> start
\endschemedisplay
A more elaborate macro could dole out indices at compile time and complain
when no more indices are available.
Virtual-registers must be treated as an application-level resource, i.e.,
libraries intended to be used by multiple applications should generally
not use virtual registers to avoid conflicts with an application's use of
the registers.
%----------------------------------------------------------------------------
\entryheader
\formdef{virtual-register-count}{\categoryprocedure}{(virtual-register-count)}
\returns the number of virtual registers
\listlibraries
\endentryheader
As of Version~9.0, the number of virtual registers is set at 16.
It cannot be changed except by recompiling {\ChezScheme} from
source.
%----------------------------------------------------------------------------
\entryheader
\formdef{set-virtual-register!}{\categoryprocedure}{(set-virtual-register! \var{k} \var{x})}
\returns unspecified
\listlibraries
\endentryheader
\scheme{set-virtual-register!} stores \var{x} in virtual register \var{k}.
\var{k} must be a nonnegative fixnum less than the value of
\scheme{(virtual-register-count)}.
%----------------------------------------------------------------------------
\entryheader
\formdef{virtual-register}{\categoryprocedure}{(virtual-register \var{k})}
\returns see below
\listlibraries
\endentryheader
\scheme{virtual-register} returns the value most recently
stored in virtual register \var{k} (on the current thread, in
threaded versions of the system).
\section{Environmental Queries and Settings\label{SECTSYSTEMENV}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{scheme-version}{\categoryprocedure}{(scheme-version)}
\returns a version string
\listlibraries
\endentryheader
The version string is in the form
\schemedisplay
"Chez Scheme Version \var{version}"
\endschemedisplay
for {\ChezScheme}, and
\schemedisplay
"Petite Chez Scheme Version \var{version}"
\endschemedisplay
for {\PetiteChezScheme}.
%----------------------------------------------------------------------------
\entryheader
\formdef{scheme-version-number}{\categoryprocedure}{(scheme-version-number)}
\returns three values: the major, minor, and sub-minor version numbers
\listlibraries
\endentryheader
Each of the three return values is a nonnegative fixnum.
In {\ChezScheme} Version 7.9.4:
\schemedisplay
(scheme-version-number) ;=> 7
;== 9
;== 4
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{petite?}{\categoryprocedure}{(petite?)}
\returns \scheme{#t} if called in {\PetiteChezScheme}, \scheme{#f} otherwise
\listlibraries
\endentryheader
The only difference between {\PetiteChezScheme} and {\ChezScheme} is that
the compiler is not available in the former, so this predicate can serve as
a way to determine if the compiler is available.
%----------------------------------------------------------------------------
\entryheader
\formdef{threaded?}{\categoryprocedure}{(threaded?)}
\returns \scheme{#t} if called in a threaded version of the system, \scheme{#f} otherwise
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\entryheader
\formdef{interactive?}{\categoryprocedure}{(interactive?)}
\returns \scheme{#t} if system is run interactively, \scheme{#f} otherwise
\listlibraries
\endentryheader
This predicate returns \scheme{#t} if the Scheme process's
stdin and stdout are connected to a tty (Unix-based systems) or console
(Windows).
Otherwise, it returns \scheme{#f}.
%----------------------------------------------------------------------------
\entryheader
\formdef{get-process-id}{\categoryprocedure}{(get-process-id)}
\returns the operating system process id if the current process
\listlibraries
\endentryheader
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{getenv}{\categoryprocedure}{(getenv \var{key})}
\returns environment value of \var{key} or \scheme{#f}
\listlibraries
\endnoskipentryheader
\noindent
\var{key} must be a string.
\scheme{getenv} returns the operating system shell's environment value
associated with \var{key}, or \scheme{#f} if no environment value
is associated with \var{key}.
\schemedisplay
(getenv "HOME") ;=> "/u/freddy"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{putenv}{\categoryprocedure}{(putenv \var{key} \var{value})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{key} and \var{value} must be strings.
\scheme{putenv} stores the \var{key}, \var{value} pair in the
environment of the process,
where it is available to the current process (e.g., via \var{getenv})
and any spawned processes.
\schemedisplay
(putenv "SCHEME" "rocks!")
(getenv "SCHEME") ;=> "rocks!"
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{get-registry}{\categoryprocedure}{(get-registry \var{key})}
\returns registry value of \var{key} or \scheme{#f}
\formdef{put-registry!}{\categoryprocedure}{(put-registry! \var{key} \var{val})}
\formdef{remove-registry!}{\categoryprocedure}{(remove-registry! \var{key})}
\returns unspecified
\listlibraries
\endentryheader
\noindent
\var{key} and \var{val} must be strings.
\scheme{get-registry} returns a string containing the registry
value of \var{key} if the value exists.
If no registry value for \var{key} exists, \scheme{get-registry} returns
\scheme{#f}.
\scheme{put-registry!} sets the registry
value of \var{key} to \var{val}.
It raises an exception with condition type \scheme{&assertion} if the
value cannot be set, which may happen if
the user has insufficient access.
\scheme{remove-registry!} removes the registry
key or value named by \var{key}.
It raises an exception with condition type \scheme{&assertion} if the
value cannot be removed.
Reasons for failure include the key not being present, the user having
insufficient access, or \var{key} being a key with subkeys.
These routines are defined for Windows only.
\schemedisplay
(get-registry "hkey_local_machine\\Software\\North\\South") ;=> #f
(put-registry! "hkey_local_machine\\Software\\North\\South" "east")
(get-registry "hkey_local_machine\\Software\\North\\South") ;=> "east"
(remove-registry! "hkey_local_machine\\Software\\North")
(get-registry "hkey_local_machine\\Software\\North\\South") ;=> #f
\endschemedisplay
\section{Subset Modes\label{SECTMISCSUBSETMODE}}
\noskipentryheader
\formdef{subset-mode}{\categorythreadparameter}{subset-mode}
\listlibraries
\endnoskipentryheader
\noindent
The value of this parameter
must be \scheme{#f} (the default) or the symbol \scheme{system}.
Setting \scheme{subset-mode} to \scheme{system} allows the manipulation
of various undocumented system variables, data structures, and
settings.
It is typically used only for system debugging.
|