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
|
/******************************** -*- C -*- ****************************
*
* Byte code compiler.
*
*
***********************************************************************/
/***********************************************************************
*
* Copyright 1988,89,90,91,92,94,95,99,2000,2001,2002,2003,2005,2006,2007,2008,2009
* Free Software Foundation, Inc.
* Written by Steve Byrne.
*
* This file is part of GNU Smalltalk.
*
* GNU Smalltalk is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any later
* version.
*
* Linking GNU Smalltalk statically or dynamically with other modules is
* making a combined work based on GNU Smalltalk. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the Free Software Foundation
* give you permission to combine GNU Smalltalk with free software
* programs or libraries that are released under the GNU LGPL and with
* independent programs running under the GNU Smalltalk virtual machine.
*
* You may copy and distribute such a system following the terms of the
* GNU GPL for GNU Smalltalk and the licenses of the other code
* concerned, provided that you include the source code of that other
* code when and as the GNU GPL requires distribution of source code.
*
* Note that people who make modified versions of GNU Smalltalk are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version without
* this exception; this exception also makes it possible to release a
* modified version which carries forward this exception.
*
* GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* GNU Smalltalk; see the file COPYING. If not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
***********************************************************************/
#include "gstpriv.h"
/* To do: extract the iterative solving of the loop jumps' size. */
/* Define this if you want declaration tracing to print the bytecodes
both *before* and *after* the optimizer is ran. Default behavior
is to print the bytecodes only after the optimization pass; usually
it is only needed to debug the optimizer -- when debugging the
compiler you should turn off optimization entirely (see NO_OPTIMIZE
in opt.c). */
/* #define PRINT_BEFORE_OPTIMIZATION */
/* Define this to verify the methods after they are compiled. This
is useless because anyway after an image is saved methods are
re-verified, but is a wonderful way of testing the compiler's
output for correctness. */
/* #define VERIFY_COMPILED_METHODS */
#define LITERAL_VEC_CHUNK_SIZE 32
typedef struct method_attributes
{
struct method_attributes *next;
int count;
OOP oop;
} method_attributes;
/* This holds whether the compiler should make the compiled methods
untrusted. */
mst_Boolean _gst_untrusted_methods = false;
/* These hold the compiler's notions of the current class for
compilations, and the current category that compiled methods are to
be placed into. */
OOP _gst_this_class = NULL;
OOP _gst_this_category = NULL;
static OOP this_method_category;
/* This holds the gst_compiled_method oop for the most recently
compiled method. It is only really valid after a compile: has been
done, but this is the only place that its used. */
OOP _gst_latest_compiled_method = NULL;
/* This flag controls whether byte codes are printed after
compilation. */
int _gst_declare_tracing = 0;
/* If true, the compilation of a set of methods will be skipped
completely; only syntax will be checked. Set by primitive, cleared
by grammar. */
mst_Boolean _gst_skip_compilation = false;
/* This is the value most recently returned by
_gst_execute_statements. It is used to communicate the returned
value past a _gst_parse_stream call, without pushing something on
the called context stack in the case of nested invocations of
_gst_prepare_execution_environment/_gst_finish_execution_environment.
Most often, the caller does not care about the returned value,
since it often is called from a radically different context. */
OOP _gst_last_returned_value = NULL;
/* Returns true if EXPR represents the symbol "super"; false if not. */
static mst_Boolean is_super (tree_node expr);
/* Returns true if OOP and CONSTEXPR represent the same literal value.
Primarily used by the compiler to store a single copy of duplicated
literals in a method. Can call itself in the case array
literals. */
static mst_Boolean equal_constant (OOP oop,
tree_node constExpr);
/* Special case compilation of a #timesRepeat: loop. EXPR is a node
for the entire keyword message send. Returns true if byte codes
were emitted, false if not. If the last argument to the message is
not a block expression, this routine cannot do its job, and so
returns false to indicate as much. */
static mst_Boolean compile_times_repeat (tree_node expr);
/* Special case compilation of a while loop whose selector is in
SELECTOR. EXPR is a node for the entire unary or keyword message
send. Returns true if byte codes were emitted, false if not. If
the last argument to the message is not a block expression, this
routine cannot do its job, and so returns false to indicate as
much. */
static mst_Boolean compile_while_loop (OOP selector,
tree_node expr);
/* Special case compilation of a 1-argument if (#ifTrue: or #ifFalse:)
whose selector is in SELECTOR; the default value for the absent
case is nil. EXPR is a node for the entire keyword message send.
Returns true if byte codes were emitted, false if not. If the last
argument to the message is not a block expression, this routine
cannot do its job, and so returns false to indicate as much. */
static mst_Boolean compile_if_statement (OOP selector,
tree_node expr);
/* Special case compilation of a #to:do: (if BY is NULL) or #to:by:do:
loop. The starting value for the iteration is given by TO, the block
is in BLOCK. Returns true if byte codes were emitted, false if
not. If the last argument to the message is not a block expression,
this routine cannot do its job, and so returns false to indicate as
much. */
static mst_Boolean compile_to_by_do (tree_node to,
tree_node by,
tree_node block);
/* Special case compilation of a #and: or #or: boolean operation; very
similar to compile_if_statement. EXPR is a node for the entire
keyword message send. Returns true if byte codes were emitted,
false if not. If the last argument to the message is not a block
expression, this routine cannot do its job, and so returns false to
indicate as much. */
static mst_Boolean compile_and_or_statement (OOP selector,
tree_node expr);
/* Special case compilation of a 2-argument if whose selector is in
SELECTOR. EXPR is a node for the entire keyword message send.
Returns true if byte codes were emitted, false if not. If the last
argument to the message is not a block expression, this routine
cannot do its job, and so returns false to indicate as much. */
static mst_Boolean compile_if_true_false_statement (OOP selector,
tree_node expr);
/* Special case compilation of an infinite loop, given by the parse
node in RECEIVER. Returns true if byte codes were emitted, false
if not. If the last argument to the message is not a block
expression, this routine cannot do its job, and so returns false to
indicate as much. */
static mst_Boolean compile_repeat (tree_node receiver);
/* Compiles all of the statements in STATEMENTLIST. If ISBLOCK is
true, adds a final instruction of the block to return the top of
stack, if the final statement isn't an explicit return from method
(^). Returns whether the last statement was a return (whatever
the value of ISBLOCK. */
static mst_Boolean compile_statements (tree_node statementList,
mst_Boolean isBlock);
/* Given a tree_node, this routine picks out and concatenates the
keywords in SELECTOREXPR (if a TREE_KEYWORD_EXPR) or extracts the
selector (if a TREE_UNARY_EXPR or TREE_BINARY_EXPR). Then it turns
them into a symbol OOP and returns that symbol. */
static OOP compute_selector (tree_node selectorExpr);
/* Creates a new Array object that contains the literals for the
method that's being compiled and returns it. As a side effect, the
currently allocated working literal vector is freed. If there were
no literals for the current method, _gst_nil_oop is returned. */
static OOP get_literals_array (void);
/* Process the attributes in ATTRIBUTELIST, return the primitive number.
Also record a <category: ...> attribute in this_method_category. */
static int process_attributes_tree (tree_node attributeList);
/* Process the attribute in MESSAGEOOP, return the primitive number
(so far, this is the only attribute we honor), -1 for a bad
primitive number, or 0 for other attributes. */
static int process_attribute (OOP messageOOP);
/* Creates and returns a CompiledMethod. The method is completely
filled in, including the descriptor, the method literals, and the
byte codes for the method. */
static OOP method_new (method_header header,
OOP literals,
bc_vector bytecodes,
OOP class,
OOP methodDesc);
/* Returns an instance of MethodInfo. This instance is used in the
reconstruction of the source code for the method, and holds the
category that the method belongs to. */
static OOP method_info_new (OOP class,
OOP selector,
method_attributes *attrs,
OOP sourceCode,
OOP categoryOOP);
/* This creates a CompiledBlock for the given BYTECODES. The bytecodes
are passed through the peephole optimizer and stored, the header is
filled according to the given number of arguments ARGS and
temporaries TEMPS, and to the cleanness of the block. STACK_DEPTH
contains the number of stack slots needed by the block except for
arguments and temporaries. */
static OOP make_block (int args,
int temps,
bc_vector bytecodes,
int stack_depth);
/* Create a BlockClosure for the given CompiledBlock, BLOCKOOP. */
static OOP make_clean_block_closure (OOP blockOOP);
/* Compiles a block tree node, EXPR, in a separate context and return
the resulting bytecodes. The block's argument declarations are
ignored since they are handled by compile_to_by_do (and are absent
for other methods like ifTrue:, and:, whileTrue:, etc.); there are
no temporaries. It is compiled as a list of statements such that
the last statement leaves the value that is produced on the stack,
as the value of the "block". */
static bc_vector compile_sub_expression (tree_node expr);
/* Like compile_sub_expression, except that after compiling EXPR this
subexpression always ends with an unconditional branch past
BRANCHLEN bytecodes. */
static bc_vector compile_sub_expression_and_jump (tree_node expr,
int branchLen);
/* Compile a send with the given RECEIVER (used to check for sends to
super), SELECTOR and number of arguments NUMARGS. */
static void compile_send (tree_node receiver,
OOP selector,
int numArgs);
/* Computes and returns the length of a parse tree list, LISTEXPR. */
static int list_length (tree_node listExpr);
/* Adds OOP to the literals associated with the method being compiled
and returns the index of the literal slot that was used (0-based).
Does not check for duplicates. Automatically puts OOP in the
incubator. */
static int add_literal (OOP oop);
/* Compiles STMT, which is a statement expression, including return
expressions. */
static void compile_statement (tree_node stmt);
/* Compile EXPR, which is an arbitrary expression, including an
assignment expression. */
static void compile_expression (tree_node expr);
/* The basic expression compiler. Often called recursively,
dispatches based on the type of EXPR to different routines that
specialize in compilations for that expression. */
static void compile_simple_expression (tree_node expr);
/* Compile code to push the value of a variable onto the stack. The
special variables, self, true, false, super, and thisContext, are
handled specially. For other variables, different code is emitted
depending on where the variable lives, such as in a global variable
or in a method temporary. */
static void compile_variable (tree_node varName);
/* Compile an expression that pushes a constant expression CONSTEXPR
onto the stack. Special cases out the constants that the byte code
interpreter knows about, which are the integers in the range -1 to
2. Tries to emit the shortest possible byte sequence. */
static void compile_constant (tree_node constExpr);
/* Compile the expressions for a block whose parse tree is BLOCKEXPR.
Also, emits code to push the BlockClosure object, and creates the
BlockClosure together with its CompiledBlock. */
static void compile_block (tree_node blockExpr);
/* Compiles all of the statements in arrayConstructor, preceded by
(Array new: <size of the list>) and with each statement followed
with a <pop into instance variable of new stack top> instead of a
simple pop. */
static void compile_array_constructor (tree_node arrayConstructor);
/* Compile code to evaluate a unary expression EXPR. Special cases
sends to "super". Also, checks to see if it's the first part of a
cascaded message send and if so emits code to duplicate the stack
top after the evaluation of the receiver for use by the subsequent
cascaded expressions. */
static void compile_unary_expr (tree_node expr);
/* Compile code to evaluate a binary expression EXPR. Special cases
sends to "super" and open codes whileTrue/whileFalse/repeat when
the receiver is a block. Also, checks to see if it's the first
part of a cascaded message send and if so emits code to duplicate
the stack top after the evaluation of the receiver for use by the
subsequent cascaded expressions. */
static void compile_binary_expr (tree_node expr);
/* Compile code to evaluate a keyword expression EXPR. Special cases
sends to "super" and open codes while loops, the 4 kinds of if
tests, and the conditional #and: and conditional #or: messages,
#to:do:, and #to:by:do: with an Integer step. Also, checks to see
if it's the first part of a cascaded message send and if so emits
code to duplicate the stack top after the evaluation of the
receiver for use by the subsequent cascaded expressions. */
static void compile_keyword_expr (tree_node expr);
/* Compiles the code for a cascaded message send. Due to the fact
that cascaded sends go to the receiver of the last message before
the first cascade "operator" (the ";"), the system to perform
cascaded message sends is a bit kludgy. We basically turn on a
flag to the compiler that indicates that the value of the receiver
of the last message before the cascaded sends is to be duplicated;
and then compile code for each cascaded expression, throwing away
the result, and duplicating the original receiver so that it can be
used by the current message send, and following ones.
Note that both the initial receiver and all the subsequent cascaded
sends can be derived from CASCADEDEXPR. */
static void compile_cascaded_message (tree_node cascadedExpr);
/* Compiles all the assignments in VARLIST, which is a tree_node of
type listNode. The generated code assumes that the value on the
top of the stack is what's to be used for the assignment. Since
this routine has no notion of now the value on top of the stack
will be used by the calling environment, it makes sure that when
the assignments are through, that the value on top of the stack
after the assignment is the same as the value on top of the stack
before the assignment. The optimizer should fix this in the
unnecessary cases. */
static void compile_assignments (tree_node varList);
/* Compiles a forward jump instruction LEN bytes away (LEN must be >
0), using the smallest possible number of bytecodes. JUMPTYPE
indicates which among the unconditional, "jump if true" and "jump
if false" opcodes is desired. Special cases for the short
unconditional jump and the short false jump that the byte code
interpreter handles. */
static void compile_jump (int len,
mst_Boolean jumpType);
/* Emit code to evaluate each argument to a keyword message send,
taking them from the parse tree node LIST. */
static void compile_keyword_list (tree_node list);
/* Called to grow the literal vector that the compiler is using. Modifies
the global variables LITERAL_VEC and LITERAL_VEC_MAX to reflect the
growth. */
static void realloc_literal_vec (void);
/* Takes a new CompiledMethod METHODOOP and installs it in the method
dictionary for the current class. If the current class does not
contain a valid method dictionary, one is allocated for it. */
static void install_method (OOP methodOOP);
/* This caches the OOP of the special UndefinedObject>>#__terminate
method, which is executed by contexts created with
_gst_prepare_execution_environment. */
static OOP termination_method;
/* Used to abort really losing compiles, jumps back to the top level
of the compiler */
static jmp_buf bad_method;
/* The linked list of attributes that are specified by the method. */
static method_attributes *method_attrs = NULL;
/* The vector of literals that the compiler uses to accumulate literal
constants into */
static OOP *literal_vec = NULL;
/* These indicate the first free slot in the vector of literals in the
method being compiled, and the first slot past the literal vector */
static OOP *literal_vec_curr, *literal_vec_max;
/* This indicates whether we are compiling a block */
static int inside_block;
/* HACK ALERT!! HACK ALERT!! This variable is used for cascading.
The tree structure is all wrong for the code in cascade processing
to find the receiver of the initial message. What this does is
when it's true, compile_unary_expr, compile_binary_expr, and
compile_keyword_expr record its value, and clear the global (to
prevent propagation to compilation of subnodes). After compiling
their receiver, if the saved value of the flag is true, they emit a
DUP_STACK_TOP, and continue compilation. Since cascaded sends are
relatively rare, I figured that this was a better alternative than
passing useless parameters around all the time. */
static mst_Boolean dup_message_receiver = false;
/* Exit a really losing compilation */
#define EXIT_COMPILATION() \
longjmp(bad_method, 1)
/* Answer whether the BLOCKNODE parse node has temporaries or
arguments. */
#define HAS_PARAMS_OR_TEMPS(blockNode) \
(blockNode->v_block.temporaries || blockNode->v_block.arguments)
/* Answer whether the BLOCKNODE parse node has temporaries and
has not exactly one argument. */
#define HAS_NOT_ONE_PARAM_OR_TEMPS(blockNode) \
(blockNode->v_block.temporaries \
|| !blockNode->v_block.arguments \
|| blockNode->v_block.arguments->v_list.next)
void
_gst_install_initial_methods (void)
{
const char *methodsForString;
/* Define the termination method first of all, because
compiling #methodsFor: will invoke an evaluation
(to get the argument of the <primitive: ...> attribute. */
_gst_set_compilation_class (_gst_undefined_object_class);
_gst_set_compilation_category (_gst_string_new ("private"));
_gst_alloc_bytecodes ();
_gst_compile_byte (EXIT_INTERPRETER, 0);
_gst_compile_byte (JUMP_BACK, 4);
/* The zeros are primitive, # of args, # of temps, stack depth */
termination_method = _gst_make_new_method (0, 0, 0, 0, _gst_nil_oop,
_gst_get_bytecodes (),
_gst_this_class,
_gst_terminate_symbol,
_gst_this_category, -1, -1);
((gst_compiled_method) OOP_TO_OBJ (termination_method))->header.headerFlag
= MTH_ANNOTATED;
install_method (termination_method);
methodsForString = "\n\
methodsFor: aCategoryString [\n\
\"Calling this method prepares the parser to receive methods \n\
to be compiled and installed in the receiver's method dictionary. \n\
The methods are put in the category identified by the parameter.\" \n\
<primitive: VMpr_Behavior_methodsFor> \n\
]";
_gst_set_compilation_class (_gst_behavior_class);
_gst_set_compilation_category (_gst_string_new ("compiling methods"));
_gst_push_smalltalk_string (_gst_string_new (methodsForString));
_gst_parse_stream (true);
_gst_pop_stream (true);
_gst_reset_compilation_category ();
}
OOP
_gst_get_termination_method (void)
{
if (!termination_method)
{
termination_method =
_gst_find_class_method (_gst_undefined_object_class,
_gst_terminate_symbol);
}
return (termination_method);
}
static void
invoke_hook_smalltalk (enum gst_vm_hook hook)
{
const char *hook_name;
if (!_gst_kernel_initialized)
return;
switch (hook) {
case GST_BEFORE_EVAL:
hook_name = "beforeEvaluation";
break;
case GST_AFTER_EVAL:
hook_name = "afterEvaluation";
break;
case GST_RETURN_FROM_SNAPSHOT:
hook_name = "returnFromSnapshot";
break;
case GST_ABOUT_TO_QUIT:
hook_name = "aboutToQuit";
break;
case GST_ABOUT_TO_SNAPSHOT:
hook_name = "aboutToSnapshot";
break;
case GST_FINISHED_SNAPSHOT:
hook_name = "finishedSnapshot";
break;
default:
return;
}
_gst_msg_sendf (NULL, "%v %o changed: %S",
_gst_object_memory_class, hook_name);
}
void
_gst_invoke_hook (enum gst_vm_hook hook)
{
int save_execution;
save_execution = _gst_execution_tracing;
if (_gst_execution_tracing == 1)
_gst_execution_tracing = 0;
invoke_hook_smalltalk (hook);
_gst_execution_tracing = save_execution;
}
void
_gst_init_compiler (void)
{
/* Prepare the literal vector for use. The literal vector is where the
compiler will store any literals that are used by the method being
compiled. */
literal_vec = (OOP *) xmalloc (LITERAL_VEC_CHUNK_SIZE * sizeof (OOP));
literal_vec_curr = literal_vec;
literal_vec_max = literal_vec + LITERAL_VEC_CHUNK_SIZE;
_gst_register_oop_array (&literal_vec, &literal_vec_curr);
_gst_reset_compilation_category ();
}
void
_gst_set_compilation_class (OOP class_oop)
{
_gst_unregister_oop (_gst_this_class);
_gst_this_class = class_oop;
_gst_register_oop (_gst_this_class);
_gst_untrusted_methods = (IS_OOP_UNTRUSTED (_gst_this_context_oop)
|| IS_OOP_UNTRUSTED (_gst_this_class));
}
void
_gst_set_compilation_category (OOP categoryOOP)
{
_gst_unregister_oop (_gst_this_category);
_gst_this_category = categoryOOP;
_gst_register_oop (_gst_this_category);
_gst_untrusted_methods = (IS_OOP_UNTRUSTED (_gst_this_context_oop)
|| IS_OOP_UNTRUSTED (_gst_this_class));
}
void
_gst_reset_compilation_category ()
{
_gst_set_compilation_class (_gst_undefined_object_class);
_gst_set_compilation_category (_gst_nil_oop);
_gst_untrusted_methods = false;
}
void
_gst_display_compilation_trace (const char *string,
mst_Boolean category)
{
if (!_gst_declare_tracing)
return;
if (category)
printf ("%s category %O for %O\n", string,
_gst_this_category, _gst_this_class);
else
printf ("%s for %O\n", string, _gst_this_class);
}
OOP
_gst_execute_statements (tree_node temps,
tree_node statements,
enum undeclared_strategy undeclared,
mst_Boolean quiet)
{
tree_node messagePattern;
int startTime, endTime, deltaTime;
unsigned long cacheHits;
#ifdef HAVE_GETRUSAGE
struct rusage startRusage, endRusage;
#endif
OOP methodOOP;
OOP oldClass, oldCategory;
enum undeclared_strategy oldUndeclared;
inc_ptr incPtr;
YYLTYPE loc;
if (_gst_regression_testing
|| _gst_verbosity < 2
|| !_gst_get_cur_stream_prompt ())
quiet = true;
oldClass = _gst_this_class;
oldCategory = _gst_this_category;
_gst_register_oop (oldClass);
_gst_register_oop (oldCategory);
_gst_set_compilation_class (_gst_undefined_object_class);
_gst_set_compilation_category (_gst_nil_oop);
loc = _gst_get_location ();
messagePattern = _gst_make_unary_expr (&statements->location,
NULL, "executeStatements");
_gst_display_compilation_trace ("Compiling doit code", false);
/* This is a big hack to let doits access the variables and classes
in the current namespace. */
oldUndeclared = _gst_set_undeclared (undeclared);
SET_CLASS_ENVIRONMENT (_gst_undefined_object_class,
_gst_current_namespace);
if (statements->nodeType != TREE_STATEMENT_LIST)
statements = _gst_make_statement_list (&statements->location, statements);
methodOOP =
_gst_compile_method (_gst_make_method (&statements->location, &loc,
messagePattern, temps, NULL,
statements, false),
true, false);
SET_CLASS_ENVIRONMENT (_gst_undefined_object_class,
_gst_smalltalk_dictionary);
_gst_set_undeclared (oldUndeclared);
_gst_set_compilation_class (oldClass);
_gst_set_compilation_category (oldCategory);
_gst_unregister_oop (oldClass);
_gst_unregister_oop (oldCategory);
if (_gst_had_error) /* don't execute on error */
return (NULL);
incPtr = INC_SAVE_POINTER ();
INC_ADD_OOP (methodOOP);
if (!_gst_raw_profile)
_gst_bytecode_counter = _gst_primitives_executed =
_gst_self_returns = _gst_inst_var_returns = _gst_literal_returns =
_gst_sample_counter = 0;
startTime = _gst_get_milli_time ();
#ifdef HAVE_GETRUSAGE
getrusage (RUSAGE_SELF, &startRusage);
#endif
_gst_invoke_hook (GST_BEFORE_EVAL);
/* send a message to NIL, which will find this synthetic method
definition in Object and execute it */
_gst_last_returned_value = _gst_nvmsg_send (_gst_nil_oop, methodOOP, NULL, 0);
INC_ADD_OOP (_gst_last_returned_value);
endTime = _gst_get_milli_time ();
#ifdef HAVE_GETRUSAGE
getrusage (RUSAGE_SELF, &endRusage);
#endif
if (!quiet && _gst_verbosity >= 3)
{
deltaTime = endTime - startTime;
#ifdef ENABLE_JIT_TRANSLATION
printf ("Execution took %.3f seconds", deltaTime / 1000.0);
#else
printf ("%lu byte codes executed\nwhich took %.3f seconds",
_gst_bytecode_counter, deltaTime / 1000.0);
#endif
#ifdef HAVE_GETRUSAGE
deltaTime = ((endRusage.ru_utime.tv_sec * 1000) +
(endRusage.ru_utime.tv_usec / 1000)) -
((startRusage.ru_utime.tv_sec * 1000) +
(startRusage.ru_utime.tv_usec / 1000));
printf (" (%.3fs user", deltaTime / 1000.0);
deltaTime = ((endRusage.ru_stime.tv_sec * 1000) +
(endRusage.ru_stime.tv_usec / 1000)) -
((startRusage.ru_stime.tv_sec * 1000) +
(startRusage.ru_stime.tv_usec / 1000));
printf ("+%.3fs sys)", deltaTime / 1000.0);
#endif
printf ("\n");
#ifndef ENABLE_JIT_TRANSLATION
if (_gst_bytecode_counter)
{
printf ("%lu primitives, percent %.2f\n", _gst_primitives_executed,
100.0 * _gst_primitives_executed / _gst_bytecode_counter);
printf ("self returns %lu, inst var returns %lu, literal returns %lu\n",
_gst_self_returns, _gst_inst_var_returns, _gst_literal_returns);
printf ("%lu method cache lookups since last cleanup, percent %.2f\n",
_gst_sample_counter,
100.0 * _gst_sample_counter / _gst_bytecode_counter);
}
#endif
if (_gst_sample_counter)
{
#ifdef ENABLE_JIT_TRANSLATION
printf
("%lu primitives, %lu inline cache misses since last cache cleanup\n",
_gst_primitives_executed, _gst_sample_counter);
#endif
cacheHits = _gst_sample_counter - _gst_cache_misses;
printf ("%lu method cache hits, %lu misses", cacheHits,
_gst_cache_misses);
if (cacheHits || _gst_cache_misses)
printf (", %.2f percent hits\n", (100.0 * cacheHits) / _gst_sample_counter);
else
printf ("\n");
}
/* Do more frequent flushing to ensure the result are well placed */
printf ("returned value is ");
fflush(stdout);
}
if (!quiet)
{
int save_execution;
save_execution = _gst_execution_tracing;
if (_gst_execution_tracing == 1)
_gst_execution_tracing = 0;
if (_gst_responds_to (_gst_last_returned_value,
_gst_intern_string ("printNl"))
|| _gst_responds_to (_gst_last_returned_value,
_gst_does_not_understand_symbol))
_gst_str_msg_send (_gst_last_returned_value, "printNl", NULL);
else
printf ("%O\n", _gst_last_returned_value);
fflush (stdout);
fflush (stderr);
_gst_execution_tracing = save_execution;
}
_gst_invoke_hook (GST_AFTER_EVAL);
INC_RESTORE_POINTER (incPtr);
return (_gst_last_returned_value);
}
OOP
_gst_compile_method (tree_node method,
mst_Boolean returnLast,
mst_Boolean install)
{
tree_node statement;
OOP selector;
OOP methodOOP;
bc_vector bytecodes;
int primitiveIndex;
int stack_depth;
inc_ptr incPtr;
gst_compiled_method compiledMethod;
dup_message_receiver = false;
literal_vec_curr = literal_vec;
this_method_category = _gst_this_category;
_gst_unregister_oop (_gst_latest_compiled_method);
_gst_latest_compiled_method = _gst_nil_oop;
incPtr = INC_SAVE_POINTER ();
_gst_alloc_bytecodes ();
_gst_push_new_scope ();
inside_block = 0;
selector = compute_selector (method->v_method.selectorExpr);
/* When we are reading from stdin, it's better to write line numbers where
1 is the first line *in the current doit*, because for now the prompt
does not include the line number. This might change in the future.
Also, do not emit line numbers if the method has no statements. */
if ((method->location.file_offset != -1 && _gst_get_cur_stream_prompt ())
|| !method->v_method.statements)
_gst_line_number (method->location.first_line, LN_RESET);
else
_gst_line_number (method->location.first_line, LN_RESET | LN_ABSOLUTE);
INC_ADD_OOP (selector);
if (_gst_declare_tracing)
printf (" class %O, selector %O\n", _gst_this_class, selector);
if (setjmp (bad_method) == 0)
{
if (_gst_declare_arguments (method->v_method.selectorExpr) == -1)
{
_gst_errorf_at (method->location.first_line,
"duplicate argument name");
EXIT_COMPILATION ();
}
if (_gst_declare_temporaries (method->v_method.temporaries) == -1)
{
_gst_errorf_at (method->location.first_line,
"duplicate temporary variable name");
EXIT_COMPILATION ();
}
primitiveIndex = process_attributes_tree (method->v_method.attributes);
for (statement = method->v_method.statements; statement; )
{
mst_Boolean wasReturn = statement->v_list.value->nodeType == TREE_RETURN_EXPR;
compile_statement (statement->v_list.value);
statement = statement->v_list.next;
if (wasReturn)
continue;
if (!statement && returnLast)
/* compile a return of the last evaluated value. Note that in
theory the pop above is not necessary in this case
(and in fact older versions did not put it),
but having it simplifies the optimizer's task
because it reduces the number of patterns it has
to look for. If necessary, the optimizer itself
will remove the pop. */
{
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
break;
}
/* ignore the result of the last statement if it's not
used */
SUB_STACK_DEPTH (1);
_gst_compile_byte (POP_STACK_TOP, 0);
if (!statement)
{
/* compile a return of self. Note that in
theory the pop above is not necessary in this case
(and in fact older versions did not put it),
but having it simplifies the optimizer's task
because it reduces the number of patterns it has
to look for. If necessary, the optimizer itself
will remove the pop. */
_gst_compile_byte (PUSH_SELF, 0);
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
break;
}
}
if (method->v_method.statements == NULL)
{
if (returnLast)
{
/* special case an empty statement body to return nil */
_gst_compile_byte (PUSH_SPECIAL, NIL_INDEX);
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
}
else
{
/* special case an empty statement body to return _gst_self */
_gst_compile_byte (PUSH_SELF, 0);
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
}
}
stack_depth = GET_STACK_DEPTH ();
bytecodes = _gst_get_bytecodes ();
methodOOP = _gst_make_new_method (primitiveIndex,
_gst_get_arg_count (),
_gst_get_temp_count (),
stack_depth, _gst_nil_oop, bytecodes,
_gst_this_class, selector,
_gst_this_category,
method->location.file_offset,
method->v_method.endPos);
compiledMethod = (gst_compiled_method) OOP_TO_OBJ (methodOOP);
compiledMethod->header.isOldSyntax = method->v_method.isOldSyntax;
INC_ADD_OOP (methodOOP);
if (install)
install_method (methodOOP);
_gst_latest_compiled_method = methodOOP; /* reachable by the
root set */
_gst_register_oop (_gst_latest_compiled_method);
}
else
{
_gst_had_error = true;
bytecodes = _gst_get_bytecodes ();
literal_vec_curr = literal_vec;
_gst_free_bytecodes (bytecodes);
}
_gst_pop_all_scopes ();
INC_RESTORE_POINTER (incPtr);
return (_gst_latest_compiled_method);
}
void
compile_statement (tree_node stmt)
{
tree_node receiver;
if (stmt->nodeType != TREE_RETURN_EXPR)
{
compile_expression (stmt);
return;
}
receiver = stmt->v_expr.receiver;
if (inside_block)
{
compile_expression (receiver);
_gst_compile_byte (RETURN_METHOD_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
return;
}
compile_expression (receiver);
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
}
void
compile_expression (tree_node expr)
{
if (expr->nodeType == TREE_ASSIGN_EXPR)
{
compile_simple_expression (expr->v_expr.expression);
compile_assignments (expr->v_expr.receiver);
}
else
compile_simple_expression (expr);
}
void
compile_simple_expression (tree_node expr)
{
_gst_line_number (expr->location.first_line, 0);
switch (expr->nodeType)
{
case TREE_VARIABLE_NODE:
compile_variable (expr);
break;
case TREE_CONST_EXPR:
compile_constant (expr);
break;
case TREE_BLOCK_NODE:
compile_block (expr);
break;
case TREE_UNARY_EXPR:
compile_unary_expr (expr);
break;
case TREE_BINARY_EXPR:
compile_binary_expr (expr);
break;
case TREE_KEYWORD_EXPR:
compile_keyword_expr (expr);
break;
case TREE_CASCADE_EXPR:
compile_cascaded_message (expr);
break;
case TREE_ARRAY_CONSTRUCTOR:
compile_array_constructor (expr);
break;
default:
compile_expression (expr);
}
}
void
compile_variable (tree_node varName)
{
symbol_entry variable;
INCR_STACK_DEPTH ();
if (!_gst_find_variable (&variable, varName))
{
if (varName->v_list.next)
_gst_errorf_at (varName->location.first_line,
"invalid scope resolution");
else
_gst_errorf_at (varName->location.first_line,
"undefined variable %s referenced",
varName->v_list.name);
EXIT_COMPILATION ();
}
if (variable.scope == SCOPE_SPECIAL)
switch (variable.varIndex)
{
case THIS_CONTEXT_INDEX:
{
static OOP contextPartAssociation;
if (!contextPartAssociation)
{
contextPartAssociation =
dictionary_association_at (_gst_smalltalk_dictionary,
_gst_intern_string ("ContextPart"));
}
_gst_compile_byte (PUSH_LIT_VARIABLE,
_gst_add_forced_object (contextPartAssociation));
_gst_compile_byte (SEND_IMMEDIATE, THIS_CONTEXT_SPECIAL);
}
return;
case RECEIVER_INDEX:
_gst_compile_byte (PUSH_SELF, 0);
return;
default:
_gst_compile_byte (PUSH_SPECIAL, variable.varIndex);
return;
}
if (variable.scope != SCOPE_GLOBAL && varName->v_list.next)
{
_gst_errorf_at (varName->location.first_line,
"invalid scope resolution");
EXIT_COMPILATION ();
}
if (variable.scopeDistance != 0)
/* must be a temporary from an outer scope */
_gst_compile_byte (PUSH_OUTER_TEMP,
variable.varIndex * 256 + variable.scopeDistance);
else if (variable.scope == SCOPE_TEMPORARY)
_gst_compile_byte (PUSH_TEMPORARY_VARIABLE, variable.varIndex);
else if (variable.scope == SCOPE_RECEIVER)
_gst_compile_byte (PUSH_RECEIVER_VARIABLE, variable.varIndex);
else
_gst_compile_byte (PUSH_LIT_VARIABLE, variable.varIndex);
}
void
compile_constant (tree_node constExpr)
{
intptr_t intVal;
int index = -1;
OOP constantOOP;
OOP *lit;
/* Scan the current literal frame, looking for a constant equal
to the one that is being compiled. */
for (lit = literal_vec; lit < literal_vec_curr; lit++)
if (equal_constant (*lit, constExpr))
{
index = lit - literal_vec;
break;
}
/* If not found, check if it can be compiled with a PUSH_INTEGER
bytecode, or add it to the literals. */
if (index == -1)
{
constantOOP = _gst_make_constant_oop (constExpr);
if (IS_INT (constantOOP))
{
intVal = TO_INT (constantOOP);
if (intVal >= 0 && intVal <= 0x7FFFFFFFL)
{
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_INTEGER, intVal);
return;
}
}
index = add_literal (constantOOP);
}
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_LIT_CONSTANT, index);
}
void
compile_block (tree_node blockExpr)
{
bc_vector current_bytecodes, blockByteCodes;
int argCount, tempCount;
int stack_depth;
OOP blockClosureOOP, blockOOP;
gst_compiled_block block;
inc_ptr incPtr;
current_bytecodes = _gst_save_bytecode_array ();
_gst_push_new_scope ();
argCount = _gst_declare_block_arguments (blockExpr->v_block.arguments);
tempCount = _gst_declare_temporaries (blockExpr->v_block.temporaries);
if (argCount == -1)
{
_gst_errorf_at (blockExpr->location.first_line,
"duplicate argument name");
EXIT_COMPILATION ();
}
if (tempCount == -1)
{
_gst_errorf_at (blockExpr->location.first_line,
"duplicate temporary variable name");
EXIT_COMPILATION ();
}
compile_statements (blockExpr->v_block.statements, true);
stack_depth = GET_STACK_DEPTH ();
blockByteCodes = _gst_get_bytecodes ();
_gst_restore_bytecode_array (current_bytecodes);
/* Always allocate objects starting from the deepest one! (that is,
subtle bugs arise if make_block triggers a GC, because
the pointer in the closure might be no longer valid!) */
incPtr = INC_SAVE_POINTER ();
blockOOP = make_block (_gst_get_arg_count (), _gst_get_temp_count (),
blockByteCodes, stack_depth);
INC_ADD_OOP (blockOOP);
_gst_pop_old_scope ();
/* emit standard byte sequence to invoke a block:
push literal (a BlockClosure)
or
push literal (a CompiledBlock)
make dirty block */
INCR_STACK_DEPTH ();
block = (gst_compiled_block) OOP_TO_OBJ (blockOOP);
if (block->header.clean == 0)
{
blockClosureOOP = make_clean_block_closure (blockOOP);
_gst_compile_byte (PUSH_LIT_CONSTANT, add_literal (blockClosureOOP));
}
else
{
_gst_compile_byte (PUSH_LIT_CONSTANT, add_literal (blockOOP));
_gst_compile_byte (MAKE_DIRTY_BLOCK, 0);
}
INC_RESTORE_POINTER (incPtr);
}
mst_Boolean
compile_statements (tree_node statementList,
mst_Boolean isBlock)
{
tree_node stmt;
if (statementList == NULL)
{
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_SPECIAL, NIL_INDEX);
if (isBlock)
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
return (false);
}
if (isBlock)
{
_gst_line_number (statementList->location.first_line, LN_FORCE);
inside_block++;
}
for (stmt = statementList;; stmt = stmt->v_list.next)
{
compile_statement (stmt->v_list.value);
if (!stmt->v_list.next)
break;
/* throw away the value on the top of the stack...we don't need
it for all but the last one. */
SUB_STACK_DEPTH (1);
_gst_compile_byte (POP_STACK_TOP, 0);
}
/* stmt is the last statement here. if it isn't a return, then
return the value on the stack as the result. For inlined blocks,
the returned value is the top of the stack (which is already
there), so we are already done. */
if (stmt->v_list.value->nodeType != TREE_RETURN_EXPR && isBlock)
_gst_compile_byte (RETURN_CONTEXT_STACK_TOP, 0);
if (isBlock)
{
_gst_line_number (-1, 0);
inside_block--;
}
return (stmt->v_list.value->nodeType == TREE_RETURN_EXPR);
}
void
compile_array_constructor (tree_node arrayConstructor)
{
tree_node stmt, statementList;
int i, n;
static OOP arrayAssociation;
statementList = arrayConstructor->v_const.val.aVal;
for (n = 0, stmt = statementList; stmt;
n++, stmt = stmt->v_list.next);
if (!arrayAssociation)
{
arrayAssociation =
dictionary_association_at (_gst_smalltalk_dictionary,
_gst_intern_string ("Array"));
}
ADD_STACK_DEPTH (2);
_gst_compile_byte (PUSH_LIT_VARIABLE,
_gst_add_forced_object (arrayAssociation));
_gst_compile_byte (PUSH_INTEGER, n);
_gst_compile_byte (SEND_IMMEDIATE, NEW_COLON_SPECIAL);
SUB_STACK_DEPTH (1);
/* compile the values now */
for (i = 0, stmt = statementList; i < n;
i++, stmt = stmt->v_list.next)
{
compile_statement (stmt->v_list.value);
_gst_compile_byte (POP_INTO_NEW_STACKTOP, i);
/* throw away the value on the top of the stack... */
SUB_STACK_DEPTH (1);
}
}
void
compile_unary_expr (tree_node expr)
{
OOP selector;
mst_Boolean savedDupFlag;
savedDupFlag = dup_message_receiver;
dup_message_receiver = false;
selector = expr->v_expr.selector;
/* check for optimized cases of messages to blocks and handle them
specially */
if (selector == _gst_while_true_symbol
|| selector == _gst_while_false_symbol)
{
if (compile_while_loop (selector, expr))
return;
}
else if (selector == _gst_repeat_symbol)
{
if (compile_repeat (expr->v_expr.receiver))
return;
}
if (expr->v_expr.receiver != NULL)
{
compile_expression (expr->v_expr.receiver);
if (savedDupFlag)
{
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
}
}
compile_send (expr, selector, 0);
}
void
compile_binary_expr (tree_node expr)
{
OOP selector;
mst_Boolean savedDupFlag;
savedDupFlag = dup_message_receiver;
dup_message_receiver = false;
selector = expr->v_expr.selector;
if (expr->v_expr.receiver != NULL)
{
compile_expression (expr->v_expr.receiver);
if (savedDupFlag)
{
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
}
}
if (expr->v_expr.expression)
compile_expression (expr->v_expr.expression);
compile_send (expr, selector, 1);
}
void
compile_keyword_expr (tree_node expr)
{
OOP selector;
int numArgs;
mst_Boolean savedDupFlag;
savedDupFlag = dup_message_receiver;
dup_message_receiver = false;
selector = compute_selector (expr);
/* check for optimized cases of messages to booleans and handle them
specially */
if (selector == _gst_while_true_colon_symbol
|| selector == _gst_while_false_colon_symbol)
{
if (compile_while_loop (selector, expr))
return;
}
if (expr->v_expr.receiver)
{
compile_expression (expr->v_expr.receiver);
if (savedDupFlag)
{
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
}
}
if (selector == _gst_if_true_symbol
|| selector == _gst_if_false_symbol)
{
if (compile_if_statement (selector, expr->v_expr.expression))
return;
}
else if (selector == _gst_if_true_if_false_symbol
|| selector == _gst_if_false_if_true_symbol)
{
if (compile_if_true_false_statement
(selector, expr->v_expr.expression))
return;
}
else if (selector == _gst_and_symbol
|| selector == _gst_or_symbol)
{
if (compile_and_or_statement (selector, expr->v_expr.expression))
return;
}
else if (selector == _gst_times_repeat_symbol)
{
if (compile_times_repeat (expr->v_expr.expression))
return;
}
else if (selector == _gst_to_do_symbol)
{
if (compile_to_by_do (expr->v_expr.expression->v_list.value, NULL,
expr->v_expr.expression->v_list.next->v_list.value))
return;
}
else if (selector == _gst_to_by_do_symbol)
{
if (compile_to_by_do (expr->v_expr.expression->v_list.value,
expr->v_expr.expression->v_list.next->v_list.value,
expr->v_expr.expression->v_list.next->v_list.next->v_list.value))
return;
}
numArgs = list_length (expr->v_expr.expression);
compile_keyword_list (expr->v_expr.expression);
compile_send (expr, selector, numArgs);
}
void
compile_send (tree_node expr,
OOP selector,
int numArgs)
{
const char *str = (const char *) OOP_TO_OBJ (selector)->data;
int len = NUM_INDEXABLE_FIELDS (selector);
struct builtin_selector *bs = _gst_lookup_builtin_selector (str, len);
int super = expr->v_expr.receiver
&& is_super (expr->v_expr.receiver);
if (super && IS_NIL (SUPERCLASS (_gst_this_class)))
{
_gst_errorf ("cannot send to super from within a root class\n");
EXIT_COMPILATION ();
}
if (super)
compile_constant (_gst_make_oop_constant (&expr->location,
SUPERCLASS (_gst_this_class)));
if (!bs)
{
int selectorIndex = _gst_add_forced_object (selector);
_gst_compile_byte (SEND | super, (selectorIndex << 8) | numArgs);
}
else if (!super && bs->bytecode < 32)
_gst_compile_byte (bs->bytecode, 0);
else
_gst_compile_byte (SEND_IMMEDIATE | super, bs->bytecode);
SUB_STACK_DEPTH (numArgs);
}
void
compile_keyword_list (tree_node list)
{
for (; list; list = list->v_list.next)
compile_expression (list->v_list.value);
}
mst_Boolean
compile_while_loop (OOP selector,
tree_node expr)
{
int finalJumpLen, finalJumpOfs, jumpAroundLen, jumpAroundOfs,
oldJumpAroundLen;
int whileCondLen;
bc_vector receiverExprCodes, whileExprCodes = NULL;
mst_Boolean colon, whileTrue;
colon = (expr->v_expr.expression != NULL);
whileTrue = (selector == _gst_while_true_colon_symbol)
|| (selector == _gst_while_true_symbol);
if (expr->v_expr.receiver->nodeType != TREE_BLOCK_NODE ||
(colon
&& expr->v_expr.expression->v_list.value->nodeType !=
TREE_BLOCK_NODE))
return (false);
if (HAS_PARAMS_OR_TEMPS (expr->v_expr.receiver) ||
(colon
&& HAS_PARAMS_OR_TEMPS (expr->v_expr.expression->v_list.value)))
return (false);
receiverExprCodes = compile_sub_expression (expr->v_expr.receiver);
whileCondLen = _gst_bytecode_length (receiverExprCodes);
_gst_compile_and_free_bytecodes (receiverExprCodes);
if (colon)
{
whileExprCodes =
compile_sub_expression (expr->v_expr.expression->v_list.value);
jumpAroundOfs = _gst_bytecode_length (whileExprCodes) + 2;
}
else
jumpAroundOfs = 0;
for (oldJumpAroundLen = finalJumpLen = 0; ; oldJumpAroundLen = jumpAroundLen)
{
finalJumpOfs = whileCondLen + 2 + oldJumpAroundLen + jumpAroundOfs;
finalJumpLen = (finalJumpOfs + finalJumpLen >= 65536 ? 6 :
finalJumpOfs + finalJumpLen >= 256 ? 4 : 2);
finalJumpLen = (finalJumpOfs + finalJumpLen >= 65536 ? 6 :
finalJumpOfs + finalJumpLen >= 256 ? 4 : 2);
jumpAroundLen = (jumpAroundOfs + finalJumpLen >= 65536 ? 6 :
jumpAroundOfs + finalJumpLen >= 256 ? 4 : 2);
if (jumpAroundLen == oldJumpAroundLen)
break;
}
/* skip to the while loop if the receiver block yields the proper
value */
compile_jump (jumpAroundLen, whileTrue);
/* otherwise, skip to the end, past the pop stack top and 2 byte
jump and exit the loop */
_gst_compile_byte (JUMP, jumpAroundOfs + finalJumpLen);
if (colon)
{
_gst_compile_and_free_bytecodes (whileExprCodes);
_gst_compile_byte (POP_STACK_TOP, 0); /* we don't care about
while expr's value */
SUB_STACK_DEPTH (1);
}
_gst_compile_byte (JUMP_BACK, finalJumpLen + finalJumpOfs);
/* while loops always return nil (ain't expression languages grand?)
-- inefficient, but anyway the optimizer deletes this. */
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_SPECIAL, NIL_INDEX);
return (true);
}
mst_Boolean
compile_repeat (tree_node receiver)
{
int repeatedLoopLen, finalJumpLen;
bc_vector receiverExprCodes;
if (receiver->nodeType != TREE_BLOCK_NODE)
return (false);
if (HAS_PARAMS_OR_TEMPS (receiver))
return (false);
receiverExprCodes = compile_sub_expression (receiver);
repeatedLoopLen = _gst_bytecode_length (receiverExprCodes);
repeatedLoopLen += 2;
finalJumpLen = (repeatedLoopLen >= 65536 ? 6 :
repeatedLoopLen >= 256 ? 4 : 2);
finalJumpLen = (repeatedLoopLen + finalJumpLen >= 65536 ? 6 :
repeatedLoopLen + finalJumpLen >= 256 ? 4 : 2);
finalJumpLen = (repeatedLoopLen + finalJumpLen >= 65536 ? 6 :
repeatedLoopLen + finalJumpLen >= 256 ? 4 : 2);
_gst_compile_and_free_bytecodes (receiverExprCodes);
_gst_compile_byte (POP_STACK_TOP, 0); /* we don't care about expr's
value */
SUB_STACK_DEPTH (1);
_gst_compile_byte (JUMP_BACK, finalJumpLen + repeatedLoopLen);
/* this code is unreachable, it is only here to please the JIT
compiler */
_gst_compile_byte (PUSH_SPECIAL, NIL_INDEX);
INCR_STACK_DEPTH ();
return (true);
}
mst_Boolean
compile_times_repeat (tree_node expr)
{
int jumpAroundOfs, oldJumpAroundLen, jumpAroundLen;
int finalJumpOfs, finalJumpLen;
bc_vector loopExprCodes;
if (expr->v_list.value->nodeType != TREE_BLOCK_NODE)
return (false);
if (HAS_PARAMS_OR_TEMPS (expr->v_list.value))
return (false);
/* save the receiver for the return value */
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
loopExprCodes = compile_sub_expression (expr->v_list.value);
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_INTEGER, 1);
INCR_STACK_DEPTH ();
_gst_compile_byte (GREATER_EQUAL_SPECIAL, 0);
SUB_STACK_DEPTH (1);
/* skip the loop if there are no more occurrences */
jumpAroundOfs = 6 + _gst_bytecode_length (loopExprCodes);
for (oldJumpAroundLen = finalJumpLen = 0; ; oldJumpAroundLen = jumpAroundLen)
{
finalJumpOfs = 6 + oldJumpAroundLen + jumpAroundOfs;
finalJumpLen = (finalJumpOfs + finalJumpLen > 65536 ? 6 :
finalJumpOfs + finalJumpLen > 256 ? 4 : 2);
finalJumpLen = (finalJumpOfs + finalJumpLen > 65536 ? 6 :
finalJumpOfs + finalJumpLen > 256 ? 4 : 2);
jumpAroundLen = (jumpAroundOfs + finalJumpLen > 65536 ? 6 :
jumpAroundOfs + finalJumpLen > 256 ? 4 : 2);
if (jumpAroundLen == oldJumpAroundLen)
break;
}
compile_jump (jumpAroundOfs + finalJumpLen, false);
_gst_compile_byte (PUSH_INTEGER, 1);
INCR_STACK_DEPTH ();
_gst_compile_byte (MINUS_SPECIAL, 0);
SUB_STACK_DEPTH (1);
/* we don't care about block expr's value */
_gst_compile_and_free_bytecodes (loopExprCodes);
_gst_compile_byte (POP_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
_gst_compile_byte (JUMP_BACK, finalJumpLen + finalJumpOfs);
/* delete the 0 that remains on the stack */
_gst_compile_byte (POP_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
return (true);
}
mst_Boolean
compile_to_by_do (tree_node to,
tree_node by,
tree_node block)
{
int jumpAroundOfs, oldJumpAroundLen, jumpAroundLen;
int finalJumpOfs, finalJumpLen;
int index;
bc_vector loopExprCodes, stepCodes = NULL; /* initialize to please
gcc */
if (block->nodeType != TREE_BLOCK_NODE)
return (false);
if (HAS_NOT_ONE_PARAM_OR_TEMPS (block))
return (false);
if (by)
{
if (by->nodeType != TREE_CONST_EXPR
|| by->v_const.constType != CONST_INT)
return (false);
}
index =
_gst_declare_name (block->v_block.arguments->v_list.name, false, true);
_gst_compile_byte (STORE_TEMPORARY_VARIABLE, index);
compile_expression (to);
_gst_compile_byte (DUP_STACK_TOP, index);
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_TEMPORARY_VARIABLE, index);
if (by)
{
bc_vector current_bytecodes;
current_bytecodes = _gst_save_bytecode_array ();
compile_expression (by);
stepCodes = _gst_get_bytecodes ();
_gst_restore_bytecode_array (current_bytecodes);
jumpAroundOfs = _gst_bytecode_length (stepCodes);
}
else
jumpAroundOfs = 2;
loopExprCodes = compile_sub_expression (block);
jumpAroundOfs += _gst_bytecode_length (loopExprCodes) + 10;
for (oldJumpAroundLen = jumpAroundLen = finalJumpLen = 0; ; oldJumpAroundLen = jumpAroundLen)
{
finalJumpOfs = jumpAroundOfs + jumpAroundLen + 2;
finalJumpLen = (finalJumpOfs + finalJumpLen > 65536 ? 6 :
finalJumpOfs + finalJumpLen > 256 ? 4 : 2);
finalJumpLen = (finalJumpOfs + finalJumpLen > 65536 ? 6 :
finalJumpOfs + finalJumpLen > 256 ? 4 : 2);
jumpAroundLen = (jumpAroundOfs + finalJumpLen > 65536 ? 6 :
jumpAroundOfs + finalJumpLen > 256 ? 4 : 2);
if (jumpAroundLen == oldJumpAroundLen)
break;
}
/* skip the loop if there are no more occurrences */
_gst_compile_byte ((!by || by->v_const.val.iVal > 0)
? GREATER_EQUAL_SPECIAL : LESS_EQUAL_SPECIAL, 0);
SUB_STACK_DEPTH (1);
compile_jump (jumpAroundOfs + finalJumpLen, false);
/* we don't care about loop expr's value */
_gst_compile_and_free_bytecodes (loopExprCodes);
_gst_compile_byte (POP_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
_gst_compile_byte (DUP_STACK_TOP, 0);
INCR_STACK_DEPTH ();
_gst_compile_byte (PUSH_TEMPORARY_VARIABLE, index);
INCR_STACK_DEPTH ();
if (by)
_gst_compile_and_free_bytecodes (stepCodes);
else
{
_gst_compile_byte (PUSH_INTEGER, 1);
INCR_STACK_DEPTH ();
}
_gst_compile_byte (PLUS_SPECIAL, 0);
SUB_STACK_DEPTH (1);
_gst_compile_byte (STORE_TEMPORARY_VARIABLE, index);
_gst_compile_byte (JUMP_BACK, finalJumpOfs + finalJumpLen);
/* delete the end from the stack */
_gst_compile_byte (POP_STACK_TOP, 0);
SUB_STACK_DEPTH (1);
_gst_undeclare_name ();
return (true);
}
mst_Boolean
compile_if_true_false_statement (OOP selector,
tree_node expr)
{
bc_vector trueByteCodes, falseByteCodes;
if (expr->v_list.value->nodeType != TREE_BLOCK_NODE
|| expr->v_list.next->v_list.value->nodeType != TREE_BLOCK_NODE)
return (false);
if (HAS_PARAMS_OR_TEMPS (expr->v_list.value)
|| HAS_PARAMS_OR_TEMPS (expr->v_list.next->v_list.value))
return (false);
if (selector == _gst_if_true_if_false_symbol)
{
falseByteCodes =
compile_sub_expression (expr->v_list.next->v_list.value);
trueByteCodes =
compile_sub_expression_and_jump (expr->v_list.value,
_gst_bytecode_length
(falseByteCodes));
}
else
{
falseByteCodes = compile_sub_expression (expr->v_list.value);
trueByteCodes =
compile_sub_expression_and_jump (expr->v_list.next->v_list.
value,
_gst_bytecode_length
(falseByteCodes));
}
compile_jump (_gst_bytecode_length (trueByteCodes), false);
_gst_compile_and_free_bytecodes (trueByteCodes);
_gst_compile_and_free_bytecodes (falseByteCodes);
return (true);
}
mst_Boolean
compile_if_statement (OOP selector,
tree_node expr)
{
bc_vector thenByteCodes;
if (expr->v_list.value->nodeType != TREE_BLOCK_NODE
|| HAS_PARAMS_OR_TEMPS (expr->v_list.value))
return (false);
/* The second parameter (2) is the size of a `push nil' bytecode */
thenByteCodes =
compile_sub_expression_and_jump (expr->v_list.value, 2);
compile_jump (_gst_bytecode_length (thenByteCodes),
selector == _gst_if_false_symbol);
_gst_compile_and_free_bytecodes (thenByteCodes);
/* Compare the code produced here with that produced in #and:/#or:
This produces less efficient bytecodes if the condition is true
(there are two jumps instead of one). Actually, the push will 99%
of the times be followed by a pop stack top, and the optimizer
changes
0: jump to 4
2: push nil
4: pop stack top
to a single pop -- so the code ends up being quite efficent. Note
that instead the result of #and:/#or: will be used (no pop) so we
use the other possible encoding. */
_gst_compile_byte (PUSH_SPECIAL, NIL_INDEX);
return (true);
}
mst_Boolean
compile_and_or_statement (OOP selector,
tree_node expr)
{
bc_vector blockByteCodes;
int blockLen;
if (expr->v_list.value->nodeType != TREE_BLOCK_NODE
|| HAS_PARAMS_OR_TEMPS (expr->v_list.value))
return (false);
blockByteCodes = compile_sub_expression (expr->v_list.value);
blockLen = _gst_bytecode_length (blockByteCodes);
_gst_compile_byte (DUP_STACK_TOP, 0);
compile_jump (blockLen + 2, selector == _gst_or_symbol);
_gst_compile_byte (POP_STACK_TOP, 0);
_gst_compile_and_free_bytecodes (blockByteCodes);
return (true);
}
bc_vector
compile_sub_expression (tree_node expr)
{
mst_Boolean returns;
bc_vector current_bytecodes, subExprByteCodes;
current_bytecodes = _gst_save_bytecode_array ();
returns = compile_statements (expr->v_block.statements, false);
if (returns)
INCR_STACK_DEPTH ();
subExprByteCodes = _gst_get_bytecodes ();
_gst_restore_bytecode_array (current_bytecodes);
return (subExprByteCodes);
}
bc_vector
compile_sub_expression_and_jump (tree_node expr,
int branchLen)
{
bc_vector current_bytecodes, subExprByteCodes;
mst_Boolean returns;
current_bytecodes = _gst_save_bytecode_array ();
returns = compile_statements (expr->v_block.statements, false);
if (returns)
INCR_STACK_DEPTH ();
if (!returns)
_gst_compile_byte (JUMP, branchLen);
subExprByteCodes = _gst_get_bytecodes ();
_gst_restore_bytecode_array (current_bytecodes);
return (subExprByteCodes);
}
void
compile_jump (int len,
mst_Boolean jumpType)
{
if (len <= 0)
{
_gst_errorf ("invalid length jump %d -- internal error\n", len);
EXIT_COMPILATION ();
}
SUB_STACK_DEPTH (1);
_gst_compile_byte (jumpType ? POP_JUMP_TRUE : POP_JUMP_FALSE, len);
}
void
compile_cascaded_message (tree_node cascadedExpr)
{
tree_node message;
dup_message_receiver = true;
compile_expression (cascadedExpr->v_expr.receiver);
for (message = cascadedExpr->v_expr.expression; message;
message = message->v_list.next)
{
_gst_compile_byte (POP_STACK_TOP, 0);
if (message->v_list.next)
_gst_compile_byte (DUP_STACK_TOP, 0);
else
SUB_STACK_DEPTH (1);
compile_expression (message->v_list.value);
/* !!! remember that unary, binary and keywordexpr should ignore
the receiver field if it is nil; that is the case for these
functions and things work out fine if that's the case. */
}
}
void
compile_assignments (tree_node varList)
{
symbol_entry variable;
for (; varList; varList = varList->v_list.next)
{
tree_node varName = varList->v_list.value;
_gst_line_number (varList->location.first_line, 0);
if (!_gst_find_variable (&variable, varName))
{
if (varName->v_list.next)
_gst_errorf_at (varName->location.first_line,
"invalid scope resolution");
else
_gst_errorf_at (varName->location.first_line,
"assignment to undeclared variable %s",
varName->v_list.name);
EXIT_COMPILATION ();
}
if (variable.readOnly)
{
_gst_errorf_at (varName->location.first_line,
"invalid assignment to %s %s",
_gst_get_scope_kind (variable.scope),
varName->v_list.name);
EXIT_COMPILATION ();
}
/* Here we have several kinds of things to store: receiver
variable, temporary variable, global variable (reference by
association). */
if (variable.scope != SCOPE_GLOBAL && varName->v_list.next)
{
_gst_errorf_at (varName->location.first_line,
"invalid scope resolution");
EXIT_COMPILATION ();
}
if (variable.scopeDistance > 0)
_gst_compile_byte (STORE_OUTER_TEMP, (variable.varIndex << 8) | variable.scopeDistance);
else if (variable.scope == SCOPE_TEMPORARY)
_gst_compile_byte (STORE_TEMPORARY_VARIABLE, variable.varIndex);
else if (variable.scope == SCOPE_RECEIVER)
_gst_compile_byte (STORE_RECEIVER_VARIABLE, variable.varIndex);
else
{
/* This can become a message send, which might not return the
value. Compile it in a way that can be easily peephole
optimized. */
_gst_compile_byte (STORE_LIT_VARIABLE, variable.varIndex);
_gst_compile_byte (POP_STACK_TOP, 0);
_gst_compile_byte (PUSH_LIT_VARIABLE, variable.varIndex);
}
}
}
mst_Boolean
is_super (tree_node expr)
{
return (expr->nodeType == TREE_VARIABLE_NODE
&& _gst_intern_string (expr->v_list.name) ==
_gst_super_symbol);
}
mst_Boolean
equal_constant (OOP oop,
tree_node constExpr)
{
tree_node arrayElt;
size_t len, i;
/* ??? this kind of special casing of the elements of arrays bothers
me...it should all be in one neat place. */
if (constExpr->nodeType == TREE_SYMBOL_NODE) /* symbol in array
constant */
return (oop == constExpr->v_expr.selector);
else if (constExpr->nodeType == TREE_ARRAY_ELT_LIST)
{
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_array_class)
{
for (len = 0, arrayElt = constExpr; arrayElt;
len++, arrayElt = arrayElt->v_list.next);
if (len == NUM_OOPS (OOP_TO_OBJ (oop)))
{
for (i = 1, arrayElt = constExpr; i <= len;
i++, arrayElt = arrayElt->v_list.next)
{
if (!equal_constant
(ARRAY_AT (oop, i), arrayElt->v_list.value))
return (false);
}
return (true);
}
}
return (false);
}
switch (constExpr->v_const.constType)
{
case CONST_INT:
if (oop == FROM_INT (constExpr->v_const.val.iVal))
return (true);
break;
case CONST_CHAR:
if (IS_OOP (oop) && is_a_kind_of (OOP_CLASS (oop), _gst_char_class)
&& CHAR_OOP_VALUE (oop) == constExpr->v_const.val.iVal)
return (true);
break;
case CONST_FLOATD:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_floatd_class)
{
double d = (double) constExpr->v_const.val.fVal;
if (!memcmp (&d, &OOP_TO_OBJ (oop)->data, sizeof (double)))
return (true);
}
break;
case CONST_FLOATE:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_floate_class)
{
float f = (float) constExpr->v_const.val.fVal;
if (!memcmp (&f, &OOP_TO_OBJ (oop)->data, sizeof (float)))
return (true);
}
break;
case CONST_FLOATQ:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_floatq_class)
{
long double ld = (long double) constExpr->v_const.val.fVal;
if (!memcmp (&ld, &OOP_TO_OBJ (oop)->data, sizeof (long double)))
return (true);
}
break;
case CONST_STRING:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_string_class)
{
len = strlen (constExpr->v_const.val.sVal);
if (len == _gst_string_oop_len (oop))
{
if (strncmp
((char *) OOP_TO_OBJ (oop)->data,
constExpr->v_const.val.sVal, len) == 0)
return (true);
}
}
break;
case CONST_DEFERRED_BINDING:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_deferred_variable_binding_class)
{
gst_deferred_variable_binding binding =
(gst_deferred_variable_binding) OOP_TO_OBJ (oop);
gst_object path = OOP_TO_OBJ (binding->path);
int i, size = NUM_OOPS (path);
OOP *pKey;
tree_node varNode = constExpr->v_const.val.aVal;
/* Use <= because we test the key first. */
for (i = 0, pKey = &binding->key; i <= size; pKey = &path->data[i++])
{
if (!varNode
|| *pKey != _gst_intern_string (varNode->v_list.name))
return (false);
varNode = varNode->v_list.next;
}
}
break;
case CONST_BINDING:
constExpr = _gst_find_variable_binding (constExpr->v_const.val.aVal);
if (!constExpr)
return (false);
assert (constExpr->v_const.constType != CONST_BINDING);
return equal_constant (oop, constExpr);
case CONST_OOP:
if (oop == constExpr->v_const.val.oopVal)
return (true);
break;
case CONST_ARRAY:
if (IS_OOP (oop) && OOP_CLASS (oop) == _gst_array_class)
{
/* ??? could keep the length in a counter */
for (len = 0, arrayElt = constExpr->v_const.val.aVal;
arrayElt; len++, arrayElt = arrayElt->v_list.next);
if (len == NUM_OOPS (OOP_TO_OBJ (oop)))
{
for (i = 1, arrayElt = constExpr->v_const.val.aVal;
i <= len; i++, arrayElt = arrayElt->v_list.next)
{
if (!equal_constant
(ARRAY_AT (oop, i), arrayElt->v_list.value))
return (false);
}
return (true);
}
}
break;
default:
break;
}
return (false);
}
OOP
_gst_make_constant_oop (tree_node constExpr)
{
tree_node subexpr;
int len, i;
OOP resultOOP, elementOOP;
inc_ptr incPtr;
byte_object bo;
gst_object result;
if (constExpr == NULL)
return (_gst_nil_oop); /* special case empty array literals */
if (constExpr->nodeType == TREE_SYMBOL_NODE) /* symbol in array
constant */
return (constExpr->v_expr.selector);
else if (constExpr->nodeType == TREE_ARRAY_ELT_LIST)
{
for (len = 0, subexpr = constExpr; subexpr;
len++, subexpr = subexpr->v_list.next);
incPtr = INC_SAVE_POINTER ();
/* this might be an uninitialized form of array creation for
speed; but not now -- with the array temporarily part of the
root set it must be completely initialized (sigh). */
instantiate_with (_gst_array_class, len, &resultOOP);
INC_ADD_OOP (resultOOP);
for (i = 0, subexpr = constExpr; i < len;
i++, subexpr = subexpr->v_list.next)
{
elementOOP = _gst_make_constant_oop (subexpr->v_list.value);
result = OOP_TO_OBJ (resultOOP);
result->data[i] = elementOOP;
}
MAKE_OOP_READONLY (resultOOP, true);
INC_RESTORE_POINTER (incPtr);
return (resultOOP);
}
switch (constExpr->v_const.constType)
{
case CONST_INT:
return (FROM_INT (constExpr->v_const.val.iVal));
case CONST_CHAR:
return (char_new (constExpr->v_const.val.iVal));
case CONST_FLOATD:
return (floatd_new (constExpr->v_const.val.fVal));
case CONST_FLOATE:
return (floate_new (constExpr->v_const.val.fVal));
case CONST_FLOATQ:
return (floatq_new (constExpr->v_const.val.fVal));
case CONST_STRING:
resultOOP = _gst_string_new (constExpr->v_const.val.sVal);
MAKE_OOP_READONLY (resultOOP, true);
return (resultOOP);
case CONST_BYTE_OBJECT:
bo = constExpr->v_const.val.boVal;
result = instantiate_with (bo->class, bo->size, &resultOOP);
memcpy (result->data, bo->body, bo->size);
MAKE_OOP_READONLY (resultOOP, true);
return (resultOOP);
case CONST_DEFERRED_BINDING:
{
gst_deferred_variable_binding dvb;
tree_node varNode = constExpr->v_const.val.aVal;
incPtr = INC_SAVE_POINTER ();
dvb = (gst_deferred_variable_binding)
instantiate (_gst_deferred_variable_binding_class, &resultOOP);
INC_ADD_OOP (resultOOP);
dvb->key = _gst_intern_string (varNode->v_list.name);
dvb->class = _gst_this_class;
dvb->defaultDictionary = _gst_get_undeclared_dictionary ();
dvb->association = _gst_nil_oop;
varNode = varNode->v_list.next;
if (varNode)
{
int i, size = list_length (varNode);
OOP arrayOOP;
gst_object array =
instantiate_with (_gst_array_class, size, &arrayOOP);
dvb->path = arrayOOP;
for (i = 0; i < size; i++, varNode = varNode->v_list.next)
array->data[i] = _gst_intern_string (varNode->v_list.name);
}
INC_RESTORE_POINTER (incPtr);
return (resultOOP);
}
case CONST_BINDING:
subexpr = _gst_find_variable_binding (constExpr->v_const.val.aVal);
if (!subexpr)
{
_gst_errorf_at (constExpr->location.first_line,
"invalid variable binding");
EXIT_COMPILATION ();
}
assert (subexpr->v_const.constType != CONST_BINDING);
return _gst_make_constant_oop (subexpr);
case CONST_OOP:
return (constExpr->v_const.val.oopVal);
case CONST_ARRAY:
for (len = 0, subexpr = constExpr->v_const.val.aVal; subexpr;
len++, subexpr = subexpr->v_list.next);
incPtr = INC_SAVE_POINTER ();
result = instantiate_with (_gst_array_class, len, &resultOOP);
INC_ADD_OOP (resultOOP);
for (i = 0, subexpr = constExpr->v_const.val.aVal; i < len;
i++, subexpr = subexpr->v_list.next)
{
elementOOP = _gst_make_constant_oop (subexpr->v_list.value);
result = OOP_TO_OBJ (resultOOP);
result->data[i] = elementOOP;
}
MAKE_OOP_READONLY (resultOOP, true);
INC_RESTORE_POINTER (incPtr);
return (resultOOP);
}
return (_gst_nil_oop);
}
OOP
make_block (int args,
int temps,
bc_vector bytecodes,
int stack_depth)
{
OOP blockOOP;
if (_gst_declare_tracing)
{
printf (" Code for enclosed block:\n");
#ifdef PRINT_BEFORE_OPTIMIZATION
_gst_print_bytecodes (bytecodes, literal_vec);
#endif
}
bytecodes = _gst_optimize_bytecodes (bytecodes);
if (_gst_declare_tracing)
_gst_print_bytecodes (bytecodes, literal_vec);
blockOOP =
_gst_block_new (args, temps, bytecodes, stack_depth, literal_vec);
_gst_free_bytecodes (bytecodes);
return (blockOOP);
}
OOP
make_clean_block_closure (OOP blockOOP)
{
gst_block_closure closure;
OOP closureOOP;
closure = (gst_block_closure) new_instance (_gst_block_closure_class,
&closureOOP);
/* Use the class as the receiver. This is blatantly wrong, but
at least sets the correct trustfulness on the contexts. If the
receiver was nil, for example, untrusted clean blocks evaluated
from a trusted environment would be treated as trusted (because
nil is trusted). */
closure->receiver = _gst_this_class;
closure->outerContext = _gst_nil_oop;
closure->block = blockOOP;
return (closureOOP);
}
OOP
compute_selector (tree_node selectorExpr)
{
if (selectorExpr->nodeType == TREE_UNARY_EXPR
|| selectorExpr->nodeType == TREE_BINARY_EXPR)
return (selectorExpr->v_expr.selector);
else
return (_gst_compute_keyword_selector (selectorExpr->v_expr.expression));
}
OOP
_gst_compute_keyword_selector (tree_node keywordList)
{
tree_node keyword;
int len;
char *nameBuf, *p;
len = 0;
for (keyword = keywordList; keyword != NULL;
keyword = keyword->v_list.next)
len += strlen (keyword->v_list.name);
p = nameBuf = (char *) alloca (len + 1);
for (keyword = keywordList; keyword != NULL;
keyword = keyword->v_list.next)
{
len = strlen (keyword->v_list.name);
strcpy (p, keyword->v_list.name);
p += len;
}
*p = '\0';
return (_gst_intern_string (nameBuf));
}
OOP
_gst_make_attribute (tree_node attribute_keywords)
{
tree_node keyword;
OOP selectorOOP, argsArrayOOP, messageOOP;
gst_object argsArray;
int i, numArgs;
inc_ptr incPtr;
incPtr = INC_SAVE_POINTER ();
if (_gst_had_error)
return _gst_nil_oop;
selectorOOP = _gst_compute_keyword_selector (attribute_keywords);
numArgs = list_length (attribute_keywords);
argsArray = instantiate_with (_gst_array_class, numArgs, &argsArrayOOP);
INC_ADD_OOP (argsArrayOOP);
for (i = 0, keyword = attribute_keywords; keyword != NULL;
i++, keyword = keyword->v_list.next)
{
tree_node value = keyword->v_list.value;
OOP result;
if (value->nodeType != TREE_CONST_EXPR)
{
result = _gst_execute_statements (NULL, value, UNDECLARED_NONE, true);
if (!result)
{
_gst_had_error = true;
return _gst_nil_oop;
}
}
else
result = _gst_make_constant_oop (value);
argsArray = OOP_TO_OBJ (argsArrayOOP);
argsArray->data[i] = result;
}
messageOOP = _gst_message_new_args (selectorOOP, argsArrayOOP);
INC_RESTORE_POINTER (incPtr);
MAKE_OOP_READONLY (argsArrayOOP, true);
MAKE_OOP_READONLY (messageOOP, true);
return (messageOOP);
}
int
process_attributes_tree (tree_node attribute_list)
{
int primitiveIndex = 0;
for (; attribute_list; attribute_list = attribute_list->v_list.next)
{
tree_node value = attribute_list->v_list.value;
OOP messageOOP = value->v_const.val.oopVal;
int result = process_attribute (messageOOP);
if (result < 0)
{
EXIT_COMPILATION ();
}
if (result > 0)
{
if (IS_OOP_UNTRUSTED (_gst_this_class))
{
_gst_errorf ("an untrusted class cannot declare primitives");
EXIT_COMPILATION ();
}
if (primitiveIndex > 0)
{
_gst_errorf ("duplicate primitive declaration");
EXIT_COMPILATION ();
}
primitiveIndex = result;
}
}
return primitiveIndex;
}
int
_gst_process_attributes_array (OOP arrayOOP)
{
int primitiveIndex = 0;
int n = NUM_WORDS (OOP_TO_OBJ (arrayOOP));
int i;
if (IS_NIL (arrayOOP))
return 0;
for (i = 0; i < n; i++)
{
OOP messageOOP = OOP_TO_OBJ (arrayOOP)->data[i];
int result = process_attribute (messageOOP);
if (result < 0)
return (-1);
if (result > 0)
{
if (primitiveIndex > 0)
return (-1);
primitiveIndex = result;
}
}
return primitiveIndex;
}
int
process_attribute (OOP messageOOP)
{
gst_message message = (gst_message) OOP_TO_OBJ (messageOOP);
OOP selectorOOP = message->selector;
OOP argumentsOOP = message->args;
gst_object arguments = OOP_TO_OBJ (argumentsOOP);
if (selectorOOP == _gst_primitive_symbol)
{
if (IS_INT (arguments->data[0]))
{
int primitiveIndex = TO_INT (arguments->data[0]);
if (primitiveIndex <= 0 || primitiveIndex >= NUM_PRIMITIVES)
{
_gst_errorf ("primitive number out of range");
return (-1);
}
return (primitiveIndex);
}
else
{
_gst_errorf ("bad primitive number, expected SmallInteger");
return (-1);
}
}
else if (selectorOOP == _gst_category_symbol)
{
this_method_category = arguments->data[0];
return (0);
}
else
{
method_attributes *new_attr = (method_attributes *)
xmalloc (sizeof (method_attributes));
new_attr->count = method_attrs ? method_attrs->count + 1 : 0;
new_attr->oop = messageOOP;
new_attr->next = method_attrs;
method_attrs = new_attr;
_gst_register_oop (messageOOP);
return (0);
}
}
void
realloc_literal_vec (void)
{
int size;
ptrdiff_t delta;
size = literal_vec_max - literal_vec + LITERAL_VEC_CHUNK_SIZE;
delta = ((OOP *) xrealloc (literal_vec, size * sizeof (OOP))) - literal_vec;
literal_vec += delta;
literal_vec_curr += delta;
literal_vec_max = literal_vec + size;
}
int
list_length (tree_node listExpr)
{
tree_node l;
long len;
for (len = 0, l = listExpr; l; l = l->v_list.next, len++);
if (sizeof (int) < sizeof (long) && len > INT_MAX)
{
_gst_errorf ("list too long, %ld", len);
len = INT_MAX;
}
return ((int) len);
}
/***********************************************************************
*
* Literal Vector manipulation routines.
*
***********************************************************************/
int
add_literal (OOP oop)
{
if (literal_vec_curr >= literal_vec_max)
realloc_literal_vec ();
*literal_vec_curr++ = oop;
return (literal_vec_curr - literal_vec - 1);
}
int
_gst_add_forced_object (OOP oop)
{
OOP *lit;
for (lit = literal_vec; lit < literal_vec_curr; lit++)
if (*lit == oop)
return (lit - literal_vec);
return (add_literal (oop));
}
OOP
get_literals_array (void)
{
OOP methodLiteralsOOP;
gst_object methodLiterals;
assert (literal_vec_curr > literal_vec);
methodLiterals = new_instance_with (_gst_array_class,
literal_vec_curr - literal_vec,
&methodLiteralsOOP);
memcpy (methodLiterals->data, literal_vec,
(literal_vec_curr - literal_vec) * sizeof(OOP));
literal_vec_curr = literal_vec;
MAKE_OOP_READONLY (methodLiteralsOOP, true);
return (methodLiteralsOOP);
}
void
install_method (OOP methodOOP)
{
OOP oldMethod, selector, methodDictionaryOOP;
gst_compiled_method method;
gst_method_info descriptor;
int num_attrs, i;
method = (gst_compiled_method) OOP_TO_OBJ (methodOOP);
descriptor = (gst_method_info) OOP_TO_OBJ (method->descriptor);
num_attrs = NUM_INDEXABLE_FIELDS (method->descriptor);
for (i = 0; i < num_attrs; i++)
{
char *result;
OOP attributeOOP = descriptor->attributes[i];
gst_message attribute = (gst_message) OOP_TO_OBJ (attributeOOP);
OOP handlerBlockOOP = _gst_find_pragma_handler (_gst_this_class,
attribute->selector);
if (!IS_NIL (handlerBlockOOP))
{
_gst_msg_sendf (&result, "%s %o value: %o value: %o",
handlerBlockOOP, methodOOP, attributeOOP);
if (result != NULL)
{
_gst_errorf ("%s", result);
EXIT_COMPILATION ();
}
}
method = (gst_compiled_method) OOP_TO_OBJ (methodOOP);
descriptor = (gst_method_info) OOP_TO_OBJ (method->descriptor);
if (num_attrs != NUM_INDEXABLE_FIELDS (method->descriptor))
{
_gst_errorf ("cannot modify method descriptor in pragma handler");
EXIT_COMPILATION ();
}
}
selector = descriptor->selector;
/* methodDictionaryOOP is held onto by the class, which is already
reachable by the root set so we don't need to hold onto it
here. */
methodDictionaryOOP =
_gst_valid_class_method_dictionary (_gst_this_class);
if (_gst_untrusted_methods)
{
oldMethod = _gst_identity_dictionary_at (methodDictionaryOOP,
selector);
if (!IS_NIL (oldMethod) && !IS_OOP_UNTRUSTED (oldMethod))
{
_gst_errorf ("cannot redefine a trusted method as untrusted");
EXIT_COMPILATION ();
}
}
MAKE_OOP_READONLY (methodOOP, true);
oldMethod = _gst_identity_dictionary_at_put (methodDictionaryOOP,
selector, methodOOP);
#ifdef ENABLE_JIT_TRANSLATION
if (oldMethod != _gst_nil_oop)
_gst_discard_native_code (oldMethod);
#endif
#ifdef VERIFY_COMPILED_METHODS
_gst_verify_sent_method (methodOOP);
#endif
_gst_invalidate_method_cache ();
}
OOP
_gst_make_new_method (int primitiveIndex,
int numArgs,
int numTemps,
int maximumStackDepth,
OOP literals,
bc_vector bytecodes,
OOP class,
OOP selector,
OOP defaultCategoryOOP,
int64_t startPos,
int64_t endPos)
{
method_header header;
int newFlags;
OOP method, methodDesc, sourceCode, category;
inc_ptr incPtr;
maximumStackDepth += numArgs + numTemps;
memset (&header, 0, sizeof (method_header));
incPtr = INC_SAVE_POINTER ();
if (primitiveIndex)
{
if (_gst_declare_tracing)
printf (" Primitive Index %d\n", primitiveIndex);
header.headerFlag = MTH_PRIMITIVE;
}
else if (method_attrs)
header.headerFlag = MTH_ANNOTATED;
/* if returning a literal, we must either use comp.c's literal pool
(IS_NIL (LITERALS)), get it from a preexisting literal pool
(LITERAL_VEC_CURR == LITERAL_VEC), or put it into an empty
pool (NUM_WORDS (...) == 0). */
else if (numArgs == 0
&& numTemps == 0
&& (newFlags = _gst_is_simple_return (bytecodes)) != 0
&& (newFlags != MTH_RETURN_LITERAL
|| IS_NIL (literals)
|| NUM_WORDS (OOP_TO_OBJ (literals)) == 0
|| literal_vec_curr == literal_vec))
{
header.headerFlag = newFlags & 0xFF;
/* if returning an instance variable, its index is indicated in
the primitive index */
primitiveIndex = newFlags >> 8;
numTemps = 0;
_gst_free_bytecodes (bytecodes);
bytecodes = NULL;
/* If returning a literal but we have none, it was added with
_gst_add_forced_object. */
}
else
header.headerFlag = MTH_NORMAL;
if (literal_vec_curr > literal_vec)
{
literals = get_literals_array ();
literal_vec_curr = literal_vec;
INC_ADD_OOP (literals);
}
if (bytecodes)
{
#ifdef PRINT_BEFORE_OPTIMIZATION
if (_gst_declare_tracing)
_gst_print_bytecodes (bytecodes, OOP_TO_OBJ (literals)->data);
#endif
bytecodes = _gst_optimize_bytecodes (bytecodes);
}
if (_gst_declare_tracing)
printf (" Allocated stack slots %d\n", maximumStackDepth);
if (_gst_declare_tracing)
_gst_print_bytecodes (bytecodes, OOP_TO_OBJ (literals)->data);
maximumStackDepth += (1 << DEPTH_SCALE) - 1; /* round */
maximumStackDepth >>= DEPTH_SCALE;
maximumStackDepth++; /* just to be sure */
header.stack_depth = maximumStackDepth;
header.primitiveIndex = primitiveIndex;
header.numArgs = numArgs;
header.numTemps = numTemps;
header.intMark = 1;
if (this_method_category)
{
category = this_method_category;
this_method_category = NULL;
}
else
category = defaultCategoryOOP;
if (IS_NIL (class))
sourceCode = _gst_nil_oop;
else
{
sourceCode = _gst_get_source_string (startPos, endPos);
INC_ADD_OOP (sourceCode);
}
methodDesc = method_info_new (class, selector, method_attrs,
sourceCode, category);
INC_ADD_OOP (methodDesc);
method = method_new (header, literals, bytecodes, class, methodDesc);
INC_RESTORE_POINTER (incPtr);
return (method);
}
OOP
method_new (method_header header,
OOP literals,
bc_vector bytecodes,
OOP class,
OOP methodDesc)
{
int numByteCodes;
gst_compiled_method method;
OOP methodOOP;
gst_object lit;
int i;
if (bytecodes != NULL)
numByteCodes = _gst_bytecode_length (bytecodes);
else
numByteCodes = 0;
method_attrs = NULL;
method = (gst_compiled_method) instantiate_with (_gst_compiled_method_class,
numByteCodes, &methodOOP);
MAKE_OOP_UNTRUSTED (methodOOP,
_gst_untrusted_methods
|| IS_OOP_UNTRUSTED (_gst_this_context_oop)
|| IS_OOP_UNTRUSTED (class));
method->header = header;
method->descriptor = methodDesc;
method->literals = literals;
for (lit = OOP_TO_OBJ (literals), i = NUM_OOPS (lit); i--;)
{
OOP blockOOP;
gst_compiled_block block;
if (IS_CLASS (lit->data[i], _gst_block_closure_class))
{
gst_block_closure bc;
bc = (gst_block_closure) OOP_TO_OBJ (lit->data[i]);
blockOOP = bc->block;
}
else if (IS_CLASS (lit->data[i], _gst_compiled_block_class))
blockOOP = lit->data[i];
else
continue;
block = (gst_compiled_block) OOP_TO_OBJ (blockOOP);
if (IS_NIL (block->method))
{
MAKE_OOP_UNTRUSTED (blockOOP, IS_OOP_UNTRUSTED (methodOOP));
block->method = methodOOP;
block->literals = literals;
}
}
if (bytecodes != NULL)
{
_gst_copy_bytecodes (method->bytecodes, bytecodes);
_gst_free_bytecodes (bytecodes);
}
return (methodOOP);
}
OOP
_gst_block_new (int numArgs,
int numTemps,
bc_vector bytecodes,
int maximumStackDepth,
OOP * literals)
{
int numByteCodes;
OOP blockOOP;
gst_compiled_block block;
block_header header;
maximumStackDepth += numArgs + numTemps;
maximumStackDepth += (1 << DEPTH_SCALE) - 1; /* round */
maximumStackDepth >>= DEPTH_SCALE;
maximumStackDepth++; /* just to be sure */
numByteCodes = _gst_bytecode_length (bytecodes);
memset (&header, 0, sizeof (header));
header.numArgs = numArgs;
header.numTemps = numTemps;
header.depth = maximumStackDepth;
header.intMark = 1;
header.clean = _gst_check_kind_of_block (bytecodes, literals);
block = (gst_compiled_block) instantiate_with (_gst_compiled_block_class,
numByteCodes, &blockOOP);
block->header = header;
block->method = block->literals = _gst_nil_oop;
_gst_copy_bytecodes (block->bytecodes, bytecodes);
MAKE_OOP_READONLY (blockOOP, true);
return (blockOOP);
}
OOP
method_info_new (OOP class,
OOP selector,
method_attributes *attrs,
OOP sourceCode,
OOP categoryOOP)
{
method_attributes *next;
gst_method_info methodInfo;
OOP methodInfoOOP;
methodInfo =
(gst_method_info) new_instance_with (_gst_method_info_class,
attrs ? attrs->count + 1 : 0,
&methodInfoOOP);
methodInfo->sourceCode = sourceCode;
methodInfo->category = categoryOOP;
methodInfo->class = class;
methodInfo->selector = selector;
while (attrs)
{
methodInfo->attributes[attrs->count] = attrs->oop;
next = attrs->next;
_gst_unregister_oop (attrs->oop);
free (attrs);
attrs = next;
}
return (methodInfoOOP);
}
|